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