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>
40 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
42 unsigned long __read_mostly tracing_max_latency
= (cycle_t
)ULONG_MAX
;
43 unsigned long __read_mostly tracing_thresh
;
45 static DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
47 static inline void ftrace_disable_cpu(void)
50 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
53 static inline void ftrace_enable_cpu(void)
55 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
59 static cpumask_t __read_mostly tracing_buffer_mask
;
61 #define for_each_tracing_cpu(cpu) \
62 for_each_cpu_mask(cpu, tracing_buffer_mask)
64 static int tracing_disabled
= 1;
67 ns2usecs(cycle_t nsec
)
74 cycle_t
ftrace_now(int cpu
)
76 u64 ts
= ring_buffer_time_stamp(cpu
);
77 ring_buffer_normalize_time_stamp(cpu
, &ts
);
82 * The global_trace is the descriptor that holds the tracing
83 * buffers for the live tracing. For each CPU, it contains
84 * a link list of pages that will store trace entries. The
85 * page descriptor of the pages in the memory is used to hold
86 * the link list by linking the lru item in the page descriptor
87 * to each of the pages in the buffer per CPU.
89 * For each active CPU there is a data field that holds the
90 * pages for the buffer for that CPU. Each CPU has the same number
91 * of pages allocated for its buffer.
93 static struct trace_array global_trace
;
95 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
98 * The max_tr is used to snapshot the global_trace when a maximum
99 * latency is reached. Some tracers will use this to store a maximum
100 * trace while it continues examining live traces.
102 * The buffers for the max_tr are set up the same as the global_trace.
103 * When a snapshot is taken, the link list of the max_tr is swapped
104 * with the link list of the global_trace and the buffers are reset for
105 * the global_trace so the tracing can continue.
107 static struct trace_array max_tr
;
109 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
111 /* tracer_enabled is used to toggle activation of a tracer */
112 static int tracer_enabled
= 1;
114 /* function tracing enabled */
115 int ftrace_function_enabled
;
118 * trace_buf_size is the size in bytes that is allocated
119 * for a buffer. Note, the number of bytes is always rounded
122 * This number is purposely set to a low number of 16384.
123 * If the dump on oops happens, it will be much appreciated
124 * to not have to wait for all that output. Anyway this can be
125 * boot time and run time configurable.
127 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
129 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
131 /* trace_types holds a link list of available tracers. */
132 static struct tracer
*trace_types __read_mostly
;
134 /* current_trace points to the tracer that is currently active */
135 static struct tracer
*current_trace __read_mostly
;
138 * max_tracer_type_len is used to simplify the allocating of
139 * buffers to read userspace tracer names. We keep track of
140 * the longest tracer name registered.
142 static int max_tracer_type_len
;
145 * trace_types_lock is used to protect the trace_types list.
146 * This lock is also used to keep user access serialized.
147 * Accesses from userspace will grab this lock while userspace
148 * activities happen inside the kernel.
150 static DEFINE_MUTEX(trace_types_lock
);
152 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
153 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
155 /* trace_flags holds iter_ctrl options */
156 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
;
159 * trace_wake_up - wake up tasks waiting for trace input
161 * Simply wakes up any task that is blocked on the trace_wait
162 * queue. These is used with trace_poll for tasks polling the trace.
164 void trace_wake_up(void)
167 * The runqueue_is_locked() can fail, but this is the best we
170 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
171 wake_up(&trace_wait
);
174 static int __init
set_buf_size(char *str
)
176 unsigned long buf_size
;
181 ret
= strict_strtoul(str
, 0, &buf_size
);
182 /* nr_entries can not be zero */
183 if (ret
< 0 || buf_size
== 0)
185 trace_buf_size
= buf_size
;
188 __setup("trace_buf_size=", set_buf_size
);
190 unsigned long nsecs_to_usecs(unsigned long nsecs
)
196 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
197 * control the output of kernel symbols.
199 #define TRACE_ITER_SYM_MASK \
200 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
202 /* These must match the bit postions in trace_iterator_flags */
203 static const char *trace_options
[] = {
219 * ftrace_max_lock is used to protect the swapping of buffers
220 * when taking a max snapshot. The buffers themselves are
221 * protected by per_cpu spinlocks. But the action of the swap
222 * needs its own lock.
224 * This is defined as a raw_spinlock_t in order to help
225 * with performance when lockdep debugging is enabled.
227 static raw_spinlock_t ftrace_max_lock
=
228 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
231 * Copy the new maximum trace into the separate maximum-trace
232 * structure. (this way the maximum trace is permanently saved,
233 * for later retrieval via /debugfs/tracing/latency_trace)
236 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
238 struct trace_array_cpu
*data
= tr
->data
[cpu
];
241 max_tr
.time_start
= data
->preempt_timestamp
;
243 data
= max_tr
.data
[cpu
];
244 data
->saved_latency
= tracing_max_latency
;
246 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
247 data
->pid
= tsk
->pid
;
248 data
->uid
= tsk
->uid
;
249 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
250 data
->policy
= tsk
->policy
;
251 data
->rt_priority
= tsk
->rt_priority
;
253 /* record this tasks comm */
254 tracing_record_cmdline(current
);
258 * trace_seq_printf - sequence printing of trace information
259 * @s: trace sequence descriptor
260 * @fmt: printf format string
262 * The tracer may use either sequence operations or its own
263 * copy to user routines. To simplify formating of a trace
264 * trace_seq_printf is used to store strings into a special
265 * buffer (@s). Then the output may be either used by
266 * the sequencer or pulled into another buffer.
269 trace_seq_printf(struct trace_seq
*s
, const char *fmt
, ...)
271 int len
= (PAGE_SIZE
- 1) - s
->len
;
279 ret
= vsnprintf(s
->buffer
+ s
->len
, len
, fmt
, ap
);
282 /* If we can't write it all, don't bother writing anything */
292 * trace_seq_puts - trace sequence printing of simple string
293 * @s: trace sequence descriptor
294 * @str: simple string to record
296 * The tracer may use either the sequence operations or its own
297 * copy to user routines. This function records a simple string
298 * into a special buffer (@s) for later retrieval by a sequencer
299 * or other mechanism.
302 trace_seq_puts(struct trace_seq
*s
, const char *str
)
304 int len
= strlen(str
);
306 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
309 memcpy(s
->buffer
+ s
->len
, str
, len
);
316 trace_seq_putc(struct trace_seq
*s
, unsigned char c
)
318 if (s
->len
>= (PAGE_SIZE
- 1))
321 s
->buffer
[s
->len
++] = c
;
327 trace_seq_putmem(struct trace_seq
*s
, void *mem
, size_t len
)
329 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
332 memcpy(s
->buffer
+ s
->len
, mem
, len
);
339 static const char hex2asc
[] = "0123456789abcdef";
342 trace_seq_putmem_hex(struct trace_seq
*s
, void *mem
, size_t len
)
344 unsigned char hex
[HEX_CHARS
];
345 unsigned char *data
= mem
;
349 BUG_ON(len
>= HEX_CHARS
);
352 for (i
= 0, j
= 0; i
< len
; i
++) {
354 for (i
= len
-1, j
= 0; i
>= 0; i
--) {
358 hex
[j
++] = hex2asc
[byte
& 0x0f];
359 hex
[j
++] = hex2asc
[byte
>> 4];
363 return trace_seq_putmem(s
, hex
, j
);
367 trace_seq_reset(struct trace_seq
*s
)
373 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
378 if (s
->len
<= s
->readpos
)
381 len
= s
->len
- s
->readpos
;
384 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
393 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
395 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
398 seq_puts(m
, s
->buffer
);
404 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
406 * @tsk: the task with the latency
407 * @cpu: The cpu that initiated the trace.
409 * Flip the buffers between the @tr and the max_tr and record information
410 * about which task was the cause of this latency.
413 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
415 struct ring_buffer
*buf
= tr
->buffer
;
417 WARN_ON_ONCE(!irqs_disabled());
418 __raw_spin_lock(&ftrace_max_lock
);
420 tr
->buffer
= max_tr
.buffer
;
423 ftrace_disable_cpu();
424 ring_buffer_reset(tr
->buffer
);
427 __update_max_tr(tr
, tsk
, cpu
);
428 __raw_spin_unlock(&ftrace_max_lock
);
432 * update_max_tr_single - only copy one trace over, and reset the rest
434 * @tsk - task with the latency
435 * @cpu - the cpu of the buffer to copy.
437 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
440 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
444 WARN_ON_ONCE(!irqs_disabled());
445 __raw_spin_lock(&ftrace_max_lock
);
447 ftrace_disable_cpu();
449 ring_buffer_reset(max_tr
.buffer
);
450 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
456 __update_max_tr(tr
, tsk
, cpu
);
457 __raw_spin_unlock(&ftrace_max_lock
);
461 * register_tracer - register a tracer with the ftrace system.
462 * @type - the plugin for the tracer
464 * Register a new plugin tracer.
466 int register_tracer(struct tracer
*type
)
473 pr_info("Tracer must have a name\n");
477 mutex_lock(&trace_types_lock
);
478 for (t
= trace_types
; t
; t
= t
->next
) {
479 if (strcmp(type
->name
, t
->name
) == 0) {
481 pr_info("Trace %s already registered\n",
488 #ifdef CONFIG_FTRACE_STARTUP_TEST
489 if (type
->selftest
) {
490 struct tracer
*saved_tracer
= current_trace
;
491 struct trace_array
*tr
= &global_trace
;
492 int saved_ctrl
= tr
->ctrl
;
495 * Run a selftest on this tracer.
496 * Here we reset the trace buffer, and set the current
497 * tracer to be this tracer. The tracer can then run some
498 * internal tracing to verify that everything is in order.
499 * If we fail, we do not register this tracer.
501 for_each_tracing_cpu(i
) {
502 tracing_reset(tr
, i
);
504 current_trace
= type
;
506 /* the test is responsible for initializing and enabling */
507 pr_info("Testing tracer %s: ", type
->name
);
508 ret
= type
->selftest(type
, tr
);
509 /* the test is responsible for resetting too */
510 current_trace
= saved_tracer
;
511 tr
->ctrl
= saved_ctrl
;
513 printk(KERN_CONT
"FAILED!\n");
516 /* Only reset on passing, to avoid touching corrupted buffers */
517 for_each_tracing_cpu(i
) {
518 tracing_reset(tr
, i
);
520 printk(KERN_CONT
"PASSED\n");
524 type
->next
= trace_types
;
526 len
= strlen(type
->name
);
527 if (len
> max_tracer_type_len
)
528 max_tracer_type_len
= len
;
531 mutex_unlock(&trace_types_lock
);
536 void unregister_tracer(struct tracer
*type
)
541 mutex_lock(&trace_types_lock
);
542 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
546 pr_info("Trace %s not registered\n", type
->name
);
551 if (strlen(type
->name
) != max_tracer_type_len
)
554 max_tracer_type_len
= 0;
555 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
556 len
= strlen((*t
)->name
);
557 if (len
> max_tracer_type_len
)
558 max_tracer_type_len
= len
;
561 mutex_unlock(&trace_types_lock
);
564 void tracing_reset(struct trace_array
*tr
, int cpu
)
566 ftrace_disable_cpu();
567 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
571 #define SAVED_CMDLINES 128
572 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
573 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
574 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
575 static int cmdline_idx
;
576 static DEFINE_SPINLOCK(trace_cmdline_lock
);
578 /* temporary disable recording */
579 atomic_t trace_record_cmdline_disabled __read_mostly
;
581 static void trace_init_cmdlines(void)
583 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
584 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
588 void trace_stop_cmdline_recording(void);
590 static void trace_save_cmdline(struct task_struct
*tsk
)
595 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
599 * It's not the end of the world if we don't get
600 * the lock, but we also don't want to spin
601 * nor do we want to disable interrupts,
602 * so if we miss here, then better luck next time.
604 if (!spin_trylock(&trace_cmdline_lock
))
607 idx
= map_pid_to_cmdline
[tsk
->pid
];
608 if (idx
>= SAVED_CMDLINES
) {
609 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
611 map
= map_cmdline_to_pid
[idx
];
612 if (map
<= PID_MAX_DEFAULT
)
613 map_pid_to_cmdline
[map
] = (unsigned)-1;
615 map_pid_to_cmdline
[tsk
->pid
] = idx
;
620 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
622 spin_unlock(&trace_cmdline_lock
);
625 static char *trace_find_cmdline(int pid
)
627 char *cmdline
= "<...>";
633 if (pid
> PID_MAX_DEFAULT
)
636 map
= map_pid_to_cmdline
[pid
];
637 if (map
>= SAVED_CMDLINES
)
640 cmdline
= saved_cmdlines
[map
];
646 void tracing_record_cmdline(struct task_struct
*tsk
)
648 if (atomic_read(&trace_record_cmdline_disabled
))
651 trace_save_cmdline(tsk
);
655 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
)
657 struct task_struct
*tsk
= current
;
660 pc
= preempt_count();
662 entry
->preempt_count
= pc
& 0xff;
663 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
665 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
666 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
667 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
668 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
672 trace_function(struct trace_array
*tr
, struct trace_array_cpu
*data
,
673 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
)
675 struct ring_buffer_event
*event
;
676 struct ftrace_entry
*entry
;
677 unsigned long irq_flags
;
679 /* If we are reading the ring buffer, don't trace */
680 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
683 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
687 entry
= ring_buffer_event_data(event
);
688 tracing_generic_entry_update(&entry
->ent
, flags
);
689 entry
->ent
.type
= TRACE_FN
;
691 entry
->parent_ip
= parent_ip
;
692 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
696 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
697 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
)
699 if (likely(!atomic_read(&data
->disabled
)))
700 trace_function(tr
, data
, ip
, parent_ip
, flags
);
703 void __trace_stack(struct trace_array
*tr
,
704 struct trace_array_cpu
*data
,
708 struct ring_buffer_event
*event
;
709 struct stack_entry
*entry
;
710 struct stack_trace trace
;
711 unsigned long irq_flags
;
713 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
716 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
720 entry
= ring_buffer_event_data(event
);
721 tracing_generic_entry_update(&entry
->ent
, flags
);
722 entry
->ent
.type
= TRACE_STACK
;
724 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
726 trace
.nr_entries
= 0;
727 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
729 trace
.entries
= entry
->caller
;
731 save_stack_trace(&trace
);
732 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
736 __trace_special(void *__tr
, void *__data
,
737 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
739 struct ring_buffer_event
*event
;
740 struct trace_array_cpu
*data
= __data
;
741 struct trace_array
*tr
= __tr
;
742 struct special_entry
*entry
;
743 unsigned long irq_flags
;
745 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
749 entry
= ring_buffer_event_data(event
);
750 tracing_generic_entry_update(&entry
->ent
, 0);
751 entry
->ent
.type
= TRACE_SPECIAL
;
755 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
756 __trace_stack(tr
, data
, irq_flags
, 4);
762 tracing_sched_switch_trace(struct trace_array
*tr
,
763 struct trace_array_cpu
*data
,
764 struct task_struct
*prev
,
765 struct task_struct
*next
,
768 struct ring_buffer_event
*event
;
769 struct ctx_switch_entry
*entry
;
770 unsigned long irq_flags
;
772 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
776 entry
= ring_buffer_event_data(event
);
777 tracing_generic_entry_update(&entry
->ent
, flags
);
778 entry
->ent
.type
= TRACE_CTX
;
779 entry
->prev_pid
= prev
->pid
;
780 entry
->prev_prio
= prev
->prio
;
781 entry
->prev_state
= prev
->state
;
782 entry
->next_pid
= next
->pid
;
783 entry
->next_prio
= next
->prio
;
784 entry
->next_state
= next
->state
;
785 entry
->next_cpu
= task_cpu(next
);
786 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
787 __trace_stack(tr
, data
, flags
, 5);
791 tracing_sched_wakeup_trace(struct trace_array
*tr
,
792 struct trace_array_cpu
*data
,
793 struct task_struct
*wakee
,
794 struct task_struct
*curr
,
797 struct ring_buffer_event
*event
;
798 struct ctx_switch_entry
*entry
;
799 unsigned long irq_flags
;
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
);
807 entry
->ent
.type
= TRACE_WAKE
;
808 entry
->prev_pid
= curr
->pid
;
809 entry
->prev_prio
= curr
->prio
;
810 entry
->prev_state
= curr
->state
;
811 entry
->next_pid
= wakee
->pid
;
812 entry
->next_prio
= wakee
->prio
;
813 entry
->next_state
= wakee
->state
;
814 entry
->next_cpu
= task_cpu(wakee
);
815 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
816 __trace_stack(tr
, data
, flags
, 6);
822 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
824 struct trace_array
*tr
= &global_trace
;
825 struct trace_array_cpu
*data
;
830 if (tracing_disabled
|| !tr
->ctrl
)
833 local_irq_save(flags
);
834 cpu
= raw_smp_processor_id();
835 data
= tr
->data
[cpu
];
836 disabled
= atomic_inc_return(&data
->disabled
);
838 if (likely(disabled
== 1))
839 __trace_special(tr
, data
, arg1
, arg2
, arg3
);
841 atomic_dec(&data
->disabled
);
842 local_irq_restore(flags
);
847 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
849 struct trace_array
*tr
= &global_trace
;
850 struct trace_array_cpu
*data
;
855 if (unlikely(!ftrace_function_enabled
))
861 local_irq_save(flags
);
862 cpu
= raw_smp_processor_id();
863 data
= tr
->data
[cpu
];
864 disabled
= atomic_inc_return(&data
->disabled
);
866 if (likely(disabled
== 1))
867 trace_function(tr
, data
, ip
, parent_ip
, flags
);
869 atomic_dec(&data
->disabled
);
870 local_irq_restore(flags
);
873 static struct ftrace_ops trace_ops __read_mostly
=
875 .func
= function_trace_call
,
878 void tracing_start_function_trace(void)
880 ftrace_function_enabled
= 0;
881 register_ftrace_function(&trace_ops
);
883 ftrace_function_enabled
= 1;
886 void tracing_stop_function_trace(void)
888 ftrace_function_enabled
= 0;
889 unregister_ftrace_function(&trace_ops
);
893 enum trace_file_type
{
894 TRACE_FILE_LAT_FMT
= 1,
897 static void trace_iterator_increment(struct trace_iterator
*iter
, int cpu
)
899 /* Don't allow ftrace to trace into the ring buffers */
900 ftrace_disable_cpu();
903 if (iter
->buffer_iter
[iter
->cpu
])
904 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
909 static struct trace_entry
*
910 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
912 struct ring_buffer_event
*event
;
913 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
915 /* Don't allow ftrace to trace into the ring buffers */
916 ftrace_disable_cpu();
919 event
= ring_buffer_iter_peek(buf_iter
, ts
);
921 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
925 return event
? ring_buffer_event_data(event
) : NULL
;
928 static struct trace_entry
*
929 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
931 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
932 struct trace_entry
*ent
, *next
= NULL
;
937 for_each_tracing_cpu(cpu
) {
939 if (ring_buffer_empty_cpu(buffer
, cpu
))
942 ent
= peek_next_entry(iter
, cpu
, &ts
);
945 * Pick the entry with the smallest timestamp:
947 if (ent
&& (!next
|| ts
< next_ts
)) {
963 /* Find the next real entry, without updating the iterator itself */
964 static struct trace_entry
*
965 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
967 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
970 /* Find the next real entry, and increment the iterator to the next entry */
971 static void *find_next_entry_inc(struct trace_iterator
*iter
)
973 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
976 trace_iterator_increment(iter
, iter
->cpu
);
978 return iter
->ent
? iter
: NULL
;
981 static void trace_consume(struct trace_iterator
*iter
)
983 /* Don't allow ftrace to trace into the ring buffers */
984 ftrace_disable_cpu();
985 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
989 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
991 struct trace_iterator
*iter
= m
->private;
997 /* can't go backwards */
1002 ent
= find_next_entry_inc(iter
);
1006 while (ent
&& iter
->idx
< i
)
1007 ent
= find_next_entry_inc(iter
);
1014 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1016 struct trace_iterator
*iter
= m
->private;
1021 mutex_lock(&trace_types_lock
);
1023 if (!current_trace
|| current_trace
!= iter
->trace
) {
1024 mutex_unlock(&trace_types_lock
);
1028 atomic_inc(&trace_record_cmdline_disabled
);
1030 /* let the tracer grab locks here if needed */
1031 if (current_trace
->start
)
1032 current_trace
->start(iter
);
1034 if (*pos
!= iter
->pos
) {
1039 ftrace_disable_cpu();
1041 for_each_tracing_cpu(cpu
) {
1042 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1045 ftrace_enable_cpu();
1047 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1052 p
= s_next(m
, p
, &l
);
1058 static void s_stop(struct seq_file
*m
, void *p
)
1060 struct trace_iterator
*iter
= m
->private;
1062 atomic_dec(&trace_record_cmdline_disabled
);
1064 /* let the tracer release locks here if needed */
1065 if (current_trace
&& current_trace
== iter
->trace
&& iter
->trace
->stop
)
1066 iter
->trace
->stop(iter
);
1068 mutex_unlock(&trace_types_lock
);
1071 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1073 #ifdef CONFIG_KRETPROBES
1074 static inline int kretprobed(unsigned long addr
)
1076 return addr
== (unsigned long)kretprobe_trampoline
;
1079 static inline int kretprobed(unsigned long addr
)
1083 #endif /* CONFIG_KRETPROBES */
1086 seq_print_sym_short(struct trace_seq
*s
, const char *fmt
, unsigned long address
)
1088 #ifdef CONFIG_KALLSYMS
1089 char str
[KSYM_SYMBOL_LEN
];
1091 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
1093 return trace_seq_printf(s
, fmt
, str
);
1099 seq_print_sym_offset(struct trace_seq
*s
, const char *fmt
,
1100 unsigned long address
)
1102 #ifdef CONFIG_KALLSYMS
1103 char str
[KSYM_SYMBOL_LEN
];
1105 sprint_symbol(str
, address
);
1106 return trace_seq_printf(s
, fmt
, str
);
1111 #ifndef CONFIG_64BIT
1112 # define IP_FMT "%08lx"
1114 # define IP_FMT "%016lx"
1118 seq_print_ip_sym(struct trace_seq
*s
, unsigned long ip
, unsigned long sym_flags
)
1123 return trace_seq_printf(s
, "0");
1125 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
1126 ret
= seq_print_sym_offset(s
, "%s", ip
);
1128 ret
= seq_print_sym_short(s
, "%s", ip
);
1133 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
1134 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
1138 static void print_lat_help_header(struct seq_file
*m
)
1140 seq_puts(m
, "# _------=> CPU# \n");
1141 seq_puts(m
, "# / _-----=> irqs-off \n");
1142 seq_puts(m
, "# | / _----=> need-resched \n");
1143 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1144 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1145 seq_puts(m
, "# |||| / \n");
1146 seq_puts(m
, "# ||||| delay \n");
1147 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1148 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1151 static void print_func_help_header(struct seq_file
*m
)
1153 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1154 seq_puts(m
, "# | | | | |\n");
1159 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1161 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1162 struct trace_array
*tr
= iter
->tr
;
1163 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1164 struct tracer
*type
= current_trace
;
1165 unsigned long total
;
1166 unsigned long entries
;
1167 const char *name
= "preemption";
1172 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1174 ring_buffer_overruns(iter
->tr
->buffer
);
1176 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1178 seq_puts(m
, "-----------------------------------"
1179 "---------------------------------\n");
1180 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1181 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1182 nsecs_to_usecs(data
->saved_latency
),
1186 #if defined(CONFIG_PREEMPT_NONE)
1188 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1190 #elif defined(CONFIG_PREEMPT)
1195 /* These are reserved for later use */
1198 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1202 seq_puts(m
, " -----------------\n");
1203 seq_printf(m
, " | task: %.16s-%d "
1204 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1205 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1206 data
->policy
, data
->rt_priority
);
1207 seq_puts(m
, " -----------------\n");
1209 if (data
->critical_start
) {
1210 seq_puts(m
, " => started at: ");
1211 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1212 trace_print_seq(m
, &iter
->seq
);
1213 seq_puts(m
, "\n => ended at: ");
1214 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1215 trace_print_seq(m
, &iter
->seq
);
1223 lat_print_generic(struct trace_seq
*s
, struct trace_entry
*entry
, int cpu
)
1225 int hardirq
, softirq
;
1228 comm
= trace_find_cmdline(entry
->pid
);
1230 trace_seq_printf(s
, "%8.8s-%-5d ", comm
, entry
->pid
);
1231 trace_seq_printf(s
, "%3d", cpu
);
1232 trace_seq_printf(s
, "%c%c",
1233 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' : '.',
1234 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
1236 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
1237 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
1238 if (hardirq
&& softirq
) {
1239 trace_seq_putc(s
, 'H');
1242 trace_seq_putc(s
, 'h');
1245 trace_seq_putc(s
, 's');
1247 trace_seq_putc(s
, '.');
1251 if (entry
->preempt_count
)
1252 trace_seq_printf(s
, "%x", entry
->preempt_count
);
1254 trace_seq_puts(s
, ".");
1257 unsigned long preempt_mark_thresh
= 100;
1260 lat_print_timestamp(struct trace_seq
*s
, u64 abs_usecs
,
1261 unsigned long rel_usecs
)
1263 trace_seq_printf(s
, " %4lldus", abs_usecs
);
1264 if (rel_usecs
> preempt_mark_thresh
)
1265 trace_seq_puts(s
, "!: ");
1266 else if (rel_usecs
> 1)
1267 trace_seq_puts(s
, "+: ");
1269 trace_seq_puts(s
, " : ");
1272 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
1275 * The message is supposed to contain an ending newline.
1276 * If the printing stops prematurely, try to add a newline of our own.
1278 void trace_seq_print_cont(struct trace_seq
*s
, struct trace_iterator
*iter
)
1280 struct trace_entry
*ent
;
1281 struct trace_field_cont
*cont
;
1284 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1285 if (!ent
|| ent
->type
!= TRACE_CONT
) {
1286 trace_seq_putc(s
, '\n');
1291 cont
= (struct trace_field_cont
*)ent
;
1293 ok
= (trace_seq_printf(s
, "%s", cont
->buf
) > 0);
1295 ftrace_disable_cpu();
1297 if (iter
->buffer_iter
[iter
->cpu
])
1298 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1300 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, NULL
);
1302 ftrace_enable_cpu();
1304 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1305 } while (ent
&& ent
->type
== TRACE_CONT
);
1308 trace_seq_putc(s
, '\n');
1311 static enum print_line_t
1312 print_lat_fmt(struct trace_iterator
*iter
, unsigned int trace_idx
, int cpu
)
1314 struct trace_seq
*s
= &iter
->seq
;
1315 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1316 struct trace_entry
*next_entry
;
1317 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
1318 struct trace_entry
*entry
= iter
->ent
;
1319 unsigned long abs_usecs
;
1320 unsigned long rel_usecs
;
1327 if (entry
->type
== TRACE_CONT
)
1328 return TRACE_TYPE_HANDLED
;
1330 next_entry
= find_next_entry(iter
, NULL
, &next_ts
);
1333 rel_usecs
= ns2usecs(next_ts
- iter
->ts
);
1334 abs_usecs
= ns2usecs(iter
->ts
- iter
->tr
->time_start
);
1337 comm
= trace_find_cmdline(entry
->pid
);
1338 trace_seq_printf(s
, "%16s %5d %3d %d %08x %08x [%08lx]"
1339 " %ld.%03ldms (+%ld.%03ldms): ",
1341 entry
->pid
, cpu
, entry
->flags
,
1342 entry
->preempt_count
, trace_idx
,
1345 abs_usecs
% 1000, rel_usecs
/1000,
1348 lat_print_generic(s
, entry
, cpu
);
1349 lat_print_timestamp(s
, abs_usecs
, rel_usecs
);
1351 switch (entry
->type
) {
1353 struct ftrace_entry
*field
= (struct ftrace_entry
*)entry
;
1355 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1356 trace_seq_puts(s
, " (");
1357 if (kretprobed(field
->parent_ip
))
1358 trace_seq_puts(s
, KRETPROBE_MSG
);
1360 seq_print_ip_sym(s
, field
->parent_ip
, sym_flags
);
1361 trace_seq_puts(s
, ")\n");
1366 struct ctx_switch_entry
*field
=
1367 (struct ctx_switch_entry
*)entry
;
1369 T
= field
->next_state
< sizeof(state_to_char
) ?
1370 state_to_char
[field
->next_state
] : 'X';
1372 state
= field
->prev_state
?
1373 __ffs(field
->prev_state
) + 1 : 0;
1374 S
= state
< sizeof(state_to_char
) - 1 ? state_to_char
[state
] : 'X';
1375 comm
= trace_find_cmdline(field
->next_pid
);
1376 trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1379 S
, entry
->type
== TRACE_CTX
? "==>" : " +",
1386 case TRACE_SPECIAL
: {
1387 struct special_entry
*field
= (struct special_entry
*)entry
;
1389 trace_seq_printf(s
, "# %ld %ld %ld\n",
1396 struct stack_entry
*field
= (struct stack_entry
*)entry
;
1398 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1400 trace_seq_puts(s
, " <= ");
1401 seq_print_ip_sym(s
, field
->caller
[i
], sym_flags
);
1403 trace_seq_puts(s
, "\n");
1407 struct print_entry
*field
= (struct print_entry
*)entry
;
1409 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1410 trace_seq_printf(s
, ": %s", field
->buf
);
1411 if (entry
->flags
& TRACE_FLAG_CONT
)
1412 trace_seq_print_cont(s
, iter
);
1416 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
1418 return TRACE_TYPE_HANDLED
;
1421 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1423 struct trace_seq
*s
= &iter
->seq
;
1424 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1425 struct trace_entry
*entry
;
1426 unsigned long usec_rem
;
1427 unsigned long long t
;
1436 if (entry
->type
== TRACE_CONT
)
1437 return TRACE_TYPE_HANDLED
;
1439 comm
= trace_find_cmdline(iter
->ent
->pid
);
1441 t
= ns2usecs(iter
->ts
);
1442 usec_rem
= do_div(t
, 1000000ULL);
1443 secs
= (unsigned long)t
;
1445 ret
= trace_seq_printf(s
, "%16s-%-5d ", comm
, entry
->pid
);
1447 return TRACE_TYPE_PARTIAL_LINE
;
1448 ret
= trace_seq_printf(s
, "[%03d] ", iter
->cpu
);
1450 return TRACE_TYPE_PARTIAL_LINE
;
1451 ret
= trace_seq_printf(s
, "%5lu.%06lu: ", secs
, usec_rem
);
1453 return TRACE_TYPE_PARTIAL_LINE
;
1455 switch (entry
->type
) {
1457 struct ftrace_entry
*field
= (struct ftrace_entry
*)entry
;
1459 ret
= seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1461 return TRACE_TYPE_PARTIAL_LINE
;
1462 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
1464 ret
= trace_seq_printf(s
, " <-");
1466 return TRACE_TYPE_PARTIAL_LINE
;
1467 if (kretprobed(field
->parent_ip
))
1468 ret
= trace_seq_puts(s
, KRETPROBE_MSG
);
1470 ret
= seq_print_ip_sym(s
,
1474 return TRACE_TYPE_PARTIAL_LINE
;
1476 ret
= trace_seq_printf(s
, "\n");
1478 return TRACE_TYPE_PARTIAL_LINE
;
1483 struct ctx_switch_entry
*field
=
1484 (struct ctx_switch_entry
*)entry
;
1486 S
= field
->prev_state
< sizeof(state_to_char
) ?
1487 state_to_char
[field
->prev_state
] : 'X';
1488 T
= field
->next_state
< sizeof(state_to_char
) ?
1489 state_to_char
[field
->next_state
] : 'X';
1490 ret
= trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1494 entry
->type
== TRACE_CTX
? "==>" : " +",
1500 return TRACE_TYPE_PARTIAL_LINE
;
1503 case TRACE_SPECIAL
: {
1504 struct special_entry
*field
= (struct special_entry
*)entry
;
1506 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1511 return TRACE_TYPE_PARTIAL_LINE
;
1515 struct stack_entry
*field
= (struct stack_entry
*)entry
;
1517 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1519 ret
= trace_seq_puts(s
, " <= ");
1521 return TRACE_TYPE_PARTIAL_LINE
;
1523 ret
= seq_print_ip_sym(s
, field
->caller
[i
],
1526 return TRACE_TYPE_PARTIAL_LINE
;
1528 ret
= trace_seq_puts(s
, "\n");
1530 return TRACE_TYPE_PARTIAL_LINE
;
1534 struct print_entry
*field
= (struct print_entry
*)entry
;
1536 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1537 trace_seq_printf(s
, ": %s", field
->buf
);
1538 if (entry
->flags
& TRACE_FLAG_CONT
)
1539 trace_seq_print_cont(s
, iter
);
1543 return TRACE_TYPE_HANDLED
;
1546 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1548 struct trace_seq
*s
= &iter
->seq
;
1549 struct trace_entry
*entry
;
1555 if (entry
->type
== TRACE_CONT
)
1556 return TRACE_TYPE_HANDLED
;
1558 ret
= trace_seq_printf(s
, "%d %d %llu ",
1559 entry
->pid
, iter
->cpu
, iter
->ts
);
1561 return TRACE_TYPE_PARTIAL_LINE
;
1563 switch (entry
->type
) {
1565 struct ftrace_entry
*field
= (struct ftrace_entry
*)entry
;
1567 ret
= trace_seq_printf(s
, "%x %x\n",
1571 return TRACE_TYPE_PARTIAL_LINE
;
1576 struct ctx_switch_entry
*field
=
1577 (struct ctx_switch_entry
*)entry
;
1579 S
= field
->prev_state
< sizeof(state_to_char
) ?
1580 state_to_char
[field
->prev_state
] : 'X';
1581 T
= field
->next_state
< sizeof(state_to_char
) ?
1582 state_to_char
[field
->next_state
] : 'X';
1583 if (entry
->type
== TRACE_WAKE
)
1585 ret
= trace_seq_printf(s
, "%d %d %c %d %d %d %c\n",
1594 return TRACE_TYPE_PARTIAL_LINE
;
1599 struct special_entry
*field
= (struct special_entry
*)entry
;
1601 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1606 return TRACE_TYPE_PARTIAL_LINE
;
1610 struct print_entry
*field
= (struct print_entry
*)entry
;
1612 trace_seq_printf(s
, "# %lx %s", field
->ip
, field
->buf
);
1613 if (entry
->flags
& TRACE_FLAG_CONT
)
1614 trace_seq_print_cont(s
, iter
);
1618 return TRACE_TYPE_HANDLED
;
1621 #define SEQ_PUT_FIELD_RET(s, x) \
1623 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1627 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1629 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1633 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1635 struct trace_seq
*s
= &iter
->seq
;
1636 unsigned char newline
= '\n';
1637 struct trace_entry
*entry
;
1642 if (entry
->type
== TRACE_CONT
)
1643 return TRACE_TYPE_HANDLED
;
1645 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1646 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1647 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1649 switch (entry
->type
) {
1651 struct ftrace_entry
*field
= (struct ftrace_entry
*)entry
;
1653 SEQ_PUT_HEX_FIELD_RET(s
, field
->ip
);
1654 SEQ_PUT_HEX_FIELD_RET(s
, field
->parent_ip
);
1659 struct ctx_switch_entry
*field
=
1660 (struct ctx_switch_entry
*)entry
;
1662 S
= field
->prev_state
< sizeof(state_to_char
) ?
1663 state_to_char
[field
->prev_state
] : 'X';
1664 T
= field
->next_state
< sizeof(state_to_char
) ?
1665 state_to_char
[field
->next_state
] : 'X';
1666 if (entry
->type
== TRACE_WAKE
)
1668 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_pid
);
1669 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_prio
);
1670 SEQ_PUT_HEX_FIELD_RET(s
, S
);
1671 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_cpu
);
1672 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_pid
);
1673 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_prio
);
1674 SEQ_PUT_HEX_FIELD_RET(s
, T
);
1679 struct special_entry
*field
= (struct special_entry
*)entry
;
1681 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg1
);
1682 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg2
);
1683 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg3
);
1687 SEQ_PUT_FIELD_RET(s
, newline
);
1689 return TRACE_TYPE_HANDLED
;
1692 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1694 struct trace_seq
*s
= &iter
->seq
;
1695 struct trace_entry
*entry
;
1699 if (entry
->type
== TRACE_CONT
)
1700 return TRACE_TYPE_HANDLED
;
1702 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1703 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1704 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1706 switch (entry
->type
) {
1708 struct ftrace_entry
*field
= (struct ftrace_entry
*)entry
;
1710 SEQ_PUT_FIELD_RET(s
, field
->ip
);
1711 SEQ_PUT_FIELD_RET(s
, field
->parent_ip
);
1715 struct ctx_switch_entry
*field
=
1716 (struct ctx_switch_entry
*)entry
;
1718 SEQ_PUT_FIELD_RET(s
, field
->prev_pid
);
1719 SEQ_PUT_FIELD_RET(s
, field
->prev_prio
);
1720 SEQ_PUT_FIELD_RET(s
, field
->prev_state
);
1721 SEQ_PUT_FIELD_RET(s
, field
->next_pid
);
1722 SEQ_PUT_FIELD_RET(s
, field
->next_prio
);
1723 SEQ_PUT_FIELD_RET(s
, field
->next_state
);
1728 struct special_entry
*field
= (struct special_entry
*)entry
;
1730 SEQ_PUT_FIELD_RET(s
, field
->arg1
);
1731 SEQ_PUT_FIELD_RET(s
, field
->arg2
);
1732 SEQ_PUT_FIELD_RET(s
, field
->arg3
);
1739 static int trace_empty(struct trace_iterator
*iter
)
1743 for_each_tracing_cpu(cpu
) {
1744 if (iter
->buffer_iter
[cpu
]) {
1745 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1748 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1753 return TRACE_TYPE_HANDLED
;
1756 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1758 enum print_line_t ret
;
1760 if (iter
->trace
&& iter
->trace
->print_line
) {
1761 ret
= iter
->trace
->print_line(iter
);
1762 if (ret
!= TRACE_TYPE_UNHANDLED
)
1766 if (trace_flags
& TRACE_ITER_BIN
)
1767 return print_bin_fmt(iter
);
1769 if (trace_flags
& TRACE_ITER_HEX
)
1770 return print_hex_fmt(iter
);
1772 if (trace_flags
& TRACE_ITER_RAW
)
1773 return print_raw_fmt(iter
);
1775 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
1776 return print_lat_fmt(iter
, iter
->idx
, iter
->cpu
);
1778 return print_trace_fmt(iter
);
1781 static int s_show(struct seq_file
*m
, void *v
)
1783 struct trace_iterator
*iter
= v
;
1785 if (iter
->ent
== NULL
) {
1787 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1790 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1791 /* print nothing if the buffers are empty */
1792 if (trace_empty(iter
))
1794 print_trace_header(m
, iter
);
1795 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1796 print_lat_help_header(m
);
1798 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1799 print_func_help_header(m
);
1802 print_trace_line(iter
);
1803 trace_print_seq(m
, &iter
->seq
);
1809 static struct seq_operations tracer_seq_ops
= {
1816 static struct trace_iterator
*
1817 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
1819 struct trace_iterator
*iter
;
1823 if (tracing_disabled
) {
1828 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1834 mutex_lock(&trace_types_lock
);
1835 if (current_trace
&& current_trace
->print_max
)
1838 iter
->tr
= inode
->i_private
;
1839 iter
->trace
= current_trace
;
1842 for_each_tracing_cpu(cpu
) {
1844 iter
->buffer_iter
[cpu
] =
1845 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1847 if (!iter
->buffer_iter
[cpu
])
1851 /* TODO stop tracer */
1852 *ret
= seq_open(file
, &tracer_seq_ops
);
1856 m
= file
->private_data
;
1859 /* stop the trace while dumping */
1860 if (iter
->tr
->ctrl
) {
1862 ftrace_function_enabled
= 0;
1865 if (iter
->trace
&& iter
->trace
->open
)
1866 iter
->trace
->open(iter
);
1868 mutex_unlock(&trace_types_lock
);
1874 for_each_tracing_cpu(cpu
) {
1875 if (iter
->buffer_iter
[cpu
])
1876 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1878 mutex_unlock(&trace_types_lock
);
1880 return ERR_PTR(-ENOMEM
);
1883 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1885 if (tracing_disabled
)
1888 filp
->private_data
= inode
->i_private
;
1892 int tracing_release(struct inode
*inode
, struct file
*file
)
1894 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1895 struct trace_iterator
*iter
= m
->private;
1898 mutex_lock(&trace_types_lock
);
1899 for_each_tracing_cpu(cpu
) {
1900 if (iter
->buffer_iter
[cpu
])
1901 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1904 if (iter
->trace
&& iter
->trace
->close
)
1905 iter
->trace
->close(iter
);
1907 /* reenable tracing if it was previously enabled */
1908 if (iter
->tr
->ctrl
) {
1911 * It is safe to enable function tracing even if it
1914 ftrace_function_enabled
= 1;
1916 mutex_unlock(&trace_types_lock
);
1918 seq_release(inode
, file
);
1923 static int tracing_open(struct inode
*inode
, struct file
*file
)
1927 __tracing_open(inode
, file
, &ret
);
1932 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
1934 struct trace_iterator
*iter
;
1937 iter
= __tracing_open(inode
, file
, &ret
);
1940 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1947 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1949 struct tracer
*t
= m
->private;
1961 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1963 struct tracer
*t
= m
->private;
1966 mutex_lock(&trace_types_lock
);
1967 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
1973 static void t_stop(struct seq_file
*m
, void *p
)
1975 mutex_unlock(&trace_types_lock
);
1978 static int t_show(struct seq_file
*m
, void *v
)
1980 struct tracer
*t
= v
;
1985 seq_printf(m
, "%s", t
->name
);
1994 static struct seq_operations show_traces_seq_ops
= {
2001 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2005 if (tracing_disabled
)
2008 ret
= seq_open(file
, &show_traces_seq_ops
);
2010 struct seq_file
*m
= file
->private_data
;
2011 m
->private = trace_types
;
2017 static struct file_operations tracing_fops
= {
2018 .open
= tracing_open
,
2020 .llseek
= seq_lseek
,
2021 .release
= tracing_release
,
2024 static struct file_operations tracing_lt_fops
= {
2025 .open
= tracing_lt_open
,
2027 .llseek
= seq_lseek
,
2028 .release
= tracing_release
,
2031 static struct file_operations show_traces_fops
= {
2032 .open
= show_traces_open
,
2034 .release
= seq_release
,
2038 * Only trace on a CPU if the bitmask is set:
2040 static cpumask_t tracing_cpumask
= CPU_MASK_ALL
;
2043 * When tracing/tracing_cpu_mask is modified then this holds
2044 * the new bitmask we are about to install:
2046 static cpumask_t tracing_cpumask_new
;
2049 * The tracer itself will not take this lock, but still we want
2050 * to provide a consistent cpumask to user-space:
2052 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2055 * Temporary storage for the character representation of the
2056 * CPU bitmask (and one more byte for the newline):
2058 static char mask_str
[NR_CPUS
+ 1];
2061 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2062 size_t count
, loff_t
*ppos
)
2066 mutex_lock(&tracing_cpumask_update_lock
);
2068 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2069 if (count
- len
< 2) {
2073 len
+= sprintf(mask_str
+ len
, "\n");
2074 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2077 mutex_unlock(&tracing_cpumask_update_lock
);
2083 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2084 size_t count
, loff_t
*ppos
)
2088 mutex_lock(&tracing_cpumask_update_lock
);
2089 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2093 raw_local_irq_disable();
2094 __raw_spin_lock(&ftrace_max_lock
);
2095 for_each_tracing_cpu(cpu
) {
2097 * Increase/decrease the disabled counter if we are
2098 * about to flip a bit in the cpumask:
2100 if (cpu_isset(cpu
, tracing_cpumask
) &&
2101 !cpu_isset(cpu
, tracing_cpumask_new
)) {
2102 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2104 if (!cpu_isset(cpu
, tracing_cpumask
) &&
2105 cpu_isset(cpu
, tracing_cpumask_new
)) {
2106 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2109 __raw_spin_unlock(&ftrace_max_lock
);
2110 raw_local_irq_enable();
2112 tracing_cpumask
= tracing_cpumask_new
;
2114 mutex_unlock(&tracing_cpumask_update_lock
);
2119 mutex_unlock(&tracing_cpumask_update_lock
);
2124 static struct file_operations tracing_cpumask_fops
= {
2125 .open
= tracing_open_generic
,
2126 .read
= tracing_cpumask_read
,
2127 .write
= tracing_cpumask_write
,
2131 tracing_iter_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2132 size_t cnt
, loff_t
*ppos
)
2139 /* calulate max size */
2140 for (i
= 0; trace_options
[i
]; i
++) {
2141 len
+= strlen(trace_options
[i
]);
2142 len
+= 3; /* "no" and space */
2145 /* +2 for \n and \0 */
2146 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2150 for (i
= 0; trace_options
[i
]; i
++) {
2151 if (trace_flags
& (1 << i
))
2152 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
2154 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
2157 r
+= sprintf(buf
+ r
, "\n");
2158 WARN_ON(r
>= len
+ 2);
2160 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2168 tracing_iter_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2169 size_t cnt
, loff_t
*ppos
)
2176 if (cnt
>= sizeof(buf
))
2179 if (copy_from_user(&buf
, ubuf
, cnt
))
2184 if (strncmp(buf
, "no", 2) == 0) {
2189 for (i
= 0; trace_options
[i
]; i
++) {
2190 int len
= strlen(trace_options
[i
]);
2192 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2194 trace_flags
&= ~(1 << i
);
2196 trace_flags
|= (1 << i
);
2201 * If no option could be set, return an error:
2203 if (!trace_options
[i
])
2211 static struct file_operations tracing_iter_fops
= {
2212 .open
= tracing_open_generic
,
2213 .read
= tracing_iter_ctrl_read
,
2214 .write
= tracing_iter_ctrl_write
,
2217 static const char readme_msg
[] =
2218 "tracing mini-HOWTO:\n\n"
2220 "# mount -t debugfs nodev /debug\n\n"
2221 "# cat /debug/tracing/available_tracers\n"
2222 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2223 "# cat /debug/tracing/current_tracer\n"
2225 "# echo sched_switch > /debug/tracing/current_tracer\n"
2226 "# cat /debug/tracing/current_tracer\n"
2228 "# cat /debug/tracing/iter_ctrl\n"
2229 "noprint-parent nosym-offset nosym-addr noverbose\n"
2230 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2231 "# echo 1 > /debug/tracing/tracing_enabled\n"
2232 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2233 "echo 0 > /debug/tracing/tracing_enabled\n"
2237 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2238 size_t cnt
, loff_t
*ppos
)
2240 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2241 readme_msg
, strlen(readme_msg
));
2244 static struct file_operations tracing_readme_fops
= {
2245 .open
= tracing_open_generic
,
2246 .read
= tracing_readme_read
,
2250 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2251 size_t cnt
, loff_t
*ppos
)
2253 struct trace_array
*tr
= filp
->private_data
;
2257 r
= sprintf(buf
, "%ld\n", tr
->ctrl
);
2258 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2262 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2263 size_t cnt
, loff_t
*ppos
)
2265 struct trace_array
*tr
= filp
->private_data
;
2270 if (cnt
>= sizeof(buf
))
2273 if (copy_from_user(&buf
, ubuf
, cnt
))
2278 ret
= strict_strtoul(buf
, 10, &val
);
2284 mutex_lock(&trace_types_lock
);
2285 if (tr
->ctrl
^ val
) {
2293 if (current_trace
&& current_trace
->ctrl_update
)
2294 current_trace
->ctrl_update(tr
);
2296 mutex_unlock(&trace_types_lock
);
2304 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2305 size_t cnt
, loff_t
*ppos
)
2307 char buf
[max_tracer_type_len
+2];
2310 mutex_lock(&trace_types_lock
);
2312 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2314 r
= sprintf(buf
, "\n");
2315 mutex_unlock(&trace_types_lock
);
2317 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2321 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2322 size_t cnt
, loff_t
*ppos
)
2324 struct trace_array
*tr
= &global_trace
;
2326 char buf
[max_tracer_type_len
+1];
2329 if (cnt
> max_tracer_type_len
)
2330 cnt
= max_tracer_type_len
;
2332 if (copy_from_user(&buf
, ubuf
, cnt
))
2337 /* strip ending whitespace. */
2338 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2341 mutex_lock(&trace_types_lock
);
2342 for (t
= trace_types
; t
; t
= t
->next
) {
2343 if (strcmp(t
->name
, buf
) == 0)
2346 if (!t
|| t
== current_trace
)
2349 if (current_trace
&& current_trace
->reset
)
2350 current_trace
->reset(tr
);
2357 mutex_unlock(&trace_types_lock
);
2365 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2366 size_t cnt
, loff_t
*ppos
)
2368 unsigned long *ptr
= filp
->private_data
;
2372 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2373 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2374 if (r
> sizeof(buf
))
2376 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2380 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2381 size_t cnt
, loff_t
*ppos
)
2383 long *ptr
= filp
->private_data
;
2388 if (cnt
>= sizeof(buf
))
2391 if (copy_from_user(&buf
, ubuf
, cnt
))
2396 ret
= strict_strtoul(buf
, 10, &val
);
2405 static atomic_t tracing_reader
;
2407 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2409 struct trace_iterator
*iter
;
2411 if (tracing_disabled
)
2414 /* We only allow for reader of the pipe */
2415 if (atomic_inc_return(&tracing_reader
) != 1) {
2416 atomic_dec(&tracing_reader
);
2420 /* create a buffer to store the information to pass to userspace */
2421 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2425 mutex_lock(&trace_types_lock
);
2426 iter
->tr
= &global_trace
;
2427 iter
->trace
= current_trace
;
2428 filp
->private_data
= iter
;
2430 if (iter
->trace
->pipe_open
)
2431 iter
->trace
->pipe_open(iter
);
2432 mutex_unlock(&trace_types_lock
);
2437 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2439 struct trace_iterator
*iter
= file
->private_data
;
2442 atomic_dec(&tracing_reader
);
2448 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2450 struct trace_iterator
*iter
= filp
->private_data
;
2452 if (trace_flags
& TRACE_ITER_BLOCK
) {
2454 * Always select as readable when in blocking mode
2456 return POLLIN
| POLLRDNORM
;
2458 if (!trace_empty(iter
))
2459 return POLLIN
| POLLRDNORM
;
2460 poll_wait(filp
, &trace_wait
, poll_table
);
2461 if (!trace_empty(iter
))
2462 return POLLIN
| POLLRDNORM
;
2472 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2473 size_t cnt
, loff_t
*ppos
)
2475 struct trace_iterator
*iter
= filp
->private_data
;
2476 #ifdef CONFIG_FTRACE
2481 /* return any leftover data */
2482 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2486 trace_seq_reset(&iter
->seq
);
2488 mutex_lock(&trace_types_lock
);
2489 if (iter
->trace
->read
) {
2490 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2497 while (trace_empty(iter
)) {
2499 if ((filp
->f_flags
& O_NONBLOCK
)) {
2505 * This is a make-shift waitqueue. The reason we don't use
2506 * an actual wait queue is because:
2507 * 1) we only ever have one waiter
2508 * 2) the tracing, traces all functions, we don't want
2509 * the overhead of calling wake_up and friends
2510 * (and tracing them too)
2511 * Anyway, this is really very primitive wakeup.
2513 set_current_state(TASK_INTERRUPTIBLE
);
2514 iter
->tr
->waiter
= current
;
2516 mutex_unlock(&trace_types_lock
);
2518 /* sleep for 100 msecs, and try again. */
2519 schedule_timeout(HZ
/10);
2521 mutex_lock(&trace_types_lock
);
2523 iter
->tr
->waiter
= NULL
;
2525 if (signal_pending(current
)) {
2530 if (iter
->trace
!= current_trace
)
2534 * We block until we read something and tracing is disabled.
2535 * We still block if tracing is disabled, but we have never
2536 * read anything. This allows a user to cat this file, and
2537 * then enable tracing. But after we have read something,
2538 * we give an EOF when tracing is again disabled.
2540 * iter->pos will be 0 if we haven't read anything.
2542 if (!tracer_enabled
&& iter
->pos
)
2548 /* stop when tracing is finished */
2549 if (trace_empty(iter
))
2552 if (cnt
>= PAGE_SIZE
)
2553 cnt
= PAGE_SIZE
- 1;
2555 /* reset all but tr, trace, and overruns */
2556 memset(&iter
->seq
, 0,
2557 sizeof(struct trace_iterator
) -
2558 offsetof(struct trace_iterator
, seq
));
2562 * We need to stop all tracing on all CPUS to read the
2563 * the next buffer. This is a bit expensive, but is
2564 * not done often. We fill all what we can read,
2565 * and then release the locks again.
2568 local_irq_disable();
2569 #ifdef CONFIG_FTRACE
2570 ftrace_save
= ftrace_enabled
;
2575 while (find_next_entry_inc(iter
) != NULL
) {
2576 enum print_line_t ret
;
2577 int len
= iter
->seq
.len
;
2579 ret
= print_trace_line(iter
);
2580 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2581 /* don't print partial lines */
2582 iter
->seq
.len
= len
;
2586 trace_consume(iter
);
2588 if (iter
->seq
.len
>= cnt
)
2592 #ifdef CONFIG_FTRACE
2593 ftrace_enabled
= ftrace_save
;
2597 /* Now copy what we have to the user */
2598 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2599 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2600 trace_seq_reset(&iter
->seq
);
2603 * If there was nothing to send to user, inspite of consuming trace
2604 * entries, go back to wait for more entries.
2610 mutex_unlock(&trace_types_lock
);
2616 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
2617 size_t cnt
, loff_t
*ppos
)
2619 struct trace_array
*tr
= filp
->private_data
;
2623 r
= sprintf(buf
, "%lu\n", tr
->entries
);
2624 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2628 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
2629 size_t cnt
, loff_t
*ppos
)
2634 struct trace_array
*tr
= filp
->private_data
;
2636 if (cnt
>= sizeof(buf
))
2639 if (copy_from_user(&buf
, ubuf
, cnt
))
2644 ret
= strict_strtoul(buf
, 10, &val
);
2648 /* must have at least 1 entry */
2652 mutex_lock(&trace_types_lock
);
2656 pr_info("ftrace: please disable tracing"
2657 " before modifying buffer size\n");
2661 if (val
!= global_trace
.entries
) {
2662 ret
= ring_buffer_resize(global_trace
.buffer
, val
);
2668 ret
= ring_buffer_resize(max_tr
.buffer
, val
);
2672 r
= ring_buffer_resize(global_trace
.buffer
,
2673 global_trace
.entries
);
2675 /* AARGH! We are left with different
2676 * size max buffer!!!! */
2678 tracing_disabled
= 1;
2683 global_trace
.entries
= val
;
2688 /* If check pages failed, return ENOMEM */
2689 if (tracing_disabled
)
2692 max_tr
.entries
= global_trace
.entries
;
2693 mutex_unlock(&trace_types_lock
);
2698 static int mark_printk(const char *fmt
, ...)
2702 va_start(args
, fmt
);
2703 ret
= trace_vprintk(0, fmt
, args
);
2709 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
2710 size_t cnt
, loff_t
*fpos
)
2714 struct trace_array
*tr
= &global_trace
;
2716 if (!tr
->ctrl
|| tracing_disabled
)
2719 if (cnt
> TRACE_BUF_SIZE
)
2720 cnt
= TRACE_BUF_SIZE
;
2722 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
2726 if (copy_from_user(buf
, ubuf
, cnt
)) {
2731 /* Cut from the first nil or newline. */
2733 end
= strchr(buf
, '\n');
2737 cnt
= mark_printk("%s\n", buf
);
2744 static struct file_operations tracing_max_lat_fops
= {
2745 .open
= tracing_open_generic
,
2746 .read
= tracing_max_lat_read
,
2747 .write
= tracing_max_lat_write
,
2750 static struct file_operations tracing_ctrl_fops
= {
2751 .open
= tracing_open_generic
,
2752 .read
= tracing_ctrl_read
,
2753 .write
= tracing_ctrl_write
,
2756 static struct file_operations set_tracer_fops
= {
2757 .open
= tracing_open_generic
,
2758 .read
= tracing_set_trace_read
,
2759 .write
= tracing_set_trace_write
,
2762 static struct file_operations tracing_pipe_fops
= {
2763 .open
= tracing_open_pipe
,
2764 .poll
= tracing_poll_pipe
,
2765 .read
= tracing_read_pipe
,
2766 .release
= tracing_release_pipe
,
2769 static struct file_operations tracing_entries_fops
= {
2770 .open
= tracing_open_generic
,
2771 .read
= tracing_entries_read
,
2772 .write
= tracing_entries_write
,
2775 static struct file_operations tracing_mark_fops
= {
2776 .open
= tracing_open_generic
,
2777 .write
= tracing_mark_write
,
2780 #ifdef CONFIG_DYNAMIC_FTRACE
2783 tracing_read_long(struct file
*filp
, char __user
*ubuf
,
2784 size_t cnt
, loff_t
*ppos
)
2786 unsigned long *p
= filp
->private_data
;
2790 r
= sprintf(buf
, "%ld\n", *p
);
2792 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2795 static struct file_operations tracing_read_long_fops
= {
2796 .open
= tracing_open_generic
,
2797 .read
= tracing_read_long
,
2801 static struct dentry
*d_tracer
;
2803 struct dentry
*tracing_init_dentry(void)
2810 d_tracer
= debugfs_create_dir("tracing", NULL
);
2812 if (!d_tracer
&& !once
) {
2814 pr_warning("Could not create debugfs directory 'tracing'\n");
2821 #ifdef CONFIG_FTRACE_SELFTEST
2822 /* Let selftest have access to static functions in this file */
2823 #include "trace_selftest.c"
2826 static __init
int tracer_init_debugfs(void)
2828 struct dentry
*d_tracer
;
2829 struct dentry
*entry
;
2831 d_tracer
= tracing_init_dentry();
2833 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
2834 &global_trace
, &tracing_ctrl_fops
);
2836 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2838 entry
= debugfs_create_file("iter_ctrl", 0644, d_tracer
,
2839 NULL
, &tracing_iter_fops
);
2841 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2843 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
2844 NULL
, &tracing_cpumask_fops
);
2846 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2848 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
2849 &global_trace
, &tracing_lt_fops
);
2851 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2853 entry
= debugfs_create_file("trace", 0444, d_tracer
,
2854 &global_trace
, &tracing_fops
);
2856 pr_warning("Could not create debugfs 'trace' entry\n");
2858 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
2859 &global_trace
, &show_traces_fops
);
2861 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2863 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
2864 &global_trace
, &set_tracer_fops
);
2866 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2868 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
2869 &tracing_max_latency
,
2870 &tracing_max_lat_fops
);
2872 pr_warning("Could not create debugfs "
2873 "'tracing_max_latency' entry\n");
2875 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
2876 &tracing_thresh
, &tracing_max_lat_fops
);
2878 pr_warning("Could not create debugfs "
2879 "'tracing_thresh' entry\n");
2880 entry
= debugfs_create_file("README", 0644, d_tracer
,
2881 NULL
, &tracing_readme_fops
);
2883 pr_warning("Could not create debugfs 'README' entry\n");
2885 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
2886 NULL
, &tracing_pipe_fops
);
2888 pr_warning("Could not create debugfs "
2889 "'trace_pipe' entry\n");
2891 entry
= debugfs_create_file("trace_entries", 0644, d_tracer
,
2892 &global_trace
, &tracing_entries_fops
);
2894 pr_warning("Could not create debugfs "
2895 "'trace_entries' entry\n");
2897 entry
= debugfs_create_file("trace_marker", 0220, d_tracer
,
2898 NULL
, &tracing_mark_fops
);
2900 pr_warning("Could not create debugfs "
2901 "'trace_marker' entry\n");
2903 #ifdef CONFIG_DYNAMIC_FTRACE
2904 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
2905 &ftrace_update_tot_cnt
,
2906 &tracing_read_long_fops
);
2908 pr_warning("Could not create debugfs "
2909 "'dyn_ftrace_total_info' entry\n");
2911 #ifdef CONFIG_SYSPROF_TRACER
2912 init_tracer_sysprof_debugfs(d_tracer
);
2917 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
2919 static DEFINE_SPINLOCK(trace_buf_lock
);
2920 static char trace_buf
[TRACE_BUF_SIZE
];
2922 struct ring_buffer_event
*event
;
2923 struct trace_array
*tr
= &global_trace
;
2924 struct trace_array_cpu
*data
;
2925 struct print_entry
*entry
;
2926 unsigned long flags
, irq_flags
;
2928 int cpu
, len
= 0, size
;
2930 if (!tr
->ctrl
|| tracing_disabled
)
2933 local_irq_save(flags
);
2934 cpu
= raw_smp_processor_id();
2935 data
= tr
->data
[cpu
];
2936 disabled
= atomic_inc_return(&data
->disabled
);
2938 if (unlikely(disabled
!= 1))
2941 spin_lock(&trace_buf_lock
);
2942 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
2944 len
= min(len
, TRACE_BUF_SIZE
-1);
2947 size
= sizeof(*entry
) + len
+ 1;
2948 event
= ring_buffer_lock_reserve(tr
->buffer
, size
, &irq_flags
);
2951 entry
= ring_buffer_event_data(event
);
2952 tracing_generic_entry_update(&entry
->ent
, flags
);
2953 entry
->ent
.type
= TRACE_PRINT
;
2956 memcpy(&entry
->buf
, trace_buf
, len
);
2957 entry
->buf
[len
] = 0;
2958 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
2961 spin_unlock(&trace_buf_lock
);
2964 atomic_dec(&data
->disabled
);
2965 local_irq_restore(flags
);
2969 EXPORT_SYMBOL_GPL(trace_vprintk
);
2971 int __ftrace_printk(unsigned long ip
, const char *fmt
, ...)
2976 if (!(trace_flags
& TRACE_ITER_PRINTK
))
2980 ret
= trace_vprintk(ip
, fmt
, ap
);
2984 EXPORT_SYMBOL_GPL(__ftrace_printk
);
2986 static int trace_panic_handler(struct notifier_block
*this,
2987 unsigned long event
, void *unused
)
2993 static struct notifier_block trace_panic_notifier
= {
2994 .notifier_call
= trace_panic_handler
,
2996 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
2999 static int trace_die_handler(struct notifier_block
*self
,
3013 static struct notifier_block trace_die_notifier
= {
3014 .notifier_call
= trace_die_handler
,
3019 * printk is set to max of 1024, we really don't need it that big.
3020 * Nothing should be printing 1000 characters anyway.
3022 #define TRACE_MAX_PRINT 1000
3025 * Define here KERN_TRACE so that we have one place to modify
3026 * it if we decide to change what log level the ftrace dump
3029 #define KERN_TRACE KERN_INFO
3032 trace_printk_seq(struct trace_seq
*s
)
3034 /* Probably should print a warning here. */
3038 /* should be zero ended, but we are paranoid. */
3039 s
->buffer
[s
->len
] = 0;
3041 printk(KERN_TRACE
"%s", s
->buffer
);
3047 void ftrace_dump(void)
3049 static DEFINE_SPINLOCK(ftrace_dump_lock
);
3050 /* use static because iter can be a bit big for the stack */
3051 static struct trace_iterator iter
;
3052 static cpumask_t mask
;
3053 static int dump_ran
;
3054 unsigned long flags
;
3058 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
3064 /* No turning back! */
3065 ftrace_kill_atomic();
3067 for_each_tracing_cpu(cpu
) {
3068 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3071 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
3073 iter
.tr
= &global_trace
;
3074 iter
.trace
= current_trace
;
3077 * We need to stop all tracing on all CPUS to read the
3078 * the next buffer. This is a bit expensive, but is
3079 * not done often. We fill all what we can read,
3080 * and then release the locks again.
3085 while (!trace_empty(&iter
)) {
3088 printk(KERN_TRACE
"---------------------------------\n");
3092 /* reset all but tr, trace, and overruns */
3093 memset(&iter
.seq
, 0,
3094 sizeof(struct trace_iterator
) -
3095 offsetof(struct trace_iterator
, seq
));
3096 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
3099 if (find_next_entry_inc(&iter
) != NULL
) {
3100 print_trace_line(&iter
);
3101 trace_consume(&iter
);
3104 trace_printk_seq(&iter
.seq
);
3108 printk(KERN_TRACE
" (ftrace buffer empty)\n");
3110 printk(KERN_TRACE
"---------------------------------\n");
3113 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
3116 __init
static int tracer_alloc_buffers(void)
3118 struct trace_array_cpu
*data
;
3121 /* TODO: make the number of buffers hot pluggable with CPUS */
3122 tracing_buffer_mask
= cpu_possible_map
;
3124 global_trace
.buffer
= ring_buffer_alloc(trace_buf_size
,
3125 TRACE_BUFFER_FLAGS
);
3126 if (!global_trace
.buffer
) {
3127 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
3131 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
3133 #ifdef CONFIG_TRACER_MAX_TRACE
3134 max_tr
.buffer
= ring_buffer_alloc(trace_buf_size
,
3135 TRACE_BUFFER_FLAGS
);
3136 if (!max_tr
.buffer
) {
3137 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
3139 ring_buffer_free(global_trace
.buffer
);
3142 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
3143 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
3146 /* Allocate the first page for all buffers */
3147 for_each_tracing_cpu(i
) {
3148 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
3149 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
3152 trace_init_cmdlines();
3154 register_tracer(&nop_trace
);
3155 #ifdef CONFIG_BOOT_TRACER
3156 register_tracer(&boot_tracer
);
3157 current_trace
= &boot_tracer
;
3158 current_trace
->init(&global_trace
);
3160 current_trace
= &nop_trace
;
3163 /* All seems OK, enable tracing */
3164 global_trace
.ctrl
= tracer_enabled
;
3165 tracing_disabled
= 0;
3167 atomic_notifier_chain_register(&panic_notifier_list
,
3168 &trace_panic_notifier
);
3170 register_die_notifier(&trace_die_notifier
);
3174 early_initcall(tracer_alloc_buffers
);
3175 fs_initcall(tracer_init_debugfs
);