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