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 iter_ctrl options */
208 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
;
211 * trace_wake_up - wake up tasks waiting for trace input
213 * Simply wakes up any task that is blocked on the trace_wait
214 * queue. These is used with trace_poll for tasks polling the trace.
216 void trace_wake_up(void)
219 * The runqueue_is_locked() can fail, but this is the best we
222 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
223 wake_up(&trace_wait
);
226 static int __init
set_buf_size(char *str
)
228 unsigned long buf_size
;
233 ret
= strict_strtoul(str
, 0, &buf_size
);
234 /* nr_entries can not be zero */
235 if (ret
< 0 || buf_size
== 0)
237 trace_buf_size
= buf_size
;
240 __setup("trace_buf_size=", set_buf_size
);
242 unsigned long nsecs_to_usecs(unsigned long nsecs
)
248 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
249 * control the output of kernel symbols.
251 #define TRACE_ITER_SYM_MASK \
252 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
254 /* These must match the bit postions in trace_iterator_flags */
255 static const char *trace_options
[] = {
272 * ftrace_max_lock is used to protect the swapping of buffers
273 * when taking a max snapshot. The buffers themselves are
274 * protected by per_cpu spinlocks. But the action of the swap
275 * needs its own lock.
277 * This is defined as a raw_spinlock_t in order to help
278 * with performance when lockdep debugging is enabled.
280 static raw_spinlock_t ftrace_max_lock
=
281 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
284 * Copy the new maximum trace into the separate maximum-trace
285 * structure. (this way the maximum trace is permanently saved,
286 * for later retrieval via /debugfs/tracing/latency_trace)
289 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
291 struct trace_array_cpu
*data
= tr
->data
[cpu
];
294 max_tr
.time_start
= data
->preempt_timestamp
;
296 data
= max_tr
.data
[cpu
];
297 data
->saved_latency
= tracing_max_latency
;
299 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
300 data
->pid
= tsk
->pid
;
301 data
->uid
= tsk
->uid
;
302 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
303 data
->policy
= tsk
->policy
;
304 data
->rt_priority
= tsk
->rt_priority
;
306 /* record this tasks comm */
307 tracing_record_cmdline(current
);
311 * trace_seq_printf - sequence printing of trace information
312 * @s: trace sequence descriptor
313 * @fmt: printf format string
315 * The tracer may use either sequence operations or its own
316 * copy to user routines. To simplify formating of a trace
317 * trace_seq_printf is used to store strings into a special
318 * buffer (@s). Then the output may be either used by
319 * the sequencer or pulled into another buffer.
322 trace_seq_printf(struct trace_seq
*s
, const char *fmt
, ...)
324 int len
= (PAGE_SIZE
- 1) - s
->len
;
332 ret
= vsnprintf(s
->buffer
+ s
->len
, len
, fmt
, ap
);
335 /* If we can't write it all, don't bother writing anything */
345 * trace_seq_puts - trace sequence printing of simple string
346 * @s: trace sequence descriptor
347 * @str: simple string to record
349 * The tracer may use either the sequence operations or its own
350 * copy to user routines. This function records a simple string
351 * into a special buffer (@s) for later retrieval by a sequencer
352 * or other mechanism.
355 trace_seq_puts(struct trace_seq
*s
, const char *str
)
357 int len
= strlen(str
);
359 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
362 memcpy(s
->buffer
+ s
->len
, str
, len
);
369 trace_seq_putc(struct trace_seq
*s
, unsigned char c
)
371 if (s
->len
>= (PAGE_SIZE
- 1))
374 s
->buffer
[s
->len
++] = c
;
380 trace_seq_putmem(struct trace_seq
*s
, void *mem
, size_t len
)
382 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
385 memcpy(s
->buffer
+ s
->len
, mem
, len
);
391 #define MAX_MEMHEX_BYTES 8
392 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
395 trace_seq_putmem_hex(struct trace_seq
*s
, void *mem
, size_t len
)
397 unsigned char hex
[HEX_CHARS
];
398 unsigned char *data
= mem
;
402 for (i
= 0, j
= 0; i
< len
; i
++) {
404 for (i
= len
-1, j
= 0; i
>= 0; i
--) {
406 hex
[j
++] = hex_asc_hi(data
[i
]);
407 hex
[j
++] = hex_asc_lo(data
[i
]);
411 return trace_seq_putmem(s
, hex
, j
);
415 trace_seq_reset(struct trace_seq
*s
)
421 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
426 if (s
->len
<= s
->readpos
)
429 len
= s
->len
- s
->readpos
;
432 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
441 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
443 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
446 seq_puts(m
, s
->buffer
);
452 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
454 * @tsk: the task with the latency
455 * @cpu: The cpu that initiated the trace.
457 * Flip the buffers between the @tr and the max_tr and record information
458 * about which task was the cause of this latency.
461 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
463 struct ring_buffer
*buf
= tr
->buffer
;
465 WARN_ON_ONCE(!irqs_disabled());
466 __raw_spin_lock(&ftrace_max_lock
);
468 tr
->buffer
= max_tr
.buffer
;
471 ftrace_disable_cpu();
472 ring_buffer_reset(tr
->buffer
);
475 __update_max_tr(tr
, tsk
, cpu
);
476 __raw_spin_unlock(&ftrace_max_lock
);
480 * update_max_tr_single - only copy one trace over, and reset the rest
482 * @tsk - task with the latency
483 * @cpu - the cpu of the buffer to copy.
485 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
488 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
492 WARN_ON_ONCE(!irqs_disabled());
493 __raw_spin_lock(&ftrace_max_lock
);
495 ftrace_disable_cpu();
497 ring_buffer_reset(max_tr
.buffer
);
498 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
504 __update_max_tr(tr
, tsk
, cpu
);
505 __raw_spin_unlock(&ftrace_max_lock
);
509 * register_tracer - register a tracer with the ftrace system.
510 * @type - the plugin for the tracer
512 * Register a new plugin tracer.
514 int register_tracer(struct tracer
*type
)
521 pr_info("Tracer must have a name\n");
525 mutex_lock(&trace_types_lock
);
526 for (t
= trace_types
; t
; t
= t
->next
) {
527 if (strcmp(type
->name
, t
->name
) == 0) {
529 pr_info("Trace %s already registered\n",
536 #ifdef CONFIG_FTRACE_STARTUP_TEST
537 if (type
->selftest
) {
538 struct tracer
*saved_tracer
= current_trace
;
539 struct trace_array
*tr
= &global_trace
;
542 * Run a selftest on this tracer.
543 * Here we reset the trace buffer, and set the current
544 * tracer to be this tracer. The tracer can then run some
545 * internal tracing to verify that everything is in order.
546 * If we fail, we do not register this tracer.
548 for_each_tracing_cpu(i
) {
549 tracing_reset(tr
, i
);
551 current_trace
= type
;
552 /* the test is responsible for initializing and enabling */
553 pr_info("Testing tracer %s: ", type
->name
);
554 ret
= type
->selftest(type
, tr
);
555 /* the test is responsible for resetting too */
556 current_trace
= saved_tracer
;
558 printk(KERN_CONT
"FAILED!\n");
561 /* Only reset on passing, to avoid touching corrupted buffers */
562 for_each_tracing_cpu(i
) {
563 tracing_reset(tr
, i
);
565 printk(KERN_CONT
"PASSED\n");
569 type
->next
= trace_types
;
571 len
= strlen(type
->name
);
572 if (len
> max_tracer_type_len
)
573 max_tracer_type_len
= len
;
576 mutex_unlock(&trace_types_lock
);
581 void unregister_tracer(struct tracer
*type
)
586 mutex_lock(&trace_types_lock
);
587 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
591 pr_info("Trace %s not registered\n", type
->name
);
596 if (strlen(type
->name
) != max_tracer_type_len
)
599 max_tracer_type_len
= 0;
600 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
601 len
= strlen((*t
)->name
);
602 if (len
> max_tracer_type_len
)
603 max_tracer_type_len
= len
;
606 mutex_unlock(&trace_types_lock
);
609 void tracing_reset(struct trace_array
*tr
, int cpu
)
611 ftrace_disable_cpu();
612 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
616 #define SAVED_CMDLINES 128
617 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
618 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
619 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
620 static int cmdline_idx
;
621 static DEFINE_SPINLOCK(trace_cmdline_lock
);
623 /* temporary disable recording */
624 atomic_t trace_record_cmdline_disabled __read_mostly
;
626 static void trace_init_cmdlines(void)
628 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
629 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
633 static int trace_stop_count
;
634 static DEFINE_SPINLOCK(tracing_start_lock
);
637 * tracing_start - quick start of the tracer
639 * If tracing is enabled but was stopped by tracing_stop,
640 * this will start the tracer back up.
642 void tracing_start(void)
644 struct ring_buffer
*buffer
;
647 if (tracing_disabled
)
650 spin_lock_irqsave(&tracing_start_lock
, flags
);
651 if (--trace_stop_count
)
654 if (trace_stop_count
< 0) {
655 /* Someone screwed up their debugging */
657 trace_stop_count
= 0;
662 buffer
= global_trace
.buffer
;
664 ring_buffer_record_enable(buffer
);
666 buffer
= max_tr
.buffer
;
668 ring_buffer_record_enable(buffer
);
672 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
676 * tracing_stop - quick stop of the tracer
678 * Light weight way to stop tracing. Use in conjunction with
681 void tracing_stop(void)
683 struct ring_buffer
*buffer
;
687 spin_lock_irqsave(&tracing_start_lock
, flags
);
688 if (trace_stop_count
++)
691 buffer
= global_trace
.buffer
;
693 ring_buffer_record_disable(buffer
);
695 buffer
= max_tr
.buffer
;
697 ring_buffer_record_disable(buffer
);
700 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
703 void trace_stop_cmdline_recording(void);
705 static void trace_save_cmdline(struct task_struct
*tsk
)
710 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
714 * It's not the end of the world if we don't get
715 * the lock, but we also don't want to spin
716 * nor do we want to disable interrupts,
717 * so if we miss here, then better luck next time.
719 if (!spin_trylock(&trace_cmdline_lock
))
722 idx
= map_pid_to_cmdline
[tsk
->pid
];
723 if (idx
>= SAVED_CMDLINES
) {
724 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
726 map
= map_cmdline_to_pid
[idx
];
727 if (map
<= PID_MAX_DEFAULT
)
728 map_pid_to_cmdline
[map
] = (unsigned)-1;
730 map_pid_to_cmdline
[tsk
->pid
] = idx
;
735 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
737 spin_unlock(&trace_cmdline_lock
);
740 static char *trace_find_cmdline(int pid
)
742 char *cmdline
= "<...>";
748 if (pid
> PID_MAX_DEFAULT
)
751 map
= map_pid_to_cmdline
[pid
];
752 if (map
>= SAVED_CMDLINES
)
755 cmdline
= saved_cmdlines
[map
];
761 void tracing_record_cmdline(struct task_struct
*tsk
)
763 if (atomic_read(&trace_record_cmdline_disabled
))
766 trace_save_cmdline(tsk
);
770 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
773 struct task_struct
*tsk
= current
;
775 entry
->preempt_count
= pc
& 0xff;
776 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
778 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
779 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
781 TRACE_FLAG_IRQS_NOSUPPORT
|
783 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
784 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
785 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
789 trace_function(struct trace_array
*tr
, struct trace_array_cpu
*data
,
790 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
793 struct ring_buffer_event
*event
;
794 struct ftrace_entry
*entry
;
795 unsigned long irq_flags
;
797 /* If we are reading the ring buffer, don't trace */
798 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
801 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
805 entry
= ring_buffer_event_data(event
);
806 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
807 entry
->ent
.type
= TRACE_FN
;
809 entry
->parent_ip
= parent_ip
;
810 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
814 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
815 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
818 if (likely(!atomic_read(&data
->disabled
)))
819 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
822 static void ftrace_trace_stack(struct trace_array
*tr
,
823 struct trace_array_cpu
*data
,
827 #ifdef CONFIG_STACKTRACE
828 struct ring_buffer_event
*event
;
829 struct stack_entry
*entry
;
830 struct stack_trace trace
;
831 unsigned long irq_flags
;
833 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
836 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
840 entry
= ring_buffer_event_data(event
);
841 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
842 entry
->ent
.type
= TRACE_STACK
;
844 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
846 trace
.nr_entries
= 0;
847 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
849 trace
.entries
= entry
->caller
;
851 save_stack_trace(&trace
);
852 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
856 void __trace_stack(struct trace_array
*tr
,
857 struct trace_array_cpu
*data
,
861 ftrace_trace_stack(tr
, data
, flags
, skip
, preempt_count());
865 ftrace_trace_special(void *__tr
, void *__data
,
866 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
869 struct ring_buffer_event
*event
;
870 struct trace_array_cpu
*data
= __data
;
871 struct trace_array
*tr
= __tr
;
872 struct special_entry
*entry
;
873 unsigned long irq_flags
;
875 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
879 entry
= ring_buffer_event_data(event
);
880 tracing_generic_entry_update(&entry
->ent
, 0, pc
);
881 entry
->ent
.type
= TRACE_SPECIAL
;
885 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
886 ftrace_trace_stack(tr
, data
, irq_flags
, 4, pc
);
892 __trace_special(void *__tr
, void *__data
,
893 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
895 ftrace_trace_special(__tr
, __data
, arg1
, arg2
, arg3
, preempt_count());
899 tracing_sched_switch_trace(struct trace_array
*tr
,
900 struct trace_array_cpu
*data
,
901 struct task_struct
*prev
,
902 struct task_struct
*next
,
903 unsigned long flags
, int pc
)
905 struct ring_buffer_event
*event
;
906 struct ctx_switch_entry
*entry
;
907 unsigned long irq_flags
;
909 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
913 entry
= ring_buffer_event_data(event
);
914 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
915 entry
->ent
.type
= TRACE_CTX
;
916 entry
->prev_pid
= prev
->pid
;
917 entry
->prev_prio
= prev
->prio
;
918 entry
->prev_state
= prev
->state
;
919 entry
->next_pid
= next
->pid
;
920 entry
->next_prio
= next
->prio
;
921 entry
->next_state
= next
->state
;
922 entry
->next_cpu
= task_cpu(next
);
923 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
924 ftrace_trace_stack(tr
, data
, flags
, 5, pc
);
928 tracing_sched_wakeup_trace(struct trace_array
*tr
,
929 struct trace_array_cpu
*data
,
930 struct task_struct
*wakee
,
931 struct task_struct
*curr
,
932 unsigned long flags
, int pc
)
934 struct ring_buffer_event
*event
;
935 struct ctx_switch_entry
*entry
;
936 unsigned long irq_flags
;
938 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
942 entry
= ring_buffer_event_data(event
);
943 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
944 entry
->ent
.type
= TRACE_WAKE
;
945 entry
->prev_pid
= curr
->pid
;
946 entry
->prev_prio
= curr
->prio
;
947 entry
->prev_state
= curr
->state
;
948 entry
->next_pid
= wakee
->pid
;
949 entry
->next_prio
= wakee
->prio
;
950 entry
->next_state
= wakee
->state
;
951 entry
->next_cpu
= task_cpu(wakee
);
952 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
953 ftrace_trace_stack(tr
, data
, flags
, 6, pc
);
959 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
961 struct trace_array
*tr
= &global_trace
;
962 struct trace_array_cpu
*data
;
967 if (tracing_disabled
)
970 pc
= preempt_count();
971 local_irq_save(flags
);
972 cpu
= raw_smp_processor_id();
973 data
= tr
->data
[cpu
];
975 if (likely(atomic_inc_return(&data
->disabled
) == 1))
976 ftrace_trace_special(tr
, data
, arg1
, arg2
, arg3
, pc
);
978 atomic_dec(&data
->disabled
);
979 local_irq_restore(flags
);
982 #ifdef CONFIG_FUNCTION_TRACER
984 function_trace_call_preempt_only(unsigned long ip
, unsigned long parent_ip
)
986 struct trace_array
*tr
= &global_trace
;
987 struct trace_array_cpu
*data
;
993 if (unlikely(!ftrace_function_enabled
))
996 pc
= preempt_count();
997 resched
= ftrace_preempt_disable();
998 local_save_flags(flags
);
999 cpu
= raw_smp_processor_id();
1000 data
= tr
->data
[cpu
];
1001 disabled
= atomic_inc_return(&data
->disabled
);
1003 if (likely(disabled
== 1))
1004 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
1006 atomic_dec(&data
->disabled
);
1007 ftrace_preempt_enable(resched
);
1011 function_trace_call(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
))
1024 * Need to use raw, since this must be called before the
1025 * recursive protection is performed.
1027 raw_local_irq_save(flags
);
1028 cpu
= raw_smp_processor_id();
1029 data
= tr
->data
[cpu
];
1030 disabled
= atomic_inc_return(&data
->disabled
);
1032 if (likely(disabled
== 1)) {
1033 pc
= preempt_count();
1034 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
1037 atomic_dec(&data
->disabled
);
1038 raw_local_irq_restore(flags
);
1041 static struct ftrace_ops trace_ops __read_mostly
=
1043 .func
= function_trace_call
,
1046 void tracing_start_function_trace(void)
1048 ftrace_function_enabled
= 0;
1050 if (trace_flags
& TRACE_ITER_PREEMPTONLY
)
1051 trace_ops
.func
= function_trace_call_preempt_only
;
1053 trace_ops
.func
= function_trace_call
;
1055 register_ftrace_function(&trace_ops
);
1056 ftrace_function_enabled
= 1;
1059 void tracing_stop_function_trace(void)
1061 ftrace_function_enabled
= 0;
1062 unregister_ftrace_function(&trace_ops
);
1066 enum trace_file_type
{
1067 TRACE_FILE_LAT_FMT
= 1,
1070 static void trace_iterator_increment(struct trace_iterator
*iter
, int cpu
)
1072 /* Don't allow ftrace to trace into the ring buffers */
1073 ftrace_disable_cpu();
1076 if (iter
->buffer_iter
[iter
->cpu
])
1077 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1079 ftrace_enable_cpu();
1082 static struct trace_entry
*
1083 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1085 struct ring_buffer_event
*event
;
1086 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1088 /* Don't allow ftrace to trace into the ring buffers */
1089 ftrace_disable_cpu();
1092 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1094 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1096 ftrace_enable_cpu();
1098 return event
? ring_buffer_event_data(event
) : NULL
;
1101 static struct trace_entry
*
1102 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1104 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1105 struct trace_entry
*ent
, *next
= NULL
;
1106 u64 next_ts
= 0, ts
;
1110 for_each_tracing_cpu(cpu
) {
1112 if (ring_buffer_empty_cpu(buffer
, cpu
))
1115 ent
= peek_next_entry(iter
, cpu
, &ts
);
1118 * Pick the entry with the smallest timestamp:
1120 if (ent
&& (!next
|| ts
< next_ts
)) {
1128 *ent_cpu
= next_cpu
;
1136 /* Find the next real entry, without updating the iterator itself */
1137 static struct trace_entry
*
1138 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1140 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1143 /* Find the next real entry, and increment the iterator to the next entry */
1144 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1146 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1149 trace_iterator_increment(iter
, iter
->cpu
);
1151 return iter
->ent
? iter
: NULL
;
1154 static void trace_consume(struct trace_iterator
*iter
)
1156 /* Don't allow ftrace to trace into the ring buffers */
1157 ftrace_disable_cpu();
1158 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1159 ftrace_enable_cpu();
1162 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1164 struct trace_iterator
*iter
= m
->private;
1170 /* can't go backwards */
1175 ent
= find_next_entry_inc(iter
);
1179 while (ent
&& iter
->idx
< i
)
1180 ent
= find_next_entry_inc(iter
);
1187 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1189 struct trace_iterator
*iter
= m
->private;
1194 mutex_lock(&trace_types_lock
);
1196 if (!current_trace
|| current_trace
!= iter
->trace
) {
1197 mutex_unlock(&trace_types_lock
);
1201 atomic_inc(&trace_record_cmdline_disabled
);
1203 if (*pos
!= iter
->pos
) {
1208 ftrace_disable_cpu();
1210 for_each_tracing_cpu(cpu
) {
1211 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1214 ftrace_enable_cpu();
1216 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1221 p
= s_next(m
, p
, &l
);
1227 static void s_stop(struct seq_file
*m
, void *p
)
1229 atomic_dec(&trace_record_cmdline_disabled
);
1230 mutex_unlock(&trace_types_lock
);
1233 #ifdef CONFIG_KRETPROBES
1234 static inline const char *kretprobed(const char *name
)
1236 static const char tramp_name
[] = "kretprobe_trampoline";
1237 int size
= sizeof(tramp_name
);
1239 if (strncmp(tramp_name
, name
, size
) == 0)
1240 return "[unknown/kretprobe'd]";
1244 static inline const char *kretprobed(const char *name
)
1248 #endif /* CONFIG_KRETPROBES */
1251 seq_print_sym_short(struct trace_seq
*s
, const char *fmt
, unsigned long address
)
1253 #ifdef CONFIG_KALLSYMS
1254 char str
[KSYM_SYMBOL_LEN
];
1257 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
1259 name
= kretprobed(str
);
1261 return trace_seq_printf(s
, fmt
, name
);
1267 seq_print_sym_offset(struct trace_seq
*s
, const char *fmt
,
1268 unsigned long address
)
1270 #ifdef CONFIG_KALLSYMS
1271 char str
[KSYM_SYMBOL_LEN
];
1274 sprint_symbol(str
, address
);
1275 name
= kretprobed(str
);
1277 return trace_seq_printf(s
, fmt
, name
);
1282 #ifndef CONFIG_64BIT
1283 # define IP_FMT "%08lx"
1285 # define IP_FMT "%016lx"
1289 seq_print_ip_sym(struct trace_seq
*s
, unsigned long ip
, unsigned long sym_flags
)
1294 return trace_seq_printf(s
, "0");
1296 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
1297 ret
= seq_print_sym_offset(s
, "%s", ip
);
1299 ret
= seq_print_sym_short(s
, "%s", ip
);
1304 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
1305 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
1309 static void print_lat_help_header(struct seq_file
*m
)
1311 seq_puts(m
, "# _------=> CPU# \n");
1312 seq_puts(m
, "# / _-----=> irqs-off \n");
1313 seq_puts(m
, "# | / _----=> need-resched \n");
1314 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1315 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1316 seq_puts(m
, "# |||| / \n");
1317 seq_puts(m
, "# ||||| delay \n");
1318 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1319 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1322 static void print_func_help_header(struct seq_file
*m
)
1324 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1325 seq_puts(m
, "# | | | | |\n");
1330 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1332 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1333 struct trace_array
*tr
= iter
->tr
;
1334 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1335 struct tracer
*type
= current_trace
;
1336 unsigned long total
;
1337 unsigned long entries
;
1338 const char *name
= "preemption";
1343 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1345 ring_buffer_overruns(iter
->tr
->buffer
);
1347 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1349 seq_puts(m
, "-----------------------------------"
1350 "---------------------------------\n");
1351 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1352 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1353 nsecs_to_usecs(data
->saved_latency
),
1357 #if defined(CONFIG_PREEMPT_NONE)
1359 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1361 #elif defined(CONFIG_PREEMPT)
1366 /* These are reserved for later use */
1369 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1373 seq_puts(m
, " -----------------\n");
1374 seq_printf(m
, " | task: %.16s-%d "
1375 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1376 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1377 data
->policy
, data
->rt_priority
);
1378 seq_puts(m
, " -----------------\n");
1380 if (data
->critical_start
) {
1381 seq_puts(m
, " => started at: ");
1382 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1383 trace_print_seq(m
, &iter
->seq
);
1384 seq_puts(m
, "\n => ended at: ");
1385 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1386 trace_print_seq(m
, &iter
->seq
);
1394 lat_print_generic(struct trace_seq
*s
, struct trace_entry
*entry
, int cpu
)
1396 int hardirq
, softirq
;
1399 comm
= trace_find_cmdline(entry
->pid
);
1401 trace_seq_printf(s
, "%8.8s-%-5d ", comm
, entry
->pid
);
1402 trace_seq_printf(s
, "%3d", cpu
);
1403 trace_seq_printf(s
, "%c%c",
1404 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
1405 (entry
->flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ? 'X' : '.',
1406 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
1408 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
1409 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
1410 if (hardirq
&& softirq
) {
1411 trace_seq_putc(s
, 'H');
1414 trace_seq_putc(s
, 'h');
1417 trace_seq_putc(s
, 's');
1419 trace_seq_putc(s
, '.');
1423 if (entry
->preempt_count
)
1424 trace_seq_printf(s
, "%x", entry
->preempt_count
);
1426 trace_seq_puts(s
, ".");
1429 unsigned long preempt_mark_thresh
= 100;
1432 lat_print_timestamp(struct trace_seq
*s
, u64 abs_usecs
,
1433 unsigned long rel_usecs
)
1435 trace_seq_printf(s
, " %4lldus", abs_usecs
);
1436 if (rel_usecs
> preempt_mark_thresh
)
1437 trace_seq_puts(s
, "!: ");
1438 else if (rel_usecs
> 1)
1439 trace_seq_puts(s
, "+: ");
1441 trace_seq_puts(s
, " : ");
1444 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
1447 * The message is supposed to contain an ending newline.
1448 * If the printing stops prematurely, try to add a newline of our own.
1450 void trace_seq_print_cont(struct trace_seq
*s
, struct trace_iterator
*iter
)
1452 struct trace_entry
*ent
;
1453 struct trace_field_cont
*cont
;
1456 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1457 if (!ent
|| ent
->type
!= TRACE_CONT
) {
1458 trace_seq_putc(s
, '\n');
1463 cont
= (struct trace_field_cont
*)ent
;
1465 ok
= (trace_seq_printf(s
, "%s", cont
->buf
) > 0);
1467 ftrace_disable_cpu();
1469 if (iter
->buffer_iter
[iter
->cpu
])
1470 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1472 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, NULL
);
1474 ftrace_enable_cpu();
1476 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1477 } while (ent
&& ent
->type
== TRACE_CONT
);
1480 trace_seq_putc(s
, '\n');
1483 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1485 struct trace_seq
*s
= &iter
->seq
;
1487 if (cpu_isset(iter
->cpu
, iter
->started
))
1490 cpu_set(iter
->cpu
, iter
->started
);
1491 trace_seq_printf(s
, "##### CPU %u buffer started ####\n", iter
->cpu
);
1494 static enum print_line_t
1495 print_lat_fmt(struct trace_iterator
*iter
, unsigned int trace_idx
, int cpu
)
1497 struct trace_seq
*s
= &iter
->seq
;
1498 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1499 struct trace_entry
*next_entry
;
1500 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
1501 struct trace_entry
*entry
= iter
->ent
;
1502 unsigned long abs_usecs
;
1503 unsigned long rel_usecs
;
1510 if (entry
->type
== TRACE_CONT
)
1511 return TRACE_TYPE_HANDLED
;
1513 test_cpu_buff_start(iter
);
1515 next_entry
= find_next_entry(iter
, NULL
, &next_ts
);
1518 rel_usecs
= ns2usecs(next_ts
- iter
->ts
);
1519 abs_usecs
= ns2usecs(iter
->ts
- iter
->tr
->time_start
);
1522 comm
= trace_find_cmdline(entry
->pid
);
1523 trace_seq_printf(s
, "%16s %5d %3d %d %08x %08x [%08lx]"
1524 " %ld.%03ldms (+%ld.%03ldms): ",
1526 entry
->pid
, cpu
, entry
->flags
,
1527 entry
->preempt_count
, trace_idx
,
1530 abs_usecs
% 1000, rel_usecs
/1000,
1533 lat_print_generic(s
, entry
, cpu
);
1534 lat_print_timestamp(s
, abs_usecs
, rel_usecs
);
1536 switch (entry
->type
) {
1538 struct ftrace_entry
*field
;
1540 trace_assign_type(field
, entry
);
1542 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1543 trace_seq_puts(s
, " (");
1544 seq_print_ip_sym(s
, field
->parent_ip
, sym_flags
);
1545 trace_seq_puts(s
, ")\n");
1550 struct ctx_switch_entry
*field
;
1552 trace_assign_type(field
, entry
);
1554 T
= field
->next_state
< sizeof(state_to_char
) ?
1555 state_to_char
[field
->next_state
] : 'X';
1557 state
= field
->prev_state
?
1558 __ffs(field
->prev_state
) + 1 : 0;
1559 S
= state
< sizeof(state_to_char
) - 1 ? state_to_char
[state
] : 'X';
1560 comm
= trace_find_cmdline(field
->next_pid
);
1561 trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1564 S
, entry
->type
== TRACE_CTX
? "==>" : " +",
1571 case TRACE_SPECIAL
: {
1572 struct special_entry
*field
;
1574 trace_assign_type(field
, entry
);
1576 trace_seq_printf(s
, "# %ld %ld %ld\n",
1583 struct stack_entry
*field
;
1585 trace_assign_type(field
, entry
);
1587 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1589 trace_seq_puts(s
, " <= ");
1590 seq_print_ip_sym(s
, field
->caller
[i
], sym_flags
);
1592 trace_seq_puts(s
, "\n");
1596 struct print_entry
*field
;
1598 trace_assign_type(field
, entry
);
1600 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1601 trace_seq_printf(s
, ": %s", field
->buf
);
1602 if (entry
->flags
& TRACE_FLAG_CONT
)
1603 trace_seq_print_cont(s
, iter
);
1607 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
1609 return TRACE_TYPE_HANDLED
;
1612 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1614 struct trace_seq
*s
= &iter
->seq
;
1615 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1616 struct trace_entry
*entry
;
1617 unsigned long usec_rem
;
1618 unsigned long long t
;
1627 if (entry
->type
== TRACE_CONT
)
1628 return TRACE_TYPE_HANDLED
;
1630 test_cpu_buff_start(iter
);
1632 comm
= trace_find_cmdline(iter
->ent
->pid
);
1634 t
= ns2usecs(iter
->ts
);
1635 usec_rem
= do_div(t
, 1000000ULL);
1636 secs
= (unsigned long)t
;
1638 ret
= trace_seq_printf(s
, "%16s-%-5d ", comm
, entry
->pid
);
1640 return TRACE_TYPE_PARTIAL_LINE
;
1641 ret
= trace_seq_printf(s
, "[%03d] ", iter
->cpu
);
1643 return TRACE_TYPE_PARTIAL_LINE
;
1644 ret
= trace_seq_printf(s
, "%5lu.%06lu: ", secs
, usec_rem
);
1646 return TRACE_TYPE_PARTIAL_LINE
;
1648 switch (entry
->type
) {
1650 struct ftrace_entry
*field
;
1652 trace_assign_type(field
, entry
);
1654 ret
= seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1656 return TRACE_TYPE_PARTIAL_LINE
;
1657 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
1659 ret
= trace_seq_printf(s
, " <-");
1661 return TRACE_TYPE_PARTIAL_LINE
;
1662 ret
= seq_print_ip_sym(s
,
1666 return TRACE_TYPE_PARTIAL_LINE
;
1668 ret
= trace_seq_printf(s
, "\n");
1670 return TRACE_TYPE_PARTIAL_LINE
;
1675 struct ctx_switch_entry
*field
;
1677 trace_assign_type(field
, entry
);
1679 S
= field
->prev_state
< sizeof(state_to_char
) ?
1680 state_to_char
[field
->prev_state
] : 'X';
1681 T
= field
->next_state
< sizeof(state_to_char
) ?
1682 state_to_char
[field
->next_state
] : 'X';
1683 ret
= trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1687 entry
->type
== TRACE_CTX
? "==>" : " +",
1693 return TRACE_TYPE_PARTIAL_LINE
;
1696 case TRACE_SPECIAL
: {
1697 struct special_entry
*field
;
1699 trace_assign_type(field
, entry
);
1701 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1706 return TRACE_TYPE_PARTIAL_LINE
;
1710 struct stack_entry
*field
;
1712 trace_assign_type(field
, entry
);
1714 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1716 ret
= trace_seq_puts(s
, " <= ");
1718 return TRACE_TYPE_PARTIAL_LINE
;
1720 ret
= seq_print_ip_sym(s
, field
->caller
[i
],
1723 return TRACE_TYPE_PARTIAL_LINE
;
1725 ret
= trace_seq_puts(s
, "\n");
1727 return TRACE_TYPE_PARTIAL_LINE
;
1731 struct print_entry
*field
;
1733 trace_assign_type(field
, entry
);
1735 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1736 trace_seq_printf(s
, ": %s", field
->buf
);
1737 if (entry
->flags
& TRACE_FLAG_CONT
)
1738 trace_seq_print_cont(s
, iter
);
1742 return TRACE_TYPE_HANDLED
;
1745 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1747 struct trace_seq
*s
= &iter
->seq
;
1748 struct trace_entry
*entry
;
1754 if (entry
->type
== TRACE_CONT
)
1755 return TRACE_TYPE_HANDLED
;
1757 ret
= trace_seq_printf(s
, "%d %d %llu ",
1758 entry
->pid
, iter
->cpu
, iter
->ts
);
1760 return TRACE_TYPE_PARTIAL_LINE
;
1762 switch (entry
->type
) {
1764 struct ftrace_entry
*field
;
1766 trace_assign_type(field
, entry
);
1768 ret
= trace_seq_printf(s
, "%x %x\n",
1772 return TRACE_TYPE_PARTIAL_LINE
;
1777 struct ctx_switch_entry
*field
;
1779 trace_assign_type(field
, entry
);
1781 S
= field
->prev_state
< sizeof(state_to_char
) ?
1782 state_to_char
[field
->prev_state
] : 'X';
1783 T
= field
->next_state
< sizeof(state_to_char
) ?
1784 state_to_char
[field
->next_state
] : 'X';
1785 if (entry
->type
== TRACE_WAKE
)
1787 ret
= trace_seq_printf(s
, "%d %d %c %d %d %d %c\n",
1796 return TRACE_TYPE_PARTIAL_LINE
;
1801 struct special_entry
*field
;
1803 trace_assign_type(field
, entry
);
1805 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1810 return TRACE_TYPE_PARTIAL_LINE
;
1814 struct print_entry
*field
;
1816 trace_assign_type(field
, entry
);
1818 trace_seq_printf(s
, "# %lx %s", field
->ip
, field
->buf
);
1819 if (entry
->flags
& TRACE_FLAG_CONT
)
1820 trace_seq_print_cont(s
, iter
);
1824 return TRACE_TYPE_HANDLED
;
1827 #define SEQ_PUT_FIELD_RET(s, x) \
1829 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1833 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1835 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1836 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1840 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1842 struct trace_seq
*s
= &iter
->seq
;
1843 unsigned char newline
= '\n';
1844 struct trace_entry
*entry
;
1849 if (entry
->type
== TRACE_CONT
)
1850 return TRACE_TYPE_HANDLED
;
1852 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1853 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1854 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1856 switch (entry
->type
) {
1858 struct ftrace_entry
*field
;
1860 trace_assign_type(field
, entry
);
1862 SEQ_PUT_HEX_FIELD_RET(s
, field
->ip
);
1863 SEQ_PUT_HEX_FIELD_RET(s
, field
->parent_ip
);
1868 struct ctx_switch_entry
*field
;
1870 trace_assign_type(field
, entry
);
1872 S
= field
->prev_state
< sizeof(state_to_char
) ?
1873 state_to_char
[field
->prev_state
] : 'X';
1874 T
= field
->next_state
< sizeof(state_to_char
) ?
1875 state_to_char
[field
->next_state
] : 'X';
1876 if (entry
->type
== TRACE_WAKE
)
1878 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_pid
);
1879 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_prio
);
1880 SEQ_PUT_HEX_FIELD_RET(s
, S
);
1881 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_cpu
);
1882 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_pid
);
1883 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_prio
);
1884 SEQ_PUT_HEX_FIELD_RET(s
, T
);
1889 struct special_entry
*field
;
1891 trace_assign_type(field
, entry
);
1893 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg1
);
1894 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg2
);
1895 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg3
);
1899 SEQ_PUT_FIELD_RET(s
, newline
);
1901 return TRACE_TYPE_HANDLED
;
1904 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1906 struct trace_seq
*s
= &iter
->seq
;
1907 struct trace_entry
*entry
;
1911 if (entry
->type
== TRACE_CONT
)
1912 return TRACE_TYPE_HANDLED
;
1914 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1915 SEQ_PUT_FIELD_RET(s
, entry
->cpu
);
1916 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1918 switch (entry
->type
) {
1920 struct ftrace_entry
*field
;
1922 trace_assign_type(field
, entry
);
1924 SEQ_PUT_FIELD_RET(s
, field
->ip
);
1925 SEQ_PUT_FIELD_RET(s
, field
->parent_ip
);
1929 struct ctx_switch_entry
*field
;
1931 trace_assign_type(field
, entry
);
1933 SEQ_PUT_FIELD_RET(s
, field
->prev_pid
);
1934 SEQ_PUT_FIELD_RET(s
, field
->prev_prio
);
1935 SEQ_PUT_FIELD_RET(s
, field
->prev_state
);
1936 SEQ_PUT_FIELD_RET(s
, field
->next_pid
);
1937 SEQ_PUT_FIELD_RET(s
, field
->next_prio
);
1938 SEQ_PUT_FIELD_RET(s
, field
->next_state
);
1943 struct special_entry
*field
;
1945 trace_assign_type(field
, entry
);
1947 SEQ_PUT_FIELD_RET(s
, field
->arg1
);
1948 SEQ_PUT_FIELD_RET(s
, field
->arg2
);
1949 SEQ_PUT_FIELD_RET(s
, field
->arg3
);
1956 static int trace_empty(struct trace_iterator
*iter
)
1960 for_each_tracing_cpu(cpu
) {
1961 if (iter
->buffer_iter
[cpu
]) {
1962 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1965 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1973 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1975 enum print_line_t ret
;
1977 if (iter
->trace
&& iter
->trace
->print_line
) {
1978 ret
= iter
->trace
->print_line(iter
);
1979 if (ret
!= TRACE_TYPE_UNHANDLED
)
1983 if (trace_flags
& TRACE_ITER_BIN
)
1984 return print_bin_fmt(iter
);
1986 if (trace_flags
& TRACE_ITER_HEX
)
1987 return print_hex_fmt(iter
);
1989 if (trace_flags
& TRACE_ITER_RAW
)
1990 return print_raw_fmt(iter
);
1992 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
1993 return print_lat_fmt(iter
, iter
->idx
, iter
->cpu
);
1995 return print_trace_fmt(iter
);
1998 static int s_show(struct seq_file
*m
, void *v
)
2000 struct trace_iterator
*iter
= v
;
2002 if (iter
->ent
== NULL
) {
2004 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
2007 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2008 /* print nothing if the buffers are empty */
2009 if (trace_empty(iter
))
2011 print_trace_header(m
, iter
);
2012 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2013 print_lat_help_header(m
);
2015 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2016 print_func_help_header(m
);
2019 print_trace_line(iter
);
2020 trace_print_seq(m
, &iter
->seq
);
2026 static struct seq_operations tracer_seq_ops
= {
2033 static struct trace_iterator
*
2034 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
2036 struct trace_iterator
*iter
;
2040 if (tracing_disabled
) {
2045 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2051 mutex_lock(&trace_types_lock
);
2052 if (current_trace
&& current_trace
->print_max
)
2055 iter
->tr
= inode
->i_private
;
2056 iter
->trace
= current_trace
;
2059 for_each_tracing_cpu(cpu
) {
2061 iter
->buffer_iter
[cpu
] =
2062 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2064 if (!iter
->buffer_iter
[cpu
])
2068 /* TODO stop tracer */
2069 *ret
= seq_open(file
, &tracer_seq_ops
);
2073 m
= file
->private_data
;
2076 /* stop the trace while dumping */
2079 if (iter
->trace
&& iter
->trace
->open
)
2080 iter
->trace
->open(iter
);
2082 mutex_unlock(&trace_types_lock
);
2088 for_each_tracing_cpu(cpu
) {
2089 if (iter
->buffer_iter
[cpu
])
2090 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2092 mutex_unlock(&trace_types_lock
);
2094 return ERR_PTR(-ENOMEM
);
2097 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2099 if (tracing_disabled
)
2102 filp
->private_data
= inode
->i_private
;
2106 int tracing_release(struct inode
*inode
, struct file
*file
)
2108 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2109 struct trace_iterator
*iter
= m
->private;
2112 mutex_lock(&trace_types_lock
);
2113 for_each_tracing_cpu(cpu
) {
2114 if (iter
->buffer_iter
[cpu
])
2115 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2118 if (iter
->trace
&& iter
->trace
->close
)
2119 iter
->trace
->close(iter
);
2121 /* reenable tracing if it was previously enabled */
2123 mutex_unlock(&trace_types_lock
);
2125 seq_release(inode
, file
);
2130 static int tracing_open(struct inode
*inode
, struct file
*file
)
2134 __tracing_open(inode
, file
, &ret
);
2139 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
2141 struct trace_iterator
*iter
;
2144 iter
= __tracing_open(inode
, file
, &ret
);
2147 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2154 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2156 struct tracer
*t
= m
->private;
2168 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2170 struct tracer
*t
= m
->private;
2173 mutex_lock(&trace_types_lock
);
2174 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2180 static void t_stop(struct seq_file
*m
, void *p
)
2182 mutex_unlock(&trace_types_lock
);
2185 static int t_show(struct seq_file
*m
, void *v
)
2187 struct tracer
*t
= v
;
2192 seq_printf(m
, "%s", t
->name
);
2201 static struct seq_operations show_traces_seq_ops
= {
2208 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2212 if (tracing_disabled
)
2215 ret
= seq_open(file
, &show_traces_seq_ops
);
2217 struct seq_file
*m
= file
->private_data
;
2218 m
->private = trace_types
;
2224 static struct file_operations tracing_fops
= {
2225 .open
= tracing_open
,
2227 .llseek
= seq_lseek
,
2228 .release
= tracing_release
,
2231 static struct file_operations tracing_lt_fops
= {
2232 .open
= tracing_lt_open
,
2234 .llseek
= seq_lseek
,
2235 .release
= tracing_release
,
2238 static struct file_operations show_traces_fops
= {
2239 .open
= show_traces_open
,
2241 .release
= seq_release
,
2245 * Only trace on a CPU if the bitmask is set:
2247 static cpumask_t tracing_cpumask
= CPU_MASK_ALL
;
2250 * When tracing/tracing_cpu_mask is modified then this holds
2251 * the new bitmask we are about to install:
2253 static cpumask_t tracing_cpumask_new
;
2256 * The tracer itself will not take this lock, but still we want
2257 * to provide a consistent cpumask to user-space:
2259 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2262 * Temporary storage for the character representation of the
2263 * CPU bitmask (and one more byte for the newline):
2265 static char mask_str
[NR_CPUS
+ 1];
2268 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2269 size_t count
, loff_t
*ppos
)
2273 mutex_lock(&tracing_cpumask_update_lock
);
2275 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2276 if (count
- len
< 2) {
2280 len
+= sprintf(mask_str
+ len
, "\n");
2281 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2284 mutex_unlock(&tracing_cpumask_update_lock
);
2290 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2291 size_t count
, loff_t
*ppos
)
2295 mutex_lock(&tracing_cpumask_update_lock
);
2296 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2300 raw_local_irq_disable();
2301 __raw_spin_lock(&ftrace_max_lock
);
2302 for_each_tracing_cpu(cpu
) {
2304 * Increase/decrease the disabled counter if we are
2305 * about to flip a bit in the cpumask:
2307 if (cpu_isset(cpu
, tracing_cpumask
) &&
2308 !cpu_isset(cpu
, tracing_cpumask_new
)) {
2309 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2311 if (!cpu_isset(cpu
, tracing_cpumask
) &&
2312 cpu_isset(cpu
, tracing_cpumask_new
)) {
2313 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2316 __raw_spin_unlock(&ftrace_max_lock
);
2317 raw_local_irq_enable();
2319 tracing_cpumask
= tracing_cpumask_new
;
2321 mutex_unlock(&tracing_cpumask_update_lock
);
2326 mutex_unlock(&tracing_cpumask_update_lock
);
2331 static struct file_operations tracing_cpumask_fops
= {
2332 .open
= tracing_open_generic
,
2333 .read
= tracing_cpumask_read
,
2334 .write
= tracing_cpumask_write
,
2338 tracing_iter_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2339 size_t cnt
, loff_t
*ppos
)
2346 /* calulate max size */
2347 for (i
= 0; trace_options
[i
]; i
++) {
2348 len
+= strlen(trace_options
[i
]);
2349 len
+= 3; /* "no" and space */
2352 /* +2 for \n and \0 */
2353 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2357 for (i
= 0; trace_options
[i
]; i
++) {
2358 if (trace_flags
& (1 << i
))
2359 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
2361 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
2364 r
+= sprintf(buf
+ r
, "\n");
2365 WARN_ON(r
>= len
+ 2);
2367 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2375 tracing_iter_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2376 size_t cnt
, loff_t
*ppos
)
2383 if (cnt
>= sizeof(buf
))
2386 if (copy_from_user(&buf
, ubuf
, cnt
))
2391 if (strncmp(buf
, "no", 2) == 0) {
2396 for (i
= 0; trace_options
[i
]; i
++) {
2397 int len
= strlen(trace_options
[i
]);
2399 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2401 trace_flags
&= ~(1 << i
);
2403 trace_flags
|= (1 << i
);
2408 * If no option could be set, return an error:
2410 if (!trace_options
[i
])
2418 static struct file_operations tracing_iter_fops
= {
2419 .open
= tracing_open_generic
,
2420 .read
= tracing_iter_ctrl_read
,
2421 .write
= tracing_iter_ctrl_write
,
2424 static const char readme_msg
[] =
2425 "tracing mini-HOWTO:\n\n"
2427 "# mount -t debugfs nodev /debug\n\n"
2428 "# cat /debug/tracing/available_tracers\n"
2429 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2430 "# cat /debug/tracing/current_tracer\n"
2432 "# echo sched_switch > /debug/tracing/current_tracer\n"
2433 "# cat /debug/tracing/current_tracer\n"
2435 "# cat /debug/tracing/iter_ctrl\n"
2436 "noprint-parent nosym-offset nosym-addr noverbose\n"
2437 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2438 "# echo 1 > /debug/tracing/tracing_enabled\n"
2439 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2440 "echo 0 > /debug/tracing/tracing_enabled\n"
2444 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2445 size_t cnt
, loff_t
*ppos
)
2447 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2448 readme_msg
, strlen(readme_msg
));
2451 static struct file_operations tracing_readme_fops
= {
2452 .open
= tracing_open_generic
,
2453 .read
= tracing_readme_read
,
2457 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2458 size_t cnt
, loff_t
*ppos
)
2463 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2464 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2468 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2469 size_t cnt
, loff_t
*ppos
)
2471 struct trace_array
*tr
= filp
->private_data
;
2476 if (cnt
>= sizeof(buf
))
2479 if (copy_from_user(&buf
, ubuf
, cnt
))
2484 ret
= strict_strtoul(buf
, 10, &val
);
2490 mutex_lock(&trace_types_lock
);
2491 if (tracer_enabled
^ val
) {
2494 if (current_trace
->start
)
2495 current_trace
->start(tr
);
2500 if (current_trace
->stop
)
2501 current_trace
->stop(tr
);
2504 mutex_unlock(&trace_types_lock
);
2512 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2513 size_t cnt
, loff_t
*ppos
)
2515 char buf
[max_tracer_type_len
+2];
2518 mutex_lock(&trace_types_lock
);
2520 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2522 r
= sprintf(buf
, "\n");
2523 mutex_unlock(&trace_types_lock
);
2525 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2528 static int tracing_set_tracer(char *buf
)
2530 struct trace_array
*tr
= &global_trace
;
2534 mutex_lock(&trace_types_lock
);
2535 for (t
= trace_types
; t
; t
= t
->next
) {
2536 if (strcmp(t
->name
, buf
) == 0)
2543 if (t
== current_trace
)
2546 if (current_trace
&& current_trace
->reset
)
2547 current_trace
->reset(tr
);
2554 mutex_unlock(&trace_types_lock
);
2560 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2561 size_t cnt
, loff_t
*ppos
)
2563 char buf
[max_tracer_type_len
+1];
2567 if (cnt
> max_tracer_type_len
)
2568 cnt
= max_tracer_type_len
;
2570 if (copy_from_user(&buf
, ubuf
, cnt
))
2575 /* strip ending whitespace. */
2576 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2579 ret
= tracing_set_tracer(buf
);
2590 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2591 size_t cnt
, loff_t
*ppos
)
2593 unsigned long *ptr
= filp
->private_data
;
2597 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2598 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2599 if (r
> sizeof(buf
))
2601 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2605 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2606 size_t cnt
, loff_t
*ppos
)
2608 long *ptr
= filp
->private_data
;
2613 if (cnt
>= sizeof(buf
))
2616 if (copy_from_user(&buf
, ubuf
, cnt
))
2621 ret
= strict_strtoul(buf
, 10, &val
);
2630 static atomic_t tracing_reader
;
2632 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2634 struct trace_iterator
*iter
;
2636 if (tracing_disabled
)
2639 /* We only allow for reader of the pipe */
2640 if (atomic_inc_return(&tracing_reader
) != 1) {
2641 atomic_dec(&tracing_reader
);
2645 /* create a buffer to store the information to pass to userspace */
2646 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2650 mutex_lock(&trace_types_lock
);
2652 /* trace pipe does not show start of buffer */
2653 cpus_setall(iter
->started
);
2655 iter
->tr
= &global_trace
;
2656 iter
->trace
= current_trace
;
2657 filp
->private_data
= iter
;
2659 if (iter
->trace
->pipe_open
)
2660 iter
->trace
->pipe_open(iter
);
2661 mutex_unlock(&trace_types_lock
);
2666 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2668 struct trace_iterator
*iter
= file
->private_data
;
2671 atomic_dec(&tracing_reader
);
2677 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2679 struct trace_iterator
*iter
= filp
->private_data
;
2681 if (trace_flags
& TRACE_ITER_BLOCK
) {
2683 * Always select as readable when in blocking mode
2685 return POLLIN
| POLLRDNORM
;
2687 if (!trace_empty(iter
))
2688 return POLLIN
| POLLRDNORM
;
2689 poll_wait(filp
, &trace_wait
, poll_table
);
2690 if (!trace_empty(iter
))
2691 return POLLIN
| POLLRDNORM
;
2701 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2702 size_t cnt
, loff_t
*ppos
)
2704 struct trace_iterator
*iter
= filp
->private_data
;
2707 /* return any leftover data */
2708 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2712 trace_seq_reset(&iter
->seq
);
2714 mutex_lock(&trace_types_lock
);
2715 if (iter
->trace
->read
) {
2716 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2723 while (trace_empty(iter
)) {
2725 if ((filp
->f_flags
& O_NONBLOCK
)) {
2731 * This is a make-shift waitqueue. The reason we don't use
2732 * an actual wait queue is because:
2733 * 1) we only ever have one waiter
2734 * 2) the tracing, traces all functions, we don't want
2735 * the overhead of calling wake_up and friends
2736 * (and tracing them too)
2737 * Anyway, this is really very primitive wakeup.
2739 set_current_state(TASK_INTERRUPTIBLE
);
2740 iter
->tr
->waiter
= current
;
2742 mutex_unlock(&trace_types_lock
);
2744 /* sleep for 100 msecs, and try again. */
2745 schedule_timeout(HZ
/10);
2747 mutex_lock(&trace_types_lock
);
2749 iter
->tr
->waiter
= NULL
;
2751 if (signal_pending(current
)) {
2756 if (iter
->trace
!= current_trace
)
2760 * We block until we read something and tracing is disabled.
2761 * We still block if tracing is disabled, but we have never
2762 * read anything. This allows a user to cat this file, and
2763 * then enable tracing. But after we have read something,
2764 * we give an EOF when tracing is again disabled.
2766 * iter->pos will be 0 if we haven't read anything.
2768 if (!tracer_enabled
&& iter
->pos
)
2774 /* stop when tracing is finished */
2775 if (trace_empty(iter
))
2778 if (cnt
>= PAGE_SIZE
)
2779 cnt
= PAGE_SIZE
- 1;
2781 /* reset all but tr, trace, and overruns */
2782 memset(&iter
->seq
, 0,
2783 sizeof(struct trace_iterator
) -
2784 offsetof(struct trace_iterator
, seq
));
2787 while (find_next_entry_inc(iter
) != NULL
) {
2788 enum print_line_t ret
;
2789 int len
= iter
->seq
.len
;
2791 ret
= print_trace_line(iter
);
2792 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2793 /* don't print partial lines */
2794 iter
->seq
.len
= len
;
2798 trace_consume(iter
);
2800 if (iter
->seq
.len
>= cnt
)
2804 /* Now copy what we have to the user */
2805 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2806 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2807 trace_seq_reset(&iter
->seq
);
2810 * If there was nothing to send to user, inspite of consuming trace
2811 * entries, go back to wait for more entries.
2817 mutex_unlock(&trace_types_lock
);
2823 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
2824 size_t cnt
, loff_t
*ppos
)
2826 struct trace_array
*tr
= filp
->private_data
;
2830 r
= sprintf(buf
, "%lu\n", tr
->entries
);
2831 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2835 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
2836 size_t cnt
, loff_t
*ppos
)
2842 if (cnt
>= sizeof(buf
))
2845 if (copy_from_user(&buf
, ubuf
, cnt
))
2850 ret
= strict_strtoul(buf
, 10, &val
);
2854 /* must have at least 1 entry */
2858 mutex_lock(&trace_types_lock
);
2862 /* disable all cpu buffers */
2863 for_each_tracing_cpu(cpu
) {
2864 if (global_trace
.data
[cpu
])
2865 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2866 if (max_tr
.data
[cpu
])
2867 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
2870 if (val
!= global_trace
.entries
) {
2871 ret
= ring_buffer_resize(global_trace
.buffer
, val
);
2877 ret
= ring_buffer_resize(max_tr
.buffer
, val
);
2881 r
= ring_buffer_resize(global_trace
.buffer
,
2882 global_trace
.entries
);
2884 /* AARGH! We are left with different
2885 * size max buffer!!!! */
2887 tracing_disabled
= 1;
2892 global_trace
.entries
= val
;
2897 /* If check pages failed, return ENOMEM */
2898 if (tracing_disabled
)
2901 for_each_tracing_cpu(cpu
) {
2902 if (global_trace
.data
[cpu
])
2903 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2904 if (max_tr
.data
[cpu
])
2905 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
2909 max_tr
.entries
= global_trace
.entries
;
2910 mutex_unlock(&trace_types_lock
);
2915 static int mark_printk(const char *fmt
, ...)
2919 va_start(args
, fmt
);
2920 ret
= trace_vprintk(0, fmt
, args
);
2926 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
2927 size_t cnt
, loff_t
*fpos
)
2932 if (tracing_disabled
)
2935 if (cnt
> TRACE_BUF_SIZE
)
2936 cnt
= TRACE_BUF_SIZE
;
2938 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
2942 if (copy_from_user(buf
, ubuf
, cnt
)) {
2947 /* Cut from the first nil or newline. */
2949 end
= strchr(buf
, '\n');
2953 cnt
= mark_printk("%s\n", buf
);
2960 static struct file_operations tracing_max_lat_fops
= {
2961 .open
= tracing_open_generic
,
2962 .read
= tracing_max_lat_read
,
2963 .write
= tracing_max_lat_write
,
2966 static struct file_operations tracing_ctrl_fops
= {
2967 .open
= tracing_open_generic
,
2968 .read
= tracing_ctrl_read
,
2969 .write
= tracing_ctrl_write
,
2972 static struct file_operations set_tracer_fops
= {
2973 .open
= tracing_open_generic
,
2974 .read
= tracing_set_trace_read
,
2975 .write
= tracing_set_trace_write
,
2978 static struct file_operations tracing_pipe_fops
= {
2979 .open
= tracing_open_pipe
,
2980 .poll
= tracing_poll_pipe
,
2981 .read
= tracing_read_pipe
,
2982 .release
= tracing_release_pipe
,
2985 static struct file_operations tracing_entries_fops
= {
2986 .open
= tracing_open_generic
,
2987 .read
= tracing_entries_read
,
2988 .write
= tracing_entries_write
,
2991 static struct file_operations tracing_mark_fops
= {
2992 .open
= tracing_open_generic
,
2993 .write
= tracing_mark_write
,
2996 #ifdef CONFIG_DYNAMIC_FTRACE
2998 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3004 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3005 size_t cnt
, loff_t
*ppos
)
3007 static char ftrace_dyn_info_buffer
[1024];
3008 static DEFINE_MUTEX(dyn_info_mutex
);
3009 unsigned long *p
= filp
->private_data
;
3010 char *buf
= ftrace_dyn_info_buffer
;
3011 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3014 mutex_lock(&dyn_info_mutex
);
3015 r
= sprintf(buf
, "%ld ", *p
);
3017 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3020 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3022 mutex_unlock(&dyn_info_mutex
);
3027 static struct file_operations tracing_dyn_info_fops
= {
3028 .open
= tracing_open_generic
,
3029 .read
= tracing_read_dyn_info
,
3033 static struct dentry
*d_tracer
;
3035 struct dentry
*tracing_init_dentry(void)
3042 d_tracer
= debugfs_create_dir("tracing", NULL
);
3044 if (!d_tracer
&& !once
) {
3046 pr_warning("Could not create debugfs directory 'tracing'\n");
3053 #ifdef CONFIG_FTRACE_SELFTEST
3054 /* Let selftest have access to static functions in this file */
3055 #include "trace_selftest.c"
3058 static __init
int tracer_init_debugfs(void)
3060 struct dentry
*d_tracer
;
3061 struct dentry
*entry
;
3063 d_tracer
= tracing_init_dentry();
3065 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
3066 &global_trace
, &tracing_ctrl_fops
);
3068 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3070 entry
= debugfs_create_file("iter_ctrl", 0644, d_tracer
,
3071 NULL
, &tracing_iter_fops
);
3073 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
3075 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
3076 NULL
, &tracing_cpumask_fops
);
3078 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3080 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
3081 &global_trace
, &tracing_lt_fops
);
3083 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3085 entry
= debugfs_create_file("trace", 0444, d_tracer
,
3086 &global_trace
, &tracing_fops
);
3088 pr_warning("Could not create debugfs 'trace' entry\n");
3090 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
3091 &global_trace
, &show_traces_fops
);
3093 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3095 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
3096 &global_trace
, &set_tracer_fops
);
3098 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3100 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
3101 &tracing_max_latency
,
3102 &tracing_max_lat_fops
);
3104 pr_warning("Could not create debugfs "
3105 "'tracing_max_latency' entry\n");
3107 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
3108 &tracing_thresh
, &tracing_max_lat_fops
);
3110 pr_warning("Could not create debugfs "
3111 "'tracing_thresh' entry\n");
3112 entry
= debugfs_create_file("README", 0644, d_tracer
,
3113 NULL
, &tracing_readme_fops
);
3115 pr_warning("Could not create debugfs 'README' entry\n");
3117 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
3118 NULL
, &tracing_pipe_fops
);
3120 pr_warning("Could not create debugfs "
3121 "'trace_pipe' entry\n");
3123 entry
= debugfs_create_file("trace_entries", 0644, d_tracer
,
3124 &global_trace
, &tracing_entries_fops
);
3126 pr_warning("Could not create debugfs "
3127 "'trace_entries' entry\n");
3129 entry
= debugfs_create_file("trace_marker", 0220, d_tracer
,
3130 NULL
, &tracing_mark_fops
);
3132 pr_warning("Could not create debugfs "
3133 "'trace_marker' entry\n");
3135 #ifdef CONFIG_DYNAMIC_FTRACE
3136 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
3137 &ftrace_update_tot_cnt
,
3138 &tracing_dyn_info_fops
);
3140 pr_warning("Could not create debugfs "
3141 "'dyn_ftrace_total_info' entry\n");
3143 #ifdef CONFIG_SYSPROF_TRACER
3144 init_tracer_sysprof_debugfs(d_tracer
);
3149 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
3151 static DEFINE_SPINLOCK(trace_buf_lock
);
3152 static char trace_buf
[TRACE_BUF_SIZE
];
3154 struct ring_buffer_event
*event
;
3155 struct trace_array
*tr
= &global_trace
;
3156 struct trace_array_cpu
*data
;
3157 struct print_entry
*entry
;
3158 unsigned long flags
, irq_flags
;
3159 int cpu
, len
= 0, size
, pc
;
3161 if (tracing_disabled
)
3164 pc
= preempt_count();
3165 preempt_disable_notrace();
3166 cpu
= raw_smp_processor_id();
3167 data
= tr
->data
[cpu
];
3169 if (unlikely(atomic_read(&data
->disabled
)))
3172 spin_lock_irqsave(&trace_buf_lock
, flags
);
3173 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
3175 len
= min(len
, TRACE_BUF_SIZE
-1);
3178 size
= sizeof(*entry
) + len
+ 1;
3179 event
= ring_buffer_lock_reserve(tr
->buffer
, size
, &irq_flags
);
3182 entry
= ring_buffer_event_data(event
);
3183 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
3184 entry
->ent
.type
= TRACE_PRINT
;
3187 memcpy(&entry
->buf
, trace_buf
, len
);
3188 entry
->buf
[len
] = 0;
3189 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
3192 spin_unlock_irqrestore(&trace_buf_lock
, flags
);
3195 preempt_enable_notrace();
3199 EXPORT_SYMBOL_GPL(trace_vprintk
);
3201 int __ftrace_printk(unsigned long ip
, const char *fmt
, ...)
3206 if (!(trace_flags
& TRACE_ITER_PRINTK
))
3210 ret
= trace_vprintk(ip
, fmt
, ap
);
3214 EXPORT_SYMBOL_GPL(__ftrace_printk
);
3216 static int trace_panic_handler(struct notifier_block
*this,
3217 unsigned long event
, void *unused
)
3219 if (ftrace_dump_on_oops
)
3224 static struct notifier_block trace_panic_notifier
= {
3225 .notifier_call
= trace_panic_handler
,
3227 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
3230 static int trace_die_handler(struct notifier_block
*self
,
3236 if (ftrace_dump_on_oops
)
3245 static struct notifier_block trace_die_notifier
= {
3246 .notifier_call
= trace_die_handler
,
3251 * printk is set to max of 1024, we really don't need it that big.
3252 * Nothing should be printing 1000 characters anyway.
3254 #define TRACE_MAX_PRINT 1000
3257 * Define here KERN_TRACE so that we have one place to modify
3258 * it if we decide to change what log level the ftrace dump
3261 #define KERN_TRACE KERN_INFO
3264 trace_printk_seq(struct trace_seq
*s
)
3266 /* Probably should print a warning here. */
3270 /* should be zero ended, but we are paranoid. */
3271 s
->buffer
[s
->len
] = 0;
3273 printk(KERN_TRACE
"%s", s
->buffer
);
3278 void ftrace_dump(void)
3280 static DEFINE_SPINLOCK(ftrace_dump_lock
);
3281 /* use static because iter can be a bit big for the stack */
3282 static struct trace_iterator iter
;
3283 static cpumask_t mask
;
3284 static int dump_ran
;
3285 unsigned long flags
;
3289 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
3295 /* No turning back! */
3298 for_each_tracing_cpu(cpu
) {
3299 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3302 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
3304 iter
.tr
= &global_trace
;
3305 iter
.trace
= current_trace
;
3308 * We need to stop all tracing on all CPUS to read the
3309 * the next buffer. This is a bit expensive, but is
3310 * not done often. We fill all what we can read,
3311 * and then release the locks again.
3316 while (!trace_empty(&iter
)) {
3319 printk(KERN_TRACE
"---------------------------------\n");
3323 /* reset all but tr, trace, and overruns */
3324 memset(&iter
.seq
, 0,
3325 sizeof(struct trace_iterator
) -
3326 offsetof(struct trace_iterator
, seq
));
3327 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
3330 if (find_next_entry_inc(&iter
) != NULL
) {
3331 print_trace_line(&iter
);
3332 trace_consume(&iter
);
3335 trace_printk_seq(&iter
.seq
);
3339 printk(KERN_TRACE
" (ftrace buffer empty)\n");
3341 printk(KERN_TRACE
"---------------------------------\n");
3344 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
3347 __init
static int tracer_alloc_buffers(void)
3349 struct trace_array_cpu
*data
;
3352 /* TODO: make the number of buffers hot pluggable with CPUS */
3353 tracing_buffer_mask
= cpu_possible_map
;
3355 global_trace
.buffer
= ring_buffer_alloc(trace_buf_size
,
3356 TRACE_BUFFER_FLAGS
);
3357 if (!global_trace
.buffer
) {
3358 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
3362 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
3364 #ifdef CONFIG_TRACER_MAX_TRACE
3365 max_tr
.buffer
= ring_buffer_alloc(trace_buf_size
,
3366 TRACE_BUFFER_FLAGS
);
3367 if (!max_tr
.buffer
) {
3368 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
3370 ring_buffer_free(global_trace
.buffer
);
3373 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
3374 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
3377 /* Allocate the first page for all buffers */
3378 for_each_tracing_cpu(i
) {
3379 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
3380 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
3383 trace_init_cmdlines();
3385 register_tracer(&nop_trace
);
3386 #ifdef CONFIG_BOOT_TRACER
3387 register_tracer(&boot_tracer
);
3388 current_trace
= &boot_tracer
;
3389 current_trace
->init(&global_trace
);
3391 current_trace
= &nop_trace
;
3394 /* All seems OK, enable tracing */
3395 tracing_disabled
= 0;
3397 atomic_notifier_chain_register(&panic_notifier_list
,
3398 &trace_panic_notifier
);
3400 register_die_notifier(&trace_die_notifier
);
3404 early_initcall(tracer_alloc_buffers
);
3405 fs_initcall(tracer_init_debugfs
);