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