trace: code style clean up
[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;
dcb6308f 786 unsigned long irq_flags;
bc0c38d1 787
d769041f
SR
788 /* If we are reading the ring buffer, don't trace */
789 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
790 return;
791
3928a8a2
SR
792 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
793 &irq_flags);
794 if (!event)
795 return;
796 entry = ring_buffer_event_data(event);
38697053 797 tracing_generic_entry_update(&entry->ent, flags, pc);
777e208d
SR
798 entry->ent.type = TRACE_FN;
799 entry->ip = ip;
800 entry->parent_ip = parent_ip;
3928a8a2 801 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
bc0c38d1
SR
802}
803
fb52607a 804#ifdef CONFIG_FUNCTION_GRAPH_TRACER
287b6e68 805static void __trace_graph_entry(struct trace_array *tr,
287b6e68
FW
806 struct ftrace_graph_ent *trace,
807 unsigned long flags,
808 int pc)
809{
810 struct ring_buffer_event *event;
811 struct ftrace_graph_ent_entry *entry;
812 unsigned long irq_flags;
813
814 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
815 return;
816
817 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
818 &irq_flags);
819 if (!event)
820 return;
821 entry = ring_buffer_event_data(event);
822 tracing_generic_entry_update(&entry->ent, flags, pc);
823 entry->ent.type = TRACE_GRAPH_ENT;
824 entry->graph_ent = *trace;
825 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
826}
827
828static void __trace_graph_return(struct trace_array *tr,
fb52607a 829 struct ftrace_graph_ret *trace,
15e6cb36
FW
830 unsigned long flags,
831 int pc)
832{
833 struct ring_buffer_event *event;
287b6e68 834 struct ftrace_graph_ret_entry *entry;
15e6cb36
FW
835 unsigned long irq_flags;
836
837 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
838 return;
839
840 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
841 &irq_flags);
842 if (!event)
843 return;
844 entry = ring_buffer_event_data(event);
845 tracing_generic_entry_update(&entry->ent, flags, pc);
287b6e68
FW
846 entry->ent.type = TRACE_GRAPH_RET;
847 entry->ret = *trace;
15e6cb36
FW
848 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
849}
850#endif
851
e309b41d 852void
2e0f5761 853ftrace(struct trace_array *tr, struct trace_array_cpu *data,
38697053
SR
854 unsigned long ip, unsigned long parent_ip, unsigned long flags,
855 int pc)
2e0f5761
IM
856{
857 if (likely(!atomic_read(&data->disabled)))
7be42151 858 trace_function(tr, ip, parent_ip, flags, pc);
2e0f5761
IM
859}
860
53614991 861static void __ftrace_trace_stack(struct trace_array *tr,
53614991
SR
862 unsigned long flags,
863 int skip, int pc)
86387f7e 864{
c2c80529 865#ifdef CONFIG_STACKTRACE
3928a8a2 866 struct ring_buffer_event *event;
777e208d 867 struct stack_entry *entry;
86387f7e 868 struct stack_trace trace;
3928a8a2 869 unsigned long irq_flags;
86387f7e 870
3928a8a2
SR
871 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
872 &irq_flags);
873 if (!event)
874 return;
875 entry = ring_buffer_event_data(event);
38697053 876 tracing_generic_entry_update(&entry->ent, flags, pc);
777e208d 877 entry->ent.type = TRACE_STACK;
86387f7e 878
777e208d 879 memset(&entry->caller, 0, sizeof(entry->caller));
86387f7e
IM
880
881 trace.nr_entries = 0;
882 trace.max_entries = FTRACE_STACK_ENTRIES;
883 trace.skip = skip;
777e208d 884 trace.entries = entry->caller;
86387f7e
IM
885
886 save_stack_trace(&trace);
3928a8a2 887 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
c2c80529 888#endif
f0a920d5
IM
889}
890
53614991 891static void ftrace_trace_stack(struct trace_array *tr,
53614991
SR
892 unsigned long flags,
893 int skip, int pc)
894{
895 if (!(trace_flags & TRACE_ITER_STACKTRACE))
896 return;
897
7be42151 898 __ftrace_trace_stack(tr, flags, skip, pc);
53614991
SR
899}
900
38697053 901void __trace_stack(struct trace_array *tr,
38697053 902 unsigned long flags,
53614991 903 int skip, int pc)
38697053 904{
7be42151 905 __ftrace_trace_stack(tr, flags, skip, pc);
38697053
SR
906}
907
02b67518 908static void ftrace_trace_userstack(struct trace_array *tr,
7be42151 909 unsigned long flags, int pc)
02b67518 910{
c7425acb 911#ifdef CONFIG_STACKTRACE
8d7c6a96 912 struct ring_buffer_event *event;
02b67518
TE
913 struct userstack_entry *entry;
914 struct stack_trace trace;
02b67518
TE
915 unsigned long irq_flags;
916
917 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
918 return;
919
920 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
921 &irq_flags);
922 if (!event)
923 return;
924 entry = ring_buffer_event_data(event);
925 tracing_generic_entry_update(&entry->ent, flags, pc);
926 entry->ent.type = TRACE_USER_STACK;
927
928 memset(&entry->caller, 0, sizeof(entry->caller));
929
930 trace.nr_entries = 0;
931 trace.max_entries = FTRACE_STACK_ENTRIES;
932 trace.skip = 0;
933 trace.entries = entry->caller;
934
935 save_stack_trace_user(&trace);
936 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
c7425acb 937#endif
02b67518
TE
938}
939
7be42151 940void __trace_userstack(struct trace_array *tr, unsigned long flags)
02b67518 941{
7be42151 942 ftrace_trace_userstack(tr, flags, preempt_count());
02b67518
TE
943}
944
38697053 945static void
7be42151 946ftrace_trace_special(void *__tr,
38697053
SR
947 unsigned long arg1, unsigned long arg2, unsigned long arg3,
948 int pc)
a4feb834 949{
3928a8a2 950 struct ring_buffer_event *event;
a4feb834 951 struct trace_array *tr = __tr;
777e208d 952 struct special_entry *entry;
a4feb834
IM
953 unsigned long irq_flags;
954
3928a8a2
SR
955 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
956 &irq_flags);
957 if (!event)
958 return;
959 entry = ring_buffer_event_data(event);
38697053 960 tracing_generic_entry_update(&entry->ent, 0, pc);
777e208d
SR
961 entry->ent.type = TRACE_SPECIAL;
962 entry->arg1 = arg1;
963 entry->arg2 = arg2;
964 entry->arg3 = arg3;
3928a8a2 965 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
7be42151
ACM
966 ftrace_trace_stack(tr, irq_flags, 4, pc);
967 ftrace_trace_userstack(tr, irq_flags, pc);
a4feb834
IM
968
969 trace_wake_up();
970}
971
38697053
SR
972void
973__trace_special(void *__tr, void *__data,
974 unsigned long arg1, unsigned long arg2, unsigned long arg3)
975{
7be42151 976 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
38697053
SR
977}
978
e309b41d 979void
bc0c38d1 980tracing_sched_switch_trace(struct trace_array *tr,
86387f7e
IM
981 struct task_struct *prev,
982 struct task_struct *next,
38697053 983 unsigned long flags, int pc)
bc0c38d1 984{
3928a8a2 985 struct ring_buffer_event *event;
777e208d 986 struct ctx_switch_entry *entry;
dcb6308f 987 unsigned long irq_flags;
bc0c38d1 988
3928a8a2
SR
989 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
990 &irq_flags);
991 if (!event)
992 return;
993 entry = ring_buffer_event_data(event);
38697053 994 tracing_generic_entry_update(&entry->ent, flags, pc);
777e208d
SR
995 entry->ent.type = TRACE_CTX;
996 entry->prev_pid = prev->pid;
997 entry->prev_prio = prev->prio;
998 entry->prev_state = prev->state;
999 entry->next_pid = next->pid;
1000 entry->next_prio = next->prio;
1001 entry->next_state = next->state;
1002 entry->next_cpu = task_cpu(next);
3928a8a2 1003 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
7be42151
ACM
1004 ftrace_trace_stack(tr, flags, 5, pc);
1005 ftrace_trace_userstack(tr, flags, pc);
bc0c38d1
SR
1006}
1007
57422797
IM
1008void
1009tracing_sched_wakeup_trace(struct trace_array *tr,
86387f7e
IM
1010 struct task_struct *wakee,
1011 struct task_struct *curr,
38697053 1012 unsigned long flags, int pc)
57422797 1013{
3928a8a2 1014 struct ring_buffer_event *event;
777e208d 1015 struct ctx_switch_entry *entry;
57422797
IM
1016 unsigned long irq_flags;
1017
3928a8a2
SR
1018 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1019 &irq_flags);
1020 if (!event)
1021 return;
1022 entry = ring_buffer_event_data(event);
38697053 1023 tracing_generic_entry_update(&entry->ent, flags, pc);
777e208d
SR
1024 entry->ent.type = TRACE_WAKE;
1025 entry->prev_pid = curr->pid;
1026 entry->prev_prio = curr->prio;
1027 entry->prev_state = curr->state;
1028 entry->next_pid = wakee->pid;
1029 entry->next_prio = wakee->prio;
1030 entry->next_state = wakee->state;
1031 entry->next_cpu = task_cpu(wakee);
3928a8a2 1032 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
7be42151
ACM
1033 ftrace_trace_stack(tr, flags, 6, pc);
1034 ftrace_trace_userstack(tr, flags, pc);
017730c1
IM
1035
1036 trace_wake_up();
57422797
IM
1037}
1038
4902f884
SR
1039void
1040ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1041{
1042 struct trace_array *tr = &global_trace;
1043 struct trace_array_cpu *data;
5aa1ba6a 1044 unsigned long flags;
4902f884 1045 int cpu;
38697053 1046 int pc;
4902f884 1047
c76f0694 1048 if (tracing_disabled)
4902f884
SR
1049 return;
1050
38697053 1051 pc = preempt_count();
5aa1ba6a 1052 local_irq_save(flags);
4902f884
SR
1053 cpu = raw_smp_processor_id();
1054 data = tr->data[cpu];
4902f884 1055
5aa1ba6a 1056 if (likely(atomic_inc_return(&data->disabled) == 1))
7be42151 1057 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
4902f884 1058
5aa1ba6a
SR
1059 atomic_dec(&data->disabled);
1060 local_irq_restore(flags);
4902f884
SR
1061}
1062
fb52607a 1063#ifdef CONFIG_FUNCTION_GRAPH_TRACER
e49dc19c 1064int trace_graph_entry(struct ftrace_graph_ent *trace)
15e6cb36
FW
1065{
1066 struct trace_array *tr = &global_trace;
1067 struct trace_array_cpu *data;
1068 unsigned long flags;
1069 long disabled;
1070 int cpu;
1071 int pc;
1072
804a6851
SR
1073 if (!ftrace_trace_task(current))
1074 return 0;
1075
ea4e2bc4
SR
1076 if (!ftrace_graph_addr(trace->func))
1077 return 0;
1078
a5e25883 1079 local_irq_save(flags);
15e6cb36
FW
1080 cpu = raw_smp_processor_id();
1081 data = tr->data[cpu];
1082 disabled = atomic_inc_return(&data->disabled);
1083 if (likely(disabled == 1)) {
1084 pc = preempt_count();
7be42151 1085 __trace_graph_entry(tr, trace, flags, pc);
287b6e68 1086 }
ea4e2bc4
SR
1087 /* Only do the atomic if it is not already set */
1088 if (!test_tsk_trace_graph(current))
1089 set_tsk_trace_graph(current);
287b6e68 1090 atomic_dec(&data->disabled);
a5e25883 1091 local_irq_restore(flags);
e49dc19c
SR
1092
1093 return 1;
287b6e68
FW
1094}
1095
1096void trace_graph_return(struct ftrace_graph_ret *trace)
1097{
1098 struct trace_array *tr = &global_trace;
1099 struct trace_array_cpu *data;
1100 unsigned long flags;
1101 long disabled;
1102 int cpu;
1103 int pc;
1104
a5e25883 1105 local_irq_save(flags);
287b6e68
FW
1106 cpu = raw_smp_processor_id();
1107 data = tr->data[cpu];
1108 disabled = atomic_inc_return(&data->disabled);
1109 if (likely(disabled == 1)) {
1110 pc = preempt_count();
7be42151 1111 __trace_graph_return(tr, trace, flags, pc);
15e6cb36 1112 }
ea4e2bc4
SR
1113 if (!trace->depth)
1114 clear_tsk_trace_graph(current);
15e6cb36 1115 atomic_dec(&data->disabled);
a5e25883 1116 local_irq_restore(flags);
15e6cb36 1117}
fb52607a 1118#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
15e6cb36 1119
bc0c38d1
SR
1120enum trace_file_type {
1121 TRACE_FILE_LAT_FMT = 1,
12ef7d44 1122 TRACE_FILE_ANNOTATE = 2,
bc0c38d1
SR
1123};
1124
e2ac8ef5 1125static void trace_iterator_increment(struct trace_iterator *iter)
5a90f577 1126{
d769041f
SR
1127 /* Don't allow ftrace to trace into the ring buffers */
1128 ftrace_disable_cpu();
1129
5a90f577 1130 iter->idx++;
d769041f
SR
1131 if (iter->buffer_iter[iter->cpu])
1132 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1133
1134 ftrace_enable_cpu();
5a90f577
SR
1135}
1136
e309b41d 1137static struct trace_entry *
3928a8a2 1138peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
dd0e545f 1139{
3928a8a2
SR
1140 struct ring_buffer_event *event;
1141 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
dd0e545f 1142
d769041f
SR
1143 /* Don't allow ftrace to trace into the ring buffers */
1144 ftrace_disable_cpu();
1145
1146 if (buf_iter)
1147 event = ring_buffer_iter_peek(buf_iter, ts);
1148 else
1149 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1150
1151 ftrace_enable_cpu();
1152
3928a8a2 1153 return event ? ring_buffer_event_data(event) : NULL;
dd0e545f 1154}
d769041f 1155
dd0e545f 1156static struct trace_entry *
3928a8a2 1157__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
bc0c38d1 1158{
3928a8a2 1159 struct ring_buffer *buffer = iter->tr->buffer;
bc0c38d1 1160 struct trace_entry *ent, *next = NULL;
3928a8a2 1161 u64 next_ts = 0, ts;
bc0c38d1
SR
1162 int next_cpu = -1;
1163 int cpu;
1164
ab46428c 1165 for_each_tracing_cpu(cpu) {
dd0e545f 1166
3928a8a2
SR
1167 if (ring_buffer_empty_cpu(buffer, cpu))
1168 continue;
dd0e545f 1169
3928a8a2 1170 ent = peek_next_entry(iter, cpu, &ts);
dd0e545f 1171
cdd31cd2
IM
1172 /*
1173 * Pick the entry with the smallest timestamp:
1174 */
3928a8a2 1175 if (ent && (!next || ts < next_ts)) {
bc0c38d1
SR
1176 next = ent;
1177 next_cpu = cpu;
3928a8a2 1178 next_ts = ts;
bc0c38d1
SR
1179 }
1180 }
1181
1182 if (ent_cpu)
1183 *ent_cpu = next_cpu;
1184
3928a8a2
SR
1185 if (ent_ts)
1186 *ent_ts = next_ts;
1187
bc0c38d1
SR
1188 return next;
1189}
1190
dd0e545f 1191/* Find the next real entry, without updating the iterator itself */
c4a8e8be
FW
1192struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1193 int *ent_cpu, u64 *ent_ts)
bc0c38d1 1194{
3928a8a2 1195 return __find_next_entry(iter, ent_cpu, ent_ts);
dd0e545f
SR
1196}
1197
1198/* Find the next real entry, and increment the iterator to the next entry */
1199static void *find_next_entry_inc(struct trace_iterator *iter)
1200{
3928a8a2 1201 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
dd0e545f 1202
3928a8a2 1203 if (iter->ent)
e2ac8ef5 1204 trace_iterator_increment(iter);
dd0e545f 1205
3928a8a2 1206 return iter->ent ? iter : NULL;
b3806b43 1207}
bc0c38d1 1208
e309b41d 1209static void trace_consume(struct trace_iterator *iter)
b3806b43 1210{
d769041f
SR
1211 /* Don't allow ftrace to trace into the ring buffers */
1212 ftrace_disable_cpu();
3928a8a2 1213 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
d769041f 1214 ftrace_enable_cpu();
bc0c38d1
SR
1215}
1216
e309b41d 1217static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
1218{
1219 struct trace_iterator *iter = m->private;
bc0c38d1 1220 int i = (int)*pos;
4e3c3333 1221 void *ent;
bc0c38d1
SR
1222
1223 (*pos)++;
1224
1225 /* can't go backwards */
1226 if (iter->idx > i)
1227 return NULL;
1228
1229 if (iter->idx < 0)
1230 ent = find_next_entry_inc(iter);
1231 else
1232 ent = iter;
1233
1234 while (ent && iter->idx < i)
1235 ent = find_next_entry_inc(iter);
1236
1237 iter->pos = *pos;
1238
bc0c38d1
SR
1239 return ent;
1240}
1241
1242static void *s_start(struct seq_file *m, loff_t *pos)
1243{
1244 struct trace_iterator *iter = m->private;
1245 void *p = NULL;
1246 loff_t l = 0;
3928a8a2 1247 int cpu;
bc0c38d1
SR
1248
1249 mutex_lock(&trace_types_lock);
1250
d15f57f2
SR
1251 if (!current_trace || current_trace != iter->trace) {
1252 mutex_unlock(&trace_types_lock);
bc0c38d1 1253 return NULL;
d15f57f2 1254 }
bc0c38d1
SR
1255
1256 atomic_inc(&trace_record_cmdline_disabled);
1257
bc0c38d1
SR
1258 if (*pos != iter->pos) {
1259 iter->ent = NULL;
1260 iter->cpu = 0;
1261 iter->idx = -1;
1262
d769041f
SR
1263 ftrace_disable_cpu();
1264
3928a8a2
SR
1265 for_each_tracing_cpu(cpu) {
1266 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
4c11d7ae 1267 }
bc0c38d1 1268
d769041f
SR
1269 ftrace_enable_cpu();
1270
bc0c38d1
SR
1271 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1272 ;
1273
1274 } else {
4c11d7ae 1275 l = *pos - 1;
bc0c38d1
SR
1276 p = s_next(m, p, &l);
1277 }
1278
1279 return p;
1280}
1281
1282static void s_stop(struct seq_file *m, void *p)
1283{
bc0c38d1 1284 atomic_dec(&trace_record_cmdline_disabled);
bc0c38d1
SR
1285 mutex_unlock(&trace_types_lock);
1286}
1287
e309b41d 1288static void print_lat_help_header(struct seq_file *m)
bc0c38d1 1289{
a6168353
ME
1290 seq_puts(m, "# _------=> CPU# \n");
1291 seq_puts(m, "# / _-----=> irqs-off \n");
1292 seq_puts(m, "# | / _----=> need-resched \n");
1293 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1294 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1295 seq_puts(m, "# |||| / \n");
1296 seq_puts(m, "# ||||| delay \n");
1297 seq_puts(m, "# cmd pid ||||| time | caller \n");
1298 seq_puts(m, "# \\ / ||||| \\ | / \n");
bc0c38d1
SR
1299}
1300
e309b41d 1301static void print_func_help_header(struct seq_file *m)
bc0c38d1 1302{
a6168353
ME
1303 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1304 seq_puts(m, "# | | | | |\n");
bc0c38d1
SR
1305}
1306
1307
e309b41d 1308static void
bc0c38d1
SR
1309print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1310{
1311 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1312 struct trace_array *tr = iter->tr;
1313 struct trace_array_cpu *data = tr->data[tr->cpu];
1314 struct tracer *type = current_trace;
3928a8a2
SR
1315 unsigned long total;
1316 unsigned long entries;
bc0c38d1
SR
1317 const char *name = "preemption";
1318
1319 if (type)
1320 name = type->name;
1321
3928a8a2
SR
1322 entries = ring_buffer_entries(iter->tr->buffer);
1323 total = entries +
1324 ring_buffer_overruns(iter->tr->buffer);
bc0c38d1
SR
1325
1326 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1327 name, UTS_RELEASE);
1328 seq_puts(m, "-----------------------------------"
1329 "---------------------------------\n");
1330 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1331 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 1332 nsecs_to_usecs(data->saved_latency),
bc0c38d1 1333 entries,
4c11d7ae 1334 total,
bc0c38d1
SR
1335 tr->cpu,
1336#if defined(CONFIG_PREEMPT_NONE)
1337 "server",
1338#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1339 "desktop",
b5c21b45 1340#elif defined(CONFIG_PREEMPT)
bc0c38d1
SR
1341 "preempt",
1342#else
1343 "unknown",
1344#endif
1345 /* These are reserved for later use */
1346 0, 0, 0, 0);
1347#ifdef CONFIG_SMP
1348 seq_printf(m, " #P:%d)\n", num_online_cpus());
1349#else
1350 seq_puts(m, ")\n");
1351#endif
1352 seq_puts(m, " -----------------\n");
1353 seq_printf(m, " | task: %.16s-%d "
1354 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1355 data->comm, data->pid, data->uid, data->nice,
1356 data->policy, data->rt_priority);
1357 seq_puts(m, " -----------------\n");
1358
1359 if (data->critical_start) {
1360 seq_puts(m, " => started at: ");
214023c3
SR
1361 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1362 trace_print_seq(m, &iter->seq);
bc0c38d1 1363 seq_puts(m, "\n => ended at: ");
214023c3
SR
1364 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1365 trace_print_seq(m, &iter->seq);
bc0c38d1
SR
1366 seq_puts(m, "\n");
1367 }
1368
1369 seq_puts(m, "\n");
1370}
1371
a309720c
SR
1372static void test_cpu_buff_start(struct trace_iterator *iter)
1373{
1374 struct trace_seq *s = &iter->seq;
1375
12ef7d44
SR
1376 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1377 return;
1378
1379 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1380 return;
1381
4462344e 1382 if (cpumask_test_cpu(iter->cpu, iter->started))
a309720c
SR
1383 return;
1384
4462344e 1385 cpumask_set_cpu(iter->cpu, iter->started);
a309720c
SR
1386 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1387}
1388
c4a8e8be 1389static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
bc0c38d1 1390{
214023c3 1391 struct trace_seq *s = &iter->seq;
bc0c38d1 1392 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
f633cef0 1393 struct trace_event *event;
bc0c38d1 1394 struct trace_entry *entry = iter->ent;
dd0e545f 1395
a309720c
SR
1396 test_cpu_buff_start(iter);
1397
c4a8e8be
FW
1398 event = ftrace_find_event(entry->type);
1399
1400 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
d9793bd8
ACM
1401 if (!trace_print_lat_context(iter))
1402 goto partial;
bc0c38d1 1403 }
777e208d 1404
268ccda0 1405 if (event)
d9793bd8
ACM
1406 return event->latency_trace(iter, sym_flags);
1407
1408 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1409 goto partial;
02b67518 1410
2c4f035f 1411 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
1412partial:
1413 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1
SR
1414}
1415
2c4f035f 1416static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 1417{
214023c3 1418 struct trace_seq *s = &iter->seq;
bc0c38d1 1419 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 1420 struct trace_entry *entry;
f633cef0 1421 struct trace_event *event;
bc0c38d1 1422
4e3c3333 1423 entry = iter->ent;
dd0e545f 1424
a309720c
SR
1425 test_cpu_buff_start(iter);
1426
c4a8e8be 1427 event = ftrace_find_event(entry->type);
bc0c38d1 1428
c4a8e8be 1429 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
d9793bd8
ACM
1430 if (!trace_print_context(iter))
1431 goto partial;
c4a8e8be 1432 }
bc0c38d1 1433
268ccda0 1434 if (event)
d9793bd8
ACM
1435 return event->trace(iter, sym_flags);
1436
1437 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1438 goto partial;
02b67518 1439
2c4f035f 1440 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
1441partial:
1442 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1
SR
1443}
1444
2c4f035f 1445static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3
IM
1446{
1447 struct trace_seq *s = &iter->seq;
1448 struct trace_entry *entry;
f633cef0 1449 struct trace_event *event;
f9896bf3
IM
1450
1451 entry = iter->ent;
dd0e545f 1452
c4a8e8be 1453 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
d9793bd8
ACM
1454 if (!trace_seq_printf(s, "%d %d %llu ",
1455 entry->pid, iter->cpu, iter->ts))
1456 goto partial;
c4a8e8be 1457 }
f9896bf3 1458
f633cef0 1459 event = ftrace_find_event(entry->type);
268ccda0 1460 if (event)
d9793bd8
ACM
1461 return event->raw(iter, 0);
1462
1463 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1464 goto partial;
777e208d 1465
2c4f035f 1466 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
1467partial:
1468 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3
IM
1469}
1470
2c4f035f 1471static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec
IM
1472{
1473 struct trace_seq *s = &iter->seq;
1474 unsigned char newline = '\n';
1475 struct trace_entry *entry;
f633cef0 1476 struct trace_event *event;
5e3ca0ec
IM
1477
1478 entry = iter->ent;
dd0e545f 1479
c4a8e8be
FW
1480 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1481 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1482 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1483 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1484 }
5e3ca0ec 1485
f633cef0 1486 event = ftrace_find_event(entry->type);
268ccda0 1487 if (event) {
ae7462b4 1488 enum print_line_t ret = event->hex(iter, 0);
d9793bd8
ACM
1489 if (ret != TRACE_TYPE_HANDLED)
1490 return ret;
1491 }
7104f300 1492
5e3ca0ec
IM
1493 SEQ_PUT_FIELD_RET(s, newline);
1494
2c4f035f 1495 return TRACE_TYPE_HANDLED;
5e3ca0ec
IM
1496}
1497
66896a85
FW
1498static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1499{
1500 struct trace_seq *s = &iter->seq;
1501 struct trace_entry *entry = iter->ent;
1502 struct print_entry *field;
1503 int ret;
1504
1505 trace_assign_type(field, entry);
1506
c4a8e8be 1507 ret = trace_seq_printf(s, "%s", field->buf);
66896a85
FW
1508 if (!ret)
1509 return TRACE_TYPE_PARTIAL_LINE;
1510
66896a85
FW
1511 return TRACE_TYPE_HANDLED;
1512}
1513
2c4f035f 1514static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa
IM
1515{
1516 struct trace_seq *s = &iter->seq;
1517 struct trace_entry *entry;
f633cef0 1518 struct trace_event *event;
cb0f12aa
IM
1519
1520 entry = iter->ent;
dd0e545f 1521
c4a8e8be
FW
1522 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1523 SEQ_PUT_FIELD_RET(s, entry->pid);
1524 SEQ_PUT_FIELD_RET(s, entry->cpu);
1525 SEQ_PUT_FIELD_RET(s, iter->ts);
1526 }
cb0f12aa 1527
f633cef0 1528 event = ftrace_find_event(entry->type);
268ccda0 1529 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
cb0f12aa
IM
1530}
1531
bc0c38d1
SR
1532static int trace_empty(struct trace_iterator *iter)
1533{
bc0c38d1
SR
1534 int cpu;
1535
ab46428c 1536 for_each_tracing_cpu(cpu) {
d769041f
SR
1537 if (iter->buffer_iter[cpu]) {
1538 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1539 return 0;
1540 } else {
1541 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1542 return 0;
1543 }
bc0c38d1 1544 }
d769041f 1545
797d3712 1546 return 1;
bc0c38d1
SR
1547}
1548
2c4f035f 1549static enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 1550{
2c4f035f
FW
1551 enum print_line_t ret;
1552
1553 if (iter->trace && iter->trace->print_line) {
1554 ret = iter->trace->print_line(iter);
1555 if (ret != TRACE_TYPE_UNHANDLED)
1556 return ret;
1557 }
72829bc3 1558
66896a85
FW
1559 if (iter->ent->type == TRACE_PRINT &&
1560 trace_flags & TRACE_ITER_PRINTK &&
1561 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1562 return print_printk_msg_only(iter);
1563
cb0f12aa
IM
1564 if (trace_flags & TRACE_ITER_BIN)
1565 return print_bin_fmt(iter);
1566
5e3ca0ec
IM
1567 if (trace_flags & TRACE_ITER_HEX)
1568 return print_hex_fmt(iter);
1569
f9896bf3
IM
1570 if (trace_flags & TRACE_ITER_RAW)
1571 return print_raw_fmt(iter);
1572
1573 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
c4a8e8be 1574 return print_lat_fmt(iter);
f9896bf3
IM
1575
1576 return print_trace_fmt(iter);
1577}
1578
bc0c38d1
SR
1579static int s_show(struct seq_file *m, void *v)
1580{
1581 struct trace_iterator *iter = v;
1582
1583 if (iter->ent == NULL) {
1584 if (iter->tr) {
1585 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1586 seq_puts(m, "#\n");
1587 }
8bba1bf5
MM
1588 if (iter->trace && iter->trace->print_header)
1589 iter->trace->print_header(m);
1590 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
bc0c38d1
SR
1591 /* print nothing if the buffers are empty */
1592 if (trace_empty(iter))
1593 return 0;
1594 print_trace_header(m, iter);
1595 if (!(trace_flags & TRACE_ITER_VERBOSE))
1596 print_lat_help_header(m);
1597 } else {
1598 if (!(trace_flags & TRACE_ITER_VERBOSE))
1599 print_func_help_header(m);
1600 }
1601 } else {
f9896bf3 1602 print_trace_line(iter);
214023c3 1603 trace_print_seq(m, &iter->seq);
bc0c38d1
SR
1604 }
1605
1606 return 0;
1607}
1608
1609static struct seq_operations tracer_seq_ops = {
4bf39a94
IM
1610 .start = s_start,
1611 .next = s_next,
1612 .stop = s_stop,
1613 .show = s_show,
bc0c38d1
SR
1614};
1615
e309b41d 1616static struct trace_iterator *
bc0c38d1
SR
1617__tracing_open(struct inode *inode, struct file *file, int *ret)
1618{
1619 struct trace_iterator *iter;
3928a8a2
SR
1620 struct seq_file *m;
1621 int cpu;
bc0c38d1 1622
60a11774
SR
1623 if (tracing_disabled) {
1624 *ret = -ENODEV;
1625 return NULL;
1626 }
1627
bc0c38d1
SR
1628 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1629 if (!iter) {
1630 *ret = -ENOMEM;
1631 goto out;
1632 }
1633
1634 mutex_lock(&trace_types_lock);
1635 if (current_trace && current_trace->print_max)
1636 iter->tr = &max_tr;
1637 else
1638 iter->tr = inode->i_private;
1639 iter->trace = current_trace;
1640 iter->pos = -1;
1641
8bba1bf5
MM
1642 /* Notify the tracer early; before we stop tracing. */
1643 if (iter->trace && iter->trace->open)
a93751ca 1644 iter->trace->open(iter);
8bba1bf5 1645
12ef7d44
SR
1646 /* Annotate start of buffers if we had overruns */
1647 if (ring_buffer_overruns(iter->tr->buffer))
1648 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1649
1650
3928a8a2 1651 for_each_tracing_cpu(cpu) {
d769041f 1652
3928a8a2
SR
1653 iter->buffer_iter[cpu] =
1654 ring_buffer_read_start(iter->tr->buffer, cpu);
d769041f 1655
3928a8a2
SR
1656 if (!iter->buffer_iter[cpu])
1657 goto fail_buffer;
1658 }
1659
bc0c38d1
SR
1660 /* TODO stop tracer */
1661 *ret = seq_open(file, &tracer_seq_ops);
3928a8a2
SR
1662 if (*ret)
1663 goto fail_buffer;
bc0c38d1 1664
3928a8a2
SR
1665 m = file->private_data;
1666 m->private = iter;
bc0c38d1 1667
3928a8a2 1668 /* stop the trace while dumping */
9036990d 1669 tracing_stop();
3928a8a2 1670
bc0c38d1
SR
1671 mutex_unlock(&trace_types_lock);
1672
1673 out:
1674 return iter;
3928a8a2
SR
1675
1676 fail_buffer:
1677 for_each_tracing_cpu(cpu) {
1678 if (iter->buffer_iter[cpu])
1679 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1680 }
1681 mutex_unlock(&trace_types_lock);
0bb943c7 1682 kfree(iter);
3928a8a2
SR
1683
1684 return ERR_PTR(-ENOMEM);
bc0c38d1
SR
1685}
1686
1687int tracing_open_generic(struct inode *inode, struct file *filp)
1688{
60a11774
SR
1689 if (tracing_disabled)
1690 return -ENODEV;
1691
bc0c38d1
SR
1692 filp->private_data = inode->i_private;
1693 return 0;
1694}
1695
1696int tracing_release(struct inode *inode, struct file *file)
1697{
1698 struct seq_file *m = (struct seq_file *)file->private_data;
1699 struct trace_iterator *iter = m->private;
3928a8a2 1700 int cpu;
bc0c38d1
SR
1701
1702 mutex_lock(&trace_types_lock);
3928a8a2
SR
1703 for_each_tracing_cpu(cpu) {
1704 if (iter->buffer_iter[cpu])
1705 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1706 }
1707
bc0c38d1
SR
1708 if (iter->trace && iter->trace->close)
1709 iter->trace->close(iter);
1710
1711 /* reenable tracing if it was previously enabled */
9036990d 1712 tracing_start();
bc0c38d1
SR
1713 mutex_unlock(&trace_types_lock);
1714
1715 seq_release(inode, file);
1716 kfree(iter);
1717 return 0;
1718}
1719
1720static int tracing_open(struct inode *inode, struct file *file)
1721{
1722 int ret;
1723
1724 __tracing_open(inode, file, &ret);
1725
1726 return ret;
1727}
1728
1729static int tracing_lt_open(struct inode *inode, struct file *file)
1730{
1731 struct trace_iterator *iter;
1732 int ret;
1733
1734 iter = __tracing_open(inode, file, &ret);
1735
1736 if (!ret)
1737 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1738
1739 return ret;
1740}
1741
1742
e309b41d 1743static void *
bc0c38d1
SR
1744t_next(struct seq_file *m, void *v, loff_t *pos)
1745{
1746 struct tracer *t = m->private;
1747
1748 (*pos)++;
1749
1750 if (t)
1751 t = t->next;
1752
1753 m->private = t;
1754
1755 return t;
1756}
1757
1758static void *t_start(struct seq_file *m, loff_t *pos)
1759{
1760 struct tracer *t = m->private;
1761 loff_t l = 0;
1762
1763 mutex_lock(&trace_types_lock);
1764 for (; t && l < *pos; t = t_next(m, t, &l))
1765 ;
1766
1767 return t;
1768}
1769
1770static void t_stop(struct seq_file *m, void *p)
1771{
1772 mutex_unlock(&trace_types_lock);
1773}
1774
1775static int t_show(struct seq_file *m, void *v)
1776{
1777 struct tracer *t = v;
1778
1779 if (!t)
1780 return 0;
1781
1782 seq_printf(m, "%s", t->name);
1783 if (t->next)
1784 seq_putc(m, ' ');
1785 else
1786 seq_putc(m, '\n');
1787
1788 return 0;
1789}
1790
1791static struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
1792 .start = t_start,
1793 .next = t_next,
1794 .stop = t_stop,
1795 .show = t_show,
bc0c38d1
SR
1796};
1797
1798static int show_traces_open(struct inode *inode, struct file *file)
1799{
1800 int ret;
1801
60a11774
SR
1802 if (tracing_disabled)
1803 return -ENODEV;
1804
bc0c38d1
SR
1805 ret = seq_open(file, &show_traces_seq_ops);
1806 if (!ret) {
1807 struct seq_file *m = file->private_data;
1808 m->private = trace_types;
1809 }
1810
1811 return ret;
1812}
1813
1814static struct file_operations tracing_fops = {
4bf39a94
IM
1815 .open = tracing_open,
1816 .read = seq_read,
1817 .llseek = seq_lseek,
1818 .release = tracing_release,
bc0c38d1
SR
1819};
1820
1821static struct file_operations tracing_lt_fops = {
4bf39a94
IM
1822 .open = tracing_lt_open,
1823 .read = seq_read,
1824 .llseek = seq_lseek,
1825 .release = tracing_release,
bc0c38d1
SR
1826};
1827
1828static struct file_operations show_traces_fops = {
c7078de1
IM
1829 .open = show_traces_open,
1830 .read = seq_read,
1831 .release = seq_release,
1832};
1833
36dfe925
IM
1834/*
1835 * Only trace on a CPU if the bitmask is set:
1836 */
9e01c1b7 1837static cpumask_var_t tracing_cpumask;
36dfe925
IM
1838
1839/*
1840 * The tracer itself will not take this lock, but still we want
1841 * to provide a consistent cpumask to user-space:
1842 */
1843static DEFINE_MUTEX(tracing_cpumask_update_lock);
1844
1845/*
1846 * Temporary storage for the character representation of the
1847 * CPU bitmask (and one more byte for the newline):
1848 */
1849static char mask_str[NR_CPUS + 1];
1850
c7078de1
IM
1851static ssize_t
1852tracing_cpumask_read(struct file *filp, char __user *ubuf,
1853 size_t count, loff_t *ppos)
1854{
36dfe925 1855 int len;
c7078de1
IM
1856
1857 mutex_lock(&tracing_cpumask_update_lock);
36dfe925 1858
9e01c1b7 1859 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
36dfe925
IM
1860 if (count - len < 2) {
1861 count = -EINVAL;
1862 goto out_err;
1863 }
1864 len += sprintf(mask_str + len, "\n");
1865 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1866
1867out_err:
c7078de1
IM
1868 mutex_unlock(&tracing_cpumask_update_lock);
1869
1870 return count;
1871}
1872
1873static ssize_t
1874tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1875 size_t count, loff_t *ppos)
1876{
36dfe925 1877 int err, cpu;
9e01c1b7
RR
1878 cpumask_var_t tracing_cpumask_new;
1879
1880 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1881 return -ENOMEM;
c7078de1
IM
1882
1883 mutex_lock(&tracing_cpumask_update_lock);
9e01c1b7 1884 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 1885 if (err)
36dfe925
IM
1886 goto err_unlock;
1887
a5e25883 1888 local_irq_disable();
92205c23 1889 __raw_spin_lock(&ftrace_max_lock);
ab46428c 1890 for_each_tracing_cpu(cpu) {
36dfe925
IM
1891 /*
1892 * Increase/decrease the disabled counter if we are
1893 * about to flip a bit in the cpumask:
1894 */
9e01c1b7
RR
1895 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
1896 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
1897 atomic_inc(&global_trace.data[cpu]->disabled);
1898 }
9e01c1b7
RR
1899 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
1900 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
1901 atomic_dec(&global_trace.data[cpu]->disabled);
1902 }
1903 }
92205c23 1904 __raw_spin_unlock(&ftrace_max_lock);
a5e25883 1905 local_irq_enable();
36dfe925 1906
9e01c1b7 1907 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
36dfe925
IM
1908
1909 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 1910 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
1911
1912 return count;
36dfe925
IM
1913
1914err_unlock:
1915 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 1916 free_cpumask_var(tracing_cpumask);
36dfe925
IM
1917
1918 return err;
c7078de1
IM
1919}
1920
1921static struct file_operations tracing_cpumask_fops = {
1922 .open = tracing_open_generic,
1923 .read = tracing_cpumask_read,
1924 .write = tracing_cpumask_write,
bc0c38d1
SR
1925};
1926
1927static ssize_t
ee6bce52 1928tracing_trace_options_read(struct file *filp, char __user *ubuf,
bc0c38d1
SR
1929 size_t cnt, loff_t *ppos)
1930{
adf9f195 1931 int i;
bc0c38d1
SR
1932 char *buf;
1933 int r = 0;
1934 int len = 0;
adf9f195
FW
1935 u32 tracer_flags = current_trace->flags->val;
1936 struct tracer_opt *trace_opts = current_trace->flags->opts;
1937
bc0c38d1
SR
1938
1939 /* calulate max size */
1940 for (i = 0; trace_options[i]; i++) {
1941 len += strlen(trace_options[i]);
1942 len += 3; /* "no" and space */
1943 }
1944
adf9f195
FW
1945 /*
1946 * Increase the size with names of options specific
1947 * of the current tracer.
1948 */
1949 for (i = 0; trace_opts[i].name; i++) {
1950 len += strlen(trace_opts[i].name);
1951 len += 3; /* "no" and space */
1952 }
1953
bc0c38d1
SR
1954 /* +2 for \n and \0 */
1955 buf = kmalloc(len + 2, GFP_KERNEL);
1956 if (!buf)
1957 return -ENOMEM;
1958
1959 for (i = 0; trace_options[i]; i++) {
1960 if (trace_flags & (1 << i))
1961 r += sprintf(buf + r, "%s ", trace_options[i]);
1962 else
1963 r += sprintf(buf + r, "no%s ", trace_options[i]);
1964 }
1965
adf9f195
FW
1966 for (i = 0; trace_opts[i].name; i++) {
1967 if (tracer_flags & trace_opts[i].bit)
1968 r += sprintf(buf + r, "%s ",
1969 trace_opts[i].name);
1970 else
1971 r += sprintf(buf + r, "no%s ",
1972 trace_opts[i].name);
1973 }
1974
bc0c38d1
SR
1975 r += sprintf(buf + r, "\n");
1976 WARN_ON(r >= len + 2);
1977
36dfe925 1978 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1979
1980 kfree(buf);
1981
1982 return r;
1983}
1984
adf9f195
FW
1985/* Try to assign a tracer specific option */
1986static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
1987{
1988 struct tracer_flags *trace_flags = trace->flags;
1989 struct tracer_opt *opts = NULL;
1990 int ret = 0, i = 0;
1991 int len;
1992
1993 for (i = 0; trace_flags->opts[i].name; i++) {
1994 opts = &trace_flags->opts[i];
1995 len = strlen(opts->name);
1996
1997 if (strncmp(cmp, opts->name, len) == 0) {
1998 ret = trace->set_flag(trace_flags->val,
1999 opts->bit, !neg);
2000 break;
2001 }
2002 }
2003 /* Not found */
2004 if (!trace_flags->opts[i].name)
2005 return -EINVAL;
2006
2007 /* Refused to handle */
2008 if (ret)
2009 return ret;
2010
2011 if (neg)
2012 trace_flags->val &= ~opts->bit;
2013 else
2014 trace_flags->val |= opts->bit;
2015
2016 return 0;
2017}
2018
bc0c38d1 2019static ssize_t
ee6bce52 2020tracing_trace_options_write(struct file *filp, const char __user *ubuf,
bc0c38d1
SR
2021 size_t cnt, loff_t *ppos)
2022{
2023 char buf[64];
2024 char *cmp = buf;
2025 int neg = 0;
adf9f195 2026 int ret;
bc0c38d1
SR
2027 int i;
2028
cffae437
SR
2029 if (cnt >= sizeof(buf))
2030 return -EINVAL;
bc0c38d1
SR
2031
2032 if (copy_from_user(&buf, ubuf, cnt))
2033 return -EFAULT;
2034
2035 buf[cnt] = 0;
2036
2037 if (strncmp(buf, "no", 2) == 0) {
2038 neg = 1;
2039 cmp += 2;
2040 }
2041
2042 for (i = 0; trace_options[i]; i++) {
2043 int len = strlen(trace_options[i]);
2044
2045 if (strncmp(cmp, trace_options[i], len) == 0) {
2046 if (neg)
2047 trace_flags &= ~(1 << i);
2048 else
2049 trace_flags |= (1 << i);
2050 break;
2051 }
2052 }
adf9f195
FW
2053
2054 /* If no option could be set, test the specific tracer options */
2055 if (!trace_options[i]) {
2056 ret = set_tracer_option(current_trace, cmp, neg);
2057 if (ret)
2058 return ret;
2059 }
bc0c38d1
SR
2060
2061 filp->f_pos += cnt;
2062
2063 return cnt;
2064}
2065
2066static struct file_operations tracing_iter_fops = {
c7078de1 2067 .open = tracing_open_generic,
ee6bce52
SR
2068 .read = tracing_trace_options_read,
2069 .write = tracing_trace_options_write,
bc0c38d1
SR
2070};
2071
7bd2f24c
IM
2072static const char readme_msg[] =
2073 "tracing mini-HOWTO:\n\n"
2074 "# mkdir /debug\n"
2075 "# mount -t debugfs nodev /debug\n\n"
2076 "# cat /debug/tracing/available_tracers\n"
2077 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2078 "# cat /debug/tracing/current_tracer\n"
2079 "none\n"
2080 "# echo sched_switch > /debug/tracing/current_tracer\n"
2081 "# cat /debug/tracing/current_tracer\n"
2082 "sched_switch\n"
ee6bce52 2083 "# cat /debug/tracing/trace_options\n"
7bd2f24c 2084 "noprint-parent nosym-offset nosym-addr noverbose\n"
ee6bce52 2085 "# echo print-parent > /debug/tracing/trace_options\n"
7bd2f24c
IM
2086 "# echo 1 > /debug/tracing/tracing_enabled\n"
2087 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2088 "echo 0 > /debug/tracing/tracing_enabled\n"
2089;
2090
2091static ssize_t
2092tracing_readme_read(struct file *filp, char __user *ubuf,
2093 size_t cnt, loff_t *ppos)
2094{
2095 return simple_read_from_buffer(ubuf, cnt, ppos,
2096 readme_msg, strlen(readme_msg));
2097}
2098
2099static struct file_operations tracing_readme_fops = {
c7078de1
IM
2100 .open = tracing_open_generic,
2101 .read = tracing_readme_read,
7bd2f24c
IM
2102};
2103
bc0c38d1
SR
2104static ssize_t
2105tracing_ctrl_read(struct file *filp, char __user *ubuf,
2106 size_t cnt, loff_t *ppos)
2107{
bc0c38d1
SR
2108 char buf[64];
2109 int r;
2110
9036990d 2111 r = sprintf(buf, "%u\n", tracer_enabled);
4e3c3333 2112 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2113}
2114
2115static ssize_t
2116tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2117 size_t cnt, loff_t *ppos)
2118{
2119 struct trace_array *tr = filp->private_data;
bc0c38d1 2120 char buf[64];
c6caeeb1
SR
2121 long val;
2122 int ret;
bc0c38d1 2123
cffae437
SR
2124 if (cnt >= sizeof(buf))
2125 return -EINVAL;
bc0c38d1
SR
2126
2127 if (copy_from_user(&buf, ubuf, cnt))
2128 return -EFAULT;
2129
2130 buf[cnt] = 0;
2131
c6caeeb1
SR
2132 ret = strict_strtoul(buf, 10, &val);
2133 if (ret < 0)
2134 return ret;
bc0c38d1
SR
2135
2136 val = !!val;
2137
2138 mutex_lock(&trace_types_lock);
9036990d
SR
2139 if (tracer_enabled ^ val) {
2140 if (val) {
bc0c38d1 2141 tracer_enabled = 1;
9036990d
SR
2142 if (current_trace->start)
2143 current_trace->start(tr);
2144 tracing_start();
2145 } else {
bc0c38d1 2146 tracer_enabled = 0;
9036990d
SR
2147 tracing_stop();
2148 if (current_trace->stop)
2149 current_trace->stop(tr);
2150 }
bc0c38d1
SR
2151 }
2152 mutex_unlock(&trace_types_lock);
2153
2154 filp->f_pos += cnt;
2155
2156 return cnt;
2157}
2158
2159static ssize_t
2160tracing_set_trace_read(struct file *filp, char __user *ubuf,
2161 size_t cnt, loff_t *ppos)
2162{
2163 char buf[max_tracer_type_len+2];
2164 int r;
2165
2166 mutex_lock(&trace_types_lock);
2167 if (current_trace)
2168 r = sprintf(buf, "%s\n", current_trace->name);
2169 else
2170 r = sprintf(buf, "\n");
2171 mutex_unlock(&trace_types_lock);
2172
4bf39a94 2173 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2174}
2175
b2821ae6 2176static int tracing_set_tracer(const char *buf)
bc0c38d1
SR
2177{
2178 struct trace_array *tr = &global_trace;
2179 struct tracer *t;
d9e54076 2180 int ret = 0;
bc0c38d1
SR
2181
2182 mutex_lock(&trace_types_lock);
2183 for (t = trace_types; t; t = t->next) {
2184 if (strcmp(t->name, buf) == 0)
2185 break;
2186 }
c2931e05
FW
2187 if (!t) {
2188 ret = -EINVAL;
2189 goto out;
2190 }
2191 if (t == current_trace)
bc0c38d1
SR
2192 goto out;
2193
9f029e83 2194 trace_branch_disable();
bc0c38d1
SR
2195 if (current_trace && current_trace->reset)
2196 current_trace->reset(tr);
2197
2198 current_trace = t;
1c80025a
FW
2199 if (t->init) {
2200 ret = t->init(tr);
2201 if (ret)
2202 goto out;
2203 }
bc0c38d1 2204
9f029e83 2205 trace_branch_enable(tr);
bc0c38d1
SR
2206 out:
2207 mutex_unlock(&trace_types_lock);
2208
d9e54076
PZ
2209 return ret;
2210}
2211
2212static ssize_t
2213tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2214 size_t cnt, loff_t *ppos)
2215{
2216 char buf[max_tracer_type_len+1];
2217 int i;
2218 size_t ret;
e6e7a65a
FW
2219 int err;
2220
2221 ret = cnt;
d9e54076
PZ
2222
2223 if (cnt > max_tracer_type_len)
2224 cnt = max_tracer_type_len;
2225
2226 if (copy_from_user(&buf, ubuf, cnt))
2227 return -EFAULT;
2228
2229 buf[cnt] = 0;
2230
2231 /* strip ending whitespace. */
2232 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2233 buf[i] = 0;
2234
e6e7a65a
FW
2235 err = tracing_set_tracer(buf);
2236 if (err)
2237 return err;
d9e54076 2238
e6e7a65a 2239 filp->f_pos += ret;
bc0c38d1 2240
c2931e05 2241 return ret;
bc0c38d1
SR
2242}
2243
2244static ssize_t
2245tracing_max_lat_read(struct file *filp, char __user *ubuf,
2246 size_t cnt, loff_t *ppos)
2247{
2248 unsigned long *ptr = filp->private_data;
2249 char buf[64];
2250 int r;
2251
cffae437 2252 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 2253 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
2254 if (r > sizeof(buf))
2255 r = sizeof(buf);
4bf39a94 2256 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2257}
2258
2259static ssize_t
2260tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2261 size_t cnt, loff_t *ppos)
2262{
2263 long *ptr = filp->private_data;
bc0c38d1 2264 char buf[64];
c6caeeb1
SR
2265 long val;
2266 int ret;
bc0c38d1 2267
cffae437
SR
2268 if (cnt >= sizeof(buf))
2269 return -EINVAL;
bc0c38d1
SR
2270
2271 if (copy_from_user(&buf, ubuf, cnt))
2272 return -EFAULT;
2273
2274 buf[cnt] = 0;
2275
c6caeeb1
SR
2276 ret = strict_strtoul(buf, 10, &val);
2277 if (ret < 0)
2278 return ret;
bc0c38d1
SR
2279
2280 *ptr = val * 1000;
2281
2282 return cnt;
2283}
2284
b3806b43
SR
2285static atomic_t tracing_reader;
2286
2287static int tracing_open_pipe(struct inode *inode, struct file *filp)
2288{
2289 struct trace_iterator *iter;
2290
2291 if (tracing_disabled)
2292 return -ENODEV;
2293
2294 /* We only allow for reader of the pipe */
2295 if (atomic_inc_return(&tracing_reader) != 1) {
2296 atomic_dec(&tracing_reader);
2297 return -EBUSY;
2298 }
2299
2300 /* create a buffer to store the information to pass to userspace */
2301 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2302 if (!iter)
2303 return -ENOMEM;
2304
4462344e
RR
2305 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2306 kfree(iter);
2307 return -ENOMEM;
2308 }
2309
107bad8b 2310 mutex_lock(&trace_types_lock);
a309720c
SR
2311
2312 /* trace pipe does not show start of buffer */
4462344e 2313 cpumask_setall(iter->started);
a309720c 2314
b3806b43 2315 iter->tr = &global_trace;
72829bc3 2316 iter->trace = current_trace;
b3806b43
SR
2317 filp->private_data = iter;
2318
107bad8b
SR
2319 if (iter->trace->pipe_open)
2320 iter->trace->pipe_open(iter);
2321 mutex_unlock(&trace_types_lock);
2322
b3806b43
SR
2323 return 0;
2324}
2325
2326static int tracing_release_pipe(struct inode *inode, struct file *file)
2327{
2328 struct trace_iterator *iter = file->private_data;
2329
4462344e 2330 free_cpumask_var(iter->started);
b3806b43
SR
2331 kfree(iter);
2332 atomic_dec(&tracing_reader);
2333
2334 return 0;
2335}
2336
2a2cc8f7
SSP
2337static unsigned int
2338tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2339{
2340 struct trace_iterator *iter = filp->private_data;
2341
2342 if (trace_flags & TRACE_ITER_BLOCK) {
2343 /*
2344 * Always select as readable when in blocking mode
2345 */
2346 return POLLIN | POLLRDNORM;
afc2abc0 2347 } else {
2a2cc8f7
SSP
2348 if (!trace_empty(iter))
2349 return POLLIN | POLLRDNORM;
2350 poll_wait(filp, &trace_wait, poll_table);
2351 if (!trace_empty(iter))
2352 return POLLIN | POLLRDNORM;
2353
2354 return 0;
2355 }
2356}
2357
b3806b43
SR
2358/*
2359 * Consumer reader.
2360 */
2361static ssize_t
2362tracing_read_pipe(struct file *filp, char __user *ubuf,
2363 size_t cnt, loff_t *ppos)
2364{
2365 struct trace_iterator *iter = filp->private_data;
6c6c2796 2366 ssize_t sret;
b3806b43
SR
2367
2368 /* return any leftover data */
6c6c2796
PP
2369 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2370 if (sret != -EBUSY)
2371 return sret;
b3806b43 2372
6c6c2796 2373 trace_seq_reset(&iter->seq);
b3806b43 2374
107bad8b
SR
2375 mutex_lock(&trace_types_lock);
2376 if (iter->trace->read) {
6c6c2796
PP
2377 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2378 if (sret)
107bad8b 2379 goto out;
107bad8b
SR
2380 }
2381
9ff4b974
PP
2382waitagain:
2383 sret = 0;
b3806b43 2384 while (trace_empty(iter)) {
2dc8f095 2385
107bad8b 2386 if ((filp->f_flags & O_NONBLOCK)) {
6c6c2796 2387 sret = -EAGAIN;
107bad8b
SR
2388 goto out;
2389 }
2dc8f095 2390
b3806b43
SR
2391 /*
2392 * This is a make-shift waitqueue. The reason we don't use
2393 * an actual wait queue is because:
2394 * 1) we only ever have one waiter
2395 * 2) the tracing, traces all functions, we don't want
2396 * the overhead of calling wake_up and friends
2397 * (and tracing them too)
2398 * Anyway, this is really very primitive wakeup.
2399 */
2400 set_current_state(TASK_INTERRUPTIBLE);
2401 iter->tr->waiter = current;
2402
107bad8b
SR
2403 mutex_unlock(&trace_types_lock);
2404
9fe068e9
IM
2405 /* sleep for 100 msecs, and try again. */
2406 schedule_timeout(HZ/10);
b3806b43 2407
107bad8b
SR
2408 mutex_lock(&trace_types_lock);
2409
b3806b43
SR
2410 iter->tr->waiter = NULL;
2411
107bad8b 2412 if (signal_pending(current)) {
6c6c2796 2413 sret = -EINTR;
107bad8b
SR
2414 goto out;
2415 }
b3806b43 2416
84527997 2417 if (iter->trace != current_trace)
107bad8b 2418 goto out;
84527997 2419
b3806b43
SR
2420 /*
2421 * We block until we read something and tracing is disabled.
2422 * We still block if tracing is disabled, but we have never
2423 * read anything. This allows a user to cat this file, and
2424 * then enable tracing. But after we have read something,
2425 * we give an EOF when tracing is again disabled.
2426 *
2427 * iter->pos will be 0 if we haven't read anything.
2428 */
2429 if (!tracer_enabled && iter->pos)
2430 break;
2431
2432 continue;
2433 }
2434
2435 /* stop when tracing is finished */
2436 if (trace_empty(iter))
107bad8b 2437 goto out;
b3806b43
SR
2438
2439 if (cnt >= PAGE_SIZE)
2440 cnt = PAGE_SIZE - 1;
2441
53d0aa77 2442 /* reset all but tr, trace, and overruns */
53d0aa77
SR
2443 memset(&iter->seq, 0,
2444 sizeof(struct trace_iterator) -
2445 offsetof(struct trace_iterator, seq));
4823ed7e 2446 iter->pos = -1;
b3806b43 2447
088b1e42 2448 while (find_next_entry_inc(iter) != NULL) {
2c4f035f 2449 enum print_line_t ret;
088b1e42
SR
2450 int len = iter->seq.len;
2451
f9896bf3 2452 ret = print_trace_line(iter);
2c4f035f 2453 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42
SR
2454 /* don't print partial lines */
2455 iter->seq.len = len;
b3806b43 2456 break;
088b1e42 2457 }
b3806b43
SR
2458
2459 trace_consume(iter);
2460
2461 if (iter->seq.len >= cnt)
2462 break;
b3806b43
SR
2463 }
2464
b3806b43 2465 /* Now copy what we have to the user */
6c6c2796
PP
2466 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2467 if (iter->seq.readpos >= iter->seq.len)
b3806b43 2468 trace_seq_reset(&iter->seq);
9ff4b974
PP
2469
2470 /*
2471 * If there was nothing to send to user, inspite of consuming trace
2472 * entries, go back to wait for more entries.
2473 */
6c6c2796 2474 if (sret == -EBUSY)
9ff4b974 2475 goto waitagain;
b3806b43 2476
107bad8b
SR
2477out:
2478 mutex_unlock(&trace_types_lock);
2479
6c6c2796 2480 return sret;
b3806b43
SR
2481}
2482
a98a3c3f
SR
2483static ssize_t
2484tracing_entries_read(struct file *filp, char __user *ubuf,
2485 size_t cnt, loff_t *ppos)
2486{
2487 struct trace_array *tr = filp->private_data;
2488 char buf[64];
2489 int r;
2490
1696b2b0 2491 r = sprintf(buf, "%lu\n", tr->entries >> 10);
a98a3c3f
SR
2492 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2493}
2494
2495static ssize_t
2496tracing_entries_write(struct file *filp, const char __user *ubuf,
2497 size_t cnt, loff_t *ppos)
2498{
2499 unsigned long val;
2500 char buf[64];
bf5e6519 2501 int ret, cpu;
a98a3c3f 2502
cffae437
SR
2503 if (cnt >= sizeof(buf))
2504 return -EINVAL;
a98a3c3f
SR
2505
2506 if (copy_from_user(&buf, ubuf, cnt))
2507 return -EFAULT;
2508
2509 buf[cnt] = 0;
2510
c6caeeb1
SR
2511 ret = strict_strtoul(buf, 10, &val);
2512 if (ret < 0)
2513 return ret;
a98a3c3f
SR
2514
2515 /* must have at least 1 entry */
2516 if (!val)
2517 return -EINVAL;
2518
2519 mutex_lock(&trace_types_lock);
2520
c76f0694 2521 tracing_stop();
a98a3c3f 2522
bf5e6519
SR
2523 /* disable all cpu buffers */
2524 for_each_tracing_cpu(cpu) {
2525 if (global_trace.data[cpu])
2526 atomic_inc(&global_trace.data[cpu]->disabled);
2527 if (max_tr.data[cpu])
2528 atomic_inc(&max_tr.data[cpu]->disabled);
2529 }
2530
1696b2b0
SR
2531 /* value is in KB */
2532 val <<= 10;
2533
3928a8a2
SR
2534 if (val != global_trace.entries) {
2535 ret = ring_buffer_resize(global_trace.buffer, val);
2536 if (ret < 0) {
2537 cnt = ret;
3eefae99
SR
2538 goto out;
2539 }
2540
3928a8a2
SR
2541 ret = ring_buffer_resize(max_tr.buffer, val);
2542 if (ret < 0) {
2543 int r;
2544 cnt = ret;
2545 r = ring_buffer_resize(global_trace.buffer,
2546 global_trace.entries);
2547 if (r < 0) {
2548 /* AARGH! We are left with different
2549 * size max buffer!!!! */
2550 WARN_ON(1);
2551 tracing_disabled = 1;
a98a3c3f 2552 }
3928a8a2 2553 goto out;
a98a3c3f 2554 }
3eefae99 2555
3928a8a2 2556 global_trace.entries = val;
a98a3c3f
SR
2557 }
2558
2559 filp->f_pos += cnt;
2560
19384c03
SR
2561 /* If check pages failed, return ENOMEM */
2562 if (tracing_disabled)
2563 cnt = -ENOMEM;
a98a3c3f 2564 out:
bf5e6519
SR
2565 for_each_tracing_cpu(cpu) {
2566 if (global_trace.data[cpu])
2567 atomic_dec(&global_trace.data[cpu]->disabled);
2568 if (max_tr.data[cpu])
2569 atomic_dec(&max_tr.data[cpu]->disabled);
2570 }
2571
c76f0694 2572 tracing_start();
a98a3c3f
SR
2573 max_tr.entries = global_trace.entries;
2574 mutex_unlock(&trace_types_lock);
2575
2576 return cnt;
2577}
2578
5bf9a1ee
PP
2579static int mark_printk(const char *fmt, ...)
2580{
2581 int ret;
2582 va_list args;
2583 va_start(args, fmt);
1fd8f2a3 2584 ret = trace_vprintk(0, -1, fmt, args);
5bf9a1ee
PP
2585 va_end(args);
2586 return ret;
2587}
2588
2589static ssize_t
2590tracing_mark_write(struct file *filp, const char __user *ubuf,
2591 size_t cnt, loff_t *fpos)
2592{
2593 char *buf;
2594 char *end;
5bf9a1ee 2595
c76f0694 2596 if (tracing_disabled)
5bf9a1ee
PP
2597 return -EINVAL;
2598
2599 if (cnt > TRACE_BUF_SIZE)
2600 cnt = TRACE_BUF_SIZE;
2601
2602 buf = kmalloc(cnt + 1, GFP_KERNEL);
2603 if (buf == NULL)
2604 return -ENOMEM;
2605
2606 if (copy_from_user(buf, ubuf, cnt)) {
2607 kfree(buf);
2608 return -EFAULT;
2609 }
2610
2611 /* Cut from the first nil or newline. */
2612 buf[cnt] = '\0';
2613 end = strchr(buf, '\n');
2614 if (end)
2615 *end = '\0';
2616
2617 cnt = mark_printk("%s\n", buf);
2618 kfree(buf);
2619 *fpos += cnt;
2620
2621 return cnt;
2622}
2623
bc0c38d1 2624static struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
2625 .open = tracing_open_generic,
2626 .read = tracing_max_lat_read,
2627 .write = tracing_max_lat_write,
bc0c38d1
SR
2628};
2629
2630static struct file_operations tracing_ctrl_fops = {
4bf39a94
IM
2631 .open = tracing_open_generic,
2632 .read = tracing_ctrl_read,
2633 .write = tracing_ctrl_write,
bc0c38d1
SR
2634};
2635
2636static struct file_operations set_tracer_fops = {
4bf39a94
IM
2637 .open = tracing_open_generic,
2638 .read = tracing_set_trace_read,
2639 .write = tracing_set_trace_write,
bc0c38d1
SR
2640};
2641
b3806b43 2642static struct file_operations tracing_pipe_fops = {
4bf39a94 2643 .open = tracing_open_pipe,
2a2cc8f7 2644 .poll = tracing_poll_pipe,
4bf39a94
IM
2645 .read = tracing_read_pipe,
2646 .release = tracing_release_pipe,
b3806b43
SR
2647};
2648
a98a3c3f
SR
2649static struct file_operations tracing_entries_fops = {
2650 .open = tracing_open_generic,
2651 .read = tracing_entries_read,
2652 .write = tracing_entries_write,
2653};
2654
5bf9a1ee 2655static struct file_operations tracing_mark_fops = {
43a15386 2656 .open = tracing_open_generic,
5bf9a1ee
PP
2657 .write = tracing_mark_write,
2658};
2659
bc0c38d1
SR
2660#ifdef CONFIG_DYNAMIC_FTRACE
2661
b807c3d0
SR
2662int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2663{
2664 return 0;
2665}
2666
bc0c38d1 2667static ssize_t
b807c3d0 2668tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
2669 size_t cnt, loff_t *ppos)
2670{
a26a2a27
SR
2671 static char ftrace_dyn_info_buffer[1024];
2672 static DEFINE_MUTEX(dyn_info_mutex);
bc0c38d1 2673 unsigned long *p = filp->private_data;
b807c3d0 2674 char *buf = ftrace_dyn_info_buffer;
a26a2a27 2675 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
bc0c38d1
SR
2676 int r;
2677
b807c3d0
SR
2678 mutex_lock(&dyn_info_mutex);
2679 r = sprintf(buf, "%ld ", *p);
4bf39a94 2680
a26a2a27 2681 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
b807c3d0
SR
2682 buf[r++] = '\n';
2683
2684 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2685
2686 mutex_unlock(&dyn_info_mutex);
2687
2688 return r;
bc0c38d1
SR
2689}
2690
b807c3d0 2691static struct file_operations tracing_dyn_info_fops = {
4bf39a94 2692 .open = tracing_open_generic,
b807c3d0 2693 .read = tracing_read_dyn_info,
bc0c38d1
SR
2694};
2695#endif
2696
2697static struct dentry *d_tracer;
2698
2699struct dentry *tracing_init_dentry(void)
2700{
2701 static int once;
2702
2703 if (d_tracer)
2704 return d_tracer;
2705
2706 d_tracer = debugfs_create_dir("tracing", NULL);
2707
2708 if (!d_tracer && !once) {
2709 once = 1;
2710 pr_warning("Could not create debugfs directory 'tracing'\n");
2711 return NULL;
2712 }
2713
2714 return d_tracer;
2715}
2716
60a11774
SR
2717#ifdef CONFIG_FTRACE_SELFTEST
2718/* Let selftest have access to static functions in this file */
2719#include "trace_selftest.c"
2720#endif
2721
b5ad384e 2722static __init int tracer_init_debugfs(void)
bc0c38d1
SR
2723{
2724 struct dentry *d_tracer;
2725 struct dentry *entry;
2726
2727 d_tracer = tracing_init_dentry();
2728
2729 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2730 &global_trace, &tracing_ctrl_fops);
2731 if (!entry)
2732 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2733
ee6bce52 2734 entry = debugfs_create_file("trace_options", 0644, d_tracer,
bc0c38d1
SR
2735 NULL, &tracing_iter_fops);
2736 if (!entry)
ee6bce52 2737 pr_warning("Could not create debugfs 'trace_options' entry\n");
bc0c38d1 2738
c7078de1
IM
2739 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2740 NULL, &tracing_cpumask_fops);
2741 if (!entry)
2742 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2743
bc0c38d1
SR
2744 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2745 &global_trace, &tracing_lt_fops);
2746 if (!entry)
2747 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2748
2749 entry = debugfs_create_file("trace", 0444, d_tracer,
2750 &global_trace, &tracing_fops);
2751 if (!entry)
2752 pr_warning("Could not create debugfs 'trace' entry\n");
2753
2754 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2755 &global_trace, &show_traces_fops);
2756 if (!entry)
98a983aa 2757 pr_warning("Could not create debugfs 'available_tracers' entry\n");
bc0c38d1
SR
2758
2759 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2760 &global_trace, &set_tracer_fops);
2761 if (!entry)
98a983aa 2762 pr_warning("Could not create debugfs 'current_tracer' entry\n");
bc0c38d1
SR
2763
2764 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2765 &tracing_max_latency,
2766 &tracing_max_lat_fops);
2767 if (!entry)
2768 pr_warning("Could not create debugfs "
2769 "'tracing_max_latency' entry\n");
2770
2771 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2772 &tracing_thresh, &tracing_max_lat_fops);
2773 if (!entry)
2774 pr_warning("Could not create debugfs "
98a983aa 2775 "'tracing_thresh' entry\n");
7bd2f24c
IM
2776 entry = debugfs_create_file("README", 0644, d_tracer,
2777 NULL, &tracing_readme_fops);
2778 if (!entry)
2779 pr_warning("Could not create debugfs 'README' entry\n");
2780
b3806b43
SR
2781 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2782 NULL, &tracing_pipe_fops);
2783 if (!entry)
2784 pr_warning("Could not create debugfs "
98a983aa 2785 "'trace_pipe' entry\n");
bc0c38d1 2786
a94c80e7 2787 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
a98a3c3f
SR
2788 &global_trace, &tracing_entries_fops);
2789 if (!entry)
2790 pr_warning("Could not create debugfs "
a94c80e7 2791 "'buffer_size_kb' entry\n");
a98a3c3f 2792
5bf9a1ee
PP
2793 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2794 NULL, &tracing_mark_fops);
2795 if (!entry)
2796 pr_warning("Could not create debugfs "
2797 "'trace_marker' entry\n");
2798
bc0c38d1
SR
2799#ifdef CONFIG_DYNAMIC_FTRACE
2800 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2801 &ftrace_update_tot_cnt,
b807c3d0 2802 &tracing_dyn_info_fops);
bc0c38d1
SR
2803 if (!entry)
2804 pr_warning("Could not create debugfs "
2805 "'dyn_ftrace_total_info' entry\n");
2806#endif
d618b3e6
IM
2807#ifdef CONFIG_SYSPROF_TRACER
2808 init_tracer_sysprof_debugfs(d_tracer);
2809#endif
b5ad384e 2810 return 0;
bc0c38d1
SR
2811}
2812
1fd8f2a3 2813int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
dd0e545f 2814{
dd0e545f
SR
2815 static DEFINE_SPINLOCK(trace_buf_lock);
2816 static char trace_buf[TRACE_BUF_SIZE];
f09ce573 2817
3928a8a2 2818 struct ring_buffer_event *event;
f09ce573 2819 struct trace_array *tr = &global_trace;
dd0e545f 2820 struct trace_array_cpu *data;
38697053 2821 int cpu, len = 0, size, pc;
e726f5f9
IM
2822 struct print_entry *entry;
2823 unsigned long irq_flags;
dd0e545f 2824
8e1b82e0 2825 if (tracing_disabled || tracing_selftest_running)
dd0e545f
SR
2826 return 0;
2827
38697053
SR
2828 pc = preempt_count();
2829 preempt_disable_notrace();
dd0e545f
SR
2830 cpu = raw_smp_processor_id();
2831 data = tr->data[cpu];
dd0e545f 2832
3ea2e6d7 2833 if (unlikely(atomic_read(&data->disabled)))
dd0e545f
SR
2834 goto out;
2835
380c4b14
FW
2836 pause_graph_tracing();
2837 spin_lock_irqsave(&trace_buf_lock, irq_flags);
801fe400 2838 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
dd0e545f
SR
2839
2840 len = min(len, TRACE_BUF_SIZE-1);
2841 trace_buf[len] = 0;
2842
777e208d
SR
2843 size = sizeof(*entry) + len + 1;
2844 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3928a8a2
SR
2845 if (!event)
2846 goto out_unlock;
777e208d 2847 entry = ring_buffer_event_data(event);
e726f5f9 2848 tracing_generic_entry_update(&entry->ent, irq_flags, pc);
777e208d
SR
2849 entry->ent.type = TRACE_PRINT;
2850 entry->ip = ip;
1fd8f2a3 2851 entry->depth = depth;
dd0e545f 2852
777e208d
SR
2853 memcpy(&entry->buf, trace_buf, len);
2854 entry->buf[len] = 0;
3928a8a2 2855 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
dd0e545f 2856
3928a8a2 2857 out_unlock:
380c4b14
FW
2858 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
2859 unpause_graph_tracing();
dd0e545f 2860 out:
38697053 2861 preempt_enable_notrace();
dd0e545f
SR
2862
2863 return len;
2864}
801fe400
PP
2865EXPORT_SYMBOL_GPL(trace_vprintk);
2866
2867int __ftrace_printk(unsigned long ip, const char *fmt, ...)
2868{
2869 int ret;
2870 va_list ap;
2871
2872 if (!(trace_flags & TRACE_ITER_PRINTK))
2873 return 0;
2874
2875 va_start(ap, fmt);
21a8c466 2876 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
801fe400
PP
2877 va_end(ap);
2878 return ret;
2879}
dd0e545f
SR
2880EXPORT_SYMBOL_GPL(__ftrace_printk);
2881
9011262a
ACM
2882int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
2883{
2884 if (!(trace_flags & TRACE_ITER_PRINTK))
2885 return 0;
2886
2887 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
2888}
2889EXPORT_SYMBOL_GPL(__ftrace_vprintk);
2890
3f5a54e3
SR
2891static int trace_panic_handler(struct notifier_block *this,
2892 unsigned long event, void *unused)
2893{
944ac425
SR
2894 if (ftrace_dump_on_oops)
2895 ftrace_dump();
3f5a54e3
SR
2896 return NOTIFY_OK;
2897}
2898
2899static struct notifier_block trace_panic_notifier = {
2900 .notifier_call = trace_panic_handler,
2901 .next = NULL,
2902 .priority = 150 /* priority: INT_MAX >= x >= 0 */
2903};
2904
2905static int trace_die_handler(struct notifier_block *self,
2906 unsigned long val,
2907 void *data)
2908{
2909 switch (val) {
2910 case DIE_OOPS:
944ac425
SR
2911 if (ftrace_dump_on_oops)
2912 ftrace_dump();
3f5a54e3
SR
2913 break;
2914 default:
2915 break;
2916 }
2917 return NOTIFY_OK;
2918}
2919
2920static struct notifier_block trace_die_notifier = {
2921 .notifier_call = trace_die_handler,
2922 .priority = 200
2923};
2924
2925/*
2926 * printk is set to max of 1024, we really don't need it that big.
2927 * Nothing should be printing 1000 characters anyway.
2928 */
2929#define TRACE_MAX_PRINT 1000
2930
2931/*
2932 * Define here KERN_TRACE so that we have one place to modify
2933 * it if we decide to change what log level the ftrace dump
2934 * should be at.
2935 */
428aee14 2936#define KERN_TRACE KERN_EMERG
3f5a54e3
SR
2937
2938static void
2939trace_printk_seq(struct trace_seq *s)
2940{
2941 /* Probably should print a warning here. */
2942 if (s->len >= 1000)
2943 s->len = 1000;
2944
2945 /* should be zero ended, but we are paranoid. */
2946 s->buffer[s->len] = 0;
2947
2948 printk(KERN_TRACE "%s", s->buffer);
2949
2950 trace_seq_reset(s);
2951}
2952
3f5a54e3
SR
2953void ftrace_dump(void)
2954{
2955 static DEFINE_SPINLOCK(ftrace_dump_lock);
2956 /* use static because iter can be a bit big for the stack */
2957 static struct trace_iterator iter;
3f5a54e3 2958 static int dump_ran;
d769041f
SR
2959 unsigned long flags;
2960 int cnt = 0, cpu;
3f5a54e3
SR
2961
2962 /* only one dump */
2963 spin_lock_irqsave(&ftrace_dump_lock, flags);
2964 if (dump_ran)
2965 goto out;
2966
2967 dump_ran = 1;
2968
2969 /* No turning back! */
0ee6b6cf 2970 tracing_off();
81adbdc0 2971 ftrace_kill();
3f5a54e3 2972
d769041f
SR
2973 for_each_tracing_cpu(cpu) {
2974 atomic_inc(&global_trace.data[cpu]->disabled);
2975 }
2976
b54d3de9
TE
2977 /* don't look at user memory in panic mode */
2978 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
2979
3f5a54e3
SR
2980 printk(KERN_TRACE "Dumping ftrace buffer:\n");
2981
2982 iter.tr = &global_trace;
2983 iter.trace = current_trace;
2984
2985 /*
2986 * We need to stop all tracing on all CPUS to read the
2987 * the next buffer. This is a bit expensive, but is
2988 * not done often. We fill all what we can read,
2989 * and then release the locks again.
2990 */
2991
3f5a54e3
SR
2992 while (!trace_empty(&iter)) {
2993
2994 if (!cnt)
2995 printk(KERN_TRACE "---------------------------------\n");
2996
2997 cnt++;
2998
2999 /* reset all but tr, trace, and overruns */
3000 memset(&iter.seq, 0,
3001 sizeof(struct trace_iterator) -
3002 offsetof(struct trace_iterator, seq));
3003 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3004 iter.pos = -1;
3005
3006 if (find_next_entry_inc(&iter) != NULL) {
3007 print_trace_line(&iter);
3008 trace_consume(&iter);
3009 }
3010
3011 trace_printk_seq(&iter.seq);
3012 }
3013
3014 if (!cnt)
3015 printk(KERN_TRACE " (ftrace buffer empty)\n");
3016 else
3017 printk(KERN_TRACE "---------------------------------\n");
3018
3f5a54e3
SR
3019 out:
3020 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3021}
3022
3928a8a2 3023__init static int tracer_alloc_buffers(void)
bc0c38d1 3024{
4c11d7ae 3025 struct trace_array_cpu *data;
4c11d7ae 3026 int i;
9e01c1b7 3027 int ret = -ENOMEM;
4c11d7ae 3028
9e01c1b7
RR
3029 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3030 goto out;
3031
3032 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3033 goto out_free_buffer_mask;
4c11d7ae 3034
9e01c1b7
RR
3035 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3036 cpumask_copy(tracing_cpumask, cpu_all_mask);
3037
3038 /* TODO: make the number of buffers hot pluggable with CPUS */
3928a8a2
SR
3039 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3040 TRACE_BUFFER_FLAGS);
3041 if (!global_trace.buffer) {
3042 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3043 WARN_ON(1);
9e01c1b7 3044 goto out_free_cpumask;
4c11d7ae 3045 }
3928a8a2 3046 global_trace.entries = ring_buffer_size(global_trace.buffer);
4c11d7ae 3047
9e01c1b7 3048
4c11d7ae 3049#ifdef CONFIG_TRACER_MAX_TRACE
3928a8a2
SR
3050 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3051 TRACE_BUFFER_FLAGS);
3052 if (!max_tr.buffer) {
3053 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3054 WARN_ON(1);
3055 ring_buffer_free(global_trace.buffer);
9e01c1b7 3056 goto out_free_cpumask;
4c11d7ae 3057 }
3928a8a2
SR
3058 max_tr.entries = ring_buffer_size(max_tr.buffer);
3059 WARN_ON(max_tr.entries != global_trace.entries);
a98a3c3f 3060#endif
ab46428c 3061
4c11d7ae 3062 /* Allocate the first page for all buffers */
ab46428c 3063 for_each_tracing_cpu(i) {
4c11d7ae 3064 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
bc0c38d1 3065 max_tr.data[i] = &per_cpu(max_data, i);
4c11d7ae 3066 }
bc0c38d1 3067
bc0c38d1
SR
3068 trace_init_cmdlines();
3069
43a15386 3070 register_tracer(&nop_trace);
79fb0768 3071 current_trace = &nop_trace;
b5ad384e
FW
3072#ifdef CONFIG_BOOT_TRACER
3073 register_tracer(&boot_tracer);
b5ad384e 3074#endif
60a11774
SR
3075 /* All seems OK, enable tracing */
3076 tracing_disabled = 0;
3928a8a2 3077
3f5a54e3
SR
3078 atomic_notifier_chain_register(&panic_notifier_list,
3079 &trace_panic_notifier);
3080
3081 register_die_notifier(&trace_die_notifier);
9e01c1b7 3082 ret = 0;
3f5a54e3 3083
9e01c1b7
RR
3084out_free_cpumask:
3085 free_cpumask_var(tracing_cpumask);
3086out_free_buffer_mask:
3087 free_cpumask_var(tracing_buffer_mask);
3088out:
3089 return ret;
bc0c38d1 3090}
b2821ae6
SR
3091
3092__init static int clear_boot_tracer(void)
3093{
3094 /*
3095 * The default tracer at boot buffer is an init section.
3096 * This function is called in lateinit. If we did not
3097 * find the boot tracer, then clear it out, to prevent
3098 * later registration from accessing the buffer that is
3099 * about to be freed.
3100 */
3101 if (!default_bootup_tracer)
3102 return 0;
3103
3104 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
3105 default_bootup_tracer);
3106 default_bootup_tracer = NULL;
3107
3108 return 0;
3109}
3110
b5ad384e
FW
3111early_initcall(tracer_alloc_buffers);
3112fs_initcall(tracer_init_debugfs);
b2821ae6 3113late_initcall(clear_boot_tracer);
This page took 0.318894 seconds and 5 git commands to generate.