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 * Kill all tracing for good (never come back).
82 * It is initialized to 1 but will turn to zero if the initialization
83 * of the tracer is successful. But that is the only place that sets
86 static int tracing_disabled = 1;
88 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
90 static inline void ftrace_disable_cpu(void)
93 __this_cpu_inc(ftrace_cpu_disabled);
96 static inline void ftrace_enable_cpu(void)
98 __this_cpu_dec(ftrace_cpu_disabled);
102 cpumask_var_t __read_mostly tracing_buffer_mask;
105 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
107 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
108 * is set, then ftrace_dump is called. This will output the contents
109 * of the ftrace buffers to the console. This is very useful for
110 * capturing traces that lead to crashes and outputing it to a
113 * It is default off, but you can enable it with either specifying
114 * "ftrace_dump_on_oops" in the kernel command line, or setting
115 * /proc/sys/kernel/ftrace_dump_on_oops
116 * Set 1 if you want to dump buffers of all CPUs
117 * Set 2 if you want to dump the buffer of the CPU that triggered oops
120 enum ftrace_dump_mode ftrace_dump_on_oops;
122 static int tracing_set_tracer(const char *buf);
124 #define MAX_TRACER_SIZE 100
125 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
126 static char *default_bootup_tracer;
128 static int __init set_cmdline_ftrace(char *str)
130 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
131 default_bootup_tracer = bootup_tracer_buf;
132 /* We are using ftrace early, expand it */
133 ring_buffer_expanded = 1;
136 __setup("ftrace=", set_cmdline_ftrace);
138 static int __init set_ftrace_dump_on_oops(char *str)
140 if (*str++ != '=' || !*str) {
141 ftrace_dump_on_oops = DUMP_ALL;
145 if (!strcmp("orig_cpu", str)) {
146 ftrace_dump_on_oops = DUMP_ORIG;
152 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
154 unsigned long long ns2usecs(cycle_t nsec)
162 * The global_trace is the descriptor that holds the tracing
163 * buffers for the live tracing. For each CPU, it contains
164 * a link list of pages that will store trace entries. The
165 * page descriptor of the pages in the memory is used to hold
166 * the link list by linking the lru item in the page descriptor
167 * to each of the pages in the buffer per CPU.
169 * For each active CPU there is a data field that holds the
170 * pages for the buffer for that CPU. Each CPU has the same number
171 * of pages allocated for its buffer.
173 static struct trace_array global_trace;
175 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
177 int filter_current_check_discard(struct ring_buffer *buffer,
178 struct ftrace_event_call *call, void *rec,
179 struct ring_buffer_event *event)
181 return filter_check_discard(call, rec, buffer, event);
183 EXPORT_SYMBOL_GPL(filter_current_check_discard);
185 cycle_t ftrace_now(int cpu)
189 /* Early boot up does not have a buffer yet */
190 if (!global_trace.buffer)
191 return trace_clock_local();
193 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
194 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
200 * The max_tr is used to snapshot the global_trace when a maximum
201 * latency is reached. Some tracers will use this to store a maximum
202 * trace while it continues examining live traces.
204 * The buffers for the max_tr are set up the same as the global_trace.
205 * When a snapshot is taken, the link list of the max_tr is swapped
206 * with the link list of the global_trace and the buffers are reset for
207 * the global_trace so the tracing can continue.
209 static struct trace_array max_tr;
211 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
213 /* tracer_enabled is used to toggle activation of a tracer */
214 static int tracer_enabled = 1;
217 * tracing_is_enabled - return tracer_enabled status
219 * This function is used by other tracers to know the status
220 * of the tracer_enabled flag. Tracers may use this function
221 * to know if it should enable their features when starting
222 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
224 int tracing_is_enabled(void)
226 return tracer_enabled;
230 * trace_buf_size is the size in bytes that is allocated
231 * for a buffer. Note, the number of bytes is always rounded
234 * This number is purposely set to a low number of 16384.
235 * If the dump on oops happens, it will be much appreciated
236 * to not have to wait for all that output. Anyway this can be
237 * boot time and run time configurable.
239 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
241 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
243 /* trace_types holds a link list of available tracers. */
244 static struct tracer *trace_types __read_mostly;
246 /* current_trace points to the tracer that is currently active */
247 static struct tracer *current_trace __read_mostly;
250 * trace_types_lock is used to protect the trace_types list.
252 static DEFINE_MUTEX(trace_types_lock);
255 * serialize the access of the ring buffer
257 * ring buffer serializes readers, but it is low level protection.
258 * The validity of the events (which returns by ring_buffer_peek() ..etc)
259 * are not protected by ring buffer.
261 * The content of events may become garbage if we allow other process consumes
262 * these events concurrently:
263 * A) the page of the consumed events may become a normal page
264 * (not reader page) in ring buffer, and this page will be rewrited
265 * by events producer.
266 * B) The page of the consumed events may become a page for splice_read,
267 * and this page will be returned to system.
269 * These primitives allow multi process access to different cpu ring buffer
272 * These primitives don't distinguish read-only and read-consume access.
273 * Multi read-only access are also serialized.
277 static DECLARE_RWSEM(all_cpu_access_lock);
278 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
280 static inline void trace_access_lock(int cpu)
282 if (cpu == TRACE_PIPE_ALL_CPU) {
283 /* gain it for accessing the whole ring buffer. */
284 down_write(&all_cpu_access_lock);
286 /* gain it for accessing a cpu ring buffer. */
288 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
289 down_read(&all_cpu_access_lock);
291 /* Secondly block other access to this @cpu ring buffer. */
292 mutex_lock(&per_cpu(cpu_access_lock, cpu));
296 static inline void trace_access_unlock(int cpu)
298 if (cpu == TRACE_PIPE_ALL_CPU) {
299 up_write(&all_cpu_access_lock);
301 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
302 up_read(&all_cpu_access_lock);
306 static inline void trace_access_lock_init(void)
310 for_each_possible_cpu(cpu)
311 mutex_init(&per_cpu(cpu_access_lock, cpu));
316 static DEFINE_MUTEX(access_lock);
318 static inline void trace_access_lock(int cpu)
321 mutex_lock(&access_lock);
324 static inline void trace_access_unlock(int cpu)
327 mutex_unlock(&access_lock);
330 static inline void trace_access_lock_init(void)
336 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
337 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
339 /* trace_flags holds trace_options default values */
340 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
341 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
342 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
345 static int trace_stop_count;
346 static DEFINE_RAW_SPINLOCK(tracing_start_lock);
348 static void wakeup_work_handler(struct work_struct *work)
350 wake_up(&trace_wait);
353 static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
356 * tracing_on - enable tracing buffers
358 * This function enables tracing buffers that may have been
359 * disabled with tracing_off.
361 void tracing_on(void)
363 if (global_trace.buffer)
364 ring_buffer_record_on(global_trace.buffer);
366 * This flag is only looked at when buffers haven't been
367 * allocated yet. We don't really care about the race
368 * between setting this flag and actually turning
371 global_trace.buffer_disabled = 0;
373 EXPORT_SYMBOL_GPL(tracing_on);
376 * tracing_off - turn off tracing buffers
378 * This function stops the tracing buffers from recording data.
379 * It does not disable any overhead the tracers themselves may
380 * be causing. This function simply causes all recording to
381 * the ring buffers to fail.
383 void tracing_off(void)
385 if (global_trace.buffer)
386 ring_buffer_record_on(global_trace.buffer);
388 * This flag is only looked at when buffers haven't been
389 * allocated yet. We don't really care about the race
390 * between setting this flag and actually turning
393 global_trace.buffer_disabled = 1;
395 EXPORT_SYMBOL_GPL(tracing_off);
398 * tracing_is_on - show state of ring buffers enabled
400 int tracing_is_on(void)
402 if (global_trace.buffer)
403 return ring_buffer_record_is_on(global_trace.buffer);
404 return !global_trace.buffer_disabled;
406 EXPORT_SYMBOL_GPL(tracing_is_on);
409 * trace_wake_up - wake up tasks waiting for trace input
411 * Schedules a delayed work to wake up any task that is blocked on the
412 * trace_wait queue. These is used with trace_poll for tasks polling the
415 void trace_wake_up(void)
417 const unsigned long delay = msecs_to_jiffies(2);
419 if (trace_flags & TRACE_ITER_BLOCK)
421 schedule_delayed_work(&wakeup_work, delay);
424 static int __init set_buf_size(char *str)
426 unsigned long buf_size;
430 buf_size = memparse(str, &str);
431 /* nr_entries can not be zero */
434 trace_buf_size = buf_size;
437 __setup("trace_buf_size=", set_buf_size);
439 static int __init set_tracing_thresh(char *str)
441 unsigned long threshhold;
446 ret = strict_strtoul(str, 0, &threshhold);
449 tracing_thresh = threshhold * 1000;
452 __setup("tracing_thresh=", set_tracing_thresh);
454 unsigned long nsecs_to_usecs(unsigned long nsecs)
459 /* These must match the bit postions in trace_iterator_flags */
460 static const char *trace_options[] = {
492 { trace_clock_local, "local" },
493 { trace_clock_global, "global" },
494 { trace_clock_counter, "counter" },
500 * trace_parser_get_init - gets the buffer for trace parser
502 int trace_parser_get_init(struct trace_parser *parser, int size)
504 memset(parser, 0, sizeof(*parser));
506 parser->buffer = kmalloc(size, GFP_KERNEL);
515 * trace_parser_put - frees the buffer for trace parser
517 void trace_parser_put(struct trace_parser *parser)
519 kfree(parser->buffer);
523 * trace_get_user - reads the user input string separated by space
524 * (matched by isspace(ch))
526 * For each string found the 'struct trace_parser' is updated,
527 * and the function returns.
529 * Returns number of bytes read.
531 * See kernel/trace/trace.h for 'struct trace_parser' details.
533 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
534 size_t cnt, loff_t *ppos)
541 trace_parser_clear(parser);
543 ret = get_user(ch, ubuf++);
551 * The parser is not finished with the last write,
552 * continue reading the user input without skipping spaces.
555 /* skip white space */
556 while (cnt && isspace(ch)) {
557 ret = get_user(ch, ubuf++);
564 /* only spaces were written */
574 /* read the non-space input */
575 while (cnt && !isspace(ch)) {
576 if (parser->idx < parser->size - 1)
577 parser->buffer[parser->idx++] = ch;
582 ret = get_user(ch, ubuf++);
589 /* We either got finished input or we have to wait for another call. */
591 parser->buffer[parser->idx] = 0;
592 parser->cont = false;
595 parser->buffer[parser->idx++] = ch;
605 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
613 if (s->len <= s->readpos)
616 len = s->len - s->readpos;
619 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
629 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
633 if (s->len <= s->readpos)
636 len = s->len - s->readpos;
639 memcpy(buf, s->buffer + s->readpos, cnt);
646 * ftrace_max_lock is used to protect the swapping of buffers
647 * when taking a max snapshot. The buffers themselves are
648 * protected by per_cpu spinlocks. But the action of the swap
649 * needs its own lock.
651 * This is defined as a arch_spinlock_t in order to help
652 * with performance when lockdep debugging is enabled.
654 * It is also used in other places outside the update_max_tr
655 * so it needs to be defined outside of the
656 * CONFIG_TRACER_MAX_TRACE.
658 static arch_spinlock_t ftrace_max_lock =
659 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
661 unsigned long __read_mostly tracing_thresh;
663 #ifdef CONFIG_TRACER_MAX_TRACE
664 unsigned long __read_mostly tracing_max_latency;
667 * Copy the new maximum trace into the separate maximum-trace
668 * structure. (this way the maximum trace is permanently saved,
669 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
672 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
674 struct trace_array_cpu *data = tr->data[cpu];
675 struct trace_array_cpu *max_data;
678 max_tr.time_start = data->preempt_timestamp;
680 max_data = max_tr.data[cpu];
681 max_data->saved_latency = tracing_max_latency;
682 max_data->critical_start = data->critical_start;
683 max_data->critical_end = data->critical_end;
685 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
686 max_data->pid = tsk->pid;
687 max_data->uid = task_uid(tsk);
688 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
689 max_data->policy = tsk->policy;
690 max_data->rt_priority = tsk->rt_priority;
692 /* record this tasks comm */
693 tracing_record_cmdline(tsk);
697 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
699 * @tsk: the task with the latency
700 * @cpu: The cpu that initiated the trace.
702 * Flip the buffers between the @tr and the max_tr and record information
703 * about which task was the cause of this latency.
706 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
708 struct ring_buffer *buf = tr->buffer;
710 if (trace_stop_count)
713 WARN_ON_ONCE(!irqs_disabled());
714 if (!current_trace->use_max_tr) {
718 arch_spin_lock(&ftrace_max_lock);
720 tr->buffer = max_tr.buffer;
723 __update_max_tr(tr, tsk, cpu);
724 arch_spin_unlock(&ftrace_max_lock);
728 * update_max_tr_single - only copy one trace over, and reset the rest
730 * @tsk - task with the latency
731 * @cpu - the cpu of the buffer to copy.
733 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
736 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
740 if (trace_stop_count)
743 WARN_ON_ONCE(!irqs_disabled());
744 if (!current_trace->use_max_tr) {
749 arch_spin_lock(&ftrace_max_lock);
751 ftrace_disable_cpu();
753 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
757 * We failed to swap the buffer due to a commit taking
758 * place on this CPU. We fail to record, but we reset
759 * the max trace buffer (no one writes directly to it)
760 * and flag that it failed.
762 trace_array_printk(&max_tr, _THIS_IP_,
763 "Failed to swap buffers due to commit in progress\n");
768 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
770 __update_max_tr(tr, tsk, cpu);
771 arch_spin_unlock(&ftrace_max_lock);
773 #endif /* CONFIG_TRACER_MAX_TRACE */
776 * register_tracer - register a tracer with the ftrace system.
777 * @type - the plugin for the tracer
779 * Register a new plugin tracer.
781 int register_tracer(struct tracer *type)
782 __releases(kernel_lock)
783 __acquires(kernel_lock)
789 pr_info("Tracer must have a name\n");
793 if (strlen(type->name) >= MAX_TRACER_SIZE) {
794 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
798 mutex_lock(&trace_types_lock);
800 tracing_selftest_running = true;
802 for (t = trace_types; t; t = t->next) {
803 if (strcmp(type->name, t->name) == 0) {
805 pr_info("Tracer %s already registered\n",
813 type->set_flag = &dummy_set_flag;
815 type->flags = &dummy_tracer_flags;
817 if (!type->flags->opts)
818 type->flags->opts = dummy_tracer_opt;
819 if (!type->wait_pipe)
820 type->wait_pipe = default_wait_pipe;
823 #ifdef CONFIG_FTRACE_STARTUP_TEST
824 if (type->selftest && !tracing_selftest_disabled) {
825 struct tracer *saved_tracer = current_trace;
826 struct trace_array *tr = &global_trace;
829 * Run a selftest on this tracer.
830 * Here we reset the trace buffer, and set the current
831 * tracer to be this tracer. The tracer can then run some
832 * internal tracing to verify that everything is in order.
833 * If we fail, we do not register this tracer.
835 tracing_reset_online_cpus(tr);
837 current_trace = type;
839 /* If we expanded the buffers, make sure the max is expanded too */
840 if (ring_buffer_expanded && type->use_max_tr)
841 ring_buffer_resize(max_tr.buffer, trace_buf_size);
843 /* the test is responsible for initializing and enabling */
844 pr_info("Testing tracer %s: ", type->name);
845 ret = type->selftest(type, tr);
846 /* the test is responsible for resetting too */
847 current_trace = saved_tracer;
849 printk(KERN_CONT "FAILED!\n");
852 /* Only reset on passing, to avoid touching corrupted buffers */
853 tracing_reset_online_cpus(tr);
855 /* Shrink the max buffer again */
856 if (ring_buffer_expanded && type->use_max_tr)
857 ring_buffer_resize(max_tr.buffer, 1);
859 printk(KERN_CONT "PASSED\n");
863 type->next = trace_types;
867 tracing_selftest_running = false;
868 mutex_unlock(&trace_types_lock);
870 if (ret || !default_bootup_tracer)
873 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
876 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
877 /* Do we want this tracer to start on bootup? */
878 tracing_set_tracer(type->name);
879 default_bootup_tracer = NULL;
880 /* disable other selftests, since this will break it. */
881 tracing_selftest_disabled = 1;
882 #ifdef CONFIG_FTRACE_STARTUP_TEST
883 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
891 void unregister_tracer(struct tracer *type)
895 mutex_lock(&trace_types_lock);
896 for (t = &trace_types; *t; t = &(*t)->next) {
900 pr_info("Tracer %s not registered\n", type->name);
906 if (type == current_trace && tracer_enabled) {
909 if (current_trace->stop)
910 current_trace->stop(&global_trace);
911 current_trace = &nop_trace;
914 mutex_unlock(&trace_types_lock);
917 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
919 ftrace_disable_cpu();
920 ring_buffer_reset_cpu(buffer, cpu);
924 void tracing_reset(struct trace_array *tr, int cpu)
926 struct ring_buffer *buffer = tr->buffer;
928 ring_buffer_record_disable(buffer);
930 /* Make sure all commits have finished */
932 __tracing_reset(buffer, cpu);
934 ring_buffer_record_enable(buffer);
937 void tracing_reset_online_cpus(struct trace_array *tr)
939 struct ring_buffer *buffer = tr->buffer;
942 ring_buffer_record_disable(buffer);
944 /* Make sure all commits have finished */
947 tr->time_start = ftrace_now(tr->cpu);
949 for_each_online_cpu(cpu)
950 __tracing_reset(buffer, cpu);
952 ring_buffer_record_enable(buffer);
955 void tracing_reset_current(int cpu)
957 tracing_reset(&global_trace, cpu);
960 void tracing_reset_current_online_cpus(void)
962 tracing_reset_online_cpus(&global_trace);
965 #define SAVED_CMDLINES 128
966 #define NO_CMDLINE_MAP UINT_MAX
967 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
968 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
969 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
970 static int cmdline_idx;
971 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
973 /* temporary disable recording */
974 static atomic_t trace_record_cmdline_disabled __read_mostly;
976 static void trace_init_cmdlines(void)
978 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
979 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
983 int is_tracing_stopped(void)
985 return trace_stop_count;
989 * ftrace_off_permanent - disable all ftrace code permanently
991 * This should only be called when a serious anomally has
992 * been detected. This will turn off the function tracing,
993 * ring buffers, and other tracing utilites. It takes no
994 * locks and can be called from any context.
996 void ftrace_off_permanent(void)
998 tracing_disabled = 1;
1000 tracing_off_permanent();
1004 * tracing_start - quick start of the tracer
1006 * If tracing is enabled but was stopped by tracing_stop,
1007 * this will start the tracer back up.
1009 void tracing_start(void)
1011 struct ring_buffer *buffer;
1012 unsigned long flags;
1014 if (tracing_disabled)
1017 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1018 if (--trace_stop_count) {
1019 if (trace_stop_count < 0) {
1020 /* Someone screwed up their debugging */
1022 trace_stop_count = 0;
1027 /* Prevent the buffers from switching */
1028 arch_spin_lock(&ftrace_max_lock);
1030 buffer = global_trace.buffer;
1032 ring_buffer_record_enable(buffer);
1034 buffer = max_tr.buffer;
1036 ring_buffer_record_enable(buffer);
1038 arch_spin_unlock(&ftrace_max_lock);
1042 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1046 * tracing_stop - quick stop of the tracer
1048 * Light weight way to stop tracing. Use in conjunction with
1051 void tracing_stop(void)
1053 struct ring_buffer *buffer;
1054 unsigned long flags;
1057 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1058 if (trace_stop_count++)
1061 /* Prevent the buffers from switching */
1062 arch_spin_lock(&ftrace_max_lock);
1064 buffer = global_trace.buffer;
1066 ring_buffer_record_disable(buffer);
1068 buffer = max_tr.buffer;
1070 ring_buffer_record_disable(buffer);
1072 arch_spin_unlock(&ftrace_max_lock);
1075 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1078 void trace_stop_cmdline_recording(void);
1080 static void trace_save_cmdline(struct task_struct *tsk)
1084 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1088 * It's not the end of the world if we don't get
1089 * the lock, but we also don't want to spin
1090 * nor do we want to disable interrupts,
1091 * so if we miss here, then better luck next time.
1093 if (!arch_spin_trylock(&trace_cmdline_lock))
1096 idx = map_pid_to_cmdline[tsk->pid];
1097 if (idx == NO_CMDLINE_MAP) {
1098 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1101 * Check whether the cmdline buffer at idx has a pid
1102 * mapped. We are going to overwrite that entry so we
1103 * need to clear the map_pid_to_cmdline. Otherwise we
1104 * would read the new comm for the old pid.
1106 pid = map_cmdline_to_pid[idx];
1107 if (pid != NO_CMDLINE_MAP)
1108 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1110 map_cmdline_to_pid[idx] = tsk->pid;
1111 map_pid_to_cmdline[tsk->pid] = idx;
1116 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1118 arch_spin_unlock(&trace_cmdline_lock);
1121 void trace_find_cmdline(int pid, char comm[])
1126 strcpy(comm, "<idle>");
1130 if (WARN_ON_ONCE(pid < 0)) {
1131 strcpy(comm, "<XXX>");
1135 if (pid > PID_MAX_DEFAULT) {
1136 strcpy(comm, "<...>");
1141 arch_spin_lock(&trace_cmdline_lock);
1142 map = map_pid_to_cmdline[pid];
1143 if (map != NO_CMDLINE_MAP)
1144 strcpy(comm, saved_cmdlines[map]);
1146 strcpy(comm, "<...>");
1148 arch_spin_unlock(&trace_cmdline_lock);
1152 void tracing_record_cmdline(struct task_struct *tsk)
1154 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1158 trace_save_cmdline(tsk);
1162 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1165 struct task_struct *tsk = current;
1167 entry->preempt_count = pc & 0xff;
1168 entry->pid = (tsk) ? tsk->pid : 0;
1171 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1172 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1174 TRACE_FLAG_IRQS_NOSUPPORT |
1176 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1177 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1178 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1180 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1182 struct ring_buffer_event *
1183 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1186 unsigned long flags, int pc)
1188 struct ring_buffer_event *event;
1190 event = ring_buffer_lock_reserve(buffer, len);
1191 if (event != NULL) {
1192 struct trace_entry *ent = ring_buffer_event_data(event);
1194 tracing_generic_entry_update(ent, flags, pc);
1202 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1203 struct ring_buffer_event *event,
1204 unsigned long flags, int pc,
1207 ring_buffer_unlock_commit(buffer, event);
1209 ftrace_trace_stack(buffer, flags, 6, pc);
1210 ftrace_trace_userstack(buffer, flags, pc);
1216 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1217 struct ring_buffer_event *event,
1218 unsigned long flags, int pc)
1220 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1223 struct ring_buffer_event *
1224 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1225 int type, unsigned long len,
1226 unsigned long flags, int pc)
1228 *current_rb = global_trace.buffer;
1229 return trace_buffer_lock_reserve(*current_rb,
1230 type, len, flags, pc);
1232 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1234 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1235 struct ring_buffer_event *event,
1236 unsigned long flags, int pc)
1238 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1240 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1242 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1243 struct ring_buffer_event *event,
1244 unsigned long flags, int pc)
1246 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1248 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1250 void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1251 struct ring_buffer_event *event,
1252 unsigned long flags, int pc,
1253 struct pt_regs *regs)
1255 ring_buffer_unlock_commit(buffer, event);
1257 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1258 ftrace_trace_userstack(buffer, flags, pc);
1260 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1262 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1263 struct ring_buffer_event *event)
1265 ring_buffer_discard_commit(buffer, event);
1267 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1270 trace_function(struct trace_array *tr,
1271 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1274 struct ftrace_event_call *call = &event_function;
1275 struct ring_buffer *buffer = tr->buffer;
1276 struct ring_buffer_event *event;
1277 struct ftrace_entry *entry;
1279 /* If we are reading the ring buffer, don't trace */
1280 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1283 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1287 entry = ring_buffer_event_data(event);
1289 entry->parent_ip = parent_ip;
1291 if (!filter_check_discard(call, entry, buffer, event))
1292 ring_buffer_unlock_commit(buffer, event);
1296 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1297 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1300 if (likely(!atomic_read(&data->disabled)))
1301 trace_function(tr, ip, parent_ip, flags, pc);
1304 #ifdef CONFIG_STACKTRACE
1306 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1307 struct ftrace_stack {
1308 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1311 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1312 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1314 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1315 unsigned long flags,
1316 int skip, int pc, struct pt_regs *regs)
1318 struct ftrace_event_call *call = &event_kernel_stack;
1319 struct ring_buffer_event *event;
1320 struct stack_entry *entry;
1321 struct stack_trace trace;
1323 int size = FTRACE_STACK_ENTRIES;
1325 trace.nr_entries = 0;
1329 * Since events can happen in NMIs there's no safe way to
1330 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1331 * or NMI comes in, it will just have to use the default
1332 * FTRACE_STACK_SIZE.
1334 preempt_disable_notrace();
1336 use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1338 * We don't need any atomic variables, just a barrier.
1339 * If an interrupt comes in, we don't care, because it would
1340 * have exited and put the counter back to what we want.
1341 * We just need a barrier to keep gcc from moving things
1345 if (use_stack == 1) {
1346 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1347 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1350 save_stack_trace_regs(regs, &trace);
1352 save_stack_trace(&trace);
1354 if (trace.nr_entries > size)
1355 size = trace.nr_entries;
1357 /* From now on, use_stack is a boolean */
1360 size *= sizeof(unsigned long);
1362 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1363 sizeof(*entry) + size, flags, pc);
1366 entry = ring_buffer_event_data(event);
1368 memset(&entry->caller, 0, size);
1371 memcpy(&entry->caller, trace.entries,
1372 trace.nr_entries * sizeof(unsigned long));
1374 trace.max_entries = FTRACE_STACK_ENTRIES;
1375 trace.entries = entry->caller;
1377 save_stack_trace_regs(regs, &trace);
1379 save_stack_trace(&trace);
1382 entry->size = trace.nr_entries;
1384 if (!filter_check_discard(call, entry, buffer, event))
1385 ring_buffer_unlock_commit(buffer, event);
1388 /* Again, don't let gcc optimize things here */
1390 __get_cpu_var(ftrace_stack_reserve)--;
1391 preempt_enable_notrace();
1395 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1396 int skip, int pc, struct pt_regs *regs)
1398 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1401 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1404 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1407 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1410 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1413 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1416 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1420 * trace_dump_stack - record a stack back trace in the trace buffer
1422 void trace_dump_stack(void)
1424 unsigned long flags;
1426 if (tracing_disabled || tracing_selftest_running)
1429 local_save_flags(flags);
1431 /* skipping 3 traces, seems to get us at the caller of this function */
1432 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1435 static DEFINE_PER_CPU(int, user_stack_count);
1438 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1440 struct ftrace_event_call *call = &event_user_stack;
1441 struct ring_buffer_event *event;
1442 struct userstack_entry *entry;
1443 struct stack_trace trace;
1445 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1449 * NMIs can not handle page faults, even with fix ups.
1450 * The save user stack can (and often does) fault.
1452 if (unlikely(in_nmi()))
1456 * prevent recursion, since the user stack tracing may
1457 * trigger other kernel events.
1460 if (__this_cpu_read(user_stack_count))
1463 __this_cpu_inc(user_stack_count);
1465 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1466 sizeof(*entry), flags, pc);
1468 goto out_drop_count;
1469 entry = ring_buffer_event_data(event);
1471 entry->tgid = current->tgid;
1472 memset(&entry->caller, 0, sizeof(entry->caller));
1474 trace.nr_entries = 0;
1475 trace.max_entries = FTRACE_STACK_ENTRIES;
1477 trace.entries = entry->caller;
1479 save_stack_trace_user(&trace);
1480 if (!filter_check_discard(call, entry, buffer, event))
1481 ring_buffer_unlock_commit(buffer, event);
1484 __this_cpu_dec(user_stack_count);
1490 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1492 ftrace_trace_userstack(tr, flags, preempt_count());
1496 #endif /* CONFIG_STACKTRACE */
1498 /* created for use with alloc_percpu */
1499 struct trace_buffer_struct {
1500 char buffer[TRACE_BUF_SIZE];
1503 static struct trace_buffer_struct *trace_percpu_buffer;
1504 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1505 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1506 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1509 * The buffer used is dependent on the context. There is a per cpu
1510 * buffer for normal context, softirq contex, hard irq context and
1511 * for NMI context. Thise allows for lockless recording.
1513 * Note, if the buffers failed to be allocated, then this returns NULL
1515 static char *get_trace_buf(void)
1517 struct trace_buffer_struct *percpu_buffer;
1518 struct trace_buffer_struct *buffer;
1521 * If we have allocated per cpu buffers, then we do not
1522 * need to do any locking.
1525 percpu_buffer = trace_percpu_nmi_buffer;
1527 percpu_buffer = trace_percpu_irq_buffer;
1528 else if (in_softirq())
1529 percpu_buffer = trace_percpu_sirq_buffer;
1531 percpu_buffer = trace_percpu_buffer;
1536 buffer = per_cpu_ptr(percpu_buffer, smp_processor_id());
1538 return buffer->buffer;
1541 static int alloc_percpu_trace_buffer(void)
1543 struct trace_buffer_struct *buffers;
1544 struct trace_buffer_struct *sirq_buffers;
1545 struct trace_buffer_struct *irq_buffers;
1546 struct trace_buffer_struct *nmi_buffers;
1548 buffers = alloc_percpu(struct trace_buffer_struct);
1552 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1556 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1560 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1564 trace_percpu_buffer = buffers;
1565 trace_percpu_sirq_buffer = sirq_buffers;
1566 trace_percpu_irq_buffer = irq_buffers;
1567 trace_percpu_nmi_buffer = nmi_buffers;
1572 free_percpu(irq_buffers);
1574 free_percpu(sirq_buffers);
1576 free_percpu(buffers);
1578 WARN(1, "Could not allocate percpu trace_printk buffer");
1582 void trace_printk_init_buffers(void)
1584 static int buffers_allocated;
1586 if (buffers_allocated)
1589 if (alloc_percpu_trace_buffer())
1592 pr_info("ftrace: Allocated trace_printk buffers\n");
1594 buffers_allocated = 1;
1598 * trace_vbprintk - write binary msg to tracing buffer
1601 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1603 struct ftrace_event_call *call = &event_bprint;
1604 struct ring_buffer_event *event;
1605 struct ring_buffer *buffer;
1606 struct trace_array *tr = &global_trace;
1607 struct bprint_entry *entry;
1608 unsigned long flags;
1610 int len = 0, size, pc;
1612 if (unlikely(tracing_selftest_running || tracing_disabled))
1615 /* Don't pollute graph traces with trace_vprintk internals */
1616 pause_graph_tracing();
1618 pc = preempt_count();
1619 preempt_disable_notrace();
1621 tbuffer = get_trace_buf();
1627 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1629 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1632 local_save_flags(flags);
1633 size = sizeof(*entry) + sizeof(u32) * len;
1634 buffer = tr->buffer;
1635 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1639 entry = ring_buffer_event_data(event);
1643 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1644 if (!filter_check_discard(call, entry, buffer, event)) {
1645 ring_buffer_unlock_commit(buffer, event);
1646 ftrace_trace_stack(buffer, flags, 6, pc);
1650 preempt_enable_notrace();
1651 unpause_graph_tracing();
1655 EXPORT_SYMBOL_GPL(trace_vbprintk);
1657 int trace_array_printk(struct trace_array *tr,
1658 unsigned long ip, const char *fmt, ...)
1663 if (!(trace_flags & TRACE_ITER_PRINTK))
1667 ret = trace_array_vprintk(tr, ip, fmt, ap);
1672 int trace_array_vprintk(struct trace_array *tr,
1673 unsigned long ip, const char *fmt, va_list args)
1675 struct ftrace_event_call *call = &event_print;
1676 struct ring_buffer_event *event;
1677 struct ring_buffer *buffer;
1678 int len = 0, size, pc;
1679 struct print_entry *entry;
1680 unsigned long flags;
1683 if (tracing_disabled || tracing_selftest_running)
1686 /* Don't pollute graph traces with trace_vprintk internals */
1687 pause_graph_tracing();
1689 pc = preempt_count();
1690 preempt_disable_notrace();
1693 tbuffer = get_trace_buf();
1699 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1700 if (len > TRACE_BUF_SIZE)
1703 local_save_flags(flags);
1704 size = sizeof(*entry) + len + 1;
1705 buffer = tr->buffer;
1706 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1710 entry = ring_buffer_event_data(event);
1713 memcpy(&entry->buf, tbuffer, len);
1714 entry->buf[len] = '\0';
1715 if (!filter_check_discard(call, entry, buffer, event)) {
1716 ring_buffer_unlock_commit(buffer, event);
1717 ftrace_trace_stack(buffer, flags, 6, pc);
1720 preempt_enable_notrace();
1721 unpause_graph_tracing();
1726 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1728 return trace_array_vprintk(&global_trace, ip, fmt, args);
1730 EXPORT_SYMBOL_GPL(trace_vprintk);
1732 static void trace_iterator_increment(struct trace_iterator *iter)
1734 /* Don't allow ftrace to trace into the ring buffers */
1735 ftrace_disable_cpu();
1738 if (iter->buffer_iter[iter->cpu])
1739 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1741 ftrace_enable_cpu();
1744 static struct trace_entry *
1745 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1746 unsigned long *lost_events)
1748 struct ring_buffer_event *event;
1749 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1751 /* Don't allow ftrace to trace into the ring buffers */
1752 ftrace_disable_cpu();
1755 event = ring_buffer_iter_peek(buf_iter, ts);
1757 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1760 ftrace_enable_cpu();
1763 iter->ent_size = ring_buffer_event_length(event);
1764 return ring_buffer_event_data(event);
1770 static struct trace_entry *
1771 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1772 unsigned long *missing_events, u64 *ent_ts)
1774 struct ring_buffer *buffer = iter->tr->buffer;
1775 struct trace_entry *ent, *next = NULL;
1776 unsigned long lost_events = 0, next_lost = 0;
1777 int cpu_file = iter->cpu_file;
1778 u64 next_ts = 0, ts;
1784 * If we are in a per_cpu trace file, don't bother by iterating over
1785 * all cpu and peek directly.
1787 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1788 if (ring_buffer_empty_cpu(buffer, cpu_file))
1790 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1792 *ent_cpu = cpu_file;
1797 for_each_tracing_cpu(cpu) {
1799 if (ring_buffer_empty_cpu(buffer, cpu))
1802 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1805 * Pick the entry with the smallest timestamp:
1807 if (ent && (!next || ts < next_ts)) {
1811 next_lost = lost_events;
1812 next_size = iter->ent_size;
1816 iter->ent_size = next_size;
1819 *ent_cpu = next_cpu;
1825 *missing_events = next_lost;
1830 /* Find the next real entry, without updating the iterator itself */
1831 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1832 int *ent_cpu, u64 *ent_ts)
1834 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1837 /* Find the next real entry, and increment the iterator to the next entry */
1838 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1840 iter->ent = __find_next_entry(iter, &iter->cpu,
1841 &iter->lost_events, &iter->ts);
1844 trace_iterator_increment(iter);
1846 return iter->ent ? iter : NULL;
1849 static void trace_consume(struct trace_iterator *iter)
1851 /* Don't allow ftrace to trace into the ring buffers */
1852 ftrace_disable_cpu();
1853 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1854 &iter->lost_events);
1855 ftrace_enable_cpu();
1858 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1860 struct trace_iterator *iter = m->private;
1864 WARN_ON_ONCE(iter->leftover);
1868 /* can't go backwards */
1873 ent = trace_find_next_entry_inc(iter);
1877 while (ent && iter->idx < i)
1878 ent = trace_find_next_entry_inc(iter);
1885 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1887 struct trace_array *tr = iter->tr;
1888 struct ring_buffer_event *event;
1889 struct ring_buffer_iter *buf_iter;
1890 unsigned long entries = 0;
1893 tr->data[cpu]->skipped_entries = 0;
1895 if (!iter->buffer_iter[cpu])
1898 buf_iter = iter->buffer_iter[cpu];
1899 ring_buffer_iter_reset(buf_iter);
1902 * We could have the case with the max latency tracers
1903 * that a reset never took place on a cpu. This is evident
1904 * by the timestamp being before the start of the buffer.
1906 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1907 if (ts >= iter->tr->time_start)
1910 ring_buffer_read(buf_iter, NULL);
1913 tr->data[cpu]->skipped_entries = entries;
1917 * The current tracer is copied to avoid a global locking
1920 static void *s_start(struct seq_file *m, loff_t *pos)
1922 struct trace_iterator *iter = m->private;
1923 static struct tracer *old_tracer;
1924 int cpu_file = iter->cpu_file;
1929 /* copy the tracer to avoid using a global lock all around */
1930 mutex_lock(&trace_types_lock);
1931 if (unlikely(old_tracer != current_trace && current_trace)) {
1932 old_tracer = current_trace;
1933 *iter->trace = *current_trace;
1935 mutex_unlock(&trace_types_lock);
1937 atomic_inc(&trace_record_cmdline_disabled);
1939 if (*pos != iter->pos) {
1944 ftrace_disable_cpu();
1946 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1947 for_each_tracing_cpu(cpu)
1948 tracing_iter_reset(iter, cpu);
1950 tracing_iter_reset(iter, cpu_file);
1952 ftrace_enable_cpu();
1955 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1960 * If we overflowed the seq_file before, then we want
1961 * to just reuse the trace_seq buffer again.
1967 p = s_next(m, p, &l);
1971 trace_event_read_lock();
1972 trace_access_lock(cpu_file);
1976 static void s_stop(struct seq_file *m, void *p)
1978 struct trace_iterator *iter = m->private;
1980 atomic_dec(&trace_record_cmdline_disabled);
1981 trace_access_unlock(iter->cpu_file);
1982 trace_event_read_unlock();
1986 get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
1988 unsigned long count;
1994 for_each_tracing_cpu(cpu) {
1995 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1997 * If this buffer has skipped entries, then we hold all
1998 * entries for the trace and we need to ignore the
1999 * ones before the time stamp.
2001 if (tr->data[cpu]->skipped_entries) {
2002 count -= tr->data[cpu]->skipped_entries;
2003 /* total is the same as the entries */
2007 ring_buffer_overrun_cpu(tr->buffer, cpu);
2012 static void print_lat_help_header(struct seq_file *m)
2014 seq_puts(m, "# _------=> CPU# \n");
2015 seq_puts(m, "# / _-----=> irqs-off \n");
2016 seq_puts(m, "# | / _----=> need-resched \n");
2017 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2018 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2019 seq_puts(m, "# |||| / delay \n");
2020 seq_puts(m, "# cmd pid ||||| time | caller \n");
2021 seq_puts(m, "# \\ / ||||| \\ | / \n");
2024 static void print_event_info(struct trace_array *tr, struct seq_file *m)
2026 unsigned long total;
2027 unsigned long entries;
2029 get_total_entries(tr, &total, &entries);
2030 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2031 entries, total, num_online_cpus());
2035 static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
2037 print_event_info(tr, m);
2038 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2039 seq_puts(m, "# | | | | |\n");
2042 static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
2044 print_event_info(tr, m);
2045 seq_puts(m, "# _-----=> irqs-off\n");
2046 seq_puts(m, "# / _----=> need-resched\n");
2047 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2048 seq_puts(m, "# || / _--=> preempt-depth\n");
2049 seq_puts(m, "# ||| / delay\n");
2050 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2051 seq_puts(m, "# | | | |||| | |\n");
2055 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2057 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2058 struct trace_array *tr = iter->tr;
2059 struct trace_array_cpu *data = tr->data[tr->cpu];
2060 struct tracer *type = current_trace;
2061 unsigned long entries;
2062 unsigned long total;
2063 const char *name = "preemption";
2068 get_total_entries(tr, &total, &entries);
2070 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2072 seq_puts(m, "# -----------------------------------"
2073 "---------------------------------\n");
2074 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2075 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2076 nsecs_to_usecs(data->saved_latency),
2080 #if defined(CONFIG_PREEMPT_NONE)
2082 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2084 #elif defined(CONFIG_PREEMPT)
2089 /* These are reserved for later use */
2092 seq_printf(m, " #P:%d)\n", num_online_cpus());
2096 seq_puts(m, "# -----------------\n");
2097 seq_printf(m, "# | task: %.16s-%d "
2098 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2099 data->comm, data->pid, data->uid, data->nice,
2100 data->policy, data->rt_priority);
2101 seq_puts(m, "# -----------------\n");
2103 if (data->critical_start) {
2104 seq_puts(m, "# => started at: ");
2105 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2106 trace_print_seq(m, &iter->seq);
2107 seq_puts(m, "\n# => ended at: ");
2108 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2109 trace_print_seq(m, &iter->seq);
2110 seq_puts(m, "\n#\n");
2116 static void test_cpu_buff_start(struct trace_iterator *iter)
2118 struct trace_seq *s = &iter->seq;
2120 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2123 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2126 if (cpumask_test_cpu(iter->cpu, iter->started))
2129 if (iter->tr->data[iter->cpu]->skipped_entries)
2132 cpumask_set_cpu(iter->cpu, iter->started);
2134 /* Don't print started cpu buffer for the first entry of the trace */
2136 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2140 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2142 struct trace_seq *s = &iter->seq;
2143 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2144 struct trace_entry *entry;
2145 struct trace_event *event;
2149 test_cpu_buff_start(iter);
2151 event = ftrace_find_event(entry->type);
2153 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2154 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2155 if (!trace_print_lat_context(iter))
2158 if (!trace_print_context(iter))
2164 return event->funcs->trace(iter, sym_flags, event);
2166 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2169 return TRACE_TYPE_HANDLED;
2171 return TRACE_TYPE_PARTIAL_LINE;
2174 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2176 struct trace_seq *s = &iter->seq;
2177 struct trace_entry *entry;
2178 struct trace_event *event;
2182 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2183 if (!trace_seq_printf(s, "%d %d %llu ",
2184 entry->pid, iter->cpu, iter->ts))
2188 event = ftrace_find_event(entry->type);
2190 return event->funcs->raw(iter, 0, event);
2192 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2195 return TRACE_TYPE_HANDLED;
2197 return TRACE_TYPE_PARTIAL_LINE;
2200 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2202 struct trace_seq *s = &iter->seq;
2203 unsigned char newline = '\n';
2204 struct trace_entry *entry;
2205 struct trace_event *event;
2209 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2210 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2211 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2212 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2215 event = ftrace_find_event(entry->type);
2217 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2218 if (ret != TRACE_TYPE_HANDLED)
2222 SEQ_PUT_FIELD_RET(s, newline);
2224 return TRACE_TYPE_HANDLED;
2227 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2229 struct trace_seq *s = &iter->seq;
2230 struct trace_entry *entry;
2231 struct trace_event *event;
2235 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2236 SEQ_PUT_FIELD_RET(s, entry->pid);
2237 SEQ_PUT_FIELD_RET(s, iter->cpu);
2238 SEQ_PUT_FIELD_RET(s, iter->ts);
2241 event = ftrace_find_event(entry->type);
2242 return event ? event->funcs->binary(iter, 0, event) :
2246 int trace_empty(struct trace_iterator *iter)
2250 /* If we are looking at one CPU buffer, only check that one */
2251 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2252 cpu = iter->cpu_file;
2253 if (iter->buffer_iter[cpu]) {
2254 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2257 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2263 for_each_tracing_cpu(cpu) {
2264 if (iter->buffer_iter[cpu]) {
2265 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2268 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2276 /* Called with trace_event_read_lock() held. */
2277 enum print_line_t print_trace_line(struct trace_iterator *iter)
2279 enum print_line_t ret;
2281 if (iter->lost_events &&
2282 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2283 iter->cpu, iter->lost_events))
2284 return TRACE_TYPE_PARTIAL_LINE;
2286 if (iter->trace && iter->trace->print_line) {
2287 ret = iter->trace->print_line(iter);
2288 if (ret != TRACE_TYPE_UNHANDLED)
2292 if (iter->ent->type == TRACE_BPRINT &&
2293 trace_flags & TRACE_ITER_PRINTK &&
2294 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2295 return trace_print_bprintk_msg_only(iter);
2297 if (iter->ent->type == TRACE_PRINT &&
2298 trace_flags & TRACE_ITER_PRINTK &&
2299 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2300 return trace_print_printk_msg_only(iter);
2302 if (trace_flags & TRACE_ITER_BIN)
2303 return print_bin_fmt(iter);
2305 if (trace_flags & TRACE_ITER_HEX)
2306 return print_hex_fmt(iter);
2308 if (trace_flags & TRACE_ITER_RAW)
2309 return print_raw_fmt(iter);
2311 return print_trace_fmt(iter);
2314 void trace_latency_header(struct seq_file *m)
2316 struct trace_iterator *iter = m->private;
2318 /* print nothing if the buffers are empty */
2319 if (trace_empty(iter))
2322 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2323 print_trace_header(m, iter);
2325 if (!(trace_flags & TRACE_ITER_VERBOSE))
2326 print_lat_help_header(m);
2329 void trace_default_header(struct seq_file *m)
2331 struct trace_iterator *iter = m->private;
2333 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2336 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2337 /* print nothing if the buffers are empty */
2338 if (trace_empty(iter))
2340 print_trace_header(m, iter);
2341 if (!(trace_flags & TRACE_ITER_VERBOSE))
2342 print_lat_help_header(m);
2344 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2345 if (trace_flags & TRACE_ITER_IRQ_INFO)
2346 print_func_help_header_irq(iter->tr, m);
2348 print_func_help_header(iter->tr, m);
2353 static void test_ftrace_alive(struct seq_file *m)
2355 if (!ftrace_is_dead())
2357 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2358 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2361 static int s_show(struct seq_file *m, void *v)
2363 struct trace_iterator *iter = v;
2366 if (iter->ent == NULL) {
2368 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2370 test_ftrace_alive(m);
2372 if (iter->trace && iter->trace->print_header)
2373 iter->trace->print_header(m);
2375 trace_default_header(m);
2377 } else if (iter->leftover) {
2379 * If we filled the seq_file buffer earlier, we
2380 * want to just show it now.
2382 ret = trace_print_seq(m, &iter->seq);
2384 /* ret should this time be zero, but you never know */
2385 iter->leftover = ret;
2388 print_trace_line(iter);
2389 ret = trace_print_seq(m, &iter->seq);
2391 * If we overflow the seq_file buffer, then it will
2392 * ask us for this data again at start up.
2394 * ret is 0 if seq_file write succeeded.
2397 iter->leftover = ret;
2403 static const struct seq_operations tracer_seq_ops = {
2410 static struct trace_iterator *
2411 __tracing_open(struct inode *inode, struct file *file)
2413 long cpu_file = (long) inode->i_private;
2414 void *fail_ret = ERR_PTR(-ENOMEM);
2415 struct trace_iterator *iter;
2419 if (tracing_disabled)
2420 return ERR_PTR(-ENODEV);
2422 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2424 return ERR_PTR(-ENOMEM);
2427 * We make a copy of the current tracer to avoid concurrent
2428 * changes on it while we are reading.
2430 mutex_lock(&trace_types_lock);
2431 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2436 *iter->trace = *current_trace;
2438 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2441 if (current_trace && current_trace->print_max)
2444 iter->tr = &global_trace;
2446 mutex_init(&iter->mutex);
2447 iter->cpu_file = cpu_file;
2449 /* Notify the tracer early; before we stop tracing. */
2450 if (iter->trace && iter->trace->open)
2451 iter->trace->open(iter);
2453 /* Annotate start of buffers if we had overruns */
2454 if (ring_buffer_overruns(iter->tr->buffer))
2455 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2457 /* stop the trace while dumping */
2460 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2461 for_each_tracing_cpu(cpu) {
2462 iter->buffer_iter[cpu] =
2463 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2465 ring_buffer_read_prepare_sync();
2466 for_each_tracing_cpu(cpu) {
2467 ring_buffer_read_start(iter->buffer_iter[cpu]);
2468 tracing_iter_reset(iter, cpu);
2471 cpu = iter->cpu_file;
2472 iter->buffer_iter[cpu] =
2473 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2474 ring_buffer_read_prepare_sync();
2475 ring_buffer_read_start(iter->buffer_iter[cpu]);
2476 tracing_iter_reset(iter, cpu);
2479 ret = seq_open(file, &tracer_seq_ops);
2481 fail_ret = ERR_PTR(ret);
2485 m = file->private_data;
2488 mutex_unlock(&trace_types_lock);
2493 for_each_tracing_cpu(cpu) {
2494 if (iter->buffer_iter[cpu])
2495 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2497 free_cpumask_var(iter->started);
2500 mutex_unlock(&trace_types_lock);
2507 int tracing_open_generic(struct inode *inode, struct file *filp)
2509 if (tracing_disabled)
2512 filp->private_data = inode->i_private;
2516 static int tracing_release(struct inode *inode, struct file *file)
2518 struct seq_file *m = file->private_data;
2519 struct trace_iterator *iter;
2522 if (!(file->f_mode & FMODE_READ))
2527 mutex_lock(&trace_types_lock);
2528 for_each_tracing_cpu(cpu) {
2529 if (iter->buffer_iter[cpu])
2530 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2533 if (iter->trace && iter->trace->close)
2534 iter->trace->close(iter);
2536 /* reenable tracing if it was previously enabled */
2538 mutex_unlock(&trace_types_lock);
2540 seq_release(inode, file);
2541 mutex_destroy(&iter->mutex);
2542 free_cpumask_var(iter->started);
2548 static int tracing_open(struct inode *inode, struct file *file)
2550 struct trace_iterator *iter;
2553 /* If this file was open for write, then erase contents */
2554 if ((file->f_mode & FMODE_WRITE) &&
2555 (file->f_flags & O_TRUNC)) {
2556 long cpu = (long) inode->i_private;
2558 if (cpu == TRACE_PIPE_ALL_CPU)
2559 tracing_reset_online_cpus(&global_trace);
2561 tracing_reset(&global_trace, cpu);
2564 if (file->f_mode & FMODE_READ) {
2565 iter = __tracing_open(inode, file);
2567 ret = PTR_ERR(iter);
2568 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2569 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2575 t_next(struct seq_file *m, void *v, loff_t *pos)
2577 struct tracer *t = v;
2587 static void *t_start(struct seq_file *m, loff_t *pos)
2592 mutex_lock(&trace_types_lock);
2593 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2599 static void t_stop(struct seq_file *m, void *p)
2601 mutex_unlock(&trace_types_lock);
2604 static int t_show(struct seq_file *m, void *v)
2606 struct tracer *t = v;
2611 seq_printf(m, "%s", t->name);
2620 static const struct seq_operations show_traces_seq_ops = {
2627 static int show_traces_open(struct inode *inode, struct file *file)
2629 if (tracing_disabled)
2632 return seq_open(file, &show_traces_seq_ops);
2636 tracing_write_stub(struct file *filp, const char __user *ubuf,
2637 size_t count, loff_t *ppos)
2642 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2644 if (file->f_mode & FMODE_READ)
2645 return seq_lseek(file, offset, origin);
2650 static const struct file_operations tracing_fops = {
2651 .open = tracing_open,
2653 .write = tracing_write_stub,
2654 .llseek = tracing_seek,
2655 .release = tracing_release,
2658 static const struct file_operations show_traces_fops = {
2659 .open = show_traces_open,
2661 .release = seq_release,
2662 .llseek = seq_lseek,
2666 * Only trace on a CPU if the bitmask is set:
2668 static cpumask_var_t tracing_cpumask;
2671 * The tracer itself will not take this lock, but still we want
2672 * to provide a consistent cpumask to user-space:
2674 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2677 * Temporary storage for the character representation of the
2678 * CPU bitmask (and one more byte for the newline):
2680 static char mask_str[NR_CPUS + 1];
2683 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2684 size_t count, loff_t *ppos)
2688 mutex_lock(&tracing_cpumask_update_lock);
2690 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2691 if (count - len < 2) {
2695 len += sprintf(mask_str + len, "\n");
2696 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2699 mutex_unlock(&tracing_cpumask_update_lock);
2705 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2706 size_t count, loff_t *ppos)
2709 cpumask_var_t tracing_cpumask_new;
2711 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2714 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2718 mutex_lock(&tracing_cpumask_update_lock);
2720 local_irq_disable();
2721 arch_spin_lock(&ftrace_max_lock);
2722 for_each_tracing_cpu(cpu) {
2724 * Increase/decrease the disabled counter if we are
2725 * about to flip a bit in the cpumask:
2727 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2728 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2729 atomic_inc(&global_trace.data[cpu]->disabled);
2731 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2732 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2733 atomic_dec(&global_trace.data[cpu]->disabled);
2736 arch_spin_unlock(&ftrace_max_lock);
2739 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2741 mutex_unlock(&tracing_cpumask_update_lock);
2742 free_cpumask_var(tracing_cpumask_new);
2747 free_cpumask_var(tracing_cpumask_new);
2752 static const struct file_operations tracing_cpumask_fops = {
2753 .open = tracing_open_generic,
2754 .read = tracing_cpumask_read,
2755 .write = tracing_cpumask_write,
2756 .llseek = generic_file_llseek,
2759 static int tracing_trace_options_show(struct seq_file *m, void *v)
2761 struct tracer_opt *trace_opts;
2765 mutex_lock(&trace_types_lock);
2766 tracer_flags = current_trace->flags->val;
2767 trace_opts = current_trace->flags->opts;
2769 for (i = 0; trace_options[i]; i++) {
2770 if (trace_flags & (1 << i))
2771 seq_printf(m, "%s\n", trace_options[i]);
2773 seq_printf(m, "no%s\n", trace_options[i]);
2776 for (i = 0; trace_opts[i].name; i++) {
2777 if (tracer_flags & trace_opts[i].bit)
2778 seq_printf(m, "%s\n", trace_opts[i].name);
2780 seq_printf(m, "no%s\n", trace_opts[i].name);
2782 mutex_unlock(&trace_types_lock);
2787 static int __set_tracer_option(struct tracer *trace,
2788 struct tracer_flags *tracer_flags,
2789 struct tracer_opt *opts, int neg)
2793 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2798 tracer_flags->val &= ~opts->bit;
2800 tracer_flags->val |= opts->bit;
2804 /* Try to assign a tracer specific option */
2805 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2807 struct tracer_flags *tracer_flags = trace->flags;
2808 struct tracer_opt *opts = NULL;
2811 for (i = 0; tracer_flags->opts[i].name; i++) {
2812 opts = &tracer_flags->opts[i];
2814 if (strcmp(cmp, opts->name) == 0)
2815 return __set_tracer_option(trace, trace->flags,
2822 static void set_tracer_flags(unsigned int mask, int enabled)
2824 /* do nothing if flag is already set */
2825 if (!!(trace_flags & mask) == !!enabled)
2829 trace_flags |= mask;
2831 trace_flags &= ~mask;
2833 if (mask == TRACE_ITER_RECORD_CMD)
2834 trace_event_enable_cmd_record(enabled);
2836 if (mask == TRACE_ITER_OVERWRITE)
2837 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2841 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2842 size_t cnt, loff_t *ppos)
2850 if (cnt >= sizeof(buf))
2853 if (copy_from_user(&buf, ubuf, cnt))
2857 cmp = strstrip(buf);
2859 if (strncmp(cmp, "no", 2) == 0) {
2864 for (i = 0; trace_options[i]; i++) {
2865 if (strcmp(cmp, trace_options[i]) == 0) {
2866 set_tracer_flags(1 << i, !neg);
2871 /* If no option could be set, test the specific tracer options */
2872 if (!trace_options[i]) {
2873 mutex_lock(&trace_types_lock);
2874 ret = set_tracer_option(current_trace, cmp, neg);
2875 mutex_unlock(&trace_types_lock);
2885 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2887 if (tracing_disabled)
2889 return single_open(file, tracing_trace_options_show, NULL);
2892 static const struct file_operations tracing_iter_fops = {
2893 .open = tracing_trace_options_open,
2895 .llseek = seq_lseek,
2896 .release = single_release,
2897 .write = tracing_trace_options_write,
2900 static const char readme_msg[] =
2901 "tracing mini-HOWTO:\n\n"
2902 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2903 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2904 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
2905 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2907 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
2908 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2910 "# cat /sys/kernel/debug/tracing/trace_options\n"
2911 "noprint-parent nosym-offset nosym-addr noverbose\n"
2912 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2913 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2914 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2915 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2919 tracing_readme_read(struct file *filp, char __user *ubuf,
2920 size_t cnt, loff_t *ppos)
2922 return simple_read_from_buffer(ubuf, cnt, ppos,
2923 readme_msg, strlen(readme_msg));
2926 static const struct file_operations tracing_readme_fops = {
2927 .open = tracing_open_generic,
2928 .read = tracing_readme_read,
2929 .llseek = generic_file_llseek,
2933 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2934 size_t cnt, loff_t *ppos)
2943 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2947 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2955 for (i = 0; i < SAVED_CMDLINES; i++) {
2958 pid = map_cmdline_to_pid[i];
2959 if (pid == -1 || pid == NO_CMDLINE_MAP)
2962 trace_find_cmdline(pid, buf_comm);
2963 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2968 len = simple_read_from_buffer(ubuf, cnt, ppos,
2977 static const struct file_operations tracing_saved_cmdlines_fops = {
2978 .open = tracing_open_generic,
2979 .read = tracing_saved_cmdlines_read,
2980 .llseek = generic_file_llseek,
2984 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2985 size_t cnt, loff_t *ppos)
2990 r = sprintf(buf, "%u\n", tracer_enabled);
2991 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2995 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2996 size_t cnt, loff_t *ppos)
2998 struct trace_array *tr = filp->private_data;
3002 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3008 mutex_lock(&trace_types_lock);
3009 if (tracer_enabled ^ val) {
3011 /* Only need to warn if this is used to change the state */
3012 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
3016 if (current_trace->start)
3017 current_trace->start(tr);
3022 if (current_trace->stop)
3023 current_trace->stop(tr);
3026 mutex_unlock(&trace_types_lock);
3034 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3035 size_t cnt, loff_t *ppos)
3037 char buf[MAX_TRACER_SIZE+2];
3040 mutex_lock(&trace_types_lock);
3042 r = sprintf(buf, "%s\n", current_trace->name);
3044 r = sprintf(buf, "\n");
3045 mutex_unlock(&trace_types_lock);
3047 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3050 int tracer_init(struct tracer *t, struct trace_array *tr)
3052 tracing_reset_online_cpus(tr);
3056 static int __tracing_resize_ring_buffer(unsigned long size)
3061 * If kernel or user changes the size of the ring buffer
3062 * we use the size that was given, and we can forget about
3063 * expanding it later.
3065 ring_buffer_expanded = 1;
3067 ret = ring_buffer_resize(global_trace.buffer, size);
3071 if (!current_trace->use_max_tr)
3074 ret = ring_buffer_resize(max_tr.buffer, size);
3078 r = ring_buffer_resize(global_trace.buffer,
3079 global_trace.entries);
3082 * AARGH! We are left with different
3083 * size max buffer!!!!
3084 * The max buffer is our "snapshot" buffer.
3085 * When a tracer needs a snapshot (one of the
3086 * latency tracers), it swaps the max buffer
3087 * with the saved snap shot. We succeeded to
3088 * update the size of the main buffer, but failed to
3089 * update the size of the max buffer. But when we tried
3090 * to reset the main buffer to the original size, we
3091 * failed there too. This is very unlikely to
3092 * happen, but if it does, warn and kill all
3096 tracing_disabled = 1;
3101 max_tr.entries = size;
3103 global_trace.entries = size;
3108 static ssize_t tracing_resize_ring_buffer(unsigned long size)
3110 int cpu, ret = size;
3112 mutex_lock(&trace_types_lock);
3116 /* disable all cpu buffers */
3117 for_each_tracing_cpu(cpu) {
3118 if (global_trace.data[cpu])
3119 atomic_inc(&global_trace.data[cpu]->disabled);
3120 if (max_tr.data[cpu])
3121 atomic_inc(&max_tr.data[cpu]->disabled);
3124 if (size != global_trace.entries)
3125 ret = __tracing_resize_ring_buffer(size);
3130 for_each_tracing_cpu(cpu) {
3131 if (global_trace.data[cpu])
3132 atomic_dec(&global_trace.data[cpu]->disabled);
3133 if (max_tr.data[cpu])
3134 atomic_dec(&max_tr.data[cpu]->disabled);
3138 mutex_unlock(&trace_types_lock);
3145 * tracing_update_buffers - used by tracing facility to expand ring buffers
3147 * To save on memory when the tracing is never used on a system with it
3148 * configured in. The ring buffers are set to a minimum size. But once
3149 * a user starts to use the tracing facility, then they need to grow
3150 * to their default size.
3152 * This function is to be called when a tracer is about to be used.
3154 int tracing_update_buffers(void)
3158 mutex_lock(&trace_types_lock);
3159 if (!ring_buffer_expanded)
3160 ret = __tracing_resize_ring_buffer(trace_buf_size);
3161 mutex_unlock(&trace_types_lock);
3166 struct trace_option_dentry;
3168 static struct trace_option_dentry *
3169 create_trace_option_files(struct tracer *tracer);
3172 destroy_trace_option_files(struct trace_option_dentry *topts);
3174 static int tracing_set_tracer(const char *buf)
3176 static struct trace_option_dentry *topts;
3177 struct trace_array *tr = &global_trace;
3181 mutex_lock(&trace_types_lock);
3183 if (!ring_buffer_expanded) {
3184 ret = __tracing_resize_ring_buffer(trace_buf_size);
3190 for (t = trace_types; t; t = t->next) {
3191 if (strcmp(t->name, buf) == 0)
3198 if (t == current_trace)
3201 trace_branch_disable();
3202 if (current_trace && current_trace->reset)
3203 current_trace->reset(tr);
3204 if (current_trace && current_trace->use_max_tr) {
3206 * We don't free the ring buffer. instead, resize it because
3207 * The max_tr ring buffer has some state (e.g. ring->clock) and
3208 * we want preserve it.
3210 ring_buffer_resize(max_tr.buffer, 1);
3213 destroy_trace_option_files(topts);
3217 topts = create_trace_option_files(current_trace);
3218 if (current_trace->use_max_tr) {
3219 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
3222 max_tr.entries = global_trace.entries;
3226 ret = tracer_init(t, tr);
3231 trace_branch_enable(tr);
3233 mutex_unlock(&trace_types_lock);
3239 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3240 size_t cnt, loff_t *ppos)
3242 char buf[MAX_TRACER_SIZE+1];
3249 if (cnt > MAX_TRACER_SIZE)
3250 cnt = MAX_TRACER_SIZE;
3252 if (copy_from_user(&buf, ubuf, cnt))
3257 /* strip ending whitespace. */
3258 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3261 err = tracing_set_tracer(buf);
3271 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3272 size_t cnt, loff_t *ppos)
3274 unsigned long *ptr = filp->private_data;
3278 r = snprintf(buf, sizeof(buf), "%ld\n",
3279 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3280 if (r > sizeof(buf))
3282 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3286 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3287 size_t cnt, loff_t *ppos)
3289 unsigned long *ptr = filp->private_data;
3293 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3302 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3304 long cpu_file = (long) inode->i_private;
3305 struct trace_iterator *iter;
3308 if (tracing_disabled)
3311 mutex_lock(&trace_types_lock);
3313 /* create a buffer to store the information to pass to userspace */
3314 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3321 * We make a copy of the current tracer to avoid concurrent
3322 * changes on it while we are reading.
3324 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3330 *iter->trace = *current_trace;
3332 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3337 /* trace pipe does not show start of buffer */
3338 cpumask_setall(iter->started);
3340 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3341 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3343 iter->cpu_file = cpu_file;
3344 iter->tr = &global_trace;
3345 mutex_init(&iter->mutex);
3346 filp->private_data = iter;
3348 if (iter->trace->pipe_open)
3349 iter->trace->pipe_open(iter);
3351 nonseekable_open(inode, filp);
3353 mutex_unlock(&trace_types_lock);
3359 mutex_unlock(&trace_types_lock);
3363 static int tracing_release_pipe(struct inode *inode, struct file *file)
3365 struct trace_iterator *iter = file->private_data;
3367 mutex_lock(&trace_types_lock);
3369 if (iter->trace->pipe_close)
3370 iter->trace->pipe_close(iter);
3372 mutex_unlock(&trace_types_lock);
3374 free_cpumask_var(iter->started);
3375 mutex_destroy(&iter->mutex);
3383 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3385 struct trace_iterator *iter = filp->private_data;
3387 if (trace_flags & TRACE_ITER_BLOCK) {
3389 * Always select as readable when in blocking mode
3391 return POLLIN | POLLRDNORM;
3393 if (!trace_empty(iter))
3394 return POLLIN | POLLRDNORM;
3395 poll_wait(filp, &trace_wait, poll_table);
3396 if (!trace_empty(iter))
3397 return POLLIN | POLLRDNORM;
3404 void default_wait_pipe(struct trace_iterator *iter)
3408 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3410 if (trace_empty(iter))
3413 finish_wait(&trace_wait, &wait);
3417 * This is a make-shift waitqueue.
3418 * A tracer might use this callback on some rare cases:
3420 * 1) the current tracer might hold the runqueue lock when it wakes up
3421 * a reader, hence a deadlock (sched, function, and function graph tracers)
3422 * 2) the function tracers, trace all functions, we don't want
3423 * the overhead of calling wake_up and friends
3424 * (and tracing them too)
3426 * Anyway, this is really very primitive wakeup.
3428 void poll_wait_pipe(struct trace_iterator *iter)
3430 set_current_state(TASK_INTERRUPTIBLE);
3431 /* sleep for 100 msecs, and try again. */
3432 schedule_timeout(HZ / 10);
3435 /* Must be called with trace_types_lock mutex held. */
3436 static int tracing_wait_pipe(struct file *filp)
3438 struct trace_iterator *iter = filp->private_data;
3440 while (trace_empty(iter)) {
3442 if ((filp->f_flags & O_NONBLOCK)) {
3446 mutex_unlock(&iter->mutex);
3448 iter->trace->wait_pipe(iter);
3450 mutex_lock(&iter->mutex);
3452 if (signal_pending(current))
3456 * We block until we read something and tracing is disabled.
3457 * We still block if tracing is disabled, but we have never
3458 * read anything. This allows a user to cat this file, and
3459 * then enable tracing. But after we have read something,
3460 * we give an EOF when tracing is again disabled.
3462 * iter->pos will be 0 if we haven't read anything.
3464 if (!tracer_enabled && iter->pos)
3475 tracing_read_pipe(struct file *filp, char __user *ubuf,
3476 size_t cnt, loff_t *ppos)
3478 struct trace_iterator *iter = filp->private_data;
3479 static struct tracer *old_tracer;
3482 /* return any leftover data */
3483 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3487 trace_seq_init(&iter->seq);
3489 /* copy the tracer to avoid using a global lock all around */
3490 mutex_lock(&trace_types_lock);
3491 if (unlikely(old_tracer != current_trace && current_trace)) {
3492 old_tracer = current_trace;
3493 *iter->trace = *current_trace;
3495 mutex_unlock(&trace_types_lock);
3498 * Avoid more than one consumer on a single file descriptor
3499 * This is just a matter of traces coherency, the ring buffer itself
3502 mutex_lock(&iter->mutex);
3503 if (iter->trace->read) {
3504 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3510 sret = tracing_wait_pipe(filp);
3514 /* stop when tracing is finished */
3515 if (trace_empty(iter)) {
3520 if (cnt >= PAGE_SIZE)
3521 cnt = PAGE_SIZE - 1;
3523 /* reset all but tr, trace, and overruns */
3524 memset(&iter->seq, 0,
3525 sizeof(struct trace_iterator) -
3526 offsetof(struct trace_iterator, seq));
3529 trace_event_read_lock();
3530 trace_access_lock(iter->cpu_file);
3531 while (trace_find_next_entry_inc(iter) != NULL) {
3532 enum print_line_t ret;
3533 int len = iter->seq.len;
3535 ret = print_trace_line(iter);
3536 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3537 /* don't print partial lines */
3538 iter->seq.len = len;
3541 if (ret != TRACE_TYPE_NO_CONSUME)
3542 trace_consume(iter);
3544 if (iter->seq.len >= cnt)
3548 * Setting the full flag means we reached the trace_seq buffer
3549 * size and we should leave by partial output condition above.
3550 * One of the trace_seq_* functions is not used properly.
3552 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3555 trace_access_unlock(iter->cpu_file);
3556 trace_event_read_unlock();
3558 /* Now copy what we have to the user */
3559 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3560 if (iter->seq.readpos >= iter->seq.len)
3561 trace_seq_init(&iter->seq);
3564 * If there was nothing to send to user, in spite of consuming trace
3565 * entries, go back to wait for more entries.
3571 mutex_unlock(&iter->mutex);
3576 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3577 struct pipe_buffer *buf)
3579 __free_page(buf->page);
3582 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3585 __free_page(spd->pages[idx]);
3588 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3590 .map = generic_pipe_buf_map,
3591 .unmap = generic_pipe_buf_unmap,
3592 .confirm = generic_pipe_buf_confirm,
3593 .release = tracing_pipe_buf_release,
3594 .steal = generic_pipe_buf_steal,
3595 .get = generic_pipe_buf_get,
3599 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3604 /* Seq buffer is page-sized, exactly what we need. */
3606 count = iter->seq.len;
3607 ret = print_trace_line(iter);
3608 count = iter->seq.len - count;
3611 iter->seq.len -= count;
3614 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3615 iter->seq.len -= count;
3619 if (ret != TRACE_TYPE_NO_CONSUME)
3620 trace_consume(iter);
3622 if (!trace_find_next_entry_inc(iter)) {
3632 static ssize_t tracing_splice_read_pipe(struct file *filp,
3634 struct pipe_inode_info *pipe,
3638 struct page *pages_def[PIPE_DEF_BUFFERS];
3639 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3640 struct trace_iterator *iter = filp->private_data;
3641 struct splice_pipe_desc spd = {
3643 .partial = partial_def,
3644 .nr_pages = 0, /* This gets updated below. */
3646 .ops = &tracing_pipe_buf_ops,
3647 .spd_release = tracing_spd_release_pipe,
3649 static struct tracer *old_tracer;
3654 if (splice_grow_spd(pipe, &spd))
3657 /* copy the tracer to avoid using a global lock all around */
3658 mutex_lock(&trace_types_lock);
3659 if (unlikely(old_tracer != current_trace && current_trace)) {
3660 old_tracer = current_trace;
3661 *iter->trace = *current_trace;
3663 mutex_unlock(&trace_types_lock);
3665 mutex_lock(&iter->mutex);
3667 if (iter->trace->splice_read) {
3668 ret = iter->trace->splice_read(iter, filp,
3669 ppos, pipe, len, flags);
3674 ret = tracing_wait_pipe(filp);
3678 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3683 trace_event_read_lock();
3684 trace_access_lock(iter->cpu_file);
3686 /* Fill as many pages as possible. */
3687 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3688 spd.pages[i] = alloc_page(GFP_KERNEL);
3692 rem = tracing_fill_pipe_page(rem, iter);
3694 /* Copy the data into the page, so we can start over. */
3695 ret = trace_seq_to_buffer(&iter->seq,
3696 page_address(spd.pages[i]),
3699 __free_page(spd.pages[i]);
3702 spd.partial[i].offset = 0;
3703 spd.partial[i].len = iter->seq.len;
3705 trace_seq_init(&iter->seq);
3708 trace_access_unlock(iter->cpu_file);
3709 trace_event_read_unlock();
3710 mutex_unlock(&iter->mutex);
3714 ret = splice_to_pipe(pipe, &spd);
3716 splice_shrink_spd(pipe, &spd);
3720 mutex_unlock(&iter->mutex);
3725 tracing_entries_read(struct file *filp, char __user *ubuf,
3726 size_t cnt, loff_t *ppos)
3728 struct trace_array *tr = filp->private_data;
3732 mutex_lock(&trace_types_lock);
3733 if (!ring_buffer_expanded)
3734 r = sprintf(buf, "%lu (expanded: %lu)\n",
3736 trace_buf_size >> 10);
3738 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3739 mutex_unlock(&trace_types_lock);
3741 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3745 tracing_entries_write(struct file *filp, const char __user *ubuf,
3746 size_t cnt, loff_t *ppos)
3751 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3755 /* must have at least 1 entry */
3759 /* value is in KB */
3762 ret = tracing_resize_ring_buffer(val);
3772 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3773 size_t cnt, loff_t *ppos)
3775 struct trace_array *tr = filp->private_data;
3778 unsigned long size = 0, expanded_size = 0;
3780 mutex_lock(&trace_types_lock);
3781 for_each_tracing_cpu(cpu) {
3782 size += tr->entries >> 10;
3783 if (!ring_buffer_expanded)
3784 expanded_size += trace_buf_size >> 10;
3786 if (ring_buffer_expanded)
3787 r = sprintf(buf, "%lu\n", size);
3789 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3790 mutex_unlock(&trace_types_lock);
3792 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3796 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3797 size_t cnt, loff_t *ppos)
3800 * There is no need to read what the user has written, this function
3801 * is just to make sure that there is no error when "echo" is used
3810 tracing_free_buffer_release(struct inode *inode, struct file *filp)
3812 /* disable tracing ? */
3813 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3815 /* resize the ring buffer to 0 */
3816 tracing_resize_ring_buffer(0);
3822 tracing_mark_write(struct file *filp, const char __user *ubuf,
3823 size_t cnt, loff_t *fpos)
3825 unsigned long addr = (unsigned long)ubuf;
3826 struct ring_buffer_event *event;
3827 struct ring_buffer *buffer;
3828 struct print_entry *entry;
3829 unsigned long irq_flags;
3830 struct page *pages[2];
3840 if (tracing_disabled)
3843 if (cnt > TRACE_BUF_SIZE)
3844 cnt = TRACE_BUF_SIZE;
3847 * Userspace is injecting traces into the kernel trace buffer.
3848 * We want to be as non intrusive as possible.
3849 * To do so, we do not want to allocate any special buffers
3850 * or take any locks, but instead write the userspace data
3851 * straight into the ring buffer.
3853 * First we need to pin the userspace buffer into memory,
3854 * which, most likely it is, because it just referenced it.
3855 * But there's no guarantee that it is. By using get_user_pages_fast()
3856 * and kmap_atomic/kunmap_atomic() we can get access to the
3857 * pages directly. We then write the data directly into the
3860 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
3862 /* check if we cross pages */
3863 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
3866 offset = addr & (PAGE_SIZE - 1);
3869 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
3870 if (ret < nr_pages) {
3872 put_page(pages[ret]);
3877 page1 = kmap_atomic(pages[0]);
3879 page2 = kmap_atomic(pages[1]);
3881 local_save_flags(irq_flags);
3882 size = sizeof(*entry) + cnt + 2; /* possible \n added */
3883 buffer = global_trace.buffer;
3884 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3885 irq_flags, preempt_count());
3887 /* Ring buffer disabled, return as if not open for write */
3892 entry = ring_buffer_event_data(event);
3893 entry->ip = _THIS_IP_;
3895 if (nr_pages == 2) {
3896 len = PAGE_SIZE - offset;
3897 memcpy(&entry->buf, page1 + offset, len);
3898 memcpy(&entry->buf[len], page2, cnt - len);
3900 memcpy(&entry->buf, page1 + offset, cnt);
3902 if (entry->buf[cnt - 1] != '\n') {
3903 entry->buf[cnt] = '\n';
3904 entry->buf[cnt + 1] = '\0';
3906 entry->buf[cnt] = '\0';
3908 ring_buffer_unlock_commit(buffer, event);
3916 kunmap_atomic(page2);
3917 kunmap_atomic(page1);
3918 while (nr_pages > 0)
3919 put_page(pages[--nr_pages]);
3924 static int tracing_clock_show(struct seq_file *m, void *v)
3928 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3930 "%s%s%s%s", i ? " " : "",
3931 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3932 i == trace_clock_id ? "]" : "");
3938 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3939 size_t cnt, loff_t *fpos)
3942 const char *clockstr;
3945 if (cnt >= sizeof(buf))
3948 if (copy_from_user(&buf, ubuf, cnt))
3953 clockstr = strstrip(buf);
3955 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3956 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3959 if (i == ARRAY_SIZE(trace_clocks))
3964 mutex_lock(&trace_types_lock);
3966 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3968 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3970 mutex_unlock(&trace_types_lock);
3977 static int tracing_clock_open(struct inode *inode, struct file *file)
3979 if (tracing_disabled)
3981 return single_open(file, tracing_clock_show, NULL);
3984 static const struct file_operations tracing_max_lat_fops = {
3985 .open = tracing_open_generic,
3986 .read = tracing_max_lat_read,
3987 .write = tracing_max_lat_write,
3988 .llseek = generic_file_llseek,
3991 static const struct file_operations tracing_ctrl_fops = {
3992 .open = tracing_open_generic,
3993 .read = tracing_ctrl_read,
3994 .write = tracing_ctrl_write,
3995 .llseek = generic_file_llseek,
3998 static const struct file_operations set_tracer_fops = {
3999 .open = tracing_open_generic,
4000 .read = tracing_set_trace_read,
4001 .write = tracing_set_trace_write,
4002 .llseek = generic_file_llseek,
4005 static const struct file_operations tracing_pipe_fops = {
4006 .open = tracing_open_pipe,
4007 .poll = tracing_poll_pipe,
4008 .read = tracing_read_pipe,
4009 .splice_read = tracing_splice_read_pipe,
4010 .release = tracing_release_pipe,
4011 .llseek = no_llseek,
4014 static const struct file_operations tracing_entries_fops = {
4015 .open = tracing_open_generic,
4016 .read = tracing_entries_read,
4017 .write = tracing_entries_write,
4018 .llseek = generic_file_llseek,
4021 static const struct file_operations tracing_total_entries_fops = {
4022 .open = tracing_open_generic,
4023 .read = tracing_total_entries_read,
4024 .llseek = generic_file_llseek,
4027 static const struct file_operations tracing_free_buffer_fops = {
4028 .write = tracing_free_buffer_write,
4029 .release = tracing_free_buffer_release,
4032 static const struct file_operations tracing_mark_fops = {
4033 .open = tracing_open_generic,
4034 .write = tracing_mark_write,
4035 .llseek = generic_file_llseek,
4038 static const struct file_operations trace_clock_fops = {
4039 .open = tracing_clock_open,
4041 .llseek = seq_lseek,
4042 .release = single_release,
4043 .write = tracing_clock_write,
4046 struct ftrace_buffer_info {
4047 struct trace_array *tr;
4053 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4055 int cpu = (int)(long)inode->i_private;
4056 struct ftrace_buffer_info *info;
4058 if (tracing_disabled)
4061 info = kzalloc(sizeof(*info), GFP_KERNEL);
4065 info->tr = &global_trace;
4068 /* Force reading ring buffer for first read */
4069 info->read = (unsigned int)-1;
4071 filp->private_data = info;
4073 return nonseekable_open(inode, filp);
4077 tracing_buffers_read(struct file *filp, char __user *ubuf,
4078 size_t count, loff_t *ppos)
4080 struct ftrace_buffer_info *info = filp->private_data;
4088 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
4092 /* Do we have previous read data to read? */
4093 if (info->read < PAGE_SIZE)
4096 trace_access_lock(info->cpu);
4097 ret = ring_buffer_read_page(info->tr->buffer,
4101 trace_access_unlock(info->cpu);
4108 size = PAGE_SIZE - info->read;
4112 ret = copy_to_user(ubuf, info->spare + info->read, size);
4123 static int tracing_buffers_release(struct inode *inode, struct file *file)
4125 struct ftrace_buffer_info *info = file->private_data;
4128 ring_buffer_free_read_page(info->tr->buffer, info->spare);
4135 struct ring_buffer *buffer;
4140 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4141 struct pipe_buffer *buf)
4143 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4148 ring_buffer_free_read_page(ref->buffer, ref->page);
4153 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
4154 struct pipe_buffer *buf)
4159 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4160 struct pipe_buffer *buf)
4162 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4167 /* Pipe buffer operations for a buffer. */
4168 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4170 .map = generic_pipe_buf_map,
4171 .unmap = generic_pipe_buf_unmap,
4172 .confirm = generic_pipe_buf_confirm,
4173 .release = buffer_pipe_buf_release,
4174 .steal = buffer_pipe_buf_steal,
4175 .get = buffer_pipe_buf_get,
4179 * Callback from splice_to_pipe(), if we need to release some pages
4180 * at the end of the spd in case we error'ed out in filling the pipe.
4182 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4184 struct buffer_ref *ref =
4185 (struct buffer_ref *)spd->partial[i].private;
4190 ring_buffer_free_read_page(ref->buffer, ref->page);
4192 spd->partial[i].private = 0;
4196 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4197 struct pipe_inode_info *pipe, size_t len,
4200 struct ftrace_buffer_info *info = file->private_data;
4201 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4202 struct page *pages_def[PIPE_DEF_BUFFERS];
4203 struct splice_pipe_desc spd = {
4205 .partial = partial_def,
4207 .ops = &buffer_pipe_buf_ops,
4208 .spd_release = buffer_spd_release,
4210 struct buffer_ref *ref;
4211 int entries, size, i;
4214 if (splice_grow_spd(pipe, &spd))
4217 if (*ppos & (PAGE_SIZE - 1)) {
4218 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
4223 if (len & (PAGE_SIZE - 1)) {
4224 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
4225 if (len < PAGE_SIZE) {
4232 trace_access_lock(info->cpu);
4233 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4235 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4239 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4244 ref->buffer = info->tr->buffer;
4245 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
4251 r = ring_buffer_read_page(ref->buffer, &ref->page,
4254 ring_buffer_free_read_page(ref->buffer, ref->page);
4260 * zero out any left over data, this is going to
4263 size = ring_buffer_page_len(ref->page);
4264 if (size < PAGE_SIZE)
4265 memset(ref->page + size, 0, PAGE_SIZE - size);
4267 page = virt_to_page(ref->page);
4269 spd.pages[i] = page;
4270 spd.partial[i].len = PAGE_SIZE;
4271 spd.partial[i].offset = 0;
4272 spd.partial[i].private = (unsigned long)ref;
4276 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4279 trace_access_unlock(info->cpu);
4282 /* did we read anything? */
4283 if (!spd.nr_pages) {
4284 if (flags & SPLICE_F_NONBLOCK)
4292 ret = splice_to_pipe(pipe, &spd);
4293 splice_shrink_spd(pipe, &spd);
4298 static const struct file_operations tracing_buffers_fops = {
4299 .open = tracing_buffers_open,
4300 .read = tracing_buffers_read,
4301 .release = tracing_buffers_release,
4302 .splice_read = tracing_buffers_splice_read,
4303 .llseek = no_llseek,
4307 tracing_stats_read(struct file *filp, char __user *ubuf,
4308 size_t count, loff_t *ppos)
4310 unsigned long cpu = (unsigned long)filp->private_data;
4311 struct trace_array *tr = &global_trace;
4312 struct trace_seq *s;
4314 unsigned long long t;
4315 unsigned long usec_rem;
4317 s = kmalloc(sizeof(*s), GFP_KERNEL);
4323 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4324 trace_seq_printf(s, "entries: %ld\n", cnt);
4326 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4327 trace_seq_printf(s, "overrun: %ld\n", cnt);
4329 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4330 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4332 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4333 trace_seq_printf(s, "bytes: %ld\n", cnt);
4335 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4336 usec_rem = do_div(t, USEC_PER_SEC);
4337 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
4339 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4340 usec_rem = do_div(t, USEC_PER_SEC);
4341 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4343 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4350 static const struct file_operations tracing_stats_fops = {
4351 .open = tracing_open_generic,
4352 .read = tracing_stats_read,
4353 .llseek = generic_file_llseek,
4356 #ifdef CONFIG_DYNAMIC_FTRACE
4358 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4364 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4365 size_t cnt, loff_t *ppos)
4367 static char ftrace_dyn_info_buffer[1024];
4368 static DEFINE_MUTEX(dyn_info_mutex);
4369 unsigned long *p = filp->private_data;
4370 char *buf = ftrace_dyn_info_buffer;
4371 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4374 mutex_lock(&dyn_info_mutex);
4375 r = sprintf(buf, "%ld ", *p);
4377 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4380 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4382 mutex_unlock(&dyn_info_mutex);
4387 static const struct file_operations tracing_dyn_info_fops = {
4388 .open = tracing_open_generic,
4389 .read = tracing_read_dyn_info,
4390 .llseek = generic_file_llseek,
4394 static struct dentry *d_tracer;
4396 struct dentry *tracing_init_dentry(void)
4403 if (!debugfs_initialized())
4406 d_tracer = debugfs_create_dir("tracing", NULL);
4408 if (!d_tracer && !once) {
4410 pr_warning("Could not create debugfs directory 'tracing'\n");
4417 static struct dentry *d_percpu;
4419 struct dentry *tracing_dentry_percpu(void)
4422 struct dentry *d_tracer;
4427 d_tracer = tracing_init_dentry();
4432 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4434 if (!d_percpu && !once) {
4436 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4443 static void tracing_init_debugfs_percpu(long cpu)
4445 struct dentry *d_percpu = tracing_dentry_percpu();
4446 struct dentry *d_cpu;
4447 char cpu_dir[30]; /* 30 characters should be more than enough */
4449 snprintf(cpu_dir, 30, "cpu%ld", cpu);
4450 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4452 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4456 /* per cpu trace_pipe */
4457 trace_create_file("trace_pipe", 0444, d_cpu,
4458 (void *) cpu, &tracing_pipe_fops);
4461 trace_create_file("trace", 0644, d_cpu,
4462 (void *) cpu, &tracing_fops);
4464 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4465 (void *) cpu, &tracing_buffers_fops);
4467 trace_create_file("stats", 0444, d_cpu,
4468 (void *) cpu, &tracing_stats_fops);
4471 #ifdef CONFIG_FTRACE_SELFTEST
4472 /* Let selftest have access to static functions in this file */
4473 #include "trace_selftest.c"
4476 struct trace_option_dentry {
4477 struct tracer_opt *opt;
4478 struct tracer_flags *flags;
4479 struct dentry *entry;
4483 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4486 struct trace_option_dentry *topt = filp->private_data;
4489 if (topt->flags->val & topt->opt->bit)
4494 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4498 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4501 struct trace_option_dentry *topt = filp->private_data;
4505 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4509 if (val != 0 && val != 1)
4512 if (!!(topt->flags->val & topt->opt->bit) != val) {
4513 mutex_lock(&trace_types_lock);
4514 ret = __set_tracer_option(current_trace, topt->flags,
4516 mutex_unlock(&trace_types_lock);
4527 static const struct file_operations trace_options_fops = {
4528 .open = tracing_open_generic,
4529 .read = trace_options_read,
4530 .write = trace_options_write,
4531 .llseek = generic_file_llseek,
4535 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4538 long index = (long)filp->private_data;
4541 if (trace_flags & (1 << index))
4546 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4550 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4553 long index = (long)filp->private_data;
4557 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4561 if (val != 0 && val != 1)
4563 set_tracer_flags(1 << index, val);
4570 static const struct file_operations trace_options_core_fops = {
4571 .open = tracing_open_generic,
4572 .read = trace_options_core_read,
4573 .write = trace_options_core_write,
4574 .llseek = generic_file_llseek,
4577 struct dentry *trace_create_file(const char *name,
4579 struct dentry *parent,
4581 const struct file_operations *fops)
4585 ret = debugfs_create_file(name, mode, parent, data, fops);
4587 pr_warning("Could not create debugfs '%s' entry\n", name);
4593 static struct dentry *trace_options_init_dentry(void)
4595 struct dentry *d_tracer;
4596 static struct dentry *t_options;
4601 d_tracer = tracing_init_dentry();
4605 t_options = debugfs_create_dir("options", d_tracer);
4607 pr_warning("Could not create debugfs directory 'options'\n");
4615 create_trace_option_file(struct trace_option_dentry *topt,
4616 struct tracer_flags *flags,
4617 struct tracer_opt *opt)
4619 struct dentry *t_options;
4621 t_options = trace_options_init_dentry();
4625 topt->flags = flags;
4628 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4629 &trace_options_fops);
4633 static struct trace_option_dentry *
4634 create_trace_option_files(struct tracer *tracer)
4636 struct trace_option_dentry *topts;
4637 struct tracer_flags *flags;
4638 struct tracer_opt *opts;
4644 flags = tracer->flags;
4646 if (!flags || !flags->opts)
4651 for (cnt = 0; opts[cnt].name; cnt++)
4654 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4658 for (cnt = 0; opts[cnt].name; cnt++)
4659 create_trace_option_file(&topts[cnt], flags,
4666 destroy_trace_option_files(struct trace_option_dentry *topts)
4673 for (cnt = 0; topts[cnt].opt; cnt++) {
4674 if (topts[cnt].entry)
4675 debugfs_remove(topts[cnt].entry);
4681 static struct dentry *
4682 create_trace_option_core_file(const char *option, long index)
4684 struct dentry *t_options;
4686 t_options = trace_options_init_dentry();
4690 return trace_create_file(option, 0644, t_options, (void *)index,
4691 &trace_options_core_fops);
4694 static __init void create_trace_options_dir(void)
4696 struct dentry *t_options;
4699 t_options = trace_options_init_dentry();
4703 for (i = 0; trace_options[i]; i++)
4704 create_trace_option_core_file(trace_options[i], i);
4708 rb_simple_read(struct file *filp, char __user *ubuf,
4709 size_t cnt, loff_t *ppos)
4711 struct ring_buffer *buffer = filp->private_data;
4716 r = ring_buffer_record_is_on(buffer);
4720 r = sprintf(buf, "%d\n", r);
4722 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4726 rb_simple_write(struct file *filp, const char __user *ubuf,
4727 size_t cnt, loff_t *ppos)
4729 struct ring_buffer *buffer = filp->private_data;
4733 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4739 ring_buffer_record_on(buffer);
4741 ring_buffer_record_off(buffer);
4749 static const struct file_operations rb_simple_fops = {
4750 .open = tracing_open_generic,
4751 .read = rb_simple_read,
4752 .write = rb_simple_write,
4753 .llseek = default_llseek,
4756 static __init int tracer_init_debugfs(void)
4758 struct dentry *d_tracer;
4761 trace_access_lock_init();
4763 d_tracer = tracing_init_dentry();
4765 trace_create_file("tracing_enabled", 0644, d_tracer,
4766 &global_trace, &tracing_ctrl_fops);
4768 trace_create_file("trace_options", 0644, d_tracer,
4769 NULL, &tracing_iter_fops);
4771 trace_create_file("tracing_cpumask", 0644, d_tracer,
4772 NULL, &tracing_cpumask_fops);
4774 trace_create_file("trace", 0644, d_tracer,
4775 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4777 trace_create_file("available_tracers", 0444, d_tracer,
4778 &global_trace, &show_traces_fops);
4780 trace_create_file("current_tracer", 0644, d_tracer,
4781 &global_trace, &set_tracer_fops);
4783 #ifdef CONFIG_TRACER_MAX_TRACE
4784 trace_create_file("tracing_max_latency", 0644, d_tracer,
4785 &tracing_max_latency, &tracing_max_lat_fops);
4788 trace_create_file("tracing_thresh", 0644, d_tracer,
4789 &tracing_thresh, &tracing_max_lat_fops);
4791 trace_create_file("README", 0444, d_tracer,
4792 NULL, &tracing_readme_fops);
4794 trace_create_file("trace_pipe", 0444, d_tracer,
4795 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4797 trace_create_file("buffer_size_kb", 0644, d_tracer,
4798 &global_trace, &tracing_entries_fops);
4800 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
4801 &global_trace, &tracing_total_entries_fops);
4803 trace_create_file("free_buffer", 0644, d_tracer,
4804 &global_trace, &tracing_free_buffer_fops);
4806 trace_create_file("trace_marker", 0220, d_tracer,
4807 NULL, &tracing_mark_fops);
4809 trace_create_file("saved_cmdlines", 0444, d_tracer,
4810 NULL, &tracing_saved_cmdlines_fops);
4812 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4815 trace_create_file("tracing_on", 0644, d_tracer,
4816 global_trace.buffer, &rb_simple_fops);
4818 #ifdef CONFIG_DYNAMIC_FTRACE
4819 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4820 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4823 create_trace_options_dir();
4825 for_each_tracing_cpu(cpu)
4826 tracing_init_debugfs_percpu(cpu);
4831 static int trace_panic_handler(struct notifier_block *this,
4832 unsigned long event, void *unused)
4834 if (ftrace_dump_on_oops)
4835 ftrace_dump(ftrace_dump_on_oops);
4839 static struct notifier_block trace_panic_notifier = {
4840 .notifier_call = trace_panic_handler,
4842 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4845 static int trace_die_handler(struct notifier_block *self,
4851 if (ftrace_dump_on_oops)
4852 ftrace_dump(ftrace_dump_on_oops);
4860 static struct notifier_block trace_die_notifier = {
4861 .notifier_call = trace_die_handler,
4866 * printk is set to max of 1024, we really don't need it that big.
4867 * Nothing should be printing 1000 characters anyway.
4869 #define TRACE_MAX_PRINT 1000
4872 * Define here KERN_TRACE so that we have one place to modify
4873 * it if we decide to change what log level the ftrace dump
4876 #define KERN_TRACE KERN_EMERG
4879 trace_printk_seq(struct trace_seq *s)
4881 /* Probably should print a warning here. */
4885 /* should be zero ended, but we are paranoid. */
4886 s->buffer[s->len] = 0;
4888 printk(KERN_TRACE "%s", s->buffer);
4893 void trace_init_global_iter(struct trace_iterator *iter)
4895 iter->tr = &global_trace;
4896 iter->trace = current_trace;
4897 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4901 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
4903 static arch_spinlock_t ftrace_dump_lock =
4904 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
4905 /* use static because iter can be a bit big for the stack */
4906 static struct trace_iterator iter;
4907 unsigned int old_userobj;
4908 static int dump_ran;
4909 unsigned long flags;
4913 local_irq_save(flags);
4914 arch_spin_lock(&ftrace_dump_lock);
4922 /* Did function tracer already get disabled? */
4923 if (ftrace_is_dead()) {
4924 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
4925 printk("# MAY BE MISSING FUNCTION EVENTS\n");
4928 if (disable_tracing)
4931 trace_init_global_iter(&iter);
4933 for_each_tracing_cpu(cpu) {
4934 atomic_inc(&iter.tr->data[cpu]->disabled);
4937 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4939 /* don't look at user memory in panic mode */
4940 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4942 /* Simulate the iterator */
4943 iter.tr = &global_trace;
4944 iter.trace = current_trace;
4946 switch (oops_dump_mode) {
4948 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4951 iter.cpu_file = raw_smp_processor_id();
4956 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4957 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4960 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4963 * We need to stop all tracing on all CPUS to read the
4964 * the next buffer. This is a bit expensive, but is
4965 * not done often. We fill all what we can read,
4966 * and then release the locks again.
4969 while (!trace_empty(&iter)) {
4972 printk(KERN_TRACE "---------------------------------\n");
4976 /* reset all but tr, trace, and overruns */
4977 memset(&iter.seq, 0,
4978 sizeof(struct trace_iterator) -
4979 offsetof(struct trace_iterator, seq));
4980 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4983 if (trace_find_next_entry_inc(&iter) != NULL) {
4986 ret = print_trace_line(&iter);
4987 if (ret != TRACE_TYPE_NO_CONSUME)
4988 trace_consume(&iter);
4990 touch_nmi_watchdog();
4992 trace_printk_seq(&iter.seq);
4996 printk(KERN_TRACE " (ftrace buffer empty)\n");
4998 printk(KERN_TRACE "---------------------------------\n");
5001 /* Re-enable tracing if requested */
5002 if (!disable_tracing) {
5003 trace_flags |= old_userobj;
5005 for_each_tracing_cpu(cpu) {
5006 atomic_dec(&iter.tr->data[cpu]->disabled);
5012 arch_spin_unlock(&ftrace_dump_lock);
5013 local_irq_restore(flags);
5016 /* By default: disable tracing after the dump */
5017 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
5019 __ftrace_dump(true, oops_dump_mode);
5021 EXPORT_SYMBOL_GPL(ftrace_dump);
5023 __init static int tracer_alloc_buffers(void)
5026 enum ring_buffer_flags rb_flags;
5031 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5034 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5035 goto out_free_buffer_mask;
5037 /* Only allocate trace_printk buffers if a trace_printk exists */
5038 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5039 trace_printk_init_buffers();
5041 /* To save memory, keep the ring buffer size to its minimum */
5042 if (ring_buffer_expanded)
5043 ring_buf_size = trace_buf_size;
5047 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5049 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5050 cpumask_copy(tracing_cpumask, cpu_all_mask);
5052 /* TODO: make the number of buffers hot pluggable with CPUS */
5053 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
5054 if (!global_trace.buffer) {
5055 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5057 goto out_free_cpumask;
5059 global_trace.entries = ring_buffer_size(global_trace.buffer);
5060 if (global_trace.buffer_disabled)
5064 #ifdef CONFIG_TRACER_MAX_TRACE
5065 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
5066 if (!max_tr.buffer) {
5067 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
5069 ring_buffer_free(global_trace.buffer);
5070 goto out_free_cpumask;
5075 /* Allocate the first page for all buffers */
5076 for_each_tracing_cpu(i) {
5077 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
5078 max_tr.data[i] = &per_cpu(max_tr_data, i);
5081 trace_init_cmdlines();
5083 register_tracer(&nop_trace);
5084 current_trace = &nop_trace;
5085 /* All seems OK, enable tracing */
5086 tracing_disabled = 0;
5088 atomic_notifier_chain_register(&panic_notifier_list,
5089 &trace_panic_notifier);
5091 register_die_notifier(&trace_die_notifier);
5096 free_cpumask_var(tracing_cpumask);
5097 out_free_buffer_mask:
5098 free_cpumask_var(tracing_buffer_mask);
5103 __init static int clear_boot_tracer(void)
5106 * The default tracer at boot buffer is an init section.
5107 * This function is called in lateinit. If we did not
5108 * find the boot tracer, then clear it out, to prevent
5109 * later registration from accessing the buffer that is
5110 * about to be freed.
5112 if (!default_bootup_tracer)
5115 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5116 default_bootup_tracer);
5117 default_bootup_tracer = NULL;
5122 early_initcall(tracer_alloc_buffers);
5123 fs_initcall(tracer_init_debugfs);
5124 late_initcall(clear_boot_tracer);