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
;
46 static DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
48 static inline void ftrace_disable_cpu(void)
51 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
54 static inline void ftrace_enable_cpu(void)
56 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
60 static cpumask_t __read_mostly tracing_buffer_mask
;
62 #define for_each_tracing_cpu(cpu) \
63 for_each_cpu_mask(cpu, tracing_buffer_mask)
65 static int tracing_disabled
= 1;
68 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
70 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
71 * is set, then ftrace_dump is called. This will output the contents
72 * of the ftrace buffers to the console. This is very useful for
73 * capturing traces that lead to crashes and outputing it to a
76 * It is default off, but you can enable it with either specifying
77 * "ftrace_dump_on_oops" in the kernel command line, or setting
78 * /proc/sys/kernel/ftrace_dump_on_oops to true.
80 int ftrace_dump_on_oops
;
82 static int tracing_set_tracer(char *buf
);
84 static int __init
set_ftrace(char *str
)
86 tracing_set_tracer(str
);
89 __setup("ftrace", set_ftrace
);
91 static int __init
set_ftrace_dump_on_oops(char *str
)
93 ftrace_dump_on_oops
= 1;
96 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
99 ns2usecs(cycle_t nsec
)
106 cycle_t
ftrace_now(int cpu
)
108 u64 ts
= ring_buffer_time_stamp(cpu
);
109 ring_buffer_normalize_time_stamp(cpu
, &ts
);
114 * The global_trace is the descriptor that holds the tracing
115 * buffers for the live tracing. For each CPU, it contains
116 * a link list of pages that will store trace entries. The
117 * page descriptor of the pages in the memory is used to hold
118 * the link list by linking the lru item in the page descriptor
119 * to each of the pages in the buffer per CPU.
121 * For each active CPU there is a data field that holds the
122 * pages for the buffer for that CPU. Each CPU has the same number
123 * of pages allocated for its buffer.
125 static struct trace_array global_trace
;
127 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
130 * The max_tr is used to snapshot the global_trace when a maximum
131 * latency is reached. Some tracers will use this to store a maximum
132 * trace while it continues examining live traces.
134 * The buffers for the max_tr are set up the same as the global_trace.
135 * When a snapshot is taken, the link list of the max_tr is swapped
136 * with the link list of the global_trace and the buffers are reset for
137 * the global_trace so the tracing can continue.
139 static struct trace_array max_tr
;
141 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
143 /* tracer_enabled is used to toggle activation of a tracer */
144 static int tracer_enabled
= 1;
146 /* function tracing enabled */
147 int ftrace_function_enabled
;
150 * trace_buf_size is the size in bytes that is allocated
151 * for a buffer. Note, the number of bytes is always rounded
154 * This number is purposely set to a low number of 16384.
155 * If the dump on oops happens, it will be much appreciated
156 * to not have to wait for all that output. Anyway this can be
157 * boot time and run time configurable.
159 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
161 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
163 /* trace_types holds a link list of available tracers. */
164 static struct tracer
*trace_types __read_mostly
;
166 /* current_trace points to the tracer that is currently active */
167 static struct tracer
*current_trace __read_mostly
;
170 * max_tracer_type_len is used to simplify the allocating of
171 * buffers to read userspace tracer names. We keep track of
172 * the longest tracer name registered.
174 static int max_tracer_type_len
;
177 * trace_types_lock is used to protect the trace_types list.
178 * This lock is also used to keep user access serialized.
179 * Accesses from userspace will grab this lock while userspace
180 * activities happen inside the kernel.
182 static DEFINE_MUTEX(trace_types_lock
);
184 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
185 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
187 /* trace_flags holds iter_ctrl options */
188 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
;
191 * trace_wake_up - wake up tasks waiting for trace input
193 * Simply wakes up any task that is blocked on the trace_wait
194 * queue. These is used with trace_poll for tasks polling the trace.
196 void trace_wake_up(void)
199 * The runqueue_is_locked() can fail, but this is the best we
202 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
203 wake_up(&trace_wait
);
206 static int __init
set_buf_size(char *str
)
208 unsigned long buf_size
;
213 ret
= strict_strtoul(str
, 0, &buf_size
);
214 /* nr_entries can not be zero */
215 if (ret
< 0 || buf_size
== 0)
217 trace_buf_size
= buf_size
;
220 __setup("trace_buf_size=", set_buf_size
);
222 unsigned long nsecs_to_usecs(unsigned long nsecs
)
228 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
229 * control the output of kernel symbols.
231 #define TRACE_ITER_SYM_MASK \
232 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
234 /* These must match the bit postions in trace_iterator_flags */
235 static const char *trace_options
[] = {
251 * ftrace_max_lock is used to protect the swapping of buffers
252 * when taking a max snapshot. The buffers themselves are
253 * protected by per_cpu spinlocks. But the action of the swap
254 * needs its own lock.
256 * This is defined as a raw_spinlock_t in order to help
257 * with performance when lockdep debugging is enabled.
259 static raw_spinlock_t ftrace_max_lock
=
260 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
263 * Copy the new maximum trace into the separate maximum-trace
264 * structure. (this way the maximum trace is permanently saved,
265 * for later retrieval via /debugfs/tracing/latency_trace)
268 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
270 struct trace_array_cpu
*data
= tr
->data
[cpu
];
273 max_tr
.time_start
= data
->preempt_timestamp
;
275 data
= max_tr
.data
[cpu
];
276 data
->saved_latency
= tracing_max_latency
;
278 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
279 data
->pid
= tsk
->pid
;
280 data
->uid
= tsk
->uid
;
281 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
282 data
->policy
= tsk
->policy
;
283 data
->rt_priority
= tsk
->rt_priority
;
285 /* record this tasks comm */
286 tracing_record_cmdline(current
);
290 * trace_seq_printf - sequence printing of trace information
291 * @s: trace sequence descriptor
292 * @fmt: printf format string
294 * The tracer may use either sequence operations or its own
295 * copy to user routines. To simplify formating of a trace
296 * trace_seq_printf is used to store strings into a special
297 * buffer (@s). Then the output may be either used by
298 * the sequencer or pulled into another buffer.
301 trace_seq_printf(struct trace_seq
*s
, const char *fmt
, ...)
303 int len
= (PAGE_SIZE
- 1) - s
->len
;
311 ret
= vsnprintf(s
->buffer
+ s
->len
, len
, fmt
, ap
);
314 /* If we can't write it all, don't bother writing anything */
324 * trace_seq_puts - trace sequence printing of simple string
325 * @s: trace sequence descriptor
326 * @str: simple string to record
328 * The tracer may use either the sequence operations or its own
329 * copy to user routines. This function records a simple string
330 * into a special buffer (@s) for later retrieval by a sequencer
331 * or other mechanism.
334 trace_seq_puts(struct trace_seq
*s
, const char *str
)
336 int len
= strlen(str
);
338 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
341 memcpy(s
->buffer
+ s
->len
, str
, len
);
348 trace_seq_putc(struct trace_seq
*s
, unsigned char c
)
350 if (s
->len
>= (PAGE_SIZE
- 1))
353 s
->buffer
[s
->len
++] = c
;
359 trace_seq_putmem(struct trace_seq
*s
, void *mem
, size_t len
)
361 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
364 memcpy(s
->buffer
+ s
->len
, mem
, len
);
370 #define MAX_MEMHEX_BYTES 8
371 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
374 trace_seq_putmem_hex(struct trace_seq
*s
, void *mem
, size_t len
)
376 unsigned char hex
[HEX_CHARS
];
377 unsigned char *data
= mem
;
381 for (i
= 0, j
= 0; i
< len
; i
++) {
383 for (i
= len
-1, j
= 0; i
>= 0; i
--) {
385 hex
[j
++] = hex_asc_hi(data
[i
]);
386 hex
[j
++] = hex_asc_lo(data
[i
]);
390 return trace_seq_putmem(s
, hex
, j
);
394 trace_seq_reset(struct trace_seq
*s
)
400 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
405 if (s
->len
<= s
->readpos
)
408 len
= s
->len
- s
->readpos
;
411 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
420 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
422 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
425 seq_puts(m
, s
->buffer
);
431 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
433 * @tsk: the task with the latency
434 * @cpu: The cpu that initiated the trace.
436 * Flip the buffers between the @tr and the max_tr and record information
437 * about which task was the cause of this latency.
440 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
442 struct ring_buffer
*buf
= tr
->buffer
;
444 WARN_ON_ONCE(!irqs_disabled());
445 __raw_spin_lock(&ftrace_max_lock
);
447 tr
->buffer
= max_tr
.buffer
;
450 ftrace_disable_cpu();
451 ring_buffer_reset(tr
->buffer
);
454 __update_max_tr(tr
, tsk
, cpu
);
455 __raw_spin_unlock(&ftrace_max_lock
);
459 * update_max_tr_single - only copy one trace over, and reset the rest
461 * @tsk - task with the latency
462 * @cpu - the cpu of the buffer to copy.
464 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
467 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
471 WARN_ON_ONCE(!irqs_disabled());
472 __raw_spin_lock(&ftrace_max_lock
);
474 ftrace_disable_cpu();
476 ring_buffer_reset(max_tr
.buffer
);
477 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
483 __update_max_tr(tr
, tsk
, cpu
);
484 __raw_spin_unlock(&ftrace_max_lock
);
488 * register_tracer - register a tracer with the ftrace system.
489 * @type - the plugin for the tracer
491 * Register a new plugin tracer.
493 int register_tracer(struct tracer
*type
)
500 pr_info("Tracer must have a name\n");
504 mutex_lock(&trace_types_lock
);
505 for (t
= trace_types
; t
; t
= t
->next
) {
506 if (strcmp(type
->name
, t
->name
) == 0) {
508 pr_info("Trace %s already registered\n",
515 #ifdef CONFIG_FTRACE_STARTUP_TEST
516 if (type
->selftest
) {
517 struct tracer
*saved_tracer
= current_trace
;
518 struct trace_array
*tr
= &global_trace
;
519 int saved_ctrl
= tr
->ctrl
;
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
528 for_each_tracing_cpu(i
) {
529 tracing_reset(tr
, i
);
531 current_trace
= type
;
533 /* the test is responsible for initializing and enabling */
534 pr_info("Testing tracer %s: ", type
->name
);
535 ret
= type
->selftest(type
, tr
);
536 /* the test is responsible for resetting too */
537 current_trace
= saved_tracer
;
538 tr
->ctrl
= saved_ctrl
;
540 printk(KERN_CONT
"FAILED!\n");
543 /* Only reset on passing, to avoid touching corrupted buffers */
544 for_each_tracing_cpu(i
) {
545 tracing_reset(tr
, i
);
547 printk(KERN_CONT
"PASSED\n");
551 type
->next
= trace_types
;
553 len
= strlen(type
->name
);
554 if (len
> max_tracer_type_len
)
555 max_tracer_type_len
= len
;
558 mutex_unlock(&trace_types_lock
);
563 void unregister_tracer(struct tracer
*type
)
568 mutex_lock(&trace_types_lock
);
569 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
573 pr_info("Trace %s not registered\n", type
->name
);
578 if (strlen(type
->name
) != max_tracer_type_len
)
581 max_tracer_type_len
= 0;
582 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
583 len
= strlen((*t
)->name
);
584 if (len
> max_tracer_type_len
)
585 max_tracer_type_len
= len
;
588 mutex_unlock(&trace_types_lock
);
591 void tracing_reset(struct trace_array
*tr
, int cpu
)
593 ftrace_disable_cpu();
594 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
598 #define SAVED_CMDLINES 128
599 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
600 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
601 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
602 static int cmdline_idx
;
603 static DEFINE_SPINLOCK(trace_cmdline_lock
);
605 /* temporary disable recording */
606 atomic_t trace_record_cmdline_disabled __read_mostly
;
608 static void trace_init_cmdlines(void)
610 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
611 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
615 void trace_stop_cmdline_recording(void);
617 static void trace_save_cmdline(struct task_struct
*tsk
)
622 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
626 * It's not the end of the world if we don't get
627 * the lock, but we also don't want to spin
628 * nor do we want to disable interrupts,
629 * so if we miss here, then better luck next time.
631 if (!spin_trylock(&trace_cmdline_lock
))
634 idx
= map_pid_to_cmdline
[tsk
->pid
];
635 if (idx
>= SAVED_CMDLINES
) {
636 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
638 map
= map_cmdline_to_pid
[idx
];
639 if (map
<= PID_MAX_DEFAULT
)
640 map_pid_to_cmdline
[map
] = (unsigned)-1;
642 map_pid_to_cmdline
[tsk
->pid
] = idx
;
647 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
649 spin_unlock(&trace_cmdline_lock
);
652 static char *trace_find_cmdline(int pid
)
654 char *cmdline
= "<...>";
660 if (pid
> PID_MAX_DEFAULT
)
663 map
= map_pid_to_cmdline
[pid
];
664 if (map
>= SAVED_CMDLINES
)
667 cmdline
= saved_cmdlines
[map
];
673 void tracing_record_cmdline(struct task_struct
*tsk
)
675 if (atomic_read(&trace_record_cmdline_disabled
))
678 trace_save_cmdline(tsk
);
682 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
685 struct task_struct
*tsk
= current
;
687 entry
->preempt_count
= pc
& 0xff;
688 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
690 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
691 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
693 TRACE_FLAG_IRQS_NOSUPPORT
|
695 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
696 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
697 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
701 trace_function(struct trace_array
*tr
, struct trace_array_cpu
*data
,
702 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
705 struct ring_buffer_event
*event
;
706 struct ftrace_entry
*entry
;
707 unsigned long irq_flags
;
709 /* If we are reading the ring buffer, don't trace */
710 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
713 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
717 entry
= ring_buffer_event_data(event
);
718 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
719 entry
->ent
.type
= TRACE_FN
;
721 entry
->parent_ip
= parent_ip
;
722 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
726 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
727 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
730 if (likely(!atomic_read(&data
->disabled
)))
731 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
734 static void ftrace_trace_stack(struct trace_array
*tr
,
735 struct trace_array_cpu
*data
,
739 #ifdef CONFIG_STACKTRACE
740 struct ring_buffer_event
*event
;
741 struct stack_entry
*entry
;
742 struct stack_trace trace
;
743 unsigned long irq_flags
;
745 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
748 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
752 entry
= ring_buffer_event_data(event
);
753 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
754 entry
->ent
.type
= TRACE_STACK
;
756 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
758 trace
.nr_entries
= 0;
759 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
761 trace
.entries
= entry
->caller
;
763 save_stack_trace(&trace
);
764 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
768 void __trace_stack(struct trace_array
*tr
,
769 struct trace_array_cpu
*data
,
773 ftrace_trace_stack(tr
, data
, flags
, skip
, preempt_count());
777 ftrace_trace_special(void *__tr
, void *__data
,
778 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
781 struct ring_buffer_event
*event
;
782 struct trace_array_cpu
*data
= __data
;
783 struct trace_array
*tr
= __tr
;
784 struct special_entry
*entry
;
785 unsigned long irq_flags
;
787 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
791 entry
= ring_buffer_event_data(event
);
792 tracing_generic_entry_update(&entry
->ent
, 0, pc
);
793 entry
->ent
.type
= TRACE_SPECIAL
;
797 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
798 ftrace_trace_stack(tr
, data
, irq_flags
, 4, pc
);
804 __trace_special(void *__tr
, void *__data
,
805 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
807 ftrace_trace_special(__tr
, __data
, arg1
, arg2
, arg3
, preempt_count());
811 tracing_sched_switch_trace(struct trace_array
*tr
,
812 struct trace_array_cpu
*data
,
813 struct task_struct
*prev
,
814 struct task_struct
*next
,
815 unsigned long flags
, int pc
)
817 struct ring_buffer_event
*event
;
818 struct ctx_switch_entry
*entry
;
819 unsigned long irq_flags
;
821 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
825 entry
= ring_buffer_event_data(event
);
826 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
827 entry
->ent
.type
= TRACE_CTX
;
828 entry
->prev_pid
= prev
->pid
;
829 entry
->prev_prio
= prev
->prio
;
830 entry
->prev_state
= prev
->state
;
831 entry
->next_pid
= next
->pid
;
832 entry
->next_prio
= next
->prio
;
833 entry
->next_state
= next
->state
;
834 entry
->next_cpu
= task_cpu(next
);
835 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
836 ftrace_trace_stack(tr
, data
, flags
, 5, pc
);
840 tracing_sched_wakeup_trace(struct trace_array
*tr
,
841 struct trace_array_cpu
*data
,
842 struct task_struct
*wakee
,
843 struct task_struct
*curr
,
844 unsigned long flags
, int pc
)
846 struct ring_buffer_event
*event
;
847 struct ctx_switch_entry
*entry
;
848 unsigned long irq_flags
;
850 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
854 entry
= ring_buffer_event_data(event
);
855 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
856 entry
->ent
.type
= TRACE_WAKE
;
857 entry
->prev_pid
= curr
->pid
;
858 entry
->prev_prio
= curr
->prio
;
859 entry
->prev_state
= curr
->state
;
860 entry
->next_pid
= wakee
->pid
;
861 entry
->next_prio
= wakee
->prio
;
862 entry
->next_state
= wakee
->state
;
863 entry
->next_cpu
= task_cpu(wakee
);
864 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
865 ftrace_trace_stack(tr
, data
, flags
, 6, pc
);
871 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
873 struct trace_array
*tr
= &global_trace
;
874 struct trace_array_cpu
*data
;
878 if (tracing_disabled
|| !tr
->ctrl
)
881 pc
= preempt_count();
882 preempt_disable_notrace();
883 cpu
= raw_smp_processor_id();
884 data
= tr
->data
[cpu
];
886 if (likely(!atomic_read(&data
->disabled
)))
887 ftrace_trace_special(tr
, data
, arg1
, arg2
, arg3
, pc
);
889 preempt_enable_notrace();
892 #ifdef CONFIG_FUNCTION_TRACER
894 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
896 struct trace_array
*tr
= &global_trace
;
897 struct trace_array_cpu
*data
;
903 if (unlikely(!ftrace_function_enabled
))
906 pc
= preempt_count();
907 resched
= ftrace_preempt_disable();
908 local_save_flags(flags
);
909 cpu
= raw_smp_processor_id();
910 data
= tr
->data
[cpu
];
911 disabled
= atomic_inc_return(&data
->disabled
);
913 if (likely(disabled
== 1))
914 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
916 atomic_dec(&data
->disabled
);
917 ftrace_preempt_enable(resched
);
920 static struct ftrace_ops trace_ops __read_mostly
=
922 .func
= function_trace_call
,
925 void tracing_start_function_trace(void)
927 ftrace_function_enabled
= 0;
928 register_ftrace_function(&trace_ops
);
930 ftrace_function_enabled
= 1;
933 void tracing_stop_function_trace(void)
935 ftrace_function_enabled
= 0;
936 unregister_ftrace_function(&trace_ops
);
940 enum trace_file_type
{
941 TRACE_FILE_LAT_FMT
= 1,
944 static void trace_iterator_increment(struct trace_iterator
*iter
, int cpu
)
946 /* Don't allow ftrace to trace into the ring buffers */
947 ftrace_disable_cpu();
950 if (iter
->buffer_iter
[iter
->cpu
])
951 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
956 static struct trace_entry
*
957 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
959 struct ring_buffer_event
*event
;
960 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
962 /* Don't allow ftrace to trace into the ring buffers */
963 ftrace_disable_cpu();
966 event
= ring_buffer_iter_peek(buf_iter
, ts
);
968 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
972 return event
? ring_buffer_event_data(event
) : NULL
;
975 static struct trace_entry
*
976 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
978 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
979 struct trace_entry
*ent
, *next
= NULL
;
984 for_each_tracing_cpu(cpu
) {
986 if (ring_buffer_empty_cpu(buffer
, cpu
))
989 ent
= peek_next_entry(iter
, cpu
, &ts
);
992 * Pick the entry with the smallest timestamp:
994 if (ent
&& (!next
|| ts
< next_ts
)) {
1002 *ent_cpu
= next_cpu
;
1010 /* Find the next real entry, without updating the iterator itself */
1011 static struct trace_entry
*
1012 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1014 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1017 /* Find the next real entry, and increment the iterator to the next entry */
1018 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1020 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1023 trace_iterator_increment(iter
, iter
->cpu
);
1025 return iter
->ent
? iter
: NULL
;
1028 static void trace_consume(struct trace_iterator
*iter
)
1030 /* Don't allow ftrace to trace into the ring buffers */
1031 ftrace_disable_cpu();
1032 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1033 ftrace_enable_cpu();
1036 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1038 struct trace_iterator
*iter
= m
->private;
1044 /* can't go backwards */
1049 ent
= find_next_entry_inc(iter
);
1053 while (ent
&& iter
->idx
< i
)
1054 ent
= find_next_entry_inc(iter
);
1061 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1063 struct trace_iterator
*iter
= m
->private;
1068 mutex_lock(&trace_types_lock
);
1070 if (!current_trace
|| current_trace
!= iter
->trace
) {
1071 mutex_unlock(&trace_types_lock
);
1075 atomic_inc(&trace_record_cmdline_disabled
);
1077 /* let the tracer grab locks here if needed */
1078 if (current_trace
->start
)
1079 current_trace
->start(iter
);
1081 if (*pos
!= iter
->pos
) {
1086 ftrace_disable_cpu();
1088 for_each_tracing_cpu(cpu
) {
1089 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1092 ftrace_enable_cpu();
1094 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1099 p
= s_next(m
, p
, &l
);
1105 static void s_stop(struct seq_file
*m
, void *p
)
1107 struct trace_iterator
*iter
= m
->private;
1109 atomic_dec(&trace_record_cmdline_disabled
);
1111 /* let the tracer release locks here if needed */
1112 if (current_trace
&& current_trace
== iter
->trace
&& iter
->trace
->stop
)
1113 iter
->trace
->stop(iter
);
1115 mutex_unlock(&trace_types_lock
);
1118 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1120 #ifdef CONFIG_KRETPROBES
1121 static inline int kretprobed(unsigned long addr
)
1123 return addr
== (unsigned long)kretprobe_trampoline
;
1126 static inline int kretprobed(unsigned long addr
)
1130 #endif /* CONFIG_KRETPROBES */
1133 seq_print_sym_short(struct trace_seq
*s
, const char *fmt
, unsigned long address
)
1135 #ifdef CONFIG_KALLSYMS
1136 char str
[KSYM_SYMBOL_LEN
];
1138 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
1140 return trace_seq_printf(s
, fmt
, str
);
1146 seq_print_sym_offset(struct trace_seq
*s
, const char *fmt
,
1147 unsigned long address
)
1149 #ifdef CONFIG_KALLSYMS
1150 char str
[KSYM_SYMBOL_LEN
];
1152 sprint_symbol(str
, address
);
1153 return trace_seq_printf(s
, fmt
, str
);
1158 #ifndef CONFIG_64BIT
1159 # define IP_FMT "%08lx"
1161 # define IP_FMT "%016lx"
1165 seq_print_ip_sym(struct trace_seq
*s
, unsigned long ip
, unsigned long sym_flags
)
1170 return trace_seq_printf(s
, "0");
1172 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
1173 ret
= seq_print_sym_offset(s
, "%s", ip
);
1175 ret
= seq_print_sym_short(s
, "%s", ip
);
1180 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
1181 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
1185 static void print_lat_help_header(struct seq_file
*m
)
1187 seq_puts(m
, "# _------=> CPU# \n");
1188 seq_puts(m
, "# / _-----=> irqs-off \n");
1189 seq_puts(m
, "# | / _----=> need-resched \n");
1190 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1191 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1192 seq_puts(m
, "# |||| / \n");
1193 seq_puts(m
, "# ||||| delay \n");
1194 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1195 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1198 static void print_func_help_header(struct seq_file
*m
)
1200 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1201 seq_puts(m
, "# | | | | |\n");
1206 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1208 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1209 struct trace_array
*tr
= iter
->tr
;
1210 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1211 struct tracer
*type
= current_trace
;
1212 unsigned long total
;
1213 unsigned long entries
;
1214 const char *name
= "preemption";
1219 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1221 ring_buffer_overruns(iter
->tr
->buffer
);
1223 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1225 seq_puts(m
, "-----------------------------------"
1226 "---------------------------------\n");
1227 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1228 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1229 nsecs_to_usecs(data
->saved_latency
),
1233 #if defined(CONFIG_PREEMPT_NONE)
1235 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1237 #elif defined(CONFIG_PREEMPT)
1242 /* These are reserved for later use */
1245 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1249 seq_puts(m
, " -----------------\n");
1250 seq_printf(m
, " | task: %.16s-%d "
1251 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1252 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1253 data
->policy
, data
->rt_priority
);
1254 seq_puts(m
, " -----------------\n");
1256 if (data
->critical_start
) {
1257 seq_puts(m
, " => started at: ");
1258 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1259 trace_print_seq(m
, &iter
->seq
);
1260 seq_puts(m
, "\n => ended at: ");
1261 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1262 trace_print_seq(m
, &iter
->seq
);
1270 lat_print_generic(struct trace_seq
*s
, struct trace_entry
*entry
, int cpu
)
1272 int hardirq
, softirq
;
1275 comm
= trace_find_cmdline(entry
->pid
);
1277 trace_seq_printf(s
, "%8.8s-%-5d ", comm
, entry
->pid
);
1278 trace_seq_printf(s
, "%3d", cpu
);
1279 trace_seq_printf(s
, "%c%c",
1280 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
1281 (entry
->flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ? 'X' : '.',
1282 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
1284 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
1285 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
1286 if (hardirq
&& softirq
) {
1287 trace_seq_putc(s
, 'H');
1290 trace_seq_putc(s
, 'h');
1293 trace_seq_putc(s
, 's');
1295 trace_seq_putc(s
, '.');
1299 if (entry
->preempt_count
)
1300 trace_seq_printf(s
, "%x", entry
->preempt_count
);
1302 trace_seq_puts(s
, ".");
1305 unsigned long preempt_mark_thresh
= 100;
1308 lat_print_timestamp(struct trace_seq
*s
, u64 abs_usecs
,
1309 unsigned long rel_usecs
)
1311 trace_seq_printf(s
, " %4lldus", abs_usecs
);
1312 if (rel_usecs
> preempt_mark_thresh
)
1313 trace_seq_puts(s
, "!: ");
1314 else if (rel_usecs
> 1)
1315 trace_seq_puts(s
, "+: ");
1317 trace_seq_puts(s
, " : ");
1320 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
1323 * The message is supposed to contain an ending newline.
1324 * If the printing stops prematurely, try to add a newline of our own.
1326 void trace_seq_print_cont(struct trace_seq
*s
, struct trace_iterator
*iter
)
1328 struct trace_entry
*ent
;
1329 struct trace_field_cont
*cont
;
1332 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1333 if (!ent
|| ent
->type
!= TRACE_CONT
) {
1334 trace_seq_putc(s
, '\n');
1339 cont
= (struct trace_field_cont
*)ent
;
1341 ok
= (trace_seq_printf(s
, "%s", cont
->buf
) > 0);
1343 ftrace_disable_cpu();
1345 if (iter
->buffer_iter
[iter
->cpu
])
1346 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1348 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, NULL
);
1350 ftrace_enable_cpu();
1352 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1353 } while (ent
&& ent
->type
== TRACE_CONT
);
1356 trace_seq_putc(s
, '\n');
1359 static enum print_line_t
1360 print_lat_fmt(struct trace_iterator
*iter
, unsigned int trace_idx
, int cpu
)
1362 struct trace_seq
*s
= &iter
->seq
;
1363 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1364 struct trace_entry
*next_entry
;
1365 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
1366 struct trace_entry
*entry
= iter
->ent
;
1367 unsigned long abs_usecs
;
1368 unsigned long rel_usecs
;
1375 if (entry
->type
== TRACE_CONT
)
1376 return TRACE_TYPE_HANDLED
;
1378 next_entry
= find_next_entry(iter
, NULL
, &next_ts
);
1381 rel_usecs
= ns2usecs(next_ts
- iter
->ts
);
1382 abs_usecs
= ns2usecs(iter
->ts
- iter
->tr
->time_start
);
1385 comm
= trace_find_cmdline(entry
->pid
);
1386 trace_seq_printf(s
, "%16s %5d %3d %d %08x %08x [%08lx]"
1387 " %ld.%03ldms (+%ld.%03ldms): ",
1389 entry
->pid
, cpu
, entry
->flags
,
1390 entry
->preempt_count
, trace_idx
,
1393 abs_usecs
% 1000, rel_usecs
/1000,
1396 lat_print_generic(s
, entry
, cpu
);
1397 lat_print_timestamp(s
, abs_usecs
, rel_usecs
);
1399 switch (entry
->type
) {
1401 struct ftrace_entry
*field
;
1403 trace_assign_type(field
, entry
);
1405 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1406 trace_seq_puts(s
, " (");
1407 if (kretprobed(field
->parent_ip
))
1408 trace_seq_puts(s
, KRETPROBE_MSG
);
1410 seq_print_ip_sym(s
, field
->parent_ip
, sym_flags
);
1411 trace_seq_puts(s
, ")\n");
1416 struct ctx_switch_entry
*field
;
1418 trace_assign_type(field
, entry
);
1420 T
= field
->next_state
< sizeof(state_to_char
) ?
1421 state_to_char
[field
->next_state
] : 'X';
1423 state
= field
->prev_state
?
1424 __ffs(field
->prev_state
) + 1 : 0;
1425 S
= state
< sizeof(state_to_char
) - 1 ? state_to_char
[state
] : 'X';
1426 comm
= trace_find_cmdline(field
->next_pid
);
1427 trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1430 S
, entry
->type
== TRACE_CTX
? "==>" : " +",
1437 case TRACE_SPECIAL
: {
1438 struct special_entry
*field
;
1440 trace_assign_type(field
, entry
);
1442 trace_seq_printf(s
, "# %ld %ld %ld\n",
1449 struct stack_entry
*field
;
1451 trace_assign_type(field
, entry
);
1453 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1455 trace_seq_puts(s
, " <= ");
1456 seq_print_ip_sym(s
, field
->caller
[i
], sym_flags
);
1458 trace_seq_puts(s
, "\n");
1462 struct print_entry
*field
;
1464 trace_assign_type(field
, entry
);
1466 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1467 trace_seq_printf(s
, ": %s", field
->buf
);
1468 if (entry
->flags
& TRACE_FLAG_CONT
)
1469 trace_seq_print_cont(s
, iter
);
1473 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
1475 return TRACE_TYPE_HANDLED
;
1478 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1480 struct trace_seq
*s
= &iter
->seq
;
1481 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1482 struct trace_entry
*entry
;
1483 unsigned long usec_rem
;
1484 unsigned long long t
;
1493 if (entry
->type
== TRACE_CONT
)
1494 return TRACE_TYPE_HANDLED
;
1496 comm
= trace_find_cmdline(iter
->ent
->pid
);
1498 t
= ns2usecs(iter
->ts
);
1499 usec_rem
= do_div(t
, 1000000ULL);
1500 secs
= (unsigned long)t
;
1502 ret
= trace_seq_printf(s
, "%16s-%-5d ", comm
, entry
->pid
);
1504 return TRACE_TYPE_PARTIAL_LINE
;
1505 ret
= trace_seq_printf(s
, "[%03d] ", iter
->cpu
);
1507 return TRACE_TYPE_PARTIAL_LINE
;
1508 ret
= trace_seq_printf(s
, "%5lu.%06lu: ", secs
, usec_rem
);
1510 return TRACE_TYPE_PARTIAL_LINE
;
1512 switch (entry
->type
) {
1514 struct ftrace_entry
*field
;
1516 trace_assign_type(field
, entry
);
1518 ret
= seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1520 return TRACE_TYPE_PARTIAL_LINE
;
1521 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
1523 ret
= trace_seq_printf(s
, " <-");
1525 return TRACE_TYPE_PARTIAL_LINE
;
1526 if (kretprobed(field
->parent_ip
))
1527 ret
= trace_seq_puts(s
, KRETPROBE_MSG
);
1529 ret
= seq_print_ip_sym(s
,
1533 return TRACE_TYPE_PARTIAL_LINE
;
1535 ret
= trace_seq_printf(s
, "\n");
1537 return TRACE_TYPE_PARTIAL_LINE
;
1542 struct ctx_switch_entry
*field
;
1544 trace_assign_type(field
, entry
);
1546 S
= field
->prev_state
< sizeof(state_to_char
) ?
1547 state_to_char
[field
->prev_state
] : 'X';
1548 T
= field
->next_state
< sizeof(state_to_char
) ?
1549 state_to_char
[field
->next_state
] : 'X';
1550 ret
= trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1554 entry
->type
== TRACE_CTX
? "==>" : " +",
1560 return TRACE_TYPE_PARTIAL_LINE
;
1563 case TRACE_SPECIAL
: {
1564 struct special_entry
*field
;
1566 trace_assign_type(field
, entry
);
1568 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1573 return TRACE_TYPE_PARTIAL_LINE
;
1577 struct stack_entry
*field
;
1579 trace_assign_type(field
, entry
);
1581 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1583 ret
= trace_seq_puts(s
, " <= ");
1585 return TRACE_TYPE_PARTIAL_LINE
;
1587 ret
= seq_print_ip_sym(s
, field
->caller
[i
],
1590 return TRACE_TYPE_PARTIAL_LINE
;
1592 ret
= trace_seq_puts(s
, "\n");
1594 return TRACE_TYPE_PARTIAL_LINE
;
1598 struct print_entry
*field
;
1600 trace_assign_type(field
, entry
);
1602 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1603 trace_seq_printf(s
, ": %s", field
->buf
);
1604 if (entry
->flags
& TRACE_FLAG_CONT
)
1605 trace_seq_print_cont(s
, iter
);
1609 return TRACE_TYPE_HANDLED
;
1612 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1614 struct trace_seq
*s
= &iter
->seq
;
1615 struct trace_entry
*entry
;
1621 if (entry
->type
== TRACE_CONT
)
1622 return TRACE_TYPE_HANDLED
;
1624 ret
= trace_seq_printf(s
, "%d %d %llu ",
1625 entry
->pid
, iter
->cpu
, iter
->ts
);
1627 return TRACE_TYPE_PARTIAL_LINE
;
1629 switch (entry
->type
) {
1631 struct ftrace_entry
*field
;
1633 trace_assign_type(field
, entry
);
1635 ret
= trace_seq_printf(s
, "%x %x\n",
1639 return TRACE_TYPE_PARTIAL_LINE
;
1644 struct ctx_switch_entry
*field
;
1646 trace_assign_type(field
, entry
);
1648 S
= field
->prev_state
< sizeof(state_to_char
) ?
1649 state_to_char
[field
->prev_state
] : 'X';
1650 T
= field
->next_state
< sizeof(state_to_char
) ?
1651 state_to_char
[field
->next_state
] : 'X';
1652 if (entry
->type
== TRACE_WAKE
)
1654 ret
= trace_seq_printf(s
, "%d %d %c %d %d %d %c\n",
1663 return TRACE_TYPE_PARTIAL_LINE
;
1668 struct special_entry
*field
;
1670 trace_assign_type(field
, entry
);
1672 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1677 return TRACE_TYPE_PARTIAL_LINE
;
1681 struct print_entry
*field
;
1683 trace_assign_type(field
, entry
);
1685 trace_seq_printf(s
, "# %lx %s", field
->ip
, field
->buf
);
1686 if (entry
->flags
& TRACE_FLAG_CONT
)
1687 trace_seq_print_cont(s
, iter
);
1691 return TRACE_TYPE_HANDLED
;
1694 #define SEQ_PUT_FIELD_RET(s, x) \
1696 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1700 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1702 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1703 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1707 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1709 struct trace_seq
*s
= &iter
->seq
;
1710 unsigned char newline
= '\n';
1711 struct trace_entry
*entry
;
1716 if (entry
->type
== TRACE_CONT
)
1717 return TRACE_TYPE_HANDLED
;
1719 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1720 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1721 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1723 switch (entry
->type
) {
1725 struct ftrace_entry
*field
;
1727 trace_assign_type(field
, entry
);
1729 SEQ_PUT_HEX_FIELD_RET(s
, field
->ip
);
1730 SEQ_PUT_HEX_FIELD_RET(s
, field
->parent_ip
);
1735 struct ctx_switch_entry
*field
;
1737 trace_assign_type(field
, entry
);
1739 S
= field
->prev_state
< sizeof(state_to_char
) ?
1740 state_to_char
[field
->prev_state
] : 'X';
1741 T
= field
->next_state
< sizeof(state_to_char
) ?
1742 state_to_char
[field
->next_state
] : 'X';
1743 if (entry
->type
== TRACE_WAKE
)
1745 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_pid
);
1746 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_prio
);
1747 SEQ_PUT_HEX_FIELD_RET(s
, S
);
1748 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_cpu
);
1749 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_pid
);
1750 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_prio
);
1751 SEQ_PUT_HEX_FIELD_RET(s
, T
);
1756 struct special_entry
*field
;
1758 trace_assign_type(field
, entry
);
1760 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg1
);
1761 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg2
);
1762 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg3
);
1766 SEQ_PUT_FIELD_RET(s
, newline
);
1768 return TRACE_TYPE_HANDLED
;
1771 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1773 struct trace_seq
*s
= &iter
->seq
;
1774 struct trace_entry
*entry
;
1778 if (entry
->type
== TRACE_CONT
)
1779 return TRACE_TYPE_HANDLED
;
1781 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1782 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1783 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1785 switch (entry
->type
) {
1787 struct ftrace_entry
*field
;
1789 trace_assign_type(field
, entry
);
1791 SEQ_PUT_FIELD_RET(s
, field
->ip
);
1792 SEQ_PUT_FIELD_RET(s
, field
->parent_ip
);
1796 struct ctx_switch_entry
*field
;
1798 trace_assign_type(field
, entry
);
1800 SEQ_PUT_FIELD_RET(s
, field
->prev_pid
);
1801 SEQ_PUT_FIELD_RET(s
, field
->prev_prio
);
1802 SEQ_PUT_FIELD_RET(s
, field
->prev_state
);
1803 SEQ_PUT_FIELD_RET(s
, field
->next_pid
);
1804 SEQ_PUT_FIELD_RET(s
, field
->next_prio
);
1805 SEQ_PUT_FIELD_RET(s
, field
->next_state
);
1810 struct special_entry
*field
;
1812 trace_assign_type(field
, entry
);
1814 SEQ_PUT_FIELD_RET(s
, field
->arg1
);
1815 SEQ_PUT_FIELD_RET(s
, field
->arg2
);
1816 SEQ_PUT_FIELD_RET(s
, field
->arg3
);
1823 static int trace_empty(struct trace_iterator
*iter
)
1827 for_each_tracing_cpu(cpu
) {
1828 if (iter
->buffer_iter
[cpu
]) {
1829 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1832 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1840 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1842 enum print_line_t ret
;
1844 if (iter
->trace
&& iter
->trace
->print_line
) {
1845 ret
= iter
->trace
->print_line(iter
);
1846 if (ret
!= TRACE_TYPE_UNHANDLED
)
1850 if (trace_flags
& TRACE_ITER_BIN
)
1851 return print_bin_fmt(iter
);
1853 if (trace_flags
& TRACE_ITER_HEX
)
1854 return print_hex_fmt(iter
);
1856 if (trace_flags
& TRACE_ITER_RAW
)
1857 return print_raw_fmt(iter
);
1859 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
1860 return print_lat_fmt(iter
, iter
->idx
, iter
->cpu
);
1862 return print_trace_fmt(iter
);
1865 static int s_show(struct seq_file
*m
, void *v
)
1867 struct trace_iterator
*iter
= v
;
1869 if (iter
->ent
== NULL
) {
1871 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1874 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1875 /* print nothing if the buffers are empty */
1876 if (trace_empty(iter
))
1878 print_trace_header(m
, iter
);
1879 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1880 print_lat_help_header(m
);
1882 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1883 print_func_help_header(m
);
1886 print_trace_line(iter
);
1887 trace_print_seq(m
, &iter
->seq
);
1893 static struct seq_operations tracer_seq_ops
= {
1900 static struct trace_iterator
*
1901 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
1903 struct trace_iterator
*iter
;
1907 if (tracing_disabled
) {
1912 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1918 mutex_lock(&trace_types_lock
);
1919 if (current_trace
&& current_trace
->print_max
)
1922 iter
->tr
= inode
->i_private
;
1923 iter
->trace
= current_trace
;
1926 for_each_tracing_cpu(cpu
) {
1928 iter
->buffer_iter
[cpu
] =
1929 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1931 if (!iter
->buffer_iter
[cpu
])
1935 /* TODO stop tracer */
1936 *ret
= seq_open(file
, &tracer_seq_ops
);
1940 m
= file
->private_data
;
1943 /* stop the trace while dumping */
1944 if (iter
->tr
->ctrl
) {
1946 ftrace_function_enabled
= 0;
1949 if (iter
->trace
&& iter
->trace
->open
)
1950 iter
->trace
->open(iter
);
1952 mutex_unlock(&trace_types_lock
);
1958 for_each_tracing_cpu(cpu
) {
1959 if (iter
->buffer_iter
[cpu
])
1960 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1962 mutex_unlock(&trace_types_lock
);
1964 return ERR_PTR(-ENOMEM
);
1967 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1969 if (tracing_disabled
)
1972 filp
->private_data
= inode
->i_private
;
1976 int tracing_release(struct inode
*inode
, struct file
*file
)
1978 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1979 struct trace_iterator
*iter
= m
->private;
1982 mutex_lock(&trace_types_lock
);
1983 for_each_tracing_cpu(cpu
) {
1984 if (iter
->buffer_iter
[cpu
])
1985 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1988 if (iter
->trace
&& iter
->trace
->close
)
1989 iter
->trace
->close(iter
);
1991 /* reenable tracing if it was previously enabled */
1992 if (iter
->tr
->ctrl
) {
1995 * It is safe to enable function tracing even if it
1998 ftrace_function_enabled
= 1;
2000 mutex_unlock(&trace_types_lock
);
2002 seq_release(inode
, file
);
2007 static int tracing_open(struct inode
*inode
, struct file
*file
)
2011 __tracing_open(inode
, file
, &ret
);
2016 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
2018 struct trace_iterator
*iter
;
2021 iter
= __tracing_open(inode
, file
, &ret
);
2024 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2031 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2033 struct tracer
*t
= m
->private;
2045 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2047 struct tracer
*t
= m
->private;
2050 mutex_lock(&trace_types_lock
);
2051 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2057 static void t_stop(struct seq_file
*m
, void *p
)
2059 mutex_unlock(&trace_types_lock
);
2062 static int t_show(struct seq_file
*m
, void *v
)
2064 struct tracer
*t
= v
;
2069 seq_printf(m
, "%s", t
->name
);
2078 static struct seq_operations show_traces_seq_ops
= {
2085 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2089 if (tracing_disabled
)
2092 ret
= seq_open(file
, &show_traces_seq_ops
);
2094 struct seq_file
*m
= file
->private_data
;
2095 m
->private = trace_types
;
2101 static struct file_operations tracing_fops
= {
2102 .open
= tracing_open
,
2104 .llseek
= seq_lseek
,
2105 .release
= tracing_release
,
2108 static struct file_operations tracing_lt_fops
= {
2109 .open
= tracing_lt_open
,
2111 .llseek
= seq_lseek
,
2112 .release
= tracing_release
,
2115 static struct file_operations show_traces_fops
= {
2116 .open
= show_traces_open
,
2118 .release
= seq_release
,
2122 * Only trace on a CPU if the bitmask is set:
2124 static cpumask_t tracing_cpumask
= CPU_MASK_ALL
;
2127 * When tracing/tracing_cpu_mask is modified then this holds
2128 * the new bitmask we are about to install:
2130 static cpumask_t tracing_cpumask_new
;
2133 * The tracer itself will not take this lock, but still we want
2134 * to provide a consistent cpumask to user-space:
2136 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2139 * Temporary storage for the character representation of the
2140 * CPU bitmask (and one more byte for the newline):
2142 static char mask_str
[NR_CPUS
+ 1];
2145 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2146 size_t count
, loff_t
*ppos
)
2150 mutex_lock(&tracing_cpumask_update_lock
);
2152 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2153 if (count
- len
< 2) {
2157 len
+= sprintf(mask_str
+ len
, "\n");
2158 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2161 mutex_unlock(&tracing_cpumask_update_lock
);
2167 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2168 size_t count
, loff_t
*ppos
)
2172 mutex_lock(&tracing_cpumask_update_lock
);
2173 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2177 raw_local_irq_disable();
2178 __raw_spin_lock(&ftrace_max_lock
);
2179 for_each_tracing_cpu(cpu
) {
2181 * Increase/decrease the disabled counter if we are
2182 * about to flip a bit in the cpumask:
2184 if (cpu_isset(cpu
, tracing_cpumask
) &&
2185 !cpu_isset(cpu
, tracing_cpumask_new
)) {
2186 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2188 if (!cpu_isset(cpu
, tracing_cpumask
) &&
2189 cpu_isset(cpu
, tracing_cpumask_new
)) {
2190 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2193 __raw_spin_unlock(&ftrace_max_lock
);
2194 raw_local_irq_enable();
2196 tracing_cpumask
= tracing_cpumask_new
;
2198 mutex_unlock(&tracing_cpumask_update_lock
);
2203 mutex_unlock(&tracing_cpumask_update_lock
);
2208 static struct file_operations tracing_cpumask_fops
= {
2209 .open
= tracing_open_generic
,
2210 .read
= tracing_cpumask_read
,
2211 .write
= tracing_cpumask_write
,
2215 tracing_iter_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2216 size_t cnt
, loff_t
*ppos
)
2223 /* calulate max size */
2224 for (i
= 0; trace_options
[i
]; i
++) {
2225 len
+= strlen(trace_options
[i
]);
2226 len
+= 3; /* "no" and space */
2229 /* +2 for \n and \0 */
2230 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2234 for (i
= 0; trace_options
[i
]; i
++) {
2235 if (trace_flags
& (1 << i
))
2236 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
2238 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
2241 r
+= sprintf(buf
+ r
, "\n");
2242 WARN_ON(r
>= len
+ 2);
2244 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2252 tracing_iter_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2253 size_t cnt
, loff_t
*ppos
)
2260 if (cnt
>= sizeof(buf
))
2263 if (copy_from_user(&buf
, ubuf
, cnt
))
2268 if (strncmp(buf
, "no", 2) == 0) {
2273 for (i
= 0; trace_options
[i
]; i
++) {
2274 int len
= strlen(trace_options
[i
]);
2276 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2278 trace_flags
&= ~(1 << i
);
2280 trace_flags
|= (1 << i
);
2285 * If no option could be set, return an error:
2287 if (!trace_options
[i
])
2295 static struct file_operations tracing_iter_fops
= {
2296 .open
= tracing_open_generic
,
2297 .read
= tracing_iter_ctrl_read
,
2298 .write
= tracing_iter_ctrl_write
,
2301 static const char readme_msg
[] =
2302 "tracing mini-HOWTO:\n\n"
2304 "# mount -t debugfs nodev /debug\n\n"
2305 "# cat /debug/tracing/available_tracers\n"
2306 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2307 "# cat /debug/tracing/current_tracer\n"
2309 "# echo sched_switch > /debug/tracing/current_tracer\n"
2310 "# cat /debug/tracing/current_tracer\n"
2312 "# cat /debug/tracing/iter_ctrl\n"
2313 "noprint-parent nosym-offset nosym-addr noverbose\n"
2314 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2315 "# echo 1 > /debug/tracing/tracing_enabled\n"
2316 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2317 "echo 0 > /debug/tracing/tracing_enabled\n"
2321 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2322 size_t cnt
, loff_t
*ppos
)
2324 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2325 readme_msg
, strlen(readme_msg
));
2328 static struct file_operations tracing_readme_fops
= {
2329 .open
= tracing_open_generic
,
2330 .read
= tracing_readme_read
,
2334 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2335 size_t cnt
, loff_t
*ppos
)
2337 struct trace_array
*tr
= filp
->private_data
;
2341 r
= sprintf(buf
, "%ld\n", tr
->ctrl
);
2342 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2346 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2347 size_t cnt
, loff_t
*ppos
)
2349 struct trace_array
*tr
= filp
->private_data
;
2354 if (cnt
>= sizeof(buf
))
2357 if (copy_from_user(&buf
, ubuf
, cnt
))
2362 ret
= strict_strtoul(buf
, 10, &val
);
2368 mutex_lock(&trace_types_lock
);
2369 if (tr
->ctrl
^ val
) {
2377 if (current_trace
&& current_trace
->ctrl_update
)
2378 current_trace
->ctrl_update(tr
);
2380 mutex_unlock(&trace_types_lock
);
2388 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2389 size_t cnt
, loff_t
*ppos
)
2391 char buf
[max_tracer_type_len
+2];
2394 mutex_lock(&trace_types_lock
);
2396 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2398 r
= sprintf(buf
, "\n");
2399 mutex_unlock(&trace_types_lock
);
2401 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2404 static int tracing_set_tracer(char *buf
)
2406 struct trace_array
*tr
= &global_trace
;
2410 mutex_lock(&trace_types_lock
);
2411 for (t
= trace_types
; t
; t
= t
->next
) {
2412 if (strcmp(t
->name
, buf
) == 0)
2419 if (t
== current_trace
)
2422 if (current_trace
&& current_trace
->reset
)
2423 current_trace
->reset(tr
);
2430 mutex_unlock(&trace_types_lock
);
2436 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2437 size_t cnt
, loff_t
*ppos
)
2439 char buf
[max_tracer_type_len
+1];
2443 if (cnt
> max_tracer_type_len
)
2444 cnt
= max_tracer_type_len
;
2446 if (copy_from_user(&buf
, ubuf
, cnt
))
2451 /* strip ending whitespace. */
2452 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2455 ret
= tracing_set_tracer(buf
);
2466 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2467 size_t cnt
, loff_t
*ppos
)
2469 unsigned long *ptr
= filp
->private_data
;
2473 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2474 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2475 if (r
> sizeof(buf
))
2477 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2481 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2482 size_t cnt
, loff_t
*ppos
)
2484 long *ptr
= filp
->private_data
;
2489 if (cnt
>= sizeof(buf
))
2492 if (copy_from_user(&buf
, ubuf
, cnt
))
2497 ret
= strict_strtoul(buf
, 10, &val
);
2506 static atomic_t tracing_reader
;
2508 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2510 struct trace_iterator
*iter
;
2512 if (tracing_disabled
)
2515 /* We only allow for reader of the pipe */
2516 if (atomic_inc_return(&tracing_reader
) != 1) {
2517 atomic_dec(&tracing_reader
);
2521 /* create a buffer to store the information to pass to userspace */
2522 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2526 mutex_lock(&trace_types_lock
);
2527 iter
->tr
= &global_trace
;
2528 iter
->trace
= current_trace
;
2529 filp
->private_data
= iter
;
2531 if (iter
->trace
->pipe_open
)
2532 iter
->trace
->pipe_open(iter
);
2533 mutex_unlock(&trace_types_lock
);
2538 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2540 struct trace_iterator
*iter
= file
->private_data
;
2543 atomic_dec(&tracing_reader
);
2549 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2551 struct trace_iterator
*iter
= filp
->private_data
;
2553 if (trace_flags
& TRACE_ITER_BLOCK
) {
2555 * Always select as readable when in blocking mode
2557 return POLLIN
| POLLRDNORM
;
2559 if (!trace_empty(iter
))
2560 return POLLIN
| POLLRDNORM
;
2561 poll_wait(filp
, &trace_wait
, poll_table
);
2562 if (!trace_empty(iter
))
2563 return POLLIN
| POLLRDNORM
;
2573 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2574 size_t cnt
, loff_t
*ppos
)
2576 struct trace_iterator
*iter
= filp
->private_data
;
2579 /* return any leftover data */
2580 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2584 trace_seq_reset(&iter
->seq
);
2586 mutex_lock(&trace_types_lock
);
2587 if (iter
->trace
->read
) {
2588 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2595 while (trace_empty(iter
)) {
2597 if ((filp
->f_flags
& O_NONBLOCK
)) {
2603 * This is a make-shift waitqueue. The reason we don't use
2604 * an actual wait queue is because:
2605 * 1) we only ever have one waiter
2606 * 2) the tracing, traces all functions, we don't want
2607 * the overhead of calling wake_up and friends
2608 * (and tracing them too)
2609 * Anyway, this is really very primitive wakeup.
2611 set_current_state(TASK_INTERRUPTIBLE
);
2612 iter
->tr
->waiter
= current
;
2614 mutex_unlock(&trace_types_lock
);
2616 /* sleep for 100 msecs, and try again. */
2617 schedule_timeout(HZ
/10);
2619 mutex_lock(&trace_types_lock
);
2621 iter
->tr
->waiter
= NULL
;
2623 if (signal_pending(current
)) {
2628 if (iter
->trace
!= current_trace
)
2632 * We block until we read something and tracing is disabled.
2633 * We still block if tracing is disabled, but we have never
2634 * read anything. This allows a user to cat this file, and
2635 * then enable tracing. But after we have read something,
2636 * we give an EOF when tracing is again disabled.
2638 * iter->pos will be 0 if we haven't read anything.
2640 if (!tracer_enabled
&& iter
->pos
)
2646 /* stop when tracing is finished */
2647 if (trace_empty(iter
))
2650 if (cnt
>= PAGE_SIZE
)
2651 cnt
= PAGE_SIZE
- 1;
2653 /* reset all but tr, trace, and overruns */
2654 memset(&iter
->seq
, 0,
2655 sizeof(struct trace_iterator
) -
2656 offsetof(struct trace_iterator
, seq
));
2659 while (find_next_entry_inc(iter
) != NULL
) {
2660 enum print_line_t ret
;
2661 int len
= iter
->seq
.len
;
2663 ret
= print_trace_line(iter
);
2664 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2665 /* don't print partial lines */
2666 iter
->seq
.len
= len
;
2670 trace_consume(iter
);
2672 if (iter
->seq
.len
>= cnt
)
2676 /* Now copy what we have to the user */
2677 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2678 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2679 trace_seq_reset(&iter
->seq
);
2682 * If there was nothing to send to user, inspite of consuming trace
2683 * entries, go back to wait for more entries.
2689 mutex_unlock(&trace_types_lock
);
2695 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
2696 size_t cnt
, loff_t
*ppos
)
2698 struct trace_array
*tr
= filp
->private_data
;
2702 r
= sprintf(buf
, "%lu\n", tr
->entries
);
2703 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2707 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
2708 size_t cnt
, loff_t
*ppos
)
2713 struct trace_array
*tr
= filp
->private_data
;
2715 if (cnt
>= sizeof(buf
))
2718 if (copy_from_user(&buf
, ubuf
, cnt
))
2723 ret
= strict_strtoul(buf
, 10, &val
);
2727 /* must have at least 1 entry */
2731 mutex_lock(&trace_types_lock
);
2735 pr_info("ftrace: please disable tracing"
2736 " before modifying buffer size\n");
2740 if (val
!= global_trace
.entries
) {
2741 ret
= ring_buffer_resize(global_trace
.buffer
, val
);
2747 ret
= ring_buffer_resize(max_tr
.buffer
, val
);
2751 r
= ring_buffer_resize(global_trace
.buffer
,
2752 global_trace
.entries
);
2754 /* AARGH! We are left with different
2755 * size max buffer!!!! */
2757 tracing_disabled
= 1;
2762 global_trace
.entries
= val
;
2767 /* If check pages failed, return ENOMEM */
2768 if (tracing_disabled
)
2771 max_tr
.entries
= global_trace
.entries
;
2772 mutex_unlock(&trace_types_lock
);
2777 static int mark_printk(const char *fmt
, ...)
2781 va_start(args
, fmt
);
2782 ret
= trace_vprintk(0, fmt
, args
);
2788 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
2789 size_t cnt
, loff_t
*fpos
)
2793 struct trace_array
*tr
= &global_trace
;
2795 if (!tr
->ctrl
|| tracing_disabled
)
2798 if (cnt
> TRACE_BUF_SIZE
)
2799 cnt
= TRACE_BUF_SIZE
;
2801 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
2805 if (copy_from_user(buf
, ubuf
, cnt
)) {
2810 /* Cut from the first nil or newline. */
2812 end
= strchr(buf
, '\n');
2816 cnt
= mark_printk("%s\n", buf
);
2823 static struct file_operations tracing_max_lat_fops
= {
2824 .open
= tracing_open_generic
,
2825 .read
= tracing_max_lat_read
,
2826 .write
= tracing_max_lat_write
,
2829 static struct file_operations tracing_ctrl_fops
= {
2830 .open
= tracing_open_generic
,
2831 .read
= tracing_ctrl_read
,
2832 .write
= tracing_ctrl_write
,
2835 static struct file_operations set_tracer_fops
= {
2836 .open
= tracing_open_generic
,
2837 .read
= tracing_set_trace_read
,
2838 .write
= tracing_set_trace_write
,
2841 static struct file_operations tracing_pipe_fops
= {
2842 .open
= tracing_open_pipe
,
2843 .poll
= tracing_poll_pipe
,
2844 .read
= tracing_read_pipe
,
2845 .release
= tracing_release_pipe
,
2848 static struct file_operations tracing_entries_fops
= {
2849 .open
= tracing_open_generic
,
2850 .read
= tracing_entries_read
,
2851 .write
= tracing_entries_write
,
2854 static struct file_operations tracing_mark_fops
= {
2855 .open
= tracing_open_generic
,
2856 .write
= tracing_mark_write
,
2859 #ifdef CONFIG_DYNAMIC_FTRACE
2861 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
2867 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
2868 size_t cnt
, loff_t
*ppos
)
2870 static char ftrace_dyn_info_buffer
[1024];
2871 static DEFINE_MUTEX(dyn_info_mutex
);
2872 unsigned long *p
= filp
->private_data
;
2873 char *buf
= ftrace_dyn_info_buffer
;
2874 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
2877 mutex_lock(&dyn_info_mutex
);
2878 r
= sprintf(buf
, "%ld ", *p
);
2880 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
2883 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2885 mutex_unlock(&dyn_info_mutex
);
2890 static struct file_operations tracing_dyn_info_fops
= {
2891 .open
= tracing_open_generic
,
2892 .read
= tracing_read_dyn_info
,
2896 static struct dentry
*d_tracer
;
2898 struct dentry
*tracing_init_dentry(void)
2905 d_tracer
= debugfs_create_dir("tracing", NULL
);
2907 if (!d_tracer
&& !once
) {
2909 pr_warning("Could not create debugfs directory 'tracing'\n");
2916 #ifdef CONFIG_FTRACE_SELFTEST
2917 /* Let selftest have access to static functions in this file */
2918 #include "trace_selftest.c"
2921 static __init
int tracer_init_debugfs(void)
2923 struct dentry
*d_tracer
;
2924 struct dentry
*entry
;
2926 d_tracer
= tracing_init_dentry();
2928 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
2929 &global_trace
, &tracing_ctrl_fops
);
2931 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2933 entry
= debugfs_create_file("iter_ctrl", 0644, d_tracer
,
2934 NULL
, &tracing_iter_fops
);
2936 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2938 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
2939 NULL
, &tracing_cpumask_fops
);
2941 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2943 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
2944 &global_trace
, &tracing_lt_fops
);
2946 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2948 entry
= debugfs_create_file("trace", 0444, d_tracer
,
2949 &global_trace
, &tracing_fops
);
2951 pr_warning("Could not create debugfs 'trace' entry\n");
2953 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
2954 &global_trace
, &show_traces_fops
);
2956 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2958 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
2959 &global_trace
, &set_tracer_fops
);
2961 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2963 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
2964 &tracing_max_latency
,
2965 &tracing_max_lat_fops
);
2967 pr_warning("Could not create debugfs "
2968 "'tracing_max_latency' entry\n");
2970 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
2971 &tracing_thresh
, &tracing_max_lat_fops
);
2973 pr_warning("Could not create debugfs "
2974 "'tracing_thresh' entry\n");
2975 entry
= debugfs_create_file("README", 0644, d_tracer
,
2976 NULL
, &tracing_readme_fops
);
2978 pr_warning("Could not create debugfs 'README' entry\n");
2980 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
2981 NULL
, &tracing_pipe_fops
);
2983 pr_warning("Could not create debugfs "
2984 "'trace_pipe' entry\n");
2986 entry
= debugfs_create_file("trace_entries", 0644, d_tracer
,
2987 &global_trace
, &tracing_entries_fops
);
2989 pr_warning("Could not create debugfs "
2990 "'trace_entries' entry\n");
2992 entry
= debugfs_create_file("trace_marker", 0220, d_tracer
,
2993 NULL
, &tracing_mark_fops
);
2995 pr_warning("Could not create debugfs "
2996 "'trace_marker' entry\n");
2998 #ifdef CONFIG_DYNAMIC_FTRACE
2999 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
3000 &ftrace_update_tot_cnt
,
3001 &tracing_dyn_info_fops
);
3003 pr_warning("Could not create debugfs "
3004 "'dyn_ftrace_total_info' entry\n");
3006 #ifdef CONFIG_SYSPROF_TRACER
3007 init_tracer_sysprof_debugfs(d_tracer
);
3012 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
3014 static DEFINE_SPINLOCK(trace_buf_lock
);
3015 static char trace_buf
[TRACE_BUF_SIZE
];
3017 struct ring_buffer_event
*event
;
3018 struct trace_array
*tr
= &global_trace
;
3019 struct trace_array_cpu
*data
;
3020 struct print_entry
*entry
;
3021 unsigned long flags
, irq_flags
;
3022 int cpu
, len
= 0, size
, pc
;
3024 if (!tr
->ctrl
|| tracing_disabled
)
3027 pc
= preempt_count();
3028 preempt_disable_notrace();
3029 cpu
= raw_smp_processor_id();
3030 data
= tr
->data
[cpu
];
3032 if (unlikely(atomic_read(&data
->disabled
)))
3035 spin_lock_irqsave(&trace_buf_lock
, flags
);
3036 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
3038 len
= min(len
, TRACE_BUF_SIZE
-1);
3041 size
= sizeof(*entry
) + len
+ 1;
3042 event
= ring_buffer_lock_reserve(tr
->buffer
, size
, &irq_flags
);
3045 entry
= ring_buffer_event_data(event
);
3046 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
3047 entry
->ent
.type
= TRACE_PRINT
;
3050 memcpy(&entry
->buf
, trace_buf
, len
);
3051 entry
->buf
[len
] = 0;
3052 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
3055 spin_unlock_irqrestore(&trace_buf_lock
, flags
);
3058 preempt_enable_notrace();
3062 EXPORT_SYMBOL_GPL(trace_vprintk
);
3064 int __ftrace_printk(unsigned long ip
, const char *fmt
, ...)
3069 if (!(trace_flags
& TRACE_ITER_PRINTK
))
3073 ret
= trace_vprintk(ip
, fmt
, ap
);
3077 EXPORT_SYMBOL_GPL(__ftrace_printk
);
3079 static int trace_panic_handler(struct notifier_block
*this,
3080 unsigned long event
, void *unused
)
3082 if (ftrace_dump_on_oops
)
3087 static struct notifier_block trace_panic_notifier
= {
3088 .notifier_call
= trace_panic_handler
,
3090 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
3093 static int trace_die_handler(struct notifier_block
*self
,
3099 if (ftrace_dump_on_oops
)
3108 static struct notifier_block trace_die_notifier
= {
3109 .notifier_call
= trace_die_handler
,
3114 * printk is set to max of 1024, we really don't need it that big.
3115 * Nothing should be printing 1000 characters anyway.
3117 #define TRACE_MAX_PRINT 1000
3120 * Define here KERN_TRACE so that we have one place to modify
3121 * it if we decide to change what log level the ftrace dump
3124 #define KERN_TRACE KERN_INFO
3127 trace_printk_seq(struct trace_seq
*s
)
3129 /* Probably should print a warning here. */
3133 /* should be zero ended, but we are paranoid. */
3134 s
->buffer
[s
->len
] = 0;
3136 printk(KERN_TRACE
"%s", s
->buffer
);
3141 void ftrace_dump(void)
3143 static DEFINE_SPINLOCK(ftrace_dump_lock
);
3144 /* use static because iter can be a bit big for the stack */
3145 static struct trace_iterator iter
;
3146 static cpumask_t mask
;
3147 static int dump_ran
;
3148 unsigned long flags
;
3152 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
3158 /* No turning back! */
3161 for_each_tracing_cpu(cpu
) {
3162 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3165 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
3167 iter
.tr
= &global_trace
;
3168 iter
.trace
= current_trace
;
3171 * We need to stop all tracing on all CPUS to read the
3172 * the next buffer. This is a bit expensive, but is
3173 * not done often. We fill all what we can read,
3174 * and then release the locks again.
3179 while (!trace_empty(&iter
)) {
3182 printk(KERN_TRACE
"---------------------------------\n");
3186 /* reset all but tr, trace, and overruns */
3187 memset(&iter
.seq
, 0,
3188 sizeof(struct trace_iterator
) -
3189 offsetof(struct trace_iterator
, seq
));
3190 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
3193 if (find_next_entry_inc(&iter
) != NULL
) {
3194 print_trace_line(&iter
);
3195 trace_consume(&iter
);
3198 trace_printk_seq(&iter
.seq
);
3202 printk(KERN_TRACE
" (ftrace buffer empty)\n");
3204 printk(KERN_TRACE
"---------------------------------\n");
3207 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
3210 __init
static int tracer_alloc_buffers(void)
3212 struct trace_array_cpu
*data
;
3215 /* TODO: make the number of buffers hot pluggable with CPUS */
3216 tracing_buffer_mask
= cpu_possible_map
;
3218 global_trace
.buffer
= ring_buffer_alloc(trace_buf_size
,
3219 TRACE_BUFFER_FLAGS
);
3220 if (!global_trace
.buffer
) {
3221 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
3225 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
3227 #ifdef CONFIG_TRACER_MAX_TRACE
3228 max_tr
.buffer
= ring_buffer_alloc(trace_buf_size
,
3229 TRACE_BUFFER_FLAGS
);
3230 if (!max_tr
.buffer
) {
3231 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
3233 ring_buffer_free(global_trace
.buffer
);
3236 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
3237 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
3240 /* Allocate the first page for all buffers */
3241 for_each_tracing_cpu(i
) {
3242 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
3243 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
3246 trace_init_cmdlines();
3248 register_tracer(&nop_trace
);
3249 #ifdef CONFIG_BOOT_TRACER
3250 register_tracer(&boot_tracer
);
3251 current_trace
= &boot_tracer
;
3252 current_trace
->init(&global_trace
);
3254 current_trace
= &nop_trace
;
3257 /* All seems OK, enable tracing */
3258 global_trace
.ctrl
= tracer_enabled
;
3259 tracing_disabled
= 0;
3261 atomic_notifier_chain_register(&panic_notifier_list
,
3262 &trace_panic_notifier
);
3264 register_die_notifier(&trace_die_notifier
);
3268 early_initcall(tracer_alloc_buffers
);
3269 fs_initcall(tracer_init_debugfs
);