2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/ctype.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/gfp.h>
42 #include "trace_output.h"
44 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
50 int ring_buffer_expanded;
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
59 static bool __read_mostly tracing_selftest_running;
62 * If a tracer is running, we do not want to run SELFTEST.
64 bool __read_mostly tracing_selftest_disabled;
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt[] = {
71 static struct tracer_flags dummy_tracer_flags = {
73 .opts = dummy_tracer_opt
76 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
82 * Kill all tracing for good (never come back).
83 * It is initialized to 1 but will turn to zero if the initialization
84 * of the tracer is successful. But that is the only place that sets
87 static int tracing_disabled = 1;
89 DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
91 static inline void ftrace_disable_cpu(void)
94 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
97 static inline void ftrace_enable_cpu(void)
99 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
103 static cpumask_var_t __read_mostly tracing_buffer_mask;
105 /* Define which cpu buffers are currently read in trace_pipe */
106 static cpumask_var_t tracing_reader_cpumask;
108 #define for_each_tracing_cpu(cpu) \
109 for_each_cpu(cpu, tracing_buffer_mask)
112 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
114 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
115 * is set, then ftrace_dump is called. This will output the contents
116 * of the ftrace buffers to the console. This is very useful for
117 * capturing traces that lead to crashes and outputing it to a
120 * It is default off, but you can enable it with either specifying
121 * "ftrace_dump_on_oops" in the kernel command line, or setting
122 * /proc/sys/kernel/ftrace_dump_on_oops to true.
124 int ftrace_dump_on_oops;
126 static int tracing_set_tracer(const char *buf);
128 #define BOOTUP_TRACER_SIZE 100
129 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
130 static char *default_bootup_tracer;
132 static int __init set_ftrace(char *str)
134 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
135 default_bootup_tracer = bootup_tracer_buf;
136 /* We are using ftrace early, expand it */
137 ring_buffer_expanded = 1;
140 __setup("ftrace=", set_ftrace);
142 static int __init set_ftrace_dump_on_oops(char *str)
144 ftrace_dump_on_oops = 1;
147 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
149 unsigned long long ns2usecs(cycle_t nsec)
157 * The global_trace is the descriptor that holds the tracing
158 * buffers for the live tracing. For each CPU, it contains
159 * a link list of pages that will store trace entries. The
160 * page descriptor of the pages in the memory is used to hold
161 * the link list by linking the lru item in the page descriptor
162 * to each of the pages in the buffer per CPU.
164 * For each active CPU there is a data field that holds the
165 * pages for the buffer for that CPU. Each CPU has the same number
166 * of pages allocated for its buffer.
168 static struct trace_array global_trace;
170 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
172 int filter_current_check_discard(struct ring_buffer *buffer,
173 struct ftrace_event_call *call, void *rec,
174 struct ring_buffer_event *event)
176 return filter_check_discard(call, rec, buffer, event);
178 EXPORT_SYMBOL_GPL(filter_current_check_discard);
180 cycle_t ftrace_now(int cpu)
184 /* Early boot up does not have a buffer yet */
185 if (!global_trace.buffer)
186 return trace_clock_local();
188 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
189 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
195 * The max_tr is used to snapshot the global_trace when a maximum
196 * latency is reached. Some tracers will use this to store a maximum
197 * trace while it continues examining live traces.
199 * The buffers for the max_tr are set up the same as the global_trace.
200 * When a snapshot is taken, the link list of the max_tr is swapped
201 * with the link list of the global_trace and the buffers are reset for
202 * the global_trace so the tracing can continue.
204 static struct trace_array max_tr;
206 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
208 /* tracer_enabled is used to toggle activation of a tracer */
209 static int tracer_enabled = 1;
212 * tracing_is_enabled - return tracer_enabled status
214 * This function is used by other tracers to know the status
215 * of the tracer_enabled flag. Tracers may use this function
216 * to know if it should enable their features when starting
217 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
219 int tracing_is_enabled(void)
221 return tracer_enabled;
225 * trace_buf_size is the size in bytes that is allocated
226 * for a buffer. Note, the number of bytes is always rounded
229 * This number is purposely set to a low number of 16384.
230 * If the dump on oops happens, it will be much appreciated
231 * to not have to wait for all that output. Anyway this can be
232 * boot time and run time configurable.
234 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
236 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
238 /* trace_types holds a link list of available tracers. */
239 static struct tracer *trace_types __read_mostly;
241 /* current_trace points to the tracer that is currently active */
242 static struct tracer *current_trace __read_mostly;
245 * max_tracer_type_len is used to simplify the allocating of
246 * buffers to read userspace tracer names. We keep track of
247 * the longest tracer name registered.
249 static int max_tracer_type_len;
252 * trace_types_lock is used to protect the trace_types list.
253 * This lock is also used to keep user access serialized.
254 * Accesses from userspace will grab this lock while userspace
255 * activities happen inside the kernel.
257 static DEFINE_MUTEX(trace_types_lock);
259 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
260 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
262 /* trace_flags holds trace_options default values */
263 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
264 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
265 TRACE_ITER_GRAPH_TIME;
267 static int trace_stop_count;
268 static DEFINE_SPINLOCK(tracing_start_lock);
271 * trace_wake_up - wake up tasks waiting for trace input
273 * Simply wakes up any task that is blocked on the trace_wait
274 * queue. These is used with trace_poll for tasks polling the trace.
276 void trace_wake_up(void)
279 * The runqueue_is_locked() can fail, but this is the best we
282 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
283 wake_up(&trace_wait);
286 static int __init set_buf_size(char *str)
288 unsigned long buf_size;
292 buf_size = memparse(str, &str);
293 /* nr_entries can not be zero */
296 trace_buf_size = buf_size;
299 __setup("trace_buf_size=", set_buf_size);
301 unsigned long nsecs_to_usecs(unsigned long nsecs)
306 /* These must match the bit postions in trace_iterator_flags */
307 static const char *trace_options[] = {
336 { trace_clock_local, "local" },
337 { trace_clock_global, "global" },
343 * trace_parser_get_init - gets the buffer for trace parser
345 int trace_parser_get_init(struct trace_parser *parser, int size)
347 memset(parser, 0, sizeof(*parser));
349 parser->buffer = kmalloc(size, GFP_KERNEL);
358 * trace_parser_put - frees the buffer for trace parser
360 void trace_parser_put(struct trace_parser *parser)
362 kfree(parser->buffer);
366 * trace_get_user - reads the user input string separated by space
367 * (matched by isspace(ch))
369 * For each string found the 'struct trace_parser' is updated,
370 * and the function returns.
372 * Returns number of bytes read.
374 * See kernel/trace/trace.h for 'struct trace_parser' details.
376 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
377 size_t cnt, loff_t *ppos)
384 trace_parser_clear(parser);
386 ret = get_user(ch, ubuf++);
394 * The parser is not finished with the last write,
395 * continue reading the user input without skipping spaces.
398 /* skip white space */
399 while (cnt && isspace(ch)) {
400 ret = get_user(ch, ubuf++);
407 /* only spaces were written */
417 /* read the non-space input */
418 while (cnt && !isspace(ch)) {
419 if (parser->idx < parser->size)
420 parser->buffer[parser->idx++] = ch;
425 ret = get_user(ch, ubuf++);
432 /* We either got finished input or we have to wait for another call. */
434 parser->buffer[parser->idx] = 0;
435 parser->cont = false;
438 parser->buffer[parser->idx++] = ch;
448 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
456 if (s->len <= s->readpos)
459 len = s->len - s->readpos;
462 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
472 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
477 if (s->len <= s->readpos)
480 len = s->len - s->readpos;
483 ret = memcpy(buf, s->buffer + s->readpos, cnt);
492 * ftrace_max_lock is used to protect the swapping of buffers
493 * when taking a max snapshot. The buffers themselves are
494 * protected by per_cpu spinlocks. But the action of the swap
495 * needs its own lock.
497 * This is defined as a raw_spinlock_t in order to help
498 * with performance when lockdep debugging is enabled.
500 * It is also used in other places outside the update_max_tr
501 * so it needs to be defined outside of the
502 * CONFIG_TRACER_MAX_TRACE.
504 static raw_spinlock_t ftrace_max_lock =
505 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
507 #ifdef CONFIG_TRACER_MAX_TRACE
508 unsigned long __read_mostly tracing_max_latency;
509 unsigned long __read_mostly tracing_thresh;
512 * Copy the new maximum trace into the separate maximum-trace
513 * structure. (this way the maximum trace is permanently saved,
514 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
517 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
519 struct trace_array_cpu *data = tr->data[cpu];
520 struct trace_array_cpu *max_data = tr->data[cpu];
523 max_tr.time_start = data->preempt_timestamp;
525 max_data = max_tr.data[cpu];
526 max_data->saved_latency = tracing_max_latency;
527 max_data->critical_start = data->critical_start;
528 max_data->critical_end = data->critical_end;
530 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
531 max_data->pid = tsk->pid;
532 max_data->uid = task_uid(tsk);
533 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
534 max_data->policy = tsk->policy;
535 max_data->rt_priority = tsk->rt_priority;
537 /* record this tasks comm */
538 tracing_record_cmdline(tsk);
542 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
544 * @tsk: the task with the latency
545 * @cpu: The cpu that initiated the trace.
547 * Flip the buffers between the @tr and the max_tr and record information
548 * about which task was the cause of this latency.
551 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
553 struct ring_buffer *buf = tr->buffer;
555 if (trace_stop_count)
558 WARN_ON_ONCE(!irqs_disabled());
559 __raw_spin_lock(&ftrace_max_lock);
561 tr->buffer = max_tr.buffer;
564 __update_max_tr(tr, tsk, cpu);
565 __raw_spin_unlock(&ftrace_max_lock);
569 * update_max_tr_single - only copy one trace over, and reset the rest
571 * @tsk - task with the latency
572 * @cpu - the cpu of the buffer to copy.
574 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
577 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
581 if (trace_stop_count)
584 WARN_ON_ONCE(!irqs_disabled());
585 __raw_spin_lock(&ftrace_max_lock);
587 ftrace_disable_cpu();
589 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
593 * We failed to swap the buffer due to a commit taking
594 * place on this CPU. We fail to record, but we reset
595 * the max trace buffer (no one writes directly to it)
596 * and flag that it failed.
598 trace_array_printk(&max_tr, _THIS_IP_,
599 "Failed to swap buffers due to commit in progress\n");
604 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
606 __update_max_tr(tr, tsk, cpu);
607 __raw_spin_unlock(&ftrace_max_lock);
609 #endif /* CONFIG_TRACER_MAX_TRACE */
612 * register_tracer - register a tracer with the ftrace system.
613 * @type - the plugin for the tracer
615 * Register a new plugin tracer.
617 int register_tracer(struct tracer *type)
618 __releases(kernel_lock)
619 __acquires(kernel_lock)
626 pr_info("Tracer must have a name\n");
631 * When this gets called we hold the BKL which means that
632 * preemption is disabled. Various trace selftests however
633 * need to disable and enable preemption for successful tests.
634 * So we drop the BKL here and grab it after the tests again.
637 mutex_lock(&trace_types_lock);
639 tracing_selftest_running = true;
641 for (t = trace_types; t; t = t->next) {
642 if (strcmp(type->name, t->name) == 0) {
644 pr_info("Trace %s already registered\n",
652 type->set_flag = &dummy_set_flag;
654 type->flags = &dummy_tracer_flags;
656 if (!type->flags->opts)
657 type->flags->opts = dummy_tracer_opt;
658 if (!type->wait_pipe)
659 type->wait_pipe = default_wait_pipe;
662 #ifdef CONFIG_FTRACE_STARTUP_TEST
663 if (type->selftest && !tracing_selftest_disabled) {
664 struct tracer *saved_tracer = current_trace;
665 struct trace_array *tr = &global_trace;
668 * Run a selftest on this tracer.
669 * Here we reset the trace buffer, and set the current
670 * tracer to be this tracer. The tracer can then run some
671 * internal tracing to verify that everything is in order.
672 * If we fail, we do not register this tracer.
674 tracing_reset_online_cpus(tr);
676 current_trace = type;
677 /* the test is responsible for initializing and enabling */
678 pr_info("Testing tracer %s: ", type->name);
679 ret = type->selftest(type, tr);
680 /* the test is responsible for resetting too */
681 current_trace = saved_tracer;
683 printk(KERN_CONT "FAILED!\n");
686 /* Only reset on passing, to avoid touching corrupted buffers */
687 tracing_reset_online_cpus(tr);
689 printk(KERN_CONT "PASSED\n");
693 type->next = trace_types;
695 len = strlen(type->name);
696 if (len > max_tracer_type_len)
697 max_tracer_type_len = len;
700 tracing_selftest_running = false;
701 mutex_unlock(&trace_types_lock);
703 if (ret || !default_bootup_tracer)
706 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
709 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
710 /* Do we want this tracer to start on bootup? */
711 tracing_set_tracer(type->name);
712 default_bootup_tracer = NULL;
713 /* disable other selftests, since this will break it. */
714 tracing_selftest_disabled = 1;
715 #ifdef CONFIG_FTRACE_STARTUP_TEST
716 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
725 void unregister_tracer(struct tracer *type)
730 mutex_lock(&trace_types_lock);
731 for (t = &trace_types; *t; t = &(*t)->next) {
735 pr_info("Trace %s not registered\n", type->name);
741 if (type == current_trace && tracer_enabled) {
744 if (current_trace->stop)
745 current_trace->stop(&global_trace);
746 current_trace = &nop_trace;
749 if (strlen(type->name) != max_tracer_type_len)
752 max_tracer_type_len = 0;
753 for (t = &trace_types; *t; t = &(*t)->next) {
754 len = strlen((*t)->name);
755 if (len > max_tracer_type_len)
756 max_tracer_type_len = len;
759 mutex_unlock(&trace_types_lock);
762 static void __tracing_reset(struct trace_array *tr, int cpu)
764 ftrace_disable_cpu();
765 ring_buffer_reset_cpu(tr->buffer, cpu);
769 void tracing_reset(struct trace_array *tr, int cpu)
771 struct ring_buffer *buffer = tr->buffer;
773 ring_buffer_record_disable(buffer);
775 /* Make sure all commits have finished */
777 __tracing_reset(tr, cpu);
779 ring_buffer_record_enable(buffer);
782 void tracing_reset_online_cpus(struct trace_array *tr)
784 struct ring_buffer *buffer = tr->buffer;
787 ring_buffer_record_disable(buffer);
789 /* Make sure all commits have finished */
792 tr->time_start = ftrace_now(tr->cpu);
794 for_each_online_cpu(cpu)
795 __tracing_reset(tr, cpu);
797 ring_buffer_record_enable(buffer);
800 void tracing_reset_current(int cpu)
802 tracing_reset(&global_trace, cpu);
805 void tracing_reset_current_online_cpus(void)
807 tracing_reset_online_cpus(&global_trace);
810 #define SAVED_CMDLINES 128
811 #define NO_CMDLINE_MAP UINT_MAX
812 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
813 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
814 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
815 static int cmdline_idx;
816 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
818 /* temporary disable recording */
819 static atomic_t trace_record_cmdline_disabled __read_mostly;
821 static void trace_init_cmdlines(void)
823 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
824 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
828 int is_tracing_stopped(void)
830 return trace_stop_count;
834 * ftrace_off_permanent - disable all ftrace code permanently
836 * This should only be called when a serious anomally has
837 * been detected. This will turn off the function tracing,
838 * ring buffers, and other tracing utilites. It takes no
839 * locks and can be called from any context.
841 void ftrace_off_permanent(void)
843 tracing_disabled = 1;
845 tracing_off_permanent();
849 * tracing_start - quick start of the tracer
851 * If tracing is enabled but was stopped by tracing_stop,
852 * this will start the tracer back up.
854 void tracing_start(void)
856 struct ring_buffer *buffer;
859 if (tracing_disabled)
862 spin_lock_irqsave(&tracing_start_lock, flags);
863 if (--trace_stop_count) {
864 if (trace_stop_count < 0) {
865 /* Someone screwed up their debugging */
867 trace_stop_count = 0;
873 buffer = global_trace.buffer;
875 ring_buffer_record_enable(buffer);
877 buffer = max_tr.buffer;
879 ring_buffer_record_enable(buffer);
883 spin_unlock_irqrestore(&tracing_start_lock, flags);
887 * tracing_stop - quick stop of the tracer
889 * Light weight way to stop tracing. Use in conjunction with
892 void tracing_stop(void)
894 struct ring_buffer *buffer;
898 spin_lock_irqsave(&tracing_start_lock, flags);
899 if (trace_stop_count++)
902 buffer = global_trace.buffer;
904 ring_buffer_record_disable(buffer);
906 buffer = max_tr.buffer;
908 ring_buffer_record_disable(buffer);
911 spin_unlock_irqrestore(&tracing_start_lock, flags);
914 void trace_stop_cmdline_recording(void);
916 static void trace_save_cmdline(struct task_struct *tsk)
920 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
924 * It's not the end of the world if we don't get
925 * the lock, but we also don't want to spin
926 * nor do we want to disable interrupts,
927 * so if we miss here, then better luck next time.
929 if (!__raw_spin_trylock(&trace_cmdline_lock))
932 idx = map_pid_to_cmdline[tsk->pid];
933 if (idx == NO_CMDLINE_MAP) {
934 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
937 * Check whether the cmdline buffer at idx has a pid
938 * mapped. We are going to overwrite that entry so we
939 * need to clear the map_pid_to_cmdline. Otherwise we
940 * would read the new comm for the old pid.
942 pid = map_cmdline_to_pid[idx];
943 if (pid != NO_CMDLINE_MAP)
944 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
946 map_cmdline_to_pid[idx] = tsk->pid;
947 map_pid_to_cmdline[tsk->pid] = idx;
952 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
954 __raw_spin_unlock(&trace_cmdline_lock);
957 void trace_find_cmdline(int pid, char comm[])
962 strcpy(comm, "<idle>");
966 if (pid > PID_MAX_DEFAULT) {
967 strcpy(comm, "<...>");
972 __raw_spin_lock(&trace_cmdline_lock);
973 map = map_pid_to_cmdline[pid];
974 if (map != NO_CMDLINE_MAP)
975 strcpy(comm, saved_cmdlines[map]);
977 strcpy(comm, "<...>");
979 __raw_spin_unlock(&trace_cmdline_lock);
983 void tracing_record_cmdline(struct task_struct *tsk)
985 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
989 trace_save_cmdline(tsk);
993 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
996 struct task_struct *tsk = current;
998 entry->preempt_count = pc & 0xff;
999 entry->pid = (tsk) ? tsk->pid : 0;
1000 entry->lock_depth = (tsk) ? tsk->lock_depth : 0;
1002 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1003 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1005 TRACE_FLAG_IRQS_NOSUPPORT |
1007 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1008 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1009 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1011 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1013 struct ring_buffer_event *
1014 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1017 unsigned long flags, int pc)
1019 struct ring_buffer_event *event;
1021 event = ring_buffer_lock_reserve(buffer, len);
1022 if (event != NULL) {
1023 struct trace_entry *ent = ring_buffer_event_data(event);
1025 tracing_generic_entry_update(ent, flags, pc);
1033 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1034 struct ring_buffer_event *event,
1035 unsigned long flags, int pc,
1038 ring_buffer_unlock_commit(buffer, event);
1040 ftrace_trace_stack(buffer, flags, 6, pc);
1041 ftrace_trace_userstack(buffer, flags, pc);
1047 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1048 struct ring_buffer_event *event,
1049 unsigned long flags, int pc)
1051 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1054 struct ring_buffer_event *
1055 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1056 int type, unsigned long len,
1057 unsigned long flags, int pc)
1059 *current_rb = global_trace.buffer;
1060 return trace_buffer_lock_reserve(*current_rb,
1061 type, len, flags, pc);
1063 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1065 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1066 struct ring_buffer_event *event,
1067 unsigned long flags, int pc)
1069 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1071 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1073 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1074 struct ring_buffer_event *event,
1075 unsigned long flags, int pc)
1077 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1079 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1081 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1082 struct ring_buffer_event *event)
1084 ring_buffer_discard_commit(buffer, event);
1086 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1089 trace_function(struct trace_array *tr,
1090 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1093 struct ftrace_event_call *call = &event_function;
1094 struct ring_buffer *buffer = tr->buffer;
1095 struct ring_buffer_event *event;
1096 struct ftrace_entry *entry;
1098 /* If we are reading the ring buffer, don't trace */
1099 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
1102 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1106 entry = ring_buffer_event_data(event);
1108 entry->parent_ip = parent_ip;
1110 if (!filter_check_discard(call, entry, buffer, event))
1111 ring_buffer_unlock_commit(buffer, event);
1115 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1116 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1119 if (likely(!atomic_read(&data->disabled)))
1120 trace_function(tr, ip, parent_ip, flags, pc);
1123 #ifdef CONFIG_STACKTRACE
1124 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1125 unsigned long flags,
1128 struct ftrace_event_call *call = &event_kernel_stack;
1129 struct ring_buffer_event *event;
1130 struct stack_entry *entry;
1131 struct stack_trace trace;
1133 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1134 sizeof(*entry), flags, pc);
1137 entry = ring_buffer_event_data(event);
1138 memset(&entry->caller, 0, sizeof(entry->caller));
1140 trace.nr_entries = 0;
1141 trace.max_entries = FTRACE_STACK_ENTRIES;
1143 trace.entries = entry->caller;
1145 save_stack_trace(&trace);
1146 if (!filter_check_discard(call, entry, buffer, event))
1147 ring_buffer_unlock_commit(buffer, event);
1150 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1153 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1156 __ftrace_trace_stack(buffer, flags, skip, pc);
1159 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1162 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
1166 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1168 struct ftrace_event_call *call = &event_user_stack;
1169 struct ring_buffer_event *event;
1170 struct userstack_entry *entry;
1171 struct stack_trace trace;
1173 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1176 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1177 sizeof(*entry), flags, pc);
1180 entry = ring_buffer_event_data(event);
1182 entry->tgid = current->tgid;
1183 memset(&entry->caller, 0, sizeof(entry->caller));
1185 trace.nr_entries = 0;
1186 trace.max_entries = FTRACE_STACK_ENTRIES;
1188 trace.entries = entry->caller;
1190 save_stack_trace_user(&trace);
1191 if (!filter_check_discard(call, entry, buffer, event))
1192 ring_buffer_unlock_commit(buffer, event);
1196 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1198 ftrace_trace_userstack(tr, flags, preempt_count());
1202 #endif /* CONFIG_STACKTRACE */
1205 ftrace_trace_special(void *__tr,
1206 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1209 struct ring_buffer_event *event;
1210 struct trace_array *tr = __tr;
1211 struct ring_buffer *buffer = tr->buffer;
1212 struct special_entry *entry;
1214 event = trace_buffer_lock_reserve(buffer, TRACE_SPECIAL,
1215 sizeof(*entry), 0, pc);
1218 entry = ring_buffer_event_data(event);
1222 trace_buffer_unlock_commit(buffer, event, 0, pc);
1226 __trace_special(void *__tr, void *__data,
1227 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1229 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1233 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1235 struct trace_array *tr = &global_trace;
1236 struct trace_array_cpu *data;
1237 unsigned long flags;
1241 if (tracing_disabled)
1244 pc = preempt_count();
1245 local_irq_save(flags);
1246 cpu = raw_smp_processor_id();
1247 data = tr->data[cpu];
1249 if (likely(atomic_inc_return(&data->disabled) == 1))
1250 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1252 atomic_dec(&data->disabled);
1253 local_irq_restore(flags);
1257 * trace_vbprintk - write binary msg to tracing buffer
1260 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1262 static raw_spinlock_t trace_buf_lock =
1263 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1264 static u32 trace_buf[TRACE_BUF_SIZE];
1266 struct ftrace_event_call *call = &event_bprint;
1267 struct ring_buffer_event *event;
1268 struct ring_buffer *buffer;
1269 struct trace_array *tr = &global_trace;
1270 struct trace_array_cpu *data;
1271 struct bprint_entry *entry;
1272 unsigned long flags;
1275 int cpu, len = 0, size, pc;
1277 if (unlikely(tracing_selftest_running || tracing_disabled))
1280 /* Don't pollute graph traces with trace_vprintk internals */
1281 pause_graph_tracing();
1283 pc = preempt_count();
1284 resched = ftrace_preempt_disable();
1285 cpu = raw_smp_processor_id();
1286 data = tr->data[cpu];
1288 disable = atomic_inc_return(&data->disabled);
1289 if (unlikely(disable != 1))
1292 /* Lockdep uses trace_printk for lock tracing */
1293 local_irq_save(flags);
1294 __raw_spin_lock(&trace_buf_lock);
1295 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1297 if (len > TRACE_BUF_SIZE || len < 0)
1300 size = sizeof(*entry) + sizeof(u32) * len;
1301 buffer = tr->buffer;
1302 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1306 entry = ring_buffer_event_data(event);
1310 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1311 if (!filter_check_discard(call, entry, buffer, event))
1312 ring_buffer_unlock_commit(buffer, event);
1315 __raw_spin_unlock(&trace_buf_lock);
1316 local_irq_restore(flags);
1319 atomic_dec_return(&data->disabled);
1320 ftrace_preempt_enable(resched);
1321 unpause_graph_tracing();
1325 EXPORT_SYMBOL_GPL(trace_vbprintk);
1327 int trace_array_printk(struct trace_array *tr,
1328 unsigned long ip, const char *fmt, ...)
1333 if (!(trace_flags & TRACE_ITER_PRINTK))
1337 ret = trace_array_vprintk(tr, ip, fmt, ap);
1342 int trace_array_vprintk(struct trace_array *tr,
1343 unsigned long ip, const char *fmt, va_list args)
1345 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1346 static char trace_buf[TRACE_BUF_SIZE];
1348 struct ftrace_event_call *call = &event_print;
1349 struct ring_buffer_event *event;
1350 struct ring_buffer *buffer;
1351 struct trace_array_cpu *data;
1352 int cpu, len = 0, size, pc;
1353 struct print_entry *entry;
1354 unsigned long irq_flags;
1357 if (tracing_disabled || tracing_selftest_running)
1360 pc = preempt_count();
1361 preempt_disable_notrace();
1362 cpu = raw_smp_processor_id();
1363 data = tr->data[cpu];
1365 disable = atomic_inc_return(&data->disabled);
1366 if (unlikely(disable != 1))
1369 pause_graph_tracing();
1370 raw_local_irq_save(irq_flags);
1371 __raw_spin_lock(&trace_buf_lock);
1372 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1374 len = min(len, TRACE_BUF_SIZE-1);
1377 size = sizeof(*entry) + len + 1;
1378 buffer = tr->buffer;
1379 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1383 entry = ring_buffer_event_data(event);
1386 memcpy(&entry->buf, trace_buf, len);
1387 entry->buf[len] = 0;
1388 if (!filter_check_discard(call, entry, buffer, event))
1389 ring_buffer_unlock_commit(buffer, event);
1392 __raw_spin_unlock(&trace_buf_lock);
1393 raw_local_irq_restore(irq_flags);
1394 unpause_graph_tracing();
1396 atomic_dec_return(&data->disabled);
1397 preempt_enable_notrace();
1402 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1404 return trace_array_printk(&global_trace, ip, fmt, args);
1406 EXPORT_SYMBOL_GPL(trace_vprintk);
1408 enum trace_file_type {
1409 TRACE_FILE_LAT_FMT = 1,
1410 TRACE_FILE_ANNOTATE = 2,
1413 static void trace_iterator_increment(struct trace_iterator *iter)
1415 /* Don't allow ftrace to trace into the ring buffers */
1416 ftrace_disable_cpu();
1419 if (iter->buffer_iter[iter->cpu])
1420 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1422 ftrace_enable_cpu();
1425 static struct trace_entry *
1426 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1428 struct ring_buffer_event *event;
1429 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1431 /* Don't allow ftrace to trace into the ring buffers */
1432 ftrace_disable_cpu();
1435 event = ring_buffer_iter_peek(buf_iter, ts);
1437 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1439 ftrace_enable_cpu();
1441 return event ? ring_buffer_event_data(event) : NULL;
1444 static struct trace_entry *
1445 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1447 struct ring_buffer *buffer = iter->tr->buffer;
1448 struct trace_entry *ent, *next = NULL;
1449 int cpu_file = iter->cpu_file;
1450 u64 next_ts = 0, ts;
1455 * If we are in a per_cpu trace file, don't bother by iterating over
1456 * all cpu and peek directly.
1458 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1459 if (ring_buffer_empty_cpu(buffer, cpu_file))
1461 ent = peek_next_entry(iter, cpu_file, ent_ts);
1463 *ent_cpu = cpu_file;
1468 for_each_tracing_cpu(cpu) {
1470 if (ring_buffer_empty_cpu(buffer, cpu))
1473 ent = peek_next_entry(iter, cpu, &ts);
1476 * Pick the entry with the smallest timestamp:
1478 if (ent && (!next || ts < next_ts)) {
1486 *ent_cpu = next_cpu;
1494 /* Find the next real entry, without updating the iterator itself */
1495 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1496 int *ent_cpu, u64 *ent_ts)
1498 return __find_next_entry(iter, ent_cpu, ent_ts);
1501 /* Find the next real entry, and increment the iterator to the next entry */
1502 static void *find_next_entry_inc(struct trace_iterator *iter)
1504 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1507 trace_iterator_increment(iter);
1509 return iter->ent ? iter : NULL;
1512 static void trace_consume(struct trace_iterator *iter)
1514 /* Don't allow ftrace to trace into the ring buffers */
1515 ftrace_disable_cpu();
1516 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1517 ftrace_enable_cpu();
1520 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1522 struct trace_iterator *iter = m->private;
1528 /* can't go backwards */
1533 ent = find_next_entry_inc(iter);
1537 while (ent && iter->idx < i)
1538 ent = find_next_entry_inc(iter);
1545 static void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1547 struct trace_array *tr = iter->tr;
1548 struct ring_buffer_event *event;
1549 struct ring_buffer_iter *buf_iter;
1550 unsigned long entries = 0;
1553 tr->data[cpu]->skipped_entries = 0;
1555 if (!iter->buffer_iter[cpu])
1558 buf_iter = iter->buffer_iter[cpu];
1559 ring_buffer_iter_reset(buf_iter);
1562 * We could have the case with the max latency tracers
1563 * that a reset never took place on a cpu. This is evident
1564 * by the timestamp being before the start of the buffer.
1566 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1567 if (ts >= iter->tr->time_start)
1570 ring_buffer_read(buf_iter, NULL);
1573 tr->data[cpu]->skipped_entries = entries;
1577 * No necessary locking here. The worst thing which can
1578 * happen is loosing events consumed at the same time
1579 * by a trace_pipe reader.
1580 * Other than that, we don't risk to crash the ring buffer
1581 * because it serializes the readers.
1583 * The current tracer is copied to avoid a global locking
1586 static void *s_start(struct seq_file *m, loff_t *pos)
1588 struct trace_iterator *iter = m->private;
1589 static struct tracer *old_tracer;
1590 int cpu_file = iter->cpu_file;
1595 /* copy the tracer to avoid using a global lock all around */
1596 mutex_lock(&trace_types_lock);
1597 if (unlikely(old_tracer != current_trace && current_trace)) {
1598 old_tracer = current_trace;
1599 *iter->trace = *current_trace;
1601 mutex_unlock(&trace_types_lock);
1603 atomic_inc(&trace_record_cmdline_disabled);
1605 if (*pos != iter->pos) {
1610 ftrace_disable_cpu();
1612 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1613 for_each_tracing_cpu(cpu)
1614 tracing_iter_reset(iter, cpu);
1616 tracing_iter_reset(iter, cpu_file);
1618 ftrace_enable_cpu();
1620 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1625 p = s_next(m, p, &l);
1628 trace_event_read_lock();
1632 static void s_stop(struct seq_file *m, void *p)
1634 atomic_dec(&trace_record_cmdline_disabled);
1635 trace_event_read_unlock();
1638 static void print_lat_help_header(struct seq_file *m)
1640 seq_puts(m, "# _------=> CPU# \n");
1641 seq_puts(m, "# / _-----=> irqs-off \n");
1642 seq_puts(m, "# | / _----=> need-resched \n");
1643 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1644 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1645 seq_puts(m, "# |||| /_--=> lock-depth \n");
1646 seq_puts(m, "# |||||/ delay \n");
1647 seq_puts(m, "# cmd pid |||||| time | caller \n");
1648 seq_puts(m, "# \\ / |||||| \\ | / \n");
1651 static void print_func_help_header(struct seq_file *m)
1653 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1654 seq_puts(m, "# | | | | |\n");
1659 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1661 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1662 struct trace_array *tr = iter->tr;
1663 struct trace_array_cpu *data = tr->data[tr->cpu];
1664 struct tracer *type = current_trace;
1665 unsigned long entries = 0;
1666 unsigned long total = 0;
1667 unsigned long count;
1668 const char *name = "preemption";
1675 for_each_tracing_cpu(cpu) {
1676 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1678 * If this buffer has skipped entries, then we hold all
1679 * entries for the trace and we need to ignore the
1680 * ones before the time stamp.
1682 if (tr->data[cpu]->skipped_entries) {
1683 count -= tr->data[cpu]->skipped_entries;
1684 /* total is the same as the entries */
1688 ring_buffer_overrun_cpu(tr->buffer, cpu);
1692 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1694 seq_puts(m, "# -----------------------------------"
1695 "---------------------------------\n");
1696 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1697 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1698 nsecs_to_usecs(data->saved_latency),
1702 #if defined(CONFIG_PREEMPT_NONE)
1704 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1706 #elif defined(CONFIG_PREEMPT)
1711 /* These are reserved for later use */
1714 seq_printf(m, " #P:%d)\n", num_online_cpus());
1718 seq_puts(m, "# -----------------\n");
1719 seq_printf(m, "# | task: %.16s-%d "
1720 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1721 data->comm, data->pid, data->uid, data->nice,
1722 data->policy, data->rt_priority);
1723 seq_puts(m, "# -----------------\n");
1725 if (data->critical_start) {
1726 seq_puts(m, "# => started at: ");
1727 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1728 trace_print_seq(m, &iter->seq);
1729 seq_puts(m, "\n# => ended at: ");
1730 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1731 trace_print_seq(m, &iter->seq);
1732 seq_puts(m, "\n#\n");
1738 static void test_cpu_buff_start(struct trace_iterator *iter)
1740 struct trace_seq *s = &iter->seq;
1742 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1745 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1748 if (cpumask_test_cpu(iter->cpu, iter->started))
1751 if (iter->tr->data[iter->cpu]->skipped_entries)
1754 cpumask_set_cpu(iter->cpu, iter->started);
1756 /* Don't print started cpu buffer for the first entry of the trace */
1758 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1762 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1764 struct trace_seq *s = &iter->seq;
1765 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1766 struct trace_entry *entry;
1767 struct trace_event *event;
1771 test_cpu_buff_start(iter);
1773 event = ftrace_find_event(entry->type);
1775 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1776 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1777 if (!trace_print_lat_context(iter))
1780 if (!trace_print_context(iter))
1786 return event->trace(iter, sym_flags);
1788 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1791 return TRACE_TYPE_HANDLED;
1793 return TRACE_TYPE_PARTIAL_LINE;
1796 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1798 struct trace_seq *s = &iter->seq;
1799 struct trace_entry *entry;
1800 struct trace_event *event;
1804 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1805 if (!trace_seq_printf(s, "%d %d %llu ",
1806 entry->pid, iter->cpu, iter->ts))
1810 event = ftrace_find_event(entry->type);
1812 return event->raw(iter, 0);
1814 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1817 return TRACE_TYPE_HANDLED;
1819 return TRACE_TYPE_PARTIAL_LINE;
1822 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1824 struct trace_seq *s = &iter->seq;
1825 unsigned char newline = '\n';
1826 struct trace_entry *entry;
1827 struct trace_event *event;
1831 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1832 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1833 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1834 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1837 event = ftrace_find_event(entry->type);
1839 enum print_line_t ret = event->hex(iter, 0);
1840 if (ret != TRACE_TYPE_HANDLED)
1844 SEQ_PUT_FIELD_RET(s, newline);
1846 return TRACE_TYPE_HANDLED;
1849 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1851 struct trace_seq *s = &iter->seq;
1852 struct trace_entry *entry;
1853 struct trace_event *event;
1857 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1858 SEQ_PUT_FIELD_RET(s, entry->pid);
1859 SEQ_PUT_FIELD_RET(s, iter->cpu);
1860 SEQ_PUT_FIELD_RET(s, iter->ts);
1863 event = ftrace_find_event(entry->type);
1864 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1867 static int trace_empty(struct trace_iterator *iter)
1871 /* If we are looking at one CPU buffer, only check that one */
1872 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1873 cpu = iter->cpu_file;
1874 if (iter->buffer_iter[cpu]) {
1875 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1878 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1884 for_each_tracing_cpu(cpu) {
1885 if (iter->buffer_iter[cpu]) {
1886 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1889 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1897 /* Called with trace_event_read_lock() held. */
1898 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1900 enum print_line_t ret;
1902 if (iter->trace && iter->trace->print_line) {
1903 ret = iter->trace->print_line(iter);
1904 if (ret != TRACE_TYPE_UNHANDLED)
1908 if (iter->ent->type == TRACE_BPRINT &&
1909 trace_flags & TRACE_ITER_PRINTK &&
1910 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1911 return trace_print_bprintk_msg_only(iter);
1913 if (iter->ent->type == TRACE_PRINT &&
1914 trace_flags & TRACE_ITER_PRINTK &&
1915 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1916 return trace_print_printk_msg_only(iter);
1918 if (trace_flags & TRACE_ITER_BIN)
1919 return print_bin_fmt(iter);
1921 if (trace_flags & TRACE_ITER_HEX)
1922 return print_hex_fmt(iter);
1924 if (trace_flags & TRACE_ITER_RAW)
1925 return print_raw_fmt(iter);
1927 return print_trace_fmt(iter);
1930 static int s_show(struct seq_file *m, void *v)
1932 struct trace_iterator *iter = v;
1934 if (iter->ent == NULL) {
1936 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1939 if (iter->trace && iter->trace->print_header)
1940 iter->trace->print_header(m);
1941 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1942 /* print nothing if the buffers are empty */
1943 if (trace_empty(iter))
1945 print_trace_header(m, iter);
1946 if (!(trace_flags & TRACE_ITER_VERBOSE))
1947 print_lat_help_header(m);
1949 if (!(trace_flags & TRACE_ITER_VERBOSE))
1950 print_func_help_header(m);
1953 print_trace_line(iter);
1954 trace_print_seq(m, &iter->seq);
1960 static struct seq_operations tracer_seq_ops = {
1967 static struct trace_iterator *
1968 __tracing_open(struct inode *inode, struct file *file)
1970 long cpu_file = (long) inode->i_private;
1971 void *fail_ret = ERR_PTR(-ENOMEM);
1972 struct trace_iterator *iter;
1976 if (tracing_disabled)
1977 return ERR_PTR(-ENODEV);
1979 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1981 return ERR_PTR(-ENOMEM);
1984 * We make a copy of the current tracer to avoid concurrent
1985 * changes on it while we are reading.
1987 mutex_lock(&trace_types_lock);
1988 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1993 *iter->trace = *current_trace;
1995 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1998 cpumask_clear(iter->started);
2000 if (current_trace && current_trace->print_max)
2003 iter->tr = &global_trace;
2005 mutex_init(&iter->mutex);
2006 iter->cpu_file = cpu_file;
2008 /* Notify the tracer early; before we stop tracing. */
2009 if (iter->trace && iter->trace->open)
2010 iter->trace->open(iter);
2012 /* Annotate start of buffers if we had overruns */
2013 if (ring_buffer_overruns(iter->tr->buffer))
2014 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2016 /* stop the trace while dumping */
2019 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2020 for_each_tracing_cpu(cpu) {
2022 iter->buffer_iter[cpu] =
2023 ring_buffer_read_start(iter->tr->buffer, cpu);
2024 tracing_iter_reset(iter, cpu);
2027 cpu = iter->cpu_file;
2028 iter->buffer_iter[cpu] =
2029 ring_buffer_read_start(iter->tr->buffer, cpu);
2030 tracing_iter_reset(iter, cpu);
2033 ret = seq_open(file, &tracer_seq_ops);
2035 fail_ret = ERR_PTR(ret);
2039 m = file->private_data;
2042 mutex_unlock(&trace_types_lock);
2047 for_each_tracing_cpu(cpu) {
2048 if (iter->buffer_iter[cpu])
2049 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2051 free_cpumask_var(iter->started);
2054 mutex_unlock(&trace_types_lock);
2061 int tracing_open_generic(struct inode *inode, struct file *filp)
2063 if (tracing_disabled)
2066 filp->private_data = inode->i_private;
2070 static int tracing_release(struct inode *inode, struct file *file)
2072 struct seq_file *m = (struct seq_file *)file->private_data;
2073 struct trace_iterator *iter;
2076 if (!(file->f_mode & FMODE_READ))
2081 mutex_lock(&trace_types_lock);
2082 for_each_tracing_cpu(cpu) {
2083 if (iter->buffer_iter[cpu])
2084 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2087 if (iter->trace && iter->trace->close)
2088 iter->trace->close(iter);
2090 /* reenable tracing if it was previously enabled */
2092 mutex_unlock(&trace_types_lock);
2094 seq_release(inode, file);
2095 mutex_destroy(&iter->mutex);
2096 free_cpumask_var(iter->started);
2102 static int tracing_open(struct inode *inode, struct file *file)
2104 struct trace_iterator *iter;
2107 /* If this file was open for write, then erase contents */
2108 if ((file->f_mode & FMODE_WRITE) &&
2109 (file->f_flags & O_TRUNC)) {
2110 long cpu = (long) inode->i_private;
2112 if (cpu == TRACE_PIPE_ALL_CPU)
2113 tracing_reset_online_cpus(&global_trace);
2115 tracing_reset(&global_trace, cpu);
2118 if (file->f_mode & FMODE_READ) {
2119 iter = __tracing_open(inode, file);
2121 ret = PTR_ERR(iter);
2122 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2123 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2129 t_next(struct seq_file *m, void *v, loff_t *pos)
2131 struct tracer *t = v;
2141 static void *t_start(struct seq_file *m, loff_t *pos)
2146 mutex_lock(&trace_types_lock);
2147 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2153 static void t_stop(struct seq_file *m, void *p)
2155 mutex_unlock(&trace_types_lock);
2158 static int t_show(struct seq_file *m, void *v)
2160 struct tracer *t = v;
2165 seq_printf(m, "%s", t->name);
2174 static struct seq_operations show_traces_seq_ops = {
2181 static int show_traces_open(struct inode *inode, struct file *file)
2183 if (tracing_disabled)
2186 return seq_open(file, &show_traces_seq_ops);
2190 tracing_write_stub(struct file *filp, const char __user *ubuf,
2191 size_t count, loff_t *ppos)
2196 static const struct file_operations tracing_fops = {
2197 .open = tracing_open,
2199 .write = tracing_write_stub,
2200 .llseek = seq_lseek,
2201 .release = tracing_release,
2204 static const struct file_operations show_traces_fops = {
2205 .open = show_traces_open,
2207 .release = seq_release,
2211 * Only trace on a CPU if the bitmask is set:
2213 static cpumask_var_t tracing_cpumask;
2216 * The tracer itself will not take this lock, but still we want
2217 * to provide a consistent cpumask to user-space:
2219 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2222 * Temporary storage for the character representation of the
2223 * CPU bitmask (and one more byte for the newline):
2225 static char mask_str[NR_CPUS + 1];
2228 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2229 size_t count, loff_t *ppos)
2233 mutex_lock(&tracing_cpumask_update_lock);
2235 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2236 if (count - len < 2) {
2240 len += sprintf(mask_str + len, "\n");
2241 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2244 mutex_unlock(&tracing_cpumask_update_lock);
2250 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2251 size_t count, loff_t *ppos)
2254 cpumask_var_t tracing_cpumask_new;
2256 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2259 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2263 mutex_lock(&tracing_cpumask_update_lock);
2265 local_irq_disable();
2266 __raw_spin_lock(&ftrace_max_lock);
2267 for_each_tracing_cpu(cpu) {
2269 * Increase/decrease the disabled counter if we are
2270 * about to flip a bit in the cpumask:
2272 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2273 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2274 atomic_inc(&global_trace.data[cpu]->disabled);
2276 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2277 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2278 atomic_dec(&global_trace.data[cpu]->disabled);
2281 __raw_spin_unlock(&ftrace_max_lock);
2284 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2286 mutex_unlock(&tracing_cpumask_update_lock);
2287 free_cpumask_var(tracing_cpumask_new);
2292 free_cpumask_var(tracing_cpumask_new);
2297 static const struct file_operations tracing_cpumask_fops = {
2298 .open = tracing_open_generic,
2299 .read = tracing_cpumask_read,
2300 .write = tracing_cpumask_write,
2304 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2305 size_t cnt, loff_t *ppos)
2307 struct tracer_opt *trace_opts;
2315 /* calculate max size */
2316 for (i = 0; trace_options[i]; i++) {
2317 len += strlen(trace_options[i]);
2318 len += 3; /* "no" and newline */
2321 mutex_lock(&trace_types_lock);
2322 tracer_flags = current_trace->flags->val;
2323 trace_opts = current_trace->flags->opts;
2326 * Increase the size with names of options specific
2327 * of the current tracer.
2329 for (i = 0; trace_opts[i].name; i++) {
2330 len += strlen(trace_opts[i].name);
2331 len += 3; /* "no" and newline */
2335 buf = kmalloc(len + 1, GFP_KERNEL);
2337 mutex_unlock(&trace_types_lock);
2341 for (i = 0; trace_options[i]; i++) {
2342 if (trace_flags & (1 << i))
2343 r += sprintf(buf + r, "%s\n", trace_options[i]);
2345 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2348 for (i = 0; trace_opts[i].name; i++) {
2349 if (tracer_flags & trace_opts[i].bit)
2350 r += sprintf(buf + r, "%s\n",
2351 trace_opts[i].name);
2353 r += sprintf(buf + r, "no%s\n",
2354 trace_opts[i].name);
2356 mutex_unlock(&trace_types_lock);
2358 WARN_ON(r >= len + 1);
2360 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2366 /* Try to assign a tracer specific option */
2367 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2369 struct tracer_flags *tracer_flags = trace->flags;
2370 struct tracer_opt *opts = NULL;
2374 for (i = 0; tracer_flags->opts[i].name; i++) {
2375 opts = &tracer_flags->opts[i];
2376 len = strlen(opts->name);
2378 if (strncmp(cmp, opts->name, len) == 0) {
2379 ret = trace->set_flag(tracer_flags->val,
2385 if (!tracer_flags->opts[i].name)
2388 /* Refused to handle */
2393 tracer_flags->val &= ~opts->bit;
2395 tracer_flags->val |= opts->bit;
2400 static void set_tracer_flags(unsigned int mask, int enabled)
2402 /* do nothing if flag is already set */
2403 if (!!(trace_flags & mask) == !!enabled)
2407 trace_flags |= mask;
2409 trace_flags &= ~mask;
2413 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2414 size_t cnt, loff_t *ppos)
2422 if (cnt >= sizeof(buf))
2425 if (copy_from_user(&buf, ubuf, cnt))
2430 if (strncmp(buf, "no", 2) == 0) {
2435 for (i = 0; trace_options[i]; i++) {
2436 int len = strlen(trace_options[i]);
2438 if (strncmp(cmp, trace_options[i], len) == 0) {
2439 set_tracer_flags(1 << i, !neg);
2444 /* If no option could be set, test the specific tracer options */
2445 if (!trace_options[i]) {
2446 mutex_lock(&trace_types_lock);
2447 ret = set_tracer_option(current_trace, cmp, neg);
2448 mutex_unlock(&trace_types_lock);
2458 static const struct file_operations tracing_iter_fops = {
2459 .open = tracing_open_generic,
2460 .read = tracing_trace_options_read,
2461 .write = tracing_trace_options_write,
2464 static const char readme_msg[] =
2465 "tracing mini-HOWTO:\n\n"
2466 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2467 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2468 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2469 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2471 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2472 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2474 "# cat /sys/kernel/debug/tracing/trace_options\n"
2475 "noprint-parent nosym-offset nosym-addr noverbose\n"
2476 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2477 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2478 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2479 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2483 tracing_readme_read(struct file *filp, char __user *ubuf,
2484 size_t cnt, loff_t *ppos)
2486 return simple_read_from_buffer(ubuf, cnt, ppos,
2487 readme_msg, strlen(readme_msg));
2490 static const struct file_operations tracing_readme_fops = {
2491 .open = tracing_open_generic,
2492 .read = tracing_readme_read,
2496 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2497 size_t cnt, loff_t *ppos)
2506 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2510 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2518 for (i = 0; i < SAVED_CMDLINES; i++) {
2521 pid = map_cmdline_to_pid[i];
2522 if (pid == -1 || pid == NO_CMDLINE_MAP)
2525 trace_find_cmdline(pid, buf_comm);
2526 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2531 len = simple_read_from_buffer(ubuf, cnt, ppos,
2540 static const struct file_operations tracing_saved_cmdlines_fops = {
2541 .open = tracing_open_generic,
2542 .read = tracing_saved_cmdlines_read,
2546 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2547 size_t cnt, loff_t *ppos)
2552 r = sprintf(buf, "%u\n", tracer_enabled);
2553 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2557 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2558 size_t cnt, loff_t *ppos)
2560 struct trace_array *tr = filp->private_data;
2565 if (cnt >= sizeof(buf))
2568 if (copy_from_user(&buf, ubuf, cnt))
2573 ret = strict_strtoul(buf, 10, &val);
2579 mutex_lock(&trace_types_lock);
2580 if (tracer_enabled ^ val) {
2583 if (current_trace->start)
2584 current_trace->start(tr);
2589 if (current_trace->stop)
2590 current_trace->stop(tr);
2593 mutex_unlock(&trace_types_lock);
2601 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2602 size_t cnt, loff_t *ppos)
2604 char buf[max_tracer_type_len+2];
2607 mutex_lock(&trace_types_lock);
2609 r = sprintf(buf, "%s\n", current_trace->name);
2611 r = sprintf(buf, "\n");
2612 mutex_unlock(&trace_types_lock);
2614 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2617 int tracer_init(struct tracer *t, struct trace_array *tr)
2619 tracing_reset_online_cpus(tr);
2623 static int tracing_resize_ring_buffer(unsigned long size)
2628 * If kernel or user changes the size of the ring buffer
2629 * we use the size that was given, and we can forget about
2630 * expanding it later.
2632 ring_buffer_expanded = 1;
2634 ret = ring_buffer_resize(global_trace.buffer, size);
2638 ret = ring_buffer_resize(max_tr.buffer, size);
2642 r = ring_buffer_resize(global_trace.buffer,
2643 global_trace.entries);
2646 * AARGH! We are left with different
2647 * size max buffer!!!!
2648 * The max buffer is our "snapshot" buffer.
2649 * When a tracer needs a snapshot (one of the
2650 * latency tracers), it swaps the max buffer
2651 * with the saved snap shot. We succeeded to
2652 * update the size of the main buffer, but failed to
2653 * update the size of the max buffer. But when we tried
2654 * to reset the main buffer to the original size, we
2655 * failed there too. This is very unlikely to
2656 * happen, but if it does, warn and kill all
2660 tracing_disabled = 1;
2665 global_trace.entries = size;
2671 * tracing_update_buffers - used by tracing facility to expand ring buffers
2673 * To save on memory when the tracing is never used on a system with it
2674 * configured in. The ring buffers are set to a minimum size. But once
2675 * a user starts to use the tracing facility, then they need to grow
2676 * to their default size.
2678 * This function is to be called when a tracer is about to be used.
2680 int tracing_update_buffers(void)
2684 mutex_lock(&trace_types_lock);
2685 if (!ring_buffer_expanded)
2686 ret = tracing_resize_ring_buffer(trace_buf_size);
2687 mutex_unlock(&trace_types_lock);
2692 struct trace_option_dentry;
2694 static struct trace_option_dentry *
2695 create_trace_option_files(struct tracer *tracer);
2698 destroy_trace_option_files(struct trace_option_dentry *topts);
2700 static int tracing_set_tracer(const char *buf)
2702 static struct trace_option_dentry *topts;
2703 struct trace_array *tr = &global_trace;
2707 mutex_lock(&trace_types_lock);
2709 if (!ring_buffer_expanded) {
2710 ret = tracing_resize_ring_buffer(trace_buf_size);
2716 for (t = trace_types; t; t = t->next) {
2717 if (strcmp(t->name, buf) == 0)
2724 if (t == current_trace)
2727 trace_branch_disable();
2728 if (current_trace && current_trace->reset)
2729 current_trace->reset(tr);
2731 destroy_trace_option_files(topts);
2735 topts = create_trace_option_files(current_trace);
2738 ret = tracer_init(t, tr);
2743 trace_branch_enable(tr);
2745 mutex_unlock(&trace_types_lock);
2751 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2752 size_t cnt, loff_t *ppos)
2754 char buf[max_tracer_type_len+1];
2761 if (cnt > max_tracer_type_len)
2762 cnt = max_tracer_type_len;
2764 if (copy_from_user(&buf, ubuf, cnt))
2769 /* strip ending whitespace. */
2770 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2773 err = tracing_set_tracer(buf);
2783 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2784 size_t cnt, loff_t *ppos)
2786 unsigned long *ptr = filp->private_data;
2790 r = snprintf(buf, sizeof(buf), "%ld\n",
2791 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2792 if (r > sizeof(buf))
2794 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2798 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2799 size_t cnt, loff_t *ppos)
2801 unsigned long *ptr = filp->private_data;
2806 if (cnt >= sizeof(buf))
2809 if (copy_from_user(&buf, ubuf, cnt))
2814 ret = strict_strtoul(buf, 10, &val);
2823 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2825 long cpu_file = (long) inode->i_private;
2826 struct trace_iterator *iter;
2829 if (tracing_disabled)
2832 mutex_lock(&trace_types_lock);
2834 /* We only allow one reader per cpu */
2835 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2836 if (!cpumask_empty(tracing_reader_cpumask)) {
2840 cpumask_setall(tracing_reader_cpumask);
2842 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2843 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2850 /* create a buffer to store the information to pass to userspace */
2851 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2858 * We make a copy of the current tracer to avoid concurrent
2859 * changes on it while we are reading.
2861 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2867 *iter->trace = *current_trace;
2869 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2874 /* trace pipe does not show start of buffer */
2875 cpumask_setall(iter->started);
2877 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2878 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2880 iter->cpu_file = cpu_file;
2881 iter->tr = &global_trace;
2882 mutex_init(&iter->mutex);
2883 filp->private_data = iter;
2885 if (iter->trace->pipe_open)
2886 iter->trace->pipe_open(iter);
2889 mutex_unlock(&trace_types_lock);
2895 mutex_unlock(&trace_types_lock);
2899 static int tracing_release_pipe(struct inode *inode, struct file *file)
2901 struct trace_iterator *iter = file->private_data;
2903 mutex_lock(&trace_types_lock);
2905 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2906 cpumask_clear(tracing_reader_cpumask);
2908 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2910 mutex_unlock(&trace_types_lock);
2912 free_cpumask_var(iter->started);
2913 mutex_destroy(&iter->mutex);
2921 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2923 struct trace_iterator *iter = filp->private_data;
2925 if (trace_flags & TRACE_ITER_BLOCK) {
2927 * Always select as readable when in blocking mode
2929 return POLLIN | POLLRDNORM;
2931 if (!trace_empty(iter))
2932 return POLLIN | POLLRDNORM;
2933 poll_wait(filp, &trace_wait, poll_table);
2934 if (!trace_empty(iter))
2935 return POLLIN | POLLRDNORM;
2942 void default_wait_pipe(struct trace_iterator *iter)
2946 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2948 if (trace_empty(iter))
2951 finish_wait(&trace_wait, &wait);
2955 * This is a make-shift waitqueue.
2956 * A tracer might use this callback on some rare cases:
2958 * 1) the current tracer might hold the runqueue lock when it wakes up
2959 * a reader, hence a deadlock (sched, function, and function graph tracers)
2960 * 2) the function tracers, trace all functions, we don't want
2961 * the overhead of calling wake_up and friends
2962 * (and tracing them too)
2964 * Anyway, this is really very primitive wakeup.
2966 void poll_wait_pipe(struct trace_iterator *iter)
2968 set_current_state(TASK_INTERRUPTIBLE);
2969 /* sleep for 100 msecs, and try again. */
2970 schedule_timeout(HZ / 10);
2973 /* Must be called with trace_types_lock mutex held. */
2974 static int tracing_wait_pipe(struct file *filp)
2976 struct trace_iterator *iter = filp->private_data;
2978 while (trace_empty(iter)) {
2980 if ((filp->f_flags & O_NONBLOCK)) {
2984 mutex_unlock(&iter->mutex);
2986 iter->trace->wait_pipe(iter);
2988 mutex_lock(&iter->mutex);
2990 if (signal_pending(current))
2994 * We block until we read something and tracing is disabled.
2995 * We still block if tracing is disabled, but we have never
2996 * read anything. This allows a user to cat this file, and
2997 * then enable tracing. But after we have read something,
2998 * we give an EOF when tracing is again disabled.
3000 * iter->pos will be 0 if we haven't read anything.
3002 if (!tracer_enabled && iter->pos)
3013 tracing_read_pipe(struct file *filp, char __user *ubuf,
3014 size_t cnt, loff_t *ppos)
3016 struct trace_iterator *iter = filp->private_data;
3017 static struct tracer *old_tracer;
3020 /* return any leftover data */
3021 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3025 trace_seq_init(&iter->seq);
3027 /* copy the tracer to avoid using a global lock all around */
3028 mutex_lock(&trace_types_lock);
3029 if (unlikely(old_tracer != current_trace && current_trace)) {
3030 old_tracer = current_trace;
3031 *iter->trace = *current_trace;
3033 mutex_unlock(&trace_types_lock);
3036 * Avoid more than one consumer on a single file descriptor
3037 * This is just a matter of traces coherency, the ring buffer itself
3040 mutex_lock(&iter->mutex);
3041 if (iter->trace->read) {
3042 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3048 sret = tracing_wait_pipe(filp);
3052 /* stop when tracing is finished */
3053 if (trace_empty(iter)) {
3058 if (cnt >= PAGE_SIZE)
3059 cnt = PAGE_SIZE - 1;
3061 /* reset all but tr, trace, and overruns */
3062 memset(&iter->seq, 0,
3063 sizeof(struct trace_iterator) -
3064 offsetof(struct trace_iterator, seq));
3067 trace_event_read_lock();
3068 while (find_next_entry_inc(iter) != NULL) {
3069 enum print_line_t ret;
3070 int len = iter->seq.len;
3072 ret = print_trace_line(iter);
3073 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3074 /* don't print partial lines */
3075 iter->seq.len = len;
3078 if (ret != TRACE_TYPE_NO_CONSUME)
3079 trace_consume(iter);
3081 if (iter->seq.len >= cnt)
3084 trace_event_read_unlock();
3086 /* Now copy what we have to the user */
3087 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3088 if (iter->seq.readpos >= iter->seq.len)
3089 trace_seq_init(&iter->seq);
3092 * If there was nothing to send to user, inspite of consuming trace
3093 * entries, go back to wait for more entries.
3099 mutex_unlock(&iter->mutex);
3104 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3105 struct pipe_buffer *buf)
3107 __free_page(buf->page);
3110 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3113 __free_page(spd->pages[idx]);
3116 static struct pipe_buf_operations tracing_pipe_buf_ops = {
3118 .map = generic_pipe_buf_map,
3119 .unmap = generic_pipe_buf_unmap,
3120 .confirm = generic_pipe_buf_confirm,
3121 .release = tracing_pipe_buf_release,
3122 .steal = generic_pipe_buf_steal,
3123 .get = generic_pipe_buf_get,
3127 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3132 /* Seq buffer is page-sized, exactly what we need. */
3134 count = iter->seq.len;
3135 ret = print_trace_line(iter);
3136 count = iter->seq.len - count;
3139 iter->seq.len -= count;
3142 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3143 iter->seq.len -= count;
3147 if (ret != TRACE_TYPE_NO_CONSUME)
3148 trace_consume(iter);
3150 if (!find_next_entry_inc(iter)) {
3160 static ssize_t tracing_splice_read_pipe(struct file *filp,
3162 struct pipe_inode_info *pipe,
3166 struct page *pages[PIPE_BUFFERS];
3167 struct partial_page partial[PIPE_BUFFERS];
3168 struct trace_iterator *iter = filp->private_data;
3169 struct splice_pipe_desc spd = {
3172 .nr_pages = 0, /* This gets updated below. */
3174 .ops = &tracing_pipe_buf_ops,
3175 .spd_release = tracing_spd_release_pipe,
3177 static struct tracer *old_tracer;
3182 /* copy the tracer to avoid using a global lock all around */
3183 mutex_lock(&trace_types_lock);
3184 if (unlikely(old_tracer != current_trace && current_trace)) {
3185 old_tracer = current_trace;
3186 *iter->trace = *current_trace;
3188 mutex_unlock(&trace_types_lock);
3190 mutex_lock(&iter->mutex);
3192 if (iter->trace->splice_read) {
3193 ret = iter->trace->splice_read(iter, filp,
3194 ppos, pipe, len, flags);
3199 ret = tracing_wait_pipe(filp);
3203 if (!iter->ent && !find_next_entry_inc(iter)) {
3208 trace_event_read_lock();
3210 /* Fill as many pages as possible. */
3211 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3212 pages[i] = alloc_page(GFP_KERNEL);
3216 rem = tracing_fill_pipe_page(rem, iter);
3218 /* Copy the data into the page, so we can start over. */
3219 ret = trace_seq_to_buffer(&iter->seq,
3220 page_address(pages[i]),
3223 __free_page(pages[i]);
3226 partial[i].offset = 0;
3227 partial[i].len = iter->seq.len;
3229 trace_seq_init(&iter->seq);
3232 trace_event_read_unlock();
3233 mutex_unlock(&iter->mutex);
3237 return splice_to_pipe(pipe, &spd);
3240 mutex_unlock(&iter->mutex);
3246 tracing_entries_read(struct file *filp, char __user *ubuf,
3247 size_t cnt, loff_t *ppos)
3249 struct trace_array *tr = filp->private_data;
3253 mutex_lock(&trace_types_lock);
3254 if (!ring_buffer_expanded)
3255 r = sprintf(buf, "%lu (expanded: %lu)\n",
3257 trace_buf_size >> 10);
3259 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3260 mutex_unlock(&trace_types_lock);
3262 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3266 tracing_entries_write(struct file *filp, const char __user *ubuf,
3267 size_t cnt, loff_t *ppos)
3273 if (cnt >= sizeof(buf))
3276 if (copy_from_user(&buf, ubuf, cnt))
3281 ret = strict_strtoul(buf, 10, &val);
3285 /* must have at least 1 entry */
3289 mutex_lock(&trace_types_lock);
3293 /* disable all cpu buffers */
3294 for_each_tracing_cpu(cpu) {
3295 if (global_trace.data[cpu])
3296 atomic_inc(&global_trace.data[cpu]->disabled);
3297 if (max_tr.data[cpu])
3298 atomic_inc(&max_tr.data[cpu]->disabled);
3301 /* value is in KB */
3304 if (val != global_trace.entries) {
3305 ret = tracing_resize_ring_buffer(val);
3314 /* If check pages failed, return ENOMEM */
3315 if (tracing_disabled)
3318 for_each_tracing_cpu(cpu) {
3319 if (global_trace.data[cpu])
3320 atomic_dec(&global_trace.data[cpu]->disabled);
3321 if (max_tr.data[cpu])
3322 atomic_dec(&max_tr.data[cpu]->disabled);
3326 max_tr.entries = global_trace.entries;
3327 mutex_unlock(&trace_types_lock);
3332 static int mark_printk(const char *fmt, ...)
3336 va_start(args, fmt);
3337 ret = trace_vprintk(0, fmt, args);
3343 tracing_mark_write(struct file *filp, const char __user *ubuf,
3344 size_t cnt, loff_t *fpos)
3349 if (tracing_disabled)
3352 if (cnt > TRACE_BUF_SIZE)
3353 cnt = TRACE_BUF_SIZE;
3355 buf = kmalloc(cnt + 1, GFP_KERNEL);
3359 if (copy_from_user(buf, ubuf, cnt)) {
3364 /* Cut from the first nil or newline. */
3366 end = strchr(buf, '\n');
3370 cnt = mark_printk("%s\n", buf);
3377 static ssize_t tracing_clock_read(struct file *filp, char __user *ubuf,
3378 size_t cnt, loff_t *ppos)
3384 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3385 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter,
3386 "%s%s%s%s", i ? " " : "",
3387 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3388 i == trace_clock_id ? "]" : "");
3389 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter, "\n");
3391 return simple_read_from_buffer(ubuf, cnt, ppos, buf, bufiter);
3394 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3395 size_t cnt, loff_t *fpos)
3398 const char *clockstr;
3401 if (cnt >= sizeof(buf))
3404 if (copy_from_user(&buf, ubuf, cnt))
3409 clockstr = strstrip(buf);
3411 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3412 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3415 if (i == ARRAY_SIZE(trace_clocks))
3420 mutex_lock(&trace_types_lock);
3422 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3424 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3426 mutex_unlock(&trace_types_lock);
3433 static const struct file_operations tracing_max_lat_fops = {
3434 .open = tracing_open_generic,
3435 .read = tracing_max_lat_read,
3436 .write = tracing_max_lat_write,
3439 static const struct file_operations tracing_ctrl_fops = {
3440 .open = tracing_open_generic,
3441 .read = tracing_ctrl_read,
3442 .write = tracing_ctrl_write,
3445 static const struct file_operations set_tracer_fops = {
3446 .open = tracing_open_generic,
3447 .read = tracing_set_trace_read,
3448 .write = tracing_set_trace_write,
3451 static const struct file_operations tracing_pipe_fops = {
3452 .open = tracing_open_pipe,
3453 .poll = tracing_poll_pipe,
3454 .read = tracing_read_pipe,
3455 .splice_read = tracing_splice_read_pipe,
3456 .release = tracing_release_pipe,
3459 static const struct file_operations tracing_entries_fops = {
3460 .open = tracing_open_generic,
3461 .read = tracing_entries_read,
3462 .write = tracing_entries_write,
3465 static const struct file_operations tracing_mark_fops = {
3466 .open = tracing_open_generic,
3467 .write = tracing_mark_write,
3470 static const struct file_operations trace_clock_fops = {
3471 .open = tracing_open_generic,
3472 .read = tracing_clock_read,
3473 .write = tracing_clock_write,
3476 struct ftrace_buffer_info {
3477 struct trace_array *tr;
3483 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3485 int cpu = (int)(long)inode->i_private;
3486 struct ftrace_buffer_info *info;
3488 if (tracing_disabled)
3491 info = kzalloc(sizeof(*info), GFP_KERNEL);
3495 info->tr = &global_trace;
3498 /* Force reading ring buffer for first read */
3499 info->read = (unsigned int)-1;
3501 filp->private_data = info;
3503 return nonseekable_open(inode, filp);
3507 tracing_buffers_read(struct file *filp, char __user *ubuf,
3508 size_t count, loff_t *ppos)
3510 struct ftrace_buffer_info *info = filp->private_data;
3519 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3523 /* Do we have previous read data to read? */
3524 if (info->read < PAGE_SIZE)
3529 ret = ring_buffer_read_page(info->tr->buffer,
3536 pos = ring_buffer_page_len(info->spare);
3538 if (pos < PAGE_SIZE)
3539 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3542 size = PAGE_SIZE - info->read;
3546 ret = copy_to_user(ubuf, info->spare + info->read, size);
3557 static int tracing_buffers_release(struct inode *inode, struct file *file)
3559 struct ftrace_buffer_info *info = file->private_data;
3562 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3569 struct ring_buffer *buffer;
3574 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3575 struct pipe_buffer *buf)
3577 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3582 ring_buffer_free_read_page(ref->buffer, ref->page);
3587 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3588 struct pipe_buffer *buf)
3593 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3594 struct pipe_buffer *buf)
3596 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3601 /* Pipe buffer operations for a buffer. */
3602 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3604 .map = generic_pipe_buf_map,
3605 .unmap = generic_pipe_buf_unmap,
3606 .confirm = generic_pipe_buf_confirm,
3607 .release = buffer_pipe_buf_release,
3608 .steal = buffer_pipe_buf_steal,
3609 .get = buffer_pipe_buf_get,
3613 * Callback from splice_to_pipe(), if we need to release some pages
3614 * at the end of the spd in case we error'ed out in filling the pipe.
3616 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3618 struct buffer_ref *ref =
3619 (struct buffer_ref *)spd->partial[i].private;
3624 ring_buffer_free_read_page(ref->buffer, ref->page);
3626 spd->partial[i].private = 0;
3630 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3631 struct pipe_inode_info *pipe, size_t len,
3634 struct ftrace_buffer_info *info = file->private_data;
3635 struct partial_page partial[PIPE_BUFFERS];
3636 struct page *pages[PIPE_BUFFERS];
3637 struct splice_pipe_desc spd = {
3641 .ops = &buffer_pipe_buf_ops,
3642 .spd_release = buffer_spd_release,
3644 struct buffer_ref *ref;
3645 int entries, size, i;
3648 if (*ppos & (PAGE_SIZE - 1)) {
3649 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3653 if (len & (PAGE_SIZE - 1)) {
3654 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3655 if (len < PAGE_SIZE)
3660 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3662 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3666 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3671 ref->buffer = info->tr->buffer;
3672 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3678 r = ring_buffer_read_page(ref->buffer, &ref->page,
3681 ring_buffer_free_read_page(ref->buffer,
3688 * zero out any left over data, this is going to
3691 size = ring_buffer_page_len(ref->page);
3692 if (size < PAGE_SIZE)
3693 memset(ref->page + size, 0, PAGE_SIZE - size);
3695 page = virt_to_page(ref->page);
3697 spd.pages[i] = page;
3698 spd.partial[i].len = PAGE_SIZE;
3699 spd.partial[i].offset = 0;
3700 spd.partial[i].private = (unsigned long)ref;
3704 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3709 /* did we read anything? */
3710 if (!spd.nr_pages) {
3711 if (flags & SPLICE_F_NONBLOCK)
3719 ret = splice_to_pipe(pipe, &spd);
3724 static const struct file_operations tracing_buffers_fops = {
3725 .open = tracing_buffers_open,
3726 .read = tracing_buffers_read,
3727 .release = tracing_buffers_release,
3728 .splice_read = tracing_buffers_splice_read,
3729 .llseek = no_llseek,
3733 tracing_stats_read(struct file *filp, char __user *ubuf,
3734 size_t count, loff_t *ppos)
3736 unsigned long cpu = (unsigned long)filp->private_data;
3737 struct trace_array *tr = &global_trace;
3738 struct trace_seq *s;
3741 s = kmalloc(sizeof(*s), GFP_KERNEL);
3747 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3748 trace_seq_printf(s, "entries: %ld\n", cnt);
3750 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3751 trace_seq_printf(s, "overrun: %ld\n", cnt);
3753 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3754 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3756 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3763 static const struct file_operations tracing_stats_fops = {
3764 .open = tracing_open_generic,
3765 .read = tracing_stats_read,
3768 #ifdef CONFIG_DYNAMIC_FTRACE
3770 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3776 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3777 size_t cnt, loff_t *ppos)
3779 static char ftrace_dyn_info_buffer[1024];
3780 static DEFINE_MUTEX(dyn_info_mutex);
3781 unsigned long *p = filp->private_data;
3782 char *buf = ftrace_dyn_info_buffer;
3783 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3786 mutex_lock(&dyn_info_mutex);
3787 r = sprintf(buf, "%ld ", *p);
3789 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3792 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3794 mutex_unlock(&dyn_info_mutex);
3799 static const struct file_operations tracing_dyn_info_fops = {
3800 .open = tracing_open_generic,
3801 .read = tracing_read_dyn_info,
3805 static struct dentry *d_tracer;
3807 struct dentry *tracing_init_dentry(void)
3814 if (!debugfs_initialized())
3817 d_tracer = debugfs_create_dir("tracing", NULL);
3819 if (!d_tracer && !once) {
3821 pr_warning("Could not create debugfs directory 'tracing'\n");
3828 static struct dentry *d_percpu;
3830 struct dentry *tracing_dentry_percpu(void)
3833 struct dentry *d_tracer;
3838 d_tracer = tracing_init_dentry();
3843 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3845 if (!d_percpu && !once) {
3847 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3854 static void tracing_init_debugfs_percpu(long cpu)
3856 struct dentry *d_percpu = tracing_dentry_percpu();
3857 struct dentry *d_cpu;
3858 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3861 if (cpu > 999 || cpu < 0)
3864 sprintf(cpu_dir, "cpu%ld", cpu);
3865 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3867 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3871 /* per cpu trace_pipe */
3872 trace_create_file("trace_pipe", 0444, d_cpu,
3873 (void *) cpu, &tracing_pipe_fops);
3876 trace_create_file("trace", 0644, d_cpu,
3877 (void *) cpu, &tracing_fops);
3879 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3880 (void *) cpu, &tracing_buffers_fops);
3882 trace_create_file("stats", 0444, d_cpu,
3883 (void *) cpu, &tracing_stats_fops);
3886 #ifdef CONFIG_FTRACE_SELFTEST
3887 /* Let selftest have access to static functions in this file */
3888 #include "trace_selftest.c"
3891 struct trace_option_dentry {
3892 struct tracer_opt *opt;
3893 struct tracer_flags *flags;
3894 struct dentry *entry;
3898 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3901 struct trace_option_dentry *topt = filp->private_data;
3904 if (topt->flags->val & topt->opt->bit)
3909 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3913 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3916 struct trace_option_dentry *topt = filp->private_data;
3921 if (cnt >= sizeof(buf))
3924 if (copy_from_user(&buf, ubuf, cnt))
3929 ret = strict_strtoul(buf, 10, &val);
3936 /* do nothing if already cleared */
3937 if (!(topt->flags->val & topt->opt->bit))
3940 mutex_lock(&trace_types_lock);
3941 if (current_trace->set_flag)
3942 ret = current_trace->set_flag(topt->flags->val,
3944 mutex_unlock(&trace_types_lock);
3947 topt->flags->val &= ~topt->opt->bit;
3950 /* do nothing if already set */
3951 if (topt->flags->val & topt->opt->bit)
3954 mutex_lock(&trace_types_lock);
3955 if (current_trace->set_flag)
3956 ret = current_trace->set_flag(topt->flags->val,
3958 mutex_unlock(&trace_types_lock);
3961 topt->flags->val |= topt->opt->bit;
3974 static const struct file_operations trace_options_fops = {
3975 .open = tracing_open_generic,
3976 .read = trace_options_read,
3977 .write = trace_options_write,
3981 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3984 long index = (long)filp->private_data;
3987 if (trace_flags & (1 << index))
3992 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3996 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3999 long index = (long)filp->private_data;
4004 if (cnt >= sizeof(buf))
4007 if (copy_from_user(&buf, ubuf, cnt))
4012 ret = strict_strtoul(buf, 10, &val);
4016 if (val != 0 && val != 1)
4018 set_tracer_flags(1 << index, val);
4025 static const struct file_operations trace_options_core_fops = {
4026 .open = tracing_open_generic,
4027 .read = trace_options_core_read,
4028 .write = trace_options_core_write,
4031 struct dentry *trace_create_file(const char *name,
4033 struct dentry *parent,
4035 const struct file_operations *fops)
4039 ret = debugfs_create_file(name, mode, parent, data, fops);
4041 pr_warning("Could not create debugfs '%s' entry\n", name);
4047 static struct dentry *trace_options_init_dentry(void)
4049 struct dentry *d_tracer;
4050 static struct dentry *t_options;
4055 d_tracer = tracing_init_dentry();
4059 t_options = debugfs_create_dir("options", d_tracer);
4061 pr_warning("Could not create debugfs directory 'options'\n");
4069 create_trace_option_file(struct trace_option_dentry *topt,
4070 struct tracer_flags *flags,
4071 struct tracer_opt *opt)
4073 struct dentry *t_options;
4075 t_options = trace_options_init_dentry();
4079 topt->flags = flags;
4082 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4083 &trace_options_fops);
4087 static struct trace_option_dentry *
4088 create_trace_option_files(struct tracer *tracer)
4090 struct trace_option_dentry *topts;
4091 struct tracer_flags *flags;
4092 struct tracer_opt *opts;
4098 flags = tracer->flags;
4100 if (!flags || !flags->opts)
4105 for (cnt = 0; opts[cnt].name; cnt++)
4108 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4112 for (cnt = 0; opts[cnt].name; cnt++)
4113 create_trace_option_file(&topts[cnt], flags,
4120 destroy_trace_option_files(struct trace_option_dentry *topts)
4127 for (cnt = 0; topts[cnt].opt; cnt++) {
4128 if (topts[cnt].entry)
4129 debugfs_remove(topts[cnt].entry);
4135 static struct dentry *
4136 create_trace_option_core_file(const char *option, long index)
4138 struct dentry *t_options;
4140 t_options = trace_options_init_dentry();
4144 return trace_create_file(option, 0644, t_options, (void *)index,
4145 &trace_options_core_fops);
4148 static __init void create_trace_options_dir(void)
4150 struct dentry *t_options;
4153 t_options = trace_options_init_dentry();
4157 for (i = 0; trace_options[i]; i++)
4158 create_trace_option_core_file(trace_options[i], i);
4161 static __init int tracer_init_debugfs(void)
4163 struct dentry *d_tracer;
4166 d_tracer = tracing_init_dentry();
4168 trace_create_file("tracing_enabled", 0644, d_tracer,
4169 &global_trace, &tracing_ctrl_fops);
4171 trace_create_file("trace_options", 0644, d_tracer,
4172 NULL, &tracing_iter_fops);
4174 trace_create_file("tracing_cpumask", 0644, d_tracer,
4175 NULL, &tracing_cpumask_fops);
4177 trace_create_file("trace", 0644, d_tracer,
4178 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4180 trace_create_file("available_tracers", 0444, d_tracer,
4181 &global_trace, &show_traces_fops);
4183 trace_create_file("current_tracer", 0644, d_tracer,
4184 &global_trace, &set_tracer_fops);
4186 #ifdef CONFIG_TRACER_MAX_TRACE
4187 trace_create_file("tracing_max_latency", 0644, d_tracer,
4188 &tracing_max_latency, &tracing_max_lat_fops);
4190 trace_create_file("tracing_thresh", 0644, d_tracer,
4191 &tracing_thresh, &tracing_max_lat_fops);
4194 trace_create_file("README", 0444, d_tracer,
4195 NULL, &tracing_readme_fops);
4197 trace_create_file("trace_pipe", 0444, d_tracer,
4198 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4200 trace_create_file("buffer_size_kb", 0644, d_tracer,
4201 &global_trace, &tracing_entries_fops);
4203 trace_create_file("trace_marker", 0220, d_tracer,
4204 NULL, &tracing_mark_fops);
4206 trace_create_file("saved_cmdlines", 0444, d_tracer,
4207 NULL, &tracing_saved_cmdlines_fops);
4209 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4212 #ifdef CONFIG_DYNAMIC_FTRACE
4213 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4214 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4216 #ifdef CONFIG_SYSPROF_TRACER
4217 init_tracer_sysprof_debugfs(d_tracer);
4220 create_trace_options_dir();
4222 for_each_tracing_cpu(cpu)
4223 tracing_init_debugfs_percpu(cpu);
4228 static int trace_panic_handler(struct notifier_block *this,
4229 unsigned long event, void *unused)
4231 if (ftrace_dump_on_oops)
4236 static struct notifier_block trace_panic_notifier = {
4237 .notifier_call = trace_panic_handler,
4239 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4242 static int trace_die_handler(struct notifier_block *self,
4248 if (ftrace_dump_on_oops)
4257 static struct notifier_block trace_die_notifier = {
4258 .notifier_call = trace_die_handler,
4263 * printk is set to max of 1024, we really don't need it that big.
4264 * Nothing should be printing 1000 characters anyway.
4266 #define TRACE_MAX_PRINT 1000
4269 * Define here KERN_TRACE so that we have one place to modify
4270 * it if we decide to change what log level the ftrace dump
4273 #define KERN_TRACE KERN_EMERG
4276 trace_printk_seq(struct trace_seq *s)
4278 /* Probably should print a warning here. */
4282 /* should be zero ended, but we are paranoid. */
4283 s->buffer[s->len] = 0;
4285 printk(KERN_TRACE "%s", s->buffer);
4290 static void __ftrace_dump(bool disable_tracing)
4292 static raw_spinlock_t ftrace_dump_lock =
4293 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
4294 /* use static because iter can be a bit big for the stack */
4295 static struct trace_iterator iter;
4296 unsigned int old_userobj;
4297 static int dump_ran;
4298 unsigned long flags;
4302 local_irq_save(flags);
4303 __raw_spin_lock(&ftrace_dump_lock);
4311 if (disable_tracing)
4314 for_each_tracing_cpu(cpu) {
4315 atomic_inc(&global_trace.data[cpu]->disabled);
4318 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4320 /* don't look at user memory in panic mode */
4321 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4323 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4325 /* Simulate the iterator */
4326 iter.tr = &global_trace;
4327 iter.trace = current_trace;
4328 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4331 * We need to stop all tracing on all CPUS to read the
4332 * the next buffer. This is a bit expensive, but is
4333 * not done often. We fill all what we can read,
4334 * and then release the locks again.
4337 while (!trace_empty(&iter)) {
4340 printk(KERN_TRACE "---------------------------------\n");
4344 /* reset all but tr, trace, and overruns */
4345 memset(&iter.seq, 0,
4346 sizeof(struct trace_iterator) -
4347 offsetof(struct trace_iterator, seq));
4348 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4351 if (find_next_entry_inc(&iter) != NULL) {
4354 ret = print_trace_line(&iter);
4355 if (ret != TRACE_TYPE_NO_CONSUME)
4356 trace_consume(&iter);
4359 trace_printk_seq(&iter.seq);
4363 printk(KERN_TRACE " (ftrace buffer empty)\n");
4365 printk(KERN_TRACE "---------------------------------\n");
4367 /* Re-enable tracing if requested */
4368 if (!disable_tracing) {
4369 trace_flags |= old_userobj;
4371 for_each_tracing_cpu(cpu) {
4372 atomic_dec(&global_trace.data[cpu]->disabled);
4378 __raw_spin_unlock(&ftrace_dump_lock);
4379 local_irq_restore(flags);
4382 /* By default: disable tracing after the dump */
4383 void ftrace_dump(void)
4385 __ftrace_dump(true);
4388 __init static int tracer_alloc_buffers(void)
4394 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4397 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4398 goto out_free_buffer_mask;
4400 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4401 goto out_free_tracing_cpumask;
4403 /* To save memory, keep the ring buffer size to its minimum */
4404 if (ring_buffer_expanded)
4405 ring_buf_size = trace_buf_size;
4409 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4410 cpumask_copy(tracing_cpumask, cpu_all_mask);
4411 cpumask_clear(tracing_reader_cpumask);
4413 /* TODO: make the number of buffers hot pluggable with CPUS */
4414 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4415 TRACE_BUFFER_FLAGS);
4416 if (!global_trace.buffer) {
4417 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4419 goto out_free_cpumask;
4421 global_trace.entries = ring_buffer_size(global_trace.buffer);
4424 #ifdef CONFIG_TRACER_MAX_TRACE
4425 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4426 TRACE_BUFFER_FLAGS);
4427 if (!max_tr.buffer) {
4428 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4430 ring_buffer_free(global_trace.buffer);
4431 goto out_free_cpumask;
4433 max_tr.entries = ring_buffer_size(max_tr.buffer);
4434 WARN_ON(max_tr.entries != global_trace.entries);
4437 /* Allocate the first page for all buffers */
4438 for_each_tracing_cpu(i) {
4439 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4440 max_tr.data[i] = &per_cpu(max_data, i);
4443 trace_init_cmdlines();
4445 register_tracer(&nop_trace);
4446 current_trace = &nop_trace;
4447 #ifdef CONFIG_BOOT_TRACER
4448 register_tracer(&boot_tracer);
4450 /* All seems OK, enable tracing */
4451 tracing_disabled = 0;
4453 atomic_notifier_chain_register(&panic_notifier_list,
4454 &trace_panic_notifier);
4456 register_die_notifier(&trace_die_notifier);
4461 free_cpumask_var(tracing_reader_cpumask);
4462 out_free_tracing_cpumask:
4463 free_cpumask_var(tracing_cpumask);
4464 out_free_buffer_mask:
4465 free_cpumask_var(tracing_buffer_mask);
4470 __init static int clear_boot_tracer(void)
4473 * The default tracer at boot buffer is an init section.
4474 * This function is called in lateinit. If we did not
4475 * find the boot tracer, then clear it out, to prevent
4476 * later registration from accessing the buffer that is
4477 * about to be freed.
4479 if (!default_bootup_tracer)
4482 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4483 default_bootup_tracer);
4484 default_bootup_tracer = NULL;
4489 early_initcall(tracer_alloc_buffers);
4490 fs_initcall(tracer_init_debugfs);
4491 late_initcall(clear_boot_tracer);