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