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