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;
537 } else if (parser->idx < parser->size - 1) {
539 parser->buffer[parser->idx++] = ch;
552 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
560 if (s->len <= s->readpos)
563 len = s->len - s->readpos;
566 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
576 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
581 if (s->len <= s->readpos)
584 len = s->len - s->readpos;
587 ret = memcpy(buf, s->buffer + s->readpos, cnt);
596 * ftrace_max_lock is used to protect the swapping of buffers
597 * when taking a max snapshot. The buffers themselves are
598 * protected by per_cpu spinlocks. But the action of the swap
599 * needs its own lock.
601 * This is defined as a arch_spinlock_t in order to help
602 * with performance when lockdep debugging is enabled.
604 * It is also used in other places outside the update_max_tr
605 * so it needs to be defined outside of the
606 * CONFIG_TRACER_MAX_TRACE.
608 static arch_spinlock_t ftrace_max_lock =
609 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
611 unsigned long __read_mostly tracing_thresh;
613 #ifdef CONFIG_TRACER_MAX_TRACE
614 unsigned long __read_mostly tracing_max_latency;
617 * Copy the new maximum trace into the separate maximum-trace
618 * structure. (this way the maximum trace is permanently saved,
619 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
622 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
624 struct trace_array_cpu *data = tr->data[cpu];
625 struct trace_array_cpu *max_data;
628 max_tr.time_start = data->preempt_timestamp;
630 max_data = max_tr.data[cpu];
631 max_data->saved_latency = tracing_max_latency;
632 max_data->critical_start = data->critical_start;
633 max_data->critical_end = data->critical_end;
635 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
636 max_data->pid = tsk->pid;
638 * If tsk == current, then use current_uid(), as that does not use
639 * RCU. The irq tracer can be called out of RCU scope.
642 max_data->uid = current_uid();
644 max_data->uid = task_uid(tsk);
646 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
647 max_data->policy = tsk->policy;
648 max_data->rt_priority = tsk->rt_priority;
650 /* record this tasks comm */
651 tracing_record_cmdline(tsk);
655 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
657 * @tsk: the task with the latency
658 * @cpu: The cpu that initiated the trace.
660 * Flip the buffers between the @tr and the max_tr and record information
661 * about which task was the cause of this latency.
664 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
666 struct ring_buffer *buf;
668 if (trace_stop_count)
671 WARN_ON_ONCE(!irqs_disabled());
672 if (!current_trace->use_max_tr) {
676 arch_spin_lock(&ftrace_max_lock);
679 tr->buffer = max_tr.buffer;
682 __update_max_tr(tr, tsk, cpu);
683 arch_spin_unlock(&ftrace_max_lock);
687 * update_max_tr_single - only copy one trace over, and reset the rest
689 * @tsk - task with the latency
690 * @cpu - the cpu of the buffer to copy.
692 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
695 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
699 if (trace_stop_count)
702 WARN_ON_ONCE(!irqs_disabled());
703 if (!current_trace->use_max_tr) {
708 arch_spin_lock(&ftrace_max_lock);
710 ftrace_disable_cpu();
712 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
716 * We failed to swap the buffer due to a commit taking
717 * place on this CPU. We fail to record, but we reset
718 * the max trace buffer (no one writes directly to it)
719 * and flag that it failed.
721 trace_array_printk(&max_tr, _THIS_IP_,
722 "Failed to swap buffers due to commit in progress\n");
727 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
729 __update_max_tr(tr, tsk, cpu);
730 arch_spin_unlock(&ftrace_max_lock);
732 #endif /* CONFIG_TRACER_MAX_TRACE */
735 * register_tracer - register a tracer with the ftrace system.
736 * @type - the plugin for the tracer
738 * Register a new plugin tracer.
740 int register_tracer(struct tracer *type)
741 __releases(kernel_lock)
742 __acquires(kernel_lock)
748 pr_info("Tracer must have a name\n");
752 if (strlen(type->name) >= MAX_TRACER_SIZE) {
753 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
757 mutex_lock(&trace_types_lock);
759 tracing_selftest_running = true;
761 for (t = trace_types; t; t = t->next) {
762 if (strcmp(type->name, t->name) == 0) {
764 pr_info("Tracer %s already registered\n",
772 type->set_flag = &dummy_set_flag;
774 type->flags = &dummy_tracer_flags;
776 if (!type->flags->opts)
777 type->flags->opts = dummy_tracer_opt;
778 if (!type->wait_pipe)
779 type->wait_pipe = default_wait_pipe;
782 #ifdef CONFIG_FTRACE_STARTUP_TEST
783 if (type->selftest && !tracing_selftest_disabled) {
784 struct tracer *saved_tracer = current_trace;
785 struct trace_array *tr = &global_trace;
788 * Run a selftest on this tracer.
789 * Here we reset the trace buffer, and set the current
790 * tracer to be this tracer. The tracer can then run some
791 * internal tracing to verify that everything is in order.
792 * If we fail, we do not register this tracer.
794 tracing_reset_online_cpus(tr);
796 current_trace = type;
798 /* If we expanded the buffers, make sure the max is expanded too */
799 if (ring_buffer_expanded && type->use_max_tr)
800 ring_buffer_resize(max_tr.buffer, trace_buf_size);
802 /* the test is responsible for initializing and enabling */
803 pr_info("Testing tracer %s: ", type->name);
804 ret = type->selftest(type, tr);
805 /* the test is responsible for resetting too */
806 current_trace = saved_tracer;
808 printk(KERN_CONT "FAILED!\n");
811 /* Only reset on passing, to avoid touching corrupted buffers */
812 tracing_reset_online_cpus(tr);
814 /* Shrink the max buffer again */
815 if (ring_buffer_expanded && type->use_max_tr)
816 ring_buffer_resize(max_tr.buffer, 1);
818 printk(KERN_CONT "PASSED\n");
822 type->next = trace_types;
826 tracing_selftest_running = false;
827 mutex_unlock(&trace_types_lock);
829 if (ret || !default_bootup_tracer)
832 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
835 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
836 /* Do we want this tracer to start on bootup? */
837 tracing_set_tracer(type->name);
838 default_bootup_tracer = NULL;
839 /* disable other selftests, since this will break it. */
840 tracing_selftest_disabled = 1;
841 #ifdef CONFIG_FTRACE_STARTUP_TEST
842 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
850 void unregister_tracer(struct tracer *type)
854 mutex_lock(&trace_types_lock);
855 for (t = &trace_types; *t; t = &(*t)->next) {
859 pr_info("Tracer %s not registered\n", type->name);
865 if (type == current_trace && tracer_enabled) {
868 if (current_trace->stop)
869 current_trace->stop(&global_trace);
870 current_trace = &nop_trace;
873 mutex_unlock(&trace_types_lock);
876 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
878 ftrace_disable_cpu();
879 ring_buffer_reset_cpu(buffer, cpu);
883 void tracing_reset(struct trace_array *tr, int cpu)
885 struct ring_buffer *buffer = tr->buffer;
887 ring_buffer_record_disable(buffer);
889 /* Make sure all commits have finished */
891 __tracing_reset(buffer, cpu);
893 ring_buffer_record_enable(buffer);
896 void tracing_reset_online_cpus(struct trace_array *tr)
898 struct ring_buffer *buffer = tr->buffer;
901 ring_buffer_record_disable(buffer);
903 /* Make sure all commits have finished */
906 tr->time_start = ftrace_now(tr->cpu);
908 for_each_online_cpu(cpu)
909 __tracing_reset(buffer, cpu);
911 ring_buffer_record_enable(buffer);
914 void tracing_reset_current(int cpu)
916 tracing_reset(&global_trace, cpu);
919 void tracing_reset_current_online_cpus(void)
921 tracing_reset_online_cpus(&global_trace);
924 #define SAVED_CMDLINES 128
925 #define NO_CMDLINE_MAP UINT_MAX
926 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
927 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
928 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
929 static int cmdline_idx;
930 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
932 /* temporary disable recording */
933 static atomic_t trace_record_cmdline_disabled __read_mostly;
935 static void trace_init_cmdlines(void)
937 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
938 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
942 int is_tracing_stopped(void)
944 return trace_stop_count;
948 * ftrace_off_permanent - disable all ftrace code permanently
950 * This should only be called when a serious anomally has
951 * been detected. This will turn off the function tracing,
952 * ring buffers, and other tracing utilites. It takes no
953 * locks and can be called from any context.
955 void ftrace_off_permanent(void)
957 tracing_disabled = 1;
959 tracing_off_permanent();
963 * tracing_start - quick start of the tracer
965 * If tracing is enabled but was stopped by tracing_stop,
966 * this will start the tracer back up.
968 void tracing_start(void)
970 struct ring_buffer *buffer;
973 if (tracing_disabled)
976 raw_spin_lock_irqsave(&tracing_start_lock, flags);
977 if (--trace_stop_count) {
978 if (trace_stop_count < 0) {
979 /* Someone screwed up their debugging */
981 trace_stop_count = 0;
986 /* Prevent the buffers from switching */
987 arch_spin_lock(&ftrace_max_lock);
989 buffer = global_trace.buffer;
991 ring_buffer_record_enable(buffer);
993 buffer = max_tr.buffer;
995 ring_buffer_record_enable(buffer);
997 arch_spin_unlock(&ftrace_max_lock);
1001 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1005 * tracing_stop - quick stop of the tracer
1007 * Light weight way to stop tracing. Use in conjunction with
1010 void tracing_stop(void)
1012 struct ring_buffer *buffer;
1013 unsigned long flags;
1016 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1017 if (trace_stop_count++)
1020 /* Prevent the buffers from switching */
1021 arch_spin_lock(&ftrace_max_lock);
1023 buffer = global_trace.buffer;
1025 ring_buffer_record_disable(buffer);
1027 buffer = max_tr.buffer;
1029 ring_buffer_record_disable(buffer);
1031 arch_spin_unlock(&ftrace_max_lock);
1034 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1037 void trace_stop_cmdline_recording(void);
1039 static void trace_save_cmdline(struct task_struct *tsk)
1043 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1047 * It's not the end of the world if we don't get
1048 * the lock, but we also don't want to spin
1049 * nor do we want to disable interrupts,
1050 * so if we miss here, then better luck next time.
1052 if (!arch_spin_trylock(&trace_cmdline_lock))
1055 idx = map_pid_to_cmdline[tsk->pid];
1056 if (idx == NO_CMDLINE_MAP) {
1057 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1060 * Check whether the cmdline buffer at idx has a pid
1061 * mapped. We are going to overwrite that entry so we
1062 * need to clear the map_pid_to_cmdline. Otherwise we
1063 * would read the new comm for the old pid.
1065 pid = map_cmdline_to_pid[idx];
1066 if (pid != NO_CMDLINE_MAP)
1067 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1069 map_cmdline_to_pid[idx] = tsk->pid;
1070 map_pid_to_cmdline[tsk->pid] = idx;
1075 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1077 arch_spin_unlock(&trace_cmdline_lock);
1080 void trace_find_cmdline(int pid, char comm[])
1085 strcpy(comm, "<idle>");
1089 if (WARN_ON_ONCE(pid < 0)) {
1090 strcpy(comm, "<XXX>");
1094 if (pid > PID_MAX_DEFAULT) {
1095 strcpy(comm, "<...>");
1100 arch_spin_lock(&trace_cmdline_lock);
1101 map = map_pid_to_cmdline[pid];
1102 if (map != NO_CMDLINE_MAP)
1103 strcpy(comm, saved_cmdlines[map]);
1105 strcpy(comm, "<...>");
1107 arch_spin_unlock(&trace_cmdline_lock);
1111 void tracing_record_cmdline(struct task_struct *tsk)
1113 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1117 trace_save_cmdline(tsk);
1121 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1124 struct task_struct *tsk = current;
1126 entry->preempt_count = pc & 0xff;
1127 entry->pid = (tsk) ? tsk->pid : 0;
1130 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1131 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1133 TRACE_FLAG_IRQS_NOSUPPORT |
1135 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1136 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1137 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1139 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1141 struct ring_buffer_event *
1142 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1145 unsigned long flags, int pc)
1147 struct ring_buffer_event *event;
1149 event = ring_buffer_lock_reserve(buffer, len);
1150 if (event != NULL) {
1151 struct trace_entry *ent = ring_buffer_event_data(event);
1153 tracing_generic_entry_update(ent, flags, pc);
1161 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1162 struct ring_buffer_event *event,
1163 unsigned long flags, int pc,
1166 ring_buffer_unlock_commit(buffer, event);
1168 ftrace_trace_stack(buffer, flags, 6, pc);
1169 ftrace_trace_userstack(buffer, flags, pc);
1175 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1176 struct ring_buffer_event *event,
1177 unsigned long flags, int pc)
1179 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1182 struct ring_buffer_event *
1183 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1184 int type, unsigned long len,
1185 unsigned long flags, int pc)
1187 *current_rb = global_trace.buffer;
1188 return trace_buffer_lock_reserve(*current_rb,
1189 type, len, flags, pc);
1191 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1193 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1194 struct ring_buffer_event *event,
1195 unsigned long flags, int pc)
1197 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1199 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1201 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1202 struct ring_buffer_event *event,
1203 unsigned long flags, int pc)
1205 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1207 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1209 void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1210 struct ring_buffer_event *event,
1211 unsigned long flags, int pc,
1212 struct pt_regs *regs)
1214 ring_buffer_unlock_commit(buffer, event);
1216 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1217 ftrace_trace_userstack(buffer, flags, pc);
1219 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1221 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1222 struct ring_buffer_event *event)
1224 ring_buffer_discard_commit(buffer, event);
1226 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1229 trace_function(struct trace_array *tr,
1230 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1233 struct ftrace_event_call *call = &event_function;
1234 struct ring_buffer *buffer = tr->buffer;
1235 struct ring_buffer_event *event;
1236 struct ftrace_entry *entry;
1238 /* If we are reading the ring buffer, don't trace */
1239 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1242 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1246 entry = ring_buffer_event_data(event);
1248 entry->parent_ip = parent_ip;
1250 if (!filter_check_discard(call, entry, buffer, event))
1251 ring_buffer_unlock_commit(buffer, event);
1255 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1256 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1259 if (likely(!atomic_read(&data->disabled)))
1260 trace_function(tr, ip, parent_ip, flags, pc);
1263 #ifdef CONFIG_STACKTRACE
1265 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1266 struct ftrace_stack {
1267 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1270 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1271 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1273 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1274 unsigned long flags,
1275 int skip, int pc, struct pt_regs *regs)
1277 struct ftrace_event_call *call = &event_kernel_stack;
1278 struct ring_buffer_event *event;
1279 struct stack_entry *entry;
1280 struct stack_trace trace;
1282 int size = FTRACE_STACK_ENTRIES;
1284 trace.nr_entries = 0;
1288 * Since events can happen in NMIs there's no safe way to
1289 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1290 * or NMI comes in, it will just have to use the default
1291 * FTRACE_STACK_SIZE.
1293 preempt_disable_notrace();
1295 use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1297 * We don't need any atomic variables, just a barrier.
1298 * If an interrupt comes in, we don't care, because it would
1299 * have exited and put the counter back to what we want.
1300 * We just need a barrier to keep gcc from moving things
1304 if (use_stack == 1) {
1305 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1306 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1309 save_stack_trace_regs(regs, &trace);
1311 save_stack_trace(&trace);
1313 if (trace.nr_entries > size)
1314 size = trace.nr_entries;
1316 /* From now on, use_stack is a boolean */
1319 size *= sizeof(unsigned long);
1321 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1322 sizeof(*entry) + size, flags, pc);
1325 entry = ring_buffer_event_data(event);
1327 memset(&entry->caller, 0, size);
1330 memcpy(&entry->caller, trace.entries,
1331 trace.nr_entries * sizeof(unsigned long));
1333 trace.max_entries = FTRACE_STACK_ENTRIES;
1334 trace.entries = entry->caller;
1336 save_stack_trace_regs(regs, &trace);
1338 save_stack_trace(&trace);
1341 entry->size = trace.nr_entries;
1343 if (!filter_check_discard(call, entry, buffer, event))
1344 ring_buffer_unlock_commit(buffer, event);
1347 /* Again, don't let gcc optimize things here */
1349 __get_cpu_var(ftrace_stack_reserve)--;
1350 preempt_enable_notrace();
1354 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1355 int skip, int pc, struct pt_regs *regs)
1357 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1360 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1363 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1366 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1369 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1372 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1375 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1379 * trace_dump_stack - record a stack back trace in the trace buffer
1381 void trace_dump_stack(void)
1383 unsigned long flags;
1385 if (tracing_disabled || tracing_selftest_running)
1388 local_save_flags(flags);
1390 /* skipping 3 traces, seems to get us at the caller of this function */
1391 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1394 static DEFINE_PER_CPU(int, user_stack_count);
1397 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1399 struct ftrace_event_call *call = &event_user_stack;
1400 struct ring_buffer_event *event;
1401 struct userstack_entry *entry;
1402 struct stack_trace trace;
1404 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1408 * NMIs can not handle page faults, even with fix ups.
1409 * The save user stack can (and often does) fault.
1411 if (unlikely(in_nmi()))
1415 * prevent recursion, since the user stack tracing may
1416 * trigger other kernel events.
1419 if (__this_cpu_read(user_stack_count))
1422 __this_cpu_inc(user_stack_count);
1424 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1425 sizeof(*entry), flags, pc);
1427 goto out_drop_count;
1428 entry = ring_buffer_event_data(event);
1430 entry->tgid = current->tgid;
1431 memset(&entry->caller, 0, sizeof(entry->caller));
1433 trace.nr_entries = 0;
1434 trace.max_entries = FTRACE_STACK_ENTRIES;
1436 trace.entries = entry->caller;
1438 save_stack_trace_user(&trace);
1439 if (!filter_check_discard(call, entry, buffer, event))
1440 ring_buffer_unlock_commit(buffer, event);
1443 __this_cpu_dec(user_stack_count);
1449 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1451 ftrace_trace_userstack(tr, flags, preempt_count());
1455 #endif /* CONFIG_STACKTRACE */
1458 * trace_vbprintk - write binary msg to tracing buffer
1461 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1463 static arch_spinlock_t trace_buf_lock =
1464 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1465 static u32 trace_buf[TRACE_BUF_SIZE];
1467 struct ftrace_event_call *call = &event_bprint;
1468 struct ring_buffer_event *event;
1469 struct ring_buffer *buffer;
1470 struct trace_array *tr = &global_trace;
1471 struct trace_array_cpu *data;
1472 struct bprint_entry *entry;
1473 unsigned long flags;
1475 int cpu, len = 0, size, pc;
1477 if (unlikely(tracing_selftest_running || tracing_disabled))
1480 /* Don't pollute graph traces with trace_vprintk internals */
1481 pause_graph_tracing();
1483 pc = preempt_count();
1484 preempt_disable_notrace();
1485 cpu = raw_smp_processor_id();
1486 data = tr->data[cpu];
1488 disable = atomic_inc_return(&data->disabled);
1489 if (unlikely(disable != 1))
1492 /* Lockdep uses trace_printk for lock tracing */
1493 local_irq_save(flags);
1494 arch_spin_lock(&trace_buf_lock);
1495 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1497 if (len > TRACE_BUF_SIZE || len < 0)
1500 size = sizeof(*entry) + sizeof(u32) * len;
1501 buffer = tr->buffer;
1502 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1506 entry = ring_buffer_event_data(event);
1510 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1511 if (!filter_check_discard(call, entry, buffer, event)) {
1512 ring_buffer_unlock_commit(buffer, event);
1513 ftrace_trace_stack(buffer, flags, 6, pc);
1517 arch_spin_unlock(&trace_buf_lock);
1518 local_irq_restore(flags);
1521 atomic_dec_return(&data->disabled);
1522 preempt_enable_notrace();
1523 unpause_graph_tracing();
1527 EXPORT_SYMBOL_GPL(trace_vbprintk);
1529 int trace_array_printk(struct trace_array *tr,
1530 unsigned long ip, const char *fmt, ...)
1535 if (!(trace_flags & TRACE_ITER_PRINTK))
1539 ret = trace_array_vprintk(tr, ip, fmt, ap);
1544 int trace_array_vprintk(struct trace_array *tr,
1545 unsigned long ip, const char *fmt, va_list args)
1547 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1548 static char trace_buf[TRACE_BUF_SIZE];
1550 struct ftrace_event_call *call = &event_print;
1551 struct ring_buffer_event *event;
1552 struct ring_buffer *buffer;
1553 struct trace_array_cpu *data;
1554 int cpu, len = 0, size, pc;
1555 struct print_entry *entry;
1556 unsigned long irq_flags;
1559 if (tracing_disabled || tracing_selftest_running)
1562 pc = preempt_count();
1563 preempt_disable_notrace();
1564 cpu = raw_smp_processor_id();
1565 data = tr->data[cpu];
1567 disable = atomic_inc_return(&data->disabled);
1568 if (unlikely(disable != 1))
1571 pause_graph_tracing();
1572 raw_local_irq_save(irq_flags);
1573 arch_spin_lock(&trace_buf_lock);
1574 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1576 size = sizeof(*entry) + len + 1;
1577 buffer = tr->buffer;
1578 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1582 entry = ring_buffer_event_data(event);
1585 memcpy(&entry->buf, trace_buf, len);
1586 entry->buf[len] = '\0';
1587 if (!filter_check_discard(call, entry, buffer, event)) {
1588 ring_buffer_unlock_commit(buffer, event);
1589 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1593 arch_spin_unlock(&trace_buf_lock);
1594 raw_local_irq_restore(irq_flags);
1595 unpause_graph_tracing();
1597 atomic_dec_return(&data->disabled);
1598 preempt_enable_notrace();
1603 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1605 return trace_array_vprintk(&global_trace, ip, fmt, args);
1607 EXPORT_SYMBOL_GPL(trace_vprintk);
1609 static void trace_iterator_increment(struct trace_iterator *iter)
1611 /* Don't allow ftrace to trace into the ring buffers */
1612 ftrace_disable_cpu();
1615 if (iter->buffer_iter[iter->cpu])
1616 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1618 ftrace_enable_cpu();
1621 static struct trace_entry *
1622 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1623 unsigned long *lost_events)
1625 struct ring_buffer_event *event;
1626 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1628 /* Don't allow ftrace to trace into the ring buffers */
1629 ftrace_disable_cpu();
1632 event = ring_buffer_iter_peek(buf_iter, ts);
1634 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1637 ftrace_enable_cpu();
1640 iter->ent_size = ring_buffer_event_length(event);
1641 return ring_buffer_event_data(event);
1647 static struct trace_entry *
1648 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1649 unsigned long *missing_events, u64 *ent_ts)
1651 struct ring_buffer *buffer = iter->tr->buffer;
1652 struct trace_entry *ent, *next = NULL;
1653 unsigned long lost_events = 0, next_lost = 0;
1654 int cpu_file = iter->cpu_file;
1655 u64 next_ts = 0, ts;
1661 * If we are in a per_cpu trace file, don't bother by iterating over
1662 * all cpu and peek directly.
1664 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1665 if (ring_buffer_empty_cpu(buffer, cpu_file))
1667 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1669 *ent_cpu = cpu_file;
1674 for_each_tracing_cpu(cpu) {
1676 if (ring_buffer_empty_cpu(buffer, cpu))
1679 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1682 * Pick the entry with the smallest timestamp:
1684 if (ent && (!next || ts < next_ts)) {
1688 next_lost = lost_events;
1689 next_size = iter->ent_size;
1693 iter->ent_size = next_size;
1696 *ent_cpu = next_cpu;
1702 *missing_events = next_lost;
1707 /* Find the next real entry, without updating the iterator itself */
1708 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1709 int *ent_cpu, u64 *ent_ts)
1711 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1714 /* Find the next real entry, and increment the iterator to the next entry */
1715 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1717 iter->ent = __find_next_entry(iter, &iter->cpu,
1718 &iter->lost_events, &iter->ts);
1721 trace_iterator_increment(iter);
1723 return iter->ent ? iter : NULL;
1726 static void trace_consume(struct trace_iterator *iter)
1728 /* Don't allow ftrace to trace into the ring buffers */
1729 ftrace_disable_cpu();
1730 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1731 &iter->lost_events);
1732 ftrace_enable_cpu();
1735 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1737 struct trace_iterator *iter = m->private;
1741 WARN_ON_ONCE(iter->leftover);
1745 /* can't go backwards */
1750 ent = trace_find_next_entry_inc(iter);
1754 while (ent && iter->idx < i)
1755 ent = trace_find_next_entry_inc(iter);
1762 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1764 struct trace_array *tr = iter->tr;
1765 struct ring_buffer_event *event;
1766 struct ring_buffer_iter *buf_iter;
1767 unsigned long entries = 0;
1770 tr->data[cpu]->skipped_entries = 0;
1772 if (!iter->buffer_iter[cpu])
1775 buf_iter = iter->buffer_iter[cpu];
1776 ring_buffer_iter_reset(buf_iter);
1779 * We could have the case with the max latency tracers
1780 * that a reset never took place on a cpu. This is evident
1781 * by the timestamp being before the start of the buffer.
1783 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1784 if (ts >= iter->tr->time_start)
1787 ring_buffer_read(buf_iter, NULL);
1790 tr->data[cpu]->skipped_entries = entries;
1794 * The current tracer is copied to avoid a global locking
1797 static void *s_start(struct seq_file *m, loff_t *pos)
1799 struct trace_iterator *iter = m->private;
1800 static struct tracer *old_tracer;
1801 int cpu_file = iter->cpu_file;
1806 /* copy the tracer to avoid using a global lock all around */
1807 mutex_lock(&trace_types_lock);
1808 if (unlikely(old_tracer != current_trace && current_trace)) {
1809 old_tracer = current_trace;
1810 *iter->trace = *current_trace;
1812 mutex_unlock(&trace_types_lock);
1814 atomic_inc(&trace_record_cmdline_disabled);
1816 if (*pos != iter->pos) {
1821 ftrace_disable_cpu();
1823 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1824 for_each_tracing_cpu(cpu)
1825 tracing_iter_reset(iter, cpu);
1827 tracing_iter_reset(iter, cpu_file);
1829 ftrace_enable_cpu();
1832 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1837 * If we overflowed the seq_file before, then we want
1838 * to just reuse the trace_seq buffer again.
1844 p = s_next(m, p, &l);
1848 trace_event_read_lock();
1849 trace_access_lock(cpu_file);
1853 static void s_stop(struct seq_file *m, void *p)
1855 struct trace_iterator *iter = m->private;
1857 atomic_dec(&trace_record_cmdline_disabled);
1858 trace_access_unlock(iter->cpu_file);
1859 trace_event_read_unlock();
1862 static void print_lat_help_header(struct seq_file *m)
1864 seq_puts(m, "# _------=> CPU# \n");
1865 seq_puts(m, "# / _-----=> irqs-off \n");
1866 seq_puts(m, "# | / _----=> need-resched \n");
1867 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1868 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1869 seq_puts(m, "# |||| / delay \n");
1870 seq_puts(m, "# cmd pid ||||| time | caller \n");
1871 seq_puts(m, "# \\ / ||||| \\ | / \n");
1874 static void print_func_help_header(struct seq_file *m)
1876 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1877 seq_puts(m, "# | | | | |\n");
1882 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1884 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1885 struct trace_array *tr = iter->tr;
1886 struct trace_array_cpu *data = tr->data[tr->cpu];
1887 struct tracer *type = current_trace;
1888 unsigned long entries = 0;
1889 unsigned long total = 0;
1890 unsigned long count;
1891 const char *name = "preemption";
1898 for_each_tracing_cpu(cpu) {
1899 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1901 * If this buffer has skipped entries, then we hold all
1902 * entries for the trace and we need to ignore the
1903 * ones before the time stamp.
1905 if (tr->data[cpu]->skipped_entries) {
1906 count -= tr->data[cpu]->skipped_entries;
1907 /* total is the same as the entries */
1911 ring_buffer_overrun_cpu(tr->buffer, cpu);
1915 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1917 seq_puts(m, "# -----------------------------------"
1918 "---------------------------------\n");
1919 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1920 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1921 nsecs_to_usecs(data->saved_latency),
1925 #if defined(CONFIG_PREEMPT_NONE)
1927 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1929 #elif defined(CONFIG_PREEMPT)
1934 /* These are reserved for later use */
1937 seq_printf(m, " #P:%d)\n", num_online_cpus());
1941 seq_puts(m, "# -----------------\n");
1942 seq_printf(m, "# | task: %.16s-%d "
1943 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1944 data->comm, data->pid, data->uid, data->nice,
1945 data->policy, data->rt_priority);
1946 seq_puts(m, "# -----------------\n");
1948 if (data->critical_start) {
1949 seq_puts(m, "# => started at: ");
1950 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1951 trace_print_seq(m, &iter->seq);
1952 seq_puts(m, "\n# => ended at: ");
1953 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1954 trace_print_seq(m, &iter->seq);
1955 seq_puts(m, "\n#\n");
1961 static void test_cpu_buff_start(struct trace_iterator *iter)
1963 struct trace_seq *s = &iter->seq;
1965 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1968 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1971 if (cpumask_test_cpu(iter->cpu, iter->started))
1974 if (iter->tr->data[iter->cpu]->skipped_entries)
1977 cpumask_set_cpu(iter->cpu, iter->started);
1979 /* Don't print started cpu buffer for the first entry of the trace */
1981 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1985 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1987 struct trace_seq *s = &iter->seq;
1988 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1989 struct trace_entry *entry;
1990 struct trace_event *event;
1994 test_cpu_buff_start(iter);
1996 event = ftrace_find_event(entry->type);
1998 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1999 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2000 if (!trace_print_lat_context(iter))
2003 if (!trace_print_context(iter))
2009 return event->funcs->trace(iter, sym_flags, event);
2011 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2014 return TRACE_TYPE_HANDLED;
2016 return TRACE_TYPE_PARTIAL_LINE;
2019 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2021 struct trace_seq *s = &iter->seq;
2022 struct trace_entry *entry;
2023 struct trace_event *event;
2027 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2028 if (!trace_seq_printf(s, "%d %d %llu ",
2029 entry->pid, iter->cpu, iter->ts))
2033 event = ftrace_find_event(entry->type);
2035 return event->funcs->raw(iter, 0, event);
2037 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2040 return TRACE_TYPE_HANDLED;
2042 return TRACE_TYPE_PARTIAL_LINE;
2045 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2047 struct trace_seq *s = &iter->seq;
2048 unsigned char newline = '\n';
2049 struct trace_entry *entry;
2050 struct trace_event *event;
2054 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2055 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2056 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2057 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2060 event = ftrace_find_event(entry->type);
2062 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2063 if (ret != TRACE_TYPE_HANDLED)
2067 SEQ_PUT_FIELD_RET(s, newline);
2069 return TRACE_TYPE_HANDLED;
2072 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2074 struct trace_seq *s = &iter->seq;
2075 struct trace_entry *entry;
2076 struct trace_event *event;
2080 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2081 SEQ_PUT_FIELD_RET(s, entry->pid);
2082 SEQ_PUT_FIELD_RET(s, iter->cpu);
2083 SEQ_PUT_FIELD_RET(s, iter->ts);
2086 event = ftrace_find_event(entry->type);
2087 return event ? event->funcs->binary(iter, 0, event) :
2091 int trace_empty(struct trace_iterator *iter)
2095 /* If we are looking at one CPU buffer, only check that one */
2096 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2097 cpu = iter->cpu_file;
2098 if (iter->buffer_iter[cpu]) {
2099 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2102 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2108 for_each_tracing_cpu(cpu) {
2109 if (iter->buffer_iter[cpu]) {
2110 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2113 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2121 /* Called with trace_event_read_lock() held. */
2122 enum print_line_t print_trace_line(struct trace_iterator *iter)
2124 enum print_line_t ret;
2126 if (iter->lost_events &&
2127 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2128 iter->cpu, iter->lost_events))
2129 return TRACE_TYPE_PARTIAL_LINE;
2131 if (iter->trace && iter->trace->print_line) {
2132 ret = iter->trace->print_line(iter);
2133 if (ret != TRACE_TYPE_UNHANDLED)
2137 if (iter->ent->type == TRACE_BPRINT &&
2138 trace_flags & TRACE_ITER_PRINTK &&
2139 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2140 return trace_print_bprintk_msg_only(iter);
2142 if (iter->ent->type == TRACE_PRINT &&
2143 trace_flags & TRACE_ITER_PRINTK &&
2144 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2145 return trace_print_printk_msg_only(iter);
2147 if (trace_flags & TRACE_ITER_BIN)
2148 return print_bin_fmt(iter);
2150 if (trace_flags & TRACE_ITER_HEX)
2151 return print_hex_fmt(iter);
2153 if (trace_flags & TRACE_ITER_RAW)
2154 return print_raw_fmt(iter);
2156 return print_trace_fmt(iter);
2159 void trace_default_header(struct seq_file *m)
2161 struct trace_iterator *iter = m->private;
2163 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2166 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2167 /* print nothing if the buffers are empty */
2168 if (trace_empty(iter))
2170 print_trace_header(m, iter);
2171 if (!(trace_flags & TRACE_ITER_VERBOSE))
2172 print_lat_help_header(m);
2174 if (!(trace_flags & TRACE_ITER_VERBOSE))
2175 print_func_help_header(m);
2179 static void test_ftrace_alive(struct seq_file *m)
2181 if (!ftrace_is_dead())
2183 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2184 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2187 static int s_show(struct seq_file *m, void *v)
2189 struct trace_iterator *iter = v;
2192 if (iter->ent == NULL) {
2194 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2196 test_ftrace_alive(m);
2198 if (iter->trace && iter->trace->print_header)
2199 iter->trace->print_header(m);
2201 trace_default_header(m);
2203 } else if (iter->leftover) {
2205 * If we filled the seq_file buffer earlier, we
2206 * want to just show it now.
2208 ret = trace_print_seq(m, &iter->seq);
2210 /* ret should this time be zero, but you never know */
2211 iter->leftover = ret;
2214 print_trace_line(iter);
2215 ret = trace_print_seq(m, &iter->seq);
2217 * If we overflow the seq_file buffer, then it will
2218 * ask us for this data again at start up.
2220 * ret is 0 if seq_file write succeeded.
2223 iter->leftover = ret;
2229 static const struct seq_operations tracer_seq_ops = {
2236 static struct trace_iterator *
2237 __tracing_open(struct inode *inode, struct file *file)
2239 long cpu_file = (long) inode->i_private;
2240 void *fail_ret = ERR_PTR(-ENOMEM);
2241 struct trace_iterator *iter;
2245 if (tracing_disabled)
2246 return ERR_PTR(-ENODEV);
2248 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2250 return ERR_PTR(-ENOMEM);
2253 * We make a copy of the current tracer to avoid concurrent
2254 * changes on it while we are reading.
2256 mutex_lock(&trace_types_lock);
2257 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2262 *iter->trace = *current_trace;
2264 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2267 if (current_trace && current_trace->print_max)
2270 iter->tr = &global_trace;
2272 mutex_init(&iter->mutex);
2273 iter->cpu_file = cpu_file;
2275 /* Notify the tracer early; before we stop tracing. */
2276 if (iter->trace && iter->trace->open)
2277 iter->trace->open(iter);
2279 /* Annotate start of buffers if we had overruns */
2280 if (ring_buffer_overruns(iter->tr->buffer))
2281 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2283 /* stop the trace while dumping */
2286 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2287 for_each_tracing_cpu(cpu) {
2288 iter->buffer_iter[cpu] =
2289 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2291 ring_buffer_read_prepare_sync();
2292 for_each_tracing_cpu(cpu) {
2293 ring_buffer_read_start(iter->buffer_iter[cpu]);
2294 tracing_iter_reset(iter, cpu);
2297 cpu = iter->cpu_file;
2298 iter->buffer_iter[cpu] =
2299 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2300 ring_buffer_read_prepare_sync();
2301 ring_buffer_read_start(iter->buffer_iter[cpu]);
2302 tracing_iter_reset(iter, cpu);
2305 ret = seq_open(file, &tracer_seq_ops);
2307 fail_ret = ERR_PTR(ret);
2311 m = file->private_data;
2314 mutex_unlock(&trace_types_lock);
2319 for_each_tracing_cpu(cpu) {
2320 if (iter->buffer_iter[cpu])
2321 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2323 free_cpumask_var(iter->started);
2326 mutex_unlock(&trace_types_lock);
2333 int tracing_open_generic(struct inode *inode, struct file *filp)
2335 if (tracing_disabled)
2338 filp->private_data = inode->i_private;
2342 static int tracing_release(struct inode *inode, struct file *file)
2344 struct seq_file *m = file->private_data;
2345 struct trace_iterator *iter;
2348 if (!(file->f_mode & FMODE_READ))
2353 mutex_lock(&trace_types_lock);
2354 for_each_tracing_cpu(cpu) {
2355 if (iter->buffer_iter[cpu])
2356 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2359 if (iter->trace && iter->trace->close)
2360 iter->trace->close(iter);
2362 /* reenable tracing if it was previously enabled */
2364 mutex_unlock(&trace_types_lock);
2366 seq_release(inode, file);
2367 mutex_destroy(&iter->mutex);
2368 free_cpumask_var(iter->started);
2374 static int tracing_open(struct inode *inode, struct file *file)
2376 struct trace_iterator *iter;
2379 /* If this file was open for write, then erase contents */
2380 if ((file->f_mode & FMODE_WRITE) &&
2381 (file->f_flags & O_TRUNC)) {
2382 long cpu = (long) inode->i_private;
2384 if (cpu == TRACE_PIPE_ALL_CPU)
2385 tracing_reset_online_cpus(&global_trace);
2387 tracing_reset(&global_trace, cpu);
2390 if (file->f_mode & FMODE_READ) {
2391 iter = __tracing_open(inode, file);
2393 ret = PTR_ERR(iter);
2394 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2395 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2401 t_next(struct seq_file *m, void *v, loff_t *pos)
2403 struct tracer *t = v;
2413 static void *t_start(struct seq_file *m, loff_t *pos)
2418 mutex_lock(&trace_types_lock);
2419 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2425 static void t_stop(struct seq_file *m, void *p)
2427 mutex_unlock(&trace_types_lock);
2430 static int t_show(struct seq_file *m, void *v)
2432 struct tracer *t = v;
2437 seq_printf(m, "%s", t->name);
2446 static const struct seq_operations show_traces_seq_ops = {
2453 static int show_traces_open(struct inode *inode, struct file *file)
2455 if (tracing_disabled)
2458 return seq_open(file, &show_traces_seq_ops);
2462 tracing_write_stub(struct file *filp, const char __user *ubuf,
2463 size_t count, loff_t *ppos)
2468 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2470 if (file->f_mode & FMODE_READ)
2471 return seq_lseek(file, offset, origin);
2476 static const struct file_operations tracing_fops = {
2477 .open = tracing_open,
2479 .write = tracing_write_stub,
2480 .llseek = tracing_seek,
2481 .release = tracing_release,
2484 static const struct file_operations show_traces_fops = {
2485 .open = show_traces_open,
2487 .release = seq_release,
2488 .llseek = seq_lseek,
2492 * Only trace on a CPU if the bitmask is set:
2494 static cpumask_var_t tracing_cpumask;
2497 * The tracer itself will not take this lock, but still we want
2498 * to provide a consistent cpumask to user-space:
2500 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2503 * Temporary storage for the character representation of the
2504 * CPU bitmask (and one more byte for the newline):
2506 static char mask_str[NR_CPUS + 1];
2509 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2510 size_t count, loff_t *ppos)
2514 mutex_lock(&tracing_cpumask_update_lock);
2516 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2517 if (count - len < 2) {
2521 len += sprintf(mask_str + len, "\n");
2522 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2525 mutex_unlock(&tracing_cpumask_update_lock);
2531 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2532 size_t count, loff_t *ppos)
2535 cpumask_var_t tracing_cpumask_new;
2537 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2540 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2544 mutex_lock(&tracing_cpumask_update_lock);
2546 local_irq_disable();
2547 arch_spin_lock(&ftrace_max_lock);
2548 for_each_tracing_cpu(cpu) {
2550 * Increase/decrease the disabled counter if we are
2551 * about to flip a bit in the cpumask:
2553 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2554 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2555 atomic_inc(&global_trace.data[cpu]->disabled);
2556 ring_buffer_record_disable_cpu(global_trace.buffer, cpu);
2558 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2559 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2560 atomic_dec(&global_trace.data[cpu]->disabled);
2561 ring_buffer_record_enable_cpu(global_trace.buffer, cpu);
2564 arch_spin_unlock(&ftrace_max_lock);
2567 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2569 mutex_unlock(&tracing_cpumask_update_lock);
2570 free_cpumask_var(tracing_cpumask_new);
2575 free_cpumask_var(tracing_cpumask_new);
2580 static const struct file_operations tracing_cpumask_fops = {
2581 .open = tracing_open_generic,
2582 .read = tracing_cpumask_read,
2583 .write = tracing_cpumask_write,
2584 .llseek = generic_file_llseek,
2587 static int tracing_trace_options_show(struct seq_file *m, void *v)
2589 struct tracer_opt *trace_opts;
2593 mutex_lock(&trace_types_lock);
2594 tracer_flags = current_trace->flags->val;
2595 trace_opts = current_trace->flags->opts;
2597 for (i = 0; trace_options[i]; i++) {
2598 if (trace_flags & (1 << i))
2599 seq_printf(m, "%s\n", trace_options[i]);
2601 seq_printf(m, "no%s\n", trace_options[i]);
2604 for (i = 0; trace_opts[i].name; i++) {
2605 if (tracer_flags & trace_opts[i].bit)
2606 seq_printf(m, "%s\n", trace_opts[i].name);
2608 seq_printf(m, "no%s\n", trace_opts[i].name);
2610 mutex_unlock(&trace_types_lock);
2615 static int __set_tracer_option(struct tracer *trace,
2616 struct tracer_flags *tracer_flags,
2617 struct tracer_opt *opts, int neg)
2621 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2626 tracer_flags->val &= ~opts->bit;
2628 tracer_flags->val |= opts->bit;
2632 /* Try to assign a tracer specific option */
2633 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2635 struct tracer_flags *tracer_flags = trace->flags;
2636 struct tracer_opt *opts = NULL;
2639 for (i = 0; tracer_flags->opts[i].name; i++) {
2640 opts = &tracer_flags->opts[i];
2642 if (strcmp(cmp, opts->name) == 0)
2643 return __set_tracer_option(trace, trace->flags,
2650 /* Some tracers require overwrite to stay enabled */
2651 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
2653 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
2659 int set_tracer_flag(unsigned int mask, int enabled)
2661 /* do nothing if flag is already set */
2662 if (!!(trace_flags & mask) == !!enabled)
2665 /* Give the tracer a chance to approve the change */
2666 if (current_trace->flag_changed)
2667 if (current_trace->flag_changed(current_trace, mask, !!enabled))
2671 trace_flags |= mask;
2673 trace_flags &= ~mask;
2675 if (mask == TRACE_ITER_RECORD_CMD)
2676 trace_event_enable_cmd_record(enabled);
2678 if (mask == TRACE_ITER_OVERWRITE) {
2679 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2680 #ifdef CONFIG_TRACER_MAX_TRACE
2681 ring_buffer_change_overwrite(max_tr.buffer, enabled);
2689 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2690 size_t cnt, loff_t *ppos)
2698 if (cnt >= sizeof(buf))
2701 if (copy_from_user(&buf, ubuf, cnt))
2705 cmp = strstrip(buf);
2707 if (strncmp(cmp, "no", 2) == 0) {
2712 mutex_lock(&trace_types_lock);
2714 for (i = 0; trace_options[i]; i++) {
2715 if (strcmp(cmp, trace_options[i]) == 0) {
2716 ret = set_tracer_flag(1 << i, !neg);
2721 /* If no option could be set, test the specific tracer options */
2722 if (!trace_options[i])
2723 ret = set_tracer_option(current_trace, cmp, neg);
2725 mutex_unlock(&trace_types_lock);
2735 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2737 if (tracing_disabled)
2739 return single_open(file, tracing_trace_options_show, NULL);
2742 static const struct file_operations tracing_iter_fops = {
2743 .open = tracing_trace_options_open,
2745 .llseek = seq_lseek,
2746 .release = single_release,
2747 .write = tracing_trace_options_write,
2750 static const char readme_msg[] =
2751 "tracing mini-HOWTO:\n\n"
2752 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2753 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2754 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2755 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2757 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2758 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2760 "# cat /sys/kernel/debug/tracing/trace_options\n"
2761 "noprint-parent nosym-offset nosym-addr noverbose\n"
2762 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2763 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2764 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2765 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2769 tracing_readme_read(struct file *filp, char __user *ubuf,
2770 size_t cnt, loff_t *ppos)
2772 return simple_read_from_buffer(ubuf, cnt, ppos,
2773 readme_msg, strlen(readme_msg));
2776 static const struct file_operations tracing_readme_fops = {
2777 .open = tracing_open_generic,
2778 .read = tracing_readme_read,
2779 .llseek = generic_file_llseek,
2783 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2784 size_t cnt, loff_t *ppos)
2793 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2797 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2805 for (i = 0; i < SAVED_CMDLINES; i++) {
2808 pid = map_cmdline_to_pid[i];
2809 if (pid == -1 || pid == NO_CMDLINE_MAP)
2812 trace_find_cmdline(pid, buf_comm);
2813 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2818 len = simple_read_from_buffer(ubuf, cnt, ppos,
2827 static const struct file_operations tracing_saved_cmdlines_fops = {
2828 .open = tracing_open_generic,
2829 .read = tracing_saved_cmdlines_read,
2830 .llseek = generic_file_llseek,
2834 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2835 size_t cnt, loff_t *ppos)
2840 r = sprintf(buf, "%u\n", tracer_enabled);
2841 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2845 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2846 size_t cnt, loff_t *ppos)
2848 struct trace_array *tr = filp->private_data;
2852 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
2858 mutex_lock(&trace_types_lock);
2859 if (tracer_enabled ^ val) {
2861 /* Only need to warn if this is used to change the state */
2862 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2866 if (current_trace->start)
2867 current_trace->start(tr);
2872 if (current_trace->stop)
2873 current_trace->stop(tr);
2876 mutex_unlock(&trace_types_lock);
2884 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2885 size_t cnt, loff_t *ppos)
2887 char buf[MAX_TRACER_SIZE+2];
2890 mutex_lock(&trace_types_lock);
2892 r = sprintf(buf, "%s\n", current_trace->name);
2894 r = sprintf(buf, "\n");
2895 mutex_unlock(&trace_types_lock);
2897 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2900 int tracer_init(struct tracer *t, struct trace_array *tr)
2902 tracing_reset_online_cpus(tr);
2906 static int __tracing_resize_ring_buffer(unsigned long size)
2911 * If kernel or user changes the size of the ring buffer
2912 * we use the size that was given, and we can forget about
2913 * expanding it later.
2915 ring_buffer_expanded = 1;
2917 ret = ring_buffer_resize(global_trace.buffer, size);
2921 if (!current_trace->use_max_tr)
2924 ret = ring_buffer_resize(max_tr.buffer, size);
2928 r = ring_buffer_resize(global_trace.buffer,
2929 global_trace.entries);
2932 * AARGH! We are left with different
2933 * size max buffer!!!!
2934 * The max buffer is our "snapshot" buffer.
2935 * When a tracer needs a snapshot (one of the
2936 * latency tracers), it swaps the max buffer
2937 * with the saved snap shot. We succeeded to
2938 * update the size of the main buffer, but failed to
2939 * update the size of the max buffer. But when we tried
2940 * to reset the main buffer to the original size, we
2941 * failed there too. This is very unlikely to
2942 * happen, but if it does, warn and kill all
2946 tracing_disabled = 1;
2951 max_tr.entries = size;
2953 global_trace.entries = size;
2958 static ssize_t tracing_resize_ring_buffer(unsigned long size)
2960 int cpu, ret = size;
2962 mutex_lock(&trace_types_lock);
2966 /* disable all cpu buffers */
2967 for_each_tracing_cpu(cpu) {
2968 if (global_trace.data[cpu])
2969 atomic_inc(&global_trace.data[cpu]->disabled);
2970 if (max_tr.data[cpu])
2971 atomic_inc(&max_tr.data[cpu]->disabled);
2974 if (size != global_trace.entries)
2975 ret = __tracing_resize_ring_buffer(size);
2980 for_each_tracing_cpu(cpu) {
2981 if (global_trace.data[cpu])
2982 atomic_dec(&global_trace.data[cpu]->disabled);
2983 if (max_tr.data[cpu])
2984 atomic_dec(&max_tr.data[cpu]->disabled);
2988 mutex_unlock(&trace_types_lock);
2995 * tracing_update_buffers - used by tracing facility to expand ring buffers
2997 * To save on memory when the tracing is never used on a system with it
2998 * configured in. The ring buffers are set to a minimum size. But once
2999 * a user starts to use the tracing facility, then they need to grow
3000 * to their default size.
3002 * This function is to be called when a tracer is about to be used.
3004 int tracing_update_buffers(void)
3008 mutex_lock(&trace_types_lock);
3009 if (!ring_buffer_expanded)
3010 ret = __tracing_resize_ring_buffer(trace_buf_size);
3011 mutex_unlock(&trace_types_lock);
3016 struct trace_option_dentry;
3018 static struct trace_option_dentry *
3019 create_trace_option_files(struct tracer *tracer);
3022 destroy_trace_option_files(struct trace_option_dentry *topts);
3024 static int tracing_set_tracer(const char *buf)
3026 static struct trace_option_dentry *topts;
3027 struct trace_array *tr = &global_trace;
3031 mutex_lock(&trace_types_lock);
3033 if (!ring_buffer_expanded) {
3034 ret = __tracing_resize_ring_buffer(trace_buf_size);
3040 for (t = trace_types; t; t = t->next) {
3041 if (strcmp(t->name, buf) == 0)
3048 if (t == current_trace)
3051 trace_branch_disable();
3053 current_trace->enabled = false;
3055 if (current_trace && current_trace->reset)
3056 current_trace->reset(tr);
3057 if (current_trace && current_trace->use_max_tr) {
3059 * We don't free the ring buffer. instead, resize it because
3060 * The max_tr ring buffer has some state (e.g. ring->clock) and
3061 * we want preserve it.
3063 ring_buffer_resize(max_tr.buffer, 1);
3066 destroy_trace_option_files(topts);
3070 topts = create_trace_option_files(current_trace);
3071 if (current_trace->use_max_tr) {
3072 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
3075 max_tr.entries = global_trace.entries;
3079 ret = tracer_init(t, tr);
3084 current_trace->enabled = true;
3085 trace_branch_enable(tr);
3087 mutex_unlock(&trace_types_lock);
3093 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3094 size_t cnt, loff_t *ppos)
3096 char buf[MAX_TRACER_SIZE+1];
3103 if (cnt > MAX_TRACER_SIZE)
3104 cnt = MAX_TRACER_SIZE;
3106 if (copy_from_user(&buf, ubuf, cnt))
3111 /* strip ending whitespace. */
3112 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3115 err = tracing_set_tracer(buf);
3125 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3126 size_t cnt, loff_t *ppos)
3128 unsigned long *ptr = filp->private_data;
3132 r = snprintf(buf, sizeof(buf), "%ld\n",
3133 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3134 if (r > sizeof(buf))
3136 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3140 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3141 size_t cnt, loff_t *ppos)
3143 unsigned long *ptr = filp->private_data;
3147 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3156 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3158 long cpu_file = (long) inode->i_private;
3159 struct trace_iterator *iter;
3162 if (tracing_disabled)
3165 mutex_lock(&trace_types_lock);
3167 /* create a buffer to store the information to pass to userspace */
3168 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3175 * We make a copy of the current tracer to avoid concurrent
3176 * changes on it while we are reading.
3178 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3184 *iter->trace = *current_trace;
3186 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3191 /* trace pipe does not show start of buffer */
3192 cpumask_setall(iter->started);
3194 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3195 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3197 iter->cpu_file = cpu_file;
3198 iter->tr = &global_trace;
3199 mutex_init(&iter->mutex);
3200 filp->private_data = iter;
3202 if (iter->trace->pipe_open)
3203 iter->trace->pipe_open(iter);
3205 nonseekable_open(inode, filp);
3207 mutex_unlock(&trace_types_lock);
3213 mutex_unlock(&trace_types_lock);
3217 static int tracing_release_pipe(struct inode *inode, struct file *file)
3219 struct trace_iterator *iter = file->private_data;
3221 mutex_lock(&trace_types_lock);
3223 if (iter->trace->pipe_close)
3224 iter->trace->pipe_close(iter);
3226 mutex_unlock(&trace_types_lock);
3228 free_cpumask_var(iter->started);
3229 mutex_destroy(&iter->mutex);
3237 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3239 struct trace_iterator *iter = filp->private_data;
3241 if (trace_flags & TRACE_ITER_BLOCK) {
3243 * Always select as readable when in blocking mode
3245 return POLLIN | POLLRDNORM;
3247 if (!trace_empty(iter))
3248 return POLLIN | POLLRDNORM;
3249 poll_wait(filp, &trace_wait, poll_table);
3250 if (!trace_empty(iter))
3251 return POLLIN | POLLRDNORM;
3258 void default_wait_pipe(struct trace_iterator *iter)
3262 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3264 if (trace_empty(iter))
3267 finish_wait(&trace_wait, &wait);
3271 * This is a make-shift waitqueue.
3272 * A tracer might use this callback on some rare cases:
3274 * 1) the current tracer might hold the runqueue lock when it wakes up
3275 * a reader, hence a deadlock (sched, function, and function graph tracers)
3276 * 2) the function tracers, trace all functions, we don't want
3277 * the overhead of calling wake_up and friends
3278 * (and tracing them too)
3280 * Anyway, this is really very primitive wakeup.
3282 void poll_wait_pipe(struct trace_iterator *iter)
3284 set_current_state(TASK_INTERRUPTIBLE);
3285 /* sleep for 100 msecs, and try again. */
3286 schedule_timeout(HZ / 10);
3289 /* Must be called with trace_types_lock mutex held. */
3290 static int tracing_wait_pipe(struct file *filp)
3292 struct trace_iterator *iter = filp->private_data;
3294 while (trace_empty(iter)) {
3296 if ((filp->f_flags & O_NONBLOCK)) {
3300 mutex_unlock(&iter->mutex);
3302 iter->trace->wait_pipe(iter);
3304 mutex_lock(&iter->mutex);
3306 if (signal_pending(current))
3310 * We block until we read something and tracing is disabled.
3311 * We still block if tracing is disabled, but we have never
3312 * read anything. This allows a user to cat this file, and
3313 * then enable tracing. But after we have read something,
3314 * we give an EOF when tracing is again disabled.
3316 * iter->pos will be 0 if we haven't read anything.
3318 if (!tracer_enabled && iter->pos)
3329 tracing_read_pipe(struct file *filp, char __user *ubuf,
3330 size_t cnt, loff_t *ppos)
3332 struct trace_iterator *iter = filp->private_data;
3333 static struct tracer *old_tracer;
3336 /* return any leftover data */
3337 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3341 trace_seq_init(&iter->seq);
3343 /* copy the tracer to avoid using a global lock all around */
3344 mutex_lock(&trace_types_lock);
3345 if (unlikely(old_tracer != current_trace && current_trace)) {
3346 old_tracer = current_trace;
3347 *iter->trace = *current_trace;
3349 mutex_unlock(&trace_types_lock);
3352 * Avoid more than one consumer on a single file descriptor
3353 * This is just a matter of traces coherency, the ring buffer itself
3356 mutex_lock(&iter->mutex);
3357 if (iter->trace->read) {
3358 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3364 sret = tracing_wait_pipe(filp);
3368 /* stop when tracing is finished */
3369 if (trace_empty(iter)) {
3374 if (cnt >= PAGE_SIZE)
3375 cnt = PAGE_SIZE - 1;
3377 /* reset all but tr, trace, and overruns */
3378 memset(&iter->seq, 0,
3379 sizeof(struct trace_iterator) -
3380 offsetof(struct trace_iterator, seq));
3381 cpumask_clear(iter->started);
3384 trace_event_read_lock();
3385 trace_access_lock(iter->cpu_file);
3386 while (trace_find_next_entry_inc(iter) != NULL) {
3387 enum print_line_t ret;