Merge branches 'arm', 'at91', 'bcmring', 'ep93xx', 'mach-types', 'misc' and 'w90x900...
[deliverable/linux.git] / kernel / trace / trace.c
1 /*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
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
13 */
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/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/ctype.h>
35 #include <linux/init.h>
36 #include <linux/poll.h>
37 #include <linux/gfp.h>
38 #include <linux/fs.h>
39
40 #include "trace.h"
41 #include "trace_output.h"
42
43 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
44
45 unsigned long __read_mostly tracing_max_latency;
46 unsigned long __read_mostly tracing_thresh;
47
48 /*
49 * On boot up, the ring buffer is set to the minimum size, so that
50 * we do not waste memory on systems that are not using tracing.
51 */
52 static int ring_buffer_expanded;
53
54 /*
55 * We need to change this state when a selftest is running.
56 * A selftest will lurk into the ring-buffer to count the
57 * entries inserted during the selftest although some concurrent
58 * insertions into the ring-buffer such as trace_printk could occurred
59 * at the same time, giving false positive or negative results.
60 */
61 static bool __read_mostly tracing_selftest_running;
62
63 /*
64 * If a tracer is running, we do not want to run SELFTEST.
65 */
66 static bool __read_mostly tracing_selftest_disabled;
67
68 /* For tracers that don't implement custom flags */
69 static struct tracer_opt dummy_tracer_opt[] = {
70 { }
71 };
72
73 static struct tracer_flags dummy_tracer_flags = {
74 .val = 0,
75 .opts = dummy_tracer_opt
76 };
77
78 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
79 {
80 return 0;
81 }
82
83 /*
84 * Kill all tracing for good (never come back).
85 * It is initialized to 1 but will turn to zero if the initialization
86 * of the tracer is successful. But that is the only place that sets
87 * this back to zero.
88 */
89 static int tracing_disabled = 1;
90
91 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
92
93 static inline void ftrace_disable_cpu(void)
94 {
95 preempt_disable();
96 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
97 }
98
99 static inline void ftrace_enable_cpu(void)
100 {
101 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
102 preempt_enable();
103 }
104
105 static cpumask_var_t __read_mostly tracing_buffer_mask;
106
107 /* Define which cpu buffers are currently read in trace_pipe */
108 static cpumask_var_t tracing_reader_cpumask;
109
110 #define for_each_tracing_cpu(cpu) \
111 for_each_cpu(cpu, tracing_buffer_mask)
112
113 /*
114 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
115 *
116 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
117 * is set, then ftrace_dump is called. This will output the contents
118 * of the ftrace buffers to the console. This is very useful for
119 * capturing traces that lead to crashes and outputing it to a
120 * serial console.
121 *
122 * It is default off, but you can enable it with either specifying
123 * "ftrace_dump_on_oops" in the kernel command line, or setting
124 * /proc/sys/kernel/ftrace_dump_on_oops to true.
125 */
126 int ftrace_dump_on_oops;
127
128 static int tracing_set_tracer(const char *buf);
129
130 #define BOOTUP_TRACER_SIZE 100
131 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
132 static char *default_bootup_tracer;
133
134 static int __init set_ftrace(char *str)
135 {
136 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
137 default_bootup_tracer = bootup_tracer_buf;
138 /* We are using ftrace early, expand it */
139 ring_buffer_expanded = 1;
140 return 1;
141 }
142 __setup("ftrace=", set_ftrace);
143
144 static int __init set_ftrace_dump_on_oops(char *str)
145 {
146 ftrace_dump_on_oops = 1;
147 return 1;
148 }
149 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
150
151 unsigned long long ns2usecs(cycle_t nsec)
152 {
153 nsec += 500;
154 do_div(nsec, 1000);
155 return nsec;
156 }
157
158 /*
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
165 *
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
169 */
170 static struct trace_array global_trace;
171
172 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
173
174 int filter_current_check_discard(struct ftrace_event_call *call, void *rec,
175 struct ring_buffer_event *event)
176 {
177 return filter_check_discard(call, rec, global_trace.buffer, event);
178 }
179 EXPORT_SYMBOL_GPL(filter_current_check_discard);
180
181 cycle_t ftrace_now(int cpu)
182 {
183 u64 ts;
184
185 /* Early boot up does not have a buffer yet */
186 if (!global_trace.buffer)
187 return trace_clock_local();
188
189 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
190 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
191
192 return ts;
193 }
194
195 /*
196 * The max_tr is used to snapshot the global_trace when a maximum
197 * latency is reached. Some tracers will use this to store a maximum
198 * trace while it continues examining live traces.
199 *
200 * The buffers for the max_tr are set up the same as the global_trace.
201 * When a snapshot is taken, the link list of the max_tr is swapped
202 * with the link list of the global_trace and the buffers are reset for
203 * the global_trace so the tracing can continue.
204 */
205 static struct trace_array max_tr;
206
207 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
208
209 /* tracer_enabled is used to toggle activation of a tracer */
210 static int tracer_enabled = 1;
211
212 /**
213 * tracing_is_enabled - return tracer_enabled status
214 *
215 * This function is used by other tracers to know the status
216 * of the tracer_enabled flag. Tracers may use this function
217 * to know if it should enable their features when starting
218 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
219 */
220 int tracing_is_enabled(void)
221 {
222 return tracer_enabled;
223 }
224
225 /*
226 * trace_buf_size is the size in bytes that is allocated
227 * for a buffer. Note, the number of bytes is always rounded
228 * to page size.
229 *
230 * This number is purposely set to a low number of 16384.
231 * If the dump on oops happens, it will be much appreciated
232 * to not have to wait for all that output. Anyway this can be
233 * boot time and run time configurable.
234 */
235 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
236
237 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
238
239 /* trace_types holds a link list of available tracers. */
240 static struct tracer *trace_types __read_mostly;
241
242 /* current_trace points to the tracer that is currently active */
243 static struct tracer *current_trace __read_mostly;
244
245 /*
246 * max_tracer_type_len is used to simplify the allocating of
247 * buffers to read userspace tracer names. We keep track of
248 * the longest tracer name registered.
249 */
250 static int max_tracer_type_len;
251
252 /*
253 * trace_types_lock is used to protect the trace_types list.
254 * This lock is also used to keep user access serialized.
255 * Accesses from userspace will grab this lock while userspace
256 * activities happen inside the kernel.
257 */
258 static DEFINE_MUTEX(trace_types_lock);
259
260 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
261 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
262
263 /* trace_flags holds trace_options default values */
264 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
265 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
266 TRACE_ITER_GRAPH_TIME;
267
268 /**
269 * trace_wake_up - wake up tasks waiting for trace input
270 *
271 * Simply wakes up any task that is blocked on the trace_wait
272 * queue. These is used with trace_poll for tasks polling the trace.
273 */
274 void trace_wake_up(void)
275 {
276 /*
277 * The runqueue_is_locked() can fail, but this is the best we
278 * have for now:
279 */
280 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
281 wake_up(&trace_wait);
282 }
283
284 static int __init set_buf_size(char *str)
285 {
286 unsigned long buf_size;
287
288 if (!str)
289 return 0;
290 buf_size = memparse(str, &str);
291 /* nr_entries can not be zero */
292 if (buf_size == 0)
293 return 0;
294 trace_buf_size = buf_size;
295 return 1;
296 }
297 __setup("trace_buf_size=", set_buf_size);
298
299 unsigned long nsecs_to_usecs(unsigned long nsecs)
300 {
301 return nsecs / 1000;
302 }
303
304 /* These must match the bit postions in trace_iterator_flags */
305 static const char *trace_options[] = {
306 "print-parent",
307 "sym-offset",
308 "sym-addr",
309 "verbose",
310 "raw",
311 "hex",
312 "bin",
313 "block",
314 "stacktrace",
315 "sched-tree",
316 "trace_printk",
317 "ftrace_preempt",
318 "branch",
319 "annotate",
320 "userstacktrace",
321 "sym-userobj",
322 "printk-msg-only",
323 "context-info",
324 "latency-format",
325 "global-clock",
326 "sleep-time",
327 "graph-time",
328 NULL
329 };
330
331 /*
332 * ftrace_max_lock is used to protect the swapping of buffers
333 * when taking a max snapshot. The buffers themselves are
334 * protected by per_cpu spinlocks. But the action of the swap
335 * needs its own lock.
336 *
337 * This is defined as a raw_spinlock_t in order to help
338 * with performance when lockdep debugging is enabled.
339 */
340 static raw_spinlock_t ftrace_max_lock =
341 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
342
343 /*
344 * Copy the new maximum trace into the separate maximum-trace
345 * structure. (this way the maximum trace is permanently saved,
346 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
347 */
348 static void
349 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
350 {
351 struct trace_array_cpu *data = tr->data[cpu];
352
353 max_tr.cpu = cpu;
354 max_tr.time_start = data->preempt_timestamp;
355
356 data = max_tr.data[cpu];
357 data->saved_latency = tracing_max_latency;
358
359 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
360 data->pid = tsk->pid;
361 data->uid = task_uid(tsk);
362 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
363 data->policy = tsk->policy;
364 data->rt_priority = tsk->rt_priority;
365
366 /* record this tasks comm */
367 tracing_record_cmdline(tsk);
368 }
369
370 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
371 {
372 int len;
373 int ret;
374
375 if (!cnt)
376 return 0;
377
378 if (s->len <= s->readpos)
379 return -EBUSY;
380
381 len = s->len - s->readpos;
382 if (cnt > len)
383 cnt = len;
384 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
385 if (ret == cnt)
386 return -EFAULT;
387
388 cnt -= ret;
389
390 s->readpos += cnt;
391 return cnt;
392 }
393
394 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
395 {
396 int len;
397 void *ret;
398
399 if (s->len <= s->readpos)
400 return -EBUSY;
401
402 len = s->len - s->readpos;
403 if (cnt > len)
404 cnt = len;
405 ret = memcpy(buf, s->buffer + s->readpos, cnt);
406 if (!ret)
407 return -EFAULT;
408
409 s->readpos += cnt;
410 return cnt;
411 }
412
413 /**
414 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
415 * @tr: tracer
416 * @tsk: the task with the latency
417 * @cpu: The cpu that initiated the trace.
418 *
419 * Flip the buffers between the @tr and the max_tr and record information
420 * about which task was the cause of this latency.
421 */
422 void
423 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
424 {
425 struct ring_buffer *buf = tr->buffer;
426
427 WARN_ON_ONCE(!irqs_disabled());
428 __raw_spin_lock(&ftrace_max_lock);
429
430 tr->buffer = max_tr.buffer;
431 max_tr.buffer = buf;
432
433 ftrace_disable_cpu();
434 ring_buffer_reset(tr->buffer);
435 ftrace_enable_cpu();
436
437 __update_max_tr(tr, tsk, cpu);
438 __raw_spin_unlock(&ftrace_max_lock);
439 }
440
441 /**
442 * update_max_tr_single - only copy one trace over, and reset the rest
443 * @tr - tracer
444 * @tsk - task with the latency
445 * @cpu - the cpu of the buffer to copy.
446 *
447 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
448 */
449 void
450 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
451 {
452 int ret;
453
454 WARN_ON_ONCE(!irqs_disabled());
455 __raw_spin_lock(&ftrace_max_lock);
456
457 ftrace_disable_cpu();
458
459 ring_buffer_reset(max_tr.buffer);
460 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
461
462 ftrace_enable_cpu();
463
464 WARN_ON_ONCE(ret && ret != -EAGAIN);
465
466 __update_max_tr(tr, tsk, cpu);
467 __raw_spin_unlock(&ftrace_max_lock);
468 }
469
470 /**
471 * register_tracer - register a tracer with the ftrace system.
472 * @type - the plugin for the tracer
473 *
474 * Register a new plugin tracer.
475 */
476 int register_tracer(struct tracer *type)
477 __releases(kernel_lock)
478 __acquires(kernel_lock)
479 {
480 struct tracer *t;
481 int len;
482 int ret = 0;
483
484 if (!type->name) {
485 pr_info("Tracer must have a name\n");
486 return -1;
487 }
488
489 /*
490 * When this gets called we hold the BKL which means that
491 * preemption is disabled. Various trace selftests however
492 * need to disable and enable preemption for successful tests.
493 * So we drop the BKL here and grab it after the tests again.
494 */
495 unlock_kernel();
496 mutex_lock(&trace_types_lock);
497
498 tracing_selftest_running = true;
499
500 for (t = trace_types; t; t = t->next) {
501 if (strcmp(type->name, t->name) == 0) {
502 /* already found */
503 pr_info("Trace %s already registered\n",
504 type->name);
505 ret = -1;
506 goto out;
507 }
508 }
509
510 if (!type->set_flag)
511 type->set_flag = &dummy_set_flag;
512 if (!type->flags)
513 type->flags = &dummy_tracer_flags;
514 else
515 if (!type->flags->opts)
516 type->flags->opts = dummy_tracer_opt;
517 if (!type->wait_pipe)
518 type->wait_pipe = default_wait_pipe;
519
520
521 #ifdef CONFIG_FTRACE_STARTUP_TEST
522 if (type->selftest && !tracing_selftest_disabled) {
523 struct tracer *saved_tracer = current_trace;
524 struct trace_array *tr = &global_trace;
525 int i;
526
527 /*
528 * Run a selftest on this tracer.
529 * Here we reset the trace buffer, and set the current
530 * tracer to be this tracer. The tracer can then run some
531 * internal tracing to verify that everything is in order.
532 * If we fail, we do not register this tracer.
533 */
534 for_each_tracing_cpu(i)
535 tracing_reset(tr, i);
536
537 current_trace = type;
538 /* the test is responsible for initializing and enabling */
539 pr_info("Testing tracer %s: ", type->name);
540 ret = type->selftest(type, tr);
541 /* the test is responsible for resetting too */
542 current_trace = saved_tracer;
543 if (ret) {
544 printk(KERN_CONT "FAILED!\n");
545 goto out;
546 }
547 /* Only reset on passing, to avoid touching corrupted buffers */
548 for_each_tracing_cpu(i)
549 tracing_reset(tr, i);
550
551 printk(KERN_CONT "PASSED\n");
552 }
553 #endif
554
555 type->next = trace_types;
556 trace_types = type;
557 len = strlen(type->name);
558 if (len > max_tracer_type_len)
559 max_tracer_type_len = len;
560
561 out:
562 tracing_selftest_running = false;
563 mutex_unlock(&trace_types_lock);
564
565 if (ret || !default_bootup_tracer)
566 goto out_unlock;
567
568 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
569 goto out_unlock;
570
571 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
572 /* Do we want this tracer to start on bootup? */
573 tracing_set_tracer(type->name);
574 default_bootup_tracer = NULL;
575 /* disable other selftests, since this will break it. */
576 tracing_selftest_disabled = 1;
577 #ifdef CONFIG_FTRACE_STARTUP_TEST
578 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
579 type->name);
580 #endif
581
582 out_unlock:
583 lock_kernel();
584 return ret;
585 }
586
587 void unregister_tracer(struct tracer *type)
588 {
589 struct tracer **t;
590 int len;
591
592 mutex_lock(&trace_types_lock);
593 for (t = &trace_types; *t; t = &(*t)->next) {
594 if (*t == type)
595 goto found;
596 }
597 pr_info("Trace %s not registered\n", type->name);
598 goto out;
599
600 found:
601 *t = (*t)->next;
602
603 if (type == current_trace && tracer_enabled) {
604 tracer_enabled = 0;
605 tracing_stop();
606 if (current_trace->stop)
607 current_trace->stop(&global_trace);
608 current_trace = &nop_trace;
609 }
610
611 if (strlen(type->name) != max_tracer_type_len)
612 goto out;
613
614 max_tracer_type_len = 0;
615 for (t = &trace_types; *t; t = &(*t)->next) {
616 len = strlen((*t)->name);
617 if (len > max_tracer_type_len)
618 max_tracer_type_len = len;
619 }
620 out:
621 mutex_unlock(&trace_types_lock);
622 }
623
624 void tracing_reset(struct trace_array *tr, int cpu)
625 {
626 ftrace_disable_cpu();
627 ring_buffer_reset_cpu(tr->buffer, cpu);
628 ftrace_enable_cpu();
629 }
630
631 void tracing_reset_online_cpus(struct trace_array *tr)
632 {
633 int cpu;
634
635 tr->time_start = ftrace_now(tr->cpu);
636
637 for_each_online_cpu(cpu)
638 tracing_reset(tr, cpu);
639 }
640
641 void tracing_reset_current(int cpu)
642 {
643 tracing_reset(&global_trace, cpu);
644 }
645
646 void tracing_reset_current_online_cpus(void)
647 {
648 tracing_reset_online_cpus(&global_trace);
649 }
650
651 #define SAVED_CMDLINES 128
652 #define NO_CMDLINE_MAP UINT_MAX
653 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
654 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
655 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
656 static int cmdline_idx;
657 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
658
659 /* temporary disable recording */
660 static atomic_t trace_record_cmdline_disabled __read_mostly;
661
662 static void trace_init_cmdlines(void)
663 {
664 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
665 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
666 cmdline_idx = 0;
667 }
668
669 static int trace_stop_count;
670 static DEFINE_SPINLOCK(tracing_start_lock);
671
672 /**
673 * ftrace_off_permanent - disable all ftrace code permanently
674 *
675 * This should only be called when a serious anomally has
676 * been detected. This will turn off the function tracing,
677 * ring buffers, and other tracing utilites. It takes no
678 * locks and can be called from any context.
679 */
680 void ftrace_off_permanent(void)
681 {
682 tracing_disabled = 1;
683 ftrace_stop();
684 tracing_off_permanent();
685 }
686
687 /**
688 * tracing_start - quick start of the tracer
689 *
690 * If tracing is enabled but was stopped by tracing_stop,
691 * this will start the tracer back up.
692 */
693 void tracing_start(void)
694 {
695 struct ring_buffer *buffer;
696 unsigned long flags;
697
698 if (tracing_disabled)
699 return;
700
701 spin_lock_irqsave(&tracing_start_lock, flags);
702 if (--trace_stop_count) {
703 if (trace_stop_count < 0) {
704 /* Someone screwed up their debugging */
705 WARN_ON_ONCE(1);
706 trace_stop_count = 0;
707 }
708 goto out;
709 }
710
711
712 buffer = global_trace.buffer;
713 if (buffer)
714 ring_buffer_record_enable(buffer);
715
716 buffer = max_tr.buffer;
717 if (buffer)
718 ring_buffer_record_enable(buffer);
719
720 ftrace_start();
721 out:
722 spin_unlock_irqrestore(&tracing_start_lock, flags);
723 }
724
725 /**
726 * tracing_stop - quick stop of the tracer
727 *
728 * Light weight way to stop tracing. Use in conjunction with
729 * tracing_start.
730 */
731 void tracing_stop(void)
732 {
733 struct ring_buffer *buffer;
734 unsigned long flags;
735
736 ftrace_stop();
737 spin_lock_irqsave(&tracing_start_lock, flags);
738 if (trace_stop_count++)
739 goto out;
740
741 buffer = global_trace.buffer;
742 if (buffer)
743 ring_buffer_record_disable(buffer);
744
745 buffer = max_tr.buffer;
746 if (buffer)
747 ring_buffer_record_disable(buffer);
748
749 out:
750 spin_unlock_irqrestore(&tracing_start_lock, flags);
751 }
752
753 void trace_stop_cmdline_recording(void);
754
755 static void trace_save_cmdline(struct task_struct *tsk)
756 {
757 unsigned pid, idx;
758
759 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
760 return;
761
762 /*
763 * It's not the end of the world if we don't get
764 * the lock, but we also don't want to spin
765 * nor do we want to disable interrupts,
766 * so if we miss here, then better luck next time.
767 */
768 if (!__raw_spin_trylock(&trace_cmdline_lock))
769 return;
770
771 idx = map_pid_to_cmdline[tsk->pid];
772 if (idx == NO_CMDLINE_MAP) {
773 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
774
775 /*
776 * Check whether the cmdline buffer at idx has a pid
777 * mapped. We are going to overwrite that entry so we
778 * need to clear the map_pid_to_cmdline. Otherwise we
779 * would read the new comm for the old pid.
780 */
781 pid = map_cmdline_to_pid[idx];
782 if (pid != NO_CMDLINE_MAP)
783 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
784
785 map_cmdline_to_pid[idx] = tsk->pid;
786 map_pid_to_cmdline[tsk->pid] = idx;
787
788 cmdline_idx = idx;
789 }
790
791 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
792
793 __raw_spin_unlock(&trace_cmdline_lock);
794 }
795
796 void trace_find_cmdline(int pid, char comm[])
797 {
798 unsigned map;
799
800 if (!pid) {
801 strcpy(comm, "<idle>");
802 return;
803 }
804
805 if (pid > PID_MAX_DEFAULT) {
806 strcpy(comm, "<...>");
807 return;
808 }
809
810 preempt_disable();
811 __raw_spin_lock(&trace_cmdline_lock);
812 map = map_pid_to_cmdline[pid];
813 if (map != NO_CMDLINE_MAP)
814 strcpy(comm, saved_cmdlines[map]);
815 else
816 strcpy(comm, "<...>");
817
818 __raw_spin_unlock(&trace_cmdline_lock);
819 preempt_enable();
820 }
821
822 void tracing_record_cmdline(struct task_struct *tsk)
823 {
824 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
825 !tracing_is_on())
826 return;
827
828 trace_save_cmdline(tsk);
829 }
830
831 void
832 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
833 int pc)
834 {
835 struct task_struct *tsk = current;
836
837 entry->preempt_count = pc & 0xff;
838 entry->pid = (tsk) ? tsk->pid : 0;
839 entry->tgid = (tsk) ? tsk->tgid : 0;
840 entry->flags =
841 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
842 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
843 #else
844 TRACE_FLAG_IRQS_NOSUPPORT |
845 #endif
846 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
847 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
848 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
849 }
850
851 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
852 int type,
853 unsigned long len,
854 unsigned long flags, int pc)
855 {
856 struct ring_buffer_event *event;
857
858 event = ring_buffer_lock_reserve(tr->buffer, len);
859 if (event != NULL) {
860 struct trace_entry *ent = ring_buffer_event_data(event);
861
862 tracing_generic_entry_update(ent, flags, pc);
863 ent->type = type;
864 }
865
866 return event;
867 }
868 static void ftrace_trace_stack(struct trace_array *tr,
869 unsigned long flags, int skip, int pc);
870 static void ftrace_trace_userstack(struct trace_array *tr,
871 unsigned long flags, int pc);
872
873 static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
874 struct ring_buffer_event *event,
875 unsigned long flags, int pc,
876 int wake)
877 {
878 ring_buffer_unlock_commit(tr->buffer, event);
879
880 ftrace_trace_stack(tr, flags, 6, pc);
881 ftrace_trace_userstack(tr, flags, pc);
882
883 if (wake)
884 trace_wake_up();
885 }
886
887 void trace_buffer_unlock_commit(struct trace_array *tr,
888 struct ring_buffer_event *event,
889 unsigned long flags, int pc)
890 {
891 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
892 }
893
894 struct ring_buffer_event *
895 trace_current_buffer_lock_reserve(int type, unsigned long len,
896 unsigned long flags, int pc)
897 {
898 return trace_buffer_lock_reserve(&global_trace,
899 type, len, flags, pc);
900 }
901 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
902
903 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
904 unsigned long flags, int pc)
905 {
906 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
907 }
908 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
909
910 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
911 unsigned long flags, int pc)
912 {
913 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
914 }
915 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
916
917 void trace_current_buffer_discard_commit(struct ring_buffer_event *event)
918 {
919 ring_buffer_discard_commit(global_trace.buffer, event);
920 }
921 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
922
923 void
924 trace_function(struct trace_array *tr,
925 unsigned long ip, unsigned long parent_ip, unsigned long flags,
926 int pc)
927 {
928 struct ftrace_event_call *call = &event_function;
929 struct ring_buffer_event *event;
930 struct ftrace_entry *entry;
931
932 /* If we are reading the ring buffer, don't trace */
933 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
934 return;
935
936 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
937 flags, pc);
938 if (!event)
939 return;
940 entry = ring_buffer_event_data(event);
941 entry->ip = ip;
942 entry->parent_ip = parent_ip;
943
944 if (!filter_check_discard(call, entry, tr->buffer, event))
945 ring_buffer_unlock_commit(tr->buffer, event);
946 }
947
948 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
949 static int __trace_graph_entry(struct trace_array *tr,
950 struct ftrace_graph_ent *trace,
951 unsigned long flags,
952 int pc)
953 {
954 struct ftrace_event_call *call = &event_funcgraph_entry;
955 struct ring_buffer_event *event;
956 struct ftrace_graph_ent_entry *entry;
957
958 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
959 return 0;
960
961 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
962 sizeof(*entry), flags, pc);
963 if (!event)
964 return 0;
965 entry = ring_buffer_event_data(event);
966 entry->graph_ent = *trace;
967 if (!filter_current_check_discard(call, entry, event))
968 ring_buffer_unlock_commit(global_trace.buffer, event);
969
970 return 1;
971 }
972
973 static void __trace_graph_return(struct trace_array *tr,
974 struct ftrace_graph_ret *trace,
975 unsigned long flags,
976 int pc)
977 {
978 struct ftrace_event_call *call = &event_funcgraph_exit;
979 struct ring_buffer_event *event;
980 struct ftrace_graph_ret_entry *entry;
981
982 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
983 return;
984
985 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
986 sizeof(*entry), flags, pc);
987 if (!event)
988 return;
989 entry = ring_buffer_event_data(event);
990 entry->ret = *trace;
991 if (!filter_current_check_discard(call, entry, event))
992 ring_buffer_unlock_commit(global_trace.buffer, event);
993 }
994 #endif
995
996 void
997 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
998 unsigned long ip, unsigned long parent_ip, unsigned long flags,
999 int pc)
1000 {
1001 if (likely(!atomic_read(&data->disabled)))
1002 trace_function(tr, ip, parent_ip, flags, pc);
1003 }
1004
1005 static void __ftrace_trace_stack(struct trace_array *tr,
1006 unsigned long flags,
1007 int skip, int pc)
1008 {
1009 #ifdef CONFIG_STACKTRACE
1010 struct ftrace_event_call *call = &event_kernel_stack;
1011 struct ring_buffer_event *event;
1012 struct stack_entry *entry;
1013 struct stack_trace trace;
1014
1015 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
1016 sizeof(*entry), flags, pc);
1017 if (!event)
1018 return;
1019 entry = ring_buffer_event_data(event);
1020 memset(&entry->caller, 0, sizeof(entry->caller));
1021
1022 trace.nr_entries = 0;
1023 trace.max_entries = FTRACE_STACK_ENTRIES;
1024 trace.skip = skip;
1025 trace.entries = entry->caller;
1026
1027 save_stack_trace(&trace);
1028 if (!filter_check_discard(call, entry, tr->buffer, event))
1029 ring_buffer_unlock_commit(tr->buffer, event);
1030 #endif
1031 }
1032
1033 static void ftrace_trace_stack(struct trace_array *tr,
1034 unsigned long flags,
1035 int skip, int pc)
1036 {
1037 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1038 return;
1039
1040 __ftrace_trace_stack(tr, flags, skip, pc);
1041 }
1042
1043 void __trace_stack(struct trace_array *tr,
1044 unsigned long flags,
1045 int skip, int pc)
1046 {
1047 __ftrace_trace_stack(tr, flags, skip, pc);
1048 }
1049
1050 static void ftrace_trace_userstack(struct trace_array *tr,
1051 unsigned long flags, int pc)
1052 {
1053 #ifdef CONFIG_STACKTRACE
1054 struct ftrace_event_call *call = &event_user_stack;
1055 struct ring_buffer_event *event;
1056 struct userstack_entry *entry;
1057 struct stack_trace trace;
1058
1059 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1060 return;
1061
1062 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1063 sizeof(*entry), flags, pc);
1064 if (!event)
1065 return;
1066 entry = ring_buffer_event_data(event);
1067
1068 memset(&entry->caller, 0, sizeof(entry->caller));
1069
1070 trace.nr_entries = 0;
1071 trace.max_entries = FTRACE_STACK_ENTRIES;
1072 trace.skip = 0;
1073 trace.entries = entry->caller;
1074
1075 save_stack_trace_user(&trace);
1076 if (!filter_check_discard(call, entry, tr->buffer, event))
1077 ring_buffer_unlock_commit(tr->buffer, event);
1078 #endif
1079 }
1080
1081 #ifdef UNUSED
1082 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1083 {
1084 ftrace_trace_userstack(tr, flags, preempt_count());
1085 }
1086 #endif /* UNUSED */
1087
1088 static void
1089 ftrace_trace_special(void *__tr,
1090 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1091 int pc)
1092 {
1093 struct ring_buffer_event *event;
1094 struct trace_array *tr = __tr;
1095 struct special_entry *entry;
1096
1097 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1098 sizeof(*entry), 0, pc);
1099 if (!event)
1100 return;
1101 entry = ring_buffer_event_data(event);
1102 entry->arg1 = arg1;
1103 entry->arg2 = arg2;
1104 entry->arg3 = arg3;
1105 trace_buffer_unlock_commit(tr, event, 0, pc);
1106 }
1107
1108 void
1109 __trace_special(void *__tr, void *__data,
1110 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1111 {
1112 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1113 }
1114
1115 void
1116 tracing_sched_switch_trace(struct trace_array *tr,
1117 struct task_struct *prev,
1118 struct task_struct *next,
1119 unsigned long flags, int pc)
1120 {
1121 struct ftrace_event_call *call = &event_context_switch;
1122 struct ring_buffer_event *event;
1123 struct ctx_switch_entry *entry;
1124
1125 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1126 sizeof(*entry), flags, pc);
1127 if (!event)
1128 return;
1129 entry = ring_buffer_event_data(event);
1130 entry->prev_pid = prev->pid;
1131 entry->prev_prio = prev->prio;
1132 entry->prev_state = prev->state;
1133 entry->next_pid = next->pid;
1134 entry->next_prio = next->prio;
1135 entry->next_state = next->state;
1136 entry->next_cpu = task_cpu(next);
1137
1138 if (!filter_check_discard(call, entry, tr->buffer, event))
1139 trace_buffer_unlock_commit(tr, event, flags, pc);
1140 }
1141
1142 void
1143 tracing_sched_wakeup_trace(struct trace_array *tr,
1144 struct task_struct *wakee,
1145 struct task_struct *curr,
1146 unsigned long flags, int pc)
1147 {
1148 struct ftrace_event_call *call = &event_wakeup;
1149 struct ring_buffer_event *event;
1150 struct ctx_switch_entry *entry;
1151
1152 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1153 sizeof(*entry), flags, pc);
1154 if (!event)
1155 return;
1156 entry = ring_buffer_event_data(event);
1157 entry->prev_pid = curr->pid;
1158 entry->prev_prio = curr->prio;
1159 entry->prev_state = curr->state;
1160 entry->next_pid = wakee->pid;
1161 entry->next_prio = wakee->prio;
1162 entry->next_state = wakee->state;
1163 entry->next_cpu = task_cpu(wakee);
1164
1165 if (!filter_check_discard(call, entry, tr->buffer, event))
1166 ring_buffer_unlock_commit(tr->buffer, event);
1167 ftrace_trace_stack(tr, flags, 6, pc);
1168 ftrace_trace_userstack(tr, flags, pc);
1169 }
1170
1171 void
1172 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1173 {
1174 struct trace_array *tr = &global_trace;
1175 struct trace_array_cpu *data;
1176 unsigned long flags;
1177 int cpu;
1178 int pc;
1179
1180 if (tracing_disabled)
1181 return;
1182
1183 pc = preempt_count();
1184 local_irq_save(flags);
1185 cpu = raw_smp_processor_id();
1186 data = tr->data[cpu];
1187
1188 if (likely(atomic_inc_return(&data->disabled) == 1))
1189 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1190
1191 atomic_dec(&data->disabled);
1192 local_irq_restore(flags);
1193 }
1194
1195 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1196 int trace_graph_entry(struct ftrace_graph_ent *trace)
1197 {
1198 struct trace_array *tr = &global_trace;
1199 struct trace_array_cpu *data;
1200 unsigned long flags;
1201 long disabled;
1202 int ret;
1203 int cpu;
1204 int pc;
1205
1206 if (!ftrace_trace_task(current))
1207 return 0;
1208
1209 if (!ftrace_graph_addr(trace->func))
1210 return 0;
1211
1212 local_irq_save(flags);
1213 cpu = raw_smp_processor_id();
1214 data = tr->data[cpu];
1215 disabled = atomic_inc_return(&data->disabled);
1216 if (likely(disabled == 1)) {
1217 pc = preempt_count();
1218 ret = __trace_graph_entry(tr, trace, flags, pc);
1219 } else {
1220 ret = 0;
1221 }
1222 /* Only do the atomic if it is not already set */
1223 if (!test_tsk_trace_graph(current))
1224 set_tsk_trace_graph(current);
1225
1226 atomic_dec(&data->disabled);
1227 local_irq_restore(flags);
1228
1229 return ret;
1230 }
1231
1232 void trace_graph_return(struct ftrace_graph_ret *trace)
1233 {
1234 struct trace_array *tr = &global_trace;
1235 struct trace_array_cpu *data;
1236 unsigned long flags;
1237 long disabled;
1238 int cpu;
1239 int pc;
1240
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_return(tr, trace, flags, pc);
1248 }
1249 if (!trace->depth)
1250 clear_tsk_trace_graph(current);
1251 atomic_dec(&data->disabled);
1252 local_irq_restore(flags);
1253 }
1254 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1255
1256
1257 /**
1258 * trace_vbprintk - write binary msg to tracing buffer
1259 *
1260 */
1261 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1262 {
1263 static raw_spinlock_t trace_buf_lock =
1264 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1265 static u32 trace_buf[TRACE_BUF_SIZE];
1266
1267 struct ftrace_event_call *call = &event_bprint;
1268 struct ring_buffer_event *event;
1269 struct trace_array *tr = &global_trace;
1270 struct trace_array_cpu *data;
1271 struct bprint_entry *entry;
1272 unsigned long flags;
1273 int disable;
1274 int resched;
1275 int cpu, len = 0, size, pc;
1276
1277 if (unlikely(tracing_selftest_running || tracing_disabled))
1278 return 0;
1279
1280 /* Don't pollute graph traces with trace_vprintk internals */
1281 pause_graph_tracing();
1282
1283 pc = preempt_count();
1284 resched = ftrace_preempt_disable();
1285 cpu = raw_smp_processor_id();
1286 data = tr->data[cpu];
1287
1288 disable = atomic_inc_return(&data->disabled);
1289 if (unlikely(disable != 1))
1290 goto out;
1291
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);
1296
1297 if (len > TRACE_BUF_SIZE || len < 0)
1298 goto out_unlock;
1299
1300 size = sizeof(*entry) + sizeof(u32) * len;
1301 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1302 if (!event)
1303 goto out_unlock;
1304 entry = ring_buffer_event_data(event);
1305 entry->ip = ip;
1306 entry->fmt = fmt;
1307
1308 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1309 if (!filter_check_discard(call, entry, tr->buffer, event))
1310 ring_buffer_unlock_commit(tr->buffer, event);
1311
1312 out_unlock:
1313 __raw_spin_unlock(&trace_buf_lock);
1314 local_irq_restore(flags);
1315
1316 out:
1317 atomic_dec_return(&data->disabled);
1318 ftrace_preempt_enable(resched);
1319 unpause_graph_tracing();
1320
1321 return len;
1322 }
1323 EXPORT_SYMBOL_GPL(trace_vbprintk);
1324
1325 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1326 {
1327 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1328 static char trace_buf[TRACE_BUF_SIZE];
1329
1330 struct ftrace_event_call *call = &event_print;
1331 struct ring_buffer_event *event;
1332 struct trace_array *tr = &global_trace;
1333 struct trace_array_cpu *data;
1334 int cpu, len = 0, size, pc;
1335 struct print_entry *entry;
1336 unsigned long irq_flags;
1337 int disable;
1338
1339 if (tracing_disabled || tracing_selftest_running)
1340 return 0;
1341
1342 pc = preempt_count();
1343 preempt_disable_notrace();
1344 cpu = raw_smp_processor_id();
1345 data = tr->data[cpu];
1346
1347 disable = atomic_inc_return(&data->disabled);
1348 if (unlikely(disable != 1))
1349 goto out;
1350
1351 pause_graph_tracing();
1352 raw_local_irq_save(irq_flags);
1353 __raw_spin_lock(&trace_buf_lock);
1354 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1355
1356 len = min(len, TRACE_BUF_SIZE-1);
1357 trace_buf[len] = 0;
1358
1359 size = sizeof(*entry) + len + 1;
1360 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1361 if (!event)
1362 goto out_unlock;
1363 entry = ring_buffer_event_data(event);
1364 entry->ip = ip;
1365
1366 memcpy(&entry->buf, trace_buf, len);
1367 entry->buf[len] = 0;
1368 if (!filter_check_discard(call, entry, tr->buffer, event))
1369 ring_buffer_unlock_commit(tr->buffer, event);
1370
1371 out_unlock:
1372 __raw_spin_unlock(&trace_buf_lock);
1373 raw_local_irq_restore(irq_flags);
1374 unpause_graph_tracing();
1375 out:
1376 atomic_dec_return(&data->disabled);
1377 preempt_enable_notrace();
1378
1379 return len;
1380 }
1381 EXPORT_SYMBOL_GPL(trace_vprintk);
1382
1383 enum trace_file_type {
1384 TRACE_FILE_LAT_FMT = 1,
1385 TRACE_FILE_ANNOTATE = 2,
1386 };
1387
1388 static void trace_iterator_increment(struct trace_iterator *iter)
1389 {
1390 /* Don't allow ftrace to trace into the ring buffers */
1391 ftrace_disable_cpu();
1392
1393 iter->idx++;
1394 if (iter->buffer_iter[iter->cpu])
1395 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1396
1397 ftrace_enable_cpu();
1398 }
1399
1400 static struct trace_entry *
1401 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1402 {
1403 struct ring_buffer_event *event;
1404 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1405
1406 /* Don't allow ftrace to trace into the ring buffers */
1407 ftrace_disable_cpu();
1408
1409 if (buf_iter)
1410 event = ring_buffer_iter_peek(buf_iter, ts);
1411 else
1412 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1413
1414 ftrace_enable_cpu();
1415
1416 return event ? ring_buffer_event_data(event) : NULL;
1417 }
1418
1419 static struct trace_entry *
1420 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1421 {
1422 struct ring_buffer *buffer = iter->tr->buffer;
1423 struct trace_entry *ent, *next = NULL;
1424 int cpu_file = iter->cpu_file;
1425 u64 next_ts = 0, ts;
1426 int next_cpu = -1;
1427 int cpu;
1428
1429 /*
1430 * If we are in a per_cpu trace file, don't bother by iterating over
1431 * all cpu and peek directly.
1432 */
1433 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1434 if (ring_buffer_empty_cpu(buffer, cpu_file))
1435 return NULL;
1436 ent = peek_next_entry(iter, cpu_file, ent_ts);
1437 if (ent_cpu)
1438 *ent_cpu = cpu_file;
1439
1440 return ent;
1441 }
1442
1443 for_each_tracing_cpu(cpu) {
1444
1445 if (ring_buffer_empty_cpu(buffer, cpu))
1446 continue;
1447
1448 ent = peek_next_entry(iter, cpu, &ts);
1449
1450 /*
1451 * Pick the entry with the smallest timestamp:
1452 */
1453 if (ent && (!next || ts < next_ts)) {
1454 next = ent;
1455 next_cpu = cpu;
1456 next_ts = ts;
1457 }
1458 }
1459
1460 if (ent_cpu)
1461 *ent_cpu = next_cpu;
1462
1463 if (ent_ts)
1464 *ent_ts = next_ts;
1465
1466 return next;
1467 }
1468
1469 /* Find the next real entry, without updating the iterator itself */
1470 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1471 int *ent_cpu, u64 *ent_ts)
1472 {
1473 return __find_next_entry(iter, ent_cpu, ent_ts);
1474 }
1475
1476 /* Find the next real entry, and increment the iterator to the next entry */
1477 static void *find_next_entry_inc(struct trace_iterator *iter)
1478 {
1479 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1480
1481 if (iter->ent)
1482 trace_iterator_increment(iter);
1483
1484 return iter->ent ? iter : NULL;
1485 }
1486
1487 static void trace_consume(struct trace_iterator *iter)
1488 {
1489 /* Don't allow ftrace to trace into the ring buffers */
1490 ftrace_disable_cpu();
1491 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1492 ftrace_enable_cpu();
1493 }
1494
1495 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1496 {
1497 struct trace_iterator *iter = m->private;
1498 int i = (int)*pos;
1499 void *ent;
1500
1501 (*pos)++;
1502
1503 /* can't go backwards */
1504 if (iter->idx > i)
1505 return NULL;
1506
1507 if (iter->idx < 0)
1508 ent = find_next_entry_inc(iter);
1509 else
1510 ent = iter;
1511
1512 while (ent && iter->idx < i)
1513 ent = find_next_entry_inc(iter);
1514
1515 iter->pos = *pos;
1516
1517 return ent;
1518 }
1519
1520 /*
1521 * No necessary locking here. The worst thing which can
1522 * happen is loosing events consumed at the same time
1523 * by a trace_pipe reader.
1524 * Other than that, we don't risk to crash the ring buffer
1525 * because it serializes the readers.
1526 *
1527 * The current tracer is copied to avoid a global locking
1528 * all around.
1529 */
1530 static void *s_start(struct seq_file *m, loff_t *pos)
1531 {
1532 struct trace_iterator *iter = m->private;
1533 static struct tracer *old_tracer;
1534 int cpu_file = iter->cpu_file;
1535 void *p = NULL;
1536 loff_t l = 0;
1537 int cpu;
1538
1539 /* copy the tracer to avoid using a global lock all around */
1540 mutex_lock(&trace_types_lock);
1541 if (unlikely(old_tracer != current_trace && current_trace)) {
1542 old_tracer = current_trace;
1543 *iter->trace = *current_trace;
1544 }
1545 mutex_unlock(&trace_types_lock);
1546
1547 atomic_inc(&trace_record_cmdline_disabled);
1548
1549 if (*pos != iter->pos) {
1550 iter->ent = NULL;
1551 iter->cpu = 0;
1552 iter->idx = -1;
1553
1554 ftrace_disable_cpu();
1555
1556 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1557 for_each_tracing_cpu(cpu)
1558 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1559 } else
1560 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1561
1562
1563 ftrace_enable_cpu();
1564
1565 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1566 ;
1567
1568 } else {
1569 l = *pos - 1;
1570 p = s_next(m, p, &l);
1571 }
1572
1573 trace_event_read_lock();
1574 return p;
1575 }
1576
1577 static void s_stop(struct seq_file *m, void *p)
1578 {
1579 atomic_dec(&trace_record_cmdline_disabled);
1580 trace_event_read_unlock();
1581 }
1582
1583 static void print_lat_help_header(struct seq_file *m)
1584 {
1585 seq_puts(m, "# _------=> CPU# \n");
1586 seq_puts(m, "# / _-----=> irqs-off \n");
1587 seq_puts(m, "# | / _----=> need-resched \n");
1588 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1589 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1590 seq_puts(m, "# |||| / \n");
1591 seq_puts(m, "# ||||| delay \n");
1592 seq_puts(m, "# cmd pid ||||| time | caller \n");
1593 seq_puts(m, "# \\ / ||||| \\ | / \n");
1594 }
1595
1596 static void print_func_help_header(struct seq_file *m)
1597 {
1598 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1599 seq_puts(m, "# | | | | |\n");
1600 }
1601
1602
1603 static void
1604 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1605 {
1606 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1607 struct trace_array *tr = iter->tr;
1608 struct trace_array_cpu *data = tr->data[tr->cpu];
1609 struct tracer *type = current_trace;
1610 unsigned long total;
1611 unsigned long entries;
1612 const char *name = "preemption";
1613
1614 if (type)
1615 name = type->name;
1616
1617 entries = ring_buffer_entries(iter->tr->buffer);
1618 total = entries +
1619 ring_buffer_overruns(iter->tr->buffer);
1620
1621 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1622 name, UTS_RELEASE);
1623 seq_puts(m, "# -----------------------------------"
1624 "---------------------------------\n");
1625 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1626 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1627 nsecs_to_usecs(data->saved_latency),
1628 entries,
1629 total,
1630 tr->cpu,
1631 #if defined(CONFIG_PREEMPT_NONE)
1632 "server",
1633 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1634 "desktop",
1635 #elif defined(CONFIG_PREEMPT)
1636 "preempt",
1637 #else
1638 "unknown",
1639 #endif
1640 /* These are reserved for later use */
1641 0, 0, 0, 0);
1642 #ifdef CONFIG_SMP
1643 seq_printf(m, " #P:%d)\n", num_online_cpus());
1644 #else
1645 seq_puts(m, ")\n");
1646 #endif
1647 seq_puts(m, "# -----------------\n");
1648 seq_printf(m, "# | task: %.16s-%d "
1649 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1650 data->comm, data->pid, data->uid, data->nice,
1651 data->policy, data->rt_priority);
1652 seq_puts(m, "# -----------------\n");
1653
1654 if (data->critical_start) {
1655 seq_puts(m, "# => started at: ");
1656 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1657 trace_print_seq(m, &iter->seq);
1658 seq_puts(m, "\n# => ended at: ");
1659 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1660 trace_print_seq(m, &iter->seq);
1661 seq_puts(m, "#\n");
1662 }
1663
1664 seq_puts(m, "#\n");
1665 }
1666
1667 static void test_cpu_buff_start(struct trace_iterator *iter)
1668 {
1669 struct trace_seq *s = &iter->seq;
1670
1671 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1672 return;
1673
1674 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1675 return;
1676
1677 if (cpumask_test_cpu(iter->cpu, iter->started))
1678 return;
1679
1680 cpumask_set_cpu(iter->cpu, iter->started);
1681
1682 /* Don't print started cpu buffer for the first entry of the trace */
1683 if (iter->idx > 1)
1684 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1685 iter->cpu);
1686 }
1687
1688 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1689 {
1690 struct trace_seq *s = &iter->seq;
1691 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1692 struct trace_entry *entry;
1693 struct trace_event *event;
1694
1695 entry = iter->ent;
1696
1697 test_cpu_buff_start(iter);
1698
1699 event = ftrace_find_event(entry->type);
1700
1701 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1702 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1703 if (!trace_print_lat_context(iter))
1704 goto partial;
1705 } else {
1706 if (!trace_print_context(iter))
1707 goto partial;
1708 }
1709 }
1710
1711 if (event)
1712 return event->trace(iter, sym_flags);
1713
1714 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1715 goto partial;
1716
1717 return TRACE_TYPE_HANDLED;
1718 partial:
1719 return TRACE_TYPE_PARTIAL_LINE;
1720 }
1721
1722 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1723 {
1724 struct trace_seq *s = &iter->seq;
1725 struct trace_entry *entry;
1726 struct trace_event *event;
1727
1728 entry = iter->ent;
1729
1730 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1731 if (!trace_seq_printf(s, "%d %d %llu ",
1732 entry->pid, iter->cpu, iter->ts))
1733 goto partial;
1734 }
1735
1736 event = ftrace_find_event(entry->type);
1737 if (event)
1738 return event->raw(iter, 0);
1739
1740 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1741 goto partial;
1742
1743 return TRACE_TYPE_HANDLED;
1744 partial:
1745 return TRACE_TYPE_PARTIAL_LINE;
1746 }
1747
1748 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1749 {
1750 struct trace_seq *s = &iter->seq;
1751 unsigned char newline = '\n';
1752 struct trace_entry *entry;
1753 struct trace_event *event;
1754
1755 entry = iter->ent;
1756
1757 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1758 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1759 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1760 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1761 }
1762
1763 event = ftrace_find_event(entry->type);
1764 if (event) {
1765 enum print_line_t ret = event->hex(iter, 0);
1766 if (ret != TRACE_TYPE_HANDLED)
1767 return ret;
1768 }
1769
1770 SEQ_PUT_FIELD_RET(s, newline);
1771
1772 return TRACE_TYPE_HANDLED;
1773 }
1774
1775 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1776 {
1777 struct trace_seq *s = &iter->seq;
1778 struct trace_entry *entry;
1779 struct trace_event *event;
1780
1781 entry = iter->ent;
1782
1783 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1784 SEQ_PUT_FIELD_RET(s, entry->pid);
1785 SEQ_PUT_FIELD_RET(s, iter->cpu);
1786 SEQ_PUT_FIELD_RET(s, iter->ts);
1787 }
1788
1789 event = ftrace_find_event(entry->type);
1790 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1791 }
1792
1793 static int trace_empty(struct trace_iterator *iter)
1794 {
1795 int cpu;
1796
1797 /* If we are looking at one CPU buffer, only check that one */
1798 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1799 cpu = iter->cpu_file;
1800 if (iter->buffer_iter[cpu]) {
1801 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1802 return 0;
1803 } else {
1804 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1805 return 0;
1806 }
1807 return 1;
1808 }
1809
1810 for_each_tracing_cpu(cpu) {
1811 if (iter->buffer_iter[cpu]) {
1812 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1813 return 0;
1814 } else {
1815 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1816 return 0;
1817 }
1818 }
1819
1820 return 1;
1821 }
1822
1823 /* Called with trace_event_read_lock() held. */
1824 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1825 {
1826 enum print_line_t ret;
1827
1828 if (iter->trace && iter->trace->print_line) {
1829 ret = iter->trace->print_line(iter);
1830 if (ret != TRACE_TYPE_UNHANDLED)
1831 return ret;
1832 }
1833
1834 if (iter->ent->type == TRACE_BPRINT &&
1835 trace_flags & TRACE_ITER_PRINTK &&
1836 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1837 return trace_print_bprintk_msg_only(iter);
1838
1839 if (iter->ent->type == TRACE_PRINT &&
1840 trace_flags & TRACE_ITER_PRINTK &&
1841 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1842 return trace_print_printk_msg_only(iter);
1843
1844 if (trace_flags & TRACE_ITER_BIN)
1845 return print_bin_fmt(iter);
1846
1847 if (trace_flags & TRACE_ITER_HEX)
1848 return print_hex_fmt(iter);
1849
1850 if (trace_flags & TRACE_ITER_RAW)
1851 return print_raw_fmt(iter);
1852
1853 return print_trace_fmt(iter);
1854 }
1855
1856 static int s_show(struct seq_file *m, void *v)
1857 {
1858 struct trace_iterator *iter = v;
1859
1860 if (iter->ent == NULL) {
1861 if (iter->tr) {
1862 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1863 seq_puts(m, "#\n");
1864 }
1865 if (iter->trace && iter->trace->print_header)
1866 iter->trace->print_header(m);
1867 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1868 /* print nothing if the buffers are empty */
1869 if (trace_empty(iter))
1870 return 0;
1871 print_trace_header(m, iter);
1872 if (!(trace_flags & TRACE_ITER_VERBOSE))
1873 print_lat_help_header(m);
1874 } else {
1875 if (!(trace_flags & TRACE_ITER_VERBOSE))
1876 print_func_help_header(m);
1877 }
1878 } else {
1879 print_trace_line(iter);
1880 trace_print_seq(m, &iter->seq);
1881 }
1882
1883 return 0;
1884 }
1885
1886 static struct seq_operations tracer_seq_ops = {
1887 .start = s_start,
1888 .next = s_next,
1889 .stop = s_stop,
1890 .show = s_show,
1891 };
1892
1893 static struct trace_iterator *
1894 __tracing_open(struct inode *inode, struct file *file)
1895 {
1896 long cpu_file = (long) inode->i_private;
1897 void *fail_ret = ERR_PTR(-ENOMEM);
1898 struct trace_iterator *iter;
1899 struct seq_file *m;
1900 int cpu, ret;
1901
1902 if (tracing_disabled)
1903 return ERR_PTR(-ENODEV);
1904
1905 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1906 if (!iter)
1907 return ERR_PTR(-ENOMEM);
1908
1909 /*
1910 * We make a copy of the current tracer to avoid concurrent
1911 * changes on it while we are reading.
1912 */
1913 mutex_lock(&trace_types_lock);
1914 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1915 if (!iter->trace)
1916 goto fail;
1917
1918 if (current_trace)
1919 *iter->trace = *current_trace;
1920
1921 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1922 goto fail;
1923
1924 cpumask_clear(iter->started);
1925
1926 if (current_trace && current_trace->print_max)
1927 iter->tr = &max_tr;
1928 else
1929 iter->tr = &global_trace;
1930 iter->pos = -1;
1931 mutex_init(&iter->mutex);
1932 iter->cpu_file = cpu_file;
1933
1934 /* Notify the tracer early; before we stop tracing. */
1935 if (iter->trace && iter->trace->open)
1936 iter->trace->open(iter);
1937
1938 /* Annotate start of buffers if we had overruns */
1939 if (ring_buffer_overruns(iter->tr->buffer))
1940 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1941
1942 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1943 for_each_tracing_cpu(cpu) {
1944
1945 iter->buffer_iter[cpu] =
1946 ring_buffer_read_start(iter->tr->buffer, cpu);
1947 }
1948 } else {
1949 cpu = iter->cpu_file;
1950 iter->buffer_iter[cpu] =
1951 ring_buffer_read_start(iter->tr->buffer, cpu);
1952 }
1953
1954 /* TODO stop tracer */
1955 ret = seq_open(file, &tracer_seq_ops);
1956 if (ret < 0) {
1957 fail_ret = ERR_PTR(ret);
1958 goto fail_buffer;
1959 }
1960
1961 m = file->private_data;
1962 m->private = iter;
1963
1964 /* stop the trace while dumping */
1965 tracing_stop();
1966
1967 mutex_unlock(&trace_types_lock);
1968
1969 return iter;
1970
1971 fail_buffer:
1972 for_each_tracing_cpu(cpu) {
1973 if (iter->buffer_iter[cpu])
1974 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1975 }
1976 free_cpumask_var(iter->started);
1977 fail:
1978 mutex_unlock(&trace_types_lock);
1979 kfree(iter->trace);
1980 kfree(iter);
1981
1982 return fail_ret;
1983 }
1984
1985 int tracing_open_generic(struct inode *inode, struct file *filp)
1986 {
1987 if (tracing_disabled)
1988 return -ENODEV;
1989
1990 filp->private_data = inode->i_private;
1991 return 0;
1992 }
1993
1994 static int tracing_release(struct inode *inode, struct file *file)
1995 {
1996 struct seq_file *m = (struct seq_file *)file->private_data;
1997 struct trace_iterator *iter;
1998 int cpu;
1999
2000 if (!(file->f_mode & FMODE_READ))
2001 return 0;
2002
2003 iter = m->private;
2004
2005 mutex_lock(&trace_types_lock);
2006 for_each_tracing_cpu(cpu) {
2007 if (iter->buffer_iter[cpu])
2008 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2009 }
2010
2011 if (iter->trace && iter->trace->close)
2012 iter->trace->close(iter);
2013
2014 /* reenable tracing if it was previously enabled */
2015 tracing_start();
2016 mutex_unlock(&trace_types_lock);
2017
2018 seq_release(inode, file);
2019 mutex_destroy(&iter->mutex);
2020 free_cpumask_var(iter->started);
2021 kfree(iter->trace);
2022 kfree(iter);
2023 return 0;
2024 }
2025
2026 static int tracing_open(struct inode *inode, struct file *file)
2027 {
2028 struct trace_iterator *iter;
2029 int ret = 0;
2030
2031 /* If this file was open for write, then erase contents */
2032 if ((file->f_mode & FMODE_WRITE) &&
2033 !(file->f_flags & O_APPEND)) {
2034 long cpu = (long) inode->i_private;
2035
2036 if (cpu == TRACE_PIPE_ALL_CPU)
2037 tracing_reset_online_cpus(&global_trace);
2038 else
2039 tracing_reset(&global_trace, cpu);
2040 }
2041
2042 if (file->f_mode & FMODE_READ) {
2043 iter = __tracing_open(inode, file);
2044 if (IS_ERR(iter))
2045 ret = PTR_ERR(iter);
2046 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2047 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2048 }
2049 return ret;
2050 }
2051
2052 static void *
2053 t_next(struct seq_file *m, void *v, loff_t *pos)
2054 {
2055 struct tracer *t = v;
2056
2057 (*pos)++;
2058
2059 if (t)
2060 t = t->next;
2061
2062 return t;
2063 }
2064
2065 static void *t_start(struct seq_file *m, loff_t *pos)
2066 {
2067 struct tracer *t;
2068 loff_t l = 0;
2069
2070 mutex_lock(&trace_types_lock);
2071 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2072 ;
2073
2074 return t;
2075 }
2076
2077 static void t_stop(struct seq_file *m, void *p)
2078 {
2079 mutex_unlock(&trace_types_lock);
2080 }
2081
2082 static int t_show(struct seq_file *m, void *v)
2083 {
2084 struct tracer *t = v;
2085
2086 if (!t)
2087 return 0;
2088
2089 seq_printf(m, "%s", t->name);
2090 if (t->next)
2091 seq_putc(m, ' ');
2092 else
2093 seq_putc(m, '\n');
2094
2095 return 0;
2096 }
2097
2098 static struct seq_operations show_traces_seq_ops = {
2099 .start = t_start,
2100 .next = t_next,
2101 .stop = t_stop,
2102 .show = t_show,
2103 };
2104
2105 static int show_traces_open(struct inode *inode, struct file *file)
2106 {
2107 if (tracing_disabled)
2108 return -ENODEV;
2109
2110 return seq_open(file, &show_traces_seq_ops);
2111 }
2112
2113 static ssize_t
2114 tracing_write_stub(struct file *filp, const char __user *ubuf,
2115 size_t count, loff_t *ppos)
2116 {
2117 return count;
2118 }
2119
2120 static const struct file_operations tracing_fops = {
2121 .open = tracing_open,
2122 .read = seq_read,
2123 .write = tracing_write_stub,
2124 .llseek = seq_lseek,
2125 .release = tracing_release,
2126 };
2127
2128 static const struct file_operations show_traces_fops = {
2129 .open = show_traces_open,
2130 .read = seq_read,
2131 .release = seq_release,
2132 };
2133
2134 /*
2135 * Only trace on a CPU if the bitmask is set:
2136 */
2137 static cpumask_var_t tracing_cpumask;
2138
2139 /*
2140 * The tracer itself will not take this lock, but still we want
2141 * to provide a consistent cpumask to user-space:
2142 */
2143 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2144
2145 /*
2146 * Temporary storage for the character representation of the
2147 * CPU bitmask (and one more byte for the newline):
2148 */
2149 static char mask_str[NR_CPUS + 1];
2150
2151 static ssize_t
2152 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2153 size_t count, loff_t *ppos)
2154 {
2155 int len;
2156
2157 mutex_lock(&tracing_cpumask_update_lock);
2158
2159 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2160 if (count - len < 2) {
2161 count = -EINVAL;
2162 goto out_err;
2163 }
2164 len += sprintf(mask_str + len, "\n");
2165 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2166
2167 out_err:
2168 mutex_unlock(&tracing_cpumask_update_lock);
2169
2170 return count;
2171 }
2172
2173 static ssize_t
2174 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2175 size_t count, loff_t *ppos)
2176 {
2177 int err, cpu;
2178 cpumask_var_t tracing_cpumask_new;
2179
2180 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2181 return -ENOMEM;
2182
2183 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2184 if (err)
2185 goto err_unlock;
2186
2187 mutex_lock(&tracing_cpumask_update_lock);
2188
2189 local_irq_disable();
2190 __raw_spin_lock(&ftrace_max_lock);
2191 for_each_tracing_cpu(cpu) {
2192 /*
2193 * Increase/decrease the disabled counter if we are
2194 * about to flip a bit in the cpumask:
2195 */
2196 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2197 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2198 atomic_inc(&global_trace.data[cpu]->disabled);
2199 }
2200 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2201 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2202 atomic_dec(&global_trace.data[cpu]->disabled);
2203 }
2204 }
2205 __raw_spin_unlock(&ftrace_max_lock);
2206 local_irq_enable();
2207
2208 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2209
2210 mutex_unlock(&tracing_cpumask_update_lock);
2211 free_cpumask_var(tracing_cpumask_new);
2212
2213 return count;
2214
2215 err_unlock:
2216 free_cpumask_var(tracing_cpumask_new);
2217
2218 return err;
2219 }
2220
2221 static const struct file_operations tracing_cpumask_fops = {
2222 .open = tracing_open_generic,
2223 .read = tracing_cpumask_read,
2224 .write = tracing_cpumask_write,
2225 };
2226
2227 static ssize_t
2228 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2229 size_t cnt, loff_t *ppos)
2230 {
2231 struct tracer_opt *trace_opts;
2232 u32 tracer_flags;
2233 int len = 0;
2234 char *buf;
2235 int r = 0;
2236 int i;
2237
2238
2239 /* calculate max size */
2240 for (i = 0; trace_options[i]; i++) {
2241 len += strlen(trace_options[i]);
2242 len += 3; /* "no" and newline */
2243 }
2244
2245 mutex_lock(&trace_types_lock);
2246 tracer_flags = current_trace->flags->val;
2247 trace_opts = current_trace->flags->opts;
2248
2249 /*
2250 * Increase the size with names of options specific
2251 * of the current tracer.
2252 */
2253 for (i = 0; trace_opts[i].name; i++) {
2254 len += strlen(trace_opts[i].name);
2255 len += 3; /* "no" and newline */
2256 }
2257
2258 /* +2 for \n and \0 */
2259 buf = kmalloc(len + 2, GFP_KERNEL);
2260 if (!buf) {
2261 mutex_unlock(&trace_types_lock);
2262 return -ENOMEM;
2263 }
2264
2265 for (i = 0; trace_options[i]; i++) {
2266 if (trace_flags & (1 << i))
2267 r += sprintf(buf + r, "%s\n", trace_options[i]);
2268 else
2269 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2270 }
2271
2272 for (i = 0; trace_opts[i].name; i++) {
2273 if (tracer_flags & trace_opts[i].bit)
2274 r += sprintf(buf + r, "%s\n",
2275 trace_opts[i].name);
2276 else
2277 r += sprintf(buf + r, "no%s\n",
2278 trace_opts[i].name);
2279 }
2280 mutex_unlock(&trace_types_lock);
2281
2282 WARN_ON(r >= len + 2);
2283
2284 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2285
2286 kfree(buf);
2287 return r;
2288 }
2289
2290 /* Try to assign a tracer specific option */
2291 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2292 {
2293 struct tracer_flags *trace_flags = trace->flags;
2294 struct tracer_opt *opts = NULL;
2295 int ret = 0, i = 0;
2296 int len;
2297
2298 for (i = 0; trace_flags->opts[i].name; i++) {
2299 opts = &trace_flags->opts[i];
2300 len = strlen(opts->name);
2301
2302 if (strncmp(cmp, opts->name, len) == 0) {
2303 ret = trace->set_flag(trace_flags->val,
2304 opts->bit, !neg);
2305 break;
2306 }
2307 }
2308 /* Not found */
2309 if (!trace_flags->opts[i].name)
2310 return -EINVAL;
2311
2312 /* Refused to handle */
2313 if (ret)
2314 return ret;
2315
2316 if (neg)
2317 trace_flags->val &= ~opts->bit;
2318 else
2319 trace_flags->val |= opts->bit;
2320
2321 return 0;
2322 }
2323
2324 static void set_tracer_flags(unsigned int mask, int enabled)
2325 {
2326 /* do nothing if flag is already set */
2327 if (!!(trace_flags & mask) == !!enabled)
2328 return;
2329
2330 if (enabled)
2331 trace_flags |= mask;
2332 else
2333 trace_flags &= ~mask;
2334
2335 if (mask == TRACE_ITER_GLOBAL_CLK) {
2336 u64 (*func)(void);
2337
2338 if (enabled)
2339 func = trace_clock_global;
2340 else
2341 func = trace_clock_local;
2342
2343 mutex_lock(&trace_types_lock);
2344 ring_buffer_set_clock(global_trace.buffer, func);
2345
2346 if (max_tr.buffer)
2347 ring_buffer_set_clock(max_tr.buffer, func);
2348 mutex_unlock(&trace_types_lock);
2349 }
2350 }
2351
2352 static ssize_t
2353 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2354 size_t cnt, loff_t *ppos)
2355 {
2356 char buf[64];
2357 char *cmp = buf;
2358 int neg = 0;
2359 int ret;
2360 int i;
2361
2362 if (cnt >= sizeof(buf))
2363 return -EINVAL;
2364
2365 if (copy_from_user(&buf, ubuf, cnt))
2366 return -EFAULT;
2367
2368 buf[cnt] = 0;
2369
2370 if (strncmp(buf, "no", 2) == 0) {
2371 neg = 1;
2372 cmp += 2;
2373 }
2374
2375 for (i = 0; trace_options[i]; i++) {
2376 int len = strlen(trace_options[i]);
2377
2378 if (strncmp(cmp, trace_options[i], len) == 0) {
2379 set_tracer_flags(1 << i, !neg);
2380 break;
2381 }
2382 }
2383
2384 /* If no option could be set, test the specific tracer options */
2385 if (!trace_options[i]) {
2386 mutex_lock(&trace_types_lock);
2387 ret = set_tracer_option(current_trace, cmp, neg);
2388 mutex_unlock(&trace_types_lock);
2389 if (ret)
2390 return ret;
2391 }
2392
2393 filp->f_pos += cnt;
2394
2395 return cnt;
2396 }
2397
2398 static const struct file_operations tracing_iter_fops = {
2399 .open = tracing_open_generic,
2400 .read = tracing_trace_options_read,
2401 .write = tracing_trace_options_write,
2402 };
2403
2404 static const char readme_msg[] =
2405 "tracing mini-HOWTO:\n\n"
2406 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2407 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2408 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2409 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2410 "nop\n"
2411 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2412 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2413 "sched_switch\n"
2414 "# cat /sys/kernel/debug/tracing/trace_options\n"
2415 "noprint-parent nosym-offset nosym-addr noverbose\n"
2416 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2417 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2418 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2419 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2420 ;
2421
2422 static ssize_t
2423 tracing_readme_read(struct file *filp, char __user *ubuf,
2424 size_t cnt, loff_t *ppos)
2425 {
2426 return simple_read_from_buffer(ubuf, cnt, ppos,
2427 readme_msg, strlen(readme_msg));
2428 }
2429
2430 static const struct file_operations tracing_readme_fops = {
2431 .open = tracing_open_generic,
2432 .read = tracing_readme_read,
2433 };
2434
2435 static ssize_t
2436 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2437 size_t cnt, loff_t *ppos)
2438 {
2439 char *buf_comm;
2440 char *file_buf;
2441 char *buf;
2442 int len = 0;
2443 int pid;
2444 int i;
2445
2446 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2447 if (!file_buf)
2448 return -ENOMEM;
2449
2450 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2451 if (!buf_comm) {
2452 kfree(file_buf);
2453 return -ENOMEM;
2454 }
2455
2456 buf = file_buf;
2457
2458 for (i = 0; i < SAVED_CMDLINES; i++) {
2459 int r;
2460
2461 pid = map_cmdline_to_pid[i];
2462 if (pid == -1 || pid == NO_CMDLINE_MAP)
2463 continue;
2464
2465 trace_find_cmdline(pid, buf_comm);
2466 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2467 buf += r;
2468 len += r;
2469 }
2470
2471 len = simple_read_from_buffer(ubuf, cnt, ppos,
2472 file_buf, len);
2473
2474 kfree(file_buf);
2475 kfree(buf_comm);
2476
2477 return len;
2478 }
2479
2480 static const struct file_operations tracing_saved_cmdlines_fops = {
2481 .open = tracing_open_generic,
2482 .read = tracing_saved_cmdlines_read,
2483 };
2484
2485 static ssize_t
2486 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2487 size_t cnt, loff_t *ppos)
2488 {
2489 char buf[64];
2490 int r;
2491
2492 r = sprintf(buf, "%u\n", tracer_enabled);
2493 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2494 }
2495
2496 static ssize_t
2497 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2498 size_t cnt, loff_t *ppos)
2499 {
2500 struct trace_array *tr = filp->private_data;
2501 char buf[64];
2502 unsigned long val;
2503 int ret;
2504
2505 if (cnt >= sizeof(buf))
2506 return -EINVAL;
2507
2508 if (copy_from_user(&buf, ubuf, cnt))
2509 return -EFAULT;
2510
2511 buf[cnt] = 0;
2512
2513 ret = strict_strtoul(buf, 10, &val);
2514 if (ret < 0)
2515 return ret;
2516
2517 val = !!val;
2518
2519 mutex_lock(&trace_types_lock);
2520 if (tracer_enabled ^ val) {
2521 if (val) {
2522 tracer_enabled = 1;
2523 if (current_trace->start)
2524 current_trace->start(tr);
2525 tracing_start();
2526 } else {
2527 tracer_enabled = 0;
2528 tracing_stop();
2529 if (current_trace->stop)
2530 current_trace->stop(tr);
2531 }
2532 }
2533 mutex_unlock(&trace_types_lock);
2534
2535 filp->f_pos += cnt;
2536
2537 return cnt;
2538 }
2539
2540 static ssize_t
2541 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2542 size_t cnt, loff_t *ppos)
2543 {
2544 char buf[max_tracer_type_len+2];
2545 int r;
2546
2547 mutex_lock(&trace_types_lock);
2548 if (current_trace)
2549 r = sprintf(buf, "%s\n", current_trace->name);
2550 else
2551 r = sprintf(buf, "\n");
2552 mutex_unlock(&trace_types_lock);
2553
2554 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2555 }
2556
2557 int tracer_init(struct tracer *t, struct trace_array *tr)
2558 {
2559 tracing_reset_online_cpus(tr);
2560 return t->init(tr);
2561 }
2562
2563 static int tracing_resize_ring_buffer(unsigned long size)
2564 {
2565 int ret;
2566
2567 /*
2568 * If kernel or user changes the size of the ring buffer
2569 * we use the size that was given, and we can forget about
2570 * expanding it later.
2571 */
2572 ring_buffer_expanded = 1;
2573
2574 ret = ring_buffer_resize(global_trace.buffer, size);
2575 if (ret < 0)
2576 return ret;
2577
2578 ret = ring_buffer_resize(max_tr.buffer, size);
2579 if (ret < 0) {
2580 int r;
2581
2582 r = ring_buffer_resize(global_trace.buffer,
2583 global_trace.entries);
2584 if (r < 0) {
2585 /*
2586 * AARGH! We are left with different
2587 * size max buffer!!!!
2588 * The max buffer is our "snapshot" buffer.
2589 * When a tracer needs a snapshot (one of the
2590 * latency tracers), it swaps the max buffer
2591 * with the saved snap shot. We succeeded to
2592 * update the size of the main buffer, but failed to
2593 * update the size of the max buffer. But when we tried
2594 * to reset the main buffer to the original size, we
2595 * failed there too. This is very unlikely to
2596 * happen, but if it does, warn and kill all
2597 * tracing.
2598 */
2599 WARN_ON(1);
2600 tracing_disabled = 1;
2601 }
2602 return ret;
2603 }
2604
2605 global_trace.entries = size;
2606
2607 return ret;
2608 }
2609
2610 /**
2611 * tracing_update_buffers - used by tracing facility to expand ring buffers
2612 *
2613 * To save on memory when the tracing is never used on a system with it
2614 * configured in. The ring buffers are set to a minimum size. But once
2615 * a user starts to use the tracing facility, then they need to grow
2616 * to their default size.
2617 *
2618 * This function is to be called when a tracer is about to be used.
2619 */
2620 int tracing_update_buffers(void)
2621 {
2622 int ret = 0;
2623
2624 mutex_lock(&trace_types_lock);
2625 if (!ring_buffer_expanded)
2626 ret = tracing_resize_ring_buffer(trace_buf_size);
2627 mutex_unlock(&trace_types_lock);
2628
2629 return ret;
2630 }
2631
2632 struct trace_option_dentry;
2633
2634 static struct trace_option_dentry *
2635 create_trace_option_files(struct tracer *tracer);
2636
2637 static void
2638 destroy_trace_option_files(struct trace_option_dentry *topts);
2639
2640 static int tracing_set_tracer(const char *buf)
2641 {
2642 static struct trace_option_dentry *topts;
2643 struct trace_array *tr = &global_trace;
2644 struct tracer *t;
2645 int ret = 0;
2646
2647 mutex_lock(&trace_types_lock);
2648
2649 if (!ring_buffer_expanded) {
2650 ret = tracing_resize_ring_buffer(trace_buf_size);
2651 if (ret < 0)
2652 goto out;
2653 ret = 0;
2654 }
2655
2656 for (t = trace_types; t; t = t->next) {
2657 if (strcmp(t->name, buf) == 0)
2658 break;
2659 }
2660 if (!t) {
2661 ret = -EINVAL;
2662 goto out;
2663 }
2664 if (t == current_trace)
2665 goto out;
2666
2667 trace_branch_disable();
2668 if (current_trace && current_trace->reset)
2669 current_trace->reset(tr);
2670
2671 destroy_trace_option_files(topts);
2672
2673 current_trace = t;
2674
2675 topts = create_trace_option_files(current_trace);
2676
2677 if (t->init) {
2678 ret = tracer_init(t, tr);
2679 if (ret)
2680 goto out;
2681 }
2682
2683 trace_branch_enable(tr);
2684 out:
2685 mutex_unlock(&trace_types_lock);
2686
2687 return ret;
2688 }
2689
2690 static ssize_t
2691 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2692 size_t cnt, loff_t *ppos)
2693 {
2694 char buf[max_tracer_type_len+1];
2695 int i;
2696 size_t ret;
2697 int err;
2698
2699 ret = cnt;
2700
2701 if (cnt > max_tracer_type_len)
2702 cnt = max_tracer_type_len;
2703
2704 if (copy_from_user(&buf, ubuf, cnt))
2705 return -EFAULT;
2706
2707 buf[cnt] = 0;
2708
2709 /* strip ending whitespace. */
2710 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2711 buf[i] = 0;
2712
2713 err = tracing_set_tracer(buf);
2714 if (err)
2715 return err;
2716
2717 filp->f_pos += ret;
2718
2719 return ret;
2720 }
2721
2722 static ssize_t
2723 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2724 size_t cnt, loff_t *ppos)
2725 {
2726 unsigned long *ptr = filp->private_data;
2727 char buf[64];
2728 int r;
2729
2730 r = snprintf(buf, sizeof(buf), "%ld\n",
2731 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2732 if (r > sizeof(buf))
2733 r = sizeof(buf);
2734 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2735 }
2736
2737 static ssize_t
2738 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2739 size_t cnt, loff_t *ppos)
2740 {
2741 unsigned long *ptr = filp->private_data;
2742 char buf[64];
2743 unsigned long val;
2744 int ret;
2745
2746 if (cnt >= sizeof(buf))
2747 return -EINVAL;
2748
2749 if (copy_from_user(&buf, ubuf, cnt))
2750 return -EFAULT;
2751
2752 buf[cnt] = 0;
2753
2754 ret = strict_strtoul(buf, 10, &val);
2755 if (ret < 0)
2756 return ret;
2757
2758 *ptr = val * 1000;
2759
2760 return cnt;
2761 }
2762
2763 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2764 {
2765 long cpu_file = (long) inode->i_private;
2766 struct trace_iterator *iter;
2767 int ret = 0;
2768
2769 if (tracing_disabled)
2770 return -ENODEV;
2771
2772 mutex_lock(&trace_types_lock);
2773
2774 /* We only allow one reader per cpu */
2775 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2776 if (!cpumask_empty(tracing_reader_cpumask)) {
2777 ret = -EBUSY;
2778 goto out;
2779 }
2780 cpumask_setall(tracing_reader_cpumask);
2781 } else {
2782 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2783 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2784 else {
2785 ret = -EBUSY;
2786 goto out;
2787 }
2788 }
2789
2790 /* create a buffer to store the information to pass to userspace */
2791 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2792 if (!iter) {
2793 ret = -ENOMEM;
2794 goto out;
2795 }
2796
2797 /*
2798 * We make a copy of the current tracer to avoid concurrent
2799 * changes on it while we are reading.
2800 */
2801 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2802 if (!iter->trace) {
2803 ret = -ENOMEM;
2804 goto fail;
2805 }
2806 if (current_trace)
2807 *iter->trace = *current_trace;
2808
2809 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2810 ret = -ENOMEM;
2811 goto fail;
2812 }
2813
2814 /* trace pipe does not show start of buffer */
2815 cpumask_setall(iter->started);
2816
2817 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2818 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2819
2820 iter->cpu_file = cpu_file;
2821 iter->tr = &global_trace;
2822 mutex_init(&iter->mutex);
2823 filp->private_data = iter;
2824
2825 if (iter->trace->pipe_open)
2826 iter->trace->pipe_open(iter);
2827
2828 out:
2829 mutex_unlock(&trace_types_lock);
2830 return ret;
2831
2832 fail:
2833 kfree(iter->trace);
2834 kfree(iter);
2835 mutex_unlock(&trace_types_lock);
2836 return ret;
2837 }
2838
2839 static int tracing_release_pipe(struct inode *inode, struct file *file)
2840 {
2841 struct trace_iterator *iter = file->private_data;
2842
2843 mutex_lock(&trace_types_lock);
2844
2845 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2846 cpumask_clear(tracing_reader_cpumask);
2847 else
2848 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2849
2850 mutex_unlock(&trace_types_lock);
2851
2852 free_cpumask_var(iter->started);
2853 mutex_destroy(&iter->mutex);
2854 kfree(iter->trace);
2855 kfree(iter);
2856
2857 return 0;
2858 }
2859
2860 static unsigned int
2861 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2862 {
2863 struct trace_iterator *iter = filp->private_data;
2864
2865 if (trace_flags & TRACE_ITER_BLOCK) {
2866 /*
2867 * Always select as readable when in blocking mode
2868 */
2869 return POLLIN | POLLRDNORM;
2870 } else {
2871 if (!trace_empty(iter))
2872 return POLLIN | POLLRDNORM;
2873 poll_wait(filp, &trace_wait, poll_table);
2874 if (!trace_empty(iter))
2875 return POLLIN | POLLRDNORM;
2876
2877 return 0;
2878 }
2879 }
2880
2881
2882 void default_wait_pipe(struct trace_iterator *iter)
2883 {
2884 DEFINE_WAIT(wait);
2885
2886 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2887
2888 if (trace_empty(iter))
2889 schedule();
2890
2891 finish_wait(&trace_wait, &wait);
2892 }
2893
2894 /*
2895 * This is a make-shift waitqueue.
2896 * A tracer might use this callback on some rare cases:
2897 *
2898 * 1) the current tracer might hold the runqueue lock when it wakes up
2899 * a reader, hence a deadlock (sched, function, and function graph tracers)
2900 * 2) the function tracers, trace all functions, we don't want
2901 * the overhead of calling wake_up and friends
2902 * (and tracing them too)
2903 *
2904 * Anyway, this is really very primitive wakeup.
2905 */
2906 void poll_wait_pipe(struct trace_iterator *iter)
2907 {
2908 set_current_state(TASK_INTERRUPTIBLE);
2909 /* sleep for 100 msecs, and try again. */
2910 schedule_timeout(HZ / 10);
2911 }
2912
2913 /* Must be called with trace_types_lock mutex held. */
2914 static int tracing_wait_pipe(struct file *filp)
2915 {
2916 struct trace_iterator *iter = filp->private_data;
2917
2918 while (trace_empty(iter)) {
2919
2920 if ((filp->f_flags & O_NONBLOCK)) {
2921 return -EAGAIN;
2922 }
2923
2924 mutex_unlock(&iter->mutex);
2925
2926 iter->trace->wait_pipe(iter);
2927
2928 mutex_lock(&iter->mutex);
2929
2930 if (signal_pending(current))
2931 return -EINTR;
2932
2933 /*
2934 * We block until we read something and tracing is disabled.
2935 * We still block if tracing is disabled, but we have never
2936 * read anything. This allows a user to cat this file, and
2937 * then enable tracing. But after we have read something,
2938 * we give an EOF when tracing is again disabled.
2939 *
2940 * iter->pos will be 0 if we haven't read anything.
2941 */
2942 if (!tracer_enabled && iter->pos)
2943 break;
2944 }
2945
2946 return 1;
2947 }
2948
2949 /*
2950 * Consumer reader.
2951 */
2952 static ssize_t
2953 tracing_read_pipe(struct file *filp, char __user *ubuf,
2954 size_t cnt, loff_t *ppos)
2955 {
2956 struct trace_iterator *iter = filp->private_data;
2957 static struct tracer *old_tracer;
2958 ssize_t sret;
2959
2960 /* return any leftover data */
2961 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2962 if (sret != -EBUSY)
2963 return sret;
2964
2965 trace_seq_init(&iter->seq);
2966
2967 /* copy the tracer to avoid using a global lock all around */
2968 mutex_lock(&trace_types_lock);
2969 if (unlikely(old_tracer != current_trace && current_trace)) {
2970 old_tracer = current_trace;
2971 *iter->trace = *current_trace;
2972 }
2973 mutex_unlock(&trace_types_lock);
2974
2975 /*
2976 * Avoid more than one consumer on a single file descriptor
2977 * This is just a matter of traces coherency, the ring buffer itself
2978 * is protected.
2979 */
2980 mutex_lock(&iter->mutex);
2981 if (iter->trace->read) {
2982 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2983 if (sret)
2984 goto out;
2985 }
2986
2987 waitagain:
2988 sret = tracing_wait_pipe(filp);
2989 if (sret <= 0)
2990 goto out;
2991
2992 /* stop when tracing is finished */
2993 if (trace_empty(iter)) {
2994 sret = 0;
2995 goto out;
2996 }
2997
2998 if (cnt >= PAGE_SIZE)
2999 cnt = PAGE_SIZE - 1;
3000
3001 /* reset all but tr, trace, and overruns */
3002 memset(&iter->seq, 0,
3003 sizeof(struct trace_iterator) -
3004 offsetof(struct trace_iterator, seq));
3005 iter->pos = -1;
3006
3007 trace_event_read_lock();
3008 while (find_next_entry_inc(iter) != NULL) {
3009 enum print_line_t ret;
3010 int len = iter->seq.len;
3011
3012 ret = print_trace_line(iter);
3013 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3014 /* don't print partial lines */
3015 iter->seq.len = len;
3016 break;
3017 }
3018 if (ret != TRACE_TYPE_NO_CONSUME)
3019 trace_consume(iter);
3020
3021 if (iter->seq.len >= cnt)
3022 break;
3023 }
3024 trace_event_read_unlock();
3025
3026 /* Now copy what we have to the user */
3027 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3028 if (iter->seq.readpos >= iter->seq.len)
3029 trace_seq_init(&iter->seq);
3030
3031 /*
3032 * If there was nothing to send to user, inspite of consuming trace
3033 * entries, go back to wait for more entries.
3034 */
3035 if (sret == -EBUSY)
3036 goto waitagain;
3037
3038 out:
3039 mutex_unlock(&iter->mutex);
3040
3041 return sret;
3042 }
3043
3044 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3045 struct pipe_buffer *buf)
3046 {
3047 __free_page(buf->page);
3048 }
3049
3050 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3051 unsigned int idx)
3052 {
3053 __free_page(spd->pages[idx]);
3054 }
3055
3056 static struct pipe_buf_operations tracing_pipe_buf_ops = {
3057 .can_merge = 0,
3058 .map = generic_pipe_buf_map,
3059 .unmap = generic_pipe_buf_unmap,
3060 .confirm = generic_pipe_buf_confirm,
3061 .release = tracing_pipe_buf_release,
3062 .steal = generic_pipe_buf_steal,
3063 .get = generic_pipe_buf_get,
3064 };
3065
3066 static size_t
3067 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3068 {
3069 size_t count;
3070 int ret;
3071
3072 /* Seq buffer is page-sized, exactly what we need. */
3073 for (;;) {
3074 count = iter->seq.len;
3075 ret = print_trace_line(iter);
3076 count = iter->seq.len - count;
3077 if (rem < count) {
3078 rem = 0;
3079 iter->seq.len -= count;
3080 break;
3081 }
3082 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3083 iter->seq.len -= count;
3084 break;
3085 }
3086
3087 trace_consume(iter);
3088 rem -= count;
3089 if (!find_next_entry_inc(iter)) {
3090 rem = 0;
3091 iter->ent = NULL;
3092 break;
3093 }
3094 }
3095
3096 return rem;
3097 }
3098
3099 static ssize_t tracing_splice_read_pipe(struct file *filp,
3100 loff_t *ppos,
3101 struct pipe_inode_info *pipe,
3102 size_t len,
3103 unsigned int flags)
3104 {
3105 struct page *pages[PIPE_BUFFERS];
3106 struct partial_page partial[PIPE_BUFFERS];
3107 struct trace_iterator *iter = filp->private_data;
3108 struct splice_pipe_desc spd = {
3109 .pages = pages,
3110 .partial = partial,
3111 .nr_pages = 0, /* This gets updated below. */
3112 .flags = flags,
3113 .ops = &tracing_pipe_buf_ops,
3114 .spd_release = tracing_spd_release_pipe,
3115 };
3116 static struct tracer *old_tracer;
3117 ssize_t ret;
3118 size_t rem;
3119 unsigned int i;
3120
3121 /* copy the tracer to avoid using a global lock all around */
3122 mutex_lock(&trace_types_lock);
3123 if (unlikely(old_tracer != current_trace && current_trace)) {
3124 old_tracer = current_trace;
3125 *iter->trace = *current_trace;
3126 }
3127 mutex_unlock(&trace_types_lock);
3128
3129 mutex_lock(&iter->mutex);
3130
3131 if (iter->trace->splice_read) {
3132 ret = iter->trace->splice_read(iter, filp,
3133 ppos, pipe, len, flags);
3134 if (ret)
3135 goto out_err;
3136 }
3137
3138 ret = tracing_wait_pipe(filp);
3139 if (ret <= 0)
3140 goto out_err;
3141
3142 if (!iter->ent && !find_next_entry_inc(iter)) {
3143 ret = -EFAULT;
3144 goto out_err;
3145 }
3146
3147 trace_event_read_lock();
3148
3149 /* Fill as many pages as possible. */
3150 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3151 pages[i] = alloc_page(GFP_KERNEL);
3152 if (!pages[i])
3153 break;
3154
3155 rem = tracing_fill_pipe_page(rem, iter);
3156
3157 /* Copy the data into the page, so we can start over. */
3158 ret = trace_seq_to_buffer(&iter->seq,
3159 page_address(pages[i]),
3160 iter->seq.len);
3161 if (ret < 0) {
3162 __free_page(pages[i]);
3163 break;
3164 }
3165 partial[i].offset = 0;
3166 partial[i].len = iter->seq.len;
3167
3168 trace_seq_init(&iter->seq);
3169 }
3170
3171 trace_event_read_unlock();
3172 mutex_unlock(&iter->mutex);
3173
3174 spd.nr_pages = i;
3175
3176 return splice_to_pipe(pipe, &spd);
3177
3178 out_err:
3179 mutex_unlock(&iter->mutex);
3180
3181 return ret;
3182 }
3183
3184 static ssize_t
3185 tracing_entries_read(struct file *filp, char __user *ubuf,
3186 size_t cnt, loff_t *ppos)
3187 {
3188 struct trace_array *tr = filp->private_data;
3189 char buf[96];
3190 int r;
3191
3192 mutex_lock(&trace_types_lock);
3193 if (!ring_buffer_expanded)
3194 r = sprintf(buf, "%lu (expanded: %lu)\n",
3195 tr->entries >> 10,
3196 trace_buf_size >> 10);
3197 else
3198 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3199 mutex_unlock(&trace_types_lock);
3200
3201 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3202 }
3203
3204 static ssize_t
3205 tracing_entries_write(struct file *filp, const char __user *ubuf,
3206 size_t cnt, loff_t *ppos)
3207 {
3208 unsigned long val;
3209 char buf[64];
3210 int ret, cpu;
3211
3212 if (cnt >= sizeof(buf))
3213 return -EINVAL;
3214
3215 if (copy_from_user(&buf, ubuf, cnt))
3216 return -EFAULT;
3217
3218 buf[cnt] = 0;
3219
3220 ret = strict_strtoul(buf, 10, &val);
3221 if (ret < 0)
3222 return ret;
3223
3224 /* must have at least 1 entry */
3225 if (!val)
3226 return -EINVAL;
3227
3228 mutex_lock(&trace_types_lock);
3229
3230 tracing_stop();
3231
3232 /* disable all cpu buffers */
3233 for_each_tracing_cpu(cpu) {
3234 if (global_trace.data[cpu])
3235 atomic_inc(&global_trace.data[cpu]->disabled);
3236 if (max_tr.data[cpu])
3237 atomic_inc(&max_tr.data[cpu]->disabled);
3238 }
3239
3240 /* value is in KB */
3241 val <<= 10;
3242
3243 if (val != global_trace.entries) {
3244 ret = tracing_resize_ring_buffer(val);
3245 if (ret < 0) {
3246 cnt = ret;
3247 goto out;
3248 }
3249 }
3250
3251 filp->f_pos += cnt;
3252
3253 /* If check pages failed, return ENOMEM */
3254 if (tracing_disabled)
3255 cnt = -ENOMEM;
3256 out:
3257 for_each_tracing_cpu(cpu) {
3258 if (global_trace.data[cpu])
3259 atomic_dec(&global_trace.data[cpu]->disabled);
3260 if (max_tr.data[cpu])
3261 atomic_dec(&max_tr.data[cpu]->disabled);
3262 }
3263
3264 tracing_start();
3265 max_tr.entries = global_trace.entries;
3266 mutex_unlock(&trace_types_lock);
3267
3268 return cnt;
3269 }
3270
3271 static int mark_printk(const char *fmt, ...)
3272 {
3273 int ret;
3274 va_list args;
3275 va_start(args, fmt);
3276 ret = trace_vprintk(0, fmt, args);
3277 va_end(args);
3278 return ret;
3279 }
3280
3281 static ssize_t
3282 tracing_mark_write(struct file *filp, const char __user *ubuf,
3283 size_t cnt, loff_t *fpos)
3284 {
3285 char *buf;
3286 char *end;
3287
3288 if (tracing_disabled)
3289 return -EINVAL;
3290
3291 if (cnt > TRACE_BUF_SIZE)
3292 cnt = TRACE_BUF_SIZE;
3293
3294 buf = kmalloc(cnt + 1, GFP_KERNEL);
3295 if (buf == NULL)
3296 return -ENOMEM;
3297
3298 if (copy_from_user(buf, ubuf, cnt)) {
3299 kfree(buf);
3300 return -EFAULT;
3301 }
3302
3303 /* Cut from the first nil or newline. */
3304 buf[cnt] = '\0';
3305 end = strchr(buf, '\n');
3306 if (end)
3307 *end = '\0';
3308
3309 cnt = mark_printk("%s\n", buf);
3310 kfree(buf);
3311 *fpos += cnt;
3312
3313 return cnt;
3314 }
3315
3316 static const struct file_operations tracing_max_lat_fops = {
3317 .open = tracing_open_generic,
3318 .read = tracing_max_lat_read,
3319 .write = tracing_max_lat_write,
3320 };
3321
3322 static const struct file_operations tracing_ctrl_fops = {
3323 .open = tracing_open_generic,
3324 .read = tracing_ctrl_read,
3325 .write = tracing_ctrl_write,
3326 };
3327
3328 static const struct file_operations set_tracer_fops = {
3329 .open = tracing_open_generic,
3330 .read = tracing_set_trace_read,
3331 .write = tracing_set_trace_write,
3332 };
3333
3334 static const struct file_operations tracing_pipe_fops = {
3335 .open = tracing_open_pipe,
3336 .poll = tracing_poll_pipe,
3337 .read = tracing_read_pipe,
3338 .splice_read = tracing_splice_read_pipe,
3339 .release = tracing_release_pipe,
3340 };
3341
3342 static const struct file_operations tracing_entries_fops = {
3343 .open = tracing_open_generic,
3344 .read = tracing_entries_read,
3345 .write = tracing_entries_write,
3346 };
3347
3348 static const struct file_operations tracing_mark_fops = {
3349 .open = tracing_open_generic,
3350 .write = tracing_mark_write,
3351 };
3352
3353 struct ftrace_buffer_info {
3354 struct trace_array *tr;
3355 void *spare;
3356 int cpu;
3357 unsigned int read;
3358 };
3359
3360 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3361 {
3362 int cpu = (int)(long)inode->i_private;
3363 struct ftrace_buffer_info *info;
3364
3365 if (tracing_disabled)
3366 return -ENODEV;
3367
3368 info = kzalloc(sizeof(*info), GFP_KERNEL);
3369 if (!info)
3370 return -ENOMEM;
3371
3372 info->tr = &global_trace;
3373 info->cpu = cpu;
3374 info->spare = NULL;
3375 /* Force reading ring buffer for first read */
3376 info->read = (unsigned int)-1;
3377
3378 filp->private_data = info;
3379
3380 return nonseekable_open(inode, filp);
3381 }
3382
3383 static ssize_t
3384 tracing_buffers_read(struct file *filp, char __user *ubuf,
3385 size_t count, loff_t *ppos)
3386 {
3387 struct ftrace_buffer_info *info = filp->private_data;
3388 unsigned int pos;
3389 ssize_t ret;
3390 size_t size;
3391
3392 if (!count)
3393 return 0;
3394
3395 if (!info->spare)
3396 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3397 if (!info->spare)
3398 return -ENOMEM;
3399
3400 /* Do we have previous read data to read? */
3401 if (info->read < PAGE_SIZE)
3402 goto read;
3403
3404 info->read = 0;
3405
3406 ret = ring_buffer_read_page(info->tr->buffer,
3407 &info->spare,
3408 count,
3409 info->cpu, 0);
3410 if (ret < 0)
3411 return 0;
3412
3413 pos = ring_buffer_page_len(info->spare);
3414
3415 if (pos < PAGE_SIZE)
3416 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3417
3418 read:
3419 size = PAGE_SIZE - info->read;
3420 if (size > count)
3421 size = count;
3422
3423 ret = copy_to_user(ubuf, info->spare + info->read, size);
3424 if (ret == size)
3425 return -EFAULT;
3426 size -= ret;
3427
3428 *ppos += size;
3429 info->read += size;
3430
3431 return size;
3432 }
3433
3434 static int tracing_buffers_release(struct inode *inode, struct file *file)
3435 {
3436 struct ftrace_buffer_info *info = file->private_data;
3437
3438 if (info->spare)
3439 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3440 kfree(info);
3441
3442 return 0;
3443 }
3444
3445 struct buffer_ref {
3446 struct ring_buffer *buffer;
3447 void *page;
3448 int ref;
3449 };
3450
3451 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3452 struct pipe_buffer *buf)
3453 {
3454 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3455
3456 if (--ref->ref)
3457 return;
3458
3459 ring_buffer_free_read_page(ref->buffer, ref->page);
3460 kfree(ref);
3461 buf->private = 0;
3462 }
3463
3464 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3465 struct pipe_buffer *buf)
3466 {
3467 return 1;
3468 }
3469
3470 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3471 struct pipe_buffer *buf)
3472 {
3473 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3474
3475 ref->ref++;
3476 }
3477
3478 /* Pipe buffer operations for a buffer. */
3479 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3480 .can_merge = 0,
3481 .map = generic_pipe_buf_map,
3482 .unmap = generic_pipe_buf_unmap,
3483 .confirm = generic_pipe_buf_confirm,
3484 .release = buffer_pipe_buf_release,
3485 .steal = buffer_pipe_buf_steal,
3486 .get = buffer_pipe_buf_get,
3487 };
3488
3489 /*
3490 * Callback from splice_to_pipe(), if we need to release some pages
3491 * at the end of the spd in case we error'ed out in filling the pipe.
3492 */
3493 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3494 {
3495 struct buffer_ref *ref =
3496 (struct buffer_ref *)spd->partial[i].private;
3497
3498 if (--ref->ref)
3499 return;
3500
3501 ring_buffer_free_read_page(ref->buffer, ref->page);
3502 kfree(ref);
3503 spd->partial[i].private = 0;
3504 }
3505
3506 static ssize_t
3507 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3508 struct pipe_inode_info *pipe, size_t len,
3509 unsigned int flags)
3510 {
3511 struct ftrace_buffer_info *info = file->private_data;
3512 struct partial_page partial[PIPE_BUFFERS];
3513 struct page *pages[PIPE_BUFFERS];
3514 struct splice_pipe_desc spd = {
3515 .pages = pages,
3516 .partial = partial,
3517 .flags = flags,
3518 .ops = &buffer_pipe_buf_ops,
3519 .spd_release = buffer_spd_release,
3520 };
3521 struct buffer_ref *ref;
3522 int entries, size, i;
3523 size_t ret;
3524
3525 if (*ppos & (PAGE_SIZE - 1)) {
3526 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3527 return -EINVAL;
3528 }
3529
3530 if (len & (PAGE_SIZE - 1)) {
3531 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3532 if (len < PAGE_SIZE)
3533 return -EINVAL;
3534 len &= PAGE_MASK;
3535 }
3536
3537 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3538
3539 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3540 struct page *page;
3541 int r;
3542
3543 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3544 if (!ref)
3545 break;
3546
3547 ref->ref = 1;
3548 ref->buffer = info->tr->buffer;
3549 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3550 if (!ref->page) {
3551 kfree(ref);
3552 break;
3553 }
3554
3555 r = ring_buffer_read_page(ref->buffer, &ref->page,
3556 len, info->cpu, 1);
3557 if (r < 0) {
3558 ring_buffer_free_read_page(ref->buffer,
3559 ref->page);
3560 kfree(ref);
3561 break;
3562 }
3563
3564 /*
3565 * zero out any left over data, this is going to
3566 * user land.
3567 */
3568 size = ring_buffer_page_len(ref->page);
3569 if (size < PAGE_SIZE)
3570 memset(ref->page + size, 0, PAGE_SIZE - size);
3571
3572 page = virt_to_page(ref->page);
3573
3574 spd.pages[i] = page;
3575 spd.partial[i].len = PAGE_SIZE;
3576 spd.partial[i].offset = 0;
3577 spd.partial[i].private = (unsigned long)ref;
3578 spd.nr_pages++;
3579 *ppos += PAGE_SIZE;
3580
3581 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3582 }
3583
3584 spd.nr_pages = i;
3585
3586 /* did we read anything? */
3587 if (!spd.nr_pages) {
3588 if (flags & SPLICE_F_NONBLOCK)
3589 ret = -EAGAIN;
3590 else
3591 ret = 0;
3592 /* TODO: block */
3593 return ret;
3594 }
3595
3596 ret = splice_to_pipe(pipe, &spd);
3597
3598 return ret;
3599 }
3600
3601 static const struct file_operations tracing_buffers_fops = {
3602 .open = tracing_buffers_open,
3603 .read = tracing_buffers_read,
3604 .release = tracing_buffers_release,
3605 .splice_read = tracing_buffers_splice_read,
3606 .llseek = no_llseek,
3607 };
3608
3609 static ssize_t
3610 tracing_stats_read(struct file *filp, char __user *ubuf,
3611 size_t count, loff_t *ppos)
3612 {
3613 unsigned long cpu = (unsigned long)filp->private_data;
3614 struct trace_array *tr = &global_trace;
3615 struct trace_seq *s;
3616 unsigned long cnt;
3617
3618 s = kmalloc(sizeof(*s), GFP_KERNEL);
3619 if (!s)
3620 return ENOMEM;
3621
3622 trace_seq_init(s);
3623
3624 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3625 trace_seq_printf(s, "entries: %ld\n", cnt);
3626
3627 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3628 trace_seq_printf(s, "overrun: %ld\n", cnt);
3629
3630 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3631 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3632
3633 cnt = ring_buffer_nmi_dropped_cpu(tr->buffer, cpu);
3634 trace_seq_printf(s, "nmi dropped: %ld\n", cnt);
3635
3636 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3637
3638 kfree(s);
3639
3640 return count;
3641 }
3642
3643 static const struct file_operations tracing_stats_fops = {
3644 .open = tracing_open_generic,
3645 .read = tracing_stats_read,
3646 };
3647
3648 #ifdef CONFIG_DYNAMIC_FTRACE
3649
3650 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3651 {
3652 return 0;
3653 }
3654
3655 static ssize_t
3656 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3657 size_t cnt, loff_t *ppos)
3658 {
3659 static char ftrace_dyn_info_buffer[1024];
3660 static DEFINE_MUTEX(dyn_info_mutex);
3661 unsigned long *p = filp->private_data;
3662 char *buf = ftrace_dyn_info_buffer;
3663 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3664 int r;
3665
3666 mutex_lock(&dyn_info_mutex);
3667 r = sprintf(buf, "%ld ", *p);
3668
3669 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3670 buf[r++] = '\n';
3671
3672 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3673
3674 mutex_unlock(&dyn_info_mutex);
3675
3676 return r;
3677 }
3678
3679 static const struct file_operations tracing_dyn_info_fops = {
3680 .open = tracing_open_generic,
3681 .read = tracing_read_dyn_info,
3682 };
3683 #endif
3684
3685 static struct dentry *d_tracer;
3686
3687 struct dentry *tracing_init_dentry(void)
3688 {
3689 static int once;
3690
3691 if (d_tracer)
3692 return d_tracer;
3693
3694 if (!debugfs_initialized())
3695 return NULL;
3696
3697 d_tracer = debugfs_create_dir("tracing", NULL);
3698
3699 if (!d_tracer && !once) {
3700 once = 1;
3701 pr_warning("Could not create debugfs directory 'tracing'\n");
3702 return NULL;
3703 }
3704
3705 return d_tracer;
3706 }
3707
3708 static struct dentry *d_percpu;
3709
3710 struct dentry *tracing_dentry_percpu(void)
3711 {
3712 static int once;
3713 struct dentry *d_tracer;
3714
3715 if (d_percpu)
3716 return d_percpu;
3717
3718 d_tracer = tracing_init_dentry();
3719
3720 if (!d_tracer)
3721 return NULL;
3722
3723 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3724
3725 if (!d_percpu && !once) {
3726 once = 1;
3727 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3728 return NULL;
3729 }
3730
3731 return d_percpu;
3732 }
3733
3734 static void tracing_init_debugfs_percpu(long cpu)
3735 {
3736 struct dentry *d_percpu = tracing_dentry_percpu();
3737 struct dentry *d_cpu;
3738 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3739 char cpu_dir[7];
3740
3741 if (cpu > 999 || cpu < 0)
3742 return;
3743
3744 sprintf(cpu_dir, "cpu%ld", cpu);
3745 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3746 if (!d_cpu) {
3747 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3748 return;
3749 }
3750
3751 /* per cpu trace_pipe */
3752 trace_create_file("trace_pipe", 0444, d_cpu,
3753 (void *) cpu, &tracing_pipe_fops);
3754
3755 /* per cpu trace */
3756 trace_create_file("trace", 0644, d_cpu,
3757 (void *) cpu, &tracing_fops);
3758
3759 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3760 (void *) cpu, &tracing_buffers_fops);
3761
3762 trace_create_file("stats", 0444, d_cpu,
3763 (void *) cpu, &tracing_stats_fops);
3764 }
3765
3766 #ifdef CONFIG_FTRACE_SELFTEST
3767 /* Let selftest have access to static functions in this file */
3768 #include "trace_selftest.c"
3769 #endif
3770
3771 struct trace_option_dentry {
3772 struct tracer_opt *opt;
3773 struct tracer_flags *flags;
3774 struct dentry *entry;
3775 };
3776
3777 static ssize_t
3778 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3779 loff_t *ppos)
3780 {
3781 struct trace_option_dentry *topt = filp->private_data;
3782 char *buf;
3783
3784 if (topt->flags->val & topt->opt->bit)
3785 buf = "1\n";
3786 else
3787 buf = "0\n";
3788
3789 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3790 }
3791
3792 static ssize_t
3793 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3794 loff_t *ppos)
3795 {
3796 struct trace_option_dentry *topt = filp->private_data;
3797 unsigned long val;
3798 char buf[64];
3799 int ret;
3800
3801 if (cnt >= sizeof(buf))
3802 return -EINVAL;
3803
3804 if (copy_from_user(&buf, ubuf, cnt))
3805 return -EFAULT;
3806
3807 buf[cnt] = 0;
3808
3809 ret = strict_strtoul(buf, 10, &val);
3810 if (ret < 0)
3811 return ret;
3812
3813 ret = 0;
3814 switch (val) {
3815 case 0:
3816 /* do nothing if already cleared */
3817 if (!(topt->flags->val & topt->opt->bit))
3818 break;
3819
3820 mutex_lock(&trace_types_lock);
3821 if (current_trace->set_flag)
3822 ret = current_trace->set_flag(topt->flags->val,
3823 topt->opt->bit, 0);
3824 mutex_unlock(&trace_types_lock);
3825 if (ret)
3826 return ret;
3827 topt->flags->val &= ~topt->opt->bit;
3828 break;
3829 case 1:
3830 /* do nothing if already set */
3831 if (topt->flags->val & topt->opt->bit)
3832 break;
3833
3834 mutex_lock(&trace_types_lock);
3835 if (current_trace->set_flag)
3836 ret = current_trace->set_flag(topt->flags->val,
3837 topt->opt->bit, 1);
3838 mutex_unlock(&trace_types_lock);
3839 if (ret)
3840 return ret;
3841 topt->flags->val |= topt->opt->bit;
3842 break;
3843
3844 default:
3845 return -EINVAL;
3846 }
3847
3848 *ppos += cnt;
3849
3850 return cnt;
3851 }
3852
3853
3854 static const struct file_operations trace_options_fops = {
3855 .open = tracing_open_generic,
3856 .read = trace_options_read,
3857 .write = trace_options_write,
3858 };
3859
3860 static ssize_t
3861 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3862 loff_t *ppos)
3863 {
3864 long index = (long)filp->private_data;
3865 char *buf;
3866
3867 if (trace_flags & (1 << index))
3868 buf = "1\n";
3869 else
3870 buf = "0\n";
3871
3872 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3873 }
3874
3875 static ssize_t
3876 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3877 loff_t *ppos)
3878 {
3879 long index = (long)filp->private_data;
3880 char buf[64];
3881 unsigned long val;
3882 int ret;
3883
3884 if (cnt >= sizeof(buf))
3885 return -EINVAL;
3886
3887 if (copy_from_user(&buf, ubuf, cnt))
3888 return -EFAULT;
3889
3890 buf[cnt] = 0;
3891
3892 ret = strict_strtoul(buf, 10, &val);
3893 if (ret < 0)
3894 return ret;
3895
3896 switch (val) {
3897 case 0:
3898 trace_flags &= ~(1 << index);
3899 break;
3900 case 1:
3901 trace_flags |= 1 << index;
3902 break;
3903
3904 default:
3905 return -EINVAL;
3906 }
3907
3908 *ppos += cnt;
3909
3910 return cnt;
3911 }
3912
3913 static const struct file_operations trace_options_core_fops = {
3914 .open = tracing_open_generic,
3915 .read = trace_options_core_read,
3916 .write = trace_options_core_write,
3917 };
3918
3919 struct dentry *trace_create_file(const char *name,
3920 mode_t mode,
3921 struct dentry *parent,
3922 void *data,
3923 const struct file_operations *fops)
3924 {
3925 struct dentry *ret;
3926
3927 ret = debugfs_create_file(name, mode, parent, data, fops);
3928 if (!ret)
3929 pr_warning("Could not create debugfs '%s' entry\n", name);
3930
3931 return ret;
3932 }
3933
3934
3935 static struct dentry *trace_options_init_dentry(void)
3936 {
3937 struct dentry *d_tracer;
3938 static struct dentry *t_options;
3939
3940 if (t_options)
3941 return t_options;
3942
3943 d_tracer = tracing_init_dentry();
3944 if (!d_tracer)
3945 return NULL;
3946
3947 t_options = debugfs_create_dir("options", d_tracer);
3948 if (!t_options) {
3949 pr_warning("Could not create debugfs directory 'options'\n");
3950 return NULL;
3951 }
3952
3953 return t_options;
3954 }
3955
3956 static void
3957 create_trace_option_file(struct trace_option_dentry *topt,
3958 struct tracer_flags *flags,
3959 struct tracer_opt *opt)
3960 {
3961 struct dentry *t_options;
3962
3963 t_options = trace_options_init_dentry();
3964 if (!t_options)
3965 return;
3966
3967 topt->flags = flags;
3968 topt->opt = opt;
3969
3970 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
3971 &trace_options_fops);
3972
3973 }
3974
3975 static struct trace_option_dentry *
3976 create_trace_option_files(struct tracer *tracer)
3977 {
3978 struct trace_option_dentry *topts;
3979 struct tracer_flags *flags;
3980 struct tracer_opt *opts;
3981 int cnt;
3982
3983 if (!tracer)
3984 return NULL;
3985
3986 flags = tracer->flags;
3987
3988 if (!flags || !flags->opts)
3989 return NULL;
3990
3991 opts = flags->opts;
3992
3993 for (cnt = 0; opts[cnt].name; cnt++)
3994 ;
3995
3996 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3997 if (!topts)
3998 return NULL;
3999
4000 for (cnt = 0; opts[cnt].name; cnt++)
4001 create_trace_option_file(&topts[cnt], flags,
4002 &opts[cnt]);
4003
4004 return topts;
4005 }
4006
4007 static void
4008 destroy_trace_option_files(struct trace_option_dentry *topts)
4009 {
4010 int cnt;
4011
4012 if (!topts)
4013 return;
4014
4015 for (cnt = 0; topts[cnt].opt; cnt++) {
4016 if (topts[cnt].entry)
4017 debugfs_remove(topts[cnt].entry);
4018 }
4019
4020 kfree(topts);
4021 }
4022
4023 static struct dentry *
4024 create_trace_option_core_file(const char *option, long index)
4025 {
4026 struct dentry *t_options;
4027
4028 t_options = trace_options_init_dentry();
4029 if (!t_options)
4030 return NULL;
4031
4032 return trace_create_file(option, 0644, t_options, (void *)index,
4033 &trace_options_core_fops);
4034 }
4035
4036 static __init void create_trace_options_dir(void)
4037 {
4038 struct dentry *t_options;
4039 int i;
4040
4041 t_options = trace_options_init_dentry();
4042 if (!t_options)
4043 return;
4044
4045 for (i = 0; trace_options[i]; i++)
4046 create_trace_option_core_file(trace_options[i], i);
4047 }
4048
4049 static __init int tracer_init_debugfs(void)
4050 {
4051 struct dentry *d_tracer;
4052 int cpu;
4053
4054 d_tracer = tracing_init_dentry();
4055
4056 trace_create_file("tracing_enabled", 0644, d_tracer,
4057 &global_trace, &tracing_ctrl_fops);
4058
4059 trace_create_file("trace_options", 0644, d_tracer,
4060 NULL, &tracing_iter_fops);
4061
4062 trace_create_file("tracing_cpumask", 0644, d_tracer,
4063 NULL, &tracing_cpumask_fops);
4064
4065 trace_create_file("trace", 0644, d_tracer,
4066 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4067
4068 trace_create_file("available_tracers", 0444, d_tracer,
4069 &global_trace, &show_traces_fops);
4070
4071 trace_create_file("current_tracer", 0644, d_tracer,
4072 &global_trace, &set_tracer_fops);
4073
4074 trace_create_file("tracing_max_latency", 0644, d_tracer,
4075 &tracing_max_latency, &tracing_max_lat_fops);
4076
4077 trace_create_file("tracing_thresh", 0644, d_tracer,
4078 &tracing_thresh, &tracing_max_lat_fops);
4079
4080 trace_create_file("README", 0444, d_tracer,
4081 NULL, &tracing_readme_fops);
4082
4083 trace_create_file("trace_pipe", 0444, d_tracer,
4084 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4085
4086 trace_create_file("buffer_size_kb", 0644, d_tracer,
4087 &global_trace, &tracing_entries_fops);
4088
4089 trace_create_file("trace_marker", 0220, d_tracer,
4090 NULL, &tracing_mark_fops);
4091
4092 trace_create_file("saved_cmdlines", 0444, d_tracer,
4093 NULL, &tracing_saved_cmdlines_fops);
4094
4095 #ifdef CONFIG_DYNAMIC_FTRACE
4096 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4097 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4098 #endif
4099 #ifdef CONFIG_SYSPROF_TRACER
4100 init_tracer_sysprof_debugfs(d_tracer);
4101 #endif
4102
4103 create_trace_options_dir();
4104
4105 for_each_tracing_cpu(cpu)
4106 tracing_init_debugfs_percpu(cpu);
4107
4108 return 0;
4109 }
4110
4111 static int trace_panic_handler(struct notifier_block *this,
4112 unsigned long event, void *unused)
4113 {
4114 if (ftrace_dump_on_oops)
4115 ftrace_dump();
4116 return NOTIFY_OK;
4117 }
4118
4119 static struct notifier_block trace_panic_notifier = {
4120 .notifier_call = trace_panic_handler,
4121 .next = NULL,
4122 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4123 };
4124
4125 static int trace_die_handler(struct notifier_block *self,
4126 unsigned long val,
4127 void *data)
4128 {
4129 switch (val) {
4130 case DIE_OOPS:
4131 if (ftrace_dump_on_oops)
4132 ftrace_dump();
4133 break;
4134 default:
4135 break;
4136 }
4137 return NOTIFY_OK;
4138 }
4139
4140 static struct notifier_block trace_die_notifier = {
4141 .notifier_call = trace_die_handler,
4142 .priority = 200
4143 };
4144
4145 /*
4146 * printk is set to max of 1024, we really don't need it that big.
4147 * Nothing should be printing 1000 characters anyway.
4148 */
4149 #define TRACE_MAX_PRINT 1000
4150
4151 /*
4152 * Define here KERN_TRACE so that we have one place to modify
4153 * it if we decide to change what log level the ftrace dump
4154 * should be at.
4155 */
4156 #define KERN_TRACE KERN_EMERG
4157
4158 static void
4159 trace_printk_seq(struct trace_seq *s)
4160 {
4161 /* Probably should print a warning here. */
4162 if (s->len >= 1000)
4163 s->len = 1000;
4164
4165 /* should be zero ended, but we are paranoid. */
4166 s->buffer[s->len] = 0;
4167
4168 printk(KERN_TRACE "%s", s->buffer);
4169
4170 trace_seq_init(s);
4171 }
4172
4173 static void __ftrace_dump(bool disable_tracing)
4174 {
4175 static raw_spinlock_t ftrace_dump_lock =
4176 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
4177 /* use static because iter can be a bit big for the stack */
4178 static struct trace_iterator iter;
4179 unsigned int old_userobj;
4180 static int dump_ran;
4181 unsigned long flags;
4182 int cnt = 0, cpu;
4183
4184 /* only one dump */
4185 local_irq_save(flags);
4186 __raw_spin_lock(&ftrace_dump_lock);
4187 if (dump_ran)
4188 goto out;
4189
4190 dump_ran = 1;
4191
4192 tracing_off();
4193
4194 if (disable_tracing)
4195 ftrace_kill();
4196
4197 for_each_tracing_cpu(cpu) {
4198 atomic_inc(&global_trace.data[cpu]->disabled);
4199 }
4200
4201 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4202
4203 /* don't look at user memory in panic mode */
4204 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4205
4206 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4207
4208 /* Simulate the iterator */
4209 iter.tr = &global_trace;
4210 iter.trace = current_trace;
4211 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4212
4213 /*
4214 * We need to stop all tracing on all CPUS to read the
4215 * the next buffer. This is a bit expensive, but is
4216 * not done often. We fill all what we can read,
4217 * and then release the locks again.
4218 */
4219
4220 while (!trace_empty(&iter)) {
4221
4222 if (!cnt)
4223 printk(KERN_TRACE "---------------------------------\n");
4224
4225 cnt++;
4226
4227 /* reset all but tr, trace, and overruns */
4228 memset(&iter.seq, 0,
4229 sizeof(struct trace_iterator) -
4230 offsetof(struct trace_iterator, seq));
4231 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4232 iter.pos = -1;
4233
4234 if (find_next_entry_inc(&iter) != NULL) {
4235 print_trace_line(&iter);
4236 trace_consume(&iter);
4237 }
4238
4239 trace_printk_seq(&iter.seq);
4240 }
4241
4242 if (!cnt)
4243 printk(KERN_TRACE " (ftrace buffer empty)\n");
4244 else
4245 printk(KERN_TRACE "---------------------------------\n");
4246
4247 /* Re-enable tracing if requested */
4248 if (!disable_tracing) {
4249 trace_flags |= old_userobj;
4250
4251 for_each_tracing_cpu(cpu) {
4252 atomic_dec(&global_trace.data[cpu]->disabled);
4253 }
4254 tracing_on();
4255 }
4256
4257 out:
4258 __raw_spin_unlock(&ftrace_dump_lock);
4259 local_irq_restore(flags);
4260 }
4261
4262 /* By default: disable tracing after the dump */
4263 void ftrace_dump(void)
4264 {
4265 __ftrace_dump(true);
4266 }
4267
4268 __init static int tracer_alloc_buffers(void)
4269 {
4270 struct trace_array_cpu *data;
4271 int ring_buf_size;
4272 int i;
4273 int ret = -ENOMEM;
4274
4275 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4276 goto out;
4277
4278 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4279 goto out_free_buffer_mask;
4280
4281 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4282 goto out_free_tracing_cpumask;
4283
4284 /* To save memory, keep the ring buffer size to its minimum */
4285 if (ring_buffer_expanded)
4286 ring_buf_size = trace_buf_size;
4287 else
4288 ring_buf_size = 1;
4289
4290 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4291 cpumask_copy(tracing_cpumask, cpu_all_mask);
4292 cpumask_clear(tracing_reader_cpumask);
4293
4294 /* TODO: make the number of buffers hot pluggable with CPUS */
4295 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4296 TRACE_BUFFER_FLAGS);
4297 if (!global_trace.buffer) {
4298 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4299 WARN_ON(1);
4300 goto out_free_cpumask;
4301 }
4302 global_trace.entries = ring_buffer_size(global_trace.buffer);
4303
4304
4305 #ifdef CONFIG_TRACER_MAX_TRACE
4306 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4307 TRACE_BUFFER_FLAGS);
4308 if (!max_tr.buffer) {
4309 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4310 WARN_ON(1);
4311 ring_buffer_free(global_trace.buffer);
4312 goto out_free_cpumask;
4313 }
4314 max_tr.entries = ring_buffer_size(max_tr.buffer);
4315 WARN_ON(max_tr.entries != global_trace.entries);
4316 #endif
4317
4318 /* Allocate the first page for all buffers */
4319 for_each_tracing_cpu(i) {
4320 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4321 max_tr.data[i] = &per_cpu(max_data, i);
4322 }
4323
4324 trace_init_cmdlines();
4325
4326 register_tracer(&nop_trace);
4327 current_trace = &nop_trace;
4328 #ifdef CONFIG_BOOT_TRACER
4329 register_tracer(&boot_tracer);
4330 #endif
4331 /* All seems OK, enable tracing */
4332 tracing_disabled = 0;
4333
4334 atomic_notifier_chain_register(&panic_notifier_list,
4335 &trace_panic_notifier);
4336
4337 register_die_notifier(&trace_die_notifier);
4338
4339 return 0;
4340
4341 out_free_cpumask:
4342 free_cpumask_var(tracing_reader_cpumask);
4343 out_free_tracing_cpumask:
4344 free_cpumask_var(tracing_cpumask);
4345 out_free_buffer_mask:
4346 free_cpumask_var(tracing_buffer_mask);
4347 out:
4348 return ret;
4349 }
4350
4351 __init static int clear_boot_tracer(void)
4352 {
4353 /*
4354 * The default tracer at boot buffer is an init section.
4355 * This function is called in lateinit. If we did not
4356 * find the boot tracer, then clear it out, to prevent
4357 * later registration from accessing the buffer that is
4358 * about to be freed.
4359 */
4360 if (!default_bootup_tracer)
4361 return 0;
4362
4363 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4364 default_bootup_tracer);
4365 default_bootup_tracer = NULL;
4366
4367 return 0;
4368 }
4369
4370 early_initcall(tracer_alloc_buffers);
4371 fs_initcall(tracer_init_debugfs);
4372 late_initcall(clear_boot_tracer);
This page took 0.15102 seconds and 6 git commands to generate.