2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
42 #include "trace_output.h"
45 * On boot up, the ring buffer is set to the minimum size, so that
46 * we do not waste memory on systems that are not using tracing.
48 int ring_buffer_expanded;
51 * We need to change this state when a selftest is running.
52 * A selftest will lurk into the ring-buffer to count the
53 * entries inserted during the selftest although some concurrent
54 * insertions into the ring-buffer such as trace_printk could occurred
55 * at the same time, giving false positive or negative results.
57 static bool __read_mostly tracing_selftest_running;
60 * If a tracer is running, we do not want to run SELFTEST.
62 bool __read_mostly tracing_selftest_disabled;
64 /* For tracers that don't implement custom flags */
65 static struct tracer_opt dummy_tracer_opt[] = {
69 static struct tracer_flags dummy_tracer_flags = {
71 .opts = dummy_tracer_opt
74 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
80 * Kill all tracing for good (never come back).
81 * It is initialized to 1 but will turn to zero if the initialization
82 * of the tracer is successful. But that is the only place that sets
85 static int tracing_disabled = 1;
87 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
89 static inline void ftrace_disable_cpu(void)
92 __this_cpu_inc(ftrace_cpu_disabled);
95 static inline void ftrace_enable_cpu(void)
97 __this_cpu_dec(ftrace_cpu_disabled);
101 cpumask_var_t __read_mostly tracing_buffer_mask;
104 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
106 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
107 * is set, then ftrace_dump is called. This will output the contents
108 * of the ftrace buffers to the console. This is very useful for
109 * capturing traces that lead to crashes and outputing it to a
112 * It is default off, but you can enable it with either specifying
113 * "ftrace_dump_on_oops" in the kernel command line, or setting
114 * /proc/sys/kernel/ftrace_dump_on_oops
115 * Set 1 if you want to dump buffers of all CPUs
116 * Set 2 if you want to dump the buffer of the CPU that triggered oops
119 enum ftrace_dump_mode ftrace_dump_on_oops;
121 static int tracing_set_tracer(const char *buf);
123 #define MAX_TRACER_SIZE 100
124 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
125 static char *default_bootup_tracer;
127 static int __init set_cmdline_ftrace(char *str)
129 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
130 default_bootup_tracer = bootup_tracer_buf;
131 /* We are using ftrace early, expand it */
132 ring_buffer_expanded = 1;
135 __setup("ftrace=", set_cmdline_ftrace);
137 static int __init set_ftrace_dump_on_oops(char *str)
139 if (*str++ != '=' || !*str) {
140 ftrace_dump_on_oops = DUMP_ALL;
144 if (!strcmp("orig_cpu", str)) {
145 ftrace_dump_on_oops = DUMP_ORIG;
151 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
153 unsigned long long ns2usecs(cycle_t nsec)
161 * The global_trace is the descriptor that holds the tracing
162 * buffers for the live tracing. For each CPU, it contains
163 * a link list of pages that will store trace entries. The
164 * page descriptor of the pages in the memory is used to hold
165 * the link list by linking the lru item in the page descriptor
166 * to each of the pages in the buffer per CPU.
168 * For each active CPU there is a data field that holds the
169 * pages for the buffer for that CPU. Each CPU has the same number
170 * of pages allocated for its buffer.
172 static struct trace_array global_trace;
174 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
176 int filter_current_check_discard(struct ring_buffer *buffer,
177 struct ftrace_event_call *call, void *rec,
178 struct ring_buffer_event *event)
180 return filter_check_discard(call, rec, buffer, event);
182 EXPORT_SYMBOL_GPL(filter_current_check_discard);
184 cycle_t ftrace_now(int cpu)
188 /* Early boot up does not have a buffer yet */
189 if (!global_trace.buffer)
190 return trace_clock_local();
192 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
193 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
199 * The max_tr is used to snapshot the global_trace when a maximum
200 * latency is reached. Some tracers will use this to store a maximum
201 * trace while it continues examining live traces.
203 * The buffers for the max_tr are set up the same as the global_trace.
204 * When a snapshot is taken, the link list of the max_tr is swapped
205 * with the link list of the global_trace and the buffers are reset for
206 * the global_trace so the tracing can continue.
208 static struct trace_array max_tr;
210 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
212 /* tracer_enabled is used to toggle activation of a tracer */
213 static int tracer_enabled = 1;
216 * tracing_is_enabled - return tracer_enabled status
218 * This function is used by other tracers to know the status
219 * of the tracer_enabled flag. Tracers may use this function
220 * to know if it should enable their features when starting
221 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
223 int tracing_is_enabled(void)
225 return tracer_enabled;
229 * trace_buf_size is the size in bytes that is allocated
230 * for a buffer. Note, the number of bytes is always rounded
233 * This number is purposely set to a low number of 16384.
234 * If the dump on oops happens, it will be much appreciated
235 * to not have to wait for all that output. Anyway this can be
236 * boot time and run time configurable.
238 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
240 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
242 /* trace_types holds a link list of available tracers. */
243 static struct tracer *trace_types __read_mostly;
245 /* current_trace points to the tracer that is currently active */
246 static struct tracer *current_trace __read_mostly;
249 * trace_types_lock is used to protect the trace_types list.
251 static DEFINE_MUTEX(trace_types_lock);
254 * serialize the access of the ring buffer
256 * ring buffer serializes readers, but it is low level protection.
257 * The validity of the events (which returns by ring_buffer_peek() ..etc)
258 * are not protected by ring buffer.
260 * The content of events may become garbage if we allow other process consumes
261 * these events concurrently:
262 * A) the page of the consumed events may become a normal page
263 * (not reader page) in ring buffer, and this page will be rewrited
264 * by events producer.
265 * B) The page of the consumed events may become a page for splice_read,
266 * and this page will be returned to system.
268 * These primitives allow multi process access to different cpu ring buffer
271 * These primitives don't distinguish read-only and read-consume access.
272 * Multi read-only access are also serialized.
276 static DECLARE_RWSEM(all_cpu_access_lock);
277 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
279 static inline void trace_access_lock(int cpu)
281 if (cpu == TRACE_PIPE_ALL_CPU) {
282 /* gain it for accessing the whole ring buffer. */
283 down_write(&all_cpu_access_lock);
285 /* gain it for accessing a cpu ring buffer. */
287 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
288 down_read(&all_cpu_access_lock);
290 /* Secondly block other access to this @cpu ring buffer. */
291 mutex_lock(&per_cpu(cpu_access_lock, cpu));
295 static inline void trace_access_unlock(int cpu)
297 if (cpu == TRACE_PIPE_ALL_CPU) {
298 up_write(&all_cpu_access_lock);
300 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
301 up_read(&all_cpu_access_lock);
305 static inline void trace_access_lock_init(void)
309 for_each_possible_cpu(cpu)
310 mutex_init(&per_cpu(cpu_access_lock, cpu));
315 static DEFINE_MUTEX(access_lock);
317 static inline void trace_access_lock(int cpu)
320 mutex_lock(&access_lock);
323 static inline void trace_access_unlock(int cpu)
326 mutex_unlock(&access_lock);
329 static inline void trace_access_lock_init(void)
335 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
336 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
338 /* trace_flags holds trace_options default values */
339 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
340 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
341 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE;
343 static int trace_stop_count;
344 static DEFINE_RAW_SPINLOCK(tracing_start_lock);
346 static void wakeup_work_handler(struct work_struct *work)
348 wake_up(&trace_wait);
351 static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
354 * trace_wake_up - wake up tasks waiting for trace input
356 * Schedules a delayed work to wake up any task that is blocked on the
357 * trace_wait queue. These is used with trace_poll for tasks polling the
360 void trace_wake_up(void)
362 const unsigned long delay = msecs_to_jiffies(2);
364 if (trace_flags & TRACE_ITER_BLOCK)
366 schedule_delayed_work(&wakeup_work, delay);
369 static int __init set_buf_size(char *str)
371 unsigned long buf_size;
375 buf_size = memparse(str, &str);
376 /* nr_entries can not be zero */
379 trace_buf_size = buf_size;
382 __setup("trace_buf_size=", set_buf_size);
384 static int __init set_tracing_thresh(char *str)
386 unsigned long threshhold;
391 ret = strict_strtoul(str, 0, &threshhold);
394 tracing_thresh = threshhold * 1000;
397 __setup("tracing_thresh=", set_tracing_thresh);
399 unsigned long nsecs_to_usecs(unsigned long nsecs)
404 /* These must match the bit postions in trace_iterator_flags */
405 static const char *trace_options[] = {
436 { trace_clock_local, "local" },
437 { trace_clock_global, "global" },
438 { trace_clock_counter, "counter" },
444 * trace_parser_get_init - gets the buffer for trace parser
446 int trace_parser_get_init(struct trace_parser *parser, int size)
448 memset(parser, 0, sizeof(*parser));
450 parser->buffer = kmalloc(size, GFP_KERNEL);
459 * trace_parser_put - frees the buffer for trace parser
461 void trace_parser_put(struct trace_parser *parser)
463 kfree(parser->buffer);
467 * trace_get_user - reads the user input string separated by space
468 * (matched by isspace(ch))
470 * For each string found the 'struct trace_parser' is updated,
471 * and the function returns.
473 * Returns number of bytes read.
475 * See kernel/trace/trace.h for 'struct trace_parser' details.
477 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
478 size_t cnt, loff_t *ppos)
485 trace_parser_clear(parser);
487 ret = get_user(ch, ubuf++);
495 * The parser is not finished with the last write,
496 * continue reading the user input without skipping spaces.
499 /* skip white space */
500 while (cnt && isspace(ch)) {
501 ret = get_user(ch, ubuf++);
508 /* only spaces were written */
518 /* read the non-space input */
519 while (cnt && !isspace(ch)) {
520 if (parser->idx < parser->size - 1)
521 parser->buffer[parser->idx++] = ch;
526 ret = get_user(ch, ubuf++);
533 /* We either got finished input or we have to wait for another call. */
535 parser->buffer[parser->idx] = 0;
536 parser->cont = false;
539 parser->buffer[parser->idx++] = ch;
549 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
557 if (s->len <= s->readpos)
560 len = s->len - s->readpos;
563 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
573 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
578 if (s->len <= s->readpos)
581 len = s->len - s->readpos;
584 ret = memcpy(buf, s->buffer + s->readpos, cnt);
593 * ftrace_max_lock is used to protect the swapping of buffers
594 * when taking a max snapshot. The buffers themselves are
595 * protected by per_cpu spinlocks. But the action of the swap
596 * needs its own lock.
598 * This is defined as a arch_spinlock_t in order to help
599 * with performance when lockdep debugging is enabled.
601 * It is also used in other places outside the update_max_tr
602 * so it needs to be defined outside of the
603 * CONFIG_TRACER_MAX_TRACE.
605 static arch_spinlock_t ftrace_max_lock =
606 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
608 unsigned long __read_mostly tracing_thresh;
610 #ifdef CONFIG_TRACER_MAX_TRACE
611 unsigned long __read_mostly tracing_max_latency;
614 * Copy the new maximum trace into the separate maximum-trace
615 * structure. (this way the maximum trace is permanently saved,
616 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
619 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
621 struct trace_array_cpu *data = tr->data[cpu];
622 struct trace_array_cpu *max_data;
625 max_tr.time_start = data->preempt_timestamp;
627 max_data = max_tr.data[cpu];
628 max_data->saved_latency = tracing_max_latency;
629 max_data->critical_start = data->critical_start;
630 max_data->critical_end = data->critical_end;
632 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
633 max_data->pid = tsk->pid;
634 max_data->uid = task_uid(tsk);
635 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
636 max_data->policy = tsk->policy;
637 max_data->rt_priority = tsk->rt_priority;
639 /* record this tasks comm */
640 tracing_record_cmdline(tsk);
644 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
646 * @tsk: the task with the latency
647 * @cpu: The cpu that initiated the trace.
649 * Flip the buffers between the @tr and the max_tr and record information
650 * about which task was the cause of this latency.
653 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
655 struct ring_buffer *buf;
657 if (trace_stop_count)
660 WARN_ON_ONCE(!irqs_disabled());
661 if (!current_trace->use_max_tr) {
665 arch_spin_lock(&ftrace_max_lock);
668 tr->buffer = max_tr.buffer;
671 __update_max_tr(tr, tsk, cpu);
672 arch_spin_unlock(&ftrace_max_lock);
676 * update_max_tr_single - only copy one trace over, and reset the rest
678 * @tsk - task with the latency
679 * @cpu - the cpu of the buffer to copy.
681 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
684 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
688 if (trace_stop_count)
691 WARN_ON_ONCE(!irqs_disabled());
692 if (!current_trace->use_max_tr) {
697 arch_spin_lock(&ftrace_max_lock);
699 ftrace_disable_cpu();
701 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
705 * We failed to swap the buffer due to a commit taking
706 * place on this CPU. We fail to record, but we reset
707 * the max trace buffer (no one writes directly to it)
708 * and flag that it failed.
710 trace_array_printk(&max_tr, _THIS_IP_,
711 "Failed to swap buffers due to commit in progress\n");
716 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
718 __update_max_tr(tr, tsk, cpu);
719 arch_spin_unlock(&ftrace_max_lock);
721 #endif /* CONFIG_TRACER_MAX_TRACE */
724 * register_tracer - register a tracer with the ftrace system.
725 * @type - the plugin for the tracer
727 * Register a new plugin tracer.
729 int register_tracer(struct tracer *type)
730 __releases(kernel_lock)
731 __acquires(kernel_lock)
737 pr_info("Tracer must have a name\n");
741 if (strlen(type->name) >= MAX_TRACER_SIZE) {
742 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
746 mutex_lock(&trace_types_lock);
748 tracing_selftest_running = true;
750 for (t = trace_types; t; t = t->next) {
751 if (strcmp(type->name, t->name) == 0) {
753 pr_info("Tracer %s already registered\n",
761 type->set_flag = &dummy_set_flag;
763 type->flags = &dummy_tracer_flags;
765 if (!type->flags->opts)
766 type->flags->opts = dummy_tracer_opt;
767 if (!type->wait_pipe)
768 type->wait_pipe = default_wait_pipe;
771 #ifdef CONFIG_FTRACE_STARTUP_TEST
772 if (type->selftest && !tracing_selftest_disabled) {
773 struct tracer *saved_tracer = current_trace;
774 struct trace_array *tr = &global_trace;
777 * Run a selftest on this tracer.
778 * Here we reset the trace buffer, and set the current
779 * tracer to be this tracer. The tracer can then run some
780 * internal tracing to verify that everything is in order.
781 * If we fail, we do not register this tracer.
783 tracing_reset_online_cpus(tr);
785 current_trace = type;
787 /* If we expanded the buffers, make sure the max is expanded too */
788 if (ring_buffer_expanded && type->use_max_tr)
789 ring_buffer_resize(max_tr.buffer, trace_buf_size);
791 /* the test is responsible for initializing and enabling */
792 pr_info("Testing tracer %s: ", type->name);
793 ret = type->selftest(type, tr);
794 /* the test is responsible for resetting too */
795 current_trace = saved_tracer;
797 printk(KERN_CONT "FAILED!\n");
800 /* Only reset on passing, to avoid touching corrupted buffers */
801 tracing_reset_online_cpus(tr);
803 /* Shrink the max buffer again */
804 if (ring_buffer_expanded && type->use_max_tr)
805 ring_buffer_resize(max_tr.buffer, 1);
807 printk(KERN_CONT "PASSED\n");
811 type->next = trace_types;
815 tracing_selftest_running = false;
816 mutex_unlock(&trace_types_lock);
818 if (ret || !default_bootup_tracer)
821 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
824 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
825 /* Do we want this tracer to start on bootup? */
826 tracing_set_tracer(type->name);
827 default_bootup_tracer = NULL;
828 /* disable other selftests, since this will break it. */
829 tracing_selftest_disabled = 1;
830 #ifdef CONFIG_FTRACE_STARTUP_TEST
831 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
839 void unregister_tracer(struct tracer *type)
843 mutex_lock(&trace_types_lock);
844 for (t = &trace_types; *t; t = &(*t)->next) {
848 pr_info("Tracer %s not registered\n", type->name);
854 if (type == current_trace && tracer_enabled) {
857 if (current_trace->stop)
858 current_trace->stop(&global_trace);
859 current_trace = &nop_trace;
862 mutex_unlock(&trace_types_lock);
865 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
867 ftrace_disable_cpu();
868 ring_buffer_reset_cpu(buffer, cpu);
872 void tracing_reset(struct trace_array *tr, int cpu)
874 struct ring_buffer *buffer = tr->buffer;
876 ring_buffer_record_disable(buffer);
878 /* Make sure all commits have finished */
880 __tracing_reset(buffer, cpu);
882 ring_buffer_record_enable(buffer);
885 void tracing_reset_online_cpus(struct trace_array *tr)
887 struct ring_buffer *buffer = tr->buffer;
890 ring_buffer_record_disable(buffer);
892 /* Make sure all commits have finished */
895 tr->time_start = ftrace_now(tr->cpu);
897 for_each_online_cpu(cpu)
898 __tracing_reset(buffer, cpu);
900 ring_buffer_record_enable(buffer);
903 void tracing_reset_current(int cpu)
905 tracing_reset(&global_trace, cpu);
908 void tracing_reset_current_online_cpus(void)
910 tracing_reset_online_cpus(&global_trace);
913 #define SAVED_CMDLINES 128
914 #define NO_CMDLINE_MAP UINT_MAX
915 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
916 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
917 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
918 static int cmdline_idx;
919 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
921 /* temporary disable recording */
922 static atomic_t trace_record_cmdline_disabled __read_mostly;
924 static void trace_init_cmdlines(void)
926 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
927 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
931 int is_tracing_stopped(void)
933 return trace_stop_count;
937 * ftrace_off_permanent - disable all ftrace code permanently
939 * This should only be called when a serious anomally has
940 * been detected. This will turn off the function tracing,
941 * ring buffers, and other tracing utilites. It takes no
942 * locks and can be called from any context.
944 void ftrace_off_permanent(void)
946 tracing_disabled = 1;
948 tracing_off_permanent();
952 * tracing_start - quick start of the tracer
954 * If tracing is enabled but was stopped by tracing_stop,
955 * this will start the tracer back up.
957 void tracing_start(void)
959 struct ring_buffer *buffer;
962 if (tracing_disabled)
965 raw_spin_lock_irqsave(&tracing_start_lock, flags);
966 if (--trace_stop_count) {
967 if (trace_stop_count < 0) {
968 /* Someone screwed up their debugging */
970 trace_stop_count = 0;
975 /* Prevent the buffers from switching */
976 arch_spin_lock(&ftrace_max_lock);
978 buffer = global_trace.buffer;
980 ring_buffer_record_enable(buffer);
982 buffer = max_tr.buffer;
984 ring_buffer_record_enable(buffer);
986 arch_spin_unlock(&ftrace_max_lock);
990 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
994 * tracing_stop - quick stop of the tracer
996 * Light weight way to stop tracing. Use in conjunction with
999 void tracing_stop(void)
1001 struct ring_buffer *buffer;
1002 unsigned long flags;
1005 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1006 if (trace_stop_count++)
1009 /* Prevent the buffers from switching */
1010 arch_spin_lock(&ftrace_max_lock);
1012 buffer = global_trace.buffer;
1014 ring_buffer_record_disable(buffer);
1016 buffer = max_tr.buffer;
1018 ring_buffer_record_disable(buffer);
1020 arch_spin_unlock(&ftrace_max_lock);
1023 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1026 void trace_stop_cmdline_recording(void);
1028 static void trace_save_cmdline(struct task_struct *tsk)
1032 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1036 * It's not the end of the world if we don't get
1037 * the lock, but we also don't want to spin
1038 * nor do we want to disable interrupts,
1039 * so if we miss here, then better luck next time.
1041 if (!arch_spin_trylock(&trace_cmdline_lock))
1044 idx = map_pid_to_cmdline[tsk->pid];
1045 if (idx == NO_CMDLINE_MAP) {
1046 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1049 * Check whether the cmdline buffer at idx has a pid
1050 * mapped. We are going to overwrite that entry so we
1051 * need to clear the map_pid_to_cmdline. Otherwise we
1052 * would read the new comm for the old pid.
1054 pid = map_cmdline_to_pid[idx];
1055 if (pid != NO_CMDLINE_MAP)
1056 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1058 map_cmdline_to_pid[idx] = tsk->pid;
1059 map_pid_to_cmdline[tsk->pid] = idx;
1064 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1066 arch_spin_unlock(&trace_cmdline_lock);
1069 void trace_find_cmdline(int pid, char comm[])
1074 strcpy(comm, "<idle>");
1078 if (WARN_ON_ONCE(pid < 0)) {
1079 strcpy(comm, "<XXX>");
1083 if (pid > PID_MAX_DEFAULT) {
1084 strcpy(comm, "<...>");
1089 arch_spin_lock(&trace_cmdline_lock);
1090 map = map_pid_to_cmdline[pid];
1091 if (map != NO_CMDLINE_MAP)
1092 strcpy(comm, saved_cmdlines[map]);
1094 strcpy(comm, "<...>");
1096 arch_spin_unlock(&trace_cmdline_lock);
1100 void tracing_record_cmdline(struct task_struct *tsk)
1102 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1106 trace_save_cmdline(tsk);
1110 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1113 struct task_struct *tsk = current;
1115 entry->preempt_count = pc & 0xff;
1116 entry->pid = (tsk) ? tsk->pid : 0;
1119 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1120 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1122 TRACE_FLAG_IRQS_NOSUPPORT |
1124 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1125 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1126 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1128 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1130 struct ring_buffer_event *
1131 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1134 unsigned long flags, int pc)
1136 struct ring_buffer_event *event;
1138 event = ring_buffer_lock_reserve(buffer, len);
1139 if (event != NULL) {
1140 struct trace_entry *ent = ring_buffer_event_data(event);
1142 tracing_generic_entry_update(ent, flags, pc);
1150 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1151 struct ring_buffer_event *event,
1152 unsigned long flags, int pc,
1155 ring_buffer_unlock_commit(buffer, event);
1157 ftrace_trace_stack(buffer, flags, 6, pc);
1158 ftrace_trace_userstack(buffer, flags, pc);
1164 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1165 struct ring_buffer_event *event,
1166 unsigned long flags, int pc)
1168 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1171 struct ring_buffer_event *
1172 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1173 int type, unsigned long len,
1174 unsigned long flags, int pc)
1176 *current_rb = global_trace.buffer;
1177 return trace_buffer_lock_reserve(*current_rb,
1178 type, len, flags, pc);
1180 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1182 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1183 struct ring_buffer_event *event,
1184 unsigned long flags, int pc)
1186 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1188 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1190 void trace_nowake_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, 0);
1196 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1198 void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1199 struct ring_buffer_event *event,
1200 unsigned long flags, int pc,
1201 struct pt_regs *regs)
1203 ring_buffer_unlock_commit(buffer, event);
1205 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1206 ftrace_trace_userstack(buffer, flags, pc);
1208 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1210 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1211 struct ring_buffer_event *event)
1213 ring_buffer_discard_commit(buffer, event);
1215 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1218 trace_function(struct trace_array *tr,
1219 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1222 struct ftrace_event_call *call = &event_function;
1223 struct ring_buffer *buffer = tr->buffer;
1224 struct ring_buffer_event *event;
1225 struct ftrace_entry *entry;
1227 /* If we are reading the ring buffer, don't trace */
1228 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1231 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1235 entry = ring_buffer_event_data(event);
1237 entry->parent_ip = parent_ip;
1239 if (!filter_check_discard(call, entry, buffer, event))
1240 ring_buffer_unlock_commit(buffer, event);
1244 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1245 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1248 if (likely(!atomic_read(&data->disabled)))
1249 trace_function(tr, ip, parent_ip, flags, pc);
1252 #ifdef CONFIG_STACKTRACE
1254 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1255 struct ftrace_stack {
1256 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1259 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1260 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1262 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1263 unsigned long flags,
1264 int skip, int pc, struct pt_regs *regs)
1266 struct ftrace_event_call *call = &event_kernel_stack;
1267 struct ring_buffer_event *event;
1268 struct stack_entry *entry;
1269 struct stack_trace trace;
1271 int size = FTRACE_STACK_ENTRIES;
1273 trace.nr_entries = 0;
1277 * Since events can happen in NMIs there's no safe way to
1278 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1279 * or NMI comes in, it will just have to use the default
1280 * FTRACE_STACK_SIZE.
1282 preempt_disable_notrace();
1284 use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1286 * We don't need any atomic variables, just a barrier.
1287 * If an interrupt comes in, we don't care, because it would
1288 * have exited and put the counter back to what we want.
1289 * We just need a barrier to keep gcc from moving things
1293 if (use_stack == 1) {
1294 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1295 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1298 save_stack_trace_regs(regs, &trace);
1300 save_stack_trace(&trace);
1302 if (trace.nr_entries > size)
1303 size = trace.nr_entries;
1305 /* From now on, use_stack is a boolean */
1308 size *= sizeof(unsigned long);
1310 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1311 sizeof(*entry) + size, flags, pc);
1314 entry = ring_buffer_event_data(event);
1316 memset(&entry->caller, 0, size);
1319 memcpy(&entry->caller, trace.entries,
1320 trace.nr_entries * sizeof(unsigned long));
1322 trace.max_entries = FTRACE_STACK_ENTRIES;
1323 trace.entries = entry->caller;
1325 save_stack_trace_regs(regs, &trace);
1327 save_stack_trace(&trace);
1330 entry->size = trace.nr_entries;
1332 if (!filter_check_discard(call, entry, buffer, event))
1333 ring_buffer_unlock_commit(buffer, event);
1336 /* Again, don't let gcc optimize things here */
1338 __get_cpu_var(ftrace_stack_reserve)--;
1339 preempt_enable_notrace();
1343 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1344 int skip, int pc, struct pt_regs *regs)
1346 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1349 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1352 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1355 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1358 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1361 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1364 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1368 * trace_dump_stack - record a stack back trace in the trace buffer
1370 void trace_dump_stack(void)
1372 unsigned long flags;
1374 if (tracing_disabled || tracing_selftest_running)
1377 local_save_flags(flags);
1379 /* skipping 3 traces, seems to get us at the caller of this function */
1380 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1383 static DEFINE_PER_CPU(int, user_stack_count);
1386 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1388 struct ftrace_event_call *call = &event_user_stack;
1389 struct ring_buffer_event *event;
1390 struct userstack_entry *entry;
1391 struct stack_trace trace;
1393 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1397 * NMIs can not handle page faults, even with fix ups.
1398 * The save user stack can (and often does) fault.
1400 if (unlikely(in_nmi()))
1404 * prevent recursion, since the user stack tracing may
1405 * trigger other kernel events.
1408 if (__this_cpu_read(user_stack_count))
1411 __this_cpu_inc(user_stack_count);
1413 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1414 sizeof(*entry), flags, pc);
1416 goto out_drop_count;
1417 entry = ring_buffer_event_data(event);
1419 entry->tgid = current->tgid;
1420 memset(&entry->caller, 0, sizeof(entry->caller));
1422 trace.nr_entries = 0;
1423 trace.max_entries = FTRACE_STACK_ENTRIES;
1425 trace.entries = entry->caller;
1427 save_stack_trace_user(&trace);
1428 if (!filter_check_discard(call, entry, buffer, event))
1429 ring_buffer_unlock_commit(buffer, event);
1432 __this_cpu_dec(user_stack_count);
1438 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1440 ftrace_trace_userstack(tr, flags, preempt_count());
1444 #endif /* CONFIG_STACKTRACE */
1447 * trace_vbprintk - write binary msg to tracing buffer
1450 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1452 static arch_spinlock_t trace_buf_lock =
1453 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1454 static u32 trace_buf[TRACE_BUF_SIZE];
1456 struct ftrace_event_call *call = &event_bprint;
1457 struct ring_buffer_event *event;
1458 struct ring_buffer *buffer;
1459 struct trace_array *tr = &global_trace;
1460 struct trace_array_cpu *data;
1461 struct bprint_entry *entry;
1462 unsigned long flags;
1464 int cpu, len = 0, size, pc;
1466 if (unlikely(tracing_selftest_running || tracing_disabled))
1469 /* Don't pollute graph traces with trace_vprintk internals */
1470 pause_graph_tracing();
1472 pc = preempt_count();
1473 preempt_disable_notrace();
1474 cpu = raw_smp_processor_id();
1475 data = tr->data[cpu];
1477 disable = atomic_inc_return(&data->disabled);
1478 if (unlikely(disable != 1))
1481 /* Lockdep uses trace_printk for lock tracing */
1482 local_irq_save(flags);
1483 arch_spin_lock(&trace_buf_lock);
1484 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1486 if (len > TRACE_BUF_SIZE || len < 0)
1489 size = sizeof(*entry) + sizeof(u32) * len;
1490 buffer = tr->buffer;
1491 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1495 entry = ring_buffer_event_data(event);
1499 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1500 if (!filter_check_discard(call, entry, buffer, event)) {
1501 ring_buffer_unlock_commit(buffer, event);
1502 ftrace_trace_stack(buffer, flags, 6, pc);
1506 arch_spin_unlock(&trace_buf_lock);
1507 local_irq_restore(flags);
1510 atomic_dec_return(&data->disabled);
1511 preempt_enable_notrace();
1512 unpause_graph_tracing();
1516 EXPORT_SYMBOL_GPL(trace_vbprintk);
1518 int trace_array_printk(struct trace_array *tr,
1519 unsigned long ip, const char *fmt, ...)
1524 if (!(trace_flags & TRACE_ITER_PRINTK))
1528 ret = trace_array_vprintk(tr, ip, fmt, ap);
1533 int trace_array_vprintk(struct trace_array *tr,
1534 unsigned long ip, const char *fmt, va_list args)
1536 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1537 static char trace_buf[TRACE_BUF_SIZE];
1539 struct ftrace_event_call *call = &event_print;
1540 struct ring_buffer_event *event;
1541 struct ring_buffer *buffer;
1542 struct trace_array_cpu *data;
1543 int cpu, len = 0, size, pc;
1544 struct print_entry *entry;
1545 unsigned long irq_flags;
1548 if (tracing_disabled || tracing_selftest_running)
1551 pc = preempt_count();
1552 preempt_disable_notrace();
1553 cpu = raw_smp_processor_id();
1554 data = tr->data[cpu];
1556 disable = atomic_inc_return(&data->disabled);
1557 if (unlikely(disable != 1))
1560 pause_graph_tracing();
1561 raw_local_irq_save(irq_flags);
1562 arch_spin_lock(&trace_buf_lock);
1563 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1565 size = sizeof(*entry) + len + 1;
1566 buffer = tr->buffer;
1567 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1571 entry = ring_buffer_event_data(event);
1574 memcpy(&entry->buf, trace_buf, len);
1575 entry->buf[len] = '\0';
1576 if (!filter_check_discard(call, entry, buffer, event)) {
1577 ring_buffer_unlock_commit(buffer, event);
1578 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1582 arch_spin_unlock(&trace_buf_lock);
1583 raw_local_irq_restore(irq_flags);
1584 unpause_graph_tracing();
1586 atomic_dec_return(&data->disabled);
1587 preempt_enable_notrace();
1592 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1594 return trace_array_vprintk(&global_trace, ip, fmt, args);
1596 EXPORT_SYMBOL_GPL(trace_vprintk);
1598 static void trace_iterator_increment(struct trace_iterator *iter)
1600 /* Don't allow ftrace to trace into the ring buffers */
1601 ftrace_disable_cpu();
1604 if (iter->buffer_iter[iter->cpu])
1605 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1607 ftrace_enable_cpu();
1610 static struct trace_entry *
1611 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1612 unsigned long *lost_events)
1614 struct ring_buffer_event *event;
1615 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1617 /* Don't allow ftrace to trace into the ring buffers */
1618 ftrace_disable_cpu();
1621 event = ring_buffer_iter_peek(buf_iter, ts);
1623 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1626 ftrace_enable_cpu();
1629 iter->ent_size = ring_buffer_event_length(event);
1630 return ring_buffer_event_data(event);
1636 static struct trace_entry *
1637 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1638 unsigned long *missing_events, u64 *ent_ts)
1640 struct ring_buffer *buffer = iter->tr->buffer;
1641 struct trace_entry *ent, *next = NULL;
1642 unsigned long lost_events = 0, next_lost = 0;
1643 int cpu_file = iter->cpu_file;
1644 u64 next_ts = 0, ts;
1650 * If we are in a per_cpu trace file, don't bother by iterating over
1651 * all cpu and peek directly.
1653 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1654 if (ring_buffer_empty_cpu(buffer, cpu_file))
1656 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1658 *ent_cpu = cpu_file;
1663 for_each_tracing_cpu(cpu) {
1665 if (ring_buffer_empty_cpu(buffer, cpu))
1668 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1671 * Pick the entry with the smallest timestamp:
1673 if (ent && (!next || ts < next_ts)) {
1677 next_lost = lost_events;
1678 next_size = iter->ent_size;
1682 iter->ent_size = next_size;
1685 *ent_cpu = next_cpu;
1691 *missing_events = next_lost;
1696 /* Find the next real entry, without updating the iterator itself */
1697 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1698 int *ent_cpu, u64 *ent_ts)
1700 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1703 /* Find the next real entry, and increment the iterator to the next entry */
1704 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1706 iter->ent = __find_next_entry(iter, &iter->cpu,
1707 &iter->lost_events, &iter->ts);
1710 trace_iterator_increment(iter);
1712 return iter->ent ? iter : NULL;
1715 static void trace_consume(struct trace_iterator *iter)
1717 /* Don't allow ftrace to trace into the ring buffers */
1718 ftrace_disable_cpu();
1719 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1720 &iter->lost_events);
1721 ftrace_enable_cpu();
1724 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1726 struct trace_iterator *iter = m->private;
1730 WARN_ON_ONCE(iter->leftover);
1734 /* can't go backwards */
1739 ent = trace_find_next_entry_inc(iter);
1743 while (ent && iter->idx < i)
1744 ent = trace_find_next_entry_inc(iter);
1751 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1753 struct trace_array *tr = iter->tr;
1754 struct ring_buffer_event *event;
1755 struct ring_buffer_iter *buf_iter;
1756 unsigned long entries = 0;
1759 tr->data[cpu]->skipped_entries = 0;
1761 if (!iter->buffer_iter[cpu])
1764 buf_iter = iter->buffer_iter[cpu];
1765 ring_buffer_iter_reset(buf_iter);
1768 * We could have the case with the max latency tracers
1769 * that a reset never took place on a cpu. This is evident
1770 * by the timestamp being before the start of the buffer.
1772 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1773 if (ts >= iter->tr->time_start)
1776 ring_buffer_read(buf_iter, NULL);
1779 tr->data[cpu]->skipped_entries = entries;
1783 * The current tracer is copied to avoid a global locking
1786 static void *s_start(struct seq_file *m, loff_t *pos)
1788 struct trace_iterator *iter = m->private;
1789 static struct tracer *old_tracer;
1790 int cpu_file = iter->cpu_file;
1795 /* copy the tracer to avoid using a global lock all around */
1796 mutex_lock(&trace_types_lock);
1797 if (unlikely(old_tracer != current_trace && current_trace)) {
1798 old_tracer = current_trace;
1799 *iter->trace = *current_trace;
1801 mutex_unlock(&trace_types_lock);
1803 atomic_inc(&trace_record_cmdline_disabled);
1805 if (*pos != iter->pos) {
1810 ftrace_disable_cpu();
1812 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1813 for_each_tracing_cpu(cpu)
1814 tracing_iter_reset(iter, cpu);
1816 tracing_iter_reset(iter, cpu_file);
1818 ftrace_enable_cpu();
1821 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1826 * If we overflowed the seq_file before, then we want
1827 * to just reuse the trace_seq buffer again.
1833 p = s_next(m, p, &l);
1837 trace_event_read_lock();
1838 trace_access_lock(cpu_file);
1842 static void s_stop(struct seq_file *m, void *p)
1844 struct trace_iterator *iter = m->private;
1846 atomic_dec(&trace_record_cmdline_disabled);
1847 trace_access_unlock(iter->cpu_file);
1848 trace_event_read_unlock();
1851 static void print_lat_help_header(struct seq_file *m)
1853 seq_puts(m, "# _------=> CPU# \n");
1854 seq_puts(m, "# / _-----=> irqs-off \n");
1855 seq_puts(m, "# | / _----=> need-resched \n");
1856 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1857 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1858 seq_puts(m, "# |||| / delay \n");
1859 seq_puts(m, "# cmd pid ||||| time | caller \n");
1860 seq_puts(m, "# \\ / ||||| \\ | / \n");
1863 static void print_func_help_header(struct seq_file *m)
1865 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1866 seq_puts(m, "# | | | | |\n");
1871 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1873 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1874 struct trace_array *tr = iter->tr;
1875 struct trace_array_cpu *data = tr->data[tr->cpu];
1876 struct tracer *type = current_trace;
1877 unsigned long entries = 0;
1878 unsigned long total = 0;
1879 unsigned long count;
1880 const char *name = "preemption";
1887 for_each_tracing_cpu(cpu) {
1888 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1890 * If this buffer has skipped entries, then we hold all
1891 * entries for the trace and we need to ignore the
1892 * ones before the time stamp.
1894 if (tr->data[cpu]->skipped_entries) {
1895 count -= tr->data[cpu]->skipped_entries;
1896 /* total is the same as the entries */
1900 ring_buffer_overrun_cpu(tr->buffer, cpu);
1904 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1906 seq_puts(m, "# -----------------------------------"
1907 "---------------------------------\n");
1908 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1909 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1910 nsecs_to_usecs(data->saved_latency),
1914 #if defined(CONFIG_PREEMPT_NONE)
1916 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1918 #elif defined(CONFIG_PREEMPT)
1923 /* These are reserved for later use */
1926 seq_printf(m, " #P:%d)\n", num_online_cpus());
1930 seq_puts(m, "# -----------------\n");
1931 seq_printf(m, "# | task: %.16s-%d "
1932 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1933 data->comm, data->pid, data->uid, data->nice,
1934 data->policy, data->rt_priority);
1935 seq_puts(m, "# -----------------\n");
1937 if (data->critical_start) {
1938 seq_puts(m, "# => started at: ");
1939 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1940 trace_print_seq(m, &iter->seq);
1941 seq_puts(m, "\n# => ended at: ");
1942 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1943 trace_print_seq(m, &iter->seq);
1944 seq_puts(m, "\n#\n");
1950 static void test_cpu_buff_start(struct trace_iterator *iter)
1952 struct trace_seq *s = &iter->seq;
1954 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1957 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1960 if (cpumask_test_cpu(iter->cpu, iter->started))
1963 if (iter->tr->data[iter->cpu]->skipped_entries)
1966 cpumask_set_cpu(iter->cpu, iter->started);
1968 /* Don't print started cpu buffer for the first entry of the trace */
1970 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1974 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1976 struct trace_seq *s = &iter->seq;
1977 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1978 struct trace_entry *entry;
1979 struct trace_event *event;
1983 test_cpu_buff_start(iter);
1985 event = ftrace_find_event(entry->type);
1987 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1988 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1989 if (!trace_print_lat_context(iter))
1992 if (!trace_print_context(iter))
1998 return event->funcs->trace(iter, sym_flags, event);
2000 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2003 return TRACE_TYPE_HANDLED;
2005 return TRACE_TYPE_PARTIAL_LINE;
2008 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2010 struct trace_seq *s = &iter->seq;
2011 struct trace_entry *entry;
2012 struct trace_event *event;
2016 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2017 if (!trace_seq_printf(s, "%d %d %llu ",
2018 entry->pid, iter->cpu, iter->ts))
2022 event = ftrace_find_event(entry->type);
2024 return event->funcs->raw(iter, 0, event);
2026 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2029 return TRACE_TYPE_HANDLED;
2031 return TRACE_TYPE_PARTIAL_LINE;
2034 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2036 struct trace_seq *s = &iter->seq;
2037 unsigned char newline = '\n';
2038 struct trace_entry *entry;
2039 struct trace_event *event;
2043 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2044 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2045 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2046 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2049 event = ftrace_find_event(entry->type);
2051 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2052 if (ret != TRACE_TYPE_HANDLED)
2056 SEQ_PUT_FIELD_RET(s, newline);
2058 return TRACE_TYPE_HANDLED;
2061 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2063 struct trace_seq *s = &iter->seq;
2064 struct trace_entry *entry;
2065 struct trace_event *event;
2069 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2070 SEQ_PUT_FIELD_RET(s, entry->pid);
2071 SEQ_PUT_FIELD_RET(s, iter->cpu);
2072 SEQ_PUT_FIELD_RET(s, iter->ts);
2075 event = ftrace_find_event(entry->type);
2076 return event ? event->funcs->binary(iter, 0, event) :
2080 int trace_empty(struct trace_iterator *iter)
2084 /* If we are looking at one CPU buffer, only check that one */
2085 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2086 cpu = iter->cpu_file;
2087 if (iter->buffer_iter[cpu]) {
2088 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2091 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2097 for_each_tracing_cpu(cpu) {
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))
2110 /* Called with trace_event_read_lock() held. */
2111 enum print_line_t print_trace_line(struct trace_iterator *iter)
2113 enum print_line_t ret;
2115 if (iter->lost_events &&
2116 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2117 iter->cpu, iter->lost_events))
2118 return TRACE_TYPE_PARTIAL_LINE;
2120 if (iter->trace && iter->trace->print_line) {
2121 ret = iter->trace->print_line(iter);
2122 if (ret != TRACE_TYPE_UNHANDLED)
2126 if (iter->ent->type == TRACE_BPRINT &&
2127 trace_flags & TRACE_ITER_PRINTK &&
2128 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2129 return trace_print_bprintk_msg_only(iter);
2131 if (iter->ent->type == TRACE_PRINT &&
2132 trace_flags & TRACE_ITER_PRINTK &&
2133 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2134 return trace_print_printk_msg_only(iter);
2136 if (trace_flags & TRACE_ITER_BIN)
2137 return print_bin_fmt(iter);
2139 if (trace_flags & TRACE_ITER_HEX)
2140 return print_hex_fmt(iter);
2142 if (trace_flags & TRACE_ITER_RAW)
2143 return print_raw_fmt(iter);
2145 return print_trace_fmt(iter);
2148 void trace_default_header(struct seq_file *m)
2150 struct trace_iterator *iter = m->private;
2152 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2155 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2156 /* print nothing if the buffers are empty */
2157 if (trace_empty(iter))
2159 print_trace_header(m, iter);
2160 if (!(trace_flags & TRACE_ITER_VERBOSE))
2161 print_lat_help_header(m);
2163 if (!(trace_flags & TRACE_ITER_VERBOSE))
2164 print_func_help_header(m);
2168 static void test_ftrace_alive(struct seq_file *m)
2170 if (!ftrace_is_dead())
2172 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2173 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2176 static int s_show(struct seq_file *m, void *v)
2178 struct trace_iterator *iter = v;
2181 if (iter->ent == NULL) {
2183 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2185 test_ftrace_alive(m);
2187 if (iter->trace && iter->trace->print_header)
2188 iter->trace->print_header(m);
2190 trace_default_header(m);
2192 } else if (iter->leftover) {
2194 * If we filled the seq_file buffer earlier, we
2195 * want to just show it now.
2197 ret = trace_print_seq(m, &iter->seq);
2199 /* ret should this time be zero, but you never know */
2200 iter->leftover = ret;
2203 print_trace_line(iter);
2204 ret = trace_print_seq(m, &iter->seq);
2206 * If we overflow the seq_file buffer, then it will
2207 * ask us for this data again at start up.
2209 * ret is 0 if seq_file write succeeded.
2212 iter->leftover = ret;
2218 static const struct seq_operations tracer_seq_ops = {
2225 static struct trace_iterator *
2226 __tracing_open(struct inode *inode, struct file *file)
2228 long cpu_file = (long) inode->i_private;
2229 void *fail_ret = ERR_PTR(-ENOMEM);
2230 struct trace_iterator *iter;
2234 if (tracing_disabled)
2235 return ERR_PTR(-ENODEV);
2237 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2239 return ERR_PTR(-ENOMEM);
2242 * We make a copy of the current tracer to avoid concurrent
2243 * changes on it while we are reading.
2245 mutex_lock(&trace_types_lock);
2246 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2251 *iter->trace = *current_trace;
2253 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2256 if (current_trace && current_trace->print_max)
2259 iter->tr = &global_trace;
2261 mutex_init(&iter->mutex);
2262 iter->cpu_file = cpu_file;
2264 /* Notify the tracer early; before we stop tracing. */
2265 if (iter->trace && iter->trace->open)
2266 iter->trace->open(iter);
2268 /* Annotate start of buffers if we had overruns */
2269 if (ring_buffer_overruns(iter->tr->buffer))
2270 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2272 /* stop the trace while dumping */
2275 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2276 for_each_tracing_cpu(cpu) {
2277 iter->buffer_iter[cpu] =
2278 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2280 ring_buffer_read_prepare_sync();
2281 for_each_tracing_cpu(cpu) {
2282 ring_buffer_read_start(iter->buffer_iter[cpu]);
2283 tracing_iter_reset(iter, cpu);
2286 cpu = iter->cpu_file;
2287 iter->buffer_iter[cpu] =
2288 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2289 ring_buffer_read_prepare_sync();
2290 ring_buffer_read_start(iter->buffer_iter[cpu]);
2291 tracing_iter_reset(iter, cpu);
2294 ret = seq_open(file, &tracer_seq_ops);
2296 fail_ret = ERR_PTR(ret);
2300 m = file->private_data;
2303 mutex_unlock(&trace_types_lock);
2308 for_each_tracing_cpu(cpu) {
2309 if (iter->buffer_iter[cpu])
2310 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2312 free_cpumask_var(iter->started);
2315 mutex_unlock(&trace_types_lock);
2322 int tracing_open_generic(struct inode *inode, struct file *filp)
2324 if (tracing_disabled)
2327 filp->private_data = inode->i_private;
2331 static int tracing_release(struct inode *inode, struct file *file)
2333 struct seq_file *m = file->private_data;
2334 struct trace_iterator *iter;
2337 if (!(file->f_mode & FMODE_READ))
2342 mutex_lock(&trace_types_lock);
2343 for_each_tracing_cpu(cpu) {
2344 if (iter->buffer_iter[cpu])
2345 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2348 if (iter->trace && iter->trace->close)
2349 iter->trace->close(iter);
2351 /* reenable tracing if it was previously enabled */
2353 mutex_unlock(&trace_types_lock);
2355 seq_release(inode, file);
2356 mutex_destroy(&iter->mutex);
2357 free_cpumask_var(iter->started);
2363 static int tracing_open(struct inode *inode, struct file *file)
2365 struct trace_iterator *iter;
2368 /* If this file was open for write, then erase contents */
2369 if ((file->f_mode & FMODE_WRITE) &&
2370 (file->f_flags & O_TRUNC)) {
2371 long cpu = (long) inode->i_private;
2373 if (cpu == TRACE_PIPE_ALL_CPU)
2374 tracing_reset_online_cpus(&global_trace);
2376 tracing_reset(&global_trace, cpu);
2379 if (file->f_mode & FMODE_READ) {
2380 iter = __tracing_open(inode, file);
2382 ret = PTR_ERR(iter);
2383 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2384 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2390 t_next(struct seq_file *m, void *v, loff_t *pos)
2392 struct tracer *t = v;
2402 static void *t_start(struct seq_file *m, loff_t *pos)
2407 mutex_lock(&trace_types_lock);
2408 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2414 static void t_stop(struct seq_file *m, void *p)
2416 mutex_unlock(&trace_types_lock);
2419 static int t_show(struct seq_file *m, void *v)
2421 struct tracer *t = v;
2426 seq_printf(m, "%s", t->name);
2435 static const struct seq_operations show_traces_seq_ops = {
2442 static int show_traces_open(struct inode *inode, struct file *file)
2444 if (tracing_disabled)
2447 return seq_open(file, &show_traces_seq_ops);
2451 tracing_write_stub(struct file *filp, const char __user *ubuf,
2452 size_t count, loff_t *ppos)
2457 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2459 if (file->f_mode & FMODE_READ)
2460 return seq_lseek(file, offset, origin);
2465 static const struct file_operations tracing_fops = {
2466 .open = tracing_open,
2468 .write = tracing_write_stub,
2469 .llseek = tracing_seek,
2470 .release = tracing_release,
2473 static const struct file_operations show_traces_fops = {
2474 .open = show_traces_open,
2476 .release = seq_release,
2477 .llseek = seq_lseek,
2481 * Only trace on a CPU if the bitmask is set:
2483 static cpumask_var_t tracing_cpumask;
2486 * The tracer itself will not take this lock, but still we want
2487 * to provide a consistent cpumask to user-space:
2489 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2492 * Temporary storage for the character representation of the
2493 * CPU bitmask (and one more byte for the newline):
2495 static char mask_str[NR_CPUS + 1];
2498 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2499 size_t count, loff_t *ppos)
2503 mutex_lock(&tracing_cpumask_update_lock);
2505 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2506 if (count - len < 2) {
2510 len += sprintf(mask_str + len, "\n");
2511 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2514 mutex_unlock(&tracing_cpumask_update_lock);
2520 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2521 size_t count, loff_t *ppos)
2524 cpumask_var_t tracing_cpumask_new;
2526 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2529 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2533 mutex_lock(&tracing_cpumask_update_lock);
2535 local_irq_disable();
2536 arch_spin_lock(&ftrace_max_lock);
2537 for_each_tracing_cpu(cpu) {
2539 * Increase/decrease the disabled counter if we are
2540 * about to flip a bit in the cpumask:
2542 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2543 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2544 atomic_inc(&global_trace.data[cpu]->disabled);
2545 ring_buffer_record_disable_cpu(global_trace.buffer, cpu);
2547 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2548 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2549 atomic_dec(&global_trace.data[cpu]->disabled);
2550 ring_buffer_record_enable_cpu(global_trace.buffer, cpu);
2553 arch_spin_unlock(&ftrace_max_lock);
2556 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2558 mutex_unlock(&tracing_cpumask_update_lock);
2559 free_cpumask_var(tracing_cpumask_new);
2564 free_cpumask_var(tracing_cpumask_new);
2569 static const struct file_operations tracing_cpumask_fops = {
2570 .open = tracing_open_generic,
2571 .read = tracing_cpumask_read,
2572 .write = tracing_cpumask_write,
2573 .llseek = generic_file_llseek,
2576 static int tracing_trace_options_show(struct seq_file *m, void *v)
2578 struct tracer_opt *trace_opts;
2582 mutex_lock(&trace_types_lock);
2583 tracer_flags = current_trace->flags->val;
2584 trace_opts = current_trace->flags->opts;
2586 for (i = 0; trace_options[i]; i++) {
2587 if (trace_flags & (1 << i))
2588 seq_printf(m, "%s\n", trace_options[i]);
2590 seq_printf(m, "no%s\n", trace_options[i]);
2593 for (i = 0; trace_opts[i].name; i++) {
2594 if (tracer_flags & trace_opts[i].bit)
2595 seq_printf(m, "%s\n", trace_opts[i].name);
2597 seq_printf(m, "no%s\n", trace_opts[i].name);
2599 mutex_unlock(&trace_types_lock);
2604 static int __set_tracer_option(struct tracer *trace,
2605 struct tracer_flags *tracer_flags,
2606 struct tracer_opt *opts, int neg)
2610 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2615 tracer_flags->val &= ~opts->bit;
2617 tracer_flags->val |= opts->bit;
2621 /* Try to assign a tracer specific option */
2622 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2624 struct tracer_flags *tracer_flags = trace->flags;
2625 struct tracer_opt *opts = NULL;
2628 for (i = 0; tracer_flags->opts[i].name; i++) {
2629 opts = &tracer_flags->opts[i];
2631 if (strcmp(cmp, opts->name) == 0)
2632 return __set_tracer_option(trace, trace->flags,
2639 static void set_tracer_flags(unsigned int mask, int enabled)
2641 /* do nothing if flag is already set */
2642 if (!!(trace_flags & mask) == !!enabled)
2646 trace_flags |= mask;
2648 trace_flags &= ~mask;
2650 if (mask == TRACE_ITER_RECORD_CMD)
2651 trace_event_enable_cmd_record(enabled);
2653 if (mask == TRACE_ITER_OVERWRITE)
2654 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2658 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2659 size_t cnt, loff_t *ppos)
2667 if (cnt >= sizeof(buf))
2670 if (copy_from_user(&buf, ubuf, cnt))
2674 cmp = strstrip(buf);
2676 if (strncmp(cmp, "no", 2) == 0) {
2681 mutex_lock(&trace_types_lock);
2683 for (i = 0; trace_options[i]; i++) {
2684 if (strcmp(cmp, trace_options[i]) == 0) {
2685 set_tracer_flags(1 << i, !neg);
2690 /* If no option could be set, test the specific tracer options */
2691 if (!trace_options[i])
2692 ret = set_tracer_option(current_trace, cmp, neg);
2694 mutex_unlock(&trace_types_lock);
2704 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2706 if (tracing_disabled)
2708 return single_open(file, tracing_trace_options_show, NULL);
2711 static const struct file_operations tracing_iter_fops = {
2712 .open = tracing_trace_options_open,
2714 .llseek = seq_lseek,
2715 .release = single_release,
2716 .write = tracing_trace_options_write,
2719 static const char readme_msg[] =
2720 "tracing mini-HOWTO:\n\n"
2721 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2722 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2723 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2724 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2726 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2727 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2729 "# cat /sys/kernel/debug/tracing/trace_options\n"
2730 "noprint-parent nosym-offset nosym-addr noverbose\n"
2731 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2732 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2733 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2734 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2738 tracing_readme_read(struct file *filp, char __user *ubuf,
2739 size_t cnt, loff_t *ppos)
2741 return simple_read_from_buffer(ubuf, cnt, ppos,
2742 readme_msg, strlen(readme_msg));
2745 static const struct file_operations tracing_readme_fops = {
2746 .open = tracing_open_generic,
2747 .read = tracing_readme_read,
2748 .llseek = generic_file_llseek,
2752 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2753 size_t cnt, loff_t *ppos)
2762 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2766 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2774 for (i = 0; i < SAVED_CMDLINES; i++) {
2777 pid = map_cmdline_to_pid[i];
2778 if (pid == -1 || pid == NO_CMDLINE_MAP)
2781 trace_find_cmdline(pid, buf_comm);
2782 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2787 len = simple_read_from_buffer(ubuf, cnt, ppos,
2796 static const struct file_operations tracing_saved_cmdlines_fops = {
2797 .open = tracing_open_generic,
2798 .read = tracing_saved_cmdlines_read,
2799 .llseek = generic_file_llseek,
2803 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2804 size_t cnt, loff_t *ppos)
2809 r = sprintf(buf, "%u\n", tracer_enabled);
2810 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2814 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2815 size_t cnt, loff_t *ppos)
2817 struct trace_array *tr = filp->private_data;
2821 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
2827 mutex_lock(&trace_types_lock);
2828 if (tracer_enabled ^ val) {
2830 /* Only need to warn if this is used to change the state */
2831 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2835 if (current_trace->start)
2836 current_trace->start(tr);
2841 if (current_trace->stop)
2842 current_trace->stop(tr);
2845 mutex_unlock(&trace_types_lock);
2853 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2854 size_t cnt, loff_t *ppos)
2856 char buf[MAX_TRACER_SIZE+2];
2859 mutex_lock(&trace_types_lock);
2861 r = sprintf(buf, "%s\n", current_trace->name);
2863 r = sprintf(buf, "\n");
2864 mutex_unlock(&trace_types_lock);
2866 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2869 int tracer_init(struct tracer *t, struct trace_array *tr)
2871 tracing_reset_online_cpus(tr);
2875 static int __tracing_resize_ring_buffer(unsigned long size)
2880 * If kernel or user changes the size of the ring buffer
2881 * we use the size that was given, and we can forget about
2882 * expanding it later.
2884 ring_buffer_expanded = 1;
2886 ret = ring_buffer_resize(global_trace.buffer, size);
2890 if (!current_trace->use_max_tr)
2893 ret = ring_buffer_resize(max_tr.buffer, size);
2897 r = ring_buffer_resize(global_trace.buffer,
2898 global_trace.entries);
2901 * AARGH! We are left with different
2902 * size max buffer!!!!
2903 * The max buffer is our "snapshot" buffer.
2904 * When a tracer needs a snapshot (one of the
2905 * latency tracers), it swaps the max buffer
2906 * with the saved snap shot. We succeeded to
2907 * update the size of the main buffer, but failed to
2908 * update the size of the max buffer. But when we tried
2909 * to reset the main buffer to the original size, we
2910 * failed there too. This is very unlikely to
2911 * happen, but if it does, warn and kill all
2915 tracing_disabled = 1;
2920 max_tr.entries = size;
2922 global_trace.entries = size;
2927 static ssize_t tracing_resize_ring_buffer(unsigned long size)
2929 int cpu, ret = size;
2931 mutex_lock(&trace_types_lock);
2935 /* disable all cpu buffers */
2936 for_each_tracing_cpu(cpu) {
2937 if (global_trace.data[cpu])
2938 atomic_inc(&global_trace.data[cpu]->disabled);
2939 if (max_tr.data[cpu])
2940 atomic_inc(&max_tr.data[cpu]->disabled);
2943 if (size != global_trace.entries)
2944 ret = __tracing_resize_ring_buffer(size);
2949 for_each_tracing_cpu(cpu) {
2950 if (global_trace.data[cpu])
2951 atomic_dec(&global_trace.data[cpu]->disabled);
2952 if (max_tr.data[cpu])
2953 atomic_dec(&max_tr.data[cpu]->disabled);
2957 mutex_unlock(&trace_types_lock);
2964 * tracing_update_buffers - used by tracing facility to expand ring buffers
2966 * To save on memory when the tracing is never used on a system with it
2967 * configured in. The ring buffers are set to a minimum size. But once
2968 * a user starts to use the tracing facility, then they need to grow
2969 * to their default size.
2971 * This function is to be called when a tracer is about to be used.
2973 int tracing_update_buffers(void)
2977 mutex_lock(&trace_types_lock);
2978 if (!ring_buffer_expanded)
2979 ret = __tracing_resize_ring_buffer(trace_buf_size);
2980 mutex_unlock(&trace_types_lock);
2985 struct trace_option_dentry;
2987 static struct trace_option_dentry *
2988 create_trace_option_files(struct tracer *tracer);
2991 destroy_trace_option_files(struct trace_option_dentry *topts);
2993 static int tracing_set_tracer(const char *buf)
2995 static struct trace_option_dentry *topts;
2996 struct trace_array *tr = &global_trace;
3000 mutex_lock(&trace_types_lock);
3002 if (!ring_buffer_expanded) {
3003 ret = __tracing_resize_ring_buffer(trace_buf_size);
3009 for (t = trace_types; t; t = t->next) {
3010 if (strcmp(t->name, buf) == 0)
3017 if (t == current_trace)
3020 trace_branch_disable();
3021 if (current_trace && current_trace->reset)
3022 current_trace->reset(tr);
3023 if (current_trace && current_trace->use_max_tr) {
3025 * We don't free the ring buffer. instead, resize it because
3026 * The max_tr ring buffer has some state (e.g. ring->clock) and
3027 * we want preserve it.
3029 ring_buffer_resize(max_tr.buffer, 1);
3032 destroy_trace_option_files(topts);
3036 topts = create_trace_option_files(current_trace);
3037 if (current_trace->use_max_tr) {
3038 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
3041 max_tr.entries = global_trace.entries;
3045 ret = tracer_init(t, tr);
3050 trace_branch_enable(tr);
3052 mutex_unlock(&trace_types_lock);
3058 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3059 size_t cnt, loff_t *ppos)
3061 char buf[MAX_TRACER_SIZE+1];
3068 if (cnt > MAX_TRACER_SIZE)
3069 cnt = MAX_TRACER_SIZE;
3071 if (copy_from_user(&buf, ubuf, cnt))
3076 /* strip ending whitespace. */
3077 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3080 err = tracing_set_tracer(buf);
3090 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3091 size_t cnt, loff_t *ppos)
3093 unsigned long *ptr = filp->private_data;
3097 r = snprintf(buf, sizeof(buf), "%ld\n",
3098 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3099 if (r > sizeof(buf))
3101 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3105 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3106 size_t cnt, loff_t *ppos)
3108 unsigned long *ptr = filp->private_data;
3112 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3121 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3123 long cpu_file = (long) inode->i_private;
3124 struct trace_iterator *iter;
3127 if (tracing_disabled)
3130 mutex_lock(&trace_types_lock);
3132 /* create a buffer to store the information to pass to userspace */
3133 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3140 * We make a copy of the current tracer to avoid concurrent
3141 * changes on it while we are reading.
3143 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3149 *iter->trace = *current_trace;
3151 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3156 /* trace pipe does not show start of buffer */
3157 cpumask_setall(iter->started);
3159 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3160 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3162 iter->cpu_file = cpu_file;
3163 iter->tr = &global_trace;
3164 mutex_init(&iter->mutex);
3165 filp->private_data = iter;
3167 if (iter->trace->pipe_open)
3168 iter->trace->pipe_open(iter);
3170 nonseekable_open(inode, filp);
3172 mutex_unlock(&trace_types_lock);
3178 mutex_unlock(&trace_types_lock);
3182 static int tracing_release_pipe(struct inode *inode, struct file *file)
3184 struct trace_iterator *iter = file->private_data;
3186 mutex_lock(&trace_types_lock);
3188 if (iter->trace->pipe_close)
3189 iter->trace->pipe_close(iter);
3191 mutex_unlock(&trace_types_lock);
3193 free_cpumask_var(iter->started);
3194 mutex_destroy(&iter->mutex);
3202 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3204 struct trace_iterator *iter = filp->private_data;
3206 if (trace_flags & TRACE_ITER_BLOCK) {
3208 * Always select as readable when in blocking mode
3210 return POLLIN | POLLRDNORM;
3212 if (!trace_empty(iter))
3213 return POLLIN | POLLRDNORM;
3214 poll_wait(filp, &trace_wait, poll_table);
3215 if (!trace_empty(iter))
3216 return POLLIN | POLLRDNORM;
3223 void default_wait_pipe(struct trace_iterator *iter)
3227 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3229 if (trace_empty(iter))
3232 finish_wait(&trace_wait, &wait);
3236 * This is a make-shift waitqueue.
3237 * A tracer might use this callback on some rare cases:
3239 * 1) the current tracer might hold the runqueue lock when it wakes up
3240 * a reader, hence a deadlock (sched, function, and function graph tracers)
3241 * 2) the function tracers, trace all functions, we don't want
3242 * the overhead of calling wake_up and friends
3243 * (and tracing them too)
3245 * Anyway, this is really very primitive wakeup.
3247 void poll_wait_pipe(struct trace_iterator *iter)
3249 set_current_state(TASK_INTERRUPTIBLE);
3250 /* sleep for 100 msecs, and try again. */
3251 schedule_timeout(HZ / 10);
3254 /* Must be called with trace_types_lock mutex held. */
3255 static int tracing_wait_pipe(struct file *filp)
3257 struct trace_iterator *iter = filp->private_data;
3259 while (trace_empty(iter)) {
3261 if ((filp->f_flags & O_NONBLOCK)) {
3265 mutex_unlock(&iter->mutex);
3267 iter->trace->wait_pipe(iter);
3269 mutex_lock(&iter->mutex);
3271 if (signal_pending(current))
3275 * We block until we read something and tracing is disabled.
3276 * We still block if tracing is disabled, but we have never
3277 * read anything. This allows a user to cat this file, and
3278 * then enable tracing. But after we have read something,
3279 * we give an EOF when tracing is again disabled.
3281 * iter->pos will be 0 if we haven't read anything.
3283 if (!tracer_enabled && iter->pos)
3294 tracing_read_pipe(struct file *filp, char __user *ubuf,
3295 size_t cnt, loff_t *ppos)
3297 struct trace_iterator *iter = filp->private_data;
3298 static struct tracer *old_tracer;
3301 /* return any leftover data */
3302 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3306 trace_seq_init(&iter->seq);
3308 /* copy the tracer to avoid using a global lock all around */
3309 mutex_lock(&trace_types_lock);
3310 if (unlikely(old_tracer != current_trace && current_trace)) {
3311 old_tracer = current_trace;
3312 *iter->trace = *current_trace;
3314 mutex_unlock(&trace_types_lock);
3317 * Avoid more than one consumer on a single file descriptor
3318 * This is just a matter of traces coherency, the ring buffer itself
3321 mutex_lock(&iter->mutex);
3322 if (iter->trace->read) {
3323 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3329 sret = tracing_wait_pipe(filp);
3333 /* stop when tracing is finished */
3334 if (trace_empty(iter)) {
3339 if (cnt >= PAGE_SIZE)
3340 cnt = PAGE_SIZE - 1;
3342 /* reset all but tr, trace, and overruns */
3343 memset(&iter->seq, 0,
3344 sizeof(struct trace_iterator) -
3345 offsetof(struct trace_iterator, seq));
3348 trace_event_read_lock();
3349 trace_access_lock(iter->cpu_file);
3350 while (trace_find_next_entry_inc(iter) != NULL) {
3351 enum print_line_t ret;
3352 int len = iter->seq.len;
3354 ret = print_trace_line(iter);
3355 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3356 /* don't print partial lines */
3357 iter->seq.len = len;
3360 if (ret != TRACE_TYPE_NO_CONSUME)
3361 trace_consume(iter);
3363 if (iter->seq.len >= cnt)
3367 * Setting the full flag means we reached the trace_seq buffer
3368 * size and we should leave by partial output condition above.
3369 * One of the trace_seq_* functions is not used properly.
3371 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3374 trace_access_unlock(iter->cpu_file);
3375 trace_event_read_unlock();
3377 /* Now copy what we have to the user */
3378 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3379 if (iter->seq.readpos >= iter->seq.len)
3380 trace_seq_init(&iter->seq);
3383 * If there was nothing to send to user, in spite of consuming trace
3384 * entries, go back to wait for more entries.
3390 mutex_unlock(&iter->mutex);
3395 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3396 struct pipe_buffer *buf)
3398 __free_page(buf->page);
3401 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3404 __free_page(spd->pages[idx]);
3407 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3409 .map = generic_pipe_buf_map,
3410 .unmap = generic_pipe_buf_unmap,
3411 .confirm = generic_pipe_buf_confirm,
3412 .release = tracing_pipe_buf_release,
3413 .steal = generic_pipe_buf_steal,
3414 .get = generic_pipe_buf_get,
3418 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3423 /* Seq buffer is page-sized, exactly what we need. */
3425 count = iter->seq.len;
3426 ret = print_trace_line(iter);
3427 count = iter->seq.len - count;
3430 iter->seq.len -= count;
3433 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3434 iter->seq.len -= count;
3438 if (ret != TRACE_TYPE_NO_CONSUME)
3439 trace_consume(iter);
3441 if (!trace_find_next_entry_inc(iter)) {
3451 static ssize_t tracing_splice_read_pipe(struct file *filp,
3453 struct pipe_inode_info *pipe,
3457 struct page *pages_def[PIPE_DEF_BUFFERS];
3458 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3459 struct trace_iterator *iter = filp->private_data;
3460 struct splice_pipe_desc spd = {
3462 .partial = partial_def,
3463 .nr_pages = 0, /* This gets updated below. */
3464 .nr_pages_max = PIPE_DEF_BUFFERS,
3466 .ops = &tracing_pipe_buf_ops,
3467 .spd_release = tracing_spd_release_pipe,
3469 static struct tracer *old_tracer;
3474 if (splice_grow_spd(pipe, &spd))
3477 /* copy the tracer to avoid using a global lock all around */
3478 mutex_lock(&trace_types_lock);
3479 if (unlikely(old_tracer != current_trace && current_trace)) {
3480 old_tracer = current_trace;
3481 *iter->trace = *current_trace;
3483 mutex_unlock(&trace_types_lock);
3485 mutex_lock(&iter->mutex);
3487 if (iter->trace->splice_read) {
3488 ret = iter->trace->splice_read(iter, filp,
3489 ppos, pipe, len, flags);
3494 ret = tracing_wait_pipe(filp);
3498 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3503 trace_event_read_lock();
3504 trace_access_lock(iter->cpu_file);
3506 /* Fill as many pages as possible. */
3507 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3508 spd.pages[i] = alloc_page(GFP_KERNEL);
3512 rem = tracing_fill_pipe_page(rem, iter);
3514 /* Copy the data into the page, so we can start over. */
3515 ret = trace_seq_to_buffer(&iter->seq,
3516 page_address(spd.pages[i]),
3519 __free_page(spd.pages[i]);
3522 spd.partial[i].offset = 0;
3523 spd.partial[i].len = iter->seq.len;
3525 trace_seq_init(&iter->seq);
3528 trace_access_unlock(iter->cpu_file);
3529 trace_event_read_unlock();
3530 mutex_unlock(&iter->mutex);
3534 ret = splice_to_pipe(pipe, &spd);
3536 splice_shrink_spd(&spd);
3540 mutex_unlock(&iter->mutex);
3545 tracing_entries_read(struct file *filp, char __user *ubuf,
3546 size_t cnt, loff_t *ppos)
3548 struct trace_array *tr = filp->private_data;
3552 mutex_lock(&trace_types_lock);
3553 if (!ring_buffer_expanded)
3554 r = sprintf(buf, "%lu (expanded: %lu)\n",
3556 trace_buf_size >> 10);
3558 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3559 mutex_unlock(&trace_types_lock);
3561 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3565 tracing_entries_write(struct file *filp, const char __user *ubuf,
3566 size_t cnt, loff_t *ppos)
3571 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3575 /* must have at least 1 entry */
3579 /* value is in KB */
3582 ret = tracing_resize_ring_buffer(val);
3592 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3593 size_t cnt, loff_t *ppos)
3595 struct trace_array *tr = filp->private_data;
3598 unsigned long size = 0, expanded_size = 0;
3600 mutex_lock(&trace_types_lock);
3601 for_each_tracing_cpu(cpu) {
3602 size += tr->entries >> 10;
3603 if (!ring_buffer_expanded)
3604 expanded_size += trace_buf_size >> 10;
3606 if (ring_buffer_expanded)
3607 r = sprintf(buf, "%lu\n", size);
3609 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3610 mutex_unlock(&trace_types_lock);
3612 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3616 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3617 size_t cnt, loff_t *ppos)
3620 * There is no need to read what the user has written, this function
3621 * is just to make sure that there is no error when "echo" is used
3630 tracing_free_buffer_release(struct inode *inode, struct file *filp)
3632 /* disable tracing ? */
3633 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3635 /* resize the ring buffer to 0 */
3636 tracing_resize_ring_buffer(0);
3642 tracing_mark_write(struct file *filp, const char __user *ubuf,
3643 size_t cnt, loff_t *fpos)
3645 unsigned long addr = (unsigned long)ubuf;
3646 struct ring_buffer_event *event;
3647 struct ring_buffer *buffer;
3648 struct print_entry *entry;
3649 unsigned long irq_flags;
3650 struct page *pages[2];
3660 if (tracing_disabled)
3663 if (cnt > TRACE_BUF_SIZE)
3664 cnt = TRACE_BUF_SIZE;
3667 * Userspace is injecting traces into the kernel trace buffer.
3668 * We want to be as non intrusive as possible.
3669 * To do so, we do not want to allocate any special buffers
3670 * or take any locks, but instead write the userspace data
3671 * straight into the ring buffer.
3673 * First we need to pin the userspace buffer into memory,
3674 * which, most likely it is, because it just referenced it.
3675 * But there's no guarantee that it is. By using get_user_pages_fast()
3676 * and kmap_atomic/kunmap_atomic() we can get access to the
3677 * pages directly. We then write the data directly into the
3680 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
3682 /* check if we cross pages */
3683 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
3686 offset = addr & (PAGE_SIZE - 1);
3689 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
3690 if (ret < nr_pages) {
3692 put_page(pages[ret]);
3697 page1 = kmap_atomic(pages[0]);
3699 page2 = kmap_atomic(pages[1]);
3701 local_save_flags(irq_flags);
3702 size = sizeof(*entry) + cnt + 2; /* possible \n added */
3703 buffer = global_trace.buffer;
3704 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3705 irq_flags, preempt_count());
3707 /* Ring buffer disabled, return as if not open for write */
3712 entry = ring_buffer_event_data(event);
3713 entry->ip = _THIS_IP_;
3715 if (nr_pages == 2) {
3716 len = PAGE_SIZE - offset;
3717 memcpy(&entry->buf, page1 + offset, len);
3718 memcpy(&entry->buf[len], page2, cnt - len);
3720 memcpy(&entry->buf, page1 + offset, cnt);
3722 if (entry->buf[cnt - 1] != '\n') {
3723 entry->buf[cnt] = '\n';
3724 entry->buf[cnt + 1] = '\0';
3726 entry->buf[cnt] = '\0';
3728 ring_buffer_unlock_commit(buffer, event);
3736 kunmap_atomic(page2);
3737 kunmap_atomic(page1);
3738 while (nr_pages > 0)
3739 put_page(pages[--nr_pages]);
3744 static int tracing_clock_show(struct seq_file *m, void *v)
3748 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3750 "%s%s%s%s", i ? " " : "",
3751 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3752 i == trace_clock_id ? "]" : "");
3758 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3759 size_t cnt, loff_t *fpos)
3762 const char *clockstr;
3765 if (cnt >= sizeof(buf))
3768 if (copy_from_user(&buf, ubuf, cnt))
3773 clockstr = strstrip(buf);
3775 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3776 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3779 if (i == ARRAY_SIZE(trace_clocks))
3784 mutex_lock(&trace_types_lock);
3786 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3788 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3790 mutex_unlock(&trace_types_lock);
3797 static int tracing_clock_open(struct inode *inode, struct file *file)
3799 if (tracing_disabled)
3801 return single_open(file, tracing_clock_show, NULL);
3804 static const struct file_operations tracing_max_lat_fops = {
3805 .open = tracing_open_generic,
3806 .read = tracing_max_lat_read,
3807 .write = tracing_max_lat_write,
3808 .llseek = generic_file_llseek,
3811 static const struct file_operations tracing_ctrl_fops = {
3812 .open = tracing_open_generic,
3813 .read = tracing_ctrl_read,
3814 .write = tracing_ctrl_write,
3815 .llseek = generic_file_llseek,
3818 static const struct file_operations set_tracer_fops = {
3819 .open = tracing_open_generic,
3820 .read = tracing_set_trace_read,
3821 .write = tracing_set_trace_write,
3822 .llseek = generic_file_llseek,
3825 static const struct file_operations tracing_pipe_fops = {
3826 .open = tracing_open_pipe,
3827 .poll = tracing_poll_pipe,
3828 .read = tracing_read_pipe,
3829 .splice_read = tracing_splice_read_pipe,
3830 .release = tracing_release_pipe,
3831 .llseek = no_llseek,
3834 static const struct file_operations tracing_entries_fops = {
3835 .open = tracing_open_generic,
3836 .read = tracing_entries_read,
3837 .write = tracing_entries_write,
3838 .llseek = generic_file_llseek,
3841 static const struct file_operations tracing_total_entries_fops = {
3842 .open = tracing_open_generic,
3843 .read = tracing_total_entries_read,
3844 .llseek = generic_file_llseek,
3847 static const struct file_operations tracing_free_buffer_fops = {
3848 .write = tracing_free_buffer_write,
3849 .release = tracing_free_buffer_release,
3852 static const struct file_operations tracing_mark_fops = {
3853 .open = tracing_open_generic,
3854 .write = tracing_mark_write,
3855 .llseek = generic_file_llseek,
3858 static const struct file_operations trace_clock_fops = {
3859 .open = tracing_clock_open,
3861 .llseek = seq_lseek,
3862 .release = single_release,
3863 .write = tracing_clock_write,
3866 struct ftrace_buffer_info {
3867 struct trace_array *tr;
3873 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3875 int cpu = (int)(long)inode->i_private;
3876 struct ftrace_buffer_info *info;
3878 if (tracing_disabled)
3881 info = kzalloc(sizeof(*info), GFP_KERNEL);
3885 info->tr = &global_trace;
3888 /* Force reading ring buffer for first read */
3889 info->read = (unsigned int)-1;
3891 filp->private_data = info;
3893 return nonseekable_open(inode, filp);
3897 tracing_buffers_read(struct file *filp, char __user *ubuf,
3898 size_t count, loff_t *ppos)
3900 struct ftrace_buffer_info *info = filp->private_data;
3908 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
3912 /* Do we have previous read data to read? */
3913 if (info->read < PAGE_SIZE)
3916 trace_access_lock(info->cpu);
3917 ret = ring_buffer_read_page(info->tr->buffer,
3921 trace_access_unlock(info->cpu);
3928 size = PAGE_SIZE - info->read;
3932 ret = copy_to_user(ubuf, info->spare + info->read, size);
3943 static int tracing_buffers_release(struct inode *inode, struct file *file)
3945 struct ftrace_buffer_info *info = file->private_data;
3948 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3955 struct ring_buffer *buffer;
3960 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3961 struct pipe_buffer *buf)
3963 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3968 ring_buffer_free_read_page(ref->buffer, ref->page);
3973 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3974 struct pipe_buffer *buf)
3979 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3980 struct pipe_buffer *buf)
3982 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3987 /* Pipe buffer operations for a buffer. */
3988 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
3990 .map = generic_pipe_buf_map,
3991 .unmap = generic_pipe_buf_unmap,
3992 .confirm = generic_pipe_buf_confirm,
3993 .release = buffer_pipe_buf_release,
3994 .steal = buffer_pipe_buf_steal,
3995 .get = buffer_pipe_buf_get,
3999 * Callback from splice_to_pipe(), if we need to release some pages
4000 * at the end of the spd in case we error'ed out in filling the pipe.
4002 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4004 struct buffer_ref *ref =
4005 (struct buffer_ref *)spd->partial[i].private;
4010 ring_buffer_free_read_page(ref->buffer, ref->page);
4012 spd->partial[i].private = 0;
4016 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4017 struct pipe_inode_info *pipe, size_t len,
4020 struct ftrace_buffer_info *info = file->private_data;
4021 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4022 struct page *pages_def[PIPE_DEF_BUFFERS];
4023 struct splice_pipe_desc spd = {
4025 .partial = partial_def,
4026 .nr_pages_max = PIPE_DEF_BUFFERS,
4028 .ops = &buffer_pipe_buf_ops,
4029 .spd_release = buffer_spd_release,
4031 struct buffer_ref *ref;
4032 int entries, size, i;
4035 if (splice_grow_spd(pipe, &spd))
4038 if (*ppos & (PAGE_SIZE - 1)) {
4039 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
4044 if (len & (PAGE_SIZE - 1)) {
4045 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
4046 if (len < PAGE_SIZE) {
4053 trace_access_lock(info->cpu);
4054 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4056 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4060 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4065 ref->buffer = info->tr->buffer;
4066 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
4072 r = ring_buffer_read_page(ref->buffer, &ref->page,
4075 ring_buffer_free_read_page(ref->buffer, ref->page);
4081 * zero out any left over data, this is going to
4084 size = ring_buffer_page_len(ref->page);
4085 if (size < PAGE_SIZE)
4086 memset(ref->page + size, 0, PAGE_SIZE - size);
4088 page = virt_to_page(ref->page);
4090 spd.pages[i] = page;
4091 spd.partial[i].len = PAGE_SIZE;
4092 spd.partial[i].offset = 0;
4093 spd.partial[i].private = (unsigned long)ref;
4097 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4100 trace_access_unlock(info->cpu);
4103 /* did we read anything? */
4104 if (!spd.nr_pages) {
4105 if (flags & SPLICE_F_NONBLOCK)
4113 ret = splice_to_pipe(pipe, &spd);
4114 splice_shrink_spd(&spd);
4119 static const struct file_operations tracing_buffers_fops = {
4120 .open = tracing_buffers_open,
4121 .read = tracing_buffers_read,
4122 .release = tracing_buffers_release,
4123 .splice_read = tracing_buffers_splice_read,
4124 .llseek = no_llseek,
4128 tracing_stats_read(struct file *filp, char __user *ubuf,
4129 size_t count, loff_t *ppos)
4131 unsigned long cpu = (unsigned long)filp->private_data;
4132 struct trace_array *tr = &global_trace;
4133 struct trace_seq *s;
4135 unsigned long long t;
4136 unsigned long usec_rem;
4138 s = kmalloc(sizeof(*s), GFP_KERNEL);
4144 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4145 trace_seq_printf(s, "entries: %ld\n", cnt);
4147 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4148 trace_seq_printf(s, "overrun: %ld\n", cnt);
4150 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4151 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4153 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4154 trace_seq_printf(s, "bytes: %ld\n", cnt);
4156 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4157 usec_rem = do_div(t, USEC_PER_SEC);
4158 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
4160 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4161 usec_rem = do_div(t, USEC_PER_SEC);
4162 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4164 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4171 static const struct file_operations tracing_stats_fops = {
4172 .open = tracing_open_generic,
4173 .read = tracing_stats_read,
4174 .llseek = generic_file_llseek,
4177 #ifdef CONFIG_DYNAMIC_FTRACE
4179 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4185 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4186 size_t cnt, loff_t *ppos)
4188 static char ftrace_dyn_info_buffer[1024];
4189 static DEFINE_MUTEX(dyn_info_mutex);
4190 unsigned long *p = filp->private_data;
4191 char *buf = ftrace_dyn_info_buffer;
4192 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4195 mutex_lock(&dyn_info_mutex);
4196 r = sprintf(buf, "%ld ", *p);
4198 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4201 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4203 mutex_unlock(&dyn_info_mutex);
4208 static const struct file_operations tracing_dyn_info_fops = {
4209 .open = tracing_open_generic,
4210 .read = tracing_read_dyn_info,
4211 .llseek = generic_file_llseek,
4215 static struct dentry *d_tracer;
4217 struct dentry *tracing_init_dentry(void)
4224 if (!debugfs_initialized())
4227 d_tracer = debugfs_create_dir("tracing", NULL);
4229 if (!d_tracer && !once) {
4231 pr_warning("Could not create debugfs directory 'tracing'\n");
4238 static struct dentry *d_percpu;
4240 struct dentry *tracing_dentry_percpu(void)
4243 struct dentry *d_tracer;
4248 d_tracer = tracing_init_dentry();
4253 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4255 if (!d_percpu && !once) {
4257 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4264 static void tracing_init_debugfs_percpu(long cpu)
4266 struct dentry *d_percpu = tracing_dentry_percpu();
4267 struct dentry *d_cpu;
4268 char cpu_dir[30]; /* 30 characters should be more than enough */
4270 snprintf(cpu_dir, 30, "cpu%ld", cpu);
4271 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4273 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4277 /* per cpu trace_pipe */
4278 trace_create_file("trace_pipe", 0444, d_cpu,
4279 (void *) cpu, &tracing_pipe_fops);
4282 trace_create_file("trace", 0644, d_cpu,
4283 (void *) cpu, &tracing_fops);
4285 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4286 (void *) cpu, &tracing_buffers_fops);
4288 trace_create_file("stats", 0444, d_cpu,
4289 (void *) cpu, &tracing_stats_fops);
4292 #ifdef CONFIG_FTRACE_SELFTEST
4293 /* Let selftest have access to static functions in this file */
4294 #include "trace_selftest.c"
4297 struct trace_option_dentry {
4298 struct tracer_opt *opt;
4299 struct tracer_flags *flags;
4300 struct dentry *entry;
4304 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4307 struct trace_option_dentry *topt = filp->private_data;
4310 if (topt->flags->val & topt->opt->bit)
4315 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4319 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4322 struct trace_option_dentry *topt = filp->private_data;
4326 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4330 if (val != 0 && val != 1)
4333 if (!!(topt->flags->val & topt->opt->bit) != val) {
4334 mutex_lock(&trace_types_lock);
4335 ret = __set_tracer_option(current_trace, topt->flags,
4337 mutex_unlock(&trace_types_lock);
4348 static const struct file_operations trace_options_fops = {
4349 .open = tracing_open_generic,
4350 .read = trace_options_read,
4351 .write = trace_options_write,
4352 .llseek = generic_file_llseek,
4356 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4359 long index = (long)filp->private_data;
4362 if (trace_flags & (1 << index))
4367 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4371 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4374 long index = (long)filp->private_data;
4378 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4382 if (val != 0 && val != 1)
4385 mutex_lock(&trace_types_lock);
4386 set_tracer_flags(1 << index, val);
4387 mutex_unlock(&trace_types_lock);
4394 static const struct file_operations trace_options_core_fops = {
4395 .open = tracing_open_generic,
4396 .read = trace_options_core_read,
4397 .write = trace_options_core_write,
4398 .llseek = generic_file_llseek,
4401 struct dentry *trace_create_file(const char *name,
4403 struct dentry *parent,
4405 const struct file_operations *fops)
4409 ret = debugfs_create_file(name, mode, parent, data, fops);
4411 pr_warning("Could not create debugfs '%s' entry\n", name);
4417 static struct dentry *trace_options_init_dentry(void)
4419 struct dentry *d_tracer;
4420 static struct dentry *t_options;
4425 d_tracer = tracing_init_dentry();
4429 t_options = debugfs_create_dir("options", d_tracer);
4431 pr_warning("Could not create debugfs directory 'options'\n");
4439 create_trace_option_file(struct trace_option_dentry *topt,
4440 struct tracer_flags *flags,
4441 struct tracer_opt *opt)
4443 struct dentry *t_options;
4445 t_options = trace_options_init_dentry();
4449 topt->flags = flags;
4452 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4453 &trace_options_fops);
4457 static struct trace_option_dentry *
4458 create_trace_option_files(struct tracer *tracer)
4460 struct trace_option_dentry *topts;
4461 struct tracer_flags *flags;
4462 struct tracer_opt *opts;
4468 flags = tracer->flags;
4470 if (!flags || !flags->opts)
4475 for (cnt = 0; opts[cnt].name; cnt++)
4478 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4482 for (cnt = 0; opts[cnt].name; cnt++)
4483 create_trace_option_file(&topts[cnt], flags,
4490 destroy_trace_option_files(struct trace_option_dentry *topts)
4497 for (cnt = 0; topts[cnt].opt; cnt++) {
4498 if (topts[cnt].entry)
4499 debugfs_remove(topts[cnt].entry);
4505 static struct dentry *
4506 create_trace_option_core_file(const char *option, long index)
4508 struct dentry *t_options;
4510 t_options = trace_options_init_dentry();
4514 return trace_create_file(option, 0644, t_options, (void *)index,
4515 &trace_options_core_fops);
4518 static __init void create_trace_options_dir(void)
4520 struct dentry *t_options;
4523 t_options = trace_options_init_dentry();
4527 for (i = 0; trace_options[i]; i++)
4528 create_trace_option_core_file(trace_options[i], i);
4531 static __init int tracer_init_debugfs(void)
4533 struct dentry *d_tracer;
4536 trace_access_lock_init();
4538 d_tracer = tracing_init_dentry();
4540 trace_create_file("tracing_enabled", 0644, d_tracer,
4541 &global_trace, &tracing_ctrl_fops);
4543 trace_create_file("trace_options", 0644, d_tracer,
4544 NULL, &tracing_iter_fops);
4546 trace_create_file("tracing_cpumask", 0644, d_tracer,
4547 NULL, &tracing_cpumask_fops);
4549 trace_create_file("trace", 0644, d_tracer,
4550 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4552 trace_create_file("available_tracers", 0444, d_tracer,
4553 &global_trace, &show_traces_fops);
4555 trace_create_file("current_tracer", 0644, d_tracer,
4556 &global_trace, &set_tracer_fops);
4558 #ifdef CONFIG_TRACER_MAX_TRACE
4559 trace_create_file("tracing_max_latency", 0644, d_tracer,
4560 &tracing_max_latency, &tracing_max_lat_fops);
4563 trace_create_file("tracing_thresh", 0644, d_tracer,
4564 &tracing_thresh, &tracing_max_lat_fops);
4566 trace_create_file("README", 0444, d_tracer,
4567 NULL, &tracing_readme_fops);
4569 trace_create_file("trace_pipe", 0444, d_tracer,
4570 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4572 trace_create_file("buffer_size_kb", 0644, d_tracer,
4573 &global_trace, &tracing_entries_fops);
4575 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
4576 &global_trace, &tracing_total_entries_fops);
4578 trace_create_file("free_buffer", 0644, d_tracer,
4579 &global_trace, &tracing_free_buffer_fops);
4581 trace_create_file("trace_marker", 0220, d_tracer,
4582 NULL, &tracing_mark_fops);
4584 trace_create_file("saved_cmdlines", 0444, d_tracer,
4585 NULL, &tracing_saved_cmdlines_fops);
4587 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4590 #ifdef CONFIG_DYNAMIC_FTRACE
4591 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4592 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4595 create_trace_options_dir();
4597 for_each_tracing_cpu(cpu)
4598 tracing_init_debugfs_percpu(cpu);
4603 static int trace_panic_handler(struct notifier_block *this,
4604 unsigned long event, void *unused)
4606 if (ftrace_dump_on_oops)
4607 ftrace_dump(ftrace_dump_on_oops);
4611 static struct notifier_block trace_panic_notifier = {
4612 .notifier_call = trace_panic_handler,
4614 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4617 static int trace_die_handler(struct notifier_block *self,
4623 if (ftrace_dump_on_oops)
4624 ftrace_dump(ftrace_dump_on_oops);
4632 static struct notifier_block trace_die_notifier = {
4633 .notifier_call = trace_die_handler,
4638 * printk is set to max of 1024, we really don't need it that big.
4639 * Nothing should be printing 1000 characters anyway.
4641 #define TRACE_MAX_PRINT 1000
4644 * Define here KERN_TRACE so that we have one place to modify
4645 * it if we decide to change what log level the ftrace dump
4648 #define KERN_TRACE KERN_EMERG
4651 trace_printk_seq(struct trace_seq *s)
4653 /* Probably should print a warning here. */
4657 /* should be zero ended, but we are paranoid. */
4658 s->buffer[s->len] = 0;
4660 printk(KERN_TRACE "%s", s->buffer);
4665 void trace_init_global_iter(struct trace_iterator *iter)
4667 iter->tr = &global_trace;
4668 iter->trace = current_trace;
4669 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4673 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
4675 static arch_spinlock_t ftrace_dump_lock =
4676 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
4677 /* use static because iter can be a bit big for the stack */
4678 static struct trace_iterator iter;
4679 unsigned int old_userobj;
4680 static int dump_ran;
4681 unsigned long flags;
4685 local_irq_save(flags);
4686 arch_spin_lock(&ftrace_dump_lock);
4694 /* Did function tracer already get disabled? */
4695 if (ftrace_is_dead()) {
4696 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
4697 printk("# MAY BE MISSING FUNCTION EVENTS\n");
4700 if (disable_tracing)
4703 trace_init_global_iter(&iter);
4705 for_each_tracing_cpu(cpu) {
4706 atomic_inc(&iter.tr->data[cpu]->disabled);
4709 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4711 /* don't look at user memory in panic mode */
4712 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4714 /* Simulate the iterator */
4715 iter.tr = &global_trace;
4716 iter.trace = current_trace;
4718 switch (oops_dump_mode) {
4720 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4723 iter.cpu_file = raw_smp_processor_id();
4728 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4729 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4732 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4735 * We need to stop all tracing on all CPUS to read the
4736 * the next buffer. This is a bit expensive, but is
4737 * not done often. We fill all what we can read,
4738 * and then release the locks again.
4741 while (!trace_empty(&iter)) {
4744 printk(KERN_TRACE "---------------------------------\n");
4748 /* reset all but tr, trace, and overruns */
4749 memset(&iter.seq, 0,
4750 sizeof(struct trace_iterator) -
4751 offsetof(struct trace_iterator, seq));
4752 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4755 if (trace_find_next_entry_inc(&iter) != NULL) {
4758 ret = print_trace_line(&iter);
4759 if (ret != TRACE_TYPE_NO_CONSUME)
4760 trace_consume(&iter);
4763 trace_printk_seq(&iter.seq);
4767 printk(KERN_TRACE " (ftrace buffer empty)\n");
4769 printk(KERN_TRACE "---------------------------------\n");
4772 /* Re-enable tracing if requested */
4773 if (!disable_tracing) {
4774 trace_flags |= old_userobj;
4776 for_each_tracing_cpu(cpu) {
4777 atomic_dec(&iter.tr->data[cpu]->disabled);
4783 arch_spin_unlock(&ftrace_dump_lock);
4784 local_irq_restore(flags);
4787 /* By default: disable tracing after the dump */
4788 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
4790 __ftrace_dump(true, oops_dump_mode);
4793 __init static int tracer_alloc_buffers(void)
4796 enum ring_buffer_flags rb_flags;
4801 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4804 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4805 goto out_free_buffer_mask;
4807 /* To save memory, keep the ring buffer size to its minimum */
4808 if (ring_buffer_expanded)
4809 ring_buf_size = trace_buf_size;
4813 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
4815 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4816 cpumask_copy(tracing_cpumask, cpu_all_mask);
4818 /* TODO: make the number of buffers hot pluggable with CPUS */
4819 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
4820 if (!global_trace.buffer) {
4821 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4823 goto out_free_cpumask;
4825 global_trace.entries = ring_buffer_size(global_trace.buffer);
4828 #ifdef CONFIG_TRACER_MAX_TRACE
4829 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
4830 if (!max_tr.buffer) {
4831 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4833 ring_buffer_free(global_trace.buffer);
4834 goto out_free_cpumask;
4839 /* Allocate the first page for all buffers */
4840 for_each_tracing_cpu(i) {
4841 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4842 max_tr.data[i] = &per_cpu(max_tr_data, i);
4845 trace_init_cmdlines();
4847 register_tracer(&nop_trace);
4848 current_trace = &nop_trace;
4849 /* All seems OK, enable tracing */
4850 tracing_disabled = 0;
4852 atomic_notifier_chain_register(&panic_notifier_list,
4853 &trace_panic_notifier);
4855 register_die_notifier(&trace_die_notifier);
4860 free_cpumask_var(tracing_cpumask);
4861 out_free_buffer_mask:
4862 free_cpumask_var(tracing_buffer_mask);
4867 __init static int clear_boot_tracer(void)
4870 * The default tracer at boot buffer is an init section.
4871 * This function is called in lateinit. If we did not
4872 * find the boot tracer, then clear it out, to prevent
4873 * later registration from accessing the buffer that is
4874 * about to be freed.
4876 if (!default_bootup_tracer)
4879 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4880 default_bootup_tracer);
4881 default_bootup_tracer = NULL;
4886 early_initcall(tracer_alloc_buffers);
4887 fs_initcall(tracer_init_debugfs);
4888 late_initcall(clear_boot_tracer);