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/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/notifier.h>
18 #include <linux/debugfs.h>
19 #include <linux/pagemap.h>
20 #include <linux/hardirq.h>
21 #include <linux/linkage.h>
22 #include <linux/uaccess.h>
23 #include <linux/ftrace.h>
24 #include <linux/module.h>
25 #include <linux/percpu.h>
26 #include <linux/kdebug.h>
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/poll.h>
30 #include <linux/gfp.h>
32 #include <linux/kprobes.h>
33 #include <linux/seq_file.h>
34 #include <linux/writeback.h>
36 #include <linux/stacktrace.h>
37 #include <linux/ring_buffer.h>
38 #include <linux/irqflags.h>
41 #include "trace_output.h"
43 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
45 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
46 unsigned long __read_mostly tracing_thresh;
49 * We need to change this state when a selftest is running.
50 * A selftest will lurk into the ring-buffer to count the
51 * entries inserted during the selftest although some concurrent
52 * insertions into the ring-buffer such as ftrace_printk could occurred
53 * at the same time, giving false positive or negative results.
55 static bool __read_mostly tracing_selftest_running;
57 /* For tracers that don't implement custom flags */
58 static struct tracer_opt dummy_tracer_opt[] = {
62 static struct tracer_flags dummy_tracer_flags = {
64 .opts = dummy_tracer_opt
67 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
73 * Kill all tracing for good (never come back).
74 * It is initialized to 1 but will turn to zero if the initialization
75 * of the tracer is successful. But that is the only place that sets
78 int tracing_disabled = 1;
80 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
82 static inline void ftrace_disable_cpu(void)
85 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
88 static inline void ftrace_enable_cpu(void)
90 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
94 static cpumask_t __read_mostly tracing_buffer_mask;
96 #define for_each_tracing_cpu(cpu) \
97 for_each_cpu_mask(cpu, tracing_buffer_mask)
100 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
102 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
103 * is set, then ftrace_dump is called. This will output the contents
104 * of the ftrace buffers to the console. This is very useful for
105 * capturing traces that lead to crashes and outputing it to a
108 * It is default off, but you can enable it with either specifying
109 * "ftrace_dump_on_oops" in the kernel command line, or setting
110 * /proc/sys/kernel/ftrace_dump_on_oops to true.
112 int ftrace_dump_on_oops;
114 static int tracing_set_tracer(char *buf);
116 static int __init set_ftrace(char *str)
118 tracing_set_tracer(str);
121 __setup("ftrace", set_ftrace);
123 static int __init set_ftrace_dump_on_oops(char *str)
125 ftrace_dump_on_oops = 1;
128 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
131 ns2usecs(cycle_t nsec)
138 cycle_t ftrace_now(int cpu)
140 u64 ts = ring_buffer_time_stamp(cpu);
141 ring_buffer_normalize_time_stamp(cpu, &ts);
146 * The global_trace is the descriptor that holds the tracing
147 * buffers for the live tracing. For each CPU, it contains
148 * a link list of pages that will store trace entries. The
149 * page descriptor of the pages in the memory is used to hold
150 * the link list by linking the lru item in the page descriptor
151 * to each of the pages in the buffer per CPU.
153 * For each active CPU there is a data field that holds the
154 * pages for the buffer for that CPU. Each CPU has the same number
155 * of pages allocated for its buffer.
157 static struct trace_array global_trace;
159 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
162 * The max_tr is used to snapshot the global_trace when a maximum
163 * latency is reached. Some tracers will use this to store a maximum
164 * trace while it continues examining live traces.
166 * The buffers for the max_tr are set up the same as the global_trace.
167 * When a snapshot is taken, the link list of the max_tr is swapped
168 * with the link list of the global_trace and the buffers are reset for
169 * the global_trace so the tracing can continue.
171 static struct trace_array max_tr;
173 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
175 /* tracer_enabled is used to toggle activation of a tracer */
176 static int tracer_enabled = 1;
179 * tracing_is_enabled - return tracer_enabled status
181 * This function is used by other tracers to know the status
182 * of the tracer_enabled flag. Tracers may use this function
183 * to know if it should enable their features when starting
184 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
186 int tracing_is_enabled(void)
188 return tracer_enabled;
191 /* function tracing enabled */
192 int ftrace_function_enabled;
195 * trace_buf_size is the size in bytes that is allocated
196 * for a buffer. Note, the number of bytes is always rounded
199 * This number is purposely set to a low number of 16384.
200 * If the dump on oops happens, it will be much appreciated
201 * to not have to wait for all that output. Anyway this can be
202 * boot time and run time configurable.
204 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
206 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
208 /* trace_types holds a link list of available tracers. */
209 static struct tracer *trace_types __read_mostly;
211 /* current_trace points to the tracer that is currently active */
212 static struct tracer *current_trace __read_mostly;
215 * max_tracer_type_len is used to simplify the allocating of
216 * buffers to read userspace tracer names. We keep track of
217 * the longest tracer name registered.
219 static int max_tracer_type_len;
222 * trace_types_lock is used to protect the trace_types list.
223 * This lock is also used to keep user access serialized.
224 * Accesses from userspace will grab this lock while userspace
225 * activities happen inside the kernel.
227 static DEFINE_MUTEX(trace_types_lock);
229 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
230 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
232 /* trace_flags holds trace_options default values */
233 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
237 * trace_wake_up - wake up tasks waiting for trace input
239 * Simply wakes up any task that is blocked on the trace_wait
240 * queue. These is used with trace_poll for tasks polling the trace.
242 void trace_wake_up(void)
245 * The runqueue_is_locked() can fail, but this is the best we
248 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
249 wake_up(&trace_wait);
252 static int __init set_buf_size(char *str)
254 unsigned long buf_size;
259 ret = strict_strtoul(str, 0, &buf_size);
260 /* nr_entries can not be zero */
261 if (ret < 0 || buf_size == 0)
263 trace_buf_size = buf_size;
266 __setup("trace_buf_size=", set_buf_size);
268 unsigned long nsecs_to_usecs(unsigned long nsecs)
273 /* These must match the bit postions in trace_iterator_flags */
274 static const char *trace_options[] = {
296 * ftrace_max_lock is used to protect the swapping of buffers
297 * when taking a max snapshot. The buffers themselves are
298 * protected by per_cpu spinlocks. But the action of the swap
299 * needs its own lock.
301 * This is defined as a raw_spinlock_t in order to help
302 * with performance when lockdep debugging is enabled.
304 static raw_spinlock_t ftrace_max_lock =
305 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
308 * Copy the new maximum trace into the separate maximum-trace
309 * structure. (this way the maximum trace is permanently saved,
310 * for later retrieval via /debugfs/tracing/latency_trace)
313 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
315 struct trace_array_cpu *data = tr->data[cpu];
318 max_tr.time_start = data->preempt_timestamp;
320 data = max_tr.data[cpu];
321 data->saved_latency = tracing_max_latency;
323 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
324 data->pid = tsk->pid;
325 data->uid = task_uid(tsk);
326 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
327 data->policy = tsk->policy;
328 data->rt_priority = tsk->rt_priority;
330 /* record this tasks comm */
331 tracing_record_cmdline(current);
335 trace_seq_reset(struct trace_seq *s)
341 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
346 if (s->len <= s->readpos)
349 len = s->len - s->readpos;
352 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
361 trace_print_seq(struct seq_file *m, struct trace_seq *s)
363 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
366 seq_puts(m, s->buffer);
372 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
374 * @tsk: the task with the latency
375 * @cpu: The cpu that initiated the trace.
377 * Flip the buffers between the @tr and the max_tr and record information
378 * about which task was the cause of this latency.
381 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
383 struct ring_buffer *buf = tr->buffer;
385 WARN_ON_ONCE(!irqs_disabled());
386 __raw_spin_lock(&ftrace_max_lock);
388 tr->buffer = max_tr.buffer;
391 ftrace_disable_cpu();
392 ring_buffer_reset(tr->buffer);
395 __update_max_tr(tr, tsk, cpu);
396 __raw_spin_unlock(&ftrace_max_lock);
400 * update_max_tr_single - only copy one trace over, and reset the rest
402 * @tsk - task with the latency
403 * @cpu - the cpu of the buffer to copy.
405 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
408 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
412 WARN_ON_ONCE(!irqs_disabled());
413 __raw_spin_lock(&ftrace_max_lock);
415 ftrace_disable_cpu();
417 ring_buffer_reset(max_tr.buffer);
418 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
424 __update_max_tr(tr, tsk, cpu);
425 __raw_spin_unlock(&ftrace_max_lock);
429 * register_tracer - register a tracer with the ftrace system.
430 * @type - the plugin for the tracer
432 * Register a new plugin tracer.
434 int register_tracer(struct tracer *type)
441 pr_info("Tracer must have a name\n");
446 * When this gets called we hold the BKL which means that
447 * preemption is disabled. Various trace selftests however
448 * need to disable and enable preemption for successful tests.
449 * So we drop the BKL here and grab it after the tests again.
452 mutex_lock(&trace_types_lock);
454 tracing_selftest_running = true;
456 for (t = trace_types; t; t = t->next) {
457 if (strcmp(type->name, t->name) == 0) {
459 pr_info("Trace %s already registered\n",
467 type->set_flag = &dummy_set_flag;
469 type->flags = &dummy_tracer_flags;
471 if (!type->flags->opts)
472 type->flags->opts = dummy_tracer_opt;
474 #ifdef CONFIG_FTRACE_STARTUP_TEST
475 if (type->selftest) {
476 struct tracer *saved_tracer = current_trace;
477 struct trace_array *tr = &global_trace;
481 * Run a selftest on this tracer.
482 * Here we reset the trace buffer, and set the current
483 * tracer to be this tracer. The tracer can then run some
484 * internal tracing to verify that everything is in order.
485 * If we fail, we do not register this tracer.
487 for_each_tracing_cpu(i)
488 tracing_reset(tr, i);
490 current_trace = type;
491 /* the test is responsible for initializing and enabling */
492 pr_info("Testing tracer %s: ", type->name);
493 ret = type->selftest(type, tr);
494 /* the test is responsible for resetting too */
495 current_trace = saved_tracer;
497 printk(KERN_CONT "FAILED!\n");
500 /* Only reset on passing, to avoid touching corrupted buffers */
501 for_each_tracing_cpu(i)
502 tracing_reset(tr, i);
504 printk(KERN_CONT "PASSED\n");
508 type->next = trace_types;
510 len = strlen(type->name);
511 if (len > max_tracer_type_len)
512 max_tracer_type_len = len;
515 tracing_selftest_running = false;
516 mutex_unlock(&trace_types_lock);
522 void unregister_tracer(struct tracer *type)
527 mutex_lock(&trace_types_lock);
528 for (t = &trace_types; *t; t = &(*t)->next) {
532 pr_info("Trace %s not registered\n", type->name);
537 if (strlen(type->name) != max_tracer_type_len)
540 max_tracer_type_len = 0;
541 for (t = &trace_types; *t; t = &(*t)->next) {
542 len = strlen((*t)->name);
543 if (len > max_tracer_type_len)
544 max_tracer_type_len = len;
547 mutex_unlock(&trace_types_lock);
550 void tracing_reset(struct trace_array *tr, int cpu)
552 ftrace_disable_cpu();
553 ring_buffer_reset_cpu(tr->buffer, cpu);
557 void tracing_reset_online_cpus(struct trace_array *tr)
561 tr->time_start = ftrace_now(tr->cpu);
563 for_each_online_cpu(cpu)
564 tracing_reset(tr, cpu);
567 #define SAVED_CMDLINES 128
568 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
569 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
570 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
571 static int cmdline_idx;
572 static DEFINE_SPINLOCK(trace_cmdline_lock);
574 /* temporary disable recording */
575 atomic_t trace_record_cmdline_disabled __read_mostly;
577 static void trace_init_cmdlines(void)
579 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
580 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
584 static int trace_stop_count;
585 static DEFINE_SPINLOCK(tracing_start_lock);
588 * ftrace_off_permanent - disable all ftrace code permanently
590 * This should only be called when a serious anomally has
591 * been detected. This will turn off the function tracing,
592 * ring buffers, and other tracing utilites. It takes no
593 * locks and can be called from any context.
595 void ftrace_off_permanent(void)
597 tracing_disabled = 1;
599 tracing_off_permanent();
603 * tracing_start - quick start of the tracer
605 * If tracing is enabled but was stopped by tracing_stop,
606 * this will start the tracer back up.
608 void tracing_start(void)
610 struct ring_buffer *buffer;
613 if (tracing_disabled)
616 spin_lock_irqsave(&tracing_start_lock, flags);
617 if (--trace_stop_count)
620 if (trace_stop_count < 0) {
621 /* Someone screwed up their debugging */
623 trace_stop_count = 0;
628 buffer = global_trace.buffer;
630 ring_buffer_record_enable(buffer);
632 buffer = max_tr.buffer;
634 ring_buffer_record_enable(buffer);
638 spin_unlock_irqrestore(&tracing_start_lock, flags);
642 * tracing_stop - quick stop of the tracer
644 * Light weight way to stop tracing. Use in conjunction with
647 void tracing_stop(void)
649 struct ring_buffer *buffer;
653 spin_lock_irqsave(&tracing_start_lock, flags);
654 if (trace_stop_count++)
657 buffer = global_trace.buffer;
659 ring_buffer_record_disable(buffer);
661 buffer = max_tr.buffer;
663 ring_buffer_record_disable(buffer);
666 spin_unlock_irqrestore(&tracing_start_lock, flags);
669 void trace_stop_cmdline_recording(void);
671 static void trace_save_cmdline(struct task_struct *tsk)
676 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
680 * It's not the end of the world if we don't get
681 * the lock, but we also don't want to spin
682 * nor do we want to disable interrupts,
683 * so if we miss here, then better luck next time.
685 if (!spin_trylock(&trace_cmdline_lock))
688 idx = map_pid_to_cmdline[tsk->pid];
689 if (idx >= SAVED_CMDLINES) {
690 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
692 map = map_cmdline_to_pid[idx];
693 if (map <= PID_MAX_DEFAULT)
694 map_pid_to_cmdline[map] = (unsigned)-1;
696 map_pid_to_cmdline[tsk->pid] = idx;
701 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
703 spin_unlock(&trace_cmdline_lock);
706 char *trace_find_cmdline(int pid)
708 char *cmdline = "<...>";
714 if (pid > PID_MAX_DEFAULT)
717 map = map_pid_to_cmdline[pid];
718 if (map >= SAVED_CMDLINES)
721 cmdline = saved_cmdlines[map];
727 void tracing_record_cmdline(struct task_struct *tsk)
729 if (atomic_read(&trace_record_cmdline_disabled))
732 trace_save_cmdline(tsk);
736 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
739 struct task_struct *tsk = current;
741 entry->preempt_count = pc & 0xff;
742 entry->pid = (tsk) ? tsk->pid : 0;
743 entry->tgid = (tsk) ? tsk->tgid : 0;
745 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
746 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
748 TRACE_FLAG_IRQS_NOSUPPORT |
750 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
751 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
752 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
756 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
757 unsigned long ip, unsigned long parent_ip, unsigned long flags,
760 struct ring_buffer_event *event;
761 struct ftrace_entry *entry;
762 unsigned long irq_flags;
764 /* If we are reading the ring buffer, don't trace */
765 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
768 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
772 entry = ring_buffer_event_data(event);
773 tracing_generic_entry_update(&entry->ent, flags, pc);
774 entry->ent.type = TRACE_FN;
776 entry->parent_ip = parent_ip;
777 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
780 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
781 static void __trace_graph_entry(struct trace_array *tr,
782 struct trace_array_cpu *data,
783 struct ftrace_graph_ent *trace,
787 struct ring_buffer_event *event;
788 struct ftrace_graph_ent_entry *entry;
789 unsigned long irq_flags;
791 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
794 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
798 entry = ring_buffer_event_data(event);
799 tracing_generic_entry_update(&entry->ent, flags, pc);
800 entry->ent.type = TRACE_GRAPH_ENT;
801 entry->graph_ent = *trace;
802 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
805 static void __trace_graph_return(struct trace_array *tr,
806 struct trace_array_cpu *data,
807 struct ftrace_graph_ret *trace,
811 struct ring_buffer_event *event;
812 struct ftrace_graph_ret_entry *entry;
813 unsigned long irq_flags;
815 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
818 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
822 entry = ring_buffer_event_data(event);
823 tracing_generic_entry_update(&entry->ent, flags, pc);
824 entry->ent.type = TRACE_GRAPH_RET;
826 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
831 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
832 unsigned long ip, unsigned long parent_ip, unsigned long flags,
835 if (likely(!atomic_read(&data->disabled)))
836 trace_function(tr, data, ip, parent_ip, flags, pc);
839 static void ftrace_trace_stack(struct trace_array *tr,
840 struct trace_array_cpu *data,
844 #ifdef CONFIG_STACKTRACE
845 struct ring_buffer_event *event;
846 struct stack_entry *entry;
847 struct stack_trace trace;
848 unsigned long irq_flags;
850 if (!(trace_flags & TRACE_ITER_STACKTRACE))
853 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
857 entry = ring_buffer_event_data(event);
858 tracing_generic_entry_update(&entry->ent, flags, pc);
859 entry->ent.type = TRACE_STACK;
861 memset(&entry->caller, 0, sizeof(entry->caller));
863 trace.nr_entries = 0;
864 trace.max_entries = FTRACE_STACK_ENTRIES;
866 trace.entries = entry->caller;
868 save_stack_trace(&trace);
869 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
873 void __trace_stack(struct trace_array *tr,
874 struct trace_array_cpu *data,
878 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
881 static void ftrace_trace_userstack(struct trace_array *tr,
882 struct trace_array_cpu *data,
883 unsigned long flags, int pc)
885 #ifdef CONFIG_STACKTRACE
886 struct ring_buffer_event *event;
887 struct userstack_entry *entry;
888 struct stack_trace trace;
889 unsigned long irq_flags;
891 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
894 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
898 entry = ring_buffer_event_data(event);
899 tracing_generic_entry_update(&entry->ent, flags, pc);
900 entry->ent.type = TRACE_USER_STACK;
902 memset(&entry->caller, 0, sizeof(entry->caller));
904 trace.nr_entries = 0;
905 trace.max_entries = FTRACE_STACK_ENTRIES;
907 trace.entries = entry->caller;
909 save_stack_trace_user(&trace);
910 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
914 void __trace_userstack(struct trace_array *tr,
915 struct trace_array_cpu *data,
918 ftrace_trace_userstack(tr, data, flags, preempt_count());
922 ftrace_trace_special(void *__tr, void *__data,
923 unsigned long arg1, unsigned long arg2, unsigned long arg3,
926 struct ring_buffer_event *event;
927 struct trace_array_cpu *data = __data;
928 struct trace_array *tr = __tr;
929 struct special_entry *entry;
930 unsigned long irq_flags;
932 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
936 entry = ring_buffer_event_data(event);
937 tracing_generic_entry_update(&entry->ent, 0, pc);
938 entry->ent.type = TRACE_SPECIAL;
942 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
943 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
944 ftrace_trace_userstack(tr, data, irq_flags, pc);
950 __trace_special(void *__tr, void *__data,
951 unsigned long arg1, unsigned long arg2, unsigned long arg3)
953 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
957 tracing_sched_switch_trace(struct trace_array *tr,
958 struct trace_array_cpu *data,
959 struct task_struct *prev,
960 struct task_struct *next,
961 unsigned long flags, int pc)
963 struct ring_buffer_event *event;
964 struct ctx_switch_entry *entry;
965 unsigned long irq_flags;
967 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
971 entry = ring_buffer_event_data(event);
972 tracing_generic_entry_update(&entry->ent, flags, pc);
973 entry->ent.type = TRACE_CTX;
974 entry->prev_pid = prev->pid;
975 entry->prev_prio = prev->prio;
976 entry->prev_state = prev->state;
977 entry->next_pid = next->pid;
978 entry->next_prio = next->prio;
979 entry->next_state = next->state;
980 entry->next_cpu = task_cpu(next);
981 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
982 ftrace_trace_stack(tr, data, flags, 5, pc);
983 ftrace_trace_userstack(tr, data, flags, pc);
987 tracing_sched_wakeup_trace(struct trace_array *tr,
988 struct trace_array_cpu *data,
989 struct task_struct *wakee,
990 struct task_struct *curr,
991 unsigned long flags, int pc)
993 struct ring_buffer_event *event;
994 struct ctx_switch_entry *entry;
995 unsigned long irq_flags;
997 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1001 entry = ring_buffer_event_data(event);
1002 tracing_generic_entry_update(&entry->ent, flags, pc);
1003 entry->ent.type = TRACE_WAKE;
1004 entry->prev_pid = curr->pid;
1005 entry->prev_prio = curr->prio;
1006 entry->prev_state = curr->state;
1007 entry->next_pid = wakee->pid;
1008 entry->next_prio = wakee->prio;
1009 entry->next_state = wakee->state;
1010 entry->next_cpu = task_cpu(wakee);
1011 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1012 ftrace_trace_stack(tr, data, flags, 6, pc);
1013 ftrace_trace_userstack(tr, data, flags, pc);
1019 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1021 struct trace_array *tr = &global_trace;
1022 struct trace_array_cpu *data;
1023 unsigned long flags;
1027 if (tracing_disabled)
1030 pc = preempt_count();
1031 local_irq_save(flags);
1032 cpu = raw_smp_processor_id();
1033 data = tr->data[cpu];
1035 if (likely(atomic_inc_return(&data->disabled) == 1))
1036 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1038 atomic_dec(&data->disabled);
1039 local_irq_restore(flags);
1042 #ifdef CONFIG_FUNCTION_TRACER
1044 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1046 struct trace_array *tr = &global_trace;
1047 struct trace_array_cpu *data;
1048 unsigned long flags;
1053 if (unlikely(!ftrace_function_enabled))
1056 pc = preempt_count();
1057 resched = ftrace_preempt_disable();
1058 local_save_flags(flags);
1059 cpu = raw_smp_processor_id();
1060 data = tr->data[cpu];
1061 disabled = atomic_inc_return(&data->disabled);
1063 if (likely(disabled == 1))
1064 trace_function(tr, data, ip, parent_ip, flags, pc);
1066 atomic_dec(&data->disabled);
1067 ftrace_preempt_enable(resched);
1071 function_trace_call(unsigned long ip, unsigned long parent_ip)
1073 struct trace_array *tr = &global_trace;
1074 struct trace_array_cpu *data;
1075 unsigned long flags;
1080 if (unlikely(!ftrace_function_enabled))
1084 * Need to use raw, since this must be called before the
1085 * recursive protection is performed.
1087 local_irq_save(flags);
1088 cpu = raw_smp_processor_id();
1089 data = tr->data[cpu];
1090 disabled = atomic_inc_return(&data->disabled);
1092 if (likely(disabled == 1)) {
1093 pc = preempt_count();
1094 trace_function(tr, data, ip, parent_ip, flags, pc);
1097 atomic_dec(&data->disabled);
1098 local_irq_restore(flags);
1101 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1102 int trace_graph_entry(struct ftrace_graph_ent *trace)
1104 struct trace_array *tr = &global_trace;
1105 struct trace_array_cpu *data;
1106 unsigned long flags;
1111 if (!ftrace_trace_task(current))
1114 if (!ftrace_graph_addr(trace->func))
1117 local_irq_save(flags);
1118 cpu = raw_smp_processor_id();
1119 data = tr->data[cpu];
1120 disabled = atomic_inc_return(&data->disabled);
1121 if (likely(disabled == 1)) {
1122 pc = preempt_count();
1123 __trace_graph_entry(tr, data, trace, flags, pc);
1125 /* Only do the atomic if it is not already set */
1126 if (!test_tsk_trace_graph(current))
1127 set_tsk_trace_graph(current);
1128 atomic_dec(&data->disabled);
1129 local_irq_restore(flags);
1134 void trace_graph_return(struct ftrace_graph_ret *trace)
1136 struct trace_array *tr = &global_trace;
1137 struct trace_array_cpu *data;
1138 unsigned long flags;
1143 local_irq_save(flags);
1144 cpu = raw_smp_processor_id();
1145 data = tr->data[cpu];
1146 disabled = atomic_inc_return(&data->disabled);
1147 if (likely(disabled == 1)) {
1148 pc = preempt_count();
1149 __trace_graph_return(tr, data, trace, flags, pc);
1152 clear_tsk_trace_graph(current);
1153 atomic_dec(&data->disabled);
1154 local_irq_restore(flags);
1156 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1158 static struct ftrace_ops trace_ops __read_mostly =
1160 .func = function_trace_call,
1163 void tracing_start_function_trace(void)
1165 ftrace_function_enabled = 0;
1167 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1168 trace_ops.func = function_trace_call_preempt_only;
1170 trace_ops.func = function_trace_call;
1172 register_ftrace_function(&trace_ops);
1173 ftrace_function_enabled = 1;
1176 void tracing_stop_function_trace(void)
1178 ftrace_function_enabled = 0;
1179 unregister_ftrace_function(&trace_ops);
1183 enum trace_file_type {
1184 TRACE_FILE_LAT_FMT = 1,
1185 TRACE_FILE_ANNOTATE = 2,
1188 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1190 /* Don't allow ftrace to trace into the ring buffers */
1191 ftrace_disable_cpu();
1194 if (iter->buffer_iter[iter->cpu])
1195 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1197 ftrace_enable_cpu();
1200 static struct trace_entry *
1201 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1203 struct ring_buffer_event *event;
1204 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1206 /* Don't allow ftrace to trace into the ring buffers */
1207 ftrace_disable_cpu();
1210 event = ring_buffer_iter_peek(buf_iter, ts);
1212 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1214 ftrace_enable_cpu();
1216 return event ? ring_buffer_event_data(event) : NULL;
1219 static struct trace_entry *
1220 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1222 struct ring_buffer *buffer = iter->tr->buffer;
1223 struct trace_entry *ent, *next = NULL;
1224 u64 next_ts = 0, ts;
1228 for_each_tracing_cpu(cpu) {
1230 if (ring_buffer_empty_cpu(buffer, cpu))
1233 ent = peek_next_entry(iter, cpu, &ts);
1236 * Pick the entry with the smallest timestamp:
1238 if (ent && (!next || ts < next_ts)) {
1246 *ent_cpu = next_cpu;
1254 /* Find the next real entry, without updating the iterator itself */
1255 static struct trace_entry *
1256 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1258 return __find_next_entry(iter, ent_cpu, ent_ts);
1261 /* Find the next real entry, and increment the iterator to the next entry */
1262 static void *find_next_entry_inc(struct trace_iterator *iter)
1264 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1267 trace_iterator_increment(iter, iter->cpu);
1269 return iter->ent ? iter : NULL;
1272 static void trace_consume(struct trace_iterator *iter)
1274 /* Don't allow ftrace to trace into the ring buffers */
1275 ftrace_disable_cpu();
1276 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1277 ftrace_enable_cpu();
1280 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1282 struct trace_iterator *iter = m->private;
1288 /* can't go backwards */
1293 ent = find_next_entry_inc(iter);
1297 while (ent && iter->idx < i)
1298 ent = find_next_entry_inc(iter);
1305 static void *s_start(struct seq_file *m, loff_t *pos)
1307 struct trace_iterator *iter = m->private;
1312 mutex_lock(&trace_types_lock);
1314 if (!current_trace || current_trace != iter->trace) {
1315 mutex_unlock(&trace_types_lock);
1319 atomic_inc(&trace_record_cmdline_disabled);
1321 if (*pos != iter->pos) {
1326 ftrace_disable_cpu();
1328 for_each_tracing_cpu(cpu) {
1329 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1332 ftrace_enable_cpu();
1334 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1339 p = s_next(m, p, &l);
1345 static void s_stop(struct seq_file *m, void *p)
1347 atomic_dec(&trace_record_cmdline_disabled);
1348 mutex_unlock(&trace_types_lock);
1351 static void print_lat_help_header(struct seq_file *m)
1353 seq_puts(m, "# _------=> CPU# \n");
1354 seq_puts(m, "# / _-----=> irqs-off \n");
1355 seq_puts(m, "# | / _----=> need-resched \n");
1356 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1357 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1358 seq_puts(m, "# |||| / \n");
1359 seq_puts(m, "# ||||| delay \n");
1360 seq_puts(m, "# cmd pid ||||| time | caller \n");
1361 seq_puts(m, "# \\ / ||||| \\ | / \n");
1364 static void print_func_help_header(struct seq_file *m)
1366 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1367 seq_puts(m, "# | | | | |\n");
1372 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1374 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1375 struct trace_array *tr = iter->tr;
1376 struct trace_array_cpu *data = tr->data[tr->cpu];
1377 struct tracer *type = current_trace;
1378 unsigned long total;
1379 unsigned long entries;
1380 const char *name = "preemption";
1385 entries = ring_buffer_entries(iter->tr->buffer);
1387 ring_buffer_overruns(iter->tr->buffer);
1389 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1391 seq_puts(m, "-----------------------------------"
1392 "---------------------------------\n");
1393 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1394 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1395 nsecs_to_usecs(data->saved_latency),
1399 #if defined(CONFIG_PREEMPT_NONE)
1401 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1403 #elif defined(CONFIG_PREEMPT)
1408 /* These are reserved for later use */
1411 seq_printf(m, " #P:%d)\n", num_online_cpus());
1415 seq_puts(m, " -----------------\n");
1416 seq_printf(m, " | task: %.16s-%d "
1417 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1418 data->comm, data->pid, data->uid, data->nice,
1419 data->policy, data->rt_priority);
1420 seq_puts(m, " -----------------\n");
1422 if (data->critical_start) {
1423 seq_puts(m, " => started at: ");
1424 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1425 trace_print_seq(m, &iter->seq);
1426 seq_puts(m, "\n => ended at: ");
1427 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1428 trace_print_seq(m, &iter->seq);
1436 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1438 int hardirq, softirq;
1441 comm = trace_find_cmdline(entry->pid);
1443 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1444 trace_seq_printf(s, "%3d", cpu);
1445 trace_seq_printf(s, "%c%c",
1446 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1447 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1448 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1450 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1451 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1452 if (hardirq && softirq) {
1453 trace_seq_putc(s, 'H');
1456 trace_seq_putc(s, 'h');
1459 trace_seq_putc(s, 's');
1461 trace_seq_putc(s, '.');
1465 if (entry->preempt_count)
1466 trace_seq_printf(s, "%x", entry->preempt_count);
1468 trace_seq_puts(s, ".");
1471 unsigned long preempt_mark_thresh = 100;
1474 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1475 unsigned long rel_usecs)
1477 trace_seq_printf(s, " %4lldus", abs_usecs);
1478 if (rel_usecs > preempt_mark_thresh)
1479 trace_seq_puts(s, "!: ");
1480 else if (rel_usecs > 1)
1481 trace_seq_puts(s, "+: ");
1483 trace_seq_puts(s, " : ");
1486 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1488 static int task_state_char(unsigned long state)
1490 int bit = state ? __ffs(state) + 1 : 0;
1492 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
1495 static void test_cpu_buff_start(struct trace_iterator *iter)
1497 struct trace_seq *s = &iter->seq;
1499 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1502 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1505 if (cpu_isset(iter->cpu, iter->started))
1508 cpu_set(iter->cpu, iter->started);
1509 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1512 static enum print_line_t
1513 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1515 struct trace_seq *s = &iter->seq;
1516 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1517 struct trace_entry *next_entry;
1518 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1519 struct trace_entry *entry = iter->ent;
1520 unsigned long abs_usecs;
1521 unsigned long rel_usecs;
1527 test_cpu_buff_start(iter);
1529 next_entry = find_next_entry(iter, NULL, &next_ts);
1532 rel_usecs = ns2usecs(next_ts - iter->ts);
1533 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1536 comm = trace_find_cmdline(entry->pid);
1537 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1538 " %ld.%03ldms (+%ld.%03ldms): ",
1540 entry->pid, cpu, entry->flags,
1541 entry->preempt_count, trace_idx,
1544 abs_usecs % 1000, rel_usecs/1000,
1547 lat_print_generic(s, entry, cpu);
1548 lat_print_timestamp(s, abs_usecs, rel_usecs);
1550 switch (entry->type) {
1552 struct ftrace_entry *field;
1554 trace_assign_type(field, entry);
1556 seq_print_ip_sym(s, field->ip, sym_flags);
1557 trace_seq_puts(s, " (");
1558 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1559 trace_seq_puts(s, ")\n");
1564 struct ctx_switch_entry *field;
1566 trace_assign_type(field, entry);
1568 T = task_state_char(field->next_state);
1569 S = task_state_char(field->prev_state);
1570 comm = trace_find_cmdline(field->next_pid);
1571 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1574 S, entry->type == TRACE_CTX ? "==>" : " +",
1581 case TRACE_SPECIAL: {
1582 struct special_entry *field;
1584 trace_assign_type(field, entry);
1586 trace_seq_printf(s, "# %ld %ld %ld\n",
1593 struct stack_entry *field;
1595 trace_assign_type(field, entry);
1597 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1599 trace_seq_puts(s, " <= ");
1600 seq_print_ip_sym(s, field->caller[i], sym_flags);
1602 trace_seq_puts(s, "\n");
1606 struct print_entry *field;
1608 trace_assign_type(field, entry);
1610 seq_print_ip_sym(s, field->ip, sym_flags);
1611 trace_seq_printf(s, ": %s", field->buf);
1614 case TRACE_BRANCH: {
1615 struct trace_branch *field;
1617 trace_assign_type(field, entry);
1619 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1620 field->correct ? " ok " : " MISS ",
1626 case TRACE_USER_STACK: {
1627 struct userstack_entry *field;
1629 trace_assign_type(field, entry);
1631 seq_print_userip_objs(field, s, sym_flags);
1632 trace_seq_putc(s, '\n');
1636 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1638 return TRACE_TYPE_HANDLED;
1641 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1643 struct trace_seq *s = &iter->seq;
1644 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1645 struct trace_entry *entry;
1646 unsigned long usec_rem;
1647 unsigned long long t;
1656 test_cpu_buff_start(iter);
1658 comm = trace_find_cmdline(iter->ent->pid);
1660 t = ns2usecs(iter->ts);
1661 usec_rem = do_div(t, 1000000ULL);
1662 secs = (unsigned long)t;
1664 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1666 return TRACE_TYPE_PARTIAL_LINE;
1667 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1669 return TRACE_TYPE_PARTIAL_LINE;
1670 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1672 return TRACE_TYPE_PARTIAL_LINE;
1674 switch (entry->type) {
1676 struct ftrace_entry *field;
1678 trace_assign_type(field, entry);
1680 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1682 return TRACE_TYPE_PARTIAL_LINE;
1683 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1685 ret = trace_seq_printf(s, " <-");
1687 return TRACE_TYPE_PARTIAL_LINE;
1688 ret = seq_print_ip_sym(s,
1692 return TRACE_TYPE_PARTIAL_LINE;
1694 ret = trace_seq_printf(s, "\n");
1696 return TRACE_TYPE_PARTIAL_LINE;
1701 struct ctx_switch_entry *field;
1703 trace_assign_type(field, entry);
1705 T = task_state_char(field->next_state);
1706 S = task_state_char(field->prev_state);
1707 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1711 entry->type == TRACE_CTX ? "==>" : " +",
1717 return TRACE_TYPE_PARTIAL_LINE;
1720 case TRACE_SPECIAL: {
1721 struct special_entry *field;
1723 trace_assign_type(field, entry);
1725 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1730 return TRACE_TYPE_PARTIAL_LINE;
1734 struct stack_entry *field;
1736 trace_assign_type(field, entry);
1738 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1740 ret = trace_seq_puts(s, " <= ");
1742 return TRACE_TYPE_PARTIAL_LINE;
1744 ret = seq_print_ip_sym(s, field->caller[i],
1747 return TRACE_TYPE_PARTIAL_LINE;
1749 ret = trace_seq_puts(s, "\n");
1751 return TRACE_TYPE_PARTIAL_LINE;
1755 struct print_entry *field;
1757 trace_assign_type(field, entry);
1759 seq_print_ip_sym(s, field->ip, sym_flags);
1760 trace_seq_printf(s, ": %s", field->buf);
1763 case TRACE_GRAPH_RET: {
1764 return print_graph_function(iter);
1766 case TRACE_GRAPH_ENT: {
1767 return print_graph_function(iter);
1769 case TRACE_BRANCH: {
1770 struct trace_branch *field;
1772 trace_assign_type(field, entry);
1774 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1775 field->correct ? " ok " : " MISS ",
1781 case TRACE_USER_STACK: {
1782 struct userstack_entry *field;
1784 trace_assign_type(field, entry);
1786 ret = seq_print_userip_objs(field, s, sym_flags);
1788 return TRACE_TYPE_PARTIAL_LINE;
1789 ret = trace_seq_putc(s, '\n');
1791 return TRACE_TYPE_PARTIAL_LINE;
1795 return TRACE_TYPE_HANDLED;
1798 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1800 struct trace_seq *s = &iter->seq;
1801 struct trace_entry *entry;
1807 ret = trace_seq_printf(s, "%d %d %llu ",
1808 entry->pid, iter->cpu, iter->ts);
1810 return TRACE_TYPE_PARTIAL_LINE;
1812 switch (entry->type) {
1814 struct ftrace_entry *field;
1816 trace_assign_type(field, entry);
1818 ret = trace_seq_printf(s, "%x %x\n",
1822 return TRACE_TYPE_PARTIAL_LINE;
1827 struct ctx_switch_entry *field;
1829 trace_assign_type(field, entry);
1831 T = task_state_char(field->next_state);
1832 S = entry->type == TRACE_WAKE ? '+' :
1833 task_state_char(field->prev_state);
1834 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1843 return TRACE_TYPE_PARTIAL_LINE;
1847 case TRACE_USER_STACK:
1849 struct special_entry *field;
1851 trace_assign_type(field, entry);
1853 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1858 return TRACE_TYPE_PARTIAL_LINE;
1862 struct print_entry *field;
1864 trace_assign_type(field, entry);
1866 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1870 return TRACE_TYPE_HANDLED;
1873 #define SEQ_PUT_FIELD_RET(s, x) \
1875 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1879 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1881 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1882 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1886 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1888 struct trace_seq *s = &iter->seq;
1889 unsigned char newline = '\n';
1890 struct trace_entry *entry;
1895 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1896 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1897 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1899 switch (entry->type) {
1901 struct ftrace_entry *field;
1903 trace_assign_type(field, entry);
1905 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1906 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1911 struct ctx_switch_entry *field;
1913 trace_assign_type(field, entry);
1915 T = task_state_char(field->next_state);
1916 S = entry->type == TRACE_WAKE ? '+' :
1917 task_state_char(field->prev_state);
1918 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1919 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1920 SEQ_PUT_HEX_FIELD_RET(s, S);
1921 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1922 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1923 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1924 SEQ_PUT_HEX_FIELD_RET(s, T);
1928 case TRACE_USER_STACK:
1930 struct special_entry *field;
1932 trace_assign_type(field, entry);
1934 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1935 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1936 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1940 SEQ_PUT_FIELD_RET(s, newline);
1942 return TRACE_TYPE_HANDLED;
1945 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1947 struct trace_seq *s = &iter->seq;
1948 struct trace_entry *entry = iter->ent;
1949 struct print_entry *field;
1952 trace_assign_type(field, entry);
1954 ret = trace_seq_printf(s, field->buf);
1956 return TRACE_TYPE_PARTIAL_LINE;
1958 return TRACE_TYPE_HANDLED;
1961 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1963 struct trace_seq *s = &iter->seq;
1964 struct trace_entry *entry;
1968 SEQ_PUT_FIELD_RET(s, entry->pid);
1969 SEQ_PUT_FIELD_RET(s, entry->cpu);
1970 SEQ_PUT_FIELD_RET(s, iter->ts);
1972 switch (entry->type) {
1974 struct ftrace_entry *field;
1976 trace_assign_type(field, entry);
1978 SEQ_PUT_FIELD_RET(s, field->ip);
1979 SEQ_PUT_FIELD_RET(s, field->parent_ip);
1983 struct ctx_switch_entry *field;
1985 trace_assign_type(field, entry);
1987 SEQ_PUT_FIELD_RET(s, field->prev_pid);
1988 SEQ_PUT_FIELD_RET(s, field->prev_prio);
1989 SEQ_PUT_FIELD_RET(s, field->prev_state);
1990 SEQ_PUT_FIELD_RET(s, field->next_pid);
1991 SEQ_PUT_FIELD_RET(s, field->next_prio);
1992 SEQ_PUT_FIELD_RET(s, field->next_state);
1996 case TRACE_USER_STACK:
1998 struct special_entry *field;
2000 trace_assign_type(field, entry);
2002 SEQ_PUT_FIELD_RET(s, field->arg1);
2003 SEQ_PUT_FIELD_RET(s, field->arg2);
2004 SEQ_PUT_FIELD_RET(s, field->arg3);
2011 static int trace_empty(struct trace_iterator *iter)
2015 for_each_tracing_cpu(cpu) {
2016 if (iter->buffer_iter[cpu]) {
2017 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2020 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2028 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2030 enum print_line_t ret;
2032 if (iter->trace && iter->trace->print_line) {
2033 ret = iter->trace->print_line(iter);
2034 if (ret != TRACE_TYPE_UNHANDLED)
2038 if (iter->ent->type == TRACE_PRINT &&
2039 trace_flags & TRACE_ITER_PRINTK &&
2040 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2041 return print_printk_msg_only(iter);
2043 if (trace_flags & TRACE_ITER_BIN)
2044 return print_bin_fmt(iter);
2046 if (trace_flags & TRACE_ITER_HEX)
2047 return print_hex_fmt(iter);
2049 if (trace_flags & TRACE_ITER_RAW)
2050 return print_raw_fmt(iter);
2052 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2053 return print_lat_fmt(iter, iter->idx, iter->cpu);
2055 return print_trace_fmt(iter);
2058 static int s_show(struct seq_file *m, void *v)
2060 struct trace_iterator *iter = v;
2062 if (iter->ent == NULL) {
2064 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2067 if (iter->trace && iter->trace->print_header)
2068 iter->trace->print_header(m);
2069 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2070 /* print nothing if the buffers are empty */
2071 if (trace_empty(iter))
2073 print_trace_header(m, iter);
2074 if (!(trace_flags & TRACE_ITER_VERBOSE))
2075 print_lat_help_header(m);
2077 if (!(trace_flags & TRACE_ITER_VERBOSE))
2078 print_func_help_header(m);
2081 print_trace_line(iter);
2082 trace_print_seq(m, &iter->seq);
2088 static struct seq_operations tracer_seq_ops = {
2095 static struct trace_iterator *
2096 __tracing_open(struct inode *inode, struct file *file, int *ret)
2098 struct trace_iterator *iter;
2102 if (tracing_disabled) {
2107 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2113 mutex_lock(&trace_types_lock);
2114 if (current_trace && current_trace->print_max)
2117 iter->tr = inode->i_private;
2118 iter->trace = current_trace;
2121 /* Notify the tracer early; before we stop tracing. */
2122 if (iter->trace && iter->trace->open)
2123 iter->trace->open(iter);
2125 /* Annotate start of buffers if we had overruns */
2126 if (ring_buffer_overruns(iter->tr->buffer))
2127 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2130 for_each_tracing_cpu(cpu) {
2132 iter->buffer_iter[cpu] =
2133 ring_buffer_read_start(iter->tr->buffer, cpu);
2135 if (!iter->buffer_iter[cpu])
2139 /* TODO stop tracer */
2140 *ret = seq_open(file, &tracer_seq_ops);
2144 m = file->private_data;
2147 /* stop the trace while dumping */
2150 mutex_unlock(&trace_types_lock);
2156 for_each_tracing_cpu(cpu) {
2157 if (iter->buffer_iter[cpu])
2158 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2160 mutex_unlock(&trace_types_lock);
2163 return ERR_PTR(-ENOMEM);
2166 int tracing_open_generic(struct inode *inode, struct file *filp)
2168 if (tracing_disabled)
2171 filp->private_data = inode->i_private;
2175 int tracing_release(struct inode *inode, struct file *file)
2177 struct seq_file *m = (struct seq_file *)file->private_data;
2178 struct trace_iterator *iter = m->private;
2181 mutex_lock(&trace_types_lock);
2182 for_each_tracing_cpu(cpu) {
2183 if (iter->buffer_iter[cpu])
2184 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2187 if (iter->trace && iter->trace->close)
2188 iter->trace->close(iter);
2190 /* reenable tracing if it was previously enabled */
2192 mutex_unlock(&trace_types_lock);
2194 seq_release(inode, file);
2199 static int tracing_open(struct inode *inode, struct file *file)
2203 __tracing_open(inode, file, &ret);
2208 static int tracing_lt_open(struct inode *inode, struct file *file)
2210 struct trace_iterator *iter;
2213 iter = __tracing_open(inode, file, &ret);
2216 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2223 t_next(struct seq_file *m, void *v, loff_t *pos)
2225 struct tracer *t = m->private;
2237 static void *t_start(struct seq_file *m, loff_t *pos)
2239 struct tracer *t = m->private;
2242 mutex_lock(&trace_types_lock);
2243 for (; t && l < *pos; t = t_next(m, t, &l))
2249 static void t_stop(struct seq_file *m, void *p)
2251 mutex_unlock(&trace_types_lock);
2254 static int t_show(struct seq_file *m, void *v)
2256 struct tracer *t = v;
2261 seq_printf(m, "%s", t->name);
2270 static struct seq_operations show_traces_seq_ops = {
2277 static int show_traces_open(struct inode *inode, struct file *file)
2281 if (tracing_disabled)
2284 ret = seq_open(file, &show_traces_seq_ops);
2286 struct seq_file *m = file->private_data;
2287 m->private = trace_types;
2293 static struct file_operations tracing_fops = {
2294 .open = tracing_open,
2296 .llseek = seq_lseek,
2297 .release = tracing_release,
2300 static struct file_operations tracing_lt_fops = {
2301 .open = tracing_lt_open,
2303 .llseek = seq_lseek,
2304 .release = tracing_release,
2307 static struct file_operations show_traces_fops = {
2308 .open = show_traces_open,
2310 .release = seq_release,
2314 * Only trace on a CPU if the bitmask is set:
2316 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2319 * When tracing/tracing_cpu_mask is modified then this holds
2320 * the new bitmask we are about to install:
2322 static cpumask_t tracing_cpumask_new;
2325 * The tracer itself will not take this lock, but still we want
2326 * to provide a consistent cpumask to user-space:
2328 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2331 * Temporary storage for the character representation of the
2332 * CPU bitmask (and one more byte for the newline):
2334 static char mask_str[NR_CPUS + 1];
2337 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2338 size_t count, loff_t *ppos)
2342 mutex_lock(&tracing_cpumask_update_lock);
2344 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2345 if (count - len < 2) {
2349 len += sprintf(mask_str + len, "\n");
2350 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2353 mutex_unlock(&tracing_cpumask_update_lock);
2359 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2360 size_t count, loff_t *ppos)
2364 mutex_lock(&tracing_cpumask_update_lock);
2365 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2369 local_irq_disable();
2370 __raw_spin_lock(&ftrace_max_lock);
2371 for_each_tracing_cpu(cpu) {
2373 * Increase/decrease the disabled counter if we are
2374 * about to flip a bit in the cpumask:
2376 if (cpu_isset(cpu, tracing_cpumask) &&
2377 !cpu_isset(cpu, tracing_cpumask_new)) {
2378 atomic_inc(&global_trace.data[cpu]->disabled);
2380 if (!cpu_isset(cpu, tracing_cpumask) &&
2381 cpu_isset(cpu, tracing_cpumask_new)) {
2382 atomic_dec(&global_trace.data[cpu]->disabled);
2385 __raw_spin_unlock(&ftrace_max_lock);
2388 tracing_cpumask = tracing_cpumask_new;
2390 mutex_unlock(&tracing_cpumask_update_lock);
2395 mutex_unlock(&tracing_cpumask_update_lock);
2400 static struct file_operations tracing_cpumask_fops = {
2401 .open = tracing_open_generic,
2402 .read = tracing_cpumask_read,
2403 .write = tracing_cpumask_write,
2407 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2408 size_t cnt, loff_t *ppos)
2414 u32 tracer_flags = current_trace->flags->val;
2415 struct tracer_opt *trace_opts = current_trace->flags->opts;
2418 /* calulate max size */
2419 for (i = 0; trace_options[i]; i++) {
2420 len += strlen(trace_options[i]);
2421 len += 3; /* "no" and space */
2425 * Increase the size with names of options specific
2426 * of the current tracer.
2428 for (i = 0; trace_opts[i].name; i++) {
2429 len += strlen(trace_opts[i].name);
2430 len += 3; /* "no" and space */
2433 /* +2 for \n and \0 */
2434 buf = kmalloc(len + 2, GFP_KERNEL);
2438 for (i = 0; trace_options[i]; i++) {
2439 if (trace_flags & (1 << i))
2440 r += sprintf(buf + r, "%s ", trace_options[i]);
2442 r += sprintf(buf + r, "no%s ", trace_options[i]);
2445 for (i = 0; trace_opts[i].name; i++) {
2446 if (tracer_flags & trace_opts[i].bit)
2447 r += sprintf(buf + r, "%s ",
2448 trace_opts[i].name);
2450 r += sprintf(buf + r, "no%s ",
2451 trace_opts[i].name);
2454 r += sprintf(buf + r, "\n");
2455 WARN_ON(r >= len + 2);
2457 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2464 /* Try to assign a tracer specific option */
2465 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2467 struct tracer_flags *trace_flags = trace->flags;
2468 struct tracer_opt *opts = NULL;
2472 for (i = 0; trace_flags->opts[i].name; i++) {
2473 opts = &trace_flags->opts[i];
2474 len = strlen(opts->name);
2476 if (strncmp(cmp, opts->name, len) == 0) {
2477 ret = trace->set_flag(trace_flags->val,
2483 if (!trace_flags->opts[i].name)
2486 /* Refused to handle */
2491 trace_flags->val &= ~opts->bit;
2493 trace_flags->val |= opts->bit;
2499 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2500 size_t cnt, loff_t *ppos)
2508 if (cnt >= sizeof(buf))
2511 if (copy_from_user(&buf, ubuf, cnt))
2516 if (strncmp(buf, "no", 2) == 0) {
2521 for (i = 0; trace_options[i]; i++) {
2522 int len = strlen(trace_options[i]);
2524 if (strncmp(cmp, trace_options[i], len) == 0) {
2526 trace_flags &= ~(1 << i);
2528 trace_flags |= (1 << i);
2533 /* If no option could be set, test the specific tracer options */
2534 if (!trace_options[i]) {
2535 ret = set_tracer_option(current_trace, cmp, neg);
2545 static struct file_operations tracing_iter_fops = {
2546 .open = tracing_open_generic,
2547 .read = tracing_trace_options_read,
2548 .write = tracing_trace_options_write,
2551 static const char readme_msg[] =
2552 "tracing mini-HOWTO:\n\n"
2554 "# mount -t debugfs nodev /debug\n\n"
2555 "# cat /debug/tracing/available_tracers\n"
2556 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2557 "# cat /debug/tracing/current_tracer\n"
2559 "# echo sched_switch > /debug/tracing/current_tracer\n"
2560 "# cat /debug/tracing/current_tracer\n"
2562 "# cat /debug/tracing/trace_options\n"
2563 "noprint-parent nosym-offset nosym-addr noverbose\n"
2564 "# echo print-parent > /debug/tracing/trace_options\n"
2565 "# echo 1 > /debug/tracing/tracing_enabled\n"
2566 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2567 "echo 0 > /debug/tracing/tracing_enabled\n"
2571 tracing_readme_read(struct file *filp, char __user *ubuf,
2572 size_t cnt, loff_t *ppos)
2574 return simple_read_from_buffer(ubuf, cnt, ppos,
2575 readme_msg, strlen(readme_msg));
2578 static struct file_operations tracing_readme_fops = {
2579 .open = tracing_open_generic,
2580 .read = tracing_readme_read,
2584 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2585 size_t cnt, loff_t *ppos)
2590 r = sprintf(buf, "%u\n", tracer_enabled);
2591 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2595 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2596 size_t cnt, loff_t *ppos)
2598 struct trace_array *tr = filp->private_data;
2603 if (cnt >= sizeof(buf))
2606 if (copy_from_user(&buf, ubuf, cnt))
2611 ret = strict_strtoul(buf, 10, &val);
2617 mutex_lock(&trace_types_lock);
2618 if (tracer_enabled ^ val) {
2621 if (current_trace->start)
2622 current_trace->start(tr);
2627 if (current_trace->stop)
2628 current_trace->stop(tr);
2631 mutex_unlock(&trace_types_lock);
2639 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2640 size_t cnt, loff_t *ppos)
2642 char buf[max_tracer_type_len+2];
2645 mutex_lock(&trace_types_lock);
2647 r = sprintf(buf, "%s\n", current_trace->name);
2649 r = sprintf(buf, "\n");
2650 mutex_unlock(&trace_types_lock);
2652 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2655 static int tracing_set_tracer(char *buf)
2657 struct trace_array *tr = &global_trace;
2661 mutex_lock(&trace_types_lock);
2662 for (t = trace_types; t; t = t->next) {
2663 if (strcmp(t->name, buf) == 0)
2670 if (t == current_trace)
2673 trace_branch_disable();
2674 if (current_trace && current_trace->reset)
2675 current_trace->reset(tr);
2684 trace_branch_enable(tr);
2686 mutex_unlock(&trace_types_lock);
2692 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2693 size_t cnt, loff_t *ppos)
2695 char buf[max_tracer_type_len+1];
2702 if (cnt > max_tracer_type_len)
2703 cnt = max_tracer_type_len;
2705 if (copy_from_user(&buf, ubuf, cnt))
2710 /* strip ending whitespace. */
2711 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2714 err = tracing_set_tracer(buf);
2724 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2725 size_t cnt, loff_t *ppos)
2727 unsigned long *ptr = filp->private_data;
2731 r = snprintf(buf, sizeof(buf), "%ld\n",
2732 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2733 if (r > sizeof(buf))
2735 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2739 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2740 size_t cnt, loff_t *ppos)
2742 long *ptr = filp->private_data;
2747 if (cnt >= sizeof(buf))
2750 if (copy_from_user(&buf, ubuf, cnt))
2755 ret = strict_strtoul(buf, 10, &val);
2764 static atomic_t tracing_reader;
2766 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2768 struct trace_iterator *iter;
2770 if (tracing_disabled)
2773 /* We only allow for reader of the pipe */
2774 if (atomic_inc_return(&tracing_reader) != 1) {
2775 atomic_dec(&tracing_reader);
2779 /* create a buffer to store the information to pass to userspace */
2780 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2784 mutex_lock(&trace_types_lock);
2786 /* trace pipe does not show start of buffer */
2787 cpus_setall(iter->started);
2789 iter->tr = &global_trace;
2790 iter->trace = current_trace;
2791 filp->private_data = iter;
2793 if (iter->trace->pipe_open)
2794 iter->trace->pipe_open(iter);
2795 mutex_unlock(&trace_types_lock);
2800 static int tracing_release_pipe(struct inode *inode, struct file *file)
2802 struct trace_iterator *iter = file->private_data;
2805 atomic_dec(&tracing_reader);
2811 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2813 struct trace_iterator *iter = filp->private_data;
2815 if (trace_flags & TRACE_ITER_BLOCK) {
2817 * Always select as readable when in blocking mode
2819 return POLLIN | POLLRDNORM;
2821 if (!trace_empty(iter))
2822 return POLLIN | POLLRDNORM;
2823 poll_wait(filp, &trace_wait, poll_table);
2824 if (!trace_empty(iter))
2825 return POLLIN | POLLRDNORM;
2835 tracing_read_pipe(struct file *filp, char __user *ubuf,
2836 size_t cnt, loff_t *ppos)
2838 struct trace_iterator *iter = filp->private_data;
2841 /* return any leftover data */
2842 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2846 trace_seq_reset(&iter->seq);
2848 mutex_lock(&trace_types_lock);
2849 if (iter->trace->read) {
2850 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2857 while (trace_empty(iter)) {
2859 if ((filp->f_flags & O_NONBLOCK)) {
2865 * This is a make-shift waitqueue. The reason we don't use
2866 * an actual wait queue is because:
2867 * 1) we only ever have one waiter
2868 * 2) the tracing, traces all functions, we don't want
2869 * the overhead of calling wake_up and friends
2870 * (and tracing them too)
2871 * Anyway, this is really very primitive wakeup.
2873 set_current_state(TASK_INTERRUPTIBLE);
2874 iter->tr->waiter = current;
2876 mutex_unlock(&trace_types_lock);
2878 /* sleep for 100 msecs, and try again. */
2879 schedule_timeout(HZ/10);
2881 mutex_lock(&trace_types_lock);
2883 iter->tr->waiter = NULL;
2885 if (signal_pending(current)) {
2890 if (iter->trace != current_trace)
2894 * We block until we read something and tracing is disabled.
2895 * We still block if tracing is disabled, but we have never
2896 * read anything. This allows a user to cat this file, and
2897 * then enable tracing. But after we have read something,
2898 * we give an EOF when tracing is again disabled.
2900 * iter->pos will be 0 if we haven't read anything.
2902 if (!tracer_enabled && iter->pos)
2908 /* stop when tracing is finished */
2909 if (trace_empty(iter))
2912 if (cnt >= PAGE_SIZE)
2913 cnt = PAGE_SIZE - 1;
2915 /* reset all but tr, trace, and overruns */
2916 memset(&iter->seq, 0,
2917 sizeof(struct trace_iterator) -
2918 offsetof(struct trace_iterator, seq));
2921 while (find_next_entry_inc(iter) != NULL) {
2922 enum print_line_t ret;
2923 int len = iter->seq.len;
2925 ret = print_trace_line(iter);
2926 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2927 /* don't print partial lines */
2928 iter->seq.len = len;
2932 trace_consume(iter);
2934 if (iter->seq.len >= cnt)
2938 /* Now copy what we have to the user */
2939 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2940 if (iter->seq.readpos >= iter->seq.len)
2941 trace_seq_reset(&iter->seq);
2944 * If there was nothing to send to user, inspite of consuming trace
2945 * entries, go back to wait for more entries.
2951 mutex_unlock(&trace_types_lock);
2957 tracing_entries_read(struct file *filp, char __user *ubuf,
2958 size_t cnt, loff_t *ppos)
2960 struct trace_array *tr = filp->private_data;
2964 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2965 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2969 tracing_entries_write(struct file *filp, const char __user *ubuf,
2970 size_t cnt, loff_t *ppos)
2976 if (cnt >= sizeof(buf))
2979 if (copy_from_user(&buf, ubuf, cnt))
2984 ret = strict_strtoul(buf, 10, &val);
2988 /* must have at least 1 entry */
2992 mutex_lock(&trace_types_lock);
2996 /* disable all cpu buffers */
2997 for_each_tracing_cpu(cpu) {
2998 if (global_trace.data[cpu])
2999 atomic_inc(&global_trace.data[cpu]->disabled);
3000 if (max_tr.data[cpu])
3001 atomic_inc(&max_tr.data[cpu]->disabled);
3004 /* value is in KB */
3007 if (val != global_trace.entries) {
3008 ret = ring_buffer_resize(global_trace.buffer, val);
3014 ret = ring_buffer_resize(max_tr.buffer, val);
3018 r = ring_buffer_resize(global_trace.buffer,
3019 global_trace.entries);
3021 /* AARGH! We are left with different
3022 * size max buffer!!!! */
3024 tracing_disabled = 1;
3029 global_trace.entries = val;
3034 /* If check pages failed, return ENOMEM */
3035 if (tracing_disabled)
3038 for_each_tracing_cpu(cpu) {
3039 if (global_trace.data[cpu])
3040 atomic_dec(&global_trace.data[cpu]->disabled);
3041 if (max_tr.data[cpu])
3042 atomic_dec(&max_tr.data[cpu]->disabled);
3046 max_tr.entries = global_trace.entries;
3047 mutex_unlock(&trace_types_lock);
3052 static int mark_printk(const char *fmt, ...)
3056 va_start(args, fmt);
3057 ret = trace_vprintk(0, -1, fmt, args);
3063 tracing_mark_write(struct file *filp, const char __user *ubuf,
3064 size_t cnt, loff_t *fpos)
3069 if (tracing_disabled)
3072 if (cnt > TRACE_BUF_SIZE)
3073 cnt = TRACE_BUF_SIZE;
3075 buf = kmalloc(cnt + 1, GFP_KERNEL);
3079 if (copy_from_user(buf, ubuf, cnt)) {
3084 /* Cut from the first nil or newline. */
3086 end = strchr(buf, '\n');
3090 cnt = mark_printk("%s\n", buf);
3097 static struct file_operations tracing_max_lat_fops = {
3098 .open = tracing_open_generic,
3099 .read = tracing_max_lat_read,
3100 .write = tracing_max_lat_write,
3103 static struct file_operations tracing_ctrl_fops = {
3104 .open = tracing_open_generic,
3105 .read = tracing_ctrl_read,
3106 .write = tracing_ctrl_write,
3109 static struct file_operations set_tracer_fops = {
3110 .open = tracing_open_generic,
3111 .read = tracing_set_trace_read,
3112 .write = tracing_set_trace_write,
3115 static struct file_operations tracing_pipe_fops = {
3116 .open = tracing_open_pipe,
3117 .poll = tracing_poll_pipe,
3118 .read = tracing_read_pipe,
3119 .release = tracing_release_pipe,
3122 static struct file_operations tracing_entries_fops = {
3123 .open = tracing_open_generic,
3124 .read = tracing_entries_read,
3125 .write = tracing_entries_write,
3128 static struct file_operations tracing_mark_fops = {
3129 .open = tracing_open_generic,
3130 .write = tracing_mark_write,
3133 #ifdef CONFIG_DYNAMIC_FTRACE
3135 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3141 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3142 size_t cnt, loff_t *ppos)
3144 static char ftrace_dyn_info_buffer[1024];
3145 static DEFINE_MUTEX(dyn_info_mutex);
3146 unsigned long *p = filp->private_data;
3147 char *buf = ftrace_dyn_info_buffer;
3148 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3151 mutex_lock(&dyn_info_mutex);
3152 r = sprintf(buf, "%ld ", *p);
3154 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3157 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3159 mutex_unlock(&dyn_info_mutex);
3164 static struct file_operations tracing_dyn_info_fops = {
3165 .open = tracing_open_generic,
3166 .read = tracing_read_dyn_info,
3170 static struct dentry *d_tracer;
3172 struct dentry *tracing_init_dentry(void)
3179 d_tracer = debugfs_create_dir("tracing", NULL);
3181 if (!d_tracer && !once) {
3183 pr_warning("Could not create debugfs directory 'tracing'\n");
3190 #ifdef CONFIG_FTRACE_SELFTEST
3191 /* Let selftest have access to static functions in this file */
3192 #include "trace_selftest.c"
3195 static __init int tracer_init_debugfs(void)
3197 struct dentry *d_tracer;
3198 struct dentry *entry;
3200 d_tracer = tracing_init_dentry();
3202 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3203 &global_trace, &tracing_ctrl_fops);
3205 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3207 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3208 NULL, &tracing_iter_fops);
3210 pr_warning("Could not create debugfs 'trace_options' entry\n");
3212 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3213 NULL, &tracing_cpumask_fops);
3215 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3217 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3218 &global_trace, &tracing_lt_fops);
3220 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3222 entry = debugfs_create_file("trace", 0444, d_tracer,
3223 &global_trace, &tracing_fops);
3225 pr_warning("Could not create debugfs 'trace' entry\n");
3227 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3228 &global_trace, &show_traces_fops);
3230 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3232 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3233 &global_trace, &set_tracer_fops);
3235 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3237 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3238 &tracing_max_latency,
3239 &tracing_max_lat_fops);
3241 pr_warning("Could not create debugfs "
3242 "'tracing_max_latency' entry\n");
3244 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3245 &tracing_thresh, &tracing_max_lat_fops);
3247 pr_warning("Could not create debugfs "
3248 "'tracing_thresh' entry\n");
3249 entry = debugfs_create_file("README", 0644, d_tracer,
3250 NULL, &tracing_readme_fops);
3252 pr_warning("Could not create debugfs 'README' entry\n");
3254 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3255 NULL, &tracing_pipe_fops);
3257 pr_warning("Could not create debugfs "
3258 "'trace_pipe' entry\n");
3260 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3261 &global_trace, &tracing_entries_fops);
3263 pr_warning("Could not create debugfs "
3264 "'buffer_size_kb' entry\n");
3266 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3267 NULL, &tracing_mark_fops);
3269 pr_warning("Could not create debugfs "
3270 "'trace_marker' entry\n");
3272 #ifdef CONFIG_DYNAMIC_FTRACE
3273 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3274 &ftrace_update_tot_cnt,
3275 &tracing_dyn_info_fops);
3277 pr_warning("Could not create debugfs "
3278 "'dyn_ftrace_total_info' entry\n");
3280 #ifdef CONFIG_SYSPROF_TRACER
3281 init_tracer_sysprof_debugfs(d_tracer);
3286 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3288 static DEFINE_SPINLOCK(trace_buf_lock);
3289 static char trace_buf[TRACE_BUF_SIZE];
3291 struct ring_buffer_event *event;
3292 struct trace_array *tr = &global_trace;
3293 struct trace_array_cpu *data;
3294 int cpu, len = 0, size, pc;
3295 struct print_entry *entry;
3296 unsigned long irq_flags;
3298 if (tracing_disabled || tracing_selftest_running)
3301 pc = preempt_count();
3302 preempt_disable_notrace();
3303 cpu = raw_smp_processor_id();
3304 data = tr->data[cpu];
3306 if (unlikely(atomic_read(&data->disabled)))
3309 pause_graph_tracing();
3310 spin_lock_irqsave(&trace_buf_lock, irq_flags);
3311 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3313 len = min(len, TRACE_BUF_SIZE-1);
3316 size = sizeof(*entry) + len + 1;
3317 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3320 entry = ring_buffer_event_data(event);
3321 tracing_generic_entry_update(&entry->ent, irq_flags, pc);
3322 entry->ent.type = TRACE_PRINT;
3324 entry->depth = depth;
3326 memcpy(&entry->buf, trace_buf, len);
3327 entry->buf[len] = 0;
3328 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3331 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
3332 unpause_graph_tracing();
3334 preempt_enable_notrace();
3338 EXPORT_SYMBOL_GPL(trace_vprintk);
3340 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3345 if (!(trace_flags & TRACE_ITER_PRINTK))
3349 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3353 EXPORT_SYMBOL_GPL(__ftrace_printk);
3355 static int trace_panic_handler(struct notifier_block *this,
3356 unsigned long event, void *unused)
3358 if (ftrace_dump_on_oops)
3363 static struct notifier_block trace_panic_notifier = {
3364 .notifier_call = trace_panic_handler,
3366 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3369 static int trace_die_handler(struct notifier_block *self,
3375 if (ftrace_dump_on_oops)
3384 static struct notifier_block trace_die_notifier = {
3385 .notifier_call = trace_die_handler,
3390 * printk is set to max of 1024, we really don't need it that big.
3391 * Nothing should be printing 1000 characters anyway.
3393 #define TRACE_MAX_PRINT 1000
3396 * Define here KERN_TRACE so that we have one place to modify
3397 * it if we decide to change what log level the ftrace dump
3400 #define KERN_TRACE KERN_INFO
3403 trace_printk_seq(struct trace_seq *s)
3405 /* Probably should print a warning here. */
3409 /* should be zero ended, but we are paranoid. */
3410 s->buffer[s->len] = 0;
3412 printk(KERN_TRACE "%s", s->buffer);
3417 void ftrace_dump(void)
3419 static DEFINE_SPINLOCK(ftrace_dump_lock);
3420 /* use static because iter can be a bit big for the stack */
3421 static struct trace_iterator iter;
3422 static cpumask_t mask;
3423 static int dump_ran;
3424 unsigned long flags;
3428 spin_lock_irqsave(&ftrace_dump_lock, flags);
3434 /* No turning back! */
3437 for_each_tracing_cpu(cpu) {
3438 atomic_inc(&global_trace.data[cpu]->disabled);
3441 /* don't look at user memory in panic mode */
3442 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3444 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3446 iter.tr = &global_trace;
3447 iter.trace = current_trace;
3450 * We need to stop all tracing on all CPUS to read the
3451 * the next buffer. This is a bit expensive, but is
3452 * not done often. We fill all what we can read,
3453 * and then release the locks again.
3458 while (!trace_empty(&iter)) {
3461 printk(KERN_TRACE "---------------------------------\n");
3465 /* reset all but tr, trace, and overruns */
3466 memset(&iter.seq, 0,
3467 sizeof(struct trace_iterator) -
3468 offsetof(struct trace_iterator, seq));
3469 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3472 if (find_next_entry_inc(&iter) != NULL) {
3473 print_trace_line(&iter);
3474 trace_consume(&iter);
3477 trace_printk_seq(&iter.seq);
3481 printk(KERN_TRACE " (ftrace buffer empty)\n");
3483 printk(KERN_TRACE "---------------------------------\n");
3486 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3489 __init static int tracer_alloc_buffers(void)
3491 struct trace_array_cpu *data;
3494 /* TODO: make the number of buffers hot pluggable with CPUS */
3495 tracing_buffer_mask = cpu_possible_map;
3497 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3498 TRACE_BUFFER_FLAGS);
3499 if (!global_trace.buffer) {
3500 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3504 global_trace.entries = ring_buffer_size(global_trace.buffer);
3506 #ifdef CONFIG_TRACER_MAX_TRACE
3507 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3508 TRACE_BUFFER_FLAGS);
3509 if (!max_tr.buffer) {
3510 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3512 ring_buffer_free(global_trace.buffer);
3515 max_tr.entries = ring_buffer_size(max_tr.buffer);
3516 WARN_ON(max_tr.entries != global_trace.entries);
3519 /* Allocate the first page for all buffers */
3520 for_each_tracing_cpu(i) {
3521 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3522 max_tr.data[i] = &per_cpu(max_data, i);
3525 trace_init_cmdlines();
3527 register_tracer(&nop_trace);
3528 #ifdef CONFIG_BOOT_TRACER
3529 register_tracer(&boot_tracer);
3530 current_trace = &boot_tracer;
3531 current_trace->init(&global_trace);
3533 current_trace = &nop_trace;
3536 /* All seems OK, enable tracing */
3537 tracing_disabled = 0;
3539 atomic_notifier_chain_register(&panic_notifier_list,
3540 &trace_panic_notifier);
3542 register_die_notifier(&trace_die_notifier);
3546 early_initcall(tracer_alloc_buffers);
3547 fs_initcall(tracer_init_debugfs);