2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/notifier.h>
18 #include <linux/debugfs.h>
19 #include <linux/pagemap.h>
20 #include <linux/hardirq.h>
21 #include <linux/linkage.h>
22 #include <linux/uaccess.h>
23 #include <linux/ftrace.h>
24 #include <linux/module.h>
25 #include <linux/percpu.h>
26 #include <linux/kdebug.h>
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/poll.h>
30 #include <linux/gfp.h>
32 #include <linux/kprobes.h>
33 #include <linux/writeback.h>
35 #include <linux/stacktrace.h>
36 #include <linux/ring_buffer.h>
37 #include <linux/irqflags.h>
41 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
43 unsigned long __read_mostly tracing_max_latency
= (cycle_t
)ULONG_MAX
;
44 unsigned long __read_mostly tracing_thresh
;
48 * Kill all tracing for good (never come back).
49 * It is initialized to 1 but will turn to zero if the initialization
50 * of the tracer is successful. But that is the only place that sets
53 int tracing_disabled
= 1;
55 static DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
57 static inline void ftrace_disable_cpu(void)
60 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
63 static inline void ftrace_enable_cpu(void)
65 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
69 static cpumask_t __read_mostly tracing_buffer_mask
;
71 #define for_each_tracing_cpu(cpu) \
72 for_each_cpu_mask(cpu, tracing_buffer_mask)
75 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
77 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
78 * is set, then ftrace_dump is called. This will output the contents
79 * of the ftrace buffers to the console. This is very useful for
80 * capturing traces that lead to crashes and outputing it to a
83 * It is default off, but you can enable it with either specifying
84 * "ftrace_dump_on_oops" in the kernel command line, or setting
85 * /proc/sys/kernel/ftrace_dump_on_oops to true.
87 int ftrace_dump_on_oops
;
89 static int tracing_set_tracer(char *buf
);
91 static int __init
set_ftrace(char *str
)
93 tracing_set_tracer(str
);
96 __setup("ftrace", set_ftrace
);
98 static int __init
set_ftrace_dump_on_oops(char *str
)
100 ftrace_dump_on_oops
= 1;
103 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
106 ns2usecs(cycle_t nsec
)
113 cycle_t
ftrace_now(int cpu
)
115 u64 ts
= ring_buffer_time_stamp(cpu
);
116 ring_buffer_normalize_time_stamp(cpu
, &ts
);
121 * The global_trace is the descriptor that holds the tracing
122 * buffers for the live tracing. For each CPU, it contains
123 * a link list of pages that will store trace entries. The
124 * page descriptor of the pages in the memory is used to hold
125 * the link list by linking the lru item in the page descriptor
126 * to each of the pages in the buffer per CPU.
128 * For each active CPU there is a data field that holds the
129 * pages for the buffer for that CPU. Each CPU has the same number
130 * of pages allocated for its buffer.
132 static struct trace_array global_trace
;
134 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
137 * The max_tr is used to snapshot the global_trace when a maximum
138 * latency is reached. Some tracers will use this to store a maximum
139 * trace while it continues examining live traces.
141 * The buffers for the max_tr are set up the same as the global_trace.
142 * When a snapshot is taken, the link list of the max_tr is swapped
143 * with the link list of the global_trace and the buffers are reset for
144 * the global_trace so the tracing can continue.
146 static struct trace_array max_tr
;
148 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
150 /* tracer_enabled is used to toggle activation of a tracer */
151 static int tracer_enabled
= 1;
154 * tracing_is_enabled - return tracer_enabled status
156 * This function is used by other tracers to know the status
157 * of the tracer_enabled flag. Tracers may use this function
158 * to know if it should enable their features when starting
159 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
161 int tracing_is_enabled(void)
163 return tracer_enabled
;
166 /* function tracing enabled */
167 int ftrace_function_enabled
;
170 * trace_buf_size is the size in bytes that is allocated
171 * for a buffer. Note, the number of bytes is always rounded
174 * This number is purposely set to a low number of 16384.
175 * If the dump on oops happens, it will be much appreciated
176 * to not have to wait for all that output. Anyway this can be
177 * boot time and run time configurable.
179 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
181 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
183 /* trace_types holds a link list of available tracers. */
184 static struct tracer
*trace_types __read_mostly
;
186 /* current_trace points to the tracer that is currently active */
187 static struct tracer
*current_trace __read_mostly
;
190 * max_tracer_type_len is used to simplify the allocating of
191 * buffers to read userspace tracer names. We keep track of
192 * the longest tracer name registered.
194 static int max_tracer_type_len
;
197 * trace_types_lock is used to protect the trace_types list.
198 * This lock is also used to keep user access serialized.
199 * Accesses from userspace will grab this lock while userspace
200 * activities happen inside the kernel.
202 static DEFINE_MUTEX(trace_types_lock
);
204 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
205 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
207 /* trace_flags holds trace_options default values */
208 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
212 * trace_wake_up - wake up tasks waiting for trace input
214 * Simply wakes up any task that is blocked on the trace_wait
215 * queue. These is used with trace_poll for tasks polling the trace.
217 void trace_wake_up(void)
220 * The runqueue_is_locked() can fail, but this is the best we
223 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
224 wake_up(&trace_wait
);
227 static int __init
set_buf_size(char *str
)
229 unsigned long buf_size
;
234 ret
= strict_strtoul(str
, 0, &buf_size
);
235 /* nr_entries can not be zero */
236 if (ret
< 0 || buf_size
== 0)
238 trace_buf_size
= buf_size
;
241 __setup("trace_buf_size=", set_buf_size
);
243 unsigned long nsecs_to_usecs(unsigned long nsecs
)
248 /* These must match the bit postions in trace_iterator_flags */
249 static const char *trace_options
[] = {
262 #ifdef CONFIG_BRANCH_TRACER
270 * ftrace_max_lock is used to protect the swapping of buffers
271 * when taking a max snapshot. The buffers themselves are
272 * protected by per_cpu spinlocks. But the action of the swap
273 * needs its own lock.
275 * This is defined as a raw_spinlock_t in order to help
276 * with performance when lockdep debugging is enabled.
278 static raw_spinlock_t ftrace_max_lock
=
279 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
282 * Copy the new maximum trace into the separate maximum-trace
283 * structure. (this way the maximum trace is permanently saved,
284 * for later retrieval via /debugfs/tracing/latency_trace)
287 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
289 struct trace_array_cpu
*data
= tr
->data
[cpu
];
292 max_tr
.time_start
= data
->preempt_timestamp
;
294 data
= max_tr
.data
[cpu
];
295 data
->saved_latency
= tracing_max_latency
;
297 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
298 data
->pid
= tsk
->pid
;
299 data
->uid
= tsk
->uid
;
300 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
301 data
->policy
= tsk
->policy
;
302 data
->rt_priority
= tsk
->rt_priority
;
304 /* record this tasks comm */
305 tracing_record_cmdline(current
);
309 * trace_seq_printf - sequence printing of trace information
310 * @s: trace sequence descriptor
311 * @fmt: printf format string
313 * The tracer may use either sequence operations or its own
314 * copy to user routines. To simplify formating of a trace
315 * trace_seq_printf is used to store strings into a special
316 * buffer (@s). Then the output may be either used by
317 * the sequencer or pulled into another buffer.
320 trace_seq_printf(struct trace_seq
*s
, const char *fmt
, ...)
322 int len
= (PAGE_SIZE
- 1) - s
->len
;
330 ret
= vsnprintf(s
->buffer
+ s
->len
, len
, fmt
, ap
);
333 /* If we can't write it all, don't bother writing anything */
343 * trace_seq_puts - trace sequence printing of simple string
344 * @s: trace sequence descriptor
345 * @str: simple string to record
347 * The tracer may use either the sequence operations or its own
348 * copy to user routines. This function records a simple string
349 * into a special buffer (@s) for later retrieval by a sequencer
350 * or other mechanism.
353 trace_seq_puts(struct trace_seq
*s
, const char *str
)
355 int len
= strlen(str
);
357 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
360 memcpy(s
->buffer
+ s
->len
, str
, len
);
367 trace_seq_putc(struct trace_seq
*s
, unsigned char c
)
369 if (s
->len
>= (PAGE_SIZE
- 1))
372 s
->buffer
[s
->len
++] = c
;
378 trace_seq_putmem(struct trace_seq
*s
, void *mem
, size_t len
)
380 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
383 memcpy(s
->buffer
+ s
->len
, mem
, len
);
389 #define MAX_MEMHEX_BYTES 8
390 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
393 trace_seq_putmem_hex(struct trace_seq
*s
, void *mem
, size_t len
)
395 unsigned char hex
[HEX_CHARS
];
396 unsigned char *data
= mem
;
400 for (i
= 0, j
= 0; i
< len
; i
++) {
402 for (i
= len
-1, j
= 0; i
>= 0; i
--) {
404 hex
[j
++] = hex_asc_hi(data
[i
]);
405 hex
[j
++] = hex_asc_lo(data
[i
]);
409 return trace_seq_putmem(s
, hex
, j
);
413 trace_seq_reset(struct trace_seq
*s
)
419 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
424 if (s
->len
<= s
->readpos
)
427 len
= s
->len
- s
->readpos
;
430 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
439 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
441 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
444 seq_puts(m
, s
->buffer
);
450 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
452 * @tsk: the task with the latency
453 * @cpu: The cpu that initiated the trace.
455 * Flip the buffers between the @tr and the max_tr and record information
456 * about which task was the cause of this latency.
459 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
461 struct ring_buffer
*buf
= tr
->buffer
;
463 WARN_ON_ONCE(!irqs_disabled());
464 __raw_spin_lock(&ftrace_max_lock
);
466 tr
->buffer
= max_tr
.buffer
;
469 ftrace_disable_cpu();
470 ring_buffer_reset(tr
->buffer
);
473 __update_max_tr(tr
, tsk
, cpu
);
474 __raw_spin_unlock(&ftrace_max_lock
);
478 * update_max_tr_single - only copy one trace over, and reset the rest
480 * @tsk - task with the latency
481 * @cpu - the cpu of the buffer to copy.
483 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
486 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
490 WARN_ON_ONCE(!irqs_disabled());
491 __raw_spin_lock(&ftrace_max_lock
);
493 ftrace_disable_cpu();
495 ring_buffer_reset(max_tr
.buffer
);
496 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
502 __update_max_tr(tr
, tsk
, cpu
);
503 __raw_spin_unlock(&ftrace_max_lock
);
507 * register_tracer - register a tracer with the ftrace system.
508 * @type - the plugin for the tracer
510 * Register a new plugin tracer.
512 int register_tracer(struct tracer
*type
)
519 pr_info("Tracer must have a name\n");
523 mutex_lock(&trace_types_lock
);
524 for (t
= trace_types
; t
; t
= t
->next
) {
525 if (strcmp(type
->name
, t
->name
) == 0) {
527 pr_info("Trace %s already registered\n",
534 #ifdef CONFIG_FTRACE_STARTUP_TEST
535 if (type
->selftest
) {
536 struct tracer
*saved_tracer
= current_trace
;
537 struct trace_array
*tr
= &global_trace
;
540 * Run a selftest on this tracer.
541 * Here we reset the trace buffer, and set the current
542 * tracer to be this tracer. The tracer can then run some
543 * internal tracing to verify that everything is in order.
544 * If we fail, we do not register this tracer.
546 for_each_tracing_cpu(i
) {
547 tracing_reset(tr
, i
);
549 current_trace
= type
;
550 /* the test is responsible for initializing and enabling */
551 pr_info("Testing tracer %s: ", type
->name
);
552 ret
= type
->selftest(type
, tr
);
553 /* the test is responsible for resetting too */
554 current_trace
= saved_tracer
;
556 printk(KERN_CONT
"FAILED!\n");
559 /* Only reset on passing, to avoid touching corrupted buffers */
560 for_each_tracing_cpu(i
) {
561 tracing_reset(tr
, i
);
563 printk(KERN_CONT
"PASSED\n");
567 type
->next
= trace_types
;
569 len
= strlen(type
->name
);
570 if (len
> max_tracer_type_len
)
571 max_tracer_type_len
= len
;
574 mutex_unlock(&trace_types_lock
);
579 void unregister_tracer(struct tracer
*type
)
584 mutex_lock(&trace_types_lock
);
585 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
589 pr_info("Trace %s not registered\n", type
->name
);
594 if (strlen(type
->name
) != max_tracer_type_len
)
597 max_tracer_type_len
= 0;
598 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
599 len
= strlen((*t
)->name
);
600 if (len
> max_tracer_type_len
)
601 max_tracer_type_len
= len
;
604 mutex_unlock(&trace_types_lock
);
607 void tracing_reset(struct trace_array
*tr
, int cpu
)
609 ftrace_disable_cpu();
610 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
614 #define SAVED_CMDLINES 128
615 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
616 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
617 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
618 static int cmdline_idx
;
619 static DEFINE_SPINLOCK(trace_cmdline_lock
);
621 /* temporary disable recording */
622 atomic_t trace_record_cmdline_disabled __read_mostly
;
624 static void trace_init_cmdlines(void)
626 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
627 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
631 static int trace_stop_count
;
632 static DEFINE_SPINLOCK(tracing_start_lock
);
635 * tracing_start - quick start of the tracer
637 * If tracing is enabled but was stopped by tracing_stop,
638 * this will start the tracer back up.
640 void tracing_start(void)
642 struct ring_buffer
*buffer
;
645 if (tracing_disabled
)
648 spin_lock_irqsave(&tracing_start_lock
, flags
);
649 if (--trace_stop_count
)
652 if (trace_stop_count
< 0) {
653 /* Someone screwed up their debugging */
655 trace_stop_count
= 0;
660 buffer
= global_trace
.buffer
;
662 ring_buffer_record_enable(buffer
);
664 buffer
= max_tr
.buffer
;
666 ring_buffer_record_enable(buffer
);
670 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
674 * tracing_stop - quick stop of the tracer
676 * Light weight way to stop tracing. Use in conjunction with
679 void tracing_stop(void)
681 struct ring_buffer
*buffer
;
685 spin_lock_irqsave(&tracing_start_lock
, flags
);
686 if (trace_stop_count
++)
689 buffer
= global_trace
.buffer
;
691 ring_buffer_record_disable(buffer
);
693 buffer
= max_tr
.buffer
;
695 ring_buffer_record_disable(buffer
);
698 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
701 void trace_stop_cmdline_recording(void);
703 static void trace_save_cmdline(struct task_struct
*tsk
)
708 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
712 * It's not the end of the world if we don't get
713 * the lock, but we also don't want to spin
714 * nor do we want to disable interrupts,
715 * so if we miss here, then better luck next time.
717 if (!spin_trylock(&trace_cmdline_lock
))
720 idx
= map_pid_to_cmdline
[tsk
->pid
];
721 if (idx
>= SAVED_CMDLINES
) {
722 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
724 map
= map_cmdline_to_pid
[idx
];
725 if (map
<= PID_MAX_DEFAULT
)
726 map_pid_to_cmdline
[map
] = (unsigned)-1;
728 map_pid_to_cmdline
[tsk
->pid
] = idx
;
733 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
735 spin_unlock(&trace_cmdline_lock
);
738 static char *trace_find_cmdline(int pid
)
740 char *cmdline
= "<...>";
746 if (pid
> PID_MAX_DEFAULT
)
749 map
= map_pid_to_cmdline
[pid
];
750 if (map
>= SAVED_CMDLINES
)
753 cmdline
= saved_cmdlines
[map
];
759 void tracing_record_cmdline(struct task_struct
*tsk
)
761 if (atomic_read(&trace_record_cmdline_disabled
))
764 trace_save_cmdline(tsk
);
768 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
771 struct task_struct
*tsk
= current
;
773 entry
->preempt_count
= pc
& 0xff;
774 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
776 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
777 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
779 TRACE_FLAG_IRQS_NOSUPPORT
|
781 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
782 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
783 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
787 trace_function(struct trace_array
*tr
, struct trace_array_cpu
*data
,
788 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
791 struct ring_buffer_event
*event
;
792 struct ftrace_entry
*entry
;
793 unsigned long irq_flags
;
795 /* If we are reading the ring buffer, don't trace */
796 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
799 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
803 entry
= ring_buffer_event_data(event
);
804 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
805 entry
->ent
.type
= TRACE_FN
;
807 entry
->parent_ip
= parent_ip
;
808 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
811 #ifdef CONFIG_FUNCTION_RET_TRACER
812 static void __trace_function_return(struct trace_array
*tr
,
813 struct trace_array_cpu
*data
,
814 struct ftrace_retfunc
*trace
,
818 struct ring_buffer_event
*event
;
819 struct ftrace_ret_entry
*entry
;
820 unsigned long irq_flags
;
822 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
825 event
= ring_buffer_lock_reserve(global_trace
.buffer
, sizeof(*entry
),
829 entry
= ring_buffer_event_data(event
);
830 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
831 entry
->ent
.type
= TRACE_FN_RET
;
832 entry
->ip
= trace
->func
;
833 entry
->parent_ip
= trace
->ret
;
834 entry
->rettime
= trace
->rettime
;
835 entry
->calltime
= trace
->calltime
;
836 ring_buffer_unlock_commit(global_trace
.buffer
, event
, irq_flags
);
841 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
842 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
845 if (likely(!atomic_read(&data
->disabled
)))
846 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
849 static void ftrace_trace_stack(struct trace_array
*tr
,
850 struct trace_array_cpu
*data
,
854 #ifdef CONFIG_STACKTRACE
855 struct ring_buffer_event
*event
;
856 struct stack_entry
*entry
;
857 struct stack_trace trace
;
858 unsigned long irq_flags
;
860 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
863 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
867 entry
= ring_buffer_event_data(event
);
868 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
869 entry
->ent
.type
= TRACE_STACK
;
871 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
873 trace
.nr_entries
= 0;
874 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
876 trace
.entries
= entry
->caller
;
878 save_stack_trace(&trace
);
879 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
883 void __trace_stack(struct trace_array
*tr
,
884 struct trace_array_cpu
*data
,
888 ftrace_trace_stack(tr
, data
, flags
, skip
, preempt_count());
892 ftrace_trace_special(void *__tr
, void *__data
,
893 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
896 struct ring_buffer_event
*event
;
897 struct trace_array_cpu
*data
= __data
;
898 struct trace_array
*tr
= __tr
;
899 struct special_entry
*entry
;
900 unsigned long irq_flags
;
902 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
906 entry
= ring_buffer_event_data(event
);
907 tracing_generic_entry_update(&entry
->ent
, 0, pc
);
908 entry
->ent
.type
= TRACE_SPECIAL
;
912 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
913 ftrace_trace_stack(tr
, data
, irq_flags
, 4, pc
);
919 __trace_special(void *__tr
, void *__data
,
920 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
922 ftrace_trace_special(__tr
, __data
, arg1
, arg2
, arg3
, preempt_count());
926 tracing_sched_switch_trace(struct trace_array
*tr
,
927 struct trace_array_cpu
*data
,
928 struct task_struct
*prev
,
929 struct task_struct
*next
,
930 unsigned long flags
, int pc
)
932 struct ring_buffer_event
*event
;
933 struct ctx_switch_entry
*entry
;
934 unsigned long irq_flags
;
936 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
940 entry
= ring_buffer_event_data(event
);
941 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
942 entry
->ent
.type
= TRACE_CTX
;
943 entry
->prev_pid
= prev
->pid
;
944 entry
->prev_prio
= prev
->prio
;
945 entry
->prev_state
= prev
->state
;
946 entry
->next_pid
= next
->pid
;
947 entry
->next_prio
= next
->prio
;
948 entry
->next_state
= next
->state
;
949 entry
->next_cpu
= task_cpu(next
);
950 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
951 ftrace_trace_stack(tr
, data
, flags
, 5, pc
);
955 tracing_sched_wakeup_trace(struct trace_array
*tr
,
956 struct trace_array_cpu
*data
,
957 struct task_struct
*wakee
,
958 struct task_struct
*curr
,
959 unsigned long flags
, int pc
)
961 struct ring_buffer_event
*event
;
962 struct ctx_switch_entry
*entry
;
963 unsigned long irq_flags
;
965 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
969 entry
= ring_buffer_event_data(event
);
970 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
971 entry
->ent
.type
= TRACE_WAKE
;
972 entry
->prev_pid
= curr
->pid
;
973 entry
->prev_prio
= curr
->prio
;
974 entry
->prev_state
= curr
->state
;
975 entry
->next_pid
= wakee
->pid
;
976 entry
->next_prio
= wakee
->prio
;
977 entry
->next_state
= wakee
->state
;
978 entry
->next_cpu
= task_cpu(wakee
);
979 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
980 ftrace_trace_stack(tr
, data
, flags
, 6, pc
);
986 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
988 struct trace_array
*tr
= &global_trace
;
989 struct trace_array_cpu
*data
;
994 if (tracing_disabled
)
997 pc
= preempt_count();
998 local_irq_save(flags
);
999 cpu
= raw_smp_processor_id();
1000 data
= tr
->data
[cpu
];
1002 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1003 ftrace_trace_special(tr
, data
, arg1
, arg2
, arg3
, pc
);
1005 atomic_dec(&data
->disabled
);
1006 local_irq_restore(flags
);
1009 #ifdef CONFIG_FUNCTION_TRACER
1011 function_trace_call_preempt_only(unsigned long ip
, unsigned long parent_ip
)
1013 struct trace_array
*tr
= &global_trace
;
1014 struct trace_array_cpu
*data
;
1015 unsigned long flags
;
1020 if (unlikely(!ftrace_function_enabled
))
1023 pc
= preempt_count();
1024 resched
= ftrace_preempt_disable();
1025 local_save_flags(flags
);
1026 cpu
= raw_smp_processor_id();
1027 data
= tr
->data
[cpu
];
1028 disabled
= atomic_inc_return(&data
->disabled
);
1030 if (likely(disabled
== 1))
1031 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
1033 atomic_dec(&data
->disabled
);
1034 ftrace_preempt_enable(resched
);
1038 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
1040 struct trace_array
*tr
= &global_trace
;
1041 struct trace_array_cpu
*data
;
1042 unsigned long flags
;
1047 if (unlikely(!ftrace_function_enabled
))
1051 * Need to use raw, since this must be called before the
1052 * recursive protection is performed.
1054 raw_local_irq_save(flags
);
1055 cpu
= raw_smp_processor_id();
1056 data
= tr
->data
[cpu
];
1057 disabled
= atomic_inc_return(&data
->disabled
);
1059 if (likely(disabled
== 1)) {
1060 pc
= preempt_count();
1061 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
1064 atomic_dec(&data
->disabled
);
1065 raw_local_irq_restore(flags
);
1068 #ifdef CONFIG_FUNCTION_RET_TRACER
1069 void trace_function_return(struct ftrace_retfunc
*trace
)
1071 struct trace_array
*tr
= &global_trace
;
1072 struct trace_array_cpu
*data
;
1073 unsigned long flags
;
1078 raw_local_irq_save(flags
);
1079 cpu
= raw_smp_processor_id();
1080 data
= tr
->data
[cpu
];
1081 disabled
= atomic_inc_return(&data
->disabled
);
1082 if (likely(disabled
== 1)) {
1083 pc
= preempt_count();
1084 __trace_function_return(tr
, data
, trace
, flags
, pc
);
1086 atomic_dec(&data
->disabled
);
1087 raw_local_irq_restore(flags
);
1089 #endif /* CONFIG_FUNCTION_RET_TRACER */
1091 static struct ftrace_ops trace_ops __read_mostly
=
1093 .func
= function_trace_call
,
1096 void tracing_start_function_trace(void)
1098 ftrace_function_enabled
= 0;
1100 if (trace_flags
& TRACE_ITER_PREEMPTONLY
)
1101 trace_ops
.func
= function_trace_call_preempt_only
;
1103 trace_ops
.func
= function_trace_call
;
1105 register_ftrace_function(&trace_ops
);
1106 ftrace_function_enabled
= 1;
1109 void tracing_stop_function_trace(void)
1111 ftrace_function_enabled
= 0;
1112 unregister_ftrace_function(&trace_ops
);
1116 enum trace_file_type
{
1117 TRACE_FILE_LAT_FMT
= 1,
1118 TRACE_FILE_ANNOTATE
= 2,
1121 static void trace_iterator_increment(struct trace_iterator
*iter
, int cpu
)
1123 /* Don't allow ftrace to trace into the ring buffers */
1124 ftrace_disable_cpu();
1127 if (iter
->buffer_iter
[iter
->cpu
])
1128 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1130 ftrace_enable_cpu();
1133 static struct trace_entry
*
1134 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1136 struct ring_buffer_event
*event
;
1137 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1139 /* Don't allow ftrace to trace into the ring buffers */
1140 ftrace_disable_cpu();
1143 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1145 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1147 ftrace_enable_cpu();
1149 return event
? ring_buffer_event_data(event
) : NULL
;
1152 static struct trace_entry
*
1153 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1155 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1156 struct trace_entry
*ent
, *next
= NULL
;
1157 u64 next_ts
= 0, ts
;
1161 for_each_tracing_cpu(cpu
) {
1163 if (ring_buffer_empty_cpu(buffer
, cpu
))
1166 ent
= peek_next_entry(iter
, cpu
, &ts
);
1169 * Pick the entry with the smallest timestamp:
1171 if (ent
&& (!next
|| ts
< next_ts
)) {
1179 *ent_cpu
= next_cpu
;
1187 /* Find the next real entry, without updating the iterator itself */
1188 static struct trace_entry
*
1189 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1191 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1194 /* Find the next real entry, and increment the iterator to the next entry */
1195 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1197 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1200 trace_iterator_increment(iter
, iter
->cpu
);
1202 return iter
->ent
? iter
: NULL
;
1205 static void trace_consume(struct trace_iterator
*iter
)
1207 /* Don't allow ftrace to trace into the ring buffers */
1208 ftrace_disable_cpu();
1209 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1210 ftrace_enable_cpu();
1213 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1215 struct trace_iterator
*iter
= m
->private;
1221 /* can't go backwards */
1226 ent
= find_next_entry_inc(iter
);
1230 while (ent
&& iter
->idx
< i
)
1231 ent
= find_next_entry_inc(iter
);
1238 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1240 struct trace_iterator
*iter
= m
->private;
1245 mutex_lock(&trace_types_lock
);
1247 if (!current_trace
|| current_trace
!= iter
->trace
) {
1248 mutex_unlock(&trace_types_lock
);
1252 atomic_inc(&trace_record_cmdline_disabled
);
1254 if (*pos
!= iter
->pos
) {
1259 ftrace_disable_cpu();
1261 for_each_tracing_cpu(cpu
) {
1262 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1265 ftrace_enable_cpu();
1267 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1272 p
= s_next(m
, p
, &l
);
1278 static void s_stop(struct seq_file
*m
, void *p
)
1280 atomic_dec(&trace_record_cmdline_disabled
);
1281 mutex_unlock(&trace_types_lock
);
1284 #ifdef CONFIG_KRETPROBES
1285 static inline const char *kretprobed(const char *name
)
1287 static const char tramp_name
[] = "kretprobe_trampoline";
1288 int size
= sizeof(tramp_name
);
1290 if (strncmp(tramp_name
, name
, size
) == 0)
1291 return "[unknown/kretprobe'd]";
1295 static inline const char *kretprobed(const char *name
)
1299 #endif /* CONFIG_KRETPROBES */
1302 seq_print_sym_short(struct trace_seq
*s
, const char *fmt
, unsigned long address
)
1304 #ifdef CONFIG_KALLSYMS
1305 char str
[KSYM_SYMBOL_LEN
];
1308 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
1310 name
= kretprobed(str
);
1312 return trace_seq_printf(s
, fmt
, name
);
1318 seq_print_sym_offset(struct trace_seq
*s
, const char *fmt
,
1319 unsigned long address
)
1321 #ifdef CONFIG_KALLSYMS
1322 char str
[KSYM_SYMBOL_LEN
];
1325 sprint_symbol(str
, address
);
1326 name
= kretprobed(str
);
1328 return trace_seq_printf(s
, fmt
, name
);
1333 #ifndef CONFIG_64BIT
1334 # define IP_FMT "%08lx"
1336 # define IP_FMT "%016lx"
1340 seq_print_ip_sym(struct trace_seq
*s
, unsigned long ip
, unsigned long sym_flags
)
1345 return trace_seq_printf(s
, "0");
1347 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
1348 ret
= seq_print_sym_offset(s
, "%s", ip
);
1350 ret
= seq_print_sym_short(s
, "%s", ip
);
1355 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
1356 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
1360 static void print_lat_help_header(struct seq_file
*m
)
1362 seq_puts(m
, "# _------=> CPU# \n");
1363 seq_puts(m
, "# / _-----=> irqs-off \n");
1364 seq_puts(m
, "# | / _----=> need-resched \n");
1365 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1366 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1367 seq_puts(m
, "# |||| / \n");
1368 seq_puts(m
, "# ||||| delay \n");
1369 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1370 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1373 static void print_func_help_header(struct seq_file
*m
)
1375 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1376 seq_puts(m
, "# | | | | |\n");
1381 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1383 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1384 struct trace_array
*tr
= iter
->tr
;
1385 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1386 struct tracer
*type
= current_trace
;
1387 unsigned long total
;
1388 unsigned long entries
;
1389 const char *name
= "preemption";
1394 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1396 ring_buffer_overruns(iter
->tr
->buffer
);
1398 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1400 seq_puts(m
, "-----------------------------------"
1401 "---------------------------------\n");
1402 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1403 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1404 nsecs_to_usecs(data
->saved_latency
),
1408 #if defined(CONFIG_PREEMPT_NONE)
1410 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1412 #elif defined(CONFIG_PREEMPT)
1417 /* These are reserved for later use */
1420 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1424 seq_puts(m
, " -----------------\n");
1425 seq_printf(m
, " | task: %.16s-%d "
1426 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1427 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1428 data
->policy
, data
->rt_priority
);
1429 seq_puts(m
, " -----------------\n");
1431 if (data
->critical_start
) {
1432 seq_puts(m
, " => started at: ");
1433 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1434 trace_print_seq(m
, &iter
->seq
);
1435 seq_puts(m
, "\n => ended at: ");
1436 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1437 trace_print_seq(m
, &iter
->seq
);
1445 lat_print_generic(struct trace_seq
*s
, struct trace_entry
*entry
, int cpu
)
1447 int hardirq
, softirq
;
1450 comm
= trace_find_cmdline(entry
->pid
);
1452 trace_seq_printf(s
, "%8.8s-%-5d ", comm
, entry
->pid
);
1453 trace_seq_printf(s
, "%3d", cpu
);
1454 trace_seq_printf(s
, "%c%c",
1455 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
1456 (entry
->flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ? 'X' : '.',
1457 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
1459 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
1460 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
1461 if (hardirq
&& softirq
) {
1462 trace_seq_putc(s
, 'H');
1465 trace_seq_putc(s
, 'h');
1468 trace_seq_putc(s
, 's');
1470 trace_seq_putc(s
, '.');
1474 if (entry
->preempt_count
)
1475 trace_seq_printf(s
, "%x", entry
->preempt_count
);
1477 trace_seq_puts(s
, ".");
1480 unsigned long preempt_mark_thresh
= 100;
1483 lat_print_timestamp(struct trace_seq
*s
, u64 abs_usecs
,
1484 unsigned long rel_usecs
)
1486 trace_seq_printf(s
, " %4lldus", abs_usecs
);
1487 if (rel_usecs
> preempt_mark_thresh
)
1488 trace_seq_puts(s
, "!: ");
1489 else if (rel_usecs
> 1)
1490 trace_seq_puts(s
, "+: ");
1492 trace_seq_puts(s
, " : ");
1495 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
1498 * The message is supposed to contain an ending newline.
1499 * If the printing stops prematurely, try to add a newline of our own.
1501 void trace_seq_print_cont(struct trace_seq
*s
, struct trace_iterator
*iter
)
1503 struct trace_entry
*ent
;
1504 struct trace_field_cont
*cont
;
1507 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1508 if (!ent
|| ent
->type
!= TRACE_CONT
) {
1509 trace_seq_putc(s
, '\n');
1514 cont
= (struct trace_field_cont
*)ent
;
1516 ok
= (trace_seq_printf(s
, "%s", cont
->buf
) > 0);
1518 ftrace_disable_cpu();
1520 if (iter
->buffer_iter
[iter
->cpu
])
1521 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1523 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, NULL
);
1525 ftrace_enable_cpu();
1527 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1528 } while (ent
&& ent
->type
== TRACE_CONT
);
1531 trace_seq_putc(s
, '\n');
1534 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1536 struct trace_seq
*s
= &iter
->seq
;
1538 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1541 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1544 if (cpu_isset(iter
->cpu
, iter
->started
))
1547 cpu_set(iter
->cpu
, iter
->started
);
1548 trace_seq_printf(s
, "##### CPU %u buffer started ####\n", iter
->cpu
);
1551 static enum print_line_t
1552 print_lat_fmt(struct trace_iterator
*iter
, unsigned int trace_idx
, int cpu
)
1554 struct trace_seq
*s
= &iter
->seq
;
1555 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1556 struct trace_entry
*next_entry
;
1557 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
1558 struct trace_entry
*entry
= iter
->ent
;
1559 unsigned long abs_usecs
;
1560 unsigned long rel_usecs
;
1567 if (entry
->type
== TRACE_CONT
)
1568 return TRACE_TYPE_HANDLED
;
1570 test_cpu_buff_start(iter
);
1572 next_entry
= find_next_entry(iter
, NULL
, &next_ts
);
1575 rel_usecs
= ns2usecs(next_ts
- iter
->ts
);
1576 abs_usecs
= ns2usecs(iter
->ts
- iter
->tr
->time_start
);
1579 comm
= trace_find_cmdline(entry
->pid
);
1580 trace_seq_printf(s
, "%16s %5d %3d %d %08x %08x [%08lx]"
1581 " %ld.%03ldms (+%ld.%03ldms): ",
1583 entry
->pid
, cpu
, entry
->flags
,
1584 entry
->preempt_count
, trace_idx
,
1587 abs_usecs
% 1000, rel_usecs
/1000,
1590 lat_print_generic(s
, entry
, cpu
);
1591 lat_print_timestamp(s
, abs_usecs
, rel_usecs
);
1593 switch (entry
->type
) {
1595 struct ftrace_entry
*field
;
1597 trace_assign_type(field
, entry
);
1599 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1600 trace_seq_puts(s
, " (");
1601 seq_print_ip_sym(s
, field
->parent_ip
, sym_flags
);
1602 trace_seq_puts(s
, ")\n");
1607 struct ctx_switch_entry
*field
;
1609 trace_assign_type(field
, entry
);
1611 T
= field
->next_state
< sizeof(state_to_char
) ?
1612 state_to_char
[field
->next_state
] : 'X';
1614 state
= field
->prev_state
?
1615 __ffs(field
->prev_state
) + 1 : 0;
1616 S
= state
< sizeof(state_to_char
) - 1 ? state_to_char
[state
] : 'X';
1617 comm
= trace_find_cmdline(field
->next_pid
);
1618 trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1621 S
, entry
->type
== TRACE_CTX
? "==>" : " +",
1628 case TRACE_SPECIAL
: {
1629 struct special_entry
*field
;
1631 trace_assign_type(field
, entry
);
1633 trace_seq_printf(s
, "# %ld %ld %ld\n",
1640 struct stack_entry
*field
;
1642 trace_assign_type(field
, entry
);
1644 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1646 trace_seq_puts(s
, " <= ");
1647 seq_print_ip_sym(s
, field
->caller
[i
], sym_flags
);
1649 trace_seq_puts(s
, "\n");
1653 struct print_entry
*field
;
1655 trace_assign_type(field
, entry
);
1657 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1658 trace_seq_printf(s
, ": %s", field
->buf
);
1659 if (entry
->flags
& TRACE_FLAG_CONT
)
1660 trace_seq_print_cont(s
, iter
);
1663 case TRACE_BRANCH
: {
1664 struct trace_branch
*field
;
1666 trace_assign_type(field
, entry
);
1668 trace_seq_printf(s
, "[%s] %s:%s:%d\n",
1669 field
->correct
? " ok " : " MISS ",
1676 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
1678 return TRACE_TYPE_HANDLED
;
1681 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1683 struct trace_seq
*s
= &iter
->seq
;
1684 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1685 struct trace_entry
*entry
;
1686 unsigned long usec_rem
;
1687 unsigned long long t
;
1696 if (entry
->type
== TRACE_CONT
)
1697 return TRACE_TYPE_HANDLED
;
1699 test_cpu_buff_start(iter
);
1701 comm
= trace_find_cmdline(iter
->ent
->pid
);
1703 t
= ns2usecs(iter
->ts
);
1704 usec_rem
= do_div(t
, 1000000ULL);
1705 secs
= (unsigned long)t
;
1707 ret
= trace_seq_printf(s
, "%16s-%-5d ", comm
, entry
->pid
);
1709 return TRACE_TYPE_PARTIAL_LINE
;
1710 ret
= trace_seq_printf(s
, "[%03d] ", iter
->cpu
);
1712 return TRACE_TYPE_PARTIAL_LINE
;
1713 ret
= trace_seq_printf(s
, "%5lu.%06lu: ", secs
, usec_rem
);
1715 return TRACE_TYPE_PARTIAL_LINE
;
1717 switch (entry
->type
) {
1719 struct ftrace_entry
*field
;
1721 trace_assign_type(field
, entry
);
1723 ret
= seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1725 return TRACE_TYPE_PARTIAL_LINE
;
1726 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
1728 ret
= trace_seq_printf(s
, " <-");
1730 return TRACE_TYPE_PARTIAL_LINE
;
1731 ret
= seq_print_ip_sym(s
,
1735 return TRACE_TYPE_PARTIAL_LINE
;
1737 ret
= trace_seq_printf(s
, "\n");
1739 return TRACE_TYPE_PARTIAL_LINE
;
1744 struct ctx_switch_entry
*field
;
1746 trace_assign_type(field
, entry
);
1748 S
= field
->prev_state
< sizeof(state_to_char
) ?
1749 state_to_char
[field
->prev_state
] : 'X';
1750 T
= field
->next_state
< sizeof(state_to_char
) ?
1751 state_to_char
[field
->next_state
] : 'X';
1752 ret
= trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1756 entry
->type
== TRACE_CTX
? "==>" : " +",
1762 return TRACE_TYPE_PARTIAL_LINE
;
1765 case TRACE_SPECIAL
: {
1766 struct special_entry
*field
;
1768 trace_assign_type(field
, entry
);
1770 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1775 return TRACE_TYPE_PARTIAL_LINE
;
1779 struct stack_entry
*field
;
1781 trace_assign_type(field
, entry
);
1783 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1785 ret
= trace_seq_puts(s
, " <= ");
1787 return TRACE_TYPE_PARTIAL_LINE
;
1789 ret
= seq_print_ip_sym(s
, field
->caller
[i
],
1792 return TRACE_TYPE_PARTIAL_LINE
;
1794 ret
= trace_seq_puts(s
, "\n");
1796 return TRACE_TYPE_PARTIAL_LINE
;
1800 struct print_entry
*field
;
1802 trace_assign_type(field
, entry
);
1804 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1805 trace_seq_printf(s
, ": %s", field
->buf
);
1806 if (entry
->flags
& TRACE_FLAG_CONT
)
1807 trace_seq_print_cont(s
, iter
);
1810 case TRACE_FN_RET
: {
1811 return print_return_function(iter
);
1814 case TRACE_BRANCH
: {
1815 struct trace_branch
*field
;
1817 trace_assign_type(field
, entry
);
1819 trace_seq_printf(s
, "[%s] %s:%s:%d\n",
1820 field
->correct
? " ok " : " MISS ",
1827 return TRACE_TYPE_HANDLED
;
1830 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1832 struct trace_seq
*s
= &iter
->seq
;
1833 struct trace_entry
*entry
;
1839 if (entry
->type
== TRACE_CONT
)
1840 return TRACE_TYPE_HANDLED
;
1842 ret
= trace_seq_printf(s
, "%d %d %llu ",
1843 entry
->pid
, iter
->cpu
, iter
->ts
);
1845 return TRACE_TYPE_PARTIAL_LINE
;
1847 switch (entry
->type
) {
1849 struct ftrace_entry
*field
;
1851 trace_assign_type(field
, entry
);
1853 ret
= trace_seq_printf(s
, "%x %x\n",
1857 return TRACE_TYPE_PARTIAL_LINE
;
1862 struct ctx_switch_entry
*field
;
1864 trace_assign_type(field
, entry
);
1866 S
= field
->prev_state
< sizeof(state_to_char
) ?
1867 state_to_char
[field
->prev_state
] : 'X';
1868 T
= field
->next_state
< sizeof(state_to_char
) ?
1869 state_to_char
[field
->next_state
] : 'X';
1870 if (entry
->type
== TRACE_WAKE
)
1872 ret
= trace_seq_printf(s
, "%d %d %c %d %d %d %c\n",
1881 return TRACE_TYPE_PARTIAL_LINE
;
1886 struct special_entry
*field
;
1888 trace_assign_type(field
, entry
);
1890 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1895 return TRACE_TYPE_PARTIAL_LINE
;
1899 struct print_entry
*field
;
1901 trace_assign_type(field
, entry
);
1903 trace_seq_printf(s
, "# %lx %s", field
->ip
, field
->buf
);
1904 if (entry
->flags
& TRACE_FLAG_CONT
)
1905 trace_seq_print_cont(s
, iter
);
1909 return TRACE_TYPE_HANDLED
;
1912 #define SEQ_PUT_FIELD_RET(s, x) \
1914 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1918 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1920 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1921 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1925 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1927 struct trace_seq
*s
= &iter
->seq
;
1928 unsigned char newline
= '\n';
1929 struct trace_entry
*entry
;
1934 if (entry
->type
== TRACE_CONT
)
1935 return TRACE_TYPE_HANDLED
;
1937 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1938 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1939 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1941 switch (entry
->type
) {
1943 struct ftrace_entry
*field
;
1945 trace_assign_type(field
, entry
);
1947 SEQ_PUT_HEX_FIELD_RET(s
, field
->ip
);
1948 SEQ_PUT_HEX_FIELD_RET(s
, field
->parent_ip
);
1953 struct ctx_switch_entry
*field
;
1955 trace_assign_type(field
, entry
);
1957 S
= field
->prev_state
< sizeof(state_to_char
) ?
1958 state_to_char
[field
->prev_state
] : 'X';
1959 T
= field
->next_state
< sizeof(state_to_char
) ?
1960 state_to_char
[field
->next_state
] : 'X';
1961 if (entry
->type
== TRACE_WAKE
)
1963 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_pid
);
1964 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_prio
);
1965 SEQ_PUT_HEX_FIELD_RET(s
, S
);
1966 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_cpu
);
1967 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_pid
);
1968 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_prio
);
1969 SEQ_PUT_HEX_FIELD_RET(s
, T
);
1974 struct special_entry
*field
;
1976 trace_assign_type(field
, entry
);
1978 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg1
);
1979 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg2
);
1980 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg3
);
1984 SEQ_PUT_FIELD_RET(s
, newline
);
1986 return TRACE_TYPE_HANDLED
;
1989 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1991 struct trace_seq
*s
= &iter
->seq
;
1992 struct trace_entry
*entry
;
1996 if (entry
->type
== TRACE_CONT
)
1997 return TRACE_TYPE_HANDLED
;
1999 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
2000 SEQ_PUT_FIELD_RET(s
, entry
->cpu
);
2001 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
2003 switch (entry
->type
) {
2005 struct ftrace_entry
*field
;
2007 trace_assign_type(field
, entry
);
2009 SEQ_PUT_FIELD_RET(s
, field
->ip
);
2010 SEQ_PUT_FIELD_RET(s
, field
->parent_ip
);
2014 struct ctx_switch_entry
*field
;
2016 trace_assign_type(field
, entry
);
2018 SEQ_PUT_FIELD_RET(s
, field
->prev_pid
);
2019 SEQ_PUT_FIELD_RET(s
, field
->prev_prio
);
2020 SEQ_PUT_FIELD_RET(s
, field
->prev_state
);
2021 SEQ_PUT_FIELD_RET(s
, field
->next_pid
);
2022 SEQ_PUT_FIELD_RET(s
, field
->next_prio
);
2023 SEQ_PUT_FIELD_RET(s
, field
->next_state
);
2028 struct special_entry
*field
;
2030 trace_assign_type(field
, entry
);
2032 SEQ_PUT_FIELD_RET(s
, field
->arg1
);
2033 SEQ_PUT_FIELD_RET(s
, field
->arg2
);
2034 SEQ_PUT_FIELD_RET(s
, field
->arg3
);
2041 static int trace_empty(struct trace_iterator
*iter
)
2045 for_each_tracing_cpu(cpu
) {
2046 if (iter
->buffer_iter
[cpu
]) {
2047 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
2050 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
2058 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
2060 enum print_line_t ret
;
2062 if (iter
->trace
&& iter
->trace
->print_line
) {
2063 ret
= iter
->trace
->print_line(iter
);
2064 if (ret
!= TRACE_TYPE_UNHANDLED
)
2068 if (trace_flags
& TRACE_ITER_BIN
)
2069 return print_bin_fmt(iter
);
2071 if (trace_flags
& TRACE_ITER_HEX
)
2072 return print_hex_fmt(iter
);
2074 if (trace_flags
& TRACE_ITER_RAW
)
2075 return print_raw_fmt(iter
);
2077 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
2078 return print_lat_fmt(iter
, iter
->idx
, iter
->cpu
);
2080 return print_trace_fmt(iter
);
2083 static int s_show(struct seq_file
*m
, void *v
)
2085 struct trace_iterator
*iter
= v
;
2087 if (iter
->ent
== NULL
) {
2089 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
2092 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2093 /* print nothing if the buffers are empty */
2094 if (trace_empty(iter
))
2096 print_trace_header(m
, iter
);
2097 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2098 print_lat_help_header(m
);
2100 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2101 print_func_help_header(m
);
2104 print_trace_line(iter
);
2105 trace_print_seq(m
, &iter
->seq
);
2111 static struct seq_operations tracer_seq_ops
= {
2118 static struct trace_iterator
*
2119 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
2121 struct trace_iterator
*iter
;
2125 if (tracing_disabled
) {
2130 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2136 mutex_lock(&trace_types_lock
);
2137 if (current_trace
&& current_trace
->print_max
)
2140 iter
->tr
= inode
->i_private
;
2141 iter
->trace
= current_trace
;
2144 /* Annotate start of buffers if we had overruns */
2145 if (ring_buffer_overruns(iter
->tr
->buffer
))
2146 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
2149 for_each_tracing_cpu(cpu
) {
2151 iter
->buffer_iter
[cpu
] =
2152 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2154 if (!iter
->buffer_iter
[cpu
])
2158 /* TODO stop tracer */
2159 *ret
= seq_open(file
, &tracer_seq_ops
);
2163 m
= file
->private_data
;
2166 /* stop the trace while dumping */
2169 if (iter
->trace
&& iter
->trace
->open
)
2170 iter
->trace
->open(iter
);
2172 mutex_unlock(&trace_types_lock
);
2178 for_each_tracing_cpu(cpu
) {
2179 if (iter
->buffer_iter
[cpu
])
2180 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2182 mutex_unlock(&trace_types_lock
);
2184 return ERR_PTR(-ENOMEM
);
2187 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2189 if (tracing_disabled
)
2192 filp
->private_data
= inode
->i_private
;
2196 int tracing_release(struct inode
*inode
, struct file
*file
)
2198 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2199 struct trace_iterator
*iter
= m
->private;
2202 mutex_lock(&trace_types_lock
);
2203 for_each_tracing_cpu(cpu
) {
2204 if (iter
->buffer_iter
[cpu
])
2205 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2208 if (iter
->trace
&& iter
->trace
->close
)
2209 iter
->trace
->close(iter
);
2211 /* reenable tracing if it was previously enabled */
2213 mutex_unlock(&trace_types_lock
);
2215 seq_release(inode
, file
);
2220 static int tracing_open(struct inode
*inode
, struct file
*file
)
2224 __tracing_open(inode
, file
, &ret
);
2229 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
2231 struct trace_iterator
*iter
;
2234 iter
= __tracing_open(inode
, file
, &ret
);
2237 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2244 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2246 struct tracer
*t
= m
->private;
2258 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2260 struct tracer
*t
= m
->private;
2263 mutex_lock(&trace_types_lock
);
2264 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2270 static void t_stop(struct seq_file
*m
, void *p
)
2272 mutex_unlock(&trace_types_lock
);
2275 static int t_show(struct seq_file
*m
, void *v
)
2277 struct tracer
*t
= v
;
2282 seq_printf(m
, "%s", t
->name
);
2291 static struct seq_operations show_traces_seq_ops
= {
2298 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2302 if (tracing_disabled
)
2305 ret
= seq_open(file
, &show_traces_seq_ops
);
2307 struct seq_file
*m
= file
->private_data
;
2308 m
->private = trace_types
;
2314 static struct file_operations tracing_fops
= {
2315 .open
= tracing_open
,
2317 .llseek
= seq_lseek
,
2318 .release
= tracing_release
,
2321 static struct file_operations tracing_lt_fops
= {
2322 .open
= tracing_lt_open
,
2324 .llseek
= seq_lseek
,
2325 .release
= tracing_release
,
2328 static struct file_operations show_traces_fops
= {
2329 .open
= show_traces_open
,
2331 .release
= seq_release
,
2335 * Only trace on a CPU if the bitmask is set:
2337 static cpumask_t tracing_cpumask
= CPU_MASK_ALL
;
2340 * When tracing/tracing_cpu_mask is modified then this holds
2341 * the new bitmask we are about to install:
2343 static cpumask_t tracing_cpumask_new
;
2346 * The tracer itself will not take this lock, but still we want
2347 * to provide a consistent cpumask to user-space:
2349 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2352 * Temporary storage for the character representation of the
2353 * CPU bitmask (and one more byte for the newline):
2355 static char mask_str
[NR_CPUS
+ 1];
2358 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2359 size_t count
, loff_t
*ppos
)
2363 mutex_lock(&tracing_cpumask_update_lock
);
2365 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2366 if (count
- len
< 2) {
2370 len
+= sprintf(mask_str
+ len
, "\n");
2371 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2374 mutex_unlock(&tracing_cpumask_update_lock
);
2380 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2381 size_t count
, loff_t
*ppos
)
2385 mutex_lock(&tracing_cpumask_update_lock
);
2386 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2390 raw_local_irq_disable();
2391 __raw_spin_lock(&ftrace_max_lock
);
2392 for_each_tracing_cpu(cpu
) {
2394 * Increase/decrease the disabled counter if we are
2395 * about to flip a bit in the cpumask:
2397 if (cpu_isset(cpu
, tracing_cpumask
) &&
2398 !cpu_isset(cpu
, tracing_cpumask_new
)) {
2399 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2401 if (!cpu_isset(cpu
, tracing_cpumask
) &&
2402 cpu_isset(cpu
, tracing_cpumask_new
)) {
2403 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2406 __raw_spin_unlock(&ftrace_max_lock
);
2407 raw_local_irq_enable();
2409 tracing_cpumask
= tracing_cpumask_new
;
2411 mutex_unlock(&tracing_cpumask_update_lock
);
2416 mutex_unlock(&tracing_cpumask_update_lock
);
2421 static struct file_operations tracing_cpumask_fops
= {
2422 .open
= tracing_open_generic
,
2423 .read
= tracing_cpumask_read
,
2424 .write
= tracing_cpumask_write
,
2428 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2429 size_t cnt
, loff_t
*ppos
)
2436 /* calulate max size */
2437 for (i
= 0; trace_options
[i
]; i
++) {
2438 len
+= strlen(trace_options
[i
]);
2439 len
+= 3; /* "no" and space */
2442 /* +2 for \n and \0 */
2443 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2447 for (i
= 0; trace_options
[i
]; i
++) {
2448 if (trace_flags
& (1 << i
))
2449 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
2451 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
2454 r
+= sprintf(buf
+ r
, "\n");
2455 WARN_ON(r
>= len
+ 2);
2457 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2465 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2466 size_t cnt
, loff_t
*ppos
)
2473 if (cnt
>= sizeof(buf
))
2476 if (copy_from_user(&buf
, ubuf
, cnt
))
2481 if (strncmp(buf
, "no", 2) == 0) {
2486 for (i
= 0; trace_options
[i
]; i
++) {
2487 int len
= strlen(trace_options
[i
]);
2489 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2491 trace_flags
&= ~(1 << i
);
2493 trace_flags
|= (1 << i
);
2498 * If no option could be set, return an error:
2500 if (!trace_options
[i
])
2508 static struct file_operations tracing_iter_fops
= {
2509 .open
= tracing_open_generic
,
2510 .read
= tracing_trace_options_read
,
2511 .write
= tracing_trace_options_write
,
2514 static const char readme_msg
[] =
2515 "tracing mini-HOWTO:\n\n"
2517 "# mount -t debugfs nodev /debug\n\n"
2518 "# cat /debug/tracing/available_tracers\n"
2519 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2520 "# cat /debug/tracing/current_tracer\n"
2522 "# echo sched_switch > /debug/tracing/current_tracer\n"
2523 "# cat /debug/tracing/current_tracer\n"
2525 "# cat /debug/tracing/trace_options\n"
2526 "noprint-parent nosym-offset nosym-addr noverbose\n"
2527 "# echo print-parent > /debug/tracing/trace_options\n"
2528 "# echo 1 > /debug/tracing/tracing_enabled\n"
2529 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2530 "echo 0 > /debug/tracing/tracing_enabled\n"
2534 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2535 size_t cnt
, loff_t
*ppos
)
2537 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2538 readme_msg
, strlen(readme_msg
));
2541 static struct file_operations tracing_readme_fops
= {
2542 .open
= tracing_open_generic
,
2543 .read
= tracing_readme_read
,
2547 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2548 size_t cnt
, loff_t
*ppos
)
2553 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2554 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2558 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2559 size_t cnt
, loff_t
*ppos
)
2561 struct trace_array
*tr
= filp
->private_data
;
2566 if (cnt
>= sizeof(buf
))
2569 if (copy_from_user(&buf
, ubuf
, cnt
))
2574 ret
= strict_strtoul(buf
, 10, &val
);
2580 mutex_lock(&trace_types_lock
);
2581 if (tracer_enabled
^ val
) {
2584 if (current_trace
->start
)
2585 current_trace
->start(tr
);
2590 if (current_trace
->stop
)
2591 current_trace
->stop(tr
);
2594 mutex_unlock(&trace_types_lock
);
2602 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2603 size_t cnt
, loff_t
*ppos
)
2605 char buf
[max_tracer_type_len
+2];
2608 mutex_lock(&trace_types_lock
);
2610 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2612 r
= sprintf(buf
, "\n");
2613 mutex_unlock(&trace_types_lock
);
2615 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2618 static int tracing_set_tracer(char *buf
)
2620 struct trace_array
*tr
= &global_trace
;
2624 mutex_lock(&trace_types_lock
);
2625 for (t
= trace_types
; t
; t
= t
->next
) {
2626 if (strcmp(t
->name
, buf
) == 0)
2633 if (t
== current_trace
)
2636 trace_branch_disable();
2637 if (current_trace
&& current_trace
->reset
)
2638 current_trace
->reset(tr
);
2644 trace_branch_enable(tr
);
2646 mutex_unlock(&trace_types_lock
);
2652 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2653 size_t cnt
, loff_t
*ppos
)
2655 char buf
[max_tracer_type_len
+1];
2659 if (cnt
> max_tracer_type_len
)
2660 cnt
= max_tracer_type_len
;
2662 if (copy_from_user(&buf
, ubuf
, cnt
))
2667 /* strip ending whitespace. */
2668 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2671 ret
= tracing_set_tracer(buf
);
2682 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2683 size_t cnt
, loff_t
*ppos
)
2685 unsigned long *ptr
= filp
->private_data
;
2689 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2690 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2691 if (r
> sizeof(buf
))
2693 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2697 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2698 size_t cnt
, loff_t
*ppos
)
2700 long *ptr
= filp
->private_data
;
2705 if (cnt
>= sizeof(buf
))
2708 if (copy_from_user(&buf
, ubuf
, cnt
))
2713 ret
= strict_strtoul(buf
, 10, &val
);
2722 static atomic_t tracing_reader
;
2724 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2726 struct trace_iterator
*iter
;
2728 if (tracing_disabled
)
2731 /* We only allow for reader of the pipe */
2732 if (atomic_inc_return(&tracing_reader
) != 1) {
2733 atomic_dec(&tracing_reader
);
2737 /* create a buffer to store the information to pass to userspace */
2738 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2742 mutex_lock(&trace_types_lock
);
2744 /* trace pipe does not show start of buffer */
2745 cpus_setall(iter
->started
);
2747 iter
->tr
= &global_trace
;
2748 iter
->trace
= current_trace
;
2749 filp
->private_data
= iter
;
2751 if (iter
->trace
->pipe_open
)
2752 iter
->trace
->pipe_open(iter
);
2753 mutex_unlock(&trace_types_lock
);
2758 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2760 struct trace_iterator
*iter
= file
->private_data
;
2763 atomic_dec(&tracing_reader
);
2769 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2771 struct trace_iterator
*iter
= filp
->private_data
;
2773 if (trace_flags
& TRACE_ITER_BLOCK
) {
2775 * Always select as readable when in blocking mode
2777 return POLLIN
| POLLRDNORM
;
2779 if (!trace_empty(iter
))
2780 return POLLIN
| POLLRDNORM
;
2781 poll_wait(filp
, &trace_wait
, poll_table
);
2782 if (!trace_empty(iter
))
2783 return POLLIN
| POLLRDNORM
;
2793 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2794 size_t cnt
, loff_t
*ppos
)
2796 struct trace_iterator
*iter
= filp
->private_data
;
2799 /* return any leftover data */
2800 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2804 trace_seq_reset(&iter
->seq
);
2806 mutex_lock(&trace_types_lock
);
2807 if (iter
->trace
->read
) {
2808 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2815 while (trace_empty(iter
)) {
2817 if ((filp
->f_flags
& O_NONBLOCK
)) {
2823 * This is a make-shift waitqueue. The reason we don't use
2824 * an actual wait queue is because:
2825 * 1) we only ever have one waiter
2826 * 2) the tracing, traces all functions, we don't want
2827 * the overhead of calling wake_up and friends
2828 * (and tracing them too)
2829 * Anyway, this is really very primitive wakeup.
2831 set_current_state(TASK_INTERRUPTIBLE
);
2832 iter
->tr
->waiter
= current
;
2834 mutex_unlock(&trace_types_lock
);
2836 /* sleep for 100 msecs, and try again. */
2837 schedule_timeout(HZ
/10);
2839 mutex_lock(&trace_types_lock
);
2841 iter
->tr
->waiter
= NULL
;
2843 if (signal_pending(current
)) {
2848 if (iter
->trace
!= current_trace
)
2852 * We block until we read something and tracing is disabled.
2853 * We still block if tracing is disabled, but we have never
2854 * read anything. This allows a user to cat this file, and
2855 * then enable tracing. But after we have read something,
2856 * we give an EOF when tracing is again disabled.
2858 * iter->pos will be 0 if we haven't read anything.
2860 if (!tracer_enabled
&& iter
->pos
)
2866 /* stop when tracing is finished */
2867 if (trace_empty(iter
))
2870 if (cnt
>= PAGE_SIZE
)
2871 cnt
= PAGE_SIZE
- 1;
2873 /* reset all but tr, trace, and overruns */
2874 memset(&iter
->seq
, 0,
2875 sizeof(struct trace_iterator
) -
2876 offsetof(struct trace_iterator
, seq
));
2879 while (find_next_entry_inc(iter
) != NULL
) {
2880 enum print_line_t ret
;
2881 int len
= iter
->seq
.len
;
2883 ret
= print_trace_line(iter
);
2884 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2885 /* don't print partial lines */
2886 iter
->seq
.len
= len
;
2890 trace_consume(iter
);
2892 if (iter
->seq
.len
>= cnt
)
2896 /* Now copy what we have to the user */
2897 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2898 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2899 trace_seq_reset(&iter
->seq
);
2902 * If there was nothing to send to user, inspite of consuming trace
2903 * entries, go back to wait for more entries.
2909 mutex_unlock(&trace_types_lock
);
2915 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
2916 size_t cnt
, loff_t
*ppos
)
2918 struct trace_array
*tr
= filp
->private_data
;
2922 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
2923 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2927 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
2928 size_t cnt
, loff_t
*ppos
)
2934 if (cnt
>= sizeof(buf
))
2937 if (copy_from_user(&buf
, ubuf
, cnt
))
2942 ret
= strict_strtoul(buf
, 10, &val
);
2946 /* must have at least 1 entry */
2950 mutex_lock(&trace_types_lock
);
2954 /* disable all cpu buffers */
2955 for_each_tracing_cpu(cpu
) {
2956 if (global_trace
.data
[cpu
])
2957 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2958 if (max_tr
.data
[cpu
])
2959 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
2962 /* value is in KB */
2965 if (val
!= global_trace
.entries
) {
2966 ret
= ring_buffer_resize(global_trace
.buffer
, val
);
2972 ret
= ring_buffer_resize(max_tr
.buffer
, val
);
2976 r
= ring_buffer_resize(global_trace
.buffer
,
2977 global_trace
.entries
);
2979 /* AARGH! We are left with different
2980 * size max buffer!!!! */
2982 tracing_disabled
= 1;
2987 global_trace
.entries
= val
;
2992 /* If check pages failed, return ENOMEM */
2993 if (tracing_disabled
)
2996 for_each_tracing_cpu(cpu
) {
2997 if (global_trace
.data
[cpu
])
2998 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2999 if (max_tr
.data
[cpu
])
3000 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3004 max_tr
.entries
= global_trace
.entries
;
3005 mutex_unlock(&trace_types_lock
);
3010 static int mark_printk(const char *fmt
, ...)
3014 va_start(args
, fmt
);
3015 ret
= trace_vprintk(0, fmt
, args
);
3021 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3022 size_t cnt
, loff_t
*fpos
)
3027 if (tracing_disabled
)
3030 if (cnt
> TRACE_BUF_SIZE
)
3031 cnt
= TRACE_BUF_SIZE
;
3033 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3037 if (copy_from_user(buf
, ubuf
, cnt
)) {
3042 /* Cut from the first nil or newline. */
3044 end
= strchr(buf
, '\n');
3048 cnt
= mark_printk("%s\n", buf
);
3055 static struct file_operations tracing_max_lat_fops
= {
3056 .open
= tracing_open_generic
,
3057 .read
= tracing_max_lat_read
,
3058 .write
= tracing_max_lat_write
,
3061 static struct file_operations tracing_ctrl_fops
= {
3062 .open
= tracing_open_generic
,
3063 .read
= tracing_ctrl_read
,
3064 .write
= tracing_ctrl_write
,
3067 static struct file_operations set_tracer_fops
= {
3068 .open
= tracing_open_generic
,
3069 .read
= tracing_set_trace_read
,
3070 .write
= tracing_set_trace_write
,
3073 static struct file_operations tracing_pipe_fops
= {
3074 .open
= tracing_open_pipe
,
3075 .poll
= tracing_poll_pipe
,
3076 .read
= tracing_read_pipe
,
3077 .release
= tracing_release_pipe
,
3080 static struct file_operations tracing_entries_fops
= {
3081 .open
= tracing_open_generic
,
3082 .read
= tracing_entries_read
,
3083 .write
= tracing_entries_write
,
3086 static struct file_operations tracing_mark_fops
= {
3087 .open
= tracing_open_generic
,
3088 .write
= tracing_mark_write
,
3091 #ifdef CONFIG_DYNAMIC_FTRACE
3093 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3099 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3100 size_t cnt
, loff_t
*ppos
)
3102 static char ftrace_dyn_info_buffer
[1024];
3103 static DEFINE_MUTEX(dyn_info_mutex
);
3104 unsigned long *p
= filp
->private_data
;
3105 char *buf
= ftrace_dyn_info_buffer
;
3106 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3109 mutex_lock(&dyn_info_mutex
);
3110 r
= sprintf(buf
, "%ld ", *p
);
3112 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3115 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3117 mutex_unlock(&dyn_info_mutex
);
3122 static struct file_operations tracing_dyn_info_fops
= {
3123 .open
= tracing_open_generic
,
3124 .read
= tracing_read_dyn_info
,
3128 static struct dentry
*d_tracer
;
3130 struct dentry
*tracing_init_dentry(void)
3137 d_tracer
= debugfs_create_dir("tracing", NULL
);
3139 if (!d_tracer
&& !once
) {
3141 pr_warning("Could not create debugfs directory 'tracing'\n");
3148 #ifdef CONFIG_FTRACE_SELFTEST
3149 /* Let selftest have access to static functions in this file */
3150 #include "trace_selftest.c"
3153 static __init
int tracer_init_debugfs(void)
3155 struct dentry
*d_tracer
;
3156 struct dentry
*entry
;
3158 d_tracer
= tracing_init_dentry();
3160 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
3161 &global_trace
, &tracing_ctrl_fops
);
3163 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3165 entry
= debugfs_create_file("trace_options", 0644, d_tracer
,
3166 NULL
, &tracing_iter_fops
);
3168 pr_warning("Could not create debugfs 'trace_options' entry\n");
3170 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
3171 NULL
, &tracing_cpumask_fops
);
3173 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3175 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
3176 &global_trace
, &tracing_lt_fops
);
3178 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3180 entry
= debugfs_create_file("trace", 0444, d_tracer
,
3181 &global_trace
, &tracing_fops
);
3183 pr_warning("Could not create debugfs 'trace' entry\n");
3185 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
3186 &global_trace
, &show_traces_fops
);
3188 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3190 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
3191 &global_trace
, &set_tracer_fops
);
3193 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3195 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
3196 &tracing_max_latency
,
3197 &tracing_max_lat_fops
);
3199 pr_warning("Could not create debugfs "
3200 "'tracing_max_latency' entry\n");
3202 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
3203 &tracing_thresh
, &tracing_max_lat_fops
);
3205 pr_warning("Could not create debugfs "
3206 "'tracing_thresh' entry\n");
3207 entry
= debugfs_create_file("README", 0644, d_tracer
,
3208 NULL
, &tracing_readme_fops
);
3210 pr_warning("Could not create debugfs 'README' entry\n");
3212 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
3213 NULL
, &tracing_pipe_fops
);
3215 pr_warning("Could not create debugfs "
3216 "'trace_pipe' entry\n");
3218 entry
= debugfs_create_file("buffer_size_kb", 0644, d_tracer
,
3219 &global_trace
, &tracing_entries_fops
);
3221 pr_warning("Could not create debugfs "
3222 "'buffer_size_kb' entry\n");
3224 entry
= debugfs_create_file("trace_marker", 0220, d_tracer
,
3225 NULL
, &tracing_mark_fops
);
3227 pr_warning("Could not create debugfs "
3228 "'trace_marker' entry\n");
3230 #ifdef CONFIG_DYNAMIC_FTRACE
3231 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
3232 &ftrace_update_tot_cnt
,
3233 &tracing_dyn_info_fops
);
3235 pr_warning("Could not create debugfs "
3236 "'dyn_ftrace_total_info' entry\n");
3238 #ifdef CONFIG_SYSPROF_TRACER
3239 init_tracer_sysprof_debugfs(d_tracer
);
3244 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
3246 static DEFINE_SPINLOCK(trace_buf_lock
);
3247 static char trace_buf
[TRACE_BUF_SIZE
];
3249 struct ring_buffer_event
*event
;
3250 struct trace_array
*tr
= &global_trace
;
3251 struct trace_array_cpu
*data
;
3252 struct print_entry
*entry
;
3253 unsigned long flags
, irq_flags
;
3254 int cpu
, len
= 0, size
, pc
;
3256 if (tracing_disabled
)
3259 pc
= preempt_count();
3260 preempt_disable_notrace();
3261 cpu
= raw_smp_processor_id();
3262 data
= tr
->data
[cpu
];
3264 if (unlikely(atomic_read(&data
->disabled
)))
3267 spin_lock_irqsave(&trace_buf_lock
, flags
);
3268 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
3270 len
= min(len
, TRACE_BUF_SIZE
-1);
3273 size
= sizeof(*entry
) + len
+ 1;
3274 event
= ring_buffer_lock_reserve(tr
->buffer
, size
, &irq_flags
);
3277 entry
= ring_buffer_event_data(event
);
3278 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
3279 entry
->ent
.type
= TRACE_PRINT
;
3282 memcpy(&entry
->buf
, trace_buf
, len
);
3283 entry
->buf
[len
] = 0;
3284 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
3287 spin_unlock_irqrestore(&trace_buf_lock
, flags
);
3290 preempt_enable_notrace();
3294 EXPORT_SYMBOL_GPL(trace_vprintk
);
3296 int __ftrace_printk(unsigned long ip
, const char *fmt
, ...)
3301 if (!(trace_flags
& TRACE_ITER_PRINTK
))
3305 ret
= trace_vprintk(ip
, fmt
, ap
);
3309 EXPORT_SYMBOL_GPL(__ftrace_printk
);
3311 static int trace_panic_handler(struct notifier_block
*this,
3312 unsigned long event
, void *unused
)
3314 if (ftrace_dump_on_oops
)
3319 static struct notifier_block trace_panic_notifier
= {
3320 .notifier_call
= trace_panic_handler
,
3322 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
3325 static int trace_die_handler(struct notifier_block
*self
,
3331 if (ftrace_dump_on_oops
)
3340 static struct notifier_block trace_die_notifier
= {
3341 .notifier_call
= trace_die_handler
,
3346 * printk is set to max of 1024, we really don't need it that big.
3347 * Nothing should be printing 1000 characters anyway.
3349 #define TRACE_MAX_PRINT 1000
3352 * Define here KERN_TRACE so that we have one place to modify
3353 * it if we decide to change what log level the ftrace dump
3356 #define KERN_TRACE KERN_INFO
3359 trace_printk_seq(struct trace_seq
*s
)
3361 /* Probably should print a warning here. */
3365 /* should be zero ended, but we are paranoid. */
3366 s
->buffer
[s
->len
] = 0;
3368 printk(KERN_TRACE
"%s", s
->buffer
);
3373 void ftrace_dump(void)
3375 static DEFINE_SPINLOCK(ftrace_dump_lock
);
3376 /* use static because iter can be a bit big for the stack */
3377 static struct trace_iterator iter
;
3378 static cpumask_t mask
;
3379 static int dump_ran
;
3380 unsigned long flags
;
3384 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
3390 /* No turning back! */
3393 for_each_tracing_cpu(cpu
) {
3394 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3397 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
3399 iter
.tr
= &global_trace
;
3400 iter
.trace
= current_trace
;
3403 * We need to stop all tracing on all CPUS to read the
3404 * the next buffer. This is a bit expensive, but is
3405 * not done often. We fill all what we can read,
3406 * and then release the locks again.
3411 while (!trace_empty(&iter
)) {
3414 printk(KERN_TRACE
"---------------------------------\n");
3418 /* reset all but tr, trace, and overruns */
3419 memset(&iter
.seq
, 0,
3420 sizeof(struct trace_iterator
) -
3421 offsetof(struct trace_iterator
, seq
));
3422 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
3425 if (find_next_entry_inc(&iter
) != NULL
) {
3426 print_trace_line(&iter
);
3427 trace_consume(&iter
);
3430 trace_printk_seq(&iter
.seq
);
3434 printk(KERN_TRACE
" (ftrace buffer empty)\n");
3436 printk(KERN_TRACE
"---------------------------------\n");
3439 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
3442 __init
static int tracer_alloc_buffers(void)
3444 struct trace_array_cpu
*data
;
3447 /* TODO: make the number of buffers hot pluggable with CPUS */
3448 tracing_buffer_mask
= cpu_possible_map
;
3450 global_trace
.buffer
= ring_buffer_alloc(trace_buf_size
,
3451 TRACE_BUFFER_FLAGS
);
3452 if (!global_trace
.buffer
) {
3453 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
3457 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
3459 #ifdef CONFIG_TRACER_MAX_TRACE
3460 max_tr
.buffer
= ring_buffer_alloc(trace_buf_size
,
3461 TRACE_BUFFER_FLAGS
);
3462 if (!max_tr
.buffer
) {
3463 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
3465 ring_buffer_free(global_trace
.buffer
);
3468 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
3469 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
3472 /* Allocate the first page for all buffers */
3473 for_each_tracing_cpu(i
) {
3474 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
3475 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
3478 trace_init_cmdlines();
3480 register_tracer(&nop_trace
);
3481 #ifdef CONFIG_BOOT_TRACER
3482 register_tracer(&boot_tracer
);
3483 current_trace
= &boot_tracer
;
3484 current_trace
->init(&global_trace
);
3486 current_trace
= &nop_trace
;
3489 /* All seems OK, enable tracing */
3490 tracing_disabled
= 0;
3492 atomic_notifier_chain_register(&panic_notifier_list
,
3493 &trace_panic_notifier
);
3495 register_die_notifier(&trace_die_notifier
);
3499 early_initcall(tracer_alloc_buffers
);
3500 fs_initcall(tracer_init_debugfs
);