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 <generated/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/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
42 #include "trace_output.h"
45 * On boot up, the ring buffer is set to the minimum size, so that
46 * we do not waste memory on systems that are not using tracing.
48 int ring_buffer_expanded;
51 * We need to change this state when a selftest is running.
52 * A selftest will lurk into the ring-buffer to count the
53 * entries inserted during the selftest although some concurrent
54 * insertions into the ring-buffer such as trace_printk could occurred
55 * at the same time, giving false positive or negative results.
57 static bool __read_mostly tracing_selftest_running;
60 * If a tracer is running, we do not want to run SELFTEST.
62 bool __read_mostly tracing_selftest_disabled;
64 /* For tracers that don't implement custom flags */
65 static struct tracer_opt dummy_tracer_opt[] = {
69 static struct tracer_flags dummy_tracer_flags = {
71 .opts = dummy_tracer_opt
74 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
80 * Kill all tracing for good (never come back).
81 * It is initialized to 1 but will turn to zero if the initialization
82 * of the tracer is successful. But that is the only place that sets
85 static int tracing_disabled = 1;
87 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
89 static inline void ftrace_disable_cpu(void)
92 __this_cpu_inc(ftrace_cpu_disabled);
95 static inline void ftrace_enable_cpu(void)
97 __this_cpu_dec(ftrace_cpu_disabled);
101 cpumask_var_t __read_mostly tracing_buffer_mask;
104 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
106 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
107 * is set, then ftrace_dump is called. This will output the contents
108 * of the ftrace buffers to the console. This is very useful for
109 * capturing traces that lead to crashes and outputing it to a
112 * It is default off, but you can enable it with either specifying
113 * "ftrace_dump_on_oops" in the kernel command line, or setting
114 * /proc/sys/kernel/ftrace_dump_on_oops
115 * Set 1 if you want to dump buffers of all CPUs
116 * Set 2 if you want to dump the buffer of the CPU that triggered oops
119 enum ftrace_dump_mode ftrace_dump_on_oops;
121 static int tracing_set_tracer(const char *buf);
123 #define MAX_TRACER_SIZE 100
124 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
125 static char *default_bootup_tracer;
127 static int __init set_cmdline_ftrace(char *str)
129 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
130 default_bootup_tracer = bootup_tracer_buf;
131 /* We are using ftrace early, expand it */
132 ring_buffer_expanded = 1;
135 __setup("ftrace=", set_cmdline_ftrace);
137 static int __init set_ftrace_dump_on_oops(char *str)
139 if (*str++ != '=' || !*str) {
140 ftrace_dump_on_oops = DUMP_ALL;
144 if (!strcmp("orig_cpu", str)) {
145 ftrace_dump_on_oops = DUMP_ORIG;
151 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
153 unsigned long long ns2usecs(cycle_t nsec)
161 * The global_trace is the descriptor that holds the tracing
162 * buffers for the live tracing. For each CPU, it contains
163 * a link list of pages that will store trace entries. The
164 * page descriptor of the pages in the memory is used to hold
165 * the link list by linking the lru item in the page descriptor
166 * to each of the pages in the buffer per CPU.
168 * For each active CPU there is a data field that holds the
169 * pages for the buffer for that CPU. Each CPU has the same number
170 * of pages allocated for its buffer.
172 static struct trace_array global_trace;
174 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
176 int filter_current_check_discard(struct ring_buffer *buffer,
177 struct ftrace_event_call *call, void *rec,
178 struct ring_buffer_event *event)
180 return filter_check_discard(call, rec, buffer, event);
182 EXPORT_SYMBOL_GPL(filter_current_check_discard);
184 cycle_t ftrace_now(int cpu)
188 /* Early boot up does not have a buffer yet */
189 if (!global_trace.buffer)
190 return trace_clock_local();
192 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
193 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
199 * The max_tr is used to snapshot the global_trace when a maximum
200 * latency is reached. Some tracers will use this to store a maximum
201 * trace while it continues examining live traces.
203 * The buffers for the max_tr are set up the same as the global_trace.
204 * When a snapshot is taken, the link list of the max_tr is swapped
205 * with the link list of the global_trace and the buffers are reset for
206 * the global_trace so the tracing can continue.
208 static struct trace_array max_tr;
210 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
212 /* tracer_enabled is used to toggle activation of a tracer */
213 static int tracer_enabled = 1;
216 * tracing_is_enabled - return tracer_enabled status
218 * This function is used by other tracers to know the status
219 * of the tracer_enabled flag. Tracers may use this function
220 * to know if it should enable their features when starting
221 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
223 int tracing_is_enabled(void)
225 return tracer_enabled;
229 * trace_buf_size is the size in bytes that is allocated
230 * for a buffer. Note, the number of bytes is always rounded
233 * This number is purposely set to a low number of 16384.
234 * If the dump on oops happens, it will be much appreciated
235 * to not have to wait for all that output. Anyway this can be
236 * boot time and run time configurable.
238 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
240 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
242 /* trace_types holds a link list of available tracers. */
243 static struct tracer *trace_types __read_mostly;
245 /* current_trace points to the tracer that is currently active */
246 static struct tracer *current_trace __read_mostly;
249 * trace_types_lock is used to protect the trace_types list.
251 static DEFINE_MUTEX(trace_types_lock);
254 * serialize the access of the ring buffer
256 * ring buffer serializes readers, but it is low level protection.
257 * The validity of the events (which returns by ring_buffer_peek() ..etc)
258 * are not protected by ring buffer.
260 * The content of events may become garbage if we allow other process consumes
261 * these events concurrently:
262 * A) the page of the consumed events may become a normal page
263 * (not reader page) in ring buffer, and this page will be rewrited
264 * by events producer.
265 * B) The page of the consumed events may become a page for splice_read,
266 * and this page will be returned to system.
268 * These primitives allow multi process access to different cpu ring buffer
271 * These primitives don't distinguish read-only and read-consume access.
272 * Multi read-only access are also serialized.
276 static DECLARE_RWSEM(all_cpu_access_lock);
277 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
279 static inline void trace_access_lock(int cpu)
281 if (cpu == TRACE_PIPE_ALL_CPU) {
282 /* gain it for accessing the whole ring buffer. */
283 down_write(&all_cpu_access_lock);
285 /* gain it for accessing a cpu ring buffer. */
287 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
288 down_read(&all_cpu_access_lock);
290 /* Secondly block other access to this @cpu ring buffer. */
291 mutex_lock(&per_cpu(cpu_access_lock, cpu));
295 static inline void trace_access_unlock(int cpu)
297 if (cpu == TRACE_PIPE_ALL_CPU) {
298 up_write(&all_cpu_access_lock);
300 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
301 up_read(&all_cpu_access_lock);
305 static inline void trace_access_lock_init(void)
309 for_each_possible_cpu(cpu)
310 mutex_init(&per_cpu(cpu_access_lock, cpu));
315 static DEFINE_MUTEX(access_lock);
317 static inline void trace_access_lock(int cpu)
320 mutex_lock(&access_lock);
323 static inline void trace_access_unlock(int cpu)
326 mutex_unlock(&access_lock);
329 static inline void trace_access_lock_init(void)
335 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
336 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
338 /* trace_flags holds trace_options default values */
339 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
340 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
341 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE;
343 static int trace_stop_count;
344 static DEFINE_RAW_SPINLOCK(tracing_start_lock);
346 static void wakeup_work_handler(struct work_struct *work)
348 wake_up(&trace_wait);
351 static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
354 * trace_wake_up - wake up tasks waiting for trace input
356 * Schedules a delayed work to wake up any task that is blocked on the
357 * trace_wait queue. These is used with trace_poll for tasks polling the
360 void trace_wake_up(void)
362 const unsigned long delay = msecs_to_jiffies(2);
364 if (trace_flags & TRACE_ITER_BLOCK)
366 schedule_delayed_work(&wakeup_work, delay);
369 static int __init set_buf_size(char *str)
371 unsigned long buf_size;
375 buf_size = memparse(str, &str);
376 /* nr_entries can not be zero */
379 trace_buf_size = buf_size;
382 __setup("trace_buf_size=", set_buf_size);
384 static int __init set_tracing_thresh(char *str)
386 unsigned long threshhold;
391 ret = strict_strtoul(str, 0, &threshhold);
394 tracing_thresh = threshhold * 1000;
397 __setup("tracing_thresh=", set_tracing_thresh);
399 unsigned long nsecs_to_usecs(unsigned long nsecs)
404 /* These must match the bit postions in trace_iterator_flags */
405 static const char *trace_options[] = {
436 { trace_clock_local, "local" },
437 { trace_clock_global, "global" },
438 { trace_clock_counter, "counter" },
444 * trace_parser_get_init - gets the buffer for trace parser
446 int trace_parser_get_init(struct trace_parser *parser, int size)
448 memset(parser, 0, sizeof(*parser));
450 parser->buffer = kmalloc(size, GFP_KERNEL);
459 * trace_parser_put - frees the buffer for trace parser
461 void trace_parser_put(struct trace_parser *parser)
463 kfree(parser->buffer);
467 * trace_get_user - reads the user input string separated by space
468 * (matched by isspace(ch))
470 * For each string found the 'struct trace_parser' is updated,
471 * and the function returns.
473 * Returns number of bytes read.
475 * See kernel/trace/trace.h for 'struct trace_parser' details.
477 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
478 size_t cnt, loff_t *ppos)
485 trace_parser_clear(parser);
487 ret = get_user(ch, ubuf++);
495 * The parser is not finished with the last write,
496 * continue reading the user input without skipping spaces.
499 /* skip white space */
500 while (cnt && isspace(ch)) {
501 ret = get_user(ch, ubuf++);
508 /* only spaces were written */
518 /* read the non-space input */
519 while (cnt && !isspace(ch)) {
520 if (parser->idx < parser->size - 1)
521 parser->buffer[parser->idx++] = ch;
526 ret = get_user(ch, ubuf++);
533 /* We either got finished input or we have to wait for another call. */
535 parser->buffer[parser->idx] = 0;
536 parser->cont = false;
539 parser->buffer[parser->idx++] = ch;
549 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
557 if (s->len <= s->readpos)
560 len = s->len - s->readpos;
563 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
573 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
578 if (s->len <= s->readpos)
581 len = s->len - s->readpos;
584 ret = memcpy(buf, s->buffer + s->readpos, cnt);
593 * ftrace_max_lock is used to protect the swapping of buffers
594 * when taking a max snapshot. The buffers themselves are
595 * protected by per_cpu spinlocks. But the action of the swap
596 * needs its own lock.
598 * This is defined as a arch_spinlock_t in order to help
599 * with performance when lockdep debugging is enabled.
601 * It is also used in other places outside the update_max_tr
602 * so it needs to be defined outside of the
603 * CONFIG_TRACER_MAX_TRACE.
605 static arch_spinlock_t ftrace_max_lock =
606 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
608 unsigned long __read_mostly tracing_thresh;
610 #ifdef CONFIG_TRACER_MAX_TRACE
611 unsigned long __read_mostly tracing_max_latency;
614 * Copy the new maximum trace into the separate maximum-trace
615 * structure. (this way the maximum trace is permanently saved,
616 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
619 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
621 struct trace_array_cpu *data = tr->data[cpu];
622 struct trace_array_cpu *max_data;
625 max_tr.time_start = data->preempt_timestamp;
627 max_data = max_tr.data[cpu];
628 max_data->saved_latency = tracing_max_latency;
629 max_data->critical_start = data->critical_start;
630 max_data->critical_end = data->critical_end;
632 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
633 max_data->pid = tsk->pid;
634 max_data->uid = task_uid(tsk);
635 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
636 max_data->policy = tsk->policy;
637 max_data->rt_priority = tsk->rt_priority;
639 /* record this tasks comm */
640 tracing_record_cmdline(tsk);
644 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
646 * @tsk: the task with the latency
647 * @cpu: The cpu that initiated the trace.
649 * Flip the buffers between the @tr and the max_tr and record information
650 * about which task was the cause of this latency.
653 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
655 struct ring_buffer *buf = tr->buffer;
657 if (trace_stop_count)
660 WARN_ON_ONCE(!irqs_disabled());
661 if (!current_trace->use_max_tr) {
665 arch_spin_lock(&ftrace_max_lock);
667 tr->buffer = max_tr.buffer;
670 __update_max_tr(tr, tsk, cpu);
671 arch_spin_unlock(&ftrace_max_lock);
675 * update_max_tr_single - only copy one trace over, and reset the rest
677 * @tsk - task with the latency
678 * @cpu - the cpu of the buffer to copy.
680 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
683 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
687 if (trace_stop_count)
690 WARN_ON_ONCE(!irqs_disabled());
691 if (!current_trace->use_max_tr) {
696 arch_spin_lock(&ftrace_max_lock);
698 ftrace_disable_cpu();
700 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
704 * We failed to swap the buffer due to a commit taking
705 * place on this CPU. We fail to record, but we reset
706 * the max trace buffer (no one writes directly to it)
707 * and flag that it failed.
709 trace_array_printk(&max_tr, _THIS_IP_,
710 "Failed to swap buffers due to commit in progress\n");
715 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
717 __update_max_tr(tr, tsk, cpu);
718 arch_spin_unlock(&ftrace_max_lock);
720 #endif /* CONFIG_TRACER_MAX_TRACE */
723 * register_tracer - register a tracer with the ftrace system.
724 * @type - the plugin for the tracer
726 * Register a new plugin tracer.
728 int register_tracer(struct tracer *type)
729 __releases(kernel_lock)
730 __acquires(kernel_lock)
736 pr_info("Tracer must have a name\n");
740 if (strlen(type->name) >= MAX_TRACER_SIZE) {
741 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
745 mutex_lock(&trace_types_lock);
747 tracing_selftest_running = true;
749 for (t = trace_types; t; t = t->next) {
750 if (strcmp(type->name, t->name) == 0) {
752 pr_info("Tracer %s already registered\n",
760 type->set_flag = &dummy_set_flag;
762 type->flags = &dummy_tracer_flags;
764 if (!type->flags->opts)
765 type->flags->opts = dummy_tracer_opt;
766 if (!type->wait_pipe)
767 type->wait_pipe = default_wait_pipe;
770 #ifdef CONFIG_FTRACE_STARTUP_TEST
771 if (type->selftest && !tracing_selftest_disabled) {
772 struct tracer *saved_tracer = current_trace;
773 struct trace_array *tr = &global_trace;
776 * Run a selftest on this tracer.
777 * Here we reset the trace buffer, and set the current
778 * tracer to be this tracer. The tracer can then run some
779 * internal tracing to verify that everything is in order.
780 * If we fail, we do not register this tracer.
782 tracing_reset_online_cpus(tr);
784 current_trace = type;
786 /* If we expanded the buffers, make sure the max is expanded too */
787 if (ring_buffer_expanded && type->use_max_tr)
788 ring_buffer_resize(max_tr.buffer, trace_buf_size);
790 /* the test is responsible for initializing and enabling */
791 pr_info("Testing tracer %s: ", type->name);
792 ret = type->selftest(type, tr);
793 /* the test is responsible for resetting too */
794 current_trace = saved_tracer;
796 printk(KERN_CONT "FAILED!\n");
799 /* Only reset on passing, to avoid touching corrupted buffers */
800 tracing_reset_online_cpus(tr);
802 /* Shrink the max buffer again */
803 if (ring_buffer_expanded && type->use_max_tr)
804 ring_buffer_resize(max_tr.buffer, 1);
806 printk(KERN_CONT "PASSED\n");
810 type->next = trace_types;
814 tracing_selftest_running = false;
815 mutex_unlock(&trace_types_lock);
817 if (ret || !default_bootup_tracer)
820 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
823 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
824 /* Do we want this tracer to start on bootup? */
825 tracing_set_tracer(type->name);
826 default_bootup_tracer = NULL;
827 /* disable other selftests, since this will break it. */
828 tracing_selftest_disabled = 1;
829 #ifdef CONFIG_FTRACE_STARTUP_TEST
830 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
838 void unregister_tracer(struct tracer *type)
842 mutex_lock(&trace_types_lock);
843 for (t = &trace_types; *t; t = &(*t)->next) {
847 pr_info("Tracer %s not registered\n", type->name);
853 if (type == current_trace && tracer_enabled) {
856 if (current_trace->stop)
857 current_trace->stop(&global_trace);
858 current_trace = &nop_trace;
861 mutex_unlock(&trace_types_lock);
864 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
866 ftrace_disable_cpu();
867 ring_buffer_reset_cpu(buffer, cpu);
871 void tracing_reset(struct trace_array *tr, int cpu)
873 struct ring_buffer *buffer = tr->buffer;
875 ring_buffer_record_disable(buffer);
877 /* Make sure all commits have finished */
879 __tracing_reset(buffer, cpu);
881 ring_buffer_record_enable(buffer);
884 void tracing_reset_online_cpus(struct trace_array *tr)
886 struct ring_buffer *buffer = tr->buffer;
889 ring_buffer_record_disable(buffer);
891 /* Make sure all commits have finished */
894 tr->time_start = ftrace_now(tr->cpu);
896 for_each_online_cpu(cpu)
897 __tracing_reset(buffer, cpu);
899 ring_buffer_record_enable(buffer);
902 void tracing_reset_current(int cpu)
904 tracing_reset(&global_trace, cpu);
907 void tracing_reset_current_online_cpus(void)
909 tracing_reset_online_cpus(&global_trace);
912 #define SAVED_CMDLINES 128
913 #define NO_CMDLINE_MAP UINT_MAX
914 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
915 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
916 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
917 static int cmdline_idx;
918 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
920 /* temporary disable recording */
921 static atomic_t trace_record_cmdline_disabled __read_mostly;
923 static void trace_init_cmdlines(void)
925 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
926 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
930 int is_tracing_stopped(void)
932 return trace_stop_count;
936 * ftrace_off_permanent - disable all ftrace code permanently
938 * This should only be called when a serious anomally has
939 * been detected. This will turn off the function tracing,
940 * ring buffers, and other tracing utilites. It takes no
941 * locks and can be called from any context.
943 void ftrace_off_permanent(void)
945 tracing_disabled = 1;
947 tracing_off_permanent();
951 * tracing_start - quick start of the tracer
953 * If tracing is enabled but was stopped by tracing_stop,
954 * this will start the tracer back up.
956 void tracing_start(void)
958 struct ring_buffer *buffer;
961 if (tracing_disabled)
964 raw_spin_lock_irqsave(&tracing_start_lock, flags);
965 if (--trace_stop_count) {
966 if (trace_stop_count < 0) {
967 /* Someone screwed up their debugging */
969 trace_stop_count = 0;
974 /* Prevent the buffers from switching */
975 arch_spin_lock(&ftrace_max_lock);
977 buffer = global_trace.buffer;
979 ring_buffer_record_enable(buffer);
981 buffer = max_tr.buffer;
983 ring_buffer_record_enable(buffer);
985 arch_spin_unlock(&ftrace_max_lock);
989 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
993 * tracing_stop - quick stop of the tracer
995 * Light weight way to stop tracing. Use in conjunction with
998 void tracing_stop(void)
1000 struct ring_buffer *buffer;
1001 unsigned long flags;
1004 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1005 if (trace_stop_count++)
1008 /* Prevent the buffers from switching */
1009 arch_spin_lock(&ftrace_max_lock);
1011 buffer = global_trace.buffer;
1013 ring_buffer_record_disable(buffer);
1015 buffer = max_tr.buffer;
1017 ring_buffer_record_disable(buffer);
1019 arch_spin_unlock(&ftrace_max_lock);
1022 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1025 void trace_stop_cmdline_recording(void);
1027 static void trace_save_cmdline(struct task_struct *tsk)
1031 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1035 * It's not the end of the world if we don't get
1036 * the lock, but we also don't want to spin
1037 * nor do we want to disable interrupts,
1038 * so if we miss here, then better luck next time.
1040 if (!arch_spin_trylock(&trace_cmdline_lock))
1043 idx = map_pid_to_cmdline[tsk->pid];
1044 if (idx == NO_CMDLINE_MAP) {
1045 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1048 * Check whether the cmdline buffer at idx has a pid
1049 * mapped. We are going to overwrite that entry so we
1050 * need to clear the map_pid_to_cmdline. Otherwise we
1051 * would read the new comm for the old pid.
1053 pid = map_cmdline_to_pid[idx];
1054 if (pid != NO_CMDLINE_MAP)
1055 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1057 map_cmdline_to_pid[idx] = tsk->pid;
1058 map_pid_to_cmdline[tsk->pid] = idx;
1063 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1065 arch_spin_unlock(&trace_cmdline_lock);
1068 void trace_find_cmdline(int pid, char comm[])
1073 strcpy(comm, "<idle>");
1077 if (WARN_ON_ONCE(pid < 0)) {
1078 strcpy(comm, "<XXX>");
1082 if (pid > PID_MAX_DEFAULT) {
1083 strcpy(comm, "<...>");
1088 arch_spin_lock(&trace_cmdline_lock);
1089 map = map_pid_to_cmdline[pid];
1090 if (map != NO_CMDLINE_MAP)
1091 strcpy(comm, saved_cmdlines[map]);
1093 strcpy(comm, "<...>");
1095 arch_spin_unlock(&trace_cmdline_lock);
1099 void tracing_record_cmdline(struct task_struct *tsk)
1101 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1105 trace_save_cmdline(tsk);
1109 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1112 struct task_struct *tsk = current;
1114 entry->preempt_count = pc & 0xff;
1115 entry->pid = (tsk) ? tsk->pid : 0;
1118 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1119 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1121 TRACE_FLAG_IRQS_NOSUPPORT |
1123 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1124 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1125 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1127 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1129 struct ring_buffer_event *
1130 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1133 unsigned long flags, int pc)
1135 struct ring_buffer_event *event;
1137 event = ring_buffer_lock_reserve(buffer, len);
1138 if (event != NULL) {
1139 struct trace_entry *ent = ring_buffer_event_data(event);
1141 tracing_generic_entry_update(ent, flags, pc);
1149 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1150 struct ring_buffer_event *event,
1151 unsigned long flags, int pc,
1154 ring_buffer_unlock_commit(buffer, event);
1156 ftrace_trace_stack(buffer, flags, 6, pc);
1157 ftrace_trace_userstack(buffer, flags, pc);
1163 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1164 struct ring_buffer_event *event,
1165 unsigned long flags, int pc)
1167 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1170 struct ring_buffer_event *
1171 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1172 int type, unsigned long len,
1173 unsigned long flags, int pc)
1175 *current_rb = global_trace.buffer;
1176 return trace_buffer_lock_reserve(*current_rb,
1177 type, len, flags, pc);
1179 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1181 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1182 struct ring_buffer_event *event,
1183 unsigned long flags, int pc)
1185 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1187 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1189 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1190 struct ring_buffer_event *event,
1191 unsigned long flags, int pc)
1193 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1195 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1197 void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1198 struct ring_buffer_event *event,
1199 unsigned long flags, int pc,
1200 struct pt_regs *regs)
1202 ring_buffer_unlock_commit(buffer, event);
1204 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1205 ftrace_trace_userstack(buffer, flags, pc);
1207 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1209 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1210 struct ring_buffer_event *event)
1212 ring_buffer_discard_commit(buffer, event);
1214 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1217 trace_function(struct trace_array *tr,
1218 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1221 struct ftrace_event_call *call = &event_function;
1222 struct ring_buffer *buffer = tr->buffer;
1223 struct ring_buffer_event *event;
1224 struct ftrace_entry *entry;
1226 /* If we are reading the ring buffer, don't trace */
1227 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1230 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1234 entry = ring_buffer_event_data(event);
1236 entry->parent_ip = parent_ip;
1238 if (!filter_check_discard(call, entry, buffer, event))
1239 ring_buffer_unlock_commit(buffer, event);
1243 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1244 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1247 if (likely(!atomic_read(&data->disabled)))
1248 trace_function(tr, ip, parent_ip, flags, pc);
1251 #ifdef CONFIG_STACKTRACE
1253 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1254 struct ftrace_stack {
1255 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1258 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1259 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1261 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1262 unsigned long flags,
1263 int skip, int pc, struct pt_regs *regs)
1265 struct ftrace_event_call *call = &event_kernel_stack;
1266 struct ring_buffer_event *event;
1267 struct stack_entry *entry;
1268 struct stack_trace trace;
1270 int size = FTRACE_STACK_ENTRIES;
1272 trace.nr_entries = 0;
1276 * Since events can happen in NMIs there's no safe way to
1277 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1278 * or NMI comes in, it will just have to use the default
1279 * FTRACE_STACK_SIZE.
1281 preempt_disable_notrace();
1283 use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1285 * We don't need any atomic variables, just a barrier.
1286 * If an interrupt comes in, we don't care, because it would
1287 * have exited and put the counter back to what we want.
1288 * We just need a barrier to keep gcc from moving things
1292 if (use_stack == 1) {
1293 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1294 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1297 save_stack_trace_regs(regs, &trace);
1299 save_stack_trace(&trace);
1301 if (trace.nr_entries > size)
1302 size = trace.nr_entries;
1304 /* From now on, use_stack is a boolean */
1307 size *= sizeof(unsigned long);
1309 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1310 sizeof(*entry) + size, flags, pc);
1313 entry = ring_buffer_event_data(event);
1315 memset(&entry->caller, 0, size);
1318 memcpy(&entry->caller, trace.entries,
1319 trace.nr_entries * sizeof(unsigned long));
1321 trace.max_entries = FTRACE_STACK_ENTRIES;
1322 trace.entries = entry->caller;
1324 save_stack_trace_regs(regs, &trace);
1326 save_stack_trace(&trace);
1329 entry->size = trace.nr_entries;
1331 if (!filter_check_discard(call, entry, buffer, event))
1332 ring_buffer_unlock_commit(buffer, event);
1335 /* Again, don't let gcc optimize things here */
1337 __get_cpu_var(ftrace_stack_reserve)--;
1338 preempt_enable_notrace();
1342 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1343 int skip, int pc, struct pt_regs *regs)
1345 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1348 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1351 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1354 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1357 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1360 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1363 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1367 * trace_dump_stack - record a stack back trace in the trace buffer
1369 void trace_dump_stack(void)
1371 unsigned long flags;
1373 if (tracing_disabled || tracing_selftest_running)
1376 local_save_flags(flags);
1378 /* skipping 3 traces, seems to get us at the caller of this function */
1379 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1382 static DEFINE_PER_CPU(int, user_stack_count);
1385 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1387 struct ftrace_event_call *call = &event_user_stack;
1388 struct ring_buffer_event *event;
1389 struct userstack_entry *entry;
1390 struct stack_trace trace;
1392 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1396 * NMIs can not handle page faults, even with fix ups.
1397 * The save user stack can (and often does) fault.
1399 if (unlikely(in_nmi()))
1403 * prevent recursion, since the user stack tracing may
1404 * trigger other kernel events.
1407 if (__this_cpu_read(user_stack_count))
1410 __this_cpu_inc(user_stack_count);
1412 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1413 sizeof(*entry), flags, pc);
1415 goto out_drop_count;
1416 entry = ring_buffer_event_data(event);
1418 entry->tgid = current->tgid;
1419 memset(&entry->caller, 0, sizeof(entry->caller));
1421 trace.nr_entries = 0;
1422 trace.max_entries = FTRACE_STACK_ENTRIES;
1424 trace.entries = entry->caller;
1426 save_stack_trace_user(&trace);
1427 if (!filter_check_discard(call, entry, buffer, event))
1428 ring_buffer_unlock_commit(buffer, event);
1431 __this_cpu_dec(user_stack_count);
1437 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1439 ftrace_trace_userstack(tr, flags, preempt_count());
1443 #endif /* CONFIG_STACKTRACE */
1446 * trace_vbprintk - write binary msg to tracing buffer
1449 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1451 static arch_spinlock_t trace_buf_lock =
1452 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1453 static u32 trace_buf[TRACE_BUF_SIZE];
1455 struct ftrace_event_call *call = &event_bprint;
1456 struct ring_buffer_event *event;
1457 struct ring_buffer *buffer;
1458 struct trace_array *tr = &global_trace;
1459 struct trace_array_cpu *data;
1460 struct bprint_entry *entry;
1461 unsigned long flags;
1463 int cpu, len = 0, size, pc;
1465 if (unlikely(tracing_selftest_running || tracing_disabled))
1468 /* Don't pollute graph traces with trace_vprintk internals */
1469 pause_graph_tracing();
1471 pc = preempt_count();
1472 preempt_disable_notrace();
1473 cpu = raw_smp_processor_id();
1474 data = tr->data[cpu];
1476 disable = atomic_inc_return(&data->disabled);
1477 if (unlikely(disable != 1))
1480 /* Lockdep uses trace_printk for lock tracing */
1481 local_irq_save(flags);
1482 arch_spin_lock(&trace_buf_lock);
1483 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1485 if (len > TRACE_BUF_SIZE || len < 0)
1488 size = sizeof(*entry) + sizeof(u32) * len;
1489 buffer = tr->buffer;
1490 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1494 entry = ring_buffer_event_data(event);
1498 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1499 if (!filter_check_discard(call, entry, buffer, event)) {
1500 ring_buffer_unlock_commit(buffer, event);
1501 ftrace_trace_stack(buffer, flags, 6, pc);
1505 arch_spin_unlock(&trace_buf_lock);
1506 local_irq_restore(flags);
1509 atomic_dec_return(&data->disabled);
1510 preempt_enable_notrace();
1511 unpause_graph_tracing();
1515 EXPORT_SYMBOL_GPL(trace_vbprintk);
1517 int trace_array_printk(struct trace_array *tr,
1518 unsigned long ip, const char *fmt, ...)
1523 if (!(trace_flags & TRACE_ITER_PRINTK))
1527 ret = trace_array_vprintk(tr, ip, fmt, ap);
1532 int trace_array_vprintk(struct trace_array *tr,
1533 unsigned long ip, const char *fmt, va_list args)
1535 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1536 static char trace_buf[TRACE_BUF_SIZE];
1538 struct ftrace_event_call *call = &event_print;
1539 struct ring_buffer_event *event;
1540 struct ring_buffer *buffer;
1541 struct trace_array_cpu *data;
1542 int cpu, len = 0, size, pc;
1543 struct print_entry *entry;
1544 unsigned long irq_flags;
1547 if (tracing_disabled || tracing_selftest_running)
1550 pc = preempt_count();
1551 preempt_disable_notrace();
1552 cpu = raw_smp_processor_id();
1553 data = tr->data[cpu];
1555 disable = atomic_inc_return(&data->disabled);
1556 if (unlikely(disable != 1))
1559 pause_graph_tracing();
1560 raw_local_irq_save(irq_flags);
1561 arch_spin_lock(&trace_buf_lock);
1562 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1564 size = sizeof(*entry) + len + 1;
1565 buffer = tr->buffer;
1566 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1570 entry = ring_buffer_event_data(event);
1573 memcpy(&entry->buf, trace_buf, len);
1574 entry->buf[len] = '\0';
1575 if (!filter_check_discard(call, entry, buffer, event)) {
1576 ring_buffer_unlock_commit(buffer, event);
1577 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1581 arch_spin_unlock(&trace_buf_lock);
1582 raw_local_irq_restore(irq_flags);
1583 unpause_graph_tracing();
1585 atomic_dec_return(&data->disabled);
1586 preempt_enable_notrace();
1591 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1593 return trace_array_vprintk(&global_trace, ip, fmt, args);
1595 EXPORT_SYMBOL_GPL(trace_vprintk);
1597 static void trace_iterator_increment(struct trace_iterator *iter)
1599 /* Don't allow ftrace to trace into the ring buffers */
1600 ftrace_disable_cpu();
1603 if (iter->buffer_iter[iter->cpu])
1604 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1606 ftrace_enable_cpu();
1609 static struct trace_entry *
1610 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1611 unsigned long *lost_events)
1613 struct ring_buffer_event *event;
1614 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1616 /* Don't allow ftrace to trace into the ring buffers */
1617 ftrace_disable_cpu();
1620 event = ring_buffer_iter_peek(buf_iter, ts);
1622 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1625 ftrace_enable_cpu();
1628 iter->ent_size = ring_buffer_event_length(event);
1629 return ring_buffer_event_data(event);
1635 static struct trace_entry *
1636 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1637 unsigned long *missing_events, u64 *ent_ts)
1639 struct ring_buffer *buffer = iter->tr->buffer;
1640 struct trace_entry *ent, *next = NULL;
1641 unsigned long lost_events = 0, next_lost = 0;
1642 int cpu_file = iter->cpu_file;
1643 u64 next_ts = 0, ts;
1649 * If we are in a per_cpu trace file, don't bother by iterating over
1650 * all cpu and peek directly.
1652 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1653 if (ring_buffer_empty_cpu(buffer, cpu_file))
1655 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1657 *ent_cpu = cpu_file;
1662 for_each_tracing_cpu(cpu) {
1664 if (ring_buffer_empty_cpu(buffer, cpu))
1667 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1670 * Pick the entry with the smallest timestamp:
1672 if (ent && (!next || ts < next_ts)) {
1676 next_lost = lost_events;
1677 next_size = iter->ent_size;
1681 iter->ent_size = next_size;
1684 *ent_cpu = next_cpu;
1690 *missing_events = next_lost;
1695 /* Find the next real entry, without updating the iterator itself */
1696 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1697 int *ent_cpu, u64 *ent_ts)
1699 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1702 /* Find the next real entry, and increment the iterator to the next entry */
1703 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1705 iter->ent = __find_next_entry(iter, &iter->cpu,
1706 &iter->lost_events, &iter->ts);
1709 trace_iterator_increment(iter);
1711 return iter->ent ? iter : NULL;
1714 static void trace_consume(struct trace_iterator *iter)
1716 /* Don't allow ftrace to trace into the ring buffers */
1717 ftrace_disable_cpu();
1718 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1719 &iter->lost_events);
1720 ftrace_enable_cpu();
1723 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1725 struct trace_iterator *iter = m->private;
1729 WARN_ON_ONCE(iter->leftover);
1733 /* can't go backwards */
1738 ent = trace_find_next_entry_inc(iter);
1742 while (ent && iter->idx < i)
1743 ent = trace_find_next_entry_inc(iter);
1750 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1752 struct trace_array *tr = iter->tr;
1753 struct ring_buffer_event *event;
1754 struct ring_buffer_iter *buf_iter;
1755 unsigned long entries = 0;
1758 tr->data[cpu]->skipped_entries = 0;
1760 if (!iter->buffer_iter[cpu])
1763 buf_iter = iter->buffer_iter[cpu];
1764 ring_buffer_iter_reset(buf_iter);
1767 * We could have the case with the max latency tracers
1768 * that a reset never took place on a cpu. This is evident
1769 * by the timestamp being before the start of the buffer.
1771 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1772 if (ts >= iter->tr->time_start)
1775 ring_buffer_read(buf_iter, NULL);
1778 tr->data[cpu]->skipped_entries = entries;
1782 * The current tracer is copied to avoid a global locking
1785 static void *s_start(struct seq_file *m, loff_t *pos)
1787 struct trace_iterator *iter = m->private;
1788 static struct tracer *old_tracer;
1789 int cpu_file = iter->cpu_file;
1794 /* copy the tracer to avoid using a global lock all around */
1795 mutex_lock(&trace_types_lock);
1796 if (unlikely(old_tracer != current_trace && current_trace)) {
1797 old_tracer = current_trace;
1798 *iter->trace = *current_trace;
1800 mutex_unlock(&trace_types_lock);
1802 atomic_inc(&trace_record_cmdline_disabled);
1804 if (*pos != iter->pos) {
1809 ftrace_disable_cpu();
1811 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1812 for_each_tracing_cpu(cpu)
1813 tracing_iter_reset(iter, cpu);
1815 tracing_iter_reset(iter, cpu_file);
1817 ftrace_enable_cpu();
1820 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1825 * If we overflowed the seq_file before, then we want
1826 * to just reuse the trace_seq buffer again.
1832 p = s_next(m, p, &l);
1836 trace_event_read_lock();
1837 trace_access_lock(cpu_file);
1841 static void s_stop(struct seq_file *m, void *p)
1843 struct trace_iterator *iter = m->private;
1845 atomic_dec(&trace_record_cmdline_disabled);
1846 trace_access_unlock(iter->cpu_file);
1847 trace_event_read_unlock();
1850 static void print_lat_help_header(struct seq_file *m)
1852 seq_puts(m, "# _------=> CPU# \n");
1853 seq_puts(m, "# / _-----=> irqs-off \n");
1854 seq_puts(m, "# | / _----=> need-resched \n");
1855 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1856 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1857 seq_puts(m, "# |||| / delay \n");
1858 seq_puts(m, "# cmd pid ||||| time | caller \n");
1859 seq_puts(m, "# \\ / ||||| \\ | / \n");
1862 static void print_func_help_header(struct seq_file *m)
1864 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1865 seq_puts(m, "# | | | | |\n");
1870 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1872 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1873 struct trace_array *tr = iter->tr;
1874 struct trace_array_cpu *data = tr->data[tr->cpu];
1875 struct tracer *type = current_trace;
1876 unsigned long entries = 0;
1877 unsigned long total = 0;
1878 unsigned long count;
1879 const char *name = "preemption";
1886 for_each_tracing_cpu(cpu) {
1887 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1889 * If this buffer has skipped entries, then we hold all
1890 * entries for the trace and we need to ignore the
1891 * ones before the time stamp.
1893 if (tr->data[cpu]->skipped_entries) {
1894 count -= tr->data[cpu]->skipped_entries;
1895 /* total is the same as the entries */
1899 ring_buffer_overrun_cpu(tr->buffer, cpu);
1903 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1905 seq_puts(m, "# -----------------------------------"
1906 "---------------------------------\n");
1907 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1908 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1909 nsecs_to_usecs(data->saved_latency),
1913 #if defined(CONFIG_PREEMPT_NONE)
1915 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1917 #elif defined(CONFIG_PREEMPT)
1922 /* These are reserved for later use */
1925 seq_printf(m, " #P:%d)\n", num_online_cpus());
1929 seq_puts(m, "# -----------------\n");
1930 seq_printf(m, "# | task: %.16s-%d "
1931 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1932 data->comm, data->pid, data->uid, data->nice,
1933 data->policy, data->rt_priority);
1934 seq_puts(m, "# -----------------\n");
1936 if (data->critical_start) {
1937 seq_puts(m, "# => started at: ");
1938 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1939 trace_print_seq(m, &iter->seq);
1940 seq_puts(m, "\n# => ended at: ");
1941 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1942 trace_print_seq(m, &iter->seq);
1943 seq_puts(m, "\n#\n");
1949 static void test_cpu_buff_start(struct trace_iterator *iter)
1951 struct trace_seq *s = &iter->seq;
1953 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1956 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1959 if (cpumask_test_cpu(iter->cpu, iter->started))
1962 if (iter->tr->data[iter->cpu]->skipped_entries)
1965 cpumask_set_cpu(iter->cpu, iter->started);
1967 /* Don't print started cpu buffer for the first entry of the trace */
1969 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1973 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1975 struct trace_seq *s = &iter->seq;
1976 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1977 struct trace_entry *entry;
1978 struct trace_event *event;
1982 test_cpu_buff_start(iter);
1984 event = ftrace_find_event(entry->type);
1986 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1987 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1988 if (!trace_print_lat_context(iter))
1991 if (!trace_print_context(iter))
1997 return event->funcs->trace(iter, sym_flags, event);
1999 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2002 return TRACE_TYPE_HANDLED;
2004 return TRACE_TYPE_PARTIAL_LINE;
2007 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2009 struct trace_seq *s = &iter->seq;
2010 struct trace_entry *entry;
2011 struct trace_event *event;
2015 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2016 if (!trace_seq_printf(s, "%d %d %llu ",
2017 entry->pid, iter->cpu, iter->ts))
2021 event = ftrace_find_event(entry->type);
2023 return event->funcs->raw(iter, 0, event);
2025 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2028 return TRACE_TYPE_HANDLED;
2030 return TRACE_TYPE_PARTIAL_LINE;
2033 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2035 struct trace_seq *s = &iter->seq;
2036 unsigned char newline = '\n';
2037 struct trace_entry *entry;
2038 struct trace_event *event;
2042 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2043 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2044 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2045 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2048 event = ftrace_find_event(entry->type);
2050 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2051 if (ret != TRACE_TYPE_HANDLED)
2055 SEQ_PUT_FIELD_RET(s, newline);
2057 return TRACE_TYPE_HANDLED;
2060 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2062 struct trace_seq *s = &iter->seq;
2063 struct trace_entry *entry;
2064 struct trace_event *event;
2068 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2069 SEQ_PUT_FIELD_RET(s, entry->pid);
2070 SEQ_PUT_FIELD_RET(s, iter->cpu);
2071 SEQ_PUT_FIELD_RET(s, iter->ts);
2074 event = ftrace_find_event(entry->type);
2075 return event ? event->funcs->binary(iter, 0, event) :
2079 int trace_empty(struct trace_iterator *iter)
2083 /* If we are looking at one CPU buffer, only check that one */
2084 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2085 cpu = iter->cpu_file;
2086 if (iter->buffer_iter[cpu]) {
2087 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2090 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2096 for_each_tracing_cpu(cpu) {
2097 if (iter->buffer_iter[cpu]) {
2098 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2101 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2109 /* Called with trace_event_read_lock() held. */
2110 enum print_line_t print_trace_line(struct trace_iterator *iter)
2112 enum print_line_t ret;
2114 if (iter->lost_events &&
2115 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2116 iter->cpu, iter->lost_events))
2117 return TRACE_TYPE_PARTIAL_LINE;
2119 if (iter->trace && iter->trace->print_line) {
2120 ret = iter->trace->print_line(iter);
2121 if (ret != TRACE_TYPE_UNHANDLED)
2125 if (iter->ent->type == TRACE_BPRINT &&
2126 trace_flags & TRACE_ITER_PRINTK &&
2127 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2128 return trace_print_bprintk_msg_only(iter);
2130 if (iter->ent->type == TRACE_PRINT &&
2131 trace_flags & TRACE_ITER_PRINTK &&
2132 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2133 return trace_print_printk_msg_only(iter);
2135 if (trace_flags & TRACE_ITER_BIN)
2136 return print_bin_fmt(iter);
2138 if (trace_flags & TRACE_ITER_HEX)
2139 return print_hex_fmt(iter);
2141 if (trace_flags & TRACE_ITER_RAW)
2142 return print_raw_fmt(iter);
2144 return print_trace_fmt(iter);
2147 void trace_default_header(struct seq_file *m)
2149 struct trace_iterator *iter = m->private;
2151 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2154 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2155 /* print nothing if the buffers are empty */
2156 if (trace_empty(iter))
2158 print_trace_header(m, iter);
2159 if (!(trace_flags & TRACE_ITER_VERBOSE))
2160 print_lat_help_header(m);
2162 if (!(trace_flags & TRACE_ITER_VERBOSE))
2163 print_func_help_header(m);
2167 static void test_ftrace_alive(struct seq_file *m)
2169 if (!ftrace_is_dead())
2171 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2172 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2175 static int s_show(struct seq_file *m, void *v)
2177 struct trace_iterator *iter = v;
2180 if (iter->ent == NULL) {
2182 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2184 test_ftrace_alive(m);
2186 if (iter->trace && iter->trace->print_header)
2187 iter->trace->print_header(m);
2189 trace_default_header(m);
2191 } else if (iter->leftover) {
2193 * If we filled the seq_file buffer earlier, we
2194 * want to just show it now.
2196 ret = trace_print_seq(m, &iter->seq);
2198 /* ret should this time be zero, but you never know */
2199 iter->leftover = ret;
2202 print_trace_line(iter);
2203 ret = trace_print_seq(m, &iter->seq);
2205 * If we overflow the seq_file buffer, then it will
2206 * ask us for this data again at start up.
2208 * ret is 0 if seq_file write succeeded.
2211 iter->leftover = ret;
2217 static const struct seq_operations tracer_seq_ops = {
2224 static struct trace_iterator *
2225 __tracing_open(struct inode *inode, struct file *file)
2227 long cpu_file = (long) inode->i_private;
2228 void *fail_ret = ERR_PTR(-ENOMEM);
2229 struct trace_iterator *iter;
2233 if (tracing_disabled)
2234 return ERR_PTR(-ENODEV);
2236 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2238 return ERR_PTR(-ENOMEM);
2241 * We make a copy of the current tracer to avoid concurrent
2242 * changes on it while we are reading.
2244 mutex_lock(&trace_types_lock);
2245 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2250 *iter->trace = *current_trace;
2252 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2255 if (current_trace && current_trace->print_max)
2258 iter->tr = &global_trace;
2260 mutex_init(&iter->mutex);
2261 iter->cpu_file = cpu_file;
2263 /* Notify the tracer early; before we stop tracing. */
2264 if (iter->trace && iter->trace->open)
2265 iter->trace->open(iter);
2267 /* Annotate start of buffers if we had overruns */
2268 if (ring_buffer_overruns(iter->tr->buffer))
2269 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2271 /* stop the trace while dumping */
2274 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2275 for_each_tracing_cpu(cpu) {
2276 iter->buffer_iter[cpu] =
2277 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2279 ring_buffer_read_prepare_sync();
2280 for_each_tracing_cpu(cpu) {
2281 ring_buffer_read_start(iter->buffer_iter[cpu]);
2282 tracing_iter_reset(iter, cpu);
2285 cpu = iter->cpu_file;
2286 iter->buffer_iter[cpu] =
2287 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2288 ring_buffer_read_prepare_sync();
2289 ring_buffer_read_start(iter->buffer_iter[cpu]);
2290 tracing_iter_reset(iter, cpu);
2293 ret = seq_open(file, &tracer_seq_ops);
2295 fail_ret = ERR_PTR(ret);
2299 m = file->private_data;
2302 mutex_unlock(&trace_types_lock);
2307 for_each_tracing_cpu(cpu) {
2308 if (iter->buffer_iter[cpu])
2309 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2311 free_cpumask_var(iter->started);
2314 mutex_unlock(&trace_types_lock);
2321 int tracing_open_generic(struct inode *inode, struct file *filp)
2323 if (tracing_disabled)
2326 filp->private_data = inode->i_private;
2330 static int tracing_release(struct inode *inode, struct file *file)
2332 struct seq_file *m = file->private_data;
2333 struct trace_iterator *iter;
2336 if (!(file->f_mode & FMODE_READ))
2341 mutex_lock(&trace_types_lock);
2342 for_each_tracing_cpu(cpu) {
2343 if (iter->buffer_iter[cpu])
2344 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2347 if (iter->trace && iter->trace->close)
2348 iter->trace->close(iter);
2350 /* reenable tracing if it was previously enabled */
2352 mutex_unlock(&trace_types_lock);
2354 seq_release(inode, file);
2355 mutex_destroy(&iter->mutex);
2356 free_cpumask_var(iter->started);
2362 static int tracing_open(struct inode *inode, struct file *file)
2364 struct trace_iterator *iter;
2367 /* If this file was open for write, then erase contents */
2368 if ((file->f_mode & FMODE_WRITE) &&
2369 (file->f_flags & O_TRUNC)) {
2370 long cpu = (long) inode->i_private;
2372 if (cpu == TRACE_PIPE_ALL_CPU)
2373 tracing_reset_online_cpus(&global_trace);
2375 tracing_reset(&global_trace, cpu);
2378 if (file->f_mode & FMODE_READ) {
2379 iter = __tracing_open(inode, file);
2381 ret = PTR_ERR(iter);
2382 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2383 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2389 t_next(struct seq_file *m, void *v, loff_t *pos)
2391 struct tracer *t = v;
2401 static void *t_start(struct seq_file *m, loff_t *pos)
2406 mutex_lock(&trace_types_lock);
2407 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2413 static void t_stop(struct seq_file *m, void *p)
2415 mutex_unlock(&trace_types_lock);
2418 static int t_show(struct seq_file *m, void *v)
2420 struct tracer *t = v;
2425 seq_printf(m, "%s", t->name);
2434 static const struct seq_operations show_traces_seq_ops = {
2441 static int show_traces_open(struct inode *inode, struct file *file)
2443 if (tracing_disabled)
2446 return seq_open(file, &show_traces_seq_ops);
2450 tracing_write_stub(struct file *filp, const char __user *ubuf,
2451 size_t count, loff_t *ppos)
2456 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2458 if (file->f_mode & FMODE_READ)
2459 return seq_lseek(file, offset, origin);
2464 static const struct file_operations tracing_fops = {
2465 .open = tracing_open,
2467 .write = tracing_write_stub,
2468 .llseek = tracing_seek,
2469 .release = tracing_release,
2472 static const struct file_operations show_traces_fops = {
2473 .open = show_traces_open,
2475 .release = seq_release,
2476 .llseek = seq_lseek,
2480 * Only trace on a CPU if the bitmask is set:
2482 static cpumask_var_t tracing_cpumask;
2485 * The tracer itself will not take this lock, but still we want
2486 * to provide a consistent cpumask to user-space:
2488 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2491 * Temporary storage for the character representation of the
2492 * CPU bitmask (and one more byte for the newline):
2494 static char mask_str[NR_CPUS + 1];
2497 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2498 size_t count, loff_t *ppos)
2502 mutex_lock(&tracing_cpumask_update_lock);
2504 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2505 if (count - len < 2) {
2509 len += sprintf(mask_str + len, "\n");
2510 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2513 mutex_unlock(&tracing_cpumask_update_lock);
2519 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2520 size_t count, loff_t *ppos)
2523 cpumask_var_t tracing_cpumask_new;
2525 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2528 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2532 mutex_lock(&tracing_cpumask_update_lock);
2534 local_irq_disable();
2535 arch_spin_lock(&ftrace_max_lock);
2536 for_each_tracing_cpu(cpu) {
2538 * Increase/decrease the disabled counter if we are
2539 * about to flip a bit in the cpumask:
2541 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2542 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2543 atomic_inc(&global_trace.data[cpu]->disabled);
2544 ring_buffer_record_disable_cpu(global_trace.buffer, cpu);
2546 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2547 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2548 atomic_dec(&global_trace.data[cpu]->disabled);
2549 ring_buffer_record_enable_cpu(global_trace.buffer, cpu);
2552 arch_spin_unlock(&ftrace_max_lock);
2555 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2557 mutex_unlock(&tracing_cpumask_update_lock);
2558 free_cpumask_var(tracing_cpumask_new);
2563 free_cpumask_var(tracing_cpumask_new);
2568 static const struct file_operations tracing_cpumask_fops = {
2569 .open = tracing_open_generic,
2570 .read = tracing_cpumask_read,
2571 .write = tracing_cpumask_write,
2572 .llseek = generic_file_llseek,
2575 static int tracing_trace_options_show(struct seq_file *m, void *v)
2577 struct tracer_opt *trace_opts;
2581 mutex_lock(&trace_types_lock);
2582 tracer_flags = current_trace->flags->val;
2583 trace_opts = current_trace->flags->opts;
2585 for (i = 0; trace_options[i]; i++) {
2586 if (trace_flags & (1 << i))
2587 seq_printf(m, "%s\n", trace_options[i]);
2589 seq_printf(m, "no%s\n", trace_options[i]);
2592 for (i = 0; trace_opts[i].name; i++) {
2593 if (tracer_flags & trace_opts[i].bit)
2594 seq_printf(m, "%s\n", trace_opts[i].name);
2596 seq_printf(m, "no%s\n", trace_opts[i].name);
2598 mutex_unlock(&trace_types_lock);
2603 static int __set_tracer_option(struct tracer *trace,
2604 struct tracer_flags *tracer_flags,
2605 struct tracer_opt *opts, int neg)
2609 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2614 tracer_flags->val &= ~opts->bit;
2616 tracer_flags->val |= opts->bit;
2620 /* Try to assign a tracer specific option */
2621 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2623 struct tracer_flags *tracer_flags = trace->flags;
2624 struct tracer_opt *opts = NULL;
2627 for (i = 0; tracer_flags->opts[i].name; i++) {
2628 opts = &tracer_flags->opts[i];
2630 if (strcmp(cmp, opts->name) == 0)
2631 return __set_tracer_option(trace, trace->flags,
2638 static void set_tracer_flags(unsigned int mask, int enabled)
2640 /* do nothing if flag is already set */
2641 if (!!(trace_flags & mask) == !!enabled)
2645 trace_flags |= mask;
2647 trace_flags &= ~mask;
2649 if (mask == TRACE_ITER_RECORD_CMD)
2650 trace_event_enable_cmd_record(enabled);
2652 if (mask == TRACE_ITER_OVERWRITE)
2653 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2657 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2658 size_t cnt, loff_t *ppos)
2666 if (cnt >= sizeof(buf))
2669 if (copy_from_user(&buf, ubuf, cnt))
2673 cmp = strstrip(buf);
2675 if (strncmp(cmp, "no", 2) == 0) {
2680 for (i = 0; trace_options[i]; i++) {
2681 if (strcmp(cmp, trace_options[i]) == 0) {
2682 set_tracer_flags(1 << i, !neg);
2687 /* If no option could be set, test the specific tracer options */
2688 if (!trace_options[i]) {
2689 mutex_lock(&trace_types_lock);
2690 ret = set_tracer_option(current_trace, cmp, neg);
2691 mutex_unlock(&trace_types_lock);
2701 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2703 if (tracing_disabled)
2705 return single_open(file, tracing_trace_options_show, NULL);
2708 static const struct file_operations tracing_iter_fops = {
2709 .open = tracing_trace_options_open,
2711 .llseek = seq_lseek,
2712 .release = single_release,
2713 .write = tracing_trace_options_write,
2716 static const char readme_msg[] =
2717 "tracing mini-HOWTO:\n\n"
2718 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2719 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2720 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2721 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2723 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2724 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2726 "# cat /sys/kernel/debug/tracing/trace_options\n"
2727 "noprint-parent nosym-offset nosym-addr noverbose\n"
2728 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2729 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2730 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2731 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2735 tracing_readme_read(struct file *filp, char __user *ubuf,
2736 size_t cnt, loff_t *ppos)
2738 return simple_read_from_buffer(ubuf, cnt, ppos,
2739 readme_msg, strlen(readme_msg));
2742 static const struct file_operations tracing_readme_fops = {
2743 .open = tracing_open_generic,
2744 .read = tracing_readme_read,
2745 .llseek = generic_file_llseek,
2749 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2750 size_t cnt, loff_t *ppos)
2759 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2763 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2771 for (i = 0; i < SAVED_CMDLINES; i++) {
2774 pid = map_cmdline_to_pid[i];
2775 if (pid == -1 || pid == NO_CMDLINE_MAP)
2778 trace_find_cmdline(pid, buf_comm);
2779 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2784 len = simple_read_from_buffer(ubuf, cnt, ppos,
2793 static const struct file_operations tracing_saved_cmdlines_fops = {
2794 .open = tracing_open_generic,
2795 .read = tracing_saved_cmdlines_read,
2796 .llseek = generic_file_llseek,
2800 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2801 size_t cnt, loff_t *ppos)
2806 r = sprintf(buf, "%u\n", tracer_enabled);
2807 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2811 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2812 size_t cnt, loff_t *ppos)
2814 struct trace_array *tr = filp->private_data;
2818 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
2824 mutex_lock(&trace_types_lock);
2825 if (tracer_enabled ^ val) {
2827 /* Only need to warn if this is used to change the state */
2828 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2832 if (current_trace->start)
2833 current_trace->start(tr);
2838 if (current_trace->stop)
2839 current_trace->stop(tr);
2842 mutex_unlock(&trace_types_lock);
2850 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2851 size_t cnt, loff_t *ppos)
2853 char buf[MAX_TRACER_SIZE+2];
2856 mutex_lock(&trace_types_lock);
2858 r = sprintf(buf, "%s\n", current_trace->name);
2860 r = sprintf(buf, "\n");
2861 mutex_unlock(&trace_types_lock);
2863 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2866 int tracer_init(struct tracer *t, struct trace_array *tr)
2868 tracing_reset_online_cpus(tr);
2872 static int __tracing_resize_ring_buffer(unsigned long size)
2877 * If kernel or user changes the size of the ring buffer
2878 * we use the size that was given, and we can forget about
2879 * expanding it later.
2881 ring_buffer_expanded = 1;
2883 ret = ring_buffer_resize(global_trace.buffer, size);
2887 if (!current_trace->use_max_tr)
2890 ret = ring_buffer_resize(max_tr.buffer, size);
2894 r = ring_buffer_resize(global_trace.buffer,
2895 global_trace.entries);
2898 * AARGH! We are left with different
2899 * size max buffer!!!!
2900 * The max buffer is our "snapshot" buffer.
2901 * When a tracer needs a snapshot (one of the
2902 * latency tracers), it swaps the max buffer
2903 * with the saved snap shot. We succeeded to
2904 * update the size of the main buffer, but failed to
2905 * update the size of the max buffer. But when we tried
2906 * to reset the main buffer to the original size, we
2907 * failed there too. This is very unlikely to
2908 * happen, but if it does, warn and kill all
2912 tracing_disabled = 1;
2917 max_tr.entries = size;
2919 global_trace.entries = size;
2924 static ssize_t tracing_resize_ring_buffer(unsigned long size)
2926 int cpu, ret = size;
2928 mutex_lock(&trace_types_lock);
2932 /* disable all cpu buffers */
2933 for_each_tracing_cpu(cpu) {
2934 if (global_trace.data[cpu])
2935 atomic_inc(&global_trace.data[cpu]->disabled);
2936 if (max_tr.data[cpu])
2937 atomic_inc(&max_tr.data[cpu]->disabled);
2940 if (size != global_trace.entries)
2941 ret = __tracing_resize_ring_buffer(size);
2946 for_each_tracing_cpu(cpu) {
2947 if (global_trace.data[cpu])
2948 atomic_dec(&global_trace.data[cpu]->disabled);
2949 if (max_tr.data[cpu])
2950 atomic_dec(&max_tr.data[cpu]->disabled);
2954 mutex_unlock(&trace_types_lock);
2961 * tracing_update_buffers - used by tracing facility to expand ring buffers
2963 * To save on memory when the tracing is never used on a system with it
2964 * configured in. The ring buffers are set to a minimum size. But once
2965 * a user starts to use the tracing facility, then they need to grow
2966 * to their default size.
2968 * This function is to be called when a tracer is about to be used.
2970 int tracing_update_buffers(void)
2974 mutex_lock(&trace_types_lock);
2975 if (!ring_buffer_expanded)
2976 ret = __tracing_resize_ring_buffer(trace_buf_size);
2977 mutex_unlock(&trace_types_lock);
2982 struct trace_option_dentry;
2984 static struct trace_option_dentry *
2985 create_trace_option_files(struct tracer *tracer);
2988 destroy_trace_option_files(struct trace_option_dentry *topts);
2990 static int tracing_set_tracer(const char *buf)
2992 static struct trace_option_dentry *topts;
2993 struct trace_array *tr = &global_trace;
2997 mutex_lock(&trace_types_lock);
2999 if (!ring_buffer_expanded) {
3000 ret = __tracing_resize_ring_buffer(trace_buf_size);
3006 for (t = trace_types; t; t = t->next) {
3007 if (strcmp(t->name, buf) == 0)
3014 if (t == current_trace)
3017 trace_branch_disable();
3018 if (current_trace && current_trace->reset)
3019 current_trace->reset(tr);
3020 if (current_trace && current_trace->use_max_tr) {
3022 * We don't free the ring buffer. instead, resize it because
3023 * The max_tr ring buffer has some state (e.g. ring->clock) and
3024 * we want preserve it.
3026 ring_buffer_resize(max_tr.buffer, 1);
3029 destroy_trace_option_files(topts);
3033 topts = create_trace_option_files(current_trace);
3034 if (current_trace->use_max_tr) {
3035 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
3038 max_tr.entries = global_trace.entries;
3042 ret = tracer_init(t, tr);
3047 trace_branch_enable(tr);
3049 mutex_unlock(&trace_types_lock);
3055 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3056 size_t cnt, loff_t *ppos)
3058 char buf[MAX_TRACER_SIZE+1];
3065 if (cnt > MAX_TRACER_SIZE)
3066 cnt = MAX_TRACER_SIZE;
3068 if (copy_from_user(&buf, ubuf, cnt))
3073 /* strip ending whitespace. */
3074 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3077 err = tracing_set_tracer(buf);
3087 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3088 size_t cnt, loff_t *ppos)
3090 unsigned long *ptr = filp->private_data;
3094 r = snprintf(buf, sizeof(buf), "%ld\n",
3095 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3096 if (r > sizeof(buf))
3098 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3102 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3103 size_t cnt, loff_t *ppos)
3105 unsigned long *ptr = filp->private_data;
3109 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3118 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3120 long cpu_file = (long) inode->i_private;
3121 struct trace_iterator *iter;
3124 if (tracing_disabled)
3127 mutex_lock(&trace_types_lock);
3129 /* create a buffer to store the information to pass to userspace */
3130 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3137 * We make a copy of the current tracer to avoid concurrent
3138 * changes on it while we are reading.
3140 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3146 *iter->trace = *current_trace;
3148 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3153 /* trace pipe does not show start of buffer */
3154 cpumask_setall(iter->started);
3156 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3157 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3159 iter->cpu_file = cpu_file;
3160 iter->tr = &global_trace;
3161 mutex_init(&iter->mutex);
3162 filp->private_data = iter;
3164 if (iter->trace->pipe_open)
3165 iter->trace->pipe_open(iter);
3167 nonseekable_open(inode, filp);
3169 mutex_unlock(&trace_types_lock);
3175 mutex_unlock(&trace_types_lock);
3179 static int tracing_release_pipe(struct inode *inode, struct file *file)
3181 struct trace_iterator *iter = file->private_data;
3183 mutex_lock(&trace_types_lock);
3185 if (iter->trace->pipe_close)
3186 iter->trace->pipe_close(iter);
3188 mutex_unlock(&trace_types_lock);
3190 free_cpumask_var(iter->started);
3191 mutex_destroy(&iter->mutex);
3199 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3201 struct trace_iterator *iter = filp->private_data;
3203 if (trace_flags & TRACE_ITER_BLOCK) {
3205 * Always select as readable when in blocking mode
3207 return POLLIN | POLLRDNORM;
3209 if (!trace_empty(iter))
3210 return POLLIN | POLLRDNORM;
3211 poll_wait(filp, &trace_wait, poll_table);
3212 if (!trace_empty(iter))
3213 return POLLIN | POLLRDNORM;
3220 void default_wait_pipe(struct trace_iterator *iter)
3224 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3226 if (trace_empty(iter))
3229 finish_wait(&trace_wait, &wait);
3233 * This is a make-shift waitqueue.
3234 * A tracer might use this callback on some rare cases:
3236 * 1) the current tracer might hold the runqueue lock when it wakes up
3237 * a reader, hence a deadlock (sched, function, and function graph tracers)
3238 * 2) the function tracers, trace all functions, we don't want
3239 * the overhead of calling wake_up and friends
3240 * (and tracing them too)
3242 * Anyway, this is really very primitive wakeup.
3244 void poll_wait_pipe(struct trace_iterator *iter)
3246 set_current_state(TASK_INTERRUPTIBLE);
3247 /* sleep for 100 msecs, and try again. */
3248 schedule_timeout(HZ / 10);
3251 /* Must be called with trace_types_lock mutex held. */
3252 static int tracing_wait_pipe(struct file *filp)
3254 struct trace_iterator *iter = filp->private_data;
3256 while (trace_empty(iter)) {
3258 if ((filp->f_flags & O_NONBLOCK)) {
3262 mutex_unlock(&iter->mutex);
3264 iter->trace->wait_pipe(iter);
3266 mutex_lock(&iter->mutex);
3268 if (signal_pending(current))
3272 * We block until we read something and tracing is disabled.
3273 * We still block if tracing is disabled, but we have never
3274 * read anything. This allows a user to cat this file, and
3275 * then enable tracing. But after we have read something,
3276 * we give an EOF when tracing is again disabled.
3278 * iter->pos will be 0 if we haven't read anything.
3280 if (!tracer_enabled && iter->pos)
3291 tracing_read_pipe(struct file *filp, char __user *ubuf,
3292 size_t cnt, loff_t *ppos)
3294 struct trace_iterator *iter = filp->private_data;
3295 static struct tracer *old_tracer;
3298 /* return any leftover data */
3299 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3303 trace_seq_init(&iter->seq);
3305 /* copy the tracer to avoid using a global lock all around */
3306 mutex_lock(&trace_types_lock);
3307 if (unlikely(old_tracer != current_trace && current_trace)) {
3308 old_tracer = current_trace;
3309 *iter->trace = *current_trace;
3311 mutex_unlock(&trace_types_lock);
3314 * Avoid more than one consumer on a single file descriptor
3315 * This is just a matter of traces coherency, the ring buffer itself
3318 mutex_lock(&iter->mutex);
3319 if (iter->trace->read) {
3320 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3326 sret = tracing_wait_pipe(filp);
3330 /* stop when tracing is finished */
3331 if (trace_empty(iter)) {
3336 if (cnt >= PAGE_SIZE)
3337 cnt = PAGE_SIZE - 1;
3339 /* reset all but tr, trace, and overruns */
3340 memset(&iter->seq, 0,
3341 sizeof(struct trace_iterator) -
3342 offsetof(struct trace_iterator, seq));
3345 trace_event_read_lock();
3346 trace_access_lock(iter->cpu_file);
3347 while (trace_find_next_entry_inc(iter) != NULL) {
3348 enum print_line_t ret;
3349 int len = iter->seq.len;
3351 ret = print_trace_line(iter);
3352 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3353 /* don't print partial lines */
3354 iter->seq.len = len;
3357 if (ret != TRACE_TYPE_NO_CONSUME)
3358 trace_consume(iter);
3360 if (iter->seq.len >= cnt)
3364 * Setting the full flag means we reached the trace_seq buffer
3365 * size and we should leave by partial output condition above.
3366 * One of the trace_seq_* functions is not used properly.
3368 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3371 trace_access_unlock(iter->cpu_file);
3372 trace_event_read_unlock();
3374 /* Now copy what we have to the user */
3375 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3376 if (iter->seq.readpos >= iter->seq.len)
3377 trace_seq_init(&iter->seq);
3380 * If there was nothing to send to user, in spite of consuming trace
3381 * entries, go back to wait for more entries.
3387 mutex_unlock(&iter->mutex);
3392 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3393 struct pipe_buffer *buf)
3395 __free_page(buf->page);
3398 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3401 __free_page(spd->pages[idx]);
3404 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3406 .map = generic_pipe_buf_map,
3407 .unmap = generic_pipe_buf_unmap,
3408 .confirm = generic_pipe_buf_confirm,
3409 .release = tracing_pipe_buf_release,
3410 .steal = generic_pipe_buf_steal,
3411 .get = generic_pipe_buf_get,
3415 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3420 /* Seq buffer is page-sized, exactly what we need. */
3422 count = iter->seq.len;
3423 ret = print_trace_line(iter);
3424 count = iter->seq.len - count;
3427 iter->seq.len -= count;
3430 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3431 iter->seq.len -= count;
3435 if (ret != TRACE_TYPE_NO_CONSUME)
3436 trace_consume(iter);
3438 if (!trace_find_next_entry_inc(iter)) {
3448 static ssize_t tracing_splice_read_pipe(struct file *filp,
3450 struct pipe_inode_info *pipe,
3454 struct page *pages_def[PIPE_DEF_BUFFERS];
3455 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3456 struct trace_iterator *iter = filp->private_data;
3457 struct splice_pipe_desc spd = {
3459 .partial = partial_def,
3460 .nr_pages = 0, /* This gets updated below. */
3461 .nr_pages_max = PIPE_DEF_BUFFERS,
3463 .ops = &tracing_pipe_buf_ops,
3464 .spd_release = tracing_spd_release_pipe,
3466 static struct tracer *old_tracer;
3471 if (splice_grow_spd(pipe, &spd))
3474 /* copy the tracer to avoid using a global lock all around */
3475 mutex_lock(&trace_types_lock);
3476 if (unlikely(old_tracer != current_trace && current_trace)) {
3477 old_tracer = current_trace;
3478 *iter->trace = *current_trace;
3480 mutex_unlock(&trace_types_lock);
3482 mutex_lock(&iter->mutex);
3484 if (iter->trace->splice_read) {
3485 ret = iter->trace->splice_read(iter, filp,
3486 ppos, pipe, len, flags);
3491 ret = tracing_wait_pipe(filp);
3495 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3500 trace_event_read_lock();
3501 trace_access_lock(iter->cpu_file);
3503 /* Fill as many pages as possible. */
3504 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3505 spd.pages[i] = alloc_page(GFP_KERNEL);
3509 rem = tracing_fill_pipe_page(rem, iter);
3511 /* Copy the data into the page, so we can start over. */
3512 ret = trace_seq_to_buffer(&iter->seq,
3513 page_address(spd.pages[i]),
3516 __free_page(spd.pages[i]);
3519 spd.partial[i].offset = 0;
3520 spd.partial[i].len = iter->seq.len;
3522 trace_seq_init(&iter->seq);
3525 trace_access_unlock(iter->cpu_file);
3526 trace_event_read_unlock();
3527 mutex_unlock(&iter->mutex);
3531 ret = splice_to_pipe(pipe, &spd);
3533 splice_shrink_spd(&spd);
3537 mutex_unlock(&iter->mutex);
3542 tracing_entries_read(struct file *filp, char __user *ubuf,
3543 size_t cnt, loff_t *ppos)
3545 struct trace_array *tr = filp->private_data;
3549 mutex_lock(&trace_types_lock);
3550 if (!ring_buffer_expanded)
3551 r = sprintf(buf, "%lu (expanded: %lu)\n",
3553 trace_buf_size >> 10);
3555 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3556 mutex_unlock(&trace_types_lock);
3558 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3562 tracing_entries_write(struct file *filp, const char __user *ubuf,
3563 size_t cnt, loff_t *ppos)
3568 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3572 /* must have at least 1 entry */
3576 /* value is in KB */
3579 ret = tracing_resize_ring_buffer(val);
3589 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3590 size_t cnt, loff_t *ppos)
3592 struct trace_array *tr = filp->private_data;
3595 unsigned long size = 0, expanded_size = 0;
3597 mutex_lock(&trace_types_lock);
3598 for_each_tracing_cpu(cpu) {
3599 size += tr->entries >> 10;
3600 if (!ring_buffer_expanded)
3601 expanded_size += trace_buf_size >> 10;
3603 if (ring_buffer_expanded)
3604 r = sprintf(buf, "%lu\n", size);
3606 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3607 mutex_unlock(&trace_types_lock);
3609 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3613 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3614 size_t cnt, loff_t *ppos)
3617 * There is no need to read what the user has written, this function
3618 * is just to make sure that there is no error when "echo" is used
3627 tracing_free_buffer_release(struct inode *inode, struct file *filp)
3629 /* disable tracing ? */
3630 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3632 /* resize the ring buffer to 0 */
3633 tracing_resize_ring_buffer(0);
3639 tracing_mark_write(struct file *filp, const char __user *ubuf,
3640 size_t cnt, loff_t *fpos)
3642 unsigned long addr = (unsigned long)ubuf;
3643 struct ring_buffer_event *event;
3644 struct ring_buffer *buffer;
3645 struct print_entry *entry;
3646 unsigned long irq_flags;
3647 struct page *pages[2];
3657 if (tracing_disabled)
3660 if (cnt > TRACE_BUF_SIZE)
3661 cnt = TRACE_BUF_SIZE;
3664 * Userspace is injecting traces into the kernel trace buffer.
3665 * We want to be as non intrusive as possible.
3666 * To do so, we do not want to allocate any special buffers
3667 * or take any locks, but instead write the userspace data
3668 * straight into the ring buffer.
3670 * First we need to pin the userspace buffer into memory,
3671 * which, most likely it is, because it just referenced it.
3672 * But there's no guarantee that it is. By using get_user_pages_fast()
3673 * and kmap_atomic/kunmap_atomic() we can get access to the
3674 * pages directly. We then write the data directly into the
3677 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
3679 /* check if we cross pages */
3680 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
3683 offset = addr & (PAGE_SIZE - 1);
3686 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
3687 if (ret < nr_pages) {
3689 put_page(pages[ret]);
3694 page1 = kmap_atomic(pages[0]);
3696 page2 = kmap_atomic(pages[1]);
3698 local_save_flags(irq_flags);
3699 size = sizeof(*entry) + cnt + 2; /* possible \n added */
3700 buffer = global_trace.buffer;
3701 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3702 irq_flags, preempt_count());
3704 /* Ring buffer disabled, return as if not open for write */
3709 entry = ring_buffer_event_data(event);
3710 entry->ip = _THIS_IP_;
3712 if (nr_pages == 2) {
3713 len = PAGE_SIZE - offset;
3714 memcpy(&entry->buf, page1 + offset, len);
3715 memcpy(&entry->buf[len], page2, cnt - len);
3717 memcpy(&entry->buf, page1 + offset, cnt);
3719 if (entry->buf[cnt - 1] != '\n') {
3720 entry->buf[cnt] = '\n';
3721 entry->buf[cnt + 1] = '\0';
3723 entry->buf[cnt] = '\0';
3725 ring_buffer_unlock_commit(buffer, event);
3733 kunmap_atomic(page2);
3734 kunmap_atomic(page1);
3735 while (nr_pages > 0)
3736 put_page(pages[--nr_pages]);
3741 static int tracing_clock_show(struct seq_file *m, void *v)
3745 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3747 "%s%s%s%s", i ? " " : "",
3748 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3749 i == trace_clock_id ? "]" : "");
3755 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3756 size_t cnt, loff_t *fpos)
3759 const char *clockstr;
3762 if (cnt >= sizeof(buf))
3765 if (copy_from_user(&buf, ubuf, cnt))
3770 clockstr = strstrip(buf);
3772 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3773 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3776 if (i == ARRAY_SIZE(trace_clocks))
3781 mutex_lock(&trace_types_lock);
3783 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3785 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3787 mutex_unlock(&trace_types_lock);
3794 static int tracing_clock_open(struct inode *inode, struct file *file)
3796 if (tracing_disabled)
3798 return single_open(file, tracing_clock_show, NULL);
3801 static const struct file_operations tracing_max_lat_fops = {
3802 .open = tracing_open_generic,
3803 .read = tracing_max_lat_read,
3804 .write = tracing_max_lat_write,
3805 .llseek = generic_file_llseek,
3808 static const struct file_operations tracing_ctrl_fops = {
3809 .open = tracing_open_generic,
3810 .read = tracing_ctrl_read,
3811 .write = tracing_ctrl_write,
3812 .llseek = generic_file_llseek,
3815 static const struct file_operations set_tracer_fops = {
3816 .open = tracing_open_generic,
3817 .read = tracing_set_trace_read,
3818 .write = tracing_set_trace_write,
3819 .llseek = generic_file_llseek,
3822 static const struct file_operations tracing_pipe_fops = {
3823 .open = tracing_open_pipe,
3824 .poll = tracing_poll_pipe,
3825 .read = tracing_read_pipe,
3826 .splice_read = tracing_splice_read_pipe,
3827 .release = tracing_release_pipe,
3828 .llseek = no_llseek,
3831 static const struct file_operations tracing_entries_fops = {
3832 .open = tracing_open_generic,
3833 .read = tracing_entries_read,
3834 .write = tracing_entries_write,
3835 .llseek = generic_file_llseek,
3838 static const struct file_operations tracing_total_entries_fops = {
3839 .open = tracing_open_generic,
3840 .read = tracing_total_entries_read,
3841 .llseek = generic_file_llseek,
3844 static const struct file_operations tracing_free_buffer_fops = {
3845 .write = tracing_free_buffer_write,
3846 .release = tracing_free_buffer_release,
3849 static const struct file_operations tracing_mark_fops = {
3850 .open = tracing_open_generic,
3851 .write = tracing_mark_write,
3852 .llseek = generic_file_llseek,
3855 static const struct file_operations trace_clock_fops = {
3856 .open = tracing_clock_open,
3858 .llseek = seq_lseek,
3859 .release = single_release,
3860 .write = tracing_clock_write,
3863 struct ftrace_buffer_info {
3864 struct trace_array *tr;
3870 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3872 int cpu = (int)(long)inode->i_private;
3873 struct ftrace_buffer_info *info;
3875 if (tracing_disabled)
3878 info = kzalloc(sizeof(*info), GFP_KERNEL);
3882 info->tr = &global_trace;
3885 /* Force reading ring buffer for first read */
3886 info->read = (unsigned int)-1;
3888 filp->private_data = info;
3890 return nonseekable_open(inode, filp);
3894 tracing_buffers_read(struct file *filp, char __user *ubuf,
3895 size_t count, loff_t *ppos)
3897 struct ftrace_buffer_info *info = filp->private_data;
3905 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
3909 /* Do we have previous read data to read? */
3910 if (info->read < PAGE_SIZE)
3913 trace_access_lock(info->cpu);
3914 ret = ring_buffer_read_page(info->tr->buffer,
3918 trace_access_unlock(info->cpu);
3925 size = PAGE_SIZE - info->read;
3929 ret = copy_to_user(ubuf, info->spare + info->read, size);
3940 static int tracing_buffers_release(struct inode *inode, struct file *file)
3942 struct ftrace_buffer_info *info = file->private_data;
3945 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3952 struct ring_buffer *buffer;
3957 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3958 struct pipe_buffer *buf)
3960 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3965 ring_buffer_free_read_page(ref->buffer, ref->page);
3970 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3971 struct pipe_buffer *buf)
3976 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3977 struct pipe_buffer *buf)
3979 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3984 /* Pipe buffer operations for a buffer. */
3985 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
3987 .map = generic_pipe_buf_map,
3988 .unmap = generic_pipe_buf_unmap,
3989 .confirm = generic_pipe_buf_confirm,
3990 .release = buffer_pipe_buf_release,
3991 .steal = buffer_pipe_buf_steal,
3992 .get = buffer_pipe_buf_get,
3996 * Callback from splice_to_pipe(), if we need to release some pages
3997 * at the end of the spd in case we error'ed out in filling the pipe.
3999 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4001 struct buffer_ref *ref =
4002 (struct buffer_ref *)spd->partial[i].private;
4007 ring_buffer_free_read_page(ref->buffer, ref->page);
4009 spd->partial[i].private = 0;
4013 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4014 struct pipe_inode_info *pipe, size_t len,
4017 struct ftrace_buffer_info *info = file->private_data;
4018 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4019 struct page *pages_def[PIPE_DEF_BUFFERS];
4020 struct splice_pipe_desc spd = {
4022 .partial = partial_def,
4023 .nr_pages_max = PIPE_DEF_BUFFERS,
4025 .ops = &buffer_pipe_buf_ops,
4026 .spd_release = buffer_spd_release,
4028 struct buffer_ref *ref;
4029 int entries, size, i;
4032 if (splice_grow_spd(pipe, &spd))
4035 if (*ppos & (PAGE_SIZE - 1)) {
4036 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
4041 if (len & (PAGE_SIZE - 1)) {
4042 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
4043 if (len < PAGE_SIZE) {
4050 trace_access_lock(info->cpu);
4051 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4053 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4057 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4062 ref->buffer = info->tr->buffer;
4063 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
4069 r = ring_buffer_read_page(ref->buffer, &ref->page,
4072 ring_buffer_free_read_page(ref->buffer, ref->page);
4078 * zero out any left over data, this is going to
4081 size = ring_buffer_page_len(ref->page);
4082 if (size < PAGE_SIZE)
4083 memset(ref->page + size, 0, PAGE_SIZE - size);
4085 page = virt_to_page(ref->page);
4087 spd.pages[i] = page;
4088 spd.partial[i].len = PAGE_SIZE;
4089 spd.partial[i].offset = 0;
4090 spd.partial[i].private = (unsigned long)ref;
4094 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4097 trace_access_unlock(info->cpu);
4100 /* did we read anything? */
4101 if (!spd.nr_pages) {
4102 if (flags & SPLICE_F_NONBLOCK)
4110 ret = splice_to_pipe(pipe, &spd);
4111 splice_shrink_spd(&spd);
4116 static const struct file_operations tracing_buffers_fops = {
4117 .open = tracing_buffers_open,
4118 .read = tracing_buffers_read,
4119 .release = tracing_buffers_release,
4120 .splice_read = tracing_buffers_splice_read,
4121 .llseek = no_llseek,
4125 tracing_stats_read(struct file *filp, char __user *ubuf,
4126 size_t count, loff_t *ppos)
4128 unsigned long cpu = (unsigned long)filp->private_data;
4129 struct trace_array *tr = &global_trace;
4130 struct trace_seq *s;
4132 unsigned long long t;
4133 unsigned long usec_rem;
4135 s = kmalloc(sizeof(*s), GFP_KERNEL);
4141 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4142 trace_seq_printf(s, "entries: %ld\n", cnt);
4144 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4145 trace_seq_printf(s, "overrun: %ld\n", cnt);
4147 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4148 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4150 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4151 trace_seq_printf(s, "bytes: %ld\n", cnt);
4153 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4154 usec_rem = do_div(t, USEC_PER_SEC);
4155 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
4157 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4158 usec_rem = do_div(t, USEC_PER_SEC);
4159 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4161 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4168 static const struct file_operations tracing_stats_fops = {
4169 .open = tracing_open_generic,
4170 .read = tracing_stats_read,
4171 .llseek = generic_file_llseek,
4174 #ifdef CONFIG_DYNAMIC_FTRACE
4176 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4182 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4183 size_t cnt, loff_t *ppos)
4185 static char ftrace_dyn_info_buffer[1024];
4186 static DEFINE_MUTEX(dyn_info_mutex);
4187 unsigned long *p = filp->private_data;
4188 char *buf = ftrace_dyn_info_buffer;
4189 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4192 mutex_lock(&dyn_info_mutex);
4193 r = sprintf(buf, "%ld ", *p);
4195 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4198 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4200 mutex_unlock(&dyn_info_mutex);
4205 static const struct file_operations tracing_dyn_info_fops = {
4206 .open = tracing_open_generic,
4207 .read = tracing_read_dyn_info,
4208 .llseek = generic_file_llseek,
4212 static struct dentry *d_tracer;
4214 struct dentry *tracing_init_dentry(void)
4221 if (!debugfs_initialized())
4224 d_tracer = debugfs_create_dir("tracing", NULL);
4226 if (!d_tracer && !once) {
4228 pr_warning("Could not create debugfs directory 'tracing'\n");
4235 static struct dentry *d_percpu;
4237 struct dentry *tracing_dentry_percpu(void)
4240 struct dentry *d_tracer;
4245 d_tracer = tracing_init_dentry();
4250 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4252 if (!d_percpu && !once) {
4254 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4261 static void tracing_init_debugfs_percpu(long cpu)
4263 struct dentry *d_percpu = tracing_dentry_percpu();
4264 struct dentry *d_cpu;
4265 char cpu_dir[30]; /* 30 characters should be more than enough */
4267 snprintf(cpu_dir, 30, "cpu%ld", cpu);
4268 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4270 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4274 /* per cpu trace_pipe */
4275 trace_create_file("trace_pipe", 0444, d_cpu,
4276 (void *) cpu, &tracing_pipe_fops);
4279 trace_create_file("trace", 0644, d_cpu,
4280 (void *) cpu, &tracing_fops);
4282 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4283 (void *) cpu, &tracing_buffers_fops);
4285 trace_create_file("stats", 0444, d_cpu,
4286 (void *) cpu, &tracing_stats_fops);
4289 #ifdef CONFIG_FTRACE_SELFTEST
4290 /* Let selftest have access to static functions in this file */
4291 #include "trace_selftest.c"
4294 struct trace_option_dentry {
4295 struct tracer_opt *opt;
4296 struct tracer_flags *flags;
4297 struct dentry *entry;
4301 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4304 struct trace_option_dentry *topt = filp->private_data;
4307 if (topt->flags->val & topt->opt->bit)
4312 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4316 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4319 struct trace_option_dentry *topt = filp->private_data;
4323 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4327 if (val != 0 && val != 1)
4330 if (!!(topt->flags->val & topt->opt->bit) != val) {
4331 mutex_lock(&trace_types_lock);
4332 ret = __set_tracer_option(current_trace, topt->flags,
4334 mutex_unlock(&trace_types_lock);
4345 static const struct file_operations trace_options_fops = {
4346 .open = tracing_open_generic,
4347 .read = trace_options_read,
4348 .write = trace_options_write,
4349 .llseek = generic_file_llseek,
4353 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4356 long index = (long)filp->private_data;
4359 if (trace_flags & (1 << index))
4364 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4368 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4371 long index = (long)filp->private_data;
4375 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4379 if (val != 0 && val != 1)
4381 set_tracer_flags(1 << index, val);
4388 static const struct file_operations trace_options_core_fops = {
4389 .open = tracing_open_generic,
4390 .read = trace_options_core_read,
4391 .write = trace_options_core_write,
4392 .llseek = generic_file_llseek,
4395 struct dentry *trace_create_file(const char *name,
4397 struct dentry *parent,
4399 const struct file_operations *fops)
4403 ret = debugfs_create_file(name, mode, parent, data, fops);
4405 pr_warning("Could not create debugfs '%s' entry\n", name);
4411 static struct dentry *trace_options_init_dentry(void)
4413 struct dentry *d_tracer;
4414 static struct dentry *t_options;
4419 d_tracer = tracing_init_dentry();
4423 t_options = debugfs_create_dir("options", d_tracer);
4425 pr_warning("Could not create debugfs directory 'options'\n");
4433 create_trace_option_file(struct trace_option_dentry *topt,
4434 struct tracer_flags *flags,
4435 struct tracer_opt *opt)
4437 struct dentry *t_options;
4439 t_options = trace_options_init_dentry();
4443 topt->flags = flags;
4446 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4447 &trace_options_fops);
4451 static struct trace_option_dentry *
4452 create_trace_option_files(struct tracer *tracer)
4454 struct trace_option_dentry *topts;
4455 struct tracer_flags *flags;
4456 struct tracer_opt *opts;
4462 flags = tracer->flags;
4464 if (!flags || !flags->opts)
4469 for (cnt = 0; opts[cnt].name; cnt++)
4472 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4476 for (cnt = 0; opts[cnt].name; cnt++)
4477 create_trace_option_file(&topts[cnt], flags,
4484 destroy_trace_option_files(struct trace_option_dentry *topts)
4491 for (cnt = 0; topts[cnt].opt; cnt++) {
4492 if (topts[cnt].entry)
4493 debugfs_remove(topts[cnt].entry);
4499 static struct dentry *
4500 create_trace_option_core_file(const char *option, long index)
4502 struct dentry *t_options;
4504 t_options = trace_options_init_dentry();
4508 return trace_create_file(option, 0644, t_options, (void *)index,
4509 &trace_options_core_fops);
4512 static __init void create_trace_options_dir(void)
4514 struct dentry *t_options;
4517 t_options = trace_options_init_dentry();
4521 for (i = 0; trace_options[i]; i++)
4522 create_trace_option_core_file(trace_options[i], i);
4525 static __init int tracer_init_debugfs(void)
4527 struct dentry *d_tracer;
4530 trace_access_lock_init();
4532 d_tracer = tracing_init_dentry();
4534 trace_create_file("tracing_enabled", 0644, d_tracer,
4535 &global_trace, &tracing_ctrl_fops);
4537 trace_create_file("trace_options", 0644, d_tracer,
4538 NULL, &tracing_iter_fops);
4540 trace_create_file("tracing_cpumask", 0644, d_tracer,
4541 NULL, &tracing_cpumask_fops);
4543 trace_create_file("trace", 0644, d_tracer,
4544 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4546 trace_create_file("available_tracers", 0444, d_tracer,
4547 &global_trace, &show_traces_fops);
4549 trace_create_file("current_tracer", 0644, d_tracer,
4550 &global_trace, &set_tracer_fops);
4552 #ifdef CONFIG_TRACER_MAX_TRACE
4553 trace_create_file("tracing_max_latency", 0644, d_tracer,
4554 &tracing_max_latency, &tracing_max_lat_fops);
4557 trace_create_file("tracing_thresh", 0644, d_tracer,
4558 &tracing_thresh, &tracing_max_lat_fops);
4560 trace_create_file("README", 0444, d_tracer,
4561 NULL, &tracing_readme_fops);
4563 trace_create_file("trace_pipe", 0444, d_tracer,
4564 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4566 trace_create_file("buffer_size_kb", 0644, d_tracer,
4567 &global_trace, &tracing_entries_fops);
4569 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
4570 &global_trace, &tracing_total_entries_fops);
4572 trace_create_file("free_buffer", 0644, d_tracer,
4573 &global_trace, &tracing_free_buffer_fops);
4575 trace_create_file("trace_marker", 0220, d_tracer,
4576 NULL, &tracing_mark_fops);
4578 trace_create_file("saved_cmdlines", 0444, d_tracer,
4579 NULL, &tracing_saved_cmdlines_fops);
4581 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4584 #ifdef CONFIG_DYNAMIC_FTRACE
4585 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4586 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4589 create_trace_options_dir();
4591 for_each_tracing_cpu(cpu)
4592 tracing_init_debugfs_percpu(cpu);
4597 static int trace_panic_handler(struct notifier_block *this,
4598 unsigned long event, void *unused)
4600 if (ftrace_dump_on_oops)
4601 ftrace_dump(ftrace_dump_on_oops);
4605 static struct notifier_block trace_panic_notifier = {
4606 .notifier_call = trace_panic_handler,
4608 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4611 static int trace_die_handler(struct notifier_block *self,
4617 if (ftrace_dump_on_oops)
4618 ftrace_dump(ftrace_dump_on_oops);
4626 static struct notifier_block trace_die_notifier = {
4627 .notifier_call = trace_die_handler,
4632 * printk is set to max of 1024, we really don't need it that big.
4633 * Nothing should be printing 1000 characters anyway.
4635 #define TRACE_MAX_PRINT 1000
4638 * Define here KERN_TRACE so that we have one place to modify
4639 * it if we decide to change what log level the ftrace dump
4642 #define KERN_TRACE KERN_EMERG
4645 trace_printk_seq(struct trace_seq *s)
4647 /* Probably should print a warning here. */
4651 /* should be zero ended, but we are paranoid. */
4652 s->buffer[s->len] = 0;
4654 printk(KERN_TRACE "%s", s->buffer);
4659 void trace_init_global_iter(struct trace_iterator *iter)
4661 iter->tr = &global_trace;
4662 iter->trace = current_trace;
4663 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4667 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
4669 static arch_spinlock_t ftrace_dump_lock =
4670 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
4671 /* use static because iter can be a bit big for the stack */
4672 static struct trace_iterator iter;
4673 unsigned int old_userobj;
4674 static int dump_ran;
4675 unsigned long flags;
4679 local_irq_save(flags);
4680 arch_spin_lock(&ftrace_dump_lock);
4688 /* Did function tracer already get disabled? */
4689 if (ftrace_is_dead()) {
4690 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
4691 printk("# MAY BE MISSING FUNCTION EVENTS\n");
4694 if (disable_tracing)
4697 trace_init_global_iter(&iter);
4699 for_each_tracing_cpu(cpu) {
4700 atomic_inc(&iter.tr->data[cpu]->disabled);
4703 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4705 /* don't look at user memory in panic mode */
4706 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4708 /* Simulate the iterator */
4709 iter.tr = &global_trace;
4710 iter.trace = current_trace;
4712 switch (oops_dump_mode) {
4714 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4717 iter.cpu_file = raw_smp_processor_id();
4722 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4723 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4726 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4729 * We need to stop all tracing on all CPUS to read the
4730 * the next buffer. This is a bit expensive, but is
4731 * not done often. We fill all what we can read,
4732 * and then release the locks again.
4735 while (!trace_empty(&iter)) {
4738 printk(KERN_TRACE "---------------------------------\n");
4742 /* reset all but tr, trace, and overruns */
4743 memset(&iter.seq, 0,
4744 sizeof(struct trace_iterator) -
4745 offsetof(struct trace_iterator, seq));
4746 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4749 if (trace_find_next_entry_inc(&iter) != NULL) {
4752 ret = print_trace_line(&iter);
4753 if (ret != TRACE_TYPE_NO_CONSUME)
4754 trace_consume(&iter);
4757 trace_printk_seq(&iter.seq);
4761 printk(KERN_TRACE " (ftrace buffer empty)\n");
4763 printk(KERN_TRACE "---------------------------------\n");
4766 /* Re-enable tracing if requested */
4767 if (!disable_tracing) {
4768 trace_flags |= old_userobj;
4770 for_each_tracing_cpu(cpu) {
4771 atomic_dec(&iter.tr->data[cpu]->disabled);
4777 arch_spin_unlock(&ftrace_dump_lock);
4778 local_irq_restore(flags);
4781 /* By default: disable tracing after the dump */
4782 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
4784 __ftrace_dump(true, oops_dump_mode);
4787 __init static int tracer_alloc_buffers(void)
4790 enum ring_buffer_flags rb_flags;
4795 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4798 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4799 goto out_free_buffer_mask;
4801 /* To save memory, keep the ring buffer size to its minimum */
4802 if (ring_buffer_expanded)
4803 ring_buf_size = trace_buf_size;
4807 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
4809 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4810 cpumask_copy(tracing_cpumask, cpu_all_mask);
4812 /* TODO: make the number of buffers hot pluggable with CPUS */
4813 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
4814 if (!global_trace.buffer) {
4815 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4817 goto out_free_cpumask;
4819 global_trace.entries = ring_buffer_size(global_trace.buffer);
4822 #ifdef CONFIG_TRACER_MAX_TRACE
4823 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
4824 if (!max_tr.buffer) {
4825 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4827 ring_buffer_free(global_trace.buffer);
4828 goto out_free_cpumask;
4833 /* Allocate the first page for all buffers */
4834 for_each_tracing_cpu(i) {
4835 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4836 max_tr.data[i] = &per_cpu(max_tr_data, i);
4839 trace_init_cmdlines();
4841 register_tracer(&nop_trace);
4842 current_trace = &nop_trace;
4843 /* All seems OK, enable tracing */
4844 tracing_disabled = 0;
4846 atomic_notifier_chain_register(&panic_notifier_list,
4847 &trace_panic_notifier);
4849 register_die_notifier(&trace_die_notifier);
4854 free_cpumask_var(tracing_cpumask);
4855 out_free_buffer_mask:
4856 free_cpumask_var(tracing_buffer_mask);
4861 __init static int clear_boot_tracer(void)
4864 * The default tracer at boot buffer is an init section.
4865 * This function is called in lateinit. If we did not
4866 * find the boot tracer, then clear it out, to prevent
4867 * later registration from accessing the buffer that is
4868 * about to be freed.
4870 if (!default_bootup_tracer)
4873 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4874 default_bootup_tracer);
4875 default_bootup_tracer = NULL;
4880 early_initcall(tracer_alloc_buffers);
4881 fs_initcall(tracer_init_debugfs);
4882 late_initcall(clear_boot_tracer);