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