sparc: Fix bus type probing for ESP and LE devices.
[linux-2.6/mini2440.git] / kernel / trace / trace.c
blobb2387c04afd6a004e73a076fc3069183a35882b6
1 /*
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>
31 #include <linux/fs.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>
39 #include "trace.h"
41 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
43 unsigned long __read_mostly tracing_max_latency;
44 unsigned long __read_mostly tracing_thresh;
47 * We need to change this state when a selftest is running.
48 * A selftest will lurk into the ring-buffer to count the
49 * entries inserted during the selftest although some concurrent
50 * insertions into the ring-buffer such as ftrace_printk could occurred
51 * at the same time, giving false positive or negative results.
53 static bool __read_mostly tracing_selftest_running;
55 /* For tracers that don't implement custom flags */
56 static struct tracer_opt dummy_tracer_opt[] = {
57 { }
60 static struct tracer_flags dummy_tracer_flags = {
61 .val = 0,
62 .opts = dummy_tracer_opt
65 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
67 return 0;
71 * Kill all tracing for good (never come back).
72 * It is initialized to 1 but will turn to zero if the initialization
73 * of the tracer is successful. But that is the only place that sets
74 * this back to zero.
76 int tracing_disabled = 1;
78 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
80 static inline void ftrace_disable_cpu(void)
82 preempt_disable();
83 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
86 static inline void ftrace_enable_cpu(void)
88 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
89 preempt_enable();
92 static cpumask_var_t __read_mostly tracing_buffer_mask;
94 #define for_each_tracing_cpu(cpu) \
95 for_each_cpu(cpu, tracing_buffer_mask)
98 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
100 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
101 * is set, then ftrace_dump is called. This will output the contents
102 * of the ftrace buffers to the console. This is very useful for
103 * capturing traces that lead to crashes and outputing it to a
104 * serial console.
106 * It is default off, but you can enable it with either specifying
107 * "ftrace_dump_on_oops" in the kernel command line, or setting
108 * /proc/sys/kernel/ftrace_dump_on_oops to true.
110 int ftrace_dump_on_oops;
112 static int tracing_set_tracer(char *buf);
114 static int __init set_ftrace(char *str)
116 tracing_set_tracer(str);
117 return 1;
119 __setup("ftrace", set_ftrace);
121 static int __init set_ftrace_dump_on_oops(char *str)
123 ftrace_dump_on_oops = 1;
124 return 1;
126 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
128 long
129 ns2usecs(cycle_t nsec)
131 nsec += 500;
132 do_div(nsec, 1000);
133 return nsec;
136 cycle_t ftrace_now(int cpu)
138 u64 ts = ring_buffer_time_stamp(cpu);
139 ring_buffer_normalize_time_stamp(cpu, &ts);
140 return ts;
144 * The global_trace is the descriptor that holds the tracing
145 * buffers for the live tracing. For each CPU, it contains
146 * a link list of pages that will store trace entries. The
147 * page descriptor of the pages in the memory is used to hold
148 * the link list by linking the lru item in the page descriptor
149 * to each of the pages in the buffer per CPU.
151 * For each active CPU there is a data field that holds the
152 * pages for the buffer for that CPU. Each CPU has the same number
153 * of pages allocated for its buffer.
155 static struct trace_array global_trace;
157 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
160 * The max_tr is used to snapshot the global_trace when a maximum
161 * latency is reached. Some tracers will use this to store a maximum
162 * trace while it continues examining live traces.
164 * The buffers for the max_tr are set up the same as the global_trace.
165 * When a snapshot is taken, the link list of the max_tr is swapped
166 * with the link list of the global_trace and the buffers are reset for
167 * the global_trace so the tracing can continue.
169 static struct trace_array max_tr;
171 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
173 /* tracer_enabled is used to toggle activation of a tracer */
174 static int tracer_enabled = 1;
177 * tracing_is_enabled - return tracer_enabled status
179 * This function is used by other tracers to know the status
180 * of the tracer_enabled flag. Tracers may use this function
181 * to know if it should enable their features when starting
182 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
184 int tracing_is_enabled(void)
186 return tracer_enabled;
189 /* function tracing enabled */
190 int ftrace_function_enabled;
193 * trace_buf_size is the size in bytes that is allocated
194 * for a buffer. Note, the number of bytes is always rounded
195 * to page size.
197 * This number is purposely set to a low number of 16384.
198 * If the dump on oops happens, it will be much appreciated
199 * to not have to wait for all that output. Anyway this can be
200 * boot time and run time configurable.
202 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
204 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
206 /* trace_types holds a link list of available tracers. */
207 static struct tracer *trace_types __read_mostly;
209 /* current_trace points to the tracer that is currently active */
210 static struct tracer *current_trace __read_mostly;
213 * max_tracer_type_len is used to simplify the allocating of
214 * buffers to read userspace tracer names. We keep track of
215 * the longest tracer name registered.
217 static int max_tracer_type_len;
220 * trace_types_lock is used to protect the trace_types list.
221 * This lock is also used to keep user access serialized.
222 * Accesses from userspace will grab this lock while userspace
223 * activities happen inside the kernel.
225 static DEFINE_MUTEX(trace_types_lock);
227 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
228 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
230 /* trace_flags holds trace_options default values */
231 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
232 TRACE_ITER_ANNOTATE;
235 * trace_wake_up - wake up tasks waiting for trace input
237 * Simply wakes up any task that is blocked on the trace_wait
238 * queue. These is used with trace_poll for tasks polling the trace.
240 void trace_wake_up(void)
243 * The runqueue_is_locked() can fail, but this is the best we
244 * have for now:
246 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
247 wake_up(&trace_wait);
250 static int __init set_buf_size(char *str)
252 unsigned long buf_size;
253 int ret;
255 if (!str)
256 return 0;
257 ret = strict_strtoul(str, 0, &buf_size);
258 /* nr_entries can not be zero */
259 if (ret < 0 || buf_size == 0)
260 return 0;
261 trace_buf_size = buf_size;
262 return 1;
264 __setup("trace_buf_size=", set_buf_size);
266 unsigned long nsecs_to_usecs(unsigned long nsecs)
268 return nsecs / 1000;
271 /* These must match the bit postions in trace_iterator_flags */
272 static const char *trace_options[] = {
273 "print-parent",
274 "sym-offset",
275 "sym-addr",
276 "verbose",
277 "raw",
278 "hex",
279 "bin",
280 "block",
281 "stacktrace",
282 "sched-tree",
283 "ftrace_printk",
284 "ftrace_preempt",
285 "branch",
286 "annotate",
287 "userstacktrace",
288 "sym-userobj",
289 "printk-msg-only",
290 NULL
294 * ftrace_max_lock is used to protect the swapping of buffers
295 * when taking a max snapshot. The buffers themselves are
296 * protected by per_cpu spinlocks. But the action of the swap
297 * needs its own lock.
299 * This is defined as a raw_spinlock_t in order to help
300 * with performance when lockdep debugging is enabled.
302 static raw_spinlock_t ftrace_max_lock =
303 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
306 * Copy the new maximum trace into the separate maximum-trace
307 * structure. (this way the maximum trace is permanently saved,
308 * for later retrieval via /debugfs/tracing/latency_trace)
310 static void
311 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
313 struct trace_array_cpu *data = tr->data[cpu];
315 max_tr.cpu = cpu;
316 max_tr.time_start = data->preempt_timestamp;
318 data = max_tr.data[cpu];
319 data->saved_latency = tracing_max_latency;
321 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
322 data->pid = tsk->pid;
323 data->uid = task_uid(tsk);
324 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
325 data->policy = tsk->policy;
326 data->rt_priority = tsk->rt_priority;
328 /* record this tasks comm */
329 tracing_record_cmdline(current);
333 * trace_seq_printf - sequence printing of trace information
334 * @s: trace sequence descriptor
335 * @fmt: printf format string
337 * The tracer may use either sequence operations or its own
338 * copy to user routines. To simplify formating of a trace
339 * trace_seq_printf is used to store strings into a special
340 * buffer (@s). Then the output may be either used by
341 * the sequencer or pulled into another buffer.
344 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
346 int len = (PAGE_SIZE - 1) - s->len;
347 va_list ap;
348 int ret;
350 if (!len)
351 return 0;
353 va_start(ap, fmt);
354 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
355 va_end(ap);
357 /* If we can't write it all, don't bother writing anything */
358 if (ret >= len)
359 return 0;
361 s->len += ret;
363 return len;
367 * trace_seq_puts - trace sequence printing of simple string
368 * @s: trace sequence descriptor
369 * @str: simple string to record
371 * The tracer may use either the sequence operations or its own
372 * copy to user routines. This function records a simple string
373 * into a special buffer (@s) for later retrieval by a sequencer
374 * or other mechanism.
376 static int
377 trace_seq_puts(struct trace_seq *s, const char *str)
379 int len = strlen(str);
381 if (len > ((PAGE_SIZE - 1) - s->len))
382 return 0;
384 memcpy(s->buffer + s->len, str, len);
385 s->len += len;
387 return len;
390 static int
391 trace_seq_putc(struct trace_seq *s, unsigned char c)
393 if (s->len >= (PAGE_SIZE - 1))
394 return 0;
396 s->buffer[s->len++] = c;
398 return 1;
401 static int
402 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
404 if (len > ((PAGE_SIZE - 1) - s->len))
405 return 0;
407 memcpy(s->buffer + s->len, mem, len);
408 s->len += len;
410 return len;
413 #define MAX_MEMHEX_BYTES 8
414 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
416 static int
417 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
419 unsigned char hex[HEX_CHARS];
420 unsigned char *data = mem;
421 int i, j;
423 #ifdef __BIG_ENDIAN
424 for (i = 0, j = 0; i < len; i++) {
425 #else
426 for (i = len-1, j = 0; i >= 0; i--) {
427 #endif
428 hex[j++] = hex_asc_hi(data[i]);
429 hex[j++] = hex_asc_lo(data[i]);
431 hex[j++] = ' ';
433 return trace_seq_putmem(s, hex, j);
436 static int
437 trace_seq_path(struct trace_seq *s, struct path *path)
439 unsigned char *p;
441 if (s->len >= (PAGE_SIZE - 1))
442 return 0;
443 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
444 if (!IS_ERR(p)) {
445 p = mangle_path(s->buffer + s->len, p, "\n");
446 if (p) {
447 s->len = p - s->buffer;
448 return 1;
450 } else {
451 s->buffer[s->len++] = '?';
452 return 1;
455 return 0;
458 static void
459 trace_seq_reset(struct trace_seq *s)
461 s->len = 0;
462 s->readpos = 0;
465 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
467 int len;
468 int ret;
470 if (s->len <= s->readpos)
471 return -EBUSY;
473 len = s->len - s->readpos;
474 if (cnt > len)
475 cnt = len;
476 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
477 if (ret)
478 return -EFAULT;
480 s->readpos += len;
481 return cnt;
484 static void
485 trace_print_seq(struct seq_file *m, struct trace_seq *s)
487 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
489 s->buffer[len] = 0;
490 seq_puts(m, s->buffer);
492 trace_seq_reset(s);
496 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
497 * @tr: tracer
498 * @tsk: the task with the latency
499 * @cpu: The cpu that initiated the trace.
501 * Flip the buffers between the @tr and the max_tr and record information
502 * about which task was the cause of this latency.
504 void
505 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
507 struct ring_buffer *buf = tr->buffer;
509 WARN_ON_ONCE(!irqs_disabled());
510 __raw_spin_lock(&ftrace_max_lock);
512 tr->buffer = max_tr.buffer;
513 max_tr.buffer = buf;
515 ftrace_disable_cpu();
516 ring_buffer_reset(tr->buffer);
517 ftrace_enable_cpu();
519 __update_max_tr(tr, tsk, cpu);
520 __raw_spin_unlock(&ftrace_max_lock);
524 * update_max_tr_single - only copy one trace over, and reset the rest
525 * @tr - tracer
526 * @tsk - task with the latency
527 * @cpu - the cpu of the buffer to copy.
529 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
531 void
532 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
534 int ret;
536 WARN_ON_ONCE(!irqs_disabled());
537 __raw_spin_lock(&ftrace_max_lock);
539 ftrace_disable_cpu();
541 ring_buffer_reset(max_tr.buffer);
542 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
544 ftrace_enable_cpu();
546 WARN_ON_ONCE(ret);
548 __update_max_tr(tr, tsk, cpu);
549 __raw_spin_unlock(&ftrace_max_lock);
553 * register_tracer - register a tracer with the ftrace system.
554 * @type - the plugin for the tracer
556 * Register a new plugin tracer.
558 int register_tracer(struct tracer *type)
560 struct tracer *t;
561 int len;
562 int ret = 0;
564 if (!type->name) {
565 pr_info("Tracer must have a name\n");
566 return -1;
570 * When this gets called we hold the BKL which means that
571 * preemption is disabled. Various trace selftests however
572 * need to disable and enable preemption for successful tests.
573 * So we drop the BKL here and grab it after the tests again.
575 unlock_kernel();
576 mutex_lock(&trace_types_lock);
578 tracing_selftest_running = true;
580 for (t = trace_types; t; t = t->next) {
581 if (strcmp(type->name, t->name) == 0) {
582 /* already found */
583 pr_info("Trace %s already registered\n",
584 type->name);
585 ret = -1;
586 goto out;
590 if (!type->set_flag)
591 type->set_flag = &dummy_set_flag;
592 if (!type->flags)
593 type->flags = &dummy_tracer_flags;
594 else
595 if (!type->flags->opts)
596 type->flags->opts = dummy_tracer_opt;
598 #ifdef CONFIG_FTRACE_STARTUP_TEST
599 if (type->selftest) {
600 struct tracer *saved_tracer = current_trace;
601 struct trace_array *tr = &global_trace;
602 int i;
605 * Run a selftest on this tracer.
606 * Here we reset the trace buffer, and set the current
607 * tracer to be this tracer. The tracer can then run some
608 * internal tracing to verify that everything is in order.
609 * If we fail, we do not register this tracer.
611 for_each_tracing_cpu(i)
612 tracing_reset(tr, i);
614 current_trace = type;
615 /* the test is responsible for initializing and enabling */
616 pr_info("Testing tracer %s: ", type->name);
617 ret = type->selftest(type, tr);
618 /* the test is responsible for resetting too */
619 current_trace = saved_tracer;
620 if (ret) {
621 printk(KERN_CONT "FAILED!\n");
622 goto out;
624 /* Only reset on passing, to avoid touching corrupted buffers */
625 for_each_tracing_cpu(i)
626 tracing_reset(tr, i);
628 printk(KERN_CONT "PASSED\n");
630 #endif
632 type->next = trace_types;
633 trace_types = type;
634 len = strlen(type->name);
635 if (len > max_tracer_type_len)
636 max_tracer_type_len = len;
638 out:
639 tracing_selftest_running = false;
640 mutex_unlock(&trace_types_lock);
641 lock_kernel();
643 return ret;
646 void unregister_tracer(struct tracer *type)
648 struct tracer **t;
649 int len;
651 mutex_lock(&trace_types_lock);
652 for (t = &trace_types; *t; t = &(*t)->next) {
653 if (*t == type)
654 goto found;
656 pr_info("Trace %s not registered\n", type->name);
657 goto out;
659 found:
660 *t = (*t)->next;
661 if (strlen(type->name) != max_tracer_type_len)
662 goto out;
664 max_tracer_type_len = 0;
665 for (t = &trace_types; *t; t = &(*t)->next) {
666 len = strlen((*t)->name);
667 if (len > max_tracer_type_len)
668 max_tracer_type_len = len;
670 out:
671 mutex_unlock(&trace_types_lock);
674 void tracing_reset(struct trace_array *tr, int cpu)
676 ftrace_disable_cpu();
677 ring_buffer_reset_cpu(tr->buffer, cpu);
678 ftrace_enable_cpu();
681 void tracing_reset_online_cpus(struct trace_array *tr)
683 int cpu;
685 tr->time_start = ftrace_now(tr->cpu);
687 for_each_online_cpu(cpu)
688 tracing_reset(tr, cpu);
691 #define SAVED_CMDLINES 128
692 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
693 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
694 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
695 static int cmdline_idx;
696 static DEFINE_SPINLOCK(trace_cmdline_lock);
698 /* temporary disable recording */
699 atomic_t trace_record_cmdline_disabled __read_mostly;
701 static void trace_init_cmdlines(void)
703 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
704 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
705 cmdline_idx = 0;
708 static int trace_stop_count;
709 static DEFINE_SPINLOCK(tracing_start_lock);
712 * ftrace_off_permanent - disable all ftrace code permanently
714 * This should only be called when a serious anomally has
715 * been detected. This will turn off the function tracing,
716 * ring buffers, and other tracing utilites. It takes no
717 * locks and can be called from any context.
719 void ftrace_off_permanent(void)
721 tracing_disabled = 1;
722 ftrace_stop();
723 tracing_off_permanent();
727 * tracing_start - quick start of the tracer
729 * If tracing is enabled but was stopped by tracing_stop,
730 * this will start the tracer back up.
732 void tracing_start(void)
734 struct ring_buffer *buffer;
735 unsigned long flags;
737 if (tracing_disabled)
738 return;
740 spin_lock_irqsave(&tracing_start_lock, flags);
741 if (--trace_stop_count)
742 goto out;
744 if (trace_stop_count < 0) {
745 /* Someone screwed up their debugging */
746 WARN_ON_ONCE(1);
747 trace_stop_count = 0;
748 goto out;
752 buffer = global_trace.buffer;
753 if (buffer)
754 ring_buffer_record_enable(buffer);
756 buffer = max_tr.buffer;
757 if (buffer)
758 ring_buffer_record_enable(buffer);
760 ftrace_start();
761 out:
762 spin_unlock_irqrestore(&tracing_start_lock, flags);
766 * tracing_stop - quick stop of the tracer
768 * Light weight way to stop tracing. Use in conjunction with
769 * tracing_start.
771 void tracing_stop(void)
773 struct ring_buffer *buffer;
774 unsigned long flags;
776 ftrace_stop();
777 spin_lock_irqsave(&tracing_start_lock, flags);
778 if (trace_stop_count++)
779 goto out;
781 buffer = global_trace.buffer;
782 if (buffer)
783 ring_buffer_record_disable(buffer);
785 buffer = max_tr.buffer;
786 if (buffer)
787 ring_buffer_record_disable(buffer);
789 out:
790 spin_unlock_irqrestore(&tracing_start_lock, flags);
793 void trace_stop_cmdline_recording(void);
795 static void trace_save_cmdline(struct task_struct *tsk)
797 unsigned map;
798 unsigned idx;
800 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
801 return;
804 * It's not the end of the world if we don't get
805 * the lock, but we also don't want to spin
806 * nor do we want to disable interrupts,
807 * so if we miss here, then better luck next time.
809 if (!spin_trylock(&trace_cmdline_lock))
810 return;
812 idx = map_pid_to_cmdline[tsk->pid];
813 if (idx >= SAVED_CMDLINES) {
814 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
816 map = map_cmdline_to_pid[idx];
817 if (map <= PID_MAX_DEFAULT)
818 map_pid_to_cmdline[map] = (unsigned)-1;
820 map_pid_to_cmdline[tsk->pid] = idx;
822 cmdline_idx = idx;
825 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
827 spin_unlock(&trace_cmdline_lock);
830 char *trace_find_cmdline(int pid)
832 char *cmdline = "<...>";
833 unsigned map;
835 if (!pid)
836 return "<idle>";
838 if (pid > PID_MAX_DEFAULT)
839 goto out;
841 map = map_pid_to_cmdline[pid];
842 if (map >= SAVED_CMDLINES)
843 goto out;
845 cmdline = saved_cmdlines[map];
847 out:
848 return cmdline;
851 void tracing_record_cmdline(struct task_struct *tsk)
853 if (atomic_read(&trace_record_cmdline_disabled))
854 return;
856 trace_save_cmdline(tsk);
859 void
860 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
861 int pc)
863 struct task_struct *tsk = current;
865 entry->preempt_count = pc & 0xff;
866 entry->pid = (tsk) ? tsk->pid : 0;
867 entry->tgid = (tsk) ? tsk->tgid : 0;
868 entry->flags =
869 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
870 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
871 #else
872 TRACE_FLAG_IRQS_NOSUPPORT |
873 #endif
874 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
875 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
876 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
879 void
880 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
881 unsigned long ip, unsigned long parent_ip, unsigned long flags,
882 int pc)
884 struct ring_buffer_event *event;
885 struct ftrace_entry *entry;
886 unsigned long irq_flags;
888 /* If we are reading the ring buffer, don't trace */
889 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
890 return;
892 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
893 &irq_flags);
894 if (!event)
895 return;
896 entry = ring_buffer_event_data(event);
897 tracing_generic_entry_update(&entry->ent, flags, pc);
898 entry->ent.type = TRACE_FN;
899 entry->ip = ip;
900 entry->parent_ip = parent_ip;
901 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
904 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
905 static void __trace_graph_entry(struct trace_array *tr,
906 struct trace_array_cpu *data,
907 struct ftrace_graph_ent *trace,
908 unsigned long flags,
909 int pc)
911 struct ring_buffer_event *event;
912 struct ftrace_graph_ent_entry *entry;
913 unsigned long irq_flags;
915 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
916 return;
918 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
919 &irq_flags);
920 if (!event)
921 return;
922 entry = ring_buffer_event_data(event);
923 tracing_generic_entry_update(&entry->ent, flags, pc);
924 entry->ent.type = TRACE_GRAPH_ENT;
925 entry->graph_ent = *trace;
926 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
929 static void __trace_graph_return(struct trace_array *tr,
930 struct trace_array_cpu *data,
931 struct ftrace_graph_ret *trace,
932 unsigned long flags,
933 int pc)
935 struct ring_buffer_event *event;
936 struct ftrace_graph_ret_entry *entry;
937 unsigned long irq_flags;
939 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
940 return;
942 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
943 &irq_flags);
944 if (!event)
945 return;
946 entry = ring_buffer_event_data(event);
947 tracing_generic_entry_update(&entry->ent, flags, pc);
948 entry->ent.type = TRACE_GRAPH_RET;
949 entry->ret = *trace;
950 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
952 #endif
954 void
955 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
956 unsigned long ip, unsigned long parent_ip, unsigned long flags,
957 int pc)
959 if (likely(!atomic_read(&data->disabled)))
960 trace_function(tr, data, ip, parent_ip, flags, pc);
963 static void ftrace_trace_stack(struct trace_array *tr,
964 struct trace_array_cpu *data,
965 unsigned long flags,
966 int skip, int pc)
968 #ifdef CONFIG_STACKTRACE
969 struct ring_buffer_event *event;
970 struct stack_entry *entry;
971 struct stack_trace trace;
972 unsigned long irq_flags;
974 if (!(trace_flags & TRACE_ITER_STACKTRACE))
975 return;
977 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
978 &irq_flags);
979 if (!event)
980 return;
981 entry = ring_buffer_event_data(event);
982 tracing_generic_entry_update(&entry->ent, flags, pc);
983 entry->ent.type = TRACE_STACK;
985 memset(&entry->caller, 0, sizeof(entry->caller));
987 trace.nr_entries = 0;
988 trace.max_entries = FTRACE_STACK_ENTRIES;
989 trace.skip = skip;
990 trace.entries = entry->caller;
992 save_stack_trace(&trace);
993 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
994 #endif
997 void __trace_stack(struct trace_array *tr,
998 struct trace_array_cpu *data,
999 unsigned long flags,
1000 int skip)
1002 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
1005 static void ftrace_trace_userstack(struct trace_array *tr,
1006 struct trace_array_cpu *data,
1007 unsigned long flags, int pc)
1009 #ifdef CONFIG_STACKTRACE
1010 struct ring_buffer_event *event;
1011 struct userstack_entry *entry;
1012 struct stack_trace trace;
1013 unsigned long irq_flags;
1015 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1016 return;
1018 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1019 &irq_flags);
1020 if (!event)
1021 return;
1022 entry = ring_buffer_event_data(event);
1023 tracing_generic_entry_update(&entry->ent, flags, pc);
1024 entry->ent.type = TRACE_USER_STACK;
1026 memset(&entry->caller, 0, sizeof(entry->caller));
1028 trace.nr_entries = 0;
1029 trace.max_entries = FTRACE_STACK_ENTRIES;
1030 trace.skip = 0;
1031 trace.entries = entry->caller;
1033 save_stack_trace_user(&trace);
1034 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1035 #endif
1038 void __trace_userstack(struct trace_array *tr,
1039 struct trace_array_cpu *data,
1040 unsigned long flags)
1042 ftrace_trace_userstack(tr, data, flags, preempt_count());
1045 static void
1046 ftrace_trace_special(void *__tr, void *__data,
1047 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1048 int pc)
1050 struct ring_buffer_event *event;
1051 struct trace_array_cpu *data = __data;
1052 struct trace_array *tr = __tr;
1053 struct special_entry *entry;
1054 unsigned long irq_flags;
1056 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1057 &irq_flags);
1058 if (!event)
1059 return;
1060 entry = ring_buffer_event_data(event);
1061 tracing_generic_entry_update(&entry->ent, 0, pc);
1062 entry->ent.type = TRACE_SPECIAL;
1063 entry->arg1 = arg1;
1064 entry->arg2 = arg2;
1065 entry->arg3 = arg3;
1066 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1067 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
1068 ftrace_trace_userstack(tr, data, irq_flags, pc);
1070 trace_wake_up();
1073 void
1074 __trace_special(void *__tr, void *__data,
1075 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1077 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
1080 void
1081 tracing_sched_switch_trace(struct trace_array *tr,
1082 struct trace_array_cpu *data,
1083 struct task_struct *prev,
1084 struct task_struct *next,
1085 unsigned long flags, int pc)
1087 struct ring_buffer_event *event;
1088 struct ctx_switch_entry *entry;
1089 unsigned long irq_flags;
1091 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1092 &irq_flags);
1093 if (!event)
1094 return;
1095 entry = ring_buffer_event_data(event);
1096 tracing_generic_entry_update(&entry->ent, flags, pc);
1097 entry->ent.type = TRACE_CTX;
1098 entry->prev_pid = prev->pid;
1099 entry->prev_prio = prev->prio;
1100 entry->prev_state = prev->state;
1101 entry->next_pid = next->pid;
1102 entry->next_prio = next->prio;
1103 entry->next_state = next->state;
1104 entry->next_cpu = task_cpu(next);
1105 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1106 ftrace_trace_stack(tr, data, flags, 5, pc);
1107 ftrace_trace_userstack(tr, data, flags, pc);
1110 void
1111 tracing_sched_wakeup_trace(struct trace_array *tr,
1112 struct trace_array_cpu *data,
1113 struct task_struct *wakee,
1114 struct task_struct *curr,
1115 unsigned long flags, int pc)
1117 struct ring_buffer_event *event;
1118 struct ctx_switch_entry *entry;
1119 unsigned long irq_flags;
1121 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1122 &irq_flags);
1123 if (!event)
1124 return;
1125 entry = ring_buffer_event_data(event);
1126 tracing_generic_entry_update(&entry->ent, flags, pc);
1127 entry->ent.type = TRACE_WAKE;
1128 entry->prev_pid = curr->pid;
1129 entry->prev_prio = curr->prio;
1130 entry->prev_state = curr->state;
1131 entry->next_pid = wakee->pid;
1132 entry->next_prio = wakee->prio;
1133 entry->next_state = wakee->state;
1134 entry->next_cpu = task_cpu(wakee);
1135 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1136 ftrace_trace_stack(tr, data, flags, 6, pc);
1137 ftrace_trace_userstack(tr, data, flags, pc);
1139 trace_wake_up();
1142 void
1143 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1145 struct trace_array *tr = &global_trace;
1146 struct trace_array_cpu *data;
1147 unsigned long flags;
1148 int cpu;
1149 int pc;
1151 if (tracing_disabled)
1152 return;
1154 pc = preempt_count();
1155 local_irq_save(flags);
1156 cpu = raw_smp_processor_id();
1157 data = tr->data[cpu];
1159 if (likely(atomic_inc_return(&data->disabled) == 1))
1160 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1162 atomic_dec(&data->disabled);
1163 local_irq_restore(flags);
1166 #ifdef CONFIG_FUNCTION_TRACER
1167 static void
1168 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1170 struct trace_array *tr = &global_trace;
1171 struct trace_array_cpu *data;
1172 unsigned long flags;
1173 long disabled;
1174 int cpu, resched;
1175 int pc;
1177 if (unlikely(!ftrace_function_enabled))
1178 return;
1180 pc = preempt_count();
1181 resched = ftrace_preempt_disable();
1182 local_save_flags(flags);
1183 cpu = raw_smp_processor_id();
1184 data = tr->data[cpu];
1185 disabled = atomic_inc_return(&data->disabled);
1187 if (likely(disabled == 1))
1188 trace_function(tr, data, ip, parent_ip, flags, pc);
1190 atomic_dec(&data->disabled);
1191 ftrace_preempt_enable(resched);
1194 static void
1195 function_trace_call(unsigned long ip, unsigned long parent_ip)
1197 struct trace_array *tr = &global_trace;
1198 struct trace_array_cpu *data;
1199 unsigned long flags;
1200 long disabled;
1201 int cpu;
1202 int pc;
1204 if (unlikely(!ftrace_function_enabled))
1205 return;
1208 * Need to use raw, since this must be called before the
1209 * recursive protection is performed.
1211 local_irq_save(flags);
1212 cpu = raw_smp_processor_id();
1213 data = tr->data[cpu];
1214 disabled = atomic_inc_return(&data->disabled);
1216 if (likely(disabled == 1)) {
1217 pc = preempt_count();
1218 trace_function(tr, data, ip, parent_ip, flags, pc);
1221 atomic_dec(&data->disabled);
1222 local_irq_restore(flags);
1225 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1226 int trace_graph_entry(struct ftrace_graph_ent *trace)
1228 struct trace_array *tr = &global_trace;
1229 struct trace_array_cpu *data;
1230 unsigned long flags;
1231 long disabled;
1232 int cpu;
1233 int pc;
1235 if (!ftrace_trace_task(current))
1236 return 0;
1238 if (!ftrace_graph_addr(trace->func))
1239 return 0;
1241 local_irq_save(flags);
1242 cpu = raw_smp_processor_id();
1243 data = tr->data[cpu];
1244 disabled = atomic_inc_return(&data->disabled);
1245 if (likely(disabled == 1)) {
1246 pc = preempt_count();
1247 __trace_graph_entry(tr, data, trace, flags, pc);
1249 /* Only do the atomic if it is not already set */
1250 if (!test_tsk_trace_graph(current))
1251 set_tsk_trace_graph(current);
1252 atomic_dec(&data->disabled);
1253 local_irq_restore(flags);
1255 return 1;
1258 void trace_graph_return(struct ftrace_graph_ret *trace)
1260 struct trace_array *tr = &global_trace;
1261 struct trace_array_cpu *data;
1262 unsigned long flags;
1263 long disabled;
1264 int cpu;
1265 int pc;
1267 local_irq_save(flags);
1268 cpu = raw_smp_processor_id();
1269 data = tr->data[cpu];
1270 disabled = atomic_inc_return(&data->disabled);
1271 if (likely(disabled == 1)) {
1272 pc = preempt_count();
1273 __trace_graph_return(tr, data, trace, flags, pc);
1275 if (!trace->depth)
1276 clear_tsk_trace_graph(current);
1277 atomic_dec(&data->disabled);
1278 local_irq_restore(flags);
1280 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1282 static struct ftrace_ops trace_ops __read_mostly =
1284 .func = function_trace_call,
1287 void tracing_start_function_trace(void)
1289 ftrace_function_enabled = 0;
1291 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1292 trace_ops.func = function_trace_call_preempt_only;
1293 else
1294 trace_ops.func = function_trace_call;
1296 register_ftrace_function(&trace_ops);
1297 ftrace_function_enabled = 1;
1300 void tracing_stop_function_trace(void)
1302 ftrace_function_enabled = 0;
1303 unregister_ftrace_function(&trace_ops);
1305 #endif
1307 enum trace_file_type {
1308 TRACE_FILE_LAT_FMT = 1,
1309 TRACE_FILE_ANNOTATE = 2,
1312 static void trace_iterator_increment(struct trace_iterator *iter)
1314 /* Don't allow ftrace to trace into the ring buffers */
1315 ftrace_disable_cpu();
1317 iter->idx++;
1318 if (iter->buffer_iter[iter->cpu])
1319 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1321 ftrace_enable_cpu();
1324 static struct trace_entry *
1325 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1327 struct ring_buffer_event *event;
1328 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1330 /* Don't allow ftrace to trace into the ring buffers */
1331 ftrace_disable_cpu();
1333 if (buf_iter)
1334 event = ring_buffer_iter_peek(buf_iter, ts);
1335 else
1336 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1338 ftrace_enable_cpu();
1340 return event ? ring_buffer_event_data(event) : NULL;
1343 static struct trace_entry *
1344 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1346 struct ring_buffer *buffer = iter->tr->buffer;
1347 struct trace_entry *ent, *next = NULL;
1348 u64 next_ts = 0, ts;
1349 int next_cpu = -1;
1350 int cpu;
1352 for_each_tracing_cpu(cpu) {
1354 if (ring_buffer_empty_cpu(buffer, cpu))
1355 continue;
1357 ent = peek_next_entry(iter, cpu, &ts);
1360 * Pick the entry with the smallest timestamp:
1362 if (ent && (!next || ts < next_ts)) {
1363 next = ent;
1364 next_cpu = cpu;
1365 next_ts = ts;
1369 if (ent_cpu)
1370 *ent_cpu = next_cpu;
1372 if (ent_ts)
1373 *ent_ts = next_ts;
1375 return next;
1378 /* Find the next real entry, without updating the iterator itself */
1379 static struct trace_entry *
1380 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1382 return __find_next_entry(iter, ent_cpu, ent_ts);
1385 /* Find the next real entry, and increment the iterator to the next entry */
1386 static void *find_next_entry_inc(struct trace_iterator *iter)
1388 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1390 if (iter->ent)
1391 trace_iterator_increment(iter);
1393 return iter->ent ? iter : NULL;
1396 static void trace_consume(struct trace_iterator *iter)
1398 /* Don't allow ftrace to trace into the ring buffers */
1399 ftrace_disable_cpu();
1400 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1401 ftrace_enable_cpu();
1404 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1406 struct trace_iterator *iter = m->private;
1407 int i = (int)*pos;
1408 void *ent;
1410 (*pos)++;
1412 /* can't go backwards */
1413 if (iter->idx > i)
1414 return NULL;
1416 if (iter->idx < 0)
1417 ent = find_next_entry_inc(iter);
1418 else
1419 ent = iter;
1421 while (ent && iter->idx < i)
1422 ent = find_next_entry_inc(iter);
1424 iter->pos = *pos;
1426 return ent;
1429 static void *s_start(struct seq_file *m, loff_t *pos)
1431 struct trace_iterator *iter = m->private;
1432 void *p = NULL;
1433 loff_t l = 0;
1434 int cpu;
1436 mutex_lock(&trace_types_lock);
1438 if (!current_trace || current_trace != iter->trace) {
1439 mutex_unlock(&trace_types_lock);
1440 return NULL;
1443 atomic_inc(&trace_record_cmdline_disabled);
1445 if (*pos != iter->pos) {
1446 iter->ent = NULL;
1447 iter->cpu = 0;
1448 iter->idx = -1;
1450 ftrace_disable_cpu();
1452 for_each_tracing_cpu(cpu) {
1453 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1456 ftrace_enable_cpu();
1458 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1461 } else {
1462 l = *pos - 1;
1463 p = s_next(m, p, &l);
1466 return p;
1469 static void s_stop(struct seq_file *m, void *p)
1471 atomic_dec(&trace_record_cmdline_disabled);
1472 mutex_unlock(&trace_types_lock);
1475 #ifdef CONFIG_KRETPROBES
1476 static inline const char *kretprobed(const char *name)
1478 static const char tramp_name[] = "kretprobe_trampoline";
1479 int size = sizeof(tramp_name);
1481 if (strncmp(tramp_name, name, size) == 0)
1482 return "[unknown/kretprobe'd]";
1483 return name;
1485 #else
1486 static inline const char *kretprobed(const char *name)
1488 return name;
1490 #endif /* CONFIG_KRETPROBES */
1492 static int
1493 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1495 #ifdef CONFIG_KALLSYMS
1496 char str[KSYM_SYMBOL_LEN];
1497 const char *name;
1499 kallsyms_lookup(address, NULL, NULL, NULL, str);
1501 name = kretprobed(str);
1503 return trace_seq_printf(s, fmt, name);
1504 #endif
1505 return 1;
1508 static int
1509 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1510 unsigned long address)
1512 #ifdef CONFIG_KALLSYMS
1513 char str[KSYM_SYMBOL_LEN];
1514 const char *name;
1516 sprint_symbol(str, address);
1517 name = kretprobed(str);
1519 return trace_seq_printf(s, fmt, name);
1520 #endif
1521 return 1;
1524 #ifndef CONFIG_64BIT
1525 # define IP_FMT "%08lx"
1526 #else
1527 # define IP_FMT "%016lx"
1528 #endif
1531 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1533 int ret;
1535 if (!ip)
1536 return trace_seq_printf(s, "0");
1538 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1539 ret = seq_print_sym_offset(s, "%s", ip);
1540 else
1541 ret = seq_print_sym_short(s, "%s", ip);
1543 if (!ret)
1544 return 0;
1546 if (sym_flags & TRACE_ITER_SYM_ADDR)
1547 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1548 return ret;
1551 static inline int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
1552 unsigned long ip, unsigned long sym_flags)
1554 struct file *file = NULL;
1555 unsigned long vmstart = 0;
1556 int ret = 1;
1558 if (mm) {
1559 const struct vm_area_struct *vma;
1561 down_read(&mm->mmap_sem);
1562 vma = find_vma(mm, ip);
1563 if (vma) {
1564 file = vma->vm_file;
1565 vmstart = vma->vm_start;
1567 if (file) {
1568 ret = trace_seq_path(s, &file->f_path);
1569 if (ret)
1570 ret = trace_seq_printf(s, "[+0x%lx]", ip - vmstart);
1572 up_read(&mm->mmap_sem);
1574 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
1575 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1576 return ret;
1579 static int
1580 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
1581 unsigned long sym_flags)
1583 struct mm_struct *mm = NULL;
1584 int ret = 1;
1585 unsigned int i;
1587 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
1588 struct task_struct *task;
1590 * we do the lookup on the thread group leader,
1591 * since individual threads might have already quit!
1593 rcu_read_lock();
1594 task = find_task_by_vpid(entry->ent.tgid);
1595 if (task)
1596 mm = get_task_mm(task);
1597 rcu_read_unlock();
1600 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1601 unsigned long ip = entry->caller[i];
1603 if (ip == ULONG_MAX || !ret)
1604 break;
1605 if (i && ret)
1606 ret = trace_seq_puts(s, " <- ");
1607 if (!ip) {
1608 if (ret)
1609 ret = trace_seq_puts(s, "??");
1610 continue;
1612 if (!ret)
1613 break;
1614 if (ret)
1615 ret = seq_print_user_ip(s, mm, ip, sym_flags);
1618 if (mm)
1619 mmput(mm);
1620 return ret;
1623 static void print_lat_help_header(struct seq_file *m)
1625 seq_puts(m, "# _------=> CPU# \n");
1626 seq_puts(m, "# / _-----=> irqs-off \n");
1627 seq_puts(m, "# | / _----=> need-resched \n");
1628 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1629 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1630 seq_puts(m, "# |||| / \n");
1631 seq_puts(m, "# ||||| delay \n");
1632 seq_puts(m, "# cmd pid ||||| time | caller \n");
1633 seq_puts(m, "# \\ / ||||| \\ | / \n");
1636 static void print_func_help_header(struct seq_file *m)
1638 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1639 seq_puts(m, "# | | | | |\n");
1643 static void
1644 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1646 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1647 struct trace_array *tr = iter->tr;
1648 struct trace_array_cpu *data = tr->data[tr->cpu];
1649 struct tracer *type = current_trace;
1650 unsigned long total;
1651 unsigned long entries;
1652 const char *name = "preemption";
1654 if (type)
1655 name = type->name;
1657 entries = ring_buffer_entries(iter->tr->buffer);
1658 total = entries +
1659 ring_buffer_overruns(iter->tr->buffer);
1661 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1662 name, UTS_RELEASE);
1663 seq_puts(m, "-----------------------------------"
1664 "---------------------------------\n");
1665 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1666 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1667 nsecs_to_usecs(data->saved_latency),
1668 entries,
1669 total,
1670 tr->cpu,
1671 #if defined(CONFIG_PREEMPT_NONE)
1672 "server",
1673 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1674 "desktop",
1675 #elif defined(CONFIG_PREEMPT)
1676 "preempt",
1677 #else
1678 "unknown",
1679 #endif
1680 /* These are reserved for later use */
1681 0, 0, 0, 0);
1682 #ifdef CONFIG_SMP
1683 seq_printf(m, " #P:%d)\n", num_online_cpus());
1684 #else
1685 seq_puts(m, ")\n");
1686 #endif
1687 seq_puts(m, " -----------------\n");
1688 seq_printf(m, " | task: %.16s-%d "
1689 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1690 data->comm, data->pid, data->uid, data->nice,
1691 data->policy, data->rt_priority);
1692 seq_puts(m, " -----------------\n");
1694 if (data->critical_start) {
1695 seq_puts(m, " => started at: ");
1696 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1697 trace_print_seq(m, &iter->seq);
1698 seq_puts(m, "\n => ended at: ");
1699 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1700 trace_print_seq(m, &iter->seq);
1701 seq_puts(m, "\n");
1704 seq_puts(m, "\n");
1707 static void
1708 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1710 int hardirq, softirq;
1711 char *comm;
1713 comm = trace_find_cmdline(entry->pid);
1715 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1716 trace_seq_printf(s, "%3d", cpu);
1717 trace_seq_printf(s, "%c%c",
1718 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1719 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1720 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1722 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1723 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1724 if (hardirq && softirq) {
1725 trace_seq_putc(s, 'H');
1726 } else {
1727 if (hardirq) {
1728 trace_seq_putc(s, 'h');
1729 } else {
1730 if (softirq)
1731 trace_seq_putc(s, 's');
1732 else
1733 trace_seq_putc(s, '.');
1737 if (entry->preempt_count)
1738 trace_seq_printf(s, "%x", entry->preempt_count);
1739 else
1740 trace_seq_puts(s, ".");
1743 unsigned long preempt_mark_thresh = 100;
1745 static void
1746 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1747 unsigned long rel_usecs)
1749 trace_seq_printf(s, " %4lldus", abs_usecs);
1750 if (rel_usecs > preempt_mark_thresh)
1751 trace_seq_puts(s, "!: ");
1752 else if (rel_usecs > 1)
1753 trace_seq_puts(s, "+: ");
1754 else
1755 trace_seq_puts(s, " : ");
1758 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1760 static int task_state_char(unsigned long state)
1762 int bit = state ? __ffs(state) + 1 : 0;
1764 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
1768 * The message is supposed to contain an ending newline.
1769 * If the printing stops prematurely, try to add a newline of our own.
1771 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1773 struct trace_entry *ent;
1774 struct trace_field_cont *cont;
1775 bool ok = true;
1777 ent = peek_next_entry(iter, iter->cpu, NULL);
1778 if (!ent || ent->type != TRACE_CONT) {
1779 trace_seq_putc(s, '\n');
1780 return;
1783 do {
1784 cont = (struct trace_field_cont *)ent;
1785 if (ok)
1786 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1788 ftrace_disable_cpu();
1790 if (iter->buffer_iter[iter->cpu])
1791 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1792 else
1793 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1795 ftrace_enable_cpu();
1797 ent = peek_next_entry(iter, iter->cpu, NULL);
1798 } while (ent && ent->type == TRACE_CONT);
1800 if (!ok)
1801 trace_seq_putc(s, '\n');
1804 static void test_cpu_buff_start(struct trace_iterator *iter)
1806 struct trace_seq *s = &iter->seq;
1808 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1809 return;
1811 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1812 return;
1814 if (cpumask_test_cpu(iter->cpu, iter->started))
1815 return;
1817 cpumask_set_cpu(iter->cpu, iter->started);
1818 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1821 static enum print_line_t
1822 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1824 struct trace_seq *s = &iter->seq;
1825 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1826 struct trace_entry *next_entry;
1827 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1828 struct trace_entry *entry = iter->ent;
1829 unsigned long abs_usecs;
1830 unsigned long rel_usecs;
1831 u64 next_ts;
1832 char *comm;
1833 int S, T;
1834 int i;
1836 if (entry->type == TRACE_CONT)
1837 return TRACE_TYPE_HANDLED;
1839 test_cpu_buff_start(iter);
1841 next_entry = find_next_entry(iter, NULL, &next_ts);
1842 if (!next_entry)
1843 next_ts = iter->ts;
1844 rel_usecs = ns2usecs(next_ts - iter->ts);
1845 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1847 if (verbose) {
1848 comm = trace_find_cmdline(entry->pid);
1849 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1850 " %ld.%03ldms (+%ld.%03ldms): ",
1851 comm,
1852 entry->pid, cpu, entry->flags,
1853 entry->preempt_count, trace_idx,
1854 ns2usecs(iter->ts),
1855 abs_usecs/1000,
1856 abs_usecs % 1000, rel_usecs/1000,
1857 rel_usecs % 1000);
1858 } else {
1859 lat_print_generic(s, entry, cpu);
1860 lat_print_timestamp(s, abs_usecs, rel_usecs);
1862 switch (entry->type) {
1863 case TRACE_FN: {
1864 struct ftrace_entry *field;
1866 trace_assign_type(field, entry);
1868 seq_print_ip_sym(s, field->ip, sym_flags);
1869 trace_seq_puts(s, " (");
1870 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1871 trace_seq_puts(s, ")\n");
1872 break;
1874 case TRACE_CTX:
1875 case TRACE_WAKE: {
1876 struct ctx_switch_entry *field;
1878 trace_assign_type(field, entry);
1880 T = task_state_char(field->next_state);
1881 S = task_state_char(field->prev_state);
1882 comm = trace_find_cmdline(field->next_pid);
1883 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1884 field->prev_pid,
1885 field->prev_prio,
1886 S, entry->type == TRACE_CTX ? "==>" : " +",
1887 field->next_cpu,
1888 field->next_pid,
1889 field->next_prio,
1890 T, comm);
1891 break;
1893 case TRACE_SPECIAL: {
1894 struct special_entry *field;
1896 trace_assign_type(field, entry);
1898 trace_seq_printf(s, "# %ld %ld %ld\n",
1899 field->arg1,
1900 field->arg2,
1901 field->arg3);
1902 break;
1904 case TRACE_STACK: {
1905 struct stack_entry *field;
1907 trace_assign_type(field, entry);
1909 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1910 if (i)
1911 trace_seq_puts(s, " <= ");
1912 seq_print_ip_sym(s, field->caller[i], sym_flags);
1914 trace_seq_puts(s, "\n");
1915 break;
1917 case TRACE_PRINT: {
1918 struct print_entry *field;
1920 trace_assign_type(field, entry);
1922 seq_print_ip_sym(s, field->ip, sym_flags);
1923 trace_seq_printf(s, ": %s", field->buf);
1924 if (entry->flags & TRACE_FLAG_CONT)
1925 trace_seq_print_cont(s, iter);
1926 break;
1928 case TRACE_BRANCH: {
1929 struct trace_branch *field;
1931 trace_assign_type(field, entry);
1933 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1934 field->correct ? " ok " : " MISS ",
1935 field->func,
1936 field->file,
1937 field->line);
1938 break;
1940 case TRACE_USER_STACK: {
1941 struct userstack_entry *field;
1943 trace_assign_type(field, entry);
1945 seq_print_userip_objs(field, s, sym_flags);
1946 trace_seq_putc(s, '\n');
1947 break;
1949 default:
1950 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1952 return TRACE_TYPE_HANDLED;
1955 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1957 struct trace_seq *s = &iter->seq;
1958 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1959 struct trace_entry *entry;
1960 unsigned long usec_rem;
1961 unsigned long long t;
1962 unsigned long secs;
1963 char *comm;
1964 int ret;
1965 int S, T;
1966 int i;
1968 entry = iter->ent;
1970 if (entry->type == TRACE_CONT)
1971 return TRACE_TYPE_HANDLED;
1973 test_cpu_buff_start(iter);
1975 comm = trace_find_cmdline(iter->ent->pid);
1977 t = ns2usecs(iter->ts);
1978 usec_rem = do_div(t, 1000000ULL);
1979 secs = (unsigned long)t;
1981 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1982 if (!ret)
1983 return TRACE_TYPE_PARTIAL_LINE;
1984 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1985 if (!ret)
1986 return TRACE_TYPE_PARTIAL_LINE;
1987 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1988 if (!ret)
1989 return TRACE_TYPE_PARTIAL_LINE;
1991 switch (entry->type) {
1992 case TRACE_FN: {
1993 struct ftrace_entry *field;
1995 trace_assign_type(field, entry);
1997 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1998 if (!ret)
1999 return TRACE_TYPE_PARTIAL_LINE;
2000 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
2001 field->parent_ip) {
2002 ret = trace_seq_printf(s, " <-");
2003 if (!ret)
2004 return TRACE_TYPE_PARTIAL_LINE;
2005 ret = seq_print_ip_sym(s,
2006 field->parent_ip,
2007 sym_flags);
2008 if (!ret)
2009 return TRACE_TYPE_PARTIAL_LINE;
2011 ret = trace_seq_printf(s, "\n");
2012 if (!ret)
2013 return TRACE_TYPE_PARTIAL_LINE;
2014 break;
2016 case TRACE_CTX:
2017 case TRACE_WAKE: {
2018 struct ctx_switch_entry *field;
2020 trace_assign_type(field, entry);
2022 T = task_state_char(field->next_state);
2023 S = task_state_char(field->prev_state);
2024 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
2025 field->prev_pid,
2026 field->prev_prio,
2028 entry->type == TRACE_CTX ? "==>" : " +",
2029 field->next_cpu,
2030 field->next_pid,
2031 field->next_prio,
2033 if (!ret)
2034 return TRACE_TYPE_PARTIAL_LINE;
2035 break;
2037 case TRACE_SPECIAL: {
2038 struct special_entry *field;
2040 trace_assign_type(field, entry);
2042 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2043 field->arg1,
2044 field->arg2,
2045 field->arg3);
2046 if (!ret)
2047 return TRACE_TYPE_PARTIAL_LINE;
2048 break;
2050 case TRACE_STACK: {
2051 struct stack_entry *field;
2053 trace_assign_type(field, entry);
2055 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
2056 if (i) {
2057 ret = trace_seq_puts(s, " <= ");
2058 if (!ret)
2059 return TRACE_TYPE_PARTIAL_LINE;
2061 ret = seq_print_ip_sym(s, field->caller[i],
2062 sym_flags);
2063 if (!ret)
2064 return TRACE_TYPE_PARTIAL_LINE;
2066 ret = trace_seq_puts(s, "\n");
2067 if (!ret)
2068 return TRACE_TYPE_PARTIAL_LINE;
2069 break;
2071 case TRACE_PRINT: {
2072 struct print_entry *field;
2074 trace_assign_type(field, entry);
2076 seq_print_ip_sym(s, field->ip, sym_flags);
2077 trace_seq_printf(s, ": %s", field->buf);
2078 if (entry->flags & TRACE_FLAG_CONT)
2079 trace_seq_print_cont(s, iter);
2080 break;
2082 case TRACE_GRAPH_RET: {
2083 return print_graph_function(iter);
2085 case TRACE_GRAPH_ENT: {
2086 return print_graph_function(iter);
2088 case TRACE_BRANCH: {
2089 struct trace_branch *field;
2091 trace_assign_type(field, entry);
2093 trace_seq_printf(s, "[%s] %s:%s:%d\n",
2094 field->correct ? " ok " : " MISS ",
2095 field->func,
2096 field->file,
2097 field->line);
2098 break;
2100 case TRACE_USER_STACK: {
2101 struct userstack_entry *field;
2103 trace_assign_type(field, entry);
2105 ret = seq_print_userip_objs(field, s, sym_flags);
2106 if (!ret)
2107 return TRACE_TYPE_PARTIAL_LINE;
2108 ret = trace_seq_putc(s, '\n');
2109 if (!ret)
2110 return TRACE_TYPE_PARTIAL_LINE;
2111 break;
2114 return TRACE_TYPE_HANDLED;
2117 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2119 struct trace_seq *s = &iter->seq;
2120 struct trace_entry *entry;
2121 int ret;
2122 int S, T;
2124 entry = iter->ent;
2126 if (entry->type == TRACE_CONT)
2127 return TRACE_TYPE_HANDLED;
2129 ret = trace_seq_printf(s, "%d %d %llu ",
2130 entry->pid, iter->cpu, iter->ts);
2131 if (!ret)
2132 return TRACE_TYPE_PARTIAL_LINE;
2134 switch (entry->type) {
2135 case TRACE_FN: {
2136 struct ftrace_entry *field;
2138 trace_assign_type(field, entry);
2140 ret = trace_seq_printf(s, "%x %x\n",
2141 field->ip,
2142 field->parent_ip);
2143 if (!ret)
2144 return TRACE_TYPE_PARTIAL_LINE;
2145 break;
2147 case TRACE_CTX:
2148 case TRACE_WAKE: {
2149 struct ctx_switch_entry *field;
2151 trace_assign_type(field, entry);
2153 T = task_state_char(field->next_state);
2154 S = entry->type == TRACE_WAKE ? '+' :
2155 task_state_char(field->prev_state);
2156 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
2157 field->prev_pid,
2158 field->prev_prio,
2160 field->next_cpu,
2161 field->next_pid,
2162 field->next_prio,
2164 if (!ret)
2165 return TRACE_TYPE_PARTIAL_LINE;
2166 break;
2168 case TRACE_SPECIAL:
2169 case TRACE_USER_STACK:
2170 case TRACE_STACK: {
2171 struct special_entry *field;
2173 trace_assign_type(field, entry);
2175 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2176 field->arg1,
2177 field->arg2,
2178 field->arg3);
2179 if (!ret)
2180 return TRACE_TYPE_PARTIAL_LINE;
2181 break;
2183 case TRACE_PRINT: {
2184 struct print_entry *field;
2186 trace_assign_type(field, entry);
2188 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
2189 if (entry->flags & TRACE_FLAG_CONT)
2190 trace_seq_print_cont(s, iter);
2191 break;
2194 return TRACE_TYPE_HANDLED;
2197 #define SEQ_PUT_FIELD_RET(s, x) \
2198 do { \
2199 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
2200 return 0; \
2201 } while (0)
2203 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
2204 do { \
2205 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
2206 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
2207 return 0; \
2208 } while (0)
2210 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2212 struct trace_seq *s = &iter->seq;
2213 unsigned char newline = '\n';
2214 struct trace_entry *entry;
2215 int S, T;
2217 entry = iter->ent;
2219 if (entry->type == TRACE_CONT)
2220 return TRACE_TYPE_HANDLED;
2222 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2223 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2224 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2226 switch (entry->type) {
2227 case TRACE_FN: {
2228 struct ftrace_entry *field;
2230 trace_assign_type(field, entry);
2232 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
2233 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
2234 break;
2236 case TRACE_CTX:
2237 case TRACE_WAKE: {
2238 struct ctx_switch_entry *field;
2240 trace_assign_type(field, entry);
2242 T = task_state_char(field->next_state);
2243 S = entry->type == TRACE_WAKE ? '+' :
2244 task_state_char(field->prev_state);
2245 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
2246 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
2247 SEQ_PUT_HEX_FIELD_RET(s, S);
2248 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
2249 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
2250 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
2251 SEQ_PUT_HEX_FIELD_RET(s, T);
2252 break;
2254 case TRACE_SPECIAL:
2255 case TRACE_USER_STACK:
2256 case TRACE_STACK: {
2257 struct special_entry *field;
2259 trace_assign_type(field, entry);
2261 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
2262 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
2263 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
2264 break;
2267 SEQ_PUT_FIELD_RET(s, newline);
2269 return TRACE_TYPE_HANDLED;
2272 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
2274 struct trace_seq *s = &iter->seq;
2275 struct trace_entry *entry = iter->ent;
2276 struct print_entry *field;
2277 int ret;
2279 trace_assign_type(field, entry);
2281 ret = trace_seq_printf(s, field->buf);
2282 if (!ret)
2283 return TRACE_TYPE_PARTIAL_LINE;
2285 if (entry->flags & TRACE_FLAG_CONT)
2286 trace_seq_print_cont(s, iter);
2288 return TRACE_TYPE_HANDLED;
2291 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2293 struct trace_seq *s = &iter->seq;
2294 struct trace_entry *entry;
2296 entry = iter->ent;
2298 if (entry->type == TRACE_CONT)
2299 return TRACE_TYPE_HANDLED;
2301 SEQ_PUT_FIELD_RET(s, entry->pid);
2302 SEQ_PUT_FIELD_RET(s, entry->cpu);
2303 SEQ_PUT_FIELD_RET(s, iter->ts);
2305 switch (entry->type) {
2306 case TRACE_FN: {
2307 struct ftrace_entry *field;
2309 trace_assign_type(field, entry);
2311 SEQ_PUT_FIELD_RET(s, field->ip);
2312 SEQ_PUT_FIELD_RET(s, field->parent_ip);
2313 break;
2315 case TRACE_CTX: {
2316 struct ctx_switch_entry *field;
2318 trace_assign_type(field, entry);
2320 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2321 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2322 SEQ_PUT_FIELD_RET(s, field->prev_state);
2323 SEQ_PUT_FIELD_RET(s, field->next_pid);
2324 SEQ_PUT_FIELD_RET(s, field->next_prio);
2325 SEQ_PUT_FIELD_RET(s, field->next_state);
2326 break;
2328 case TRACE_SPECIAL:
2329 case TRACE_USER_STACK:
2330 case TRACE_STACK: {
2331 struct special_entry *field;
2333 trace_assign_type(field, entry);
2335 SEQ_PUT_FIELD_RET(s, field->arg1);
2336 SEQ_PUT_FIELD_RET(s, field->arg2);
2337 SEQ_PUT_FIELD_RET(s, field->arg3);
2338 break;
2341 return 1;
2344 static int trace_empty(struct trace_iterator *iter)
2346 int cpu;
2348 for_each_tracing_cpu(cpu) {
2349 if (iter->buffer_iter[cpu]) {
2350 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2351 return 0;
2352 } else {
2353 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2354 return 0;
2358 return 1;
2361 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2363 enum print_line_t ret;
2365 if (iter->trace && iter->trace->print_line) {
2366 ret = iter->trace->print_line(iter);
2367 if (ret != TRACE_TYPE_UNHANDLED)
2368 return ret;
2371 if (iter->ent->type == TRACE_PRINT &&
2372 trace_flags & TRACE_ITER_PRINTK &&
2373 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2374 return print_printk_msg_only(iter);
2376 if (trace_flags & TRACE_ITER_BIN)
2377 return print_bin_fmt(iter);
2379 if (trace_flags & TRACE_ITER_HEX)
2380 return print_hex_fmt(iter);
2382 if (trace_flags & TRACE_ITER_RAW)
2383 return print_raw_fmt(iter);
2385 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2386 return print_lat_fmt(iter, iter->idx, iter->cpu);
2388 return print_trace_fmt(iter);
2391 static int s_show(struct seq_file *m, void *v)
2393 struct trace_iterator *iter = v;
2395 if (iter->ent == NULL) {
2396 if (iter->tr) {
2397 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2398 seq_puts(m, "#\n");
2400 if (iter->trace && iter->trace->print_header)
2401 iter->trace->print_header(m);
2402 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2403 /* print nothing if the buffers are empty */
2404 if (trace_empty(iter))
2405 return 0;
2406 print_trace_header(m, iter);
2407 if (!(trace_flags & TRACE_ITER_VERBOSE))
2408 print_lat_help_header(m);
2409 } else {
2410 if (!(trace_flags & TRACE_ITER_VERBOSE))
2411 print_func_help_header(m);
2413 } else {
2414 print_trace_line(iter);
2415 trace_print_seq(m, &iter->seq);
2418 return 0;
2421 static struct seq_operations tracer_seq_ops = {
2422 .start = s_start,
2423 .next = s_next,
2424 .stop = s_stop,
2425 .show = s_show,
2428 static struct trace_iterator *
2429 __tracing_open(struct inode *inode, struct file *file, int *ret)
2431 struct trace_iterator *iter;
2432 struct seq_file *m;
2433 int cpu;
2435 if (tracing_disabled) {
2436 *ret = -ENODEV;
2437 return NULL;
2440 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2441 if (!iter) {
2442 *ret = -ENOMEM;
2443 goto out;
2446 mutex_lock(&trace_types_lock);
2447 if (current_trace && current_trace->print_max)
2448 iter->tr = &max_tr;
2449 else
2450 iter->tr = inode->i_private;
2451 iter->trace = current_trace;
2452 iter->pos = -1;
2454 /* Notify the tracer early; before we stop tracing. */
2455 if (iter->trace && iter->trace->open)
2456 iter->trace->open(iter);
2458 /* Annotate start of buffers if we had overruns */
2459 if (ring_buffer_overruns(iter->tr->buffer))
2460 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2463 for_each_tracing_cpu(cpu) {
2465 iter->buffer_iter[cpu] =
2466 ring_buffer_read_start(iter->tr->buffer, cpu);
2468 if (!iter->buffer_iter[cpu])
2469 goto fail_buffer;
2472 /* TODO stop tracer */
2473 *ret = seq_open(file, &tracer_seq_ops);
2474 if (*ret)
2475 goto fail_buffer;
2477 m = file->private_data;
2478 m->private = iter;
2480 /* stop the trace while dumping */
2481 tracing_stop();
2483 mutex_unlock(&trace_types_lock);
2485 out:
2486 return iter;
2488 fail_buffer:
2489 for_each_tracing_cpu(cpu) {
2490 if (iter->buffer_iter[cpu])
2491 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2493 mutex_unlock(&trace_types_lock);
2494 kfree(iter);
2496 return ERR_PTR(-ENOMEM);
2499 int tracing_open_generic(struct inode *inode, struct file *filp)
2501 if (tracing_disabled)
2502 return -ENODEV;
2504 filp->private_data = inode->i_private;
2505 return 0;
2508 int tracing_release(struct inode *inode, struct file *file)
2510 struct seq_file *m = (struct seq_file *)file->private_data;
2511 struct trace_iterator *iter = m->private;
2512 int cpu;
2514 mutex_lock(&trace_types_lock);
2515 for_each_tracing_cpu(cpu) {
2516 if (iter->buffer_iter[cpu])
2517 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2520 if (iter->trace && iter->trace->close)
2521 iter->trace->close(iter);
2523 /* reenable tracing if it was previously enabled */
2524 tracing_start();
2525 mutex_unlock(&trace_types_lock);
2527 seq_release(inode, file);
2528 kfree(iter);
2529 return 0;
2532 static int tracing_open(struct inode *inode, struct file *file)
2534 int ret;
2536 __tracing_open(inode, file, &ret);
2538 return ret;
2541 static int tracing_lt_open(struct inode *inode, struct file *file)
2543 struct trace_iterator *iter;
2544 int ret;
2546 iter = __tracing_open(inode, file, &ret);
2548 if (!ret)
2549 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2551 return ret;
2555 static void *
2556 t_next(struct seq_file *m, void *v, loff_t *pos)
2558 struct tracer *t = m->private;
2560 (*pos)++;
2562 if (t)
2563 t = t->next;
2565 m->private = t;
2567 return t;
2570 static void *t_start(struct seq_file *m, loff_t *pos)
2572 struct tracer *t = m->private;
2573 loff_t l = 0;
2575 mutex_lock(&trace_types_lock);
2576 for (; t && l < *pos; t = t_next(m, t, &l))
2579 return t;
2582 static void t_stop(struct seq_file *m, void *p)
2584 mutex_unlock(&trace_types_lock);
2587 static int t_show(struct seq_file *m, void *v)
2589 struct tracer *t = v;
2591 if (!t)
2592 return 0;
2594 seq_printf(m, "%s", t->name);
2595 if (t->next)
2596 seq_putc(m, ' ');
2597 else
2598 seq_putc(m, '\n');
2600 return 0;
2603 static struct seq_operations show_traces_seq_ops = {
2604 .start = t_start,
2605 .next = t_next,
2606 .stop = t_stop,
2607 .show = t_show,
2610 static int show_traces_open(struct inode *inode, struct file *file)
2612 int ret;
2614 if (tracing_disabled)
2615 return -ENODEV;
2617 ret = seq_open(file, &show_traces_seq_ops);
2618 if (!ret) {
2619 struct seq_file *m = file->private_data;
2620 m->private = trace_types;
2623 return ret;
2626 static struct file_operations tracing_fops = {
2627 .open = tracing_open,
2628 .read = seq_read,
2629 .llseek = seq_lseek,
2630 .release = tracing_release,
2633 static struct file_operations tracing_lt_fops = {
2634 .open = tracing_lt_open,
2635 .read = seq_read,
2636 .llseek = seq_lseek,
2637 .release = tracing_release,
2640 static struct file_operations show_traces_fops = {
2641 .open = show_traces_open,
2642 .read = seq_read,
2643 .release = seq_release,
2647 * Only trace on a CPU if the bitmask is set:
2649 static cpumask_var_t tracing_cpumask;
2652 * The tracer itself will not take this lock, but still we want
2653 * to provide a consistent cpumask to user-space:
2655 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2658 * Temporary storage for the character representation of the
2659 * CPU bitmask (and one more byte for the newline):
2661 static char mask_str[NR_CPUS + 1];
2663 static ssize_t
2664 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2665 size_t count, loff_t *ppos)
2667 int len;
2669 mutex_lock(&tracing_cpumask_update_lock);
2671 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2672 if (count - len < 2) {
2673 count = -EINVAL;
2674 goto out_err;
2676 len += sprintf(mask_str + len, "\n");
2677 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2679 out_err:
2680 mutex_unlock(&tracing_cpumask_update_lock);
2682 return count;
2685 static ssize_t
2686 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2687 size_t count, loff_t *ppos)
2689 int err, cpu;
2690 cpumask_var_t tracing_cpumask_new;
2692 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2693 return -ENOMEM;
2695 mutex_lock(&tracing_cpumask_update_lock);
2696 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2697 if (err)
2698 goto err_unlock;
2700 local_irq_disable();
2701 __raw_spin_lock(&ftrace_max_lock);
2702 for_each_tracing_cpu(cpu) {
2704 * Increase/decrease the disabled counter if we are
2705 * about to flip a bit in the cpumask:
2707 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2708 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2709 atomic_inc(&global_trace.data[cpu]->disabled);
2711 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2712 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2713 atomic_dec(&global_trace.data[cpu]->disabled);
2716 __raw_spin_unlock(&ftrace_max_lock);
2717 local_irq_enable();
2719 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2721 mutex_unlock(&tracing_cpumask_update_lock);
2722 free_cpumask_var(tracing_cpumask_new);
2724 return count;
2726 err_unlock:
2727 mutex_unlock(&tracing_cpumask_update_lock);
2728 free_cpumask_var(tracing_cpumask);
2730 return err;
2733 static struct file_operations tracing_cpumask_fops = {
2734 .open = tracing_open_generic,
2735 .read = tracing_cpumask_read,
2736 .write = tracing_cpumask_write,
2739 static ssize_t
2740 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2741 size_t cnt, loff_t *ppos)
2743 int i;
2744 char *buf;
2745 int r = 0;
2746 int len = 0;
2747 u32 tracer_flags = current_trace->flags->val;
2748 struct tracer_opt *trace_opts = current_trace->flags->opts;
2751 /* calulate max size */
2752 for (i = 0; trace_options[i]; i++) {
2753 len += strlen(trace_options[i]);
2754 len += 3; /* "no" and space */
2758 * Increase the size with names of options specific
2759 * of the current tracer.
2761 for (i = 0; trace_opts[i].name; i++) {
2762 len += strlen(trace_opts[i].name);
2763 len += 3; /* "no" and space */
2766 /* +2 for \n and \0 */
2767 buf = kmalloc(len + 2, GFP_KERNEL);
2768 if (!buf)
2769 return -ENOMEM;
2771 for (i = 0; trace_options[i]; i++) {
2772 if (trace_flags & (1 << i))
2773 r += sprintf(buf + r, "%s ", trace_options[i]);
2774 else
2775 r += sprintf(buf + r, "no%s ", trace_options[i]);
2778 for (i = 0; trace_opts[i].name; i++) {
2779 if (tracer_flags & trace_opts[i].bit)
2780 r += sprintf(buf + r, "%s ",
2781 trace_opts[i].name);
2782 else
2783 r += sprintf(buf + r, "no%s ",
2784 trace_opts[i].name);
2787 r += sprintf(buf + r, "\n");
2788 WARN_ON(r >= len + 2);
2790 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2792 kfree(buf);
2794 return r;
2797 /* Try to assign a tracer specific option */
2798 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2800 struct tracer_flags *trace_flags = trace->flags;
2801 struct tracer_opt *opts = NULL;
2802 int ret = 0, i = 0;
2803 int len;
2805 for (i = 0; trace_flags->opts[i].name; i++) {
2806 opts = &trace_flags->opts[i];
2807 len = strlen(opts->name);
2809 if (strncmp(cmp, opts->name, len) == 0) {
2810 ret = trace->set_flag(trace_flags->val,
2811 opts->bit, !neg);
2812 break;
2815 /* Not found */
2816 if (!trace_flags->opts[i].name)
2817 return -EINVAL;
2819 /* Refused to handle */
2820 if (ret)
2821 return ret;
2823 if (neg)
2824 trace_flags->val &= ~opts->bit;
2825 else
2826 trace_flags->val |= opts->bit;
2828 return 0;
2831 static ssize_t
2832 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2833 size_t cnt, loff_t *ppos)
2835 char buf[64];
2836 char *cmp = buf;
2837 int neg = 0;
2838 int ret;
2839 int i;
2841 if (cnt >= sizeof(buf))
2842 return -EINVAL;
2844 if (copy_from_user(&buf, ubuf, cnt))
2845 return -EFAULT;
2847 buf[cnt] = 0;
2849 if (strncmp(buf, "no", 2) == 0) {
2850 neg = 1;
2851 cmp += 2;
2854 for (i = 0; trace_options[i]; i++) {
2855 int len = strlen(trace_options[i]);
2857 if (strncmp(cmp, trace_options[i], len) == 0) {
2858 if (neg)
2859 trace_flags &= ~(1 << i);
2860 else
2861 trace_flags |= (1 << i);
2862 break;
2866 /* If no option could be set, test the specific tracer options */
2867 if (!trace_options[i]) {
2868 ret = set_tracer_option(current_trace, cmp, neg);
2869 if (ret)
2870 return ret;
2873 filp->f_pos += cnt;
2875 return cnt;
2878 static struct file_operations tracing_iter_fops = {
2879 .open = tracing_open_generic,
2880 .read = tracing_trace_options_read,
2881 .write = tracing_trace_options_write,
2884 static const char readme_msg[] =
2885 "tracing mini-HOWTO:\n\n"
2886 "# mkdir /debug\n"
2887 "# mount -t debugfs nodev /debug\n\n"
2888 "# cat /debug/tracing/available_tracers\n"
2889 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2890 "# cat /debug/tracing/current_tracer\n"
2891 "none\n"
2892 "# echo sched_switch > /debug/tracing/current_tracer\n"
2893 "# cat /debug/tracing/current_tracer\n"
2894 "sched_switch\n"
2895 "# cat /debug/tracing/trace_options\n"
2896 "noprint-parent nosym-offset nosym-addr noverbose\n"
2897 "# echo print-parent > /debug/tracing/trace_options\n"
2898 "# echo 1 > /debug/tracing/tracing_enabled\n"
2899 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2900 "echo 0 > /debug/tracing/tracing_enabled\n"
2903 static ssize_t
2904 tracing_readme_read(struct file *filp, char __user *ubuf,
2905 size_t cnt, loff_t *ppos)
2907 return simple_read_from_buffer(ubuf, cnt, ppos,
2908 readme_msg, strlen(readme_msg));
2911 static struct file_operations tracing_readme_fops = {
2912 .open = tracing_open_generic,
2913 .read = tracing_readme_read,
2916 static ssize_t
2917 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2918 size_t cnt, loff_t *ppos)
2920 char buf[64];
2921 int r;
2923 r = sprintf(buf, "%u\n", tracer_enabled);
2924 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2927 static ssize_t
2928 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2929 size_t cnt, loff_t *ppos)
2931 struct trace_array *tr = filp->private_data;
2932 char buf[64];
2933 long val;
2934 int ret;
2936 if (cnt >= sizeof(buf))
2937 return -EINVAL;
2939 if (copy_from_user(&buf, ubuf, cnt))
2940 return -EFAULT;
2942 buf[cnt] = 0;
2944 ret = strict_strtoul(buf, 10, &val);
2945 if (ret < 0)
2946 return ret;
2948 val = !!val;
2950 mutex_lock(&trace_types_lock);
2951 if (tracer_enabled ^ val) {
2952 if (val) {
2953 tracer_enabled = 1;
2954 if (current_trace->start)
2955 current_trace->start(tr);
2956 tracing_start();
2957 } else {
2958 tracer_enabled = 0;
2959 tracing_stop();
2960 if (current_trace->stop)
2961 current_trace->stop(tr);
2964 mutex_unlock(&trace_types_lock);
2966 filp->f_pos += cnt;
2968 return cnt;
2971 static ssize_t
2972 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2973 size_t cnt, loff_t *ppos)
2975 char buf[max_tracer_type_len+2];
2976 int r;
2978 mutex_lock(&trace_types_lock);
2979 if (current_trace)
2980 r = sprintf(buf, "%s\n", current_trace->name);
2981 else
2982 r = sprintf(buf, "\n");
2983 mutex_unlock(&trace_types_lock);
2985 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2988 static int tracing_set_tracer(char *buf)
2990 struct trace_array *tr = &global_trace;
2991 struct tracer *t;
2992 int ret = 0;
2994 mutex_lock(&trace_types_lock);
2995 for (t = trace_types; t; t = t->next) {
2996 if (strcmp(t->name, buf) == 0)
2997 break;
2999 if (!t) {
3000 ret = -EINVAL;
3001 goto out;
3003 if (t == current_trace)
3004 goto out;
3006 trace_branch_disable();
3007 if (current_trace && current_trace->reset)
3008 current_trace->reset(tr);
3010 current_trace = t;
3011 if (t->init) {
3012 ret = t->init(tr);
3013 if (ret)
3014 goto out;
3017 trace_branch_enable(tr);
3018 out:
3019 mutex_unlock(&trace_types_lock);
3021 return ret;
3024 static ssize_t
3025 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3026 size_t cnt, loff_t *ppos)
3028 char buf[max_tracer_type_len+1];
3029 int i;
3030 size_t ret;
3031 int err;
3033 ret = cnt;
3035 if (cnt > max_tracer_type_len)
3036 cnt = max_tracer_type_len;
3038 if (copy_from_user(&buf, ubuf, cnt))
3039 return -EFAULT;
3041 buf[cnt] = 0;
3043 /* strip ending whitespace. */
3044 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3045 buf[i] = 0;
3047 err = tracing_set_tracer(buf);
3048 if (err)
3049 return err;
3051 filp->f_pos += ret;
3053 return ret;
3056 static ssize_t
3057 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3058 size_t cnt, loff_t *ppos)
3060 unsigned long *ptr = filp->private_data;
3061 char buf[64];
3062 int r;
3064 r = snprintf(buf, sizeof(buf), "%ld\n",
3065 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3066 if (r > sizeof(buf))
3067 r = sizeof(buf);
3068 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3071 static ssize_t
3072 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3073 size_t cnt, loff_t *ppos)
3075 long *ptr = filp->private_data;
3076 char buf[64];
3077 long val;
3078 int ret;
3080 if (cnt >= sizeof(buf))
3081 return -EINVAL;
3083 if (copy_from_user(&buf, ubuf, cnt))
3084 return -EFAULT;
3086 buf[cnt] = 0;
3088 ret = strict_strtoul(buf, 10, &val);
3089 if (ret < 0)
3090 return ret;
3092 *ptr = val * 1000;
3094 return cnt;
3097 static atomic_t tracing_reader;
3099 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3101 struct trace_iterator *iter;
3103 if (tracing_disabled)
3104 return -ENODEV;
3106 /* We only allow for reader of the pipe */
3107 if (atomic_inc_return(&tracing_reader) != 1) {
3108 atomic_dec(&tracing_reader);
3109 return -EBUSY;
3112 /* create a buffer to store the information to pass to userspace */
3113 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3114 if (!iter)
3115 return -ENOMEM;
3117 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3118 kfree(iter);
3119 return -ENOMEM;
3122 mutex_lock(&trace_types_lock);
3124 /* trace pipe does not show start of buffer */
3125 cpumask_setall(iter->started);
3127 iter->tr = &global_trace;
3128 iter->trace = current_trace;
3129 filp->private_data = iter;
3131 if (iter->trace->pipe_open)
3132 iter->trace->pipe_open(iter);
3133 mutex_unlock(&trace_types_lock);
3135 return 0;
3138 static int tracing_release_pipe(struct inode *inode, struct file *file)
3140 struct trace_iterator *iter = file->private_data;
3142 free_cpumask_var(iter->started);
3143 kfree(iter);
3144 atomic_dec(&tracing_reader);
3146 return 0;
3149 static unsigned int
3150 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3152 struct trace_iterator *iter = filp->private_data;
3154 if (trace_flags & TRACE_ITER_BLOCK) {
3156 * Always select as readable when in blocking mode
3158 return POLLIN | POLLRDNORM;
3159 } else {
3160 if (!trace_empty(iter))
3161 return POLLIN | POLLRDNORM;
3162 poll_wait(filp, &trace_wait, poll_table);
3163 if (!trace_empty(iter))
3164 return POLLIN | POLLRDNORM;
3166 return 0;
3171 * Consumer reader.
3173 static ssize_t
3174 tracing_read_pipe(struct file *filp, char __user *ubuf,
3175 size_t cnt, loff_t *ppos)
3177 struct trace_iterator *iter = filp->private_data;
3178 ssize_t sret;
3180 /* return any leftover data */
3181 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3182 if (sret != -EBUSY)
3183 return sret;
3185 trace_seq_reset(&iter->seq);
3187 mutex_lock(&trace_types_lock);
3188 if (iter->trace->read) {
3189 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3190 if (sret)
3191 goto out;
3194 waitagain:
3195 sret = 0;
3196 while (trace_empty(iter)) {
3198 if ((filp->f_flags & O_NONBLOCK)) {
3199 sret = -EAGAIN;
3200 goto out;
3204 * This is a make-shift waitqueue. The reason we don't use
3205 * an actual wait queue is because:
3206 * 1) we only ever have one waiter
3207 * 2) the tracing, traces all functions, we don't want
3208 * the overhead of calling wake_up and friends
3209 * (and tracing them too)
3210 * Anyway, this is really very primitive wakeup.
3212 set_current_state(TASK_INTERRUPTIBLE);
3213 iter->tr->waiter = current;
3215 mutex_unlock(&trace_types_lock);
3217 /* sleep for 100 msecs, and try again. */
3218 schedule_timeout(HZ/10);
3220 mutex_lock(&trace_types_lock);
3222 iter->tr->waiter = NULL;
3224 if (signal_pending(current)) {
3225 sret = -EINTR;
3226 goto out;
3229 if (iter->trace != current_trace)
3230 goto out;
3233 * We block until we read something and tracing is disabled.
3234 * We still block if tracing is disabled, but we have never
3235 * read anything. This allows a user to cat this file, and
3236 * then enable tracing. But after we have read something,
3237 * we give an EOF when tracing is again disabled.
3239 * iter->pos will be 0 if we haven't read anything.
3241 if (!tracer_enabled && iter->pos)
3242 break;
3244 continue;
3247 /* stop when tracing is finished */
3248 if (trace_empty(iter))
3249 goto out;
3251 if (cnt >= PAGE_SIZE)
3252 cnt = PAGE_SIZE - 1;
3254 /* reset all but tr, trace, and overruns */
3255 memset(&iter->seq, 0,
3256 sizeof(struct trace_iterator) -
3257 offsetof(struct trace_iterator, seq));
3258 iter->pos = -1;
3260 while (find_next_entry_inc(iter) != NULL) {
3261 enum print_line_t ret;
3262 int len = iter->seq.len;
3264 ret = print_trace_line(iter);
3265 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3266 /* don't print partial lines */
3267 iter->seq.len = len;
3268 break;
3271 trace_consume(iter);
3273 if (iter->seq.len >= cnt)
3274 break;
3277 /* Now copy what we have to the user */
3278 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3279 if (iter->seq.readpos >= iter->seq.len)
3280 trace_seq_reset(&iter->seq);
3283 * If there was nothing to send to user, inspite of consuming trace
3284 * entries, go back to wait for more entries.
3286 if (sret == -EBUSY)
3287 goto waitagain;
3289 out:
3290 mutex_unlock(&trace_types_lock);
3292 return sret;
3295 static ssize_t
3296 tracing_entries_read(struct file *filp, char __user *ubuf,
3297 size_t cnt, loff_t *ppos)
3299 struct trace_array *tr = filp->private_data;
3300 char buf[64];
3301 int r;
3303 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3304 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3307 static ssize_t
3308 tracing_entries_write(struct file *filp, const char __user *ubuf,
3309 size_t cnt, loff_t *ppos)
3311 unsigned long val;
3312 char buf[64];
3313 int ret, cpu;
3315 if (cnt >= sizeof(buf))
3316 return -EINVAL;
3318 if (copy_from_user(&buf, ubuf, cnt))
3319 return -EFAULT;
3321 buf[cnt] = 0;
3323 ret = strict_strtoul(buf, 10, &val);
3324 if (ret < 0)
3325 return ret;
3327 /* must have at least 1 entry */
3328 if (!val)
3329 return -EINVAL;
3331 mutex_lock(&trace_types_lock);
3333 tracing_stop();
3335 /* disable all cpu buffers */
3336 for_each_tracing_cpu(cpu) {
3337 if (global_trace.data[cpu])
3338 atomic_inc(&global_trace.data[cpu]->disabled);
3339 if (max_tr.data[cpu])
3340 atomic_inc(&max_tr.data[cpu]->disabled);
3343 /* value is in KB */
3344 val <<= 10;
3346 if (val != global_trace.entries) {
3347 ret = ring_buffer_resize(global_trace.buffer, val);
3348 if (ret < 0) {
3349 cnt = ret;
3350 goto out;
3353 ret = ring_buffer_resize(max_tr.buffer, val);
3354 if (ret < 0) {
3355 int r;
3356 cnt = ret;
3357 r = ring_buffer_resize(global_trace.buffer,
3358 global_trace.entries);
3359 if (r < 0) {
3360 /* AARGH! We are left with different
3361 * size max buffer!!!! */
3362 WARN_ON(1);
3363 tracing_disabled = 1;
3365 goto out;
3368 global_trace.entries = val;
3371 filp->f_pos += cnt;
3373 /* If check pages failed, return ENOMEM */
3374 if (tracing_disabled)
3375 cnt = -ENOMEM;
3376 out:
3377 for_each_tracing_cpu(cpu) {
3378 if (global_trace.data[cpu])
3379 atomic_dec(&global_trace.data[cpu]->disabled);
3380 if (max_tr.data[cpu])
3381 atomic_dec(&max_tr.data[cpu]->disabled);
3384 tracing_start();
3385 max_tr.entries = global_trace.entries;
3386 mutex_unlock(&trace_types_lock);
3388 return cnt;
3391 static int mark_printk(const char *fmt, ...)
3393 int ret;
3394 va_list args;
3395 va_start(args, fmt);
3396 ret = trace_vprintk(0, -1, fmt, args);
3397 va_end(args);
3398 return ret;
3401 static ssize_t
3402 tracing_mark_write(struct file *filp, const char __user *ubuf,
3403 size_t cnt, loff_t *fpos)
3405 char *buf;
3406 char *end;
3408 if (tracing_disabled)
3409 return -EINVAL;
3411 if (cnt > TRACE_BUF_SIZE)
3412 cnt = TRACE_BUF_SIZE;
3414 buf = kmalloc(cnt + 1, GFP_KERNEL);
3415 if (buf == NULL)
3416 return -ENOMEM;
3418 if (copy_from_user(buf, ubuf, cnt)) {
3419 kfree(buf);
3420 return -EFAULT;
3423 /* Cut from the first nil or newline. */
3424 buf[cnt] = '\0';
3425 end = strchr(buf, '\n');
3426 if (end)
3427 *end = '\0';
3429 cnt = mark_printk("%s\n", buf);
3430 kfree(buf);
3431 *fpos += cnt;
3433 return cnt;
3436 static struct file_operations tracing_max_lat_fops = {
3437 .open = tracing_open_generic,
3438 .read = tracing_max_lat_read,
3439 .write = tracing_max_lat_write,
3442 static struct file_operations tracing_ctrl_fops = {
3443 .open = tracing_open_generic,
3444 .read = tracing_ctrl_read,
3445 .write = tracing_ctrl_write,
3448 static struct file_operations set_tracer_fops = {
3449 .open = tracing_open_generic,
3450 .read = tracing_set_trace_read,
3451 .write = tracing_set_trace_write,
3454 static struct file_operations tracing_pipe_fops = {
3455 .open = tracing_open_pipe,
3456 .poll = tracing_poll_pipe,
3457 .read = tracing_read_pipe,
3458 .release = tracing_release_pipe,
3461 static struct file_operations tracing_entries_fops = {
3462 .open = tracing_open_generic,
3463 .read = tracing_entries_read,
3464 .write = tracing_entries_write,
3467 static struct file_operations tracing_mark_fops = {
3468 .open = tracing_open_generic,
3469 .write = tracing_mark_write,
3472 #ifdef CONFIG_DYNAMIC_FTRACE
3474 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3476 return 0;
3479 static ssize_t
3480 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3481 size_t cnt, loff_t *ppos)
3483 static char ftrace_dyn_info_buffer[1024];
3484 static DEFINE_MUTEX(dyn_info_mutex);
3485 unsigned long *p = filp->private_data;
3486 char *buf = ftrace_dyn_info_buffer;
3487 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3488 int r;
3490 mutex_lock(&dyn_info_mutex);
3491 r = sprintf(buf, "%ld ", *p);
3493 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3494 buf[r++] = '\n';
3496 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3498 mutex_unlock(&dyn_info_mutex);
3500 return r;
3503 static struct file_operations tracing_dyn_info_fops = {
3504 .open = tracing_open_generic,
3505 .read = tracing_read_dyn_info,
3507 #endif
3509 static struct dentry *d_tracer;
3511 struct dentry *tracing_init_dentry(void)
3513 static int once;
3515 if (d_tracer)
3516 return d_tracer;
3518 d_tracer = debugfs_create_dir("tracing", NULL);
3520 if (!d_tracer && !once) {
3521 once = 1;
3522 pr_warning("Could not create debugfs directory 'tracing'\n");
3523 return NULL;
3526 return d_tracer;
3529 #ifdef CONFIG_FTRACE_SELFTEST
3530 /* Let selftest have access to static functions in this file */
3531 #include "trace_selftest.c"
3532 #endif
3534 static __init int tracer_init_debugfs(void)
3536 struct dentry *d_tracer;
3537 struct dentry *entry;
3539 d_tracer = tracing_init_dentry();
3541 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3542 &global_trace, &tracing_ctrl_fops);
3543 if (!entry)
3544 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3546 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3547 NULL, &tracing_iter_fops);
3548 if (!entry)
3549 pr_warning("Could not create debugfs 'trace_options' entry\n");
3551 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3552 NULL, &tracing_cpumask_fops);
3553 if (!entry)
3554 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3556 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3557 &global_trace, &tracing_lt_fops);
3558 if (!entry)
3559 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3561 entry = debugfs_create_file("trace", 0444, d_tracer,
3562 &global_trace, &tracing_fops);
3563 if (!entry)
3564 pr_warning("Could not create debugfs 'trace' entry\n");
3566 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3567 &global_trace, &show_traces_fops);
3568 if (!entry)
3569 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3571 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3572 &global_trace, &set_tracer_fops);
3573 if (!entry)
3574 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3576 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3577 &tracing_max_latency,
3578 &tracing_max_lat_fops);
3579 if (!entry)
3580 pr_warning("Could not create debugfs "
3581 "'tracing_max_latency' entry\n");
3583 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3584 &tracing_thresh, &tracing_max_lat_fops);
3585 if (!entry)
3586 pr_warning("Could not create debugfs "
3587 "'tracing_thresh' entry\n");
3588 entry = debugfs_create_file("README", 0644, d_tracer,
3589 NULL, &tracing_readme_fops);
3590 if (!entry)
3591 pr_warning("Could not create debugfs 'README' entry\n");
3593 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3594 NULL, &tracing_pipe_fops);
3595 if (!entry)
3596 pr_warning("Could not create debugfs "
3597 "'trace_pipe' entry\n");
3599 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3600 &global_trace, &tracing_entries_fops);
3601 if (!entry)
3602 pr_warning("Could not create debugfs "
3603 "'buffer_size_kb' entry\n");
3605 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3606 NULL, &tracing_mark_fops);
3607 if (!entry)
3608 pr_warning("Could not create debugfs "
3609 "'trace_marker' entry\n");
3611 #ifdef CONFIG_DYNAMIC_FTRACE
3612 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3613 &ftrace_update_tot_cnt,
3614 &tracing_dyn_info_fops);
3615 if (!entry)
3616 pr_warning("Could not create debugfs "
3617 "'dyn_ftrace_total_info' entry\n");
3618 #endif
3619 #ifdef CONFIG_SYSPROF_TRACER
3620 init_tracer_sysprof_debugfs(d_tracer);
3621 #endif
3622 return 0;
3625 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3627 static DEFINE_SPINLOCK(trace_buf_lock);
3628 static char trace_buf[TRACE_BUF_SIZE];
3630 struct ring_buffer_event *event;
3631 struct trace_array *tr = &global_trace;
3632 struct trace_array_cpu *data;
3633 int cpu, len = 0, size, pc;
3634 struct print_entry *entry;
3635 unsigned long irq_flags;
3637 if (tracing_disabled || tracing_selftest_running)
3638 return 0;
3640 pc = preempt_count();
3641 preempt_disable_notrace();
3642 cpu = raw_smp_processor_id();
3643 data = tr->data[cpu];
3645 if (unlikely(atomic_read(&data->disabled)))
3646 goto out;
3648 pause_graph_tracing();
3649 spin_lock_irqsave(&trace_buf_lock, irq_flags);
3650 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3652 len = min(len, TRACE_BUF_SIZE-1);
3653 trace_buf[len] = 0;
3655 size = sizeof(*entry) + len + 1;
3656 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3657 if (!event)
3658 goto out_unlock;
3659 entry = ring_buffer_event_data(event);
3660 tracing_generic_entry_update(&entry->ent, irq_flags, pc);
3661 entry->ent.type = TRACE_PRINT;
3662 entry->ip = ip;
3663 entry->depth = depth;
3665 memcpy(&entry->buf, trace_buf, len);
3666 entry->buf[len] = 0;
3667 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3669 out_unlock:
3670 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
3671 unpause_graph_tracing();
3672 out:
3673 preempt_enable_notrace();
3675 return len;
3677 EXPORT_SYMBOL_GPL(trace_vprintk);
3679 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3681 int ret;
3682 va_list ap;
3684 if (!(trace_flags & TRACE_ITER_PRINTK))
3685 return 0;
3687 va_start(ap, fmt);
3688 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3689 va_end(ap);
3690 return ret;
3692 EXPORT_SYMBOL_GPL(__ftrace_printk);
3694 static int trace_panic_handler(struct notifier_block *this,
3695 unsigned long event, void *unused)
3697 if (ftrace_dump_on_oops)
3698 ftrace_dump();
3699 return NOTIFY_OK;
3702 static struct notifier_block trace_panic_notifier = {
3703 .notifier_call = trace_panic_handler,
3704 .next = NULL,
3705 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3708 static int trace_die_handler(struct notifier_block *self,
3709 unsigned long val,
3710 void *data)
3712 switch (val) {
3713 case DIE_OOPS:
3714 if (ftrace_dump_on_oops)
3715 ftrace_dump();
3716 break;
3717 default:
3718 break;
3720 return NOTIFY_OK;
3723 static struct notifier_block trace_die_notifier = {
3724 .notifier_call = trace_die_handler,
3725 .priority = 200
3729 * printk is set to max of 1024, we really don't need it that big.
3730 * Nothing should be printing 1000 characters anyway.
3732 #define TRACE_MAX_PRINT 1000
3735 * Define here KERN_TRACE so that we have one place to modify
3736 * it if we decide to change what log level the ftrace dump
3737 * should be at.
3739 #define KERN_TRACE KERN_EMERG
3741 static void
3742 trace_printk_seq(struct trace_seq *s)
3744 /* Probably should print a warning here. */
3745 if (s->len >= 1000)
3746 s->len = 1000;
3748 /* should be zero ended, but we are paranoid. */
3749 s->buffer[s->len] = 0;
3751 printk(KERN_TRACE "%s", s->buffer);
3753 trace_seq_reset(s);
3756 void ftrace_dump(void)
3758 static DEFINE_SPINLOCK(ftrace_dump_lock);
3759 /* use static because iter can be a bit big for the stack */
3760 static struct trace_iterator iter;
3761 static int dump_ran;
3762 unsigned long flags;
3763 int cnt = 0, cpu;
3765 /* only one dump */
3766 spin_lock_irqsave(&ftrace_dump_lock, flags);
3767 if (dump_ran)
3768 goto out;
3770 dump_ran = 1;
3772 /* No turning back! */
3773 tracing_off();
3774 ftrace_kill();
3776 for_each_tracing_cpu(cpu) {
3777 atomic_inc(&global_trace.data[cpu]->disabled);
3780 /* don't look at user memory in panic mode */
3781 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3783 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3785 iter.tr = &global_trace;
3786 iter.trace = current_trace;
3789 * We need to stop all tracing on all CPUS to read the
3790 * the next buffer. This is a bit expensive, but is
3791 * not done often. We fill all what we can read,
3792 * and then release the locks again.
3795 while (!trace_empty(&iter)) {
3797 if (!cnt)
3798 printk(KERN_TRACE "---------------------------------\n");
3800 cnt++;
3802 /* reset all but tr, trace, and overruns */
3803 memset(&iter.seq, 0,
3804 sizeof(struct trace_iterator) -
3805 offsetof(struct trace_iterator, seq));
3806 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3807 iter.pos = -1;
3809 if (find_next_entry_inc(&iter) != NULL) {
3810 print_trace_line(&iter);
3811 trace_consume(&iter);
3814 trace_printk_seq(&iter.seq);
3817 if (!cnt)
3818 printk(KERN_TRACE " (ftrace buffer empty)\n");
3819 else
3820 printk(KERN_TRACE "---------------------------------\n");
3822 out:
3823 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3826 __init static int tracer_alloc_buffers(void)
3828 struct trace_array_cpu *data;
3829 int i;
3830 int ret = -ENOMEM;
3832 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3833 goto out;
3835 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3836 goto out_free_buffer_mask;
3838 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3839 cpumask_copy(tracing_cpumask, cpu_all_mask);
3841 /* TODO: make the number of buffers hot pluggable with CPUS */
3842 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3843 TRACE_BUFFER_FLAGS);
3844 if (!global_trace.buffer) {
3845 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3846 WARN_ON(1);
3847 goto out_free_cpumask;
3849 global_trace.entries = ring_buffer_size(global_trace.buffer);
3852 #ifdef CONFIG_TRACER_MAX_TRACE
3853 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3854 TRACE_BUFFER_FLAGS);
3855 if (!max_tr.buffer) {
3856 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3857 WARN_ON(1);
3858 ring_buffer_free(global_trace.buffer);
3859 goto out_free_cpumask;
3861 max_tr.entries = ring_buffer_size(max_tr.buffer);
3862 WARN_ON(max_tr.entries != global_trace.entries);
3863 #endif
3865 /* Allocate the first page for all buffers */
3866 for_each_tracing_cpu(i) {
3867 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3868 max_tr.data[i] = &per_cpu(max_data, i);
3871 trace_init_cmdlines();
3873 register_tracer(&nop_trace);
3874 #ifdef CONFIG_BOOT_TRACER
3875 register_tracer(&boot_tracer);
3876 current_trace = &boot_tracer;
3877 current_trace->init(&global_trace);
3878 #else
3879 current_trace = &nop_trace;
3880 #endif
3882 /* All seems OK, enable tracing */
3883 tracing_disabled = 0;
3885 atomic_notifier_chain_register(&panic_notifier_list,
3886 &trace_panic_notifier);
3888 register_die_notifier(&trace_die_notifier);
3890 return 0;
3892 out_free_cpumask:
3893 free_cpumask_var(tracing_cpumask);
3894 out_free_buffer_mask:
3895 free_cpumask_var(tracing_buffer_mask);
3896 out:
3897 return ret;
3899 early_initcall(tracer_alloc_buffers);
3900 fs_initcall(tracer_init_debugfs);