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