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