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 cpumask_var_t __read_mostly tracing_buffer_mask;
107 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
109 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
110 * is set, then ftrace_dump is called. This will output the contents
111 * of the ftrace buffers to the console. This is very useful for
112 * capturing traces that lead to crashes and outputing it to a
115 * It is default off, but you can enable it with either specifying
116 * "ftrace_dump_on_oops" in the kernel command line, or setting
117 * /proc/sys/kernel/ftrace_dump_on_oops
118 * Set 1 if you want to dump buffers of all CPUs
119 * Set 2 if you want to dump the buffer of the CPU that triggered oops
122 enum ftrace_dump_mode 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 if (*str++ != '=' || !*str) {
143 ftrace_dump_on_oops = DUMP_ALL;
147 if (!strcmp("orig_cpu", str)) {
148 ftrace_dump_on_oops = DUMP_ORIG;
154 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
156 unsigned long long ns2usecs(cycle_t nsec)
164 * The global_trace is the descriptor that holds the tracing
165 * buffers for the live tracing. For each CPU, it contains
166 * a link list of pages that will store trace entries. The
167 * page descriptor of the pages in the memory is used to hold
168 * the link list by linking the lru item in the page descriptor
169 * to each of the pages in the buffer per CPU.
171 * For each active CPU there is a data field that holds the
172 * pages for the buffer for that CPU. Each CPU has the same number
173 * of pages allocated for its buffer.
175 static struct trace_array global_trace;
177 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
179 int filter_current_check_discard(struct ring_buffer *buffer,
180 struct ftrace_event_call *call, void *rec,
181 struct ring_buffer_event *event)
183 return filter_check_discard(call, rec, buffer, event);
185 EXPORT_SYMBOL_GPL(filter_current_check_discard);
187 cycle_t ftrace_now(int cpu)
191 /* Early boot up does not have a buffer yet */
192 if (!global_trace.buffer)
193 return trace_clock_local();
195 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
196 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
202 * The max_tr is used to snapshot the global_trace when a maximum
203 * latency is reached. Some tracers will use this to store a maximum
204 * trace while it continues examining live traces.
206 * The buffers for the max_tr are set up the same as the global_trace.
207 * When a snapshot is taken, the link list of the max_tr is swapped
208 * with the link list of the global_trace and the buffers are reset for
209 * the global_trace so the tracing can continue.
211 static struct trace_array max_tr;
213 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
215 /* tracer_enabled is used to toggle activation of a tracer */
216 static int tracer_enabled = 1;
219 * tracing_is_enabled - return tracer_enabled status
221 * This function is used by other tracers to know the status
222 * of the tracer_enabled flag. Tracers may use this function
223 * to know if it should enable their features when starting
224 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
226 int tracing_is_enabled(void)
228 return tracer_enabled;
232 * trace_buf_size is the size in bytes that is allocated
233 * for a buffer. Note, the number of bytes is always rounded
236 * This number is purposely set to a low number of 16384.
237 * If the dump on oops happens, it will be much appreciated
238 * to not have to wait for all that output. Anyway this can be
239 * boot time and run time configurable.
241 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
243 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
245 /* trace_types holds a link list of available tracers. */
246 static struct tracer *trace_types __read_mostly;
248 /* current_trace points to the tracer that is currently active */
249 static struct tracer *current_trace __read_mostly;
252 * trace_types_lock is used to protect the trace_types list.
254 static DEFINE_MUTEX(trace_types_lock);
257 * serialize the access of the ring buffer
259 * ring buffer serializes readers, but it is low level protection.
260 * The validity of the events (which returns by ring_buffer_peek() ..etc)
261 * are not protected by ring buffer.
263 * The content of events may become garbage if we allow other process consumes
264 * these events concurrently:
265 * A) the page of the consumed events may become a normal page
266 * (not reader page) in ring buffer, and this page will be rewrited
267 * by events producer.
268 * B) The page of the consumed events may become a page for splice_read,
269 * and this page will be returned to system.
271 * These primitives allow multi process access to different cpu ring buffer
274 * These primitives don't distinguish read-only and read-consume access.
275 * Multi read-only access are also serialized.
279 static DECLARE_RWSEM(all_cpu_access_lock);
280 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
282 static inline void trace_access_lock(int cpu)
284 if (cpu == TRACE_PIPE_ALL_CPU) {
285 /* gain it for accessing the whole ring buffer. */
286 down_write(&all_cpu_access_lock);
288 /* gain it for accessing a cpu ring buffer. */
290 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
291 down_read(&all_cpu_access_lock);
293 /* Secondly block other access to this @cpu ring buffer. */
294 mutex_lock(&per_cpu(cpu_access_lock, cpu));
298 static inline void trace_access_unlock(int cpu)
300 if (cpu == TRACE_PIPE_ALL_CPU) {
301 up_write(&all_cpu_access_lock);
303 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
304 up_read(&all_cpu_access_lock);
308 static inline void trace_access_lock_init(void)
312 for_each_possible_cpu(cpu)
313 mutex_init(&per_cpu(cpu_access_lock, cpu));
318 static DEFINE_MUTEX(access_lock);
320 static inline void trace_access_lock(int cpu)
323 mutex_lock(&access_lock);
326 static inline void trace_access_unlock(int cpu)
329 mutex_unlock(&access_lock);
332 static inline void trace_access_lock_init(void)
338 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
339 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
341 /* trace_flags holds trace_options default values */
342 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
343 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
344 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD;
346 static int trace_stop_count;
347 static DEFINE_SPINLOCK(tracing_start_lock);
350 * trace_wake_up - wake up tasks waiting for trace input
352 * Simply wakes up any task that is blocked on the trace_wait
353 * queue. These is used with trace_poll for tasks polling the trace.
355 void trace_wake_up(void)
359 if (trace_flags & TRACE_ITER_BLOCK)
362 * The runqueue_is_locked() can fail, but this is the best we
366 if (!runqueue_is_locked(cpu))
367 wake_up(&trace_wait);
371 static int __init set_buf_size(char *str)
373 unsigned long buf_size;
377 buf_size = memparse(str, &str);
378 /* nr_entries can not be zero */
381 trace_buf_size = buf_size;
384 __setup("trace_buf_size=", set_buf_size);
386 static int __init set_tracing_thresh(char *str)
388 unsigned long threshhold;
393 ret = strict_strtoul(str, 0, &threshhold);
396 tracing_thresh = threshhold * 1000;
399 __setup("tracing_thresh=", set_tracing_thresh);
401 unsigned long nsecs_to_usecs(unsigned long nsecs)
406 /* These must match the bit postions in trace_iterator_flags */
407 static const char *trace_options[] = {
436 { trace_clock_local, "local" },
437 { trace_clock_global, "global" },
443 * trace_parser_get_init - gets the buffer for trace parser
445 int trace_parser_get_init(struct trace_parser *parser, int size)
447 memset(parser, 0, sizeof(*parser));
449 parser->buffer = kmalloc(size, GFP_KERNEL);
458 * trace_parser_put - frees the buffer for trace parser
460 void trace_parser_put(struct trace_parser *parser)
462 kfree(parser->buffer);
466 * trace_get_user - reads the user input string separated by space
467 * (matched by isspace(ch))
469 * For each string found the 'struct trace_parser' is updated,
470 * and the function returns.
472 * Returns number of bytes read.
474 * See kernel/trace/trace.h for 'struct trace_parser' details.
476 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
477 size_t cnt, loff_t *ppos)
484 trace_parser_clear(parser);
486 ret = get_user(ch, ubuf++);
494 * The parser is not finished with the last write,
495 * continue reading the user input without skipping spaces.
498 /* skip white space */
499 while (cnt && isspace(ch)) {
500 ret = get_user(ch, ubuf++);
507 /* only spaces were written */
517 /* read the non-space input */
518 while (cnt && !isspace(ch)) {
519 if (parser->idx < parser->size - 1)
520 parser->buffer[parser->idx++] = ch;
525 ret = get_user(ch, ubuf++);
532 /* We either got finished input or we have to wait for another call. */
534 parser->buffer[parser->idx] = 0;
535 parser->cont = false;
538 parser->buffer[parser->idx++] = ch;
548 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
556 if (s->len <= s->readpos)
559 len = s->len - s->readpos;
562 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
572 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
577 if (s->len <= s->readpos)
580 len = s->len - s->readpos;
583 ret = memcpy(buf, s->buffer + s->readpos, cnt);
592 * ftrace_max_lock is used to protect the swapping of buffers
593 * when taking a max snapshot. The buffers themselves are
594 * protected by per_cpu spinlocks. But the action of the swap
595 * needs its own lock.
597 * This is defined as a arch_spinlock_t in order to help
598 * with performance when lockdep debugging is enabled.
600 * It is also used in other places outside the update_max_tr
601 * so it needs to be defined outside of the
602 * CONFIG_TRACER_MAX_TRACE.
604 static arch_spinlock_t ftrace_max_lock =
605 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
607 unsigned long __read_mostly tracing_thresh;
609 #ifdef CONFIG_TRACER_MAX_TRACE
610 unsigned long __read_mostly tracing_max_latency;
613 * Copy the new maximum trace into the separate maximum-trace
614 * structure. (this way the maximum trace is permanently saved,
615 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
618 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
620 struct trace_array_cpu *data = tr->data[cpu];
621 struct trace_array_cpu *max_data;
624 max_tr.time_start = data->preempt_timestamp;
626 max_data = max_tr.data[cpu];
627 max_data->saved_latency = tracing_max_latency;
628 max_data->critical_start = data->critical_start;
629 max_data->critical_end = data->critical_end;
631 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
632 max_data->pid = tsk->pid;
633 max_data->uid = task_uid(tsk);
634 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
635 max_data->policy = tsk->policy;
636 max_data->rt_priority = tsk->rt_priority;
638 /* record this tasks comm */
639 tracing_record_cmdline(tsk);
643 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
645 * @tsk: the task with the latency
646 * @cpu: The cpu that initiated the trace.
648 * Flip the buffers between the @tr and the max_tr and record information
649 * about which task was the cause of this latency.
652 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
654 struct ring_buffer *buf = tr->buffer;
656 if (trace_stop_count)
659 WARN_ON_ONCE(!irqs_disabled());
660 if (!current_trace->use_max_tr) {
664 arch_spin_lock(&ftrace_max_lock);
666 tr->buffer = max_tr.buffer;
669 __update_max_tr(tr, tsk, cpu);
670 arch_spin_unlock(&ftrace_max_lock);
674 * update_max_tr_single - only copy one trace over, and reset the rest
676 * @tsk - task with the latency
677 * @cpu - the cpu of the buffer to copy.
679 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
682 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
686 if (trace_stop_count)
689 WARN_ON_ONCE(!irqs_disabled());
690 if (!current_trace->use_max_tr) {
695 arch_spin_lock(&ftrace_max_lock);
697 ftrace_disable_cpu();
699 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
703 * We failed to swap the buffer due to a commit taking
704 * place on this CPU. We fail to record, but we reset
705 * the max trace buffer (no one writes directly to it)
706 * and flag that it failed.
708 trace_array_printk(&max_tr, _THIS_IP_,
709 "Failed to swap buffers due to commit in progress\n");
714 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
716 __update_max_tr(tr, tsk, cpu);
717 arch_spin_unlock(&ftrace_max_lock);
719 #endif /* CONFIG_TRACER_MAX_TRACE */
722 * register_tracer - register a tracer with the ftrace system.
723 * @type - the plugin for the tracer
725 * Register a new plugin tracer.
727 int register_tracer(struct tracer *type)
728 __releases(kernel_lock)
729 __acquires(kernel_lock)
735 pr_info("Tracer must have a name\n");
739 if (strlen(type->name) >= MAX_TRACER_SIZE) {
740 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
744 mutex_lock(&trace_types_lock);
746 tracing_selftest_running = true;
748 for (t = trace_types; t; t = t->next) {
749 if (strcmp(type->name, t->name) == 0) {
751 pr_info("Tracer %s already registered\n",
759 type->set_flag = &dummy_set_flag;
761 type->flags = &dummy_tracer_flags;
763 if (!type->flags->opts)
764 type->flags->opts = dummy_tracer_opt;
765 if (!type->wait_pipe)
766 type->wait_pipe = default_wait_pipe;
769 #ifdef CONFIG_FTRACE_STARTUP_TEST
770 if (type->selftest && !tracing_selftest_disabled) {
771 struct tracer *saved_tracer = current_trace;
772 struct trace_array *tr = &global_trace;
775 * Run a selftest on this tracer.
776 * Here we reset the trace buffer, and set the current
777 * tracer to be this tracer. The tracer can then run some
778 * internal tracing to verify that everything is in order.
779 * If we fail, we do not register this tracer.
781 tracing_reset_online_cpus(tr);
783 current_trace = type;
784 /* the test is responsible for initializing and enabling */
785 pr_info("Testing tracer %s: ", type->name);
786 ret = type->selftest(type, tr);
787 /* the test is responsible for resetting too */
788 current_trace = saved_tracer;
790 printk(KERN_CONT "FAILED!\n");
793 /* Only reset on passing, to avoid touching corrupted buffers */
794 tracing_reset_online_cpus(tr);
796 printk(KERN_CONT "PASSED\n");
800 type->next = trace_types;
804 tracing_selftest_running = false;
805 mutex_unlock(&trace_types_lock);
807 if (ret || !default_bootup_tracer)
810 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
813 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
814 /* Do we want this tracer to start on bootup? */
815 tracing_set_tracer(type->name);
816 default_bootup_tracer = NULL;
817 /* disable other selftests, since this will break it. */
818 tracing_selftest_disabled = 1;
819 #ifdef CONFIG_FTRACE_STARTUP_TEST
820 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
828 void unregister_tracer(struct tracer *type)
832 mutex_lock(&trace_types_lock);
833 for (t = &trace_types; *t; t = &(*t)->next) {
837 pr_info("Tracer %s not registered\n", type->name);
843 if (type == current_trace && tracer_enabled) {
846 if (current_trace->stop)
847 current_trace->stop(&global_trace);
848 current_trace = &nop_trace;
851 mutex_unlock(&trace_types_lock);
854 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
856 ftrace_disable_cpu();
857 ring_buffer_reset_cpu(buffer, cpu);
861 void tracing_reset(struct trace_array *tr, int cpu)
863 struct ring_buffer *buffer = tr->buffer;
865 ring_buffer_record_disable(buffer);
867 /* Make sure all commits have finished */
869 __tracing_reset(buffer, cpu);
871 ring_buffer_record_enable(buffer);
874 void tracing_reset_online_cpus(struct trace_array *tr)
876 struct ring_buffer *buffer = tr->buffer;
879 ring_buffer_record_disable(buffer);
881 /* Make sure all commits have finished */
884 tr->time_start = ftrace_now(tr->cpu);
886 for_each_online_cpu(cpu)
887 __tracing_reset(buffer, cpu);
889 ring_buffer_record_enable(buffer);
892 void tracing_reset_current(int cpu)
894 tracing_reset(&global_trace, cpu);
897 void tracing_reset_current_online_cpus(void)
899 tracing_reset_online_cpus(&global_trace);
902 #define SAVED_CMDLINES 128
903 #define NO_CMDLINE_MAP UINT_MAX
904 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
905 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
906 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
907 static int cmdline_idx;
908 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
910 /* temporary disable recording */
911 static atomic_t trace_record_cmdline_disabled __read_mostly;
913 static void trace_init_cmdlines(void)
915 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
916 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
920 int is_tracing_stopped(void)
922 return trace_stop_count;
926 * ftrace_off_permanent - disable all ftrace code permanently
928 * This should only be called when a serious anomally has
929 * been detected. This will turn off the function tracing,
930 * ring buffers, and other tracing utilites. It takes no
931 * locks and can be called from any context.
933 void ftrace_off_permanent(void)
935 tracing_disabled = 1;
937 tracing_off_permanent();
941 * tracing_start - quick start of the tracer
943 * If tracing is enabled but was stopped by tracing_stop,
944 * this will start the tracer back up.
946 void tracing_start(void)
948 struct ring_buffer *buffer;
951 if (tracing_disabled)
954 spin_lock_irqsave(&tracing_start_lock, flags);
955 if (--trace_stop_count) {
956 if (trace_stop_count < 0) {
957 /* Someone screwed up their debugging */
959 trace_stop_count = 0;
964 /* Prevent the buffers from switching */
965 arch_spin_lock(&ftrace_max_lock);
967 buffer = global_trace.buffer;
969 ring_buffer_record_enable(buffer);
971 buffer = max_tr.buffer;
973 ring_buffer_record_enable(buffer);
975 arch_spin_unlock(&ftrace_max_lock);
979 spin_unlock_irqrestore(&tracing_start_lock, flags);
983 * tracing_stop - quick stop of the tracer
985 * Light weight way to stop tracing. Use in conjunction with
988 void tracing_stop(void)
990 struct ring_buffer *buffer;
994 spin_lock_irqsave(&tracing_start_lock, flags);
995 if (trace_stop_count++)
998 /* Prevent the buffers from switching */
999 arch_spin_lock(&ftrace_max_lock);
1001 buffer = global_trace.buffer;
1003 ring_buffer_record_disable(buffer);
1005 buffer = max_tr.buffer;
1007 ring_buffer_record_disable(buffer);
1009 arch_spin_unlock(&ftrace_max_lock);
1012 spin_unlock_irqrestore(&tracing_start_lock, flags);
1015 void trace_stop_cmdline_recording(void);
1017 static void trace_save_cmdline(struct task_struct *tsk)
1021 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1025 * It's not the end of the world if we don't get
1026 * the lock, but we also don't want to spin
1027 * nor do we want to disable interrupts,
1028 * so if we miss here, then better luck next time.
1030 if (!arch_spin_trylock(&trace_cmdline_lock))
1033 idx = map_pid_to_cmdline[tsk->pid];
1034 if (idx == NO_CMDLINE_MAP) {
1035 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1038 * Check whether the cmdline buffer at idx has a pid
1039 * mapped. We are going to overwrite that entry so we
1040 * need to clear the map_pid_to_cmdline. Otherwise we
1041 * would read the new comm for the old pid.
1043 pid = map_cmdline_to_pid[idx];
1044 if (pid != NO_CMDLINE_MAP)
1045 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1047 map_cmdline_to_pid[idx] = tsk->pid;
1048 map_pid_to_cmdline[tsk->pid] = idx;
1053 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1055 arch_spin_unlock(&trace_cmdline_lock);
1058 void trace_find_cmdline(int pid, char comm[])
1063 strcpy(comm, "<idle>");
1067 if (WARN_ON_ONCE(pid < 0)) {
1068 strcpy(comm, "<XXX>");
1072 if (pid > PID_MAX_DEFAULT) {
1073 strcpy(comm, "<...>");
1078 arch_spin_lock(&trace_cmdline_lock);
1079 map = map_pid_to_cmdline[pid];
1080 if (map != NO_CMDLINE_MAP)
1081 strcpy(comm, saved_cmdlines[map]);
1083 strcpy(comm, "<...>");
1085 arch_spin_unlock(&trace_cmdline_lock);
1089 void tracing_record_cmdline(struct task_struct *tsk)
1091 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1095 trace_save_cmdline(tsk);
1099 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1102 struct task_struct *tsk = current;
1104 entry->preempt_count = pc & 0xff;
1105 entry->pid = (tsk) ? tsk->pid : 0;
1106 entry->lock_depth = (tsk) ? tsk->lock_depth : 0;
1108 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1109 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1111 TRACE_FLAG_IRQS_NOSUPPORT |
1113 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1114 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1115 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1117 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1119 struct ring_buffer_event *
1120 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1123 unsigned long flags, int pc)
1125 struct ring_buffer_event *event;
1127 event = ring_buffer_lock_reserve(buffer, len);
1128 if (event != NULL) {
1129 struct trace_entry *ent = ring_buffer_event_data(event);
1131 tracing_generic_entry_update(ent, flags, pc);
1139 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1140 struct ring_buffer_event *event,
1141 unsigned long flags, int pc,
1144 ring_buffer_unlock_commit(buffer, event);
1146 ftrace_trace_stack(buffer, flags, 6, pc);
1147 ftrace_trace_userstack(buffer, flags, pc);
1153 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1154 struct ring_buffer_event *event,
1155 unsigned long flags, int pc)
1157 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1160 struct ring_buffer_event *
1161 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1162 int type, unsigned long len,
1163 unsigned long flags, int pc)
1165 *current_rb = global_trace.buffer;
1166 return trace_buffer_lock_reserve(*current_rb,
1167 type, len, flags, pc);
1169 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1171 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1172 struct ring_buffer_event *event,
1173 unsigned long flags, int pc)
1175 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1177 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1179 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1180 struct ring_buffer_event *event,
1181 unsigned long flags, int pc)
1183 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1185 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1187 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1188 struct ring_buffer_event *event)
1190 ring_buffer_discard_commit(buffer, event);
1192 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1195 trace_function(struct trace_array *tr,
1196 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1199 struct ftrace_event_call *call = &event_function;
1200 struct ring_buffer *buffer = tr->buffer;
1201 struct ring_buffer_event *event;
1202 struct ftrace_entry *entry;
1204 /* If we are reading the ring buffer, don't trace */
1205 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1208 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1212 entry = ring_buffer_event_data(event);
1214 entry->parent_ip = parent_ip;
1216 if (!filter_check_discard(call, entry, buffer, event))
1217 ring_buffer_unlock_commit(buffer, event);
1221 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1222 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1225 if (likely(!atomic_read(&data->disabled)))
1226 trace_function(tr, ip, parent_ip, flags, pc);
1229 #ifdef CONFIG_STACKTRACE
1230 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1231 unsigned long flags,
1234 struct ftrace_event_call *call = &event_kernel_stack;
1235 struct ring_buffer_event *event;
1236 struct stack_entry *entry;
1237 struct stack_trace trace;
1239 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1240 sizeof(*entry), flags, pc);
1243 entry = ring_buffer_event_data(event);
1244 memset(&entry->caller, 0, sizeof(entry->caller));
1246 trace.nr_entries = 0;
1247 trace.max_entries = FTRACE_STACK_ENTRIES;
1249 trace.entries = entry->caller;
1251 save_stack_trace(&trace);
1252 if (!filter_check_discard(call, entry, buffer, event))
1253 ring_buffer_unlock_commit(buffer, event);
1256 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1259 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1262 __ftrace_trace_stack(buffer, flags, skip, pc);
1265 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1268 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
1272 * trace_dump_stack - record a stack back trace in the trace buffer
1274 void trace_dump_stack(void)
1276 unsigned long flags;
1278 if (tracing_disabled || tracing_selftest_running)
1281 local_save_flags(flags);
1283 /* skipping 3 traces, seems to get us at the caller of this function */
1284 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1287 static DEFINE_PER_CPU(int, user_stack_count);
1290 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1292 struct ftrace_event_call *call = &event_user_stack;
1293 struct ring_buffer_event *event;
1294 struct userstack_entry *entry;
1295 struct stack_trace trace;
1297 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1301 * NMIs can not handle page faults, even with fix ups.
1302 * The save user stack can (and often does) fault.
1304 if (unlikely(in_nmi()))
1308 * prevent recursion, since the user stack tracing may
1309 * trigger other kernel events.
1312 if (__this_cpu_read(user_stack_count))
1315 __this_cpu_inc(user_stack_count);
1319 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1320 sizeof(*entry), flags, pc);
1323 entry = ring_buffer_event_data(event);
1325 entry->tgid = current->tgid;
1326 memset(&entry->caller, 0, sizeof(entry->caller));
1328 trace.nr_entries = 0;
1329 trace.max_entries = FTRACE_STACK_ENTRIES;
1331 trace.entries = entry->caller;
1333 save_stack_trace_user(&trace);
1334 if (!filter_check_discard(call, entry, buffer, event))
1335 ring_buffer_unlock_commit(buffer, event);
1337 __this_cpu_dec(user_stack_count);
1344 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1346 ftrace_trace_userstack(tr, flags, preempt_count());
1350 #endif /* CONFIG_STACKTRACE */
1353 * trace_vbprintk - write binary msg to tracing buffer
1356 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1358 static arch_spinlock_t trace_buf_lock =
1359 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1360 static u32 trace_buf[TRACE_BUF_SIZE];
1362 struct ftrace_event_call *call = &event_bprint;
1363 struct ring_buffer_event *event;
1364 struct ring_buffer *buffer;
1365 struct trace_array *tr = &global_trace;
1366 struct trace_array_cpu *data;
1367 struct bprint_entry *entry;
1368 unsigned long flags;
1370 int cpu, len = 0, size, pc;
1372 if (unlikely(tracing_selftest_running || tracing_disabled))
1375 /* Don't pollute graph traces with trace_vprintk internals */
1376 pause_graph_tracing();
1378 pc = preempt_count();
1379 preempt_disable_notrace();
1380 cpu = raw_smp_processor_id();
1381 data = tr->data[cpu];
1383 disable = atomic_inc_return(&data->disabled);
1384 if (unlikely(disable != 1))
1387 /* Lockdep uses trace_printk for lock tracing */
1388 local_irq_save(flags);
1389 arch_spin_lock(&trace_buf_lock);
1390 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1392 if (len > TRACE_BUF_SIZE || len < 0)
1395 size = sizeof(*entry) + sizeof(u32) * len;
1396 buffer = tr->buffer;
1397 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1401 entry = ring_buffer_event_data(event);
1405 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1406 if (!filter_check_discard(call, entry, buffer, event)) {
1407 ring_buffer_unlock_commit(buffer, event);
1408 ftrace_trace_stack(buffer, flags, 6, pc);
1412 arch_spin_unlock(&trace_buf_lock);
1413 local_irq_restore(flags);
1416 atomic_dec_return(&data->disabled);
1417 preempt_enable_notrace();
1418 unpause_graph_tracing();
1422 EXPORT_SYMBOL_GPL(trace_vbprintk);
1424 int trace_array_printk(struct trace_array *tr,
1425 unsigned long ip, const char *fmt, ...)
1430 if (!(trace_flags & TRACE_ITER_PRINTK))
1434 ret = trace_array_vprintk(tr, ip, fmt, ap);
1439 int trace_array_vprintk(struct trace_array *tr,
1440 unsigned long ip, const char *fmt, va_list args)
1442 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1443 static char trace_buf[TRACE_BUF_SIZE];
1445 struct ftrace_event_call *call = &event_print;
1446 struct ring_buffer_event *event;
1447 struct ring_buffer *buffer;
1448 struct trace_array_cpu *data;
1449 int cpu, len = 0, size, pc;
1450 struct print_entry *entry;
1451 unsigned long irq_flags;
1454 if (tracing_disabled || tracing_selftest_running)
1457 pc = preempt_count();
1458 preempt_disable_notrace();
1459 cpu = raw_smp_processor_id();
1460 data = tr->data[cpu];
1462 disable = atomic_inc_return(&data->disabled);
1463 if (unlikely(disable != 1))
1466 pause_graph_tracing();
1467 raw_local_irq_save(irq_flags);
1468 arch_spin_lock(&trace_buf_lock);
1469 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1471 size = sizeof(*entry) + len + 1;
1472 buffer = tr->buffer;
1473 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1477 entry = ring_buffer_event_data(event);
1480 memcpy(&entry->buf, trace_buf, len);
1481 entry->buf[len] = '\0';
1482 if (!filter_check_discard(call, entry, buffer, event)) {
1483 ring_buffer_unlock_commit(buffer, event);
1484 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1488 arch_spin_unlock(&trace_buf_lock);
1489 raw_local_irq_restore(irq_flags);
1490 unpause_graph_tracing();
1492 atomic_dec_return(&data->disabled);
1493 preempt_enable_notrace();
1498 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1500 return trace_array_vprintk(&global_trace, ip, fmt, args);
1502 EXPORT_SYMBOL_GPL(trace_vprintk);
1504 static void trace_iterator_increment(struct trace_iterator *iter)
1506 /* Don't allow ftrace to trace into the ring buffers */
1507 ftrace_disable_cpu();
1510 if (iter->buffer_iter[iter->cpu])
1511 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1513 ftrace_enable_cpu();
1516 static struct trace_entry *
1517 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1518 unsigned long *lost_events)
1520 struct ring_buffer_event *event;
1521 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1523 /* Don't allow ftrace to trace into the ring buffers */
1524 ftrace_disable_cpu();
1527 event = ring_buffer_iter_peek(buf_iter, ts);
1529 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1532 ftrace_enable_cpu();
1534 return event ? ring_buffer_event_data(event) : NULL;
1537 static struct trace_entry *
1538 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1539 unsigned long *missing_events, u64 *ent_ts)
1541 struct ring_buffer *buffer = iter->tr->buffer;
1542 struct trace_entry *ent, *next = NULL;
1543 unsigned long lost_events = 0, next_lost = 0;
1544 int cpu_file = iter->cpu_file;
1545 u64 next_ts = 0, ts;
1550 * If we are in a per_cpu trace file, don't bother by iterating over
1551 * all cpu and peek directly.
1553 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1554 if (ring_buffer_empty_cpu(buffer, cpu_file))
1556 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1558 *ent_cpu = cpu_file;
1563 for_each_tracing_cpu(cpu) {
1565 if (ring_buffer_empty_cpu(buffer, cpu))
1568 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1571 * Pick the entry with the smallest timestamp:
1573 if (ent && (!next || ts < next_ts)) {
1577 next_lost = lost_events;
1582 *ent_cpu = next_cpu;
1588 *missing_events = next_lost;
1593 /* Find the next real entry, without updating the iterator itself */
1594 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1595 int *ent_cpu, u64 *ent_ts)
1597 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1600 /* Find the next real entry, and increment the iterator to the next entry */
1601 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1603 iter->ent = __find_next_entry(iter, &iter->cpu,
1604 &iter->lost_events, &iter->ts);
1607 trace_iterator_increment(iter);
1609 return iter->ent ? iter : NULL;
1612 static void trace_consume(struct trace_iterator *iter)
1614 /* Don't allow ftrace to trace into the ring buffers */
1615 ftrace_disable_cpu();
1616 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1617 &iter->lost_events);
1618 ftrace_enable_cpu();
1621 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1623 struct trace_iterator *iter = m->private;
1627 WARN_ON_ONCE(iter->leftover);
1631 /* can't go backwards */
1636 ent = trace_find_next_entry_inc(iter);
1640 while (ent && iter->idx < i)
1641 ent = trace_find_next_entry_inc(iter);
1648 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1650 struct trace_array *tr = iter->tr;
1651 struct ring_buffer_event *event;
1652 struct ring_buffer_iter *buf_iter;
1653 unsigned long entries = 0;
1656 tr->data[cpu]->skipped_entries = 0;
1658 if (!iter->buffer_iter[cpu])
1661 buf_iter = iter->buffer_iter[cpu];
1662 ring_buffer_iter_reset(buf_iter);
1665 * We could have the case with the max latency tracers
1666 * that a reset never took place on a cpu. This is evident
1667 * by the timestamp being before the start of the buffer.
1669 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1670 if (ts >= iter->tr->time_start)
1673 ring_buffer_read(buf_iter, NULL);
1676 tr->data[cpu]->skipped_entries = entries;
1680 * The current tracer is copied to avoid a global locking
1683 static void *s_start(struct seq_file *m, loff_t *pos)
1685 struct trace_iterator *iter = m->private;
1686 static struct tracer *old_tracer;
1687 int cpu_file = iter->cpu_file;
1692 /* copy the tracer to avoid using a global lock all around */
1693 mutex_lock(&trace_types_lock);
1694 if (unlikely(old_tracer != current_trace && current_trace)) {
1695 old_tracer = current_trace;
1696 *iter->trace = *current_trace;
1698 mutex_unlock(&trace_types_lock);
1700 atomic_inc(&trace_record_cmdline_disabled);
1702 if (*pos != iter->pos) {
1707 ftrace_disable_cpu();
1709 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1710 for_each_tracing_cpu(cpu)
1711 tracing_iter_reset(iter, cpu);
1713 tracing_iter_reset(iter, cpu_file);
1715 ftrace_enable_cpu();
1718 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1723 * If we overflowed the seq_file before, then we want
1724 * to just reuse the trace_seq buffer again.
1730 p = s_next(m, p, &l);
1734 trace_event_read_lock();
1735 trace_access_lock(cpu_file);
1739 static void s_stop(struct seq_file *m, void *p)
1741 struct trace_iterator *iter = m->private;
1743 atomic_dec(&trace_record_cmdline_disabled);
1744 trace_access_unlock(iter->cpu_file);
1745 trace_event_read_unlock();
1748 static void print_lat_help_header(struct seq_file *m)
1750 seq_puts(m, "# _------=> CPU# \n");
1751 seq_puts(m, "# / _-----=> irqs-off \n");
1752 seq_puts(m, "# | / _----=> need-resched \n");
1753 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1754 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1755 seq_puts(m, "# |||| /_--=> lock-depth \n");
1756 seq_puts(m, "# |||||/ delay \n");
1757 seq_puts(m, "# cmd pid |||||| time | caller \n");
1758 seq_puts(m, "# \\ / |||||| \\ | / \n");
1761 static void print_func_help_header(struct seq_file *m)
1763 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1764 seq_puts(m, "# | | | | |\n");
1769 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1771 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1772 struct trace_array *tr = iter->tr;
1773 struct trace_array_cpu *data = tr->data[tr->cpu];
1774 struct tracer *type = current_trace;
1775 unsigned long entries = 0;
1776 unsigned long total = 0;
1777 unsigned long count;
1778 const char *name = "preemption";
1785 for_each_tracing_cpu(cpu) {
1786 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1788 * If this buffer has skipped entries, then we hold all
1789 * entries for the trace and we need to ignore the
1790 * ones before the time stamp.
1792 if (tr->data[cpu]->skipped_entries) {
1793 count -= tr->data[cpu]->skipped_entries;
1794 /* total is the same as the entries */
1798 ring_buffer_overrun_cpu(tr->buffer, cpu);
1802 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1804 seq_puts(m, "# -----------------------------------"
1805 "---------------------------------\n");
1806 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1807 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1808 nsecs_to_usecs(data->saved_latency),
1812 #if defined(CONFIG_PREEMPT_NONE)
1814 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1816 #elif defined(CONFIG_PREEMPT)
1821 /* These are reserved for later use */
1824 seq_printf(m, " #P:%d)\n", num_online_cpus());
1828 seq_puts(m, "# -----------------\n");
1829 seq_printf(m, "# | task: %.16s-%d "
1830 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1831 data->comm, data->pid, data->uid, data->nice,
1832 data->policy, data->rt_priority);
1833 seq_puts(m, "# -----------------\n");
1835 if (data->critical_start) {
1836 seq_puts(m, "# => started at: ");
1837 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1838 trace_print_seq(m, &iter->seq);
1839 seq_puts(m, "\n# => ended at: ");
1840 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1841 trace_print_seq(m, &iter->seq);
1842 seq_puts(m, "\n#\n");
1848 static void test_cpu_buff_start(struct trace_iterator *iter)
1850 struct trace_seq *s = &iter->seq;
1852 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1855 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1858 if (cpumask_test_cpu(iter->cpu, iter->started))
1861 if (iter->tr->data[iter->cpu]->skipped_entries)
1864 cpumask_set_cpu(iter->cpu, iter->started);
1866 /* Don't print started cpu buffer for the first entry of the trace */
1868 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1872 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1874 struct trace_seq *s = &iter->seq;
1875 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1876 struct trace_entry *entry;
1877 struct trace_event *event;
1881 test_cpu_buff_start(iter);
1883 event = ftrace_find_event(entry->type);
1885 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1886 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1887 if (!trace_print_lat_context(iter))
1890 if (!trace_print_context(iter))
1896 return event->funcs->trace(iter, sym_flags, event);
1898 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1901 return TRACE_TYPE_HANDLED;
1903 return TRACE_TYPE_PARTIAL_LINE;
1906 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1908 struct trace_seq *s = &iter->seq;
1909 struct trace_entry *entry;
1910 struct trace_event *event;
1914 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1915 if (!trace_seq_printf(s, "%d %d %llu ",
1916 entry->pid, iter->cpu, iter->ts))
1920 event = ftrace_find_event(entry->type);
1922 return event->funcs->raw(iter, 0, event);
1924 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1927 return TRACE_TYPE_HANDLED;
1929 return TRACE_TYPE_PARTIAL_LINE;
1932 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1934 struct trace_seq *s = &iter->seq;
1935 unsigned char newline = '\n';
1936 struct trace_entry *entry;
1937 struct trace_event *event;
1941 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1942 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1943 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1944 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1947 event = ftrace_find_event(entry->type);
1949 enum print_line_t ret = event->funcs->hex(iter, 0, event);
1950 if (ret != TRACE_TYPE_HANDLED)
1954 SEQ_PUT_FIELD_RET(s, newline);
1956 return TRACE_TYPE_HANDLED;
1959 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1961 struct trace_seq *s = &iter->seq;
1962 struct trace_entry *entry;
1963 struct trace_event *event;
1967 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1968 SEQ_PUT_FIELD_RET(s, entry->pid);
1969 SEQ_PUT_FIELD_RET(s, iter->cpu);
1970 SEQ_PUT_FIELD_RET(s, iter->ts);
1973 event = ftrace_find_event(entry->type);
1974 return event ? event->funcs->binary(iter, 0, event) :
1978 int trace_empty(struct trace_iterator *iter)
1982 /* If we are looking at one CPU buffer, only check that one */
1983 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1984 cpu = iter->cpu_file;
1985 if (iter->buffer_iter[cpu]) {
1986 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1989 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1995 for_each_tracing_cpu(cpu) {
1996 if (iter->buffer_iter[cpu]) {
1997 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2000 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2008 /* Called with trace_event_read_lock() held. */
2009 enum print_line_t print_trace_line(struct trace_iterator *iter)
2011 enum print_line_t ret;
2013 if (iter->lost_events)
2014 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2015 iter->cpu, iter->lost_events);
2017 if (iter->trace && iter->trace->print_line) {
2018 ret = iter->trace->print_line(iter);
2019 if (ret != TRACE_TYPE_UNHANDLED)
2023 if (iter->ent->type == TRACE_BPRINT &&
2024 trace_flags & TRACE_ITER_PRINTK &&
2025 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2026 return trace_print_bprintk_msg_only(iter);
2028 if (iter->ent->type == TRACE_PRINT &&
2029 trace_flags & TRACE_ITER_PRINTK &&
2030 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2031 return trace_print_printk_msg_only(iter);
2033 if (trace_flags & TRACE_ITER_BIN)
2034 return print_bin_fmt(iter);
2036 if (trace_flags & TRACE_ITER_HEX)
2037 return print_hex_fmt(iter);
2039 if (trace_flags & TRACE_ITER_RAW)
2040 return print_raw_fmt(iter);
2042 return print_trace_fmt(iter);
2045 void trace_default_header(struct seq_file *m)
2047 struct trace_iterator *iter = m->private;
2049 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2050 /* print nothing if the buffers are empty */
2051 if (trace_empty(iter))
2053 print_trace_header(m, iter);
2054 if (!(trace_flags & TRACE_ITER_VERBOSE))
2055 print_lat_help_header(m);
2057 if (!(trace_flags & TRACE_ITER_VERBOSE))
2058 print_func_help_header(m);
2062 static int s_show(struct seq_file *m, void *v)
2064 struct trace_iterator *iter = v;
2067 if (iter->ent == NULL) {
2069 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2072 if (iter->trace && iter->trace->print_header)
2073 iter->trace->print_header(m);
2075 trace_default_header(m);
2077 } else if (iter->leftover) {
2079 * If we filled the seq_file buffer earlier, we
2080 * want to just show it now.
2082 ret = trace_print_seq(m, &iter->seq);
2084 /* ret should this time be zero, but you never know */
2085 iter->leftover = ret;
2088 print_trace_line(iter);
2089 ret = trace_print_seq(m, &iter->seq);
2091 * If we overflow the seq_file buffer, then it will
2092 * ask us for this data again at start up.
2094 * ret is 0 if seq_file write succeeded.
2097 iter->leftover = ret;
2103 static const struct seq_operations tracer_seq_ops = {
2110 static struct trace_iterator *
2111 __tracing_open(struct inode *inode, struct file *file)
2113 long cpu_file = (long) inode->i_private;
2114 void *fail_ret = ERR_PTR(-ENOMEM);
2115 struct trace_iterator *iter;
2119 if (tracing_disabled)
2120 return ERR_PTR(-ENODEV);
2122 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2124 return ERR_PTR(-ENOMEM);
2127 * We make a copy of the current tracer to avoid concurrent
2128 * changes on it while we are reading.
2130 mutex_lock(&trace_types_lock);
2131 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2136 *iter->trace = *current_trace;
2138 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2141 if (current_trace && current_trace->print_max)
2144 iter->tr = &global_trace;
2146 mutex_init(&iter->mutex);
2147 iter->cpu_file = cpu_file;
2149 /* Notify the tracer early; before we stop tracing. */
2150 if (iter->trace && iter->trace->open)
2151 iter->trace->open(iter);
2153 /* Annotate start of buffers if we had overruns */
2154 if (ring_buffer_overruns(iter->tr->buffer))
2155 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2157 /* stop the trace while dumping */
2160 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2161 for_each_tracing_cpu(cpu) {
2162 iter->buffer_iter[cpu] =
2163 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2165 ring_buffer_read_prepare_sync();
2166 for_each_tracing_cpu(cpu) {
2167 ring_buffer_read_start(iter->buffer_iter[cpu]);
2168 tracing_iter_reset(iter, cpu);
2171 cpu = iter->cpu_file;
2172 iter->buffer_iter[cpu] =
2173 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2174 ring_buffer_read_prepare_sync();
2175 ring_buffer_read_start(iter->buffer_iter[cpu]);
2176 tracing_iter_reset(iter, cpu);
2179 ret = seq_open(file, &tracer_seq_ops);
2181 fail_ret = ERR_PTR(ret);
2185 m = file->private_data;
2188 mutex_unlock(&trace_types_lock);
2193 for_each_tracing_cpu(cpu) {
2194 if (iter->buffer_iter[cpu])
2195 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2197 free_cpumask_var(iter->started);
2200 mutex_unlock(&trace_types_lock);
2207 int tracing_open_generic(struct inode *inode, struct file *filp)
2209 if (tracing_disabled)
2212 filp->private_data = inode->i_private;
2216 static int tracing_release(struct inode *inode, struct file *file)
2218 struct seq_file *m = file->private_data;
2219 struct trace_iterator *iter;
2222 if (!(file->f_mode & FMODE_READ))
2227 mutex_lock(&trace_types_lock);
2228 for_each_tracing_cpu(cpu) {
2229 if (iter->buffer_iter[cpu])
2230 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2233 if (iter->trace && iter->trace->close)
2234 iter->trace->close(iter);
2236 /* reenable tracing if it was previously enabled */
2238 mutex_unlock(&trace_types_lock);
2240 seq_release(inode, file);
2241 mutex_destroy(&iter->mutex);
2242 free_cpumask_var(iter->started);
2248 static int tracing_open(struct inode *inode, struct file *file)
2250 struct trace_iterator *iter;
2253 /* If this file was open for write, then erase contents */
2254 if ((file->f_mode & FMODE_WRITE) &&
2255 (file->f_flags & O_TRUNC)) {
2256 long cpu = (long) inode->i_private;
2258 if (cpu == TRACE_PIPE_ALL_CPU)
2259 tracing_reset_online_cpus(&global_trace);
2261 tracing_reset(&global_trace, cpu);
2264 if (file->f_mode & FMODE_READ) {
2265 iter = __tracing_open(inode, file);
2267 ret = PTR_ERR(iter);
2268 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2269 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2275 t_next(struct seq_file *m, void *v, loff_t *pos)
2277 struct tracer *t = v;
2287 static void *t_start(struct seq_file *m, loff_t *pos)
2292 mutex_lock(&trace_types_lock);
2293 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2299 static void t_stop(struct seq_file *m, void *p)
2301 mutex_unlock(&trace_types_lock);
2304 static int t_show(struct seq_file *m, void *v)
2306 struct tracer *t = v;
2311 seq_printf(m, "%s", t->name);
2320 static const struct seq_operations show_traces_seq_ops = {
2327 static int show_traces_open(struct inode *inode, struct file *file)
2329 if (tracing_disabled)
2332 return seq_open(file, &show_traces_seq_ops);
2336 tracing_write_stub(struct file *filp, const char __user *ubuf,
2337 size_t count, loff_t *ppos)
2342 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2344 if (file->f_mode & FMODE_READ)
2345 return seq_lseek(file, offset, origin);
2350 static const struct file_operations tracing_fops = {
2351 .open = tracing_open,
2353 .write = tracing_write_stub,
2354 .llseek = tracing_seek,
2355 .release = tracing_release,
2358 static const struct file_operations show_traces_fops = {
2359 .open = show_traces_open,
2361 .release = seq_release,
2362 .llseek = seq_lseek,
2366 * Only trace on a CPU if the bitmask is set:
2368 static cpumask_var_t tracing_cpumask;
2371 * The tracer itself will not take this lock, but still we want
2372 * to provide a consistent cpumask to user-space:
2374 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2377 * Temporary storage for the character representation of the
2378 * CPU bitmask (and one more byte for the newline):
2380 static char mask_str[NR_CPUS + 1];
2383 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2384 size_t count, loff_t *ppos)
2388 mutex_lock(&tracing_cpumask_update_lock);
2390 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2391 if (count - len < 2) {
2395 len += sprintf(mask_str + len, "\n");
2396 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2399 mutex_unlock(&tracing_cpumask_update_lock);
2405 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2406 size_t count, loff_t *ppos)
2409 cpumask_var_t tracing_cpumask_new;
2411 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2414 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2418 mutex_lock(&tracing_cpumask_update_lock);
2420 local_irq_disable();
2421 arch_spin_lock(&ftrace_max_lock);
2422 for_each_tracing_cpu(cpu) {
2424 * Increase/decrease the disabled counter if we are
2425 * about to flip a bit in the cpumask:
2427 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2428 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2429 atomic_inc(&global_trace.data[cpu]->disabled);
2431 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2432 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2433 atomic_dec(&global_trace.data[cpu]->disabled);
2436 arch_spin_unlock(&ftrace_max_lock);
2439 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2441 mutex_unlock(&tracing_cpumask_update_lock);
2442 free_cpumask_var(tracing_cpumask_new);
2447 free_cpumask_var(tracing_cpumask_new);
2452 static const struct file_operations tracing_cpumask_fops = {
2453 .open = tracing_open_generic,
2454 .read = tracing_cpumask_read,
2455 .write = tracing_cpumask_write,
2456 .llseek = generic_file_llseek,
2459 static int tracing_trace_options_show(struct seq_file *m, void *v)
2461 struct tracer_opt *trace_opts;
2465 mutex_lock(&trace_types_lock);
2466 tracer_flags = current_trace->flags->val;
2467 trace_opts = current_trace->flags->opts;
2469 for (i = 0; trace_options[i]; i++) {
2470 if (trace_flags & (1 << i))
2471 seq_printf(m, "%s\n", trace_options[i]);
2473 seq_printf(m, "no%s\n", trace_options[i]);
2476 for (i = 0; trace_opts[i].name; i++) {
2477 if (tracer_flags & trace_opts[i].bit)
2478 seq_printf(m, "%s\n", trace_opts[i].name);
2480 seq_printf(m, "no%s\n", trace_opts[i].name);
2482 mutex_unlock(&trace_types_lock);
2487 static int __set_tracer_option(struct tracer *trace,
2488 struct tracer_flags *tracer_flags,
2489 struct tracer_opt *opts, int neg)
2493 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2498 tracer_flags->val &= ~opts->bit;
2500 tracer_flags->val |= opts->bit;
2504 /* Try to assign a tracer specific option */
2505 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2507 struct tracer_flags *tracer_flags = trace->flags;
2508 struct tracer_opt *opts = NULL;
2511 for (i = 0; tracer_flags->opts[i].name; i++) {
2512 opts = &tracer_flags->opts[i];
2514 if (strcmp(cmp, opts->name) == 0)
2515 return __set_tracer_option(trace, trace->flags,
2522 static void set_tracer_flags(unsigned int mask, int enabled)
2524 /* do nothing if flag is already set */
2525 if (!!(trace_flags & mask) == !!enabled)
2529 trace_flags |= mask;
2531 trace_flags &= ~mask;
2533 if (mask == TRACE_ITER_RECORD_CMD)
2534 trace_event_enable_cmd_record(enabled);
2538 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2539 size_t cnt, loff_t *ppos)
2547 if (cnt >= sizeof(buf))
2550 if (copy_from_user(&buf, ubuf, cnt))
2554 cmp = strstrip(buf);
2556 if (strncmp(cmp, "no", 2) == 0) {
2561 for (i = 0; trace_options[i]; i++) {
2562 if (strcmp(cmp, trace_options[i]) == 0) {
2563 set_tracer_flags(1 << i, !neg);
2568 /* If no option could be set, test the specific tracer options */
2569 if (!trace_options[i]) {
2570 mutex_lock(&trace_types_lock);
2571 ret = set_tracer_option(current_trace, cmp, neg);
2572 mutex_unlock(&trace_types_lock);
2582 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2584 if (tracing_disabled)
2586 return single_open(file, tracing_trace_options_show, NULL);
2589 static const struct file_operations tracing_iter_fops = {
2590 .open = tracing_trace_options_open,
2592 .llseek = seq_lseek,
2593 .release = single_release,
2594 .write = tracing_trace_options_write,
2597 static const char readme_msg[] =
2598 "tracing mini-HOWTO:\n\n"
2599 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2600 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2601 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2602 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2604 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2605 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2607 "# cat /sys/kernel/debug/tracing/trace_options\n"
2608 "noprint-parent nosym-offset nosym-addr noverbose\n"
2609 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2610 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2611 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2612 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2616 tracing_readme_read(struct file *filp, char __user *ubuf,
2617 size_t cnt, loff_t *ppos)
2619 return simple_read_from_buffer(ubuf, cnt, ppos,
2620 readme_msg, strlen(readme_msg));
2623 static const struct file_operations tracing_readme_fops = {
2624 .open = tracing_open_generic,
2625 .read = tracing_readme_read,
2626 .llseek = generic_file_llseek,
2630 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2631 size_t cnt, loff_t *ppos)
2640 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2644 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2652 for (i = 0; i < SAVED_CMDLINES; i++) {
2655 pid = map_cmdline_to_pid[i];
2656 if (pid == -1 || pid == NO_CMDLINE_MAP)
2659 trace_find_cmdline(pid, buf_comm);
2660 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2665 len = simple_read_from_buffer(ubuf, cnt, ppos,
2674 static const struct file_operations tracing_saved_cmdlines_fops = {
2675 .open = tracing_open_generic,
2676 .read = tracing_saved_cmdlines_read,
2677 .llseek = generic_file_llseek,
2681 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2682 size_t cnt, loff_t *ppos)
2687 r = sprintf(buf, "%u\n", tracer_enabled);
2688 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2692 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2693 size_t cnt, loff_t *ppos)
2695 struct trace_array *tr = filp->private_data;
2700 if (cnt >= sizeof(buf))
2703 if (copy_from_user(&buf, ubuf, cnt))
2708 ret = strict_strtoul(buf, 10, &val);
2714 mutex_lock(&trace_types_lock);
2715 if (tracer_enabled ^ val) {
2718 if (current_trace->start)
2719 current_trace->start(tr);
2724 if (current_trace->stop)
2725 current_trace->stop(tr);
2728 mutex_unlock(&trace_types_lock);
2736 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2737 size_t cnt, loff_t *ppos)
2739 char buf[MAX_TRACER_SIZE+2];
2742 mutex_lock(&trace_types_lock);
2744 r = sprintf(buf, "%s\n", current_trace->name);
2746 r = sprintf(buf, "\n");
2747 mutex_unlock(&trace_types_lock);
2749 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2752 int tracer_init(struct tracer *t, struct trace_array *tr)
2754 tracing_reset_online_cpus(tr);
2758 static int tracing_resize_ring_buffer(unsigned long size)
2763 * If kernel or user changes the size of the ring buffer
2764 * we use the size that was given, and we can forget about
2765 * expanding it later.
2767 ring_buffer_expanded = 1;
2769 ret = ring_buffer_resize(global_trace.buffer, size);
2773 if (!current_trace->use_max_tr)
2776 ret = ring_buffer_resize(max_tr.buffer, size);
2780 r = ring_buffer_resize(global_trace.buffer,
2781 global_trace.entries);
2784 * AARGH! We are left with different
2785 * size max buffer!!!!
2786 * The max buffer is our "snapshot" buffer.
2787 * When a tracer needs a snapshot (one of the
2788 * latency tracers), it swaps the max buffer
2789 * with the saved snap shot. We succeeded to
2790 * update the size of the main buffer, but failed to
2791 * update the size of the max buffer. But when we tried
2792 * to reset the main buffer to the original size, we
2793 * failed there too. This is very unlikely to
2794 * happen, but if it does, warn and kill all
2798 tracing_disabled = 1;
2803 max_tr.entries = size;
2805 global_trace.entries = size;
2812 * tracing_update_buffers - used by tracing facility to expand ring buffers
2814 * To save on memory when the tracing is never used on a system with it
2815 * configured in. The ring buffers are set to a minimum size. But once
2816 * a user starts to use the tracing facility, then they need to grow
2817 * to their default size.
2819 * This function is to be called when a tracer is about to be used.
2821 int tracing_update_buffers(void)
2825 mutex_lock(&trace_types_lock);
2826 if (!ring_buffer_expanded)
2827 ret = tracing_resize_ring_buffer(trace_buf_size);
2828 mutex_unlock(&trace_types_lock);
2833 struct trace_option_dentry;
2835 static struct trace_option_dentry *
2836 create_trace_option_files(struct tracer *tracer);
2839 destroy_trace_option_files(struct trace_option_dentry *topts);
2841 static int tracing_set_tracer(const char *buf)
2843 static struct trace_option_dentry *topts;
2844 struct trace_array *tr = &global_trace;
2848 mutex_lock(&trace_types_lock);
2850 if (!ring_buffer_expanded) {
2851 ret = tracing_resize_ring_buffer(trace_buf_size);
2857 for (t = trace_types; t; t = t->next) {
2858 if (strcmp(t->name, buf) == 0)
2865 if (t == current_trace)
2868 trace_branch_disable();
2869 if (current_trace && current_trace->reset)
2870 current_trace->reset(tr);
2871 if (current_trace && current_trace->use_max_tr) {
2873 * We don't free the ring buffer. instead, resize it because
2874 * The max_tr ring buffer has some state (e.g. ring->clock) and
2875 * we want preserve it.
2877 ring_buffer_resize(max_tr.buffer, 1);
2880 destroy_trace_option_files(topts);
2884 topts = create_trace_option_files(current_trace);
2885 if (current_trace->use_max_tr) {
2886 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
2889 max_tr.entries = global_trace.entries;
2893 ret = tracer_init(t, tr);
2898 trace_branch_enable(tr);
2900 mutex_unlock(&trace_types_lock);
2906 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2907 size_t cnt, loff_t *ppos)
2909 char buf[MAX_TRACER_SIZE+1];
2916 if (cnt > MAX_TRACER_SIZE)
2917 cnt = MAX_TRACER_SIZE;
2919 if (copy_from_user(&buf, ubuf, cnt))
2924 /* strip ending whitespace. */
2925 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2928 err = tracing_set_tracer(buf);
2938 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2939 size_t cnt, loff_t *ppos)
2941 unsigned long *ptr = filp->private_data;
2945 r = snprintf(buf, sizeof(buf), "%ld\n",
2946 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2947 if (r > sizeof(buf))
2949 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2953 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2954 size_t cnt, loff_t *ppos)
2956 unsigned long *ptr = filp->private_data;
2961 if (cnt >= sizeof(buf))
2964 if (copy_from_user(&buf, ubuf, cnt))
2969 ret = strict_strtoul(buf, 10, &val);
2978 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2980 long cpu_file = (long) inode->i_private;
2981 struct trace_iterator *iter;
2984 if (tracing_disabled)
2987 mutex_lock(&trace_types_lock);
2989 /* create a buffer to store the information to pass to userspace */
2990 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2997 * We make a copy of the current tracer to avoid concurrent
2998 * changes on it while we are reading.
3000 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3006 *iter->trace = *current_trace;
3008 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3013 /* trace pipe does not show start of buffer */
3014 cpumask_setall(iter->started);
3016 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3017 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3019 iter->cpu_file = cpu_file;
3020 iter->tr = &global_trace;
3021 mutex_init(&iter->mutex);
3022 filp->private_data = iter;
3024 if (iter->trace->pipe_open)
3025 iter->trace->pipe_open(iter);
3027 nonseekable_open(inode, filp);
3029 mutex_unlock(&trace_types_lock);
3035 mutex_unlock(&trace_types_lock);
3039 static int tracing_release_pipe(struct inode *inode, struct file *file)
3041 struct trace_iterator *iter = file->private_data;
3043 mutex_lock(&trace_types_lock);
3045 if (iter->trace->pipe_close)
3046 iter->trace->pipe_close(iter);
3048 mutex_unlock(&trace_types_lock);
3050 free_cpumask_var(iter->started);
3051 mutex_destroy(&iter->mutex);
3059 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3061 struct trace_iterator *iter = filp->private_data;
3063 if (trace_flags & TRACE_ITER_BLOCK) {
3065 * Always select as readable when in blocking mode
3067 return POLLIN | POLLRDNORM;
3069 if (!trace_empty(iter))
3070 return POLLIN | POLLRDNORM;
3071 poll_wait(filp, &trace_wait, poll_table);
3072 if (!trace_empty(iter))
3073 return POLLIN | POLLRDNORM;
3080 void default_wait_pipe(struct trace_iterator *iter)
3084 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3086 if (trace_empty(iter))
3089 finish_wait(&trace_wait, &wait);
3093 * This is a make-shift waitqueue.
3094 * A tracer might use this callback on some rare cases:
3096 * 1) the current tracer might hold the runqueue lock when it wakes up
3097 * a reader, hence a deadlock (sched, function, and function graph tracers)
3098 * 2) the function tracers, trace all functions, we don't want
3099 * the overhead of calling wake_up and friends
3100 * (and tracing them too)
3102 * Anyway, this is really very primitive wakeup.
3104 void poll_wait_pipe(struct trace_iterator *iter)
3106 set_current_state(TASK_INTERRUPTIBLE);
3107 /* sleep for 100 msecs, and try again. */
3108 schedule_timeout(HZ / 10);
3111 /* Must be called with trace_types_lock mutex held. */
3112 static int tracing_wait_pipe(struct file *filp)
3114 struct trace_iterator *iter = filp->private_data;
3116 while (trace_empty(iter)) {
3118 if ((filp->f_flags & O_NONBLOCK)) {
3122 mutex_unlock(&iter->mutex);
3124 iter->trace->wait_pipe(iter);
3126 mutex_lock(&iter->mutex);
3128 if (signal_pending(current))
3132 * We block until we read something and tracing is disabled.
3133 * We still block if tracing is disabled, but we have never
3134 * read anything. This allows a user to cat this file, and
3135 * then enable tracing. But after we have read something,
3136 * we give an EOF when tracing is again disabled.
3138 * iter->pos will be 0 if we haven't read anything.
3140 if (!tracer_enabled && iter->pos)
3151 tracing_read_pipe(struct file *filp, char __user *ubuf,
3152 size_t cnt, loff_t *ppos)
3154 struct trace_iterator *iter = filp->private_data;
3155 static struct tracer *old_tracer;
3158 /* return any leftover data */
3159 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3163 trace_seq_init(&iter->seq);
3165 /* copy the tracer to avoid using a global lock all around */
3166 mutex_lock(&trace_types_lock);
3167 if (unlikely(old_tracer != current_trace && current_trace)) {
3168 old_tracer = current_trace;
3169 *iter->trace = *current_trace;
3171 mutex_unlock(&trace_types_lock);
3174 * Avoid more than one consumer on a single file descriptor
3175 * This is just a matter of traces coherency, the ring buffer itself
3178 mutex_lock(&iter->mutex);
3179 if (iter->trace->read) {
3180 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3186 sret = tracing_wait_pipe(filp);
3190 /* stop when tracing is finished */
3191 if (trace_empty(iter)) {
3196 if (cnt >= PAGE_SIZE)
3197 cnt = PAGE_SIZE - 1;
3199 /* reset all but tr, trace, and overruns */
3200 memset(&iter->seq, 0,
3201 sizeof(struct trace_iterator) -
3202 offsetof(struct trace_iterator, seq));
3205 trace_event_read_lock();
3206 trace_access_lock(iter->cpu_file);
3207 while (trace_find_next_entry_inc(iter) != NULL) {
3208 enum print_line_t ret;
3209 int len = iter->seq.len;
3211 ret = print_trace_line(iter);
3212 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3213 /* don't print partial lines */
3214 iter->seq.len = len;
3217 if (ret != TRACE_TYPE_NO_CONSUME)
3218 trace_consume(iter);
3220 if (iter->seq.len >= cnt)
3223 trace_access_unlock(iter->cpu_file);
3224 trace_event_read_unlock();
3226 /* Now copy what we have to the user */
3227 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3228 if (iter->seq.readpos >= iter->seq.len)
3229 trace_seq_init(&iter->seq);
3232 * If there was nothing to send to user, inspite of consuming trace
3233 * entries, go back to wait for more entries.
3239 mutex_unlock(&iter->mutex);
3244 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3245 struct pipe_buffer *buf)
3247 __free_page(buf->page);
3250 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3253 __free_page(spd->pages[idx]);
3256 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3258 .map = generic_pipe_buf_map,
3259 .unmap = generic_pipe_buf_unmap,
3260 .confirm = generic_pipe_buf_confirm,
3261 .release = tracing_pipe_buf_release,
3262 .steal = generic_pipe_buf_steal,
3263 .get = generic_pipe_buf_get,
3267 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3272 /* Seq buffer is page-sized, exactly what we need. */
3274 count = iter->seq.len;
3275 ret = print_trace_line(iter);
3276 count = iter->seq.len - count;
3279 iter->seq.len -= count;
3282 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3283 iter->seq.len -= count;
3287 if (ret != TRACE_TYPE_NO_CONSUME)
3288 trace_consume(iter);
3290 if (!trace_find_next_entry_inc(iter)) {
3300 static ssize_t tracing_splice_read_pipe(struct file *filp,
3302 struct pipe_inode_info *pipe,
3306 struct page *pages_def[PIPE_DEF_BUFFERS];
3307 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3308 struct trace_iterator *iter = filp->private_data;
3309 struct splice_pipe_desc spd = {
3311 .partial = partial_def,
3312 .nr_pages = 0, /* This gets updated below. */
3314 .ops = &tracing_pipe_buf_ops,
3315 .spd_release = tracing_spd_release_pipe,
3317 static struct tracer *old_tracer;
3322 if (splice_grow_spd(pipe, &spd))
3325 /* copy the tracer to avoid using a global lock all around */
3326 mutex_lock(&trace_types_lock);
3327 if (unlikely(old_tracer != current_trace && current_trace)) {
3328 old_tracer = current_trace;
3329 *iter->trace = *current_trace;
3331 mutex_unlock(&trace_types_lock);
3333 mutex_lock(&iter->mutex);
3335 if (iter->trace->splice_read) {
3336 ret = iter->trace->splice_read(iter, filp,
3337 ppos, pipe, len, flags);
3342 ret = tracing_wait_pipe(filp);
3346 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3351 trace_event_read_lock();
3352 trace_access_lock(iter->cpu_file);
3354 /* Fill as many pages as possible. */
3355 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3356 spd.pages[i] = alloc_page(GFP_KERNEL);
3360 rem = tracing_fill_pipe_page(rem, iter);
3362 /* Copy the data into the page, so we can start over. */
3363 ret = trace_seq_to_buffer(&iter->seq,
3364 page_address(spd.pages[i]),
3367 __free_page(spd.pages[i]);
3370 spd.partial[i].offset = 0;
3371 spd.partial[i].len = iter->seq.len;
3373 trace_seq_init(&iter->seq);
3376 trace_access_unlock(iter->cpu_file);
3377 trace_event_read_unlock();
3378 mutex_unlock(&iter->mutex);
3382 ret = splice_to_pipe(pipe, &spd);
3384 splice_shrink_spd(pipe, &spd);
3388 mutex_unlock(&iter->mutex);
3393 tracing_entries_read(struct file *filp, char __user *ubuf,
3394 size_t cnt, loff_t *ppos)
3396 struct trace_array *tr = filp->private_data;
3400 mutex_lock(&trace_types_lock);
3401 if (!ring_buffer_expanded)
3402 r = sprintf(buf, "%lu (expanded: %lu)\n",
3404 trace_buf_size >> 10);
3406 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3407 mutex_unlock(&trace_types_lock);
3409 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3413 tracing_entries_write(struct file *filp, const char __user *ubuf,
3414 size_t cnt, loff_t *ppos)
3420 if (cnt >= sizeof(buf))
3423 if (copy_from_user(&buf, ubuf, cnt))
3428 ret = strict_strtoul(buf, 10, &val);
3432 /* must have at least 1 entry */
3436 mutex_lock(&trace_types_lock);
3440 /* disable all cpu buffers */
3441 for_each_tracing_cpu(cpu) {
3442 if (global_trace.data[cpu])
3443 atomic_inc(&global_trace.data[cpu]->disabled);
3444 if (max_tr.data[cpu])
3445 atomic_inc(&max_tr.data[cpu]->disabled);
3448 /* value is in KB */
3451 if (val != global_trace.entries) {
3452 ret = tracing_resize_ring_buffer(val);
3461 /* If check pages failed, return ENOMEM */
3462 if (tracing_disabled)
3465 for_each_tracing_cpu(cpu) {
3466 if (global_trace.data[cpu])
3467 atomic_dec(&global_trace.data[cpu]->disabled);
3468 if (max_tr.data[cpu])
3469 atomic_dec(&max_tr.data[cpu]->disabled);
3473 mutex_unlock(&trace_types_lock);
3478 static int mark_printk(const char *fmt, ...)
3482 va_start(args, fmt);
3483 ret = trace_vprintk(0, fmt, args);
3489 tracing_mark_write(struct file *filp, const char __user *ubuf,
3490 size_t cnt, loff_t *fpos)
3495 if (tracing_disabled)
3498 if (cnt > TRACE_BUF_SIZE)
3499 cnt = TRACE_BUF_SIZE;
3501 buf = kmalloc(cnt + 2, GFP_KERNEL);
3505 if (copy_from_user(buf, ubuf, cnt)) {
3509 if (buf[cnt-1] != '\n') {
3515 written = mark_printk("%s", buf);
3519 /* don't tell userspace we wrote more - it might confuse them */
3526 static int tracing_clock_show(struct seq_file *m, void *v)
3530 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3532 "%s%s%s%s", i ? " " : "",
3533 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3534 i == trace_clock_id ? "]" : "");
3540 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3541 size_t cnt, loff_t *fpos)
3544 const char *clockstr;
3547 if (cnt >= sizeof(buf))
3550 if (copy_from_user(&buf, ubuf, cnt))
3555 clockstr = strstrip(buf);
3557 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3558 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3561 if (i == ARRAY_SIZE(trace_clocks))
3566 mutex_lock(&trace_types_lock);
3568 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3570 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3572 mutex_unlock(&trace_types_lock);
3579 static int tracing_clock_open(struct inode *inode, struct file *file)
3581 if (tracing_disabled)
3583 return single_open(file, tracing_clock_show, NULL);
3586 static const struct file_operations tracing_max_lat_fops = {
3587 .open = tracing_open_generic,
3588 .read = tracing_max_lat_read,
3589 .write = tracing_max_lat_write,
3590 .llseek = generic_file_llseek,
3593 static const struct file_operations tracing_ctrl_fops = {
3594 .open = tracing_open_generic,
3595 .read = tracing_ctrl_read,
3596 .write = tracing_ctrl_write,
3597 .llseek = generic_file_llseek,
3600 static const struct file_operations set_tracer_fops = {
3601 .open = tracing_open_generic,
3602 .read = tracing_set_trace_read,
3603 .write = tracing_set_trace_write,
3604 .llseek = generic_file_llseek,
3607 static const struct file_operations tracing_pipe_fops = {
3608 .open = tracing_open_pipe,
3609 .poll = tracing_poll_pipe,
3610 .read = tracing_read_pipe,
3611 .splice_read = tracing_splice_read_pipe,
3612 .release = tracing_release_pipe,
3613 .llseek = no_llseek,
3616 static const struct file_operations tracing_entries_fops = {
3617 .open = tracing_open_generic,
3618 .read = tracing_entries_read,
3619 .write = tracing_entries_write,
3620 .llseek = generic_file_llseek,
3623 static const struct file_operations tracing_mark_fops = {
3624 .open = tracing_open_generic,
3625 .write = tracing_mark_write,
3626 .llseek = generic_file_llseek,
3629 static const struct file_operations trace_clock_fops = {
3630 .open = tracing_clock_open,
3632 .llseek = seq_lseek,
3633 .release = single_release,
3634 .write = tracing_clock_write,
3637 struct ftrace_buffer_info {
3638 struct trace_array *tr;
3644 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3646 int cpu = (int)(long)inode->i_private;
3647 struct ftrace_buffer_info *info;
3649 if (tracing_disabled)
3652 info = kzalloc(sizeof(*info), GFP_KERNEL);
3656 info->tr = &global_trace;
3659 /* Force reading ring buffer for first read */
3660 info->read = (unsigned int)-1;
3662 filp->private_data = info;
3664 return nonseekable_open(inode, filp);
3668 tracing_buffers_read(struct file *filp, char __user *ubuf,
3669 size_t count, loff_t *ppos)
3671 struct ftrace_buffer_info *info = filp->private_data;
3679 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3683 /* Do we have previous read data to read? */
3684 if (info->read < PAGE_SIZE)
3689 trace_access_lock(info->cpu);
3690 ret = ring_buffer_read_page(info->tr->buffer,
3694 trace_access_unlock(info->cpu);
3699 size = PAGE_SIZE - info->read;
3703 ret = copy_to_user(ubuf, info->spare + info->read, size);
3714 static int tracing_buffers_release(struct inode *inode, struct file *file)
3716 struct ftrace_buffer_info *info = file->private_data;
3719 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3726 struct ring_buffer *buffer;
3731 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3732 struct pipe_buffer *buf)
3734 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3739 ring_buffer_free_read_page(ref->buffer, ref->page);
3744 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3745 struct pipe_buffer *buf)
3750 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3751 struct pipe_buffer *buf)
3753 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3758 /* Pipe buffer operations for a buffer. */
3759 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
3761 .map = generic_pipe_buf_map,
3762 .unmap = generic_pipe_buf_unmap,
3763 .confirm = generic_pipe_buf_confirm,
3764 .release = buffer_pipe_buf_release,
3765 .steal = buffer_pipe_buf_steal,
3766 .get = buffer_pipe_buf_get,
3770 * Callback from splice_to_pipe(), if we need to release some pages
3771 * at the end of the spd in case we error'ed out in filling the pipe.
3773 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3775 struct buffer_ref *ref =
3776 (struct buffer_ref *)spd->partial[i].private;
3781 ring_buffer_free_read_page(ref->buffer, ref->page);
3783 spd->partial[i].private = 0;
3787 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3788 struct pipe_inode_info *pipe, size_t len,
3791 struct ftrace_buffer_info *info = file->private_data;
3792 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3793 struct page *pages_def[PIPE_DEF_BUFFERS];
3794 struct splice_pipe_desc spd = {
3796 .partial = partial_def,
3798 .ops = &buffer_pipe_buf_ops,
3799 .spd_release = buffer_spd_release,
3801 struct buffer_ref *ref;
3802 int entries, size, i;
3805 if (splice_grow_spd(pipe, &spd))
3808 if (*ppos & (PAGE_SIZE - 1)) {
3809 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3814 if (len & (PAGE_SIZE - 1)) {
3815 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3816 if (len < PAGE_SIZE) {
3823 trace_access_lock(info->cpu);
3824 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3826 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
3830 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3835 ref->buffer = info->tr->buffer;
3836 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3842 r = ring_buffer_read_page(ref->buffer, &ref->page,
3845 ring_buffer_free_read_page(ref->buffer,
3852 * zero out any left over data, this is going to
3855 size = ring_buffer_page_len(ref->page);
3856 if (size < PAGE_SIZE)
3857 memset(ref->page + size, 0, PAGE_SIZE - size);
3859 page = virt_to_page(ref->page);
3861 spd.pages[i] = page;
3862 spd.partial[i].len = PAGE_SIZE;
3863 spd.partial[i].offset = 0;
3864 spd.partial[i].private = (unsigned long)ref;
3868 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3871 trace_access_unlock(info->cpu);
3874 /* did we read anything? */
3875 if (!spd.nr_pages) {
3876 if (flags & SPLICE_F_NONBLOCK)
3884 ret = splice_to_pipe(pipe, &spd);
3885 splice_shrink_spd(pipe, &spd);
3890 static const struct file_operations tracing_buffers_fops = {
3891 .open = tracing_buffers_open,
3892 .read = tracing_buffers_read,
3893 .release = tracing_buffers_release,
3894 .splice_read = tracing_buffers_splice_read,
3895 .llseek = no_llseek,
3899 tracing_stats_read(struct file *filp, char __user *ubuf,
3900 size_t count, loff_t *ppos)
3902 unsigned long cpu = (unsigned long)filp->private_data;
3903 struct trace_array *tr = &global_trace;
3904 struct trace_seq *s;
3907 s = kmalloc(sizeof(*s), GFP_KERNEL);
3913 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3914 trace_seq_printf(s, "entries: %ld\n", cnt);
3916 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3917 trace_seq_printf(s, "overrun: %ld\n", cnt);
3919 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3920 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3922 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3929 static const struct file_operations tracing_stats_fops = {
3930 .open = tracing_open_generic,
3931 .read = tracing_stats_read,
3932 .llseek = generic_file_llseek,
3935 #ifdef CONFIG_DYNAMIC_FTRACE
3937 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3943 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3944 size_t cnt, loff_t *ppos)
3946 static char ftrace_dyn_info_buffer[1024];
3947 static DEFINE_MUTEX(dyn_info_mutex);
3948 unsigned long *p = filp->private_data;
3949 char *buf = ftrace_dyn_info_buffer;
3950 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3953 mutex_lock(&dyn_info_mutex);
3954 r = sprintf(buf, "%ld ", *p);
3956 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3959 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3961 mutex_unlock(&dyn_info_mutex);
3966 static const struct file_operations tracing_dyn_info_fops = {
3967 .open = tracing_open_generic,
3968 .read = tracing_read_dyn_info,
3969 .llseek = generic_file_llseek,
3973 static struct dentry *d_tracer;
3975 struct dentry *tracing_init_dentry(void)
3982 if (!debugfs_initialized())
3985 d_tracer = debugfs_create_dir("tracing", NULL);
3987 if (!d_tracer && !once) {
3989 pr_warning("Could not create debugfs directory 'tracing'\n");
3996 static struct dentry *d_percpu;
3998 struct dentry *tracing_dentry_percpu(void)
4001 struct dentry *d_tracer;
4006 d_tracer = tracing_init_dentry();
4011 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4013 if (!d_percpu && !once) {
4015 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4022 static void tracing_init_debugfs_percpu(long cpu)
4024 struct dentry *d_percpu = tracing_dentry_percpu();
4025 struct dentry *d_cpu;
4026 char cpu_dir[30]; /* 30 characters should be more than enough */
4028 snprintf(cpu_dir, 30, "cpu%ld", cpu);
4029 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4031 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4035 /* per cpu trace_pipe */
4036 trace_create_file("trace_pipe", 0444, d_cpu,
4037 (void *) cpu, &tracing_pipe_fops);
4040 trace_create_file("trace", 0644, d_cpu,
4041 (void *) cpu, &tracing_fops);
4043 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4044 (void *) cpu, &tracing_buffers_fops);
4046 trace_create_file("stats", 0444, d_cpu,
4047 (void *) cpu, &tracing_stats_fops);
4050 #ifdef CONFIG_FTRACE_SELFTEST
4051 /* Let selftest have access to static functions in this file */
4052 #include "trace_selftest.c"
4055 struct trace_option_dentry {
4056 struct tracer_opt *opt;
4057 struct tracer_flags *flags;
4058 struct dentry *entry;
4062 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4065 struct trace_option_dentry *topt = filp->private_data;
4068 if (topt->flags->val & topt->opt->bit)
4073 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4077 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4080 struct trace_option_dentry *topt = filp->private_data;
4085 if (cnt >= sizeof(buf))
4088 if (copy_from_user(&buf, ubuf, cnt))
4093 ret = strict_strtoul(buf, 10, &val);
4097 if (val != 0 && val != 1)
4100 if (!!(topt->flags->val & topt->opt->bit) != val) {
4101 mutex_lock(&trace_types_lock);
4102 ret = __set_tracer_option(current_trace, topt->flags,
4104 mutex_unlock(&trace_types_lock);
4115 static const struct file_operations trace_options_fops = {
4116 .open = tracing_open_generic,
4117 .read = trace_options_read,
4118 .write = trace_options_write,
4119 .llseek = generic_file_llseek,
4123 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4126 long index = (long)filp->private_data;
4129 if (trace_flags & (1 << index))
4134 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4138 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4141 long index = (long)filp->private_data;
4146 if (cnt >= sizeof(buf))
4149 if (copy_from_user(&buf, ubuf, cnt))
4154 ret = strict_strtoul(buf, 10, &val);
4158 if (val != 0 && val != 1)
4160 set_tracer_flags(1 << index, val);
4167 static const struct file_operations trace_options_core_fops = {
4168 .open = tracing_open_generic,
4169 .read = trace_options_core_read,
4170 .write = trace_options_core_write,
4171 .llseek = generic_file_llseek,
4174 struct dentry *trace_create_file(const char *name,
4176 struct dentry *parent,
4178 const struct file_operations *fops)
4182 ret = debugfs_create_file(name, mode, parent, data, fops);
4184 pr_warning("Could not create debugfs '%s' entry\n", name);
4190 static struct dentry *trace_options_init_dentry(void)
4192 struct dentry *d_tracer;
4193 static struct dentry *t_options;
4198 d_tracer = tracing_init_dentry();
4202 t_options = debugfs_create_dir("options", d_tracer);
4204 pr_warning("Could not create debugfs directory 'options'\n");
4212 create_trace_option_file(struct trace_option_dentry *topt,
4213 struct tracer_flags *flags,
4214 struct tracer_opt *opt)
4216 struct dentry *t_options;
4218 t_options = trace_options_init_dentry();
4222 topt->flags = flags;
4225 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4226 &trace_options_fops);
4230 static struct trace_option_dentry *
4231 create_trace_option_files(struct tracer *tracer)
4233 struct trace_option_dentry *topts;
4234 struct tracer_flags *flags;
4235 struct tracer_opt *opts;
4241 flags = tracer->flags;
4243 if (!flags || !flags->opts)
4248 for (cnt = 0; opts[cnt].name; cnt++)
4251 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4255 for (cnt = 0; opts[cnt].name; cnt++)
4256 create_trace_option_file(&topts[cnt], flags,
4263 destroy_trace_option_files(struct trace_option_dentry *topts)
4270 for (cnt = 0; topts[cnt].opt; cnt++) {
4271 if (topts[cnt].entry)
4272 debugfs_remove(topts[cnt].entry);
4278 static struct dentry *
4279 create_trace_option_core_file(const char *option, long index)
4281 struct dentry *t_options;
4283 t_options = trace_options_init_dentry();
4287 return trace_create_file(option, 0644, t_options, (void *)index,
4288 &trace_options_core_fops);
4291 static __init void create_trace_options_dir(void)
4293 struct dentry *t_options;
4296 t_options = trace_options_init_dentry();
4300 for (i = 0; trace_options[i]; i++)
4301 create_trace_option_core_file(trace_options[i], i);
4304 static __init int tracer_init_debugfs(void)
4306 struct dentry *d_tracer;
4309 trace_access_lock_init();
4311 d_tracer = tracing_init_dentry();
4313 trace_create_file("tracing_enabled", 0644, d_tracer,
4314 &global_trace, &tracing_ctrl_fops);
4316 trace_create_file("trace_options", 0644, d_tracer,
4317 NULL, &tracing_iter_fops);
4319 trace_create_file("tracing_cpumask", 0644, d_tracer,
4320 NULL, &tracing_cpumask_fops);
4322 trace_create_file("trace", 0644, d_tracer,
4323 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4325 trace_create_file("available_tracers", 0444, d_tracer,
4326 &global_trace, &show_traces_fops);
4328 trace_create_file("current_tracer", 0644, d_tracer,
4329 &global_trace, &set_tracer_fops);
4331 #ifdef CONFIG_TRACER_MAX_TRACE
4332 trace_create_file("tracing_max_latency", 0644, d_tracer,
4333 &tracing_max_latency, &tracing_max_lat_fops);
4336 trace_create_file("tracing_thresh", 0644, d_tracer,
4337 &tracing_thresh, &tracing_max_lat_fops);
4339 trace_create_file("README", 0444, d_tracer,
4340 NULL, &tracing_readme_fops);
4342 trace_create_file("trace_pipe", 0444, d_tracer,
4343 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4345 trace_create_file("buffer_size_kb", 0644, d_tracer,
4346 &global_trace, &tracing_entries_fops);
4348 trace_create_file("trace_marker", 0220, d_tracer,
4349 NULL, &tracing_mark_fops);
4351 trace_create_file("saved_cmdlines", 0444, d_tracer,
4352 NULL, &tracing_saved_cmdlines_fops);
4354 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4357 #ifdef CONFIG_DYNAMIC_FTRACE
4358 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4359 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4362 create_trace_options_dir();
4364 for_each_tracing_cpu(cpu)
4365 tracing_init_debugfs_percpu(cpu);
4370 static int trace_panic_handler(struct notifier_block *this,
4371 unsigned long event, void *unused)
4373 if (ftrace_dump_on_oops)
4374 ftrace_dump(ftrace_dump_on_oops);
4378 static struct notifier_block trace_panic_notifier = {
4379 .notifier_call = trace_panic_handler,
4381 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4384 static int trace_die_handler(struct notifier_block *self,
4390 if (ftrace_dump_on_oops)
4391 ftrace_dump(ftrace_dump_on_oops);
4399 static struct notifier_block trace_die_notifier = {
4400 .notifier_call = trace_die_handler,
4405 * printk is set to max of 1024, we really don't need it that big.
4406 * Nothing should be printing 1000 characters anyway.
4408 #define TRACE_MAX_PRINT 1000
4411 * Define here KERN_TRACE so that we have one place to modify
4412 * it if we decide to change what log level the ftrace dump
4415 #define KERN_TRACE KERN_EMERG
4418 trace_printk_seq(struct trace_seq *s)
4420 /* Probably should print a warning here. */
4424 /* should be zero ended, but we are paranoid. */
4425 s->buffer[s->len] = 0;
4427 printk(KERN_TRACE "%s", s->buffer);
4432 void trace_init_global_iter(struct trace_iterator *iter)
4434 iter->tr = &global_trace;
4435 iter->trace = current_trace;
4436 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4440 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
4442 static arch_spinlock_t ftrace_dump_lock =
4443 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
4444 /* use static because iter can be a bit big for the stack */
4445 static struct trace_iterator iter;
4446 unsigned int old_userobj;
4447 static int dump_ran;
4448 unsigned long flags;
4452 local_irq_save(flags);
4453 arch_spin_lock(&ftrace_dump_lock);
4461 if (disable_tracing)
4464 trace_init_global_iter(&iter);
4466 for_each_tracing_cpu(cpu) {
4467 atomic_inc(&iter.tr->data[cpu]->disabled);
4470 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4472 /* don't look at user memory in panic mode */
4473 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4475 /* Simulate the iterator */
4476 iter.tr = &global_trace;
4477 iter.trace = current_trace;
4479 switch (oops_dump_mode) {
4481 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4484 iter.cpu_file = raw_smp_processor_id();
4489 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4490 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4493 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4496 * We need to stop all tracing on all CPUS to read the
4497 * the next buffer. This is a bit expensive, but is
4498 * not done often. We fill all what we can read,
4499 * and then release the locks again.
4502 while (!trace_empty(&iter)) {
4505 printk(KERN_TRACE "---------------------------------\n");
4509 /* reset all but tr, trace, and overruns */
4510 memset(&iter.seq, 0,
4511 sizeof(struct trace_iterator) -
4512 offsetof(struct trace_iterator, seq));
4513 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4516 if (trace_find_next_entry_inc(&iter) != NULL) {
4519 ret = print_trace_line(&iter);
4520 if (ret != TRACE_TYPE_NO_CONSUME)
4521 trace_consume(&iter);
4524 trace_printk_seq(&iter.seq);
4528 printk(KERN_TRACE " (ftrace buffer empty)\n");
4530 printk(KERN_TRACE "---------------------------------\n");
4533 /* Re-enable tracing if requested */
4534 if (!disable_tracing) {
4535 trace_flags |= old_userobj;
4537 for_each_tracing_cpu(cpu) {
4538 atomic_dec(&iter.tr->data[cpu]->disabled);
4544 arch_spin_unlock(&ftrace_dump_lock);
4545 local_irq_restore(flags);
4548 /* By default: disable tracing after the dump */
4549 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
4551 __ftrace_dump(true, oops_dump_mode);
4554 __init static int tracer_alloc_buffers(void)
4560 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4563 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4564 goto out_free_buffer_mask;
4566 /* To save memory, keep the ring buffer size to its minimum */
4567 if (ring_buffer_expanded)
4568 ring_buf_size = trace_buf_size;
4572 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4573 cpumask_copy(tracing_cpumask, cpu_all_mask);
4575 /* TODO: make the number of buffers hot pluggable with CPUS */
4576 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4577 TRACE_BUFFER_FLAGS);
4578 if (!global_trace.buffer) {
4579 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4581 goto out_free_cpumask;
4583 global_trace.entries = ring_buffer_size(global_trace.buffer);
4586 #ifdef CONFIG_TRACER_MAX_TRACE
4587 max_tr.buffer = ring_buffer_alloc(1, TRACE_BUFFER_FLAGS);
4588 if (!max_tr.buffer) {
4589 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4591 ring_buffer_free(global_trace.buffer);
4592 goto out_free_cpumask;
4597 /* Allocate the first page for all buffers */
4598 for_each_tracing_cpu(i) {
4599 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4600 max_tr.data[i] = &per_cpu(max_tr_data, i);
4603 trace_init_cmdlines();
4605 register_tracer(&nop_trace);
4606 current_trace = &nop_trace;
4607 /* All seems OK, enable tracing */
4608 tracing_disabled = 0;
4610 atomic_notifier_chain_register(&panic_notifier_list,
4611 &trace_panic_notifier);
4613 register_die_notifier(&trace_die_notifier);
4618 free_cpumask_var(tracing_cpumask);
4619 out_free_buffer_mask:
4620 free_cpumask_var(tracing_buffer_mask);
4625 __init static int clear_boot_tracer(void)
4628 * The default tracer at boot buffer is an init section.
4629 * This function is called in lateinit. If we did not
4630 * find the boot tracer, then clear it out, to prevent
4631 * later registration from accessing the buffer that is
4632 * about to be freed.
4634 if (!default_bootup_tracer)
4637 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4638 default_bootup_tracer);
4639 default_bootup_tracer = NULL;
4644 early_initcall(tracer_alloc_buffers);
4645 fs_initcall(tracer_init_debugfs);
4646 late_initcall(clear_boot_tracer);