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/writeback.h>
35 #include <linux/stacktrace.h>
36 #include <linux/ring_buffer.h>
37 #include <linux/irqflags.h>
41 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
43 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
44 unsigned long __read_mostly tracing_thresh;
48 * Kill all tracing for good (never come back).
49 * It is initialized to 1 but will turn to zero if the initialization
50 * of the tracer is successful. But that is the only place that sets
53 int tracing_disabled = 1;
55 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
57 static inline void ftrace_disable_cpu(void)
60 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
63 static inline void ftrace_enable_cpu(void)
65 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
69 static cpumask_t __read_mostly tracing_buffer_mask;
71 #define for_each_tracing_cpu(cpu) \
72 for_each_cpu_mask(cpu, tracing_buffer_mask)
75 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
77 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
78 * is set, then ftrace_dump is called. This will output the contents
79 * of the ftrace buffers to the console. This is very useful for
80 * capturing traces that lead to crashes and outputing it to a
83 * It is default off, but you can enable it with either specifying
84 * "ftrace_dump_on_oops" in the kernel command line, or setting
85 * /proc/sys/kernel/ftrace_dump_on_oops to true.
87 int ftrace_dump_on_oops;
89 static int tracing_set_tracer(char *buf);
91 static int __init set_ftrace(char *str)
93 tracing_set_tracer(str);
96 __setup("ftrace", set_ftrace);
98 static int __init set_ftrace_dump_on_oops(char *str)
100 ftrace_dump_on_oops = 1;
103 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
106 ns2usecs(cycle_t nsec)
113 cycle_t ftrace_now(int cpu)
115 u64 ts = ring_buffer_time_stamp(cpu);
116 ring_buffer_normalize_time_stamp(cpu, &ts);
121 * The global_trace is the descriptor that holds the tracing
122 * buffers for the live tracing. For each CPU, it contains
123 * a link list of pages that will store trace entries. The
124 * page descriptor of the pages in the memory is used to hold
125 * the link list by linking the lru item in the page descriptor
126 * to each of the pages in the buffer per CPU.
128 * For each active CPU there is a data field that holds the
129 * pages for the buffer for that CPU. Each CPU has the same number
130 * of pages allocated for its buffer.
132 static struct trace_array global_trace;
134 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
137 * The max_tr is used to snapshot the global_trace when a maximum
138 * latency is reached. Some tracers will use this to store a maximum
139 * trace while it continues examining live traces.
141 * The buffers for the max_tr are set up the same as the global_trace.
142 * When a snapshot is taken, the link list of the max_tr is swapped
143 * with the link list of the global_trace and the buffers are reset for
144 * the global_trace so the tracing can continue.
146 static struct trace_array max_tr;
148 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
150 /* tracer_enabled is used to toggle activation of a tracer */
151 static int tracer_enabled = 1;
154 * tracing_is_enabled - return tracer_enabled status
156 * This function is used by other tracers to know the status
157 * of the tracer_enabled flag. Tracers may use this function
158 * to know if it should enable their features when starting
159 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
161 int tracing_is_enabled(void)
163 return tracer_enabled;
166 /* function tracing enabled */
167 int ftrace_function_enabled;
170 * trace_buf_size is the size in bytes that is allocated
171 * for a buffer. Note, the number of bytes is always rounded
174 * This number is purposely set to a low number of 16384.
175 * If the dump on oops happens, it will be much appreciated
176 * to not have to wait for all that output. Anyway this can be
177 * boot time and run time configurable.
179 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
181 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
183 /* trace_types holds a link list of available tracers. */
184 static struct tracer *trace_types __read_mostly;
186 /* current_trace points to the tracer that is currently active */
187 static struct tracer *current_trace __read_mostly;
190 * max_tracer_type_len is used to simplify the allocating of
191 * buffers to read userspace tracer names. We keep track of
192 * the longest tracer name registered.
194 static int max_tracer_type_len;
197 * trace_types_lock is used to protect the trace_types list.
198 * This lock is also used to keep user access serialized.
199 * Accesses from userspace will grab this lock while userspace
200 * activities happen inside the kernel.
202 static DEFINE_MUTEX(trace_types_lock);
204 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
205 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
207 /* trace_flags holds iter_ctrl options */
208 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK;
211 * trace_wake_up - wake up tasks waiting for trace input
213 * Simply wakes up any task that is blocked on the trace_wait
214 * queue. These is used with trace_poll for tasks polling the trace.
216 void trace_wake_up(void)
219 * The runqueue_is_locked() can fail, but this is the best we
222 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
223 wake_up(&trace_wait);
226 static int __init set_buf_size(char *str)
228 unsigned long buf_size;
233 ret = strict_strtoul(str, 0, &buf_size);
234 /* nr_entries can not be zero */
235 if (ret < 0 || buf_size == 0)
237 trace_buf_size = buf_size;
240 __setup("trace_buf_size=", set_buf_size);
242 unsigned long nsecs_to_usecs(unsigned long nsecs)
248 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
249 * control the output of kernel symbols.
251 #define TRACE_ITER_SYM_MASK \
252 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
254 /* These must match the bit postions in trace_iterator_flags */
255 static const char *trace_options[] = {
272 * ftrace_max_lock is used to protect the swapping of buffers
273 * when taking a max snapshot. The buffers themselves are
274 * protected by per_cpu spinlocks. But the action of the swap
275 * needs its own lock.
277 * This is defined as a raw_spinlock_t in order to help
278 * with performance when lockdep debugging is enabled.
280 static raw_spinlock_t ftrace_max_lock =
281 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
284 * Copy the new maximum trace into the separate maximum-trace
285 * structure. (this way the maximum trace is permanently saved,
286 * for later retrieval via /debugfs/tracing/latency_trace)
289 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
291 struct trace_array_cpu *data = tr->data[cpu];
294 max_tr.time_start = data->preempt_timestamp;
296 data = max_tr.data[cpu];
297 data->saved_latency = tracing_max_latency;
299 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
300 data->pid = tsk->pid;
301 data->uid = tsk->uid;
302 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
303 data->policy = tsk->policy;
304 data->rt_priority = tsk->rt_priority;
306 /* record this tasks comm */
307 tracing_record_cmdline(current);
311 * trace_seq_printf - sequence printing of trace information
312 * @s: trace sequence descriptor
313 * @fmt: printf format string
315 * The tracer may use either sequence operations or its own
316 * copy to user routines. To simplify formating of a trace
317 * trace_seq_printf is used to store strings into a special
318 * buffer (@s). Then the output may be either used by
319 * the sequencer or pulled into another buffer.
322 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
324 int len = (PAGE_SIZE - 1) - s->len;
332 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
335 /* If we can't write it all, don't bother writing anything */
345 * trace_seq_puts - trace sequence printing of simple string
346 * @s: trace sequence descriptor
347 * @str: simple string to record
349 * The tracer may use either the sequence operations or its own
350 * copy to user routines. This function records a simple string
351 * into a special buffer (@s) for later retrieval by a sequencer
352 * or other mechanism.
355 trace_seq_puts(struct trace_seq *s, const char *str)
357 int len = strlen(str);
359 if (len > ((PAGE_SIZE - 1) - s->len))
362 memcpy(s->buffer + s->len, str, len);
369 trace_seq_putc(struct trace_seq *s, unsigned char c)
371 if (s->len >= (PAGE_SIZE - 1))
374 s->buffer[s->len++] = c;
380 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
382 if (len > ((PAGE_SIZE - 1) - s->len))
385 memcpy(s->buffer + s->len, mem, len);
391 #define MAX_MEMHEX_BYTES 8
392 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
395 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
397 unsigned char hex[HEX_CHARS];
398 unsigned char *data = mem;
402 for (i = 0, j = 0; i < len; i++) {
404 for (i = len-1, j = 0; i >= 0; i--) {
406 hex[j++] = hex_asc_hi(data[i]);
407 hex[j++] = hex_asc_lo(data[i]);
411 return trace_seq_putmem(s, hex, j);
415 trace_seq_reset(struct trace_seq *s)
421 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
426 if (s->len <= s->readpos)
429 len = s->len - s->readpos;
432 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
441 trace_print_seq(struct seq_file *m, struct trace_seq *s)
443 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
446 seq_puts(m, s->buffer);
452 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
454 * @tsk: the task with the latency
455 * @cpu: The cpu that initiated the trace.
457 * Flip the buffers between the @tr and the max_tr and record information
458 * about which task was the cause of this latency.
461 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
463 struct ring_buffer *buf = tr->buffer;
465 WARN_ON_ONCE(!irqs_disabled());
466 __raw_spin_lock(&ftrace_max_lock);
468 tr->buffer = max_tr.buffer;
471 ftrace_disable_cpu();
472 ring_buffer_reset(tr->buffer);
475 __update_max_tr(tr, tsk, cpu);
476 __raw_spin_unlock(&ftrace_max_lock);
480 * update_max_tr_single - only copy one trace over, and reset the rest
482 * @tsk - task with the latency
483 * @cpu - the cpu of the buffer to copy.
485 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
488 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
492 WARN_ON_ONCE(!irqs_disabled());
493 __raw_spin_lock(&ftrace_max_lock);
495 ftrace_disable_cpu();
497 ring_buffer_reset(max_tr.buffer);
498 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
504 __update_max_tr(tr, tsk, cpu);
505 __raw_spin_unlock(&ftrace_max_lock);
509 * register_tracer - register a tracer with the ftrace system.
510 * @type - the plugin for the tracer
512 * Register a new plugin tracer.
514 int register_tracer(struct tracer *type)
521 pr_info("Tracer must have a name\n");
525 mutex_lock(&trace_types_lock);
526 for (t = trace_types; t; t = t->next) {
527 if (strcmp(type->name, t->name) == 0) {
529 pr_info("Trace %s already registered\n",
536 #ifdef CONFIG_FTRACE_STARTUP_TEST
537 if (type->selftest) {
538 struct tracer *saved_tracer = current_trace;
539 struct trace_array *tr = &global_trace;
540 int saved_ctrl = tr->ctrl;
543 * Run a selftest on this tracer.
544 * Here we reset the trace buffer, and set the current
545 * tracer to be this tracer. The tracer can then run some
546 * internal tracing to verify that everything is in order.
547 * If we fail, we do not register this tracer.
549 for_each_tracing_cpu(i) {
550 tracing_reset(tr, i);
552 current_trace = type;
554 /* the test is responsible for initializing and enabling */
555 pr_info("Testing tracer %s: ", type->name);
556 ret = type->selftest(type, tr);
557 /* the test is responsible for resetting too */
558 current_trace = saved_tracer;
559 tr->ctrl = saved_ctrl;
561 printk(KERN_CONT "FAILED!\n");
564 /* Only reset on passing, to avoid touching corrupted buffers */
565 for_each_tracing_cpu(i) {
566 tracing_reset(tr, i);
568 printk(KERN_CONT "PASSED\n");
572 type->next = trace_types;
574 len = strlen(type->name);
575 if (len > max_tracer_type_len)
576 max_tracer_type_len = len;
579 mutex_unlock(&trace_types_lock);
584 void unregister_tracer(struct tracer *type)
589 mutex_lock(&trace_types_lock);
590 for (t = &trace_types; *t; t = &(*t)->next) {
594 pr_info("Trace %s not registered\n", type->name);
599 if (strlen(type->name) != max_tracer_type_len)
602 max_tracer_type_len = 0;
603 for (t = &trace_types; *t; t = &(*t)->next) {
604 len = strlen((*t)->name);
605 if (len > max_tracer_type_len)
606 max_tracer_type_len = len;
609 mutex_unlock(&trace_types_lock);
612 void tracing_reset(struct trace_array *tr, int cpu)
614 ftrace_disable_cpu();
615 ring_buffer_reset_cpu(tr->buffer, cpu);
619 #define SAVED_CMDLINES 128
620 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
621 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
622 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
623 static int cmdline_idx;
624 static DEFINE_SPINLOCK(trace_cmdline_lock);
626 /* temporary disable recording */
627 atomic_t trace_record_cmdline_disabled __read_mostly;
629 static void trace_init_cmdlines(void)
631 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
632 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
636 static int trace_stop_count;
637 static DEFINE_SPINLOCK(tracing_start_lock);
640 * tracing_start - quick start of the tracer
642 * If tracing is enabled but was stopped by tracing_stop,
643 * this will start the tracer back up.
645 void tracing_start(void)
647 struct ring_buffer *buffer;
650 if (tracing_disabled)
653 spin_lock_irqsave(&tracing_start_lock, flags);
654 if (--trace_stop_count)
657 if (trace_stop_count < 0) {
658 /* Someone screwed up their debugging */
660 trace_stop_count = 0;
665 buffer = global_trace.buffer;
667 ring_buffer_record_enable(buffer);
669 buffer = max_tr.buffer;
671 ring_buffer_record_enable(buffer);
675 spin_unlock_irqrestore(&tracing_start_lock, flags);
679 * tracing_stop - quick stop of the tracer
681 * Light weight way to stop tracing. Use in conjunction with
684 void tracing_stop(void)
686 struct ring_buffer *buffer;
690 spin_lock_irqsave(&tracing_start_lock, flags);
691 if (trace_stop_count++)
694 buffer = global_trace.buffer;
696 ring_buffer_record_disable(buffer);
698 buffer = max_tr.buffer;
700 ring_buffer_record_disable(buffer);
703 spin_unlock_irqrestore(&tracing_start_lock, flags);
706 void trace_stop_cmdline_recording(void);
708 static void trace_save_cmdline(struct task_struct *tsk)
713 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
717 * It's not the end of the world if we don't get
718 * the lock, but we also don't want to spin
719 * nor do we want to disable interrupts,
720 * so if we miss here, then better luck next time.
722 if (!spin_trylock(&trace_cmdline_lock))
725 idx = map_pid_to_cmdline[tsk->pid];
726 if (idx >= SAVED_CMDLINES) {
727 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
729 map = map_cmdline_to_pid[idx];
730 if (map <= PID_MAX_DEFAULT)
731 map_pid_to_cmdline[map] = (unsigned)-1;
733 map_pid_to_cmdline[tsk->pid] = idx;
738 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
740 spin_unlock(&trace_cmdline_lock);
743 static char *trace_find_cmdline(int pid)
745 char *cmdline = "<...>";
751 if (pid > PID_MAX_DEFAULT)
754 map = map_pid_to_cmdline[pid];
755 if (map >= SAVED_CMDLINES)
758 cmdline = saved_cmdlines[map];
764 void tracing_record_cmdline(struct task_struct *tsk)
766 if (atomic_read(&trace_record_cmdline_disabled))
769 trace_save_cmdline(tsk);
773 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
776 struct task_struct *tsk = current;
778 entry->preempt_count = pc & 0xff;
779 entry->pid = (tsk) ? tsk->pid : 0;
781 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
782 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
784 TRACE_FLAG_IRQS_NOSUPPORT |
786 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
787 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
788 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
792 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
793 unsigned long ip, unsigned long parent_ip, unsigned long flags,
796 struct ring_buffer_event *event;
797 struct ftrace_entry *entry;
798 unsigned long irq_flags;
800 /* If we are reading the ring buffer, don't trace */
801 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
804 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
808 entry = ring_buffer_event_data(event);
809 tracing_generic_entry_update(&entry->ent, flags, pc);
810 entry->ent.type = TRACE_FN;
812 entry->parent_ip = parent_ip;
813 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
817 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
818 unsigned long ip, unsigned long parent_ip, unsigned long flags,
821 if (likely(!atomic_read(&data->disabled)))
822 trace_function(tr, data, ip, parent_ip, flags, pc);
825 static void ftrace_trace_stack(struct trace_array *tr,
826 struct trace_array_cpu *data,
830 #ifdef CONFIG_STACKTRACE
831 struct ring_buffer_event *event;
832 struct stack_entry *entry;
833 struct stack_trace trace;
834 unsigned long irq_flags;
836 if (!(trace_flags & TRACE_ITER_STACKTRACE))
839 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
843 entry = ring_buffer_event_data(event);
844 tracing_generic_entry_update(&entry->ent, flags, pc);
845 entry->ent.type = TRACE_STACK;
847 memset(&entry->caller, 0, sizeof(entry->caller));
849 trace.nr_entries = 0;
850 trace.max_entries = FTRACE_STACK_ENTRIES;
852 trace.entries = entry->caller;
854 save_stack_trace(&trace);
855 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
859 void __trace_stack(struct trace_array *tr,
860 struct trace_array_cpu *data,
864 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
868 ftrace_trace_special(void *__tr, void *__data,
869 unsigned long arg1, unsigned long arg2, unsigned long arg3,
872 struct ring_buffer_event *event;
873 struct trace_array_cpu *data = __data;
874 struct trace_array *tr = __tr;
875 struct special_entry *entry;
876 unsigned long irq_flags;
878 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
882 entry = ring_buffer_event_data(event);
883 tracing_generic_entry_update(&entry->ent, 0, pc);
884 entry->ent.type = TRACE_SPECIAL;
888 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
889 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
895 __trace_special(void *__tr, void *__data,
896 unsigned long arg1, unsigned long arg2, unsigned long arg3)
898 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
902 tracing_sched_switch_trace(struct trace_array *tr,
903 struct trace_array_cpu *data,
904 struct task_struct *prev,
905 struct task_struct *next,
906 unsigned long flags, int pc)
908 struct ring_buffer_event *event;
909 struct ctx_switch_entry *entry;
910 unsigned long irq_flags;
912 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
916 entry = ring_buffer_event_data(event);
917 tracing_generic_entry_update(&entry->ent, flags, pc);
918 entry->ent.type = TRACE_CTX;
919 entry->prev_pid = prev->pid;
920 entry->prev_prio = prev->prio;
921 entry->prev_state = prev->state;
922 entry->next_pid = next->pid;
923 entry->next_prio = next->prio;
924 entry->next_state = next->state;
925 entry->next_cpu = task_cpu(next);
926 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
927 ftrace_trace_stack(tr, data, flags, 5, pc);
931 tracing_sched_wakeup_trace(struct trace_array *tr,
932 struct trace_array_cpu *data,
933 struct task_struct *wakee,
934 struct task_struct *curr,
935 unsigned long flags, int pc)
937 struct ring_buffer_event *event;
938 struct ctx_switch_entry *entry;
939 unsigned long irq_flags;
941 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
945 entry = ring_buffer_event_data(event);
946 tracing_generic_entry_update(&entry->ent, flags, pc);
947 entry->ent.type = TRACE_WAKE;
948 entry->prev_pid = curr->pid;
949 entry->prev_prio = curr->prio;
950 entry->prev_state = curr->state;
951 entry->next_pid = wakee->pid;
952 entry->next_prio = wakee->prio;
953 entry->next_state = wakee->state;
954 entry->next_cpu = task_cpu(wakee);
955 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
956 ftrace_trace_stack(tr, data, flags, 6, pc);
962 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
964 struct trace_array *tr = &global_trace;
965 struct trace_array_cpu *data;
969 if (tracing_disabled || !tr->ctrl)
972 pc = preempt_count();
973 preempt_disable_notrace();
974 cpu = raw_smp_processor_id();
975 data = tr->data[cpu];
977 if (likely(!atomic_read(&data->disabled)))
978 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
980 preempt_enable_notrace();
983 #ifdef CONFIG_FUNCTION_TRACER
985 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
987 struct trace_array *tr = &global_trace;
988 struct trace_array_cpu *data;
994 if (unlikely(!ftrace_function_enabled))
997 pc = preempt_count();
998 resched = ftrace_preempt_disable();
999 local_save_flags(flags);
1000 cpu = raw_smp_processor_id();
1001 data = tr->data[cpu];
1002 disabled = atomic_inc_return(&data->disabled);
1004 if (likely(disabled == 1))
1005 trace_function(tr, data, ip, parent_ip, flags, pc);
1007 atomic_dec(&data->disabled);
1008 ftrace_preempt_enable(resched);
1012 function_trace_call(unsigned long ip, unsigned long parent_ip)
1014 struct trace_array *tr = &global_trace;
1015 struct trace_array_cpu *data;
1016 unsigned long flags;
1021 if (unlikely(!ftrace_function_enabled))
1025 * Need to use raw, since this must be called before the
1026 * recursive protection is performed.
1028 raw_local_irq_save(flags);
1029 cpu = raw_smp_processor_id();
1030 data = tr->data[cpu];
1031 disabled = atomic_inc_return(&data->disabled);
1033 if (likely(disabled == 1)) {
1034 pc = preempt_count();
1035 trace_function(tr, data, ip, parent_ip, flags, pc);
1038 atomic_dec(&data->disabled);
1039 raw_local_irq_restore(flags);
1042 static struct ftrace_ops trace_ops __read_mostly =
1044 .func = function_trace_call,
1047 void tracing_start_function_trace(void)
1049 ftrace_function_enabled = 0;
1051 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1052 trace_ops.func = function_trace_call_preempt_only;
1054 trace_ops.func = function_trace_call;
1056 register_ftrace_function(&trace_ops);
1057 ftrace_function_enabled = 1;
1060 void tracing_stop_function_trace(void)
1062 ftrace_function_enabled = 0;
1063 unregister_ftrace_function(&trace_ops);
1067 enum trace_file_type {
1068 TRACE_FILE_LAT_FMT = 1,
1071 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1073 /* Don't allow ftrace to trace into the ring buffers */
1074 ftrace_disable_cpu();
1077 if (iter->buffer_iter[iter->cpu])
1078 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1080 ftrace_enable_cpu();
1083 static struct trace_entry *
1084 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1086 struct ring_buffer_event *event;
1087 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1089 /* Don't allow ftrace to trace into the ring buffers */
1090 ftrace_disable_cpu();
1093 event = ring_buffer_iter_peek(buf_iter, ts);
1095 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1097 ftrace_enable_cpu();
1099 return event ? ring_buffer_event_data(event) : NULL;
1102 static struct trace_entry *
1103 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1105 struct ring_buffer *buffer = iter->tr->buffer;
1106 struct trace_entry *ent, *next = NULL;
1107 u64 next_ts = 0, ts;
1111 for_each_tracing_cpu(cpu) {
1113 if (ring_buffer_empty_cpu(buffer, cpu))
1116 ent = peek_next_entry(iter, cpu, &ts);
1119 * Pick the entry with the smallest timestamp:
1121 if (ent && (!next || ts < next_ts)) {
1129 *ent_cpu = next_cpu;
1137 /* Find the next real entry, without updating the iterator itself */
1138 static struct trace_entry *
1139 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1141 return __find_next_entry(iter, ent_cpu, ent_ts);
1144 /* Find the next real entry, and increment the iterator to the next entry */
1145 static void *find_next_entry_inc(struct trace_iterator *iter)
1147 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1150 trace_iterator_increment(iter, iter->cpu);
1152 return iter->ent ? iter : NULL;
1155 static void trace_consume(struct trace_iterator *iter)
1157 /* Don't allow ftrace to trace into the ring buffers */
1158 ftrace_disable_cpu();
1159 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1160 ftrace_enable_cpu();
1163 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1165 struct trace_iterator *iter = m->private;
1171 /* can't go backwards */
1176 ent = find_next_entry_inc(iter);
1180 while (ent && iter->idx < i)
1181 ent = find_next_entry_inc(iter);
1188 static void *s_start(struct seq_file *m, loff_t *pos)
1190 struct trace_iterator *iter = m->private;
1195 mutex_lock(&trace_types_lock);
1197 if (!current_trace || current_trace != iter->trace) {
1198 mutex_unlock(&trace_types_lock);
1202 atomic_inc(&trace_record_cmdline_disabled);
1204 if (*pos != iter->pos) {
1209 ftrace_disable_cpu();
1211 for_each_tracing_cpu(cpu) {
1212 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1215 ftrace_enable_cpu();
1217 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1222 p = s_next(m, p, &l);
1228 static void s_stop(struct seq_file *m, void *p)
1230 atomic_dec(&trace_record_cmdline_disabled);
1231 mutex_unlock(&trace_types_lock);
1234 #ifdef CONFIG_KRETPROBES
1235 static inline const char *kretprobed(const char *name)
1237 static const char tramp_name[] = "kretprobe_trampoline";
1238 int size = sizeof(tramp_name);
1240 if (strncmp(tramp_name, name, size) == 0)
1241 return "[unknown/kretprobe'd]";
1245 static inline const char *kretprobed(const char *name)
1249 #endif /* CONFIG_KRETPROBES */
1252 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1254 #ifdef CONFIG_KALLSYMS
1255 char str[KSYM_SYMBOL_LEN];
1258 kallsyms_lookup(address, NULL, NULL, NULL, str);
1260 name = kretprobed(str);
1262 return trace_seq_printf(s, fmt, name);
1268 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1269 unsigned long address)
1271 #ifdef CONFIG_KALLSYMS
1272 char str[KSYM_SYMBOL_LEN];
1275 sprint_symbol(str, address);
1276 name = kretprobed(str);
1278 return trace_seq_printf(s, fmt, name);
1283 #ifndef CONFIG_64BIT
1284 # define IP_FMT "%08lx"
1286 # define IP_FMT "%016lx"
1290 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1295 return trace_seq_printf(s, "0");
1297 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1298 ret = seq_print_sym_offset(s, "%s", ip);
1300 ret = seq_print_sym_short(s, "%s", ip);
1305 if (sym_flags & TRACE_ITER_SYM_ADDR)
1306 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1310 static void print_lat_help_header(struct seq_file *m)
1312 seq_puts(m, "# _------=> CPU# \n");
1313 seq_puts(m, "# / _-----=> irqs-off \n");
1314 seq_puts(m, "# | / _----=> need-resched \n");
1315 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1316 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1317 seq_puts(m, "# |||| / \n");
1318 seq_puts(m, "# ||||| delay \n");
1319 seq_puts(m, "# cmd pid ||||| time | caller \n");
1320 seq_puts(m, "# \\ / ||||| \\ | / \n");
1323 static void print_func_help_header(struct seq_file *m)
1325 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1326 seq_puts(m, "# | | | | |\n");
1331 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1333 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1334 struct trace_array *tr = iter->tr;
1335 struct trace_array_cpu *data = tr->data[tr->cpu];
1336 struct tracer *type = current_trace;
1337 unsigned long total;
1338 unsigned long entries;
1339 const char *name = "preemption";
1344 entries = ring_buffer_entries(iter->tr->buffer);
1346 ring_buffer_overruns(iter->tr->buffer);
1348 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1350 seq_puts(m, "-----------------------------------"
1351 "---------------------------------\n");
1352 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1353 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1354 nsecs_to_usecs(data->saved_latency),
1358 #if defined(CONFIG_PREEMPT_NONE)
1360 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1362 #elif defined(CONFIG_PREEMPT)
1367 /* These are reserved for later use */
1370 seq_printf(m, " #P:%d)\n", num_online_cpus());
1374 seq_puts(m, " -----------------\n");
1375 seq_printf(m, " | task: %.16s-%d "
1376 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1377 data->comm, data->pid, data->uid, data->nice,
1378 data->policy, data->rt_priority);
1379 seq_puts(m, " -----------------\n");
1381 if (data->critical_start) {
1382 seq_puts(m, " => started at: ");
1383 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1384 trace_print_seq(m, &iter->seq);
1385 seq_puts(m, "\n => ended at: ");
1386 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1387 trace_print_seq(m, &iter->seq);
1395 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1397 int hardirq, softirq;
1400 comm = trace_find_cmdline(entry->pid);
1402 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1403 trace_seq_printf(s, "%3d", cpu);
1404 trace_seq_printf(s, "%c%c",
1405 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1406 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1407 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1409 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1410 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1411 if (hardirq && softirq) {
1412 trace_seq_putc(s, 'H');
1415 trace_seq_putc(s, 'h');
1418 trace_seq_putc(s, 's');
1420 trace_seq_putc(s, '.');
1424 if (entry->preempt_count)
1425 trace_seq_printf(s, "%x", entry->preempt_count);
1427 trace_seq_puts(s, ".");
1430 unsigned long preempt_mark_thresh = 100;
1433 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1434 unsigned long rel_usecs)
1436 trace_seq_printf(s, " %4lldus", abs_usecs);
1437 if (rel_usecs > preempt_mark_thresh)
1438 trace_seq_puts(s, "!: ");
1439 else if (rel_usecs > 1)
1440 trace_seq_puts(s, "+: ");
1442 trace_seq_puts(s, " : ");
1445 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1448 * The message is supposed to contain an ending newline.
1449 * If the printing stops prematurely, try to add a newline of our own.
1451 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1453 struct trace_entry *ent;
1454 struct trace_field_cont *cont;
1457 ent = peek_next_entry(iter, iter->cpu, NULL);
1458 if (!ent || ent->type != TRACE_CONT) {
1459 trace_seq_putc(s, '\n');
1464 cont = (struct trace_field_cont *)ent;
1466 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1468 ftrace_disable_cpu();
1470 if (iter->buffer_iter[iter->cpu])
1471 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1473 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1475 ftrace_enable_cpu();
1477 ent = peek_next_entry(iter, iter->cpu, NULL);
1478 } while (ent && ent->type == TRACE_CONT);
1481 trace_seq_putc(s, '\n');
1484 static enum print_line_t
1485 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1487 struct trace_seq *s = &iter->seq;
1488 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1489 struct trace_entry *next_entry;
1490 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1491 struct trace_entry *entry = iter->ent;
1492 unsigned long abs_usecs;
1493 unsigned long rel_usecs;
1500 if (entry->type == TRACE_CONT)
1501 return TRACE_TYPE_HANDLED;
1503 next_entry = find_next_entry(iter, NULL, &next_ts);
1506 rel_usecs = ns2usecs(next_ts - iter->ts);
1507 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1510 comm = trace_find_cmdline(entry->pid);
1511 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1512 " %ld.%03ldms (+%ld.%03ldms): ",
1514 entry->pid, cpu, entry->flags,
1515 entry->preempt_count, trace_idx,
1518 abs_usecs % 1000, rel_usecs/1000,
1521 lat_print_generic(s, entry, cpu);
1522 lat_print_timestamp(s, abs_usecs, rel_usecs);
1524 switch (entry->type) {
1526 struct ftrace_entry *field;
1528 trace_assign_type(field, entry);
1530 seq_print_ip_sym(s, field->ip, sym_flags);
1531 trace_seq_puts(s, " (");
1532 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1533 trace_seq_puts(s, ")\n");
1538 struct ctx_switch_entry *field;
1540 trace_assign_type(field, entry);
1542 T = field->next_state < sizeof(state_to_char) ?
1543 state_to_char[field->next_state] : 'X';
1545 state = field->prev_state ?
1546 __ffs(field->prev_state) + 1 : 0;
1547 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1548 comm = trace_find_cmdline(field->next_pid);
1549 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1552 S, entry->type == TRACE_CTX ? "==>" : " +",
1559 case TRACE_SPECIAL: {
1560 struct special_entry *field;
1562 trace_assign_type(field, entry);
1564 trace_seq_printf(s, "# %ld %ld %ld\n",
1571 struct stack_entry *field;
1573 trace_assign_type(field, entry);
1575 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1577 trace_seq_puts(s, " <= ");
1578 seq_print_ip_sym(s, field->caller[i], sym_flags);
1580 trace_seq_puts(s, "\n");
1584 struct print_entry *field;
1586 trace_assign_type(field, entry);
1588 seq_print_ip_sym(s, field->ip, sym_flags);
1589 trace_seq_printf(s, ": %s", field->buf);
1590 if (entry->flags & TRACE_FLAG_CONT)
1591 trace_seq_print_cont(s, iter);
1595 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1597 return TRACE_TYPE_HANDLED;
1600 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1602 struct trace_seq *s = &iter->seq;
1603 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1604 struct trace_entry *entry;
1605 unsigned long usec_rem;
1606 unsigned long long t;
1615 if (entry->type == TRACE_CONT)
1616 return TRACE_TYPE_HANDLED;
1618 comm = trace_find_cmdline(iter->ent->pid);
1620 t = ns2usecs(iter->ts);
1621 usec_rem = do_div(t, 1000000ULL);
1622 secs = (unsigned long)t;
1624 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1626 return TRACE_TYPE_PARTIAL_LINE;
1627 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1629 return TRACE_TYPE_PARTIAL_LINE;
1630 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1632 return TRACE_TYPE_PARTIAL_LINE;
1634 switch (entry->type) {
1636 struct ftrace_entry *field;
1638 trace_assign_type(field, entry);
1640 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1642 return TRACE_TYPE_PARTIAL_LINE;
1643 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1645 ret = trace_seq_printf(s, " <-");
1647 return TRACE_TYPE_PARTIAL_LINE;
1648 ret = seq_print_ip_sym(s,
1652 return TRACE_TYPE_PARTIAL_LINE;
1654 ret = trace_seq_printf(s, "\n");
1656 return TRACE_TYPE_PARTIAL_LINE;
1661 struct ctx_switch_entry *field;
1663 trace_assign_type(field, entry);
1665 S = field->prev_state < sizeof(state_to_char) ?
1666 state_to_char[field->prev_state] : 'X';
1667 T = field->next_state < sizeof(state_to_char) ?
1668 state_to_char[field->next_state] : 'X';
1669 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1673 entry->type == TRACE_CTX ? "==>" : " +",
1679 return TRACE_TYPE_PARTIAL_LINE;
1682 case TRACE_SPECIAL: {
1683 struct special_entry *field;
1685 trace_assign_type(field, entry);
1687 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1692 return TRACE_TYPE_PARTIAL_LINE;
1696 struct stack_entry *field;
1698 trace_assign_type(field, entry);
1700 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1702 ret = trace_seq_puts(s, " <= ");
1704 return TRACE_TYPE_PARTIAL_LINE;
1706 ret = seq_print_ip_sym(s, field->caller[i],
1709 return TRACE_TYPE_PARTIAL_LINE;
1711 ret = trace_seq_puts(s, "\n");
1713 return TRACE_TYPE_PARTIAL_LINE;
1717 struct print_entry *field;
1719 trace_assign_type(field, entry);
1721 seq_print_ip_sym(s, field->ip, sym_flags);
1722 trace_seq_printf(s, ": %s", field->buf);
1723 if (entry->flags & TRACE_FLAG_CONT)
1724 trace_seq_print_cont(s, iter);
1728 return TRACE_TYPE_HANDLED;
1731 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1733 struct trace_seq *s = &iter->seq;
1734 struct trace_entry *entry;
1740 if (entry->type == TRACE_CONT)
1741 return TRACE_TYPE_HANDLED;
1743 ret = trace_seq_printf(s, "%d %d %llu ",
1744 entry->pid, iter->cpu, iter->ts);
1746 return TRACE_TYPE_PARTIAL_LINE;
1748 switch (entry->type) {
1750 struct ftrace_entry *field;
1752 trace_assign_type(field, entry);
1754 ret = trace_seq_printf(s, "%x %x\n",
1758 return TRACE_TYPE_PARTIAL_LINE;
1763 struct ctx_switch_entry *field;
1765 trace_assign_type(field, entry);
1767 S = field->prev_state < sizeof(state_to_char) ?
1768 state_to_char[field->prev_state] : 'X';
1769 T = field->next_state < sizeof(state_to_char) ?
1770 state_to_char[field->next_state] : 'X';
1771 if (entry->type == TRACE_WAKE)
1773 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1782 return TRACE_TYPE_PARTIAL_LINE;
1787 struct special_entry *field;
1789 trace_assign_type(field, entry);
1791 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1796 return TRACE_TYPE_PARTIAL_LINE;
1800 struct print_entry *field;
1802 trace_assign_type(field, entry);
1804 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1805 if (entry->flags & TRACE_FLAG_CONT)
1806 trace_seq_print_cont(s, iter);
1810 return TRACE_TYPE_HANDLED;
1813 #define SEQ_PUT_FIELD_RET(s, x) \
1815 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1819 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1821 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1822 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1826 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1828 struct trace_seq *s = &iter->seq;
1829 unsigned char newline = '\n';
1830 struct trace_entry *entry;
1835 if (entry->type == TRACE_CONT)
1836 return TRACE_TYPE_HANDLED;
1838 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1839 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1840 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1842 switch (entry->type) {
1844 struct ftrace_entry *field;
1846 trace_assign_type(field, entry);
1848 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1849 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1854 struct ctx_switch_entry *field;
1856 trace_assign_type(field, entry);
1858 S = field->prev_state < sizeof(state_to_char) ?
1859 state_to_char[field->prev_state] : 'X';
1860 T = field->next_state < sizeof(state_to_char) ?
1861 state_to_char[field->next_state] : 'X';
1862 if (entry->type == TRACE_WAKE)
1864 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1865 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1866 SEQ_PUT_HEX_FIELD_RET(s, S);
1867 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1868 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1869 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1870 SEQ_PUT_HEX_FIELD_RET(s, T);
1875 struct special_entry *field;
1877 trace_assign_type(field, entry);
1879 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1880 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1881 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1885 SEQ_PUT_FIELD_RET(s, newline);
1887 return TRACE_TYPE_HANDLED;
1890 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1892 struct trace_seq *s = &iter->seq;
1893 struct trace_entry *entry;
1897 if (entry->type == TRACE_CONT)
1898 return TRACE_TYPE_HANDLED;
1900 SEQ_PUT_FIELD_RET(s, entry->pid);
1901 SEQ_PUT_FIELD_RET(s, entry->cpu);
1902 SEQ_PUT_FIELD_RET(s, iter->ts);
1904 switch (entry->type) {
1906 struct ftrace_entry *field;
1908 trace_assign_type(field, entry);
1910 SEQ_PUT_FIELD_RET(s, field->ip);
1911 SEQ_PUT_FIELD_RET(s, field->parent_ip);
1915 struct ctx_switch_entry *field;
1917 trace_assign_type(field, entry);
1919 SEQ_PUT_FIELD_RET(s, field->prev_pid);
1920 SEQ_PUT_FIELD_RET(s, field->prev_prio);
1921 SEQ_PUT_FIELD_RET(s, field->prev_state);
1922 SEQ_PUT_FIELD_RET(s, field->next_pid);
1923 SEQ_PUT_FIELD_RET(s, field->next_prio);
1924 SEQ_PUT_FIELD_RET(s, field->next_state);
1929 struct special_entry *field;
1931 trace_assign_type(field, entry);
1933 SEQ_PUT_FIELD_RET(s, field->arg1);
1934 SEQ_PUT_FIELD_RET(s, field->arg2);
1935 SEQ_PUT_FIELD_RET(s, field->arg3);
1942 static int trace_empty(struct trace_iterator *iter)
1946 for_each_tracing_cpu(cpu) {
1947 if (iter->buffer_iter[cpu]) {
1948 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1951 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1959 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1961 enum print_line_t ret;
1963 if (iter->trace && iter->trace->print_line) {
1964 ret = iter->trace->print_line(iter);
1965 if (ret != TRACE_TYPE_UNHANDLED)
1969 if (trace_flags & TRACE_ITER_BIN)
1970 return print_bin_fmt(iter);
1972 if (trace_flags & TRACE_ITER_HEX)
1973 return print_hex_fmt(iter);
1975 if (trace_flags & TRACE_ITER_RAW)
1976 return print_raw_fmt(iter);
1978 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1979 return print_lat_fmt(iter, iter->idx, iter->cpu);
1981 return print_trace_fmt(iter);
1984 static int s_show(struct seq_file *m, void *v)
1986 struct trace_iterator *iter = v;
1988 if (iter->ent == NULL) {
1990 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1993 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1994 /* print nothing if the buffers are empty */
1995 if (trace_empty(iter))
1997 print_trace_header(m, iter);
1998 if (!(trace_flags & TRACE_ITER_VERBOSE))
1999 print_lat_help_header(m);
2001 if (!(trace_flags & TRACE_ITER_VERBOSE))
2002 print_func_help_header(m);
2005 print_trace_line(iter);
2006 trace_print_seq(m, &iter->seq);
2012 static struct seq_operations tracer_seq_ops = {
2019 static struct trace_iterator *
2020 __tracing_open(struct inode *inode, struct file *file, int *ret)
2022 struct trace_iterator *iter;
2026 if (tracing_disabled) {
2031 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2037 mutex_lock(&trace_types_lock);
2038 if (current_trace && current_trace->print_max)
2041 iter->tr = inode->i_private;
2042 iter->trace = current_trace;
2045 for_each_tracing_cpu(cpu) {
2047 iter->buffer_iter[cpu] =
2048 ring_buffer_read_start(iter->tr->buffer, cpu);
2050 if (!iter->buffer_iter[cpu])
2054 /* TODO stop tracer */
2055 *ret = seq_open(file, &tracer_seq_ops);
2059 m = file->private_data;
2062 /* stop the trace while dumping */
2065 if (iter->trace && iter->trace->open)
2066 iter->trace->open(iter);
2068 mutex_unlock(&trace_types_lock);
2074 for_each_tracing_cpu(cpu) {
2075 if (iter->buffer_iter[cpu])
2076 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2078 mutex_unlock(&trace_types_lock);
2080 return ERR_PTR(-ENOMEM);
2083 int tracing_open_generic(struct inode *inode, struct file *filp)
2085 if (tracing_disabled)
2088 filp->private_data = inode->i_private;
2092 int tracing_release(struct inode *inode, struct file *file)
2094 struct seq_file *m = (struct seq_file *)file->private_data;
2095 struct trace_iterator *iter = m->private;
2098 mutex_lock(&trace_types_lock);
2099 for_each_tracing_cpu(cpu) {
2100 if (iter->buffer_iter[cpu])
2101 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2104 if (iter->trace && iter->trace->close)
2105 iter->trace->close(iter);
2107 /* reenable tracing if it was previously enabled */
2109 mutex_unlock(&trace_types_lock);
2111 seq_release(inode, file);
2116 static int tracing_open(struct inode *inode, struct file *file)
2120 __tracing_open(inode, file, &ret);
2125 static int tracing_lt_open(struct inode *inode, struct file *file)
2127 struct trace_iterator *iter;
2130 iter = __tracing_open(inode, file, &ret);
2133 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2140 t_next(struct seq_file *m, void *v, loff_t *pos)
2142 struct tracer *t = m->private;
2154 static void *t_start(struct seq_file *m, loff_t *pos)
2156 struct tracer *t = m->private;
2159 mutex_lock(&trace_types_lock);
2160 for (; t && l < *pos; t = t_next(m, t, &l))
2166 static void t_stop(struct seq_file *m, void *p)
2168 mutex_unlock(&trace_types_lock);
2171 static int t_show(struct seq_file *m, void *v)
2173 struct tracer *t = v;
2178 seq_printf(m, "%s", t->name);
2187 static struct seq_operations show_traces_seq_ops = {
2194 static int show_traces_open(struct inode *inode, struct file *file)
2198 if (tracing_disabled)
2201 ret = seq_open(file, &show_traces_seq_ops);
2203 struct seq_file *m = file->private_data;
2204 m->private = trace_types;
2210 static struct file_operations tracing_fops = {
2211 .open = tracing_open,
2213 .llseek = seq_lseek,
2214 .release = tracing_release,
2217 static struct file_operations tracing_lt_fops = {
2218 .open = tracing_lt_open,
2220 .llseek = seq_lseek,
2221 .release = tracing_release,
2224 static struct file_operations show_traces_fops = {
2225 .open = show_traces_open,
2227 .release = seq_release,
2231 * Only trace on a CPU if the bitmask is set:
2233 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2236 * When tracing/tracing_cpu_mask is modified then this holds
2237 * the new bitmask we are about to install:
2239 static cpumask_t tracing_cpumask_new;
2242 * The tracer itself will not take this lock, but still we want
2243 * to provide a consistent cpumask to user-space:
2245 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2248 * Temporary storage for the character representation of the
2249 * CPU bitmask (and one more byte for the newline):
2251 static char mask_str[NR_CPUS + 1];
2254 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2255 size_t count, loff_t *ppos)
2259 mutex_lock(&tracing_cpumask_update_lock);
2261 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2262 if (count - len < 2) {
2266 len += sprintf(mask_str + len, "\n");
2267 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2270 mutex_unlock(&tracing_cpumask_update_lock);
2276 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2277 size_t count, loff_t *ppos)
2281 mutex_lock(&tracing_cpumask_update_lock);
2282 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2286 raw_local_irq_disable();
2287 __raw_spin_lock(&ftrace_max_lock);
2288 for_each_tracing_cpu(cpu) {
2290 * Increase/decrease the disabled counter if we are
2291 * about to flip a bit in the cpumask:
2293 if (cpu_isset(cpu, tracing_cpumask) &&
2294 !cpu_isset(cpu, tracing_cpumask_new)) {
2295 atomic_inc(&global_trace.data[cpu]->disabled);
2297 if (!cpu_isset(cpu, tracing_cpumask) &&
2298 cpu_isset(cpu, tracing_cpumask_new)) {
2299 atomic_dec(&global_trace.data[cpu]->disabled);
2302 __raw_spin_unlock(&ftrace_max_lock);
2303 raw_local_irq_enable();
2305 tracing_cpumask = tracing_cpumask_new;
2307 mutex_unlock(&tracing_cpumask_update_lock);
2312 mutex_unlock(&tracing_cpumask_update_lock);
2317 static struct file_operations tracing_cpumask_fops = {
2318 .open = tracing_open_generic,
2319 .read = tracing_cpumask_read,
2320 .write = tracing_cpumask_write,
2324 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2325 size_t cnt, loff_t *ppos)
2332 /* calulate max size */
2333 for (i = 0; trace_options[i]; i++) {
2334 len += strlen(trace_options[i]);
2335 len += 3; /* "no" and space */
2338 /* +2 for \n and \0 */
2339 buf = kmalloc(len + 2, GFP_KERNEL);
2343 for (i = 0; trace_options[i]; i++) {
2344 if (trace_flags & (1 << i))
2345 r += sprintf(buf + r, "%s ", trace_options[i]);
2347 r += sprintf(buf + r, "no%s ", trace_options[i]);
2350 r += sprintf(buf + r, "\n");
2351 WARN_ON(r >= len + 2);
2353 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2361 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2362 size_t cnt, loff_t *ppos)
2369 if (cnt >= sizeof(buf))
2372 if (copy_from_user(&buf, ubuf, cnt))
2377 if (strncmp(buf, "no", 2) == 0) {
2382 for (i = 0; trace_options[i]; i++) {
2383 int len = strlen(trace_options[i]);
2385 if (strncmp(cmp, trace_options[i], len) == 0) {
2387 trace_flags &= ~(1 << i);
2389 trace_flags |= (1 << i);
2394 * If no option could be set, return an error:
2396 if (!trace_options[i])
2404 static struct file_operations tracing_iter_fops = {
2405 .open = tracing_open_generic,
2406 .read = tracing_iter_ctrl_read,
2407 .write = tracing_iter_ctrl_write,
2410 static const char readme_msg[] =
2411 "tracing mini-HOWTO:\n\n"
2413 "# mount -t debugfs nodev /debug\n\n"
2414 "# cat /debug/tracing/available_tracers\n"
2415 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2416 "# cat /debug/tracing/current_tracer\n"
2418 "# echo sched_switch > /debug/tracing/current_tracer\n"
2419 "# cat /debug/tracing/current_tracer\n"
2421 "# cat /debug/tracing/iter_ctrl\n"
2422 "noprint-parent nosym-offset nosym-addr noverbose\n"
2423 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2424 "# echo 1 > /debug/tracing/tracing_enabled\n"
2425 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2426 "echo 0 > /debug/tracing/tracing_enabled\n"
2430 tracing_readme_read(struct file *filp, char __user *ubuf,
2431 size_t cnt, loff_t *ppos)
2433 return simple_read_from_buffer(ubuf, cnt, ppos,
2434 readme_msg, strlen(readme_msg));
2437 static struct file_operations tracing_readme_fops = {
2438 .open = tracing_open_generic,
2439 .read = tracing_readme_read,
2443 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2444 size_t cnt, loff_t *ppos)
2449 r = sprintf(buf, "%u\n", tracer_enabled);
2450 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2454 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2455 size_t cnt, loff_t *ppos)
2457 struct trace_array *tr = filp->private_data;
2462 if (cnt >= sizeof(buf))
2465 if (copy_from_user(&buf, ubuf, cnt))
2470 ret = strict_strtoul(buf, 10, &val);
2476 mutex_lock(&trace_types_lock);
2477 if (tracer_enabled ^ val) {
2480 if (current_trace->start)
2481 current_trace->start(tr);
2486 if (current_trace->stop)
2487 current_trace->stop(tr);
2490 mutex_unlock(&trace_types_lock);
2498 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2499 size_t cnt, loff_t *ppos)
2501 char buf[max_tracer_type_len+2];
2504 mutex_lock(&trace_types_lock);
2506 r = sprintf(buf, "%s\n", current_trace->name);
2508 r = sprintf(buf, "\n");
2509 mutex_unlock(&trace_types_lock);
2511 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2514 static int tracing_set_tracer(char *buf)
2516 struct trace_array *tr = &global_trace;
2520 mutex_lock(&trace_types_lock);
2521 for (t = trace_types; t; t = t->next) {
2522 if (strcmp(t->name, buf) == 0)
2529 if (t == current_trace)
2532 if (current_trace && current_trace->reset)
2533 current_trace->reset(tr);
2540 mutex_unlock(&trace_types_lock);
2546 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2547 size_t cnt, loff_t *ppos)
2549 char buf[max_tracer_type_len+1];
2553 if (cnt > max_tracer_type_len)
2554 cnt = max_tracer_type_len;
2556 if (copy_from_user(&buf, ubuf, cnt))
2561 /* strip ending whitespace. */
2562 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2565 ret = tracing_set_tracer(buf);
2576 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2577 size_t cnt, loff_t *ppos)
2579 unsigned long *ptr = filp->private_data;
2583 r = snprintf(buf, sizeof(buf), "%ld\n",
2584 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2585 if (r > sizeof(buf))
2587 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2591 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2592 size_t cnt, loff_t *ppos)
2594 long *ptr = filp->private_data;
2599 if (cnt >= sizeof(buf))
2602 if (copy_from_user(&buf, ubuf, cnt))
2607 ret = strict_strtoul(buf, 10, &val);
2616 static atomic_t tracing_reader;
2618 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2620 struct trace_iterator *iter;
2622 if (tracing_disabled)
2625 /* We only allow for reader of the pipe */
2626 if (atomic_inc_return(&tracing_reader) != 1) {
2627 atomic_dec(&tracing_reader);
2631 /* create a buffer to store the information to pass to userspace */
2632 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2636 mutex_lock(&trace_types_lock);
2637 iter->tr = &global_trace;
2638 iter->trace = current_trace;
2639 filp->private_data = iter;
2641 if (iter->trace->pipe_open)
2642 iter->trace->pipe_open(iter);
2643 mutex_unlock(&trace_types_lock);
2648 static int tracing_release_pipe(struct inode *inode, struct file *file)
2650 struct trace_iterator *iter = file->private_data;
2653 atomic_dec(&tracing_reader);
2659 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2661 struct trace_iterator *iter = filp->private_data;
2663 if (trace_flags & TRACE_ITER_BLOCK) {
2665 * Always select as readable when in blocking mode
2667 return POLLIN | POLLRDNORM;
2669 if (!trace_empty(iter))
2670 return POLLIN | POLLRDNORM;
2671 poll_wait(filp, &trace_wait, poll_table);
2672 if (!trace_empty(iter))
2673 return POLLIN | POLLRDNORM;
2683 tracing_read_pipe(struct file *filp, char __user *ubuf,
2684 size_t cnt, loff_t *ppos)
2686 struct trace_iterator *iter = filp->private_data;
2689 /* return any leftover data */
2690 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2694 trace_seq_reset(&iter->seq);
2696 mutex_lock(&trace_types_lock);
2697 if (iter->trace->read) {
2698 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2705 while (trace_empty(iter)) {
2707 if ((filp->f_flags & O_NONBLOCK)) {
2713 * This is a make-shift waitqueue. The reason we don't use
2714 * an actual wait queue is because:
2715 * 1) we only ever have one waiter
2716 * 2) the tracing, traces all functions, we don't want
2717 * the overhead of calling wake_up and friends
2718 * (and tracing them too)
2719 * Anyway, this is really very primitive wakeup.
2721 set_current_state(TASK_INTERRUPTIBLE);
2722 iter->tr->waiter = current;
2724 mutex_unlock(&trace_types_lock);
2726 /* sleep for 100 msecs, and try again. */
2727 schedule_timeout(HZ/10);
2729 mutex_lock(&trace_types_lock);
2731 iter->tr->waiter = NULL;
2733 if (signal_pending(current)) {
2738 if (iter->trace != current_trace)
2742 * We block until we read something and tracing is disabled.
2743 * We still block if tracing is disabled, but we have never
2744 * read anything. This allows a user to cat this file, and
2745 * then enable tracing. But after we have read something,
2746 * we give an EOF when tracing is again disabled.
2748 * iter->pos will be 0 if we haven't read anything.
2750 if (!tracer_enabled && iter->pos)
2756 /* stop when tracing is finished */
2757 if (trace_empty(iter))
2760 if (cnt >= PAGE_SIZE)
2761 cnt = PAGE_SIZE - 1;
2763 /* reset all but tr, trace, and overruns */
2764 memset(&iter->seq, 0,
2765 sizeof(struct trace_iterator) -
2766 offsetof(struct trace_iterator, seq));
2769 while (find_next_entry_inc(iter) != NULL) {
2770 enum print_line_t ret;
2771 int len = iter->seq.len;
2773 ret = print_trace_line(iter);
2774 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2775 /* don't print partial lines */
2776 iter->seq.len = len;
2780 trace_consume(iter);
2782 if (iter->seq.len >= cnt)
2786 /* Now copy what we have to the user */
2787 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2788 if (iter->seq.readpos >= iter->seq.len)
2789 trace_seq_reset(&iter->seq);
2792 * If there was nothing to send to user, inspite of consuming trace
2793 * entries, go back to wait for more entries.
2799 mutex_unlock(&trace_types_lock);
2805 tracing_entries_read(struct file *filp, char __user *ubuf,
2806 size_t cnt, loff_t *ppos)
2808 struct trace_array *tr = filp->private_data;
2812 r = sprintf(buf, "%lu\n", tr->entries);
2813 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2817 tracing_entries_write(struct file *filp, const char __user *ubuf,
2818 size_t cnt, loff_t *ppos)
2823 struct trace_array *tr = filp->private_data;
2825 if (cnt >= sizeof(buf))
2828 if (copy_from_user(&buf, ubuf, cnt))
2833 ret = strict_strtoul(buf, 10, &val);
2837 /* must have at least 1 entry */
2841 mutex_lock(&trace_types_lock);
2845 pr_info("ftrace: please disable tracing"
2846 " before modifying buffer size\n");
2850 if (val != global_trace.entries) {
2851 ret = ring_buffer_resize(global_trace.buffer, val);
2857 ret = ring_buffer_resize(max_tr.buffer, val);
2861 r = ring_buffer_resize(global_trace.buffer,
2862 global_trace.entries);
2864 /* AARGH! We are left with different
2865 * size max buffer!!!! */
2867 tracing_disabled = 1;
2872 global_trace.entries = val;
2877 /* If check pages failed, return ENOMEM */
2878 if (tracing_disabled)
2881 max_tr.entries = global_trace.entries;
2882 mutex_unlock(&trace_types_lock);
2887 static int mark_printk(const char *fmt, ...)
2891 va_start(args, fmt);
2892 ret = trace_vprintk(0, fmt, args);
2898 tracing_mark_write(struct file *filp, const char __user *ubuf,
2899 size_t cnt, loff_t *fpos)
2903 struct trace_array *tr = &global_trace;
2905 if (!tr->ctrl || tracing_disabled)
2908 if (cnt > TRACE_BUF_SIZE)
2909 cnt = TRACE_BUF_SIZE;
2911 buf = kmalloc(cnt + 1, GFP_KERNEL);
2915 if (copy_from_user(buf, ubuf, cnt)) {
2920 /* Cut from the first nil or newline. */
2922 end = strchr(buf, '\n');
2926 cnt = mark_printk("%s\n", buf);
2933 static struct file_operations tracing_max_lat_fops = {
2934 .open = tracing_open_generic,
2935 .read = tracing_max_lat_read,
2936 .write = tracing_max_lat_write,
2939 static struct file_operations tracing_ctrl_fops = {
2940 .open = tracing_open_generic,
2941 .read = tracing_ctrl_read,
2942 .write = tracing_ctrl_write,
2945 static struct file_operations set_tracer_fops = {
2946 .open = tracing_open_generic,
2947 .read = tracing_set_trace_read,
2948 .write = tracing_set_trace_write,
2951 static struct file_operations tracing_pipe_fops = {
2952 .open = tracing_open_pipe,
2953 .poll = tracing_poll_pipe,
2954 .read = tracing_read_pipe,
2955 .release = tracing_release_pipe,
2958 static struct file_operations tracing_entries_fops = {
2959 .open = tracing_open_generic,
2960 .read = tracing_entries_read,
2961 .write = tracing_entries_write,
2964 static struct file_operations tracing_mark_fops = {
2965 .open = tracing_open_generic,
2966 .write = tracing_mark_write,
2969 #ifdef CONFIG_DYNAMIC_FTRACE
2971 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2977 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
2978 size_t cnt, loff_t *ppos)
2980 static char ftrace_dyn_info_buffer[1024];
2981 static DEFINE_MUTEX(dyn_info_mutex);
2982 unsigned long *p = filp->private_data;
2983 char *buf = ftrace_dyn_info_buffer;
2984 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
2987 mutex_lock(&dyn_info_mutex);
2988 r = sprintf(buf, "%ld ", *p);
2990 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
2993 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2995 mutex_unlock(&dyn_info_mutex);
3000 static struct file_operations tracing_dyn_info_fops = {
3001 .open = tracing_open_generic,
3002 .read = tracing_read_dyn_info,
3006 static struct dentry *d_tracer;
3008 struct dentry *tracing_init_dentry(void)
3015 d_tracer = debugfs_create_dir("tracing", NULL);
3017 if (!d_tracer && !once) {
3019 pr_warning("Could not create debugfs directory 'tracing'\n");
3026 #ifdef CONFIG_FTRACE_SELFTEST
3027 /* Let selftest have access to static functions in this file */
3028 #include "trace_selftest.c"
3031 static __init int tracer_init_debugfs(void)
3033 struct dentry *d_tracer;
3034 struct dentry *entry;
3036 d_tracer = tracing_init_dentry();
3038 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3039 &global_trace, &tracing_ctrl_fops);
3041 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3043 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
3044 NULL, &tracing_iter_fops);
3046 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
3048 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3049 NULL, &tracing_cpumask_fops);
3051 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3053 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3054 &global_trace, &tracing_lt_fops);
3056 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3058 entry = debugfs_create_file("trace", 0444, d_tracer,
3059 &global_trace, &tracing_fops);
3061 pr_warning("Could not create debugfs 'trace' entry\n");
3063 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3064 &global_trace, &show_traces_fops);
3066 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3068 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3069 &global_trace, &set_tracer_fops);
3071 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3073 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3074 &tracing_max_latency,
3075 &tracing_max_lat_fops);
3077 pr_warning("Could not create debugfs "
3078 "'tracing_max_latency' entry\n");
3080 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3081 &tracing_thresh, &tracing_max_lat_fops);
3083 pr_warning("Could not create debugfs "
3084 "'tracing_thresh' entry\n");
3085 entry = debugfs_create_file("README", 0644, d_tracer,
3086 NULL, &tracing_readme_fops);
3088 pr_warning("Could not create debugfs 'README' entry\n");
3090 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3091 NULL, &tracing_pipe_fops);
3093 pr_warning("Could not create debugfs "
3094 "'trace_pipe' entry\n");
3096 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3097 &global_trace, &tracing_entries_fops);
3099 pr_warning("Could not create debugfs "
3100 "'trace_entries' entry\n");
3102 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3103 NULL, &tracing_mark_fops);
3105 pr_warning("Could not create debugfs "
3106 "'trace_marker' entry\n");
3108 #ifdef CONFIG_DYNAMIC_FTRACE
3109 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3110 &ftrace_update_tot_cnt,
3111 &tracing_dyn_info_fops);
3113 pr_warning("Could not create debugfs "
3114 "'dyn_ftrace_total_info' entry\n");
3116 #ifdef CONFIG_SYSPROF_TRACER
3117 init_tracer_sysprof_debugfs(d_tracer);
3122 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3124 static DEFINE_SPINLOCK(trace_buf_lock);
3125 static char trace_buf[TRACE_BUF_SIZE];
3127 struct ring_buffer_event *event;
3128 struct trace_array *tr = &global_trace;
3129 struct trace_array_cpu *data;
3130 struct print_entry *entry;
3131 unsigned long flags, irq_flags;
3132 int cpu, len = 0, size, pc;
3134 if (!tr->ctrl || tracing_disabled)
3137 pc = preempt_count();
3138 preempt_disable_notrace();
3139 cpu = raw_smp_processor_id();
3140 data = tr->data[cpu];
3142 if (unlikely(atomic_read(&data->disabled)))
3145 spin_lock_irqsave(&trace_buf_lock, flags);
3146 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3148 len = min(len, TRACE_BUF_SIZE-1);
3151 size = sizeof(*entry) + len + 1;
3152 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3155 entry = ring_buffer_event_data(event);
3156 tracing_generic_entry_update(&entry->ent, flags, pc);
3157 entry->ent.type = TRACE_PRINT;
3160 memcpy(&entry->buf, trace_buf, len);
3161 entry->buf[len] = 0;
3162 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3165 spin_unlock_irqrestore(&trace_buf_lock, flags);
3168 preempt_enable_notrace();
3172 EXPORT_SYMBOL_GPL(trace_vprintk);
3174 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3179 if (!(trace_flags & TRACE_ITER_PRINTK))
3183 ret = trace_vprintk(ip, fmt, ap);
3187 EXPORT_SYMBOL_GPL(__ftrace_printk);
3189 static int trace_panic_handler(struct notifier_block *this,
3190 unsigned long event, void *unused)
3192 if (ftrace_dump_on_oops)
3197 static struct notifier_block trace_panic_notifier = {
3198 .notifier_call = trace_panic_handler,
3200 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3203 static int trace_die_handler(struct notifier_block *self,
3209 if (ftrace_dump_on_oops)
3218 static struct notifier_block trace_die_notifier = {
3219 .notifier_call = trace_die_handler,
3224 * printk is set to max of 1024, we really don't need it that big.
3225 * Nothing should be printing 1000 characters anyway.
3227 #define TRACE_MAX_PRINT 1000
3230 * Define here KERN_TRACE so that we have one place to modify
3231 * it if we decide to change what log level the ftrace dump
3234 #define KERN_TRACE KERN_INFO
3237 trace_printk_seq(struct trace_seq *s)
3239 /* Probably should print a warning here. */
3243 /* should be zero ended, but we are paranoid. */
3244 s->buffer[s->len] = 0;
3246 printk(KERN_TRACE "%s", s->buffer);
3251 void ftrace_dump(void)
3253 static DEFINE_SPINLOCK(ftrace_dump_lock);
3254 /* use static because iter can be a bit big for the stack */
3255 static struct trace_iterator iter;
3256 static cpumask_t mask;
3257 static int dump_ran;
3258 unsigned long flags;
3262 spin_lock_irqsave(&ftrace_dump_lock, flags);
3268 /* No turning back! */
3271 for_each_tracing_cpu(cpu) {
3272 atomic_inc(&global_trace.data[cpu]->disabled);
3275 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3277 iter.tr = &global_trace;
3278 iter.trace = current_trace;
3281 * We need to stop all tracing on all CPUS to read the
3282 * the next buffer. This is a bit expensive, but is
3283 * not done often. We fill all what we can read,
3284 * and then release the locks again.
3289 while (!trace_empty(&iter)) {
3292 printk(KERN_TRACE "---------------------------------\n");
3296 /* reset all but tr, trace, and overruns */
3297 memset(&iter.seq, 0,
3298 sizeof(struct trace_iterator) -
3299 offsetof(struct trace_iterator, seq));
3300 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3303 if (find_next_entry_inc(&iter) != NULL) {
3304 print_trace_line(&iter);
3305 trace_consume(&iter);
3308 trace_printk_seq(&iter.seq);
3312 printk(KERN_TRACE " (ftrace buffer empty)\n");
3314 printk(KERN_TRACE "---------------------------------\n");
3317 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3320 __init static int tracer_alloc_buffers(void)
3322 struct trace_array_cpu *data;
3325 /* TODO: make the number of buffers hot pluggable with CPUS */
3326 tracing_buffer_mask = cpu_possible_map;
3328 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3329 TRACE_BUFFER_FLAGS);
3330 if (!global_trace.buffer) {
3331 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3335 global_trace.entries = ring_buffer_size(global_trace.buffer);
3337 #ifdef CONFIG_TRACER_MAX_TRACE
3338 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3339 TRACE_BUFFER_FLAGS);
3340 if (!max_tr.buffer) {
3341 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3343 ring_buffer_free(global_trace.buffer);
3346 max_tr.entries = ring_buffer_size(max_tr.buffer);
3347 WARN_ON(max_tr.entries != global_trace.entries);
3350 /* Allocate the first page for all buffers */
3351 for_each_tracing_cpu(i) {
3352 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3353 max_tr.data[i] = &per_cpu(max_data, i);
3356 trace_init_cmdlines();
3358 register_tracer(&nop_trace);
3359 #ifdef CONFIG_BOOT_TRACER
3360 /* We don't want to launch sched_switch tracer yet */
3361 global_trace.ctrl = 0;
3362 register_tracer(&boot_tracer);
3363 current_trace = &boot_tracer;
3364 current_trace->init(&global_trace);
3366 current_trace = &nop_trace;
3369 /* All seems OK, enable tracing */
3370 global_trace.ctrl = 1;
3371 tracing_disabled = 0;
3373 atomic_notifier_chain_register(&panic_notifier_list,
3374 &trace_panic_notifier);
3376 register_die_notifier(&trace_die_notifier);
3380 early_initcall(tracer_alloc_buffers);
3381 fs_initcall(tracer_init_debugfs);