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