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