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 ns2usecs(cycle_t nsec
)
75 cycle_t
ftrace_now(int cpu
)
77 u64 ts
= ring_buffer_time_stamp(cpu
);
78 ring_buffer_normalize_time_stamp(cpu
, &ts
);
83 * The global_trace is the descriptor that holds the tracing
84 * buffers for the live tracing. For each CPU, it contains
85 * a link list of pages that will store trace entries. The
86 * page descriptor of the pages in the memory is used to hold
87 * the link list by linking the lru item in the page descriptor
88 * to each of the pages in the buffer per CPU.
90 * For each active CPU there is a data field that holds the
91 * pages for the buffer for that CPU. Each CPU has the same number
92 * of pages allocated for its buffer.
94 static struct trace_array global_trace
;
96 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
99 * The max_tr is used to snapshot the global_trace when a maximum
100 * latency is reached. Some tracers will use this to store a maximum
101 * trace while it continues examining live traces.
103 * The buffers for the max_tr are set up the same as the global_trace.
104 * When a snapshot is taken, the link list of the max_tr is swapped
105 * with the link list of the global_trace and the buffers are reset for
106 * the global_trace so the tracing can continue.
108 static struct trace_array max_tr
;
110 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
112 /* tracer_enabled is used to toggle activation of a tracer */
113 static int tracer_enabled
= 1;
115 /* function tracing enabled */
116 int ftrace_function_enabled
;
119 * trace_buf_size is the size in bytes that is allocated
120 * for a buffer. Note, the number of bytes is always rounded
123 * This number is purposely set to a low number of 16384.
124 * If the dump on oops happens, it will be much appreciated
125 * to not have to wait for all that output. Anyway this can be
126 * boot time and run time configurable.
128 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
130 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
132 /* trace_types holds a link list of available tracers. */
133 static struct tracer
*trace_types __read_mostly
;
135 /* current_trace points to the tracer that is currently active */
136 static struct tracer
*current_trace __read_mostly
;
139 * max_tracer_type_len is used to simplify the allocating of
140 * buffers to read userspace tracer names. We keep track of
141 * the longest tracer name registered.
143 static int max_tracer_type_len
;
146 * trace_types_lock is used to protect the trace_types list.
147 * This lock is also used to keep user access serialized.
148 * Accesses from userspace will grab this lock while userspace
149 * activities happen inside the kernel.
151 static DEFINE_MUTEX(trace_types_lock
);
153 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
154 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
156 /* trace_flags holds iter_ctrl options */
157 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
;
160 * trace_wake_up - wake up tasks waiting for trace input
162 * Simply wakes up any task that is blocked on the trace_wait
163 * queue. These is used with trace_poll for tasks polling the trace.
165 void trace_wake_up(void)
168 * The runqueue_is_locked() can fail, but this is the best we
171 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
172 wake_up(&trace_wait
);
175 static int __init
set_buf_size(char *str
)
177 unsigned long buf_size
;
182 ret
= strict_strtoul(str
, 0, &buf_size
);
183 /* nr_entries can not be zero */
184 if (ret
< 0 || buf_size
== 0)
186 trace_buf_size
= buf_size
;
189 __setup("trace_buf_size=", set_buf_size
);
191 unsigned long nsecs_to_usecs(unsigned long nsecs
)
197 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
198 * control the output of kernel symbols.
200 #define TRACE_ITER_SYM_MASK \
201 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
203 /* These must match the bit postions in trace_iterator_flags */
204 static const char *trace_options
[] = {
220 * ftrace_max_lock is used to protect the swapping of buffers
221 * when taking a max snapshot. The buffers themselves are
222 * protected by per_cpu spinlocks. But the action of the swap
223 * needs its own lock.
225 * This is defined as a raw_spinlock_t in order to help
226 * with performance when lockdep debugging is enabled.
228 static raw_spinlock_t ftrace_max_lock
=
229 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
232 * Copy the new maximum trace into the separate maximum-trace
233 * structure. (this way the maximum trace is permanently saved,
234 * for later retrieval via /debugfs/tracing/latency_trace)
237 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
239 struct trace_array_cpu
*data
= tr
->data
[cpu
];
242 max_tr
.time_start
= data
->preempt_timestamp
;
244 data
= max_tr
.data
[cpu
];
245 data
->saved_latency
= tracing_max_latency
;
247 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
248 data
->pid
= tsk
->pid
;
249 data
->uid
= tsk
->uid
;
250 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
251 data
->policy
= tsk
->policy
;
252 data
->rt_priority
= tsk
->rt_priority
;
254 /* record this tasks comm */
255 tracing_record_cmdline(current
);
259 * trace_seq_printf - sequence printing of trace information
260 * @s: trace sequence descriptor
261 * @fmt: printf format string
263 * The tracer may use either sequence operations or its own
264 * copy to user routines. To simplify formating of a trace
265 * trace_seq_printf is used to store strings into a special
266 * buffer (@s). Then the output may be either used by
267 * the sequencer or pulled into another buffer.
270 trace_seq_printf(struct trace_seq
*s
, const char *fmt
, ...)
272 int len
= (PAGE_SIZE
- 1) - s
->len
;
280 ret
= vsnprintf(s
->buffer
+ s
->len
, len
, fmt
, ap
);
283 /* If we can't write it all, don't bother writing anything */
293 * trace_seq_puts - trace sequence printing of simple string
294 * @s: trace sequence descriptor
295 * @str: simple string to record
297 * The tracer may use either the sequence operations or its own
298 * copy to user routines. This function records a simple string
299 * into a special buffer (@s) for later retrieval by a sequencer
300 * or other mechanism.
303 trace_seq_puts(struct trace_seq
*s
, const char *str
)
305 int len
= strlen(str
);
307 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
310 memcpy(s
->buffer
+ s
->len
, str
, len
);
317 trace_seq_putc(struct trace_seq
*s
, unsigned char c
)
319 if (s
->len
>= (PAGE_SIZE
- 1))
322 s
->buffer
[s
->len
++] = c
;
328 trace_seq_putmem(struct trace_seq
*s
, void *mem
, size_t len
)
330 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
333 memcpy(s
->buffer
+ s
->len
, mem
, len
);
339 #define MAX_MEMHEX_BYTES 8
340 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
343 trace_seq_putmem_hex(struct trace_seq
*s
, void *mem
, size_t len
)
345 unsigned char hex
[HEX_CHARS
];
346 unsigned char *data
= mem
;
350 for (i
= 0, j
= 0; i
< len
; i
++) {
352 for (i
= len
-1, j
= 0; i
>= 0; i
--) {
354 hex
[j
++] = hex_asc_hi(data
[i
]);
355 hex
[j
++] = hex_asc_lo(data
[i
]);
359 return trace_seq_putmem(s
, hex
, j
);
363 trace_seq_reset(struct trace_seq
*s
)
369 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
374 if (s
->len
<= s
->readpos
)
377 len
= s
->len
- s
->readpos
;
380 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
389 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
391 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
394 seq_puts(m
, s
->buffer
);
400 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
402 * @tsk: the task with the latency
403 * @cpu: The cpu that initiated the trace.
405 * Flip the buffers between the @tr and the max_tr and record information
406 * about which task was the cause of this latency.
409 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
411 struct ring_buffer
*buf
= tr
->buffer
;
413 WARN_ON_ONCE(!irqs_disabled());
414 __raw_spin_lock(&ftrace_max_lock
);
416 tr
->buffer
= max_tr
.buffer
;
419 ftrace_disable_cpu();
420 ring_buffer_reset(tr
->buffer
);
423 __update_max_tr(tr
, tsk
, cpu
);
424 __raw_spin_unlock(&ftrace_max_lock
);
428 * update_max_tr_single - only copy one trace over, and reset the rest
430 * @tsk - task with the latency
431 * @cpu - the cpu of the buffer to copy.
433 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
436 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
440 WARN_ON_ONCE(!irqs_disabled());
441 __raw_spin_lock(&ftrace_max_lock
);
443 ftrace_disable_cpu();
445 ring_buffer_reset(max_tr
.buffer
);
446 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
452 __update_max_tr(tr
, tsk
, cpu
);
453 __raw_spin_unlock(&ftrace_max_lock
);
457 * register_tracer - register a tracer with the ftrace system.
458 * @type - the plugin for the tracer
460 * Register a new plugin tracer.
462 int register_tracer(struct tracer
*type
)
469 pr_info("Tracer must have a name\n");
473 mutex_lock(&trace_types_lock
);
474 for (t
= trace_types
; t
; t
= t
->next
) {
475 if (strcmp(type
->name
, t
->name
) == 0) {
477 pr_info("Trace %s already registered\n",
484 #ifdef CONFIG_FTRACE_STARTUP_TEST
485 if (type
->selftest
) {
486 struct tracer
*saved_tracer
= current_trace
;
487 struct trace_array
*tr
= &global_trace
;
488 int saved_ctrl
= tr
->ctrl
;
491 * Run a selftest on this tracer.
492 * Here we reset the trace buffer, and set the current
493 * tracer to be this tracer. The tracer can then run some
494 * internal tracing to verify that everything is in order.
495 * If we fail, we do not register this tracer.
497 for_each_tracing_cpu(i
) {
498 tracing_reset(tr
, i
);
500 current_trace
= type
;
502 /* the test is responsible for initializing and enabling */
503 pr_info("Testing tracer %s: ", type
->name
);
504 ret
= type
->selftest(type
, tr
);
505 /* the test is responsible for resetting too */
506 current_trace
= saved_tracer
;
507 tr
->ctrl
= saved_ctrl
;
509 printk(KERN_CONT
"FAILED!\n");
512 /* Only reset on passing, to avoid touching corrupted buffers */
513 for_each_tracing_cpu(i
) {
514 tracing_reset(tr
, i
);
516 printk(KERN_CONT
"PASSED\n");
520 type
->next
= trace_types
;
522 len
= strlen(type
->name
);
523 if (len
> max_tracer_type_len
)
524 max_tracer_type_len
= len
;
527 mutex_unlock(&trace_types_lock
);
532 void unregister_tracer(struct tracer
*type
)
537 mutex_lock(&trace_types_lock
);
538 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
542 pr_info("Trace %s not registered\n", type
->name
);
547 if (strlen(type
->name
) != max_tracer_type_len
)
550 max_tracer_type_len
= 0;
551 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
552 len
= strlen((*t
)->name
);
553 if (len
> max_tracer_type_len
)
554 max_tracer_type_len
= len
;
557 mutex_unlock(&trace_types_lock
);
560 void tracing_reset(struct trace_array
*tr
, int cpu
)
562 ftrace_disable_cpu();
563 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
567 #define SAVED_CMDLINES 128
568 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
569 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
570 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
571 static int cmdline_idx
;
572 static DEFINE_SPINLOCK(trace_cmdline_lock
);
574 /* temporary disable recording */
575 atomic_t trace_record_cmdline_disabled __read_mostly
;
577 static void trace_init_cmdlines(void)
579 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
580 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
584 void trace_stop_cmdline_recording(void);
586 static void trace_save_cmdline(struct task_struct
*tsk
)
591 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
595 * It's not the end of the world if we don't get
596 * the lock, but we also don't want to spin
597 * nor do we want to disable interrupts,
598 * so if we miss here, then better luck next time.
600 if (!spin_trylock(&trace_cmdline_lock
))
603 idx
= map_pid_to_cmdline
[tsk
->pid
];
604 if (idx
>= SAVED_CMDLINES
) {
605 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
607 map
= map_cmdline_to_pid
[idx
];
608 if (map
<= PID_MAX_DEFAULT
)
609 map_pid_to_cmdline
[map
] = (unsigned)-1;
611 map_pid_to_cmdline
[tsk
->pid
] = idx
;
616 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
618 spin_unlock(&trace_cmdline_lock
);
621 static char *trace_find_cmdline(int pid
)
623 char *cmdline
= "<...>";
629 if (pid
> PID_MAX_DEFAULT
)
632 map
= map_pid_to_cmdline
[pid
];
633 if (map
>= SAVED_CMDLINES
)
636 cmdline
= saved_cmdlines
[map
];
642 void tracing_record_cmdline(struct task_struct
*tsk
)
644 if (atomic_read(&trace_record_cmdline_disabled
))
647 trace_save_cmdline(tsk
);
651 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
654 struct task_struct
*tsk
= current
;
656 entry
->preempt_count
= pc
& 0xff;
657 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
659 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
660 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
661 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
662 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
666 trace_function(struct trace_array
*tr
, struct trace_array_cpu
*data
,
667 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
670 struct ring_buffer_event
*event
;
671 struct ftrace_entry
*entry
;
672 unsigned long irq_flags
;
674 /* If we are reading the ring buffer, don't trace */
675 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
678 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
682 entry
= ring_buffer_event_data(event
);
683 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
684 entry
->ent
.type
= TRACE_FN
;
686 entry
->parent_ip
= parent_ip
;
687 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
691 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
692 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
695 if (likely(!atomic_read(&data
->disabled
)))
696 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
699 static void ftrace_trace_stack(struct trace_array
*tr
,
700 struct trace_array_cpu
*data
,
704 struct ring_buffer_event
*event
;
705 struct stack_entry
*entry
;
706 struct stack_trace trace
;
707 unsigned long irq_flags
;
709 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
712 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
716 entry
= ring_buffer_event_data(event
);
717 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
718 entry
->ent
.type
= TRACE_STACK
;
720 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
722 trace
.nr_entries
= 0;
723 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
725 trace
.entries
= entry
->caller
;
727 save_stack_trace(&trace
);
728 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
731 void __trace_stack(struct trace_array
*tr
,
732 struct trace_array_cpu
*data
,
736 ftrace_trace_stack(tr
, data
, flags
, skip
, preempt_count());
740 ftrace_trace_special(void *__tr
, void *__data
,
741 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
744 struct ring_buffer_event
*event
;
745 struct trace_array_cpu
*data
= __data
;
746 struct trace_array
*tr
= __tr
;
747 struct special_entry
*entry
;
748 unsigned long irq_flags
;
750 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
754 entry
= ring_buffer_event_data(event
);
755 tracing_generic_entry_update(&entry
->ent
, 0, pc
);
756 entry
->ent
.type
= TRACE_SPECIAL
;
760 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
761 ftrace_trace_stack(tr
, data
, irq_flags
, 4, pc
);
767 __trace_special(void *__tr
, void *__data
,
768 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
770 ftrace_trace_special(__tr
, __data
, arg1
, arg2
, arg3
, preempt_count());
774 tracing_sched_switch_trace(struct trace_array
*tr
,
775 struct trace_array_cpu
*data
,
776 struct task_struct
*prev
,
777 struct task_struct
*next
,
778 unsigned long flags
, int pc
)
780 struct ring_buffer_event
*event
;
781 struct ctx_switch_entry
*entry
;
782 unsigned long irq_flags
;
784 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
788 entry
= ring_buffer_event_data(event
);
789 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
790 entry
->ent
.type
= TRACE_CTX
;
791 entry
->prev_pid
= prev
->pid
;
792 entry
->prev_prio
= prev
->prio
;
793 entry
->prev_state
= prev
->state
;
794 entry
->next_pid
= next
->pid
;
795 entry
->next_prio
= next
->prio
;
796 entry
->next_state
= next
->state
;
797 entry
->next_cpu
= task_cpu(next
);
798 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
799 ftrace_trace_stack(tr
, data
, flags
, 5, pc
);
803 tracing_sched_wakeup_trace(struct trace_array
*tr
,
804 struct trace_array_cpu
*data
,
805 struct task_struct
*wakee
,
806 struct task_struct
*curr
,
807 unsigned long flags
, int pc
)
809 struct ring_buffer_event
*event
;
810 struct ctx_switch_entry
*entry
;
811 unsigned long irq_flags
;
813 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
817 entry
= ring_buffer_event_data(event
);
818 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
819 entry
->ent
.type
= TRACE_WAKE
;
820 entry
->prev_pid
= curr
->pid
;
821 entry
->prev_prio
= curr
->prio
;
822 entry
->prev_state
= curr
->state
;
823 entry
->next_pid
= wakee
->pid
;
824 entry
->next_prio
= wakee
->prio
;
825 entry
->next_state
= wakee
->state
;
826 entry
->next_cpu
= task_cpu(wakee
);
827 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
828 ftrace_trace_stack(tr
, data
, flags
, 6, pc
);
834 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
836 struct trace_array
*tr
= &global_trace
;
837 struct trace_array_cpu
*data
;
841 if (tracing_disabled
|| !tr
->ctrl
)
844 pc
= preempt_count();
845 preempt_disable_notrace();
846 cpu
= raw_smp_processor_id();
847 data
= tr
->data
[cpu
];
849 if (likely(!atomic_read(&data
->disabled
)))
850 ftrace_trace_special(tr
, data
, arg1
, arg2
, arg3
, pc
);
852 preempt_enable_notrace();
855 #ifdef CONFIG_FUNCTION_TRACER
857 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
859 struct trace_array
*tr
= &global_trace
;
860 struct trace_array_cpu
*data
;
866 if (unlikely(!ftrace_function_enabled
))
869 pc
= preempt_count();
870 resched
= need_resched();
871 preempt_disable_notrace();
872 local_save_flags(flags
);
873 cpu
= raw_smp_processor_id();
874 data
= tr
->data
[cpu
];
875 disabled
= atomic_inc_return(&data
->disabled
);
877 if (likely(disabled
== 1))
878 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
880 atomic_dec(&data
->disabled
);
882 preempt_enable_no_resched_notrace();
884 preempt_enable_notrace();
887 static struct ftrace_ops trace_ops __read_mostly
=
889 .func
= function_trace_call
,
892 void tracing_start_function_trace(void)
894 ftrace_function_enabled
= 0;
895 register_ftrace_function(&trace_ops
);
897 ftrace_function_enabled
= 1;
900 void tracing_stop_function_trace(void)
902 ftrace_function_enabled
= 0;
903 unregister_ftrace_function(&trace_ops
);
907 enum trace_file_type
{
908 TRACE_FILE_LAT_FMT
= 1,
911 static void trace_iterator_increment(struct trace_iterator
*iter
, int cpu
)
913 /* Don't allow ftrace to trace into the ring buffers */
914 ftrace_disable_cpu();
917 if (iter
->buffer_iter
[iter
->cpu
])
918 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
923 static struct trace_entry
*
924 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
926 struct ring_buffer_event
*event
;
927 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
929 /* Don't allow ftrace to trace into the ring buffers */
930 ftrace_disable_cpu();
933 event
= ring_buffer_iter_peek(buf_iter
, ts
);
935 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
939 return event
? ring_buffer_event_data(event
) : NULL
;
942 static struct trace_entry
*
943 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
945 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
946 struct trace_entry
*ent
, *next
= NULL
;
951 for_each_tracing_cpu(cpu
) {
953 if (ring_buffer_empty_cpu(buffer
, cpu
))
956 ent
= peek_next_entry(iter
, cpu
, &ts
);
959 * Pick the entry with the smallest timestamp:
961 if (ent
&& (!next
|| ts
< next_ts
)) {
977 /* Find the next real entry, without updating the iterator itself */
978 static struct trace_entry
*
979 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
981 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
984 /* Find the next real entry, and increment the iterator to the next entry */
985 static void *find_next_entry_inc(struct trace_iterator
*iter
)
987 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
990 trace_iterator_increment(iter
, iter
->cpu
);
992 return iter
->ent
? iter
: NULL
;
995 static void trace_consume(struct trace_iterator
*iter
)
997 /* Don't allow ftrace to trace into the ring buffers */
998 ftrace_disable_cpu();
999 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1000 ftrace_enable_cpu();
1003 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1005 struct trace_iterator
*iter
= m
->private;
1011 /* can't go backwards */
1016 ent
= find_next_entry_inc(iter
);
1020 while (ent
&& iter
->idx
< i
)
1021 ent
= find_next_entry_inc(iter
);
1028 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1030 struct trace_iterator
*iter
= m
->private;
1035 mutex_lock(&trace_types_lock
);
1037 if (!current_trace
|| current_trace
!= iter
->trace
) {
1038 mutex_unlock(&trace_types_lock
);
1042 atomic_inc(&trace_record_cmdline_disabled
);
1044 /* let the tracer grab locks here if needed */
1045 if (current_trace
->start
)
1046 current_trace
->start(iter
);
1048 if (*pos
!= iter
->pos
) {
1053 ftrace_disable_cpu();
1055 for_each_tracing_cpu(cpu
) {
1056 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1059 ftrace_enable_cpu();
1061 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1066 p
= s_next(m
, p
, &l
);
1072 static void s_stop(struct seq_file
*m
, void *p
)
1074 struct trace_iterator
*iter
= m
->private;
1076 atomic_dec(&trace_record_cmdline_disabled
);
1078 /* let the tracer release locks here if needed */
1079 if (current_trace
&& current_trace
== iter
->trace
&& iter
->trace
->stop
)
1080 iter
->trace
->stop(iter
);
1082 mutex_unlock(&trace_types_lock
);
1085 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1087 #ifdef CONFIG_KRETPROBES
1088 static inline int kretprobed(unsigned long addr
)
1090 return addr
== (unsigned long)kretprobe_trampoline
;
1093 static inline int kretprobed(unsigned long addr
)
1097 #endif /* CONFIG_KRETPROBES */
1100 seq_print_sym_short(struct trace_seq
*s
, const char *fmt
, unsigned long address
)
1102 #ifdef CONFIG_KALLSYMS
1103 char str
[KSYM_SYMBOL_LEN
];
1105 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
1107 return trace_seq_printf(s
, fmt
, str
);
1113 seq_print_sym_offset(struct trace_seq
*s
, const char *fmt
,
1114 unsigned long address
)
1116 #ifdef CONFIG_KALLSYMS
1117 char str
[KSYM_SYMBOL_LEN
];
1119 sprint_symbol(str
, address
);
1120 return trace_seq_printf(s
, fmt
, str
);
1125 #ifndef CONFIG_64BIT
1126 # define IP_FMT "%08lx"
1128 # define IP_FMT "%016lx"
1132 seq_print_ip_sym(struct trace_seq
*s
, unsigned long ip
, unsigned long sym_flags
)
1137 return trace_seq_printf(s
, "0");
1139 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
1140 ret
= seq_print_sym_offset(s
, "%s", ip
);
1142 ret
= seq_print_sym_short(s
, "%s", ip
);
1147 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
1148 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
1152 static void print_lat_help_header(struct seq_file
*m
)
1154 seq_puts(m
, "# _------=> CPU# \n");
1155 seq_puts(m
, "# / _-----=> irqs-off \n");
1156 seq_puts(m
, "# | / _----=> need-resched \n");
1157 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1158 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1159 seq_puts(m
, "# |||| / \n");
1160 seq_puts(m
, "# ||||| delay \n");
1161 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1162 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1165 static void print_func_help_header(struct seq_file
*m
)
1167 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1168 seq_puts(m
, "# | | | | |\n");
1173 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1175 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1176 struct trace_array
*tr
= iter
->tr
;
1177 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1178 struct tracer
*type
= current_trace
;
1179 unsigned long total
;
1180 unsigned long entries
;
1181 const char *name
= "preemption";
1186 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1188 ring_buffer_overruns(iter
->tr
->buffer
);
1190 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1192 seq_puts(m
, "-----------------------------------"
1193 "---------------------------------\n");
1194 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1195 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1196 nsecs_to_usecs(data
->saved_latency
),
1200 #if defined(CONFIG_PREEMPT_NONE)
1202 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1204 #elif defined(CONFIG_PREEMPT)
1209 /* These are reserved for later use */
1212 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1216 seq_puts(m
, " -----------------\n");
1217 seq_printf(m
, " | task: %.16s-%d "
1218 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1219 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1220 data
->policy
, data
->rt_priority
);
1221 seq_puts(m
, " -----------------\n");
1223 if (data
->critical_start
) {
1224 seq_puts(m
, " => started at: ");
1225 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1226 trace_print_seq(m
, &iter
->seq
);
1227 seq_puts(m
, "\n => ended at: ");
1228 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1229 trace_print_seq(m
, &iter
->seq
);
1237 lat_print_generic(struct trace_seq
*s
, struct trace_entry
*entry
, int cpu
)
1239 int hardirq
, softirq
;
1242 comm
= trace_find_cmdline(entry
->pid
);
1244 trace_seq_printf(s
, "%8.8s-%-5d ", comm
, entry
->pid
);
1245 trace_seq_printf(s
, "%3d", cpu
);
1246 trace_seq_printf(s
, "%c%c",
1247 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' : '.',
1248 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
1250 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
1251 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
1252 if (hardirq
&& softirq
) {
1253 trace_seq_putc(s
, 'H');
1256 trace_seq_putc(s
, 'h');
1259 trace_seq_putc(s
, 's');
1261 trace_seq_putc(s
, '.');
1265 if (entry
->preempt_count
)
1266 trace_seq_printf(s
, "%x", entry
->preempt_count
);
1268 trace_seq_puts(s
, ".");
1271 unsigned long preempt_mark_thresh
= 100;
1274 lat_print_timestamp(struct trace_seq
*s
, u64 abs_usecs
,
1275 unsigned long rel_usecs
)
1277 trace_seq_printf(s
, " %4lldus", abs_usecs
);
1278 if (rel_usecs
> preempt_mark_thresh
)
1279 trace_seq_puts(s
, "!: ");
1280 else if (rel_usecs
> 1)
1281 trace_seq_puts(s
, "+: ");
1283 trace_seq_puts(s
, " : ");
1286 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
1289 * The message is supposed to contain an ending newline.
1290 * If the printing stops prematurely, try to add a newline of our own.
1292 void trace_seq_print_cont(struct trace_seq
*s
, struct trace_iterator
*iter
)
1294 struct trace_entry
*ent
;
1295 struct trace_field_cont
*cont
;
1298 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1299 if (!ent
|| ent
->type
!= TRACE_CONT
) {
1300 trace_seq_putc(s
, '\n');
1305 cont
= (struct trace_field_cont
*)ent
;
1307 ok
= (trace_seq_printf(s
, "%s", cont
->buf
) > 0);
1309 ftrace_disable_cpu();
1311 if (iter
->buffer_iter
[iter
->cpu
])
1312 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1314 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, NULL
);
1316 ftrace_enable_cpu();
1318 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1319 } while (ent
&& ent
->type
== TRACE_CONT
);
1322 trace_seq_putc(s
, '\n');
1325 static enum print_line_t
1326 print_lat_fmt(struct trace_iterator
*iter
, unsigned int trace_idx
, int cpu
)
1328 struct trace_seq
*s
= &iter
->seq
;
1329 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1330 struct trace_entry
*next_entry
;
1331 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
1332 struct trace_entry
*entry
= iter
->ent
;
1333 unsigned long abs_usecs
;
1334 unsigned long rel_usecs
;
1341 if (entry
->type
== TRACE_CONT
)
1342 return TRACE_TYPE_HANDLED
;
1344 next_entry
= find_next_entry(iter
, NULL
, &next_ts
);
1347 rel_usecs
= ns2usecs(next_ts
- iter
->ts
);
1348 abs_usecs
= ns2usecs(iter
->ts
- iter
->tr
->time_start
);
1351 comm
= trace_find_cmdline(entry
->pid
);
1352 trace_seq_printf(s
, "%16s %5d %3d %d %08x %08x [%08lx]"
1353 " %ld.%03ldms (+%ld.%03ldms): ",
1355 entry
->pid
, cpu
, entry
->flags
,
1356 entry
->preempt_count
, trace_idx
,
1359 abs_usecs
% 1000, rel_usecs
/1000,
1362 lat_print_generic(s
, entry
, cpu
);
1363 lat_print_timestamp(s
, abs_usecs
, rel_usecs
);
1365 switch (entry
->type
) {
1367 struct ftrace_entry
*field
;
1369 trace_assign_type(field
, entry
);
1371 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1372 trace_seq_puts(s
, " (");
1373 if (kretprobed(field
->parent_ip
))
1374 trace_seq_puts(s
, KRETPROBE_MSG
);
1376 seq_print_ip_sym(s
, field
->parent_ip
, sym_flags
);
1377 trace_seq_puts(s
, ")\n");
1382 struct ctx_switch_entry
*field
;
1384 trace_assign_type(field
, entry
);
1386 T
= field
->next_state
< sizeof(state_to_char
) ?
1387 state_to_char
[field
->next_state
] : 'X';
1389 state
= field
->prev_state
?
1390 __ffs(field
->prev_state
) + 1 : 0;
1391 S
= state
< sizeof(state_to_char
) - 1 ? state_to_char
[state
] : 'X';
1392 comm
= trace_find_cmdline(field
->next_pid
);
1393 trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1396 S
, entry
->type
== TRACE_CTX
? "==>" : " +",
1403 case TRACE_SPECIAL
: {
1404 struct special_entry
*field
;
1406 trace_assign_type(field
, entry
);
1408 trace_seq_printf(s
, "# %ld %ld %ld\n",
1415 struct stack_entry
*field
;
1417 trace_assign_type(field
, entry
);
1419 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1421 trace_seq_puts(s
, " <= ");
1422 seq_print_ip_sym(s
, field
->caller
[i
], sym_flags
);
1424 trace_seq_puts(s
, "\n");
1428 struct print_entry
*field
;
1430 trace_assign_type(field
, entry
);
1432 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1433 trace_seq_printf(s
, ": %s", field
->buf
);
1434 if (entry
->flags
& TRACE_FLAG_CONT
)
1435 trace_seq_print_cont(s
, iter
);
1439 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
1441 return TRACE_TYPE_HANDLED
;
1444 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1446 struct trace_seq
*s
= &iter
->seq
;
1447 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1448 struct trace_entry
*entry
;
1449 unsigned long usec_rem
;
1450 unsigned long long t
;
1459 if (entry
->type
== TRACE_CONT
)
1460 return TRACE_TYPE_HANDLED
;
1462 comm
= trace_find_cmdline(iter
->ent
->pid
);
1464 t
= ns2usecs(iter
->ts
);
1465 usec_rem
= do_div(t
, 1000000ULL);
1466 secs
= (unsigned long)t
;
1468 ret
= trace_seq_printf(s
, "%16s-%-5d ", comm
, entry
->pid
);
1470 return TRACE_TYPE_PARTIAL_LINE
;
1471 ret
= trace_seq_printf(s
, "[%03d] ", iter
->cpu
);
1473 return TRACE_TYPE_PARTIAL_LINE
;
1474 ret
= trace_seq_printf(s
, "%5lu.%06lu: ", secs
, usec_rem
);
1476 return TRACE_TYPE_PARTIAL_LINE
;
1478 switch (entry
->type
) {
1480 struct ftrace_entry
*field
;
1482 trace_assign_type(field
, entry
);
1484 ret
= seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1486 return TRACE_TYPE_PARTIAL_LINE
;
1487 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
1489 ret
= trace_seq_printf(s
, " <-");
1491 return TRACE_TYPE_PARTIAL_LINE
;
1492 if (kretprobed(field
->parent_ip
))
1493 ret
= trace_seq_puts(s
, KRETPROBE_MSG
);
1495 ret
= seq_print_ip_sym(s
,
1499 return TRACE_TYPE_PARTIAL_LINE
;
1501 ret
= trace_seq_printf(s
, "\n");
1503 return TRACE_TYPE_PARTIAL_LINE
;
1508 struct ctx_switch_entry
*field
;
1510 trace_assign_type(field
, entry
);
1512 S
= field
->prev_state
< sizeof(state_to_char
) ?
1513 state_to_char
[field
->prev_state
] : 'X';
1514 T
= field
->next_state
< sizeof(state_to_char
) ?
1515 state_to_char
[field
->next_state
] : 'X';
1516 ret
= trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1520 entry
->type
== TRACE_CTX
? "==>" : " +",
1526 return TRACE_TYPE_PARTIAL_LINE
;
1529 case TRACE_SPECIAL
: {
1530 struct special_entry
*field
;
1532 trace_assign_type(field
, entry
);
1534 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1539 return TRACE_TYPE_PARTIAL_LINE
;
1543 struct stack_entry
*field
;
1545 trace_assign_type(field
, entry
);
1547 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1549 ret
= trace_seq_puts(s
, " <= ");
1551 return TRACE_TYPE_PARTIAL_LINE
;
1553 ret
= seq_print_ip_sym(s
, field
->caller
[i
],
1556 return TRACE_TYPE_PARTIAL_LINE
;
1558 ret
= trace_seq_puts(s
, "\n");
1560 return TRACE_TYPE_PARTIAL_LINE
;
1564 struct print_entry
*field
;
1566 trace_assign_type(field
, entry
);
1568 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1569 trace_seq_printf(s
, ": %s", field
->buf
);
1570 if (entry
->flags
& TRACE_FLAG_CONT
)
1571 trace_seq_print_cont(s
, iter
);
1575 return TRACE_TYPE_HANDLED
;
1578 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1580 struct trace_seq
*s
= &iter
->seq
;
1581 struct trace_entry
*entry
;
1587 if (entry
->type
== TRACE_CONT
)
1588 return TRACE_TYPE_HANDLED
;
1590 ret
= trace_seq_printf(s
, "%d %d %llu ",
1591 entry
->pid
, iter
->cpu
, iter
->ts
);
1593 return TRACE_TYPE_PARTIAL_LINE
;
1595 switch (entry
->type
) {
1597 struct ftrace_entry
*field
;
1599 trace_assign_type(field
, entry
);
1601 ret
= trace_seq_printf(s
, "%x %x\n",
1605 return TRACE_TYPE_PARTIAL_LINE
;
1610 struct ctx_switch_entry
*field
;
1612 trace_assign_type(field
, entry
);
1614 S
= field
->prev_state
< sizeof(state_to_char
) ?
1615 state_to_char
[field
->prev_state
] : 'X';
1616 T
= field
->next_state
< sizeof(state_to_char
) ?
1617 state_to_char
[field
->next_state
] : 'X';
1618 if (entry
->type
== TRACE_WAKE
)
1620 ret
= trace_seq_printf(s
, "%d %d %c %d %d %d %c\n",
1629 return TRACE_TYPE_PARTIAL_LINE
;
1634 struct special_entry
*field
;
1636 trace_assign_type(field
, entry
);
1638 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1643 return TRACE_TYPE_PARTIAL_LINE
;
1647 struct print_entry
*field
;
1649 trace_assign_type(field
, entry
);
1651 trace_seq_printf(s
, "# %lx %s", field
->ip
, field
->buf
);
1652 if (entry
->flags
& TRACE_FLAG_CONT
)
1653 trace_seq_print_cont(s
, iter
);
1657 return TRACE_TYPE_HANDLED
;
1660 #define SEQ_PUT_FIELD_RET(s, x) \
1662 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1666 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1668 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1669 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1673 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1675 struct trace_seq
*s
= &iter
->seq
;
1676 unsigned char newline
= '\n';
1677 struct trace_entry
*entry
;
1682 if (entry
->type
== TRACE_CONT
)
1683 return TRACE_TYPE_HANDLED
;
1685 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1686 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1687 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1689 switch (entry
->type
) {
1691 struct ftrace_entry
*field
;
1693 trace_assign_type(field
, entry
);
1695 SEQ_PUT_HEX_FIELD_RET(s
, field
->ip
);
1696 SEQ_PUT_HEX_FIELD_RET(s
, field
->parent_ip
);
1701 struct ctx_switch_entry
*field
;
1703 trace_assign_type(field
, entry
);
1705 S
= field
->prev_state
< sizeof(state_to_char
) ?
1706 state_to_char
[field
->prev_state
] : 'X';
1707 T
= field
->next_state
< sizeof(state_to_char
) ?
1708 state_to_char
[field
->next_state
] : 'X';
1709 if (entry
->type
== TRACE_WAKE
)
1711 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_pid
);
1712 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_prio
);
1713 SEQ_PUT_HEX_FIELD_RET(s
, S
);
1714 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_cpu
);
1715 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_pid
);
1716 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_prio
);
1717 SEQ_PUT_HEX_FIELD_RET(s
, T
);
1722 struct special_entry
*field
;
1724 trace_assign_type(field
, entry
);
1726 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg1
);
1727 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg2
);
1728 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg3
);
1732 SEQ_PUT_FIELD_RET(s
, newline
);
1734 return TRACE_TYPE_HANDLED
;
1737 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1739 struct trace_seq
*s
= &iter
->seq
;
1740 struct trace_entry
*entry
;
1744 if (entry
->type
== TRACE_CONT
)
1745 return TRACE_TYPE_HANDLED
;
1747 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1748 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1749 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1751 switch (entry
->type
) {
1753 struct ftrace_entry
*field
;
1755 trace_assign_type(field
, entry
);
1757 SEQ_PUT_FIELD_RET(s
, field
->ip
);
1758 SEQ_PUT_FIELD_RET(s
, field
->parent_ip
);
1762 struct ctx_switch_entry
*field
;
1764 trace_assign_type(field
, entry
);
1766 SEQ_PUT_FIELD_RET(s
, field
->prev_pid
);
1767 SEQ_PUT_FIELD_RET(s
, field
->prev_prio
);
1768 SEQ_PUT_FIELD_RET(s
, field
->prev_state
);
1769 SEQ_PUT_FIELD_RET(s
, field
->next_pid
);
1770 SEQ_PUT_FIELD_RET(s
, field
->next_prio
);
1771 SEQ_PUT_FIELD_RET(s
, field
->next_state
);
1776 struct special_entry
*field
;
1778 trace_assign_type(field
, entry
);
1780 SEQ_PUT_FIELD_RET(s
, field
->arg1
);
1781 SEQ_PUT_FIELD_RET(s
, field
->arg2
);
1782 SEQ_PUT_FIELD_RET(s
, field
->arg3
);
1789 static int trace_empty(struct trace_iterator
*iter
)
1793 for_each_tracing_cpu(cpu
) {
1794 if (iter
->buffer_iter
[cpu
]) {
1795 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1798 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1806 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1808 enum print_line_t ret
;
1810 if (iter
->trace
&& iter
->trace
->print_line
) {
1811 ret
= iter
->trace
->print_line(iter
);
1812 if (ret
!= TRACE_TYPE_UNHANDLED
)
1816 if (trace_flags
& TRACE_ITER_BIN
)
1817 return print_bin_fmt(iter
);
1819 if (trace_flags
& TRACE_ITER_HEX
)
1820 return print_hex_fmt(iter
);
1822 if (trace_flags
& TRACE_ITER_RAW
)
1823 return print_raw_fmt(iter
);
1825 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
1826 return print_lat_fmt(iter
, iter
->idx
, iter
->cpu
);
1828 return print_trace_fmt(iter
);
1831 static int s_show(struct seq_file
*m
, void *v
)
1833 struct trace_iterator
*iter
= v
;
1835 if (iter
->ent
== NULL
) {
1837 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1840 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1841 /* print nothing if the buffers are empty */
1842 if (trace_empty(iter
))
1844 print_trace_header(m
, iter
);
1845 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1846 print_lat_help_header(m
);
1848 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1849 print_func_help_header(m
);
1852 print_trace_line(iter
);
1853 trace_print_seq(m
, &iter
->seq
);
1859 static struct seq_operations tracer_seq_ops
= {
1866 static struct trace_iterator
*
1867 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
1869 struct trace_iterator
*iter
;
1873 if (tracing_disabled
) {
1878 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1884 mutex_lock(&trace_types_lock
);
1885 if (current_trace
&& current_trace
->print_max
)
1888 iter
->tr
= inode
->i_private
;
1889 iter
->trace
= current_trace
;
1892 for_each_tracing_cpu(cpu
) {
1894 iter
->buffer_iter
[cpu
] =
1895 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1897 if (!iter
->buffer_iter
[cpu
])
1901 /* TODO stop tracer */
1902 *ret
= seq_open(file
, &tracer_seq_ops
);
1906 m
= file
->private_data
;
1909 /* stop the trace while dumping */
1910 if (iter
->tr
->ctrl
) {
1912 ftrace_function_enabled
= 0;
1915 if (iter
->trace
&& iter
->trace
->open
)
1916 iter
->trace
->open(iter
);
1918 mutex_unlock(&trace_types_lock
);
1924 for_each_tracing_cpu(cpu
) {
1925 if (iter
->buffer_iter
[cpu
])
1926 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1928 mutex_unlock(&trace_types_lock
);
1930 return ERR_PTR(-ENOMEM
);
1933 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1935 if (tracing_disabled
)
1938 filp
->private_data
= inode
->i_private
;
1942 int tracing_release(struct inode
*inode
, struct file
*file
)
1944 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1945 struct trace_iterator
*iter
= m
->private;
1948 mutex_lock(&trace_types_lock
);
1949 for_each_tracing_cpu(cpu
) {
1950 if (iter
->buffer_iter
[cpu
])
1951 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1954 if (iter
->trace
&& iter
->trace
->close
)
1955 iter
->trace
->close(iter
);
1957 /* reenable tracing if it was previously enabled */
1958 if (iter
->tr
->ctrl
) {
1961 * It is safe to enable function tracing even if it
1964 ftrace_function_enabled
= 1;
1966 mutex_unlock(&trace_types_lock
);
1968 seq_release(inode
, file
);
1973 static int tracing_open(struct inode
*inode
, struct file
*file
)
1977 __tracing_open(inode
, file
, &ret
);
1982 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
1984 struct trace_iterator
*iter
;
1987 iter
= __tracing_open(inode
, file
, &ret
);
1990 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1997 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1999 struct tracer
*t
= m
->private;
2011 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2013 struct tracer
*t
= m
->private;
2016 mutex_lock(&trace_types_lock
);
2017 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2023 static void t_stop(struct seq_file
*m
, void *p
)
2025 mutex_unlock(&trace_types_lock
);
2028 static int t_show(struct seq_file
*m
, void *v
)
2030 struct tracer
*t
= v
;
2035 seq_printf(m
, "%s", t
->name
);
2044 static struct seq_operations show_traces_seq_ops
= {
2051 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2055 if (tracing_disabled
)
2058 ret
= seq_open(file
, &show_traces_seq_ops
);
2060 struct seq_file
*m
= file
->private_data
;
2061 m
->private = trace_types
;
2067 static struct file_operations tracing_fops
= {
2068 .open
= tracing_open
,
2070 .llseek
= seq_lseek
,
2071 .release
= tracing_release
,
2074 static struct file_operations tracing_lt_fops
= {
2075 .open
= tracing_lt_open
,
2077 .llseek
= seq_lseek
,
2078 .release
= tracing_release
,
2081 static struct file_operations show_traces_fops
= {
2082 .open
= show_traces_open
,
2084 .release
= seq_release
,
2088 * Only trace on a CPU if the bitmask is set:
2090 static cpumask_t tracing_cpumask
= CPU_MASK_ALL
;
2093 * When tracing/tracing_cpu_mask is modified then this holds
2094 * the new bitmask we are about to install:
2096 static cpumask_t tracing_cpumask_new
;
2099 * The tracer itself will not take this lock, but still we want
2100 * to provide a consistent cpumask to user-space:
2102 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2105 * Temporary storage for the character representation of the
2106 * CPU bitmask (and one more byte for the newline):
2108 static char mask_str
[NR_CPUS
+ 1];
2111 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2112 size_t count
, loff_t
*ppos
)
2116 mutex_lock(&tracing_cpumask_update_lock
);
2118 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2119 if (count
- len
< 2) {
2123 len
+= sprintf(mask_str
+ len
, "\n");
2124 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2127 mutex_unlock(&tracing_cpumask_update_lock
);
2133 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2134 size_t count
, loff_t
*ppos
)
2138 mutex_lock(&tracing_cpumask_update_lock
);
2139 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2143 raw_local_irq_disable();
2144 __raw_spin_lock(&ftrace_max_lock
);
2145 for_each_tracing_cpu(cpu
) {
2147 * Increase/decrease the disabled counter if we are
2148 * about to flip a bit in the cpumask:
2150 if (cpu_isset(cpu
, tracing_cpumask
) &&
2151 !cpu_isset(cpu
, tracing_cpumask_new
)) {
2152 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2154 if (!cpu_isset(cpu
, tracing_cpumask
) &&
2155 cpu_isset(cpu
, tracing_cpumask_new
)) {
2156 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2159 __raw_spin_unlock(&ftrace_max_lock
);
2160 raw_local_irq_enable();
2162 tracing_cpumask
= tracing_cpumask_new
;
2164 mutex_unlock(&tracing_cpumask_update_lock
);
2169 mutex_unlock(&tracing_cpumask_update_lock
);
2174 static struct file_operations tracing_cpumask_fops
= {
2175 .open
= tracing_open_generic
,
2176 .read
= tracing_cpumask_read
,
2177 .write
= tracing_cpumask_write
,
2181 tracing_iter_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2182 size_t cnt
, loff_t
*ppos
)
2189 /* calulate max size */
2190 for (i
= 0; trace_options
[i
]; i
++) {
2191 len
+= strlen(trace_options
[i
]);
2192 len
+= 3; /* "no" and space */
2195 /* +2 for \n and \0 */
2196 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2200 for (i
= 0; trace_options
[i
]; i
++) {
2201 if (trace_flags
& (1 << i
))
2202 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
2204 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
2207 r
+= sprintf(buf
+ r
, "\n");
2208 WARN_ON(r
>= len
+ 2);
2210 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2218 tracing_iter_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2219 size_t cnt
, loff_t
*ppos
)
2226 if (cnt
>= sizeof(buf
))
2229 if (copy_from_user(&buf
, ubuf
, cnt
))
2234 if (strncmp(buf
, "no", 2) == 0) {
2239 for (i
= 0; trace_options
[i
]; i
++) {
2240 int len
= strlen(trace_options
[i
]);
2242 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2244 trace_flags
&= ~(1 << i
);
2246 trace_flags
|= (1 << i
);
2251 * If no option could be set, return an error:
2253 if (!trace_options
[i
])
2261 static struct file_operations tracing_iter_fops
= {
2262 .open
= tracing_open_generic
,
2263 .read
= tracing_iter_ctrl_read
,
2264 .write
= tracing_iter_ctrl_write
,
2267 static const char readme_msg
[] =
2268 "tracing mini-HOWTO:\n\n"
2270 "# mount -t debugfs nodev /debug\n\n"
2271 "# cat /debug/tracing/available_tracers\n"
2272 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2273 "# cat /debug/tracing/current_tracer\n"
2275 "# echo sched_switch > /debug/tracing/current_tracer\n"
2276 "# cat /debug/tracing/current_tracer\n"
2278 "# cat /debug/tracing/iter_ctrl\n"
2279 "noprint-parent nosym-offset nosym-addr noverbose\n"
2280 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2281 "# echo 1 > /debug/tracing/tracing_enabled\n"
2282 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2283 "echo 0 > /debug/tracing/tracing_enabled\n"
2287 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2288 size_t cnt
, loff_t
*ppos
)
2290 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2291 readme_msg
, strlen(readme_msg
));
2294 static struct file_operations tracing_readme_fops
= {
2295 .open
= tracing_open_generic
,
2296 .read
= tracing_readme_read
,
2300 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2301 size_t cnt
, loff_t
*ppos
)
2303 struct trace_array
*tr
= filp
->private_data
;
2307 r
= sprintf(buf
, "%ld\n", tr
->ctrl
);
2308 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2312 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2313 size_t cnt
, loff_t
*ppos
)
2315 struct trace_array
*tr
= filp
->private_data
;
2320 if (cnt
>= sizeof(buf
))
2323 if (copy_from_user(&buf
, ubuf
, cnt
))
2328 ret
= strict_strtoul(buf
, 10, &val
);
2334 mutex_lock(&trace_types_lock
);
2335 if (tr
->ctrl
^ val
) {
2343 if (current_trace
&& current_trace
->ctrl_update
)
2344 current_trace
->ctrl_update(tr
);
2346 mutex_unlock(&trace_types_lock
);
2354 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2355 size_t cnt
, loff_t
*ppos
)
2357 char buf
[max_tracer_type_len
+2];
2360 mutex_lock(&trace_types_lock
);
2362 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2364 r
= sprintf(buf
, "\n");
2365 mutex_unlock(&trace_types_lock
);
2367 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2371 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2372 size_t cnt
, loff_t
*ppos
)
2374 struct trace_array
*tr
= &global_trace
;
2376 char buf
[max_tracer_type_len
+1];
2380 if (cnt
> max_tracer_type_len
)
2381 cnt
= max_tracer_type_len
;
2384 if (copy_from_user(&buf
, ubuf
, cnt
))
2389 /* strip ending whitespace. */
2390 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2393 mutex_lock(&trace_types_lock
);
2394 for (t
= trace_types
; t
; t
= t
->next
) {
2395 if (strcmp(t
->name
, buf
) == 0)
2402 if (t
== current_trace
)
2405 if (current_trace
&& current_trace
->reset
)
2406 current_trace
->reset(tr
);
2413 mutex_unlock(&trace_types_lock
);
2422 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2423 size_t cnt
, loff_t
*ppos
)
2425 unsigned long *ptr
= filp
->private_data
;
2429 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2430 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2431 if (r
> sizeof(buf
))
2433 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2437 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2438 size_t cnt
, loff_t
*ppos
)
2440 long *ptr
= filp
->private_data
;
2445 if (cnt
>= sizeof(buf
))
2448 if (copy_from_user(&buf
, ubuf
, cnt
))
2453 ret
= strict_strtoul(buf
, 10, &val
);
2462 static atomic_t tracing_reader
;
2464 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2466 struct trace_iterator
*iter
;
2468 if (tracing_disabled
)
2471 /* We only allow for reader of the pipe */
2472 if (atomic_inc_return(&tracing_reader
) != 1) {
2473 atomic_dec(&tracing_reader
);
2477 /* create a buffer to store the information to pass to userspace */
2478 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2482 mutex_lock(&trace_types_lock
);
2483 iter
->tr
= &global_trace
;
2484 iter
->trace
= current_trace
;
2485 filp
->private_data
= iter
;
2487 if (iter
->trace
->pipe_open
)
2488 iter
->trace
->pipe_open(iter
);
2489 mutex_unlock(&trace_types_lock
);
2494 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2496 struct trace_iterator
*iter
= file
->private_data
;
2499 atomic_dec(&tracing_reader
);
2505 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2507 struct trace_iterator
*iter
= filp
->private_data
;
2509 if (trace_flags
& TRACE_ITER_BLOCK
) {
2511 * Always select as readable when in blocking mode
2513 return POLLIN
| POLLRDNORM
;
2515 if (!trace_empty(iter
))
2516 return POLLIN
| POLLRDNORM
;
2517 poll_wait(filp
, &trace_wait
, poll_table
);
2518 if (!trace_empty(iter
))
2519 return POLLIN
| POLLRDNORM
;
2529 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2530 size_t cnt
, loff_t
*ppos
)
2532 struct trace_iterator
*iter
= filp
->private_data
;
2535 /* return any leftover data */
2536 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2540 trace_seq_reset(&iter
->seq
);
2542 mutex_lock(&trace_types_lock
);
2543 if (iter
->trace
->read
) {
2544 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2551 while (trace_empty(iter
)) {
2553 if ((filp
->f_flags
& O_NONBLOCK
)) {
2559 * This is a make-shift waitqueue. The reason we don't use
2560 * an actual wait queue is because:
2561 * 1) we only ever have one waiter
2562 * 2) the tracing, traces all functions, we don't want
2563 * the overhead of calling wake_up and friends
2564 * (and tracing them too)
2565 * Anyway, this is really very primitive wakeup.
2567 set_current_state(TASK_INTERRUPTIBLE
);
2568 iter
->tr
->waiter
= current
;
2570 mutex_unlock(&trace_types_lock
);
2572 /* sleep for 100 msecs, and try again. */
2573 schedule_timeout(HZ
/10);
2575 mutex_lock(&trace_types_lock
);
2577 iter
->tr
->waiter
= NULL
;
2579 if (signal_pending(current
)) {
2584 if (iter
->trace
!= current_trace
)
2588 * We block until we read something and tracing is disabled.
2589 * We still block if tracing is disabled, but we have never
2590 * read anything. This allows a user to cat this file, and
2591 * then enable tracing. But after we have read something,
2592 * we give an EOF when tracing is again disabled.
2594 * iter->pos will be 0 if we haven't read anything.
2596 if (!tracer_enabled
&& iter
->pos
)
2602 /* stop when tracing is finished */
2603 if (trace_empty(iter
))
2606 if (cnt
>= PAGE_SIZE
)
2607 cnt
= PAGE_SIZE
- 1;
2609 /* reset all but tr, trace, and overruns */
2610 memset(&iter
->seq
, 0,
2611 sizeof(struct trace_iterator
) -
2612 offsetof(struct trace_iterator
, seq
));
2615 while (find_next_entry_inc(iter
) != NULL
) {
2616 enum print_line_t ret
;
2617 int len
= iter
->seq
.len
;
2619 ret
= print_trace_line(iter
);
2620 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2621 /* don't print partial lines */
2622 iter
->seq
.len
= len
;
2626 trace_consume(iter
);
2628 if (iter
->seq
.len
>= cnt
)
2632 /* Now copy what we have to the user */
2633 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2634 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2635 trace_seq_reset(&iter
->seq
);
2638 * If there was nothing to send to user, inspite of consuming trace
2639 * entries, go back to wait for more entries.
2645 mutex_unlock(&trace_types_lock
);
2651 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
2652 size_t cnt
, loff_t
*ppos
)
2654 struct trace_array
*tr
= filp
->private_data
;
2658 r
= sprintf(buf
, "%lu\n", tr
->entries
);
2659 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2663 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
2664 size_t cnt
, loff_t
*ppos
)
2669 struct trace_array
*tr
= filp
->private_data
;
2671 if (cnt
>= sizeof(buf
))
2674 if (copy_from_user(&buf
, ubuf
, cnt
))
2679 ret
= strict_strtoul(buf
, 10, &val
);
2683 /* must have at least 1 entry */
2687 mutex_lock(&trace_types_lock
);
2691 pr_info("ftrace: please disable tracing"
2692 " before modifying buffer size\n");
2696 if (val
!= global_trace
.entries
) {
2697 ret
= ring_buffer_resize(global_trace
.buffer
, val
);
2703 ret
= ring_buffer_resize(max_tr
.buffer
, val
);
2707 r
= ring_buffer_resize(global_trace
.buffer
,
2708 global_trace
.entries
);
2710 /* AARGH! We are left with different
2711 * size max buffer!!!! */
2713 tracing_disabled
= 1;
2718 global_trace
.entries
= val
;
2723 /* If check pages failed, return ENOMEM */
2724 if (tracing_disabled
)
2727 max_tr
.entries
= global_trace
.entries
;
2728 mutex_unlock(&trace_types_lock
);
2733 static int mark_printk(const char *fmt
, ...)
2737 va_start(args
, fmt
);
2738 ret
= trace_vprintk(0, fmt
, args
);
2744 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
2745 size_t cnt
, loff_t
*fpos
)
2749 struct trace_array
*tr
= &global_trace
;
2751 if (!tr
->ctrl
|| tracing_disabled
)
2754 if (cnt
> TRACE_BUF_SIZE
)
2755 cnt
= TRACE_BUF_SIZE
;
2757 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
2761 if (copy_from_user(buf
, ubuf
, cnt
)) {
2766 /* Cut from the first nil or newline. */
2768 end
= strchr(buf
, '\n');
2772 cnt
= mark_printk("%s\n", buf
);
2779 static struct file_operations tracing_max_lat_fops
= {
2780 .open
= tracing_open_generic
,
2781 .read
= tracing_max_lat_read
,
2782 .write
= tracing_max_lat_write
,
2785 static struct file_operations tracing_ctrl_fops
= {
2786 .open
= tracing_open_generic
,
2787 .read
= tracing_ctrl_read
,
2788 .write
= tracing_ctrl_write
,
2791 static struct file_operations set_tracer_fops
= {
2792 .open
= tracing_open_generic
,
2793 .read
= tracing_set_trace_read
,
2794 .write
= tracing_set_trace_write
,
2797 static struct file_operations tracing_pipe_fops
= {
2798 .open
= tracing_open_pipe
,
2799 .poll
= tracing_poll_pipe
,
2800 .read
= tracing_read_pipe
,
2801 .release
= tracing_release_pipe
,
2804 static struct file_operations tracing_entries_fops
= {
2805 .open
= tracing_open_generic
,
2806 .read
= tracing_entries_read
,
2807 .write
= tracing_entries_write
,
2810 static struct file_operations tracing_mark_fops
= {
2811 .open
= tracing_open_generic
,
2812 .write
= tracing_mark_write
,
2815 #ifdef CONFIG_DYNAMIC_FTRACE
2818 tracing_read_long(struct file
*filp
, char __user
*ubuf
,
2819 size_t cnt
, loff_t
*ppos
)
2821 unsigned long *p
= filp
->private_data
;
2825 r
= sprintf(buf
, "%ld\n", *p
);
2827 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2830 static struct file_operations tracing_read_long_fops
= {
2831 .open
= tracing_open_generic
,
2832 .read
= tracing_read_long
,
2836 static struct dentry
*d_tracer
;
2838 struct dentry
*tracing_init_dentry(void)
2845 d_tracer
= debugfs_create_dir("tracing", NULL
);
2847 if (!d_tracer
&& !once
) {
2849 pr_warning("Could not create debugfs directory 'tracing'\n");
2856 #ifdef CONFIG_FTRACE_SELFTEST
2857 /* Let selftest have access to static functions in this file */
2858 #include "trace_selftest.c"
2861 static __init
int tracer_init_debugfs(void)
2863 struct dentry
*d_tracer
;
2864 struct dentry
*entry
;
2866 d_tracer
= tracing_init_dentry();
2868 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
2869 &global_trace
, &tracing_ctrl_fops
);
2871 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2873 entry
= debugfs_create_file("iter_ctrl", 0644, d_tracer
,
2874 NULL
, &tracing_iter_fops
);
2876 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2878 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
2879 NULL
, &tracing_cpumask_fops
);
2881 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2883 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
2884 &global_trace
, &tracing_lt_fops
);
2886 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2888 entry
= debugfs_create_file("trace", 0444, d_tracer
,
2889 &global_trace
, &tracing_fops
);
2891 pr_warning("Could not create debugfs 'trace' entry\n");
2893 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
2894 &global_trace
, &show_traces_fops
);
2896 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2898 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
2899 &global_trace
, &set_tracer_fops
);
2901 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2903 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
2904 &tracing_max_latency
,
2905 &tracing_max_lat_fops
);
2907 pr_warning("Could not create debugfs "
2908 "'tracing_max_latency' entry\n");
2910 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
2911 &tracing_thresh
, &tracing_max_lat_fops
);
2913 pr_warning("Could not create debugfs "
2914 "'tracing_thresh' entry\n");
2915 entry
= debugfs_create_file("README", 0644, d_tracer
,
2916 NULL
, &tracing_readme_fops
);
2918 pr_warning("Could not create debugfs 'README' entry\n");
2920 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
2921 NULL
, &tracing_pipe_fops
);
2923 pr_warning("Could not create debugfs "
2924 "'trace_pipe' entry\n");
2926 entry
= debugfs_create_file("trace_entries", 0644, d_tracer
,
2927 &global_trace
, &tracing_entries_fops
);
2929 pr_warning("Could not create debugfs "
2930 "'trace_entries' entry\n");
2932 entry
= debugfs_create_file("trace_marker", 0220, d_tracer
,
2933 NULL
, &tracing_mark_fops
);
2935 pr_warning("Could not create debugfs "
2936 "'trace_marker' entry\n");
2938 #ifdef CONFIG_DYNAMIC_FTRACE
2939 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
2940 &ftrace_update_tot_cnt
,
2941 &tracing_read_long_fops
);
2943 pr_warning("Could not create debugfs "
2944 "'dyn_ftrace_total_info' entry\n");
2946 #ifdef CONFIG_SYSPROF_TRACER
2947 init_tracer_sysprof_debugfs(d_tracer
);
2952 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
2954 static DEFINE_SPINLOCK(trace_buf_lock
);
2955 static char trace_buf
[TRACE_BUF_SIZE
];
2957 struct ring_buffer_event
*event
;
2958 struct trace_array
*tr
= &global_trace
;
2959 struct trace_array_cpu
*data
;
2960 struct print_entry
*entry
;
2961 unsigned long flags
, irq_flags
;
2962 int cpu
, len
= 0, size
, pc
;
2964 if (!tr
->ctrl
|| tracing_disabled
)
2967 pc
= preempt_count();
2968 preempt_disable_notrace();
2969 cpu
= raw_smp_processor_id();
2970 data
= tr
->data
[cpu
];
2972 if (unlikely(atomic_read(&data
->disabled
)))
2975 spin_lock_irqsave(&trace_buf_lock
, flags
);
2976 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
2978 len
= min(len
, TRACE_BUF_SIZE
-1);
2981 size
= sizeof(*entry
) + len
+ 1;
2982 event
= ring_buffer_lock_reserve(tr
->buffer
, size
, &irq_flags
);
2985 entry
= ring_buffer_event_data(event
);
2986 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
2987 entry
->ent
.type
= TRACE_PRINT
;
2990 memcpy(&entry
->buf
, trace_buf
, len
);
2991 entry
->buf
[len
] = 0;
2992 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
2995 spin_unlock_irqrestore(&trace_buf_lock
, flags
);
2998 preempt_enable_notrace();
3002 EXPORT_SYMBOL_GPL(trace_vprintk
);
3004 int __ftrace_printk(unsigned long ip
, const char *fmt
, ...)
3009 if (!(trace_flags
& TRACE_ITER_PRINTK
))
3013 ret
= trace_vprintk(ip
, fmt
, ap
);
3017 EXPORT_SYMBOL_GPL(__ftrace_printk
);
3019 static int trace_panic_handler(struct notifier_block
*this,
3020 unsigned long event
, void *unused
)
3026 static struct notifier_block trace_panic_notifier
= {
3027 .notifier_call
= trace_panic_handler
,
3029 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
3032 static int trace_die_handler(struct notifier_block
*self
,
3046 static struct notifier_block trace_die_notifier
= {
3047 .notifier_call
= trace_die_handler
,
3052 * printk is set to max of 1024, we really don't need it that big.
3053 * Nothing should be printing 1000 characters anyway.
3055 #define TRACE_MAX_PRINT 1000
3058 * Define here KERN_TRACE so that we have one place to modify
3059 * it if we decide to change what log level the ftrace dump
3062 #define KERN_TRACE KERN_INFO
3065 trace_printk_seq(struct trace_seq
*s
)
3067 /* Probably should print a warning here. */
3071 /* should be zero ended, but we are paranoid. */
3072 s
->buffer
[s
->len
] = 0;
3074 printk(KERN_TRACE
"%s", s
->buffer
);
3080 void ftrace_dump(void)
3082 static DEFINE_SPINLOCK(ftrace_dump_lock
);
3083 /* use static because iter can be a bit big for the stack */
3084 static struct trace_iterator iter
;
3085 static cpumask_t mask
;
3086 static int dump_ran
;
3087 unsigned long flags
;
3091 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
3097 /* No turning back! */
3100 for_each_tracing_cpu(cpu
) {
3101 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3104 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
3106 iter
.tr
= &global_trace
;
3107 iter
.trace
= current_trace
;
3110 * We need to stop all tracing on all CPUS to read the
3111 * the next buffer. This is a bit expensive, but is
3112 * not done often. We fill all what we can read,
3113 * and then release the locks again.
3118 while (!trace_empty(&iter
)) {
3121 printk(KERN_TRACE
"---------------------------------\n");
3125 /* reset all but tr, trace, and overruns */
3126 memset(&iter
.seq
, 0,
3127 sizeof(struct trace_iterator
) -
3128 offsetof(struct trace_iterator
, seq
));
3129 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
3132 if (find_next_entry_inc(&iter
) != NULL
) {
3133 print_trace_line(&iter
);
3134 trace_consume(&iter
);
3137 trace_printk_seq(&iter
.seq
);
3141 printk(KERN_TRACE
" (ftrace buffer empty)\n");
3143 printk(KERN_TRACE
"---------------------------------\n");
3146 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
3149 __init
static int tracer_alloc_buffers(void)
3151 struct trace_array_cpu
*data
;
3154 /* TODO: make the number of buffers hot pluggable with CPUS */
3155 tracing_buffer_mask
= cpu_possible_map
;
3157 global_trace
.buffer
= ring_buffer_alloc(trace_buf_size
,
3158 TRACE_BUFFER_FLAGS
);
3159 if (!global_trace
.buffer
) {
3160 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
3164 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
3166 #ifdef CONFIG_TRACER_MAX_TRACE
3167 max_tr
.buffer
= ring_buffer_alloc(trace_buf_size
,
3168 TRACE_BUFFER_FLAGS
);
3169 if (!max_tr
.buffer
) {
3170 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
3172 ring_buffer_free(global_trace
.buffer
);
3175 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
3176 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
3179 /* Allocate the first page for all buffers */
3180 for_each_tracing_cpu(i
) {
3181 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
3182 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
3185 trace_init_cmdlines();
3187 register_tracer(&nop_trace
);
3188 #ifdef CONFIG_BOOT_TRACER
3189 register_tracer(&boot_tracer
);
3190 current_trace
= &boot_tracer
;
3191 current_trace
->init(&global_trace
);
3193 current_trace
= &nop_trace
;
3196 /* All seems OK, enable tracing */
3197 global_trace
.ctrl
= tracer_enabled
;
3198 tracing_disabled
= 0;
3200 atomic_notifier_chain_register(&panic_notifier_list
,
3201 &trace_panic_notifier
);
3203 register_die_notifier(&trace_die_notifier
);
3207 early_initcall(tracer_alloc_buffers
);
3208 fs_initcall(tracer_init_debugfs
);