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>
39 #include <linux/nmi.h>
43 #include "trace_output.h"
46 * On boot up, the ring buffer is set to the minimum size, so that
47 * we do not waste memory on systems that are not using tracing.
49 int ring_buffer_expanded;
52 * We need to change this state when a selftest is running.
53 * A selftest will lurk into the ring-buffer to count the
54 * entries inserted during the selftest although some concurrent
55 * insertions into the ring-buffer such as trace_printk could occurred
56 * at the same time, giving false positive or negative results.
58 static bool __read_mostly tracing_selftest_running;
61 * If a tracer is running, we do not want to run SELFTEST.
63 bool __read_mostly tracing_selftest_disabled;
65 /* For tracers that don't implement custom flags */
66 static struct tracer_opt dummy_tracer_opt[] = {
70 static struct tracer_flags dummy_tracer_flags = {
72 .opts = dummy_tracer_opt
75 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
81 * To prevent the comm cache from being overwritten when no
82 * tracing is active, only save the comm when a trace event
85 static DEFINE_PER_CPU(bool, trace_cmdline_save);
88 * Kill all tracing for good (never come back).
89 * It is initialized to 1 but will turn to zero if the initialization
90 * of the tracer is successful. But that is the only place that sets
93 static int tracing_disabled = 1;
95 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
97 cpumask_var_t __read_mostly tracing_buffer_mask;
100 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
102 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
103 * is set, then ftrace_dump is called. This will output the contents
104 * of the ftrace buffers to the console. This is very useful for
105 * capturing traces that lead to crashes and outputing it to a
108 * It is default off, but you can enable it with either specifying
109 * "ftrace_dump_on_oops" in the kernel command line, or setting
110 * /proc/sys/kernel/ftrace_dump_on_oops
111 * Set 1 if you want to dump buffers of all CPUs
112 * Set 2 if you want to dump the buffer of the CPU that triggered oops
115 enum ftrace_dump_mode ftrace_dump_on_oops;
117 static int tracing_set_tracer(const char *buf);
119 #define MAX_TRACER_SIZE 100
120 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
121 static char *default_bootup_tracer;
123 static int __init set_cmdline_ftrace(char *str)
125 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
126 default_bootup_tracer = bootup_tracer_buf;
127 /* We are using ftrace early, expand it */
128 ring_buffer_expanded = 1;
131 __setup("ftrace=", set_cmdline_ftrace);
133 static int __init set_ftrace_dump_on_oops(char *str)
135 if (*str++ != '=' || !*str) {
136 ftrace_dump_on_oops = DUMP_ALL;
140 if (!strcmp("orig_cpu", str)) {
141 ftrace_dump_on_oops = DUMP_ORIG;
147 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
149 unsigned long long ns2usecs(cycle_t nsec)
157 * The global_trace is the descriptor that holds the tracing
158 * buffers for the live tracing. For each CPU, it contains
159 * a link list of pages that will store trace entries. The
160 * page descriptor of the pages in the memory is used to hold
161 * the link list by linking the lru item in the page descriptor
162 * to each of the pages in the buffer per CPU.
164 * For each active CPU there is a data field that holds the
165 * pages for the buffer for that CPU. Each CPU has the same number
166 * of pages allocated for its buffer.
168 static struct trace_array global_trace;
170 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
172 int filter_current_check_discard(struct ring_buffer *buffer,
173 struct ftrace_event_call *call, void *rec,
174 struct ring_buffer_event *event)
176 return filter_check_discard(call, rec, buffer, event);
178 EXPORT_SYMBOL_GPL(filter_current_check_discard);
180 cycle_t ftrace_now(int cpu)
184 /* Early boot up does not have a buffer yet */
185 if (!global_trace.buffer)
186 return trace_clock_local();
188 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
189 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
195 * The max_tr is used to snapshot the global_trace when a maximum
196 * latency is reached. Some tracers will use this to store a maximum
197 * trace while it continues examining live traces.
199 * The buffers for the max_tr are set up the same as the global_trace.
200 * When a snapshot is taken, the link list of the max_tr is swapped
201 * with the link list of the global_trace and the buffers are reset for
202 * the global_trace so the tracing can continue.
204 static struct trace_array max_tr;
206 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
208 int tracing_is_enabled(void)
210 return tracing_is_on();
214 * trace_buf_size is the size in bytes that is allocated
215 * for a buffer. Note, the number of bytes is always rounded
218 * This number is purposely set to a low number of 16384.
219 * If the dump on oops happens, it will be much appreciated
220 * to not have to wait for all that output. Anyway this can be
221 * boot time and run time configurable.
223 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
225 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
227 /* trace_types holds a link list of available tracers. */
228 static struct tracer *trace_types __read_mostly;
230 /* current_trace points to the tracer that is currently active */
231 static struct tracer *current_trace __read_mostly;
234 * trace_types_lock is used to protect the trace_types list.
236 static DEFINE_MUTEX(trace_types_lock);
239 * serialize the access of the ring buffer
241 * ring buffer serializes readers, but it is low level protection.
242 * The validity of the events (which returns by ring_buffer_peek() ..etc)
243 * are not protected by ring buffer.
245 * The content of events may become garbage if we allow other process consumes
246 * these events concurrently:
247 * A) the page of the consumed events may become a normal page
248 * (not reader page) in ring buffer, and this page will be rewrited
249 * by events producer.
250 * B) The page of the consumed events may become a page for splice_read,
251 * and this page will be returned to system.
253 * These primitives allow multi process access to different cpu ring buffer
256 * These primitives don't distinguish read-only and read-consume access.
257 * Multi read-only access are also serialized.
261 static DECLARE_RWSEM(all_cpu_access_lock);
262 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
264 static inline void trace_access_lock(int cpu)
266 if (cpu == TRACE_PIPE_ALL_CPU) {
267 /* gain it for accessing the whole ring buffer. */
268 down_write(&all_cpu_access_lock);
270 /* gain it for accessing a cpu ring buffer. */
272 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
273 down_read(&all_cpu_access_lock);
275 /* Secondly block other access to this @cpu ring buffer. */
276 mutex_lock(&per_cpu(cpu_access_lock, cpu));
280 static inline void trace_access_unlock(int cpu)
282 if (cpu == TRACE_PIPE_ALL_CPU) {
283 up_write(&all_cpu_access_lock);
285 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
286 up_read(&all_cpu_access_lock);
290 static inline void trace_access_lock_init(void)
294 for_each_possible_cpu(cpu)
295 mutex_init(&per_cpu(cpu_access_lock, cpu));
300 static DEFINE_MUTEX(access_lock);
302 static inline void trace_access_lock(int cpu)
305 mutex_lock(&access_lock);
308 static inline void trace_access_unlock(int cpu)
311 mutex_unlock(&access_lock);
314 static inline void trace_access_lock_init(void)
320 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
321 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
323 /* trace_flags holds trace_options default values */
324 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
325 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
326 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
327 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
329 static int trace_stop_count;
330 static DEFINE_RAW_SPINLOCK(tracing_start_lock);
332 static void wakeup_work_handler(struct work_struct *work)
334 wake_up(&trace_wait);
337 static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
340 * tracing_on - enable tracing buffers
342 * This function enables tracing buffers that may have been
343 * disabled with tracing_off.
345 void tracing_on(void)
347 if (global_trace.buffer)
348 ring_buffer_record_on(global_trace.buffer);
350 * This flag is only looked at when buffers haven't been
351 * allocated yet. We don't really care about the race
352 * between setting this flag and actually turning
355 global_trace.buffer_disabled = 0;
357 EXPORT_SYMBOL_GPL(tracing_on);
360 * tracing_off - turn off tracing buffers
362 * This function stops the tracing buffers from recording data.
363 * It does not disable any overhead the tracers themselves may
364 * be causing. This function simply causes all recording to
365 * the ring buffers to fail.
367 void tracing_off(void)
369 if (global_trace.buffer)
370 ring_buffer_record_off(global_trace.buffer);
372 * This flag is only looked at when buffers haven't been
373 * allocated yet. We don't really care about the race
374 * between setting this flag and actually turning
377 global_trace.buffer_disabled = 1;
379 EXPORT_SYMBOL_GPL(tracing_off);
382 * tracing_is_on - show state of ring buffers enabled
384 int tracing_is_on(void)
386 if (global_trace.buffer)
387 return ring_buffer_record_is_on(global_trace.buffer);
388 return !global_trace.buffer_disabled;
390 EXPORT_SYMBOL_GPL(tracing_is_on);
393 * trace_wake_up - wake up tasks waiting for trace input
395 * Schedules a delayed work to wake up any task that is blocked on the
396 * trace_wait queue. These is used with trace_poll for tasks polling the
399 void trace_wake_up(void)
401 const unsigned long delay = msecs_to_jiffies(2);
403 if (trace_flags & TRACE_ITER_BLOCK)
405 schedule_delayed_work(&wakeup_work, delay);
408 static int __init set_buf_size(char *str)
410 unsigned long buf_size;
414 buf_size = memparse(str, &str);
415 /* nr_entries can not be zero */
418 trace_buf_size = buf_size;
421 __setup("trace_buf_size=", set_buf_size);
423 static int __init set_tracing_thresh(char *str)
425 unsigned long threshold;
430 ret = kstrtoul(str, 0, &threshold);
433 tracing_thresh = threshold * 1000;
436 __setup("tracing_thresh=", set_tracing_thresh);
438 unsigned long nsecs_to_usecs(unsigned long nsecs)
443 /* These must match the bit postions in trace_iterator_flags */
444 static const char *trace_options[] = {
477 { trace_clock_local, "local" },
478 { trace_clock_global, "global" },
479 { trace_clock_counter, "counter" },
485 * trace_parser_get_init - gets the buffer for trace parser
487 int trace_parser_get_init(struct trace_parser *parser, int size)
489 memset(parser, 0, sizeof(*parser));
491 parser->buffer = kmalloc(size, GFP_KERNEL);
500 * trace_parser_put - frees the buffer for trace parser
502 void trace_parser_put(struct trace_parser *parser)
504 kfree(parser->buffer);
508 * trace_get_user - reads the user input string separated by space
509 * (matched by isspace(ch))
511 * For each string found the 'struct trace_parser' is updated,
512 * and the function returns.
514 * Returns number of bytes read.
516 * See kernel/trace/trace.h for 'struct trace_parser' details.
518 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
519 size_t cnt, loff_t *ppos)
526 trace_parser_clear(parser);
528 ret = get_user(ch, ubuf++);
536 * The parser is not finished with the last write,
537 * continue reading the user input without skipping spaces.
540 /* skip white space */
541 while (cnt && isspace(ch)) {
542 ret = get_user(ch, ubuf++);
549 /* only spaces were written */
559 /* read the non-space input */
560 while (cnt && !isspace(ch)) {
561 if (parser->idx < parser->size - 1)
562 parser->buffer[parser->idx++] = ch;
567 ret = get_user(ch, ubuf++);
574 /* We either got finished input or we have to wait for another call. */
576 parser->buffer[parser->idx] = 0;
577 parser->cont = false;
580 parser->buffer[parser->idx++] = ch;
590 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
598 if (s->len <= s->readpos)
601 len = s->len - s->readpos;
604 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
614 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
618 if (s->len <= s->readpos)
621 len = s->len - s->readpos;
624 memcpy(buf, s->buffer + s->readpos, cnt);
631 * ftrace_max_lock is used to protect the swapping of buffers
632 * when taking a max snapshot. The buffers themselves are
633 * protected by per_cpu spinlocks. But the action of the swap
634 * needs its own lock.
636 * This is defined as a arch_spinlock_t in order to help
637 * with performance when lockdep debugging is enabled.
639 * It is also used in other places outside the update_max_tr
640 * so it needs to be defined outside of the
641 * CONFIG_TRACER_MAX_TRACE.
643 static arch_spinlock_t ftrace_max_lock =
644 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
646 unsigned long __read_mostly tracing_thresh;
648 #ifdef CONFIG_TRACER_MAX_TRACE
649 unsigned long __read_mostly tracing_max_latency;
652 * Copy the new maximum trace into the separate maximum-trace
653 * structure. (this way the maximum trace is permanently saved,
654 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
657 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
659 struct trace_array_cpu *data = tr->data[cpu];
660 struct trace_array_cpu *max_data;
663 max_tr.time_start = data->preempt_timestamp;
665 max_data = max_tr.data[cpu];
666 max_data->saved_latency = tracing_max_latency;
667 max_data->critical_start = data->critical_start;
668 max_data->critical_end = data->critical_end;
670 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
671 max_data->pid = tsk->pid;
672 max_data->uid = task_uid(tsk);
673 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
674 max_data->policy = tsk->policy;
675 max_data->rt_priority = tsk->rt_priority;
677 /* record this tasks comm */
678 tracing_record_cmdline(tsk);
682 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
684 * @tsk: the task with the latency
685 * @cpu: The cpu that initiated the trace.
687 * Flip the buffers between the @tr and the max_tr and record information
688 * about which task was the cause of this latency.
691 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
693 struct ring_buffer *buf = tr->buffer;
695 if (trace_stop_count)
698 WARN_ON_ONCE(!irqs_disabled());
699 if (!current_trace->use_max_tr) {
703 arch_spin_lock(&ftrace_max_lock);
705 tr->buffer = max_tr.buffer;
708 __update_max_tr(tr, tsk, cpu);
709 arch_spin_unlock(&ftrace_max_lock);
713 * update_max_tr_single - only copy one trace over, and reset the rest
715 * @tsk - task with the latency
716 * @cpu - the cpu of the buffer to copy.
718 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
721 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
725 if (trace_stop_count)
728 WARN_ON_ONCE(!irqs_disabled());
729 if (!current_trace->use_max_tr) {
734 arch_spin_lock(&ftrace_max_lock);
736 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
740 * We failed to swap the buffer due to a commit taking
741 * place on this CPU. We fail to record, but we reset
742 * the max trace buffer (no one writes directly to it)
743 * and flag that it failed.
745 trace_array_printk(&max_tr, _THIS_IP_,
746 "Failed to swap buffers due to commit in progress\n");
749 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
751 __update_max_tr(tr, tsk, cpu);
752 arch_spin_unlock(&ftrace_max_lock);
754 #endif /* CONFIG_TRACER_MAX_TRACE */
757 * register_tracer - register a tracer with the ftrace system.
758 * @type - the plugin for the tracer
760 * Register a new plugin tracer.
762 int register_tracer(struct tracer *type)
768 pr_info("Tracer must have a name\n");
772 if (strlen(type->name) >= MAX_TRACER_SIZE) {
773 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
777 mutex_lock(&trace_types_lock);
779 tracing_selftest_running = true;
781 for (t = trace_types; t; t = t->next) {
782 if (strcmp(type->name, t->name) == 0) {
784 pr_info("Tracer %s already registered\n",
792 type->set_flag = &dummy_set_flag;
794 type->flags = &dummy_tracer_flags;
796 if (!type->flags->opts)
797 type->flags->opts = dummy_tracer_opt;
798 if (!type->wait_pipe)
799 type->wait_pipe = default_wait_pipe;
802 #ifdef CONFIG_FTRACE_STARTUP_TEST
803 if (type->selftest && !tracing_selftest_disabled) {
804 struct tracer *saved_tracer = current_trace;
805 struct trace_array *tr = &global_trace;
808 * Run a selftest on this tracer.
809 * Here we reset the trace buffer, and set the current
810 * tracer to be this tracer. The tracer can then run some
811 * internal tracing to verify that everything is in order.
812 * If we fail, we do not register this tracer.
814 tracing_reset_online_cpus(tr);
816 current_trace = type;
818 /* If we expanded the buffers, make sure the max is expanded too */
819 if (ring_buffer_expanded && type->use_max_tr)
820 ring_buffer_resize(max_tr.buffer, trace_buf_size,
821 RING_BUFFER_ALL_CPUS);
823 /* the test is responsible for initializing and enabling */
824 pr_info("Testing tracer %s: ", type->name);
825 ret = type->selftest(type, tr);
826 /* the test is responsible for resetting too */
827 current_trace = saved_tracer;
829 printk(KERN_CONT "FAILED!\n");
830 /* Add the warning after printing 'FAILED' */
834 /* Only reset on passing, to avoid touching corrupted buffers */
835 tracing_reset_online_cpus(tr);
837 /* Shrink the max buffer again */
838 if (ring_buffer_expanded && type->use_max_tr)
839 ring_buffer_resize(max_tr.buffer, 1,
840 RING_BUFFER_ALL_CPUS);
842 printk(KERN_CONT "PASSED\n");
846 type->next = trace_types;
850 tracing_selftest_running = false;
851 mutex_unlock(&trace_types_lock);
853 if (ret || !default_bootup_tracer)
856 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
859 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
860 /* Do we want this tracer to start on bootup? */
861 tracing_set_tracer(type->name);
862 default_bootup_tracer = NULL;
863 /* disable other selftests, since this will break it. */
864 tracing_selftest_disabled = 1;
865 #ifdef CONFIG_FTRACE_STARTUP_TEST
866 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
874 void tracing_reset(struct trace_array *tr, int cpu)
876 struct ring_buffer *buffer = tr->buffer;
878 ring_buffer_record_disable(buffer);
880 /* Make sure all commits have finished */
882 ring_buffer_reset_cpu(buffer, cpu);
884 ring_buffer_record_enable(buffer);
887 void tracing_reset_online_cpus(struct trace_array *tr)
889 struct ring_buffer *buffer = tr->buffer;
892 ring_buffer_record_disable(buffer);
894 /* Make sure all commits have finished */
897 tr->time_start = ftrace_now(tr->cpu);
899 for_each_online_cpu(cpu)
900 ring_buffer_reset_cpu(buffer, cpu);
902 ring_buffer_record_enable(buffer);
905 void tracing_reset_current(int cpu)
907 tracing_reset(&global_trace, cpu);
910 void tracing_reset_current_online_cpus(void)
912 tracing_reset_online_cpus(&global_trace);
915 #define SAVED_CMDLINES 128
916 #define NO_CMDLINE_MAP UINT_MAX
917 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
918 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
919 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
920 static int cmdline_idx;
921 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
923 /* temporary disable recording */
924 static atomic_t trace_record_cmdline_disabled __read_mostly;
926 static void trace_init_cmdlines(void)
928 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
929 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
933 int is_tracing_stopped(void)
935 return trace_stop_count;
939 * ftrace_off_permanent - disable all ftrace code permanently
941 * This should only be called when a serious anomally has
942 * been detected. This will turn off the function tracing,
943 * ring buffers, and other tracing utilites. It takes no
944 * locks and can be called from any context.
946 void ftrace_off_permanent(void)
948 tracing_disabled = 1;
950 tracing_off_permanent();
954 * tracing_start - quick start of the tracer
956 * If tracing is enabled but was stopped by tracing_stop,
957 * this will start the tracer back up.
959 void tracing_start(void)
961 struct ring_buffer *buffer;
964 if (tracing_disabled)
967 raw_spin_lock_irqsave(&tracing_start_lock, flags);
968 if (--trace_stop_count) {
969 if (trace_stop_count < 0) {
970 /* Someone screwed up their debugging */
972 trace_stop_count = 0;
977 /* Prevent the buffers from switching */
978 arch_spin_lock(&ftrace_max_lock);
980 buffer = global_trace.buffer;
982 ring_buffer_record_enable(buffer);
984 buffer = max_tr.buffer;
986 ring_buffer_record_enable(buffer);
988 arch_spin_unlock(&ftrace_max_lock);
992 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
996 * tracing_stop - quick stop of the tracer
998 * Light weight way to stop tracing. Use in conjunction with
1001 void tracing_stop(void)
1003 struct ring_buffer *buffer;
1004 unsigned long flags;
1007 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1008 if (trace_stop_count++)
1011 /* Prevent the buffers from switching */
1012 arch_spin_lock(&ftrace_max_lock);
1014 buffer = global_trace.buffer;
1016 ring_buffer_record_disable(buffer);
1018 buffer = max_tr.buffer;
1020 ring_buffer_record_disable(buffer);
1022 arch_spin_unlock(&ftrace_max_lock);
1025 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1028 void trace_stop_cmdline_recording(void);
1030 static void trace_save_cmdline(struct task_struct *tsk)
1034 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1038 * It's not the end of the world if we don't get
1039 * the lock, but we also don't want to spin
1040 * nor do we want to disable interrupts,
1041 * so if we miss here, then better luck next time.
1043 if (!arch_spin_trylock(&trace_cmdline_lock))
1046 idx = map_pid_to_cmdline[tsk->pid];
1047 if (idx == NO_CMDLINE_MAP) {
1048 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1051 * Check whether the cmdline buffer at idx has a pid
1052 * mapped. We are going to overwrite that entry so we
1053 * need to clear the map_pid_to_cmdline. Otherwise we
1054 * would read the new comm for the old pid.
1056 pid = map_cmdline_to_pid[idx];
1057 if (pid != NO_CMDLINE_MAP)
1058 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1060 map_cmdline_to_pid[idx] = tsk->pid;
1061 map_pid_to_cmdline[tsk->pid] = idx;
1066 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1068 arch_spin_unlock(&trace_cmdline_lock);
1071 void trace_find_cmdline(int pid, char comm[])
1076 strcpy(comm, "<idle>");
1080 if (WARN_ON_ONCE(pid < 0)) {
1081 strcpy(comm, "<XXX>");
1085 if (pid > PID_MAX_DEFAULT) {
1086 strcpy(comm, "<...>");
1091 arch_spin_lock(&trace_cmdline_lock);
1092 map = map_pid_to_cmdline[pid];
1093 if (map != NO_CMDLINE_MAP)
1094 strcpy(comm, saved_cmdlines[map]);
1096 strcpy(comm, "<...>");
1098 arch_spin_unlock(&trace_cmdline_lock);
1102 void tracing_record_cmdline(struct task_struct *tsk)
1104 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1107 if (!__this_cpu_read(trace_cmdline_save))
1110 __this_cpu_write(trace_cmdline_save, false);
1112 trace_save_cmdline(tsk);
1116 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1119 struct task_struct *tsk = current;
1121 entry->preempt_count = pc & 0xff;
1122 entry->pid = (tsk) ? tsk->pid : 0;
1125 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1126 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1128 TRACE_FLAG_IRQS_NOSUPPORT |
1130 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1131 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1132 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1134 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1136 struct ring_buffer_event *
1137 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1140 unsigned long flags, int pc)
1142 struct ring_buffer_event *event;
1144 event = ring_buffer_lock_reserve(buffer, len);
1145 if (event != NULL) {
1146 struct trace_entry *ent = ring_buffer_event_data(event);
1148 tracing_generic_entry_update(ent, flags, pc);
1156 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1158 __this_cpu_write(trace_cmdline_save, true);
1159 ring_buffer_unlock_commit(buffer, event);
1163 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1164 struct ring_buffer_event *event,
1165 unsigned long flags, int pc,
1168 __buffer_unlock_commit(buffer, event);
1170 ftrace_trace_stack(buffer, flags, 6, pc);
1171 ftrace_trace_userstack(buffer, flags, pc);
1177 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1178 struct ring_buffer_event *event,
1179 unsigned long flags, int pc)
1181 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1184 struct ring_buffer_event *
1185 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1186 int type, unsigned long len,
1187 unsigned long flags, int pc)
1189 *current_rb = global_trace.buffer;
1190 return trace_buffer_lock_reserve(*current_rb,
1191 type, len, flags, pc);
1193 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1195 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1196 struct ring_buffer_event *event,
1197 unsigned long flags, int pc)
1199 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1201 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1203 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1204 struct ring_buffer_event *event,
1205 unsigned long flags, int pc)
1207 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1209 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1211 void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1212 struct ring_buffer_event *event,
1213 unsigned long flags, int pc,
1214 struct pt_regs *regs)
1216 __buffer_unlock_commit(buffer, event);
1218 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1219 ftrace_trace_userstack(buffer, flags, pc);
1221 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1223 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1224 struct ring_buffer_event *event)
1226 ring_buffer_discard_commit(buffer, event);
1228 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1231 trace_function(struct trace_array *tr,
1232 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1235 struct ftrace_event_call *call = &event_function;
1236 struct ring_buffer *buffer = tr->buffer;
1237 struct ring_buffer_event *event;
1238 struct ftrace_entry *entry;
1240 /* If we are reading the ring buffer, don't trace */
1241 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1244 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1248 entry = ring_buffer_event_data(event);
1250 entry->parent_ip = parent_ip;
1252 if (!filter_check_discard(call, entry, buffer, event))
1253 __buffer_unlock_commit(buffer, event);
1257 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1258 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1261 if (likely(!atomic_read(&data->disabled)))
1262 trace_function(tr, ip, parent_ip, flags, pc);
1265 #ifdef CONFIG_STACKTRACE
1267 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1268 struct ftrace_stack {
1269 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1272 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1273 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1275 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1276 unsigned long flags,
1277 int skip, int pc, struct pt_regs *regs)
1279 struct ftrace_event_call *call = &event_kernel_stack;
1280 struct ring_buffer_event *event;
1281 struct stack_entry *entry;
1282 struct stack_trace trace;
1284 int size = FTRACE_STACK_ENTRIES;
1286 trace.nr_entries = 0;
1290 * Since events can happen in NMIs there's no safe way to
1291 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1292 * or NMI comes in, it will just have to use the default
1293 * FTRACE_STACK_SIZE.
1295 preempt_disable_notrace();
1297 use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1299 * We don't need any atomic variables, just a barrier.
1300 * If an interrupt comes in, we don't care, because it would
1301 * have exited and put the counter back to what we want.
1302 * We just need a barrier to keep gcc from moving things
1306 if (use_stack == 1) {
1307 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1308 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1311 save_stack_trace_regs(regs, &trace);
1313 save_stack_trace(&trace);
1315 if (trace.nr_entries > size)
1316 size = trace.nr_entries;
1318 /* From now on, use_stack is a boolean */
1321 size *= sizeof(unsigned long);
1323 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1324 sizeof(*entry) + size, flags, pc);
1327 entry = ring_buffer_event_data(event);
1329 memset(&entry->caller, 0, size);
1332 memcpy(&entry->caller, trace.entries,
1333 trace.nr_entries * sizeof(unsigned long));
1335 trace.max_entries = FTRACE_STACK_ENTRIES;
1336 trace.entries = entry->caller;
1338 save_stack_trace_regs(regs, &trace);
1340 save_stack_trace(&trace);
1343 entry->size = trace.nr_entries;
1345 if (!filter_check_discard(call, entry, buffer, event))
1346 __buffer_unlock_commit(buffer, event);
1349 /* Again, don't let gcc optimize things here */
1351 __get_cpu_var(ftrace_stack_reserve)--;
1352 preempt_enable_notrace();
1356 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1357 int skip, int pc, struct pt_regs *regs)
1359 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1362 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1365 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1368 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1371 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1374 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1377 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1381 * trace_dump_stack - record a stack back trace in the trace buffer
1383 void trace_dump_stack(void)
1385 unsigned long flags;
1387 if (tracing_disabled || tracing_selftest_running)
1390 local_save_flags(flags);
1392 /* skipping 3 traces, seems to get us at the caller of this function */
1393 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1396 static DEFINE_PER_CPU(int, user_stack_count);
1399 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1401 struct ftrace_event_call *call = &event_user_stack;
1402 struct ring_buffer_event *event;
1403 struct userstack_entry *entry;
1404 struct stack_trace trace;
1406 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1410 * NMIs can not handle page faults, even with fix ups.
1411 * The save user stack can (and often does) fault.
1413 if (unlikely(in_nmi()))
1417 * prevent recursion, since the user stack tracing may
1418 * trigger other kernel events.
1421 if (__this_cpu_read(user_stack_count))
1424 __this_cpu_inc(user_stack_count);
1426 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1427 sizeof(*entry), flags, pc);
1429 goto out_drop_count;
1430 entry = ring_buffer_event_data(event);
1432 entry->tgid = current->tgid;
1433 memset(&entry->caller, 0, sizeof(entry->caller));
1435 trace.nr_entries = 0;
1436 trace.max_entries = FTRACE_STACK_ENTRIES;
1438 trace.entries = entry->caller;
1440 save_stack_trace_user(&trace);
1441 if (!filter_check_discard(call, entry, buffer, event))
1442 __buffer_unlock_commit(buffer, event);
1445 __this_cpu_dec(user_stack_count);
1451 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1453 ftrace_trace_userstack(tr, flags, preempt_count());
1457 #endif /* CONFIG_STACKTRACE */
1459 /* created for use with alloc_percpu */
1460 struct trace_buffer_struct {
1461 char buffer[TRACE_BUF_SIZE];
1464 static struct trace_buffer_struct *trace_percpu_buffer;
1465 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1466 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1467 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1470 * The buffer used is dependent on the context. There is a per cpu
1471 * buffer for normal context, softirq contex, hard irq context and
1472 * for NMI context. Thise allows for lockless recording.
1474 * Note, if the buffers failed to be allocated, then this returns NULL
1476 static char *get_trace_buf(void)
1478 struct trace_buffer_struct *percpu_buffer;
1479 struct trace_buffer_struct *buffer;
1482 * If we have allocated per cpu buffers, then we do not
1483 * need to do any locking.
1486 percpu_buffer = trace_percpu_nmi_buffer;
1488 percpu_buffer = trace_percpu_irq_buffer;
1489 else if (in_softirq())
1490 percpu_buffer = trace_percpu_sirq_buffer;
1492 percpu_buffer = trace_percpu_buffer;
1497 buffer = per_cpu_ptr(percpu_buffer, smp_processor_id());
1499 return buffer->buffer;
1502 static int alloc_percpu_trace_buffer(void)
1504 struct trace_buffer_struct *buffers;
1505 struct trace_buffer_struct *sirq_buffers;
1506 struct trace_buffer_struct *irq_buffers;
1507 struct trace_buffer_struct *nmi_buffers;
1509 buffers = alloc_percpu(struct trace_buffer_struct);
1513 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1517 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1521 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1525 trace_percpu_buffer = buffers;
1526 trace_percpu_sirq_buffer = sirq_buffers;
1527 trace_percpu_irq_buffer = irq_buffers;
1528 trace_percpu_nmi_buffer = nmi_buffers;
1533 free_percpu(irq_buffers);
1535 free_percpu(sirq_buffers);
1537 free_percpu(buffers);
1539 WARN(1, "Could not allocate percpu trace_printk buffer");
1543 static int buffers_allocated;
1545 void trace_printk_init_buffers(void)
1547 if (buffers_allocated)
1550 if (alloc_percpu_trace_buffer())
1553 pr_info("ftrace: Allocated trace_printk buffers\n");
1555 /* Expand the buffers to set size */
1556 tracing_update_buffers();
1558 buffers_allocated = 1;
1561 * trace_printk_init_buffers() can be called by modules.
1562 * If that happens, then we need to start cmdline recording
1563 * directly here. If the global_trace.buffer is already
1564 * allocated here, then this was called by module code.
1566 if (global_trace.buffer)
1567 tracing_start_cmdline_record();
1570 void trace_printk_start_comm(void)
1572 /* Start tracing comms if trace printk is set */
1573 if (!buffers_allocated)
1575 tracing_start_cmdline_record();
1578 static void trace_printk_start_stop_comm(int enabled)
1580 if (!buffers_allocated)
1584 tracing_start_cmdline_record();
1586 tracing_stop_cmdline_record();
1590 * trace_vbprintk - write binary msg to tracing buffer
1593 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1595 struct ftrace_event_call *call = &event_bprint;
1596 struct ring_buffer_event *event;
1597 struct ring_buffer *buffer;
1598 struct trace_array *tr = &global_trace;
1599 struct bprint_entry *entry;
1600 unsigned long flags;
1602 int len = 0, size, pc;
1604 if (unlikely(tracing_selftest_running || tracing_disabled))
1607 /* Don't pollute graph traces with trace_vprintk internals */
1608 pause_graph_tracing();
1610 pc = preempt_count();
1611 preempt_disable_notrace();
1613 tbuffer = get_trace_buf();
1619 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1621 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1624 local_save_flags(flags);
1625 size = sizeof(*entry) + sizeof(u32) * len;
1626 buffer = tr->buffer;
1627 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1631 entry = ring_buffer_event_data(event);
1635 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1636 if (!filter_check_discard(call, entry, buffer, event)) {
1637 __buffer_unlock_commit(buffer, event);
1638 ftrace_trace_stack(buffer, flags, 6, pc);
1642 preempt_enable_notrace();
1643 unpause_graph_tracing();
1647 EXPORT_SYMBOL_GPL(trace_vbprintk);
1649 int trace_array_printk(struct trace_array *tr,
1650 unsigned long ip, const char *fmt, ...)
1655 if (!(trace_flags & TRACE_ITER_PRINTK))
1659 ret = trace_array_vprintk(tr, ip, fmt, ap);
1664 int trace_array_vprintk(struct trace_array *tr,
1665 unsigned long ip, const char *fmt, va_list args)
1667 struct ftrace_event_call *call = &event_print;
1668 struct ring_buffer_event *event;
1669 struct ring_buffer *buffer;
1670 int len = 0, size, pc;
1671 struct print_entry *entry;
1672 unsigned long flags;
1675 if (tracing_disabled || tracing_selftest_running)
1678 /* Don't pollute graph traces with trace_vprintk internals */
1679 pause_graph_tracing();
1681 pc = preempt_count();
1682 preempt_disable_notrace();
1685 tbuffer = get_trace_buf();
1691 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1692 if (len > TRACE_BUF_SIZE)
1695 local_save_flags(flags);
1696 size = sizeof(*entry) + len + 1;
1697 buffer = tr->buffer;
1698 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1702 entry = ring_buffer_event_data(event);
1705 memcpy(&entry->buf, tbuffer, len);
1706 entry->buf[len] = '\0';
1707 if (!filter_check_discard(call, entry, buffer, event)) {
1708 __buffer_unlock_commit(buffer, event);
1709 ftrace_trace_stack(buffer, flags, 6, pc);
1712 preempt_enable_notrace();
1713 unpause_graph_tracing();
1718 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1720 return trace_array_vprintk(&global_trace, ip, fmt, args);
1722 EXPORT_SYMBOL_GPL(trace_vprintk);
1724 static void trace_iterator_increment(struct trace_iterator *iter)
1726 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
1730 ring_buffer_read(buf_iter, NULL);
1733 static struct trace_entry *
1734 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1735 unsigned long *lost_events)
1737 struct ring_buffer_event *event;
1738 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
1741 event = ring_buffer_iter_peek(buf_iter, ts);
1743 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1747 iter->ent_size = ring_buffer_event_length(event);
1748 return ring_buffer_event_data(event);
1754 static struct trace_entry *
1755 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1756 unsigned long *missing_events, u64 *ent_ts)
1758 struct ring_buffer *buffer = iter->tr->buffer;
1759 struct trace_entry *ent, *next = NULL;
1760 unsigned long lost_events = 0, next_lost = 0;
1761 int cpu_file = iter->cpu_file;
1762 u64 next_ts = 0, ts;
1768 * If we are in a per_cpu trace file, don't bother by iterating over
1769 * all cpu and peek directly.
1771 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1772 if (ring_buffer_empty_cpu(buffer, cpu_file))
1774 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1776 *ent_cpu = cpu_file;
1781 for_each_tracing_cpu(cpu) {
1783 if (ring_buffer_empty_cpu(buffer, cpu))
1786 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1789 * Pick the entry with the smallest timestamp:
1791 if (ent && (!next || ts < next_ts)) {
1795 next_lost = lost_events;
1796 next_size = iter->ent_size;
1800 iter->ent_size = next_size;
1803 *ent_cpu = next_cpu;
1809 *missing_events = next_lost;
1814 /* Find the next real entry, without updating the iterator itself */
1815 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1816 int *ent_cpu, u64 *ent_ts)
1818 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1821 /* Find the next real entry, and increment the iterator to the next entry */
1822 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1824 iter->ent = __find_next_entry(iter, &iter->cpu,
1825 &iter->lost_events, &iter->ts);
1828 trace_iterator_increment(iter);
1830 return iter->ent ? iter : NULL;
1833 static void trace_consume(struct trace_iterator *iter)
1835 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1836 &iter->lost_events);
1839 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1841 struct trace_iterator *iter = m->private;
1845 WARN_ON_ONCE(iter->leftover);
1849 /* can't go backwards */
1854 ent = trace_find_next_entry_inc(iter);
1858 while (ent && iter->idx < i)
1859 ent = trace_find_next_entry_inc(iter);
1866 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1868 struct trace_array *tr = iter->tr;
1869 struct ring_buffer_event *event;
1870 struct ring_buffer_iter *buf_iter;
1871 unsigned long entries = 0;
1874 tr->data[cpu]->skipped_entries = 0;
1876 buf_iter = trace_buffer_iter(iter, cpu);
1880 ring_buffer_iter_reset(buf_iter);
1883 * We could have the case with the max latency tracers
1884 * that a reset never took place on a cpu. This is evident
1885 * by the timestamp being before the start of the buffer.
1887 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1888 if (ts >= iter->tr->time_start)
1891 ring_buffer_read(buf_iter, NULL);
1894 tr->data[cpu]->skipped_entries = entries;
1898 * The current tracer is copied to avoid a global locking
1901 static void *s_start(struct seq_file *m, loff_t *pos)
1903 struct trace_iterator *iter = m->private;
1904 static struct tracer *old_tracer;
1905 int cpu_file = iter->cpu_file;
1910 /* copy the tracer to avoid using a global lock all around */
1911 mutex_lock(&trace_types_lock);
1912 if (unlikely(old_tracer != current_trace && current_trace)) {
1913 old_tracer = current_trace;
1914 *iter->trace = *current_trace;
1916 mutex_unlock(&trace_types_lock);
1918 atomic_inc(&trace_record_cmdline_disabled);
1920 if (*pos != iter->pos) {
1925 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1926 for_each_tracing_cpu(cpu)
1927 tracing_iter_reset(iter, cpu);
1929 tracing_iter_reset(iter, cpu_file);
1932 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1937 * If we overflowed the seq_file before, then we want
1938 * to just reuse the trace_seq buffer again.
1944 p = s_next(m, p, &l);
1948 trace_event_read_lock();
1949 trace_access_lock(cpu_file);
1953 static void s_stop(struct seq_file *m, void *p)
1955 struct trace_iterator *iter = m->private;
1957 atomic_dec(&trace_record_cmdline_disabled);
1958 trace_access_unlock(iter->cpu_file);
1959 trace_event_read_unlock();
1963 get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
1965 unsigned long count;
1971 for_each_tracing_cpu(cpu) {
1972 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1974 * If this buffer has skipped entries, then we hold all
1975 * entries for the trace and we need to ignore the
1976 * ones before the time stamp.
1978 if (tr->data[cpu]->skipped_entries) {
1979 count -= tr->data[cpu]->skipped_entries;
1980 /* total is the same as the entries */
1984 ring_buffer_overrun_cpu(tr->buffer, cpu);
1989 static void print_lat_help_header(struct seq_file *m)
1991 seq_puts(m, "# _------=> CPU# \n");
1992 seq_puts(m, "# / _-----=> irqs-off \n");
1993 seq_puts(m, "# | / _----=> need-resched \n");
1994 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1995 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1996 seq_puts(m, "# |||| / delay \n");
1997 seq_puts(m, "# cmd pid ||||| time | caller \n");
1998 seq_puts(m, "# \\ / ||||| \\ | / \n");
2001 static void print_event_info(struct trace_array *tr, struct seq_file *m)
2003 unsigned long total;
2004 unsigned long entries;
2006 get_total_entries(tr, &total, &entries);
2007 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2008 entries, total, num_online_cpus());
2012 static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
2014 print_event_info(tr, m);
2015 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2016 seq_puts(m, "# | | | | |\n");
2019 static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
2021 print_event_info(tr, m);
2022 seq_puts(m, "# _-----=> irqs-off\n");
2023 seq_puts(m, "# / _----=> need-resched\n");
2024 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2025 seq_puts(m, "# || / _--=> preempt-depth\n");
2026 seq_puts(m, "# ||| / delay\n");
2027 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2028 seq_puts(m, "# | | | |||| | |\n");
2032 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2034 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2035 struct trace_array *tr = iter->tr;
2036 struct trace_array_cpu *data = tr->data[tr->cpu];
2037 struct tracer *type = current_trace;
2038 unsigned long entries;
2039 unsigned long total;
2040 const char *name = "preemption";
2045 get_total_entries(tr, &total, &entries);
2047 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2049 seq_puts(m, "# -----------------------------------"
2050 "---------------------------------\n");
2051 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2052 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2053 nsecs_to_usecs(data->saved_latency),
2057 #if defined(CONFIG_PREEMPT_NONE)
2059 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2061 #elif defined(CONFIG_PREEMPT)
2066 /* These are reserved for later use */
2069 seq_printf(m, " #P:%d)\n", num_online_cpus());
2073 seq_puts(m, "# -----------------\n");
2074 seq_printf(m, "# | task: %.16s-%d "
2075 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2076 data->comm, data->pid,
2077 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2078 data->policy, data->rt_priority);
2079 seq_puts(m, "# -----------------\n");
2081 if (data->critical_start) {
2082 seq_puts(m, "# => started at: ");
2083 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2084 trace_print_seq(m, &iter->seq);
2085 seq_puts(m, "\n# => ended at: ");
2086 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2087 trace_print_seq(m, &iter->seq);
2088 seq_puts(m, "\n#\n");
2094 static void test_cpu_buff_start(struct trace_iterator *iter)
2096 struct trace_seq *s = &iter->seq;
2098 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2101 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2104 if (cpumask_test_cpu(iter->cpu, iter->started))
2107 if (iter->tr->data[iter->cpu]->skipped_entries)
2110 cpumask_set_cpu(iter->cpu, iter->started);
2112 /* Don't print started cpu buffer for the first entry of the trace */
2114 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2118 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2120 struct trace_seq *s = &iter->seq;
2121 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2122 struct trace_entry *entry;
2123 struct trace_event *event;
2127 test_cpu_buff_start(iter);
2129 event = ftrace_find_event(entry->type);
2131 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2132 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2133 if (!trace_print_lat_context(iter))
2136 if (!trace_print_context(iter))
2142 return event->funcs->trace(iter, sym_flags, event);
2144 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2147 return TRACE_TYPE_HANDLED;
2149 return TRACE_TYPE_PARTIAL_LINE;
2152 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2154 struct trace_seq *s = &iter->seq;
2155 struct trace_entry *entry;
2156 struct trace_event *event;
2160 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2161 if (!trace_seq_printf(s, "%d %d %llu ",
2162 entry->pid, iter->cpu, iter->ts))
2166 event = ftrace_find_event(entry->type);
2168 return event->funcs->raw(iter, 0, event);
2170 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2173 return TRACE_TYPE_HANDLED;
2175 return TRACE_TYPE_PARTIAL_LINE;
2178 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2180 struct trace_seq *s = &iter->seq;
2181 unsigned char newline = '\n';
2182 struct trace_entry *entry;
2183 struct trace_event *event;
2187 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2188 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2189 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2190 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2193 event = ftrace_find_event(entry->type);
2195 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2196 if (ret != TRACE_TYPE_HANDLED)
2200 SEQ_PUT_FIELD_RET(s, newline);
2202 return TRACE_TYPE_HANDLED;
2205 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2207 struct trace_seq *s = &iter->seq;
2208 struct trace_entry *entry;
2209 struct trace_event *event;
2213 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2214 SEQ_PUT_FIELD_RET(s, entry->pid);
2215 SEQ_PUT_FIELD_RET(s, iter->cpu);
2216 SEQ_PUT_FIELD_RET(s, iter->ts);
2219 event = ftrace_find_event(entry->type);
2220 return event ? event->funcs->binary(iter, 0, event) :
2224 int trace_empty(struct trace_iterator *iter)
2226 struct ring_buffer_iter *buf_iter;
2229 /* If we are looking at one CPU buffer, only check that one */
2230 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2231 cpu = iter->cpu_file;
2232 buf_iter = trace_buffer_iter(iter, cpu);
2234 if (!ring_buffer_iter_empty(buf_iter))
2237 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2243 for_each_tracing_cpu(cpu) {
2244 buf_iter = trace_buffer_iter(iter, cpu);
2246 if (!ring_buffer_iter_empty(buf_iter))
2249 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2257 /* Called with trace_event_read_lock() held. */
2258 enum print_line_t print_trace_line(struct trace_iterator *iter)
2260 enum print_line_t ret;
2262 if (iter->lost_events &&
2263 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2264 iter->cpu, iter->lost_events))
2265 return TRACE_TYPE_PARTIAL_LINE;
2267 if (iter->trace && iter->trace->print_line) {
2268 ret = iter->trace->print_line(iter);
2269 if (ret != TRACE_TYPE_UNHANDLED)
2273 if (iter->ent->type == TRACE_BPRINT &&
2274 trace_flags & TRACE_ITER_PRINTK &&
2275 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2276 return trace_print_bprintk_msg_only(iter);
2278 if (iter->ent->type == TRACE_PRINT &&
2279 trace_flags & TRACE_ITER_PRINTK &&
2280 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2281 return trace_print_printk_msg_only(iter);
2283 if (trace_flags & TRACE_ITER_BIN)
2284 return print_bin_fmt(iter);
2286 if (trace_flags & TRACE_ITER_HEX)
2287 return print_hex_fmt(iter);
2289 if (trace_flags & TRACE_ITER_RAW)
2290 return print_raw_fmt(iter);
2292 return print_trace_fmt(iter);
2295 void trace_latency_header(struct seq_file *m)
2297 struct trace_iterator *iter = m->private;
2299 /* print nothing if the buffers are empty */
2300 if (trace_empty(iter))
2303 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2304 print_trace_header(m, iter);
2306 if (!(trace_flags & TRACE_ITER_VERBOSE))
2307 print_lat_help_header(m);
2310 void trace_default_header(struct seq_file *m)
2312 struct trace_iterator *iter = m->private;
2314 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2317 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2318 /* print nothing if the buffers are empty */
2319 if (trace_empty(iter))
2321 print_trace_header(m, iter);
2322 if (!(trace_flags & TRACE_ITER_VERBOSE))
2323 print_lat_help_header(m);
2325 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2326 if (trace_flags & TRACE_ITER_IRQ_INFO)
2327 print_func_help_header_irq(iter->tr, m);
2329 print_func_help_header(iter->tr, m);
2334 static void test_ftrace_alive(struct seq_file *m)
2336 if (!ftrace_is_dead())
2338 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2339 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2342 static int s_show(struct seq_file *m, void *v)
2344 struct trace_iterator *iter = v;
2347 if (iter->ent == NULL) {
2349 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2351 test_ftrace_alive(m);
2353 if (iter->trace && iter->trace->print_header)
2354 iter->trace->print_header(m);
2356 trace_default_header(m);
2358 } else if (iter->leftover) {
2360 * If we filled the seq_file buffer earlier, we
2361 * want to just show it now.
2363 ret = trace_print_seq(m, &iter->seq);
2365 /* ret should this time be zero, but you never know */
2366 iter->leftover = ret;
2369 print_trace_line(iter);
2370 ret = trace_print_seq(m, &iter->seq);
2372 * If we overflow the seq_file buffer, then it will
2373 * ask us for this data again at start up.
2375 * ret is 0 if seq_file write succeeded.
2378 iter->leftover = ret;
2384 static const struct seq_operations tracer_seq_ops = {
2391 static struct trace_iterator *
2392 __tracing_open(struct inode *inode, struct file *file)
2394 long cpu_file = (long) inode->i_private;
2395 struct trace_iterator *iter;
2398 if (tracing_disabled)
2399 return ERR_PTR(-ENODEV);
2401 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2403 return ERR_PTR(-ENOMEM);
2405 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2407 if (!iter->buffer_iter)
2411 * We make a copy of the current tracer to avoid concurrent
2412 * changes on it while we are reading.
2414 mutex_lock(&trace_types_lock);
2415 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2420 *iter->trace = *current_trace;
2422 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2425 if (current_trace && current_trace->print_max)
2428 iter->tr = &global_trace;
2430 mutex_init(&iter->mutex);
2431 iter->cpu_file = cpu_file;
2433 /* Notify the tracer early; before we stop tracing. */
2434 if (iter->trace && iter->trace->open)
2435 iter->trace->open(iter);
2437 /* Annotate start of buffers if we had overruns */
2438 if (ring_buffer_overruns(iter->tr->buffer))
2439 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2441 /* stop the trace while dumping */
2444 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2445 for_each_tracing_cpu(cpu) {
2446 iter->buffer_iter[cpu] =
2447 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2449 ring_buffer_read_prepare_sync();
2450 for_each_tracing_cpu(cpu) {
2451 ring_buffer_read_start(iter->buffer_iter[cpu]);
2452 tracing_iter_reset(iter, cpu);
2455 cpu = iter->cpu_file;
2456 iter->buffer_iter[cpu] =
2457 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2458 ring_buffer_read_prepare_sync();
2459 ring_buffer_read_start(iter->buffer_iter[cpu]);
2460 tracing_iter_reset(iter, cpu);
2463 mutex_unlock(&trace_types_lock);
2468 mutex_unlock(&trace_types_lock);
2470 kfree(iter->buffer_iter);
2472 seq_release_private(inode, file);
2473 return ERR_PTR(-ENOMEM);
2476 int tracing_open_generic(struct inode *inode, struct file *filp)
2478 if (tracing_disabled)
2481 filp->private_data = inode->i_private;
2485 static int tracing_release(struct inode *inode, struct file *file)
2487 struct seq_file *m = file->private_data;
2488 struct trace_iterator *iter;
2491 if (!(file->f_mode & FMODE_READ))
2496 mutex_lock(&trace_types_lock);
2497 for_each_tracing_cpu(cpu) {
2498 if (iter->buffer_iter[cpu])
2499 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2502 if (iter->trace && iter->trace->close)
2503 iter->trace->close(iter);
2505 /* reenable tracing if it was previously enabled */
2507 mutex_unlock(&trace_types_lock);
2509 mutex_destroy(&iter->mutex);
2510 free_cpumask_var(iter->started);
2512 kfree(iter->buffer_iter);
2513 seq_release_private(inode, file);
2517 static int tracing_open(struct inode *inode, struct file *file)
2519 struct trace_iterator *iter;
2522 /* If this file was open for write, then erase contents */
2523 if ((file->f_mode & FMODE_WRITE) &&
2524 (file->f_flags & O_TRUNC)) {
2525 long cpu = (long) inode->i_private;
2527 if (cpu == TRACE_PIPE_ALL_CPU)
2528 tracing_reset_online_cpus(&global_trace);
2530 tracing_reset(&global_trace, cpu);
2533 if (file->f_mode & FMODE_READ) {
2534 iter = __tracing_open(inode, file);
2536 ret = PTR_ERR(iter);
2537 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2538 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2544 t_next(struct seq_file *m, void *v, loff_t *pos)
2546 struct tracer *t = v;
2556 static void *t_start(struct seq_file *m, loff_t *pos)
2561 mutex_lock(&trace_types_lock);
2562 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2568 static void t_stop(struct seq_file *m, void *p)
2570 mutex_unlock(&trace_types_lock);
2573 static int t_show(struct seq_file *m, void *v)
2575 struct tracer *t = v;
2580 seq_printf(m, "%s", t->name);
2589 static const struct seq_operations show_traces_seq_ops = {
2596 static int show_traces_open(struct inode *inode, struct file *file)
2598 if (tracing_disabled)
2601 return seq_open(file, &show_traces_seq_ops);
2605 tracing_write_stub(struct file *filp, const char __user *ubuf,
2606 size_t count, loff_t *ppos)
2611 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2613 if (file->f_mode & FMODE_READ)
2614 return seq_lseek(file, offset, origin);
2619 static const struct file_operations tracing_fops = {
2620 .open = tracing_open,
2622 .write = tracing_write_stub,
2623 .llseek = tracing_seek,
2624 .release = tracing_release,
2627 static const struct file_operations show_traces_fops = {
2628 .open = show_traces_open,
2630 .release = seq_release,
2631 .llseek = seq_lseek,
2635 * Only trace on a CPU if the bitmask is set:
2637 static cpumask_var_t tracing_cpumask;
2640 * The tracer itself will not take this lock, but still we want
2641 * to provide a consistent cpumask to user-space:
2643 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2646 * Temporary storage for the character representation of the
2647 * CPU bitmask (and one more byte for the newline):
2649 static char mask_str[NR_CPUS + 1];
2652 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2653 size_t count, loff_t *ppos)
2657 mutex_lock(&tracing_cpumask_update_lock);
2659 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2660 if (count - len < 2) {
2664 len += sprintf(mask_str + len, "\n");
2665 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2668 mutex_unlock(&tracing_cpumask_update_lock);
2674 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2675 size_t count, loff_t *ppos)
2678 cpumask_var_t tracing_cpumask_new;
2680 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2683 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2687 mutex_lock(&tracing_cpumask_update_lock);
2689 local_irq_disable();
2690 arch_spin_lock(&ftrace_max_lock);
2691 for_each_tracing_cpu(cpu) {
2693 * Increase/decrease the disabled counter if we are
2694 * about to flip a bit in the cpumask:
2696 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2697 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2698 atomic_inc(&global_trace.data[cpu]->disabled);
2699 ring_buffer_record_disable_cpu(global_trace.buffer, cpu);
2701 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2702 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2703 atomic_dec(&global_trace.data[cpu]->disabled);
2704 ring_buffer_record_enable_cpu(global_trace.buffer, cpu);
2707 arch_spin_unlock(&ftrace_max_lock);
2710 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2712 mutex_unlock(&tracing_cpumask_update_lock);
2713 free_cpumask_var(tracing_cpumask_new);
2718 free_cpumask_var(tracing_cpumask_new);
2723 static const struct file_operations tracing_cpumask_fops = {
2724 .open = tracing_open_generic,
2725 .read = tracing_cpumask_read,
2726 .write = tracing_cpumask_write,
2727 .llseek = generic_file_llseek,
2730 static int tracing_trace_options_show(struct seq_file *m, void *v)
2732 struct tracer_opt *trace_opts;
2736 mutex_lock(&trace_types_lock);
2737 tracer_flags = current_trace->flags->val;
2738 trace_opts = current_trace->flags->opts;
2740 for (i = 0; trace_options[i]; i++) {
2741 if (trace_flags & (1 << i))
2742 seq_printf(m, "%s\n", trace_options[i]);
2744 seq_printf(m, "no%s\n", trace_options[i]);
2747 for (i = 0; trace_opts[i].name; i++) {
2748 if (tracer_flags & trace_opts[i].bit)
2749 seq_printf(m, "%s\n", trace_opts[i].name);
2751 seq_printf(m, "no%s\n", trace_opts[i].name);
2753 mutex_unlock(&trace_types_lock);
2758 static int __set_tracer_option(struct tracer *trace,
2759 struct tracer_flags *tracer_flags,
2760 struct tracer_opt *opts, int neg)
2764 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2769 tracer_flags->val &= ~opts->bit;
2771 tracer_flags->val |= opts->bit;
2775 /* Try to assign a tracer specific option */
2776 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2778 struct tracer_flags *tracer_flags = trace->flags;
2779 struct tracer_opt *opts = NULL;
2782 for (i = 0; tracer_flags->opts[i].name; i++) {
2783 opts = &tracer_flags->opts[i];
2785 if (strcmp(cmp, opts->name) == 0)
2786 return __set_tracer_option(trace, trace->flags,
2793 static void set_tracer_flags(unsigned int mask, int enabled)
2795 /* do nothing if flag is already set */
2796 if (!!(trace_flags & mask) == !!enabled)
2800 trace_flags |= mask;
2802 trace_flags &= ~mask;
2804 if (mask == TRACE_ITER_RECORD_CMD)
2805 trace_event_enable_cmd_record(enabled);
2807 if (mask == TRACE_ITER_OVERWRITE)
2808 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2810 if (mask == TRACE_ITER_PRINTK)
2811 trace_printk_start_stop_comm(enabled);
2815 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2816 size_t cnt, loff_t *ppos)
2824 if (cnt >= sizeof(buf))
2827 if (copy_from_user(&buf, ubuf, cnt))
2831 cmp = strstrip(buf);
2833 if (strncmp(cmp, "no", 2) == 0) {
2838 for (i = 0; trace_options[i]; i++) {
2839 if (strcmp(cmp, trace_options[i]) == 0) {
2840 set_tracer_flags(1 << i, !neg);
2845 /* If no option could be set, test the specific tracer options */
2846 if (!trace_options[i]) {
2847 mutex_lock(&trace_types_lock);
2848 ret = set_tracer_option(current_trace, cmp, neg);
2849 mutex_unlock(&trace_types_lock);
2859 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2861 if (tracing_disabled)
2863 return single_open(file, tracing_trace_options_show, NULL);
2866 static const struct file_operations tracing_iter_fops = {
2867 .open = tracing_trace_options_open,
2869 .llseek = seq_lseek,
2870 .release = single_release,
2871 .write = tracing_trace_options_write,
2874 static const char readme_msg[] =
2875 "tracing mini-HOWTO:\n\n"
2876 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2877 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2878 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
2879 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2881 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
2882 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2884 "# cat /sys/kernel/debug/tracing/trace_options\n"
2885 "noprint-parent nosym-offset nosym-addr noverbose\n"
2886 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2887 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2888 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2889 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2893 tracing_readme_read(struct file *filp, char __user *ubuf,
2894 size_t cnt, loff_t *ppos)
2896 return simple_read_from_buffer(ubuf, cnt, ppos,
2897 readme_msg, strlen(readme_msg));
2900 static const struct file_operations tracing_readme_fops = {
2901 .open = tracing_open_generic,
2902 .read = tracing_readme_read,
2903 .llseek = generic_file_llseek,
2907 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2908 size_t cnt, loff_t *ppos)
2917 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2921 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2929 for (i = 0; i < SAVED_CMDLINES; i++) {
2932 pid = map_cmdline_to_pid[i];
2933 if (pid == -1 || pid == NO_CMDLINE_MAP)
2936 trace_find_cmdline(pid, buf_comm);
2937 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2942 len = simple_read_from_buffer(ubuf, cnt, ppos,
2951 static const struct file_operations tracing_saved_cmdlines_fops = {
2952 .open = tracing_open_generic,
2953 .read = tracing_saved_cmdlines_read,
2954 .llseek = generic_file_llseek,
2958 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2959 size_t cnt, loff_t *ppos)
2961 char buf[MAX_TRACER_SIZE+2];
2964 mutex_lock(&trace_types_lock);
2966 r = sprintf(buf, "%s\n", current_trace->name);
2968 r = sprintf(buf, "\n");
2969 mutex_unlock(&trace_types_lock);
2971 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2974 int tracer_init(struct tracer *t, struct trace_array *tr)
2976 tracing_reset_online_cpus(tr);
2980 static void set_buffer_entries(struct trace_array *tr, unsigned long val)
2983 for_each_tracing_cpu(cpu)
2984 tr->data[cpu]->entries = val;
2987 static int __tracing_resize_ring_buffer(unsigned long size, int cpu)
2992 * If kernel or user changes the size of the ring buffer
2993 * we use the size that was given, and we can forget about
2994 * expanding it later.
2996 ring_buffer_expanded = 1;
2998 /* May be called before buffers are initialized */
2999 if (!global_trace.buffer)
3002 ret = ring_buffer_resize(global_trace.buffer, size, cpu);
3006 if (!current_trace->use_max_tr)
3009 ret = ring_buffer_resize(max_tr.buffer, size, cpu);
3013 if (cpu == RING_BUFFER_ALL_CPUS) {
3015 for_each_tracing_cpu(i) {
3016 r = ring_buffer_resize(global_trace.buffer,
3017 global_trace.data[i]->entries,
3023 r = ring_buffer_resize(global_trace.buffer,
3024 global_trace.data[cpu]->entries,
3030 * AARGH! We are left with different
3031 * size max buffer!!!!
3032 * The max buffer is our "snapshot" buffer.
3033 * When a tracer needs a snapshot (one of the
3034 * latency tracers), it swaps the max buffer
3035 * with the saved snap shot. We succeeded to
3036 * update the size of the main buffer, but failed to
3037 * update the size of the max buffer. But when we tried
3038 * to reset the main buffer to the original size, we
3039 * failed there too. This is very unlikely to
3040 * happen, but if it does, warn and kill all
3044 tracing_disabled = 1;
3049 if (cpu == RING_BUFFER_ALL_CPUS)
3050 set_buffer_entries(&max_tr, size);
3052 max_tr.data[cpu]->entries = size;
3055 if (cpu == RING_BUFFER_ALL_CPUS)
3056 set_buffer_entries(&global_trace, size);
3058 global_trace.data[cpu]->entries = size;
3063 static ssize_t tracing_resize_ring_buffer(unsigned long size, int cpu_id)
3067 mutex_lock(&trace_types_lock);
3069 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3070 /* make sure, this cpu is enabled in the mask */
3071 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3077 ret = __tracing_resize_ring_buffer(size, cpu_id);
3082 mutex_unlock(&trace_types_lock);
3089 * tracing_update_buffers - used by tracing facility to expand ring buffers
3091 * To save on memory when the tracing is never used on a system with it
3092 * configured in. The ring buffers are set to a minimum size. But once
3093 * a user starts to use the tracing facility, then they need to grow
3094 * to their default size.
3096 * This function is to be called when a tracer is about to be used.
3098 int tracing_update_buffers(void)
3102 mutex_lock(&trace_types_lock);
3103 if (!ring_buffer_expanded)
3104 ret = __tracing_resize_ring_buffer(trace_buf_size,
3105 RING_BUFFER_ALL_CPUS);
3106 mutex_unlock(&trace_types_lock);
3111 struct trace_option_dentry;
3113 static struct trace_option_dentry *
3114 create_trace_option_files(struct tracer *tracer);
3117 destroy_trace_option_files(struct trace_option_dentry *topts);
3119 static int tracing_set_tracer(const char *buf)
3121 static struct trace_option_dentry *topts;
3122 struct trace_array *tr = &global_trace;
3126 mutex_lock(&trace_types_lock);
3128 if (!ring_buffer_expanded) {
3129 ret = __tracing_resize_ring_buffer(trace_buf_size,
3130 RING_BUFFER_ALL_CPUS);
3136 for (t = trace_types; t; t = t->next) {
3137 if (strcmp(t->name, buf) == 0)
3144 if (t == current_trace)
3147 trace_branch_disable();
3148 if (current_trace && current_trace->reset)
3149 current_trace->reset(tr);
3150 if (current_trace && current_trace->use_max_tr) {
3152 * We don't free the ring buffer. instead, resize it because
3153 * The max_tr ring buffer has some state (e.g. ring->clock) and
3154 * we want preserve it.
3156 ring_buffer_resize(max_tr.buffer, 1, RING_BUFFER_ALL_CPUS);
3157 set_buffer_entries(&max_tr, 1);
3159 destroy_trace_option_files(topts);
3161 current_trace = &nop_trace;
3163 topts = create_trace_option_files(t);
3164 if (t->use_max_tr) {
3166 /* we need to make per cpu buffer sizes equivalent */
3167 for_each_tracing_cpu(cpu) {
3168 ret = ring_buffer_resize(max_tr.buffer,
3169 global_trace.data[cpu]->entries,
3173 max_tr.data[cpu]->entries =
3174 global_trace.data[cpu]->entries;
3179 ret = tracer_init(t, tr);
3185 trace_branch_enable(tr);
3187 mutex_unlock(&trace_types_lock);
3193 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3194 size_t cnt, loff_t *ppos)
3196 char buf[MAX_TRACER_SIZE+1];
3203 if (cnt > MAX_TRACER_SIZE)
3204 cnt = MAX_TRACER_SIZE;
3206 if (copy_from_user(&buf, ubuf, cnt))
3211 /* strip ending whitespace. */
3212 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3215 err = tracing_set_tracer(buf);
3225 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3226 size_t cnt, loff_t *ppos)
3228 unsigned long *ptr = filp->private_data;
3232 r = snprintf(buf, sizeof(buf), "%ld\n",
3233 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3234 if (r > sizeof(buf))
3236 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3240 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3241 size_t cnt, loff_t *ppos)
3243 unsigned long *ptr = filp->private_data;
3247 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3256 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3258 long cpu_file = (long) inode->i_private;
3259 struct trace_iterator *iter;
3262 if (tracing_disabled)
3265 mutex_lock(&trace_types_lock);
3267 /* create a buffer to store the information to pass to userspace */
3268 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3275 * We make a copy of the current tracer to avoid concurrent
3276 * changes on it while we are reading.
3278 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3284 *iter->trace = *current_trace;
3286 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3291 /* trace pipe does not show start of buffer */
3292 cpumask_setall(iter->started);
3294 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3295 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3297 iter->cpu_file = cpu_file;
3298 iter->tr = &global_trace;
3299 mutex_init(&iter->mutex);
3300 filp->private_data = iter;
3302 if (iter->trace->pipe_open)
3303 iter->trace->pipe_open(iter);
3305 nonseekable_open(inode, filp);
3307 mutex_unlock(&trace_types_lock);
3313 mutex_unlock(&trace_types_lock);
3317 static int tracing_release_pipe(struct inode *inode, struct file *file)
3319 struct trace_iterator *iter = file->private_data;
3321 mutex_lock(&trace_types_lock);
3323 if (iter->trace->pipe_close)
3324 iter->trace->pipe_close(iter);
3326 mutex_unlock(&trace_types_lock);
3328 free_cpumask_var(iter->started);
3329 mutex_destroy(&iter->mutex);
3337 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3339 struct trace_iterator *iter = filp->private_data;
3341 if (trace_flags & TRACE_ITER_BLOCK) {
3343 * Always select as readable when in blocking mode
3345 return POLLIN | POLLRDNORM;
3347 if (!trace_empty(iter))
3348 return POLLIN | POLLRDNORM;
3349 poll_wait(filp, &trace_wait, poll_table);
3350 if (!trace_empty(iter))
3351 return POLLIN | POLLRDNORM;
3358 void default_wait_pipe(struct trace_iterator *iter)
3362 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3364 if (trace_empty(iter))
3367 finish_wait(&trace_wait, &wait);
3371 * This is a make-shift waitqueue.
3372 * A tracer might use this callback on some rare cases:
3374 * 1) the current tracer might hold the runqueue lock when it wakes up
3375 * a reader, hence a deadlock (sched, function, and function graph tracers)
3376 * 2) the function tracers, trace all functions, we don't want
3377 * the overhead of calling wake_up and friends
3378 * (and tracing them too)
3380 * Anyway, this is really very primitive wakeup.
3382 void poll_wait_pipe(struct trace_iterator *iter)
3384 set_current_state(TASK_INTERRUPTIBLE);
3385 /* sleep for 100 msecs, and try again. */
3386 schedule_timeout(HZ / 10);
3389 /* Must be called with trace_types_lock mutex held. */
3390 static int tracing_wait_pipe(struct file *filp)
3392 struct trace_iterator *iter = filp->private_data;
3394 while (trace_empty(iter)) {
3396 if ((filp->f_flags & O_NONBLOCK)) {
3400 mutex_unlock(&iter->mutex);
3402 iter->trace->wait_pipe(iter);
3404 mutex_lock(&iter->mutex);
3406 if (signal_pending(current))
3410 * We block until we read something and tracing is enabled.
3411 * We still block if tracing is disabled, but we have never
3412 * read anything. This allows a user to cat this file, and
3413 * then enable tracing. But after we have read something,
3414 * we give an EOF when tracing is again disabled.
3416 * iter->pos will be 0 if we haven't read anything.
3418 if (tracing_is_enabled() && iter->pos)
3429 tracing_read_pipe(struct file *filp, char __user *ubuf,
3430 size_t cnt, loff_t *ppos)
3432 struct trace_iterator *iter = filp->private_data;
3433 static struct tracer *old_tracer;
3436 /* return any leftover data */
3437 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3441 trace_seq_init(&iter->seq);
3443 /* copy the tracer to avoid using a global lock all around */
3444 mutex_lock(&trace_types_lock);
3445 if (unlikely(old_tracer != current_trace && current_trace)) {
3446 old_tracer = current_trace;
3447 *iter->trace = *current_trace;
3449 mutex_unlock(&trace_types_lock);
3452 * Avoid more than one consumer on a single file descriptor
3453 * This is just a matter of traces coherency, the ring buffer itself
3456 mutex_lock(&iter->mutex);
3457 if (iter->trace->read) {
3458 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3464 sret = tracing_wait_pipe(filp);
3468 /* stop when tracing is finished */
3469 if (trace_empty(iter)) {
3474 if (cnt >= PAGE_SIZE)
3475 cnt = PAGE_SIZE - 1;
3477 /* reset all but tr, trace, and overruns */
3478 memset(&iter->seq, 0,
3479 sizeof(struct trace_iterator) -
3480 offsetof(struct trace_iterator, seq));
3483 trace_event_read_lock();
3484 trace_access_lock(iter->cpu_file);
3485 while (trace_find_next_entry_inc(iter) != NULL) {
3486 enum print_line_t ret;
3487 int len = iter->seq.len;
3489 ret = print_trace_line(iter);
3490 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3491 /* don't print partial lines */
3492 iter->seq.len = len;
3495 if (ret != TRACE_TYPE_NO_CONSUME)
3496 trace_consume(iter);
3498 if (iter->seq.len >= cnt)
3502 * Setting the full flag means we reached the trace_seq buffer
3503 * size and we should leave by partial output condition above.
3504 * One of the trace_seq_* functions is not used properly.
3506 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3509 trace_access_unlock(iter->cpu_file);
3510 trace_event_read_unlock();
3512 /* Now copy what we have to the user */
3513 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3514 if (iter->seq.readpos >= iter->seq.len)
3515 trace_seq_init(&iter->seq);
3518 * If there was nothing to send to user, in spite of consuming trace
3519 * entries, go back to wait for more entries.
3525 mutex_unlock(&iter->mutex);
3530 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3531 struct pipe_buffer *buf)
3533 __free_page(buf->page);
3536 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3539 __free_page(spd->pages[idx]);
3542 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3544 .map = generic_pipe_buf_map,
3545 .unmap = generic_pipe_buf_unmap,
3546 .confirm = generic_pipe_buf_confirm,
3547 .release = tracing_pipe_buf_release,
3548 .steal = generic_pipe_buf_steal,
3549 .get = generic_pipe_buf_get,
3553 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3558 /* Seq buffer is page-sized, exactly what we need. */
3560 count = iter->seq.len;
3561 ret = print_trace_line(iter);
3562 count = iter->seq.len - count;
3565 iter->seq.len -= count;
3568 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3569 iter->seq.len -= count;
3573 if (ret != TRACE_TYPE_NO_CONSUME)
3574 trace_consume(iter);
3576 if (!trace_find_next_entry_inc(iter)) {
3586 static ssize_t tracing_splice_read_pipe(struct file *filp,
3588 struct pipe_inode_info *pipe,
3592 struct page *pages_def[PIPE_DEF_BUFFERS];
3593 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3594 struct trace_iterator *iter = filp->private_data;
3595 struct splice_pipe_desc spd = {
3597 .partial = partial_def,
3598 .nr_pages = 0, /* This gets updated below. */
3599 .nr_pages_max = PIPE_DEF_BUFFERS,
3601 .ops = &tracing_pipe_buf_ops,
3602 .spd_release = tracing_spd_release_pipe,
3604 static struct tracer *old_tracer;
3609 if (splice_grow_spd(pipe, &spd))
3612 /* copy the tracer to avoid using a global lock all around */
3613 mutex_lock(&trace_types_lock);
3614 if (unlikely(old_tracer != current_trace && current_trace)) {
3615 old_tracer = current_trace;
3616 *iter->trace = *current_trace;
3618 mutex_unlock(&trace_types_lock);
3620 mutex_lock(&iter->mutex);
3622 if (iter->trace->splice_read) {
3623 ret = iter->trace->splice_read(iter, filp,
3624 ppos, pipe, len, flags);
3629 ret = tracing_wait_pipe(filp);
3633 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3638 trace_event_read_lock();
3639 trace_access_lock(iter->cpu_file);
3641 /* Fill as many pages as possible. */
3642 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3643 spd.pages[i] = alloc_page(GFP_KERNEL);
3647 rem = tracing_fill_pipe_page(rem, iter);
3649 /* Copy the data into the page, so we can start over. */
3650 ret = trace_seq_to_buffer(&iter->seq,
3651 page_address(spd.pages[i]),
3654 __free_page(spd.pages[i]);
3657 spd.partial[i].offset = 0;
3658 spd.partial[i].len = iter->seq.len;
3660 trace_seq_init(&iter->seq);
3663 trace_access_unlock(iter->cpu_file);
3664 trace_event_read_unlock();
3665 mutex_unlock(&iter->mutex);
3669 ret = splice_to_pipe(pipe, &spd);
3671 splice_shrink_spd(&spd);
3675 mutex_unlock(&iter->mutex);
3679 struct ftrace_entries_info {
3680 struct trace_array *tr;
3684 static int tracing_entries_open(struct inode *inode, struct file *filp)
3686 struct ftrace_entries_info *info;
3688 if (tracing_disabled)
3691 info = kzalloc(sizeof(*info), GFP_KERNEL);
3695 info->tr = &global_trace;
3696 info->cpu = (unsigned long)inode->i_private;
3698 filp->private_data = info;
3704 tracing_entries_read(struct file *filp, char __user *ubuf,
3705 size_t cnt, loff_t *ppos)
3707 struct ftrace_entries_info *info = filp->private_data;
3708 struct trace_array *tr = info->tr;
3713 mutex_lock(&trace_types_lock);
3715 if (info->cpu == RING_BUFFER_ALL_CPUS) {
3716 int cpu, buf_size_same;
3721 /* check if all cpu sizes are same */
3722 for_each_tracing_cpu(cpu) {
3723 /* fill in the size from first enabled cpu */
3725 size = tr->data[cpu]->entries;
3726 if (size != tr->data[cpu]->entries) {
3732 if (buf_size_same) {
3733 if (!ring_buffer_expanded)
3734 r = sprintf(buf, "%lu (expanded: %lu)\n",
3736 trace_buf_size >> 10);
3738 r = sprintf(buf, "%lu\n", size >> 10);
3740 r = sprintf(buf, "X\n");
3742 r = sprintf(buf, "%lu\n", tr->data[info->cpu]->entries >> 10);
3744 mutex_unlock(&trace_types_lock);
3746 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3751 tracing_entries_write(struct file *filp, const char __user *ubuf,
3752 size_t cnt, loff_t *ppos)
3754 struct ftrace_entries_info *info = filp->private_data;
3758 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3762 /* must have at least 1 entry */
3766 /* value is in KB */
3769 ret = tracing_resize_ring_buffer(val, info->cpu);
3779 tracing_entries_release(struct inode *inode, struct file *filp)
3781 struct ftrace_entries_info *info = filp->private_data;
3789 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3790 size_t cnt, loff_t *ppos)
3792 struct trace_array *tr = filp->private_data;
3795 unsigned long size = 0, expanded_size = 0;
3797 mutex_lock(&trace_types_lock);
3798 for_each_tracing_cpu(cpu) {
3799 size += tr->data[cpu]->entries >> 10;
3800 if (!ring_buffer_expanded)
3801 expanded_size += trace_buf_size >> 10;
3803 if (ring_buffer_expanded)
3804 r = sprintf(buf, "%lu\n", size);
3806 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3807 mutex_unlock(&trace_types_lock);
3809 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3813 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3814 size_t cnt, loff_t *ppos)
3817 * There is no need to read what the user has written, this function
3818 * is just to make sure that there is no error when "echo" is used
3827 tracing_free_buffer_release(struct inode *inode, struct file *filp)
3829 /* disable tracing ? */
3830 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3832 /* resize the ring buffer to 0 */
3833 tracing_resize_ring_buffer(0, RING_BUFFER_ALL_CPUS);
3839 tracing_mark_write(struct file *filp, const char __user *ubuf,
3840 size_t cnt, loff_t *fpos)
3842 unsigned long addr = (unsigned long)ubuf;
3843 struct ring_buffer_event *event;
3844 struct ring_buffer *buffer;
3845 struct print_entry *entry;
3846 unsigned long irq_flags;
3847 struct page *pages[2];
3857 if (tracing_disabled)
3860 if (!(trace_flags & TRACE_ITER_MARKERS))
3863 if (cnt > TRACE_BUF_SIZE)
3864 cnt = TRACE_BUF_SIZE;
3867 * Userspace is injecting traces into the kernel trace buffer.
3868 * We want to be as non intrusive as possible.
3869 * To do so, we do not want to allocate any special buffers
3870 * or take any locks, but instead write the userspace data
3871 * straight into the ring buffer.
3873 * First we need to pin the userspace buffer into memory,
3874 * which, most likely it is, because it just referenced it.
3875 * But there's no guarantee that it is. By using get_user_pages_fast()
3876 * and kmap_atomic/kunmap_atomic() we can get access to the
3877 * pages directly. We then write the data directly into the
3880 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
3882 /* check if we cross pages */
3883 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
3886 offset = addr & (PAGE_SIZE - 1);
3889 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
3890 if (ret < nr_pages) {
3892 put_page(pages[ret]);
3897 for (i = 0; i < nr_pages; i++)
3898 map_page[i] = kmap_atomic(pages[i]);
3900 local_save_flags(irq_flags);
3901 size = sizeof(*entry) + cnt + 2; /* possible \n added */
3902 buffer = global_trace.buffer;
3903 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3904 irq_flags, preempt_count());
3906 /* Ring buffer disabled, return as if not open for write */
3911 entry = ring_buffer_event_data(event);
3912 entry->ip = _THIS_IP_;
3914 if (nr_pages == 2) {
3915 len = PAGE_SIZE - offset;
3916 memcpy(&entry->buf, map_page[0] + offset, len);
3917 memcpy(&entry->buf[len], map_page[1], cnt - len);
3919 memcpy(&entry->buf, map_page[0] + offset, cnt);
3921 if (entry->buf[cnt - 1] != '\n') {
3922 entry->buf[cnt] = '\n';
3923 entry->buf[cnt + 1] = '\0';
3925 entry->buf[cnt] = '\0';
3927 __buffer_unlock_commit(buffer, event);
3934 for (i = 0; i < nr_pages; i++){
3935 kunmap_atomic(map_page[i]);
3942 static int tracing_clock_show(struct seq_file *m, void *v)
3946 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3948 "%s%s%s%s", i ? " " : "",
3949 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3950 i == trace_clock_id ? "]" : "");
3956 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3957 size_t cnt, loff_t *fpos)
3960 const char *clockstr;
3963 if (cnt >= sizeof(buf))
3966 if (copy_from_user(&buf, ubuf, cnt))
3971 clockstr = strstrip(buf);
3973 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3974 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3977 if (i == ARRAY_SIZE(trace_clocks))
3982 mutex_lock(&trace_types_lock);
3984 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3986 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3989 * New clock may not be consistent with the previous clock.
3990 * Reset the buffer so that it doesn't have incomparable timestamps.
3992 tracing_reset_online_cpus(&global_trace);
3994 tracing_reset_online_cpus(&max_tr);
3996 mutex_unlock(&trace_types_lock);
4003 static int tracing_clock_open(struct inode *inode, struct file *file)
4005 if (tracing_disabled)
4007 return single_open(file, tracing_clock_show, NULL);
4010 static const struct file_operations tracing_max_lat_fops = {
4011 .open = tracing_open_generic,
4012 .read = tracing_max_lat_read,
4013 .write = tracing_max_lat_write,
4014 .llseek = generic_file_llseek,
4017 static const struct file_operations set_tracer_fops = {
4018 .open = tracing_open_generic,
4019 .read = tracing_set_trace_read,
4020 .write = tracing_set_trace_write,
4021 .llseek = generic_file_llseek,
4024 static const struct file_operations tracing_pipe_fops = {
4025 .open = tracing_open_pipe,
4026 .poll = tracing_poll_pipe,
4027 .read = tracing_read_pipe,
4028 .splice_read = tracing_splice_read_pipe,
4029 .release = tracing_release_pipe,
4030 .llseek = no_llseek,
4033 static const struct file_operations tracing_entries_fops = {
4034 .open = tracing_entries_open,
4035 .read = tracing_entries_read,
4036 .write = tracing_entries_write,
4037 .release = tracing_entries_release,
4038 .llseek = generic_file_llseek,
4041 static const struct file_operations tracing_total_entries_fops = {
4042 .open = tracing_open_generic,
4043 .read = tracing_total_entries_read,
4044 .llseek = generic_file_llseek,
4047 static const struct file_operations tracing_free_buffer_fops = {
4048 .write = tracing_free_buffer_write,
4049 .release = tracing_free_buffer_release,
4052 static const struct file_operations tracing_mark_fops = {
4053 .open = tracing_open_generic,
4054 .write = tracing_mark_write,
4055 .llseek = generic_file_llseek,
4058 static const struct file_operations trace_clock_fops = {
4059 .open = tracing_clock_open,
4061 .llseek = seq_lseek,
4062 .release = single_release,
4063 .write = tracing_clock_write,
4066 struct ftrace_buffer_info {
4067 struct trace_array *tr;
4073 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4075 int cpu = (int)(long)inode->i_private;
4076 struct ftrace_buffer_info *info;
4078 if (tracing_disabled)
4081 info = kzalloc(sizeof(*info), GFP_KERNEL);
4085 info->tr = &global_trace;
4088 /* Force reading ring buffer for first read */
4089 info->read = (unsigned int)-1;
4091 filp->private_data = info;
4093 return nonseekable_open(inode, filp);
4097 tracing_buffers_read(struct file *filp, char __user *ubuf,
4098 size_t count, loff_t *ppos)
4100 struct ftrace_buffer_info *info = filp->private_data;
4108 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
4112 /* Do we have previous read data to read? */
4113 if (info->read < PAGE_SIZE)
4116 trace_access_lock(info->cpu);
4117 ret = ring_buffer_read_page(info->tr->buffer,
4121 trace_access_unlock(info->cpu);
4128 size = PAGE_SIZE - info->read;
4132 ret = copy_to_user(ubuf, info->spare + info->read, size);
4143 static int tracing_buffers_release(struct inode *inode, struct file *file)
4145 struct ftrace_buffer_info *info = file->private_data;
4148 ring_buffer_free_read_page(info->tr->buffer, info->spare);
4155 struct ring_buffer *buffer;
4160 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4161 struct pipe_buffer *buf)
4163 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4168 ring_buffer_free_read_page(ref->buffer, ref->page);
4173 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4174 struct pipe_buffer *buf)
4176 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4181 /* Pipe buffer operations for a buffer. */
4182 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4184 .map = generic_pipe_buf_map,
4185 .unmap = generic_pipe_buf_unmap,
4186 .confirm = generic_pipe_buf_confirm,
4187 .release = buffer_pipe_buf_release,
4188 .steal = generic_pipe_buf_steal,
4189 .get = buffer_pipe_buf_get,
4193 * Callback from splice_to_pipe(), if we need to release some pages
4194 * at the end of the spd in case we error'ed out in filling the pipe.
4196 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4198 struct buffer_ref *ref =
4199 (struct buffer_ref *)spd->partial[i].private;
4204 ring_buffer_free_read_page(ref->buffer, ref->page);
4206 spd->partial[i].private = 0;
4210 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4211 struct pipe_inode_info *pipe, size_t len,
4214 struct ftrace_buffer_info *info = file->private_data;
4215 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4216 struct page *pages_def[PIPE_DEF_BUFFERS];
4217 struct splice_pipe_desc spd = {
4219 .partial = partial_def,
4220 .nr_pages_max = PIPE_DEF_BUFFERS,
4222 .ops = &buffer_pipe_buf_ops,
4223 .spd_release = buffer_spd_release,
4225 struct buffer_ref *ref;
4226 int entries, size, i;
4229 if (splice_grow_spd(pipe, &spd))
4232 if (*ppos & (PAGE_SIZE - 1)) {
4233 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
4238 if (len & (PAGE_SIZE - 1)) {
4239 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
4240 if (len < PAGE_SIZE) {
4247 trace_access_lock(info->cpu);
4248 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4250 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4254 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4259 ref->buffer = info->tr->buffer;
4260 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
4266 r = ring_buffer_read_page(ref->buffer, &ref->page,
4269 ring_buffer_free_read_page(ref->buffer, ref->page);
4275 * zero out any left over data, this is going to
4278 size = ring_buffer_page_len(ref->page);
4279 if (size < PAGE_SIZE)
4280 memset(ref->page + size, 0, PAGE_SIZE - size);
4282 page = virt_to_page(ref->page);
4284 spd.pages[i] = page;
4285 spd.partial[i].len = PAGE_SIZE;
4286 spd.partial[i].offset = 0;
4287 spd.partial[i].private = (unsigned long)ref;
4291 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4294 trace_access_unlock(info->cpu);
4297 /* did we read anything? */
4298 if (!spd.nr_pages) {
4299 if (flags & SPLICE_F_NONBLOCK)
4307 ret = splice_to_pipe(pipe, &spd);
4308 splice_shrink_spd(&spd);
4313 static const struct file_operations tracing_buffers_fops = {
4314 .open = tracing_buffers_open,
4315 .read = tracing_buffers_read,
4316 .release = tracing_buffers_release,
4317 .splice_read = tracing_buffers_splice_read,
4318 .llseek = no_llseek,
4322 tracing_stats_read(struct file *filp, char __user *ubuf,
4323 size_t count, loff_t *ppos)
4325 unsigned long cpu = (unsigned long)filp->private_data;
4326 struct trace_array *tr = &global_trace;
4327 struct trace_seq *s;
4329 unsigned long long t;
4330 unsigned long usec_rem;
4332 s = kmalloc(sizeof(*s), GFP_KERNEL);
4338 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4339 trace_seq_printf(s, "entries: %ld\n", cnt);
4341 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4342 trace_seq_printf(s, "overrun: %ld\n", cnt);
4344 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4345 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4347 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4348 trace_seq_printf(s, "bytes: %ld\n", cnt);
4350 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4351 usec_rem = do_div(t, USEC_PER_SEC);
4352 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
4354 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4355 usec_rem = do_div(t, USEC_PER_SEC);
4356 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4358 cnt = ring_buffer_dropped_events_cpu(tr->buffer, cpu);
4359 trace_seq_printf(s, "dropped events: %ld\n", cnt);
4361 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4368 static const struct file_operations tracing_stats_fops = {
4369 .open = tracing_open_generic,
4370 .read = tracing_stats_read,
4371 .llseek = generic_file_llseek,
4374 #ifdef CONFIG_DYNAMIC_FTRACE
4376 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4382 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4383 size_t cnt, loff_t *ppos)
4385 static char ftrace_dyn_info_buffer[1024];
4386 static DEFINE_MUTEX(dyn_info_mutex);
4387 unsigned long *p = filp->private_data;
4388 char *buf = ftrace_dyn_info_buffer;
4389 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4392 mutex_lock(&dyn_info_mutex);
4393 r = sprintf(buf, "%ld ", *p);
4395 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4398 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4400 mutex_unlock(&dyn_info_mutex);
4405 static const struct file_operations tracing_dyn_info_fops = {
4406 .open = tracing_open_generic,
4407 .read = tracing_read_dyn_info,
4408 .llseek = generic_file_llseek,
4412 static struct dentry *d_tracer;
4414 struct dentry *tracing_init_dentry(void)
4421 if (!debugfs_initialized())
4424 d_tracer = debugfs_create_dir("tracing", NULL);
4426 if (!d_tracer && !once) {
4428 pr_warning("Could not create debugfs directory 'tracing'\n");
4435 static struct dentry *d_percpu;
4437 struct dentry *tracing_dentry_percpu(void)
4440 struct dentry *d_tracer;
4445 d_tracer = tracing_init_dentry();
4450 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4452 if (!d_percpu && !once) {
4454 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4461 static void tracing_init_debugfs_percpu(long cpu)
4463 struct dentry *d_percpu = tracing_dentry_percpu();
4464 struct dentry *d_cpu;
4465 char cpu_dir[30]; /* 30 characters should be more than enough */
4470 snprintf(cpu_dir, 30, "cpu%ld", cpu);
4471 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4473 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4477 /* per cpu trace_pipe */
4478 trace_create_file("trace_pipe", 0444, d_cpu,
4479 (void *) cpu, &tracing_pipe_fops);
4482 trace_create_file("trace", 0644, d_cpu,
4483 (void *) cpu, &tracing_fops);
4485 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4486 (void *) cpu, &tracing_buffers_fops);
4488 trace_create_file("stats", 0444, d_cpu,
4489 (void *) cpu, &tracing_stats_fops);
4491 trace_create_file("buffer_size_kb", 0444, d_cpu,
4492 (void *) cpu, &tracing_entries_fops);
4495 #ifdef CONFIG_FTRACE_SELFTEST
4496 /* Let selftest have access to static functions in this file */
4497 #include "trace_selftest.c"
4500 struct trace_option_dentry {
4501 struct tracer_opt *opt;
4502 struct tracer_flags *flags;
4503 struct dentry *entry;
4507 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4510 struct trace_option_dentry *topt = filp->private_data;
4513 if (topt->flags->val & topt->opt->bit)
4518 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4522 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4525 struct trace_option_dentry *topt = filp->private_data;
4529 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4533 if (val != 0 && val != 1)
4536 if (!!(topt->flags->val & topt->opt->bit) != val) {
4537 mutex_lock(&trace_types_lock);
4538 ret = __set_tracer_option(current_trace, topt->flags,
4540 mutex_unlock(&trace_types_lock);
4551 static const struct file_operations trace_options_fops = {
4552 .open = tracing_open_generic,
4553 .read = trace_options_read,
4554 .write = trace_options_write,
4555 .llseek = generic_file_llseek,
4559 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4562 long index = (long)filp->private_data;
4565 if (trace_flags & (1 << index))
4570 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4574 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4577 long index = (long)filp->private_data;
4581 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4585 if (val != 0 && val != 1)
4587 set_tracer_flags(1 << index, val);
4594 static const struct file_operations trace_options_core_fops = {
4595 .open = tracing_open_generic,
4596 .read = trace_options_core_read,
4597 .write = trace_options_core_write,
4598 .llseek = generic_file_llseek,
4601 struct dentry *trace_create_file(const char *name,
4603 struct dentry *parent,
4605 const struct file_operations *fops)
4609 ret = debugfs_create_file(name, mode, parent, data, fops);
4611 pr_warning("Could not create debugfs '%s' entry\n", name);
4617 static struct dentry *trace_options_init_dentry(void)
4619 struct dentry *d_tracer;
4620 static struct dentry *t_options;
4625 d_tracer = tracing_init_dentry();
4629 t_options = debugfs_create_dir("options", d_tracer);
4631 pr_warning("Could not create debugfs directory 'options'\n");
4639 create_trace_option_file(struct trace_option_dentry *topt,
4640 struct tracer_flags *flags,
4641 struct tracer_opt *opt)
4643 struct dentry *t_options;
4645 t_options = trace_options_init_dentry();
4649 topt->flags = flags;
4652 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4653 &trace_options_fops);
4657 static struct trace_option_dentry *
4658 create_trace_option_files(struct tracer *tracer)
4660 struct trace_option_dentry *topts;
4661 struct tracer_flags *flags;
4662 struct tracer_opt *opts;
4668 flags = tracer->flags;
4670 if (!flags || !flags->opts)
4675 for (cnt = 0; opts[cnt].name; cnt++)
4678 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4682 for (cnt = 0; opts[cnt].name; cnt++)
4683 create_trace_option_file(&topts[cnt], flags,
4690 destroy_trace_option_files(struct trace_option_dentry *topts)
4697 for (cnt = 0; topts[cnt].opt; cnt++) {
4698 if (topts[cnt].entry)
4699 debugfs_remove(topts[cnt].entry);
4705 static struct dentry *
4706 create_trace_option_core_file(const char *option, long index)
4708 struct dentry *t_options;
4710 t_options = trace_options_init_dentry();
4714 return trace_create_file(option, 0644, t_options, (void *)index,
4715 &trace_options_core_fops);
4718 static __init void create_trace_options_dir(void)
4720 struct dentry *t_options;
4723 t_options = trace_options_init_dentry();
4727 for (i = 0; trace_options[i]; i++)
4728 create_trace_option_core_file(trace_options[i], i);
4732 rb_simple_read(struct file *filp, char __user *ubuf,
4733 size_t cnt, loff_t *ppos)
4735 struct trace_array *tr = filp->private_data;
4736 struct ring_buffer *buffer = tr->buffer;
4741 r = ring_buffer_record_is_on(buffer);
4745 r = sprintf(buf, "%d\n", r);
4747 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4751 rb_simple_write(struct file *filp, const char __user *ubuf,
4752 size_t cnt, loff_t *ppos)
4754 struct trace_array *tr = filp->private_data;
4755 struct ring_buffer *buffer = tr->buffer;
4759 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4765 ring_buffer_record_on(buffer);
4767 ring_buffer_record_off(buffer);
4775 static const struct file_operations rb_simple_fops = {
4776 .open = tracing_open_generic,
4777 .read = rb_simple_read,
4778 .write = rb_simple_write,
4779 .llseek = default_llseek,
4782 static __init int tracer_init_debugfs(void)
4784 struct dentry *d_tracer;
4787 trace_access_lock_init();
4789 d_tracer = tracing_init_dentry();
4791 trace_create_file("trace_options", 0644, d_tracer,
4792 NULL, &tracing_iter_fops);
4794 trace_create_file("tracing_cpumask", 0644, d_tracer,
4795 NULL, &tracing_cpumask_fops);
4797 trace_create_file("trace", 0644, d_tracer,
4798 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4800 trace_create_file("available_tracers", 0444, d_tracer,
4801 &global_trace, &show_traces_fops);
4803 trace_create_file("current_tracer", 0644, d_tracer,
4804 &global_trace, &set_tracer_fops);
4806 #ifdef CONFIG_TRACER_MAX_TRACE
4807 trace_create_file("tracing_max_latency", 0644, d_tracer,
4808 &tracing_max_latency, &tracing_max_lat_fops);
4811 trace_create_file("tracing_thresh", 0644, d_tracer,
4812 &tracing_thresh, &tracing_max_lat_fops);
4814 trace_create_file("README", 0444, d_tracer,
4815 NULL, &tracing_readme_fops);
4817 trace_create_file("trace_pipe", 0444, d_tracer,
4818 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4820 trace_create_file("buffer_size_kb", 0644, d_tracer,
4821 (void *) RING_BUFFER_ALL_CPUS, &tracing_entries_fops);
4823 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
4824 &global_trace, &tracing_total_entries_fops);
4826 trace_create_file("free_buffer", 0644, d_tracer,
4827 &global_trace, &tracing_free_buffer_fops);
4829 trace_create_file("trace_marker", 0220, d_tracer,
4830 NULL, &tracing_mark_fops);
4832 trace_create_file("saved_cmdlines", 0444, d_tracer,
4833 NULL, &tracing_saved_cmdlines_fops);
4835 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4838 trace_create_file("tracing_on", 0644, d_tracer,
4839 &global_trace, &rb_simple_fops);
4841 #ifdef CONFIG_DYNAMIC_FTRACE
4842 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4843 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4846 create_trace_options_dir();
4848 for_each_tracing_cpu(cpu)
4849 tracing_init_debugfs_percpu(cpu);
4854 static int trace_panic_handler(struct notifier_block *this,
4855 unsigned long event, void *unused)
4857 if (ftrace_dump_on_oops)
4858 ftrace_dump(ftrace_dump_on_oops);
4862 static struct notifier_block trace_panic_notifier = {
4863 .notifier_call = trace_panic_handler,
4865 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4868 static int trace_die_handler(struct notifier_block *self,
4874 if (ftrace_dump_on_oops)
4875 ftrace_dump(ftrace_dump_on_oops);
4883 static struct notifier_block trace_die_notifier = {
4884 .notifier_call = trace_die_handler,
4889 * printk is set to max of 1024, we really don't need it that big.
4890 * Nothing should be printing 1000 characters anyway.
4892 #define TRACE_MAX_PRINT 1000
4895 * Define here KERN_TRACE so that we have one place to modify
4896 * it if we decide to change what log level the ftrace dump
4899 #define KERN_TRACE KERN_EMERG
4902 trace_printk_seq(struct trace_seq *s)
4904 /* Probably should print a warning here. */
4908 /* should be zero ended, but we are paranoid. */
4909 s->buffer[s->len] = 0;
4911 printk(KERN_TRACE "%s", s->buffer);
4916 void trace_init_global_iter(struct trace_iterator *iter)
4918 iter->tr = &global_trace;
4919 iter->trace = current_trace;
4920 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4924 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
4926 static arch_spinlock_t ftrace_dump_lock =
4927 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
4928 /* use static because iter can be a bit big for the stack */
4929 static struct trace_iterator iter;
4930 unsigned int old_userobj;
4931 static int dump_ran;
4932 unsigned long flags;
4936 local_irq_save(flags);
4937 arch_spin_lock(&ftrace_dump_lock);
4945 /* Did function tracer already get disabled? */
4946 if (ftrace_is_dead()) {
4947 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
4948 printk("# MAY BE MISSING FUNCTION EVENTS\n");
4951 if (disable_tracing)
4954 trace_init_global_iter(&iter);
4956 for_each_tracing_cpu(cpu) {
4957 atomic_inc(&iter.tr->data[cpu]->disabled);
4960 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4962 /* don't look at user memory in panic mode */
4963 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4965 /* Simulate the iterator */
4966 iter.tr = &global_trace;
4967 iter.trace = current_trace;
4969 switch (oops_dump_mode) {
4971 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4974 iter.cpu_file = raw_smp_processor_id();
4979 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4980 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4983 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4986 * We need to stop all tracing on all CPUS to read the
4987 * the next buffer. This is a bit expensive, but is
4988 * not done often. We fill all what we can read,
4989 * and then release the locks again.
4992 while (!trace_empty(&iter)) {
4995 printk(KERN_TRACE "---------------------------------\n");
4999 /* reset all but tr, trace, and overruns */
5000 memset(&iter.seq, 0,
5001 sizeof(struct trace_iterator) -
5002 offsetof(struct trace_iterator, seq));
5003 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5006 if (trace_find_next_entry_inc(&iter) != NULL) {
5009 ret = print_trace_line(&iter);
5010 if (ret != TRACE_TYPE_NO_CONSUME)
5011 trace_consume(&iter);
5013 touch_nmi_watchdog();
5015 trace_printk_seq(&iter.seq);
5019 printk(KERN_TRACE " (ftrace buffer empty)\n");
5021 printk(KERN_TRACE "---------------------------------\n");
5024 /* Re-enable tracing if requested */
5025 if (!disable_tracing) {
5026 trace_flags |= old_userobj;
5028 for_each_tracing_cpu(cpu) {
5029 atomic_dec(&iter.tr->data[cpu]->disabled);
5035 arch_spin_unlock(&ftrace_dump_lock);
5036 local_irq_restore(flags);
5039 /* By default: disable tracing after the dump */
5040 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
5042 __ftrace_dump(true, oops_dump_mode);
5044 EXPORT_SYMBOL_GPL(ftrace_dump);
5046 __init static int tracer_alloc_buffers(void)
5049 enum ring_buffer_flags rb_flags;
5054 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5057 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5058 goto out_free_buffer_mask;
5060 /* Only allocate trace_printk buffers if a trace_printk exists */
5061 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5062 /* Must be called before global_trace.buffer is allocated */
5063 trace_printk_init_buffers();
5065 /* To save memory, keep the ring buffer size to its minimum */
5066 if (ring_buffer_expanded)
5067 ring_buf_size = trace_buf_size;
5071 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5073 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5074 cpumask_copy(tracing_cpumask, cpu_all_mask);
5076 /* TODO: make the number of buffers hot pluggable with CPUS */
5077 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
5078 if (!global_trace.buffer) {
5079 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5081 goto out_free_cpumask;
5083 if (global_trace.buffer_disabled)
5087 #ifdef CONFIG_TRACER_MAX_TRACE
5088 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
5089 if (!max_tr.buffer) {
5090 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
5092 ring_buffer_free(global_trace.buffer);
5093 goto out_free_cpumask;
5097 /* Allocate the first page for all buffers */
5098 for_each_tracing_cpu(i) {
5099 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
5100 max_tr.data[i] = &per_cpu(max_tr_data, i);
5103 set_buffer_entries(&global_trace,
5104 ring_buffer_size(global_trace.buffer, 0));
5105 #ifdef CONFIG_TRACER_MAX_TRACE
5106 set_buffer_entries(&max_tr, 1);
5109 trace_init_cmdlines();
5111 register_tracer(&nop_trace);
5112 current_trace = &nop_trace;
5113 /* All seems OK, enable tracing */
5114 tracing_disabled = 0;
5116 atomic_notifier_chain_register(&panic_notifier_list,
5117 &trace_panic_notifier);
5119 register_die_notifier(&trace_die_notifier);
5124 free_cpumask_var(tracing_cpumask);
5125 out_free_buffer_mask:
5126 free_cpumask_var(tracing_buffer_mask);
5131 __init static int clear_boot_tracer(void)
5134 * The default tracer at boot buffer is an init section.
5135 * This function is called in lateinit. If we did not
5136 * find the boot tracer, then clear it out, to prevent
5137 * later registration from accessing the buffer that is
5138 * about to be freed.
5140 if (!default_bootup_tracer)
5143 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5144 default_bootup_tracer);
5145 default_bootup_tracer = NULL;
5150 early_initcall(tracer_alloc_buffers);
5151 fs_initcall(tracer_init_debugfs);
5152 late_initcall(clear_boot_tracer);