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