2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
42 #include "trace_output.h"
45 * On boot up, the ring buffer is set to the minimum size, so that
46 * we do not waste memory on systems that are not using tracing.
48 int ring_buffer_expanded;
51 * We need to change this state when a selftest is running.
52 * A selftest will lurk into the ring-buffer to count the
53 * entries inserted during the selftest although some concurrent
54 * insertions into the ring-buffer such as trace_printk could occurred
55 * at the same time, giving false positive or negative results.
57 static bool __read_mostly tracing_selftest_running;
60 * If a tracer is running, we do not want to run SELFTEST.
62 bool __read_mostly tracing_selftest_disabled;
64 /* For tracers that don't implement custom flags */
65 static struct tracer_opt dummy_tracer_opt[] = {
69 static struct tracer_flags dummy_tracer_flags = {
71 .opts = dummy_tracer_opt
74 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
80 * Kill all tracing for good (never come back).
81 * It is initialized to 1 but will turn to zero if the initialization
82 * of the tracer is successful. But that is the only place that sets
85 static int tracing_disabled = 1;
87 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
89 static inline void ftrace_disable_cpu(void)
92 __this_cpu_inc(ftrace_cpu_disabled);
95 static inline void ftrace_enable_cpu(void)
97 __this_cpu_dec(ftrace_cpu_disabled);
101 cpumask_var_t __read_mostly tracing_buffer_mask;
104 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
106 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
107 * is set, then ftrace_dump is called. This will output the contents
108 * of the ftrace buffers to the console. This is very useful for
109 * capturing traces that lead to crashes and outputing it to a
112 * It is default off, but you can enable it with either specifying
113 * "ftrace_dump_on_oops" in the kernel command line, or setting
114 * /proc/sys/kernel/ftrace_dump_on_oops
115 * Set 1 if you want to dump buffers of all CPUs
116 * Set 2 if you want to dump the buffer of the CPU that triggered oops
119 enum ftrace_dump_mode ftrace_dump_on_oops;
121 static int tracing_set_tracer(const char *buf);
123 #define MAX_TRACER_SIZE 100
124 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
125 static char *default_bootup_tracer;
127 static int __init set_cmdline_ftrace(char *str)
129 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
130 default_bootup_tracer = bootup_tracer_buf;
131 /* We are using ftrace early, expand it */
132 ring_buffer_expanded = 1;
135 __setup("ftrace=", set_cmdline_ftrace);
137 static int __init set_ftrace_dump_on_oops(char *str)
139 if (*str++ != '=' || !*str) {
140 ftrace_dump_on_oops = DUMP_ALL;
144 if (!strcmp("orig_cpu", str)) {
145 ftrace_dump_on_oops = DUMP_ORIG;
151 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
153 unsigned long long ns2usecs(cycle_t nsec)
161 * The global_trace is the descriptor that holds the tracing
162 * buffers for the live tracing. For each CPU, it contains
163 * a link list of pages that will store trace entries. The
164 * page descriptor of the pages in the memory is used to hold
165 * the link list by linking the lru item in the page descriptor
166 * to each of the pages in the buffer per CPU.
168 * For each active CPU there is a data field that holds the
169 * pages for the buffer for that CPU. Each CPU has the same number
170 * of pages allocated for its buffer.
172 static struct trace_array global_trace;
174 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
176 int filter_current_check_discard(struct ring_buffer *buffer,
177 struct ftrace_event_call *call, void *rec,
178 struct ring_buffer_event *event)
180 return filter_check_discard(call, rec, buffer, event);
182 EXPORT_SYMBOL_GPL(filter_current_check_discard);
184 cycle_t ftrace_now(int cpu)
188 /* Early boot up does not have a buffer yet */
189 if (!global_trace.buffer)
190 return trace_clock_local();
192 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
193 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
199 * The max_tr is used to snapshot the global_trace when a maximum
200 * latency is reached. Some tracers will use this to store a maximum
201 * trace while it continues examining live traces.
203 * The buffers for the max_tr are set up the same as the global_trace.
204 * When a snapshot is taken, the link list of the max_tr is swapped
205 * with the link list of the global_trace and the buffers are reset for
206 * the global_trace so the tracing can continue.
208 static struct trace_array max_tr;
210 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
212 /* tracer_enabled is used to toggle activation of a tracer */
213 static int tracer_enabled = 1;
216 * tracing_is_enabled - return tracer_enabled status
218 * This function is used by other tracers to know the status
219 * of the tracer_enabled flag. Tracers may use this function
220 * to know if it should enable their features when starting
221 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
223 int tracing_is_enabled(void)
225 return tracer_enabled;
229 * trace_buf_size is the size in bytes that is allocated
230 * for a buffer. Note, the number of bytes is always rounded
233 * This number is purposely set to a low number of 16384.
234 * If the dump on oops happens, it will be much appreciated
235 * to not have to wait for all that output. Anyway this can be
236 * boot time and run time configurable.
238 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
240 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
242 /* trace_types holds a link list of available tracers. */
243 static struct tracer *trace_types __read_mostly;
245 /* current_trace points to the tracer that is currently active */
246 static struct tracer *current_trace __read_mostly;
249 * trace_types_lock is used to protect the trace_types list.
251 static DEFINE_MUTEX(trace_types_lock);
254 * serialize the access of the ring buffer
256 * ring buffer serializes readers, but it is low level protection.
257 * The validity of the events (which returns by ring_buffer_peek() ..etc)
258 * are not protected by ring buffer.
260 * The content of events may become garbage if we allow other process consumes
261 * these events concurrently:
262 * A) the page of the consumed events may become a normal page
263 * (not reader page) in ring buffer, and this page will be rewrited
264 * by events producer.
265 * B) The page of the consumed events may become a page for splice_read,
266 * and this page will be returned to system.
268 * These primitives allow multi process access to different cpu ring buffer
271 * These primitives don't distinguish read-only and read-consume access.
272 * Multi read-only access are also serialized.
276 static DECLARE_RWSEM(all_cpu_access_lock);
277 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
279 static inline void trace_access_lock(int cpu)
281 if (cpu == TRACE_PIPE_ALL_CPU) {
282 /* gain it for accessing the whole ring buffer. */
283 down_write(&all_cpu_access_lock);
285 /* gain it for accessing a cpu ring buffer. */
287 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
288 down_read(&all_cpu_access_lock);
290 /* Secondly block other access to this @cpu ring buffer. */
291 mutex_lock(&per_cpu(cpu_access_lock, cpu));
295 static inline void trace_access_unlock(int cpu)
297 if (cpu == TRACE_PIPE_ALL_CPU) {
298 up_write(&all_cpu_access_lock);
300 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
301 up_read(&all_cpu_access_lock);
305 static inline void trace_access_lock_init(void)
309 for_each_possible_cpu(cpu)
310 mutex_init(&per_cpu(cpu_access_lock, cpu));
315 static DEFINE_MUTEX(access_lock);
317 static inline void trace_access_lock(int cpu)
320 mutex_lock(&access_lock);
323 static inline void trace_access_unlock(int cpu)
326 mutex_unlock(&access_lock);
329 static inline void trace_access_lock_init(void)
335 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
336 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
338 /* trace_flags holds trace_options default values */
339 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
340 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
341 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE;
343 static int trace_stop_count;
344 static DEFINE_SPINLOCK(tracing_start_lock);
346 static void wakeup_work_handler(struct work_struct *work)
348 wake_up(&trace_wait);
351 static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
354 * trace_wake_up - wake up tasks waiting for trace input
356 * Schedules a delayed work to wake up any task that is blocked on the
357 * trace_wait queue. These is used with trace_poll for tasks polling the
360 void trace_wake_up(void)
362 const unsigned long delay = msecs_to_jiffies(2);
364 if (trace_flags & TRACE_ITER_BLOCK)
366 schedule_delayed_work(&wakeup_work, delay);
369 static int __init set_buf_size(char *str)
371 unsigned long buf_size;
375 buf_size = memparse(str, &str);
376 /* nr_entries can not be zero */
379 trace_buf_size = buf_size;
382 __setup("trace_buf_size=", set_buf_size);
384 static int __init set_tracing_thresh(char *str)
386 unsigned long threshhold;
391 ret = strict_strtoul(str, 0, &threshhold);
394 tracing_thresh = threshhold * 1000;
397 __setup("tracing_thresh=", set_tracing_thresh);
399 unsigned long nsecs_to_usecs(unsigned long nsecs)
404 /* These must match the bit postions in trace_iterator_flags */
405 static const char *trace_options[] = {
436 { trace_clock_local, "local" },
437 { trace_clock_global, "global" },
443 * trace_parser_get_init - gets the buffer for trace parser
445 int trace_parser_get_init(struct trace_parser *parser, int size)
447 memset(parser, 0, sizeof(*parser));
449 parser->buffer = kmalloc(size, GFP_KERNEL);
458 * trace_parser_put - frees the buffer for trace parser
460 void trace_parser_put(struct trace_parser *parser)
462 kfree(parser->buffer);
466 * trace_get_user - reads the user input string separated by space
467 * (matched by isspace(ch))
469 * For each string found the 'struct trace_parser' is updated,
470 * and the function returns.
472 * Returns number of bytes read.
474 * See kernel/trace/trace.h for 'struct trace_parser' details.
476 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
477 size_t cnt, loff_t *ppos)
484 trace_parser_clear(parser);
486 ret = get_user(ch, ubuf++);
494 * The parser is not finished with the last write,
495 * continue reading the user input without skipping spaces.
498 /* skip white space */
499 while (cnt && isspace(ch)) {
500 ret = get_user(ch, ubuf++);
507 /* only spaces were written */
517 /* read the non-space input */
518 while (cnt && !isspace(ch)) {
519 if (parser->idx < parser->size - 1)
520 parser->buffer[parser->idx++] = ch;
525 ret = get_user(ch, ubuf++);
532 /* We either got finished input or we have to wait for another call. */
534 parser->buffer[parser->idx] = 0;
535 parser->cont = false;
538 parser->buffer[parser->idx++] = ch;
548 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
556 if (s->len <= s->readpos)
559 len = s->len - s->readpos;
562 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
572 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
577 if (s->len <= s->readpos)
580 len = s->len - s->readpos;
583 ret = memcpy(buf, s->buffer + s->readpos, cnt);
592 * ftrace_max_lock is used to protect the swapping of buffers
593 * when taking a max snapshot. The buffers themselves are
594 * protected by per_cpu spinlocks. But the action of the swap
595 * needs its own lock.
597 * This is defined as a arch_spinlock_t in order to help
598 * with performance when lockdep debugging is enabled.
600 * It is also used in other places outside the update_max_tr
601 * so it needs to be defined outside of the
602 * CONFIG_TRACER_MAX_TRACE.
604 static arch_spinlock_t ftrace_max_lock =
605 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
607 unsigned long __read_mostly tracing_thresh;
609 #ifdef CONFIG_TRACER_MAX_TRACE
610 unsigned long __read_mostly tracing_max_latency;
613 * Copy the new maximum trace into the separate maximum-trace
614 * structure. (this way the maximum trace is permanently saved,
615 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
618 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
620 struct trace_array_cpu *data = tr->data[cpu];
621 struct trace_array_cpu *max_data;
624 max_tr.time_start = data->preempt_timestamp;
626 max_data = max_tr.data[cpu];
627 max_data->saved_latency = tracing_max_latency;
628 max_data->critical_start = data->critical_start;
629 max_data->critical_end = data->critical_end;
631 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
632 max_data->pid = tsk->pid;
633 max_data->uid = task_uid(tsk);
634 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
635 max_data->policy = tsk->policy;
636 max_data->rt_priority = tsk->rt_priority;
638 /* record this tasks comm */
639 tracing_record_cmdline(tsk);
643 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
645 * @tsk: the task with the latency
646 * @cpu: The cpu that initiated the trace.
648 * Flip the buffers between the @tr and the max_tr and record information
649 * about which task was the cause of this latency.
652 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
654 struct ring_buffer *buf = tr->buffer;
656 if (trace_stop_count)
659 WARN_ON_ONCE(!irqs_disabled());
660 if (!current_trace->use_max_tr) {
664 arch_spin_lock(&ftrace_max_lock);
666 tr->buffer = max_tr.buffer;
669 __update_max_tr(tr, tsk, cpu);
670 arch_spin_unlock(&ftrace_max_lock);
674 * update_max_tr_single - only copy one trace over, and reset the rest
676 * @tsk - task with the latency
677 * @cpu - the cpu of the buffer to copy.
679 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
682 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
686 if (trace_stop_count)
689 WARN_ON_ONCE(!irqs_disabled());
690 if (!current_trace->use_max_tr) {
695 arch_spin_lock(&ftrace_max_lock);
697 ftrace_disable_cpu();
699 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
703 * We failed to swap the buffer due to a commit taking
704 * place on this CPU. We fail to record, but we reset
705 * the max trace buffer (no one writes directly to it)
706 * and flag that it failed.
708 trace_array_printk(&max_tr, _THIS_IP_,
709 "Failed to swap buffers due to commit in progress\n");
714 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
716 __update_max_tr(tr, tsk, cpu);
717 arch_spin_unlock(&ftrace_max_lock);
719 #endif /* CONFIG_TRACER_MAX_TRACE */
722 * register_tracer - register a tracer with the ftrace system.
723 * @type - the plugin for the tracer
725 * Register a new plugin tracer.
727 int register_tracer(struct tracer *type)
728 __releases(kernel_lock)
729 __acquires(kernel_lock)
735 pr_info("Tracer must have a name\n");
739 if (strlen(type->name) >= MAX_TRACER_SIZE) {
740 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
744 mutex_lock(&trace_types_lock);
746 tracing_selftest_running = true;
748 for (t = trace_types; t; t = t->next) {
749 if (strcmp(type->name, t->name) == 0) {
751 pr_info("Tracer %s already registered\n",
759 type->set_flag = &dummy_set_flag;
761 type->flags = &dummy_tracer_flags;
763 if (!type->flags->opts)
764 type->flags->opts = dummy_tracer_opt;
765 if (!type->wait_pipe)
766 type->wait_pipe = default_wait_pipe;
769 #ifdef CONFIG_FTRACE_STARTUP_TEST
770 if (type->selftest && !tracing_selftest_disabled) {
771 struct tracer *saved_tracer = current_trace;
772 struct trace_array *tr = &global_trace;
775 * Run a selftest on this tracer.
776 * Here we reset the trace buffer, and set the current
777 * tracer to be this tracer. The tracer can then run some
778 * internal tracing to verify that everything is in order.
779 * If we fail, we do not register this tracer.
781 tracing_reset_online_cpus(tr);
783 current_trace = type;
785 /* If we expanded the buffers, make sure the max is expanded too */
786 if (ring_buffer_expanded && type->use_max_tr)
787 ring_buffer_resize(max_tr.buffer, trace_buf_size);
789 /* the test is responsible for initializing and enabling */
790 pr_info("Testing tracer %s: ", type->name);
791 ret = type->selftest(type, tr);
792 /* the test is responsible for resetting too */
793 current_trace = saved_tracer;
795 printk(KERN_CONT "FAILED!\n");
798 /* Only reset on passing, to avoid touching corrupted buffers */
799 tracing_reset_online_cpus(tr);
801 /* Shrink the max buffer again */
802 if (ring_buffer_expanded && type->use_max_tr)
803 ring_buffer_resize(max_tr.buffer, 1);
805 printk(KERN_CONT "PASSED\n");
809 type->next = trace_types;
813 tracing_selftest_running = false;
814 mutex_unlock(&trace_types_lock);
816 if (ret || !default_bootup_tracer)
819 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
822 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
823 /* Do we want this tracer to start on bootup? */
824 tracing_set_tracer(type->name);
825 default_bootup_tracer = NULL;
826 /* disable other selftests, since this will break it. */
827 tracing_selftest_disabled = 1;
828 #ifdef CONFIG_FTRACE_STARTUP_TEST
829 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
837 void unregister_tracer(struct tracer *type)
841 mutex_lock(&trace_types_lock);
842 for (t = &trace_types; *t; t = &(*t)->next) {
846 pr_info("Tracer %s not registered\n", type->name);
852 if (type == current_trace && tracer_enabled) {
855 if (current_trace->stop)
856 current_trace->stop(&global_trace);
857 current_trace = &nop_trace;
860 mutex_unlock(&trace_types_lock);
863 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
865 ftrace_disable_cpu();
866 ring_buffer_reset_cpu(buffer, cpu);
870 void tracing_reset(struct trace_array *tr, int cpu)
872 struct ring_buffer *buffer = tr->buffer;
874 ring_buffer_record_disable(buffer);
876 /* Make sure all commits have finished */
878 __tracing_reset(buffer, cpu);
880 ring_buffer_record_enable(buffer);
883 void tracing_reset_online_cpus(struct trace_array *tr)
885 struct ring_buffer *buffer = tr->buffer;
888 ring_buffer_record_disable(buffer);
890 /* Make sure all commits have finished */
893 tr->time_start = ftrace_now(tr->cpu);
895 for_each_online_cpu(cpu)
896 __tracing_reset(buffer, cpu);
898 ring_buffer_record_enable(buffer);
901 void tracing_reset_current(int cpu)
903 tracing_reset(&global_trace, cpu);
906 void tracing_reset_current_online_cpus(void)
908 tracing_reset_online_cpus(&global_trace);
911 #define SAVED_CMDLINES 128
912 #define NO_CMDLINE_MAP UINT_MAX
913 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
914 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
915 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
916 static int cmdline_idx;
917 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
919 /* temporary disable recording */
920 static atomic_t trace_record_cmdline_disabled __read_mostly;
922 static void trace_init_cmdlines(void)
924 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
925 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
929 int is_tracing_stopped(void)
931 return trace_stop_count;
935 * ftrace_off_permanent - disable all ftrace code permanently
937 * This should only be called when a serious anomally has
938 * been detected. This will turn off the function tracing,
939 * ring buffers, and other tracing utilites. It takes no
940 * locks and can be called from any context.
942 void ftrace_off_permanent(void)
944 tracing_disabled = 1;
946 tracing_off_permanent();
950 * tracing_start - quick start of the tracer
952 * If tracing is enabled but was stopped by tracing_stop,
953 * this will start the tracer back up.
955 void tracing_start(void)
957 struct ring_buffer *buffer;
960 if (tracing_disabled)
963 spin_lock_irqsave(&tracing_start_lock, flags);
964 if (--trace_stop_count) {
965 if (trace_stop_count < 0) {
966 /* Someone screwed up their debugging */
968 trace_stop_count = 0;
973 /* Prevent the buffers from switching */
974 arch_spin_lock(&ftrace_max_lock);
976 buffer = global_trace.buffer;
978 ring_buffer_record_enable(buffer);
980 buffer = max_tr.buffer;
982 ring_buffer_record_enable(buffer);
984 arch_spin_unlock(&ftrace_max_lock);
988 spin_unlock_irqrestore(&tracing_start_lock, flags);
992 * tracing_stop - quick stop of the tracer
994 * Light weight way to stop tracing. Use in conjunction with
997 void tracing_stop(void)
999 struct ring_buffer *buffer;
1000 unsigned long flags;
1003 spin_lock_irqsave(&tracing_start_lock, flags);
1004 if (trace_stop_count++)
1007 /* Prevent the buffers from switching */
1008 arch_spin_lock(&ftrace_max_lock);
1010 buffer = global_trace.buffer;
1012 ring_buffer_record_disable(buffer);
1014 buffer = max_tr.buffer;
1016 ring_buffer_record_disable(buffer);
1018 arch_spin_unlock(&ftrace_max_lock);
1021 spin_unlock_irqrestore(&tracing_start_lock, flags);
1024 void trace_stop_cmdline_recording(void);
1026 static void trace_save_cmdline(struct task_struct *tsk)
1030 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1034 * It's not the end of the world if we don't get
1035 * the lock, but we also don't want to spin
1036 * nor do we want to disable interrupts,
1037 * so if we miss here, then better luck next time.
1039 if (!arch_spin_trylock(&trace_cmdline_lock))
1042 idx = map_pid_to_cmdline[tsk->pid];
1043 if (idx == NO_CMDLINE_MAP) {
1044 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1047 * Check whether the cmdline buffer at idx has a pid
1048 * mapped. We are going to overwrite that entry so we
1049 * need to clear the map_pid_to_cmdline. Otherwise we
1050 * would read the new comm for the old pid.
1052 pid = map_cmdline_to_pid[idx];
1053 if (pid != NO_CMDLINE_MAP)
1054 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1056 map_cmdline_to_pid[idx] = tsk->pid;
1057 map_pid_to_cmdline[tsk->pid] = idx;
1062 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1064 arch_spin_unlock(&trace_cmdline_lock);
1067 void trace_find_cmdline(int pid, char comm[])
1072 strcpy(comm, "<idle>");
1076 if (WARN_ON_ONCE(pid < 0)) {
1077 strcpy(comm, "<XXX>");
1081 if (pid > PID_MAX_DEFAULT) {
1082 strcpy(comm, "<...>");
1087 arch_spin_lock(&trace_cmdline_lock);
1088 map = map_pid_to_cmdline[pid];
1089 if (map != NO_CMDLINE_MAP)
1090 strcpy(comm, saved_cmdlines[map]);
1092 strcpy(comm, "<...>");
1094 arch_spin_unlock(&trace_cmdline_lock);
1098 void tracing_record_cmdline(struct task_struct *tsk)
1100 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1104 trace_save_cmdline(tsk);
1108 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1111 struct task_struct *tsk = current;
1113 entry->preempt_count = pc & 0xff;
1114 entry->pid = (tsk) ? tsk->pid : 0;
1117 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1118 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1120 TRACE_FLAG_IRQS_NOSUPPORT |
1122 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1123 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1124 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1126 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1128 struct ring_buffer_event *
1129 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1132 unsigned long flags, int pc)
1134 struct ring_buffer_event *event;
1136 event = ring_buffer_lock_reserve(buffer, len);
1137 if (event != NULL) {
1138 struct trace_entry *ent = ring_buffer_event_data(event);
1140 tracing_generic_entry_update(ent, flags, pc);
1148 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1149 struct ring_buffer_event *event,
1150 unsigned long flags, int pc,
1153 ring_buffer_unlock_commit(buffer, event);
1155 ftrace_trace_stack(buffer, flags, 6, pc);
1156 ftrace_trace_userstack(buffer, flags, pc);
1162 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1163 struct ring_buffer_event *event,
1164 unsigned long flags, int pc)
1166 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1169 struct ring_buffer_event *
1170 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1171 int type, unsigned long len,
1172 unsigned long flags, int pc)
1174 *current_rb = global_trace.buffer;
1175 return trace_buffer_lock_reserve(*current_rb,
1176 type, len, flags, pc);
1178 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1180 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1181 struct ring_buffer_event *event,
1182 unsigned long flags, int pc)
1184 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1186 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1188 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1189 struct ring_buffer_event *event,
1190 unsigned long flags, int pc)
1192 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1194 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1196 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1197 struct ring_buffer_event *event)
1199 ring_buffer_discard_commit(buffer, event);
1201 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1204 trace_function(struct trace_array *tr,
1205 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1208 struct ftrace_event_call *call = &event_function;
1209 struct ring_buffer *buffer = tr->buffer;
1210 struct ring_buffer_event *event;
1211 struct ftrace_entry *entry;
1213 /* If we are reading the ring buffer, don't trace */
1214 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1217 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1221 entry = ring_buffer_event_data(event);
1223 entry->parent_ip = parent_ip;
1225 if (!filter_check_discard(call, entry, buffer, event))
1226 ring_buffer_unlock_commit(buffer, event);
1230 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1231 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1234 if (likely(!atomic_read(&data->disabled)))
1235 trace_function(tr, ip, parent_ip, flags, pc);
1238 #ifdef CONFIG_STACKTRACE
1239 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1240 unsigned long flags,
1243 struct ftrace_event_call *call = &event_kernel_stack;
1244 struct ring_buffer_event *event;
1245 struct stack_entry *entry;
1246 struct stack_trace trace;
1248 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1249 sizeof(*entry), flags, pc);
1252 entry = ring_buffer_event_data(event);
1253 memset(&entry->caller, 0, sizeof(entry->caller));
1255 trace.nr_entries = 0;
1256 trace.max_entries = FTRACE_STACK_ENTRIES;
1258 trace.entries = entry->caller;
1260 save_stack_trace(&trace);
1261 if (!filter_check_discard(call, entry, buffer, event))
1262 ring_buffer_unlock_commit(buffer, event);
1265 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1268 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1271 __ftrace_trace_stack(buffer, flags, skip, pc);
1274 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1277 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
1281 * trace_dump_stack - record a stack back trace in the trace buffer
1283 void trace_dump_stack(void)
1285 unsigned long flags;
1287 if (tracing_disabled || tracing_selftest_running)
1290 local_save_flags(flags);
1292 /* skipping 3 traces, seems to get us at the caller of this function */
1293 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1296 static DEFINE_PER_CPU(int, user_stack_count);
1299 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1301 struct ftrace_event_call *call = &event_user_stack;
1302 struct ring_buffer_event *event;
1303 struct userstack_entry *entry;
1304 struct stack_trace trace;
1306 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1310 * NMIs can not handle page faults, even with fix ups.
1311 * The save user stack can (and often does) fault.
1313 if (unlikely(in_nmi()))
1317 * prevent recursion, since the user stack tracing may
1318 * trigger other kernel events.
1321 if (__this_cpu_read(user_stack_count))
1324 __this_cpu_inc(user_stack_count);
1326 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1327 sizeof(*entry), flags, pc);
1329 goto out_drop_count;
1330 entry = ring_buffer_event_data(event);
1332 entry->tgid = current->tgid;
1333 memset(&entry->caller, 0, sizeof(entry->caller));
1335 trace.nr_entries = 0;
1336 trace.max_entries = FTRACE_STACK_ENTRIES;
1338 trace.entries = entry->caller;
1340 save_stack_trace_user(&trace);
1341 if (!filter_check_discard(call, entry, buffer, event))
1342 ring_buffer_unlock_commit(buffer, event);
1345 __this_cpu_dec(user_stack_count);
1351 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1353 ftrace_trace_userstack(tr, flags, preempt_count());
1357 #endif /* CONFIG_STACKTRACE */
1360 * trace_vbprintk - write binary msg to tracing buffer
1363 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1365 static arch_spinlock_t trace_buf_lock =
1366 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1367 static u32 trace_buf[TRACE_BUF_SIZE];
1369 struct ftrace_event_call *call = &event_bprint;
1370 struct ring_buffer_event *event;
1371 struct ring_buffer *buffer;
1372 struct trace_array *tr = &global_trace;
1373 struct trace_array_cpu *data;
1374 struct bprint_entry *entry;
1375 unsigned long flags;
1377 int cpu, len = 0, size, pc;
1379 if (unlikely(tracing_selftest_running || tracing_disabled))
1382 /* Don't pollute graph traces with trace_vprintk internals */
1383 pause_graph_tracing();
1385 pc = preempt_count();
1386 preempt_disable_notrace();
1387 cpu = raw_smp_processor_id();
1388 data = tr->data[cpu];
1390 disable = atomic_inc_return(&data->disabled);
1391 if (unlikely(disable != 1))
1394 /* Lockdep uses trace_printk for lock tracing */
1395 local_irq_save(flags);
1396 arch_spin_lock(&trace_buf_lock);
1397 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1399 if (len > TRACE_BUF_SIZE || len < 0)
1402 size = sizeof(*entry) + sizeof(u32) * len;
1403 buffer = tr->buffer;
1404 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1408 entry = ring_buffer_event_data(event);
1412 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1413 if (!filter_check_discard(call, entry, buffer, event)) {
1414 ring_buffer_unlock_commit(buffer, event);
1415 ftrace_trace_stack(buffer, flags, 6, pc);
1419 arch_spin_unlock(&trace_buf_lock);
1420 local_irq_restore(flags);
1423 atomic_dec_return(&data->disabled);
1424 preempt_enable_notrace();
1425 unpause_graph_tracing();
1429 EXPORT_SYMBOL_GPL(trace_vbprintk);
1431 int trace_array_printk(struct trace_array *tr,
1432 unsigned long ip, const char *fmt, ...)
1437 if (!(trace_flags & TRACE_ITER_PRINTK))
1441 ret = trace_array_vprintk(tr, ip, fmt, ap);
1446 int trace_array_vprintk(struct trace_array *tr,
1447 unsigned long ip, const char *fmt, va_list args)
1449 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1450 static char trace_buf[TRACE_BUF_SIZE];
1452 struct ftrace_event_call *call = &event_print;
1453 struct ring_buffer_event *event;
1454 struct ring_buffer *buffer;
1455 struct trace_array_cpu *data;
1456 int cpu, len = 0, size, pc;
1457 struct print_entry *entry;
1458 unsigned long irq_flags;
1461 if (tracing_disabled || tracing_selftest_running)
1464 pc = preempt_count();
1465 preempt_disable_notrace();
1466 cpu = raw_smp_processor_id();
1467 data = tr->data[cpu];
1469 disable = atomic_inc_return(&data->disabled);
1470 if (unlikely(disable != 1))
1473 pause_graph_tracing();
1474 raw_local_irq_save(irq_flags);
1475 arch_spin_lock(&trace_buf_lock);
1476 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1478 size = sizeof(*entry) + len + 1;
1479 buffer = tr->buffer;
1480 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1484 entry = ring_buffer_event_data(event);
1487 memcpy(&entry->buf, trace_buf, len);
1488 entry->buf[len] = '\0';
1489 if (!filter_check_discard(call, entry, buffer, event)) {
1490 ring_buffer_unlock_commit(buffer, event);
1491 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1495 arch_spin_unlock(&trace_buf_lock);
1496 raw_local_irq_restore(irq_flags);
1497 unpause_graph_tracing();
1499 atomic_dec_return(&data->disabled);
1500 preempt_enable_notrace();
1505 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1507 return trace_array_vprintk(&global_trace, ip, fmt, args);
1509 EXPORT_SYMBOL_GPL(trace_vprintk);
1511 static void trace_iterator_increment(struct trace_iterator *iter)
1513 /* Don't allow ftrace to trace into the ring buffers */
1514 ftrace_disable_cpu();
1517 if (iter->buffer_iter[iter->cpu])
1518 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1520 ftrace_enable_cpu();
1523 static struct trace_entry *
1524 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1525 unsigned long *lost_events)
1527 struct ring_buffer_event *event;
1528 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1530 /* Don't allow ftrace to trace into the ring buffers */
1531 ftrace_disable_cpu();
1534 event = ring_buffer_iter_peek(buf_iter, ts);
1536 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1539 ftrace_enable_cpu();
1541 return event ? ring_buffer_event_data(event) : NULL;
1544 static struct trace_entry *
1545 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1546 unsigned long *missing_events, u64 *ent_ts)
1548 struct ring_buffer *buffer = iter->tr->buffer;
1549 struct trace_entry *ent, *next = NULL;
1550 unsigned long lost_events = 0, next_lost = 0;
1551 int cpu_file = iter->cpu_file;
1552 u64 next_ts = 0, ts;
1557 * If we are in a per_cpu trace file, don't bother by iterating over
1558 * all cpu and peek directly.
1560 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1561 if (ring_buffer_empty_cpu(buffer, cpu_file))
1563 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1565 *ent_cpu = cpu_file;
1570 for_each_tracing_cpu(cpu) {
1572 if (ring_buffer_empty_cpu(buffer, cpu))
1575 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1578 * Pick the entry with the smallest timestamp:
1580 if (ent && (!next || ts < next_ts)) {
1584 next_lost = lost_events;
1589 *ent_cpu = next_cpu;
1595 *missing_events = next_lost;
1600 /* Find the next real entry, without updating the iterator itself */
1601 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1602 int *ent_cpu, u64 *ent_ts)
1604 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1607 /* Find the next real entry, and increment the iterator to the next entry */
1608 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1610 iter->ent = __find_next_entry(iter, &iter->cpu,
1611 &iter->lost_events, &iter->ts);
1614 trace_iterator_increment(iter);
1616 return iter->ent ? iter : NULL;
1619 static void trace_consume(struct trace_iterator *iter)
1621 /* Don't allow ftrace to trace into the ring buffers */
1622 ftrace_disable_cpu();
1623 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1624 &iter->lost_events);
1625 ftrace_enable_cpu();
1628 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1630 struct trace_iterator *iter = m->private;
1634 WARN_ON_ONCE(iter->leftover);
1638 /* can't go backwards */
1643 ent = trace_find_next_entry_inc(iter);
1647 while (ent && iter->idx < i)
1648 ent = trace_find_next_entry_inc(iter);
1655 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1657 struct trace_array *tr = iter->tr;
1658 struct ring_buffer_event *event;
1659 struct ring_buffer_iter *buf_iter;
1660 unsigned long entries = 0;
1663 tr->data[cpu]->skipped_entries = 0;
1665 if (!iter->buffer_iter[cpu])
1668 buf_iter = iter->buffer_iter[cpu];
1669 ring_buffer_iter_reset(buf_iter);
1672 * We could have the case with the max latency tracers
1673 * that a reset never took place on a cpu. This is evident
1674 * by the timestamp being before the start of the buffer.
1676 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1677 if (ts >= iter->tr->time_start)
1680 ring_buffer_read(buf_iter, NULL);
1683 tr->data[cpu]->skipped_entries = entries;
1687 * The current tracer is copied to avoid a global locking
1690 static void *s_start(struct seq_file *m, loff_t *pos)
1692 struct trace_iterator *iter = m->private;
1693 static struct tracer *old_tracer;
1694 int cpu_file = iter->cpu_file;
1699 /* copy the tracer to avoid using a global lock all around */
1700 mutex_lock(&trace_types_lock);
1701 if (unlikely(old_tracer != current_trace && current_trace)) {
1702 old_tracer = current_trace;
1703 *iter->trace = *current_trace;
1705 mutex_unlock(&trace_types_lock);
1707 atomic_inc(&trace_record_cmdline_disabled);
1709 if (*pos != iter->pos) {
1714 ftrace_disable_cpu();
1716 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1717 for_each_tracing_cpu(cpu)
1718 tracing_iter_reset(iter, cpu);
1720 tracing_iter_reset(iter, cpu_file);
1722 ftrace_enable_cpu();
1725 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1730 * If we overflowed the seq_file before, then we want
1731 * to just reuse the trace_seq buffer again.
1737 p = s_next(m, p, &l);
1741 trace_event_read_lock();
1742 trace_access_lock(cpu_file);
1746 static void s_stop(struct seq_file *m, void *p)
1748 struct trace_iterator *iter = m->private;
1750 atomic_dec(&trace_record_cmdline_disabled);
1751 trace_access_unlock(iter->cpu_file);
1752 trace_event_read_unlock();
1755 static void print_lat_help_header(struct seq_file *m)
1757 seq_puts(m, "# _------=> CPU# \n");
1758 seq_puts(m, "# / _-----=> irqs-off \n");
1759 seq_puts(m, "# | / _----=> need-resched \n");
1760 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1761 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1762 seq_puts(m, "# |||| / delay \n");
1763 seq_puts(m, "# cmd pid ||||| time | caller \n");
1764 seq_puts(m, "# \\ / ||||| \\ | / \n");
1767 static void print_func_help_header(struct seq_file *m)
1769 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1770 seq_puts(m, "# | | | | |\n");
1775 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1777 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1778 struct trace_array *tr = iter->tr;
1779 struct trace_array_cpu *data = tr->data[tr->cpu];
1780 struct tracer *type = current_trace;
1781 unsigned long entries = 0;
1782 unsigned long total = 0;
1783 unsigned long count;
1784 const char *name = "preemption";
1791 for_each_tracing_cpu(cpu) {
1792 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1794 * If this buffer has skipped entries, then we hold all
1795 * entries for the trace and we need to ignore the
1796 * ones before the time stamp.
1798 if (tr->data[cpu]->skipped_entries) {
1799 count -= tr->data[cpu]->skipped_entries;
1800 /* total is the same as the entries */
1804 ring_buffer_overrun_cpu(tr->buffer, cpu);
1808 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1810 seq_puts(m, "# -----------------------------------"
1811 "---------------------------------\n");
1812 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1813 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1814 nsecs_to_usecs(data->saved_latency),
1818 #if defined(CONFIG_PREEMPT_NONE)
1820 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1822 #elif defined(CONFIG_PREEMPT)
1827 /* These are reserved for later use */
1830 seq_printf(m, " #P:%d)\n", num_online_cpus());
1834 seq_puts(m, "# -----------------\n");
1835 seq_printf(m, "# | task: %.16s-%d "
1836 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1837 data->comm, data->pid, data->uid, data->nice,
1838 data->policy, data->rt_priority);
1839 seq_puts(m, "# -----------------\n");
1841 if (data->critical_start) {
1842 seq_puts(m, "# => started at: ");
1843 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1844 trace_print_seq(m, &iter->seq);
1845 seq_puts(m, "\n# => ended at: ");
1846 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1847 trace_print_seq(m, &iter->seq);
1848 seq_puts(m, "\n#\n");
1854 static void test_cpu_buff_start(struct trace_iterator *iter)
1856 struct trace_seq *s = &iter->seq;
1858 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1861 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1864 if (cpumask_test_cpu(iter->cpu, iter->started))
1867 if (iter->tr->data[iter->cpu]->skipped_entries)
1870 cpumask_set_cpu(iter->cpu, iter->started);
1872 /* Don't print started cpu buffer for the first entry of the trace */
1874 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1878 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1880 struct trace_seq *s = &iter->seq;
1881 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1882 struct trace_entry *entry;
1883 struct trace_event *event;
1887 test_cpu_buff_start(iter);
1889 event = ftrace_find_event(entry->type);
1891 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1892 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1893 if (!trace_print_lat_context(iter))
1896 if (!trace_print_context(iter))
1902 return event->funcs->trace(iter, sym_flags, event);
1904 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1907 return TRACE_TYPE_HANDLED;
1909 return TRACE_TYPE_PARTIAL_LINE;
1912 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1914 struct trace_seq *s = &iter->seq;
1915 struct trace_entry *entry;
1916 struct trace_event *event;
1920 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1921 if (!trace_seq_printf(s, "%d %d %llu ",
1922 entry->pid, iter->cpu, iter->ts))
1926 event = ftrace_find_event(entry->type);
1928 return event->funcs->raw(iter, 0, event);
1930 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1933 return TRACE_TYPE_HANDLED;
1935 return TRACE_TYPE_PARTIAL_LINE;
1938 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1940 struct trace_seq *s = &iter->seq;
1941 unsigned char newline = '\n';
1942 struct trace_entry *entry;
1943 struct trace_event *event;
1947 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1948 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1949 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1950 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1953 event = ftrace_find_event(entry->type);
1955 enum print_line_t ret = event->funcs->hex(iter, 0, event);
1956 if (ret != TRACE_TYPE_HANDLED)
1960 SEQ_PUT_FIELD_RET(s, newline);
1962 return TRACE_TYPE_HANDLED;
1965 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1967 struct trace_seq *s = &iter->seq;
1968 struct trace_entry *entry;
1969 struct trace_event *event;
1973 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1974 SEQ_PUT_FIELD_RET(s, entry->pid);
1975 SEQ_PUT_FIELD_RET(s, iter->cpu);
1976 SEQ_PUT_FIELD_RET(s, iter->ts);
1979 event = ftrace_find_event(entry->type);
1980 return event ? event->funcs->binary(iter, 0, event) :
1984 int trace_empty(struct trace_iterator *iter)
1988 /* If we are looking at one CPU buffer, only check that one */
1989 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1990 cpu = iter->cpu_file;
1991 if (iter->buffer_iter[cpu]) {
1992 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1995 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2001 for_each_tracing_cpu(cpu) {
2002 if (iter->buffer_iter[cpu]) {
2003 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2006 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2014 /* Called with trace_event_read_lock() held. */
2015 enum print_line_t print_trace_line(struct trace_iterator *iter)
2017 enum print_line_t ret;
2019 if (iter->lost_events &&
2020 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2021 iter->cpu, iter->lost_events))
2022 return TRACE_TYPE_PARTIAL_LINE;
2024 if (iter->trace && iter->trace->print_line) {
2025 ret = iter->trace->print_line(iter);
2026 if (ret != TRACE_TYPE_UNHANDLED)
2030 if (iter->ent->type == TRACE_BPRINT &&
2031 trace_flags & TRACE_ITER_PRINTK &&
2032 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2033 return trace_print_bprintk_msg_only(iter);
2035 if (iter->ent->type == TRACE_PRINT &&
2036 trace_flags & TRACE_ITER_PRINTK &&
2037 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2038 return trace_print_printk_msg_only(iter);
2040 if (trace_flags & TRACE_ITER_BIN)
2041 return print_bin_fmt(iter);
2043 if (trace_flags & TRACE_ITER_HEX)
2044 return print_hex_fmt(iter);
2046 if (trace_flags & TRACE_ITER_RAW)
2047 return print_raw_fmt(iter);
2049 return print_trace_fmt(iter);
2052 void trace_default_header(struct seq_file *m)
2054 struct trace_iterator *iter = m->private;
2056 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2059 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2060 /* print nothing if the buffers are empty */
2061 if (trace_empty(iter))
2063 print_trace_header(m, iter);
2064 if (!(trace_flags & TRACE_ITER_VERBOSE))
2065 print_lat_help_header(m);
2067 if (!(trace_flags & TRACE_ITER_VERBOSE))
2068 print_func_help_header(m);
2072 static int s_show(struct seq_file *m, void *v)
2074 struct trace_iterator *iter = v;
2077 if (iter->ent == NULL) {
2079 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2082 if (iter->trace && iter->trace->print_header)
2083 iter->trace->print_header(m);
2085 trace_default_header(m);
2087 } else if (iter->leftover) {
2089 * If we filled the seq_file buffer earlier, we
2090 * want to just show it now.
2092 ret = trace_print_seq(m, &iter->seq);
2094 /* ret should this time be zero, but you never know */
2095 iter->leftover = ret;
2098 print_trace_line(iter);
2099 ret = trace_print_seq(m, &iter->seq);
2101 * If we overflow the seq_file buffer, then it will
2102 * ask us for this data again at start up.
2104 * ret is 0 if seq_file write succeeded.
2107 iter->leftover = ret;
2113 static const struct seq_operations tracer_seq_ops = {
2120 static struct trace_iterator *
2121 __tracing_open(struct inode *inode, struct file *file)
2123 long cpu_file = (long) inode->i_private;
2124 void *fail_ret = ERR_PTR(-ENOMEM);
2125 struct trace_iterator *iter;
2129 if (tracing_disabled)
2130 return ERR_PTR(-ENODEV);
2132 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2134 return ERR_PTR(-ENOMEM);
2137 * We make a copy of the current tracer to avoid concurrent
2138 * changes on it while we are reading.
2140 mutex_lock(&trace_types_lock);
2141 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2146 *iter->trace = *current_trace;
2148 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2151 if (current_trace && current_trace->print_max)
2154 iter->tr = &global_trace;
2156 mutex_init(&iter->mutex);
2157 iter->cpu_file = cpu_file;
2159 /* Notify the tracer early; before we stop tracing. */
2160 if (iter->trace && iter->trace->open)
2161 iter->trace->open(iter);
2163 /* Annotate start of buffers if we had overruns */
2164 if (ring_buffer_overruns(iter->tr->buffer))
2165 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2167 /* stop the trace while dumping */
2170 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2171 for_each_tracing_cpu(cpu) {
2172 iter->buffer_iter[cpu] =
2173 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2175 ring_buffer_read_prepare_sync();
2176 for_each_tracing_cpu(cpu) {
2177 ring_buffer_read_start(iter->buffer_iter[cpu]);
2178 tracing_iter_reset(iter, cpu);
2181 cpu = iter->cpu_file;
2182 iter->buffer_iter[cpu] =
2183 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2184 ring_buffer_read_prepare_sync();
2185 ring_buffer_read_start(iter->buffer_iter[cpu]);
2186 tracing_iter_reset(iter, cpu);
2189 ret = seq_open(file, &tracer_seq_ops);
2191 fail_ret = ERR_PTR(ret);
2195 m = file->private_data;
2198 mutex_unlock(&trace_types_lock);
2203 for_each_tracing_cpu(cpu) {
2204 if (iter->buffer_iter[cpu])
2205 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2207 free_cpumask_var(iter->started);
2210 mutex_unlock(&trace_types_lock);
2217 int tracing_open_generic(struct inode *inode, struct file *filp)
2219 if (tracing_disabled)
2222 filp->private_data = inode->i_private;
2226 static int tracing_release(struct inode *inode, struct file *file)
2228 struct seq_file *m = file->private_data;
2229 struct trace_iterator *iter;
2232 if (!(file->f_mode & FMODE_READ))
2237 mutex_lock(&trace_types_lock);
2238 for_each_tracing_cpu(cpu) {
2239 if (iter->buffer_iter[cpu])
2240 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2243 if (iter->trace && iter->trace->close)
2244 iter->trace->close(iter);
2246 /* reenable tracing if it was previously enabled */
2248 mutex_unlock(&trace_types_lock);
2250 seq_release(inode, file);
2251 mutex_destroy(&iter->mutex);
2252 free_cpumask_var(iter->started);
2258 static int tracing_open(struct inode *inode, struct file *file)
2260 struct trace_iterator *iter;
2263 /* If this file was open for write, then erase contents */
2264 if ((file->f_mode & FMODE_WRITE) &&
2265 (file->f_flags & O_TRUNC)) {
2266 long cpu = (long) inode->i_private;
2268 if (cpu == TRACE_PIPE_ALL_CPU)
2269 tracing_reset_online_cpus(&global_trace);
2271 tracing_reset(&global_trace, cpu);
2274 if (file->f_mode & FMODE_READ) {
2275 iter = __tracing_open(inode, file);
2277 ret = PTR_ERR(iter);
2278 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2279 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2285 t_next(struct seq_file *m, void *v, loff_t *pos)
2287 struct tracer *t = v;
2297 static void *t_start(struct seq_file *m, loff_t *pos)
2302 mutex_lock(&trace_types_lock);
2303 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2309 static void t_stop(struct seq_file *m, void *p)
2311 mutex_unlock(&trace_types_lock);
2314 static int t_show(struct seq_file *m, void *v)
2316 struct tracer *t = v;
2321 seq_printf(m, "%s", t->name);
2330 static const struct seq_operations show_traces_seq_ops = {
2337 static int show_traces_open(struct inode *inode, struct file *file)
2339 if (tracing_disabled)
2342 return seq_open(file, &show_traces_seq_ops);
2346 tracing_write_stub(struct file *filp, const char __user *ubuf,
2347 size_t count, loff_t *ppos)
2352 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2354 if (file->f_mode & FMODE_READ)
2355 return seq_lseek(file, offset, origin);
2360 static const struct file_operations tracing_fops = {
2361 .open = tracing_open,
2363 .write = tracing_write_stub,
2364 .llseek = tracing_seek,
2365 .release = tracing_release,
2368 static const struct file_operations show_traces_fops = {
2369 .open = show_traces_open,
2371 .release = seq_release,
2372 .llseek = seq_lseek,
2376 * Only trace on a CPU if the bitmask is set:
2378 static cpumask_var_t tracing_cpumask;
2381 * The tracer itself will not take this lock, but still we want
2382 * to provide a consistent cpumask to user-space:
2384 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2387 * Temporary storage for the character representation of the
2388 * CPU bitmask (and one more byte for the newline):
2390 static char mask_str[NR_CPUS + 1];
2393 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2394 size_t count, loff_t *ppos)
2398 mutex_lock(&tracing_cpumask_update_lock);
2400 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2401 if (count - len < 2) {
2405 len += sprintf(mask_str + len, "\n");
2406 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2409 mutex_unlock(&tracing_cpumask_update_lock);
2415 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2416 size_t count, loff_t *ppos)
2419 cpumask_var_t tracing_cpumask_new;
2421 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2424 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2428 mutex_lock(&tracing_cpumask_update_lock);
2430 local_irq_disable();
2431 arch_spin_lock(&ftrace_max_lock);
2432 for_each_tracing_cpu(cpu) {
2434 * Increase/decrease the disabled counter if we are
2435 * about to flip a bit in the cpumask:
2437 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2438 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2439 atomic_inc(&global_trace.data[cpu]->disabled);
2441 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2442 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2443 atomic_dec(&global_trace.data[cpu]->disabled);
2446 arch_spin_unlock(&ftrace_max_lock);
2449 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2451 mutex_unlock(&tracing_cpumask_update_lock);
2452 free_cpumask_var(tracing_cpumask_new);
2457 free_cpumask_var(tracing_cpumask_new);
2462 static const struct file_operations tracing_cpumask_fops = {
2463 .open = tracing_open_generic,
2464 .read = tracing_cpumask_read,
2465 .write = tracing_cpumask_write,
2466 .llseek = generic_file_llseek,
2469 static int tracing_trace_options_show(struct seq_file *m, void *v)
2471 struct tracer_opt *trace_opts;
2475 mutex_lock(&trace_types_lock);
2476 tracer_flags = current_trace->flags->val;
2477 trace_opts = current_trace->flags->opts;
2479 for (i = 0; trace_options[i]; i++) {
2480 if (trace_flags & (1 << i))
2481 seq_printf(m, "%s\n", trace_options[i]);
2483 seq_printf(m, "no%s\n", trace_options[i]);
2486 for (i = 0; trace_opts[i].name; i++) {
2487 if (tracer_flags & trace_opts[i].bit)
2488 seq_printf(m, "%s\n", trace_opts[i].name);
2490 seq_printf(m, "no%s\n", trace_opts[i].name);
2492 mutex_unlock(&trace_types_lock);
2497 static int __set_tracer_option(struct tracer *trace,
2498 struct tracer_flags *tracer_flags,
2499 struct tracer_opt *opts, int neg)
2503 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2508 tracer_flags->val &= ~opts->bit;
2510 tracer_flags->val |= opts->bit;
2514 /* Try to assign a tracer specific option */
2515 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2517 struct tracer_flags *tracer_flags = trace->flags;
2518 struct tracer_opt *opts = NULL;
2521 for (i = 0; tracer_flags->opts[i].name; i++) {
2522 opts = &tracer_flags->opts[i];
2524 if (strcmp(cmp, opts->name) == 0)
2525 return __set_tracer_option(trace, trace->flags,
2532 static void set_tracer_flags(unsigned int mask, int enabled)
2534 /* do nothing if flag is already set */
2535 if (!!(trace_flags & mask) == !!enabled)
2539 trace_flags |= mask;
2541 trace_flags &= ~mask;
2543 if (mask == TRACE_ITER_RECORD_CMD)
2544 trace_event_enable_cmd_record(enabled);
2546 if (mask == TRACE_ITER_OVERWRITE)
2547 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2551 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2552 size_t cnt, loff_t *ppos)
2560 if (cnt >= sizeof(buf))
2563 if (copy_from_user(&buf, ubuf, cnt))
2567 cmp = strstrip(buf);
2569 if (strncmp(cmp, "no", 2) == 0) {
2574 for (i = 0; trace_options[i]; i++) {
2575 if (strcmp(cmp, trace_options[i]) == 0) {
2576 set_tracer_flags(1 << i, !neg);
2581 /* If no option could be set, test the specific tracer options */
2582 if (!trace_options[i]) {
2583 mutex_lock(&trace_types_lock);
2584 ret = set_tracer_option(current_trace, cmp, neg);
2585 mutex_unlock(&trace_types_lock);
2595 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2597 if (tracing_disabled)
2599 return single_open(file, tracing_trace_options_show, NULL);
2602 static const struct file_operations tracing_iter_fops = {
2603 .open = tracing_trace_options_open,
2605 .llseek = seq_lseek,
2606 .release = single_release,
2607 .write = tracing_trace_options_write,
2610 static const char readme_msg[] =
2611 "tracing mini-HOWTO:\n\n"
2612 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2613 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2614 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2615 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2617 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2618 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2620 "# cat /sys/kernel/debug/tracing/trace_options\n"
2621 "noprint-parent nosym-offset nosym-addr noverbose\n"
2622 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2623 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2624 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2625 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2629 tracing_readme_read(struct file *filp, char __user *ubuf,
2630 size_t cnt, loff_t *ppos)
2632 return simple_read_from_buffer(ubuf, cnt, ppos,
2633 readme_msg, strlen(readme_msg));
2636 static const struct file_operations tracing_readme_fops = {
2637 .open = tracing_open_generic,
2638 .read = tracing_readme_read,
2639 .llseek = generic_file_llseek,
2643 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2644 size_t cnt, loff_t *ppos)
2653 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2657 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2665 for (i = 0; i < SAVED_CMDLINES; i++) {
2668 pid = map_cmdline_to_pid[i];
2669 if (pid == -1 || pid == NO_CMDLINE_MAP)
2672 trace_find_cmdline(pid, buf_comm);
2673 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2678 len = simple_read_from_buffer(ubuf, cnt, ppos,
2687 static const struct file_operations tracing_saved_cmdlines_fops = {
2688 .open = tracing_open_generic,
2689 .read = tracing_saved_cmdlines_read,
2690 .llseek = generic_file_llseek,
2694 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2695 size_t cnt, loff_t *ppos)
2700 r = sprintf(buf, "%u\n", tracer_enabled);
2701 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2705 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2706 size_t cnt, loff_t *ppos)
2708 struct trace_array *tr = filp->private_data;
2712 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
2718 mutex_lock(&trace_types_lock);
2719 if (tracer_enabled ^ val) {
2721 /* Only need to warn if this is used to change the state */
2722 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2726 if (current_trace->start)
2727 current_trace->start(tr);
2732 if (current_trace->stop)
2733 current_trace->stop(tr);
2736 mutex_unlock(&trace_types_lock);
2744 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2745 size_t cnt, loff_t *ppos)
2747 char buf[MAX_TRACER_SIZE+2];
2750 mutex_lock(&trace_types_lock);
2752 r = sprintf(buf, "%s\n", current_trace->name);
2754 r = sprintf(buf, "\n");
2755 mutex_unlock(&trace_types_lock);
2757 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2760 int tracer_init(struct tracer *t, struct trace_array *tr)
2762 tracing_reset_online_cpus(tr);
2766 static int __tracing_resize_ring_buffer(unsigned long size)
2771 * If kernel or user changes the size of the ring buffer
2772 * we use the size that was given, and we can forget about
2773 * expanding it later.
2775 ring_buffer_expanded = 1;
2777 ret = ring_buffer_resize(global_trace.buffer, size);
2781 if (!current_trace->use_max_tr)
2784 ret = ring_buffer_resize(max_tr.buffer, size);
2788 r = ring_buffer_resize(global_trace.buffer,
2789 global_trace.entries);
2792 * AARGH! We are left with different
2793 * size max buffer!!!!
2794 * The max buffer is our "snapshot" buffer.
2795 * When a tracer needs a snapshot (one of the
2796 * latency tracers), it swaps the max buffer
2797 * with the saved snap shot. We succeeded to
2798 * update the size of the main buffer, but failed to
2799 * update the size of the max buffer. But when we tried
2800 * to reset the main buffer to the original size, we
2801 * failed there too. This is very unlikely to
2802 * happen, but if it does, warn and kill all
2806 tracing_disabled = 1;
2811 max_tr.entries = size;
2813 global_trace.entries = size;
2818 static ssize_t tracing_resize_ring_buffer(unsigned long size)
2820 int cpu, ret = size;
2822 mutex_lock(&trace_types_lock);
2826 /* disable all cpu buffers */
2827 for_each_tracing_cpu(cpu) {
2828 if (global_trace.data[cpu])
2829 atomic_inc(&global_trace.data[cpu]->disabled);
2830 if (max_tr.data[cpu])
2831 atomic_inc(&max_tr.data[cpu]->disabled);
2834 if (size != global_trace.entries)
2835 ret = __tracing_resize_ring_buffer(size);
2840 for_each_tracing_cpu(cpu) {
2841 if (global_trace.data[cpu])
2842 atomic_dec(&global_trace.data[cpu]->disabled);
2843 if (max_tr.data[cpu])
2844 atomic_dec(&max_tr.data[cpu]->disabled);
2848 mutex_unlock(&trace_types_lock);
2855 * tracing_update_buffers - used by tracing facility to expand ring buffers
2857 * To save on memory when the tracing is never used on a system with it
2858 * configured in. The ring buffers are set to a minimum size. But once
2859 * a user starts to use the tracing facility, then they need to grow
2860 * to their default size.
2862 * This function is to be called when a tracer is about to be used.
2864 int tracing_update_buffers(void)
2868 mutex_lock(&trace_types_lock);
2869 if (!ring_buffer_expanded)
2870 ret = __tracing_resize_ring_buffer(trace_buf_size);
2871 mutex_unlock(&trace_types_lock);
2876 struct trace_option_dentry;
2878 static struct trace_option_dentry *
2879 create_trace_option_files(struct tracer *tracer);
2882 destroy_trace_option_files(struct trace_option_dentry *topts);
2884 static int tracing_set_tracer(const char *buf)
2886 static struct trace_option_dentry *topts;
2887 struct trace_array *tr = &global_trace;
2891 mutex_lock(&trace_types_lock);
2893 if (!ring_buffer_expanded) {
2894 ret = __tracing_resize_ring_buffer(trace_buf_size);
2900 for (t = trace_types; t; t = t->next) {
2901 if (strcmp(t->name, buf) == 0)
2908 if (t == current_trace)
2911 trace_branch_disable();
2912 if (current_trace && current_trace->reset)
2913 current_trace->reset(tr);
2914 if (current_trace && current_trace->use_max_tr) {
2916 * We don't free the ring buffer. instead, resize it because
2917 * The max_tr ring buffer has some state (e.g. ring->clock) and
2918 * we want preserve it.
2920 ring_buffer_resize(max_tr.buffer, 1);
2923 destroy_trace_option_files(topts);
2927 topts = create_trace_option_files(current_trace);
2928 if (current_trace->use_max_tr) {
2929 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
2932 max_tr.entries = global_trace.entries;
2936 ret = tracer_init(t, tr);
2941 trace_branch_enable(tr);
2943 mutex_unlock(&trace_types_lock);
2949 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2950 size_t cnt, loff_t *ppos)
2952 char buf[MAX_TRACER_SIZE+1];
2959 if (cnt > MAX_TRACER_SIZE)
2960 cnt = MAX_TRACER_SIZE;
2962 if (copy_from_user(&buf, ubuf, cnt))
2967 /* strip ending whitespace. */
2968 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2971 err = tracing_set_tracer(buf);
2981 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2982 size_t cnt, loff_t *ppos)
2984 unsigned long *ptr = filp->private_data;
2988 r = snprintf(buf, sizeof(buf), "%ld\n",
2989 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2990 if (r > sizeof(buf))
2992 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2996 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2997 size_t cnt, loff_t *ppos)
2999 unsigned long *ptr = filp->private_data;
3003 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3012 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3014 long cpu_file = (long) inode->i_private;
3015 struct trace_iterator *iter;
3018 if (tracing_disabled)
3021 mutex_lock(&trace_types_lock);
3023 /* create a buffer to store the information to pass to userspace */
3024 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3031 * We make a copy of the current tracer to avoid concurrent
3032 * changes on it while we are reading.
3034 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3040 *iter->trace = *current_trace;
3042 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3047 /* trace pipe does not show start of buffer */
3048 cpumask_setall(iter->started);
3050 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3051 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3053 iter->cpu_file = cpu_file;
3054 iter->tr = &global_trace;
3055 mutex_init(&iter->mutex);
3056 filp->private_data = iter;
3058 if (iter->trace->pipe_open)
3059 iter->trace->pipe_open(iter);
3061 nonseekable_open(inode, filp);
3063 mutex_unlock(&trace_types_lock);
3069 mutex_unlock(&trace_types_lock);
3073 static int tracing_release_pipe(struct inode *inode, struct file *file)
3075 struct trace_iterator *iter = file->private_data;
3077 mutex_lock(&trace_types_lock);
3079 if (iter->trace->pipe_close)
3080 iter->trace->pipe_close(iter);
3082 mutex_unlock(&trace_types_lock);
3084 free_cpumask_var(iter->started);
3085 mutex_destroy(&iter->mutex);
3093 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3095 struct trace_iterator *iter = filp->private_data;
3097 if (trace_flags & TRACE_ITER_BLOCK) {
3099 * Always select as readable when in blocking mode
3101 return POLLIN | POLLRDNORM;
3103 if (!trace_empty(iter))
3104 return POLLIN | POLLRDNORM;
3105 poll_wait(filp, &trace_wait, poll_table);
3106 if (!trace_empty(iter))
3107 return POLLIN | POLLRDNORM;
3114 void default_wait_pipe(struct trace_iterator *iter)
3118 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3120 if (trace_empty(iter))
3123 finish_wait(&trace_wait, &wait);
3127 * This is a make-shift waitqueue.
3128 * A tracer might use this callback on some rare cases:
3130 * 1) the current tracer might hold the runqueue lock when it wakes up
3131 * a reader, hence a deadlock (sched, function, and function graph tracers)
3132 * 2) the function tracers, trace all functions, we don't want
3133 * the overhead of calling wake_up and friends
3134 * (and tracing them too)
3136 * Anyway, this is really very primitive wakeup.
3138 void poll_wait_pipe(struct trace_iterator *iter)
3140 set_current_state(TASK_INTERRUPTIBLE);
3141 /* sleep for 100 msecs, and try again. */
3142 schedule_timeout(HZ / 10);
3145 /* Must be called with trace_types_lock mutex held. */
3146 static int tracing_wait_pipe(struct file *filp)
3148 struct trace_iterator *iter = filp->private_data;
3150 while (trace_empty(iter)) {
3152 if ((filp->f_flags & O_NONBLOCK)) {
3156 mutex_unlock(&iter->mutex);
3158 iter->trace->wait_pipe(iter);
3160 mutex_lock(&iter->mutex);
3162 if (signal_pending(current))
3166 * We block until we read something and tracing is disabled.
3167 * We still block if tracing is disabled, but we have never
3168 * read anything. This allows a user to cat this file, and
3169 * then enable tracing. But after we have read something,
3170 * we give an EOF when tracing is again disabled.
3172 * iter->pos will be 0 if we haven't read anything.
3174 if (!tracer_enabled && iter->pos)
3185 tracing_read_pipe(struct file *filp, char __user *ubuf,
3186 size_t cnt, loff_t *ppos)
3188 struct trace_iterator *iter = filp->private_data;
3189 static struct tracer *old_tracer;
3192 /* return any leftover data */
3193 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3197 trace_seq_init(&iter->seq);
3199 /* copy the tracer to avoid using a global lock all around */
3200 mutex_lock(&trace_types_lock);
3201 if (unlikely(old_tracer != current_trace && current_trace)) {
3202 old_tracer = current_trace;
3203 *iter->trace = *current_trace;
3205 mutex_unlock(&trace_types_lock);
3208 * Avoid more than one consumer on a single file descriptor
3209 * This is just a matter of traces coherency, the ring buffer itself
3212 mutex_lock(&iter->mutex);
3213 if (iter->trace->read) {
3214 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3220 sret = tracing_wait_pipe(filp);
3224 /* stop when tracing is finished */
3225 if (trace_empty(iter)) {
3230 if (cnt >= PAGE_SIZE)
3231 cnt = PAGE_SIZE - 1;
3233 /* reset all but tr, trace, and overruns */
3234 memset(&iter->seq, 0,
3235 sizeof(struct trace_iterator) -
3236 offsetof(struct trace_iterator, seq));
3239 trace_event_read_lock();
3240 trace_access_lock(iter->cpu_file);
3241 while (trace_find_next_entry_inc(iter) != NULL) {
3242 enum print_line_t ret;
3243 int len = iter->seq.len;
3245 ret = print_trace_line(iter);
3246 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3247 /* don't print partial lines */
3248 iter->seq.len = len;
3251 if (ret != TRACE_TYPE_NO_CONSUME)
3252 trace_consume(iter);
3254 if (iter->seq.len >= cnt)
3258 * Setting the full flag means we reached the trace_seq buffer
3259 * size and we should leave by partial output condition above.
3260 * One of the trace_seq_* functions is not used properly.
3262 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3265 trace_access_unlock(iter->cpu_file);
3266 trace_event_read_unlock();
3268 /* Now copy what we have to the user */
3269 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3270 if (iter->seq.readpos >= iter->seq.len)
3271 trace_seq_init(&iter->seq);
3274 * If there was nothing to send to user, in spite of consuming trace
3275 * entries, go back to wait for more entries.
3281 mutex_unlock(&iter->mutex);
3286 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3287 struct pipe_buffer *buf)
3289 __free_page(buf->page);
3292 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3295 __free_page(spd->pages[idx]);
3298 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3300 .map = generic_pipe_buf_map,
3301 .unmap = generic_pipe_buf_unmap,
3302 .confirm = generic_pipe_buf_confirm,
3303 .release = tracing_pipe_buf_release,
3304 .steal = generic_pipe_buf_steal,
3305 .get = generic_pipe_buf_get,
3309 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3314 /* Seq buffer is page-sized, exactly what we need. */
3316 count = iter->seq.len;
3317 ret = print_trace_line(iter);
3318 count = iter->seq.len - count;
3321 iter->seq.len -= count;
3324 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3325 iter->seq.len -= count;
3329 if (ret != TRACE_TYPE_NO_CONSUME)
3330 trace_consume(iter);
3332 if (!trace_find_next_entry_inc(iter)) {
3342 static ssize_t tracing_splice_read_pipe(struct file *filp,
3344 struct pipe_inode_info *pipe,
3348 struct page *pages_def[PIPE_DEF_BUFFERS];
3349 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3350 struct trace_iterator *iter = filp->private_data;
3351 struct splice_pipe_desc spd = {
3353 .partial = partial_def,
3354 .nr_pages = 0, /* This gets updated below. */
3356 .ops = &tracing_pipe_buf_ops,
3357 .spd_release = tracing_spd_release_pipe,
3359 static struct tracer *old_tracer;
3364 if (splice_grow_spd(pipe, &spd))
3367 /* copy the tracer to avoid using a global lock all around */
3368 mutex_lock(&trace_types_lock);
3369 if (unlikely(old_tracer != current_trace && current_trace)) {
3370 old_tracer = current_trace;
3371 *iter->trace = *current_trace;
3373 mutex_unlock(&trace_types_lock);
3375 mutex_lock(&iter->mutex);
3377 if (iter->trace->splice_read) {
3378 ret = iter->trace->splice_read(iter, filp,
3379 ppos, pipe, len, flags);
3384 ret = tracing_wait_pipe(filp);
3388 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3393 trace_event_read_lock();
3394 trace_access_lock(iter->cpu_file);
3396 /* Fill as many pages as possible. */
3397 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3398 spd.pages[i] = alloc_page(GFP_KERNEL);
3402 rem = tracing_fill_pipe_page(rem, iter);
3404 /* Copy the data into the page, so we can start over. */
3405 ret = trace_seq_to_buffer(&iter->seq,
3406 page_address(spd.pages[i]),
3409 __free_page(spd.pages[i]);
3412 spd.partial[i].offset = 0;
3413 spd.partial[i].len = iter->seq.len;
3415 trace_seq_init(&iter->seq);
3418 trace_access_unlock(iter->cpu_file);
3419 trace_event_read_unlock();
3420 mutex_unlock(&iter->mutex);
3424 ret = splice_to_pipe(pipe, &spd);
3426 splice_shrink_spd(pipe, &spd);
3430 mutex_unlock(&iter->mutex);
3435 tracing_entries_read(struct file *filp, char __user *ubuf,
3436 size_t cnt, loff_t *ppos)
3438 struct trace_array *tr = filp->private_data;
3442 mutex_lock(&trace_types_lock);
3443 if (!ring_buffer_expanded)
3444 r = sprintf(buf, "%lu (expanded: %lu)\n",
3446 trace_buf_size >> 10);
3448 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3449 mutex_unlock(&trace_types_lock);
3451 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3455 tracing_entries_write(struct file *filp, const char __user *ubuf,
3456 size_t cnt, loff_t *ppos)
3461 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3465 /* must have at least 1 entry */
3469 /* value is in KB */
3472 ret = tracing_resize_ring_buffer(val);
3482 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3483 size_t cnt, loff_t *ppos)
3486 * There is no need to read what the user has written, this function
3487 * is just to make sure that there is no error when "echo" is used
3496 tracing_free_buffer_release(struct inode *inode, struct file *filp)
3498 /* disable tracing ? */
3499 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3501 /* resize the ring buffer to 0 */
3502 tracing_resize_ring_buffer(0);
3507 static int mark_printk(const char *fmt, ...)
3511 va_start(args, fmt);
3512 ret = trace_vprintk(0, fmt, args);
3518 tracing_mark_write(struct file *filp, const char __user *ubuf,
3519 size_t cnt, loff_t *fpos)
3524 if (tracing_disabled)
3527 if (cnt > TRACE_BUF_SIZE)
3528 cnt = TRACE_BUF_SIZE;
3530 buf = kmalloc(cnt + 2, GFP_KERNEL);
3534 if (copy_from_user(buf, ubuf, cnt)) {
3538 if (buf[cnt-1] != '\n') {
3544 written = mark_printk("%s", buf);
3548 /* don't tell userspace we wrote more - it might confuse them */
3555 static int tracing_clock_show(struct seq_file *m, void *v)
3559 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3561 "%s%s%s%s", i ? " " : "",
3562 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3563 i == trace_clock_id ? "]" : "");
3569 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3570 size_t cnt, loff_t *fpos)
3573 const char *clockstr;
3576 if (cnt >= sizeof(buf))
3579 if (copy_from_user(&buf, ubuf, cnt))
3584 clockstr = strstrip(buf);
3586 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3587 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3590 if (i == ARRAY_SIZE(trace_clocks))
3595 mutex_lock(&trace_types_lock);
3597 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3599 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3601 mutex_unlock(&trace_types_lock);
3608 static int tracing_clock_open(struct inode *inode, struct file *file)
3610 if (tracing_disabled)
3612 return single_open(file, tracing_clock_show, NULL);
3615 static const struct file_operations tracing_max_lat_fops = {
3616 .open = tracing_open_generic,
3617 .read = tracing_max_lat_read,
3618 .write = tracing_max_lat_write,
3619 .llseek = generic_file_llseek,
3622 static const struct file_operations tracing_ctrl_fops = {
3623 .open = tracing_open_generic,
3624 .read = tracing_ctrl_read,
3625 .write = tracing_ctrl_write,
3626 .llseek = generic_file_llseek,
3629 static const struct file_operations set_tracer_fops = {
3630 .open = tracing_open_generic,
3631 .read = tracing_set_trace_read,
3632 .write = tracing_set_trace_write,
3633 .llseek = generic_file_llseek,
3636 static const struct file_operations tracing_pipe_fops = {
3637 .open = tracing_open_pipe,
3638 .poll = tracing_poll_pipe,
3639 .read = tracing_read_pipe,
3640 .splice_read = tracing_splice_read_pipe,
3641 .release = tracing_release_pipe,
3642 .llseek = no_llseek,
3645 static const struct file_operations tracing_entries_fops = {
3646 .open = tracing_open_generic,
3647 .read = tracing_entries_read,
3648 .write = tracing_entries_write,
3649 .llseek = generic_file_llseek,
3652 static const struct file_operations tracing_free_buffer_fops = {
3653 .write = tracing_free_buffer_write,
3654 .release = tracing_free_buffer_release,
3657 static const struct file_operations tracing_mark_fops = {
3658 .open = tracing_open_generic,
3659 .write = tracing_mark_write,
3660 .llseek = generic_file_llseek,
3663 static const struct file_operations trace_clock_fops = {
3664 .open = tracing_clock_open,
3666 .llseek = seq_lseek,
3667 .release = single_release,
3668 .write = tracing_clock_write,
3671 struct ftrace_buffer_info {
3672 struct trace_array *tr;
3678 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3680 int cpu = (int)(long)inode->i_private;
3681 struct ftrace_buffer_info *info;
3683 if (tracing_disabled)
3686 info = kzalloc(sizeof(*info), GFP_KERNEL);
3690 info->tr = &global_trace;
3693 /* Force reading ring buffer for first read */
3694 info->read = (unsigned int)-1;
3696 filp->private_data = info;
3698 return nonseekable_open(inode, filp);
3702 tracing_buffers_read(struct file *filp, char __user *ubuf,
3703 size_t count, loff_t *ppos)
3705 struct ftrace_buffer_info *info = filp->private_data;
3713 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
3717 /* Do we have previous read data to read? */
3718 if (info->read < PAGE_SIZE)
3723 trace_access_lock(info->cpu);
3724 ret = ring_buffer_read_page(info->tr->buffer,
3728 trace_access_unlock(info->cpu);
3733 size = PAGE_SIZE - info->read;
3737 ret = copy_to_user(ubuf, info->spare + info->read, size);
3748 static int tracing_buffers_release(struct inode *inode, struct file *file)
3750 struct ftrace_buffer_info *info = file->private_data;
3753 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3760 struct ring_buffer *buffer;
3765 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3766 struct pipe_buffer *buf)
3768 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3773 ring_buffer_free_read_page(ref->buffer, ref->page);
3778 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3779 struct pipe_buffer *buf)
3784 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3785 struct pipe_buffer *buf)
3787 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3792 /* Pipe buffer operations for a buffer. */
3793 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
3795 .map = generic_pipe_buf_map,
3796 .unmap = generic_pipe_buf_unmap,
3797 .confirm = generic_pipe_buf_confirm,
3798 .release = buffer_pipe_buf_release,
3799 .steal = buffer_pipe_buf_steal,
3800 .get = buffer_pipe_buf_get,
3804 * Callback from splice_to_pipe(), if we need to release some pages
3805 * at the end of the spd in case we error'ed out in filling the pipe.
3807 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3809 struct buffer_ref *ref =
3810 (struct buffer_ref *)spd->partial[i].private;
3815 ring_buffer_free_read_page(ref->buffer, ref->page);
3817 spd->partial[i].private = 0;
3821 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3822 struct pipe_inode_info *pipe, size_t len,
3825 struct ftrace_buffer_info *info = file->private_data;
3826 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3827 struct page *pages_def[PIPE_DEF_BUFFERS];
3828 struct splice_pipe_desc spd = {
3830 .partial = partial_def,
3832 .ops = &buffer_pipe_buf_ops,
3833 .spd_release = buffer_spd_release,
3835 struct buffer_ref *ref;
3836 int entries, size, i;
3839 if (splice_grow_spd(pipe, &spd))
3842 if (*ppos & (PAGE_SIZE - 1)) {
3843 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3848 if (len & (PAGE_SIZE - 1)) {
3849 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3850 if (len < PAGE_SIZE) {
3857 trace_access_lock(info->cpu);
3858 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3860 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
3864 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3869 ref->buffer = info->tr->buffer;
3870 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
3876 r = ring_buffer_read_page(ref->buffer, &ref->page,
3879 ring_buffer_free_read_page(ref->buffer, ref->page);
3885 * zero out any left over data, this is going to
3888 size = ring_buffer_page_len(ref->page);
3889 if (size < PAGE_SIZE)
3890 memset(ref->page + size, 0, PAGE_SIZE - size);
3892 page = virt_to_page(ref->page);
3894 spd.pages[i] = page;
3895 spd.partial[i].len = PAGE_SIZE;
3896 spd.partial[i].offset = 0;
3897 spd.partial[i].private = (unsigned long)ref;
3901 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3904 trace_access_unlock(info->cpu);
3907 /* did we read anything? */
3908 if (!spd.nr_pages) {
3909 if (flags & SPLICE_F_NONBLOCK)
3917 ret = splice_to_pipe(pipe, &spd);
3918 splice_shrink_spd(pipe, &spd);
3923 static const struct file_operations tracing_buffers_fops = {
3924 .open = tracing_buffers_open,
3925 .read = tracing_buffers_read,
3926 .release = tracing_buffers_release,
3927 .splice_read = tracing_buffers_splice_read,
3928 .llseek = no_llseek,
3932 tracing_stats_read(struct file *filp, char __user *ubuf,
3933 size_t count, loff_t *ppos)
3935 unsigned long cpu = (unsigned long)filp->private_data;
3936 struct trace_array *tr = &global_trace;
3937 struct trace_seq *s;
3940 s = kmalloc(sizeof(*s), GFP_KERNEL);
3946 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3947 trace_seq_printf(s, "entries: %ld\n", cnt);
3949 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3950 trace_seq_printf(s, "overrun: %ld\n", cnt);
3952 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3953 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3955 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3962 static const struct file_operations tracing_stats_fops = {
3963 .open = tracing_open_generic,
3964 .read = tracing_stats_read,
3965 .llseek = generic_file_llseek,
3968 #ifdef CONFIG_DYNAMIC_FTRACE
3970 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3976 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3977 size_t cnt, loff_t *ppos)
3979 static char ftrace_dyn_info_buffer[1024];
3980 static DEFINE_MUTEX(dyn_info_mutex);
3981 unsigned long *p = filp->private_data;
3982 char *buf = ftrace_dyn_info_buffer;
3983 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3986 mutex_lock(&dyn_info_mutex);
3987 r = sprintf(buf, "%ld ", *p);
3989 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3992 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3994 mutex_unlock(&dyn_info_mutex);
3999 static const struct file_operations tracing_dyn_info_fops = {
4000 .open = tracing_open_generic,
4001 .read = tracing_read_dyn_info,
4002 .llseek = generic_file_llseek,
4006 static struct dentry *d_tracer;
4008 struct dentry *tracing_init_dentry(void)
4015 if (!debugfs_initialized())
4018 d_tracer = debugfs_create_dir("tracing", NULL);
4020 if (!d_tracer && !once) {
4022 pr_warning("Could not create debugfs directory 'tracing'\n");
4029 static struct dentry *d_percpu;
4031 struct dentry *tracing_dentry_percpu(void)
4034 struct dentry *d_tracer;
4039 d_tracer = tracing_init_dentry();
4044 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4046 if (!d_percpu && !once) {
4048 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4055 static void tracing_init_debugfs_percpu(long cpu)
4057 struct dentry *d_percpu = tracing_dentry_percpu();
4058 struct dentry *d_cpu;
4059 char cpu_dir[30]; /* 30 characters should be more than enough */
4061 snprintf(cpu_dir, 30, "cpu%ld", cpu);
4062 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4064 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4068 /* per cpu trace_pipe */
4069 trace_create_file("trace_pipe", 0444, d_cpu,
4070 (void *) cpu, &tracing_pipe_fops);
4073 trace_create_file("trace", 0644, d_cpu,
4074 (void *) cpu, &tracing_fops);
4076 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4077 (void *) cpu, &tracing_buffers_fops);
4079 trace_create_file("stats", 0444, d_cpu,
4080 (void *) cpu, &tracing_stats_fops);
4083 #ifdef CONFIG_FTRACE_SELFTEST
4084 /* Let selftest have access to static functions in this file */
4085 #include "trace_selftest.c"
4088 struct trace_option_dentry {
4089 struct tracer_opt *opt;
4090 struct tracer_flags *flags;
4091 struct dentry *entry;
4095 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4098 struct trace_option_dentry *topt = filp->private_data;
4101 if (topt->flags->val & topt->opt->bit)
4106 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4110 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4113 struct trace_option_dentry *topt = filp->private_data;
4117 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4121 if (val != 0 && val != 1)
4124 if (!!(topt->flags->val & topt->opt->bit) != val) {
4125 mutex_lock(&trace_types_lock);
4126 ret = __set_tracer_option(current_trace, topt->flags,
4128 mutex_unlock(&trace_types_lock);
4139 static const struct file_operations trace_options_fops = {
4140 .open = tracing_open_generic,
4141 .read = trace_options_read,
4142 .write = trace_options_write,
4143 .llseek = generic_file_llseek,
4147 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4150 long index = (long)filp->private_data;
4153 if (trace_flags & (1 << index))
4158 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4162 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4165 long index = (long)filp->private_data;
4169 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4173 if (val != 0 && val != 1)
4175 set_tracer_flags(1 << index, val);
4182 static const struct file_operations trace_options_core_fops = {
4183 .open = tracing_open_generic,
4184 .read = trace_options_core_read,
4185 .write = trace_options_core_write,
4186 .llseek = generic_file_llseek,
4189 struct dentry *trace_create_file(const char *name,
4191 struct dentry *parent,
4193 const struct file_operations *fops)
4197 ret = debugfs_create_file(name, mode, parent, data, fops);
4199 pr_warning("Could not create debugfs '%s' entry\n", name);
4205 static struct dentry *trace_options_init_dentry(void)
4207 struct dentry *d_tracer;
4208 static struct dentry *t_options;
4213 d_tracer = tracing_init_dentry();
4217 t_options = debugfs_create_dir("options", d_tracer);
4219 pr_warning("Could not create debugfs directory 'options'\n");
4227 create_trace_option_file(struct trace_option_dentry *topt,
4228 struct tracer_flags *flags,
4229 struct tracer_opt *opt)
4231 struct dentry *t_options;
4233 t_options = trace_options_init_dentry();
4237 topt->flags = flags;
4240 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4241 &trace_options_fops);
4245 static struct trace_option_dentry *
4246 create_trace_option_files(struct tracer *tracer)
4248 struct trace_option_dentry *topts;
4249 struct tracer_flags *flags;
4250 struct tracer_opt *opts;
4256 flags = tracer->flags;
4258 if (!flags || !flags->opts)
4263 for (cnt = 0; opts[cnt].name; cnt++)
4266 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4270 for (cnt = 0; opts[cnt].name; cnt++)
4271 create_trace_option_file(&topts[cnt], flags,
4278 destroy_trace_option_files(struct trace_option_dentry *topts)
4285 for (cnt = 0; topts[cnt].opt; cnt++) {
4286 if (topts[cnt].entry)
4287 debugfs_remove(topts[cnt].entry);
4293 static struct dentry *
4294 create_trace_option_core_file(const char *option, long index)
4296 struct dentry *t_options;
4298 t_options = trace_options_init_dentry();
4302 return trace_create_file(option, 0644, t_options, (void *)index,
4303 &trace_options_core_fops);
4306 static __init void create_trace_options_dir(void)
4308 struct dentry *t_options;
4311 t_options = trace_options_init_dentry();
4315 for (i = 0; trace_options[i]; i++)
4316 create_trace_option_core_file(trace_options[i], i);
4319 static __init int tracer_init_debugfs(void)
4321 struct dentry *d_tracer;
4324 trace_access_lock_init();
4326 d_tracer = tracing_init_dentry();
4328 trace_create_file("tracing_enabled", 0644, d_tracer,
4329 &global_trace, &tracing_ctrl_fops);
4331 trace_create_file("trace_options", 0644, d_tracer,
4332 NULL, &tracing_iter_fops);
4334 trace_create_file("tracing_cpumask", 0644, d_tracer,
4335 NULL, &tracing_cpumask_fops);
4337 trace_create_file("trace", 0644, d_tracer,
4338 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4340 trace_create_file("available_tracers", 0444, d_tracer,
4341 &global_trace, &show_traces_fops);
4343 trace_create_file("current_tracer", 0644, d_tracer,
4344 &global_trace, &set_tracer_fops);
4346 #ifdef CONFIG_TRACER_MAX_TRACE
4347 trace_create_file("tracing_max_latency", 0644, d_tracer,
4348 &tracing_max_latency, &tracing_max_lat_fops);
4351 trace_create_file("tracing_thresh", 0644, d_tracer,
4352 &tracing_thresh, &tracing_max_lat_fops);
4354 trace_create_file("README", 0444, d_tracer,
4355 NULL, &tracing_readme_fops);
4357 trace_create_file("trace_pipe", 0444, d_tracer,
4358 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4360 trace_create_file("buffer_size_kb", 0644, d_tracer,
4361 &global_trace, &tracing_entries_fops);
4363 trace_create_file("free_buffer", 0644, d_tracer,
4364 &global_trace, &tracing_free_buffer_fops);
4366 trace_create_file("trace_marker", 0220, d_tracer,
4367 NULL, &tracing_mark_fops);
4369 trace_create_file("saved_cmdlines", 0444, d_tracer,
4370 NULL, &tracing_saved_cmdlines_fops);
4372 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4375 #ifdef CONFIG_DYNAMIC_FTRACE
4376 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4377 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4380 create_trace_options_dir();
4382 for_each_tracing_cpu(cpu)
4383 tracing_init_debugfs_percpu(cpu);
4388 static int trace_panic_handler(struct notifier_block *this,
4389 unsigned long event, void *unused)
4391 if (ftrace_dump_on_oops)
4392 ftrace_dump(ftrace_dump_on_oops);
4396 static struct notifier_block trace_panic_notifier = {
4397 .notifier_call = trace_panic_handler,
4399 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4402 static int trace_die_handler(struct notifier_block *self,
4408 if (ftrace_dump_on_oops)
4409 ftrace_dump(ftrace_dump_on_oops);
4417 static struct notifier_block trace_die_notifier = {
4418 .notifier_call = trace_die_handler,
4423 * printk is set to max of 1024, we really don't need it that big.
4424 * Nothing should be printing 1000 characters anyway.
4426 #define TRACE_MAX_PRINT 1000
4429 * Define here KERN_TRACE so that we have one place to modify
4430 * it if we decide to change what log level the ftrace dump
4433 #define KERN_TRACE KERN_EMERG
4436 trace_printk_seq(struct trace_seq *s)
4438 /* Probably should print a warning here. */
4442 /* should be zero ended, but we are paranoid. */
4443 s->buffer[s->len] = 0;
4445 printk(KERN_TRACE "%s", s->buffer);
4450 void trace_init_global_iter(struct trace_iterator *iter)
4452 iter->tr = &global_trace;
4453 iter->trace = current_trace;
4454 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4458 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
4460 static arch_spinlock_t ftrace_dump_lock =
4461 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
4462 /* use static because iter can be a bit big for the stack */
4463 static struct trace_iterator iter;
4464 unsigned int old_userobj;
4465 static int dump_ran;
4466 unsigned long flags;
4470 local_irq_save(flags);
4471 arch_spin_lock(&ftrace_dump_lock);
4479 if (disable_tracing)
4482 trace_init_global_iter(&iter);
4484 for_each_tracing_cpu(cpu) {
4485 atomic_inc(&iter.tr->data[cpu]->disabled);
4488 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4490 /* don't look at user memory in panic mode */
4491 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4493 /* Simulate the iterator */
4494 iter.tr = &global_trace;
4495 iter.trace = current_trace;
4497 switch (oops_dump_mode) {
4499 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4502 iter.cpu_file = raw_smp_processor_id();
4507 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4508 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4511 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4514 * We need to stop all tracing on all CPUS to read the
4515 * the next buffer. This is a bit expensive, but is
4516 * not done often. We fill all what we can read,
4517 * and then release the locks again.
4520 while (!trace_empty(&iter)) {
4523 printk(KERN_TRACE "---------------------------------\n");
4527 /* reset all but tr, trace, and overruns */
4528 memset(&iter.seq, 0,
4529 sizeof(struct trace_iterator) -
4530 offsetof(struct trace_iterator, seq));
4531 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4534 if (trace_find_next_entry_inc(&iter) != NULL) {
4537 ret = print_trace_line(&iter);
4538 if (ret != TRACE_TYPE_NO_CONSUME)
4539 trace_consume(&iter);
4542 trace_printk_seq(&iter.seq);
4546 printk(KERN_TRACE " (ftrace buffer empty)\n");
4548 printk(KERN_TRACE "---------------------------------\n");
4551 /* Re-enable tracing if requested */
4552 if (!disable_tracing) {
4553 trace_flags |= old_userobj;
4555 for_each_tracing_cpu(cpu) {
4556 atomic_dec(&iter.tr->data[cpu]->disabled);
4562 arch_spin_unlock(&ftrace_dump_lock);
4563 local_irq_restore(flags);
4566 /* By default: disable tracing after the dump */
4567 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
4569 __ftrace_dump(true, oops_dump_mode);
4572 __init static int tracer_alloc_buffers(void)
4575 enum ring_buffer_flags rb_flags;
4580 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4583 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4584 goto out_free_buffer_mask;
4586 /* To save memory, keep the ring buffer size to its minimum */
4587 if (ring_buffer_expanded)
4588 ring_buf_size = trace_buf_size;
4592 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
4594 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4595 cpumask_copy(tracing_cpumask, cpu_all_mask);
4597 /* TODO: make the number of buffers hot pluggable with CPUS */
4598 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
4599 if (!global_trace.buffer) {
4600 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4602 goto out_free_cpumask;
4604 global_trace.entries = ring_buffer_size(global_trace.buffer);
4607 #ifdef CONFIG_TRACER_MAX_TRACE
4608 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
4609 if (!max_tr.buffer) {
4610 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4612 ring_buffer_free(global_trace.buffer);
4613 goto out_free_cpumask;
4618 /* Allocate the first page for all buffers */
4619 for_each_tracing_cpu(i) {
4620 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4621 max_tr.data[i] = &per_cpu(max_tr_data, i);
4624 trace_init_cmdlines();
4626 register_tracer(&nop_trace);
4627 current_trace = &nop_trace;
4628 /* All seems OK, enable tracing */
4629 tracing_disabled = 0;
4631 atomic_notifier_chain_register(&panic_notifier_list,
4632 &trace_panic_notifier);
4634 register_die_notifier(&trace_die_notifier);
4639 free_cpumask_var(tracing_cpumask);
4640 out_free_buffer_mask:
4641 free_cpumask_var(tracing_buffer_mask);
4646 __init static int clear_boot_tracer(void)
4649 * The default tracer at boot buffer is an init section.
4650 * This function is called in lateinit. If we did not
4651 * find the boot tracer, then clear it out, to prevent
4652 * later registration from accessing the buffer that is
4653 * about to be freed.
4655 if (!default_bootup_tracer)
4658 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4659 default_bootup_tracer);
4660 default_bootup_tracer = NULL;
4665 early_initcall(tracer_alloc_buffers);
4666 fs_initcall(tracer_init_debugfs);
4667 late_initcall(clear_boot_tracer);