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