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/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/rwsem.h>
36 #include <linux/slab.h>
37 #include <linux/ctype.h>
38 #include <linux/init.h>
39 #include <linux/poll.h>
43 #include "trace_output.h"
45 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
48 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
51 int ring_buffer_expanded;
54 * We need to change this state when a selftest is running.
55 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
57 * insertions into the ring-buffer such as trace_printk could occurred
58 * at the same time, giving false positive or negative results.
60 static bool __read_mostly tracing_selftest_running;
63 * If a tracer is running, we do not want to run SELFTEST.
65 bool __read_mostly tracing_selftest_disabled;
67 /* For tracers that don't implement custom flags */
68 static struct tracer_opt dummy_tracer_opt[] = {
72 static struct tracer_flags dummy_tracer_flags = {
74 .opts = dummy_tracer_opt
77 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
83 * Kill all tracing for good (never come back).
84 * It is initialized to 1 but will turn to zero if the initialization
85 * of the tracer is successful. But that is the only place that sets
88 static int tracing_disabled = 1;
90 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
92 static inline void ftrace_disable_cpu(void)
95 __this_cpu_inc(ftrace_cpu_disabled);
98 static inline void ftrace_enable_cpu(void)
100 __this_cpu_dec(ftrace_cpu_disabled);
104 static cpumask_var_t __read_mostly tracing_buffer_mask;
106 #define for_each_tracing_cpu(cpu) \
107 for_each_cpu(cpu, tracing_buffer_mask)
110 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
112 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
113 * is set, then ftrace_dump is called. This will output the contents
114 * of the ftrace buffers to the console. This is very useful for
115 * capturing traces that lead to crashes and outputing it to a
118 * It is default off, but you can enable it with either specifying
119 * "ftrace_dump_on_oops" in the kernel command line, or setting
120 * /proc/sys/kernel/ftrace_dump_on_oops to true.
122 int ftrace_dump_on_oops;
124 static int tracing_set_tracer(const char *buf);
126 #define MAX_TRACER_SIZE 100
127 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
128 static char *default_bootup_tracer;
130 static int __init set_cmdline_ftrace(char *str)
132 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
133 default_bootup_tracer = bootup_tracer_buf;
134 /* We are using ftrace early, expand it */
135 ring_buffer_expanded = 1;
138 __setup("ftrace=", set_cmdline_ftrace);
140 static int __init set_ftrace_dump_on_oops(char *str)
142 ftrace_dump_on_oops = 1;
145 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
147 unsigned long long ns2usecs(cycle_t nsec)
155 * The global_trace is the descriptor that holds the tracing
156 * buffers for the live tracing. For each CPU, it contains
157 * a link list of pages that will store trace entries. The
158 * page descriptor of the pages in the memory is used to hold
159 * the link list by linking the lru item in the page descriptor
160 * to each of the pages in the buffer per CPU.
162 * For each active CPU there is a data field that holds the
163 * pages for the buffer for that CPU. Each CPU has the same number
164 * of pages allocated for its buffer.
166 static struct trace_array global_trace;
168 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
170 int filter_current_check_discard(struct ring_buffer *buffer,
171 struct ftrace_event_call *call, void *rec,
172 struct ring_buffer_event *event)
174 return filter_check_discard(call, rec, buffer, event);
176 EXPORT_SYMBOL_GPL(filter_current_check_discard);
178 cycle_t ftrace_now(int cpu)
182 /* Early boot up does not have a buffer yet */
183 if (!global_trace.buffer)
184 return trace_clock_local();
186 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
187 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
193 * The max_tr is used to snapshot the global_trace when a maximum
194 * latency is reached. Some tracers will use this to store a maximum
195 * trace while it continues examining live traces.
197 * The buffers for the max_tr are set up the same as the global_trace.
198 * When a snapshot is taken, the link list of the max_tr is swapped
199 * with the link list of the global_trace and the buffers are reset for
200 * the global_trace so the tracing can continue.
202 static struct trace_array max_tr;
204 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
206 /* tracer_enabled is used to toggle activation of a tracer */
207 static int tracer_enabled = 1;
210 * tracing_is_enabled - return tracer_enabled status
212 * This function is used by other tracers to know the status
213 * of the tracer_enabled flag. Tracers may use this function
214 * to know if it should enable their features when starting
215 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
217 int tracing_is_enabled(void)
219 return tracer_enabled;
223 * trace_buf_size is the size in bytes that is allocated
224 * for a buffer. Note, the number of bytes is always rounded
227 * This number is purposely set to a low number of 16384.
228 * If the dump on oops happens, it will be much appreciated
229 * to not have to wait for all that output. Anyway this can be
230 * boot time and run time configurable.
232 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
234 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
236 /* trace_types holds a link list of available tracers. */
237 static struct tracer *trace_types __read_mostly;
239 /* current_trace points to the tracer that is currently active */
240 static struct tracer *current_trace __read_mostly;
243 * trace_types_lock is used to protect the trace_types list.
245 static DEFINE_MUTEX(trace_types_lock);
248 * serialize the access of the ring buffer
250 * ring buffer serializes readers, but it is low level protection.
251 * The validity of the events (which returns by ring_buffer_peek() ..etc)
252 * are not protected by ring buffer.
254 * The content of events may become garbage if we allow other process consumes
255 * these events concurrently:
256 * A) the page of the consumed events may become a normal page
257 * (not reader page) in ring buffer, and this page will be rewrited
258 * by events producer.
259 * B) The page of the consumed events may become a page for splice_read,
260 * and this page will be returned to system.
262 * These primitives allow multi process access to different cpu ring buffer
265 * These primitives don't distinguish read-only and read-consume access.
266 * Multi read-only access are also serialized.
270 static DECLARE_RWSEM(all_cpu_access_lock);
271 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
273 static inline void trace_access_lock(int cpu)
275 if (cpu == TRACE_PIPE_ALL_CPU) {
276 /* gain it for accessing the whole ring buffer. */
277 down_write(&all_cpu_access_lock);
279 /* gain it for accessing a cpu ring buffer. */
281 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
282 down_read(&all_cpu_access_lock);
284 /* Secondly block other access to this @cpu ring buffer. */
285 mutex_lock(&per_cpu(cpu_access_lock, cpu));
289 static inline void trace_access_unlock(int cpu)
291 if (cpu == TRACE_PIPE_ALL_CPU) {
292 up_write(&all_cpu_access_lock);
294 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
295 up_read(&all_cpu_access_lock);
299 static inline void trace_access_lock_init(void)
303 for_each_possible_cpu(cpu)
304 mutex_init(&per_cpu(cpu_access_lock, cpu));
309 static DEFINE_MUTEX(access_lock);
311 static inline void trace_access_lock(int cpu)
314 mutex_lock(&access_lock);
317 static inline void trace_access_unlock(int cpu)
320 mutex_unlock(&access_lock);
323 static inline void trace_access_lock_init(void)
329 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
330 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
332 /* trace_flags holds trace_options default values */
333 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
334 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
335 TRACE_ITER_GRAPH_TIME;
337 static int trace_stop_count;
338 static DEFINE_SPINLOCK(tracing_start_lock);
341 * trace_wake_up - wake up tasks waiting for trace input
343 * Simply wakes up any task that is blocked on the trace_wait
344 * queue. These is used with trace_poll for tasks polling the trace.
346 void trace_wake_up(void)
350 if (trace_flags & TRACE_ITER_BLOCK)
353 * The runqueue_is_locked() can fail, but this is the best we
357 if (!runqueue_is_locked(cpu))
358 wake_up(&trace_wait);
362 static int __init set_buf_size(char *str)
364 unsigned long buf_size;
368 buf_size = memparse(str, &str);
369 /* nr_entries can not be zero */
372 trace_buf_size = buf_size;
375 __setup("trace_buf_size=", set_buf_size);
377 static int __init set_tracing_thresh(char *str)
379 unsigned long threshhold;
384 ret = strict_strtoul(str, 0, &threshhold);
387 tracing_thresh = threshhold * 1000;
390 __setup("tracing_thresh=", set_tracing_thresh);
392 unsigned long nsecs_to_usecs(unsigned long nsecs)
397 /* These must match the bit postions in trace_iterator_flags */
398 static const char *trace_options[] = {
426 { trace_clock_local, "local" },
427 { trace_clock_global, "global" },
433 * trace_parser_get_init - gets the buffer for trace parser
435 int trace_parser_get_init(struct trace_parser *parser, int size)
437 memset(parser, 0, sizeof(*parser));
439 parser->buffer = kmalloc(size, GFP_KERNEL);
448 * trace_parser_put - frees the buffer for trace parser
450 void trace_parser_put(struct trace_parser *parser)
452 kfree(parser->buffer);
456 * trace_get_user - reads the user input string separated by space
457 * (matched by isspace(ch))
459 * For each string found the 'struct trace_parser' is updated,
460 * and the function returns.
462 * Returns number of bytes read.
464 * See kernel/trace/trace.h for 'struct trace_parser' details.
466 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
467 size_t cnt, loff_t *ppos)
474 trace_parser_clear(parser);
476 ret = get_user(ch, ubuf++);
484 * The parser is not finished with the last write,
485 * continue reading the user input without skipping spaces.
488 /* skip white space */
489 while (cnt && isspace(ch)) {
490 ret = get_user(ch, ubuf++);
497 /* only spaces were written */
507 /* read the non-space input */
508 while (cnt && !isspace(ch)) {
509 if (parser->idx < parser->size - 1)
510 parser->buffer[parser->idx++] = ch;
515 ret = get_user(ch, ubuf++);
522 /* We either got finished input or we have to wait for another call. */
524 parser->buffer[parser->idx] = 0;
525 parser->cont = false;
528 parser->buffer[parser->idx++] = ch;
538 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
546 if (s->len <= s->readpos)
549 len = s->len - s->readpos;
552 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
562 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
567 if (s->len <= s->readpos)
570 len = s->len - s->readpos;
573 ret = memcpy(buf, s->buffer + s->readpos, cnt);
582 * ftrace_max_lock is used to protect the swapping of buffers
583 * when taking a max snapshot. The buffers themselves are
584 * protected by per_cpu spinlocks. But the action of the swap
585 * needs its own lock.
587 * This is defined as a arch_spinlock_t in order to help
588 * with performance when lockdep debugging is enabled.
590 * It is also used in other places outside the update_max_tr
591 * so it needs to be defined outside of the
592 * CONFIG_TRACER_MAX_TRACE.
594 static arch_spinlock_t ftrace_max_lock =
595 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
597 unsigned long __read_mostly tracing_thresh;
599 #ifdef CONFIG_TRACER_MAX_TRACE
600 unsigned long __read_mostly tracing_max_latency;
603 * Copy the new maximum trace into the separate maximum-trace
604 * structure. (this way the maximum trace is permanently saved,
605 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
608 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
610 struct trace_array_cpu *data = tr->data[cpu];
611 struct trace_array_cpu *max_data;
614 max_tr.time_start = data->preempt_timestamp;
616 max_data = max_tr.data[cpu];
617 max_data->saved_latency = tracing_max_latency;
618 max_data->critical_start = data->critical_start;
619 max_data->critical_end = data->critical_end;
621 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
622 max_data->pid = tsk->pid;
623 max_data->uid = task_uid(tsk);
624 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
625 max_data->policy = tsk->policy;
626 max_data->rt_priority = tsk->rt_priority;
628 /* record this tasks comm */
629 tracing_record_cmdline(tsk);
633 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
635 * @tsk: the task with the latency
636 * @cpu: The cpu that initiated the trace.
638 * Flip the buffers between the @tr and the max_tr and record information
639 * about which task was the cause of this latency.
642 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
644 struct ring_buffer *buf = tr->buffer;
646 if (trace_stop_count)
649 WARN_ON_ONCE(!irqs_disabled());
650 arch_spin_lock(&ftrace_max_lock);
652 tr->buffer = max_tr.buffer;
655 __update_max_tr(tr, tsk, cpu);
656 arch_spin_unlock(&ftrace_max_lock);
660 * update_max_tr_single - only copy one trace over, and reset the rest
662 * @tsk - task with the latency
663 * @cpu - the cpu of the buffer to copy.
665 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
668 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
672 if (trace_stop_count)
675 WARN_ON_ONCE(!irqs_disabled());
676 arch_spin_lock(&ftrace_max_lock);
678 ftrace_disable_cpu();
680 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
684 * We failed to swap the buffer due to a commit taking
685 * place on this CPU. We fail to record, but we reset
686 * the max trace buffer (no one writes directly to it)
687 * and flag that it failed.
689 trace_array_printk(&max_tr, _THIS_IP_,
690 "Failed to swap buffers due to commit in progress\n");
695 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
697 __update_max_tr(tr, tsk, cpu);
698 arch_spin_unlock(&ftrace_max_lock);
700 #endif /* CONFIG_TRACER_MAX_TRACE */
703 * register_tracer - register a tracer with the ftrace system.
704 * @type - the plugin for the tracer
706 * Register a new plugin tracer.
708 int register_tracer(struct tracer *type)
709 __releases(kernel_lock)
710 __acquires(kernel_lock)
716 pr_info("Tracer must have a name\n");
720 if (strlen(type->name) > MAX_TRACER_SIZE) {
721 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
726 * When this gets called we hold the BKL which means that
727 * preemption is disabled. Various trace selftests however
728 * need to disable and enable preemption for successful tests.
729 * So we drop the BKL here and grab it after the tests again.
732 mutex_lock(&trace_types_lock);
734 tracing_selftest_running = true;
736 for (t = trace_types; t; t = t->next) {
737 if (strcmp(type->name, t->name) == 0) {
739 pr_info("Tracer %s already registered\n",
747 type->set_flag = &dummy_set_flag;
749 type->flags = &dummy_tracer_flags;
751 if (!type->flags->opts)
752 type->flags->opts = dummy_tracer_opt;
753 if (!type->wait_pipe)
754 type->wait_pipe = default_wait_pipe;
757 #ifdef CONFIG_FTRACE_STARTUP_TEST
758 if (type->selftest && !tracing_selftest_disabled) {
759 struct tracer *saved_tracer = current_trace;
760 struct trace_array *tr = &global_trace;
763 * Run a selftest on this tracer.
764 * Here we reset the trace buffer, and set the current
765 * tracer to be this tracer. The tracer can then run some
766 * internal tracing to verify that everything is in order.
767 * If we fail, we do not register this tracer.
769 tracing_reset_online_cpus(tr);
771 current_trace = type;
772 /* the test is responsible for initializing and enabling */
773 pr_info("Testing tracer %s: ", type->name);
774 ret = type->selftest(type, tr);
775 /* the test is responsible for resetting too */
776 current_trace = saved_tracer;
778 printk(KERN_CONT "FAILED!\n");
781 /* Only reset on passing, to avoid touching corrupted buffers */
782 tracing_reset_online_cpus(tr);
784 printk(KERN_CONT "PASSED\n");
788 type->next = trace_types;
792 tracing_selftest_running = false;
793 mutex_unlock(&trace_types_lock);
795 if (ret || !default_bootup_tracer)
798 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
801 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
802 /* Do we want this tracer to start on bootup? */
803 tracing_set_tracer(type->name);
804 default_bootup_tracer = NULL;
805 /* disable other selftests, since this will break it. */
806 tracing_selftest_disabled = 1;
807 #ifdef CONFIG_FTRACE_STARTUP_TEST
808 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
817 void unregister_tracer(struct tracer *type)
821 mutex_lock(&trace_types_lock);
822 for (t = &trace_types; *t; t = &(*t)->next) {
826 pr_info("Tracer %s not registered\n", type->name);
832 if (type == current_trace && tracer_enabled) {
835 if (current_trace->stop)
836 current_trace->stop(&global_trace);
837 current_trace = &nop_trace;
840 mutex_unlock(&trace_types_lock);
843 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
845 ftrace_disable_cpu();
846 ring_buffer_reset_cpu(buffer, cpu);
850 void tracing_reset(struct trace_array *tr, int cpu)
852 struct ring_buffer *buffer = tr->buffer;
854 ring_buffer_record_disable(buffer);
856 /* Make sure all commits have finished */
858 __tracing_reset(buffer, cpu);
860 ring_buffer_record_enable(buffer);
863 void tracing_reset_online_cpus(struct trace_array *tr)
865 struct ring_buffer *buffer = tr->buffer;
868 ring_buffer_record_disable(buffer);
870 /* Make sure all commits have finished */
873 tr->time_start = ftrace_now(tr->cpu);
875 for_each_online_cpu(cpu)
876 __tracing_reset(buffer, cpu);
878 ring_buffer_record_enable(buffer);
881 void tracing_reset_current(int cpu)
883 tracing_reset(&global_trace, cpu);
886 void tracing_reset_current_online_cpus(void)
888 tracing_reset_online_cpus(&global_trace);
891 #define SAVED_CMDLINES 128
892 #define NO_CMDLINE_MAP UINT_MAX
893 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
894 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
895 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
896 static int cmdline_idx;
897 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
899 /* temporary disable recording */
900 static atomic_t trace_record_cmdline_disabled __read_mostly;
902 static void trace_init_cmdlines(void)
904 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
905 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
909 int is_tracing_stopped(void)
911 return trace_stop_count;
915 * ftrace_off_permanent - disable all ftrace code permanently
917 * This should only be called when a serious anomally has
918 * been detected. This will turn off the function tracing,
919 * ring buffers, and other tracing utilites. It takes no
920 * locks and can be called from any context.
922 void ftrace_off_permanent(void)
924 tracing_disabled = 1;
926 tracing_off_permanent();
930 * tracing_start - quick start of the tracer
932 * If tracing is enabled but was stopped by tracing_stop,
933 * this will start the tracer back up.
935 void tracing_start(void)
937 struct ring_buffer *buffer;
940 if (tracing_disabled)
943 spin_lock_irqsave(&tracing_start_lock, flags);
944 if (--trace_stop_count) {
945 if (trace_stop_count < 0) {
946 /* Someone screwed up their debugging */
948 trace_stop_count = 0;
953 /* Prevent the buffers from switching */
954 arch_spin_lock(&ftrace_max_lock);
956 buffer = global_trace.buffer;
958 ring_buffer_record_enable(buffer);
960 buffer = max_tr.buffer;
962 ring_buffer_record_enable(buffer);
964 arch_spin_unlock(&ftrace_max_lock);
968 spin_unlock_irqrestore(&tracing_start_lock, flags);
972 * tracing_stop - quick stop of the tracer
974 * Light weight way to stop tracing. Use in conjunction with
977 void tracing_stop(void)
979 struct ring_buffer *buffer;
983 spin_lock_irqsave(&tracing_start_lock, flags);
984 if (trace_stop_count++)
987 /* Prevent the buffers from switching */
988 arch_spin_lock(&ftrace_max_lock);
990 buffer = global_trace.buffer;
992 ring_buffer_record_disable(buffer);
994 buffer = max_tr.buffer;
996 ring_buffer_record_disable(buffer);
998 arch_spin_unlock(&ftrace_max_lock);
1001 spin_unlock_irqrestore(&tracing_start_lock, flags);
1004 void trace_stop_cmdline_recording(void);
1006 static void trace_save_cmdline(struct task_struct *tsk)
1010 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1014 * It's not the end of the world if we don't get
1015 * the lock, but we also don't want to spin
1016 * nor do we want to disable interrupts,
1017 * so if we miss here, then better luck next time.
1019 if (!arch_spin_trylock(&trace_cmdline_lock))
1022 idx = map_pid_to_cmdline[tsk->pid];
1023 if (idx == NO_CMDLINE_MAP) {
1024 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1027 * Check whether the cmdline buffer at idx has a pid
1028 * mapped. We are going to overwrite that entry so we
1029 * need to clear the map_pid_to_cmdline. Otherwise we
1030 * would read the new comm for the old pid.
1032 pid = map_cmdline_to_pid[idx];
1033 if (pid != NO_CMDLINE_MAP)
1034 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1036 map_cmdline_to_pid[idx] = tsk->pid;
1037 map_pid_to_cmdline[tsk->pid] = idx;
1042 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1044 arch_spin_unlock(&trace_cmdline_lock);
1047 void trace_find_cmdline(int pid, char comm[])
1052 strcpy(comm, "<idle>");
1056 if (WARN_ON_ONCE(pid < 0)) {
1057 strcpy(comm, "<XXX>");
1061 if (pid > PID_MAX_DEFAULT) {
1062 strcpy(comm, "<...>");
1067 arch_spin_lock(&trace_cmdline_lock);
1068 map = map_pid_to_cmdline[pid];
1069 if (map != NO_CMDLINE_MAP)
1070 strcpy(comm, saved_cmdlines[map]);
1072 strcpy(comm, "<...>");
1074 arch_spin_unlock(&trace_cmdline_lock);
1078 void tracing_record_cmdline(struct task_struct *tsk)
1080 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1084 trace_save_cmdline(tsk);
1088 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1091 struct task_struct *tsk = current;
1093 entry->preempt_count = pc & 0xff;
1094 entry->pid = (tsk) ? tsk->pid : 0;
1095 entry->lock_depth = (tsk) ? tsk->lock_depth : 0;
1097 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1098 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1100 TRACE_FLAG_IRQS_NOSUPPORT |
1102 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1103 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1104 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1106 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1108 struct ring_buffer_event *
1109 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1112 unsigned long flags, int pc)
1114 struct ring_buffer_event *event;
1116 event = ring_buffer_lock_reserve(buffer, len);
1117 if (event != NULL) {
1118 struct trace_entry *ent = ring_buffer_event_data(event);
1120 tracing_generic_entry_update(ent, flags, pc);
1128 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1129 struct ring_buffer_event *event,
1130 unsigned long flags, int pc,
1133 ring_buffer_unlock_commit(buffer, event);
1135 ftrace_trace_stack(buffer, flags, 6, pc);
1136 ftrace_trace_userstack(buffer, flags, pc);
1142 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1143 struct ring_buffer_event *event,
1144 unsigned long flags, int pc)
1146 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1149 struct ring_buffer_event *
1150 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1151 int type, unsigned long len,
1152 unsigned long flags, int pc)
1154 *current_rb = global_trace.buffer;
1155 return trace_buffer_lock_reserve(*current_rb,
1156 type, len, flags, pc);
1158 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1160 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1161 struct ring_buffer_event *event,
1162 unsigned long flags, int pc)
1164 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1166 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1168 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1169 struct ring_buffer_event *event,
1170 unsigned long flags, int pc)
1172 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1174 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1176 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1177 struct ring_buffer_event *event)
1179 ring_buffer_discard_commit(buffer, event);
1181 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1184 trace_function(struct trace_array *tr,
1185 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1188 struct ftrace_event_call *call = &event_function;
1189 struct ring_buffer *buffer = tr->buffer;
1190 struct ring_buffer_event *event;
1191 struct ftrace_entry *entry;
1193 /* If we are reading the ring buffer, don't trace */
1194 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1197 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1201 entry = ring_buffer_event_data(event);
1203 entry->parent_ip = parent_ip;
1205 if (!filter_check_discard(call, entry, buffer, event))
1206 ring_buffer_unlock_commit(buffer, event);
1210 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1211 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1214 if (likely(!atomic_read(&data->disabled)))
1215 trace_function(tr, ip, parent_ip, flags, pc);
1218 #ifdef CONFIG_STACKTRACE
1219 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1220 unsigned long flags,
1223 struct ftrace_event_call *call = &event_kernel_stack;
1224 struct ring_buffer_event *event;
1225 struct stack_entry *entry;
1226 struct stack_trace trace;
1228 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1229 sizeof(*entry), flags, pc);
1232 entry = ring_buffer_event_data(event);
1233 memset(&entry->caller, 0, sizeof(entry->caller));
1235 trace.nr_entries = 0;
1236 trace.max_entries = FTRACE_STACK_ENTRIES;
1238 trace.entries = entry->caller;
1240 save_stack_trace(&trace);
1241 if (!filter_check_discard(call, entry, buffer, event))
1242 ring_buffer_unlock_commit(buffer, event);
1245 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1248 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1251 __ftrace_trace_stack(buffer, flags, skip, pc);
1254 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1257 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
1261 * trace_dump_stack - record a stack back trace in the trace buffer
1263 void trace_dump_stack(void)
1265 unsigned long flags;
1267 if (tracing_disabled || tracing_selftest_running)
1270 local_save_flags(flags);
1272 /* skipping 3 traces, seems to get us at the caller of this function */
1273 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1277 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1279 struct ftrace_event_call *call = &event_user_stack;
1280 struct ring_buffer_event *event;
1281 struct userstack_entry *entry;
1282 struct stack_trace trace;
1284 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1288 * NMIs can not handle page faults, even with fix ups.
1289 * The save user stack can (and often does) fault.
1291 if (unlikely(in_nmi()))
1294 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1295 sizeof(*entry), flags, pc);
1298 entry = ring_buffer_event_data(event);
1300 entry->tgid = current->tgid;
1301 memset(&entry->caller, 0, sizeof(entry->caller));
1303 trace.nr_entries = 0;
1304 trace.max_entries = FTRACE_STACK_ENTRIES;
1306 trace.entries = entry->caller;
1308 save_stack_trace_user(&trace);
1309 if (!filter_check_discard(call, entry, buffer, event))
1310 ring_buffer_unlock_commit(buffer, event);
1314 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1316 ftrace_trace_userstack(tr, flags, preempt_count());
1320 #endif /* CONFIG_STACKTRACE */
1323 ftrace_trace_special(void *__tr,
1324 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1327 struct ftrace_event_call *call = &event_special;
1328 struct ring_buffer_event *event;
1329 struct trace_array *tr = __tr;
1330 struct ring_buffer *buffer = tr->buffer;
1331 struct special_entry *entry;
1333 event = trace_buffer_lock_reserve(buffer, TRACE_SPECIAL,
1334 sizeof(*entry), 0, pc);
1337 entry = ring_buffer_event_data(event);
1342 if (!filter_check_discard(call, entry, buffer, event))
1343 trace_buffer_unlock_commit(buffer, event, 0, pc);
1347 __trace_special(void *__tr, void *__data,
1348 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1350 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1354 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1356 struct trace_array *tr = &global_trace;
1357 struct trace_array_cpu *data;
1358 unsigned long flags;
1362 if (tracing_disabled)
1365 pc = preempt_count();
1366 local_irq_save(flags);
1367 cpu = raw_smp_processor_id();
1368 data = tr->data[cpu];
1370 if (likely(atomic_inc_return(&data->disabled) == 1))
1371 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1373 atomic_dec(&data->disabled);
1374 local_irq_restore(flags);
1378 * trace_vbprintk - write binary msg to tracing buffer
1381 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1383 static arch_spinlock_t trace_buf_lock =
1384 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1385 static u32 trace_buf[TRACE_BUF_SIZE];
1387 struct ftrace_event_call *call = &event_bprint;
1388 struct ring_buffer_event *event;
1389 struct ring_buffer *buffer;
1390 struct trace_array *tr = &global_trace;
1391 struct trace_array_cpu *data;
1392 struct bprint_entry *entry;
1393 unsigned long flags;
1396 int cpu, len = 0, size, pc;
1398 if (unlikely(tracing_selftest_running || tracing_disabled))
1401 /* Don't pollute graph traces with trace_vprintk internals */
1402 pause_graph_tracing();
1404 pc = preempt_count();
1405 resched = ftrace_preempt_disable();
1406 cpu = raw_smp_processor_id();
1407 data = tr->data[cpu];
1409 disable = atomic_inc_return(&data->disabled);
1410 if (unlikely(disable != 1))
1413 /* Lockdep uses trace_printk for lock tracing */
1414 local_irq_save(flags);
1415 arch_spin_lock(&trace_buf_lock);
1416 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1418 if (len > TRACE_BUF_SIZE || len < 0)
1421 size = sizeof(*entry) + sizeof(u32) * len;
1422 buffer = tr->buffer;
1423 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1427 entry = ring_buffer_event_data(event);
1431 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1432 if (!filter_check_discard(call, entry, buffer, event)) {
1433 ring_buffer_unlock_commit(buffer, event);
1434 ftrace_trace_stack(buffer, flags, 6, pc);
1438 arch_spin_unlock(&trace_buf_lock);
1439 local_irq_restore(flags);
1442 atomic_dec_return(&data->disabled);
1443 ftrace_preempt_enable(resched);
1444 unpause_graph_tracing();
1448 EXPORT_SYMBOL_GPL(trace_vbprintk);
1450 int trace_array_printk(struct trace_array *tr,
1451 unsigned long ip, const char *fmt, ...)
1456 if (!(trace_flags & TRACE_ITER_PRINTK))
1460 ret = trace_array_vprintk(tr, ip, fmt, ap);
1465 int trace_array_vprintk(struct trace_array *tr,
1466 unsigned long ip, const char *fmt, va_list args)
1468 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1469 static char trace_buf[TRACE_BUF_SIZE];
1471 struct ftrace_event_call *call = &event_print;
1472 struct ring_buffer_event *event;
1473 struct ring_buffer *buffer;
1474 struct trace_array_cpu *data;
1475 int cpu, len = 0, size, pc;
1476 struct print_entry *entry;
1477 unsigned long irq_flags;
1480 if (tracing_disabled || tracing_selftest_running)
1483 pc = preempt_count();
1484 preempt_disable_notrace();
1485 cpu = raw_smp_processor_id();
1486 data = tr->data[cpu];
1488 disable = atomic_inc_return(&data->disabled);
1489 if (unlikely(disable != 1))
1492 pause_graph_tracing();
1493 raw_local_irq_save(irq_flags);
1494 arch_spin_lock(&trace_buf_lock);
1495 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1497 size = sizeof(*entry) + len + 1;
1498 buffer = tr->buffer;
1499 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1503 entry = ring_buffer_event_data(event);
1506 memcpy(&entry->buf, trace_buf, len);
1507 entry->buf[len] = '\0';
1508 if (!filter_check_discard(call, entry, buffer, event)) {
1509 ring_buffer_unlock_commit(buffer, event);
1510 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1514 arch_spin_unlock(&trace_buf_lock);
1515 raw_local_irq_restore(irq_flags);
1516 unpause_graph_tracing();
1518 atomic_dec_return(&data->disabled);
1519 preempt_enable_notrace();
1524 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1526 return trace_array_vprintk(&global_trace, ip, fmt, args);
1528 EXPORT_SYMBOL_GPL(trace_vprintk);
1530 enum trace_file_type {
1531 TRACE_FILE_LAT_FMT = 1,
1532 TRACE_FILE_ANNOTATE = 2,
1535 static void trace_iterator_increment(struct trace_iterator *iter)
1537 /* Don't allow ftrace to trace into the ring buffers */
1538 ftrace_disable_cpu();
1541 if (iter->buffer_iter[iter->cpu])
1542 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1544 ftrace_enable_cpu();
1547 static struct trace_entry *
1548 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1549 unsigned long *lost_events)
1551 struct ring_buffer_event *event;
1552 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1554 /* Don't allow ftrace to trace into the ring buffers */
1555 ftrace_disable_cpu();
1558 event = ring_buffer_iter_peek(buf_iter, ts);
1560 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1563 ftrace_enable_cpu();
1565 return event ? ring_buffer_event_data(event) : NULL;
1568 static struct trace_entry *
1569 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1570 unsigned long *missing_events, u64 *ent_ts)
1572 struct ring_buffer *buffer = iter->tr->buffer;
1573 struct trace_entry *ent, *next = NULL;
1574 unsigned long lost_events, next_lost = 0;
1575 int cpu_file = iter->cpu_file;
1576 u64 next_ts = 0, ts;
1581 * If we are in a per_cpu trace file, don't bother by iterating over
1582 * all cpu and peek directly.
1584 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1585 if (ring_buffer_empty_cpu(buffer, cpu_file))
1587 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1589 *ent_cpu = cpu_file;
1594 for_each_tracing_cpu(cpu) {
1596 if (ring_buffer_empty_cpu(buffer, cpu))
1599 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1602 * Pick the entry with the smallest timestamp:
1604 if (ent && (!next || ts < next_ts)) {
1608 next_lost = lost_events;
1613 *ent_cpu = next_cpu;
1619 *missing_events = next_lost;
1624 /* Find the next real entry, without updating the iterator itself */
1625 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1626 int *ent_cpu, u64 *ent_ts)
1628 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1631 /* Find the next real entry, and increment the iterator to the next entry */
1632 static void *find_next_entry_inc(struct trace_iterator *iter)
1634 iter->ent = __find_next_entry(iter, &iter->cpu,
1635 &iter->lost_events, &iter->ts);
1638 trace_iterator_increment(iter);
1640 return iter->ent ? iter : NULL;
1643 static void trace_consume(struct trace_iterator *iter)
1645 /* Don't allow ftrace to trace into the ring buffers */
1646 ftrace_disable_cpu();
1647 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1648 &iter->lost_events);
1649 ftrace_enable_cpu();
1652 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1654 struct trace_iterator *iter = m->private;
1658 WARN_ON_ONCE(iter->leftover);
1662 /* can't go backwards */
1667 ent = find_next_entry_inc(iter);
1671 while (ent && iter->idx < i)
1672 ent = find_next_entry_inc(iter);
1679 static void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1681 struct trace_array *tr = iter->tr;
1682 struct ring_buffer_event *event;
1683 struct ring_buffer_iter *buf_iter;
1684 unsigned long entries = 0;
1687 tr->data[cpu]->skipped_entries = 0;
1689 if (!iter->buffer_iter[cpu])
1692 buf_iter = iter->buffer_iter[cpu];
1693 ring_buffer_iter_reset(buf_iter);
1696 * We could have the case with the max latency tracers
1697 * that a reset never took place on a cpu. This is evident
1698 * by the timestamp being before the start of the buffer.
1700 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1701 if (ts >= iter->tr->time_start)
1704 ring_buffer_read(buf_iter, NULL);
1707 tr->data[cpu]->skipped_entries = entries;
1711 * The current tracer is copied to avoid a global locking
1714 static void *s_start(struct seq_file *m, loff_t *pos)
1716 struct trace_iterator *iter = m->private;
1717 static struct tracer *old_tracer;
1718 int cpu_file = iter->cpu_file;
1723 /* copy the tracer to avoid using a global lock all around */
1724 mutex_lock(&trace_types_lock);
1725 if (unlikely(old_tracer != current_trace && current_trace)) {
1726 old_tracer = current_trace;
1727 *iter->trace = *current_trace;
1729 mutex_unlock(&trace_types_lock);
1731 atomic_inc(&trace_record_cmdline_disabled);
1733 if (*pos != iter->pos) {
1738 ftrace_disable_cpu();
1740 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1741 for_each_tracing_cpu(cpu)
1742 tracing_iter_reset(iter, cpu);
1744 tracing_iter_reset(iter, cpu_file);
1746 ftrace_enable_cpu();
1749 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1754 * If we overflowed the seq_file before, then we want
1755 * to just reuse the trace_seq buffer again.
1761 p = s_next(m, p, &l);
1765 trace_event_read_lock();
1766 trace_access_lock(cpu_file);
1770 static void s_stop(struct seq_file *m, void *p)
1772 struct trace_iterator *iter = m->private;
1774 atomic_dec(&trace_record_cmdline_disabled);
1775 trace_access_unlock(iter->cpu_file);
1776 trace_event_read_unlock();
1779 static void print_lat_help_header(struct seq_file *m)
1781 seq_puts(m, "# _------=> CPU# \n");
1782 seq_puts(m, "# / _-----=> irqs-off \n");
1783 seq_puts(m, "# | / _----=> need-resched \n");
1784 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1785 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1786 seq_puts(m, "# |||| /_--=> lock-depth \n");
1787 seq_puts(m, "# |||||/ delay \n");
1788 seq_puts(m, "# cmd pid |||||| time | caller \n");
1789 seq_puts(m, "# \\ / |||||| \\ | / \n");
1792 static void print_func_help_header(struct seq_file *m)
1794 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1795 seq_puts(m, "# | | | | |\n");
1800 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1802 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1803 struct trace_array *tr = iter->tr;
1804 struct trace_array_cpu *data = tr->data[tr->cpu];
1805 struct tracer *type = current_trace;
1806 unsigned long entries = 0;
1807 unsigned long total = 0;
1808 unsigned long count;
1809 const char *name = "preemption";
1816 for_each_tracing_cpu(cpu) {
1817 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1819 * If this buffer has skipped entries, then we hold all
1820 * entries for the trace and we need to ignore the
1821 * ones before the time stamp.
1823 if (tr->data[cpu]->skipped_entries) {
1824 count -= tr->data[cpu]->skipped_entries;
1825 /* total is the same as the entries */
1829 ring_buffer_overrun_cpu(tr->buffer, cpu);
1833 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1835 seq_puts(m, "# -----------------------------------"
1836 "---------------------------------\n");
1837 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1838 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1839 nsecs_to_usecs(data->saved_latency),
1843 #if defined(CONFIG_PREEMPT_NONE)
1845 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1847 #elif defined(CONFIG_PREEMPT)
1852 /* These are reserved for later use */
1855 seq_printf(m, " #P:%d)\n", num_online_cpus());
1859 seq_puts(m, "# -----------------\n");
1860 seq_printf(m, "# | task: %.16s-%d "
1861 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1862 data->comm, data->pid, data->uid, data->nice,
1863 data->policy, data->rt_priority);
1864 seq_puts(m, "# -----------------\n");
1866 if (data->critical_start) {
1867 seq_puts(m, "# => started at: ");
1868 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1869 trace_print_seq(m, &iter->seq);
1870 seq_puts(m, "\n# => ended at: ");
1871 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1872 trace_print_seq(m, &iter->seq);
1873 seq_puts(m, "\n#\n");
1879 static void test_cpu_buff_start(struct trace_iterator *iter)
1881 struct trace_seq *s = &iter->seq;
1883 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1886 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1889 if (cpumask_test_cpu(iter->cpu, iter->started))
1892 if (iter->tr->data[iter->cpu]->skipped_entries)
1895 cpumask_set_cpu(iter->cpu, iter->started);
1897 /* Don't print started cpu buffer for the first entry of the trace */
1899 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1903 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1905 struct trace_seq *s = &iter->seq;
1906 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1907 struct trace_entry *entry;
1908 struct trace_event *event;
1912 test_cpu_buff_start(iter);
1914 event = ftrace_find_event(entry->type);
1916 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1917 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1918 if (!trace_print_lat_context(iter))
1921 if (!trace_print_context(iter))
1927 return event->trace(iter, sym_flags);
1929 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1932 return TRACE_TYPE_HANDLED;
1934 return TRACE_TYPE_PARTIAL_LINE;
1937 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1939 struct trace_seq *s = &iter->seq;
1940 struct trace_entry *entry;
1941 struct trace_event *event;
1945 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1946 if (!trace_seq_printf(s, "%d %d %llu ",
1947 entry->pid, iter->cpu, iter->ts))
1951 event = ftrace_find_event(entry->type);
1953 return event->raw(iter, 0);
1955 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1958 return TRACE_TYPE_HANDLED;
1960 return TRACE_TYPE_PARTIAL_LINE;
1963 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1965 struct trace_seq *s = &iter->seq;
1966 unsigned char newline = '\n';
1967 struct trace_entry *entry;
1968 struct trace_event *event;
1972 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1973 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1974 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1975 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1978 event = ftrace_find_event(entry->type);
1980 enum print_line_t ret = event->hex(iter, 0);
1981 if (ret != TRACE_TYPE_HANDLED)
1985 SEQ_PUT_FIELD_RET(s, newline);
1987 return TRACE_TYPE_HANDLED;
1990 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1992 struct trace_seq *s = &iter->seq;
1993 struct trace_entry *entry;
1994 struct trace_event *event;
1998 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1999 SEQ_PUT_FIELD_RET(s, entry->pid);
2000 SEQ_PUT_FIELD_RET(s, iter->cpu);
2001 SEQ_PUT_FIELD_RET(s, iter->ts);
2004 event = ftrace_find_event(entry->type);
2005 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
2008 static int trace_empty(struct trace_iterator *iter)
2012 /* If we are looking at one CPU buffer, only check that one */
2013 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2014 cpu = iter->cpu_file;
2015 if (iter->buffer_iter[cpu]) {
2016 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2019 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2025 for_each_tracing_cpu(cpu) {
2026 if (iter->buffer_iter[cpu]) {
2027 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2030 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2038 /* Called with trace_event_read_lock() held. */
2039 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2041 enum print_line_t ret;
2043 if (iter->lost_events)
2044 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2045 iter->cpu, iter->lost_events);
2047 if (iter->trace && iter->trace->print_line) {
2048 ret = iter->trace->print_line(iter);
2049 if (ret != TRACE_TYPE_UNHANDLED)
2053 if (iter->ent->type == TRACE_BPRINT &&
2054 trace_flags & TRACE_ITER_PRINTK &&
2055 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2056 return trace_print_bprintk_msg_only(iter);
2058 if (iter->ent->type == TRACE_PRINT &&
2059 trace_flags & TRACE_ITER_PRINTK &&
2060 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2061 return trace_print_printk_msg_only(iter);
2063 if (trace_flags & TRACE_ITER_BIN)
2064 return print_bin_fmt(iter);
2066 if (trace_flags & TRACE_ITER_HEX)
2067 return print_hex_fmt(iter);
2069 if (trace_flags & TRACE_ITER_RAW)
2070 return print_raw_fmt(iter);
2072 return print_trace_fmt(iter);
2075 static int s_show(struct seq_file *m, void *v)
2077 struct trace_iterator *iter = v;
2080 if (iter->ent == NULL) {
2082 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2085 if (iter->trace && iter->trace->print_header)
2086 iter->trace->print_header(m);
2087 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2088 /* print nothing if the buffers are empty */
2089 if (trace_empty(iter))
2091 print_trace_header(m, iter);
2092 if (!(trace_flags & TRACE_ITER_VERBOSE))
2093 print_lat_help_header(m);
2095 if (!(trace_flags & TRACE_ITER_VERBOSE))
2096 print_func_help_header(m);
2098 } else if (iter->leftover) {
2100 * If we filled the seq_file buffer earlier, we
2101 * want to just show it now.
2103 ret = trace_print_seq(m, &iter->seq);
2105 /* ret should this time be zero, but you never know */
2106 iter->leftover = ret;
2109 print_trace_line(iter);
2110 ret = trace_print_seq(m, &iter->seq);
2112 * If we overflow the seq_file buffer, then it will
2113 * ask us for this data again at start up.
2115 * ret is 0 if seq_file write succeeded.
2118 iter->leftover = ret;
2124 static const struct seq_operations tracer_seq_ops = {
2131 static struct trace_iterator *
2132 __tracing_open(struct inode *inode, struct file *file)
2134 long cpu_file = (long) inode->i_private;
2135 void *fail_ret = ERR_PTR(-ENOMEM);
2136 struct trace_iterator *iter;
2140 if (tracing_disabled)
2141 return ERR_PTR(-ENODEV);
2143 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2145 return ERR_PTR(-ENOMEM);
2148 * We make a copy of the current tracer to avoid concurrent
2149 * changes on it while we are reading.
2151 mutex_lock(&trace_types_lock);
2152 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2157 *iter->trace = *current_trace;
2159 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2162 if (current_trace && current_trace->print_max)
2165 iter->tr = &global_trace;
2167 mutex_init(&iter->mutex);
2168 iter->cpu_file = cpu_file;
2170 /* Notify the tracer early; before we stop tracing. */
2171 if (iter->trace && iter->trace->open)
2172 iter->trace->open(iter);
2174 /* Annotate start of buffers if we had overruns */
2175 if (ring_buffer_overruns(iter->tr->buffer))
2176 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2178 /* stop the trace while dumping */
2181 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2182 for_each_tracing_cpu(cpu) {
2184 iter->buffer_iter[cpu] =
2185 ring_buffer_read_start(iter->tr->buffer, cpu);
2186 tracing_iter_reset(iter, cpu);
2189 cpu = iter->cpu_file;
2190 iter->buffer_iter[cpu] =
2191 ring_buffer_read_start(iter->tr->buffer, cpu);
2192 tracing_iter_reset(iter, cpu);
2195 ret = seq_open(file, &tracer_seq_ops);
2197 fail_ret = ERR_PTR(ret);
2201 m = file->private_data;
2204 mutex_unlock(&trace_types_lock);
2209 for_each_tracing_cpu(cpu) {
2210 if (iter->buffer_iter[cpu])
2211 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2213 free_cpumask_var(iter->started);
2216 mutex_unlock(&trace_types_lock);
2223 int tracing_open_generic(struct inode *inode, struct file *filp)
2225 if (tracing_disabled)
2228 filp->private_data = inode->i_private;
2232 static int tracing_release(struct inode *inode, struct file *file)
2234 struct seq_file *m = (struct seq_file *)file->private_data;
2235 struct trace_iterator *iter;
2238 if (!(file->f_mode & FMODE_READ))
2243 mutex_lock(&trace_types_lock);
2244 for_each_tracing_cpu(cpu) {
2245 if (iter->buffer_iter[cpu])
2246 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2249 if (iter->trace && iter->trace->close)
2250 iter->trace->close(iter);
2252 /* reenable tracing if it was previously enabled */
2254 mutex_unlock(&trace_types_lock);
2256 seq_release(inode, file);
2257 mutex_destroy(&iter->mutex);
2258 free_cpumask_var(iter->started);
2264 static int tracing_open(struct inode *inode, struct file *file)
2266 struct trace_iterator *iter;
2269 /* If this file was open for write, then erase contents */
2270 if ((file->f_mode & FMODE_WRITE) &&
2271 (file->f_flags & O_TRUNC)) {
2272 long cpu = (long) inode->i_private;
2274 if (cpu == TRACE_PIPE_ALL_CPU)
2275 tracing_reset_online_cpus(&global_trace);
2277 tracing_reset(&global_trace, cpu);
2280 if (file->f_mode & FMODE_READ) {
2281 iter = __tracing_open(inode, file);
2283 ret = PTR_ERR(iter);
2284 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2285 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2291 t_next(struct seq_file *m, void *v, loff_t *pos)
2293 struct tracer *t = v;
2303 static void *t_start(struct seq_file *m, loff_t *pos)
2308 mutex_lock(&trace_types_lock);
2309 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2315 static void t_stop(struct seq_file *m, void *p)
2317 mutex_unlock(&trace_types_lock);
2320 static int t_show(struct seq_file *m, void *v)
2322 struct tracer *t = v;
2327 seq_printf(m, "%s", t->name);
2336 static const struct seq_operations show_traces_seq_ops = {
2343 static int show_traces_open(struct inode *inode, struct file *file)
2345 if (tracing_disabled)
2348 return seq_open(file, &show_traces_seq_ops);
2352 tracing_write_stub(struct file *filp, const char __user *ubuf,
2353 size_t count, loff_t *ppos)
2358 static const struct file_operations tracing_fops = {
2359 .open = tracing_open,
2361 .write = tracing_write_stub,
2362 .llseek = seq_lseek,
2363 .release = tracing_release,
2366 static const struct file_operations show_traces_fops = {
2367 .open = show_traces_open,
2369 .release = seq_release,
2373 * Only trace on a CPU if the bitmask is set:
2375 static cpumask_var_t tracing_cpumask;
2378 * The tracer itself will not take this lock, but still we want
2379 * to provide a consistent cpumask to user-space:
2381 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2384 * Temporary storage for the character representation of the
2385 * CPU bitmask (and one more byte for the newline):
2387 static char mask_str[NR_CPUS + 1];
2390 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2391 size_t count, loff_t *ppos)
2395 mutex_lock(&tracing_cpumask_update_lock);
2397 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2398 if (count - len < 2) {
2402 len += sprintf(mask_str + len, "\n");
2403 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2406 mutex_unlock(&tracing_cpumask_update_lock);
2412 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2413 size_t count, loff_t *ppos)
2416 cpumask_var_t tracing_cpumask_new;
2418 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2421 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2425 mutex_lock(&tracing_cpumask_update_lock);
2427 local_irq_disable();
2428 arch_spin_lock(&ftrace_max_lock);
2429 for_each_tracing_cpu(cpu) {
2431 * Increase/decrease the disabled counter if we are
2432 * about to flip a bit in the cpumask:
2434 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2435 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2436 atomic_inc(&global_trace.data[cpu]->disabled);
2438 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2439 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2440 atomic_dec(&global_trace.data[cpu]->disabled);
2443 arch_spin_unlock(&ftrace_max_lock);
2446 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2448 mutex_unlock(&tracing_cpumask_update_lock);
2449 free_cpumask_var(tracing_cpumask_new);
2454 free_cpumask_var(tracing_cpumask_new);
2459 static const struct file_operations tracing_cpumask_fops = {
2460 .open = tracing_open_generic,
2461 .read = tracing_cpumask_read,
2462 .write = tracing_cpumask_write,
2465 static int tracing_trace_options_show(struct seq_file *m, void *v)
2467 struct tracer_opt *trace_opts;
2471 mutex_lock(&trace_types_lock);
2472 tracer_flags = current_trace->flags->val;
2473 trace_opts = current_trace->flags->opts;
2475 for (i = 0; trace_options[i]; i++) {
2476 if (trace_flags & (1 << i))
2477 seq_printf(m, "%s\n", trace_options[i]);
2479 seq_printf(m, "no%s\n", trace_options[i]);
2482 for (i = 0; trace_opts[i].name; i++) {
2483 if (tracer_flags & trace_opts[i].bit)
2484 seq_printf(m, "%s\n", trace_opts[i].name);
2486 seq_printf(m, "no%s\n", trace_opts[i].name);
2488 mutex_unlock(&trace_types_lock);
2493 static int __set_tracer_option(struct tracer *trace,
2494 struct tracer_flags *tracer_flags,
2495 struct tracer_opt *opts, int neg)
2499 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2504 tracer_flags->val &= ~opts->bit;
2506 tracer_flags->val |= opts->bit;
2510 /* Try to assign a tracer specific option */
2511 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2513 struct tracer_flags *tracer_flags = trace->flags;
2514 struct tracer_opt *opts = NULL;
2517 for (i = 0; tracer_flags->opts[i].name; i++) {
2518 opts = &tracer_flags->opts[i];
2520 if (strcmp(cmp, opts->name) == 0)
2521 return __set_tracer_option(trace, trace->flags,
2528 static void set_tracer_flags(unsigned int mask, int enabled)
2530 /* do nothing if flag is already set */
2531 if (!!(trace_flags & mask) == !!enabled)
2535 trace_flags |= mask;
2537 trace_flags &= ~mask;
2541 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2542 size_t cnt, loff_t *ppos)
2550 if (cnt >= sizeof(buf))
2553 if (copy_from_user(&buf, ubuf, cnt))
2557 cmp = strstrip(buf);
2559 if (strncmp(cmp, "no", 2) == 0) {
2564 for (i = 0; trace_options[i]; i++) {
2565 if (strcmp(cmp, trace_options[i]) == 0) {
2566 set_tracer_flags(1 << i, !neg);
2571 /* If no option could be set, test the specific tracer options */
2572 if (!trace_options[i]) {
2573 mutex_lock(&trace_types_lock);
2574 ret = set_tracer_option(current_trace, cmp, neg);
2575 mutex_unlock(&trace_types_lock);
2585 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2587 if (tracing_disabled)
2589 return single_open(file, tracing_trace_options_show, NULL);
2592 static const struct file_operations tracing_iter_fops = {
2593 .open = tracing_trace_options_open,
2595 .llseek = seq_lseek,
2596 .release = single_release,
2597 .write = tracing_trace_options_write,
2600 static const char readme_msg[] =
2601 "tracing mini-HOWTO:\n\n"
2602 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2603 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2604 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2605 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2607 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2608 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2610 "# cat /sys/kernel/debug/tracing/trace_options\n"
2611 "noprint-parent nosym-offset nosym-addr noverbose\n"
2612 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2613 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2614 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2615 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2619 tracing_readme_read(struct file *filp, char __user *ubuf,
2620 size_t cnt, loff_t *ppos)
2622 return simple_read_from_buffer(ubuf, cnt, ppos,
2623 readme_msg, strlen(readme_msg));
2626 static const struct file_operations tracing_readme_fops = {
2627 .open = tracing_open_generic,
2628 .read = tracing_readme_read,
2632 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2633 size_t cnt, loff_t *ppos)
2642 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2646 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2654 for (i = 0; i < SAVED_CMDLINES; i++) {
2657 pid = map_cmdline_to_pid[i];
2658 if (pid == -1 || pid == NO_CMDLINE_MAP)
2661 trace_find_cmdline(pid, buf_comm);
2662 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2667 len = simple_read_from_buffer(ubuf, cnt, ppos,
2676 static const struct file_operations tracing_saved_cmdlines_fops = {
2677 .open = tracing_open_generic,
2678 .read = tracing_saved_cmdlines_read,
2682 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2683 size_t cnt, loff_t *ppos)
2688 r = sprintf(buf, "%u\n", tracer_enabled);
2689 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2693 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2694 size_t cnt, loff_t *ppos)
2696 struct trace_array *tr = filp->private_data;
2701 if (cnt >= sizeof(buf))
2704 if (copy_from_user(&buf, ubuf, cnt))
2709 ret = strict_strtoul(buf, 10, &val);
2715 mutex_lock(&trace_types_lock);
2716 if (tracer_enabled ^ val) {
2719 if (current_trace->start)
2720 current_trace->start(tr);
2725 if (current_trace->stop)
2726 current_trace->stop(tr);
2729 mutex_unlock(&trace_types_lock);
2737 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2738 size_t cnt, loff_t *ppos)
2740 char buf[MAX_TRACER_SIZE+2];
2743 mutex_lock(&trace_types_lock);
2745 r = sprintf(buf, "%s\n", current_trace->name);
2747 r = sprintf(buf, "\n");
2748 mutex_unlock(&trace_types_lock);
2750 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2753 int tracer_init(struct tracer *t, struct trace_array *tr)
2755 tracing_reset_online_cpus(tr);
2759 static int tracing_resize_ring_buffer(unsigned long size)
2764 * If kernel or user changes the size of the ring buffer
2765 * we use the size that was given, and we can forget about
2766 * expanding it later.
2768 ring_buffer_expanded = 1;
2770 ret = ring_buffer_resize(global_trace.buffer, size);
2774 ret = ring_buffer_resize(max_tr.buffer, size);
2778 r = ring_buffer_resize(global_trace.buffer,
2779 global_trace.entries);
2782 * AARGH! We are left with different
2783 * size max buffer!!!!
2784 * The max buffer is our "snapshot" buffer.
2785 * When a tracer needs a snapshot (one of the
2786 * latency tracers), it swaps the max buffer
2787 * with the saved snap shot. We succeeded to
2788 * update the size of the main buffer, but failed to
2789 * update the size of the max buffer. But when we tried
2790 * to reset the main buffer to the original size, we
2791 * failed there too. This is very unlikely to
2792 * happen, but if it does, warn and kill all
2796 tracing_disabled = 1;
2801 global_trace.entries = size;
2807 * tracing_update_buffers - used by tracing facility to expand ring buffers
2809 * To save on memory when the tracing is never used on a system with it
2810 * configured in. The ring buffers are set to a minimum size. But once
2811 * a user starts to use the tracing facility, then they need to grow
2812 * to their default size.
2814 * This function is to be called when a tracer is about to be used.
2816 int tracing_update_buffers(void)
2820 mutex_lock(&trace_types_lock);
2821 if (!ring_buffer_expanded)
2822 ret = tracing_resize_ring_buffer(trace_buf_size);
2823 mutex_unlock(&trace_types_lock);
2828 struct trace_option_dentry;
2830 static struct trace_option_dentry *
2831 create_trace_option_files(struct tracer *tracer);
2834 destroy_trace_option_files(struct trace_option_dentry *topts);
2836 static int tracing_set_tracer(const char *buf)
2838 static struct trace_option_dentry *topts;
2839 struct trace_array *tr = &global_trace;
2843 mutex_lock(&trace_types_lock);
2845 if (!ring_buffer_expanded) {
2846 ret = tracing_resize_ring_buffer(trace_buf_size);
2852 for (t = trace_types; t; t = t->next) {
2853 if (strcmp(t->name, buf) == 0)
2860 if (t == current_trace)
2863 trace_branch_disable();
2864 if (current_trace && current_trace->reset)
2865 current_trace->reset(tr);
2867 destroy_trace_option_files(topts);
2871 topts = create_trace_option_files(current_trace);
2874 ret = tracer_init(t, tr);
2879 trace_branch_enable(tr);
2881 mutex_unlock(&trace_types_lock);
2887 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2888 size_t cnt, loff_t *ppos)
2890 char buf[MAX_TRACER_SIZE+1];
2897 if (cnt > MAX_TRACER_SIZE)
2898 cnt = MAX_TRACER_SIZE;
2900 if (copy_from_user(&buf, ubuf, cnt))
2905 /* strip ending whitespace. */
2906 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2909 err = tracing_set_tracer(buf);
2919 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2920 size_t cnt, loff_t *ppos)
2922 unsigned long *ptr = filp->private_data;
2926 r = snprintf(buf, sizeof(buf), "%ld\n",
2927 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2928 if (r > sizeof(buf))
2930 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2934 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2935 size_t cnt, loff_t *ppos)
2937 unsigned long *ptr = filp->private_data;
2942 if (cnt >= sizeof(buf))
2945 if (copy_from_user(&buf, ubuf, cnt))
2950 ret = strict_strtoul(buf, 10, &val);
2959 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2961 long cpu_file = (long) inode->i_private;
2962 struct trace_iterator *iter;
2965 if (tracing_disabled)
2968 mutex_lock(&trace_types_lock);
2970 /* create a buffer to store the information to pass to userspace */
2971 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2978 * We make a copy of the current tracer to avoid concurrent
2979 * changes on it while we are reading.
2981 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2987 *iter->trace = *current_trace;
2989 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2994 /* trace pipe does not show start of buffer */
2995 cpumask_setall(iter->started);
2997 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2998 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3000 iter->cpu_file = cpu_file;
3001 iter->tr = &global_trace;
3002 mutex_init(&iter->mutex);
3003 filp->private_data = iter;
3005 if (iter->trace->pipe_open)
3006 iter->trace->pipe_open(iter);
3009 mutex_unlock(&trace_types_lock);
3015 mutex_unlock(&trace_types_lock);
3019 static int tracing_release_pipe(struct inode *inode, struct file *file)
3021 struct trace_iterator *iter = file->private_data;
3023 mutex_lock(&trace_types_lock);
3025 if (iter->trace->pipe_close)
3026 iter->trace->pipe_close(iter);
3028 mutex_unlock(&trace_types_lock);
3030 free_cpumask_var(iter->started);
3031 mutex_destroy(&iter->mutex);
3039 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3041 struct trace_iterator *iter = filp->private_data;
3043 if (trace_flags & TRACE_ITER_BLOCK) {
3045 * Always select as readable when in blocking mode
3047 return POLLIN | POLLRDNORM;
3049 if (!trace_empty(iter))
3050 return POLLIN | POLLRDNORM;
3051 poll_wait(filp, &trace_wait, poll_table);
3052 if (!trace_empty(iter))
3053 return POLLIN | POLLRDNORM;
3060 void default_wait_pipe(struct trace_iterator *iter)
3064 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3066 if (trace_empty(iter))
3069 finish_wait(&trace_wait, &wait);
3073 * This is a make-shift waitqueue.
3074 * A tracer might use this callback on some rare cases:
3076 * 1) the current tracer might hold the runqueue lock when it wakes up
3077 * a reader, hence a deadlock (sched, function, and function graph tracers)
3078 * 2) the function tracers, trace all functions, we don't want
3079 * the overhead of calling wake_up and friends
3080 * (and tracing them too)
3082 * Anyway, this is really very primitive wakeup.
3084 void poll_wait_pipe(struct trace_iterator *iter)
3086 set_current_state(TASK_INTERRUPTIBLE);
3087 /* sleep for 100 msecs, and try again. */
3088 schedule_timeout(HZ / 10);
3091 /* Must be called with trace_types_lock mutex held. */
3092 static int tracing_wait_pipe(struct file *filp)
3094 struct trace_iterator *iter = filp->private_data;
3096 while (trace_empty(iter)) {
3098 if ((filp->f_flags & O_NONBLOCK)) {
3102 mutex_unlock(&iter->mutex);
3104 iter->trace->wait_pipe(iter);
3106 mutex_lock(&iter->mutex);
3108 if (signal_pending(current))
3112 * We block until we read something and tracing is disabled.
3113 * We still block if tracing is disabled, but we have never
3114 * read anything. This allows a user to cat this file, and
3115 * then enable tracing. But after we have read something,
3116 * we give an EOF when tracing is again disabled.
3118 * iter->pos will be 0 if we haven't read anything.
3120 if (!tracer_enabled && iter->pos)
3131 tracing_read_pipe(struct file *filp, char __user *ubuf,
3132 size_t cnt, loff_t *ppos)
3134 struct trace_iterator *iter = filp->private_data;
3135 static struct tracer *old_tracer;
3138 /* return any leftover data */
3139 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3143 trace_seq_init(&iter->seq);
3145 /* copy the tracer to avoid using a global lock all around */
3146 mutex_lock(&trace_types_lock);
3147 if (unlikely(old_tracer != current_trace && current_trace)) {
3148 old_tracer = current_trace;
3149 *iter->trace = *current_trace;
3151 mutex_unlock(&trace_types_lock);
3154 * Avoid more than one consumer on a single file descriptor
3155 * This is just a matter of traces coherency, the ring buffer itself
3158 mutex_lock(&iter->mutex);
3159 if (iter->trace->read) {
3160 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3166 sret = tracing_wait_pipe(filp);
3170 /* stop when tracing is finished */
3171 if (trace_empty(iter)) {
3176 if (cnt >= PAGE_SIZE)
3177 cnt = PAGE_SIZE - 1;
3179 /* reset all but tr, trace, and overruns */
3180 memset(&iter->seq, 0,
3181 sizeof(struct trace_iterator) -
3182 offsetof(struct trace_iterator, seq));
3185 trace_event_read_lock();
3186 trace_access_lock(iter->cpu_file);
3187 while (find_next_entry_inc(iter) != NULL) {
3188 enum print_line_t ret;
3189 int len = iter->seq.len;
3191 ret = print_trace_line(iter);
3192 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3193 /* don't print partial lines */
3194 iter->seq.len = len;
3197 if (ret != TRACE_TYPE_NO_CONSUME)
3198 trace_consume(iter);
3200 if (iter->seq.len >= cnt)
3203 trace_access_unlock(iter->cpu_file);
3204 trace_event_read_unlock();
3206 /* Now copy what we have to the user */
3207 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3208 if (iter->seq.readpos >= iter->seq.len)
3209 trace_seq_init(&iter->seq);
3212 * If there was nothing to send to user, inspite of consuming trace
3213 * entries, go back to wait for more entries.
3219 mutex_unlock(&iter->mutex);
3224 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3225 struct pipe_buffer *buf)
3227 __free_page(buf->page);
3230 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3233 __free_page(spd->pages[idx]);
3236 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3238 .map = generic_pipe_buf_map,
3239 .unmap = generic_pipe_buf_unmap,
3240 .confirm = generic_pipe_buf_confirm,
3241 .release = tracing_pipe_buf_release,
3242 .steal = generic_pipe_buf_steal,
3243 .get = generic_pipe_buf_get,
3247 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3252 /* Seq buffer is page-sized, exactly what we need. */
3254 count = iter->seq.len;
3255 ret = print_trace_line(iter);
3256 count = iter->seq.len - count;
3259 iter->seq.len -= count;
3262 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3263 iter->seq.len -= count;
3267 if (ret != TRACE_TYPE_NO_CONSUME)
3268 trace_consume(iter);
3270 if (!find_next_entry_inc(iter)) {
3280 static ssize_t tracing_splice_read_pipe(struct file *filp,
3282 struct pipe_inode_info *pipe,
3286 struct page *pages[PIPE_BUFFERS];
3287 struct partial_page partial[PIPE_BUFFERS];
3288 struct trace_iterator *iter = filp->private_data;
3289 struct splice_pipe_desc spd = {
3292 .nr_pages = 0, /* This gets updated below. */
3294 .ops = &tracing_pipe_buf_ops,
3295 .spd_release = tracing_spd_release_pipe,
3297 static struct tracer *old_tracer;
3302 /* copy the tracer to avoid using a global lock all around */
3303 mutex_lock(&trace_types_lock);
3304 if (unlikely(old_tracer != current_trace && current_trace)) {
3305 old_tracer = current_trace;
3306 *iter->trace = *current_trace;
3308 mutex_unlock(&trace_types_lock);
3310 mutex_lock(&iter->mutex);
3312 if (iter->trace->splice_read) {
3313 ret = iter->trace->splice_read(iter, filp,
3314 ppos, pipe, len, flags);
3319 ret = tracing_wait_pipe(filp);
3323 if (!iter->ent && !find_next_entry_inc(iter)) {
3328 trace_event_read_lock();
3329 trace_access_lock(iter->cpu_file);
3331 /* Fill as many pages as possible. */
3332 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3333 pages[i] = alloc_page(GFP_KERNEL);
3337 rem = tracing_fill_pipe_page(rem, iter);
3339 /* Copy the data into the page, so we can start over. */
3340 ret = trace_seq_to_buffer(&iter->seq,
3341 page_address(pages[i]),
3344 __free_page(pages[i]);
3347 partial[i].offset = 0;
3348 partial[i].len = iter->seq.len;
3350 trace_seq_init(&iter->seq);
3353 trace_access_unlock(iter->cpu_file);
3354 trace_event_read_unlock();
3355 mutex_unlock(&iter->mutex);
3359 return splice_to_pipe(pipe, &spd);
3362 mutex_unlock(&iter->mutex);
3368 tracing_entries_read(struct file *filp, char __user *ubuf,
3369 size_t cnt, loff_t *ppos)
3371 struct trace_array *tr = filp->private_data;
3375 mutex_lock(&trace_types_lock);
3376 if (!ring_buffer_expanded)
3377 r = sprintf(buf, "%lu (expanded: %lu)\n",
3379 trace_buf_size >> 10);
3381 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3382 mutex_unlock(&trace_types_lock);
3384 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3388 tracing_entries_write(struct file *filp, const char __user *ubuf,
3389 size_t cnt, loff_t *ppos)
3395 if (cnt >= sizeof(buf))
3398 if (copy_from_user(&buf, ubuf, cnt))
3403 ret = strict_strtoul(buf, 10, &val);
3407 /* must have at least 1 entry */
3411 mutex_lock(&trace_types_lock);
3415 /* disable all cpu buffers */
3416 for_each_tracing_cpu(cpu) {
3417 if (global_trace.data[cpu])
3418 atomic_inc(&global_trace.data[cpu]->disabled);
3419 if (max_tr.data[cpu])
3420 atomic_inc(&max_tr.data[cpu]->disabled);
3423 /* value is in KB */
3426 if (val != global_trace.entries) {
3427 ret = tracing_resize_ring_buffer(val);
3436 /* If check pages failed, return ENOMEM */
3437 if (tracing_disabled)
3440 for_each_tracing_cpu(cpu) {
3441 if (global_trace.data[cpu])
3442 atomic_dec(&global_trace.data[cpu]->disabled);
3443 if (max_tr.data[cpu])
3444 atomic_dec(&max_tr.data[cpu]->disabled);
3448 max_tr.entries = global_trace.entries;
3449 mutex_unlock(&trace_types_lock);
3454 static int mark_printk(const char *fmt, ...)
3458 va_start(args, fmt);
3459 ret = trace_vprintk(0, fmt, args);
3465 tracing_mark_write(struct file *filp, const char __user *ubuf,
3466 size_t cnt, loff_t *fpos)
3470 if (tracing_disabled)
3473 if (cnt > TRACE_BUF_SIZE)
3474 cnt = TRACE_BUF_SIZE;
3476 buf = kmalloc(cnt + 2, GFP_KERNEL);
3480 if (copy_from_user(buf, ubuf, cnt)) {
3484 if (buf[cnt-1] != '\n') {
3490 cnt = mark_printk("%s", buf);
3497 static int tracing_clock_show(struct seq_file *m, void *v)
3501 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3503 "%s%s%s%s", i ? " " : "",
3504 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3505 i == trace_clock_id ? "]" : "");
3511 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3512 size_t cnt, loff_t *fpos)
3515 const char *clockstr;
3518 if (cnt >= sizeof(buf))
3521 if (copy_from_user(&buf, ubuf, cnt))
3526 clockstr = strstrip(buf);
3528 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3529 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3532 if (i == ARRAY_SIZE(trace_clocks))
3537 mutex_lock(&trace_types_lock);
3539 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3541 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3543 mutex_unlock(&trace_types_lock);
3550 static int tracing_clock_open(struct inode *inode, struct file *file)
3552 if (tracing_disabled)
3554 return single_open(file, tracing_clock_show, NULL);
3557 static const struct file_operations tracing_max_lat_fops = {
3558 .open = tracing_open_generic,
3559 .read = tracing_max_lat_read,
3560 .write = tracing_max_lat_write,
3563 static const struct file_operations tracing_ctrl_fops = {
3564 .open = tracing_open_generic,
3565 .read = tracing_ctrl_read,
3566 .write = tracing_ctrl_write,
3569 static const struct file_operations set_tracer_fops = {
3570 .open = tracing_open_generic,
3571 .read = tracing_set_trace_read,
3572 .write = tracing_set_trace_write,
3575 static const struct file_operations tracing_pipe_fops = {
3576 .open = tracing_open_pipe,
3577 .poll = tracing_poll_pipe,
3578 .read = tracing_read_pipe,
3579 .splice_read = tracing_splice_read_pipe,
3580 .release = tracing_release_pipe,
3583 static const struct file_operations tracing_entries_fops = {
3584 .open = tracing_open_generic,
3585 .read = tracing_entries_read,
3586 .write = tracing_entries_write,
3589 static const struct file_operations tracing_mark_fops = {
3590 .open = tracing_open_generic,
3591 .write = tracing_mark_write,
3594 static const struct file_operations trace_clock_fops = {
3595 .open = tracing_clock_open,
3597 .llseek = seq_lseek,
3598 .release = single_release,
3599 .write = tracing_clock_write,
3602 struct ftrace_buffer_info {
3603 struct trace_array *tr;
3609 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3611 int cpu = (int)(long)inode->i_private;
3612 struct ftrace_buffer_info *info;
3614 if (tracing_disabled)
3617 info = kzalloc(sizeof(*info), GFP_KERNEL);
3621 info->tr = &global_trace;
3624 /* Force reading ring buffer for first read */
3625 info->read = (unsigned int)-1;
3627 filp->private_data = info;
3629 return nonseekable_open(inode, filp);
3633 tracing_buffers_read(struct file *filp, char __user *ubuf,
3634 size_t count, loff_t *ppos)
3636 struct ftrace_buffer_info *info = filp->private_data;
3645 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3649 /* Do we have previous read data to read? */
3650 if (info->read < PAGE_SIZE)
3655 trace_access_lock(info->cpu);
3656 ret = ring_buffer_read_page(info->tr->buffer,
3660 trace_access_unlock(info->cpu);
3664 pos = ring_buffer_page_len(info->spare);
3666 if (pos < PAGE_SIZE)
3667 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3670 size = PAGE_SIZE - info->read;
3674 ret = copy_to_user(ubuf, info->spare + info->read, size);
3685 static int tracing_buffers_release(struct inode *inode, struct file *file)
3687 struct ftrace_buffer_info *info = file->private_data;
3690 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3697 struct ring_buffer *buffer;
3702 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3703 struct pipe_buffer *buf)
3705 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3710 ring_buffer_free_read_page(ref->buffer, ref->page);
3715 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3716 struct pipe_buffer *buf)
3721 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3722 struct pipe_buffer *buf)
3724 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3729 /* Pipe buffer operations for a buffer. */
3730 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
3732 .map = generic_pipe_buf_map,
3733 .unmap = generic_pipe_buf_unmap,
3734 .confirm = generic_pipe_buf_confirm,
3735 .release = buffer_pipe_buf_release,
3736 .steal = buffer_pipe_buf_steal,
3737 .get = buffer_pipe_buf_get,
3741 * Callback from splice_to_pipe(), if we need to release some pages
3742 * at the end of the spd in case we error'ed out in filling the pipe.
3744 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3746 struct buffer_ref *ref =
3747 (struct buffer_ref *)spd->partial[i].private;
3752 ring_buffer_free_read_page(ref->buffer, ref->page);
3754 spd->partial[i].private = 0;
3758 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3759 struct pipe_inode_info *pipe, size_t len,
3762 struct ftrace_buffer_info *info = file->private_data;
3763 struct partial_page partial[PIPE_BUFFERS];
3764 struct page *pages[PIPE_BUFFERS];
3765 struct splice_pipe_desc spd = {
3769 .ops = &buffer_pipe_buf_ops,
3770 .spd_release = buffer_spd_release,
3772 struct buffer_ref *ref;
3773 int entries, size, i;
3776 if (*ppos & (PAGE_SIZE - 1)) {
3777 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3781 if (len & (PAGE_SIZE - 1)) {
3782 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3783 if (len < PAGE_SIZE)
3788 trace_access_lock(info->cpu);
3789 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3791 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3795 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3800 ref->buffer = info->tr->buffer;
3801 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3807 r = ring_buffer_read_page(ref->buffer, &ref->page,
3810 ring_buffer_free_read_page(ref->buffer,
3817 * zero out any left over data, this is going to
3820 size = ring_buffer_page_len(ref->page);
3821 if (size < PAGE_SIZE)
3822 memset(ref->page + size, 0, PAGE_SIZE - size);
3824 page = virt_to_page(ref->page);
3826 spd.pages[i] = page;
3827 spd.partial[i].len = PAGE_SIZE;
3828 spd.partial[i].offset = 0;
3829 spd.partial[i].private = (unsigned long)ref;
3833 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3836 trace_access_unlock(info->cpu);
3839 /* did we read anything? */
3840 if (!spd.nr_pages) {
3841 if (flags & SPLICE_F_NONBLOCK)
3849 ret = splice_to_pipe(pipe, &spd);
3854 static const struct file_operations tracing_buffers_fops = {
3855 .open = tracing_buffers_open,
3856 .read = tracing_buffers_read,
3857 .release = tracing_buffers_release,
3858 .splice_read = tracing_buffers_splice_read,
3859 .llseek = no_llseek,
3863 tracing_stats_read(struct file *filp, char __user *ubuf,
3864 size_t count, loff_t *ppos)
3866 unsigned long cpu = (unsigned long)filp->private_data;
3867 struct trace_array *tr = &global_trace;
3868 struct trace_seq *s;
3871 s = kmalloc(sizeof(*s), GFP_KERNEL);
3877 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3878 trace_seq_printf(s, "entries: %ld\n", cnt);
3880 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3881 trace_seq_printf(s, "overrun: %ld\n", cnt);
3883 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3884 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3886 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3893 static const struct file_operations tracing_stats_fops = {
3894 .open = tracing_open_generic,
3895 .read = tracing_stats_read,
3898 #ifdef CONFIG_DYNAMIC_FTRACE
3900 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3906 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3907 size_t cnt, loff_t *ppos)
3909 static char ftrace_dyn_info_buffer[1024];
3910 static DEFINE_MUTEX(dyn_info_mutex);
3911 unsigned long *p = filp->private_data;
3912 char *buf = ftrace_dyn_info_buffer;
3913 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3916 mutex_lock(&dyn_info_mutex);
3917 r = sprintf(buf, "%ld ", *p);
3919 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3922 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3924 mutex_unlock(&dyn_info_mutex);
3929 static const struct file_operations tracing_dyn_info_fops = {
3930 .open = tracing_open_generic,
3931 .read = tracing_read_dyn_info,
3935 static struct dentry *d_tracer;
3937 struct dentry *tracing_init_dentry(void)
3944 if (!debugfs_initialized())
3947 d_tracer = debugfs_create_dir("tracing", NULL);
3949 if (!d_tracer && !once) {
3951 pr_warning("Could not create debugfs directory 'tracing'\n");
3958 static struct dentry *d_percpu;
3960 struct dentry *tracing_dentry_percpu(void)
3963 struct dentry *d_tracer;
3968 d_tracer = tracing_init_dentry();
3973 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3975 if (!d_percpu && !once) {
3977 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3984 static void tracing_init_debugfs_percpu(long cpu)
3986 struct dentry *d_percpu = tracing_dentry_percpu();
3987 struct dentry *d_cpu;
3988 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3991 if (cpu > 999 || cpu < 0)
3994 sprintf(cpu_dir, "cpu%ld", cpu);
3995 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3997 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4001 /* per cpu trace_pipe */
4002 trace_create_file("trace_pipe", 0444, d_cpu,
4003 (void *) cpu, &tracing_pipe_fops);
4006 trace_create_file("trace", 0644, d_cpu,
4007 (void *) cpu, &tracing_fops);
4009 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4010 (void *) cpu, &tracing_buffers_fops);
4012 trace_create_file("stats", 0444, d_cpu,
4013 (void *) cpu, &tracing_stats_fops);
4016 #ifdef CONFIG_FTRACE_SELFTEST
4017 /* Let selftest have access to static functions in this file */
4018 #include "trace_selftest.c"
4021 struct trace_option_dentry {
4022 struct tracer_opt *opt;
4023 struct tracer_flags *flags;
4024 struct dentry *entry;
4028 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4031 struct trace_option_dentry *topt = filp->private_data;
4034 if (topt->flags->val & topt->opt->bit)
4039 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4043 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4046 struct trace_option_dentry *topt = filp->private_data;
4051 if (cnt >= sizeof(buf))
4054 if (copy_from_user(&buf, ubuf, cnt))
4059 ret = strict_strtoul(buf, 10, &val);
4063 if (val != 0 && val != 1)
4066 if (!!(topt->flags->val & topt->opt->bit) != val) {
4067 mutex_lock(&trace_types_lock);
4068 ret = __set_tracer_option(current_trace, topt->flags,
4070 mutex_unlock(&trace_types_lock);
4081 static const struct file_operations trace_options_fops = {
4082 .open = tracing_open_generic,
4083 .read = trace_options_read,
4084 .write = trace_options_write,
4088 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4091 long index = (long)filp->private_data;
4094 if (trace_flags & (1 << index))
4099 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4103 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4106 long index = (long)filp->private_data;
4111 if (cnt >= sizeof(buf))
4114 if (copy_from_user(&buf, ubuf, cnt))
4119 ret = strict_strtoul(buf, 10, &val);
4123 if (val != 0 && val != 1)
4125 set_tracer_flags(1 << index, val);
4132 static const struct file_operations trace_options_core_fops = {
4133 .open = tracing_open_generic,
4134 .read = trace_options_core_read,
4135 .write = trace_options_core_write,
4138 struct dentry *trace_create_file(const char *name,
4140 struct dentry *parent,
4142 const struct file_operations *fops)
4146 ret = debugfs_create_file(name, mode, parent, data, fops);
4148 pr_warning("Could not create debugfs '%s' entry\n", name);
4154 static struct dentry *trace_options_init_dentry(void)
4156 struct dentry *d_tracer;
4157 static struct dentry *t_options;
4162 d_tracer = tracing_init_dentry();
4166 t_options = debugfs_create_dir("options", d_tracer);
4168 pr_warning("Could not create debugfs directory 'options'\n");
4176 create_trace_option_file(struct trace_option_dentry *topt,
4177 struct tracer_flags *flags,
4178 struct tracer_opt *opt)
4180 struct dentry *t_options;
4182 t_options = trace_options_init_dentry();
4186 topt->flags = flags;
4189 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4190 &trace_options_fops);
4194 static struct trace_option_dentry *
4195 create_trace_option_files(struct tracer *tracer)
4197 struct trace_option_dentry *topts;
4198 struct tracer_flags *flags;
4199 struct tracer_opt *opts;
4205 flags = tracer->flags;
4207 if (!flags || !flags->opts)
4212 for (cnt = 0; opts[cnt].name; cnt++)
4215 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4219 for (cnt = 0; opts[cnt].name; cnt++)
4220 create_trace_option_file(&topts[cnt], flags,
4227 destroy_trace_option_files(struct trace_option_dentry *topts)
4234 for (cnt = 0; topts[cnt].opt; cnt++) {
4235 if (topts[cnt].entry)
4236 debugfs_remove(topts[cnt].entry);
4242 static struct dentry *
4243 create_trace_option_core_file(const char *option, long index)
4245 struct dentry *t_options;
4247 t_options = trace_options_init_dentry();
4251 return trace_create_file(option, 0644, t_options, (void *)index,
4252 &trace_options_core_fops);
4255 static __init void create_trace_options_dir(void)
4257 struct dentry *t_options;
4260 t_options = trace_options_init_dentry();
4264 for (i = 0; trace_options[i]; i++)
4265 create_trace_option_core_file(trace_options[i], i);
4268 static __init int tracer_init_debugfs(void)
4270 struct dentry *d_tracer;
4273 trace_access_lock_init();
4275 d_tracer = tracing_init_dentry();
4277 trace_create_file("tracing_enabled", 0644, d_tracer,
4278 &global_trace, &tracing_ctrl_fops);
4280 trace_create_file("trace_options", 0644, d_tracer,
4281 NULL, &tracing_iter_fops);
4283 trace_create_file("tracing_cpumask", 0644, d_tracer,
4284 NULL, &tracing_cpumask_fops);
4286 trace_create_file("trace", 0644, d_tracer,
4287 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4289 trace_create_file("available_tracers", 0444, d_tracer,
4290 &global_trace, &show_traces_fops);
4292 trace_create_file("current_tracer", 0644, d_tracer,
4293 &global_trace, &set_tracer_fops);
4295 #ifdef CONFIG_TRACER_MAX_TRACE
4296 trace_create_file("tracing_max_latency", 0644, d_tracer,
4297 &tracing_max_latency, &tracing_max_lat_fops);
4300 trace_create_file("tracing_thresh", 0644, d_tracer,
4301 &tracing_thresh, &tracing_max_lat_fops);
4303 trace_create_file("README", 0444, d_tracer,
4304 NULL, &tracing_readme_fops);
4306 trace_create_file("trace_pipe", 0444, d_tracer,
4307 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4309 trace_create_file("buffer_size_kb", 0644, d_tracer,
4310 &global_trace, &tracing_entries_fops);
4312 trace_create_file("trace_marker", 0220, d_tracer,
4313 NULL, &tracing_mark_fops);
4315 trace_create_file("saved_cmdlines", 0444, d_tracer,
4316 NULL, &tracing_saved_cmdlines_fops);
4318 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4321 #ifdef CONFIG_DYNAMIC_FTRACE
4322 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4323 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4325 #ifdef CONFIG_SYSPROF_TRACER
4326 init_tracer_sysprof_debugfs(d_tracer);
4329 create_trace_options_dir();
4331 for_each_tracing_cpu(cpu)
4332 tracing_init_debugfs_percpu(cpu);
4337 static int trace_panic_handler(struct notifier_block *this,
4338 unsigned long event, void *unused)
4340 if (ftrace_dump_on_oops)
4345 static struct notifier_block trace_panic_notifier = {
4346 .notifier_call = trace_panic_handler,
4348 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4351 static int trace_die_handler(struct notifier_block *self,
4357 if (ftrace_dump_on_oops)
4366 static struct notifier_block trace_die_notifier = {
4367 .notifier_call = trace_die_handler,
4372 * printk is set to max of 1024, we really don't need it that big.
4373 * Nothing should be printing 1000 characters anyway.
4375 #define TRACE_MAX_PRINT 1000
4378 * Define here KERN_TRACE so that we have one place to modify
4379 * it if we decide to change what log level the ftrace dump
4382 #define KERN_TRACE KERN_EMERG
4385 trace_printk_seq(struct trace_seq *s)
4387 /* Probably should print a warning here. */
4391 /* should be zero ended, but we are paranoid. */
4392 s->buffer[s->len] = 0;
4394 printk(KERN_TRACE "%s", s->buffer);
4399 static void __ftrace_dump(bool disable_tracing)
4401 static arch_spinlock_t ftrace_dump_lock =
4402 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
4403 /* use static because iter can be a bit big for the stack */
4404 static struct trace_iterator iter;
4405 unsigned int old_userobj;
4406 static int dump_ran;
4407 unsigned long flags;
4411 local_irq_save(flags);
4412 arch_spin_lock(&ftrace_dump_lock);
4420 if (disable_tracing)
4423 for_each_tracing_cpu(cpu) {
4424 atomic_inc(&global_trace.data[cpu]->disabled);
4427 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4429 /* don't look at user memory in panic mode */
4430 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4432 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4434 /* Simulate the iterator */
4435 iter.tr = &global_trace;
4436 iter.trace = current_trace;
4437 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4440 * We need to stop all tracing on all CPUS to read the
4441 * the next buffer. This is a bit expensive, but is
4442 * not done often. We fill all what we can read,
4443 * and then release the locks again.
4446 while (!trace_empty(&iter)) {
4449 printk(KERN_TRACE "---------------------------------\n");
4453 /* reset all but tr, trace, and overruns */
4454 memset(&iter.seq, 0,
4455 sizeof(struct trace_iterator) -
4456 offsetof(struct trace_iterator, seq));
4457 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4460 if (find_next_entry_inc(&iter) != NULL) {
4463 ret = print_trace_line(&iter);
4464 if (ret != TRACE_TYPE_NO_CONSUME)
4465 trace_consume(&iter);
4468 trace_printk_seq(&iter.seq);
4472 printk(KERN_TRACE " (ftrace buffer empty)\n");
4474 printk(KERN_TRACE "---------------------------------\n");
4476 /* Re-enable tracing if requested */
4477 if (!disable_tracing) {
4478 trace_flags |= old_userobj;
4480 for_each_tracing_cpu(cpu) {
4481 atomic_dec(&global_trace.data[cpu]->disabled);
4487 arch_spin_unlock(&ftrace_dump_lock);
4488 local_irq_restore(flags);
4491 /* By default: disable tracing after the dump */
4492 void ftrace_dump(void)
4494 __ftrace_dump(true);
4497 __init static int tracer_alloc_buffers(void)
4503 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4506 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4507 goto out_free_buffer_mask;
4509 /* To save memory, keep the ring buffer size to its minimum */
4510 if (ring_buffer_expanded)
4511 ring_buf_size = trace_buf_size;
4515 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4516 cpumask_copy(tracing_cpumask, cpu_all_mask);
4518 /* TODO: make the number of buffers hot pluggable with CPUS */
4519 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4520 TRACE_BUFFER_FLAGS);
4521 if (!global_trace.buffer) {
4522 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4524 goto out_free_cpumask;
4526 global_trace.entries = ring_buffer_size(global_trace.buffer);
4529 #ifdef CONFIG_TRACER_MAX_TRACE
4530 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4531 TRACE_BUFFER_FLAGS);
4532 if (!max_tr.buffer) {
4533 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4535 ring_buffer_free(global_trace.buffer);
4536 goto out_free_cpumask;
4538 max_tr.entries = ring_buffer_size(max_tr.buffer);
4539 WARN_ON(max_tr.entries != global_trace.entries);
4542 /* Allocate the first page for all buffers */
4543 for_each_tracing_cpu(i) {
4544 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4545 max_tr.data[i] = &per_cpu(max_tr_data, i);
4548 trace_init_cmdlines();
4550 register_tracer(&nop_trace);
4551 current_trace = &nop_trace;
4552 #ifdef CONFIG_BOOT_TRACER
4553 register_tracer(&boot_tracer);
4555 /* All seems OK, enable tracing */
4556 tracing_disabled = 0;
4558 atomic_notifier_chain_register(&panic_notifier_list,
4559 &trace_panic_notifier);
4561 register_die_notifier(&trace_die_notifier);
4566 free_cpumask_var(tracing_cpumask);
4567 out_free_buffer_mask:
4568 free_cpumask_var(tracing_buffer_mask);
4573 __init static int clear_boot_tracer(void)
4576 * The default tracer at boot buffer is an init section.
4577 * This function is called in lateinit. If we did not
4578 * find the boot tracer, then clear it out, to prevent
4579 * later registration from accessing the buffer that is
4580 * about to be freed.
4582 if (!default_bootup_tracer)
4585 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4586 default_bootup_tracer);
4587 default_bootup_tracer = NULL;
4592 early_initcall(tracer_alloc_buffers);
4593 fs_initcall(tracer_init_debugfs);
4594 late_initcall(clear_boot_tracer);