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