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