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/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/ctype.h>
35 #include <linux/init.h>
36 #include <linux/poll.h>
37 #include <linux/gfp.h>
41 #include "trace_output.h"
43 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
45 unsigned long __read_mostly tracing_max_latency;
46 unsigned long __read_mostly tracing_thresh;
49 * On boot up, the ring buffer is set to the minimum size, so that
50 * we do not waste memory on systems that are not using tracing.
52 static int ring_buffer_expanded;
55 * We need to change this state when a selftest is running.
56 * A selftest will lurk into the ring-buffer to count the
57 * entries inserted during the selftest although some concurrent
58 * insertions into the ring-buffer such as trace_printk could occurred
59 * at the same time, giving false positive or negative results.
61 static bool __read_mostly tracing_selftest_running;
64 * If a tracer is running, we do not want to run SELFTEST.
66 static bool __read_mostly tracing_selftest_disabled;
68 /* For tracers that don't implement custom flags */
69 static struct tracer_opt dummy_tracer_opt[] = {
73 static struct tracer_flags dummy_tracer_flags = {
75 .opts = dummy_tracer_opt
78 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
84 * Kill all tracing for good (never come back).
85 * It is initialized to 1 but will turn to zero if the initialization
86 * of the tracer is successful. But that is the only place that sets
89 static int tracing_disabled = 1;
91 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
93 static inline void ftrace_disable_cpu(void)
96 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
99 static inline void ftrace_enable_cpu(void)
101 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
105 static cpumask_var_t __read_mostly tracing_buffer_mask;
107 /* Define which cpu buffers are currently read in trace_pipe */
108 static cpumask_var_t tracing_reader_cpumask;
110 #define for_each_tracing_cpu(cpu) \
111 for_each_cpu(cpu, tracing_buffer_mask)
114 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
116 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
117 * is set, then ftrace_dump is called. This will output the contents
118 * of the ftrace buffers to the console. This is very useful for
119 * capturing traces that lead to crashes and outputing it to a
122 * It is default off, but you can enable it with either specifying
123 * "ftrace_dump_on_oops" in the kernel command line, or setting
124 * /proc/sys/kernel/ftrace_dump_on_oops to true.
126 int ftrace_dump_on_oops;
128 static int tracing_set_tracer(const char *buf);
130 #define BOOTUP_TRACER_SIZE 100
131 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
132 static char *default_bootup_tracer;
134 static int __init set_ftrace(char *str)
136 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
137 default_bootup_tracer = bootup_tracer_buf;
138 /* We are using ftrace early, expand it */
139 ring_buffer_expanded = 1;
142 __setup("ftrace=", set_ftrace);
144 static int __init set_ftrace_dump_on_oops(char *str)
146 ftrace_dump_on_oops = 1;
149 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
151 unsigned long long ns2usecs(cycle_t nsec)
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
170 static struct trace_array global_trace;
172 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
174 cycle_t ftrace_now(int cpu)
178 /* Early boot up does not have a buffer yet */
179 if (!global_trace.buffer)
180 return trace_clock_local();
182 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
183 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
189 * The max_tr is used to snapshot the global_trace when a maximum
190 * latency is reached. Some tracers will use this to store a maximum
191 * trace while it continues examining live traces.
193 * The buffers for the max_tr are set up the same as the global_trace.
194 * When a snapshot is taken, the link list of the max_tr is swapped
195 * with the link list of the global_trace and the buffers are reset for
196 * the global_trace so the tracing can continue.
198 static struct trace_array max_tr;
200 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
202 /* tracer_enabled is used to toggle activation of a tracer */
203 static int tracer_enabled = 1;
206 * tracing_is_enabled - return tracer_enabled status
208 * This function is used by other tracers to know the status
209 * of the tracer_enabled flag. Tracers may use this function
210 * to know if it should enable their features when starting
211 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
213 int tracing_is_enabled(void)
215 return tracer_enabled;
219 * trace_buf_size is the size in bytes that is allocated
220 * for a buffer. Note, the number of bytes is always rounded
223 * This number is purposely set to a low number of 16384.
224 * If the dump on oops happens, it will be much appreciated
225 * to not have to wait for all that output. Anyway this can be
226 * boot time and run time configurable.
228 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
230 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
232 /* trace_types holds a link list of available tracers. */
233 static struct tracer *trace_types __read_mostly;
235 /* current_trace points to the tracer that is currently active */
236 static struct tracer *current_trace __read_mostly;
239 * max_tracer_type_len is used to simplify the allocating of
240 * buffers to read userspace tracer names. We keep track of
241 * the longest tracer name registered.
243 static int max_tracer_type_len;
246 * trace_types_lock is used to protect the trace_types list.
247 * This lock is also used to keep user access serialized.
248 * Accesses from userspace will grab this lock while userspace
249 * activities happen inside the kernel.
251 static DEFINE_MUTEX(trace_types_lock);
253 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
254 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
256 /* trace_flags holds trace_options default values */
257 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
258 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
259 TRACE_ITER_GRAPH_TIME;
262 * trace_wake_up - wake up tasks waiting for trace input
264 * Simply wakes up any task that is blocked on the trace_wait
265 * queue. These is used with trace_poll for tasks polling the trace.
267 void trace_wake_up(void)
270 * The runqueue_is_locked() can fail, but this is the best we
273 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
274 wake_up(&trace_wait);
277 static int __init set_buf_size(char *str)
279 unsigned long buf_size;
284 ret = strict_strtoul(str, 0, &buf_size);
285 /* nr_entries can not be zero */
286 if (ret < 0 || buf_size == 0)
288 trace_buf_size = buf_size;
291 __setup("trace_buf_size=", set_buf_size);
293 unsigned long nsecs_to_usecs(unsigned long nsecs)
298 /* These must match the bit postions in trace_iterator_flags */
299 static const char *trace_options[] = {
326 * ftrace_max_lock is used to protect the swapping of buffers
327 * when taking a max snapshot. The buffers themselves are
328 * protected by per_cpu spinlocks. But the action of the swap
329 * needs its own lock.
331 * This is defined as a raw_spinlock_t in order to help
332 * with performance when lockdep debugging is enabled.
334 static raw_spinlock_t ftrace_max_lock =
335 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
338 * Copy the new maximum trace into the separate maximum-trace
339 * structure. (this way the maximum trace is permanently saved,
340 * for later retrieval via /debugfs/tracing/latency_trace)
343 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
345 struct trace_array_cpu *data = tr->data[cpu];
348 max_tr.time_start = data->preempt_timestamp;
350 data = max_tr.data[cpu];
351 data->saved_latency = tracing_max_latency;
353 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
354 data->pid = tsk->pid;
355 data->uid = task_uid(tsk);
356 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
357 data->policy = tsk->policy;
358 data->rt_priority = tsk->rt_priority;
360 /* record this tasks comm */
361 tracing_record_cmdline(tsk);
364 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
372 if (s->len <= s->readpos)
375 len = s->len - s->readpos;
378 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
388 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
393 if (s->len <= s->readpos)
396 len = s->len - s->readpos;
399 ret = memcpy(buf, s->buffer + s->readpos, cnt);
408 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
410 * @tsk: the task with the latency
411 * @cpu: The cpu that initiated the trace.
413 * Flip the buffers between the @tr and the max_tr and record information
414 * about which task was the cause of this latency.
417 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
419 struct ring_buffer *buf = tr->buffer;
421 WARN_ON_ONCE(!irqs_disabled());
422 __raw_spin_lock(&ftrace_max_lock);
424 tr->buffer = max_tr.buffer;
427 ftrace_disable_cpu();
428 ring_buffer_reset(tr->buffer);
431 __update_max_tr(tr, tsk, cpu);
432 __raw_spin_unlock(&ftrace_max_lock);
436 * update_max_tr_single - only copy one trace over, and reset the rest
438 * @tsk - task with the latency
439 * @cpu - the cpu of the buffer to copy.
441 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
444 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
448 WARN_ON_ONCE(!irqs_disabled());
449 __raw_spin_lock(&ftrace_max_lock);
451 ftrace_disable_cpu();
453 ring_buffer_reset(max_tr.buffer);
454 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
458 WARN_ON_ONCE(ret && ret != -EAGAIN);
460 __update_max_tr(tr, tsk, cpu);
461 __raw_spin_unlock(&ftrace_max_lock);
465 * register_tracer - register a tracer with the ftrace system.
466 * @type - the plugin for the tracer
468 * Register a new plugin tracer.
470 int register_tracer(struct tracer *type)
471 __releases(kernel_lock)
472 __acquires(kernel_lock)
479 pr_info("Tracer must have a name\n");
484 * When this gets called we hold the BKL which means that
485 * preemption is disabled. Various trace selftests however
486 * need to disable and enable preemption for successful tests.
487 * So we drop the BKL here and grab it after the tests again.
490 mutex_lock(&trace_types_lock);
492 tracing_selftest_running = true;
494 for (t = trace_types; t; t = t->next) {
495 if (strcmp(type->name, t->name) == 0) {
497 pr_info("Trace %s already registered\n",
505 type->set_flag = &dummy_set_flag;
507 type->flags = &dummy_tracer_flags;
509 if (!type->flags->opts)
510 type->flags->opts = dummy_tracer_opt;
511 if (!type->wait_pipe)
512 type->wait_pipe = default_wait_pipe;
515 #ifdef CONFIG_FTRACE_STARTUP_TEST
516 if (type->selftest && !tracing_selftest_disabled) {
517 struct tracer *saved_tracer = current_trace;
518 struct trace_array *tr = &global_trace;
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
528 for_each_tracing_cpu(i)
529 tracing_reset(tr, i);
531 current_trace = type;
532 /* the test is responsible for initializing and enabling */
533 pr_info("Testing tracer %s: ", type->name);
534 ret = type->selftest(type, tr);
535 /* the test is responsible for resetting too */
536 current_trace = saved_tracer;
538 printk(KERN_CONT "FAILED!\n");
541 /* Only reset on passing, to avoid touching corrupted buffers */
542 for_each_tracing_cpu(i)
543 tracing_reset(tr, i);
545 printk(KERN_CONT "PASSED\n");
549 type->next = trace_types;
551 len = strlen(type->name);
552 if (len > max_tracer_type_len)
553 max_tracer_type_len = len;
556 tracing_selftest_running = false;
557 mutex_unlock(&trace_types_lock);
559 if (ret || !default_bootup_tracer)
562 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
565 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
566 /* Do we want this tracer to start on bootup? */
567 tracing_set_tracer(type->name);
568 default_bootup_tracer = NULL;
569 /* disable other selftests, since this will break it. */
570 tracing_selftest_disabled = 1;
571 #ifdef CONFIG_FTRACE_STARTUP_TEST
572 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
581 void unregister_tracer(struct tracer *type)
586 mutex_lock(&trace_types_lock);
587 for (t = &trace_types; *t; t = &(*t)->next) {
591 pr_info("Trace %s not registered\n", type->name);
597 if (type == current_trace && tracer_enabled) {
600 if (current_trace->stop)
601 current_trace->stop(&global_trace);
602 current_trace = &nop_trace;
605 if (strlen(type->name) != max_tracer_type_len)
608 max_tracer_type_len = 0;
609 for (t = &trace_types; *t; t = &(*t)->next) {
610 len = strlen((*t)->name);
611 if (len > max_tracer_type_len)
612 max_tracer_type_len = len;
615 mutex_unlock(&trace_types_lock);
618 void tracing_reset(struct trace_array *tr, int cpu)
620 ftrace_disable_cpu();
621 ring_buffer_reset_cpu(tr->buffer, cpu);
625 void tracing_reset_online_cpus(struct trace_array *tr)
629 tr->time_start = ftrace_now(tr->cpu);
631 for_each_online_cpu(cpu)
632 tracing_reset(tr, cpu);
635 #define SAVED_CMDLINES 128
636 #define NO_CMDLINE_MAP UINT_MAX
637 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
638 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
639 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
640 static int cmdline_idx;
641 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
643 /* temporary disable recording */
644 static atomic_t trace_record_cmdline_disabled __read_mostly;
646 static void trace_init_cmdlines(void)
648 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
649 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
653 static int trace_stop_count;
654 static DEFINE_SPINLOCK(tracing_start_lock);
657 * ftrace_off_permanent - disable all ftrace code permanently
659 * This should only be called when a serious anomally has
660 * been detected. This will turn off the function tracing,
661 * ring buffers, and other tracing utilites. It takes no
662 * locks and can be called from any context.
664 void ftrace_off_permanent(void)
666 tracing_disabled = 1;
668 tracing_off_permanent();
672 * tracing_start - quick start of the tracer
674 * If tracing is enabled but was stopped by tracing_stop,
675 * this will start the tracer back up.
677 void tracing_start(void)
679 struct ring_buffer *buffer;
682 if (tracing_disabled)
685 spin_lock_irqsave(&tracing_start_lock, flags);
686 if (--trace_stop_count) {
687 if (trace_stop_count < 0) {
688 /* Someone screwed up their debugging */
690 trace_stop_count = 0;
696 buffer = global_trace.buffer;
698 ring_buffer_record_enable(buffer);
700 buffer = max_tr.buffer;
702 ring_buffer_record_enable(buffer);
706 spin_unlock_irqrestore(&tracing_start_lock, flags);
710 * tracing_stop - quick stop of the tracer
712 * Light weight way to stop tracing. Use in conjunction with
715 void tracing_stop(void)
717 struct ring_buffer *buffer;
721 spin_lock_irqsave(&tracing_start_lock, flags);
722 if (trace_stop_count++)
725 buffer = global_trace.buffer;
727 ring_buffer_record_disable(buffer);
729 buffer = max_tr.buffer;
731 ring_buffer_record_disable(buffer);
734 spin_unlock_irqrestore(&tracing_start_lock, flags);
737 void trace_stop_cmdline_recording(void);
739 static void trace_save_cmdline(struct task_struct *tsk)
743 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
747 * It's not the end of the world if we don't get
748 * the lock, but we also don't want to spin
749 * nor do we want to disable interrupts,
750 * so if we miss here, then better luck next time.
752 if (!__raw_spin_trylock(&trace_cmdline_lock))
755 idx = map_pid_to_cmdline[tsk->pid];
756 if (idx == NO_CMDLINE_MAP) {
757 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
760 * Check whether the cmdline buffer at idx has a pid
761 * mapped. We are going to overwrite that entry so we
762 * need to clear the map_pid_to_cmdline. Otherwise we
763 * would read the new comm for the old pid.
765 pid = map_cmdline_to_pid[idx];
766 if (pid != NO_CMDLINE_MAP)
767 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
769 map_cmdline_to_pid[idx] = tsk->pid;
770 map_pid_to_cmdline[tsk->pid] = idx;
775 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
777 __raw_spin_unlock(&trace_cmdline_lock);
780 void trace_find_cmdline(int pid, char comm[])
785 strcpy(comm, "<idle>");
789 if (pid > PID_MAX_DEFAULT) {
790 strcpy(comm, "<...>");
794 __raw_spin_lock(&trace_cmdline_lock);
795 map = map_pid_to_cmdline[pid];
796 if (map != NO_CMDLINE_MAP)
797 strcpy(comm, saved_cmdlines[map]);
799 strcpy(comm, "<...>");
801 __raw_spin_unlock(&trace_cmdline_lock);
804 void tracing_record_cmdline(struct task_struct *tsk)
806 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
810 trace_save_cmdline(tsk);
814 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
817 struct task_struct *tsk = current;
819 entry->preempt_count = pc & 0xff;
820 entry->pid = (tsk) ? tsk->pid : 0;
821 entry->tgid = (tsk) ? tsk->tgid : 0;
823 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
824 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
826 TRACE_FLAG_IRQS_NOSUPPORT |
828 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
829 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
830 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
833 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
836 unsigned long flags, int pc)
838 struct ring_buffer_event *event;
840 event = ring_buffer_lock_reserve(tr->buffer, len);
842 struct trace_entry *ent = ring_buffer_event_data(event);
844 tracing_generic_entry_update(ent, flags, pc);
850 static void ftrace_trace_stack(struct trace_array *tr,
851 unsigned long flags, int skip, int pc);
852 static void ftrace_trace_userstack(struct trace_array *tr,
853 unsigned long flags, int pc);
855 static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
856 struct ring_buffer_event *event,
857 unsigned long flags, int pc,
860 ring_buffer_unlock_commit(tr->buffer, event);
862 ftrace_trace_stack(tr, flags, 6, pc);
863 ftrace_trace_userstack(tr, flags, pc);
869 void trace_buffer_unlock_commit(struct trace_array *tr,
870 struct ring_buffer_event *event,
871 unsigned long flags, int pc)
873 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
876 struct ring_buffer_event *
877 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
878 unsigned long flags, int pc)
880 return trace_buffer_lock_reserve(&global_trace,
881 type, len, flags, pc);
884 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
885 unsigned long flags, int pc)
887 return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
890 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
891 unsigned long flags, int pc)
893 return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
897 trace_function(struct trace_array *tr,
898 unsigned long ip, unsigned long parent_ip, unsigned long flags,
901 struct ring_buffer_event *event;
902 struct ftrace_entry *entry;
904 /* If we are reading the ring buffer, don't trace */
905 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
908 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
912 entry = ring_buffer_event_data(event);
914 entry->parent_ip = parent_ip;
915 ring_buffer_unlock_commit(tr->buffer, event);
918 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
919 static int __trace_graph_entry(struct trace_array *tr,
920 struct ftrace_graph_ent *trace,
924 struct ring_buffer_event *event;
925 struct ftrace_graph_ent_entry *entry;
927 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
930 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
931 sizeof(*entry), flags, pc);
934 entry = ring_buffer_event_data(event);
935 entry->graph_ent = *trace;
936 ring_buffer_unlock_commit(global_trace.buffer, event);
941 static void __trace_graph_return(struct trace_array *tr,
942 struct ftrace_graph_ret *trace,
946 struct ring_buffer_event *event;
947 struct ftrace_graph_ret_entry *entry;
949 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
952 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
953 sizeof(*entry), flags, pc);
956 entry = ring_buffer_event_data(event);
958 ring_buffer_unlock_commit(global_trace.buffer, event);
963 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
964 unsigned long ip, unsigned long parent_ip, unsigned long flags,
967 if (likely(!atomic_read(&data->disabled)))
968 trace_function(tr, ip, parent_ip, flags, pc);
971 static void __ftrace_trace_stack(struct trace_array *tr,
975 #ifdef CONFIG_STACKTRACE
976 struct ring_buffer_event *event;
977 struct stack_entry *entry;
978 struct stack_trace trace;
980 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
981 sizeof(*entry), flags, pc);
984 entry = ring_buffer_event_data(event);
985 memset(&entry->caller, 0, sizeof(entry->caller));
987 trace.nr_entries = 0;
988 trace.max_entries = FTRACE_STACK_ENTRIES;
990 trace.entries = entry->caller;
992 save_stack_trace(&trace);
993 ring_buffer_unlock_commit(tr->buffer, event);
997 static void ftrace_trace_stack(struct trace_array *tr,
1001 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1004 __ftrace_trace_stack(tr, flags, skip, pc);
1007 void __trace_stack(struct trace_array *tr,
1008 unsigned long flags,
1011 __ftrace_trace_stack(tr, flags, skip, pc);
1014 static void ftrace_trace_userstack(struct trace_array *tr,
1015 unsigned long flags, int pc)
1017 #ifdef CONFIG_STACKTRACE
1018 struct ring_buffer_event *event;
1019 struct userstack_entry *entry;
1020 struct stack_trace trace;
1022 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1025 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1026 sizeof(*entry), flags, pc);
1029 entry = ring_buffer_event_data(event);
1031 memset(&entry->caller, 0, sizeof(entry->caller));
1033 trace.nr_entries = 0;
1034 trace.max_entries = FTRACE_STACK_ENTRIES;
1036 trace.entries = entry->caller;
1038 save_stack_trace_user(&trace);
1039 ring_buffer_unlock_commit(tr->buffer, event);
1044 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1046 ftrace_trace_userstack(tr, flags, preempt_count());
1051 ftrace_trace_special(void *__tr,
1052 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1055 struct ring_buffer_event *event;
1056 struct trace_array *tr = __tr;
1057 struct special_entry *entry;
1059 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1060 sizeof(*entry), 0, pc);
1063 entry = ring_buffer_event_data(event);
1067 trace_buffer_unlock_commit(tr, event, 0, pc);
1071 __trace_special(void *__tr, void *__data,
1072 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1074 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1078 tracing_sched_switch_trace(struct trace_array *tr,
1079 struct task_struct *prev,
1080 struct task_struct *next,
1081 unsigned long flags, int pc)
1083 struct ring_buffer_event *event;
1084 struct ctx_switch_entry *entry;
1086 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1087 sizeof(*entry), flags, pc);
1090 entry = ring_buffer_event_data(event);
1091 entry->prev_pid = prev->pid;
1092 entry->prev_prio = prev->prio;
1093 entry->prev_state = prev->state;
1094 entry->next_pid = next->pid;
1095 entry->next_prio = next->prio;
1096 entry->next_state = next->state;
1097 entry->next_cpu = task_cpu(next);
1098 trace_buffer_unlock_commit(tr, event, flags, pc);
1102 tracing_sched_wakeup_trace(struct trace_array *tr,
1103 struct task_struct *wakee,
1104 struct task_struct *curr,
1105 unsigned long flags, int pc)
1107 struct ring_buffer_event *event;
1108 struct ctx_switch_entry *entry;
1110 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1111 sizeof(*entry), flags, pc);
1114 entry = ring_buffer_event_data(event);
1115 entry->prev_pid = curr->pid;
1116 entry->prev_prio = curr->prio;
1117 entry->prev_state = curr->state;
1118 entry->next_pid = wakee->pid;
1119 entry->next_prio = wakee->prio;
1120 entry->next_state = wakee->state;
1121 entry->next_cpu = task_cpu(wakee);
1123 ring_buffer_unlock_commit(tr->buffer, event);
1124 ftrace_trace_stack(tr, flags, 6, pc);
1125 ftrace_trace_userstack(tr, flags, pc);
1129 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1131 struct trace_array *tr = &global_trace;
1132 struct trace_array_cpu *data;
1133 unsigned long flags;
1137 if (tracing_disabled)
1140 pc = preempt_count();
1141 local_irq_save(flags);
1142 cpu = raw_smp_processor_id();
1143 data = tr->data[cpu];
1145 if (likely(atomic_inc_return(&data->disabled) == 1))
1146 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1148 atomic_dec(&data->disabled);
1149 local_irq_restore(flags);
1152 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1153 int trace_graph_entry(struct ftrace_graph_ent *trace)
1155 struct trace_array *tr = &global_trace;
1156 struct trace_array_cpu *data;
1157 unsigned long flags;
1163 if (!ftrace_trace_task(current))
1166 if (!ftrace_graph_addr(trace->func))
1169 local_irq_save(flags);
1170 cpu = raw_smp_processor_id();
1171 data = tr->data[cpu];
1172 disabled = atomic_inc_return(&data->disabled);
1173 if (likely(disabled == 1)) {
1174 pc = preempt_count();
1175 ret = __trace_graph_entry(tr, trace, flags, pc);
1179 /* Only do the atomic if it is not already set */
1180 if (!test_tsk_trace_graph(current))
1181 set_tsk_trace_graph(current);
1183 atomic_dec(&data->disabled);
1184 local_irq_restore(flags);
1189 void trace_graph_return(struct ftrace_graph_ret *trace)
1191 struct trace_array *tr = &global_trace;
1192 struct trace_array_cpu *data;
1193 unsigned long flags;
1198 local_irq_save(flags);
1199 cpu = raw_smp_processor_id();
1200 data = tr->data[cpu];
1201 disabled = atomic_inc_return(&data->disabled);
1202 if (likely(disabled == 1)) {
1203 pc = preempt_count();
1204 __trace_graph_return(tr, trace, flags, pc);
1207 clear_tsk_trace_graph(current);
1208 atomic_dec(&data->disabled);
1209 local_irq_restore(flags);
1211 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1215 * trace_vbprintk - write binary msg to tracing buffer
1218 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1220 static raw_spinlock_t trace_buf_lock =
1221 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1222 static u32 trace_buf[TRACE_BUF_SIZE];
1224 struct ring_buffer_event *event;
1225 struct trace_array *tr = &global_trace;
1226 struct trace_array_cpu *data;
1227 struct bprint_entry *entry;
1228 unsigned long flags;
1230 int cpu, len = 0, size, pc;
1232 if (unlikely(tracing_selftest_running || tracing_disabled))
1235 /* Don't pollute graph traces with trace_vprintk internals */
1236 pause_graph_tracing();
1238 pc = preempt_count();
1239 resched = ftrace_preempt_disable();
1240 cpu = raw_smp_processor_id();
1241 data = tr->data[cpu];
1243 if (unlikely(atomic_read(&data->disabled)))
1246 /* Lockdep uses trace_printk for lock tracing */
1247 local_irq_save(flags);
1248 __raw_spin_lock(&trace_buf_lock);
1249 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1251 if (len > TRACE_BUF_SIZE || len < 0)
1254 size = sizeof(*entry) + sizeof(u32) * len;
1255 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1258 entry = ring_buffer_event_data(event);
1262 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1263 ring_buffer_unlock_commit(tr->buffer, event);
1266 __raw_spin_unlock(&trace_buf_lock);
1267 local_irq_restore(flags);
1270 ftrace_preempt_enable(resched);
1271 unpause_graph_tracing();
1275 EXPORT_SYMBOL_GPL(trace_vbprintk);
1277 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1279 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1280 static char trace_buf[TRACE_BUF_SIZE];
1282 struct ring_buffer_event *event;
1283 struct trace_array *tr = &global_trace;
1284 struct trace_array_cpu *data;
1285 int cpu, len = 0, size, pc;
1286 struct print_entry *entry;
1287 unsigned long irq_flags;
1289 if (tracing_disabled || tracing_selftest_running)
1292 pc = preempt_count();
1293 preempt_disable_notrace();
1294 cpu = raw_smp_processor_id();
1295 data = tr->data[cpu];
1297 if (unlikely(atomic_read(&data->disabled)))
1300 pause_graph_tracing();
1301 raw_local_irq_save(irq_flags);
1302 __raw_spin_lock(&trace_buf_lock);
1303 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1305 len = min(len, TRACE_BUF_SIZE-1);
1308 size = sizeof(*entry) + len + 1;
1309 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1312 entry = ring_buffer_event_data(event);
1315 memcpy(&entry->buf, trace_buf, len);
1316 entry->buf[len] = 0;
1317 ring_buffer_unlock_commit(tr->buffer, event);
1320 __raw_spin_unlock(&trace_buf_lock);
1321 raw_local_irq_restore(irq_flags);
1322 unpause_graph_tracing();
1324 preempt_enable_notrace();
1328 EXPORT_SYMBOL_GPL(trace_vprintk);
1330 enum trace_file_type {
1331 TRACE_FILE_LAT_FMT = 1,
1332 TRACE_FILE_ANNOTATE = 2,
1335 static void trace_iterator_increment(struct trace_iterator *iter)
1337 /* Don't allow ftrace to trace into the ring buffers */
1338 ftrace_disable_cpu();
1341 if (iter->buffer_iter[iter->cpu])
1342 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1344 ftrace_enable_cpu();
1347 static struct trace_entry *
1348 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1350 struct ring_buffer_event *event;
1351 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1353 /* Don't allow ftrace to trace into the ring buffers */
1354 ftrace_disable_cpu();
1357 event = ring_buffer_iter_peek(buf_iter, ts);
1359 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1361 ftrace_enable_cpu();
1363 return event ? ring_buffer_event_data(event) : NULL;
1366 static struct trace_entry *
1367 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1369 struct ring_buffer *buffer = iter->tr->buffer;
1370 struct trace_entry *ent, *next = NULL;
1371 int cpu_file = iter->cpu_file;
1372 u64 next_ts = 0, ts;
1377 * If we are in a per_cpu trace file, don't bother by iterating over
1378 * all cpu and peek directly.
1380 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1381 if (ring_buffer_empty_cpu(buffer, cpu_file))
1383 ent = peek_next_entry(iter, cpu_file, ent_ts);
1385 *ent_cpu = cpu_file;
1390 for_each_tracing_cpu(cpu) {
1392 if (ring_buffer_empty_cpu(buffer, cpu))
1395 ent = peek_next_entry(iter, cpu, &ts);
1398 * Pick the entry with the smallest timestamp:
1400 if (ent && (!next || ts < next_ts)) {
1408 *ent_cpu = next_cpu;
1416 /* Find the next real entry, without updating the iterator itself */
1417 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1418 int *ent_cpu, u64 *ent_ts)
1420 return __find_next_entry(iter, ent_cpu, ent_ts);
1423 /* Find the next real entry, and increment the iterator to the next entry */
1424 static void *find_next_entry_inc(struct trace_iterator *iter)
1426 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1429 trace_iterator_increment(iter);
1431 return iter->ent ? iter : NULL;
1434 static void trace_consume(struct trace_iterator *iter)
1436 /* Don't allow ftrace to trace into the ring buffers */
1437 ftrace_disable_cpu();
1438 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1439 ftrace_enable_cpu();
1442 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1444 struct trace_iterator *iter = m->private;
1450 /* can't go backwards */
1455 ent = find_next_entry_inc(iter);
1459 while (ent && iter->idx < i)
1460 ent = find_next_entry_inc(iter);
1468 * No necessary locking here. The worst thing which can
1469 * happen is loosing events consumed at the same time
1470 * by a trace_pipe reader.
1471 * Other than that, we don't risk to crash the ring buffer
1472 * because it serializes the readers.
1474 * The current tracer is copied to avoid a global locking
1477 static void *s_start(struct seq_file *m, loff_t *pos)
1479 struct trace_iterator *iter = m->private;
1480 static struct tracer *old_tracer;
1481 int cpu_file = iter->cpu_file;
1486 /* copy the tracer to avoid using a global lock all around */
1487 mutex_lock(&trace_types_lock);
1488 if (unlikely(old_tracer != current_trace && current_trace)) {
1489 old_tracer = current_trace;
1490 *iter->trace = *current_trace;
1492 mutex_unlock(&trace_types_lock);
1494 atomic_inc(&trace_record_cmdline_disabled);
1496 if (*pos != iter->pos) {
1501 ftrace_disable_cpu();
1503 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1504 for_each_tracing_cpu(cpu)
1505 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1507 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1510 ftrace_enable_cpu();
1512 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1517 p = s_next(m, p, &l);
1523 static void s_stop(struct seq_file *m, void *p)
1525 atomic_dec(&trace_record_cmdline_disabled);
1528 static void print_lat_help_header(struct seq_file *m)
1530 seq_puts(m, "# _------=> CPU# \n");
1531 seq_puts(m, "# / _-----=> irqs-off \n");
1532 seq_puts(m, "# | / _----=> need-resched \n");
1533 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1534 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1535 seq_puts(m, "# |||| / \n");
1536 seq_puts(m, "# ||||| delay \n");
1537 seq_puts(m, "# cmd pid ||||| time | caller \n");
1538 seq_puts(m, "# \\ / ||||| \\ | / \n");
1541 static void print_func_help_header(struct seq_file *m)
1543 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1544 seq_puts(m, "# | | | | |\n");
1549 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1551 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1552 struct trace_array *tr = iter->tr;
1553 struct trace_array_cpu *data = tr->data[tr->cpu];
1554 struct tracer *type = current_trace;
1555 unsigned long total;
1556 unsigned long entries;
1557 const char *name = "preemption";
1562 entries = ring_buffer_entries(iter->tr->buffer);
1564 ring_buffer_overruns(iter->tr->buffer);
1566 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1568 seq_puts(m, "# -----------------------------------"
1569 "---------------------------------\n");
1570 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1571 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1572 nsecs_to_usecs(data->saved_latency),
1576 #if defined(CONFIG_PREEMPT_NONE)
1578 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1580 #elif defined(CONFIG_PREEMPT)
1585 /* These are reserved for later use */
1588 seq_printf(m, " #P:%d)\n", num_online_cpus());
1592 seq_puts(m, "# -----------------\n");
1593 seq_printf(m, "# | task: %.16s-%d "
1594 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1595 data->comm, data->pid, data->uid, data->nice,
1596 data->policy, data->rt_priority);
1597 seq_puts(m, "# -----------------\n");
1599 if (data->critical_start) {
1600 seq_puts(m, "# => started at: ");
1601 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1602 trace_print_seq(m, &iter->seq);
1603 seq_puts(m, "\n# => ended at: ");
1604 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1605 trace_print_seq(m, &iter->seq);
1612 static void test_cpu_buff_start(struct trace_iterator *iter)
1614 struct trace_seq *s = &iter->seq;
1616 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1619 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1622 if (cpumask_test_cpu(iter->cpu, iter->started))
1625 cpumask_set_cpu(iter->cpu, iter->started);
1627 /* Don't print started cpu buffer for the first entry of the trace */
1629 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1633 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1635 struct trace_seq *s = &iter->seq;
1636 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1637 struct trace_entry *entry;
1638 struct trace_event *event;
1642 test_cpu_buff_start(iter);
1644 event = ftrace_find_event(entry->type);
1646 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1647 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1648 if (!trace_print_lat_context(iter))
1651 if (!trace_print_context(iter))
1657 return event->trace(iter, sym_flags);
1659 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1662 return TRACE_TYPE_HANDLED;
1664 return TRACE_TYPE_PARTIAL_LINE;
1667 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1669 struct trace_seq *s = &iter->seq;
1670 struct trace_entry *entry;
1671 struct trace_event *event;
1675 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1676 if (!trace_seq_printf(s, "%d %d %llu ",
1677 entry->pid, iter->cpu, iter->ts))
1681 event = ftrace_find_event(entry->type);
1683 return event->raw(iter, 0);
1685 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1688 return TRACE_TYPE_HANDLED;
1690 return TRACE_TYPE_PARTIAL_LINE;
1693 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1695 struct trace_seq *s = &iter->seq;
1696 unsigned char newline = '\n';
1697 struct trace_entry *entry;
1698 struct trace_event *event;
1702 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1703 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1704 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1705 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1708 event = ftrace_find_event(entry->type);
1710 enum print_line_t ret = event->hex(iter, 0);
1711 if (ret != TRACE_TYPE_HANDLED)
1715 SEQ_PUT_FIELD_RET(s, newline);
1717 return TRACE_TYPE_HANDLED;
1720 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1722 struct trace_seq *s = &iter->seq;
1723 struct trace_entry *entry;
1724 struct trace_event *event;
1728 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1729 SEQ_PUT_FIELD_RET(s, entry->pid);
1730 SEQ_PUT_FIELD_RET(s, iter->cpu);
1731 SEQ_PUT_FIELD_RET(s, iter->ts);
1734 event = ftrace_find_event(entry->type);
1735 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1738 static int trace_empty(struct trace_iterator *iter)
1742 /* If we are looking at one CPU buffer, only check that one */
1743 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1744 cpu = iter->cpu_file;
1745 if (iter->buffer_iter[cpu]) {
1746 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1749 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1755 for_each_tracing_cpu(cpu) {
1756 if (iter->buffer_iter[cpu]) {
1757 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1760 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1768 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1770 enum print_line_t ret;
1772 if (iter->trace && iter->trace->print_line) {
1773 ret = iter->trace->print_line(iter);
1774 if (ret != TRACE_TYPE_UNHANDLED)
1778 if (iter->ent->type == TRACE_BPRINT &&
1779 trace_flags & TRACE_ITER_PRINTK &&
1780 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1781 return trace_print_bprintk_msg_only(iter);
1783 if (iter->ent->type == TRACE_PRINT &&
1784 trace_flags & TRACE_ITER_PRINTK &&
1785 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1786 return trace_print_printk_msg_only(iter);
1788 if (trace_flags & TRACE_ITER_BIN)
1789 return print_bin_fmt(iter);
1791 if (trace_flags & TRACE_ITER_HEX)
1792 return print_hex_fmt(iter);
1794 if (trace_flags & TRACE_ITER_RAW)
1795 return print_raw_fmt(iter);
1797 return print_trace_fmt(iter);
1800 static int s_show(struct seq_file *m, void *v)
1802 struct trace_iterator *iter = v;
1804 if (iter->ent == NULL) {
1806 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1809 if (iter->trace && iter->trace->print_header)
1810 iter->trace->print_header(m);
1811 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1812 /* print nothing if the buffers are empty */
1813 if (trace_empty(iter))
1815 print_trace_header(m, iter);
1816 if (!(trace_flags & TRACE_ITER_VERBOSE))
1817 print_lat_help_header(m);
1819 if (!(trace_flags & TRACE_ITER_VERBOSE))
1820 print_func_help_header(m);
1823 print_trace_line(iter);
1824 trace_print_seq(m, &iter->seq);
1830 static struct seq_operations tracer_seq_ops = {
1837 static struct trace_iterator *
1838 __tracing_open(struct inode *inode, struct file *file)
1840 long cpu_file = (long) inode->i_private;
1841 void *fail_ret = ERR_PTR(-ENOMEM);
1842 struct trace_iterator *iter;
1846 if (tracing_disabled)
1847 return ERR_PTR(-ENODEV);
1849 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1851 return ERR_PTR(-ENOMEM);
1854 * We make a copy of the current tracer to avoid concurrent
1855 * changes on it while we are reading.
1857 mutex_lock(&trace_types_lock);
1858 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1863 *iter->trace = *current_trace;
1865 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1868 cpumask_clear(iter->started);
1870 if (current_trace && current_trace->print_max)
1873 iter->tr = &global_trace;
1875 mutex_init(&iter->mutex);
1876 iter->cpu_file = cpu_file;
1878 /* Notify the tracer early; before we stop tracing. */
1879 if (iter->trace && iter->trace->open)
1880 iter->trace->open(iter);
1882 /* Annotate start of buffers if we had overruns */
1883 if (ring_buffer_overruns(iter->tr->buffer))
1884 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1886 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1887 for_each_tracing_cpu(cpu) {
1889 iter->buffer_iter[cpu] =
1890 ring_buffer_read_start(iter->tr->buffer, cpu);
1893 cpu = iter->cpu_file;
1894 iter->buffer_iter[cpu] =
1895 ring_buffer_read_start(iter->tr->buffer, cpu);
1898 /* TODO stop tracer */
1899 ret = seq_open(file, &tracer_seq_ops);
1901 fail_ret = ERR_PTR(ret);
1905 m = file->private_data;
1908 /* stop the trace while dumping */
1911 mutex_unlock(&trace_types_lock);
1916 for_each_tracing_cpu(cpu) {
1917 if (iter->buffer_iter[cpu])
1918 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1920 free_cpumask_var(iter->started);
1922 mutex_unlock(&trace_types_lock);
1929 int tracing_open_generic(struct inode *inode, struct file *filp)
1931 if (tracing_disabled)
1934 filp->private_data = inode->i_private;
1938 static int tracing_release(struct inode *inode, struct file *file)
1940 struct seq_file *m = (struct seq_file *)file->private_data;
1941 struct trace_iterator *iter;
1944 if (!(file->f_mode & FMODE_READ))
1949 mutex_lock(&trace_types_lock);
1950 for_each_tracing_cpu(cpu) {
1951 if (iter->buffer_iter[cpu])
1952 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1955 if (iter->trace && iter->trace->close)
1956 iter->trace->close(iter);
1958 /* reenable tracing if it was previously enabled */
1960 mutex_unlock(&trace_types_lock);
1962 seq_release(inode, file);
1963 mutex_destroy(&iter->mutex);
1964 free_cpumask_var(iter->started);
1970 static int tracing_open(struct inode *inode, struct file *file)
1972 struct trace_iterator *iter;
1975 /* If this file was open for write, then erase contents */
1976 if ((file->f_mode & FMODE_WRITE) &&
1977 !(file->f_flags & O_APPEND)) {
1978 long cpu = (long) inode->i_private;
1980 if (cpu == TRACE_PIPE_ALL_CPU)
1981 tracing_reset_online_cpus(&global_trace);
1983 tracing_reset(&global_trace, cpu);
1986 if (file->f_mode & FMODE_READ) {
1987 iter = __tracing_open(inode, file);
1989 ret = PTR_ERR(iter);
1990 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1991 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;
2068 tracing_write_stub(struct file *filp, const char __user *ubuf,
2069 size_t count, loff_t *ppos)
2074 static const struct file_operations tracing_fops = {
2075 .open = tracing_open,
2077 .write = tracing_write_stub,
2078 .llseek = seq_lseek,
2079 .release = tracing_release,
2082 static const struct file_operations show_traces_fops = {
2083 .open = show_traces_open,
2085 .release = seq_release,
2089 * Only trace on a CPU if the bitmask is set:
2091 static cpumask_var_t tracing_cpumask;
2094 * The tracer itself will not take this lock, but still we want
2095 * to provide a consistent cpumask to user-space:
2097 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2100 * Temporary storage for the character representation of the
2101 * CPU bitmask (and one more byte for the newline):
2103 static char mask_str[NR_CPUS + 1];
2106 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2107 size_t count, loff_t *ppos)
2111 mutex_lock(&tracing_cpumask_update_lock);
2113 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2114 if (count - len < 2) {
2118 len += sprintf(mask_str + len, "\n");
2119 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2122 mutex_unlock(&tracing_cpumask_update_lock);
2128 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2129 size_t count, loff_t *ppos)
2132 cpumask_var_t tracing_cpumask_new;
2134 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2137 mutex_lock(&tracing_cpumask_update_lock);
2138 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2142 local_irq_disable();
2143 __raw_spin_lock(&ftrace_max_lock);
2144 for_each_tracing_cpu(cpu) {
2146 * Increase/decrease the disabled counter if we are
2147 * about to flip a bit in the cpumask:
2149 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2150 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2151 atomic_inc(&global_trace.data[cpu]->disabled);
2153 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2154 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2155 atomic_dec(&global_trace.data[cpu]->disabled);
2158 __raw_spin_unlock(&ftrace_max_lock);
2161 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2163 mutex_unlock(&tracing_cpumask_update_lock);
2164 free_cpumask_var(tracing_cpumask_new);
2169 mutex_unlock(&tracing_cpumask_update_lock);
2170 free_cpumask_var(tracing_cpumask);
2175 static const struct file_operations tracing_cpumask_fops = {
2176 .open = tracing_open_generic,
2177 .read = tracing_cpumask_read,
2178 .write = tracing_cpumask_write,
2182 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2183 size_t cnt, loff_t *ppos)
2185 struct tracer_opt *trace_opts;
2193 /* calculate max size */
2194 for (i = 0; trace_options[i]; i++) {
2195 len += strlen(trace_options[i]);
2196 len += 3; /* "no" and newline */
2199 mutex_lock(&trace_types_lock);
2200 tracer_flags = current_trace->flags->val;
2201 trace_opts = current_trace->flags->opts;
2204 * Increase the size with names of options specific
2205 * of the current tracer.
2207 for (i = 0; trace_opts[i].name; i++) {
2208 len += strlen(trace_opts[i].name);
2209 len += 3; /* "no" and newline */
2212 /* +2 for \n and \0 */
2213 buf = kmalloc(len + 2, GFP_KERNEL);
2215 mutex_unlock(&trace_types_lock);
2219 for (i = 0; trace_options[i]; i++) {
2220 if (trace_flags & (1 << i))
2221 r += sprintf(buf + r, "%s\n", trace_options[i]);
2223 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2226 for (i = 0; trace_opts[i].name; i++) {
2227 if (tracer_flags & trace_opts[i].bit)
2228 r += sprintf(buf + r, "%s\n",
2229 trace_opts[i].name);
2231 r += sprintf(buf + r, "no%s\n",
2232 trace_opts[i].name);
2234 mutex_unlock(&trace_types_lock);
2236 WARN_ON(r >= len + 2);
2238 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2244 /* Try to assign a tracer specific option */
2245 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2247 struct tracer_flags *trace_flags = trace->flags;
2248 struct tracer_opt *opts = NULL;
2252 for (i = 0; trace_flags->opts[i].name; i++) {
2253 opts = &trace_flags->opts[i];
2254 len = strlen(opts->name);
2256 if (strncmp(cmp, opts->name, len) == 0) {
2257 ret = trace->set_flag(trace_flags->val,
2263 if (!trace_flags->opts[i].name)
2266 /* Refused to handle */
2271 trace_flags->val &= ~opts->bit;
2273 trace_flags->val |= opts->bit;
2278 static void set_tracer_flags(unsigned int mask, int enabled)
2280 /* do nothing if flag is already set */
2281 if (!!(trace_flags & mask) == !!enabled)
2285 trace_flags |= mask;
2287 trace_flags &= ~mask;
2289 if (mask == TRACE_ITER_GLOBAL_CLK) {
2293 func = trace_clock_global;
2295 func = trace_clock_local;
2297 mutex_lock(&trace_types_lock);
2298 ring_buffer_set_clock(global_trace.buffer, func);
2301 ring_buffer_set_clock(max_tr.buffer, func);
2302 mutex_unlock(&trace_types_lock);
2307 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2308 size_t cnt, loff_t *ppos)
2316 if (cnt >= sizeof(buf))
2319 if (copy_from_user(&buf, ubuf, cnt))
2324 if (strncmp(buf, "no", 2) == 0) {
2329 for (i = 0; trace_options[i]; i++) {
2330 int len = strlen(trace_options[i]);
2332 if (strncmp(cmp, trace_options[i], len) == 0) {
2333 set_tracer_flags(1 << i, !neg);
2338 /* If no option could be set, test the specific tracer options */
2339 if (!trace_options[i]) {
2340 mutex_lock(&trace_types_lock);
2341 ret = set_tracer_option(current_trace, cmp, neg);
2342 mutex_unlock(&trace_types_lock);
2352 static const struct file_operations tracing_iter_fops = {
2353 .open = tracing_open_generic,
2354 .read = tracing_trace_options_read,
2355 .write = tracing_trace_options_write,
2358 static const char readme_msg[] =
2359 "tracing mini-HOWTO:\n\n"
2361 "# mount -t debugfs nodev /debug\n\n"
2362 "# cat /debug/tracing/available_tracers\n"
2363 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2364 "# cat /debug/tracing/current_tracer\n"
2366 "# echo sched_switch > /debug/tracing/current_tracer\n"
2367 "# cat /debug/tracing/current_tracer\n"
2369 "# cat /debug/tracing/trace_options\n"
2370 "noprint-parent nosym-offset nosym-addr noverbose\n"
2371 "# echo print-parent > /debug/tracing/trace_options\n"
2372 "# echo 1 > /debug/tracing/tracing_enabled\n"
2373 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2374 "echo 0 > /debug/tracing/tracing_enabled\n"
2378 tracing_readme_read(struct file *filp, char __user *ubuf,
2379 size_t cnt, loff_t *ppos)
2381 return simple_read_from_buffer(ubuf, cnt, ppos,
2382 readme_msg, strlen(readme_msg));
2385 static const struct file_operations tracing_readme_fops = {
2386 .open = tracing_open_generic,
2387 .read = tracing_readme_read,
2391 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2392 size_t cnt, loff_t *ppos)
2397 r = sprintf(buf, "%u\n", tracer_enabled);
2398 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2402 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2403 size_t cnt, loff_t *ppos)
2405 struct trace_array *tr = filp->private_data;
2410 if (cnt >= sizeof(buf))
2413 if (copy_from_user(&buf, ubuf, cnt))
2418 ret = strict_strtoul(buf, 10, &val);
2424 mutex_lock(&trace_types_lock);
2425 if (tracer_enabled ^ val) {
2428 if (current_trace->start)
2429 current_trace->start(tr);
2434 if (current_trace->stop)
2435 current_trace->stop(tr);
2438 mutex_unlock(&trace_types_lock);
2446 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2447 size_t cnt, loff_t *ppos)
2449 char buf[max_tracer_type_len+2];
2452 mutex_lock(&trace_types_lock);
2454 r = sprintf(buf, "%s\n", current_trace->name);
2456 r = sprintf(buf, "\n");
2457 mutex_unlock(&trace_types_lock);
2459 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2462 int tracer_init(struct tracer *t, struct trace_array *tr)
2464 tracing_reset_online_cpus(tr);
2468 static int tracing_resize_ring_buffer(unsigned long size)
2473 * If kernel or user changes the size of the ring buffer
2474 * we use the size that was given, and we can forget about
2475 * expanding it later.
2477 ring_buffer_expanded = 1;
2479 ret = ring_buffer_resize(global_trace.buffer, size);
2483 ret = ring_buffer_resize(max_tr.buffer, size);
2487 r = ring_buffer_resize(global_trace.buffer,
2488 global_trace.entries);
2491 * AARGH! We are left with different
2492 * size max buffer!!!!
2493 * The max buffer is our "snapshot" buffer.
2494 * When a tracer needs a snapshot (one of the
2495 * latency tracers), it swaps the max buffer
2496 * with the saved snap shot. We succeeded to
2497 * update the size of the main buffer, but failed to
2498 * update the size of the max buffer. But when we tried
2499 * to reset the main buffer to the original size, we
2500 * failed there too. This is very unlikely to
2501 * happen, but if it does, warn and kill all
2505 tracing_disabled = 1;
2510 global_trace.entries = size;
2516 * tracing_update_buffers - used by tracing facility to expand ring buffers
2518 * To save on memory when the tracing is never used on a system with it
2519 * configured in. The ring buffers are set to a minimum size. But once
2520 * a user starts to use the tracing facility, then they need to grow
2521 * to their default size.
2523 * This function is to be called when a tracer is about to be used.
2525 int tracing_update_buffers(void)
2529 mutex_lock(&trace_types_lock);
2530 if (!ring_buffer_expanded)
2531 ret = tracing_resize_ring_buffer(trace_buf_size);
2532 mutex_unlock(&trace_types_lock);
2537 struct trace_option_dentry;
2539 static struct trace_option_dentry *
2540 create_trace_option_files(struct tracer *tracer);
2543 destroy_trace_option_files(struct trace_option_dentry *topts);
2545 static int tracing_set_tracer(const char *buf)
2547 static struct trace_option_dentry *topts;
2548 struct trace_array *tr = &global_trace;
2552 mutex_lock(&trace_types_lock);
2554 if (!ring_buffer_expanded) {
2555 ret = tracing_resize_ring_buffer(trace_buf_size);
2561 for (t = trace_types; t; t = t->next) {
2562 if (strcmp(t->name, buf) == 0)
2569 if (t == current_trace)
2572 trace_branch_disable();
2573 if (current_trace && current_trace->reset)
2574 current_trace->reset(tr);
2576 destroy_trace_option_files(topts);
2580 topts = create_trace_option_files(current_trace);
2583 ret = tracer_init(t, tr);
2588 trace_branch_enable(tr);
2590 mutex_unlock(&trace_types_lock);
2596 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2597 size_t cnt, loff_t *ppos)
2599 char buf[max_tracer_type_len+1];
2606 if (cnt > max_tracer_type_len)
2607 cnt = max_tracer_type_len;
2609 if (copy_from_user(&buf, ubuf, cnt))
2614 /* strip ending whitespace. */
2615 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2618 err = tracing_set_tracer(buf);
2628 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2629 size_t cnt, loff_t *ppos)
2631 unsigned long *ptr = filp->private_data;
2635 r = snprintf(buf, sizeof(buf), "%ld\n",
2636 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2637 if (r > sizeof(buf))
2639 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2643 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2644 size_t cnt, loff_t *ppos)
2646 unsigned long *ptr = filp->private_data;
2651 if (cnt >= sizeof(buf))
2654 if (copy_from_user(&buf, ubuf, cnt))
2659 ret = strict_strtoul(buf, 10, &val);
2668 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2670 long cpu_file = (long) inode->i_private;
2671 struct trace_iterator *iter;
2674 if (tracing_disabled)
2677 mutex_lock(&trace_types_lock);
2679 /* We only allow one reader per cpu */
2680 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2681 if (!cpumask_empty(tracing_reader_cpumask)) {
2685 cpumask_setall(tracing_reader_cpumask);
2687 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2688 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2695 /* create a buffer to store the information to pass to userspace */
2696 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2703 * We make a copy of the current tracer to avoid concurrent
2704 * changes on it while we are reading.
2706 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2712 *iter->trace = *current_trace;
2714 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2719 /* trace pipe does not show start of buffer */
2720 cpumask_setall(iter->started);
2722 iter->cpu_file = cpu_file;
2723 iter->tr = &global_trace;
2724 mutex_init(&iter->mutex);
2725 filp->private_data = iter;
2727 if (iter->trace->pipe_open)
2728 iter->trace->pipe_open(iter);
2731 mutex_unlock(&trace_types_lock);
2737 mutex_unlock(&trace_types_lock);
2741 static int tracing_release_pipe(struct inode *inode, struct file *file)
2743 struct trace_iterator *iter = file->private_data;
2745 mutex_lock(&trace_types_lock);
2747 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2748 cpumask_clear(tracing_reader_cpumask);
2750 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2752 mutex_unlock(&trace_types_lock);
2754 free_cpumask_var(iter->started);
2755 mutex_destroy(&iter->mutex);
2763 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2765 struct trace_iterator *iter = filp->private_data;
2767 if (trace_flags & TRACE_ITER_BLOCK) {
2769 * Always select as readable when in blocking mode
2771 return POLLIN | POLLRDNORM;
2773 if (!trace_empty(iter))
2774 return POLLIN | POLLRDNORM;
2775 poll_wait(filp, &trace_wait, poll_table);
2776 if (!trace_empty(iter))
2777 return POLLIN | POLLRDNORM;
2784 void default_wait_pipe(struct trace_iterator *iter)
2788 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2790 if (trace_empty(iter))
2793 finish_wait(&trace_wait, &wait);
2797 * This is a make-shift waitqueue.
2798 * A tracer might use this callback on some rare cases:
2800 * 1) the current tracer might hold the runqueue lock when it wakes up
2801 * a reader, hence a deadlock (sched, function, and function graph tracers)
2802 * 2) the function tracers, trace all functions, we don't want
2803 * the overhead of calling wake_up and friends
2804 * (and tracing them too)
2806 * Anyway, this is really very primitive wakeup.
2808 void poll_wait_pipe(struct trace_iterator *iter)
2810 set_current_state(TASK_INTERRUPTIBLE);
2811 /* sleep for 100 msecs, and try again. */
2812 schedule_timeout(HZ / 10);
2815 /* Must be called with trace_types_lock mutex held. */
2816 static int tracing_wait_pipe(struct file *filp)
2818 struct trace_iterator *iter = filp->private_data;
2820 while (trace_empty(iter)) {
2822 if ((filp->f_flags & O_NONBLOCK)) {
2826 mutex_unlock(&iter->mutex);
2828 iter->trace->wait_pipe(iter);
2830 mutex_lock(&iter->mutex);
2832 if (signal_pending(current))
2836 * We block until we read something and tracing is disabled.
2837 * We still block if tracing is disabled, but we have never
2838 * read anything. This allows a user to cat this file, and
2839 * then enable tracing. But after we have read something,
2840 * we give an EOF when tracing is again disabled.
2842 * iter->pos will be 0 if we haven't read anything.
2844 if (!tracer_enabled && iter->pos)
2855 tracing_read_pipe(struct file *filp, char __user *ubuf,
2856 size_t cnt, loff_t *ppos)
2858 struct trace_iterator *iter = filp->private_data;
2859 static struct tracer *old_tracer;
2862 /* return any leftover data */
2863 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2867 trace_seq_init(&iter->seq);
2869 /* copy the tracer to avoid using a global lock all around */
2870 mutex_lock(&trace_types_lock);
2871 if (unlikely(old_tracer != current_trace && current_trace)) {
2872 old_tracer = current_trace;
2873 *iter->trace = *current_trace;
2875 mutex_unlock(&trace_types_lock);
2878 * Avoid more than one consumer on a single file descriptor
2879 * This is just a matter of traces coherency, the ring buffer itself
2882 mutex_lock(&iter->mutex);
2883 if (iter->trace->read) {
2884 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2890 sret = tracing_wait_pipe(filp);
2894 /* stop when tracing is finished */
2895 if (trace_empty(iter)) {
2900 if (cnt >= PAGE_SIZE)
2901 cnt = PAGE_SIZE - 1;
2903 /* reset all but tr, trace, and overruns */
2904 memset(&iter->seq, 0,
2905 sizeof(struct trace_iterator) -
2906 offsetof(struct trace_iterator, seq));
2909 while (find_next_entry_inc(iter) != NULL) {
2910 enum print_line_t ret;
2911 int len = iter->seq.len;
2913 ret = print_trace_line(iter);
2914 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2915 /* don't print partial lines */
2916 iter->seq.len = len;
2919 if (ret != TRACE_TYPE_NO_CONSUME)
2920 trace_consume(iter);
2922 if (iter->seq.len >= cnt)
2926 /* Now copy what we have to the user */
2927 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2928 if (iter->seq.readpos >= iter->seq.len)
2929 trace_seq_init(&iter->seq);
2932 * If there was nothing to send to user, inspite of consuming trace
2933 * entries, go back to wait for more entries.
2939 mutex_unlock(&iter->mutex);
2944 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2945 struct pipe_buffer *buf)
2947 __free_page(buf->page);
2950 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2953 __free_page(spd->pages[idx]);
2956 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2958 .map = generic_pipe_buf_map,
2959 .unmap = generic_pipe_buf_unmap,
2960 .confirm = generic_pipe_buf_confirm,
2961 .release = tracing_pipe_buf_release,
2962 .steal = generic_pipe_buf_steal,
2963 .get = generic_pipe_buf_get,
2967 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2972 /* Seq buffer is page-sized, exactly what we need. */
2974 count = iter->seq.len;
2975 ret = print_trace_line(iter);
2976 count = iter->seq.len - count;
2979 iter->seq.len -= count;
2982 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2983 iter->seq.len -= count;
2987 trace_consume(iter);
2989 if (!find_next_entry_inc(iter)) {
2999 static ssize_t tracing_splice_read_pipe(struct file *filp,
3001 struct pipe_inode_info *pipe,
3005 struct page *pages[PIPE_BUFFERS];
3006 struct partial_page partial[PIPE_BUFFERS];
3007 struct trace_iterator *iter = filp->private_data;
3008 struct splice_pipe_desc spd = {
3011 .nr_pages = 0, /* This gets updated below. */
3013 .ops = &tracing_pipe_buf_ops,
3014 .spd_release = tracing_spd_release_pipe,
3016 static struct tracer *old_tracer;
3021 /* copy the tracer to avoid using a global lock all around */
3022 mutex_lock(&trace_types_lock);
3023 if (unlikely(old_tracer != current_trace && current_trace)) {
3024 old_tracer = current_trace;
3025 *iter->trace = *current_trace;
3027 mutex_unlock(&trace_types_lock);
3029 mutex_lock(&iter->mutex);
3031 if (iter->trace->splice_read) {
3032 ret = iter->trace->splice_read(iter, filp,
3033 ppos, pipe, len, flags);
3038 ret = tracing_wait_pipe(filp);
3042 if (!iter->ent && !find_next_entry_inc(iter)) {
3047 /* Fill as many pages as possible. */
3048 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3049 pages[i] = alloc_page(GFP_KERNEL);
3053 rem = tracing_fill_pipe_page(rem, iter);
3055 /* Copy the data into the page, so we can start over. */
3056 ret = trace_seq_to_buffer(&iter->seq,
3057 page_address(pages[i]),
3060 __free_page(pages[i]);
3063 partial[i].offset = 0;
3064 partial[i].len = iter->seq.len;
3066 trace_seq_init(&iter->seq);
3069 mutex_unlock(&iter->mutex);
3073 return splice_to_pipe(pipe, &spd);
3076 mutex_unlock(&iter->mutex);
3082 tracing_entries_read(struct file *filp, char __user *ubuf,
3083 size_t cnt, loff_t *ppos)
3085 struct trace_array *tr = filp->private_data;
3089 mutex_lock(&trace_types_lock);
3090 if (!ring_buffer_expanded)
3091 r = sprintf(buf, "%lu (expanded: %lu)\n",
3093 trace_buf_size >> 10);
3095 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3096 mutex_unlock(&trace_types_lock);
3098 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3102 tracing_entries_write(struct file *filp, const char __user *ubuf,
3103 size_t cnt, loff_t *ppos)
3109 if (cnt >= sizeof(buf))
3112 if (copy_from_user(&buf, ubuf, cnt))
3117 ret = strict_strtoul(buf, 10, &val);
3121 /* must have at least 1 entry */
3125 mutex_lock(&trace_types_lock);
3129 /* disable all cpu buffers */
3130 for_each_tracing_cpu(cpu) {
3131 if (global_trace.data[cpu])
3132 atomic_inc(&global_trace.data[cpu]->disabled);
3133 if (max_tr.data[cpu])
3134 atomic_inc(&max_tr.data[cpu]->disabled);
3137 /* value is in KB */
3140 if (val != global_trace.entries) {
3141 ret = tracing_resize_ring_buffer(val);
3150 /* If check pages failed, return ENOMEM */
3151 if (tracing_disabled)
3154 for_each_tracing_cpu(cpu) {
3155 if (global_trace.data[cpu])
3156 atomic_dec(&global_trace.data[cpu]->disabled);
3157 if (max_tr.data[cpu])
3158 atomic_dec(&max_tr.data[cpu]->disabled);
3162 max_tr.entries = global_trace.entries;
3163 mutex_unlock(&trace_types_lock);
3168 static int mark_printk(const char *fmt, ...)
3172 va_start(args, fmt);
3173 ret = trace_vprintk(0, fmt, args);
3179 tracing_mark_write(struct file *filp, const char __user *ubuf,
3180 size_t cnt, loff_t *fpos)
3185 if (tracing_disabled)
3188 if (cnt > TRACE_BUF_SIZE)
3189 cnt = TRACE_BUF_SIZE;
3191 buf = kmalloc(cnt + 1, GFP_KERNEL);
3195 if (copy_from_user(buf, ubuf, cnt)) {
3200 /* Cut from the first nil or newline. */
3202 end = strchr(buf, '\n');
3206 cnt = mark_printk("%s\n", buf);
3213 static const struct file_operations tracing_max_lat_fops = {
3214 .open = tracing_open_generic,
3215 .read = tracing_max_lat_read,
3216 .write = tracing_max_lat_write,
3219 static const struct file_operations tracing_ctrl_fops = {
3220 .open = tracing_open_generic,
3221 .read = tracing_ctrl_read,
3222 .write = tracing_ctrl_write,
3225 static const struct file_operations set_tracer_fops = {
3226 .open = tracing_open_generic,
3227 .read = tracing_set_trace_read,
3228 .write = tracing_set_trace_write,
3231 static const struct file_operations tracing_pipe_fops = {
3232 .open = tracing_open_pipe,
3233 .poll = tracing_poll_pipe,
3234 .read = tracing_read_pipe,
3235 .splice_read = tracing_splice_read_pipe,
3236 .release = tracing_release_pipe,
3239 static const struct file_operations tracing_entries_fops = {
3240 .open = tracing_open_generic,
3241 .read = tracing_entries_read,
3242 .write = tracing_entries_write,
3245 static const struct file_operations tracing_mark_fops = {
3246 .open = tracing_open_generic,
3247 .write = tracing_mark_write,
3250 struct ftrace_buffer_info {
3251 struct trace_array *tr;
3257 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3259 int cpu = (int)(long)inode->i_private;
3260 struct ftrace_buffer_info *info;
3262 if (tracing_disabled)
3265 info = kzalloc(sizeof(*info), GFP_KERNEL);
3269 info->tr = &global_trace;
3272 /* Force reading ring buffer for first read */
3273 info->read = (unsigned int)-1;
3275 filp->private_data = info;
3277 return nonseekable_open(inode, filp);
3281 tracing_buffers_read(struct file *filp, char __user *ubuf,
3282 size_t count, loff_t *ppos)
3284 struct ftrace_buffer_info *info = filp->private_data;
3293 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3297 /* Do we have previous read data to read? */
3298 if (info->read < PAGE_SIZE)
3303 ret = ring_buffer_read_page(info->tr->buffer,
3310 pos = ring_buffer_page_len(info->spare);
3312 if (pos < PAGE_SIZE)
3313 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3316 size = PAGE_SIZE - info->read;
3320 ret = copy_to_user(ubuf, info->spare + info->read, size);
3331 static int tracing_buffers_release(struct inode *inode, struct file *file)
3333 struct ftrace_buffer_info *info = file->private_data;
3336 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3343 struct ring_buffer *buffer;
3348 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3349 struct pipe_buffer *buf)
3351 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3356 ring_buffer_free_read_page(ref->buffer, ref->page);
3361 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3362 struct pipe_buffer *buf)
3367 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3368 struct pipe_buffer *buf)
3370 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3375 /* Pipe buffer operations for a buffer. */
3376 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3378 .map = generic_pipe_buf_map,
3379 .unmap = generic_pipe_buf_unmap,
3380 .confirm = generic_pipe_buf_confirm,
3381 .release = buffer_pipe_buf_release,
3382 .steal = buffer_pipe_buf_steal,
3383 .get = buffer_pipe_buf_get,
3387 * Callback from splice_to_pipe(), if we need to release some pages
3388 * at the end of the spd in case we error'ed out in filling the pipe.
3390 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3392 struct buffer_ref *ref =
3393 (struct buffer_ref *)spd->partial[i].private;
3398 ring_buffer_free_read_page(ref->buffer, ref->page);
3400 spd->partial[i].private = 0;
3404 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3405 struct pipe_inode_info *pipe, size_t len,
3408 struct ftrace_buffer_info *info = file->private_data;
3409 struct partial_page partial[PIPE_BUFFERS];
3410 struct page *pages[PIPE_BUFFERS];
3411 struct splice_pipe_desc spd = {
3415 .ops = &buffer_pipe_buf_ops,
3416 .spd_release = buffer_spd_release,
3418 struct buffer_ref *ref;
3422 if (*ppos & (PAGE_SIZE - 1)) {
3423 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3427 if (len & (PAGE_SIZE - 1)) {
3428 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3429 if (len < PAGE_SIZE)
3434 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= PAGE_SIZE) {
3438 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3442 ref->buffer = info->tr->buffer;
3443 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3449 r = ring_buffer_read_page(ref->buffer, &ref->page,
3452 ring_buffer_free_read_page(ref->buffer,
3459 * zero out any left over data, this is going to
3462 size = ring_buffer_page_len(ref->page);
3463 if (size < PAGE_SIZE)
3464 memset(ref->page + size, 0, PAGE_SIZE - size);
3466 page = virt_to_page(ref->page);
3468 spd.pages[i] = page;
3469 spd.partial[i].len = PAGE_SIZE;
3470 spd.partial[i].offset = 0;
3471 spd.partial[i].private = (unsigned long)ref;
3478 /* did we read anything? */
3479 if (!spd.nr_pages) {
3480 if (flags & SPLICE_F_NONBLOCK)
3488 ret = splice_to_pipe(pipe, &spd);
3493 static const struct file_operations tracing_buffers_fops = {
3494 .open = tracing_buffers_open,
3495 .read = tracing_buffers_read,
3496 .release = tracing_buffers_release,
3497 .splice_read = tracing_buffers_splice_read,
3498 .llseek = no_llseek,
3501 #ifdef CONFIG_DYNAMIC_FTRACE
3503 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3509 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3510 size_t cnt, loff_t *ppos)
3512 static char ftrace_dyn_info_buffer[1024];
3513 static DEFINE_MUTEX(dyn_info_mutex);
3514 unsigned long *p = filp->private_data;
3515 char *buf = ftrace_dyn_info_buffer;
3516 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3519 mutex_lock(&dyn_info_mutex);
3520 r = sprintf(buf, "%ld ", *p);
3522 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3525 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3527 mutex_unlock(&dyn_info_mutex);