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/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/rwsem.h>
36 #include <linux/slab.h>
37 #include <linux/ctype.h>
38 #include <linux/init.h>
39 #include <linux/poll.h>
43 #include "trace_output.h"
45 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
48 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
51 int ring_buffer_expanded;
54 * We need to change this state when a selftest is running.
55 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
57 * insertions into the ring-buffer such as trace_printk could occurred
58 * at the same time, giving false positive or negative results.
60 static bool __read_mostly tracing_selftest_running;
63 * If a tracer is running, we do not want to run SELFTEST.
65 bool __read_mostly tracing_selftest_disabled;
67 /* For tracers that don't implement custom flags */
68 static struct tracer_opt dummy_tracer_opt[] = {
72 static struct tracer_flags dummy_tracer_flags = {
74 .opts = dummy_tracer_opt
77 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
83 * Kill all tracing for good (never come back).
84 * It is initialized to 1 but will turn to zero if the initialization
85 * of the tracer is successful. But that is the only place that sets
88 static int tracing_disabled = 1;
90 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
92 static inline void ftrace_disable_cpu(void)
95 __this_cpu_inc(ftrace_cpu_disabled);
98 static inline void ftrace_enable_cpu(void)
100 __this_cpu_dec(ftrace_cpu_disabled);
104 cpumask_var_t __read_mostly tracing_buffer_mask;
107 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
109 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
110 * is set, then ftrace_dump is called. This will output the contents
111 * of the ftrace buffers to the console. This is very useful for
112 * capturing traces that lead to crashes and outputing it to a
115 * It is default off, but you can enable it with either specifying
116 * "ftrace_dump_on_oops" in the kernel command line, or setting
117 * /proc/sys/kernel/ftrace_dump_on_oops
118 * Set 1 if you want to dump buffers of all CPUs
119 * Set 2 if you want to dump the buffer of the CPU that triggered oops
122 enum ftrace_dump_mode ftrace_dump_on_oops;
124 static int tracing_set_tracer(const char *buf);
126 #define MAX_TRACER_SIZE 100
127 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
128 static char *default_bootup_tracer;
130 static int __init set_cmdline_ftrace(char *str)
132 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
133 default_bootup_tracer = bootup_tracer_buf;
134 /* We are using ftrace early, expand it */
135 ring_buffer_expanded = 1;
138 __setup("ftrace=", set_cmdline_ftrace);
140 static int __init set_ftrace_dump_on_oops(char *str)
142 if (*str++ != '=' || !*str) {
143 ftrace_dump_on_oops = DUMP_ALL;
147 if (!strcmp("orig_cpu", str)) {
148 ftrace_dump_on_oops = DUMP_ORIG;
154 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
156 unsigned long long ns2usecs(cycle_t nsec)
164 * The global_trace is the descriptor that holds the tracing
165 * buffers for the live tracing. For each CPU, it contains
166 * a link list of pages that will store trace entries. The
167 * page descriptor of the pages in the memory is used to hold
168 * the link list by linking the lru item in the page descriptor
169 * to each of the pages in the buffer per CPU.
171 * For each active CPU there is a data field that holds the
172 * pages for the buffer for that CPU. Each CPU has the same number
173 * of pages allocated for its buffer.
175 static struct trace_array global_trace;
177 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
179 int filter_current_check_discard(struct ring_buffer *buffer,
180 struct ftrace_event_call *call, void *rec,
181 struct ring_buffer_event *event)
183 return filter_check_discard(call, rec, buffer, event);
185 EXPORT_SYMBOL_GPL(filter_current_check_discard);
187 cycle_t ftrace_now(int cpu)
191 /* Early boot up does not have a buffer yet */
192 if (!global_trace.buffer)
193 return trace_clock_local();
195 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
196 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
202 * The max_tr is used to snapshot the global_trace when a maximum
203 * latency is reached. Some tracers will use this to store a maximum
204 * trace while it continues examining live traces.
206 * The buffers for the max_tr are set up the same as the global_trace.
207 * When a snapshot is taken, the link list of the max_tr is swapped
208 * with the link list of the global_trace and the buffers are reset for
209 * the global_trace so the tracing can continue.
211 static struct trace_array max_tr;
213 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
215 /* tracer_enabled is used to toggle activation of a tracer */
216 static int tracer_enabled = 1;
219 * tracing_is_enabled - return tracer_enabled status
221 * This function is used by other tracers to know the status
222 * of the tracer_enabled flag. Tracers may use this function
223 * to know if it should enable their features when starting
224 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
226 int tracing_is_enabled(void)
228 return tracer_enabled;
232 * trace_buf_size is the size in bytes that is allocated
233 * for a buffer. Note, the number of bytes is always rounded
236 * This number is purposely set to a low number of 16384.
237 * If the dump on oops happens, it will be much appreciated
238 * to not have to wait for all that output. Anyway this can be
239 * boot time and run time configurable.
241 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
243 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
245 /* trace_types holds a link list of available tracers. */
246 static struct tracer *trace_types __read_mostly;
248 /* current_trace points to the tracer that is currently active */
249 static struct tracer *current_trace __read_mostly;
252 * trace_types_lock is used to protect the trace_types list.
254 static DEFINE_MUTEX(trace_types_lock);
257 * serialize the access of the ring buffer
259 * ring buffer serializes readers, but it is low level protection.
260 * The validity of the events (which returns by ring_buffer_peek() ..etc)
261 * are not protected by ring buffer.
263 * The content of events may become garbage if we allow other process consumes
264 * these events concurrently:
265 * A) the page of the consumed events may become a normal page
266 * (not reader page) in ring buffer, and this page will be rewrited
267 * by events producer.
268 * B) The page of the consumed events may become a page for splice_read,
269 * and this page will be returned to system.
271 * These primitives allow multi process access to different cpu ring buffer
274 * These primitives don't distinguish read-only and read-consume access.
275 * Multi read-only access are also serialized.
279 static DECLARE_RWSEM(all_cpu_access_lock);
280 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
282 static inline void trace_access_lock(int cpu)
284 if (cpu == TRACE_PIPE_ALL_CPU) {
285 /* gain it for accessing the whole ring buffer. */
286 down_write(&all_cpu_access_lock);
288 /* gain it for accessing a cpu ring buffer. */
290 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
291 down_read(&all_cpu_access_lock);
293 /* Secondly block other access to this @cpu ring buffer. */
294 mutex_lock(&per_cpu(cpu_access_lock, cpu));
298 static inline void trace_access_unlock(int cpu)
300 if (cpu == TRACE_PIPE_ALL_CPU) {
301 up_write(&all_cpu_access_lock);
303 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
304 up_read(&all_cpu_access_lock);
308 static inline void trace_access_lock_init(void)
312 for_each_possible_cpu(cpu)
313 mutex_init(&per_cpu(cpu_access_lock, cpu));
318 static DEFINE_MUTEX(access_lock);
320 static inline void trace_access_lock(int cpu)
323 mutex_lock(&access_lock);
326 static inline void trace_access_unlock(int cpu)
329 mutex_unlock(&access_lock);
332 static inline void trace_access_lock_init(void)
338 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
339 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
341 /* trace_flags holds trace_options default values */
342 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
343 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
344 TRACE_ITER_GRAPH_TIME;
346 static int trace_stop_count;
347 static DEFINE_SPINLOCK(tracing_start_lock);
350 * trace_wake_up - wake up tasks waiting for trace input
352 * Simply wakes up any task that is blocked on the trace_wait
353 * queue. These is used with trace_poll for tasks polling the trace.
355 void trace_wake_up(void)
359 if (trace_flags & TRACE_ITER_BLOCK)
362 * The runqueue_is_locked() can fail, but this is the best we
366 if (!runqueue_is_locked(cpu))
367 wake_up(&trace_wait);
371 static int __init set_buf_size(char *str)
373 unsigned long buf_size;
377 buf_size = memparse(str, &str);
378 /* nr_entries can not be zero */
381 trace_buf_size = buf_size;
384 __setup("trace_buf_size=", set_buf_size);
386 static int __init set_tracing_thresh(char *str)
388 unsigned long threshhold;
393 ret = strict_strtoul(str, 0, &threshhold);
396 tracing_thresh = threshhold * 1000;
399 __setup("tracing_thresh=", set_tracing_thresh);
401 unsigned long nsecs_to_usecs(unsigned long nsecs)
406 /* These must match the bit postions in trace_iterator_flags */
407 static const char *trace_options[] = {
435 { trace_clock_local, "local" },
436 { trace_clock_global, "global" },
442 * trace_parser_get_init - gets the buffer for trace parser
444 int trace_parser_get_init(struct trace_parser *parser, int size)
446 memset(parser, 0, sizeof(*parser));
448 parser->buffer = kmalloc(size, GFP_KERNEL);
457 * trace_parser_put - frees the buffer for trace parser
459 void trace_parser_put(struct trace_parser *parser)
461 kfree(parser->buffer);
465 * trace_get_user - reads the user input string separated by space
466 * (matched by isspace(ch))
468 * For each string found the 'struct trace_parser' is updated,
469 * and the function returns.
471 * Returns number of bytes read.
473 * See kernel/trace/trace.h for 'struct trace_parser' details.
475 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
476 size_t cnt, loff_t *ppos)
483 trace_parser_clear(parser);
485 ret = get_user(ch, ubuf++);
493 * The parser is not finished with the last write,
494 * continue reading the user input without skipping spaces.
497 /* skip white space */
498 while (cnt && isspace(ch)) {
499 ret = get_user(ch, ubuf++);
506 /* only spaces were written */
516 /* read the non-space input */
517 while (cnt && !isspace(ch)) {
518 if (parser->idx < parser->size - 1)
519 parser->buffer[parser->idx++] = ch;
524 ret = get_user(ch, ubuf++);
531 /* We either got finished input or we have to wait for another call. */
533 parser->buffer[parser->idx] = 0;
534 parser->cont = false;
537 parser->buffer[parser->idx++] = ch;
547 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
555 if (s->len <= s->readpos)
558 len = s->len - s->readpos;
561 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
571 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
576 if (s->len <= s->readpos)
579 len = s->len - s->readpos;
582 ret = memcpy(buf, s->buffer + s->readpos, cnt);
591 * ftrace_max_lock is used to protect the swapping of buffers
592 * when taking a max snapshot. The buffers themselves are
593 * protected by per_cpu spinlocks. But the action of the swap
594 * needs its own lock.
596 * This is defined as a arch_spinlock_t in order to help
597 * with performance when lockdep debugging is enabled.
599 * It is also used in other places outside the update_max_tr
600 * so it needs to be defined outside of the
601 * CONFIG_TRACER_MAX_TRACE.
603 static arch_spinlock_t ftrace_max_lock =
604 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
606 unsigned long __read_mostly tracing_thresh;
608 #ifdef CONFIG_TRACER_MAX_TRACE
609 unsigned long __read_mostly tracing_max_latency;
612 * Copy the new maximum trace into the separate maximum-trace
613 * structure. (this way the maximum trace is permanently saved,
614 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
617 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
619 struct trace_array_cpu *data = tr->data[cpu];
620 struct trace_array_cpu *max_data;
623 max_tr.time_start = data->preempt_timestamp;
625 max_data = max_tr.data[cpu];
626 max_data->saved_latency = tracing_max_latency;
627 max_data->critical_start = data->critical_start;
628 max_data->critical_end = data->critical_end;
630 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
631 max_data->pid = tsk->pid;
632 max_data->uid = task_uid(tsk);
633 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
634 max_data->policy = tsk->policy;
635 max_data->rt_priority = tsk->rt_priority;
637 /* record this tasks comm */
638 tracing_record_cmdline(tsk);
642 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
644 * @tsk: the task with the latency
645 * @cpu: The cpu that initiated the trace.
647 * Flip the buffers between the @tr and the max_tr and record information
648 * about which task was the cause of this latency.
651 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
653 struct ring_buffer *buf = tr->buffer;
655 if (trace_stop_count)
658 WARN_ON_ONCE(!irqs_disabled());
659 arch_spin_lock(&ftrace_max_lock);
661 tr->buffer = max_tr.buffer;
664 __update_max_tr(tr, tsk, cpu);
665 arch_spin_unlock(&ftrace_max_lock);
669 * update_max_tr_single - only copy one trace over, and reset the rest
671 * @tsk - task with the latency
672 * @cpu - the cpu of the buffer to copy.
674 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
677 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
681 if (trace_stop_count)
684 WARN_ON_ONCE(!irqs_disabled());
685 arch_spin_lock(&ftrace_max_lock);
687 ftrace_disable_cpu();
689 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
693 * We failed to swap the buffer due to a commit taking
694 * place on this CPU. We fail to record, but we reset
695 * the max trace buffer (no one writes directly to it)
696 * and flag that it failed.
698 trace_array_printk(&max_tr, _THIS_IP_,
699 "Failed to swap buffers due to commit in progress\n");
704 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
706 __update_max_tr(tr, tsk, cpu);
707 arch_spin_unlock(&ftrace_max_lock);
709 #endif /* CONFIG_TRACER_MAX_TRACE */
712 * register_tracer - register a tracer with the ftrace system.
713 * @type - the plugin for the tracer
715 * Register a new plugin tracer.
717 int register_tracer(struct tracer *type)
718 __releases(kernel_lock)
719 __acquires(kernel_lock)
725 pr_info("Tracer must have a name\n");
729 if (strlen(type->name) > MAX_TRACER_SIZE) {
730 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
735 * When this gets called we hold the BKL which means that
736 * preemption is disabled. Various trace selftests however
737 * need to disable and enable preemption for successful tests.
738 * So we drop the BKL here and grab it after the tests again.
741 mutex_lock(&trace_types_lock);
743 tracing_selftest_running = true;
745 for (t = trace_types; t; t = t->next) {
746 if (strcmp(type->name, t->name) == 0) {
748 pr_info("Tracer %s already registered\n",
756 type->set_flag = &dummy_set_flag;
758 type->flags = &dummy_tracer_flags;
760 if (!type->flags->opts)
761 type->flags->opts = dummy_tracer_opt;
762 if (!type->wait_pipe)
763 type->wait_pipe = default_wait_pipe;
766 #ifdef CONFIG_FTRACE_STARTUP_TEST
767 if (type->selftest && !tracing_selftest_disabled) {
768 struct tracer *saved_tracer = current_trace;
769 struct trace_array *tr = &global_trace;
772 * Run a selftest on this tracer.
773 * Here we reset the trace buffer, and set the current
774 * tracer to be this tracer. The tracer can then run some
775 * internal tracing to verify that everything is in order.
776 * If we fail, we do not register this tracer.
778 tracing_reset_online_cpus(tr);
780 current_trace = type;
781 /* the test is responsible for initializing and enabling */
782 pr_info("Testing tracer %s: ", type->name);
783 ret = type->selftest(type, tr);
784 /* the test is responsible for resetting too */
785 current_trace = saved_tracer;
787 printk(KERN_CONT "FAILED!\n");
790 /* Only reset on passing, to avoid touching corrupted buffers */
791 tracing_reset_online_cpus(tr);
793 printk(KERN_CONT "PASSED\n");
797 type->next = trace_types;
801 tracing_selftest_running = false;
802 mutex_unlock(&trace_types_lock);
804 if (ret || !default_bootup_tracer)
807 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
810 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
811 /* Do we want this tracer to start on bootup? */
812 tracing_set_tracer(type->name);
813 default_bootup_tracer = NULL;
814 /* disable other selftests, since this will break it. */
815 tracing_selftest_disabled = 1;
816 #ifdef CONFIG_FTRACE_STARTUP_TEST
817 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
826 void unregister_tracer(struct tracer *type)
830 mutex_lock(&trace_types_lock);
831 for (t = &trace_types; *t; t = &(*t)->next) {
835 pr_info("Tracer %s not registered\n", type->name);
841 if (type == current_trace && tracer_enabled) {
844 if (current_trace->stop)
845 current_trace->stop(&global_trace);
846 current_trace = &nop_trace;
849 mutex_unlock(&trace_types_lock);
852 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
854 ftrace_disable_cpu();
855 ring_buffer_reset_cpu(buffer, cpu);
859 void tracing_reset(struct trace_array *tr, int cpu)
861 struct ring_buffer *buffer = tr->buffer;
863 ring_buffer_record_disable(buffer);
865 /* Make sure all commits have finished */
867 __tracing_reset(buffer, cpu);
869 ring_buffer_record_enable(buffer);
872 void tracing_reset_online_cpus(struct trace_array *tr)
874 struct ring_buffer *buffer = tr->buffer;
877 ring_buffer_record_disable(buffer);
879 /* Make sure all commits have finished */
882 tr->time_start = ftrace_now(tr->cpu);
884 for_each_online_cpu(cpu)
885 __tracing_reset(buffer, cpu);
887 ring_buffer_record_enable(buffer);
890 void tracing_reset_current(int cpu)
892 tracing_reset(&global_trace, cpu);
895 void tracing_reset_current_online_cpus(void)
897 tracing_reset_online_cpus(&global_trace);
900 #define SAVED_CMDLINES 128
901 #define NO_CMDLINE_MAP UINT_MAX
902 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
903 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
904 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
905 static int cmdline_idx;
906 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
908 /* temporary disable recording */
909 static atomic_t trace_record_cmdline_disabled __read_mostly;
911 static void trace_init_cmdlines(void)
913 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
914 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
918 int is_tracing_stopped(void)
920 return trace_stop_count;
924 * ftrace_off_permanent - disable all ftrace code permanently
926 * This should only be called when a serious anomally has
927 * been detected. This will turn off the function tracing,
928 * ring buffers, and other tracing utilites. It takes no
929 * locks and can be called from any context.
931 void ftrace_off_permanent(void)
933 tracing_disabled = 1;
935 tracing_off_permanent();
939 * tracing_start - quick start of the tracer
941 * If tracing is enabled but was stopped by tracing_stop,
942 * this will start the tracer back up.
944 void tracing_start(void)
946 struct ring_buffer *buffer;
949 if (tracing_disabled)
952 spin_lock_irqsave(&tracing_start_lock, flags);
953 if (--trace_stop_count) {
954 if (trace_stop_count < 0) {
955 /* Someone screwed up their debugging */
957 trace_stop_count = 0;
962 /* Prevent the buffers from switching */
963 arch_spin_lock(&ftrace_max_lock);
965 buffer = global_trace.buffer;
967 ring_buffer_record_enable(buffer);
969 buffer = max_tr.buffer;
971 ring_buffer_record_enable(buffer);
973 arch_spin_unlock(&ftrace_max_lock);
977 spin_unlock_irqrestore(&tracing_start_lock, flags);
981 * tracing_stop - quick stop of the tracer
983 * Light weight way to stop tracing. Use in conjunction with
986 void tracing_stop(void)
988 struct ring_buffer *buffer;
992 spin_lock_irqsave(&tracing_start_lock, flags);
993 if (trace_stop_count++)
996 /* Prevent the buffers from switching */
997 arch_spin_lock(&ftrace_max_lock);
999 buffer = global_trace.buffer;
1001 ring_buffer_record_disable(buffer);
1003 buffer = max_tr.buffer;
1005 ring_buffer_record_disable(buffer);
1007 arch_spin_unlock(&ftrace_max_lock);
1010 spin_unlock_irqrestore(&tracing_start_lock, flags);
1013 void trace_stop_cmdline_recording(void);
1015 static void trace_save_cmdline(struct task_struct *tsk)
1019 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1023 * It's not the end of the world if we don't get
1024 * the lock, but we also don't want to spin
1025 * nor do we want to disable interrupts,
1026 * so if we miss here, then better luck next time.
1028 if (!arch_spin_trylock(&trace_cmdline_lock))
1031 idx = map_pid_to_cmdline[tsk->pid];
1032 if (idx == NO_CMDLINE_MAP) {
1033 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1036 * Check whether the cmdline buffer at idx has a pid
1037 * mapped. We are going to overwrite that entry so we
1038 * need to clear the map_pid_to_cmdline. Otherwise we
1039 * would read the new comm for the old pid.
1041 pid = map_cmdline_to_pid[idx];
1042 if (pid != NO_CMDLINE_MAP)
1043 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1045 map_cmdline_to_pid[idx] = tsk->pid;
1046 map_pid_to_cmdline[tsk->pid] = idx;
1051 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1053 arch_spin_unlock(&trace_cmdline_lock);
1056 void trace_find_cmdline(int pid, char comm[])
1061 strcpy(comm, "<idle>");
1065 if (WARN_ON_ONCE(pid < 0)) {
1066 strcpy(comm, "<XXX>");
1070 if (pid > PID_MAX_DEFAULT) {
1071 strcpy(comm, "<...>");
1076 arch_spin_lock(&trace_cmdline_lock);
1077 map = map_pid_to_cmdline[pid];
1078 if (map != NO_CMDLINE_MAP)
1079 strcpy(comm, saved_cmdlines[map]);
1081 strcpy(comm, "<...>");
1083 arch_spin_unlock(&trace_cmdline_lock);
1087 void tracing_record_cmdline(struct task_struct *tsk)
1089 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1093 trace_save_cmdline(tsk);
1097 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1100 struct task_struct *tsk = current;
1102 entry->preempt_count = pc & 0xff;
1103 entry->pid = (tsk) ? tsk->pid : 0;
1104 entry->lock_depth = (tsk) ? tsk->lock_depth : 0;
1106 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1107 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1109 TRACE_FLAG_IRQS_NOSUPPORT |
1111 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1112 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1113 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1115 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1117 struct ring_buffer_event *
1118 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1121 unsigned long flags, int pc)
1123 struct ring_buffer_event *event;
1125 event = ring_buffer_lock_reserve(buffer, len);
1126 if (event != NULL) {
1127 struct trace_entry *ent = ring_buffer_event_data(event);
1129 tracing_generic_entry_update(ent, flags, pc);
1137 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1138 struct ring_buffer_event *event,
1139 unsigned long flags, int pc,
1142 ring_buffer_unlock_commit(buffer, event);
1144 ftrace_trace_stack(buffer, flags, 6, pc);
1145 ftrace_trace_userstack(buffer, flags, pc);
1151 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1152 struct ring_buffer_event *event,
1153 unsigned long flags, int pc)
1155 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1158 struct ring_buffer_event *
1159 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1160 int type, unsigned long len,
1161 unsigned long flags, int pc)
1163 *current_rb = global_trace.buffer;
1164 return trace_buffer_lock_reserve(*current_rb,
1165 type, len, flags, pc);
1167 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1169 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1170 struct ring_buffer_event *event,
1171 unsigned long flags, int pc)
1173 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1175 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1177 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1178 struct ring_buffer_event *event,
1179 unsigned long flags, int pc)
1181 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1183 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1185 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1186 struct ring_buffer_event *event)
1188 ring_buffer_discard_commit(buffer, event);
1190 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1193 trace_function(struct trace_array *tr,
1194 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1197 struct ftrace_event_call *call = &event_function;
1198 struct ring_buffer *buffer = tr->buffer;
1199 struct ring_buffer_event *event;
1200 struct ftrace_entry *entry;
1202 /* If we are reading the ring buffer, don't trace */
1203 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1206 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1210 entry = ring_buffer_event_data(event);
1212 entry->parent_ip = parent_ip;
1214 if (!filter_check_discard(call, entry, buffer, event))
1215 ring_buffer_unlock_commit(buffer, event);
1219 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1220 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1223 if (likely(!atomic_read(&data->disabled)))
1224 trace_function(tr, ip, parent_ip, flags, pc);
1227 #ifdef CONFIG_STACKTRACE
1228 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1229 unsigned long flags,
1232 struct ftrace_event_call *call = &event_kernel_stack;
1233 struct ring_buffer_event *event;
1234 struct stack_entry *entry;
1235 struct stack_trace trace;
1237 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1238 sizeof(*entry), flags, pc);
1241 entry = ring_buffer_event_data(event);
1242 memset(&entry->caller, 0, sizeof(entry->caller));
1244 trace.nr_entries = 0;
1245 trace.max_entries = FTRACE_STACK_ENTRIES;
1247 trace.entries = entry->caller;
1249 save_stack_trace(&trace);
1250 if (!filter_check_discard(call, entry, buffer, event))
1251 ring_buffer_unlock_commit(buffer, event);
1254 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1257 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1260 __ftrace_trace_stack(buffer, flags, skip, pc);
1263 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1266 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
1270 * trace_dump_stack - record a stack back trace in the trace buffer
1272 void trace_dump_stack(void)
1274 unsigned long flags;
1276 if (tracing_disabled || tracing_selftest_running)
1279 local_save_flags(flags);
1281 /* skipping 3 traces, seems to get us at the caller of this function */
1282 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1286 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1288 struct ftrace_event_call *call = &event_user_stack;
1289 struct ring_buffer_event *event;
1290 struct userstack_entry *entry;
1291 struct stack_trace trace;
1293 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1297 * NMIs can not handle page faults, even with fix ups.
1298 * The save user stack can (and often does) fault.
1300 if (unlikely(in_nmi()))
1303 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1304 sizeof(*entry), flags, pc);
1307 entry = ring_buffer_event_data(event);
1309 entry->tgid = current->tgid;
1310 memset(&entry->caller, 0, sizeof(entry->caller));
1312 trace.nr_entries = 0;
1313 trace.max_entries = FTRACE_STACK_ENTRIES;
1315 trace.entries = entry->caller;
1317 save_stack_trace_user(&trace);
1318 if (!filter_check_discard(call, entry, buffer, event))
1319 ring_buffer_unlock_commit(buffer, event);
1323 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1325 ftrace_trace_userstack(tr, flags, preempt_count());
1329 #endif /* CONFIG_STACKTRACE */
1332 ftrace_trace_special(void *__tr,
1333 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1336 struct ftrace_event_call *call = &event_special;
1337 struct ring_buffer_event *event;
1338 struct trace_array *tr = __tr;
1339 struct ring_buffer *buffer = tr->buffer;
1340 struct special_entry *entry;
1342 event = trace_buffer_lock_reserve(buffer, TRACE_SPECIAL,
1343 sizeof(*entry), 0, pc);
1346 entry = ring_buffer_event_data(event);
1351 if (!filter_check_discard(call, entry, buffer, event))
1352 trace_buffer_unlock_commit(buffer, event, 0, pc);
1356 __trace_special(void *__tr, void *__data,
1357 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1359 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1363 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1365 struct trace_array *tr = &global_trace;
1366 struct trace_array_cpu *data;
1367 unsigned long flags;
1371 if (tracing_disabled)
1374 pc = preempt_count();
1375 local_irq_save(flags);
1376 cpu = raw_smp_processor_id();
1377 data = tr->data[cpu];
1379 if (likely(atomic_inc_return(&data->disabled) == 1))
1380 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1382 atomic_dec(&data->disabled);
1383 local_irq_restore(flags);
1387 * trace_vbprintk - write binary msg to tracing buffer
1390 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1392 static arch_spinlock_t trace_buf_lock =
1393 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1394 static u32 trace_buf[TRACE_BUF_SIZE];
1396 struct ftrace_event_call *call = &event_bprint;
1397 struct ring_buffer_event *event;
1398 struct ring_buffer *buffer;
1399 struct trace_array *tr = &global_trace;
1400 struct trace_array_cpu *data;
1401 struct bprint_entry *entry;
1402 unsigned long flags;
1405 int cpu, len = 0, size, pc;
1407 if (unlikely(tracing_selftest_running || tracing_disabled))
1410 /* Don't pollute graph traces with trace_vprintk internals */
1411 pause_graph_tracing();
1413 pc = preempt_count();
1414 resched = ftrace_preempt_disable();
1415 cpu = raw_smp_processor_id();
1416 data = tr->data[cpu];
1418 disable = atomic_inc_return(&data->disabled);
1419 if (unlikely(disable != 1))
1422 /* Lockdep uses trace_printk for lock tracing */
1423 local_irq_save(flags);
1424 arch_spin_lock(&trace_buf_lock);
1425 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1427 if (len > TRACE_BUF_SIZE || len < 0)
1430 size = sizeof(*entry) + sizeof(u32) * len;
1431 buffer = tr->buffer;
1432 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1436 entry = ring_buffer_event_data(event);
1440 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1441 if (!filter_check_discard(call, entry, buffer, event)) {
1442 ring_buffer_unlock_commit(buffer, event);
1443 ftrace_trace_stack(buffer, flags, 6, pc);
1447 arch_spin_unlock(&trace_buf_lock);
1448 local_irq_restore(flags);
1451 atomic_dec_return(&data->disabled);
1452 ftrace_preempt_enable(resched);
1453 unpause_graph_tracing();
1457 EXPORT_SYMBOL_GPL(trace_vbprintk);
1459 int trace_array_printk(struct trace_array *tr,
1460 unsigned long ip, const char *fmt, ...)
1465 if (!(trace_flags & TRACE_ITER_PRINTK))
1469 ret = trace_array_vprintk(tr, ip, fmt, ap);
1474 int trace_array_vprintk(struct trace_array *tr,
1475 unsigned long ip, const char *fmt, va_list args)
1477 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1478 static char trace_buf[TRACE_BUF_SIZE];
1480 struct ftrace_event_call *call = &event_print;
1481 struct ring_buffer_event *event;
1482 struct ring_buffer *buffer;
1483 struct trace_array_cpu *data;
1484 int cpu, len = 0, size, pc;
1485 struct print_entry *entry;
1486 unsigned long irq_flags;
1489 if (tracing_disabled || tracing_selftest_running)
1492 pc = preempt_count();
1493 preempt_disable_notrace();
1494 cpu = raw_smp_processor_id();
1495 data = tr->data[cpu];
1497 disable = atomic_inc_return(&data->disabled);
1498 if (unlikely(disable != 1))
1501 pause_graph_tracing();
1502 raw_local_irq_save(irq_flags);
1503 arch_spin_lock(&trace_buf_lock);
1504 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1506 size = sizeof(*entry) + len + 1;
1507 buffer = tr->buffer;
1508 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1512 entry = ring_buffer_event_data(event);
1515 memcpy(&entry->buf, trace_buf, len);
1516 entry->buf[len] = '\0';
1517 if (!filter_check_discard(call, entry, buffer, event)) {
1518 ring_buffer_unlock_commit(buffer, event);
1519 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1523 arch_spin_unlock(&trace_buf_lock);
1524 raw_local_irq_restore(irq_flags);
1525 unpause_graph_tracing();
1527 atomic_dec_return(&data->disabled);
1528 preempt_enable_notrace();
1533 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1535 return trace_array_vprintk(&global_trace, ip, fmt, args);
1537 EXPORT_SYMBOL_GPL(trace_vprintk);
1539 static void trace_iterator_increment(struct trace_iterator *iter)
1541 /* Don't allow ftrace to trace into the ring buffers */
1542 ftrace_disable_cpu();
1545 if (iter->buffer_iter[iter->cpu])
1546 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1548 ftrace_enable_cpu();
1551 static struct trace_entry *
1552 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1553 unsigned long *lost_events)
1555 struct ring_buffer_event *event;
1556 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1558 /* Don't allow ftrace to trace into the ring buffers */
1559 ftrace_disable_cpu();
1562 event = ring_buffer_iter_peek(buf_iter, ts);
1564 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1567 ftrace_enable_cpu();
1569 return event ? ring_buffer_event_data(event) : NULL;
1572 static struct trace_entry *
1573 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1574 unsigned long *missing_events, u64 *ent_ts)
1576 struct ring_buffer *buffer = iter->tr->buffer;
1577 struct trace_entry *ent, *next = NULL;
1578 unsigned long lost_events = 0, next_lost = 0;
1579 int cpu_file = iter->cpu_file;
1580 u64 next_ts = 0, ts;
1585 * If we are in a per_cpu trace file, don't bother by iterating over
1586 * all cpu and peek directly.
1588 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1589 if (ring_buffer_empty_cpu(buffer, cpu_file))
1591 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1593 *ent_cpu = cpu_file;
1598 for_each_tracing_cpu(cpu) {
1600 if (ring_buffer_empty_cpu(buffer, cpu))
1603 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1606 * Pick the entry with the smallest timestamp:
1608 if (ent && (!next || ts < next_ts)) {
1612 next_lost = lost_events;
1617 *ent_cpu = next_cpu;
1623 *missing_events = next_lost;
1628 /* Find the next real entry, without updating the iterator itself */
1629 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1630 int *ent_cpu, u64 *ent_ts)
1632 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1635 /* Find the next real entry, and increment the iterator to the next entry */
1636 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1638 iter->ent = __find_next_entry(iter, &iter->cpu,
1639 &iter->lost_events, &iter->ts);
1642 trace_iterator_increment(iter);
1644 return iter->ent ? iter : NULL;
1647 static void trace_consume(struct trace_iterator *iter)
1649 /* Don't allow ftrace to trace into the ring buffers */
1650 ftrace_disable_cpu();
1651 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1652 &iter->lost_events);
1653 ftrace_enable_cpu();
1656 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1658 struct trace_iterator *iter = m->private;
1662 WARN_ON_ONCE(iter->leftover);
1666 /* can't go backwards */
1671 ent = trace_find_next_entry_inc(iter);
1675 while (ent && iter->idx < i)
1676 ent = trace_find_next_entry_inc(iter);
1683 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1685 struct trace_array *tr = iter->tr;
1686 struct ring_buffer_event *event;
1687 struct ring_buffer_iter *buf_iter;
1688 unsigned long entries = 0;
1691 tr->data[cpu]->skipped_entries = 0;
1693 if (!iter->buffer_iter[cpu])
1696 buf_iter = iter->buffer_iter[cpu];
1697 ring_buffer_iter_reset(buf_iter);
1700 * We could have the case with the max latency tracers
1701 * that a reset never took place on a cpu. This is evident
1702 * by the timestamp being before the start of the buffer.
1704 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1705 if (ts >= iter->tr->time_start)
1708 ring_buffer_read(buf_iter, NULL);
1711 tr->data[cpu]->skipped_entries = entries;
1715 * The current tracer is copied to avoid a global locking
1718 static void *s_start(struct seq_file *m, loff_t *pos)
1720 struct trace_iterator *iter = m->private;
1721 static struct tracer *old_tracer;
1722 int cpu_file = iter->cpu_file;
1727 /* copy the tracer to avoid using a global lock all around */
1728 mutex_lock(&trace_types_lock);
1729 if (unlikely(old_tracer != current_trace && current_trace)) {
1730 old_tracer = current_trace;
1731 *iter->trace = *current_trace;
1733 mutex_unlock(&trace_types_lock);
1735 atomic_inc(&trace_record_cmdline_disabled);
1737 if (*pos != iter->pos) {
1742 ftrace_disable_cpu();
1744 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1745 for_each_tracing_cpu(cpu)
1746 tracing_iter_reset(iter, cpu);
1748 tracing_iter_reset(iter, cpu_file);
1750 ftrace_enable_cpu();
1753 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1758 * If we overflowed the seq_file before, then we want
1759 * to just reuse the trace_seq buffer again.
1765 p = s_next(m, p, &l);
1769 trace_event_read_lock();
1770 trace_access_lock(cpu_file);
1774 static void s_stop(struct seq_file *m, void *p)
1776 struct trace_iterator *iter = m->private;
1778 atomic_dec(&trace_record_cmdline_disabled);
1779 trace_access_unlock(iter->cpu_file);
1780 trace_event_read_unlock();
1783 static void print_lat_help_header(struct seq_file *m)
1785 seq_puts(m, "# _------=> CPU# \n");
1786 seq_puts(m, "# / _-----=> irqs-off \n");
1787 seq_puts(m, "# | / _----=> need-resched \n");
1788 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1789 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1790 seq_puts(m, "# |||| /_--=> lock-depth \n");
1791 seq_puts(m, "# |||||/ delay \n");
1792 seq_puts(m, "# cmd pid |||||| time | caller \n");
1793 seq_puts(m, "# \\ / |||||| \\ | / \n");
1796 static void print_func_help_header(struct seq_file *m)
1798 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1799 seq_puts(m, "# | | | | |\n");
1804 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1806 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1807 struct trace_array *tr = iter->tr;
1808 struct trace_array_cpu *data = tr->data[tr->cpu];
1809 struct tracer *type = current_trace;
1810 unsigned long entries = 0;
1811 unsigned long total = 0;
1812 unsigned long count;
1813 const char *name = "preemption";
1820 for_each_tracing_cpu(cpu) {
1821 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1823 * If this buffer has skipped entries, then we hold all
1824 * entries for the trace and we need to ignore the
1825 * ones before the time stamp.
1827 if (tr->data[cpu]->skipped_entries) {
1828 count -= tr->data[cpu]->skipped_entries;
1829 /* total is the same as the entries */
1833 ring_buffer_overrun_cpu(tr->buffer, cpu);
1837 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1839 seq_puts(m, "# -----------------------------------"
1840 "---------------------------------\n");
1841 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1842 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1843 nsecs_to_usecs(data->saved_latency),
1847 #if defined(CONFIG_PREEMPT_NONE)
1849 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1851 #elif defined(CONFIG_PREEMPT)
1856 /* These are reserved for later use */
1859 seq_printf(m, " #P:%d)\n", num_online_cpus());
1863 seq_puts(m, "# -----------------\n");
1864 seq_printf(m, "# | task: %.16s-%d "
1865 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1866 data->comm, data->pid, data->uid, data->nice,
1867 data->policy, data->rt_priority);
1868 seq_puts(m, "# -----------------\n");
1870 if (data->critical_start) {
1871 seq_puts(m, "# => started at: ");
1872 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1873 trace_print_seq(m, &iter->seq);
1874 seq_puts(m, "\n# => ended at: ");
1875 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1876 trace_print_seq(m, &iter->seq);
1877 seq_puts(m, "\n#\n");
1883 static void test_cpu_buff_start(struct trace_iterator *iter)
1885 struct trace_seq *s = &iter->seq;
1887 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1890 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1893 if (cpumask_test_cpu(iter->cpu, iter->started))
1896 if (iter->tr->data[iter->cpu]->skipped_entries)
1899 cpumask_set_cpu(iter->cpu, iter->started);
1901 /* Don't print started cpu buffer for the first entry of the trace */
1903 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1907 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1909 struct trace_seq *s = &iter->seq;
1910 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1911 struct trace_entry *entry;
1912 struct trace_event *event;
1916 test_cpu_buff_start(iter);
1918 event = ftrace_find_event(entry->type);
1920 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1921 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1922 if (!trace_print_lat_context(iter))
1925 if (!trace_print_context(iter))
1931 return event->funcs->trace(iter, sym_flags, event);
1933 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1936 return TRACE_TYPE_HANDLED;
1938 return TRACE_TYPE_PARTIAL_LINE;
1941 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1943 struct trace_seq *s = &iter->seq;
1944 struct trace_entry *entry;
1945 struct trace_event *event;
1949 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1950 if (!trace_seq_printf(s, "%d %d %llu ",
1951 entry->pid, iter->cpu, iter->ts))
1955 event = ftrace_find_event(entry->type);
1957 return event->funcs->raw(iter, 0, event);
1959 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1962 return TRACE_TYPE_HANDLED;
1964 return TRACE_TYPE_PARTIAL_LINE;
1967 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1969 struct trace_seq *s = &iter->seq;
1970 unsigned char newline = '\n';
1971 struct trace_entry *entry;
1972 struct trace_event *event;
1976 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1977 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1978 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1979 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1982 event = ftrace_find_event(entry->type);
1984 enum print_line_t ret = event->funcs->hex(iter, 0, event);
1985 if (ret != TRACE_TYPE_HANDLED)
1989 SEQ_PUT_FIELD_RET(s, newline);
1991 return TRACE_TYPE_HANDLED;
1994 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1996 struct trace_seq *s = &iter->seq;
1997 struct trace_entry *entry;
1998 struct trace_event *event;
2002 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2003 SEQ_PUT_FIELD_RET(s, entry->pid);
2004 SEQ_PUT_FIELD_RET(s, iter->cpu);
2005 SEQ_PUT_FIELD_RET(s, iter->ts);
2008 event = ftrace_find_event(entry->type);
2009 return event ? event->funcs->binary(iter, 0, event) :
2013 int trace_empty(struct trace_iterator *iter)
2017 /* If we are looking at one CPU buffer, only check that one */
2018 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2019 cpu = iter->cpu_file;
2020 if (iter->buffer_iter[cpu]) {
2021 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2024 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2030 for_each_tracing_cpu(cpu) {
2031 if (iter->buffer_iter[cpu]) {
2032 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2035 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2043 /* Called with trace_event_read_lock() held. */
2044 enum print_line_t print_trace_line(struct trace_iterator *iter)
2046 enum print_line_t ret;
2048 if (iter->lost_events)
2049 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2050 iter->cpu, iter->lost_events);
2052 if (iter->trace && iter->trace->print_line) {
2053 ret = iter->trace->print_line(iter);
2054 if (ret != TRACE_TYPE_UNHANDLED)
2058 if (iter->ent->type == TRACE_BPRINT &&
2059 trace_flags & TRACE_ITER_PRINTK &&
2060 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2061 return trace_print_bprintk_msg_only(iter);
2063 if (iter->ent->type == TRACE_PRINT &&
2064 trace_flags & TRACE_ITER_PRINTK &&
2065 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2066 return trace_print_printk_msg_only(iter);
2068 if (trace_flags & TRACE_ITER_BIN)
2069 return print_bin_fmt(iter);
2071 if (trace_flags & TRACE_ITER_HEX)
2072 return print_hex_fmt(iter);
2074 if (trace_flags & TRACE_ITER_RAW)
2075 return print_raw_fmt(iter);
2077 return print_trace_fmt(iter);
2080 void trace_default_header(struct seq_file *m)
2082 struct trace_iterator *iter = m->private;
2084 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2085 /* print nothing if the buffers are empty */
2086 if (trace_empty(iter))
2088 print_trace_header(m, iter);
2089 if (!(trace_flags & TRACE_ITER_VERBOSE))
2090 print_lat_help_header(m);
2092 if (!(trace_flags & TRACE_ITER_VERBOSE))
2093 print_func_help_header(m);
2097 static int s_show(struct seq_file *m, void *v)
2099 struct trace_iterator *iter = v;
2102 if (iter->ent == NULL) {
2104 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2107 if (iter->trace && iter->trace->print_header)
2108 iter->trace->print_header(m);
2110 trace_default_header(m);
2112 } else if (iter->leftover) {
2114 * If we filled the seq_file buffer earlier, we
2115 * want to just show it now.
2117 ret = trace_print_seq(m, &iter->seq);
2119 /* ret should this time be zero, but you never know */
2120 iter->leftover = ret;
2123 print_trace_line(iter);
2124 ret = trace_print_seq(m, &iter->seq);
2126 * If we overflow the seq_file buffer, then it will
2127 * ask us for this data again at start up.
2129 * ret is 0 if seq_file write succeeded.
2132 iter->leftover = ret;
2138 static const struct seq_operations tracer_seq_ops = {
2145 static struct trace_iterator *
2146 __tracing_open(struct inode *inode, struct file *file)
2148 long cpu_file = (long) inode->i_private;
2149 void *fail_ret = ERR_PTR(-ENOMEM);
2150 struct trace_iterator *iter;
2154 if (tracing_disabled)
2155 return ERR_PTR(-ENODEV);
2157 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2159 return ERR_PTR(-ENOMEM);
2162 * We make a copy of the current tracer to avoid concurrent
2163 * changes on it while we are reading.
2165 mutex_lock(&trace_types_lock);
2166 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2171 *iter->trace = *current_trace;
2173 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2176 if (current_trace && current_trace->print_max)
2179 iter->tr = &global_trace;
2181 mutex_init(&iter->mutex);
2182 iter->cpu_file = cpu_file;
2184 /* Notify the tracer early; before we stop tracing. */
2185 if (iter->trace && iter->trace->open)
2186 iter->trace->open(iter);
2188 /* Annotate start of buffers if we had overruns */
2189 if (ring_buffer_overruns(iter->tr->buffer))
2190 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2192 /* stop the trace while dumping */
2195 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2196 for_each_tracing_cpu(cpu) {
2197 iter->buffer_iter[cpu] =
2198 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2200 ring_buffer_read_prepare_sync();
2201 for_each_tracing_cpu(cpu) {
2202 ring_buffer_read_start(iter->buffer_iter[cpu]);
2203 tracing_iter_reset(iter, cpu);
2206 cpu = iter->cpu_file;
2207 iter->buffer_iter[cpu] =
2208 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2209 ring_buffer_read_prepare_sync();
2210 ring_buffer_read_start(iter->buffer_iter[cpu]);
2211 tracing_iter_reset(iter, cpu);
2214 ret = seq_open(file, &tracer_seq_ops);
2216 fail_ret = ERR_PTR(ret);
2220 m = file->private_data;
2223 mutex_unlock(&trace_types_lock);
2228 for_each_tracing_cpu(cpu) {
2229 if (iter->buffer_iter[cpu])
2230 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2232 free_cpumask_var(iter->started);
2235 mutex_unlock(&trace_types_lock);
2242 int tracing_open_generic(struct inode *inode, struct file *filp)
2244 if (tracing_disabled)
2247 filp->private_data = inode->i_private;
2251 static int tracing_release(struct inode *inode, struct file *file)
2253 struct seq_file *m = (struct seq_file *)file->private_data;
2254 struct trace_iterator *iter;
2257 if (!(file->f_mode & FMODE_READ))
2262 mutex_lock(&trace_types_lock);
2263 for_each_tracing_cpu(cpu) {
2264 if (iter->buffer_iter[cpu])
2265 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2268 if (iter->trace && iter->trace->close)
2269 iter->trace->close(iter);
2271 /* reenable tracing if it was previously enabled */
2273 mutex_unlock(&trace_types_lock);
2275 seq_release(inode, file);
2276 mutex_destroy(&iter->mutex);
2277 free_cpumask_var(iter->started);
2283 static int tracing_open(struct inode *inode, struct file *file)
2285 struct trace_iterator *iter;
2288 /* If this file was open for write, then erase contents */
2289 if ((file->f_mode & FMODE_WRITE) &&
2290 (file->f_flags & O_TRUNC)) {
2291 long cpu = (long) inode->i_private;
2293 if (cpu == TRACE_PIPE_ALL_CPU)
2294 tracing_reset_online_cpus(&global_trace);
2296 tracing_reset(&global_trace, cpu);
2299 if (file->f_mode & FMODE_READ) {
2300 iter = __tracing_open(inode, file);
2302 ret = PTR_ERR(iter);
2303 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2304 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2310 t_next(struct seq_file *m, void *v, loff_t *pos)
2312 struct tracer *t = v;
2322 static void *t_start(struct seq_file *m, loff_t *pos)
2327 mutex_lock(&trace_types_lock);
2328 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2334 static void t_stop(struct seq_file *m, void *p)
2336 mutex_unlock(&trace_types_lock);
2339 static int t_show(struct seq_file *m, void *v)
2341 struct tracer *t = v;
2346 seq_printf(m, "%s", t->name);
2355 static const struct seq_operations show_traces_seq_ops = {
2362 static int show_traces_open(struct inode *inode, struct file *file)
2364 if (tracing_disabled)
2367 return seq_open(file, &show_traces_seq_ops);
2371 tracing_write_stub(struct file *filp, const char __user *ubuf,
2372 size_t count, loff_t *ppos)
2377 static const struct file_operations tracing_fops = {
2378 .open = tracing_open,
2380 .write = tracing_write_stub,
2381 .llseek = seq_lseek,
2382 .release = tracing_release,
2385 static const struct file_operations show_traces_fops = {
2386 .open = show_traces_open,
2388 .release = seq_release,
2392 * Only trace on a CPU if the bitmask is set:
2394 static cpumask_var_t tracing_cpumask;
2397 * The tracer itself will not take this lock, but still we want
2398 * to provide a consistent cpumask to user-space:
2400 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2403 * Temporary storage for the character representation of the
2404 * CPU bitmask (and one more byte for the newline):
2406 static char mask_str[NR_CPUS + 1];
2409 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2410 size_t count, loff_t *ppos)
2414 mutex_lock(&tracing_cpumask_update_lock);
2416 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2417 if (count - len < 2) {
2421 len += sprintf(mask_str + len, "\n");
2422 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2425 mutex_unlock(&tracing_cpumask_update_lock);
2431 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2432 size_t count, loff_t *ppos)
2435 cpumask_var_t tracing_cpumask_new;
2437 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2440 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2444 mutex_lock(&tracing_cpumask_update_lock);
2446 local_irq_disable();
2447 arch_spin_lock(&ftrace_max_lock);
2448 for_each_tracing_cpu(cpu) {
2450 * Increase/decrease the disabled counter if we are
2451 * about to flip a bit in the cpumask:
2453 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2454 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2455 atomic_inc(&global_trace.data[cpu]->disabled);
2457 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2458 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2459 atomic_dec(&global_trace.data[cpu]->disabled);
2462 arch_spin_unlock(&ftrace_max_lock);
2465 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2467 mutex_unlock(&tracing_cpumask_update_lock);
2468 free_cpumask_var(tracing_cpumask_new);
2473 free_cpumask_var(tracing_cpumask_new);
2478 static const struct file_operations tracing_cpumask_fops = {
2479 .open = tracing_open_generic,
2480 .read = tracing_cpumask_read,
2481 .write = tracing_cpumask_write,
2484 static int tracing_trace_options_show(struct seq_file *m, void *v)
2486 struct tracer_opt *trace_opts;
2490 mutex_lock(&trace_types_lock);
2491 tracer_flags = current_trace->flags->val;
2492 trace_opts = current_trace->flags->opts;
2494 for (i = 0; trace_options[i]; i++) {
2495 if (trace_flags & (1 << i))
2496 seq_printf(m, "%s\n", trace_options[i]);
2498 seq_printf(m, "no%s\n", trace_options[i]);
2501 for (i = 0; trace_opts[i].name; i++) {
2502 if (tracer_flags & trace_opts[i].bit)
2503 seq_printf(m, "%s\n", trace_opts[i].name);
2505 seq_printf(m, "no%s\n", trace_opts[i].name);
2507 mutex_unlock(&trace_types_lock);
2512 static int __set_tracer_option(struct tracer *trace,
2513 struct tracer_flags *tracer_flags,
2514 struct tracer_opt *opts, int neg)
2518 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2523 tracer_flags->val &= ~opts->bit;
2525 tracer_flags->val |= opts->bit;
2529 /* Try to assign a tracer specific option */
2530 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2532 struct tracer_flags *tracer_flags = trace->flags;
2533 struct tracer_opt *opts = NULL;
2536 for (i = 0; tracer_flags->opts[i].name; i++) {
2537 opts = &tracer_flags->opts[i];
2539 if (strcmp(cmp, opts->name) == 0)
2540 return __set_tracer_option(trace, trace->flags,
2547 static void set_tracer_flags(unsigned int mask, int enabled)
2549 /* do nothing if flag is already set */
2550 if (!!(trace_flags & mask) == !!enabled)
2554 trace_flags |= mask;
2556 trace_flags &= ~mask;
2560 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2561 size_t cnt, loff_t *ppos)
2569 if (cnt >= sizeof(buf))
2572 if (copy_from_user(&buf, ubuf, cnt))
2576 cmp = strstrip(buf);
2578 if (strncmp(cmp, "no", 2) == 0) {
2583 for (i = 0; trace_options[i]; i++) {
2584 if (strcmp(cmp, trace_options[i]) == 0) {
2585 set_tracer_flags(1 << i, !neg);
2590 /* If no option could be set, test the specific tracer options */
2591 if (!trace_options[i]) {
2592 mutex_lock(&trace_types_lock);
2593 ret = set_tracer_option(current_trace, cmp, neg);
2594 mutex_unlock(&trace_types_lock);
2604 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2606 if (tracing_disabled)
2608 return single_open(file, tracing_trace_options_show, NULL);
2611 static const struct file_operations tracing_iter_fops = {
2612 .open = tracing_trace_options_open,
2614 .llseek = seq_lseek,
2615 .release = single_release,
2616 .write = tracing_trace_options_write,
2619 static const char readme_msg[] =
2620 "tracing mini-HOWTO:\n\n"
2621 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2622 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2623 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2624 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2626 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2627 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2629 "# cat /sys/kernel/debug/tracing/trace_options\n"
2630 "noprint-parent nosym-offset nosym-addr noverbose\n"
2631 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2632 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2633 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2634 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2638 tracing_readme_read(struct file *filp, char __user *ubuf,
2639 size_t cnt, loff_t *ppos)
2641 return simple_read_from_buffer(ubuf, cnt, ppos,
2642 readme_msg, strlen(readme_msg));
2645 static const struct file_operations tracing_readme_fops = {
2646 .open = tracing_open_generic,
2647 .read = tracing_readme_read,
2651 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2652 size_t cnt, loff_t *ppos)
2661 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2665 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2673 for (i = 0; i < SAVED_CMDLINES; i++) {
2676 pid = map_cmdline_to_pid[i];
2677 if (pid == -1 || pid == NO_CMDLINE_MAP)
2680 trace_find_cmdline(pid, buf_comm);
2681 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2686 len = simple_read_from_buffer(ubuf, cnt, ppos,
2695 static const struct file_operations tracing_saved_cmdlines_fops = {
2696 .open = tracing_open_generic,
2697 .read = tracing_saved_cmdlines_read,
2701 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2702 size_t cnt, loff_t *ppos)
2707 r = sprintf(buf, "%u\n", tracer_enabled);
2708 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2712 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2713 size_t cnt, loff_t *ppos)
2715 struct trace_array *tr = filp->private_data;
2720 if (cnt >= sizeof(buf))
2723 if (copy_from_user(&buf, ubuf, cnt))
2728 ret = strict_strtoul(buf, 10, &val);
2734 mutex_lock(&trace_types_lock);
2735 if (tracer_enabled ^ val) {
2738 if (current_trace->start)
2739 current_trace->start(tr);
2744 if (current_trace->stop)
2745 current_trace->stop(tr);
2748 mutex_unlock(&trace_types_lock);
2756 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2757 size_t cnt, loff_t *ppos)
2759 char buf[MAX_TRACER_SIZE+2];
2762 mutex_lock(&trace_types_lock);
2764 r = sprintf(buf, "%s\n", current_trace->name);
2766 r = sprintf(buf, "\n");
2767 mutex_unlock(&trace_types_lock);
2769 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2772 int tracer_init(struct tracer *t, struct trace_array *tr)
2774 tracing_reset_online_cpus(tr);
2778 static int tracing_resize_ring_buffer(unsigned long size)
2783 * If kernel or user changes the size of the ring buffer
2784 * we use the size that was given, and we can forget about
2785 * expanding it later.
2787 ring_buffer_expanded = 1;
2789 ret = ring_buffer_resize(global_trace.buffer, size);
2793 ret = ring_buffer_resize(max_tr.buffer, size);
2797 r = ring_buffer_resize(global_trace.buffer,
2798 global_trace.entries);
2801 * AARGH! We are left with different
2802 * size max buffer!!!!
2803 * The max buffer is our "snapshot" buffer.
2804 * When a tracer needs a snapshot (one of the
2805 * latency tracers), it swaps the max buffer
2806 * with the saved snap shot. We succeeded to
2807 * update the size of the main buffer, but failed to
2808 * update the size of the max buffer. But when we tried
2809 * to reset the main buffer to the original size, we
2810 * failed there too. This is very unlikely to
2811 * happen, but if it does, warn and kill all
2815 tracing_disabled = 1;
2820 global_trace.entries = size;
2826 * tracing_update_buffers - used by tracing facility to expand ring buffers
2828 * To save on memory when the tracing is never used on a system with it
2829 * configured in. The ring buffers are set to a minimum size. But once
2830 * a user starts to use the tracing facility, then they need to grow
2831 * to their default size.
2833 * This function is to be called when a tracer is about to be used.
2835 int tracing_update_buffers(void)
2839 mutex_lock(&trace_types_lock);
2840 if (!ring_buffer_expanded)
2841 ret = tracing_resize_ring_buffer(trace_buf_size);
2842 mutex_unlock(&trace_types_lock);
2847 struct trace_option_dentry;
2849 static struct trace_option_dentry *
2850 create_trace_option_files(struct tracer *tracer);
2853 destroy_trace_option_files(struct trace_option_dentry *topts);
2855 static int tracing_set_tracer(const char *buf)
2857 static struct trace_option_dentry *topts;
2858 struct trace_array *tr = &global_trace;
2862 mutex_lock(&trace_types_lock);
2864 if (!ring_buffer_expanded) {
2865 ret = tracing_resize_ring_buffer(trace_buf_size);
2871 for (t = trace_types; t; t = t->next) {
2872 if (strcmp(t->name, buf) == 0)
2879 if (t == current_trace)
2882 trace_branch_disable();
2883 if (current_trace && current_trace->reset)
2884 current_trace->reset(tr);
2886 destroy_trace_option_files(topts);
2890 topts = create_trace_option_files(current_trace);
2893 ret = tracer_init(t, tr);
2898 trace_branch_enable(tr);
2900 mutex_unlock(&trace_types_lock);
2906 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2907 size_t cnt, loff_t *ppos)
2909 char buf[MAX_TRACER_SIZE+1];
2916 if (cnt > MAX_TRACER_SIZE)
2917 cnt = MAX_TRACER_SIZE;
2919 if (copy_from_user(&buf, ubuf, cnt))
2924 /* strip ending whitespace. */
2925 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2928 err = tracing_set_tracer(buf);
2938 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2939 size_t cnt, loff_t *ppos)
2941 unsigned long *ptr = filp->private_data;
2945 r = snprintf(buf, sizeof(buf), "%ld\n",
2946 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2947 if (r > sizeof(buf))
2949 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2953 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2954 size_t cnt, loff_t *ppos)
2956 unsigned long *ptr = filp->private_data;
2961 if (cnt >= sizeof(buf))
2964 if (copy_from_user(&buf, ubuf, cnt))
2969 ret = strict_strtoul(buf, 10, &val);
2978 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2980 long cpu_file = (long) inode->i_private;
2981 struct trace_iterator *iter;
2984 if (tracing_disabled)
2987 mutex_lock(&trace_types_lock);
2989 /* create a buffer to store the information to pass to userspace */
2990 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2997 * We make a copy of the current tracer to avoid concurrent
2998 * changes on it while we are reading.
3000 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3006 *iter->trace = *current_trace;
3008 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3013 /* trace pipe does not show start of buffer */
3014 cpumask_setall(iter->started);
3016 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3017 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3019 iter->cpu_file = cpu_file;
3020 iter->tr = &global_trace;
3021 mutex_init(&iter->mutex);
3022 filp->private_data = iter;
3024 if (iter->trace->pipe_open)
3025 iter->trace->pipe_open(iter);
3028 mutex_unlock(&trace_types_lock);
3034 mutex_unlock(&trace_types_lock);
3038 static int tracing_release_pipe(struct inode *inode, struct file *file)
3040 struct trace_iterator *iter = file->private_data;
3042 mutex_lock(&trace_types_lock);
3044 if (iter->trace->pipe_close)
3045 iter->trace->pipe_close(iter);
3047 mutex_unlock(&trace_types_lock);
3049 free_cpumask_var(iter->started);
3050 mutex_destroy(&iter->mutex);
3058 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3060 struct trace_iterator *iter = filp->private_data;
3062 if (trace_flags & TRACE_ITER_BLOCK) {
3064 * Always select as readable when in blocking mode
3066 return POLLIN | POLLRDNORM;
3068 if (!trace_empty(iter))
3069 return POLLIN | POLLRDNORM;
3070 poll_wait(filp, &trace_wait, poll_table);
3071 if (!trace_empty(iter))
3072 return POLLIN | POLLRDNORM;
3079 void default_wait_pipe(struct trace_iterator *iter)
3083 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3085 if (trace_empty(iter))
3088 finish_wait(&trace_wait, &wait);
3092 * This is a make-shift waitqueue.
3093 * A tracer might use this callback on some rare cases:
3095 * 1) the current tracer might hold the runqueue lock when it wakes up
3096 * a reader, hence a deadlock (sched, function, and function graph tracers)
3097 * 2) the function tracers, trace all functions, we don't want
3098 * the overhead of calling wake_up and friends
3099 * (and tracing them too)
3101 * Anyway, this is really very primitive wakeup.
3103 void poll_wait_pipe(struct trace_iterator *iter)
3105 set_current_state(TASK_INTERRUPTIBLE);
3106 /* sleep for 100 msecs, and try again. */
3107 schedule_timeout(HZ / 10);
3110 /* Must be called with trace_types_lock mutex held. */
3111 static int tracing_wait_pipe(struct file *filp)
3113 struct trace_iterator *iter = filp->private_data;
3115 while (trace_empty(iter)) {
3117 if ((filp->f_flags & O_NONBLOCK)) {
3121 mutex_unlock(&iter->mutex);
3123 iter->trace->wait_pipe(iter);
3125 mutex_lock(&iter->mutex);
3127 if (signal_pending(current))
3131 * We block until we read something and tracing is disabled.
3132 * We still block if tracing is disabled, but we have never
3133 * read anything. This allows a user to cat this file, and
3134 * then enable tracing. But after we have read something,
3135 * we give an EOF when tracing is again disabled.
3137 * iter->pos will be 0 if we haven't read anything.
3139 if (!tracer_enabled && iter->pos)
3150 tracing_read_pipe(struct file *filp, char __user *ubuf,
3151 size_t cnt, loff_t *ppos)
3153 struct trace_iterator *iter = filp->private_data;
3154 static struct tracer *old_tracer;
3157 /* return any leftover data */
3158 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3162 trace_seq_init(&iter->seq);
3164 /* copy the tracer to avoid using a global lock all around */
3165 mutex_lock(&trace_types_lock);
3166 if (unlikely(old_tracer != current_trace && current_trace)) {
3167 old_tracer = current_trace;
3168 *iter->trace = *current_trace;
3170 mutex_unlock(&trace_types_lock);
3173 * Avoid more than one consumer on a single file descriptor
3174 * This is just a matter of traces coherency, the ring buffer itself
3177 mutex_lock(&iter->mutex);
3178 if (iter->trace->read) {
3179 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3185 sret = tracing_wait_pipe(filp);
3189 /* stop when tracing is finished */
3190 if (trace_empty(iter)) {
3195 if (cnt >= PAGE_SIZE)
3196 cnt = PAGE_SIZE - 1;
3198 /* reset all but tr, trace, and overruns */
3199 memset(&iter->seq, 0,
3200 sizeof(struct trace_iterator) -
3201 offsetof(struct trace_iterator, seq));
3204 trace_event_read_lock();
3205 trace_access_lock(iter->cpu_file);
3206 while (trace_find_next_entry_inc(iter) != NULL) {
3207 enum print_line_t ret;
3208 int len = iter->seq.len;
3210 ret = print_trace_line(iter);
3211 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3212 /* don't print partial lines */
3213 iter->seq.len = len;
3216 if (ret != TRACE_TYPE_NO_CONSUME)
3217 trace_consume(iter);
3219 if (iter->seq.len >= cnt)
3222 trace_access_unlock(iter->cpu_file);
3223 trace_event_read_unlock();
3225 /* Now copy what we have to the user */
3226 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3227 if (iter->seq.readpos >= iter->seq.len)
3228 trace_seq_init(&iter->seq);
3231 * If there was nothing to send to user, inspite of consuming trace
3232 * entries, go back to wait for more entries.
3238 mutex_unlock(&iter->mutex);
3243 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3244 struct pipe_buffer *buf)
3246 __free_page(buf->page);
3249 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3252 __free_page(spd->pages[idx]);
3255 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3257 .map = generic_pipe_buf_map,
3258 .unmap = generic_pipe_buf_unmap,
3259 .confirm = generic_pipe_buf_confirm,
3260 .release = tracing_pipe_buf_release,
3261 .steal = generic_pipe_buf_steal,
3262 .get = generic_pipe_buf_get,
3266 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3271 /* Seq buffer is page-sized, exactly what we need. */
3273 count = iter->seq.len;
3274 ret = print_trace_line(iter);
3275 count = iter->seq.len - count;
3278 iter->seq.len -= count;
3281 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3282 iter->seq.len -= count;
3286 if (ret != TRACE_TYPE_NO_CONSUME)
3287 trace_consume(iter);
3289 if (!trace_find_next_entry_inc(iter)) {
3299 static ssize_t tracing_splice_read_pipe(struct file *filp,
3301 struct pipe_inode_info *pipe,
3305 struct page *pages_def[PIPE_DEF_BUFFERS];
3306 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3307 struct trace_iterator *iter = filp->private_data;
3308 struct splice_pipe_desc spd = {
3310 .partial = partial_def,
3311 .nr_pages = 0, /* This gets updated below. */
3313 .ops = &tracing_pipe_buf_ops,
3314 .spd_release = tracing_spd_release_pipe,
3316 static struct tracer *old_tracer;
3321 if (splice_grow_spd(pipe, &spd))
3324 /* copy the tracer to avoid using a global lock all around */
3325 mutex_lock(&trace_types_lock);
3326 if (unlikely(old_tracer != current_trace && current_trace)) {
3327 old_tracer = current_trace;
3328 *iter->trace = *current_trace;
3330 mutex_unlock(&trace_types_lock);
3332 mutex_lock(&iter->mutex);
3334 if (iter->trace->splice_read) {
3335 ret = iter->trace->splice_read(iter, filp,
3336 ppos, pipe, len, flags);
3341 ret = tracing_wait_pipe(filp);
3345 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3350 trace_event_read_lock();
3351 trace_access_lock(iter->cpu_file);
3353 /* Fill as many pages as possible. */
3354 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3355 spd.pages[i] = alloc_page(GFP_KERNEL);
3359 rem = tracing_fill_pipe_page(rem, iter);
3361 /* Copy the data into the page, so we can start over. */
3362 ret = trace_seq_to_buffer(&iter->seq,
3363 page_address(spd.pages[i]),
3366 __free_page(spd.pages[i]);
3369 spd.partial[i].offset = 0;
3370 spd.partial[i].len = iter->seq.len;
3372 trace_seq_init(&iter->seq);
3375 trace_access_unlock(iter->cpu_file);
3376 trace_event_read_unlock();
3377 mutex_unlock(&iter->mutex);
3381 ret = splice_to_pipe(pipe, &spd);
3383 splice_shrink_spd(pipe, &spd);
3387 mutex_unlock(&iter->mutex);
3392 tracing_entries_read(struct file *filp, char __user *ubuf,
3393 size_t cnt, loff_t *ppos)
3395 struct trace_array *tr = filp->private_data;
3399 mutex_lock(&trace_types_lock);
3400 if (!ring_buffer_expanded)
3401 r = sprintf(buf, "%lu (expanded: %lu)\n",
3403 trace_buf_size >> 10);
3405 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3406 mutex_unlock(&trace_types_lock);
3408 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3412 tracing_entries_write(struct file *filp, const char __user *ubuf,
3413 size_t cnt, loff_t *ppos)
3419 if (cnt >= sizeof(buf))
3422 if (copy_from_user(&buf, ubuf, cnt))
3427 ret = strict_strtoul(buf, 10, &val);
3431 /* must have at least 1 entry */
3435 mutex_lock(&trace_types_lock);
3439 /* disable all cpu buffers */
3440 for_each_tracing_cpu(cpu) {
3441 if (global_trace.data[cpu])
3442 atomic_inc(&global_trace.data[cpu]->disabled);
3443 if (max_tr.data[cpu])