2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
42 #include "trace_output.h"
45 * On boot up, the ring buffer is set to the minimum size, so that
46 * we do not waste memory on systems that are not using tracing.
48 int ring_buffer_expanded;
51 * We need to change this state when a selftest is running.
52 * A selftest will lurk into the ring-buffer to count the
53 * entries inserted during the selftest although some concurrent
54 * insertions into the ring-buffer such as trace_printk could occurred
55 * at the same time, giving false positive or negative results.
57 static bool __read_mostly tracing_selftest_running;
60 * If a tracer is running, we do not want to run SELFTEST.
62 bool __read_mostly tracing_selftest_disabled;
64 /* For tracers that don't implement custom flags */
65 static struct tracer_opt dummy_tracer_opt[] = {
69 static struct tracer_flags dummy_tracer_flags = {
71 .opts = dummy_tracer_opt
74 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
80 * Kill all tracing for good (never come back).
81 * It is initialized to 1 but will turn to zero if the initialization
82 * of the tracer is successful. But that is the only place that sets
85 static int tracing_disabled = 1;
87 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
89 static inline void ftrace_disable_cpu(void)
92 __this_cpu_inc(ftrace_cpu_disabled);
95 static inline void ftrace_enable_cpu(void)
97 __this_cpu_dec(ftrace_cpu_disabled);
101 cpumask_var_t __read_mostly tracing_buffer_mask;
104 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
106 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
107 * is set, then ftrace_dump is called. This will output the contents
108 * of the ftrace buffers to the console. This is very useful for
109 * capturing traces that lead to crashes and outputing it to a
112 * It is default off, but you can enable it with either specifying
113 * "ftrace_dump_on_oops" in the kernel command line, or setting
114 * /proc/sys/kernel/ftrace_dump_on_oops
115 * Set 1 if you want to dump buffers of all CPUs
116 * Set 2 if you want to dump the buffer of the CPU that triggered oops
119 enum ftrace_dump_mode ftrace_dump_on_oops;
121 static int tracing_set_tracer(const char *buf);
123 #define MAX_TRACER_SIZE 100
124 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
125 static char *default_bootup_tracer;
127 static int __init set_cmdline_ftrace(char *str)
129 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
130 default_bootup_tracer = bootup_tracer_buf;
131 /* We are using ftrace early, expand it */
132 ring_buffer_expanded = 1;
135 __setup("ftrace=", set_cmdline_ftrace);
137 static int __init set_ftrace_dump_on_oops(char *str)
139 if (*str++ != '=' || !*str) {
140 ftrace_dump_on_oops = DUMP_ALL;
144 if (!strcmp("orig_cpu", str)) {
145 ftrace_dump_on_oops = DUMP_ORIG;
151 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
153 unsigned long long ns2usecs(cycle_t nsec)
161 * The global_trace is the descriptor that holds the tracing
162 * buffers for the live tracing. For each CPU, it contains
163 * a link list of pages that will store trace entries. The
164 * page descriptor of the pages in the memory is used to hold
165 * the link list by linking the lru item in the page descriptor
166 * to each of the pages in the buffer per CPU.
168 * For each active CPU there is a data field that holds the
169 * pages for the buffer for that CPU. Each CPU has the same number
170 * of pages allocated for its buffer.
172 static struct trace_array global_trace;
174 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
176 int filter_current_check_discard(struct ring_buffer *buffer,
177 struct ftrace_event_call *call, void *rec,
178 struct ring_buffer_event *event)
180 return filter_check_discard(call, rec, buffer, event);
182 EXPORT_SYMBOL_GPL(filter_current_check_discard);
184 cycle_t ftrace_now(int cpu)
188 /* Early boot up does not have a buffer yet */
189 if (!global_trace.buffer)
190 return trace_clock_local();
192 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
193 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
199 * The max_tr is used to snapshot the global_trace when a maximum
200 * latency is reached. Some tracers will use this to store a maximum
201 * trace while it continues examining live traces.
203 * The buffers for the max_tr are set up the same as the global_trace.
204 * When a snapshot is taken, the link list of the max_tr is swapped
205 * with the link list of the global_trace and the buffers are reset for
206 * the global_trace so the tracing can continue.
208 static struct trace_array max_tr;
210 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
212 /* tracer_enabled is used to toggle activation of a tracer */
213 static int tracer_enabled = 1;
216 * tracing_is_enabled - return tracer_enabled status
218 * This function is used by other tracers to know the status
219 * of the tracer_enabled flag. Tracers may use this function
220 * to know if it should enable their features when starting
221 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
223 int tracing_is_enabled(void)
225 return tracer_enabled;
229 * trace_buf_size is the size in bytes that is allocated
230 * for a buffer. Note, the number of bytes is always rounded
233 * This number is purposely set to a low number of 16384.
234 * If the dump on oops happens, it will be much appreciated
235 * to not have to wait for all that output. Anyway this can be
236 * boot time and run time configurable.
238 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
240 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
242 /* trace_types holds a link list of available tracers. */
243 static struct tracer *trace_types __read_mostly;
245 /* current_trace points to the tracer that is currently active */
246 static struct tracer *current_trace __read_mostly;
249 * trace_types_lock is used to protect the trace_types list.
251 static DEFINE_MUTEX(trace_types_lock);
254 * serialize the access of the ring buffer
256 * ring buffer serializes readers, but it is low level protection.
257 * The validity of the events (which returns by ring_buffer_peek() ..etc)
258 * are not protected by ring buffer.
260 * The content of events may become garbage if we allow other process consumes
261 * these events concurrently:
262 * A) the page of the consumed events may become a normal page
263 * (not reader page) in ring buffer, and this page will be rewrited
264 * by events producer.
265 * B) The page of the consumed events may become a page for splice_read,
266 * and this page will be returned to system.
268 * These primitives allow multi process access to different cpu ring buffer
271 * These primitives don't distinguish read-only and read-consume access.
272 * Multi read-only access are also serialized.
276 static DECLARE_RWSEM(all_cpu_access_lock);
277 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
279 static inline void trace_access_lock(int cpu)
281 if (cpu == TRACE_PIPE_ALL_CPU) {
282 /* gain it for accessing the whole ring buffer. */
283 down_write(&all_cpu_access_lock);
285 /* gain it for accessing a cpu ring buffer. */
287 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
288 down_read(&all_cpu_access_lock);
290 /* Secondly block other access to this @cpu ring buffer. */
291 mutex_lock(&per_cpu(cpu_access_lock, cpu));
295 static inline void trace_access_unlock(int cpu)
297 if (cpu == TRACE_PIPE_ALL_CPU) {
298 up_write(&all_cpu_access_lock);
300 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
301 up_read(&all_cpu_access_lock);
305 static inline void trace_access_lock_init(void)
309 for_each_possible_cpu(cpu)
310 mutex_init(&per_cpu(cpu_access_lock, cpu));
315 static DEFINE_MUTEX(access_lock);
317 static inline void trace_access_lock(int cpu)
320 mutex_lock(&access_lock);
323 static inline void trace_access_unlock(int cpu)
326 mutex_unlock(&access_lock);
329 static inline void trace_access_lock_init(void)
335 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
336 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
338 /* trace_flags holds trace_options default values */
339 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
340 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
341 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE;
343 static int trace_stop_count;
344 static DEFINE_RAW_SPINLOCK(tracing_start_lock);
346 static void wakeup_work_handler(struct work_struct *work)
348 wake_up(&trace_wait);
351 static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
354 * trace_wake_up - wake up tasks waiting for trace input
356 * Schedules a delayed work to wake up any task that is blocked on the
357 * trace_wait queue. These is used with trace_poll for tasks polling the
360 void trace_wake_up(void)
362 const unsigned long delay = msecs_to_jiffies(2);
364 if (trace_flags & TRACE_ITER_BLOCK)
366 schedule_delayed_work(&wakeup_work, delay);
369 static int __init set_buf_size(char *str)
371 unsigned long buf_size;
375 buf_size = memparse(str, &str);
376 /* nr_entries can not be zero */
379 trace_buf_size = buf_size;
382 __setup("trace_buf_size=", set_buf_size);
384 static int __init set_tracing_thresh(char *str)
386 unsigned long threshhold;
391 ret = strict_strtoul(str, 0, &threshhold);
394 tracing_thresh = threshhold * 1000;
397 __setup("tracing_thresh=", set_tracing_thresh);
399 unsigned long nsecs_to_usecs(unsigned long nsecs)
404 /* These must match the bit postions in trace_iterator_flags */
405 static const char *trace_options[] = {
436 { trace_clock_local, "local" },
437 { trace_clock_global, "global" },
438 { trace_clock_counter, "counter" },
444 * trace_parser_get_init - gets the buffer for trace parser
446 int trace_parser_get_init(struct trace_parser *parser, int size)
448 memset(parser, 0, sizeof(*parser));
450 parser->buffer = kmalloc(size, GFP_KERNEL);
459 * trace_parser_put - frees the buffer for trace parser
461 void trace_parser_put(struct trace_parser *parser)
463 kfree(parser->buffer);
467 * trace_get_user - reads the user input string separated by space
468 * (matched by isspace(ch))
470 * For each string found the 'struct trace_parser' is updated,
471 * and the function returns.
473 * Returns number of bytes read.
475 * See kernel/trace/trace.h for 'struct trace_parser' details.
477 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
478 size_t cnt, loff_t *ppos)
485 trace_parser_clear(parser);
487 ret = get_user(ch, ubuf++);
495 * The parser is not finished with the last write,
496 * continue reading the user input without skipping spaces.
499 /* skip white space */
500 while (cnt && isspace(ch)) {
501 ret = get_user(ch, ubuf++);
508 /* only spaces were written */
518 /* read the non-space input */
519 while (cnt && !isspace(ch)) {
520 if (parser->idx < parser->size - 1)
521 parser->buffer[parser->idx++] = ch;
526 ret = get_user(ch, ubuf++);
533 /* We either got finished input or we have to wait for another call. */
535 parser->buffer[parser->idx] = 0;
536 parser->cont = false;
539 parser->buffer[parser->idx++] = ch;
549 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
557 if (s->len <= s->readpos)
560 len = s->len - s->readpos;
563 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
573 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
578 if (s->len <= s->readpos)
581 len = s->len - s->readpos;
584 ret = memcpy(buf, s->buffer + s->readpos, cnt);
593 * ftrace_max_lock is used to protect the swapping of buffers
594 * when taking a max snapshot. The buffers themselves are
595 * protected by per_cpu spinlocks. But the action of the swap
596 * needs its own lock.
598 * This is defined as a arch_spinlock_t in order to help
599 * with performance when lockdep debugging is enabled.
601 * It is also used in other places outside the update_max_tr
602 * so it needs to be defined outside of the
603 * CONFIG_TRACER_MAX_TRACE.
605 static arch_spinlock_t ftrace_max_lock =
606 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
608 unsigned long __read_mostly tracing_thresh;
610 #ifdef CONFIG_TRACER_MAX_TRACE
611 unsigned long __read_mostly tracing_max_latency;
614 * Copy the new maximum trace into the separate maximum-trace
615 * structure. (this way the maximum trace is permanently saved,
616 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
619 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
621 struct trace_array_cpu *data = tr->data[cpu];
622 struct trace_array_cpu *max_data;
625 max_tr.time_start = data->preempt_timestamp;
627 max_data = max_tr.data[cpu];
628 max_data->saved_latency = tracing_max_latency;
629 max_data->critical_start = data->critical_start;
630 max_data->critical_end = data->critical_end;
632 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
633 max_data->pid = tsk->pid;
635 * If tsk == current, then use current_uid(), as that does not use
636 * RCU. The irq tracer can be called out of RCU scope.
639 max_data->uid = current_uid();
641 max_data->uid = task_uid(tsk);
643 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
644 max_data->policy = tsk->policy;
645 max_data->rt_priority = tsk->rt_priority;
647 /* record this tasks comm */
648 tracing_record_cmdline(tsk);
652 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
654 * @tsk: the task with the latency
655 * @cpu: The cpu that initiated the trace.
657 * Flip the buffers between the @tr and the max_tr and record information
658 * about which task was the cause of this latency.
661 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
663 struct ring_buffer *buf;
665 if (trace_stop_count)
668 WARN_ON_ONCE(!irqs_disabled());
669 if (!current_trace->use_max_tr) {
673 arch_spin_lock(&ftrace_max_lock);
676 tr->buffer = max_tr.buffer;
679 __update_max_tr(tr, tsk, cpu);
680 arch_spin_unlock(&ftrace_max_lock);
684 * update_max_tr_single - only copy one trace over, and reset the rest
686 * @tsk - task with the latency
687 * @cpu - the cpu of the buffer to copy.
689 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
692 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
696 if (trace_stop_count)
699 WARN_ON_ONCE(!irqs_disabled());
700 if (!current_trace->use_max_tr) {
705 arch_spin_lock(&ftrace_max_lock);
707 ftrace_disable_cpu();
709 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
713 * We failed to swap the buffer due to a commit taking
714 * place on this CPU. We fail to record, but we reset
715 * the max trace buffer (no one writes directly to it)
716 * and flag that it failed.
718 trace_array_printk(&max_tr, _THIS_IP_,
719 "Failed to swap buffers due to commit in progress\n");
724 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
726 __update_max_tr(tr, tsk, cpu);
727 arch_spin_unlock(&ftrace_max_lock);
729 #endif /* CONFIG_TRACER_MAX_TRACE */
732 * register_tracer - register a tracer with the ftrace system.
733 * @type - the plugin for the tracer
735 * Register a new plugin tracer.
737 int register_tracer(struct tracer *type)
738 __releases(kernel_lock)
739 __acquires(kernel_lock)
745 pr_info("Tracer must have a name\n");
749 if (strlen(type->name) >= MAX_TRACER_SIZE) {
750 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
754 mutex_lock(&trace_types_lock);
756 tracing_selftest_running = true;
758 for (t = trace_types; t; t = t->next) {
759 if (strcmp(type->name, t->name) == 0) {
761 pr_info("Tracer %s already registered\n",
769 type->set_flag = &dummy_set_flag;
771 type->flags = &dummy_tracer_flags;
773 if (!type->flags->opts)
774 type->flags->opts = dummy_tracer_opt;
775 if (!type->wait_pipe)
776 type->wait_pipe = default_wait_pipe;
779 #ifdef CONFIG_FTRACE_STARTUP_TEST
780 if (type->selftest && !tracing_selftest_disabled) {
781 struct tracer *saved_tracer = current_trace;
782 struct trace_array *tr = &global_trace;
785 * Run a selftest on this tracer.
786 * Here we reset the trace buffer, and set the current
787 * tracer to be this tracer. The tracer can then run some
788 * internal tracing to verify that everything is in order.
789 * If we fail, we do not register this tracer.
791 tracing_reset_online_cpus(tr);
793 current_trace = type;
795 /* If we expanded the buffers, make sure the max is expanded too */
796 if (ring_buffer_expanded && type->use_max_tr)
797 ring_buffer_resize(max_tr.buffer, trace_buf_size);
799 /* the test is responsible for initializing and enabling */
800 pr_info("Testing tracer %s: ", type->name);
801 ret = type->selftest(type, tr);
802 /* the test is responsible for resetting too */
803 current_trace = saved_tracer;
805 printk(KERN_CONT "FAILED!\n");
808 /* Only reset on passing, to avoid touching corrupted buffers */
809 tracing_reset_online_cpus(tr);
811 /* Shrink the max buffer again */
812 if (ring_buffer_expanded && type->use_max_tr)
813 ring_buffer_resize(max_tr.buffer, 1);
815 printk(KERN_CONT "PASSED\n");
819 type->next = trace_types;
823 tracing_selftest_running = false;
824 mutex_unlock(&trace_types_lock);
826 if (ret || !default_bootup_tracer)
829 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
832 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
833 /* Do we want this tracer to start on bootup? */
834 tracing_set_tracer(type->name);
835 default_bootup_tracer = NULL;
836 /* disable other selftests, since this will break it. */
837 tracing_selftest_disabled = 1;
838 #ifdef CONFIG_FTRACE_STARTUP_TEST
839 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
847 void unregister_tracer(struct tracer *type)
851 mutex_lock(&trace_types_lock);
852 for (t = &trace_types; *t; t = &(*t)->next) {
856 pr_info("Tracer %s not registered\n", type->name);
862 if (type == current_trace && tracer_enabled) {
865 if (current_trace->stop)
866 current_trace->stop(&global_trace);
867 current_trace = &nop_trace;
870 mutex_unlock(&trace_types_lock);
873 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
875 ftrace_disable_cpu();
876 ring_buffer_reset_cpu(buffer, cpu);
880 void tracing_reset(struct trace_array *tr, int cpu)
882 struct ring_buffer *buffer = tr->buffer;
884 ring_buffer_record_disable(buffer);
886 /* Make sure all commits have finished */
888 __tracing_reset(buffer, cpu);
890 ring_buffer_record_enable(buffer);
893 void tracing_reset_online_cpus(struct trace_array *tr)
895 struct ring_buffer *buffer = tr->buffer;
898 ring_buffer_record_disable(buffer);
900 /* Make sure all commits have finished */
903 tr->time_start = ftrace_now(tr->cpu);
905 for_each_online_cpu(cpu)
906 __tracing_reset(buffer, cpu);
908 ring_buffer_record_enable(buffer);
911 void tracing_reset_current(int cpu)
913 tracing_reset(&global_trace, cpu);
916 void tracing_reset_current_online_cpus(void)
918 tracing_reset_online_cpus(&global_trace);
921 #define SAVED_CMDLINES 128
922 #define NO_CMDLINE_MAP UINT_MAX
923 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
924 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
925 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
926 static int cmdline_idx;
927 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
929 /* temporary disable recording */
930 static atomic_t trace_record_cmdline_disabled __read_mostly;
932 static void trace_init_cmdlines(void)
934 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
935 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
939 int is_tracing_stopped(void)
941 return trace_stop_count;
945 * ftrace_off_permanent - disable all ftrace code permanently
947 * This should only be called when a serious anomally has
948 * been detected. This will turn off the function tracing,
949 * ring buffers, and other tracing utilites. It takes no
950 * locks and can be called from any context.
952 void ftrace_off_permanent(void)
954 tracing_disabled = 1;
956 tracing_off_permanent();
960 * tracing_start - quick start of the tracer
962 * If tracing is enabled but was stopped by tracing_stop,
963 * this will start the tracer back up.
965 void tracing_start(void)
967 struct ring_buffer *buffer;
970 if (tracing_disabled)
973 raw_spin_lock_irqsave(&tracing_start_lock, flags);
974 if (--trace_stop_count) {
975 if (trace_stop_count < 0) {
976 /* Someone screwed up their debugging */
978 trace_stop_count = 0;
983 /* Prevent the buffers from switching */
984 arch_spin_lock(&ftrace_max_lock);
986 buffer = global_trace.buffer;
988 ring_buffer_record_enable(buffer);
990 buffer = max_tr.buffer;
992 ring_buffer_record_enable(buffer);
994 arch_spin_unlock(&ftrace_max_lock);
998 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1002 * tracing_stop - quick stop of the tracer
1004 * Light weight way to stop tracing. Use in conjunction with
1007 void tracing_stop(void)
1009 struct ring_buffer *buffer;
1010 unsigned long flags;
1013 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1014 if (trace_stop_count++)
1017 /* Prevent the buffers from switching */
1018 arch_spin_lock(&ftrace_max_lock);
1020 buffer = global_trace.buffer;
1022 ring_buffer_record_disable(buffer);
1024 buffer = max_tr.buffer;
1026 ring_buffer_record_disable(buffer);
1028 arch_spin_unlock(&ftrace_max_lock);
1031 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1034 void trace_stop_cmdline_recording(void);
1036 static void trace_save_cmdline(struct task_struct *tsk)
1040 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1044 * It's not the end of the world if we don't get
1045 * the lock, but we also don't want to spin
1046 * nor do we want to disable interrupts,
1047 * so if we miss here, then better luck next time.
1049 if (!arch_spin_trylock(&trace_cmdline_lock))
1052 idx = map_pid_to_cmdline[tsk->pid];
1053 if (idx == NO_CMDLINE_MAP) {
1054 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1057 * Check whether the cmdline buffer at idx has a pid
1058 * mapped. We are going to overwrite that entry so we
1059 * need to clear the map_pid_to_cmdline. Otherwise we
1060 * would read the new comm for the old pid.
1062 pid = map_cmdline_to_pid[idx];
1063 if (pid != NO_CMDLINE_MAP)
1064 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1066 map_cmdline_to_pid[idx] = tsk->pid;
1067 map_pid_to_cmdline[tsk->pid] = idx;
1072 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1074 arch_spin_unlock(&trace_cmdline_lock);
1077 void trace_find_cmdline(int pid, char comm[])
1082 strcpy(comm, "<idle>");
1086 if (WARN_ON_ONCE(pid < 0)) {
1087 strcpy(comm, "<XXX>");
1091 if (pid > PID_MAX_DEFAULT) {
1092 strcpy(comm, "<...>");
1097 arch_spin_lock(&trace_cmdline_lock);
1098 map = map_pid_to_cmdline[pid];
1099 if (map != NO_CMDLINE_MAP)
1100 strcpy(comm, saved_cmdlines[map]);
1102 strcpy(comm, "<...>");
1104 arch_spin_unlock(&trace_cmdline_lock);
1108 void tracing_record_cmdline(struct task_struct *tsk)
1110 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1114 trace_save_cmdline(tsk);
1118 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1121 struct task_struct *tsk = current;
1123 entry->preempt_count = pc & 0xff;
1124 entry->pid = (tsk) ? tsk->pid : 0;
1127 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1128 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1130 TRACE_FLAG_IRQS_NOSUPPORT |
1132 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1133 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1134 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1136 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1138 struct ring_buffer_event *
1139 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1142 unsigned long flags, int pc)
1144 struct ring_buffer_event *event;
1146 event = ring_buffer_lock_reserve(buffer, len);
1147 if (event != NULL) {
1148 struct trace_entry *ent = ring_buffer_event_data(event);
1150 tracing_generic_entry_update(ent, flags, pc);
1158 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1159 struct ring_buffer_event *event,
1160 unsigned long flags, int pc,
1163 ring_buffer_unlock_commit(buffer, event);
1165 ftrace_trace_stack(buffer, flags, 6, pc);
1166 ftrace_trace_userstack(buffer, flags, pc);
1172 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1173 struct ring_buffer_event *event,
1174 unsigned long flags, int pc)
1176 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1179 struct ring_buffer_event *
1180 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1181 int type, unsigned long len,
1182 unsigned long flags, int pc)
1184 *current_rb = global_trace.buffer;
1185 return trace_buffer_lock_reserve(*current_rb,
1186 type, len, flags, pc);
1188 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1190 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1191 struct ring_buffer_event *event,
1192 unsigned long flags, int pc)
1194 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1196 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1198 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1199 struct ring_buffer_event *event,
1200 unsigned long flags, int pc)
1202 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1204 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1206 void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1207 struct ring_buffer_event *event,
1208 unsigned long flags, int pc,
1209 struct pt_regs *regs)
1211 ring_buffer_unlock_commit(buffer, event);
1213 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1214 ftrace_trace_userstack(buffer, flags, pc);
1216 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1218 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1219 struct ring_buffer_event *event)
1221 ring_buffer_discard_commit(buffer, event);
1223 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1226 trace_function(struct trace_array *tr,
1227 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1230 struct ftrace_event_call *call = &event_function;
1231 struct ring_buffer *buffer = tr->buffer;
1232 struct ring_buffer_event *event;
1233 struct ftrace_entry *entry;
1235 /* If we are reading the ring buffer, don't trace */
1236 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1239 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1243 entry = ring_buffer_event_data(event);
1245 entry->parent_ip = parent_ip;
1247 if (!filter_check_discard(call, entry, buffer, event))
1248 ring_buffer_unlock_commit(buffer, event);
1252 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1253 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1256 if (likely(!atomic_read(&data->disabled)))
1257 trace_function(tr, ip, parent_ip, flags, pc);
1260 #ifdef CONFIG_STACKTRACE
1262 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1263 struct ftrace_stack {
1264 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1267 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1268 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1270 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1271 unsigned long flags,
1272 int skip, int pc, struct pt_regs *regs)
1274 struct ftrace_event_call *call = &event_kernel_stack;
1275 struct ring_buffer_event *event;
1276 struct stack_entry *entry;
1277 struct stack_trace trace;
1279 int size = FTRACE_STACK_ENTRIES;
1281 trace.nr_entries = 0;
1285 * Since events can happen in NMIs there's no safe way to
1286 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1287 * or NMI comes in, it will just have to use the default
1288 * FTRACE_STACK_SIZE.
1290 preempt_disable_notrace();
1292 use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1294 * We don't need any atomic variables, just a barrier.
1295 * If an interrupt comes in, we don't care, because it would
1296 * have exited and put the counter back to what we want.
1297 * We just need a barrier to keep gcc from moving things
1301 if (use_stack == 1) {
1302 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1303 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1306 save_stack_trace_regs(regs, &trace);
1308 save_stack_trace(&trace);
1310 if (trace.nr_entries > size)
1311 size = trace.nr_entries;
1313 /* From now on, use_stack is a boolean */
1316 size *= sizeof(unsigned long);
1318 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1319 sizeof(*entry) + size, flags, pc);
1322 entry = ring_buffer_event_data(event);
1324 memset(&entry->caller, 0, size);
1327 memcpy(&entry->caller, trace.entries,
1328 trace.nr_entries * sizeof(unsigned long));
1330 trace.max_entries = FTRACE_STACK_ENTRIES;
1331 trace.entries = entry->caller;
1333 save_stack_trace_regs(regs, &trace);
1335 save_stack_trace(&trace);
1338 entry->size = trace.nr_entries;
1340 if (!filter_check_discard(call, entry, buffer, event))
1341 ring_buffer_unlock_commit(buffer, event);
1344 /* Again, don't let gcc optimize things here */
1346 __get_cpu_var(ftrace_stack_reserve)--;
1347 preempt_enable_notrace();
1351 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1352 int skip, int pc, struct pt_regs *regs)
1354 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1357 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1360 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1363 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1366 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1369 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1372 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1376 * trace_dump_stack - record a stack back trace in the trace buffer
1378 void trace_dump_stack(void)
1380 unsigned long flags;
1382 if (tracing_disabled || tracing_selftest_running)
1385 local_save_flags(flags);
1387 /* skipping 3 traces, seems to get us at the caller of this function */
1388 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1391 static DEFINE_PER_CPU(int, user_stack_count);
1394 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1396 struct ftrace_event_call *call = &event_user_stack;
1397 struct ring_buffer_event *event;
1398 struct userstack_entry *entry;
1399 struct stack_trace trace;
1401 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1405 * NMIs can not handle page faults, even with fix ups.
1406 * The save user stack can (and often does) fault.
1408 if (unlikely(in_nmi()))
1412 * prevent recursion, since the user stack tracing may
1413 * trigger other kernel events.
1416 if (__this_cpu_read(user_stack_count))
1419 __this_cpu_inc(user_stack_count);
1421 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1422 sizeof(*entry), flags, pc);
1424 goto out_drop_count;
1425 entry = ring_buffer_event_data(event);
1427 entry->tgid = current->tgid;
1428 memset(&entry->caller, 0, sizeof(entry->caller));
1430 trace.nr_entries = 0;
1431 trace.max_entries = FTRACE_STACK_ENTRIES;
1433 trace.entries = entry->caller;
1435 save_stack_trace_user(&trace);
1436 if (!filter_check_discard(call, entry, buffer, event))
1437 ring_buffer_unlock_commit(buffer, event);
1440 __this_cpu_dec(user_stack_count);
1446 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1448 ftrace_trace_userstack(tr, flags, preempt_count());
1452 #endif /* CONFIG_STACKTRACE */
1455 * trace_vbprintk - write binary msg to tracing buffer
1458 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1460 static arch_spinlock_t trace_buf_lock =
1461 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1462 static u32 trace_buf[TRACE_BUF_SIZE];
1464 struct ftrace_event_call *call = &event_bprint;
1465 struct ring_buffer_event *event;
1466 struct ring_buffer *buffer;
1467 struct trace_array *tr = &global_trace;
1468 struct trace_array_cpu *data;
1469 struct bprint_entry *entry;
1470 unsigned long flags;
1472 int cpu, len = 0, size, pc;
1474 if (unlikely(tracing_selftest_running || tracing_disabled))
1477 /* Don't pollute graph traces with trace_vprintk internals */
1478 pause_graph_tracing();
1480 pc = preempt_count();
1481 preempt_disable_notrace();
1482 cpu = raw_smp_processor_id();
1483 data = tr->data[cpu];
1485 disable = atomic_inc_return(&data->disabled);
1486 if (unlikely(disable != 1))
1489 /* Lockdep uses trace_printk for lock tracing */
1490 local_irq_save(flags);
1491 arch_spin_lock(&trace_buf_lock);
1492 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1494 if (len > TRACE_BUF_SIZE || len < 0)
1497 size = sizeof(*entry) + sizeof(u32) * len;
1498 buffer = tr->buffer;
1499 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1503 entry = ring_buffer_event_data(event);
1507 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1508 if (!filter_check_discard(call, entry, buffer, event)) {
1509 ring_buffer_unlock_commit(buffer, event);
1510 ftrace_trace_stack(buffer, flags, 6, pc);
1514 arch_spin_unlock(&trace_buf_lock);
1515 local_irq_restore(flags);
1518 atomic_dec_return(&data->disabled);
1519 preempt_enable_notrace();
1520 unpause_graph_tracing();
1524 EXPORT_SYMBOL_GPL(trace_vbprintk);
1526 int trace_array_printk(struct trace_array *tr,
1527 unsigned long ip, const char *fmt, ...)
1532 if (!(trace_flags & TRACE_ITER_PRINTK))
1536 ret = trace_array_vprintk(tr, ip, fmt, ap);
1541 int trace_array_vprintk(struct trace_array *tr,
1542 unsigned long ip, const char *fmt, va_list args)
1544 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1545 static char trace_buf[TRACE_BUF_SIZE];
1547 struct ftrace_event_call *call = &event_print;
1548 struct ring_buffer_event *event;
1549 struct ring_buffer *buffer;
1550 struct trace_array_cpu *data;
1551 int cpu, len = 0, size, pc;
1552 struct print_entry *entry;
1553 unsigned long irq_flags;
1556 if (tracing_disabled || tracing_selftest_running)
1559 pc = preempt_count();
1560 preempt_disable_notrace();
1561 cpu = raw_smp_processor_id();
1562 data = tr->data[cpu];
1564 disable = atomic_inc_return(&data->disabled);
1565 if (unlikely(disable != 1))
1568 pause_graph_tracing();
1569 raw_local_irq_save(irq_flags);
1570 arch_spin_lock(&trace_buf_lock);
1571 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1573 size = sizeof(*entry) + len + 1;
1574 buffer = tr->buffer;
1575 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1579 entry = ring_buffer_event_data(event);
1582 memcpy(&entry->buf, trace_buf, len);
1583 entry->buf[len] = '\0';
1584 if (!filter_check_discard(call, entry, buffer, event)) {
1585 ring_buffer_unlock_commit(buffer, event);
1586 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1590 arch_spin_unlock(&trace_buf_lock);
1591 raw_local_irq_restore(irq_flags);
1592 unpause_graph_tracing();
1594 atomic_dec_return(&data->disabled);
1595 preempt_enable_notrace();
1600 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1602 return trace_array_vprintk(&global_trace, ip, fmt, args);
1604 EXPORT_SYMBOL_GPL(trace_vprintk);
1606 static void trace_iterator_increment(struct trace_iterator *iter)
1608 /* Don't allow ftrace to trace into the ring buffers */
1609 ftrace_disable_cpu();
1612 if (iter->buffer_iter[iter->cpu])
1613 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1615 ftrace_enable_cpu();
1618 static struct trace_entry *
1619 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1620 unsigned long *lost_events)
1622 struct ring_buffer_event *event;
1623 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1625 /* Don't allow ftrace to trace into the ring buffers */
1626 ftrace_disable_cpu();
1629 event = ring_buffer_iter_peek(buf_iter, ts);
1631 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1634 ftrace_enable_cpu();
1637 iter->ent_size = ring_buffer_event_length(event);
1638 return ring_buffer_event_data(event);
1644 static struct trace_entry *
1645 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1646 unsigned long *missing_events, u64 *ent_ts)
1648 struct ring_buffer *buffer = iter->tr->buffer;
1649 struct trace_entry *ent, *next = NULL;
1650 unsigned long lost_events = 0, next_lost = 0;
1651 int cpu_file = iter->cpu_file;
1652 u64 next_ts = 0, ts;
1658 * If we are in a per_cpu trace file, don't bother by iterating over
1659 * all cpu and peek directly.
1661 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1662 if (ring_buffer_empty_cpu(buffer, cpu_file))
1664 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1666 *ent_cpu = cpu_file;
1671 for_each_tracing_cpu(cpu) {
1673 if (ring_buffer_empty_cpu(buffer, cpu))
1676 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1679 * Pick the entry with the smallest timestamp:
1681 if (ent && (!next || ts < next_ts)) {
1685 next_lost = lost_events;
1686 next_size = iter->ent_size;
1690 iter->ent_size = next_size;
1693 *ent_cpu = next_cpu;
1699 *missing_events = next_lost;
1704 /* Find the next real entry, without updating the iterator itself */
1705 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1706 int *ent_cpu, u64 *ent_ts)
1708 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1711 /* Find the next real entry, and increment the iterator to the next entry */
1712 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1714 iter->ent = __find_next_entry(iter, &iter->cpu,
1715 &iter->lost_events, &iter->ts);
1718 trace_iterator_increment(iter);
1720 return iter->ent ? iter : NULL;
1723 static void trace_consume(struct trace_iterator *iter)
1725 /* Don't allow ftrace to trace into the ring buffers */
1726 ftrace_disable_cpu();
1727 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1728 &iter->lost_events);
1729 ftrace_enable_cpu();
1732 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1734 struct trace_iterator *iter = m->private;
1738 WARN_ON_ONCE(iter->leftover);
1742 /* can't go backwards */
1747 ent = trace_find_next_entry_inc(iter);
1751 while (ent && iter->idx < i)
1752 ent = trace_find_next_entry_inc(iter);
1759 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1761 struct trace_array *tr = iter->tr;
1762 struct ring_buffer_event *event;
1763 struct ring_buffer_iter *buf_iter;
1764 unsigned long entries = 0;
1767 tr->data[cpu]->skipped_entries = 0;
1769 if (!iter->buffer_iter[cpu])
1772 buf_iter = iter->buffer_iter[cpu];
1773 ring_buffer_iter_reset(buf_iter);
1776 * We could have the case with the max latency tracers
1777 * that a reset never took place on a cpu. This is evident
1778 * by the timestamp being before the start of the buffer.
1780 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1781 if (ts >= iter->tr->time_start)
1784 ring_buffer_read(buf_iter, NULL);
1787 tr->data[cpu]->skipped_entries = entries;
1791 * The current tracer is copied to avoid a global locking
1794 static void *s_start(struct seq_file *m, loff_t *pos)
1796 struct trace_iterator *iter = m->private;
1797 static struct tracer *old_tracer;
1798 int cpu_file = iter->cpu_file;
1803 /* copy the tracer to avoid using a global lock all around */
1804 mutex_lock(&trace_types_lock);
1805 if (unlikely(old_tracer != current_trace && current_trace)) {
1806 old_tracer = current_trace;
1807 *iter->trace = *current_trace;
1809 mutex_unlock(&trace_types_lock);
1811 atomic_inc(&trace_record_cmdline_disabled);
1813 if (*pos != iter->pos) {
1818 ftrace_disable_cpu();
1820 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1821 for_each_tracing_cpu(cpu)
1822 tracing_iter_reset(iter, cpu);
1824 tracing_iter_reset(iter, cpu_file);
1826 ftrace_enable_cpu();
1829 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1834 * If we overflowed the seq_file before, then we want
1835 * to just reuse the trace_seq buffer again.
1841 p = s_next(m, p, &l);
1845 trace_event_read_lock();
1846 trace_access_lock(cpu_file);
1850 static void s_stop(struct seq_file *m, void *p)
1852 struct trace_iterator *iter = m->private;
1854 atomic_dec(&trace_record_cmdline_disabled);
1855 trace_access_unlock(iter->cpu_file);
1856 trace_event_read_unlock();
1859 static void print_lat_help_header(struct seq_file *m)
1861 seq_puts(m, "# _------=> CPU# \n");
1862 seq_puts(m, "# / _-----=> irqs-off \n");
1863 seq_puts(m, "# | / _----=> need-resched \n");
1864 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1865 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1866 seq_puts(m, "# |||| / delay \n");
1867 seq_puts(m, "# cmd pid ||||| time | caller \n");
1868 seq_puts(m, "# \\ / ||||| \\ | / \n");
1871 static void print_func_help_header(struct seq_file *m)
1873 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1874 seq_puts(m, "# | | | | |\n");
1879 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1881 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1882 struct trace_array *tr = iter->tr;
1883 struct trace_array_cpu *data = tr->data[tr->cpu];
1884 struct tracer *type = current_trace;
1885 unsigned long entries = 0;
1886 unsigned long total = 0;
1887 unsigned long count;
1888 const char *name = "preemption";
1895 for_each_tracing_cpu(cpu) {
1896 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1898 * If this buffer has skipped entries, then we hold all
1899 * entries for the trace and we need to ignore the
1900 * ones before the time stamp.
1902 if (tr->data[cpu]->skipped_entries) {
1903 count -= tr->data[cpu]->skipped_entries;
1904 /* total is the same as the entries */
1908 ring_buffer_overrun_cpu(tr->buffer, cpu);
1912 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1914 seq_puts(m, "# -----------------------------------"
1915 "---------------------------------\n");
1916 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1917 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1918 nsecs_to_usecs(data->saved_latency),
1922 #if defined(CONFIG_PREEMPT_NONE)
1924 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1926 #elif defined(CONFIG_PREEMPT)
1931 /* These are reserved for later use */
1934 seq_printf(m, " #P:%d)\n", num_online_cpus());
1938 seq_puts(m, "# -----------------\n");
1939 seq_printf(m, "# | task: %.16s-%d "
1940 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1941 data->comm, data->pid, data->uid, data->nice,
1942 data->policy, data->rt_priority);
1943 seq_puts(m, "# -----------------\n");
1945 if (data->critical_start) {
1946 seq_puts(m, "# => started at: ");
1947 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1948 trace_print_seq(m, &iter->seq);
1949 seq_puts(m, "\n# => ended at: ");
1950 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1951 trace_print_seq(m, &iter->seq);
1952 seq_puts(m, "\n#\n");
1958 static void test_cpu_buff_start(struct trace_iterator *iter)
1960 struct trace_seq *s = &iter->seq;
1962 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1965 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1968 if (cpumask_test_cpu(iter->cpu, iter->started))
1971 if (iter->tr->data[iter->cpu]->skipped_entries)
1974 cpumask_set_cpu(iter->cpu, iter->started);
1976 /* Don't print started cpu buffer for the first entry of the trace */
1978 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1982 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1984 struct trace_seq *s = &iter->seq;
1985 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1986 struct trace_entry *entry;
1987 struct trace_event *event;
1991 test_cpu_buff_start(iter);
1993 event = ftrace_find_event(entry->type);
1995 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1996 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1997 if (!trace_print_lat_context(iter))
2000 if (!trace_print_context(iter))
2006 return event->funcs->trace(iter, sym_flags, event);
2008 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2011 return TRACE_TYPE_HANDLED;
2013 return TRACE_TYPE_PARTIAL_LINE;
2016 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2018 struct trace_seq *s = &iter->seq;
2019 struct trace_entry *entry;
2020 struct trace_event *event;
2024 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2025 if (!trace_seq_printf(s, "%d %d %llu ",
2026 entry->pid, iter->cpu, iter->ts))
2030 event = ftrace_find_event(entry->type);
2032 return event->funcs->raw(iter, 0, event);
2034 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2037 return TRACE_TYPE_HANDLED;
2039 return TRACE_TYPE_PARTIAL_LINE;
2042 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2044 struct trace_seq *s = &iter->seq;
2045 unsigned char newline = '\n';
2046 struct trace_entry *entry;
2047 struct trace_event *event;
2051 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2052 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2053 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2054 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2057 event = ftrace_find_event(entry->type);
2059 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2060 if (ret != TRACE_TYPE_HANDLED)
2064 SEQ_PUT_FIELD_RET(s, newline);
2066 return TRACE_TYPE_HANDLED;
2069 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2071 struct trace_seq *s = &iter->seq;
2072 struct trace_entry *entry;
2073 struct trace_event *event;
2077 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2078 SEQ_PUT_FIELD_RET(s, entry->pid);
2079 SEQ_PUT_FIELD_RET(s, iter->cpu);
2080 SEQ_PUT_FIELD_RET(s, iter->ts);
2083 event = ftrace_find_event(entry->type);
2084 return event ? event->funcs->binary(iter, 0, event) :
2088 int trace_empty(struct trace_iterator *iter)
2092 /* If we are looking at one CPU buffer, only check that one */
2093 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2094 cpu = iter->cpu_file;
2095 if (iter->buffer_iter[cpu]) {
2096 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2099 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2105 for_each_tracing_cpu(cpu) {
2106 if (iter->buffer_iter[cpu]) {
2107 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2110 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2118 /* Called with trace_event_read_lock() held. */
2119 enum print_line_t print_trace_line(struct trace_iterator *iter)
2121 enum print_line_t ret;
2123 if (iter->lost_events &&
2124 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2125 iter->cpu, iter->lost_events))
2126 return TRACE_TYPE_PARTIAL_LINE;
2128 if (iter->trace && iter->trace->print_line) {
2129 ret = iter->trace->print_line(iter);
2130 if (ret != TRACE_TYPE_UNHANDLED)
2134 if (iter->ent->type == TRACE_BPRINT &&
2135 trace_flags & TRACE_ITER_PRINTK &&
2136 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2137 return trace_print_bprintk_msg_only(iter);
2139 if (iter->ent->type == TRACE_PRINT &&
2140 trace_flags & TRACE_ITER_PRINTK &&
2141 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2142 return trace_print_printk_msg_only(iter);
2144 if (trace_flags & TRACE_ITER_BIN)
2145 return print_bin_fmt(iter);
2147 if (trace_flags & TRACE_ITER_HEX)
2148 return print_hex_fmt(iter);
2150 if (trace_flags & TRACE_ITER_RAW)
2151 return print_raw_fmt(iter);
2153 return print_trace_fmt(iter);
2156 void trace_default_header(struct seq_file *m)
2158 struct trace_iterator *iter = m->private;
2160 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2163 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2164 /* print nothing if the buffers are empty */
2165 if (trace_empty(iter))
2167 print_trace_header(m, iter);
2168 if (!(trace_flags & TRACE_ITER_VERBOSE))
2169 print_lat_help_header(m);
2171 if (!(trace_flags & TRACE_ITER_VERBOSE))
2172 print_func_help_header(m);
2176 static void test_ftrace_alive(struct seq_file *m)
2178 if (!ftrace_is_dead())
2180 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2181 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2184 static int s_show(struct seq_file *m, void *v)
2186 struct trace_iterator *iter = v;
2189 if (iter->ent == NULL) {
2191 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2193 test_ftrace_alive(m);
2195 if (iter->trace && iter->trace->print_header)
2196 iter->trace->print_header(m);
2198 trace_default_header(m);
2200 } else if (iter->leftover) {
2202 * If we filled the seq_file buffer earlier, we
2203 * want to just show it now.
2205 ret = trace_print_seq(m, &iter->seq);
2207 /* ret should this time be zero, but you never know */
2208 iter->leftover = ret;
2211 print_trace_line(iter);
2212 ret = trace_print_seq(m, &iter->seq);
2214 * If we overflow the seq_file buffer, then it will
2215 * ask us for this data again at start up.
2217 * ret is 0 if seq_file write succeeded.
2220 iter->leftover = ret;
2226 static const struct seq_operations tracer_seq_ops = {
2233 static struct trace_iterator *
2234 __tracing_open(struct inode *inode, struct file *file)
2236 long cpu_file = (long) inode->i_private;
2237 void *fail_ret = ERR_PTR(-ENOMEM);
2238 struct trace_iterator *iter;
2242 if (tracing_disabled)
2243 return ERR_PTR(-ENODEV);
2245 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2247 return ERR_PTR(-ENOMEM);
2250 * We make a copy of the current tracer to avoid concurrent
2251 * changes on it while we are reading.
2253 mutex_lock(&trace_types_lock);
2254 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2259 *iter->trace = *current_trace;
2261 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2264 if (current_trace && current_trace->print_max)
2267 iter->tr = &global_trace;
2269 mutex_init(&iter->mutex);
2270 iter->cpu_file = cpu_file;
2272 /* Notify the tracer early; before we stop tracing. */
2273 if (iter->trace && iter->trace->open)
2274 iter->trace->open(iter);
2276 /* Annotate start of buffers if we had overruns */
2277 if (ring_buffer_overruns(iter->tr->buffer))
2278 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2280 /* stop the trace while dumping */
2283 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2284 for_each_tracing_cpu(cpu) {
2285 iter->buffer_iter[cpu] =
2286 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2288 ring_buffer_read_prepare_sync();
2289 for_each_tracing_cpu(cpu) {
2290 ring_buffer_read_start(iter->buffer_iter[cpu]);
2291 tracing_iter_reset(iter, cpu);
2294 cpu = iter->cpu_file;
2295 iter->buffer_iter[cpu] =
2296 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2297 ring_buffer_read_prepare_sync();
2298 ring_buffer_read_start(iter->buffer_iter[cpu]);
2299 tracing_iter_reset(iter, cpu);
2302 ret = seq_open(file, &tracer_seq_ops);
2304 fail_ret = ERR_PTR(ret);
2308 m = file->private_data;
2311 mutex_unlock(&trace_types_lock);
2316 for_each_tracing_cpu(cpu) {
2317 if (iter->buffer_iter[cpu])
2318 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2320 free_cpumask_var(iter->started);
2323 mutex_unlock(&trace_types_lock);
2330 int tracing_open_generic(struct inode *inode, struct file *filp)
2332 if (tracing_disabled)
2335 filp->private_data = inode->i_private;
2339 static int tracing_release(struct inode *inode, struct file *file)
2341 struct seq_file *m = file->private_data;
2342 struct trace_iterator *iter;
2345 if (!(file->f_mode & FMODE_READ))
2350 mutex_lock(&trace_types_lock);
2351 for_each_tracing_cpu(cpu) {
2352 if (iter->buffer_iter[cpu])
2353 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2356 if (iter->trace && iter->trace->close)
2357 iter->trace->close(iter);
2359 /* reenable tracing if it was previously enabled */
2361 mutex_unlock(&trace_types_lock);
2363 seq_release(inode, file);
2364 mutex_destroy(&iter->mutex);
2365 free_cpumask_var(iter->started);
2371 static int tracing_open(struct inode *inode, struct file *file)
2373 struct trace_iterator *iter;
2376 /* If this file was open for write, then erase contents */
2377 if ((file->f_mode & FMODE_WRITE) &&
2378 (file->f_flags & O_TRUNC)) {
2379 long cpu = (long) inode->i_private;
2381 if (cpu == TRACE_PIPE_ALL_CPU)
2382 tracing_reset_online_cpus(&global_trace);
2384 tracing_reset(&global_trace, cpu);
2387 if (file->f_mode & FMODE_READ) {
2388 iter = __tracing_open(inode, file);
2390 ret = PTR_ERR(iter);
2391 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2392 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2398 t_next(struct seq_file *m, void *v, loff_t *pos)
2400 struct tracer *t = v;
2410 static void *t_start(struct seq_file *m, loff_t *pos)
2415 mutex_lock(&trace_types_lock);
2416 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2422 static void t_stop(struct seq_file *m, void *p)
2424 mutex_unlock(&trace_types_lock);
2427 static int t_show(struct seq_file *m, void *v)
2429 struct tracer *t = v;
2434 seq_printf(m, "%s", t->name);
2443 static const struct seq_operations show_traces_seq_ops = {
2450 static int show_traces_open(struct inode *inode, struct file *file)
2452 if (tracing_disabled)
2455 return seq_open(file, &show_traces_seq_ops);
2459 tracing_write_stub(struct file *filp, const char __user *ubuf,
2460 size_t count, loff_t *ppos)
2465 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2467 if (file->f_mode & FMODE_READ)
2468 return seq_lseek(file, offset, origin);
2473 static const struct file_operations tracing_fops = {
2474 .open = tracing_open,
2476 .write = tracing_write_stub,
2477 .llseek = tracing_seek,
2478 .release = tracing_release,
2481 static const struct file_operations show_traces_fops = {
2482 .open = show_traces_open,
2484 .release = seq_release,
2485 .llseek = seq_lseek,
2489 * Only trace on a CPU if the bitmask is set:
2491 static cpumask_var_t tracing_cpumask;
2494 * The tracer itself will not take this lock, but still we want
2495 * to provide a consistent cpumask to user-space:
2497 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2500 * Temporary storage for the character representation of the
2501 * CPU bitmask (and one more byte for the newline):
2503 static char mask_str[NR_CPUS + 1];
2506 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2507 size_t count, loff_t *ppos)
2511 mutex_lock(&tracing_cpumask_update_lock);
2513 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2514 if (count - len < 2) {
2518 len += sprintf(mask_str + len, "\n");
2519 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2522 mutex_unlock(&tracing_cpumask_update_lock);
2528 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2529 size_t count, loff_t *ppos)
2532 cpumask_var_t tracing_cpumask_new;
2534 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2537 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2541 mutex_lock(&tracing_cpumask_update_lock);
2543 local_irq_disable();
2544 arch_spin_lock(&ftrace_max_lock);
2545 for_each_tracing_cpu(cpu) {
2547 * Increase/decrease the disabled counter if we are
2548 * about to flip a bit in the cpumask:
2550 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2551 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2552 atomic_inc(&global_trace.data[cpu]->disabled);
2553 ring_buffer_record_disable_cpu(global_trace.buffer, cpu);
2555 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2556 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2557 atomic_dec(&global_trace.data[cpu]->disabled);
2558 ring_buffer_record_enable_cpu(global_trace.buffer, cpu);
2561 arch_spin_unlock(&ftrace_max_lock);
2564 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2566 mutex_unlock(&tracing_cpumask_update_lock);
2567 free_cpumask_var(tracing_cpumask_new);
2572 free_cpumask_var(tracing_cpumask_new);
2577 static const struct file_operations tracing_cpumask_fops = {
2578 .open = tracing_open_generic,
2579 .read = tracing_cpumask_read,
2580 .write = tracing_cpumask_write,
2581 .llseek = generic_file_llseek,
2584 static int tracing_trace_options_show(struct seq_file *m, void *v)
2586 struct tracer_opt *trace_opts;
2590 mutex_lock(&trace_types_lock);
2591 tracer_flags = current_trace->flags->val;
2592 trace_opts = current_trace->flags->opts;
2594 for (i = 0; trace_options[i]; i++) {
2595 if (trace_flags & (1 << i))
2596 seq_printf(m, "%s\n", trace_options[i]);
2598 seq_printf(m, "no%s\n", trace_options[i]);
2601 for (i = 0; trace_opts[i].name; i++) {
2602 if (tracer_flags & trace_opts[i].bit)
2603 seq_printf(m, "%s\n", trace_opts[i].name);
2605 seq_printf(m, "no%s\n", trace_opts[i].name);
2607 mutex_unlock(&trace_types_lock);
2612 static int __set_tracer_option(struct tracer *trace,
2613 struct tracer_flags *tracer_flags,
2614 struct tracer_opt *opts, int neg)
2618 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2623 tracer_flags->val &= ~opts->bit;
2625 tracer_flags->val |= opts->bit;
2629 /* Try to assign a tracer specific option */
2630 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2632 struct tracer_flags *tracer_flags = trace->flags;
2633 struct tracer_opt *opts = NULL;
2636 for (i = 0; tracer_flags->opts[i].name; i++) {
2637 opts = &tracer_flags->opts[i];
2639 if (strcmp(cmp, opts->name) == 0)
2640 return __set_tracer_option(trace, trace->flags,
2647 /* Some tracers require overwrite to stay enabled */
2648 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
2650 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
2656 int set_tracer_flag(unsigned int mask, int enabled)
2658 /* do nothing if flag is already set */
2659 if (!!(trace_flags & mask) == !!enabled)
2662 /* Give the tracer a chance to approve the change */
2663 if (current_trace->flag_changed)
2664 if (current_trace->flag_changed(current_trace, mask, !!enabled))
2668 trace_flags |= mask;
2670 trace_flags &= ~mask;
2672 if (mask == TRACE_ITER_RECORD_CMD)
2673 trace_event_enable_cmd_record(enabled);
2675 if (mask == TRACE_ITER_OVERWRITE) {
2676 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2677 #ifdef CONFIG_TRACER_MAX_TRACE
2678 ring_buffer_change_overwrite(max_tr.buffer, enabled);
2686 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2687 size_t cnt, loff_t *ppos)
2695 if (cnt >= sizeof(buf))
2698 if (copy_from_user(&buf, ubuf, cnt))
2702 cmp = strstrip(buf);
2704 if (strncmp(cmp, "no", 2) == 0) {
2709 mutex_lock(&trace_types_lock);
2711 for (i = 0; trace_options[i]; i++) {
2712 if (strcmp(cmp, trace_options[i]) == 0) {
2713 ret = set_tracer_flag(1 << i, !neg);
2718 /* If no option could be set, test the specific tracer options */
2719 if (!trace_options[i])
2720 ret = set_tracer_option(current_trace, cmp, neg);
2722 mutex_unlock(&trace_types_lock);
2732 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2734 if (tracing_disabled)
2736 return single_open(file, tracing_trace_options_show, NULL);
2739 static const struct file_operations tracing_iter_fops = {
2740 .open = tracing_trace_options_open,
2742 .llseek = seq_lseek,
2743 .release = single_release,
2744 .write = tracing_trace_options_write,
2747 static const char readme_msg[] =
2748 "tracing mini-HOWTO:\n\n"
2749 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2750 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2751 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2752 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2754 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2755 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2757 "# cat /sys/kernel/debug/tracing/trace_options\n"
2758 "noprint-parent nosym-offset nosym-addr noverbose\n"
2759 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2760 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2761 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2762 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2766 tracing_readme_read(struct file *filp, char __user *ubuf,
2767 size_t cnt, loff_t *ppos)
2769 return simple_read_from_buffer(ubuf, cnt, ppos,
2770 readme_msg, strlen(readme_msg));
2773 static const struct file_operations tracing_readme_fops = {
2774 .open = tracing_open_generic,
2775 .read = tracing_readme_read,
2776 .llseek = generic_file_llseek,
2780 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2781 size_t cnt, loff_t *ppos)
2790 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2794 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2802 for (i = 0; i < SAVED_CMDLINES; i++) {
2805 pid = map_cmdline_to_pid[i];
2806 if (pid == -1 || pid == NO_CMDLINE_MAP)
2809 trace_find_cmdline(pid, buf_comm);
2810 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2815 len = simple_read_from_buffer(ubuf, cnt, ppos,
2824 static const struct file_operations tracing_saved_cmdlines_fops = {
2825 .open = tracing_open_generic,
2826 .read = tracing_saved_cmdlines_read,
2827 .llseek = generic_file_llseek,
2831 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2832 size_t cnt, loff_t *ppos)
2837 r = sprintf(buf, "%u\n", tracer_enabled);
2838 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2842 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2843 size_t cnt, loff_t *ppos)
2845 struct trace_array *tr = filp->private_data;
2849 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
2855 mutex_lock(&trace_types_lock);
2856 if (tracer_enabled ^ val) {
2858 /* Only need to warn if this is used to change the state */
2859 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2863 if (current_trace->start)
2864 current_trace->start(tr);
2869 if (current_trace->stop)
2870 current_trace->stop(tr);
2873 mutex_unlock(&trace_types_lock);
2881 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2882 size_t cnt, loff_t *ppos)
2884 char buf[MAX_TRACER_SIZE+2];
2887 mutex_lock(&trace_types_lock);
2889 r = sprintf(buf, "%s\n", current_trace->name);
2891 r = sprintf(buf, "\n");
2892 mutex_unlock(&trace_types_lock);
2894 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2897 int tracer_init(struct tracer *t, struct trace_array *tr)
2899 tracing_reset_online_cpus(tr);
2903 static int __tracing_resize_ring_buffer(unsigned long size)
2908 * If kernel or user changes the size of the ring buffer
2909 * we use the size that was given, and we can forget about
2910 * expanding it later.
2912 ring_buffer_expanded = 1;
2914 ret = ring_buffer_resize(global_trace.buffer, size);
2918 if (!current_trace->use_max_tr)
2921 ret = ring_buffer_resize(max_tr.buffer, size);
2925 r = ring_buffer_resize(global_trace.buffer,
2926 global_trace.entries);
2929 * AARGH! We are left with different
2930 * size max buffer!!!!
2931 * The max buffer is our "snapshot" buffer.
2932 * When a tracer needs a snapshot (one of the
2933 * latency tracers), it swaps the max buffer
2934 * with the saved snap shot. We succeeded to
2935 * update the size of the main buffer, but failed to
2936 * update the size of the max buffer. But when we tried
2937 * to reset the main buffer to the original size, we
2938 * failed there too. This is very unlikely to
2939 * happen, but if it does, warn and kill all
2943 tracing_disabled = 1;
2948 max_tr.entries = size;
2950 global_trace.entries = size;
2955 static ssize_t tracing_resize_ring_buffer(unsigned long size)
2957 int cpu, ret = size;
2959 mutex_lock(&trace_types_lock);
2963 /* disable all cpu buffers */
2964 for_each_tracing_cpu(cpu) {
2965 if (global_trace.data[cpu])
2966 atomic_inc(&global_trace.data[cpu]->disabled);
2967 if (max_tr.data[cpu])
2968 atomic_inc(&max_tr.data[cpu]->disabled);
2971 if (size != global_trace.entries)
2972 ret = __tracing_resize_ring_buffer(size);
2977 for_each_tracing_cpu(cpu) {
2978 if (global_trace.data[cpu])
2979 atomic_dec(&global_trace.data[cpu]->disabled);
2980 if (max_tr.data[cpu])
2981 atomic_dec(&max_tr.data[cpu]->disabled);
2985 mutex_unlock(&trace_types_lock);
2992 * tracing_update_buffers - used by tracing facility to expand ring buffers
2994 * To save on memory when the tracing is never used on a system with it
2995 * configured in. The ring buffers are set to a minimum size. But once
2996 * a user starts to use the tracing facility, then they need to grow
2997 * to their default size.
2999 * This function is to be called when a tracer is about to be used.
3001 int tracing_update_buffers(void)
3005 mutex_lock(&trace_types_lock);
3006 if (!ring_buffer_expanded)
3007 ret = __tracing_resize_ring_buffer(trace_buf_size);
3008 mutex_unlock(&trace_types_lock);
3013 struct trace_option_dentry;
3015 static struct trace_option_dentry *
3016 create_trace_option_files(struct tracer *tracer);
3019 destroy_trace_option_files(struct trace_option_dentry *topts);
3021 static int tracing_set_tracer(const char *buf)
3023 static struct trace_option_dentry *topts;
3024 struct trace_array *tr = &global_trace;
3028 mutex_lock(&trace_types_lock);
3030 if (!ring_buffer_expanded) {
3031 ret = __tracing_resize_ring_buffer(trace_buf_size);
3037 for (t = trace_types; t; t = t->next) {
3038 if (strcmp(t->name, buf) == 0)
3045 if (t == current_trace)
3048 trace_branch_disable();
3050 current_trace->enabled = false;
3052 if (current_trace && current_trace->reset)
3053 current_trace->reset(tr);
3054 if (current_trace && current_trace->use_max_tr) {
3056 * We don't free the ring buffer. instead, resize it because
3057 * The max_tr ring buffer has some state (e.g. ring->clock) and
3058 * we want preserve it.
3060 ring_buffer_resize(max_tr.buffer, 1);
3063 destroy_trace_option_files(topts);
3067 topts = create_trace_option_files(current_trace);
3068 if (current_trace->use_max_tr) {
3069 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
3072 max_tr.entries = global_trace.entries;
3076 ret = tracer_init(t, tr);
3081 current_trace->enabled = true;
3082 trace_branch_enable(tr);
3084 mutex_unlock(&trace_types_lock);
3090 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3091 size_t cnt, loff_t *ppos)
3093 char buf[MAX_TRACER_SIZE+1];
3100 if (cnt > MAX_TRACER_SIZE)
3101 cnt = MAX_TRACER_SIZE;
3103 if (copy_from_user(&buf, ubuf, cnt))
3108 /* strip ending whitespace. */
3109 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3112 err = tracing_set_tracer(buf);
3122 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3123 size_t cnt, loff_t *ppos)
3125 unsigned long *ptr = filp->private_data;
3129 r = snprintf(buf, sizeof(buf), "%ld\n",
3130 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3131 if (r > sizeof(buf))
3133 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3137 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3138 size_t cnt, loff_t *ppos)
3140 unsigned long *ptr = filp->private_data;
3144 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3153 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3155 long cpu_file = (long) inode->i_private;
3156 struct trace_iterator *iter;
3159 if (tracing_disabled)
3162 mutex_lock(&trace_types_lock);
3164 /* create a buffer to store the information to pass to userspace */
3165 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3172 * We make a copy of the current tracer to avoid concurrent
3173 * changes on it while we are reading.
3175 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3181 *iter->trace = *current_trace;
3183 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3188 /* trace pipe does not show start of buffer */
3189 cpumask_setall(iter->started);
3191 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3192 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3194 iter->cpu_file = cpu_file;
3195 iter->tr = &global_trace;
3196 mutex_init(&iter->mutex);
3197 filp->private_data = iter;
3199 if (iter->trace->pipe_open)
3200 iter->trace->pipe_open(iter);
3202 nonseekable_open(inode, filp);
3204 mutex_unlock(&trace_types_lock);
3210 mutex_unlock(&trace_types_lock);
3214 static int tracing_release_pipe(struct inode *inode, struct file *file)
3216 struct trace_iterator *iter = file->private_data;
3218 mutex_lock(&trace_types_lock);
3220 if (iter->trace->pipe_close)
3221 iter->trace->pipe_close(iter);
3223 mutex_unlock(&trace_types_lock);
3225 free_cpumask_var(iter->started);
3226 mutex_destroy(&iter->mutex);
3234 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3236 struct trace_iterator *iter = filp->private_data;
3238 if (trace_flags & TRACE_ITER_BLOCK) {
3240 * Always select as readable when in blocking mode
3242 return POLLIN | POLLRDNORM;
3244 if (!trace_empty(iter))
3245 return POLLIN | POLLRDNORM;
3246 poll_wait(filp, &trace_wait, poll_table);
3247 if (!trace_empty(iter))
3248 return POLLIN | POLLRDNORM;
3255 void default_wait_pipe(struct trace_iterator *iter)
3259 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3261 if (trace_empty(iter))
3264 finish_wait(&trace_wait, &wait);
3268 * This is a make-shift waitqueue.
3269 * A tracer might use this callback on some rare cases:
3271 * 1) the current tracer might hold the runqueue lock when it wakes up
3272 * a reader, hence a deadlock (sched, function, and function graph tracers)
3273 * 2) the function tracers, trace all functions, we don't want
3274 * the overhead of calling wake_up and friends
3275 * (and tracing them too)
3277 * Anyway, this is really very primitive wakeup.
3279 void poll_wait_pipe(struct trace_iterator *iter)
3281 set_current_state(TASK_INTERRUPTIBLE);
3282 /* sleep for 100 msecs, and try again. */
3283 schedule_timeout(HZ / 10);
3286 /* Must be called with trace_types_lock mutex held. */
3287 static int tracing_wait_pipe(struct file *filp)
3289 struct trace_iterator *iter = filp->private_data;
3291 while (trace_empty(iter)) {
3293 if ((filp->f_flags & O_NONBLOCK)) {
3297 mutex_unlock(&iter->mutex);
3299 iter->trace->wait_pipe(iter);
3301 mutex_lock(&iter->mutex);
3303 if (signal_pending(current))
3307 * We block until we read something and tracing is disabled.
3308 * We still block if tracing is disabled, but we have never
3309 * read anything. This allows a user to cat this file, and
3310 * then enable tracing. But after we have read something,
3311 * we give an EOF when tracing is again disabled.
3313 * iter->pos will be 0 if we haven't read anything.
3315 if (!tracer_enabled && iter->pos)
3326 tracing_read_pipe(struct file *filp, char __user *ubuf,
3327 size_t cnt, loff_t *ppos)
3329 struct trace_iterator *iter = filp->private_data;
3330 static struct tracer *old_tracer;
3333 /* return any leftover data */
3334 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3338 trace_seq_init(&iter->seq);
3340 /* copy the tracer to avoid using a global lock all around */
3341 mutex_lock(&trace_types_lock);
3342 if (unlikely(old_tracer != current_trace && current_trace)) {
3343 old_tracer = current_trace;
3344 *iter->trace = *current_trace;
3346 mutex_unlock(&trace_types_lock);
3349 * Avoid more than one consumer on a single file descriptor
3350 * This is just a matter of traces coherency, the ring buffer itself
3353 mutex_lock(&iter->mutex);
3354 if (iter->trace->read) {
3355 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3361 sret = tracing_wait_pipe(filp);
3365 /* stop when tracing is finished */
3366 if (trace_empty(iter)) {
3371 if (cnt >= PAGE_SIZE)
3372 cnt = PAGE_SIZE - 1;
3374 /* reset all but tr, trace, and overruns */
3375 memset(&iter->seq, 0,
3376 sizeof(struct trace_iterator) -
3377 offsetof(struct trace_iterator, seq));
3378 cpumask_clear(iter->started);
3381 trace_event_read_lock();
3382 trace_access_lock(iter->cpu_file);
3383 while (trace_find_next_entry_inc(iter) != NULL) {
3384 enum print_line_t ret;
3385 int len = iter->seq.len;
3387 ret = print_trace_line(iter);
3388 if (ret == TRACE_TYPE_PARTIAL_LINE) {