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/seq_file.h>
34 #include <linux/writeback.h>
36 #include <linux/stacktrace.h>
37 #include <linux/ring_buffer.h>
38 #include <linux/irqflags.h>
42 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
44 unsigned long __read_mostly tracing_max_latency
= (cycle_t
)ULONG_MAX
;
45 unsigned long __read_mostly tracing_thresh
;
48 * We need to change this state when a selftest is running.
49 * A selftest will lurk into the ring-buffer to count the
50 * entries inserted during the selftest although some concurrent
51 * insertions into the ring-buffer such as ftrace_printk could occurred
52 * at the same time, giving false positive or negative results.
54 static bool __read_mostly tracing_selftest_running
;
56 /* For tracers that don't implement custom flags */
57 static struct tracer_opt dummy_tracer_opt
[] = {
61 static struct tracer_flags dummy_tracer_flags
= {
63 .opts
= dummy_tracer_opt
66 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
72 * Kill all tracing for good (never come back).
73 * It is initialized to 1 but will turn to zero if the initialization
74 * of the tracer is successful. But that is the only place that sets
77 int tracing_disabled
= 1;
79 static DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
81 static inline void ftrace_disable_cpu(void)
84 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
87 static inline void ftrace_enable_cpu(void)
89 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
93 static cpumask_t __read_mostly tracing_buffer_mask
;
95 #define for_each_tracing_cpu(cpu) \
96 for_each_cpu_mask(cpu, tracing_buffer_mask)
99 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
101 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
102 * is set, then ftrace_dump is called. This will output the contents
103 * of the ftrace buffers to the console. This is very useful for
104 * capturing traces that lead to crashes and outputing it to a
107 * It is default off, but you can enable it with either specifying
108 * "ftrace_dump_on_oops" in the kernel command line, or setting
109 * /proc/sys/kernel/ftrace_dump_on_oops to true.
111 int ftrace_dump_on_oops
;
113 static int tracing_set_tracer(char *buf
);
115 static int __init
set_ftrace(char *str
)
117 tracing_set_tracer(str
);
120 __setup("ftrace", set_ftrace
);
122 static int __init
set_ftrace_dump_on_oops(char *str
)
124 ftrace_dump_on_oops
= 1;
127 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
130 ns2usecs(cycle_t nsec
)
137 cycle_t
ftrace_now(int cpu
)
139 u64 ts
= ring_buffer_time_stamp(cpu
);
140 ring_buffer_normalize_time_stamp(cpu
, &ts
);
145 * The global_trace is the descriptor that holds the tracing
146 * buffers for the live tracing. For each CPU, it contains
147 * a link list of pages that will store trace entries. The
148 * page descriptor of the pages in the memory is used to hold
149 * the link list by linking the lru item in the page descriptor
150 * to each of the pages in the buffer per CPU.
152 * For each active CPU there is a data field that holds the
153 * pages for the buffer for that CPU. Each CPU has the same number
154 * of pages allocated for its buffer.
156 static struct trace_array global_trace
;
158 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
161 * The max_tr is used to snapshot the global_trace when a maximum
162 * latency is reached. Some tracers will use this to store a maximum
163 * trace while it continues examining live traces.
165 * The buffers for the max_tr are set up the same as the global_trace.
166 * When a snapshot is taken, the link list of the max_tr is swapped
167 * with the link list of the global_trace and the buffers are reset for
168 * the global_trace so the tracing can continue.
170 static struct trace_array max_tr
;
172 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
174 /* tracer_enabled is used to toggle activation of a tracer */
175 static int tracer_enabled
= 1;
178 * tracing_is_enabled - return tracer_enabled status
180 * This function is used by other tracers to know the status
181 * of the tracer_enabled flag. Tracers may use this function
182 * to know if it should enable their features when starting
183 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
185 int tracing_is_enabled(void)
187 return tracer_enabled
;
190 /* function tracing enabled */
191 int ftrace_function_enabled
;
194 * trace_buf_size is the size in bytes that is allocated
195 * for a buffer. Note, the number of bytes is always rounded
198 * This number is purposely set to a low number of 16384.
199 * If the dump on oops happens, it will be much appreciated
200 * to not have to wait for all that output. Anyway this can be
201 * boot time and run time configurable.
203 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
205 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
207 /* trace_types holds a link list of available tracers. */
208 static struct tracer
*trace_types __read_mostly
;
210 /* current_trace points to the tracer that is currently active */
211 static struct tracer
*current_trace __read_mostly
;
214 * max_tracer_type_len is used to simplify the allocating of
215 * buffers to read userspace tracer names. We keep track of
216 * the longest tracer name registered.
218 static int max_tracer_type_len
;
221 * trace_types_lock is used to protect the trace_types list.
222 * This lock is also used to keep user access serialized.
223 * Accesses from userspace will grab this lock while userspace
224 * activities happen inside the kernel.
226 static DEFINE_MUTEX(trace_types_lock
);
228 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
229 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
231 /* trace_flags holds trace_options default values */
232 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
236 * trace_wake_up - wake up tasks waiting for trace input
238 * Simply wakes up any task that is blocked on the trace_wait
239 * queue. These is used with trace_poll for tasks polling the trace.
241 void trace_wake_up(void)
244 * The runqueue_is_locked() can fail, but this is the best we
247 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
248 wake_up(&trace_wait
);
251 static int __init
set_buf_size(char *str
)
253 unsigned long buf_size
;
258 ret
= strict_strtoul(str
, 0, &buf_size
);
259 /* nr_entries can not be zero */
260 if (ret
< 0 || buf_size
== 0)
262 trace_buf_size
= buf_size
;
265 __setup("trace_buf_size=", set_buf_size
);
267 unsigned long nsecs_to_usecs(unsigned long nsecs
)
272 /* These must match the bit postions in trace_iterator_flags */
273 static const char *trace_options
[] = {
295 * ftrace_max_lock is used to protect the swapping of buffers
296 * when taking a max snapshot. The buffers themselves are
297 * protected by per_cpu spinlocks. But the action of the swap
298 * needs its own lock.
300 * This is defined as a raw_spinlock_t in order to help
301 * with performance when lockdep debugging is enabled.
303 static raw_spinlock_t ftrace_max_lock
=
304 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
307 * Copy the new maximum trace into the separate maximum-trace
308 * structure. (this way the maximum trace is permanently saved,
309 * for later retrieval via /debugfs/tracing/latency_trace)
312 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
314 struct trace_array_cpu
*data
= tr
->data
[cpu
];
317 max_tr
.time_start
= data
->preempt_timestamp
;
319 data
= max_tr
.data
[cpu
];
320 data
->saved_latency
= tracing_max_latency
;
322 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
323 data
->pid
= tsk
->pid
;
324 data
->uid
= task_uid(tsk
);
325 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
326 data
->policy
= tsk
->policy
;
327 data
->rt_priority
= tsk
->rt_priority
;
329 /* record this tasks comm */
330 tracing_record_cmdline(current
);
334 * trace_seq_printf - sequence printing of trace information
335 * @s: trace sequence descriptor
336 * @fmt: printf format string
338 * The tracer may use either sequence operations or its own
339 * copy to user routines. To simplify formating of a trace
340 * trace_seq_printf is used to store strings into a special
341 * buffer (@s). Then the output may be either used by
342 * the sequencer or pulled into another buffer.
345 trace_seq_printf(struct trace_seq
*s
, const char *fmt
, ...)
347 int len
= (PAGE_SIZE
- 1) - s
->len
;
355 ret
= vsnprintf(s
->buffer
+ s
->len
, len
, fmt
, ap
);
358 /* If we can't write it all, don't bother writing anything */
368 * trace_seq_puts - trace sequence printing of simple string
369 * @s: trace sequence descriptor
370 * @str: simple string to record
372 * The tracer may use either the sequence operations or its own
373 * copy to user routines. This function records a simple string
374 * into a special buffer (@s) for later retrieval by a sequencer
375 * or other mechanism.
378 trace_seq_puts(struct trace_seq
*s
, const char *str
)
380 int len
= strlen(str
);
382 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
385 memcpy(s
->buffer
+ s
->len
, str
, len
);
392 trace_seq_putc(struct trace_seq
*s
, unsigned char c
)
394 if (s
->len
>= (PAGE_SIZE
- 1))
397 s
->buffer
[s
->len
++] = c
;
403 trace_seq_putmem(struct trace_seq
*s
, void *mem
, size_t len
)
405 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
408 memcpy(s
->buffer
+ s
->len
, mem
, len
);
414 #define MAX_MEMHEX_BYTES 8
415 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
418 trace_seq_putmem_hex(struct trace_seq
*s
, void *mem
, size_t len
)
420 unsigned char hex
[HEX_CHARS
];
421 unsigned char *data
= mem
;
425 for (i
= 0, j
= 0; i
< len
; i
++) {
427 for (i
= len
-1, j
= 0; i
>= 0; i
--) {
429 hex
[j
++] = hex_asc_hi(data
[i
]);
430 hex
[j
++] = hex_asc_lo(data
[i
]);
434 return trace_seq_putmem(s
, hex
, j
);
438 trace_seq_path(struct trace_seq
*s
, struct path
*path
)
442 if (s
->len
>= (PAGE_SIZE
- 1))
444 p
= d_path(path
, s
->buffer
+ s
->len
, PAGE_SIZE
- s
->len
);
446 p
= mangle_path(s
->buffer
+ s
->len
, p
, "\n");
448 s
->len
= p
- s
->buffer
;
452 s
->buffer
[s
->len
++] = '?';
460 trace_seq_reset(struct trace_seq
*s
)
466 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
471 if (s
->len
<= s
->readpos
)
474 len
= s
->len
- s
->readpos
;
477 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
486 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
488 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
491 seq_puts(m
, s
->buffer
);
497 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
499 * @tsk: the task with the latency
500 * @cpu: The cpu that initiated the trace.
502 * Flip the buffers between the @tr and the max_tr and record information
503 * about which task was the cause of this latency.
506 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
508 struct ring_buffer
*buf
= tr
->buffer
;
510 WARN_ON_ONCE(!irqs_disabled());
511 __raw_spin_lock(&ftrace_max_lock
);
513 tr
->buffer
= max_tr
.buffer
;
516 ftrace_disable_cpu();
517 ring_buffer_reset(tr
->buffer
);
520 __update_max_tr(tr
, tsk
, cpu
);
521 __raw_spin_unlock(&ftrace_max_lock
);
525 * update_max_tr_single - only copy one trace over, and reset the rest
527 * @tsk - task with the latency
528 * @cpu - the cpu of the buffer to copy.
530 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
533 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
537 WARN_ON_ONCE(!irqs_disabled());
538 __raw_spin_lock(&ftrace_max_lock
);
540 ftrace_disable_cpu();
542 ring_buffer_reset(max_tr
.buffer
);
543 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
549 __update_max_tr(tr
, tsk
, cpu
);
550 __raw_spin_unlock(&ftrace_max_lock
);
554 * register_tracer - register a tracer with the ftrace system.
555 * @type - the plugin for the tracer
557 * Register a new plugin tracer.
559 int register_tracer(struct tracer
*type
)
566 pr_info("Tracer must have a name\n");
571 * When this gets called we hold the BKL which means that
572 * preemption is disabled. Various trace selftests however
573 * need to disable and enable preemption for successful tests.
574 * So we drop the BKL here and grab it after the tests again.
577 mutex_lock(&trace_types_lock
);
579 tracing_selftest_running
= true;
581 for (t
= trace_types
; t
; t
= t
->next
) {
582 if (strcmp(type
->name
, t
->name
) == 0) {
584 pr_info("Trace %s already registered\n",
592 type
->set_flag
= &dummy_set_flag
;
594 type
->flags
= &dummy_tracer_flags
;
596 if (!type
->flags
->opts
)
597 type
->flags
->opts
= dummy_tracer_opt
;
599 #ifdef CONFIG_FTRACE_STARTUP_TEST
600 if (type
->selftest
) {
601 struct tracer
*saved_tracer
= current_trace
;
602 struct trace_array
*tr
= &global_trace
;
606 * Run a selftest on this tracer.
607 * Here we reset the trace buffer, and set the current
608 * tracer to be this tracer. The tracer can then run some
609 * internal tracing to verify that everything is in order.
610 * If we fail, we do not register this tracer.
612 for_each_tracing_cpu(i
)
613 tracing_reset(tr
, i
);
615 current_trace
= type
;
616 /* the test is responsible for initializing and enabling */
617 pr_info("Testing tracer %s: ", type
->name
);
618 ret
= type
->selftest(type
, tr
);
619 /* the test is responsible for resetting too */
620 current_trace
= saved_tracer
;
622 printk(KERN_CONT
"FAILED!\n");
625 /* Only reset on passing, to avoid touching corrupted buffers */
626 for_each_tracing_cpu(i
)
627 tracing_reset(tr
, i
);
629 printk(KERN_CONT
"PASSED\n");
633 type
->next
= trace_types
;
635 len
= strlen(type
->name
);
636 if (len
> max_tracer_type_len
)
637 max_tracer_type_len
= len
;
640 tracing_selftest_running
= false;
641 mutex_unlock(&trace_types_lock
);
647 void unregister_tracer(struct tracer
*type
)
652 mutex_lock(&trace_types_lock
);
653 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
657 pr_info("Trace %s not registered\n", type
->name
);
662 if (strlen(type
->name
) != max_tracer_type_len
)
665 max_tracer_type_len
= 0;
666 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
667 len
= strlen((*t
)->name
);
668 if (len
> max_tracer_type_len
)
669 max_tracer_type_len
= len
;
672 mutex_unlock(&trace_types_lock
);
675 void tracing_reset(struct trace_array
*tr
, int cpu
)
677 ftrace_disable_cpu();
678 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
682 void tracing_reset_online_cpus(struct trace_array
*tr
)
686 tr
->time_start
= ftrace_now(tr
->cpu
);
688 for_each_online_cpu(cpu
)
689 tracing_reset(tr
, cpu
);
692 #define SAVED_CMDLINES 128
693 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
694 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
695 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
696 static int cmdline_idx
;
697 static DEFINE_SPINLOCK(trace_cmdline_lock
);
699 /* temporary disable recording */
700 atomic_t trace_record_cmdline_disabled __read_mostly
;
702 static void trace_init_cmdlines(void)
704 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
705 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
709 static int trace_stop_count
;
710 static DEFINE_SPINLOCK(tracing_start_lock
);
713 * ftrace_off_permanent - disable all ftrace code permanently
715 * This should only be called when a serious anomally has
716 * been detected. This will turn off the function tracing,
717 * ring buffers, and other tracing utilites. It takes no
718 * locks and can be called from any context.
720 void ftrace_off_permanent(void)
722 tracing_disabled
= 1;
724 tracing_off_permanent();
728 * tracing_start - quick start of the tracer
730 * If tracing is enabled but was stopped by tracing_stop,
731 * this will start the tracer back up.
733 void tracing_start(void)
735 struct ring_buffer
*buffer
;
738 if (tracing_disabled
)
741 spin_lock_irqsave(&tracing_start_lock
, flags
);
742 if (--trace_stop_count
)
745 if (trace_stop_count
< 0) {
746 /* Someone screwed up their debugging */
748 trace_stop_count
= 0;
753 buffer
= global_trace
.buffer
;
755 ring_buffer_record_enable(buffer
);
757 buffer
= max_tr
.buffer
;
759 ring_buffer_record_enable(buffer
);
763 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
767 * tracing_stop - quick stop of the tracer
769 * Light weight way to stop tracing. Use in conjunction with
772 void tracing_stop(void)
774 struct ring_buffer
*buffer
;
778 spin_lock_irqsave(&tracing_start_lock
, flags
);
779 if (trace_stop_count
++)
782 buffer
= global_trace
.buffer
;
784 ring_buffer_record_disable(buffer
);
786 buffer
= max_tr
.buffer
;
788 ring_buffer_record_disable(buffer
);
791 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
794 void trace_stop_cmdline_recording(void);
796 static void trace_save_cmdline(struct task_struct
*tsk
)
801 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
805 * It's not the end of the world if we don't get
806 * the lock, but we also don't want to spin
807 * nor do we want to disable interrupts,
808 * so if we miss here, then better luck next time.
810 if (!spin_trylock(&trace_cmdline_lock
))
813 idx
= map_pid_to_cmdline
[tsk
->pid
];
814 if (idx
>= SAVED_CMDLINES
) {
815 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
817 map
= map_cmdline_to_pid
[idx
];
818 if (map
<= PID_MAX_DEFAULT
)
819 map_pid_to_cmdline
[map
] = (unsigned)-1;
821 map_pid_to_cmdline
[tsk
->pid
] = idx
;
826 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
828 spin_unlock(&trace_cmdline_lock
);
831 char *trace_find_cmdline(int pid
)
833 char *cmdline
= "<...>";
839 if (pid
> PID_MAX_DEFAULT
)
842 map
= map_pid_to_cmdline
[pid
];
843 if (map
>= SAVED_CMDLINES
)
846 cmdline
= saved_cmdlines
[map
];
852 void tracing_record_cmdline(struct task_struct
*tsk
)
854 if (atomic_read(&trace_record_cmdline_disabled
))
857 trace_save_cmdline(tsk
);
861 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
864 struct task_struct
*tsk
= current
;
866 entry
->preempt_count
= pc
& 0xff;
867 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
868 entry
->tgid
= (tsk
) ? tsk
->tgid
: 0;
870 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
871 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
873 TRACE_FLAG_IRQS_NOSUPPORT
|
875 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
876 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
877 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
881 trace_function(struct trace_array
*tr
, struct trace_array_cpu
*data
,
882 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
885 struct ring_buffer_event
*event
;
886 struct ftrace_entry
*entry
;
887 unsigned long irq_flags
;
889 /* If we are reading the ring buffer, don't trace */
890 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
893 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
897 entry
= ring_buffer_event_data(event
);
898 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
899 entry
->ent
.type
= TRACE_FN
;
901 entry
->parent_ip
= parent_ip
;
902 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
905 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
906 static void __trace_graph_entry(struct trace_array
*tr
,
907 struct trace_array_cpu
*data
,
908 struct ftrace_graph_ent
*trace
,
912 struct ring_buffer_event
*event
;
913 struct ftrace_graph_ent_entry
*entry
;
914 unsigned long irq_flags
;
916 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
919 event
= ring_buffer_lock_reserve(global_trace
.buffer
, sizeof(*entry
),
923 entry
= ring_buffer_event_data(event
);
924 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
925 entry
->ent
.type
= TRACE_GRAPH_ENT
;
926 entry
->graph_ent
= *trace
;
927 ring_buffer_unlock_commit(global_trace
.buffer
, event
, irq_flags
);
930 static void __trace_graph_return(struct trace_array
*tr
,
931 struct trace_array_cpu
*data
,
932 struct ftrace_graph_ret
*trace
,
936 struct ring_buffer_event
*event
;
937 struct ftrace_graph_ret_entry
*entry
;
938 unsigned long irq_flags
;
940 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
943 event
= ring_buffer_lock_reserve(global_trace
.buffer
, sizeof(*entry
),
947 entry
= ring_buffer_event_data(event
);
948 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
949 entry
->ent
.type
= TRACE_GRAPH_RET
;
951 ring_buffer_unlock_commit(global_trace
.buffer
, event
, irq_flags
);
956 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
957 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
960 if (likely(!atomic_read(&data
->disabled
)))
961 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
964 static void ftrace_trace_stack(struct trace_array
*tr
,
965 struct trace_array_cpu
*data
,
969 #ifdef CONFIG_STACKTRACE
970 struct ring_buffer_event
*event
;
971 struct stack_entry
*entry
;
972 struct stack_trace trace
;
973 unsigned long irq_flags
;
975 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
978 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
982 entry
= ring_buffer_event_data(event
);
983 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
984 entry
->ent
.type
= TRACE_STACK
;
986 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
988 trace
.nr_entries
= 0;
989 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
991 trace
.entries
= entry
->caller
;
993 save_stack_trace(&trace
);
994 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
998 void __trace_stack(struct trace_array
*tr
,
999 struct trace_array_cpu
*data
,
1000 unsigned long flags
,
1003 ftrace_trace_stack(tr
, data
, flags
, skip
, preempt_count());
1006 static void ftrace_trace_userstack(struct trace_array
*tr
,
1007 struct trace_array_cpu
*data
,
1008 unsigned long flags
, int pc
)
1010 #ifdef CONFIG_STACKTRACE
1011 struct ring_buffer_event
*event
;
1012 struct userstack_entry
*entry
;
1013 struct stack_trace trace
;
1014 unsigned long irq_flags
;
1016 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1019 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
1023 entry
= ring_buffer_event_data(event
);
1024 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
1025 entry
->ent
.type
= TRACE_USER_STACK
;
1027 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1029 trace
.nr_entries
= 0;
1030 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1032 trace
.entries
= entry
->caller
;
1034 save_stack_trace_user(&trace
);
1035 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
1039 void __trace_userstack(struct trace_array
*tr
,
1040 struct trace_array_cpu
*data
,
1041 unsigned long flags
)
1043 ftrace_trace_userstack(tr
, data
, flags
, preempt_count());
1047 ftrace_trace_special(void *__tr
, void *__data
,
1048 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1051 struct ring_buffer_event
*event
;
1052 struct trace_array_cpu
*data
= __data
;
1053 struct trace_array
*tr
= __tr
;
1054 struct special_entry
*entry
;
1055 unsigned long irq_flags
;
1057 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
1061 entry
= ring_buffer_event_data(event
);
1062 tracing_generic_entry_update(&entry
->ent
, 0, pc
);
1063 entry
->ent
.type
= TRACE_SPECIAL
;
1067 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
1068 ftrace_trace_stack(tr
, data
, irq_flags
, 4, pc
);
1069 ftrace_trace_userstack(tr
, data
, irq_flags
, pc
);
1075 __trace_special(void *__tr
, void *__data
,
1076 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1078 ftrace_trace_special(__tr
, __data
, arg1
, arg2
, arg3
, preempt_count());
1082 tracing_sched_switch_trace(struct trace_array
*tr
,
1083 struct trace_array_cpu
*data
,
1084 struct task_struct
*prev
,
1085 struct task_struct
*next
,
1086 unsigned long flags
, int pc
)
1088 struct ring_buffer_event
*event
;
1089 struct ctx_switch_entry
*entry
;
1090 unsigned long irq_flags
;
1092 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
1096 entry
= ring_buffer_event_data(event
);
1097 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
1098 entry
->ent
.type
= TRACE_CTX
;
1099 entry
->prev_pid
= prev
->pid
;
1100 entry
->prev_prio
= prev
->prio
;
1101 entry
->prev_state
= prev
->state
;
1102 entry
->next_pid
= next
->pid
;
1103 entry
->next_prio
= next
->prio
;
1104 entry
->next_state
= next
->state
;
1105 entry
->next_cpu
= task_cpu(next
);
1106 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
1107 ftrace_trace_stack(tr
, data
, flags
, 5, pc
);
1108 ftrace_trace_userstack(tr
, data
, flags
, pc
);
1112 tracing_sched_wakeup_trace(struct trace_array
*tr
,
1113 struct trace_array_cpu
*data
,
1114 struct task_struct
*wakee
,
1115 struct task_struct
*curr
,
1116 unsigned long flags
, int pc
)
1118 struct ring_buffer_event
*event
;
1119 struct ctx_switch_entry
*entry
;
1120 unsigned long irq_flags
;
1122 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
1126 entry
= ring_buffer_event_data(event
);
1127 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
1128 entry
->ent
.type
= TRACE_WAKE
;
1129 entry
->prev_pid
= curr
->pid
;
1130 entry
->prev_prio
= curr
->prio
;
1131 entry
->prev_state
= curr
->state
;
1132 entry
->next_pid
= wakee
->pid
;
1133 entry
->next_prio
= wakee
->prio
;
1134 entry
->next_state
= wakee
->state
;
1135 entry
->next_cpu
= task_cpu(wakee
);
1136 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
1137 ftrace_trace_stack(tr
, data
, flags
, 6, pc
);
1138 ftrace_trace_userstack(tr
, data
, flags
, pc
);
1144 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1146 struct trace_array
*tr
= &global_trace
;
1147 struct trace_array_cpu
*data
;
1148 unsigned long flags
;
1152 if (tracing_disabled
)
1155 pc
= preempt_count();
1156 local_irq_save(flags
);
1157 cpu
= raw_smp_processor_id();
1158 data
= tr
->data
[cpu
];
1160 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1161 ftrace_trace_special(tr
, data
, arg1
, arg2
, arg3
, pc
);
1163 atomic_dec(&data
->disabled
);
1164 local_irq_restore(flags
);
1167 #ifdef CONFIG_FUNCTION_TRACER
1169 function_trace_call_preempt_only(unsigned long ip
, unsigned long parent_ip
)
1171 struct trace_array
*tr
= &global_trace
;
1172 struct trace_array_cpu
*data
;
1173 unsigned long flags
;
1178 if (unlikely(!ftrace_function_enabled
))
1181 pc
= preempt_count();
1182 resched
= ftrace_preempt_disable();
1183 local_save_flags(flags
);
1184 cpu
= raw_smp_processor_id();
1185 data
= tr
->data
[cpu
];
1186 disabled
= atomic_inc_return(&data
->disabled
);
1188 if (likely(disabled
== 1))
1189 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
1191 atomic_dec(&data
->disabled
);
1192 ftrace_preempt_enable(resched
);
1196 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
1198 struct trace_array
*tr
= &global_trace
;
1199 struct trace_array_cpu
*data
;
1200 unsigned long flags
;
1205 if (unlikely(!ftrace_function_enabled
))
1209 * Need to use raw, since this must be called before the
1210 * recursive protection is performed.
1212 local_irq_save(flags
);
1213 cpu
= raw_smp_processor_id();
1214 data
= tr
->data
[cpu
];
1215 disabled
= atomic_inc_return(&data
->disabled
);
1217 if (likely(disabled
== 1)) {
1218 pc
= preempt_count();
1219 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
1222 atomic_dec(&data
->disabled
);
1223 local_irq_restore(flags
);
1226 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1227 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
1229 struct trace_array
*tr
= &global_trace
;
1230 struct trace_array_cpu
*data
;
1231 unsigned long flags
;
1236 if (!ftrace_trace_task(current
))
1239 if (!ftrace_graph_addr(trace
->func
))
1242 local_irq_save(flags
);
1243 cpu
= raw_smp_processor_id();
1244 data
= tr
->data
[cpu
];
1245 disabled
= atomic_inc_return(&data
->disabled
);
1246 if (likely(disabled
== 1)) {
1247 pc
= preempt_count();
1248 __trace_graph_entry(tr
, data
, trace
, flags
, pc
);
1250 /* Only do the atomic if it is not already set */
1251 if (!test_tsk_trace_graph(current
))
1252 set_tsk_trace_graph(current
);
1253 atomic_dec(&data
->disabled
);
1254 local_irq_restore(flags
);
1259 void trace_graph_return(struct ftrace_graph_ret
*trace
)
1261 struct trace_array
*tr
= &global_trace
;
1262 struct trace_array_cpu
*data
;
1263 unsigned long flags
;
1268 local_irq_save(flags
);
1269 cpu
= raw_smp_processor_id();
1270 data
= tr
->data
[cpu
];
1271 disabled
= atomic_inc_return(&data
->disabled
);
1272 if (likely(disabled
== 1)) {
1273 pc
= preempt_count();
1274 __trace_graph_return(tr
, data
, trace
, flags
, pc
);
1277 clear_tsk_trace_graph(current
);
1278 atomic_dec(&data
->disabled
);
1279 local_irq_restore(flags
);
1281 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1283 static struct ftrace_ops trace_ops __read_mostly
=
1285 .func
= function_trace_call
,
1288 void tracing_start_function_trace(void)
1290 ftrace_function_enabled
= 0;
1292 if (trace_flags
& TRACE_ITER_PREEMPTONLY
)
1293 trace_ops
.func
= function_trace_call_preempt_only
;
1295 trace_ops
.func
= function_trace_call
;
1297 register_ftrace_function(&trace_ops
);
1298 ftrace_function_enabled
= 1;
1301 void tracing_stop_function_trace(void)
1303 ftrace_function_enabled
= 0;
1304 unregister_ftrace_function(&trace_ops
);
1308 enum trace_file_type
{
1309 TRACE_FILE_LAT_FMT
= 1,
1310 TRACE_FILE_ANNOTATE
= 2,
1313 static void trace_iterator_increment(struct trace_iterator
*iter
, int cpu
)
1315 /* Don't allow ftrace to trace into the ring buffers */
1316 ftrace_disable_cpu();
1319 if (iter
->buffer_iter
[iter
->cpu
])
1320 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1322 ftrace_enable_cpu();
1325 static struct trace_entry
*
1326 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1328 struct ring_buffer_event
*event
;
1329 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1331 /* Don't allow ftrace to trace into the ring buffers */
1332 ftrace_disable_cpu();
1335 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1337 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1339 ftrace_enable_cpu();
1341 return event
? ring_buffer_event_data(event
) : NULL
;
1344 static struct trace_entry
*
1345 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1347 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1348 struct trace_entry
*ent
, *next
= NULL
;
1349 u64 next_ts
= 0, ts
;
1353 for_each_tracing_cpu(cpu
) {
1355 if (ring_buffer_empty_cpu(buffer
, cpu
))
1358 ent
= peek_next_entry(iter
, cpu
, &ts
);
1361 * Pick the entry with the smallest timestamp:
1363 if (ent
&& (!next
|| ts
< next_ts
)) {
1371 *ent_cpu
= next_cpu
;
1379 /* Find the next real entry, without updating the iterator itself */
1380 static struct trace_entry
*
1381 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1383 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1386 /* Find the next real entry, and increment the iterator to the next entry */
1387 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1389 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1392 trace_iterator_increment(iter
, iter
->cpu
);
1394 return iter
->ent
? iter
: NULL
;
1397 static void trace_consume(struct trace_iterator
*iter
)
1399 /* Don't allow ftrace to trace into the ring buffers */
1400 ftrace_disable_cpu();
1401 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1402 ftrace_enable_cpu();
1405 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1407 struct trace_iterator
*iter
= m
->private;
1413 /* can't go backwards */
1418 ent
= find_next_entry_inc(iter
);
1422 while (ent
&& iter
->idx
< i
)
1423 ent
= find_next_entry_inc(iter
);
1430 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1432 struct trace_iterator
*iter
= m
->private;
1437 mutex_lock(&trace_types_lock
);
1439 if (!current_trace
|| current_trace
!= iter
->trace
) {
1440 mutex_unlock(&trace_types_lock
);
1444 atomic_inc(&trace_record_cmdline_disabled
);
1446 if (*pos
!= iter
->pos
) {
1451 ftrace_disable_cpu();
1453 for_each_tracing_cpu(cpu
) {
1454 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1457 ftrace_enable_cpu();
1459 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1464 p
= s_next(m
, p
, &l
);
1470 static void s_stop(struct seq_file
*m
, void *p
)
1472 atomic_dec(&trace_record_cmdline_disabled
);
1473 mutex_unlock(&trace_types_lock
);
1476 #ifdef CONFIG_KRETPROBES
1477 static inline const char *kretprobed(const char *name
)
1479 static const char tramp_name
[] = "kretprobe_trampoline";
1480 int size
= sizeof(tramp_name
);
1482 if (strncmp(tramp_name
, name
, size
) == 0)
1483 return "[unknown/kretprobe'd]";
1487 static inline const char *kretprobed(const char *name
)
1491 #endif /* CONFIG_KRETPROBES */
1494 seq_print_sym_short(struct trace_seq
*s
, const char *fmt
, unsigned long address
)
1496 #ifdef CONFIG_KALLSYMS
1497 char str
[KSYM_SYMBOL_LEN
];
1500 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
1502 name
= kretprobed(str
);
1504 return trace_seq_printf(s
, fmt
, name
);
1510 seq_print_sym_offset(struct trace_seq
*s
, const char *fmt
,
1511 unsigned long address
)
1513 #ifdef CONFIG_KALLSYMS
1514 char str
[KSYM_SYMBOL_LEN
];
1517 sprint_symbol(str
, address
);
1518 name
= kretprobed(str
);
1520 return trace_seq_printf(s
, fmt
, name
);
1525 #ifndef CONFIG_64BIT
1526 # define IP_FMT "%08lx"
1528 # define IP_FMT "%016lx"
1532 seq_print_ip_sym(struct trace_seq
*s
, unsigned long ip
, unsigned long sym_flags
)
1537 return trace_seq_printf(s
, "0");
1539 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
1540 ret
= seq_print_sym_offset(s
, "%s", ip
);
1542 ret
= seq_print_sym_short(s
, "%s", ip
);
1547 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
1548 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
1552 static inline int seq_print_user_ip(struct trace_seq
*s
, struct mm_struct
*mm
,
1553 unsigned long ip
, unsigned long sym_flags
)
1555 struct file
*file
= NULL
;
1556 unsigned long vmstart
= 0;
1560 const struct vm_area_struct
*vma
;
1562 down_read(&mm
->mmap_sem
);
1563 vma
= find_vma(mm
, ip
);
1565 file
= vma
->vm_file
;
1566 vmstart
= vma
->vm_start
;
1569 ret
= trace_seq_path(s
, &file
->f_path
);
1571 ret
= trace_seq_printf(s
, "[+0x%lx]", ip
- vmstart
);
1573 up_read(&mm
->mmap_sem
);
1575 if (ret
&& ((sym_flags
& TRACE_ITER_SYM_ADDR
) || !file
))
1576 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
1581 seq_print_userip_objs(const struct userstack_entry
*entry
, struct trace_seq
*s
,
1582 unsigned long sym_flags
)
1584 struct mm_struct
*mm
= NULL
;
1588 if (trace_flags
& TRACE_ITER_SYM_USEROBJ
) {
1589 struct task_struct
*task
;
1591 * we do the lookup on the thread group leader,
1592 * since individual threads might have already quit!
1595 task
= find_task_by_vpid(entry
->ent
.tgid
);
1597 mm
= get_task_mm(task
);
1601 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1602 unsigned long ip
= entry
->caller
[i
];
1604 if (ip
== ULONG_MAX
|| !ret
)
1607 ret
= trace_seq_puts(s
, " <- ");
1610 ret
= trace_seq_puts(s
, "??");
1616 ret
= seq_print_user_ip(s
, mm
, ip
, sym_flags
);
1624 static void print_lat_help_header(struct seq_file
*m
)
1626 seq_puts(m
, "# _------=> CPU# \n");
1627 seq_puts(m
, "# / _-----=> irqs-off \n");
1628 seq_puts(m
, "# | / _----=> need-resched \n");
1629 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1630 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1631 seq_puts(m
, "# |||| / \n");
1632 seq_puts(m
, "# ||||| delay \n");
1633 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1634 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1637 static void print_func_help_header(struct seq_file
*m
)
1639 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1640 seq_puts(m
, "# | | | | |\n");
1645 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1647 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1648 struct trace_array
*tr
= iter
->tr
;
1649 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1650 struct tracer
*type
= current_trace
;
1651 unsigned long total
;
1652 unsigned long entries
;
1653 const char *name
= "preemption";
1658 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1660 ring_buffer_overruns(iter
->tr
->buffer
);
1662 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1664 seq_puts(m
, "-----------------------------------"
1665 "---------------------------------\n");
1666 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1667 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1668 nsecs_to_usecs(data
->saved_latency
),
1672 #if defined(CONFIG_PREEMPT_NONE)
1674 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1676 #elif defined(CONFIG_PREEMPT)
1681 /* These are reserved for later use */
1684 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1688 seq_puts(m
, " -----------------\n");
1689 seq_printf(m
, " | task: %.16s-%d "
1690 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1691 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1692 data
->policy
, data
->rt_priority
);
1693 seq_puts(m
, " -----------------\n");
1695 if (data
->critical_start
) {
1696 seq_puts(m
, " => started at: ");
1697 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1698 trace_print_seq(m
, &iter
->seq
);
1699 seq_puts(m
, "\n => ended at: ");
1700 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1701 trace_print_seq(m
, &iter
->seq
);
1709 lat_print_generic(struct trace_seq
*s
, struct trace_entry
*entry
, int cpu
)
1711 int hardirq
, softirq
;
1714 comm
= trace_find_cmdline(entry
->pid
);
1716 trace_seq_printf(s
, "%8.8s-%-5d ", comm
, entry
->pid
);
1717 trace_seq_printf(s
, "%3d", cpu
);
1718 trace_seq_printf(s
, "%c%c",
1719 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
1720 (entry
->flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ? 'X' : '.',
1721 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
1723 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
1724 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
1725 if (hardirq
&& softirq
) {
1726 trace_seq_putc(s
, 'H');
1729 trace_seq_putc(s
, 'h');
1732 trace_seq_putc(s
, 's');
1734 trace_seq_putc(s
, '.');
1738 if (entry
->preempt_count
)
1739 trace_seq_printf(s
, "%x", entry
->preempt_count
);
1741 trace_seq_puts(s
, ".");
1744 unsigned long preempt_mark_thresh
= 100;
1747 lat_print_timestamp(struct trace_seq
*s
, u64 abs_usecs
,
1748 unsigned long rel_usecs
)
1750 trace_seq_printf(s
, " %4lldus", abs_usecs
);
1751 if (rel_usecs
> preempt_mark_thresh
)
1752 trace_seq_puts(s
, "!: ");
1753 else if (rel_usecs
> 1)
1754 trace_seq_puts(s
, "+: ");
1756 trace_seq_puts(s
, " : ");
1759 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
1761 static int task_state_char(unsigned long state
)
1763 int bit
= state
? __ffs(state
) + 1 : 0;
1765 return bit
< sizeof(state_to_char
) - 1 ? state_to_char
[bit
] : '?';
1769 * The message is supposed to contain an ending newline.
1770 * If the printing stops prematurely, try to add a newline of our own.
1772 void trace_seq_print_cont(struct trace_seq
*s
, struct trace_iterator
*iter
)
1774 struct trace_entry
*ent
;
1775 struct trace_field_cont
*cont
;
1778 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1779 if (!ent
|| ent
->type
!= TRACE_CONT
) {
1780 trace_seq_putc(s
, '\n');
1785 cont
= (struct trace_field_cont
*)ent
;
1787 ok
= (trace_seq_printf(s
, "%s", cont
->buf
) > 0);
1789 ftrace_disable_cpu();
1791 if (iter
->buffer_iter
[iter
->cpu
])
1792 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1794 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, NULL
);
1796 ftrace_enable_cpu();
1798 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1799 } while (ent
&& ent
->type
== TRACE_CONT
);
1802 trace_seq_putc(s
, '\n');
1805 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1807 struct trace_seq
*s
= &iter
->seq
;
1809 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1812 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1815 if (cpu_isset(iter
->cpu
, iter
->started
))
1818 cpu_set(iter
->cpu
, iter
->started
);
1819 trace_seq_printf(s
, "##### CPU %u buffer started ####\n", iter
->cpu
);
1822 static enum print_line_t
1823 print_lat_fmt(struct trace_iterator
*iter
, unsigned int trace_idx
, int cpu
)
1825 struct trace_seq
*s
= &iter
->seq
;
1826 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1827 struct trace_entry
*next_entry
;
1828 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
1829 struct trace_entry
*entry
= iter
->ent
;
1830 unsigned long abs_usecs
;
1831 unsigned long rel_usecs
;
1837 if (entry
->type
== TRACE_CONT
)
1838 return TRACE_TYPE_HANDLED
;
1840 test_cpu_buff_start(iter
);
1842 next_entry
= find_next_entry(iter
, NULL
, &next_ts
);
1845 rel_usecs
= ns2usecs(next_ts
- iter
->ts
);
1846 abs_usecs
= ns2usecs(iter
->ts
- iter
->tr
->time_start
);
1849 comm
= trace_find_cmdline(entry
->pid
);
1850 trace_seq_printf(s
, "%16s %5d %3d %d %08x %08x [%08lx]"
1851 " %ld.%03ldms (+%ld.%03ldms): ",
1853 entry
->pid
, cpu
, entry
->flags
,
1854 entry
->preempt_count
, trace_idx
,
1857 abs_usecs
% 1000, rel_usecs
/1000,
1860 lat_print_generic(s
, entry
, cpu
);
1861 lat_print_timestamp(s
, abs_usecs
, rel_usecs
);
1863 switch (entry
->type
) {
1865 struct ftrace_entry
*field
;
1867 trace_assign_type(field
, entry
);
1869 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1870 trace_seq_puts(s
, " (");
1871 seq_print_ip_sym(s
, field
->parent_ip
, sym_flags
);
1872 trace_seq_puts(s
, ")\n");
1877 struct ctx_switch_entry
*field
;
1879 trace_assign_type(field
, entry
);
1881 T
= task_state_char(field
->next_state
);
1882 S
= task_state_char(field
->prev_state
);
1883 comm
= trace_find_cmdline(field
->next_pid
);
1884 trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1887 S
, entry
->type
== TRACE_CTX
? "==>" : " +",
1894 case TRACE_SPECIAL
: {
1895 struct special_entry
*field
;
1897 trace_assign_type(field
, entry
);
1899 trace_seq_printf(s
, "# %ld %ld %ld\n",
1906 struct stack_entry
*field
;
1908 trace_assign_type(field
, entry
);
1910 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1912 trace_seq_puts(s
, " <= ");
1913 seq_print_ip_sym(s
, field
->caller
[i
], sym_flags
);
1915 trace_seq_puts(s
, "\n");
1919 struct print_entry
*field
;
1921 trace_assign_type(field
, entry
);
1923 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1924 trace_seq_printf(s
, ": %s", field
->buf
);
1925 if (entry
->flags
& TRACE_FLAG_CONT
)
1926 trace_seq_print_cont(s
, iter
);
1929 case TRACE_BRANCH
: {
1930 struct trace_branch
*field
;
1932 trace_assign_type(field
, entry
);
1934 trace_seq_printf(s
, "[%s] %s:%s:%d\n",
1935 field
->correct
? " ok " : " MISS ",
1941 case TRACE_USER_STACK
: {
1942 struct userstack_entry
*field
;
1944 trace_assign_type(field
, entry
);
1946 seq_print_userip_objs(field
, s
, sym_flags
);
1947 trace_seq_putc(s
, '\n');
1951 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
1953 return TRACE_TYPE_HANDLED
;
1956 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1958 struct trace_seq
*s
= &iter
->seq
;
1959 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1960 struct trace_entry
*entry
;
1961 unsigned long usec_rem
;
1962 unsigned long long t
;
1971 if (entry
->type
== TRACE_CONT
)
1972 return TRACE_TYPE_HANDLED
;
1974 test_cpu_buff_start(iter
);
1976 comm
= trace_find_cmdline(iter
->ent
->pid
);
1978 t
= ns2usecs(iter
->ts
);
1979 usec_rem
= do_div(t
, 1000000ULL);
1980 secs
= (unsigned long)t
;
1982 ret
= trace_seq_printf(s
, "%16s-%-5d ", comm
, entry
->pid
);
1984 return TRACE_TYPE_PARTIAL_LINE
;
1985 ret
= trace_seq_printf(s
, "[%03d] ", iter
->cpu
);
1987 return TRACE_TYPE_PARTIAL_LINE
;
1988 ret
= trace_seq_printf(s
, "%5lu.%06lu: ", secs
, usec_rem
);
1990 return TRACE_TYPE_PARTIAL_LINE
;
1992 switch (entry
->type
) {
1994 struct ftrace_entry
*field
;
1996 trace_assign_type(field
, entry
);
1998 ret
= seq_print_ip_sym(s
, field
->ip
, sym_flags
);
2000 return TRACE_TYPE_PARTIAL_LINE
;
2001 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
2003 ret
= trace_seq_printf(s
, " <-");
2005 return TRACE_TYPE_PARTIAL_LINE
;
2006 ret
= seq_print_ip_sym(s
,
2010 return TRACE_TYPE_PARTIAL_LINE
;
2012 ret
= trace_seq_printf(s
, "\n");
2014 return TRACE_TYPE_PARTIAL_LINE
;
2019 struct ctx_switch_entry
*field
;
2021 trace_assign_type(field
, entry
);
2023 T
= task_state_char(field
->next_state
);
2024 S
= task_state_char(field
->prev_state
);
2025 ret
= trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
2029 entry
->type
== TRACE_CTX
? "==>" : " +",
2035 return TRACE_TYPE_PARTIAL_LINE
;
2038 case TRACE_SPECIAL
: {
2039 struct special_entry
*field
;
2041 trace_assign_type(field
, entry
);
2043 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
2048 return TRACE_TYPE_PARTIAL_LINE
;
2052 struct stack_entry
*field
;
2054 trace_assign_type(field
, entry
);
2056 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
2058 ret
= trace_seq_puts(s
, " <= ");
2060 return TRACE_TYPE_PARTIAL_LINE
;
2062 ret
= seq_print_ip_sym(s
, field
->caller
[i
],
2065 return TRACE_TYPE_PARTIAL_LINE
;
2067 ret
= trace_seq_puts(s
, "\n");
2069 return TRACE_TYPE_PARTIAL_LINE
;
2073 struct print_entry
*field
;
2075 trace_assign_type(field
, entry
);
2077 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
2078 trace_seq_printf(s
, ": %s", field
->buf
);
2079 if (entry
->flags
& TRACE_FLAG_CONT
)
2080 trace_seq_print_cont(s
, iter
);
2083 case TRACE_GRAPH_RET
: {
2084 return print_graph_function(iter
);
2086 case TRACE_GRAPH_ENT
: {
2087 return print_graph_function(iter
);
2089 case TRACE_BRANCH
: {
2090 struct trace_branch
*field
;
2092 trace_assign_type(field
, entry
);
2094 trace_seq_printf(s
, "[%s] %s:%s:%d\n",
2095 field
->correct
? " ok " : " MISS ",
2101 case TRACE_USER_STACK
: {
2102 struct userstack_entry
*field
;
2104 trace_assign_type(field
, entry
);
2106 ret
= seq_print_userip_objs(field
, s
, sym_flags
);
2108 return TRACE_TYPE_PARTIAL_LINE
;
2109 ret
= trace_seq_putc(s
, '\n');
2111 return TRACE_TYPE_PARTIAL_LINE
;
2115 return TRACE_TYPE_HANDLED
;
2118 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
2120 struct trace_seq
*s
= &iter
->seq
;
2121 struct trace_entry
*entry
;
2127 if (entry
->type
== TRACE_CONT
)
2128 return TRACE_TYPE_HANDLED
;
2130 ret
= trace_seq_printf(s
, "%d %d %llu ",
2131 entry
->pid
, iter
->cpu
, iter
->ts
);
2133 return TRACE_TYPE_PARTIAL_LINE
;
2135 switch (entry
->type
) {
2137 struct ftrace_entry
*field
;
2139 trace_assign_type(field
, entry
);
2141 ret
= trace_seq_printf(s
, "%x %x\n",
2145 return TRACE_TYPE_PARTIAL_LINE
;
2150 struct ctx_switch_entry
*field
;
2152 trace_assign_type(field
, entry
);
2154 T
= task_state_char(field
->next_state
);
2155 S
= entry
->type
== TRACE_WAKE
? '+' :
2156 task_state_char(field
->prev_state
);
2157 ret
= trace_seq_printf(s
, "%d %d %c %d %d %d %c\n",
2166 return TRACE_TYPE_PARTIAL_LINE
;
2170 case TRACE_USER_STACK
:
2172 struct special_entry
*field
;
2174 trace_assign_type(field
, entry
);
2176 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
2181 return TRACE_TYPE_PARTIAL_LINE
;
2185 struct print_entry
*field
;
2187 trace_assign_type(field
, entry
);
2189 trace_seq_printf(s
, "# %lx %s", field
->ip
, field
->buf
);
2190 if (entry
->flags
& TRACE_FLAG_CONT
)
2191 trace_seq_print_cont(s
, iter
);
2195 return TRACE_TYPE_HANDLED
;
2198 #define SEQ_PUT_FIELD_RET(s, x) \
2200 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
2204 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
2206 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
2207 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
2211 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
2213 struct trace_seq
*s
= &iter
->seq
;
2214 unsigned char newline
= '\n';
2215 struct trace_entry
*entry
;
2220 if (entry
->type
== TRACE_CONT
)
2221 return TRACE_TYPE_HANDLED
;
2223 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
2224 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
2225 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
2227 switch (entry
->type
) {
2229 struct ftrace_entry
*field
;
2231 trace_assign_type(field
, entry
);
2233 SEQ_PUT_HEX_FIELD_RET(s
, field
->ip
);
2234 SEQ_PUT_HEX_FIELD_RET(s
, field
->parent_ip
);
2239 struct ctx_switch_entry
*field
;
2241 trace_assign_type(field
, entry
);
2243 T
= task_state_char(field
->next_state
);
2244 S
= entry
->type
== TRACE_WAKE
? '+' :
2245 task_state_char(field
->prev_state
);
2246 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_pid
);
2247 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_prio
);
2248 SEQ_PUT_HEX_FIELD_RET(s
, S
);
2249 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_cpu
);
2250 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_pid
);
2251 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_prio
);
2252 SEQ_PUT_HEX_FIELD_RET(s
, T
);
2256 case TRACE_USER_STACK
:
2258 struct special_entry
*field
;
2260 trace_assign_type(field
, entry
);
2262 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg1
);
2263 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg2
);
2264 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg3
);
2268 SEQ_PUT_FIELD_RET(s
, newline
);
2270 return TRACE_TYPE_HANDLED
;
2273 static enum print_line_t
print_printk_msg_only(struct trace_iterator
*iter
)
2275 struct trace_seq
*s
= &iter
->seq
;
2276 struct trace_entry
*entry
= iter
->ent
;
2277 struct print_entry
*field
;
2280 trace_assign_type(field
, entry
);
2282 ret
= trace_seq_printf(s
, field
->buf
);
2284 return TRACE_TYPE_PARTIAL_LINE
;
2286 if (entry
->flags
& TRACE_FLAG_CONT
)
2287 trace_seq_print_cont(s
, iter
);
2289 return TRACE_TYPE_HANDLED
;
2292 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
2294 struct trace_seq
*s
= &iter
->seq
;
2295 struct trace_entry
*entry
;
2299 if (entry
->type
== TRACE_CONT
)
2300 return TRACE_TYPE_HANDLED
;
2302 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
2303 SEQ_PUT_FIELD_RET(s
, entry
->cpu
);
2304 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
2306 switch (entry
->type
) {
2308 struct ftrace_entry
*field
;
2310 trace_assign_type(field
, entry
);
2312 SEQ_PUT_FIELD_RET(s
, field
->ip
);
2313 SEQ_PUT_FIELD_RET(s
, field
->parent_ip
);
2317 struct ctx_switch_entry
*field
;
2319 trace_assign_type(field
, entry
);
2321 SEQ_PUT_FIELD_RET(s
, field
->prev_pid
);
2322 SEQ_PUT_FIELD_RET(s
, field
->prev_prio
);
2323 SEQ_PUT_FIELD_RET(s
, field
->prev_state
);
2324 SEQ_PUT_FIELD_RET(s
, field
->next_pid
);
2325 SEQ_PUT_FIELD_RET(s
, field
->next_prio
);
2326 SEQ_PUT_FIELD_RET(s
, field
->next_state
);
2330 case TRACE_USER_STACK
:
2332 struct special_entry
*field
;
2334 trace_assign_type(field
, entry
);
2336 SEQ_PUT_FIELD_RET(s
, field
->arg1
);
2337 SEQ_PUT_FIELD_RET(s
, field
->arg2
);
2338 SEQ_PUT_FIELD_RET(s
, field
->arg3
);
2345 static int trace_empty(struct trace_iterator
*iter
)
2349 for_each_tracing_cpu(cpu
) {
2350 if (iter
->buffer_iter
[cpu
]) {
2351 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
2354 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
2362 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
2364 enum print_line_t ret
;
2366 if (iter
->trace
&& iter
->trace
->print_line
) {
2367 ret
= iter
->trace
->print_line(iter
);
2368 if (ret
!= TRACE_TYPE_UNHANDLED
)
2372 if (iter
->ent
->type
== TRACE_PRINT
&&
2373 trace_flags
& TRACE_ITER_PRINTK
&&
2374 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2375 return print_printk_msg_only(iter
);
2377 if (trace_flags
& TRACE_ITER_BIN
)
2378 return print_bin_fmt(iter
);
2380 if (trace_flags
& TRACE_ITER_HEX
)
2381 return print_hex_fmt(iter
);
2383 if (trace_flags
& TRACE_ITER_RAW
)
2384 return print_raw_fmt(iter
);
2386 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
2387 return print_lat_fmt(iter
, iter
->idx
, iter
->cpu
);
2389 return print_trace_fmt(iter
);
2392 static int s_show(struct seq_file
*m
, void *v
)
2394 struct trace_iterator
*iter
= v
;
2396 if (iter
->ent
== NULL
) {
2398 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
2401 if (iter
->trace
&& iter
->trace
->print_header
)
2402 iter
->trace
->print_header(m
);
2403 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2404 /* print nothing if the buffers are empty */
2405 if (trace_empty(iter
))
2407 print_trace_header(m
, iter
);
2408 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2409 print_lat_help_header(m
);
2411 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2412 print_func_help_header(m
);
2415 print_trace_line(iter
);
2416 trace_print_seq(m
, &iter
->seq
);
2422 static struct seq_operations tracer_seq_ops
= {
2429 static struct trace_iterator
*
2430 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
2432 struct trace_iterator
*iter
;
2436 if (tracing_disabled
) {
2441 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2447 mutex_lock(&trace_types_lock
);
2448 if (current_trace
&& current_trace
->print_max
)
2451 iter
->tr
= inode
->i_private
;
2452 iter
->trace
= current_trace
;
2455 /* Notify the tracer early; before we stop tracing. */
2456 if (iter
->trace
&& iter
->trace
->open
)
2457 iter
->trace
->open(iter
);
2459 /* Annotate start of buffers if we had overruns */
2460 if (ring_buffer_overruns(iter
->tr
->buffer
))
2461 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
2464 for_each_tracing_cpu(cpu
) {
2466 iter
->buffer_iter
[cpu
] =
2467 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2469 if (!iter
->buffer_iter
[cpu
])
2473 /* TODO stop tracer */
2474 *ret
= seq_open(file
, &tracer_seq_ops
);
2478 m
= file
->private_data
;
2481 /* stop the trace while dumping */
2484 mutex_unlock(&trace_types_lock
);
2490 for_each_tracing_cpu(cpu
) {
2491 if (iter
->buffer_iter
[cpu
])
2492 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2494 mutex_unlock(&trace_types_lock
);
2497 return ERR_PTR(-ENOMEM
);
2500 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2502 if (tracing_disabled
)
2505 filp
->private_data
= inode
->i_private
;
2509 int tracing_release(struct inode
*inode
, struct file
*file
)
2511 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2512 struct trace_iterator
*iter
= m
->private;
2515 mutex_lock(&trace_types_lock
);
2516 for_each_tracing_cpu(cpu
) {
2517 if (iter
->buffer_iter
[cpu
])
2518 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2521 if (iter
->trace
&& iter
->trace
->close
)
2522 iter
->trace
->close(iter
);
2524 /* reenable tracing if it was previously enabled */
2526 mutex_unlock(&trace_types_lock
);
2528 seq_release(inode
, file
);
2533 static int tracing_open(struct inode
*inode
, struct file
*file
)
2537 __tracing_open(inode
, file
, &ret
);
2542 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
2544 struct trace_iterator
*iter
;
2547 iter
= __tracing_open(inode
, file
, &ret
);
2550 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2557 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2559 struct tracer
*t
= m
->private;
2571 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2573 struct tracer
*t
= m
->private;
2576 mutex_lock(&trace_types_lock
);
2577 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2583 static void t_stop(struct seq_file
*m
, void *p
)
2585 mutex_unlock(&trace_types_lock
);
2588 static int t_show(struct seq_file
*m
, void *v
)
2590 struct tracer
*t
= v
;
2595 seq_printf(m
, "%s", t
->name
);
2604 static struct seq_operations show_traces_seq_ops
= {
2611 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2615 if (tracing_disabled
)
2618 ret
= seq_open(file
, &show_traces_seq_ops
);
2620 struct seq_file
*m
= file
->private_data
;
2621 m
->private = trace_types
;
2627 static struct file_operations tracing_fops
= {
2628 .open
= tracing_open
,
2630 .llseek
= seq_lseek
,
2631 .release
= tracing_release
,
2634 static struct file_operations tracing_lt_fops
= {
2635 .open
= tracing_lt_open
,
2637 .llseek
= seq_lseek
,
2638 .release
= tracing_release
,
2641 static struct file_operations show_traces_fops
= {
2642 .open
= show_traces_open
,
2644 .release
= seq_release
,
2648 * Only trace on a CPU if the bitmask is set:
2650 static cpumask_t tracing_cpumask
= CPU_MASK_ALL
;
2653 * When tracing/tracing_cpu_mask is modified then this holds
2654 * the new bitmask we are about to install:
2656 static cpumask_t tracing_cpumask_new
;
2659 * The tracer itself will not take this lock, but still we want
2660 * to provide a consistent cpumask to user-space:
2662 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2665 * Temporary storage for the character representation of the
2666 * CPU bitmask (and one more byte for the newline):
2668 static char mask_str
[NR_CPUS
+ 1];
2671 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2672 size_t count
, loff_t
*ppos
)
2676 mutex_lock(&tracing_cpumask_update_lock
);
2678 len
= cpumask_scnprintf(mask_str
, count
, &tracing_cpumask
);
2679 if (count
- len
< 2) {
2683 len
+= sprintf(mask_str
+ len
, "\n");
2684 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2687 mutex_unlock(&tracing_cpumask_update_lock
);
2693 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2694 size_t count
, loff_t
*ppos
)
2698 mutex_lock(&tracing_cpumask_update_lock
);
2699 err
= cpumask_parse_user(ubuf
, count
, &tracing_cpumask_new
);
2703 local_irq_disable();
2704 __raw_spin_lock(&ftrace_max_lock
);
2705 for_each_tracing_cpu(cpu
) {
2707 * Increase/decrease the disabled counter if we are
2708 * about to flip a bit in the cpumask:
2710 if (cpu_isset(cpu
, tracing_cpumask
) &&
2711 !cpu_isset(cpu
, tracing_cpumask_new
)) {
2712 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2714 if (!cpu_isset(cpu
, tracing_cpumask
) &&
2715 cpu_isset(cpu
, tracing_cpumask_new
)) {
2716 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2719 __raw_spin_unlock(&ftrace_max_lock
);
2722 tracing_cpumask
= tracing_cpumask_new
;
2724 mutex_unlock(&tracing_cpumask_update_lock
);
2729 mutex_unlock(&tracing_cpumask_update_lock
);
2734 static struct file_operations tracing_cpumask_fops
= {
2735 .open
= tracing_open_generic
,
2736 .read
= tracing_cpumask_read
,
2737 .write
= tracing_cpumask_write
,
2741 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2742 size_t cnt
, loff_t
*ppos
)
2748 u32 tracer_flags
= current_trace
->flags
->val
;
2749 struct tracer_opt
*trace_opts
= current_trace
->flags
->opts
;
2752 /* calulate max size */
2753 for (i
= 0; trace_options
[i
]; i
++) {
2754 len
+= strlen(trace_options
[i
]);
2755 len
+= 3; /* "no" and space */
2759 * Increase the size with names of options specific
2760 * of the current tracer.
2762 for (i
= 0; trace_opts
[i
].name
; i
++) {
2763 len
+= strlen(trace_opts
[i
].name
);
2764 len
+= 3; /* "no" and space */
2767 /* +2 for \n and \0 */
2768 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2772 for (i
= 0; trace_options
[i
]; i
++) {
2773 if (trace_flags
& (1 << i
))
2774 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
2776 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
2779 for (i
= 0; trace_opts
[i
].name
; i
++) {
2780 if (tracer_flags
& trace_opts
[i
].bit
)
2781 r
+= sprintf(buf
+ r
, "%s ",
2782 trace_opts
[i
].name
);
2784 r
+= sprintf(buf
+ r
, "no%s ",
2785 trace_opts
[i
].name
);
2788 r
+= sprintf(buf
+ r
, "\n");
2789 WARN_ON(r
>= len
+ 2);
2791 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2798 /* Try to assign a tracer specific option */
2799 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2801 struct tracer_flags
*trace_flags
= trace
->flags
;
2802 struct tracer_opt
*opts
= NULL
;
2806 for (i
= 0; trace_flags
->opts
[i
].name
; i
++) {
2807 opts
= &trace_flags
->opts
[i
];
2808 len
= strlen(opts
->name
);
2810 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2811 ret
= trace
->set_flag(trace_flags
->val
,
2817 if (!trace_flags
->opts
[i
].name
)
2820 /* Refused to handle */
2825 trace_flags
->val
&= ~opts
->bit
;
2827 trace_flags
->val
|= opts
->bit
;
2833 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2834 size_t cnt
, loff_t
*ppos
)
2842 if (cnt
>= sizeof(buf
))
2845 if (copy_from_user(&buf
, ubuf
, cnt
))
2850 if (strncmp(buf
, "no", 2) == 0) {
2855 for (i
= 0; trace_options
[i
]; i
++) {
2856 int len
= strlen(trace_options
[i
]);
2858 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2860 trace_flags
&= ~(1 << i
);
2862 trace_flags
|= (1 << i
);
2867 /* If no option could be set, test the specific tracer options */
2868 if (!trace_options
[i
]) {
2869 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2879 static struct file_operations tracing_iter_fops
= {
2880 .open
= tracing_open_generic
,
2881 .read
= tracing_trace_options_read
,
2882 .write
= tracing_trace_options_write
,
2885 static const char readme_msg
[] =
2886 "tracing mini-HOWTO:\n\n"
2888 "# mount -t debugfs nodev /debug\n\n"
2889 "# cat /debug/tracing/available_tracers\n"
2890 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2891 "# cat /debug/tracing/current_tracer\n"
2893 "# echo sched_switch > /debug/tracing/current_tracer\n"
2894 "# cat /debug/tracing/current_tracer\n"
2896 "# cat /debug/tracing/trace_options\n"
2897 "noprint-parent nosym-offset nosym-addr noverbose\n"
2898 "# echo print-parent > /debug/tracing/trace_options\n"
2899 "# echo 1 > /debug/tracing/tracing_enabled\n"
2900 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2901 "echo 0 > /debug/tracing/tracing_enabled\n"
2905 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2906 size_t cnt
, loff_t
*ppos
)
2908 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2909 readme_msg
, strlen(readme_msg
));
2912 static struct file_operations tracing_readme_fops
= {
2913 .open
= tracing_open_generic
,
2914 .read
= tracing_readme_read
,
2918 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2919 size_t cnt
, loff_t
*ppos
)
2924 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2925 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2929 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2930 size_t cnt
, loff_t
*ppos
)
2932 struct trace_array
*tr
= filp
->private_data
;
2937 if (cnt
>= sizeof(buf
))
2940 if (copy_from_user(&buf
, ubuf
, cnt
))
2945 ret
= strict_strtoul(buf
, 10, &val
);
2951 mutex_lock(&trace_types_lock
);
2952 if (tracer_enabled
^ val
) {
2955 if (current_trace
->start
)
2956 current_trace
->start(tr
);
2961 if (current_trace
->stop
)
2962 current_trace
->stop(tr
);
2965 mutex_unlock(&trace_types_lock
);
2973 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2974 size_t cnt
, loff_t
*ppos
)
2976 char buf
[max_tracer_type_len
+2];
2979 mutex_lock(&trace_types_lock
);
2981 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2983 r
= sprintf(buf
, "\n");
2984 mutex_unlock(&trace_types_lock
);
2986 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2989 static int tracing_set_tracer(char *buf
)
2991 struct trace_array
*tr
= &global_trace
;
2995 mutex_lock(&trace_types_lock
);
2996 for (t
= trace_types
; t
; t
= t
->next
) {
2997 if (strcmp(t
->name
, buf
) == 0)
3004 if (t
== current_trace
)
3007 trace_branch_disable();
3008 if (current_trace
&& current_trace
->reset
)
3009 current_trace
->reset(tr
);
3018 trace_branch_enable(tr
);
3020 mutex_unlock(&trace_types_lock
);
3026 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
3027 size_t cnt
, loff_t
*ppos
)
3029 char buf
[max_tracer_type_len
+1];
3036 if (cnt
> max_tracer_type_len
)
3037 cnt
= max_tracer_type_len
;
3039 if (copy_from_user(&buf
, ubuf
, cnt
))
3044 /* strip ending whitespace. */
3045 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
3048 err
= tracing_set_tracer(buf
);
3058 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
3059 size_t cnt
, loff_t
*ppos
)
3061 unsigned long *ptr
= filp
->private_data
;
3065 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
3066 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
3067 if (r
> sizeof(buf
))
3069 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3073 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
3074 size_t cnt
, loff_t
*ppos
)
3076 long *ptr
= filp
->private_data
;
3081 if (cnt
>= sizeof(buf
))
3084 if (copy_from_user(&buf
, ubuf
, cnt
))
3089 ret
= strict_strtoul(buf
, 10, &val
);
3098 static atomic_t tracing_reader
;
3100 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
3102 struct trace_iterator
*iter
;
3104 if (tracing_disabled
)
3107 /* We only allow for reader of the pipe */
3108 if (atomic_inc_return(&tracing_reader
) != 1) {
3109 atomic_dec(&tracing_reader
);
3113 /* create a buffer to store the information to pass to userspace */
3114 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
3118 mutex_lock(&trace_types_lock
);
3120 /* trace pipe does not show start of buffer */
3121 cpus_setall(iter
->started
);
3123 iter
->tr
= &global_trace
;
3124 iter
->trace
= current_trace
;
3125 filp
->private_data
= iter
;
3127 if (iter
->trace
->pipe_open
)
3128 iter
->trace
->pipe_open(iter
);
3129 mutex_unlock(&trace_types_lock
);
3134 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
3136 struct trace_iterator
*iter
= file
->private_data
;
3139 atomic_dec(&tracing_reader
);
3145 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
3147 struct trace_iterator
*iter
= filp
->private_data
;
3149 if (trace_flags
& TRACE_ITER_BLOCK
) {
3151 * Always select as readable when in blocking mode
3153 return POLLIN
| POLLRDNORM
;
3155 if (!trace_empty(iter
))
3156 return POLLIN
| POLLRDNORM
;
3157 poll_wait(filp
, &trace_wait
, poll_table
);
3158 if (!trace_empty(iter
))
3159 return POLLIN
| POLLRDNORM
;
3169 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
3170 size_t cnt
, loff_t
*ppos
)
3172 struct trace_iterator
*iter
= filp
->private_data
;
3175 /* return any leftover data */
3176 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3180 trace_seq_reset(&iter
->seq
);
3182 mutex_lock(&trace_types_lock
);
3183 if (iter
->trace
->read
) {
3184 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
3191 while (trace_empty(iter
)) {
3193 if ((filp
->f_flags
& O_NONBLOCK
)) {
3199 * This is a make-shift waitqueue. The reason we don't use
3200 * an actual wait queue is because:
3201 * 1) we only ever have one waiter
3202 * 2) the tracing, traces all functions, we don't want
3203 * the overhead of calling wake_up and friends
3204 * (and tracing them too)
3205 * Anyway, this is really very primitive wakeup.
3207 set_current_state(TASK_INTERRUPTIBLE
);
3208 iter
->tr
->waiter
= current
;
3210 mutex_unlock(&trace_types_lock
);
3212 /* sleep for 100 msecs, and try again. */
3213 schedule_timeout(HZ
/10);
3215 mutex_lock(&trace_types_lock
);
3217 iter
->tr
->waiter
= NULL
;
3219 if (signal_pending(current
)) {
3224 if (iter
->trace
!= current_trace
)
3228 * We block until we read something and tracing is disabled.
3229 * We still block if tracing is disabled, but we have never
3230 * read anything. This allows a user to cat this file, and
3231 * then enable tracing. But after we have read something,
3232 * we give an EOF when tracing is again disabled.
3234 * iter->pos will be 0 if we haven't read anything.
3236 if (!tracer_enabled
&& iter
->pos
)
3242 /* stop when tracing is finished */
3243 if (trace_empty(iter
))
3246 if (cnt
>= PAGE_SIZE
)
3247 cnt
= PAGE_SIZE
- 1;
3249 /* reset all but tr, trace, and overruns */
3250 memset(&iter
->seq
, 0,
3251 sizeof(struct trace_iterator
) -
3252 offsetof(struct trace_iterator
, seq
));
3255 while (find_next_entry_inc(iter
) != NULL
) {
3256 enum print_line_t ret
;
3257 int len
= iter
->seq
.len
;
3259 ret
= print_trace_line(iter
);
3260 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3261 /* don't print partial lines */
3262 iter
->seq
.len
= len
;
3266 trace_consume(iter
);
3268 if (iter
->seq
.len
>= cnt
)
3272 /* Now copy what we have to the user */
3273 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3274 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3275 trace_seq_reset(&iter
->seq
);
3278 * If there was nothing to send to user, inspite of consuming trace
3279 * entries, go back to wait for more entries.
3285 mutex_unlock(&trace_types_lock
);
3291 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3292 size_t cnt
, loff_t
*ppos
)
3294 struct trace_array
*tr
= filp
->private_data
;
3298 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3299 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3303 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3304 size_t cnt
, loff_t
*ppos
)
3310 if (cnt
>= sizeof(buf
))
3313 if (copy_from_user(&buf
, ubuf
, cnt
))
3318 ret
= strict_strtoul(buf
, 10, &val
);
3322 /* must have at least 1 entry */
3326 mutex_lock(&trace_types_lock
);
3330 /* disable all cpu buffers */
3331 for_each_tracing_cpu(cpu
) {
3332 if (global_trace
.data
[cpu
])
3333 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3334 if (max_tr
.data
[cpu
])
3335 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3338 /* value is in KB */
3341 if (val
!= global_trace
.entries
) {
3342 ret
= ring_buffer_resize(global_trace
.buffer
, val
);
3348 ret
= ring_buffer_resize(max_tr
.buffer
, val
);
3352 r
= ring_buffer_resize(global_trace
.buffer
,
3353 global_trace
.entries
);
3355 /* AARGH! We are left with different
3356 * size max buffer!!!! */
3358 tracing_disabled
= 1;
3363 global_trace
.entries
= val
;
3368 /* If check pages failed, return ENOMEM */
3369 if (tracing_disabled
)
3372 for_each_tracing_cpu(cpu
) {
3373 if (global_trace
.data
[cpu
])
3374 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3375 if (max_tr
.data
[cpu
])
3376 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3380 max_tr
.entries
= global_trace
.entries
;
3381 mutex_unlock(&trace_types_lock
);
3386 static int mark_printk(const char *fmt
, ...)
3390 va_start(args
, fmt
);
3391 ret
= trace_vprintk(0, -1, fmt
, args
);
3397 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3398 size_t cnt
, loff_t
*fpos
)
3403 if (tracing_disabled
)
3406 if (cnt
> TRACE_BUF_SIZE
)
3407 cnt
= TRACE_BUF_SIZE
;
3409 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3413 if (copy_from_user(buf
, ubuf
, cnt
)) {
3418 /* Cut from the first nil or newline. */
3420 end
= strchr(buf
, '\n');
3424 cnt
= mark_printk("%s\n", buf
);
3431 static struct file_operations tracing_max_lat_fops
= {
3432 .open
= tracing_open_generic
,
3433 .read
= tracing_max_lat_read
,
3434 .write
= tracing_max_lat_write
,
3437 static struct file_operations tracing_ctrl_fops
= {
3438 .open
= tracing_open_generic
,
3439 .read
= tracing_ctrl_read
,
3440 .write
= tracing_ctrl_write
,
3443 static struct file_operations set_tracer_fops
= {
3444 .open
= tracing_open_generic
,
3445 .read
= tracing_set_trace_read
,
3446 .write
= tracing_set_trace_write
,
3449 static struct file_operations tracing_pipe_fops
= {
3450 .open
= tracing_open_pipe
,
3451 .poll
= tracing_poll_pipe
,
3452 .read
= tracing_read_pipe
,
3453 .release
= tracing_release_pipe
,
3456 static struct file_operations tracing_entries_fops
= {
3457 .open
= tracing_open_generic
,
3458 .read
= tracing_entries_read
,
3459 .write
= tracing_entries_write
,
3462 static struct file_operations tracing_mark_fops
= {
3463 .open
= tracing_open_generic
,
3464 .write
= tracing_mark_write
,
3467 #ifdef CONFIG_DYNAMIC_FTRACE
3469 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3475 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3476 size_t cnt
, loff_t
*ppos
)
3478 static char ftrace_dyn_info_buffer
[1024];
3479 static DEFINE_MUTEX(dyn_info_mutex
);
3480 unsigned long *p
= filp
->private_data
;
3481 char *buf
= ftrace_dyn_info_buffer
;
3482 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3485 mutex_lock(&dyn_info_mutex
);
3486 r
= sprintf(buf
, "%ld ", *p
);
3488 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3491 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3493 mutex_unlock(&dyn_info_mutex
);
3498 static struct file_operations tracing_dyn_info_fops
= {
3499 .open
= tracing_open_generic
,
3500 .read
= tracing_read_dyn_info
,
3504 static struct dentry
*d_tracer
;
3506 struct dentry
*tracing_init_dentry(void)
3513 d_tracer
= debugfs_create_dir("tracing", NULL
);
3515 if (!d_tracer
&& !once
) {
3517 pr_warning("Could not create debugfs directory 'tracing'\n");
3524 #ifdef CONFIG_FTRACE_SELFTEST
3525 /* Let selftest have access to static functions in this file */
3526 #include "trace_selftest.c"
3529 static __init
int tracer_init_debugfs(void)
3531 struct dentry
*d_tracer
;
3532 struct dentry
*entry
;
3534 d_tracer
= tracing_init_dentry();
3536 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
3537 &global_trace
, &tracing_ctrl_fops
);
3539 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3541 entry
= debugfs_create_file("trace_options", 0644, d_tracer
,
3542 NULL
, &tracing_iter_fops
);
3544 pr_warning("Could not create debugfs 'trace_options' entry\n");
3546 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
3547 NULL
, &tracing_cpumask_fops
);
3549 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3551 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
3552 &global_trace
, &tracing_lt_fops
);
3554 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3556 entry
= debugfs_create_file("trace", 0444, d_tracer
,
3557 &global_trace
, &tracing_fops
);
3559 pr_warning("Could not create debugfs 'trace' entry\n");
3561 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
3562 &global_trace
, &show_traces_fops
);
3564 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3566 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
3567 &global_trace
, &set_tracer_fops
);
3569 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3571 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
3572 &tracing_max_latency
,
3573 &tracing_max_lat_fops
);
3575 pr_warning("Could not create debugfs "
3576 "'tracing_max_latency' entry\n");
3578 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
3579 &tracing_thresh
, &tracing_max_lat_fops
);
3581 pr_warning("Could not create debugfs "
3582 "'tracing_thresh' entry\n");
3583 entry
= debugfs_create_file("README", 0644, d_tracer
,
3584 NULL
, &tracing_readme_fops
);
3586 pr_warning("Could not create debugfs 'README' entry\n");
3588 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
3589 NULL
, &tracing_pipe_fops
);
3591 pr_warning("Could not create debugfs "
3592 "'trace_pipe' entry\n");
3594 entry
= debugfs_create_file("buffer_size_kb", 0644, d_tracer
,
3595 &global_trace
, &tracing_entries_fops
);
3597 pr_warning("Could not create debugfs "
3598 "'buffer_size_kb' entry\n");
3600 entry
= debugfs_create_file("trace_marker", 0220, d_tracer
,
3601 NULL
, &tracing_mark_fops
);
3603 pr_warning("Could not create debugfs "
3604 "'trace_marker' entry\n");
3606 #ifdef CONFIG_DYNAMIC_FTRACE
3607 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
3608 &ftrace_update_tot_cnt
,
3609 &tracing_dyn_info_fops
);
3611 pr_warning("Could not create debugfs "
3612 "'dyn_ftrace_total_info' entry\n");
3614 #ifdef CONFIG_SYSPROF_TRACER
3615 init_tracer_sysprof_debugfs(d_tracer
);
3620 int trace_vprintk(unsigned long ip
, int depth
, const char *fmt
, va_list args
)
3622 static DEFINE_SPINLOCK(trace_buf_lock
);
3623 static char trace_buf
[TRACE_BUF_SIZE
];
3625 struct ring_buffer_event
*event
;
3626 struct trace_array
*tr
= &global_trace
;
3627 struct trace_array_cpu
*data
;
3628 int cpu
, len
= 0, size
, pc
;
3629 struct print_entry
*entry
;
3630 unsigned long irq_flags
;
3632 if (tracing_disabled
|| tracing_selftest_running
)
3635 pc
= preempt_count();
3636 preempt_disable_notrace();
3637 cpu
= raw_smp_processor_id();
3638 data
= tr
->data
[cpu
];
3640 if (unlikely(atomic_read(&data
->disabled
)))
3643 pause_graph_tracing();
3644 spin_lock_irqsave(&trace_buf_lock
, irq_flags
);
3645 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
3647 len
= min(len
, TRACE_BUF_SIZE
-1);
3650 size
= sizeof(*entry
) + len
+ 1;
3651 event
= ring_buffer_lock_reserve(tr
->buffer
, size
, &irq_flags
);
3654 entry
= ring_buffer_event_data(event
);
3655 tracing_generic_entry_update(&entry
->ent
, irq_flags
, pc
);
3656 entry
->ent
.type
= TRACE_PRINT
;
3658 entry
->depth
= depth
;
3660 memcpy(&entry
->buf
, trace_buf
, len
);
3661 entry
->buf
[len
] = 0;
3662 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
3665 spin_unlock_irqrestore(&trace_buf_lock
, irq_flags
);
3666 unpause_graph_tracing();
3668 preempt_enable_notrace();
3672 EXPORT_SYMBOL_GPL(trace_vprintk
);
3674 int __ftrace_printk(unsigned long ip
, const char *fmt
, ...)
3679 if (!(trace_flags
& TRACE_ITER_PRINTK
))
3683 ret
= trace_vprintk(ip
, task_curr_ret_stack(current
), fmt
, ap
);
3687 EXPORT_SYMBOL_GPL(__ftrace_printk
);
3689 static int trace_panic_handler(struct notifier_block
*this,
3690 unsigned long event
, void *unused
)
3692 if (ftrace_dump_on_oops
)
3697 static struct notifier_block trace_panic_notifier
= {
3698 .notifier_call
= trace_panic_handler
,
3700 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
3703 static int trace_die_handler(struct notifier_block
*self
,
3709 if (ftrace_dump_on_oops
)
3718 static struct notifier_block trace_die_notifier
= {
3719 .notifier_call
= trace_die_handler
,
3724 * printk is set to max of 1024, we really don't need it that big.
3725 * Nothing should be printing 1000 characters anyway.
3727 #define TRACE_MAX_PRINT 1000
3730 * Define here KERN_TRACE so that we have one place to modify
3731 * it if we decide to change what log level the ftrace dump
3734 #define KERN_TRACE KERN_INFO
3737 trace_printk_seq(struct trace_seq
*s
)
3739 /* Probably should print a warning here. */
3743 /* should be zero ended, but we are paranoid. */
3744 s
->buffer
[s
->len
] = 0;
3746 printk(KERN_TRACE
"%s", s
->buffer
);
3751 void ftrace_dump(void)
3753 static DEFINE_SPINLOCK(ftrace_dump_lock
);
3754 /* use static because iter can be a bit big for the stack */
3755 static struct trace_iterator iter
;
3756 static cpumask_t mask
;
3757 static int dump_ran
;
3758 unsigned long flags
;
3762 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
3768 /* No turning back! */
3771 for_each_tracing_cpu(cpu
) {
3772 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3775 /* don't look at user memory in panic mode */
3776 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
3778 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
3780 iter
.tr
= &global_trace
;
3781 iter
.trace
= current_trace
;
3784 * We need to stop all tracing on all CPUS to read the
3785 * the next buffer. This is a bit expensive, but is
3786 * not done often. We fill all what we can read,
3787 * and then release the locks again.
3792 while (!trace_empty(&iter
)) {
3795 printk(KERN_TRACE
"---------------------------------\n");
3799 /* reset all but tr, trace, and overruns */
3800 memset(&iter
.seq
, 0,
3801 sizeof(struct trace_iterator
) -
3802 offsetof(struct trace_iterator
, seq
));
3803 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
3806 if (find_next_entry_inc(&iter
) != NULL
) {
3807 print_trace_line(&iter
);
3808 trace_consume(&iter
);
3811 trace_printk_seq(&iter
.seq
);
3815 printk(KERN_TRACE
" (ftrace buffer empty)\n");
3817 printk(KERN_TRACE
"---------------------------------\n");
3820 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
3823 __init
static int tracer_alloc_buffers(void)
3825 struct trace_array_cpu
*data
;
3828 /* TODO: make the number of buffers hot pluggable with CPUS */
3829 tracing_buffer_mask
= cpu_possible_map
;
3831 global_trace
.buffer
= ring_buffer_alloc(trace_buf_size
,
3832 TRACE_BUFFER_FLAGS
);
3833 if (!global_trace
.buffer
) {
3834 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
3838 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
3840 #ifdef CONFIG_TRACER_MAX_TRACE
3841 max_tr
.buffer
= ring_buffer_alloc(trace_buf_size
,
3842 TRACE_BUFFER_FLAGS
);
3843 if (!max_tr
.buffer
) {
3844 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
3846 ring_buffer_free(global_trace
.buffer
);
3849 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
3850 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
3853 /* Allocate the first page for all buffers */
3854 for_each_tracing_cpu(i
) {
3855 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
3856 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
3859 trace_init_cmdlines();
3861 register_tracer(&nop_trace
);
3862 #ifdef CONFIG_BOOT_TRACER
3863 register_tracer(&boot_tracer
);
3864 current_trace
= &boot_tracer
;
3865 current_trace
->init(&global_trace
);
3867 current_trace
= &nop_trace
;
3870 /* All seems OK, enable tracing */
3871 tracing_disabled
= 0;
3873 atomic_notifier_chain_register(&panic_notifier_list
,
3874 &trace_panic_notifier
);
3876 register_die_notifier(&trace_die_notifier
);
3880 early_initcall(tracer_alloc_buffers
);
3881 fs_initcall(tracer_init_debugfs
);