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