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