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