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