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