]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/trace/trace.c
rcu: Track idleness independent of idle tasks
[mirror_ubuntu-jammy-kernel.git] / kernel / trace / trace.c
CommitLineData
bc0c38d1
SR
1/*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
13 */
2cadf913 14#include <linux/ring_buffer.h>
273b281f 15#include <generated/utsrelease.h>
2cadf913
SR
16#include <linux/stacktrace.h>
17#include <linux/writeback.h>
bc0c38d1
SR
18#include <linux/kallsyms.h>
19#include <linux/seq_file.h>
3f5a54e3 20#include <linux/notifier.h>
2cadf913 21#include <linux/irqflags.h>
bc0c38d1 22#include <linux/debugfs.h>
4c11d7ae 23#include <linux/pagemap.h>
bc0c38d1
SR
24#include <linux/hardirq.h>
25#include <linux/linkage.h>
26#include <linux/uaccess.h>
2cadf913 27#include <linux/kprobes.h>
bc0c38d1
SR
28#include <linux/ftrace.h>
29#include <linux/module.h>
30#include <linux/percpu.h>
2cadf913 31#include <linux/splice.h>
3f5a54e3 32#include <linux/kdebug.h>
5f0c6c03 33#include <linux/string.h>
7e53bd42 34#include <linux/rwsem.h>
5a0e3ad6 35#include <linux/slab.h>
bc0c38d1
SR
36#include <linux/ctype.h>
37#include <linux/init.h>
2a2cc8f7 38#include <linux/poll.h>
bc0c38d1 39#include <linux/fs.h>
86387f7e 40
bc0c38d1 41#include "trace.h"
f0868d1e 42#include "trace_output.h"
bc0c38d1 43
73c5162a
SR
44/*
45 * On boot up, the ring buffer is set to the minimum size, so that
46 * we do not waste memory on systems that are not using tracing.
47 */
020e5f85 48int ring_buffer_expanded;
73c5162a 49
8e1b82e0
FW
50/*
51 * We need to change this state when a selftest is running.
ff32504f
FW
52 * A selftest will lurk into the ring-buffer to count the
53 * entries inserted during the selftest although some concurrent
5e1607a0 54 * insertions into the ring-buffer such as trace_printk could occurred
ff32504f
FW
55 * at the same time, giving false positive or negative results.
56 */
8e1b82e0 57static bool __read_mostly tracing_selftest_running;
ff32504f 58
b2821ae6
SR
59/*
60 * If a tracer is running, we do not want to run SELFTEST.
61 */
020e5f85 62bool __read_mostly tracing_selftest_disabled;
b2821ae6 63
adf9f195
FW
64/* For tracers that don't implement custom flags */
65static struct tracer_opt dummy_tracer_opt[] = {
66 { }
67};
68
69static struct tracer_flags dummy_tracer_flags = {
70 .val = 0,
71 .opts = dummy_tracer_opt
72};
73
74static int dummy_set_flag(u32 old_flags, u32 bit, int set)
75{
76 return 0;
77}
0f048701
SR
78
79/*
80 * Kill all tracing for good (never come back).
81 * It is initialized to 1 but will turn to zero if the initialization
82 * of the tracer is successful. But that is the only place that sets
83 * this back to zero.
84 */
4fd27358 85static int tracing_disabled = 1;
0f048701 86
9288f99a 87DEFINE_PER_CPU(int, ftrace_cpu_disabled);
d769041f
SR
88
89static inline void ftrace_disable_cpu(void)
90{
91 preempt_disable();
dd17c8f7 92 __this_cpu_inc(ftrace_cpu_disabled);
d769041f
SR
93}
94
95static inline void ftrace_enable_cpu(void)
96{
dd17c8f7 97 __this_cpu_dec(ftrace_cpu_disabled);
d769041f
SR
98 preempt_enable();
99}
100
955b61e5 101cpumask_var_t __read_mostly 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
cecbca96
FW
114 * /proc/sys/kernel/ftrace_dump_on_oops
115 * Set 1 if you want to dump buffers of all CPUs
116 * Set 2 if you want to dump the buffer of the CPU that triggered oops
944ac425 117 */
cecbca96
FW
118
119enum ftrace_dump_mode ftrace_dump_on_oops;
944ac425 120
b2821ae6
SR
121static int tracing_set_tracer(const char *buf);
122
ee6c2c1b
LZ
123#define MAX_TRACER_SIZE 100
124static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
b2821ae6 125static char *default_bootup_tracer;
d9e54076 126
1beee96b 127static int __init set_cmdline_ftrace(char *str)
d9e54076 128{
ee6c2c1b 129 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
b2821ae6 130 default_bootup_tracer = bootup_tracer_buf;
73c5162a
SR
131 /* We are using ftrace early, expand it */
132 ring_buffer_expanded = 1;
d9e54076
PZ
133 return 1;
134}
1beee96b 135__setup("ftrace=", set_cmdline_ftrace);
d9e54076 136
944ac425
SR
137static int __init set_ftrace_dump_on_oops(char *str)
138{
cecbca96
FW
139 if (*str++ != '=' || !*str) {
140 ftrace_dump_on_oops = DUMP_ALL;
141 return 1;
142 }
143
144 if (!strcmp("orig_cpu", str)) {
145 ftrace_dump_on_oops = DUMP_ORIG;
146 return 1;
147 }
148
149 return 0;
944ac425
SR
150}
151__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
60a11774 152
cf8e3474 153unsigned long long ns2usecs(cycle_t nsec)
bc0c38d1
SR
154{
155 nsec += 500;
156 do_div(nsec, 1000);
157 return nsec;
158}
159
4fcdae83
SR
160/*
161 * The global_trace is the descriptor that holds the tracing
162 * buffers for the live tracing. For each CPU, it contains
163 * a link list of pages that will store trace entries. The
164 * page descriptor of the pages in the memory is used to hold
165 * the link list by linking the lru item in the page descriptor
166 * to each of the pages in the buffer per CPU.
167 *
168 * For each active CPU there is a data field that holds the
169 * pages for the buffer for that CPU. Each CPU has the same number
170 * of pages allocated for its buffer.
171 */
bc0c38d1
SR
172static struct trace_array global_trace;
173
174static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
175
e77405ad
SR
176int filter_current_check_discard(struct ring_buffer *buffer,
177 struct ftrace_event_call *call, void *rec,
eb02ce01
TZ
178 struct ring_buffer_event *event)
179{
e77405ad 180 return filter_check_discard(call, rec, buffer, event);
eb02ce01 181}
17c873ec 182EXPORT_SYMBOL_GPL(filter_current_check_discard);
eb02ce01 183
37886f6a
SR
184cycle_t ftrace_now(int cpu)
185{
186 u64 ts;
187
188 /* Early boot up does not have a buffer yet */
189 if (!global_trace.buffer)
190 return trace_clock_local();
191
192 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
193 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
194
195 return ts;
196}
bc0c38d1 197
4fcdae83
SR
198/*
199 * The max_tr is used to snapshot the global_trace when a maximum
200 * latency is reached. Some tracers will use this to store a maximum
201 * trace while it continues examining live traces.
202 *
203 * The buffers for the max_tr are set up the same as the global_trace.
204 * When a snapshot is taken, the link list of the max_tr is swapped
205 * with the link list of the global_trace and the buffers are reset for
206 * the global_trace so the tracing can continue.
207 */
bc0c38d1
SR
208static struct trace_array max_tr;
209
9705f69e 210static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
bc0c38d1 211
4fcdae83 212/* tracer_enabled is used to toggle activation of a tracer */
26994ead 213static int tracer_enabled = 1;
4fcdae83 214
9036990d
SR
215/**
216 * tracing_is_enabled - return tracer_enabled status
217 *
218 * This function is used by other tracers to know the status
219 * of the tracer_enabled flag. Tracers may use this function
220 * to know if it should enable their features when starting
221 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
222 */
223int tracing_is_enabled(void)
224{
225 return tracer_enabled;
226}
227
4fcdae83 228/*
3928a8a2
SR
229 * trace_buf_size is the size in bytes that is allocated
230 * for a buffer. Note, the number of bytes is always rounded
231 * to page size.
3f5a54e3
SR
232 *
233 * This number is purposely set to a low number of 16384.
234 * If the dump on oops happens, it will be much appreciated
235 * to not have to wait for all that output. Anyway this can be
236 * boot time and run time configurable.
4fcdae83 237 */
3928a8a2 238#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
3f5a54e3 239
3928a8a2 240static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
bc0c38d1 241
4fcdae83 242/* trace_types holds a link list of available tracers. */
bc0c38d1 243static struct tracer *trace_types __read_mostly;
4fcdae83
SR
244
245/* current_trace points to the tracer that is currently active */
bc0c38d1 246static struct tracer *current_trace __read_mostly;
4fcdae83 247
4fcdae83
SR
248/*
249 * trace_types_lock is used to protect the trace_types list.
4fcdae83 250 */
bc0c38d1 251static DEFINE_MUTEX(trace_types_lock);
4fcdae83 252
7e53bd42
LJ
253/*
254 * serialize the access of the ring buffer
255 *
256 * ring buffer serializes readers, but it is low level protection.
257 * The validity of the events (which returns by ring_buffer_peek() ..etc)
258 * are not protected by ring buffer.
259 *
260 * The content of events may become garbage if we allow other process consumes
261 * these events concurrently:
262 * A) the page of the consumed events may become a normal page
263 * (not reader page) in ring buffer, and this page will be rewrited
264 * by events producer.
265 * B) The page of the consumed events may become a page for splice_read,
266 * and this page will be returned to system.
267 *
268 * These primitives allow multi process access to different cpu ring buffer
269 * concurrently.
270 *
271 * These primitives don't distinguish read-only and read-consume access.
272 * Multi read-only access are also serialized.
273 */
274
275#ifdef CONFIG_SMP
276static DECLARE_RWSEM(all_cpu_access_lock);
277static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
278
279static inline void trace_access_lock(int cpu)
280{
281 if (cpu == TRACE_PIPE_ALL_CPU) {
282 /* gain it for accessing the whole ring buffer. */
283 down_write(&all_cpu_access_lock);
284 } else {
285 /* gain it for accessing a cpu ring buffer. */
286
287 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
288 down_read(&all_cpu_access_lock);
289
290 /* Secondly block other access to this @cpu ring buffer. */
291 mutex_lock(&per_cpu(cpu_access_lock, cpu));
292 }
293}
294
295static inline void trace_access_unlock(int cpu)
296{
297 if (cpu == TRACE_PIPE_ALL_CPU) {
298 up_write(&all_cpu_access_lock);
299 } else {
300 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
301 up_read(&all_cpu_access_lock);
302 }
303}
304
305static inline void trace_access_lock_init(void)
306{
307 int cpu;
308
309 for_each_possible_cpu(cpu)
310 mutex_init(&per_cpu(cpu_access_lock, cpu));
311}
312
313#else
314
315static DEFINE_MUTEX(access_lock);
316
317static inline void trace_access_lock(int cpu)
318{
319 (void)cpu;
320 mutex_lock(&access_lock);
321}
322
323static inline void trace_access_unlock(int cpu)
324{
325 (void)cpu;
326 mutex_unlock(&access_lock);
327}
328
329static inline void trace_access_lock_init(void)
330{
331}
332
333#endif
334
4fcdae83 335/* trace_wait is a waitqueue for tasks blocked on trace_poll */
4e655519
IM
336static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
337
ee6bce52 338/* trace_flags holds trace_options default values */
12ef7d44 339unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
a2a16d6a 340 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
750912fa 341 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE;
4e655519 342
b8de7bd1 343static int trace_stop_count;
5389f6fa 344static DEFINE_RAW_SPINLOCK(tracing_start_lock);
b8de7bd1 345
e7e2ee89
VN
346static void wakeup_work_handler(struct work_struct *work)
347{
348 wake_up(&trace_wait);
349}
350
351static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
352
4fcdae83
SR
353/**
354 * trace_wake_up - wake up tasks waiting for trace input
355 *
e7e2ee89
VN
356 * Schedules a delayed work to wake up any task that is blocked on the
357 * trace_wait queue. These is used with trace_poll for tasks polling the
358 * trace.
4fcdae83 359 */
4e655519
IM
360void trace_wake_up(void)
361{
e7e2ee89 362 const unsigned long delay = msecs_to_jiffies(2);
89f19f04
AM
363
364 if (trace_flags & TRACE_ITER_BLOCK)
365 return;
e7e2ee89 366 schedule_delayed_work(&wakeup_work, delay);
4e655519 367}
bc0c38d1 368
3928a8a2 369static int __init set_buf_size(char *str)
bc0c38d1 370{
3928a8a2 371 unsigned long buf_size;
c6caeeb1 372
bc0c38d1
SR
373 if (!str)
374 return 0;
9d612bef 375 buf_size = memparse(str, &str);
c6caeeb1 376 /* nr_entries can not be zero */
9d612bef 377 if (buf_size == 0)
c6caeeb1 378 return 0;
3928a8a2 379 trace_buf_size = buf_size;
bc0c38d1
SR
380 return 1;
381}
3928a8a2 382__setup("trace_buf_size=", set_buf_size);
bc0c38d1 383
0e950173
TB
384static int __init set_tracing_thresh(char *str)
385{
386 unsigned long threshhold;
387 int ret;
388
389 if (!str)
390 return 0;
391 ret = strict_strtoul(str, 0, &threshhold);
392 if (ret < 0)
393 return 0;
394 tracing_thresh = threshhold * 1000;
395 return 1;
396}
397__setup("tracing_thresh=", set_tracing_thresh);
398
57f50be1
SR
399unsigned long nsecs_to_usecs(unsigned long nsecs)
400{
401 return nsecs / 1000;
402}
403
4fcdae83 404/* These must match the bit postions in trace_iterator_flags */
bc0c38d1
SR
405static const char *trace_options[] = {
406 "print-parent",
407 "sym-offset",
408 "sym-addr",
409 "verbose",
f9896bf3 410 "raw",
5e3ca0ec 411 "hex",
cb0f12aa 412 "bin",
2a2cc8f7 413 "block",
86387f7e 414 "stacktrace",
5e1607a0 415 "trace_printk",
b2a866f9 416 "ftrace_preempt",
9f029e83 417 "branch",
12ef7d44 418 "annotate",
02b67518 419 "userstacktrace",
b54d3de9 420 "sym-userobj",
66896a85 421 "printk-msg-only",
c4a8e8be 422 "context-info",
c032ef64 423 "latency-format",
be6f164a 424 "sleep-time",
a2a16d6a 425 "graph-time",
e870e9a1 426 "record-cmd",
750912fa 427 "overwrite",
cf30cf67 428 "disable_on_free",
bc0c38d1
SR
429 NULL
430};
431
5079f326
Z
432static struct {
433 u64 (*func)(void);
434 const char *name;
435} trace_clocks[] = {
436 { trace_clock_local, "local" },
437 { trace_clock_global, "global" },
6249687f 438 { trace_clock_counter, "counter" },
5079f326
Z
439};
440
441int trace_clock_id;
442
b63f39ea 443/*
444 * trace_parser_get_init - gets the buffer for trace parser
445 */
446int trace_parser_get_init(struct trace_parser *parser, int size)
447{
448 memset(parser, 0, sizeof(*parser));
449
450 parser->buffer = kmalloc(size, GFP_KERNEL);
451 if (!parser->buffer)
452 return 1;
453
454 parser->size = size;
455 return 0;
456}
457
458/*
459 * trace_parser_put - frees the buffer for trace parser
460 */
461void trace_parser_put(struct trace_parser *parser)
462{
463 kfree(parser->buffer);
464}
465
466/*
467 * trace_get_user - reads the user input string separated by space
468 * (matched by isspace(ch))
469 *
470 * For each string found the 'struct trace_parser' is updated,
471 * and the function returns.
472 *
473 * Returns number of bytes read.
474 *
475 * See kernel/trace/trace.h for 'struct trace_parser' details.
476 */
477int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
478 size_t cnt, loff_t *ppos)
479{
480 char ch;
481 size_t read = 0;
482 ssize_t ret;
483
484 if (!*ppos)
485 trace_parser_clear(parser);
486
487 ret = get_user(ch, ubuf++);
488 if (ret)
489 goto out;
490
491 read++;
492 cnt--;
493
494 /*
495 * The parser is not finished with the last write,
496 * continue reading the user input without skipping spaces.
497 */
498 if (!parser->cont) {
499 /* skip white space */
500 while (cnt && isspace(ch)) {
501 ret = get_user(ch, ubuf++);
502 if (ret)
503 goto out;
504 read++;
505 cnt--;
506 }
507
508 /* only spaces were written */
509 if (isspace(ch)) {
510 *ppos += read;
511 ret = read;
512 goto out;
513 }
514
515 parser->idx = 0;
516 }
517
518 /* read the non-space input */
519 while (cnt && !isspace(ch)) {
3c235a33 520 if (parser->idx < parser->size - 1)
b63f39ea 521 parser->buffer[parser->idx++] = ch;
522 else {
523 ret = -EINVAL;
524 goto out;
525 }
526 ret = get_user(ch, ubuf++);
527 if (ret)
528 goto out;
529 read++;
530 cnt--;
531 }
532
533 /* We either got finished input or we have to wait for another call. */
534 if (isspace(ch)) {
535 parser->buffer[parser->idx] = 0;
536 parser->cont = false;
537 } else {
538 parser->cont = true;
539 parser->buffer[parser->idx++] = ch;
540 }
541
542 *ppos += read;
543 ret = read;
544
545out:
546 return ret;
547}
548
6c6c2796
PP
549ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
550{
551 int len;
552 int ret;
553
2dc5d12b
SR
554 if (!cnt)
555 return 0;
556
6c6c2796
PP
557 if (s->len <= s->readpos)
558 return -EBUSY;
559
560 len = s->len - s->readpos;
561 if (cnt > len)
562 cnt = len;
563 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
2dc5d12b 564 if (ret == cnt)
6c6c2796
PP
565 return -EFAULT;
566
2dc5d12b
SR
567 cnt -= ret;
568
e74da523 569 s->readpos += cnt;
6c6c2796 570 return cnt;
214023c3
SR
571}
572
b8b94265 573static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
3c56819b
EGM
574{
575 int len;
576 void *ret;
577
578 if (s->len <= s->readpos)
579 return -EBUSY;
580
581 len = s->len - s->readpos;
582 if (cnt > len)
583 cnt = len;
584 ret = memcpy(buf, s->buffer + s->readpos, cnt);
585 if (!ret)
586 return -EFAULT;
587
e74da523 588 s->readpos += cnt;
3c56819b
EGM
589 return cnt;
590}
591
5d4a9dba
SR
592/*
593 * ftrace_max_lock is used to protect the swapping of buffers
594 * when taking a max snapshot. The buffers themselves are
595 * protected by per_cpu spinlocks. But the action of the swap
596 * needs its own lock.
597 *
445c8951 598 * This is defined as a arch_spinlock_t in order to help
5d4a9dba
SR
599 * with performance when lockdep debugging is enabled.
600 *
601 * It is also used in other places outside the update_max_tr
602 * so it needs to be defined outside of the
603 * CONFIG_TRACER_MAX_TRACE.
604 */
445c8951 605static arch_spinlock_t ftrace_max_lock =
edc35bd7 606 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5d4a9dba 607
0e950173
TB
608unsigned long __read_mostly tracing_thresh;
609
5d4a9dba
SR
610#ifdef CONFIG_TRACER_MAX_TRACE
611unsigned long __read_mostly tracing_max_latency;
5d4a9dba
SR
612
613/*
614 * Copy the new maximum trace into the separate maximum-trace
615 * structure. (this way the maximum trace is permanently saved,
616 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
617 */
618static void
619__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
620{
621 struct trace_array_cpu *data = tr->data[cpu];
1acaa1b2 622 struct trace_array_cpu *max_data;
5d4a9dba
SR
623
624 max_tr.cpu = cpu;
625 max_tr.time_start = data->preempt_timestamp;
626
8248ac05
SR
627 max_data = max_tr.data[cpu];
628 max_data->saved_latency = tracing_max_latency;
629 max_data->critical_start = data->critical_start;
630 max_data->critical_end = data->critical_end;
5d4a9dba 631
1acaa1b2 632 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
8248ac05
SR
633 max_data->pid = tsk->pid;
634 max_data->uid = task_uid(tsk);
635 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
636 max_data->policy = tsk->policy;
637 max_data->rt_priority = tsk->rt_priority;
5d4a9dba
SR
638
639 /* record this tasks comm */
640 tracing_record_cmdline(tsk);
641}
642
4fcdae83
SR
643/**
644 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
645 * @tr: tracer
646 * @tsk: the task with the latency
647 * @cpu: The cpu that initiated the trace.
648 *
649 * Flip the buffers between the @tr and the max_tr and record information
650 * about which task was the cause of this latency.
651 */
e309b41d 652void
bc0c38d1
SR
653update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
654{
3928a8a2 655 struct ring_buffer *buf = tr->buffer;
bc0c38d1 656
b8de7bd1
SR
657 if (trace_stop_count)
658 return;
659
4c11d7ae 660 WARN_ON_ONCE(!irqs_disabled());
ef710e10
KM
661 if (!current_trace->use_max_tr) {
662 WARN_ON_ONCE(1);
663 return;
664 }
0199c4e6 665 arch_spin_lock(&ftrace_max_lock);
3928a8a2
SR
666
667 tr->buffer = max_tr.buffer;
668 max_tr.buffer = buf;
669
bc0c38d1 670 __update_max_tr(tr, tsk, cpu);
0199c4e6 671 arch_spin_unlock(&ftrace_max_lock);
bc0c38d1
SR
672}
673
674/**
675 * update_max_tr_single - only copy one trace over, and reset the rest
676 * @tr - tracer
677 * @tsk - task with the latency
678 * @cpu - the cpu of the buffer to copy.
4fcdae83
SR
679 *
680 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
bc0c38d1 681 */
e309b41d 682void
bc0c38d1
SR
683update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
684{
3928a8a2 685 int ret;
bc0c38d1 686
b8de7bd1
SR
687 if (trace_stop_count)
688 return;
689
4c11d7ae 690 WARN_ON_ONCE(!irqs_disabled());
ef710e10
KM
691 if (!current_trace->use_max_tr) {
692 WARN_ON_ONCE(1);
693 return;
694 }
695
0199c4e6 696 arch_spin_lock(&ftrace_max_lock);
bc0c38d1 697
d769041f
SR
698 ftrace_disable_cpu();
699
3928a8a2
SR
700 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
701
e8165dbb
SR
702 if (ret == -EBUSY) {
703 /*
704 * We failed to swap the buffer due to a commit taking
705 * place on this CPU. We fail to record, but we reset
706 * the max trace buffer (no one writes directly to it)
707 * and flag that it failed.
708 */
709 trace_array_printk(&max_tr, _THIS_IP_,
710 "Failed to swap buffers due to commit in progress\n");
711 }
712
d769041f
SR
713 ftrace_enable_cpu();
714
e8165dbb 715 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
bc0c38d1
SR
716
717 __update_max_tr(tr, tsk, cpu);
0199c4e6 718 arch_spin_unlock(&ftrace_max_lock);
bc0c38d1 719}
5d4a9dba 720#endif /* CONFIG_TRACER_MAX_TRACE */
bc0c38d1 721
4fcdae83
SR
722/**
723 * register_tracer - register a tracer with the ftrace system.
724 * @type - the plugin for the tracer
725 *
726 * Register a new plugin tracer.
727 */
bc0c38d1 728int register_tracer(struct tracer *type)
e7669b8e
HE
729__releases(kernel_lock)
730__acquires(kernel_lock)
bc0c38d1
SR
731{
732 struct tracer *t;
bc0c38d1
SR
733 int ret = 0;
734
735 if (!type->name) {
736 pr_info("Tracer must have a name\n");
737 return -1;
738 }
739
24a461d5 740 if (strlen(type->name) >= MAX_TRACER_SIZE) {
ee6c2c1b
LZ
741 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
742 return -1;
743 }
744
bc0c38d1 745 mutex_lock(&trace_types_lock);
86fa2f60 746
8e1b82e0
FW
747 tracing_selftest_running = true;
748
bc0c38d1
SR
749 for (t = trace_types; t; t = t->next) {
750 if (strcmp(type->name, t->name) == 0) {
751 /* already found */
ee6c2c1b 752 pr_info("Tracer %s already registered\n",
bc0c38d1
SR
753 type->name);
754 ret = -1;
755 goto out;
756 }
757 }
758
adf9f195
FW
759 if (!type->set_flag)
760 type->set_flag = &dummy_set_flag;
761 if (!type->flags)
762 type->flags = &dummy_tracer_flags;
763 else
764 if (!type->flags->opts)
765 type->flags->opts = dummy_tracer_opt;
6eaaa5d5
FW
766 if (!type->wait_pipe)
767 type->wait_pipe = default_wait_pipe;
768
adf9f195 769
60a11774 770#ifdef CONFIG_FTRACE_STARTUP_TEST
b2821ae6 771 if (type->selftest && !tracing_selftest_disabled) {
60a11774 772 struct tracer *saved_tracer = current_trace;
60a11774 773 struct trace_array *tr = &global_trace;
ff32504f 774
60a11774
SR
775 /*
776 * Run a selftest on this tracer.
777 * Here we reset the trace buffer, and set the current
778 * tracer to be this tracer. The tracer can then run some
779 * internal tracing to verify that everything is in order.
780 * If we fail, we do not register this tracer.
781 */
76f0d073 782 tracing_reset_online_cpus(tr);
86fa2f60 783
60a11774 784 current_trace = type;
4a0b1665
SR
785
786 /* If we expanded the buffers, make sure the max is expanded too */
787 if (ring_buffer_expanded && type->use_max_tr)
788 ring_buffer_resize(max_tr.buffer, trace_buf_size);
789
60a11774
SR
790 /* the test is responsible for initializing and enabling */
791 pr_info("Testing tracer %s: ", type->name);
792 ret = type->selftest(type, tr);
793 /* the test is responsible for resetting too */
794 current_trace = saved_tracer;
60a11774
SR
795 if (ret) {
796 printk(KERN_CONT "FAILED!\n");
797 goto out;
798 }
1d4db00a 799 /* Only reset on passing, to avoid touching corrupted buffers */
76f0d073 800 tracing_reset_online_cpus(tr);
86fa2f60 801
4a0b1665
SR
802 /* Shrink the max buffer again */
803 if (ring_buffer_expanded && type->use_max_tr)
804 ring_buffer_resize(max_tr.buffer, 1);
805
60a11774
SR
806 printk(KERN_CONT "PASSED\n");
807 }
808#endif
809
bc0c38d1
SR
810 type->next = trace_types;
811 trace_types = type;
60a11774 812
bc0c38d1 813 out:
8e1b82e0 814 tracing_selftest_running = false;
bc0c38d1
SR
815 mutex_unlock(&trace_types_lock);
816
dac74940
SR
817 if (ret || !default_bootup_tracer)
818 goto out_unlock;
819
ee6c2c1b 820 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
dac74940
SR
821 goto out_unlock;
822
823 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
824 /* Do we want this tracer to start on bootup? */
825 tracing_set_tracer(type->name);
826 default_bootup_tracer = NULL;
827 /* disable other selftests, since this will break it. */
828 tracing_selftest_disabled = 1;
b2821ae6 829#ifdef CONFIG_FTRACE_STARTUP_TEST
dac74940
SR
830 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
831 type->name);
b2821ae6 832#endif
b2821ae6 833
dac74940 834 out_unlock:
bc0c38d1
SR
835 return ret;
836}
837
838void unregister_tracer(struct tracer *type)
839{
840 struct tracer **t;
bc0c38d1
SR
841
842 mutex_lock(&trace_types_lock);
843 for (t = &trace_types; *t; t = &(*t)->next) {
844 if (*t == type)
845 goto found;
846 }
ee6c2c1b 847 pr_info("Tracer %s not registered\n", type->name);
bc0c38d1
SR
848 goto out;
849
850 found:
851 *t = (*t)->next;
b5db03c4
ACM
852
853 if (type == current_trace && tracer_enabled) {
854 tracer_enabled = 0;
855 tracing_stop();
856 if (current_trace->stop)
857 current_trace->stop(&global_trace);
858 current_trace = &nop_trace;
859 }
ee6c2c1b 860out:
bc0c38d1
SR
861 mutex_unlock(&trace_types_lock);
862}
863
283740c6 864static void __tracing_reset(struct ring_buffer *buffer, int cpu)
bc0c38d1 865{
d769041f 866 ftrace_disable_cpu();
283740c6 867 ring_buffer_reset_cpu(buffer, cpu);
d769041f 868 ftrace_enable_cpu();
bc0c38d1
SR
869}
870
f633903a
SR
871void tracing_reset(struct trace_array *tr, int cpu)
872{
873 struct ring_buffer *buffer = tr->buffer;
874
875 ring_buffer_record_disable(buffer);
876
877 /* Make sure all commits have finished */
878 synchronize_sched();
283740c6 879 __tracing_reset(buffer, cpu);
f633903a
SR
880
881 ring_buffer_record_enable(buffer);
882}
883
213cc060
PE
884void tracing_reset_online_cpus(struct trace_array *tr)
885{
621968cd 886 struct ring_buffer *buffer = tr->buffer;
213cc060
PE
887 int cpu;
888
621968cd
SR
889 ring_buffer_record_disable(buffer);
890
891 /* Make sure all commits have finished */
892 synchronize_sched();
893
213cc060
PE
894 tr->time_start = ftrace_now(tr->cpu);
895
896 for_each_online_cpu(cpu)
283740c6 897 __tracing_reset(buffer, cpu);
621968cd
SR
898
899 ring_buffer_record_enable(buffer);
213cc060
PE
900}
901
9456f0fa
SR
902void tracing_reset_current(int cpu)
903{
904 tracing_reset(&global_trace, cpu);
905}
906
907void tracing_reset_current_online_cpus(void)
908{
909 tracing_reset_online_cpus(&global_trace);
910}
911
bc0c38d1 912#define SAVED_CMDLINES 128
2c7eea4c 913#define NO_CMDLINE_MAP UINT_MAX
bc0c38d1
SR
914static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
915static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
916static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
917static int cmdline_idx;
edc35bd7 918static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
25b0b44a 919
25b0b44a 920/* temporary disable recording */
4fd27358 921static atomic_t trace_record_cmdline_disabled __read_mostly;
bc0c38d1
SR
922
923static void trace_init_cmdlines(void)
924{
2c7eea4c
TG
925 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
926 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
bc0c38d1
SR
927 cmdline_idx = 0;
928}
929
b5130b1e
CE
930int is_tracing_stopped(void)
931{
932 return trace_stop_count;
933}
934
69bb54ec
SR
935/**
936 * ftrace_off_permanent - disable all ftrace code permanently
937 *
938 * This should only be called when a serious anomally has
939 * been detected. This will turn off the function tracing,
940 * ring buffers, and other tracing utilites. It takes no
941 * locks and can be called from any context.
942 */
943void ftrace_off_permanent(void)
944{
945 tracing_disabled = 1;
946 ftrace_stop();
947 tracing_off_permanent();
948}
949
0f048701
SR
950/**
951 * tracing_start - quick start of the tracer
952 *
953 * If tracing is enabled but was stopped by tracing_stop,
954 * this will start the tracer back up.
955 */
956void tracing_start(void)
957{
958 struct ring_buffer *buffer;
959 unsigned long flags;
960
961 if (tracing_disabled)
962 return;
963
5389f6fa 964 raw_spin_lock_irqsave(&tracing_start_lock, flags);
b06a8301
SR
965 if (--trace_stop_count) {
966 if (trace_stop_count < 0) {
967 /* Someone screwed up their debugging */
968 WARN_ON_ONCE(1);
969 trace_stop_count = 0;
970 }
0f048701
SR
971 goto out;
972 }
973
a2f80714
SR
974 /* Prevent the buffers from switching */
975 arch_spin_lock(&ftrace_max_lock);
0f048701
SR
976
977 buffer = global_trace.buffer;
978 if (buffer)
979 ring_buffer_record_enable(buffer);
980
981 buffer = max_tr.buffer;
982 if (buffer)
983 ring_buffer_record_enable(buffer);
984
a2f80714
SR
985 arch_spin_unlock(&ftrace_max_lock);
986
0f048701
SR
987 ftrace_start();
988 out:
5389f6fa 989 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
0f048701
SR
990}
991
992/**
993 * tracing_stop - quick stop of the tracer
994 *
995 * Light weight way to stop tracing. Use in conjunction with
996 * tracing_start.
997 */
998void tracing_stop(void)
999{
1000 struct ring_buffer *buffer;
1001 unsigned long flags;
1002
1003 ftrace_stop();
5389f6fa 1004 raw_spin_lock_irqsave(&tracing_start_lock, flags);
0f048701
SR
1005 if (trace_stop_count++)
1006 goto out;
1007
a2f80714
SR
1008 /* Prevent the buffers from switching */
1009 arch_spin_lock(&ftrace_max_lock);
1010
0f048701
SR
1011 buffer = global_trace.buffer;
1012 if (buffer)
1013 ring_buffer_record_disable(buffer);
1014
1015 buffer = max_tr.buffer;
1016 if (buffer)
1017 ring_buffer_record_disable(buffer);
1018
a2f80714
SR
1019 arch_spin_unlock(&ftrace_max_lock);
1020
0f048701 1021 out:
5389f6fa 1022 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
0f048701
SR
1023}
1024
e309b41d 1025void trace_stop_cmdline_recording(void);
bc0c38d1 1026
e309b41d 1027static void trace_save_cmdline(struct task_struct *tsk)
bc0c38d1 1028{
a635cf04 1029 unsigned pid, idx;
bc0c38d1
SR
1030
1031 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1032 return;
1033
1034 /*
1035 * It's not the end of the world if we don't get
1036 * the lock, but we also don't want to spin
1037 * nor do we want to disable interrupts,
1038 * so if we miss here, then better luck next time.
1039 */
0199c4e6 1040 if (!arch_spin_trylock(&trace_cmdline_lock))
bc0c38d1
SR
1041 return;
1042
1043 idx = map_pid_to_cmdline[tsk->pid];
2c7eea4c 1044 if (idx == NO_CMDLINE_MAP) {
bc0c38d1
SR
1045 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1046
a635cf04
CE
1047 /*
1048 * Check whether the cmdline buffer at idx has a pid
1049 * mapped. We are going to overwrite that entry so we
1050 * need to clear the map_pid_to_cmdline. Otherwise we
1051 * would read the new comm for the old pid.
1052 */
1053 pid = map_cmdline_to_pid[idx];
1054 if (pid != NO_CMDLINE_MAP)
1055 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
bc0c38d1 1056
a635cf04 1057 map_cmdline_to_pid[idx] = tsk->pid;
bc0c38d1
SR
1058 map_pid_to_cmdline[tsk->pid] = idx;
1059
1060 cmdline_idx = idx;
1061 }
1062
1063 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1064
0199c4e6 1065 arch_spin_unlock(&trace_cmdline_lock);
bc0c38d1
SR
1066}
1067
4ca53085 1068void trace_find_cmdline(int pid, char comm[])
bc0c38d1 1069{
bc0c38d1
SR
1070 unsigned map;
1071
4ca53085
SR
1072 if (!pid) {
1073 strcpy(comm, "<idle>");
1074 return;
1075 }
bc0c38d1 1076
74bf4076
SR
1077 if (WARN_ON_ONCE(pid < 0)) {
1078 strcpy(comm, "<XXX>");
1079 return;
1080 }
1081
4ca53085
SR
1082 if (pid > PID_MAX_DEFAULT) {
1083 strcpy(comm, "<...>");
1084 return;
1085 }
bc0c38d1 1086
5b6045a9 1087 preempt_disable();
0199c4e6 1088 arch_spin_lock(&trace_cmdline_lock);
bc0c38d1 1089 map = map_pid_to_cmdline[pid];
50d88758
TG
1090 if (map != NO_CMDLINE_MAP)
1091 strcpy(comm, saved_cmdlines[map]);
1092 else
1093 strcpy(comm, "<...>");
bc0c38d1 1094
0199c4e6 1095 arch_spin_unlock(&trace_cmdline_lock);
5b6045a9 1096 preempt_enable();
bc0c38d1
SR
1097}
1098
e309b41d 1099void tracing_record_cmdline(struct task_struct *tsk)
bc0c38d1 1100{
18aecd36
TG
1101 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1102 !tracing_is_on())
bc0c38d1
SR
1103 return;
1104
1105 trace_save_cmdline(tsk);
1106}
1107
45dcd8b8 1108void
38697053
SR
1109tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1110 int pc)
bc0c38d1
SR
1111{
1112 struct task_struct *tsk = current;
bc0c38d1 1113
777e208d
SR
1114 entry->preempt_count = pc & 0xff;
1115 entry->pid = (tsk) ? tsk->pid : 0;
a3a4a5ac 1116 entry->padding = 0;
777e208d 1117 entry->flags =
9244489a 1118#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
2e2ca155 1119 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
9244489a
SR
1120#else
1121 TRACE_FLAG_IRQS_NOSUPPORT |
1122#endif
bc0c38d1
SR
1123 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1124 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1125 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1126}
f413cdb8 1127EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
bc0c38d1 1128
e77405ad
SR
1129struct ring_buffer_event *
1130trace_buffer_lock_reserve(struct ring_buffer *buffer,
1131 int type,
1132 unsigned long len,
1133 unsigned long flags, int pc)
51a763dd
ACM
1134{
1135 struct ring_buffer_event *event;
1136
e77405ad 1137 event = ring_buffer_lock_reserve(buffer, len);
51a763dd
ACM
1138 if (event != NULL) {
1139 struct trace_entry *ent = ring_buffer_event_data(event);
1140
1141 tracing_generic_entry_update(ent, flags, pc);
1142 ent->type = type;
1143 }
1144
1145 return event;
1146}
51a763dd 1147
e77405ad
SR
1148static inline void
1149__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1150 struct ring_buffer_event *event,
1151 unsigned long flags, int pc,
1152 int wake)
51a763dd 1153{
e77405ad 1154 ring_buffer_unlock_commit(buffer, event);
51a763dd 1155
e77405ad
SR
1156 ftrace_trace_stack(buffer, flags, 6, pc);
1157 ftrace_trace_userstack(buffer, flags, pc);
07edf712
FW
1158
1159 if (wake)
1160 trace_wake_up();
1161}
1162
e77405ad
SR
1163void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1164 struct ring_buffer_event *event,
1165 unsigned long flags, int pc)
07edf712 1166{
e77405ad 1167 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
51a763dd
ACM
1168}
1169
ef5580d0 1170struct ring_buffer_event *
e77405ad
SR
1171trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1172 int type, unsigned long len,
ef5580d0
SR
1173 unsigned long flags, int pc)
1174{
e77405ad
SR
1175 *current_rb = global_trace.buffer;
1176 return trace_buffer_lock_reserve(*current_rb,
ef5580d0
SR
1177 type, len, flags, pc);
1178}
94487d6d 1179EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
ef5580d0 1180
e77405ad
SR
1181void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1182 struct ring_buffer_event *event,
ef5580d0
SR
1183 unsigned long flags, int pc)
1184{
e77405ad 1185 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
07edf712 1186}
94487d6d 1187EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
07edf712 1188
e77405ad
SR
1189void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1190 struct ring_buffer_event *event,
1191 unsigned long flags, int pc)
07edf712 1192{
e77405ad 1193 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
77d9f465 1194}
94487d6d 1195EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
77d9f465 1196
1fd8df2c
MH
1197void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1198 struct ring_buffer_event *event,
1199 unsigned long flags, int pc,
1200 struct pt_regs *regs)
1201{
1202 ring_buffer_unlock_commit(buffer, event);
1203
1204 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1205 ftrace_trace_userstack(buffer, flags, pc);
1206}
1207EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1208
e77405ad
SR
1209void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1210 struct ring_buffer_event *event)
77d9f465 1211{
e77405ad 1212 ring_buffer_discard_commit(buffer, event);
ef5580d0 1213}
12acd473 1214EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
ef5580d0 1215
e309b41d 1216void
7be42151 1217trace_function(struct trace_array *tr,
38697053
SR
1218 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1219 int pc)
bc0c38d1 1220{
e1112b4d 1221 struct ftrace_event_call *call = &event_function;
e77405ad 1222 struct ring_buffer *buffer = tr->buffer;
3928a8a2 1223 struct ring_buffer_event *event;
777e208d 1224 struct ftrace_entry *entry;
bc0c38d1 1225
d769041f 1226 /* If we are reading the ring buffer, don't trace */
dd17c8f7 1227 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
d769041f
SR
1228 return;
1229
e77405ad 1230 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
51a763dd 1231 flags, pc);
3928a8a2
SR
1232 if (!event)
1233 return;
1234 entry = ring_buffer_event_data(event);
777e208d
SR
1235 entry->ip = ip;
1236 entry->parent_ip = parent_ip;
e1112b4d 1237
e77405ad
SR
1238 if (!filter_check_discard(call, entry, buffer, event))
1239 ring_buffer_unlock_commit(buffer, event);
bc0c38d1
SR
1240}
1241
e309b41d 1242void
2e0f5761 1243ftrace(struct trace_array *tr, struct trace_array_cpu *data,
38697053
SR
1244 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1245 int pc)
2e0f5761
IM
1246{
1247 if (likely(!atomic_read(&data->disabled)))
7be42151 1248 trace_function(tr, ip, parent_ip, flags, pc);
2e0f5761
IM
1249}
1250
c0a0d0d3 1251#ifdef CONFIG_STACKTRACE
4a9bd3f1
SR
1252
1253#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1254struct ftrace_stack {
1255 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1256};
1257
1258static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1259static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1260
e77405ad 1261static void __ftrace_trace_stack(struct ring_buffer *buffer,
53614991 1262 unsigned long flags,
1fd8df2c 1263 int skip, int pc, struct pt_regs *regs)
86387f7e 1264{
e1112b4d 1265 struct ftrace_event_call *call = &event_kernel_stack;
3928a8a2 1266 struct ring_buffer_event *event;
777e208d 1267 struct stack_entry *entry;
86387f7e 1268 struct stack_trace trace;
4a9bd3f1
SR
1269 int use_stack;
1270 int size = FTRACE_STACK_ENTRIES;
1271
1272 trace.nr_entries = 0;
1273 trace.skip = skip;
1274
1275 /*
1276 * Since events can happen in NMIs there's no safe way to
1277 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1278 * or NMI comes in, it will just have to use the default
1279 * FTRACE_STACK_SIZE.
1280 */
1281 preempt_disable_notrace();
1282
1283 use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1284 /*
1285 * We don't need any atomic variables, just a barrier.
1286 * If an interrupt comes in, we don't care, because it would
1287 * have exited and put the counter back to what we want.
1288 * We just need a barrier to keep gcc from moving things
1289 * around.
1290 */
1291 barrier();
1292 if (use_stack == 1) {
1293 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1294 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1295
1296 if (regs)
1297 save_stack_trace_regs(regs, &trace);
1298 else
1299 save_stack_trace(&trace);
1300
1301 if (trace.nr_entries > size)
1302 size = trace.nr_entries;
1303 } else
1304 /* From now on, use_stack is a boolean */
1305 use_stack = 0;
1306
1307 size *= sizeof(unsigned long);
86387f7e 1308
e77405ad 1309 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
4a9bd3f1 1310 sizeof(*entry) + size, flags, pc);
3928a8a2 1311 if (!event)
4a9bd3f1
SR
1312 goto out;
1313 entry = ring_buffer_event_data(event);
86387f7e 1314
4a9bd3f1
SR
1315 memset(&entry->caller, 0, size);
1316
1317 if (use_stack)
1318 memcpy(&entry->caller, trace.entries,
1319 trace.nr_entries * sizeof(unsigned long));
1320 else {
1321 trace.max_entries = FTRACE_STACK_ENTRIES;
1322 trace.entries = entry->caller;
1323 if (regs)
1324 save_stack_trace_regs(regs, &trace);
1325 else
1326 save_stack_trace(&trace);
1327 }
1328
1329 entry->size = trace.nr_entries;
86387f7e 1330
e77405ad
SR
1331 if (!filter_check_discard(call, entry, buffer, event))
1332 ring_buffer_unlock_commit(buffer, event);
4a9bd3f1
SR
1333
1334 out:
1335 /* Again, don't let gcc optimize things here */
1336 barrier();
1337 __get_cpu_var(ftrace_stack_reserve)--;
1338 preempt_enable_notrace();
1339
f0a920d5
IM
1340}
1341
1fd8df2c
MH
1342void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1343 int skip, int pc, struct pt_regs *regs)
1344{
1345 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1346 return;
1347
1348 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1349}
1350
e77405ad
SR
1351void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1352 int skip, int pc)
53614991
SR
1353{
1354 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1355 return;
1356
1fd8df2c 1357 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
53614991
SR
1358}
1359
c0a0d0d3
FW
1360void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1361 int pc)
38697053 1362{
1fd8df2c 1363 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
38697053
SR
1364}
1365
03889384
SR
1366/**
1367 * trace_dump_stack - record a stack back trace in the trace buffer
1368 */
1369void trace_dump_stack(void)
1370{
1371 unsigned long flags;
1372
1373 if (tracing_disabled || tracing_selftest_running)
e36c5458 1374 return;
03889384
SR
1375
1376 local_save_flags(flags);
1377
1378 /* skipping 3 traces, seems to get us at the caller of this function */
1fd8df2c 1379 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
03889384
SR
1380}
1381
91e86e56
SR
1382static DEFINE_PER_CPU(int, user_stack_count);
1383
e77405ad
SR
1384void
1385ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
02b67518 1386{
e1112b4d 1387 struct ftrace_event_call *call = &event_user_stack;
8d7c6a96 1388 struct ring_buffer_event *event;
02b67518
TE
1389 struct userstack_entry *entry;
1390 struct stack_trace trace;
02b67518
TE
1391
1392 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1393 return;
1394
b6345879
SR
1395 /*
1396 * NMIs can not handle page faults, even with fix ups.
1397 * The save user stack can (and often does) fault.
1398 */
1399 if (unlikely(in_nmi()))
1400 return;
02b67518 1401
91e86e56
SR
1402 /*
1403 * prevent recursion, since the user stack tracing may
1404 * trigger other kernel events.
1405 */
1406 preempt_disable();
1407 if (__this_cpu_read(user_stack_count))
1408 goto out;
1409
1410 __this_cpu_inc(user_stack_count);
1411
e77405ad 1412 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
51a763dd 1413 sizeof(*entry), flags, pc);
02b67518 1414 if (!event)
1dbd1951 1415 goto out_drop_count;
02b67518 1416 entry = ring_buffer_event_data(event);
02b67518 1417
48659d31 1418 entry->tgid = current->tgid;
02b67518
TE
1419 memset(&entry->caller, 0, sizeof(entry->caller));
1420
1421 trace.nr_entries = 0;
1422 trace.max_entries = FTRACE_STACK_ENTRIES;
1423 trace.skip = 0;
1424 trace.entries = entry->caller;
1425
1426 save_stack_trace_user(&trace);
e77405ad
SR
1427 if (!filter_check_discard(call, entry, buffer, event))
1428 ring_buffer_unlock_commit(buffer, event);
91e86e56 1429
1dbd1951 1430 out_drop_count:
91e86e56 1431 __this_cpu_dec(user_stack_count);
91e86e56
SR
1432 out:
1433 preempt_enable();
02b67518
TE
1434}
1435
4fd27358
HE
1436#ifdef UNUSED
1437static void __trace_userstack(struct trace_array *tr, unsigned long flags)
02b67518 1438{
7be42151 1439 ftrace_trace_userstack(tr, flags, preempt_count());
02b67518 1440}
4fd27358 1441#endif /* UNUSED */
02b67518 1442
c0a0d0d3
FW
1443#endif /* CONFIG_STACKTRACE */
1444
769b0441 1445/**
48ead020 1446 * trace_vbprintk - write binary msg to tracing buffer
769b0441
FW
1447 *
1448 */
40ce74f1 1449int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
769b0441 1450{
445c8951 1451 static arch_spinlock_t trace_buf_lock =
edc35bd7 1452 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
769b0441
FW
1453 static u32 trace_buf[TRACE_BUF_SIZE];
1454
e1112b4d 1455 struct ftrace_event_call *call = &event_bprint;
769b0441 1456 struct ring_buffer_event *event;
e77405ad 1457 struct ring_buffer *buffer;
769b0441
FW
1458 struct trace_array *tr = &global_trace;
1459 struct trace_array_cpu *data;
48ead020 1460 struct bprint_entry *entry;
769b0441 1461 unsigned long flags;
3189cdb3 1462 int disable;
769b0441
FW
1463 int cpu, len = 0, size, pc;
1464
1465 if (unlikely(tracing_selftest_running || tracing_disabled))
1466 return 0;
1467
1468 /* Don't pollute graph traces with trace_vprintk internals */
1469 pause_graph_tracing();
1470
1471 pc = preempt_count();
5168ae50 1472 preempt_disable_notrace();
769b0441
FW
1473 cpu = raw_smp_processor_id();
1474 data = tr->data[cpu];
1475
3189cdb3
SR
1476 disable = atomic_inc_return(&data->disabled);
1477 if (unlikely(disable != 1))
769b0441
FW
1478 goto out;
1479
80370cb7
SR
1480 /* Lockdep uses trace_printk for lock tracing */
1481 local_irq_save(flags);
0199c4e6 1482 arch_spin_lock(&trace_buf_lock);
769b0441
FW
1483 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1484
1485 if (len > TRACE_BUF_SIZE || len < 0)
1486 goto out_unlock;
1487
1488 size = sizeof(*entry) + sizeof(u32) * len;
e77405ad
SR
1489 buffer = tr->buffer;
1490 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1491 flags, pc);
769b0441
FW
1492 if (!event)
1493 goto out_unlock;
1494 entry = ring_buffer_event_data(event);
1495 entry->ip = ip;
769b0441
FW
1496 entry->fmt = fmt;
1497
1498 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
d931369b 1499 if (!filter_check_discard(call, entry, buffer, event)) {
e77405ad 1500 ring_buffer_unlock_commit(buffer, event);
d931369b
SR
1501 ftrace_trace_stack(buffer, flags, 6, pc);
1502 }
769b0441
FW
1503
1504out_unlock:
0199c4e6 1505 arch_spin_unlock(&trace_buf_lock);
80370cb7 1506 local_irq_restore(flags);
769b0441
FW
1507
1508out:
3189cdb3 1509 atomic_dec_return(&data->disabled);
5168ae50 1510 preempt_enable_notrace();
769b0441
FW
1511 unpause_graph_tracing();
1512
1513 return len;
1514}
48ead020
FW
1515EXPORT_SYMBOL_GPL(trace_vbprintk);
1516
659372d3
SR
1517int trace_array_printk(struct trace_array *tr,
1518 unsigned long ip, const char *fmt, ...)
1519{
1520 int ret;
1521 va_list ap;
1522
1523 if (!(trace_flags & TRACE_ITER_PRINTK))
1524 return 0;
1525
1526 va_start(ap, fmt);
1527 ret = trace_array_vprintk(tr, ip, fmt, ap);
1528 va_end(ap);
1529 return ret;
1530}
1531
1532int trace_array_vprintk(struct trace_array *tr,
1533 unsigned long ip, const char *fmt, va_list args)
48ead020 1534{
edc35bd7 1535 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
48ead020
FW
1536 static char trace_buf[TRACE_BUF_SIZE];
1537
e1112b4d 1538 struct ftrace_event_call *call = &event_print;
48ead020 1539 struct ring_buffer_event *event;
e77405ad 1540 struct ring_buffer *buffer;
48ead020
FW
1541 struct trace_array_cpu *data;
1542 int cpu, len = 0, size, pc;
1543 struct print_entry *entry;
1544 unsigned long irq_flags;
3189cdb3 1545 int disable;
48ead020
FW
1546
1547 if (tracing_disabled || tracing_selftest_running)
1548 return 0;
1549
1550 pc = preempt_count();
1551 preempt_disable_notrace();
1552 cpu = raw_smp_processor_id();
1553 data = tr->data[cpu];
1554
3189cdb3
SR
1555 disable = atomic_inc_return(&data->disabled);
1556 if (unlikely(disable != 1))
48ead020
FW
1557 goto out;
1558
1559 pause_graph_tracing();
1560 raw_local_irq_save(irq_flags);
0199c4e6 1561 arch_spin_lock(&trace_buf_lock);
f2942487 1562 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
48ead020
FW
1563
1564 size = sizeof(*entry) + len + 1;
e77405ad
SR
1565 buffer = tr->buffer;
1566 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1567 irq_flags, pc);
48ead020
FW
1568 if (!event)
1569 goto out_unlock;
1570 entry = ring_buffer_event_data(event);
c13d2f7c 1571 entry->ip = ip;
48ead020
FW
1572
1573 memcpy(&entry->buf, trace_buf, len);
c13d2f7c 1574 entry->buf[len] = '\0';
d931369b 1575 if (!filter_check_discard(call, entry, buffer, event)) {
e77405ad 1576 ring_buffer_unlock_commit(buffer, event);
d931369b
SR
1577 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1578 }
48ead020
FW
1579
1580 out_unlock:
0199c4e6 1581 arch_spin_unlock(&trace_buf_lock);
48ead020
FW
1582 raw_local_irq_restore(irq_flags);
1583 unpause_graph_tracing();
1584 out:
3189cdb3 1585 atomic_dec_return(&data->disabled);
48ead020
FW
1586 preempt_enable_notrace();
1587
1588 return len;
1589}
659372d3
SR
1590
1591int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1592{
a813a159 1593 return trace_array_vprintk(&global_trace, ip, fmt, args);
659372d3 1594}
769b0441
FW
1595EXPORT_SYMBOL_GPL(trace_vprintk);
1596
e2ac8ef5 1597static void trace_iterator_increment(struct trace_iterator *iter)
5a90f577 1598{
d769041f
SR
1599 /* Don't allow ftrace to trace into the ring buffers */
1600 ftrace_disable_cpu();
1601
5a90f577 1602 iter->idx++;
d769041f
SR
1603 if (iter->buffer_iter[iter->cpu])
1604 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1605
1606 ftrace_enable_cpu();
5a90f577
SR
1607}
1608
e309b41d 1609static struct trace_entry *
bc21b478
SR
1610peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1611 unsigned long *lost_events)
dd0e545f 1612{
3928a8a2
SR
1613 struct ring_buffer_event *event;
1614 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
dd0e545f 1615
d769041f
SR
1616 /* Don't allow ftrace to trace into the ring buffers */
1617 ftrace_disable_cpu();
1618
1619 if (buf_iter)
1620 event = ring_buffer_iter_peek(buf_iter, ts);
1621 else
bc21b478
SR
1622 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1623 lost_events);
d769041f
SR
1624
1625 ftrace_enable_cpu();
1626
4a9bd3f1
SR
1627 if (event) {
1628 iter->ent_size = ring_buffer_event_length(event);
1629 return ring_buffer_event_data(event);
1630 }
1631 iter->ent_size = 0;
1632 return NULL;
dd0e545f 1633}
d769041f 1634
dd0e545f 1635static struct trace_entry *
bc21b478
SR
1636__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1637 unsigned long *missing_events, u64 *ent_ts)
bc0c38d1 1638{
3928a8a2 1639 struct ring_buffer *buffer = iter->tr->buffer;
bc0c38d1 1640 struct trace_entry *ent, *next = NULL;
aa27497c 1641 unsigned long lost_events = 0, next_lost = 0;
b04cc6b1 1642 int cpu_file = iter->cpu_file;
3928a8a2 1643 u64 next_ts = 0, ts;
bc0c38d1
SR
1644 int next_cpu = -1;
1645 int cpu;
1646
b04cc6b1
FW
1647 /*
1648 * If we are in a per_cpu trace file, don't bother by iterating over
1649 * all cpu and peek directly.
1650 */
1651 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1652 if (ring_buffer_empty_cpu(buffer, cpu_file))
1653 return NULL;
bc21b478 1654 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
b04cc6b1
FW
1655 if (ent_cpu)
1656 *ent_cpu = cpu_file;
1657
1658 return ent;
1659 }
1660
ab46428c 1661 for_each_tracing_cpu(cpu) {
dd0e545f 1662
3928a8a2
SR
1663 if (ring_buffer_empty_cpu(buffer, cpu))
1664 continue;
dd0e545f 1665
bc21b478 1666 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
dd0e545f 1667
cdd31cd2
IM
1668 /*
1669 * Pick the entry with the smallest timestamp:
1670 */
3928a8a2 1671 if (ent && (!next || ts < next_ts)) {
bc0c38d1
SR
1672 next = ent;
1673 next_cpu = cpu;
3928a8a2 1674 next_ts = ts;
bc21b478 1675 next_lost = lost_events;
bc0c38d1
SR
1676 }
1677 }
1678
1679 if (ent_cpu)
1680 *ent_cpu = next_cpu;
1681
3928a8a2
SR
1682 if (ent_ts)
1683 *ent_ts = next_ts;
1684
bc21b478
SR
1685 if (missing_events)
1686 *missing_events = next_lost;
1687
bc0c38d1
SR
1688 return next;
1689}
1690
dd0e545f 1691/* Find the next real entry, without updating the iterator itself */
c4a8e8be
FW
1692struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1693 int *ent_cpu, u64 *ent_ts)
bc0c38d1 1694{
bc21b478 1695 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
dd0e545f
SR
1696}
1697
1698/* Find the next real entry, and increment the iterator to the next entry */
955b61e5 1699void *trace_find_next_entry_inc(struct trace_iterator *iter)
dd0e545f 1700{
bc21b478
SR
1701 iter->ent = __find_next_entry(iter, &iter->cpu,
1702 &iter->lost_events, &iter->ts);
dd0e545f 1703
3928a8a2 1704 if (iter->ent)
e2ac8ef5 1705 trace_iterator_increment(iter);
dd0e545f 1706
3928a8a2 1707 return iter->ent ? iter : NULL;
b3806b43 1708}
bc0c38d1 1709
e309b41d 1710static void trace_consume(struct trace_iterator *iter)
b3806b43 1711{
d769041f
SR
1712 /* Don't allow ftrace to trace into the ring buffers */
1713 ftrace_disable_cpu();
bc21b478
SR
1714 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1715 &iter->lost_events);
d769041f 1716 ftrace_enable_cpu();
bc0c38d1
SR
1717}
1718
e309b41d 1719static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
1720{
1721 struct trace_iterator *iter = m->private;
bc0c38d1 1722 int i = (int)*pos;
4e3c3333 1723 void *ent;
bc0c38d1 1724
a63ce5b3
SR
1725 WARN_ON_ONCE(iter->leftover);
1726
bc0c38d1
SR
1727 (*pos)++;
1728
1729 /* can't go backwards */
1730 if (iter->idx > i)
1731 return NULL;
1732
1733 if (iter->idx < 0)
955b61e5 1734 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
1735 else
1736 ent = iter;
1737
1738 while (ent && iter->idx < i)
955b61e5 1739 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
1740
1741 iter->pos = *pos;
1742
bc0c38d1
SR
1743 return ent;
1744}
1745
955b61e5 1746void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2f26ebd5
SR
1747{
1748 struct trace_array *tr = iter->tr;
1749 struct ring_buffer_event *event;
1750 struct ring_buffer_iter *buf_iter;
1751 unsigned long entries = 0;
1752 u64 ts;
1753
1754 tr->data[cpu]->skipped_entries = 0;
1755
1756 if (!iter->buffer_iter[cpu])
1757 return;
1758
1759 buf_iter = iter->buffer_iter[cpu];
1760 ring_buffer_iter_reset(buf_iter);
1761
1762 /*
1763 * We could have the case with the max latency tracers
1764 * that a reset never took place on a cpu. This is evident
1765 * by the timestamp being before the start of the buffer.
1766 */
1767 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1768 if (ts >= iter->tr->time_start)
1769 break;
1770 entries++;
1771 ring_buffer_read(buf_iter, NULL);
1772 }
1773
1774 tr->data[cpu]->skipped_entries = entries;
1775}
1776
d7350c3f 1777/*
d7350c3f
FW
1778 * The current tracer is copied to avoid a global locking
1779 * all around.
1780 */
bc0c38d1
SR
1781static void *s_start(struct seq_file *m, loff_t *pos)
1782{
1783 struct trace_iterator *iter = m->private;
d7350c3f 1784 static struct tracer *old_tracer;
b04cc6b1 1785 int cpu_file = iter->cpu_file;
bc0c38d1
SR
1786 void *p = NULL;
1787 loff_t l = 0;
3928a8a2 1788 int cpu;
bc0c38d1 1789
d7350c3f 1790 /* copy the tracer to avoid using a global lock all around */
bc0c38d1 1791 mutex_lock(&trace_types_lock);
d7350c3f
FW
1792 if (unlikely(old_tracer != current_trace && current_trace)) {
1793 old_tracer = current_trace;
1794 *iter->trace = *current_trace;
d15f57f2 1795 }
d7350c3f 1796 mutex_unlock(&trace_types_lock);
bc0c38d1
SR
1797
1798 atomic_inc(&trace_record_cmdline_disabled);
1799
bc0c38d1
SR
1800 if (*pos != iter->pos) {
1801 iter->ent = NULL;
1802 iter->cpu = 0;
1803 iter->idx = -1;
1804
d769041f
SR
1805 ftrace_disable_cpu();
1806
b04cc6b1
FW
1807 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1808 for_each_tracing_cpu(cpu)
2f26ebd5 1809 tracing_iter_reset(iter, cpu);
b04cc6b1 1810 } else
2f26ebd5 1811 tracing_iter_reset(iter, cpu_file);
bc0c38d1 1812
d769041f
SR
1813 ftrace_enable_cpu();
1814
ac91d854 1815 iter->leftover = 0;
bc0c38d1
SR
1816 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1817 ;
1818
1819 } else {
a63ce5b3
SR
1820 /*
1821 * If we overflowed the seq_file before, then we want
1822 * to just reuse the trace_seq buffer again.
1823 */
1824 if (iter->leftover)
1825 p = iter;
1826 else {
1827 l = *pos - 1;
1828 p = s_next(m, p, &l);
1829 }
bc0c38d1
SR
1830 }
1831
4f535968 1832 trace_event_read_lock();
7e53bd42 1833 trace_access_lock(cpu_file);
bc0c38d1
SR
1834 return p;
1835}
1836
1837static void s_stop(struct seq_file *m, void *p)
1838{
7e53bd42
LJ
1839 struct trace_iterator *iter = m->private;
1840
bc0c38d1 1841 atomic_dec(&trace_record_cmdline_disabled);
7e53bd42 1842 trace_access_unlock(iter->cpu_file);
4f535968 1843 trace_event_read_unlock();
bc0c38d1
SR
1844}
1845
e309b41d 1846static void print_lat_help_header(struct seq_file *m)
bc0c38d1 1847{
a6168353
ME
1848 seq_puts(m, "# _------=> CPU# \n");
1849 seq_puts(m, "# / _-----=> irqs-off \n");
1850 seq_puts(m, "# | / _----=> need-resched \n");
1851 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1852 seq_puts(m, "# ||| / _--=> preempt-depth \n");
e6e1e259
SR
1853 seq_puts(m, "# |||| / delay \n");
1854 seq_puts(m, "# cmd pid ||||| time | caller \n");
1855 seq_puts(m, "# \\ / ||||| \\ | / \n");
bc0c38d1
SR
1856}
1857
e309b41d 1858static void print_func_help_header(struct seq_file *m)
bc0c38d1 1859{
a6168353
ME
1860 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1861 seq_puts(m, "# | | | | |\n");
bc0c38d1
SR
1862}
1863
1864
62b915f1 1865void
bc0c38d1
SR
1866print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1867{
1868 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1869 struct trace_array *tr = iter->tr;
1870 struct trace_array_cpu *data = tr->data[tr->cpu];
1871 struct tracer *type = current_trace;
2f26ebd5
SR
1872 unsigned long entries = 0;
1873 unsigned long total = 0;
1874 unsigned long count;
bc0c38d1 1875 const char *name = "preemption";
2f26ebd5 1876 int cpu;
bc0c38d1
SR
1877
1878 if (type)
1879 name = type->name;
1880
2f26ebd5
SR
1881
1882 for_each_tracing_cpu(cpu) {
1883 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1884 /*
1885 * If this buffer has skipped entries, then we hold all
1886 * entries for the trace and we need to ignore the
1887 * ones before the time stamp.
1888 */
1889 if (tr->data[cpu]->skipped_entries) {
1890 count -= tr->data[cpu]->skipped_entries;
1891 /* total is the same as the entries */
1892 total += count;
1893 } else
1894 total += count +
1895 ring_buffer_overrun_cpu(tr->buffer, cpu);
1896 entries += count;
1897 }
bc0c38d1 1898
888b55dc 1899 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
bc0c38d1 1900 name, UTS_RELEASE);
888b55dc 1901 seq_puts(m, "# -----------------------------------"
bc0c38d1 1902 "---------------------------------\n");
888b55dc 1903 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
bc0c38d1 1904 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 1905 nsecs_to_usecs(data->saved_latency),
bc0c38d1 1906 entries,
4c11d7ae 1907 total,
bc0c38d1
SR
1908 tr->cpu,
1909#if defined(CONFIG_PREEMPT_NONE)
1910 "server",
1911#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1912 "desktop",
b5c21b45 1913#elif defined(CONFIG_PREEMPT)
bc0c38d1
SR
1914 "preempt",
1915#else
1916 "unknown",
1917#endif
1918 /* These are reserved for later use */
1919 0, 0, 0, 0);
1920#ifdef CONFIG_SMP
1921 seq_printf(m, " #P:%d)\n", num_online_cpus());
1922#else
1923 seq_puts(m, ")\n");
1924#endif
888b55dc
KM
1925 seq_puts(m, "# -----------------\n");
1926 seq_printf(m, "# | task: %.16s-%d "
bc0c38d1
SR
1927 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1928 data->comm, data->pid, data->uid, data->nice,
1929 data->policy, data->rt_priority);
888b55dc 1930 seq_puts(m, "# -----------------\n");
bc0c38d1
SR
1931
1932 if (data->critical_start) {
888b55dc 1933 seq_puts(m, "# => started at: ");
214023c3
SR
1934 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1935 trace_print_seq(m, &iter->seq);
888b55dc 1936 seq_puts(m, "\n# => ended at: ");
214023c3
SR
1937 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1938 trace_print_seq(m, &iter->seq);
8248ac05 1939 seq_puts(m, "\n#\n");
bc0c38d1
SR
1940 }
1941
888b55dc 1942 seq_puts(m, "#\n");
bc0c38d1
SR
1943}
1944
a309720c
SR
1945static void test_cpu_buff_start(struct trace_iterator *iter)
1946{
1947 struct trace_seq *s = &iter->seq;
1948
12ef7d44
SR
1949 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1950 return;
1951
1952 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1953 return;
1954
4462344e 1955 if (cpumask_test_cpu(iter->cpu, iter->started))
a309720c
SR
1956 return;
1957
2f26ebd5
SR
1958 if (iter->tr->data[iter->cpu]->skipped_entries)
1959 return;
1960
4462344e 1961 cpumask_set_cpu(iter->cpu, iter->started);
b0dfa978
FW
1962
1963 /* Don't print started cpu buffer for the first entry of the trace */
1964 if (iter->idx > 1)
1965 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1966 iter->cpu);
a309720c
SR
1967}
1968
2c4f035f 1969static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 1970{
214023c3 1971 struct trace_seq *s = &iter->seq;
bc0c38d1 1972 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 1973 struct trace_entry *entry;
f633cef0 1974 struct trace_event *event;
bc0c38d1 1975
4e3c3333 1976 entry = iter->ent;
dd0e545f 1977
a309720c
SR
1978 test_cpu_buff_start(iter);
1979
c4a8e8be 1980 event = ftrace_find_event(entry->type);
bc0c38d1 1981
c4a8e8be 1982 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
27d48be8
SR
1983 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1984 if (!trace_print_lat_context(iter))
1985 goto partial;
1986 } else {
1987 if (!trace_print_context(iter))
1988 goto partial;
1989 }
c4a8e8be 1990 }
bc0c38d1 1991
268ccda0 1992 if (event)
a9a57763 1993 return event->funcs->trace(iter, sym_flags, event);
d9793bd8
ACM
1994
1995 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1996 goto partial;
02b67518 1997
2c4f035f 1998 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
1999partial:
2000 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1
SR
2001}
2002
2c4f035f 2003static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3
IM
2004{
2005 struct trace_seq *s = &iter->seq;
2006 struct trace_entry *entry;
f633cef0 2007 struct trace_event *event;
f9896bf3
IM
2008
2009 entry = iter->ent;
dd0e545f 2010
c4a8e8be 2011 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
d9793bd8
ACM
2012 if (!trace_seq_printf(s, "%d %d %llu ",
2013 entry->pid, iter->cpu, iter->ts))
2014 goto partial;
c4a8e8be 2015 }
f9896bf3 2016
f633cef0 2017 event = ftrace_find_event(entry->type);
268ccda0 2018 if (event)
a9a57763 2019 return event->funcs->raw(iter, 0, event);
d9793bd8
ACM
2020
2021 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2022 goto partial;
777e208d 2023
2c4f035f 2024 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
2025partial:
2026 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3
IM
2027}
2028
2c4f035f 2029static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec
IM
2030{
2031 struct trace_seq *s = &iter->seq;
2032 unsigned char newline = '\n';
2033 struct trace_entry *entry;
f633cef0 2034 struct trace_event *event;
5e3ca0ec
IM
2035
2036 entry = iter->ent;
dd0e545f 2037
c4a8e8be
FW
2038 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2039 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2040 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2041 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2042 }
5e3ca0ec 2043
f633cef0 2044 event = ftrace_find_event(entry->type);
268ccda0 2045 if (event) {
a9a57763 2046 enum print_line_t ret = event->funcs->hex(iter, 0, event);
d9793bd8
ACM
2047 if (ret != TRACE_TYPE_HANDLED)
2048 return ret;
2049 }
7104f300 2050
5e3ca0ec
IM
2051 SEQ_PUT_FIELD_RET(s, newline);
2052
2c4f035f 2053 return TRACE_TYPE_HANDLED;
5e3ca0ec
IM
2054}
2055
2c4f035f 2056static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa
IM
2057{
2058 struct trace_seq *s = &iter->seq;
2059 struct trace_entry *entry;
f633cef0 2060 struct trace_event *event;
cb0f12aa
IM
2061
2062 entry = iter->ent;
dd0e545f 2063
c4a8e8be
FW
2064 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2065 SEQ_PUT_FIELD_RET(s, entry->pid);
1830b52d 2066 SEQ_PUT_FIELD_RET(s, iter->cpu);
c4a8e8be
FW
2067 SEQ_PUT_FIELD_RET(s, iter->ts);
2068 }
cb0f12aa 2069
f633cef0 2070 event = ftrace_find_event(entry->type);
a9a57763
SR
2071 return event ? event->funcs->binary(iter, 0, event) :
2072 TRACE_TYPE_HANDLED;
cb0f12aa
IM
2073}
2074
62b915f1 2075int trace_empty(struct trace_iterator *iter)
bc0c38d1 2076{
bc0c38d1
SR
2077 int cpu;
2078
9aba60fe
SR
2079 /* If we are looking at one CPU buffer, only check that one */
2080 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2081 cpu = iter->cpu_file;
2082 if (iter->buffer_iter[cpu]) {
2083 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2084 return 0;
2085 } else {
2086 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2087 return 0;
2088 }
2089 return 1;
2090 }
2091
ab46428c 2092 for_each_tracing_cpu(cpu) {
d769041f
SR
2093 if (iter->buffer_iter[cpu]) {
2094 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2095 return 0;
2096 } else {
2097 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2098 return 0;
2099 }
bc0c38d1 2100 }
d769041f 2101
797d3712 2102 return 1;
bc0c38d1
SR
2103}
2104
4f535968 2105/* Called with trace_event_read_lock() held. */
955b61e5 2106enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 2107{
2c4f035f
FW
2108 enum print_line_t ret;
2109
ee5e51f5
JO
2110 if (iter->lost_events &&
2111 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2112 iter->cpu, iter->lost_events))
2113 return TRACE_TYPE_PARTIAL_LINE;
bc21b478 2114
2c4f035f
FW
2115 if (iter->trace && iter->trace->print_line) {
2116 ret = iter->trace->print_line(iter);
2117 if (ret != TRACE_TYPE_UNHANDLED)
2118 return ret;
2119 }
72829bc3 2120
48ead020
FW
2121 if (iter->ent->type == TRACE_BPRINT &&
2122 trace_flags & TRACE_ITER_PRINTK &&
2123 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2124 return trace_print_bprintk_msg_only(iter);
48ead020 2125
66896a85
FW
2126 if (iter->ent->type == TRACE_PRINT &&
2127 trace_flags & TRACE_ITER_PRINTK &&
2128 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2129 return trace_print_printk_msg_only(iter);
66896a85 2130
cb0f12aa
IM
2131 if (trace_flags & TRACE_ITER_BIN)
2132 return print_bin_fmt(iter);
2133
5e3ca0ec
IM
2134 if (trace_flags & TRACE_ITER_HEX)
2135 return print_hex_fmt(iter);
2136
f9896bf3
IM
2137 if (trace_flags & TRACE_ITER_RAW)
2138 return print_raw_fmt(iter);
2139
f9896bf3
IM
2140 return print_trace_fmt(iter);
2141}
2142
62b915f1
JO
2143void trace_default_header(struct seq_file *m)
2144{
2145 struct trace_iterator *iter = m->private;
2146
f56e7f8e
JO
2147 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2148 return;
2149
62b915f1
JO
2150 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2151 /* print nothing if the buffers are empty */
2152 if (trace_empty(iter))
2153 return;
2154 print_trace_header(m, iter);
2155 if (!(trace_flags & TRACE_ITER_VERBOSE))
2156 print_lat_help_header(m);
2157 } else {
2158 if (!(trace_flags & TRACE_ITER_VERBOSE))
2159 print_func_help_header(m);
2160 }
2161}
2162
e0a413f6
SR
2163static void test_ftrace_alive(struct seq_file *m)
2164{
2165 if (!ftrace_is_dead())
2166 return;
2167 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2168 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2169}
2170
bc0c38d1
SR
2171static int s_show(struct seq_file *m, void *v)
2172{
2173 struct trace_iterator *iter = v;
a63ce5b3 2174 int ret;
bc0c38d1
SR
2175
2176 if (iter->ent == NULL) {
2177 if (iter->tr) {
2178 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2179 seq_puts(m, "#\n");
e0a413f6 2180 test_ftrace_alive(m);
bc0c38d1 2181 }
8bba1bf5
MM
2182 if (iter->trace && iter->trace->print_header)
2183 iter->trace->print_header(m);
62b915f1
JO
2184 else
2185 trace_default_header(m);
2186
a63ce5b3
SR
2187 } else if (iter->leftover) {
2188 /*
2189 * If we filled the seq_file buffer earlier, we
2190 * want to just show it now.
2191 */
2192 ret = trace_print_seq(m, &iter->seq);
2193
2194 /* ret should this time be zero, but you never know */
2195 iter->leftover = ret;
2196
bc0c38d1 2197 } else {
f9896bf3 2198 print_trace_line(iter);
a63ce5b3
SR
2199 ret = trace_print_seq(m, &iter->seq);
2200 /*
2201 * If we overflow the seq_file buffer, then it will
2202 * ask us for this data again at start up.
2203 * Use that instead.
2204 * ret is 0 if seq_file write succeeded.
2205 * -1 otherwise.
2206 */
2207 iter->leftover = ret;
bc0c38d1
SR
2208 }
2209
2210 return 0;
2211}
2212
88e9d34c 2213static const struct seq_operations tracer_seq_ops = {
4bf39a94
IM
2214 .start = s_start,
2215 .next = s_next,
2216 .stop = s_stop,
2217 .show = s_show,
bc0c38d1
SR
2218};
2219
e309b41d 2220static struct trace_iterator *
85a2f9b4 2221__tracing_open(struct inode *inode, struct file *file)
bc0c38d1 2222{
b04cc6b1 2223 long cpu_file = (long) inode->i_private;
85a2f9b4 2224 void *fail_ret = ERR_PTR(-ENOMEM);
bc0c38d1 2225 struct trace_iterator *iter;
3928a8a2 2226 struct seq_file *m;
85a2f9b4 2227 int cpu, ret;
bc0c38d1 2228
85a2f9b4
SR
2229 if (tracing_disabled)
2230 return ERR_PTR(-ENODEV);
60a11774 2231
bc0c38d1 2232 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
85a2f9b4
SR
2233 if (!iter)
2234 return ERR_PTR(-ENOMEM);
bc0c38d1 2235
d7350c3f
FW
2236 /*
2237 * We make a copy of the current tracer to avoid concurrent
2238 * changes on it while we are reading.
2239 */
bc0c38d1 2240 mutex_lock(&trace_types_lock);
d7350c3f 2241 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
85a2f9b4 2242 if (!iter->trace)
d7350c3f 2243 goto fail;
85a2f9b4 2244
d7350c3f
FW
2245 if (current_trace)
2246 *iter->trace = *current_trace;
2247
79f55997 2248 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
b0dfa978
FW
2249 goto fail;
2250
bc0c38d1
SR
2251 if (current_trace && current_trace->print_max)
2252 iter->tr = &max_tr;
2253 else
b04cc6b1 2254 iter->tr = &global_trace;
bc0c38d1 2255 iter->pos = -1;
d7350c3f 2256 mutex_init(&iter->mutex);
b04cc6b1 2257 iter->cpu_file = cpu_file;
bc0c38d1 2258
8bba1bf5
MM
2259 /* Notify the tracer early; before we stop tracing. */
2260 if (iter->trace && iter->trace->open)
a93751ca 2261 iter->trace->open(iter);
8bba1bf5 2262
12ef7d44
SR
2263 /* Annotate start of buffers if we had overruns */
2264 if (ring_buffer_overruns(iter->tr->buffer))
2265 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2266
2f26ebd5
SR
2267 /* stop the trace while dumping */
2268 tracing_stop();
2269
b04cc6b1
FW
2270 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2271 for_each_tracing_cpu(cpu) {
b04cc6b1 2272 iter->buffer_iter[cpu] =
72c9ddfd
DM
2273 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2274 }
2275 ring_buffer_read_prepare_sync();
2276 for_each_tracing_cpu(cpu) {
2277 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 2278 tracing_iter_reset(iter, cpu);
b04cc6b1
FW
2279 }
2280 } else {
2281 cpu = iter->cpu_file;
3928a8a2 2282 iter->buffer_iter[cpu] =
72c9ddfd
DM
2283 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2284 ring_buffer_read_prepare_sync();
2285 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 2286 tracing_iter_reset(iter, cpu);
3928a8a2
SR
2287 }
2288
85a2f9b4
SR
2289 ret = seq_open(file, &tracer_seq_ops);
2290 if (ret < 0) {
2291 fail_ret = ERR_PTR(ret);
3928a8a2 2292 goto fail_buffer;
85a2f9b4 2293 }
bc0c38d1 2294
3928a8a2
SR
2295 m = file->private_data;
2296 m->private = iter;
bc0c38d1 2297
bc0c38d1
SR
2298 mutex_unlock(&trace_types_lock);
2299
bc0c38d1 2300 return iter;
3928a8a2
SR
2301
2302 fail_buffer:
2303 for_each_tracing_cpu(cpu) {
2304 if (iter->buffer_iter[cpu])
2305 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2306 }
b0dfa978 2307 free_cpumask_var(iter->started);
2f26ebd5 2308 tracing_start();
d7350c3f 2309 fail:
3928a8a2 2310 mutex_unlock(&trace_types_lock);
d7350c3f 2311 kfree(iter->trace);
0bb943c7 2312 kfree(iter);
3928a8a2 2313
85a2f9b4 2314 return fail_ret;
bc0c38d1
SR
2315}
2316
2317int tracing_open_generic(struct inode *inode, struct file *filp)
2318{
60a11774
SR
2319 if (tracing_disabled)
2320 return -ENODEV;
2321
bc0c38d1
SR
2322 filp->private_data = inode->i_private;
2323 return 0;
2324}
2325
4fd27358 2326static int tracing_release(struct inode *inode, struct file *file)
bc0c38d1 2327{
907f2784 2328 struct seq_file *m = file->private_data;
4acd4d00 2329 struct trace_iterator *iter;
3928a8a2 2330 int cpu;
bc0c38d1 2331
4acd4d00
SR
2332 if (!(file->f_mode & FMODE_READ))
2333 return 0;
2334
2335 iter = m->private;
2336
bc0c38d1 2337 mutex_lock(&trace_types_lock);
3928a8a2
SR
2338 for_each_tracing_cpu(cpu) {
2339 if (iter->buffer_iter[cpu])
2340 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2341 }
2342
bc0c38d1
SR
2343 if (iter->trace && iter->trace->close)
2344 iter->trace->close(iter);
2345
2346 /* reenable tracing if it was previously enabled */
9036990d 2347 tracing_start();
bc0c38d1
SR
2348 mutex_unlock(&trace_types_lock);
2349
2350 seq_release(inode, file);
d7350c3f 2351 mutex_destroy(&iter->mutex);
b0dfa978 2352 free_cpumask_var(iter->started);
d7350c3f 2353 kfree(iter->trace);
bc0c38d1
SR
2354 kfree(iter);
2355 return 0;
2356}
2357
2358static int tracing_open(struct inode *inode, struct file *file)
2359{
85a2f9b4
SR
2360 struct trace_iterator *iter;
2361 int ret = 0;
bc0c38d1 2362
4acd4d00
SR
2363 /* If this file was open for write, then erase contents */
2364 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 2365 (file->f_flags & O_TRUNC)) {
4acd4d00 2366 long cpu = (long) inode->i_private;
bc0c38d1 2367
4acd4d00
SR
2368 if (cpu == TRACE_PIPE_ALL_CPU)
2369 tracing_reset_online_cpus(&global_trace);
2370 else
2371 tracing_reset(&global_trace, cpu);
2372 }
bc0c38d1 2373
4acd4d00
SR
2374 if (file->f_mode & FMODE_READ) {
2375 iter = __tracing_open(inode, file);
2376 if (IS_ERR(iter))
2377 ret = PTR_ERR(iter);
2378 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2379 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2380 }
bc0c38d1
SR
2381 return ret;
2382}
2383
e309b41d 2384static void *
bc0c38d1
SR
2385t_next(struct seq_file *m, void *v, loff_t *pos)
2386{
f129e965 2387 struct tracer *t = v;
bc0c38d1
SR
2388
2389 (*pos)++;
2390
2391 if (t)
2392 t = t->next;
2393
bc0c38d1
SR
2394 return t;
2395}
2396
2397static void *t_start(struct seq_file *m, loff_t *pos)
2398{
f129e965 2399 struct tracer *t;
bc0c38d1
SR
2400 loff_t l = 0;
2401
2402 mutex_lock(&trace_types_lock);
f129e965 2403 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
bc0c38d1
SR
2404 ;
2405
2406 return t;
2407}
2408
2409static void t_stop(struct seq_file *m, void *p)
2410{
2411 mutex_unlock(&trace_types_lock);
2412}
2413
2414static int t_show(struct seq_file *m, void *v)
2415{
2416 struct tracer *t = v;
2417
2418 if (!t)
2419 return 0;
2420
2421 seq_printf(m, "%s", t->name);
2422 if (t->next)
2423 seq_putc(m, ' ');
2424 else
2425 seq_putc(m, '\n');
2426
2427 return 0;
2428}
2429
88e9d34c 2430static const struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
2431 .start = t_start,
2432 .next = t_next,
2433 .stop = t_stop,
2434 .show = t_show,
bc0c38d1
SR
2435};
2436
2437static int show_traces_open(struct inode *inode, struct file *file)
2438{
60a11774
SR
2439 if (tracing_disabled)
2440 return -ENODEV;
2441
f129e965 2442 return seq_open(file, &show_traces_seq_ops);
bc0c38d1
SR
2443}
2444
4acd4d00
SR
2445static ssize_t
2446tracing_write_stub(struct file *filp, const char __user *ubuf,
2447 size_t count, loff_t *ppos)
2448{
2449 return count;
2450}
2451
364829b1
SP
2452static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2453{
2454 if (file->f_mode & FMODE_READ)
2455 return seq_lseek(file, offset, origin);
2456 else
2457 return 0;
2458}
2459
5e2336a0 2460static const struct file_operations tracing_fops = {
4bf39a94
IM
2461 .open = tracing_open,
2462 .read = seq_read,
4acd4d00 2463 .write = tracing_write_stub,
364829b1 2464 .llseek = tracing_seek,
4bf39a94 2465 .release = tracing_release,
bc0c38d1
SR
2466};
2467
5e2336a0 2468static const struct file_operations show_traces_fops = {
c7078de1
IM
2469 .open = show_traces_open,
2470 .read = seq_read,
2471 .release = seq_release,
b444786f 2472 .llseek = seq_lseek,
c7078de1
IM
2473};
2474
36dfe925
IM
2475/*
2476 * Only trace on a CPU if the bitmask is set:
2477 */
9e01c1b7 2478static cpumask_var_t tracing_cpumask;
36dfe925
IM
2479
2480/*
2481 * The tracer itself will not take this lock, but still we want
2482 * to provide a consistent cpumask to user-space:
2483 */
2484static DEFINE_MUTEX(tracing_cpumask_update_lock);
2485
2486/*
2487 * Temporary storage for the character representation of the
2488 * CPU bitmask (and one more byte for the newline):
2489 */
2490static char mask_str[NR_CPUS + 1];
2491
c7078de1
IM
2492static ssize_t
2493tracing_cpumask_read(struct file *filp, char __user *ubuf,
2494 size_t count, loff_t *ppos)
2495{
36dfe925 2496 int len;
c7078de1
IM
2497
2498 mutex_lock(&tracing_cpumask_update_lock);
36dfe925 2499
9e01c1b7 2500 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
36dfe925
IM
2501 if (count - len < 2) {
2502 count = -EINVAL;
2503 goto out_err;
2504 }
2505 len += sprintf(mask_str + len, "\n");
2506 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2507
2508out_err:
c7078de1
IM
2509 mutex_unlock(&tracing_cpumask_update_lock);
2510
2511 return count;
2512}
2513
2514static ssize_t
2515tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2516 size_t count, loff_t *ppos)
2517{
36dfe925 2518 int err, cpu;
9e01c1b7
RR
2519 cpumask_var_t tracing_cpumask_new;
2520
2521 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2522 return -ENOMEM;
c7078de1 2523
9e01c1b7 2524 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 2525 if (err)
36dfe925
IM
2526 goto err_unlock;
2527
215368e8
LZ
2528 mutex_lock(&tracing_cpumask_update_lock);
2529
a5e25883 2530 local_irq_disable();
0199c4e6 2531 arch_spin_lock(&ftrace_max_lock);
ab46428c 2532 for_each_tracing_cpu(cpu) {
36dfe925
IM
2533 /*
2534 * Increase/decrease the disabled counter if we are
2535 * about to flip a bit in the cpumask:
2536 */
9e01c1b7
RR
2537 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2538 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
2539 atomic_inc(&global_trace.data[cpu]->disabled);
2540 }
9e01c1b7
RR
2541 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2542 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
2543 atomic_dec(&global_trace.data[cpu]->disabled);
2544 }
2545 }
0199c4e6 2546 arch_spin_unlock(&ftrace_max_lock);
a5e25883 2547 local_irq_enable();
36dfe925 2548
9e01c1b7 2549 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
36dfe925
IM
2550
2551 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 2552 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
2553
2554 return count;
36dfe925
IM
2555
2556err_unlock:
215368e8 2557 free_cpumask_var(tracing_cpumask_new);
36dfe925
IM
2558
2559 return err;
c7078de1
IM
2560}
2561
5e2336a0 2562static const struct file_operations tracing_cpumask_fops = {
c7078de1
IM
2563 .open = tracing_open_generic,
2564 .read = tracing_cpumask_read,
2565 .write = tracing_cpumask_write,
b444786f 2566 .llseek = generic_file_llseek,
bc0c38d1
SR
2567};
2568
fdb372ed 2569static int tracing_trace_options_show(struct seq_file *m, void *v)
bc0c38d1 2570{
d8e83d26
SR
2571 struct tracer_opt *trace_opts;
2572 u32 tracer_flags;
d8e83d26 2573 int i;
adf9f195 2574
d8e83d26
SR
2575 mutex_lock(&trace_types_lock);
2576 tracer_flags = current_trace->flags->val;
2577 trace_opts = current_trace->flags->opts;
2578
bc0c38d1
SR
2579 for (i = 0; trace_options[i]; i++) {
2580 if (trace_flags & (1 << i))
fdb372ed 2581 seq_printf(m, "%s\n", trace_options[i]);
bc0c38d1 2582 else
fdb372ed 2583 seq_printf(m, "no%s\n", trace_options[i]);
bc0c38d1
SR
2584 }
2585
adf9f195
FW
2586 for (i = 0; trace_opts[i].name; i++) {
2587 if (tracer_flags & trace_opts[i].bit)
fdb372ed 2588 seq_printf(m, "%s\n", trace_opts[i].name);
adf9f195 2589 else
fdb372ed 2590 seq_printf(m, "no%s\n", trace_opts[i].name);
adf9f195 2591 }
d8e83d26 2592 mutex_unlock(&trace_types_lock);
adf9f195 2593
fdb372ed 2594 return 0;
bc0c38d1 2595}
bc0c38d1 2596
8d18eaaf
LZ
2597static int __set_tracer_option(struct tracer *trace,
2598 struct tracer_flags *tracer_flags,
2599 struct tracer_opt *opts, int neg)
2600{
2601 int ret;
bc0c38d1 2602
8d18eaaf
LZ
2603 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2604 if (ret)
2605 return ret;
2606
2607 if (neg)
2608 tracer_flags->val &= ~opts->bit;
2609 else
2610 tracer_flags->val |= opts->bit;
2611 return 0;
bc0c38d1
SR
2612}
2613
adf9f195
FW
2614/* Try to assign a tracer specific option */
2615static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2616{
7770841e 2617 struct tracer_flags *tracer_flags = trace->flags;
adf9f195 2618 struct tracer_opt *opts = NULL;
8d18eaaf 2619 int i;
adf9f195 2620
7770841e
Z
2621 for (i = 0; tracer_flags->opts[i].name; i++) {
2622 opts = &tracer_flags->opts[i];
adf9f195 2623
8d18eaaf
LZ
2624 if (strcmp(cmp, opts->name) == 0)
2625 return __set_tracer_option(trace, trace->flags,
2626 opts, neg);
adf9f195 2627 }
adf9f195 2628
8d18eaaf 2629 return -EINVAL;
adf9f195
FW
2630}
2631
af4617bd
SR
2632static void set_tracer_flags(unsigned int mask, int enabled)
2633{
2634 /* do nothing if flag is already set */
2635 if (!!(trace_flags & mask) == !!enabled)
2636 return;
2637
2638 if (enabled)
2639 trace_flags |= mask;
2640 else
2641 trace_flags &= ~mask;
e870e9a1
LZ
2642
2643 if (mask == TRACE_ITER_RECORD_CMD)
2644 trace_event_enable_cmd_record(enabled);
750912fa
DS
2645
2646 if (mask == TRACE_ITER_OVERWRITE)
2647 ring_buffer_change_overwrite(global_trace.buffer, enabled);
af4617bd
SR
2648}
2649
bc0c38d1 2650static ssize_t
ee6bce52 2651tracing_trace_options_write(struct file *filp, const char __user *ubuf,
bc0c38d1
SR
2652 size_t cnt, loff_t *ppos)
2653{
2654 char buf[64];
8d18eaaf 2655 char *cmp;
bc0c38d1 2656 int neg = 0;
adf9f195 2657 int ret;
bc0c38d1
SR
2658 int i;
2659
cffae437
SR
2660 if (cnt >= sizeof(buf))
2661 return -EINVAL;
bc0c38d1
SR
2662
2663 if (copy_from_user(&buf, ubuf, cnt))
2664 return -EFAULT;
2665
2666 buf[cnt] = 0;
8d18eaaf 2667 cmp = strstrip(buf);
bc0c38d1 2668
8d18eaaf 2669 if (strncmp(cmp, "no", 2) == 0) {
bc0c38d1
SR
2670 neg = 1;
2671 cmp += 2;
2672 }
2673
2674 for (i = 0; trace_options[i]; i++) {
8d18eaaf 2675 if (strcmp(cmp, trace_options[i]) == 0) {
af4617bd 2676 set_tracer_flags(1 << i, !neg);
bc0c38d1
SR
2677 break;
2678 }
2679 }
adf9f195
FW
2680
2681 /* If no option could be set, test the specific tracer options */
2682 if (!trace_options[i]) {
d8e83d26 2683 mutex_lock(&trace_types_lock);
adf9f195 2684 ret = set_tracer_option(current_trace, cmp, neg);
d8e83d26 2685 mutex_unlock(&trace_types_lock);
adf9f195
FW
2686 if (ret)
2687 return ret;
2688 }
bc0c38d1 2689
cf8517cf 2690 *ppos += cnt;
bc0c38d1
SR
2691
2692 return cnt;
2693}
2694
fdb372ed
LZ
2695static int tracing_trace_options_open(struct inode *inode, struct file *file)
2696{
2697 if (tracing_disabled)
2698 return -ENODEV;
2699 return single_open(file, tracing_trace_options_show, NULL);
2700}
2701
5e2336a0 2702static const struct file_operations tracing_iter_fops = {
fdb372ed
LZ
2703 .open = tracing_trace_options_open,
2704 .read = seq_read,
2705 .llseek = seq_lseek,
2706 .release = single_release,
ee6bce52 2707 .write = tracing_trace_options_write,
bc0c38d1
SR
2708};
2709
7bd2f24c
IM
2710static const char readme_msg[] =
2711 "tracing mini-HOWTO:\n\n"
156f5a78
GL
2712 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2713 "# cat /sys/kernel/debug/tracing/available_tracers\n"
bc2b6871 2714 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
156f5a78 2715 "# cat /sys/kernel/debug/tracing/current_tracer\n"
bc2b6871 2716 "nop\n"
156f5a78
GL
2717 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2718 "# cat /sys/kernel/debug/tracing/current_tracer\n"
7bd2f24c 2719 "sched_switch\n"
156f5a78 2720 "# cat /sys/kernel/debug/tracing/trace_options\n"
7bd2f24c 2721 "noprint-parent nosym-offset nosym-addr noverbose\n"
156f5a78 2722 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
9b5f8b31 2723 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
156f5a78 2724 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
9b5f8b31 2725 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
7bd2f24c
IM
2726;
2727
2728static ssize_t
2729tracing_readme_read(struct file *filp, char __user *ubuf,
2730 size_t cnt, loff_t *ppos)
2731{
2732 return simple_read_from_buffer(ubuf, cnt, ppos,
2733 readme_msg, strlen(readme_msg));
2734}
2735
5e2336a0 2736static const struct file_operations tracing_readme_fops = {
c7078de1
IM
2737 .open = tracing_open_generic,
2738 .read = tracing_readme_read,
b444786f 2739 .llseek = generic_file_llseek,
7bd2f24c
IM
2740};
2741
69abe6a5
AP
2742static ssize_t
2743tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2744 size_t cnt, loff_t *ppos)
2745{
2746 char *buf_comm;
2747 char *file_buf;
2748 char *buf;
2749 int len = 0;
2750 int pid;
2751 int i;
2752
2753 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2754 if (!file_buf)
2755 return -ENOMEM;
2756
2757 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2758 if (!buf_comm) {
2759 kfree(file_buf);
2760 return -ENOMEM;
2761 }
2762
2763 buf = file_buf;
2764
2765 for (i = 0; i < SAVED_CMDLINES; i++) {
2766 int r;
2767
2768 pid = map_cmdline_to_pid[i];
2769 if (pid == -1 || pid == NO_CMDLINE_MAP)
2770 continue;
2771
2772 trace_find_cmdline(pid, buf_comm);
2773 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2774 buf += r;
2775 len += r;
2776 }
2777
2778 len = simple_read_from_buffer(ubuf, cnt, ppos,
2779 file_buf, len);
2780
2781 kfree(file_buf);
2782 kfree(buf_comm);
2783
2784 return len;
2785}
2786
2787static const struct file_operations tracing_saved_cmdlines_fops = {
2788 .open = tracing_open_generic,
2789 .read = tracing_saved_cmdlines_read,
b444786f 2790 .llseek = generic_file_llseek,
69abe6a5
AP
2791};
2792
bc0c38d1
SR
2793static ssize_t
2794tracing_ctrl_read(struct file *filp, char __user *ubuf,
2795 size_t cnt, loff_t *ppos)
2796{
bc0c38d1
SR
2797 char buf[64];
2798 int r;
2799
9036990d 2800 r = sprintf(buf, "%u\n", tracer_enabled);
4e3c3333 2801 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2802}
2803
2804static ssize_t
2805tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2806 size_t cnt, loff_t *ppos)
2807{
2808 struct trace_array *tr = filp->private_data;
5e39841c 2809 unsigned long val;
c6caeeb1 2810 int ret;
bc0c38d1 2811
22fe9b54
PH
2812 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
2813 if (ret)
c6caeeb1 2814 return ret;
bc0c38d1
SR
2815
2816 val = !!val;
2817
2818 mutex_lock(&trace_types_lock);
9036990d 2819 if (tracer_enabled ^ val) {
6752ab4a
SR
2820
2821 /* Only need to warn if this is used to change the state */
2822 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2823
9036990d 2824 if (val) {
bc0c38d1 2825 tracer_enabled = 1;
9036990d
SR
2826 if (current_trace->start)
2827 current_trace->start(tr);
2828 tracing_start();
2829 } else {
bc0c38d1 2830 tracer_enabled = 0;
9036990d
SR
2831 tracing_stop();
2832 if (current_trace->stop)
2833 current_trace->stop(tr);
2834 }
bc0c38d1
SR
2835 }
2836 mutex_unlock(&trace_types_lock);
2837
cf8517cf 2838 *ppos += cnt;
bc0c38d1
SR
2839
2840 return cnt;
2841}
2842
2843static ssize_t
2844tracing_set_trace_read(struct file *filp, char __user *ubuf,
2845 size_t cnt, loff_t *ppos)
2846{
ee6c2c1b 2847 char buf[MAX_TRACER_SIZE+2];
bc0c38d1
SR
2848 int r;
2849
2850 mutex_lock(&trace_types_lock);
2851 if (current_trace)
2852 r = sprintf(buf, "%s\n", current_trace->name);
2853 else
2854 r = sprintf(buf, "\n");
2855 mutex_unlock(&trace_types_lock);
2856
4bf39a94 2857 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2858}
2859
b6f11df2
ACM
2860int tracer_init(struct tracer *t, struct trace_array *tr)
2861{
2862 tracing_reset_online_cpus(tr);
2863 return t->init(tr);
2864}
2865
4f271a2a 2866static int __tracing_resize_ring_buffer(unsigned long size)
73c5162a
SR
2867{
2868 int ret;
2869
2870 /*
2871 * If kernel or user changes the size of the ring buffer
a123c52b
SR
2872 * we use the size that was given, and we can forget about
2873 * expanding it later.
73c5162a
SR
2874 */
2875 ring_buffer_expanded = 1;
2876
2877 ret = ring_buffer_resize(global_trace.buffer, size);
2878 if (ret < 0)
2879 return ret;
2880
ef710e10
KM
2881 if (!current_trace->use_max_tr)
2882 goto out;
2883
73c5162a
SR
2884 ret = ring_buffer_resize(max_tr.buffer, size);
2885 if (ret < 0) {
2886 int r;
2887
2888 r = ring_buffer_resize(global_trace.buffer,
2889 global_trace.entries);
2890 if (r < 0) {
a123c52b
SR
2891 /*
2892 * AARGH! We are left with different
2893 * size max buffer!!!!
2894 * The max buffer is our "snapshot" buffer.
2895 * When a tracer needs a snapshot (one of the
2896 * latency tracers), it swaps the max buffer
2897 * with the saved snap shot. We succeeded to
2898 * update the size of the main buffer, but failed to
2899 * update the size of the max buffer. But when we tried
2900 * to reset the main buffer to the original size, we
2901 * failed there too. This is very unlikely to
2902 * happen, but if it does, warn and kill all
2903 * tracing.
2904 */
73c5162a
SR
2905 WARN_ON(1);
2906 tracing_disabled = 1;
2907 }
2908 return ret;
2909 }
2910
ef710e10
KM
2911 max_tr.entries = size;
2912 out:
73c5162a
SR
2913 global_trace.entries = size;
2914
2915 return ret;
2916}
2917
4f271a2a
VN
2918static ssize_t tracing_resize_ring_buffer(unsigned long size)
2919{
2920 int cpu, ret = size;
2921
2922 mutex_lock(&trace_types_lock);
2923
2924 tracing_stop();
2925
2926 /* disable all cpu buffers */
2927 for_each_tracing_cpu(cpu) {
2928 if (global_trace.data[cpu])
2929 atomic_inc(&global_trace.data[cpu]->disabled);
2930 if (max_tr.data[cpu])
2931 atomic_inc(&max_tr.data[cpu]->disabled);
2932 }
2933
2934 if (size != global_trace.entries)
2935 ret = __tracing_resize_ring_buffer(size);
2936
2937 if (ret < 0)
2938 ret = -ENOMEM;
2939
2940 for_each_tracing_cpu(cpu) {
2941 if (global_trace.data[cpu])
2942 atomic_dec(&global_trace.data[cpu]->disabled);
2943 if (max_tr.data[cpu])
2944 atomic_dec(&max_tr.data[cpu]->disabled);
2945 }
2946
2947 tracing_start();
2948 mutex_unlock(&trace_types_lock);
2949
2950 return ret;
2951}
2952
ef710e10 2953
1852fcce
SR
2954/**
2955 * tracing_update_buffers - used by tracing facility to expand ring buffers
2956 *
2957 * To save on memory when the tracing is never used on a system with it
2958 * configured in. The ring buffers are set to a minimum size. But once
2959 * a user starts to use the tracing facility, then they need to grow
2960 * to their default size.
2961 *
2962 * This function is to be called when a tracer is about to be used.
2963 */
2964int tracing_update_buffers(void)
2965{
2966 int ret = 0;
2967
1027fcb2 2968 mutex_lock(&trace_types_lock);
1852fcce 2969 if (!ring_buffer_expanded)
4f271a2a 2970 ret = __tracing_resize_ring_buffer(trace_buf_size);
1027fcb2 2971 mutex_unlock(&trace_types_lock);
1852fcce
SR
2972
2973 return ret;
2974}
2975
577b785f
SR
2976struct trace_option_dentry;
2977
2978static struct trace_option_dentry *
2979create_trace_option_files(struct tracer *tracer);
2980
2981static void
2982destroy_trace_option_files(struct trace_option_dentry *topts);
2983
b2821ae6 2984static int tracing_set_tracer(const char *buf)
bc0c38d1 2985{
577b785f 2986 static struct trace_option_dentry *topts;
bc0c38d1
SR
2987 struct trace_array *tr = &global_trace;
2988 struct tracer *t;
d9e54076 2989 int ret = 0;
bc0c38d1 2990
1027fcb2
SR
2991 mutex_lock(&trace_types_lock);
2992
73c5162a 2993 if (!ring_buffer_expanded) {
4f271a2a 2994 ret = __tracing_resize_ring_buffer(trace_buf_size);
73c5162a 2995 if (ret < 0)
59f586db 2996 goto out;
73c5162a
SR
2997 ret = 0;
2998 }
2999
bc0c38d1
SR
3000 for (t = trace_types; t; t = t->next) {
3001 if (strcmp(t->name, buf) == 0)
3002 break;
3003 }
c2931e05
FW
3004 if (!t) {
3005 ret = -EINVAL;
3006 goto out;
3007 }
3008 if (t == current_trace)
bc0c38d1
SR
3009 goto out;
3010
9f029e83 3011 trace_branch_disable();
bc0c38d1
SR
3012 if (current_trace && current_trace->reset)
3013 current_trace->reset(tr);
ef710e10
KM
3014 if (current_trace && current_trace->use_max_tr) {
3015 /*
3016 * We don't free the ring buffer. instead, resize it because
3017 * The max_tr ring buffer has some state (e.g. ring->clock) and
3018 * we want preserve it.
3019 */
3020 ring_buffer_resize(max_tr.buffer, 1);
3021 max_tr.entries = 1;
3022 }
577b785f
SR
3023 destroy_trace_option_files(topts);
3024
bc0c38d1 3025 current_trace = t;
577b785f
SR
3026
3027 topts = create_trace_option_files(current_trace);
ef710e10
KM
3028 if (current_trace->use_max_tr) {
3029 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
3030 if (ret < 0)
3031 goto out;
3032 max_tr.entries = global_trace.entries;
3033 }
577b785f 3034
1c80025a 3035 if (t->init) {
b6f11df2 3036 ret = tracer_init(t, tr);
1c80025a
FW
3037 if (ret)
3038 goto out;
3039 }
bc0c38d1 3040
9f029e83 3041 trace_branch_enable(tr);
bc0c38d1
SR
3042 out:
3043 mutex_unlock(&trace_types_lock);
3044
d9e54076
PZ
3045 return ret;
3046}
3047
3048static ssize_t
3049tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3050 size_t cnt, loff_t *ppos)
3051{
ee6c2c1b 3052 char buf[MAX_TRACER_SIZE+1];
d9e54076
PZ
3053 int i;
3054 size_t ret;
e6e7a65a
FW
3055 int err;
3056
3057 ret = cnt;
d9e54076 3058
ee6c2c1b
LZ
3059 if (cnt > MAX_TRACER_SIZE)
3060 cnt = MAX_TRACER_SIZE;
d9e54076
PZ
3061
3062 if (copy_from_user(&buf, ubuf, cnt))
3063 return -EFAULT;
3064
3065 buf[cnt] = 0;
3066
3067 /* strip ending whitespace. */
3068 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3069 buf[i] = 0;
3070
e6e7a65a
FW
3071 err = tracing_set_tracer(buf);
3072 if (err)
3073 return err;
d9e54076 3074
cf8517cf 3075 *ppos += ret;
bc0c38d1 3076
c2931e05 3077 return ret;
bc0c38d1
SR
3078}
3079
3080static ssize_t
3081tracing_max_lat_read(struct file *filp, char __user *ubuf,
3082 size_t cnt, loff_t *ppos)
3083{
3084 unsigned long *ptr = filp->private_data;
3085 char buf[64];
3086 int r;
3087
cffae437 3088 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 3089 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
3090 if (r > sizeof(buf))
3091 r = sizeof(buf);
4bf39a94 3092 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
3093}
3094
3095static ssize_t
3096tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3097 size_t cnt, loff_t *ppos)
3098{
5e39841c 3099 unsigned long *ptr = filp->private_data;
5e39841c 3100 unsigned long val;
c6caeeb1 3101 int ret;
bc0c38d1 3102
22fe9b54
PH
3103 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3104 if (ret)
c6caeeb1 3105 return ret;
bc0c38d1
SR
3106
3107 *ptr = val * 1000;
3108
3109 return cnt;
3110}
3111
b3806b43
SR
3112static int tracing_open_pipe(struct inode *inode, struct file *filp)
3113{
b04cc6b1 3114 long cpu_file = (long) inode->i_private;
b3806b43 3115 struct trace_iterator *iter;
b04cc6b1 3116 int ret = 0;
b3806b43
SR
3117
3118 if (tracing_disabled)
3119 return -ENODEV;
3120
b04cc6b1
FW
3121 mutex_lock(&trace_types_lock);
3122
b3806b43
SR
3123 /* create a buffer to store the information to pass to userspace */
3124 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
b04cc6b1
FW
3125 if (!iter) {
3126 ret = -ENOMEM;
3127 goto out;
3128 }
b3806b43 3129
d7350c3f
FW
3130 /*
3131 * We make a copy of the current tracer to avoid concurrent
3132 * changes on it while we are reading.
3133 */
3134 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3135 if (!iter->trace) {
3136 ret = -ENOMEM;
3137 goto fail;
3138 }
3139 if (current_trace)
3140 *iter->trace = *current_trace;
3141
4462344e 3142 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
b04cc6b1 3143 ret = -ENOMEM;
d7350c3f 3144 goto fail;
4462344e
RR
3145 }
3146
a309720c 3147 /* trace pipe does not show start of buffer */
4462344e 3148 cpumask_setall(iter->started);
a309720c 3149
112f38a7
SR
3150 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3151 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3152
b04cc6b1 3153 iter->cpu_file = cpu_file;
b3806b43 3154 iter->tr = &global_trace;
d7350c3f 3155 mutex_init(&iter->mutex);
b3806b43
SR
3156 filp->private_data = iter;
3157
107bad8b
SR
3158 if (iter->trace->pipe_open)
3159 iter->trace->pipe_open(iter);
107bad8b 3160
b444786f 3161 nonseekable_open(inode, filp);
b04cc6b1
FW
3162out:
3163 mutex_unlock(&trace_types_lock);
3164 return ret;
d7350c3f
FW
3165
3166fail:
3167 kfree(iter->trace);
3168 kfree(iter);
3169 mutex_unlock(&trace_types_lock);
3170 return ret;
b3806b43
SR
3171}
3172
3173static int tracing_release_pipe(struct inode *inode, struct file *file)
3174{
3175 struct trace_iterator *iter = file->private_data;
3176
b04cc6b1
FW
3177 mutex_lock(&trace_types_lock);
3178
29bf4a5e 3179 if (iter->trace->pipe_close)
c521efd1
SR
3180 iter->trace->pipe_close(iter);
3181
b04cc6b1
FW
3182 mutex_unlock(&trace_types_lock);
3183
4462344e 3184 free_cpumask_var(iter->started);
d7350c3f
FW
3185 mutex_destroy(&iter->mutex);
3186 kfree(iter->trace);
b3806b43 3187 kfree(iter);
b3806b43
SR
3188
3189 return 0;
3190}
3191
2a2cc8f7
SSP
3192static unsigned int
3193tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3194{
3195 struct trace_iterator *iter = filp->private_data;
3196
3197 if (trace_flags & TRACE_ITER_BLOCK) {
3198 /*
3199 * Always select as readable when in blocking mode
3200 */
3201 return POLLIN | POLLRDNORM;
afc2abc0 3202 } else {
2a2cc8f7
SSP
3203 if (!trace_empty(iter))
3204 return POLLIN | POLLRDNORM;
3205 poll_wait(filp, &trace_wait, poll_table);
3206 if (!trace_empty(iter))
3207 return POLLIN | POLLRDNORM;
3208
3209 return 0;
3210 }
3211}
3212
6eaaa5d5
FW
3213
3214void default_wait_pipe(struct trace_iterator *iter)
3215{
3216 DEFINE_WAIT(wait);
3217
3218 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3219
3220 if (trace_empty(iter))
3221 schedule();
3222
3223 finish_wait(&trace_wait, &wait);
3224}
3225
3226/*
3227 * This is a make-shift waitqueue.
3228 * A tracer might use this callback on some rare cases:
3229 *
3230 * 1) the current tracer might hold the runqueue lock when it wakes up
3231 * a reader, hence a deadlock (sched, function, and function graph tracers)
3232 * 2) the function tracers, trace all functions, we don't want
3233 * the overhead of calling wake_up and friends
3234 * (and tracing them too)
3235 *
3236 * Anyway, this is really very primitive wakeup.
3237 */
3238void poll_wait_pipe(struct trace_iterator *iter)
3239{
3240 set_current_state(TASK_INTERRUPTIBLE);
3241 /* sleep for 100 msecs, and try again. */
3242 schedule_timeout(HZ / 10);
3243}
3244
ff98781b
EGM
3245/* Must be called with trace_types_lock mutex held. */
3246static int tracing_wait_pipe(struct file *filp)
b3806b43
SR
3247{
3248 struct trace_iterator *iter = filp->private_data;
b3806b43 3249
b3806b43 3250 while (trace_empty(iter)) {
2dc8f095 3251
107bad8b 3252 if ((filp->f_flags & O_NONBLOCK)) {
ff98781b 3253 return -EAGAIN;
107bad8b 3254 }
2dc8f095 3255
d7350c3f 3256 mutex_unlock(&iter->mutex);
107bad8b 3257
6eaaa5d5 3258 iter->trace->wait_pipe(iter);
b3806b43 3259
d7350c3f 3260 mutex_lock(&iter->mutex);
107bad8b 3261
6eaaa5d5 3262 if (signal_pending(current))
ff98781b 3263 return -EINTR;
b3806b43
SR
3264
3265 /*
3266 * We block until we read something and tracing is disabled.
3267 * We still block if tracing is disabled, but we have never
3268 * read anything. This allows a user to cat this file, and
3269 * then enable tracing. But after we have read something,
3270 * we give an EOF when tracing is again disabled.
3271 *
3272 * iter->pos will be 0 if we haven't read anything.
3273 */
3274 if (!tracer_enabled && iter->pos)
3275 break;
b3806b43
SR
3276 }
3277
ff98781b
EGM
3278 return 1;
3279}
3280
3281/*
3282 * Consumer reader.
3283 */
3284static ssize_t
3285tracing_read_pipe(struct file *filp, char __user *ubuf,
3286 size_t cnt, loff_t *ppos)
3287{
3288 struct trace_iterator *iter = filp->private_data;
d7350c3f 3289 static struct tracer *old_tracer;
ff98781b
EGM
3290 ssize_t sret;
3291
3292 /* return any leftover data */
3293 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3294 if (sret != -EBUSY)
3295 return sret;
3296
f9520750 3297 trace_seq_init(&iter->seq);
ff98781b 3298
d7350c3f 3299 /* copy the tracer to avoid using a global lock all around */
ff98781b 3300 mutex_lock(&trace_types_lock);
d7350c3f
FW
3301 if (unlikely(old_tracer != current_trace && current_trace)) {
3302 old_tracer = current_trace;
3303 *iter->trace = *current_trace;
3304 }
3305 mutex_unlock(&trace_types_lock);
3306
3307 /*
3308 * Avoid more than one consumer on a single file descriptor
3309 * This is just a matter of traces coherency, the ring buffer itself
3310 * is protected.
3311 */
3312 mutex_lock(&iter->mutex);
ff98781b
EGM
3313 if (iter->trace->read) {
3314 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3315 if (sret)
3316 goto out;
3317 }
3318
3319waitagain:
3320 sret = tracing_wait_pipe(filp);
3321 if (sret <= 0)
3322 goto out;
3323
b3806b43 3324 /* stop when tracing is finished */
ff98781b
EGM
3325 if (trace_empty(iter)) {
3326 sret = 0;
107bad8b 3327 goto out;
ff98781b 3328 }
b3806b43
SR
3329
3330 if (cnt >= PAGE_SIZE)
3331 cnt = PAGE_SIZE - 1;
3332
53d0aa77 3333 /* reset all but tr, trace, and overruns */
53d0aa77
SR
3334 memset(&iter->seq, 0,
3335 sizeof(struct trace_iterator) -
3336 offsetof(struct trace_iterator, seq));
4823ed7e 3337 iter->pos = -1;
b3806b43 3338
4f535968 3339 trace_event_read_lock();
7e53bd42 3340 trace_access_lock(iter->cpu_file);
955b61e5 3341 while (trace_find_next_entry_inc(iter) != NULL) {
2c4f035f 3342 enum print_line_t ret;
088b1e42
SR
3343 int len = iter->seq.len;
3344
f9896bf3 3345 ret = print_trace_line(iter);
2c4f035f 3346 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42
SR
3347 /* don't print partial lines */
3348 iter->seq.len = len;
b3806b43 3349 break;
088b1e42 3350 }
b91facc3
FW
3351 if (ret != TRACE_TYPE_NO_CONSUME)
3352 trace_consume(iter);
b3806b43
SR
3353
3354 if (iter->seq.len >= cnt)
3355 break;
ee5e51f5
JO
3356
3357 /*
3358 * Setting the full flag means we reached the trace_seq buffer
3359 * size and we should leave by partial output condition above.
3360 * One of the trace_seq_* functions is not used properly.
3361 */
3362 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3363 iter->ent->type);
b3806b43 3364 }
7e53bd42 3365 trace_access_unlock(iter->cpu_file);
4f535968 3366 trace_event_read_unlock();
b3806b43 3367
b3806b43 3368 /* Now copy what we have to the user */
6c6c2796
PP
3369 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3370 if (iter->seq.readpos >= iter->seq.len)
f9520750 3371 trace_seq_init(&iter->seq);
9ff4b974
PP
3372
3373 /*
25985edc 3374 * If there was nothing to send to user, in spite of consuming trace
9ff4b974
PP
3375 * entries, go back to wait for more entries.
3376 */
6c6c2796 3377 if (sret == -EBUSY)
9ff4b974 3378 goto waitagain;
b3806b43 3379
107bad8b 3380out:
d7350c3f 3381 mutex_unlock(&iter->mutex);
107bad8b 3382
6c6c2796 3383 return sret;
b3806b43
SR
3384}
3385
3c56819b
EGM
3386static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3387 struct pipe_buffer *buf)
3388{
3389 __free_page(buf->page);
3390}
3391
3392static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3393 unsigned int idx)
3394{
3395 __free_page(spd->pages[idx]);
3396}
3397
28dfef8f 3398static const struct pipe_buf_operations tracing_pipe_buf_ops = {
34cd4998
SR
3399 .can_merge = 0,
3400 .map = generic_pipe_buf_map,
3401 .unmap = generic_pipe_buf_unmap,
3402 .confirm = generic_pipe_buf_confirm,
3403 .release = tracing_pipe_buf_release,
3404 .steal = generic_pipe_buf_steal,
3405 .get = generic_pipe_buf_get,
3c56819b
EGM
3406};
3407
34cd4998 3408static size_t
fa7c7f6e 3409tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
34cd4998
SR
3410{
3411 size_t count;
3412 int ret;
3413
3414 /* Seq buffer is page-sized, exactly what we need. */
3415 for (;;) {
3416 count = iter->seq.len;
3417 ret = print_trace_line(iter);
3418 count = iter->seq.len - count;
3419 if (rem < count) {
3420 rem = 0;
3421 iter->seq.len -= count;
3422 break;
3423 }
3424 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3425 iter->seq.len -= count;
3426 break;
3427 }
3428
74e7ff8c
LJ
3429 if (ret != TRACE_TYPE_NO_CONSUME)
3430 trace_consume(iter);
34cd4998 3431 rem -= count;
955b61e5 3432 if (!trace_find_next_entry_inc(iter)) {
34cd4998
SR
3433 rem = 0;
3434 iter->ent = NULL;
3435 break;
3436 }
3437 }
3438
3439 return rem;
3440}
3441
3c56819b
EGM
3442static ssize_t tracing_splice_read_pipe(struct file *filp,
3443 loff_t *ppos,
3444 struct pipe_inode_info *pipe,
3445 size_t len,
3446 unsigned int flags)
3447{
35f3d14d
JA
3448 struct page *pages_def[PIPE_DEF_BUFFERS];
3449 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3c56819b
EGM
3450 struct trace_iterator *iter = filp->private_data;
3451 struct splice_pipe_desc spd = {
35f3d14d
JA
3452 .pages = pages_def,
3453 .partial = partial_def,
34cd4998
SR
3454 .nr_pages = 0, /* This gets updated below. */
3455 .flags = flags,
3456 .ops = &tracing_pipe_buf_ops,
3457 .spd_release = tracing_spd_release_pipe,
3c56819b 3458 };
d7350c3f 3459 static struct tracer *old_tracer;
3c56819b 3460 ssize_t ret;
34cd4998 3461 size_t rem;
3c56819b
EGM
3462 unsigned int i;
3463
35f3d14d
JA
3464 if (splice_grow_spd(pipe, &spd))
3465 return -ENOMEM;
3466
d7350c3f 3467 /* copy the tracer to avoid using a global lock all around */
3c56819b 3468 mutex_lock(&trace_types_lock);
d7350c3f
FW
3469 if (unlikely(old_tracer != current_trace && current_trace)) {
3470 old_tracer = current_trace;
3471 *iter->trace = *current_trace;
3472 }
3473 mutex_unlock(&trace_types_lock);
3474
3475 mutex_lock(&iter->mutex);
3c56819b
EGM
3476
3477 if (iter->trace->splice_read) {
3478 ret = iter->trace->splice_read(iter, filp,
3479 ppos, pipe, len, flags);
3480 if (ret)
34cd4998 3481 goto out_err;
3c56819b
EGM
3482 }
3483
3484 ret = tracing_wait_pipe(filp);
3485 if (ret <= 0)
34cd4998 3486 goto out_err;
3c56819b 3487
955b61e5 3488 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3c56819b 3489 ret = -EFAULT;
34cd4998 3490 goto out_err;
3c56819b
EGM
3491 }
3492
4f535968 3493 trace_event_read_lock();
7e53bd42 3494 trace_access_lock(iter->cpu_file);
4f535968 3495
3c56819b 3496 /* Fill as many pages as possible. */
35f3d14d
JA
3497 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3498 spd.pages[i] = alloc_page(GFP_KERNEL);
3499 if (!spd.pages[i])
34cd4998 3500 break;
3c56819b 3501
fa7c7f6e 3502 rem = tracing_fill_pipe_page(rem, iter);
3c56819b
EGM
3503
3504 /* Copy the data into the page, so we can start over. */
3505 ret = trace_seq_to_buffer(&iter->seq,
35f3d14d 3506 page_address(spd.pages[i]),
3c56819b
EGM
3507 iter->seq.len);
3508 if (ret < 0) {
35f3d14d 3509 __free_page(spd.pages[i]);
3c56819b
EGM
3510 break;
3511 }
35f3d14d
JA
3512 spd.partial[i].offset = 0;
3513 spd.partial[i].len = iter->seq.len;
3c56819b 3514
f9520750 3515 trace_seq_init(&iter->seq);
3c56819b
EGM
3516 }
3517
7e53bd42 3518 trace_access_unlock(iter->cpu_file);
4f535968 3519 trace_event_read_unlock();
d7350c3f 3520 mutex_unlock(&iter->mutex);
3c56819b
EGM
3521
3522 spd.nr_pages = i;
3523
35f3d14d
JA
3524 ret = splice_to_pipe(pipe, &spd);
3525out:
3526 splice_shrink_spd(pipe, &spd);
3527 return ret;
3c56819b 3528
34cd4998 3529out_err:
d7350c3f 3530 mutex_unlock(&iter->mutex);
35f3d14d 3531 goto out;
3c56819b
EGM
3532}
3533
a98a3c3f
SR
3534static ssize_t
3535tracing_entries_read(struct file *filp, char __user *ubuf,
3536 size_t cnt, loff_t *ppos)
3537{
3538 struct trace_array *tr = filp->private_data;
db526ca3 3539 char buf[96];
a98a3c3f
SR
3540 int r;
3541
db526ca3
SR
3542 mutex_lock(&trace_types_lock);
3543 if (!ring_buffer_expanded)
3544 r = sprintf(buf, "%lu (expanded: %lu)\n",
3545 tr->entries >> 10,
3546 trace_buf_size >> 10);
3547 else
3548 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3549 mutex_unlock(&trace_types_lock);
3550
a98a3c3f
SR
3551 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3552}
3553
3554static ssize_t
3555tracing_entries_write(struct file *filp, const char __user *ubuf,
3556 size_t cnt, loff_t *ppos)
3557{
3558 unsigned long val;
4f271a2a 3559 int ret;
a98a3c3f 3560
22fe9b54
PH
3561 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3562 if (ret)
c6caeeb1 3563 return ret;
a98a3c3f
SR
3564
3565 /* must have at least 1 entry */
3566 if (!val)
3567 return -EINVAL;
3568
1696b2b0
SR
3569 /* value is in KB */
3570 val <<= 10;
3571
4f271a2a
VN
3572 ret = tracing_resize_ring_buffer(val);
3573 if (ret < 0)
3574 return ret;
a98a3c3f 3575
cf8517cf 3576 *ppos += cnt;
a98a3c3f 3577
4f271a2a
VN
3578 return cnt;
3579}
bf5e6519 3580
f81ab074
VN
3581static ssize_t
3582tracing_total_entries_read(struct file *filp, char __user *ubuf,
3583 size_t cnt, loff_t *ppos)
3584{
3585 struct trace_array *tr = filp->private_data;
3586 char buf[64];
3587 int r, cpu;
3588 unsigned long size = 0, expanded_size = 0;
3589
3590 mutex_lock(&trace_types_lock);
3591 for_each_tracing_cpu(cpu) {
3592 size += tr->entries >> 10;
3593 if (!ring_buffer_expanded)
3594 expanded_size += trace_buf_size >> 10;
3595 }
3596 if (ring_buffer_expanded)
3597 r = sprintf(buf, "%lu\n", size);
3598 else
3599 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3600 mutex_unlock(&trace_types_lock);
3601
3602 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3603}
3604
4f271a2a
VN
3605static ssize_t
3606tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3607 size_t cnt, loff_t *ppos)
3608{
3609 /*
3610 * There is no need to read what the user has written, this function
3611 * is just to make sure that there is no error when "echo" is used
3612 */
3613
3614 *ppos += cnt;
a98a3c3f
SR
3615
3616 return cnt;
3617}
3618
4f271a2a
VN
3619static int
3620tracing_free_buffer_release(struct inode *inode, struct file *filp)
3621{
cf30cf67
SR
3622 /* disable tracing ? */
3623 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3624 tracing_off();
4f271a2a
VN
3625 /* resize the ring buffer to 0 */
3626 tracing_resize_ring_buffer(0);
3627
3628 return 0;
3629}
3630
5bf9a1ee
PP
3631static ssize_t
3632tracing_mark_write(struct file *filp, const char __user *ubuf,
3633 size_t cnt, loff_t *fpos)
3634{
d696b58c
SR
3635 unsigned long addr = (unsigned long)ubuf;
3636 struct ring_buffer_event *event;
3637 struct ring_buffer *buffer;
3638 struct print_entry *entry;
3639 unsigned long irq_flags;
3640 struct page *pages[2];
3641 int nr_pages = 1;
3642 ssize_t written;
3643 void *page1;
3644 void *page2;
3645 int offset;
3646 int size;
3647 int len;
3648 int ret;
5bf9a1ee 3649
c76f0694 3650 if (tracing_disabled)
5bf9a1ee
PP
3651 return -EINVAL;
3652
3653 if (cnt > TRACE_BUF_SIZE)
3654 cnt = TRACE_BUF_SIZE;
3655
d696b58c
SR
3656 /*
3657 * Userspace is injecting traces into the kernel trace buffer.
3658 * We want to be as non intrusive as possible.
3659 * To do so, we do not want to allocate any special buffers
3660 * or take any locks, but instead write the userspace data
3661 * straight into the ring buffer.
3662 *
3663 * First we need to pin the userspace buffer into memory,
3664 * which, most likely it is, because it just referenced it.
3665 * But there's no guarantee that it is. By using get_user_pages_fast()
3666 * and kmap_atomic/kunmap_atomic() we can get access to the
3667 * pages directly. We then write the data directly into the
3668 * ring buffer.
3669 */
3670 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
5bf9a1ee 3671
d696b58c
SR
3672 /* check if we cross pages */
3673 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
3674 nr_pages = 2;
3675
3676 offset = addr & (PAGE_SIZE - 1);
3677 addr &= PAGE_MASK;
3678
3679 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
3680 if (ret < nr_pages) {
3681 while (--ret >= 0)
3682 put_page(pages[ret]);
3683 written = -EFAULT;
3684 goto out;
5bf9a1ee 3685 }
d696b58c
SR
3686
3687 page1 = kmap_atomic(pages[0]);
3688 if (nr_pages == 2)
3689 page2 = kmap_atomic(pages[1]);
3690
3691 local_save_flags(irq_flags);
3692 size = sizeof(*entry) + cnt + 2; /* possible \n added */
3693 buffer = global_trace.buffer;
3694 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3695 irq_flags, preempt_count());
3696 if (!event) {
3697 /* Ring buffer disabled, return as if not open for write */
3698 written = -EBADF;
3699 goto out_unlock;
5bf9a1ee 3700 }
d696b58c
SR
3701
3702 entry = ring_buffer_event_data(event);
3703 entry->ip = _THIS_IP_;
3704
3705 if (nr_pages == 2) {
3706 len = PAGE_SIZE - offset;
3707 memcpy(&entry->buf, page1 + offset, len);
3708 memcpy(&entry->buf[len], page2, cnt - len);
c13d2f7c 3709 } else
d696b58c 3710 memcpy(&entry->buf, page1 + offset, cnt);
5bf9a1ee 3711
d696b58c
SR
3712 if (entry->buf[cnt - 1] != '\n') {
3713 entry->buf[cnt] = '\n';
3714 entry->buf[cnt + 1] = '\0';
3715 } else
3716 entry->buf[cnt] = '\0';
3717
3718 ring_buffer_unlock_commit(buffer, event);
5bf9a1ee 3719
d696b58c 3720 written = cnt;
5bf9a1ee 3721
d696b58c 3722 *fpos += written;
1aa54bca 3723
d696b58c
SR
3724 out_unlock:
3725 if (nr_pages == 2)
3726 kunmap_atomic(page2);
3727 kunmap_atomic(page1);
3728 while (nr_pages > 0)
3729 put_page(pages[--nr_pages]);
3730 out:
1aa54bca 3731 return written;
5bf9a1ee
PP
3732}
3733
13f16d20 3734static int tracing_clock_show(struct seq_file *m, void *v)
5079f326 3735{
5079f326
Z
3736 int i;
3737
3738 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
13f16d20 3739 seq_printf(m,
5079f326
Z
3740 "%s%s%s%s", i ? " " : "",
3741 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3742 i == trace_clock_id ? "]" : "");
13f16d20 3743 seq_putc(m, '\n');
5079f326 3744
13f16d20 3745 return 0;
5079f326
Z
3746}
3747
3748static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3749 size_t cnt, loff_t *fpos)
3750{
3751 char buf[64];
3752 const char *clockstr;
3753 int i;
3754
3755 if (cnt >= sizeof(buf))
3756 return -EINVAL;
3757
3758 if (copy_from_user(&buf, ubuf, cnt))
3759 return -EFAULT;
3760
3761 buf[cnt] = 0;
3762
3763 clockstr = strstrip(buf);
3764
3765 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3766 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3767 break;
3768 }
3769 if (i == ARRAY_SIZE(trace_clocks))
3770 return -EINVAL;
3771
3772 trace_clock_id = i;
3773
3774 mutex_lock(&trace_types_lock);
3775
3776 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3777 if (max_tr.buffer)
3778 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3779
3780 mutex_unlock(&trace_types_lock);
3781
3782 *fpos += cnt;
3783
3784 return cnt;
3785}
3786
13f16d20
LZ
3787static int tracing_clock_open(struct inode *inode, struct file *file)
3788{
3789 if (tracing_disabled)
3790 return -ENODEV;
3791 return single_open(file, tracing_clock_show, NULL);
3792}
3793
5e2336a0 3794static const struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
3795 .open = tracing_open_generic,
3796 .read = tracing_max_lat_read,
3797 .write = tracing_max_lat_write,
b444786f 3798 .llseek = generic_file_llseek,
bc0c38d1
SR
3799};
3800
5e2336a0 3801static const struct file_operations tracing_ctrl_fops = {
4bf39a94
IM
3802 .open = tracing_open_generic,
3803 .read = tracing_ctrl_read,
3804 .write = tracing_ctrl_write,
b444786f 3805 .llseek = generic_file_llseek,
bc0c38d1
SR
3806};
3807
5e2336a0 3808static const struct file_operations set_tracer_fops = {
4bf39a94
IM
3809 .open = tracing_open_generic,
3810 .read = tracing_set_trace_read,
3811 .write = tracing_set_trace_write,
b444786f 3812 .llseek = generic_file_llseek,
bc0c38d1
SR
3813};
3814
5e2336a0 3815static const struct file_operations tracing_pipe_fops = {
4bf39a94 3816 .open = tracing_open_pipe,
2a2cc8f7 3817 .poll = tracing_poll_pipe,
4bf39a94 3818 .read = tracing_read_pipe,
3c56819b 3819 .splice_read = tracing_splice_read_pipe,
4bf39a94 3820 .release = tracing_release_pipe,
b444786f 3821 .llseek = no_llseek,
b3806b43
SR
3822};
3823
5e2336a0 3824static const struct file_operations tracing_entries_fops = {
a98a3c3f
SR
3825 .open = tracing_open_generic,
3826 .read = tracing_entries_read,
3827 .write = tracing_entries_write,
b444786f 3828 .llseek = generic_file_llseek,
a98a3c3f
SR
3829};
3830
f81ab074
VN
3831static const struct file_operations tracing_total_entries_fops = {
3832 .open = tracing_open_generic,
3833 .read = tracing_total_entries_read,
3834 .llseek = generic_file_llseek,
3835};
3836
4f271a2a
VN
3837static const struct file_operations tracing_free_buffer_fops = {
3838 .write = tracing_free_buffer_write,
3839 .release = tracing_free_buffer_release,
3840};
3841
5e2336a0 3842static const struct file_operations tracing_mark_fops = {
43a15386 3843 .open = tracing_open_generic,
5bf9a1ee 3844 .write = tracing_mark_write,
b444786f 3845 .llseek = generic_file_llseek,
5bf9a1ee
PP
3846};
3847
5079f326 3848static const struct file_operations trace_clock_fops = {
13f16d20
LZ
3849 .open = tracing_clock_open,
3850 .read = seq_read,
3851 .llseek = seq_lseek,
3852 .release = single_release,
5079f326
Z
3853 .write = tracing_clock_write,
3854};
3855
2cadf913
SR
3856struct ftrace_buffer_info {
3857 struct trace_array *tr;
3858 void *spare;
3859 int cpu;
3860 unsigned int read;
3861};
3862
3863static int tracing_buffers_open(struct inode *inode, struct file *filp)
3864{
3865 int cpu = (int)(long)inode->i_private;
3866 struct ftrace_buffer_info *info;
3867
3868 if (tracing_disabled)
3869 return -ENODEV;
3870
3871 info = kzalloc(sizeof(*info), GFP_KERNEL);
3872 if (!info)
3873 return -ENOMEM;
3874
3875 info->tr = &global_trace;
3876 info->cpu = cpu;
ddd538f3 3877 info->spare = NULL;
2cadf913
SR
3878 /* Force reading ring buffer for first read */
3879 info->read = (unsigned int)-1;
2cadf913
SR
3880
3881 filp->private_data = info;
3882
d1e7e02f 3883 return nonseekable_open(inode, filp);
2cadf913
SR
3884}
3885
3886static ssize_t
3887tracing_buffers_read(struct file *filp, char __user *ubuf,
3888 size_t count, loff_t *ppos)
3889{
3890 struct ftrace_buffer_info *info = filp->private_data;
2cadf913
SR
3891 ssize_t ret;
3892 size_t size;
3893
2dc5d12b
SR
3894 if (!count)
3895 return 0;
3896
ddd538f3 3897 if (!info->spare)
7ea59064 3898 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
ddd538f3
LJ
3899 if (!info->spare)
3900 return -ENOMEM;
3901
2cadf913
SR
3902 /* Do we have previous read data to read? */
3903 if (info->read < PAGE_SIZE)
3904 goto read;
3905
7e53bd42 3906 trace_access_lock(info->cpu);
2cadf913
SR
3907 ret = ring_buffer_read_page(info->tr->buffer,
3908 &info->spare,
3909 count,
3910 info->cpu, 0);
7e53bd42 3911 trace_access_unlock(info->cpu);
2cadf913
SR
3912 if (ret < 0)
3913 return 0;
3914
436fc280
SR
3915 info->read = 0;
3916
2cadf913
SR
3917read:
3918 size = PAGE_SIZE - info->read;
3919 if (size > count)
3920 size = count;
3921
3922 ret = copy_to_user(ubuf, info->spare + info->read, size);
2dc5d12b 3923 if (ret == size)
2cadf913 3924 return -EFAULT;
2dc5d12b
SR
3925 size -= ret;
3926
2cadf913
SR
3927 *ppos += size;
3928 info->read += size;
3929
3930 return size;
3931}
3932
3933static int tracing_buffers_release(struct inode *inode, struct file *file)
3934{
3935 struct ftrace_buffer_info *info = file->private_data;
3936
ddd538f3
LJ
3937 if (info->spare)
3938 ring_buffer_free_read_page(info->tr->buffer, info->spare);
2cadf913
SR
3939 kfree(info);
3940
3941 return 0;
3942}
3943
3944struct buffer_ref {
3945 struct ring_buffer *buffer;
3946 void *page;
3947 int ref;
3948};
3949
3950static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3951 struct pipe_buffer *buf)
3952{
3953 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3954
3955 if (--ref->ref)
3956 return;
3957
3958 ring_buffer_free_read_page(ref->buffer, ref->page);
3959 kfree(ref);
3960 buf->private = 0;
3961}
3962
3963static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3964 struct pipe_buffer *buf)
3965{
3966 return 1;
3967}
3968
3969static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3970 struct pipe_buffer *buf)
3971{
3972 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3973
3974 ref->ref++;
3975}
3976
3977/* Pipe buffer operations for a buffer. */
28dfef8f 3978static const struct pipe_buf_operations buffer_pipe_buf_ops = {
2cadf913
SR
3979 .can_merge = 0,
3980 .map = generic_pipe_buf_map,
3981 .unmap = generic_pipe_buf_unmap,
3982 .confirm = generic_pipe_buf_confirm,
3983 .release = buffer_pipe_buf_release,
3984 .steal = buffer_pipe_buf_steal,
3985 .get = buffer_pipe_buf_get,
3986};
3987
3988/*
3989 * Callback from splice_to_pipe(), if we need to release some pages
3990 * at the end of the spd in case we error'ed out in filling the pipe.
3991 */
3992static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3993{
3994 struct buffer_ref *ref =
3995 (struct buffer_ref *)spd->partial[i].private;
3996
3997 if (--ref->ref)
3998 return;
3999
4000 ring_buffer_free_read_page(ref->buffer, ref->page);
4001 kfree(ref);
4002 spd->partial[i].private = 0;
4003}
4004
4005static ssize_t
4006tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4007 struct pipe_inode_info *pipe, size_t len,
4008 unsigned int flags)
4009{
4010 struct ftrace_buffer_info *info = file->private_data;
35f3d14d
JA
4011 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4012 struct page *pages_def[PIPE_DEF_BUFFERS];
2cadf913 4013 struct splice_pipe_desc spd = {
35f3d14d
JA
4014 .pages = pages_def,
4015 .partial = partial_def,
2cadf913
SR
4016 .flags = flags,
4017 .ops = &buffer_pipe_buf_ops,
4018 .spd_release = buffer_spd_release,
4019 };
4020 struct buffer_ref *ref;
93459c6c 4021 int entries, size, i;
2cadf913
SR
4022 size_t ret;
4023
35f3d14d
JA
4024 if (splice_grow_spd(pipe, &spd))
4025 return -ENOMEM;
4026
93cfb3c9
LJ
4027 if (*ppos & (PAGE_SIZE - 1)) {
4028 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
35f3d14d
JA
4029 ret = -EINVAL;
4030 goto out;
93cfb3c9
LJ
4031 }
4032
4033 if (len & (PAGE_SIZE - 1)) {
4034 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
35f3d14d
JA
4035 if (len < PAGE_SIZE) {
4036 ret = -EINVAL;
4037 goto out;
4038 }
93cfb3c9
LJ
4039 len &= PAGE_MASK;
4040 }
4041
7e53bd42 4042 trace_access_lock(info->cpu);
93459c6c
SR
4043 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4044
35f3d14d 4045 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
2cadf913
SR
4046 struct page *page;
4047 int r;
4048
4049 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4050 if (!ref)
4051 break;
4052
7267fa68 4053 ref->ref = 1;
2cadf913 4054 ref->buffer = info->tr->buffer;
7ea59064 4055 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
2cadf913
SR
4056 if (!ref->page) {
4057 kfree(ref);
4058 break;
4059 }
4060
4061 r = ring_buffer_read_page(ref->buffer, &ref->page,
f2957f1f 4062 len, info->cpu, 1);
2cadf913 4063 if (r < 0) {
7ea59064 4064 ring_buffer_free_read_page(ref->buffer, ref->page);
2cadf913
SR
4065 kfree(ref);
4066 break;
4067 }
4068
4069 /*
4070 * zero out any left over data, this is going to
4071 * user land.
4072 */
4073 size = ring_buffer_page_len(ref->page);
4074 if (size < PAGE_SIZE)
4075 memset(ref->page + size, 0, PAGE_SIZE - size);
4076
4077 page = virt_to_page(ref->page);
4078
4079 spd.pages[i] = page;
4080 spd.partial[i].len = PAGE_SIZE;
4081 spd.partial[i].offset = 0;
4082 spd.partial[i].private = (unsigned long)ref;
4083 spd.nr_pages++;
93cfb3c9 4084 *ppos += PAGE_SIZE;
93459c6c
SR
4085
4086 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
2cadf913
SR
4087 }
4088
7e53bd42 4089 trace_access_unlock(info->cpu);
2cadf913
SR
4090 spd.nr_pages = i;
4091
4092 /* did we read anything? */
4093 if (!spd.nr_pages) {
4094 if (flags & SPLICE_F_NONBLOCK)
4095 ret = -EAGAIN;
4096 else
4097 ret = 0;
4098 /* TODO: block */
35f3d14d 4099 goto out;
2cadf913
SR
4100 }
4101
4102 ret = splice_to_pipe(pipe, &spd);
35f3d14d
JA
4103 splice_shrink_spd(pipe, &spd);
4104out:
2cadf913
SR
4105 return ret;
4106}
4107
4108static const struct file_operations tracing_buffers_fops = {
4109 .open = tracing_buffers_open,
4110 .read = tracing_buffers_read,
4111 .release = tracing_buffers_release,
4112 .splice_read = tracing_buffers_splice_read,
4113 .llseek = no_llseek,
4114};
4115
c8d77183
SR
4116static ssize_t
4117tracing_stats_read(struct file *filp, char __user *ubuf,
4118 size_t count, loff_t *ppos)
4119{
4120 unsigned long cpu = (unsigned long)filp->private_data;
4121 struct trace_array *tr = &global_trace;
4122 struct trace_seq *s;
4123 unsigned long cnt;
c64e148a
VN
4124 unsigned long long t;
4125 unsigned long usec_rem;
c8d77183 4126
e4f2d10f 4127 s = kmalloc(sizeof(*s), GFP_KERNEL);
c8d77183 4128 if (!s)
a646365c 4129 return -ENOMEM;
c8d77183
SR
4130
4131 trace_seq_init(s);
4132
4133 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4134 trace_seq_printf(s, "entries: %ld\n", cnt);
4135
4136 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4137 trace_seq_printf(s, "overrun: %ld\n", cnt);
4138
4139 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4140 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4141
c64e148a
VN
4142 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4143 trace_seq_printf(s, "bytes: %ld\n", cnt);
4144
4145 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4146 usec_rem = do_div(t, USEC_PER_SEC);
4147 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
4148
4149 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4150 usec_rem = do_div(t, USEC_PER_SEC);
4151 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4152
c8d77183
SR
4153 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4154
4155 kfree(s);
4156
4157 return count;
4158}
4159
4160static const struct file_operations tracing_stats_fops = {
4161 .open = tracing_open_generic,
4162 .read = tracing_stats_read,
b444786f 4163 .llseek = generic_file_llseek,
c8d77183
SR
4164};
4165
bc0c38d1
SR
4166#ifdef CONFIG_DYNAMIC_FTRACE
4167
b807c3d0
SR
4168int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4169{
4170 return 0;
4171}
4172
bc0c38d1 4173static ssize_t
b807c3d0 4174tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
4175 size_t cnt, loff_t *ppos)
4176{
a26a2a27
SR
4177 static char ftrace_dyn_info_buffer[1024];
4178 static DEFINE_MUTEX(dyn_info_mutex);
bc0c38d1 4179 unsigned long *p = filp->private_data;
b807c3d0 4180 char *buf = ftrace_dyn_info_buffer;
a26a2a27 4181 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
bc0c38d1
SR
4182 int r;
4183
b807c3d0
SR
4184 mutex_lock(&dyn_info_mutex);
4185 r = sprintf(buf, "%ld ", *p);
4bf39a94 4186
a26a2a27 4187 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
b807c3d0
SR
4188 buf[r++] = '\n';
4189
4190 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4191
4192 mutex_unlock(&dyn_info_mutex);
4193
4194 return r;
bc0c38d1
SR
4195}
4196
5e2336a0 4197static const struct file_operations tracing_dyn_info_fops = {
4bf39a94 4198 .open = tracing_open_generic,
b807c3d0 4199 .read = tracing_read_dyn_info,
b444786f 4200 .llseek = generic_file_llseek,
bc0c38d1
SR
4201};
4202#endif
4203
4204static struct dentry *d_tracer;
4205
4206struct dentry *tracing_init_dentry(void)
4207{
4208 static int once;
4209
4210 if (d_tracer)
4211 return d_tracer;
4212
3e1f60b8
FW
4213 if (!debugfs_initialized())
4214 return NULL;
4215
bc0c38d1
SR
4216 d_tracer = debugfs_create_dir("tracing", NULL);
4217
4218 if (!d_tracer && !once) {
4219 once = 1;
4220 pr_warning("Could not create debugfs directory 'tracing'\n");
4221 return NULL;
4222 }
4223
4224 return d_tracer;
4225}
4226
b04cc6b1
FW
4227static struct dentry *d_percpu;
4228
4229struct dentry *tracing_dentry_percpu(void)
4230{
4231 static int once;
4232 struct dentry *d_tracer;
4233
4234 if (d_percpu)
4235 return d_percpu;
4236
4237 d_tracer = tracing_init_dentry();
4238
4239 if (!d_tracer)
4240 return NULL;
4241
4242 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4243
4244 if (!d_percpu && !once) {
4245 once = 1;
4246 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4247 return NULL;
4248 }
4249
4250 return d_percpu;
4251}
4252
4253static void tracing_init_debugfs_percpu(long cpu)
4254{
4255 struct dentry *d_percpu = tracing_dentry_percpu();
5452af66 4256 struct dentry *d_cpu;
dd49a38c 4257 char cpu_dir[30]; /* 30 characters should be more than enough */
b04cc6b1 4258
dd49a38c 4259 snprintf(cpu_dir, 30, "cpu%ld", cpu);
8656e7a2
FW
4260 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4261 if (!d_cpu) {
4262 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4263 return;
4264 }
b04cc6b1 4265
8656e7a2 4266 /* per cpu trace_pipe */
5452af66
FW
4267 trace_create_file("trace_pipe", 0444, d_cpu,
4268 (void *) cpu, &tracing_pipe_fops);
b04cc6b1
FW
4269
4270 /* per cpu trace */
5452af66
FW
4271 trace_create_file("trace", 0644, d_cpu,
4272 (void *) cpu, &tracing_fops);
7f96f93f 4273
5452af66
FW
4274 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4275 (void *) cpu, &tracing_buffers_fops);
7f96f93f 4276
c8d77183
SR
4277 trace_create_file("stats", 0444, d_cpu,
4278 (void *) cpu, &tracing_stats_fops);
b04cc6b1
FW
4279}
4280
60a11774
SR
4281#ifdef CONFIG_FTRACE_SELFTEST
4282/* Let selftest have access to static functions in this file */
4283#include "trace_selftest.c"
4284#endif
4285
577b785f
SR
4286struct trace_option_dentry {
4287 struct tracer_opt *opt;
4288 struct tracer_flags *flags;
4289 struct dentry *entry;
4290};
4291
4292static ssize_t
4293trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4294 loff_t *ppos)
4295{
4296 struct trace_option_dentry *topt = filp->private_data;
4297 char *buf;
4298
4299 if (topt->flags->val & topt->opt->bit)
4300 buf = "1\n";
4301 else
4302 buf = "0\n";
4303
4304 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4305}
4306
4307static ssize_t
4308trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4309 loff_t *ppos)
4310{
4311 struct trace_option_dentry *topt = filp->private_data;
4312 unsigned long val;
577b785f
SR
4313 int ret;
4314
22fe9b54
PH
4315 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4316 if (ret)
577b785f
SR
4317 return ret;
4318
8d18eaaf
LZ
4319 if (val != 0 && val != 1)
4320 return -EINVAL;
577b785f 4321
8d18eaaf 4322 if (!!(topt->flags->val & topt->opt->bit) != val) {
577b785f 4323 mutex_lock(&trace_types_lock);
8d18eaaf 4324 ret = __set_tracer_option(current_trace, topt->flags,
c757bea9 4325 topt->opt, !val);
577b785f
SR
4326 mutex_unlock(&trace_types_lock);
4327 if (ret)
4328 return ret;
577b785f
SR
4329 }
4330
4331 *ppos += cnt;
4332
4333 return cnt;
4334}
4335
4336
4337static const struct file_operations trace_options_fops = {
4338 .open = tracing_open_generic,
4339 .read = trace_options_read,
4340 .write = trace_options_write,
b444786f 4341 .llseek = generic_file_llseek,
577b785f
SR
4342};
4343
a8259075
SR
4344static ssize_t
4345trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4346 loff_t *ppos)
4347{
4348 long index = (long)filp->private_data;
4349 char *buf;
4350
4351 if (trace_flags & (1 << index))
4352 buf = "1\n";
4353 else
4354 buf = "0\n";
4355
4356 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4357}
4358
4359static ssize_t
4360trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4361 loff_t *ppos)
4362{
4363 long index = (long)filp->private_data;
a8259075
SR
4364 unsigned long val;
4365 int ret;
4366
22fe9b54
PH
4367 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4368 if (ret)
a8259075
SR
4369 return ret;
4370
f2d84b65 4371 if (val != 0 && val != 1)
a8259075 4372 return -EINVAL;
f2d84b65 4373 set_tracer_flags(1 << index, val);
a8259075
SR
4374
4375 *ppos += cnt;
4376
4377 return cnt;
4378}
4379
a8259075
SR
4380static const struct file_operations trace_options_core_fops = {
4381 .open = tracing_open_generic,
4382 .read = trace_options_core_read,
4383 .write = trace_options_core_write,
b444786f 4384 .llseek = generic_file_llseek,
a8259075
SR
4385};
4386
5452af66
FW
4387struct dentry *trace_create_file(const char *name,
4388 mode_t mode,
4389 struct dentry *parent,
4390 void *data,
4391 const struct file_operations *fops)
4392{
4393 struct dentry *ret;
4394
4395 ret = debugfs_create_file(name, mode, parent, data, fops);
4396 if (!ret)
4397 pr_warning("Could not create debugfs '%s' entry\n", name);
4398
4399 return ret;
4400}
4401
4402
a8259075
SR
4403static struct dentry *trace_options_init_dentry(void)
4404{
4405 struct dentry *d_tracer;
4406 static struct dentry *t_options;
4407
4408 if (t_options)
4409 return t_options;
4410
4411 d_tracer = tracing_init_dentry();
4412 if (!d_tracer)
4413 return NULL;
4414
4415 t_options = debugfs_create_dir("options", d_tracer);
4416 if (!t_options) {
4417 pr_warning("Could not create debugfs directory 'options'\n");
4418 return NULL;
4419 }
4420
4421 return t_options;
4422}
4423
577b785f
SR
4424static void
4425create_trace_option_file(struct trace_option_dentry *topt,
4426 struct tracer_flags *flags,
4427 struct tracer_opt *opt)
4428{
4429 struct dentry *t_options;
577b785f
SR
4430
4431 t_options = trace_options_init_dentry();
4432 if (!t_options)
4433 return;
4434
4435 topt->flags = flags;
4436 topt->opt = opt;
4437
5452af66 4438 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
577b785f
SR
4439 &trace_options_fops);
4440
577b785f
SR
4441}
4442
4443static struct trace_option_dentry *
4444create_trace_option_files(struct tracer *tracer)
4445{
4446 struct trace_option_dentry *topts;
4447 struct tracer_flags *flags;
4448 struct tracer_opt *opts;
4449 int cnt;
4450
4451 if (!tracer)
4452 return NULL;
4453
4454 flags = tracer->flags;
4455
4456 if (!flags || !flags->opts)
4457 return NULL;
4458
4459 opts = flags->opts;
4460
4461 for (cnt = 0; opts[cnt].name; cnt++)
4462 ;
4463
0cfe8245 4464 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
577b785f
SR
4465 if (!topts)
4466 return NULL;
4467
4468 for (cnt = 0; opts[cnt].name; cnt++)
4469 create_trace_option_file(&topts[cnt], flags,
4470 &opts[cnt]);
4471
4472 return topts;
4473}
4474
4475static void
4476destroy_trace_option_files(struct trace_option_dentry *topts)
4477{
4478 int cnt;
4479
4480 if (!topts)
4481 return;
4482
4483 for (cnt = 0; topts[cnt].opt; cnt++) {
4484 if (topts[cnt].entry)
4485 debugfs_remove(topts[cnt].entry);
4486 }
4487
4488 kfree(topts);
4489}
4490
a8259075
SR
4491static struct dentry *
4492create_trace_option_core_file(const char *option, long index)
4493{
4494 struct dentry *t_options;
a8259075
SR
4495
4496 t_options = trace_options_init_dentry();
4497 if (!t_options)
4498 return NULL;
4499
5452af66 4500 return trace_create_file(option, 0644, t_options, (void *)index,
a8259075 4501 &trace_options_core_fops);
a8259075
SR
4502}
4503
4504static __init void create_trace_options_dir(void)
4505{
4506 struct dentry *t_options;
a8259075
SR
4507 int i;
4508
4509 t_options = trace_options_init_dentry();
4510 if (!t_options)
4511 return;
4512
5452af66
FW
4513 for (i = 0; trace_options[i]; i++)
4514 create_trace_option_core_file(trace_options[i], i);
a8259075
SR
4515}
4516
b5ad384e 4517static __init int tracer_init_debugfs(void)
bc0c38d1
SR
4518{
4519 struct dentry *d_tracer;
b04cc6b1 4520 int cpu;
bc0c38d1 4521
7e53bd42
LJ
4522 trace_access_lock_init();
4523
bc0c38d1
SR
4524 d_tracer = tracing_init_dentry();
4525
5452af66
FW
4526 trace_create_file("tracing_enabled", 0644, d_tracer,
4527 &global_trace, &tracing_ctrl_fops);
bc0c38d1 4528
5452af66
FW
4529 trace_create_file("trace_options", 0644, d_tracer,
4530 NULL, &tracing_iter_fops);
bc0c38d1 4531
5452af66
FW
4532 trace_create_file("tracing_cpumask", 0644, d_tracer,
4533 NULL, &tracing_cpumask_fops);
4534
4535 trace_create_file("trace", 0644, d_tracer,
4536 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
a8259075 4537
5452af66
FW
4538 trace_create_file("available_tracers", 0444, d_tracer,
4539 &global_trace, &show_traces_fops);
4540
339ae5d3 4541 trace_create_file("current_tracer", 0644, d_tracer,
5452af66
FW
4542 &global_trace, &set_tracer_fops);
4543
5d4a9dba 4544#ifdef CONFIG_TRACER_MAX_TRACE
5452af66
FW
4545 trace_create_file("tracing_max_latency", 0644, d_tracer,
4546 &tracing_max_latency, &tracing_max_lat_fops);
0e950173 4547#endif
5452af66
FW
4548
4549 trace_create_file("tracing_thresh", 0644, d_tracer,
4550 &tracing_thresh, &tracing_max_lat_fops);
a8259075 4551
339ae5d3 4552 trace_create_file("README", 0444, d_tracer,
5452af66
FW
4553 NULL, &tracing_readme_fops);
4554
4555 trace_create_file("trace_pipe", 0444, d_tracer,
b04cc6b1 4556 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
5452af66
FW
4557
4558 trace_create_file("buffer_size_kb", 0644, d_tracer,
4559 &global_trace, &tracing_entries_fops);
4560
f81ab074
VN
4561 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
4562 &global_trace, &tracing_total_entries_fops);
4563
4f271a2a
VN
4564 trace_create_file("free_buffer", 0644, d_tracer,
4565 &global_trace, &tracing_free_buffer_fops);
4566
5452af66
FW
4567 trace_create_file("trace_marker", 0220, d_tracer,
4568 NULL, &tracing_mark_fops);
5bf9a1ee 4569
69abe6a5
AP
4570 trace_create_file("saved_cmdlines", 0444, d_tracer,
4571 NULL, &tracing_saved_cmdlines_fops);
5bf9a1ee 4572
5079f326
Z
4573 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4574 &trace_clock_fops);
4575
bc0c38d1 4576#ifdef CONFIG_DYNAMIC_FTRACE
5452af66
FW
4577 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4578 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
bc0c38d1 4579#endif
b04cc6b1 4580
5452af66
FW
4581 create_trace_options_dir();
4582
b04cc6b1
FW
4583 for_each_tracing_cpu(cpu)
4584 tracing_init_debugfs_percpu(cpu);
4585
b5ad384e 4586 return 0;
bc0c38d1
SR
4587}
4588
3f5a54e3
SR
4589static int trace_panic_handler(struct notifier_block *this,
4590 unsigned long event, void *unused)
4591{
944ac425 4592 if (ftrace_dump_on_oops)
cecbca96 4593 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
4594 return NOTIFY_OK;
4595}
4596
4597static struct notifier_block trace_panic_notifier = {
4598 .notifier_call = trace_panic_handler,
4599 .next = NULL,
4600 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4601};
4602
4603static int trace_die_handler(struct notifier_block *self,
4604 unsigned long val,
4605 void *data)
4606{
4607 switch (val) {
4608 case DIE_OOPS:
944ac425 4609 if (ftrace_dump_on_oops)
cecbca96 4610 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
4611 break;
4612 default:
4613 break;
4614 }
4615 return NOTIFY_OK;
4616}
4617
4618static struct notifier_block trace_die_notifier = {
4619 .notifier_call = trace_die_handler,
4620 .priority = 200
4621};
4622
4623/*
4624 * printk is set to max of 1024, we really don't need it that big.
4625 * Nothing should be printing 1000 characters anyway.
4626 */
4627#define TRACE_MAX_PRINT 1000
4628
4629/*
4630 * Define here KERN_TRACE so that we have one place to modify
4631 * it if we decide to change what log level the ftrace dump
4632 * should be at.
4633 */
428aee14 4634#define KERN_TRACE KERN_EMERG
3f5a54e3 4635
955b61e5 4636void
3f5a54e3
SR
4637trace_printk_seq(struct trace_seq *s)
4638{
4639 /* Probably should print a warning here. */
4640 if (s->len >= 1000)
4641 s->len = 1000;
4642
4643 /* should be zero ended, but we are paranoid. */
4644 s->buffer[s->len] = 0;
4645
4646 printk(KERN_TRACE "%s", s->buffer);
4647
f9520750 4648 trace_seq_init(s);
3f5a54e3
SR
4649}
4650
955b61e5
JW
4651void trace_init_global_iter(struct trace_iterator *iter)
4652{
4653 iter->tr = &global_trace;
4654 iter->trace = current_trace;
4655 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4656}
4657
cecbca96
FW
4658static void
4659__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
3f5a54e3 4660{
445c8951 4661 static arch_spinlock_t ftrace_dump_lock =
edc35bd7 4662 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
3f5a54e3
SR
4663 /* use static because iter can be a bit big for the stack */
4664 static struct trace_iterator iter;
cf586b61 4665 unsigned int old_userobj;
3f5a54e3 4666 static int dump_ran;
d769041f
SR
4667 unsigned long flags;
4668 int cnt = 0, cpu;
3f5a54e3
SR
4669
4670 /* only one dump */
cd891ae0 4671 local_irq_save(flags);
0199c4e6 4672 arch_spin_lock(&ftrace_dump_lock);
3f5a54e3
SR
4673 if (dump_ran)
4674 goto out;
4675
4676 dump_ran = 1;
4677
0ee6b6cf 4678 tracing_off();
cf586b61 4679
e0a413f6
SR
4680 /* Did function tracer already get disabled? */
4681 if (ftrace_is_dead()) {
4682 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
4683 printk("# MAY BE MISSING FUNCTION EVENTS\n");
4684 }
4685
cf586b61
FW
4686 if (disable_tracing)
4687 ftrace_kill();
3f5a54e3 4688
955b61e5
JW
4689 trace_init_global_iter(&iter);
4690
d769041f 4691 for_each_tracing_cpu(cpu) {
955b61e5 4692 atomic_inc(&iter.tr->data[cpu]->disabled);
d769041f
SR
4693 }
4694
cf586b61
FW
4695 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4696
b54d3de9
TE
4697 /* don't look at user memory in panic mode */
4698 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4699
e543ad76 4700 /* Simulate the iterator */
3f5a54e3
SR
4701 iter.tr = &global_trace;
4702 iter.trace = current_trace;
cecbca96
FW
4703
4704 switch (oops_dump_mode) {
4705 case DUMP_ALL:
4706 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4707 break;
4708 case DUMP_ORIG:
4709 iter.cpu_file = raw_smp_processor_id();
4710 break;
4711 case DUMP_NONE:
4712 goto out_enable;
4713 default:
4714 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4715 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4716 }
4717
4718 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3f5a54e3
SR
4719
4720 /*
4721 * We need to stop all tracing on all CPUS to read the
4722 * the next buffer. This is a bit expensive, but is
4723 * not done often. We fill all what we can read,
4724 * and then release the locks again.
4725 */
4726
3f5a54e3
SR
4727 while (!trace_empty(&iter)) {
4728
4729 if (!cnt)
4730 printk(KERN_TRACE "---------------------------------\n");
4731
4732 cnt++;
4733
4734 /* reset all but tr, trace, and overruns */
4735 memset(&iter.seq, 0,
4736 sizeof(struct trace_iterator) -
4737 offsetof(struct trace_iterator, seq));
4738 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4739 iter.pos = -1;
4740
955b61e5 4741 if (trace_find_next_entry_inc(&iter) != NULL) {
74e7ff8c
LJ
4742 int ret;
4743
4744 ret = print_trace_line(&iter);
4745 if (ret != TRACE_TYPE_NO_CONSUME)
4746 trace_consume(&iter);
3f5a54e3
SR
4747 }
4748
4749 trace_printk_seq(&iter.seq);
4750 }
4751
4752 if (!cnt)
4753 printk(KERN_TRACE " (ftrace buffer empty)\n");
4754 else
4755 printk(KERN_TRACE "---------------------------------\n");
4756
cecbca96 4757 out_enable:
cf586b61
FW
4758 /* Re-enable tracing if requested */
4759 if (!disable_tracing) {
4760 trace_flags |= old_userobj;
4761
4762 for_each_tracing_cpu(cpu) {
955b61e5 4763 atomic_dec(&iter.tr->data[cpu]->disabled);
cf586b61
FW
4764 }
4765 tracing_on();
4766 }
4767
3f5a54e3 4768 out:
0199c4e6 4769 arch_spin_unlock(&ftrace_dump_lock);
cd891ae0 4770 local_irq_restore(flags);
3f5a54e3
SR
4771}
4772
cf586b61 4773/* By default: disable tracing after the dump */
cecbca96 4774void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
cf586b61 4775{
cecbca96 4776 __ftrace_dump(true, oops_dump_mode);
cf586b61
FW
4777}
4778
3928a8a2 4779__init static int tracer_alloc_buffers(void)
bc0c38d1 4780{
73c5162a 4781 int ring_buf_size;
750912fa 4782 enum ring_buffer_flags rb_flags;
4c11d7ae 4783 int i;
9e01c1b7 4784 int ret = -ENOMEM;
4c11d7ae 4785
750912fa 4786
9e01c1b7
RR
4787 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4788 goto out;
4789
4790 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4791 goto out_free_buffer_mask;
4c11d7ae 4792
73c5162a
SR
4793 /* To save memory, keep the ring buffer size to its minimum */
4794 if (ring_buffer_expanded)
4795 ring_buf_size = trace_buf_size;
4796 else
4797 ring_buf_size = 1;
4798
750912fa
DS
4799 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
4800
9e01c1b7
RR
4801 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4802 cpumask_copy(tracing_cpumask, cpu_all_mask);
4803
4804 /* TODO: make the number of buffers hot pluggable with CPUS */
750912fa 4805 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
3928a8a2
SR
4806 if (!global_trace.buffer) {
4807 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4808 WARN_ON(1);
9e01c1b7 4809 goto out_free_cpumask;
4c11d7ae 4810 }
3928a8a2 4811 global_trace.entries = ring_buffer_size(global_trace.buffer);
4c11d7ae 4812
9e01c1b7 4813
4c11d7ae 4814#ifdef CONFIG_TRACER_MAX_TRACE
750912fa 4815 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
3928a8a2
SR
4816 if (!max_tr.buffer) {
4817 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4818 WARN_ON(1);
4819 ring_buffer_free(global_trace.buffer);
9e01c1b7 4820 goto out_free_cpumask;
4c11d7ae 4821 }
ef710e10 4822 max_tr.entries = 1;
a98a3c3f 4823#endif
ab46428c 4824
4c11d7ae 4825 /* Allocate the first page for all buffers */
ab46428c 4826 for_each_tracing_cpu(i) {
566b0aaf 4827 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
9705f69e 4828 max_tr.data[i] = &per_cpu(max_tr_data, i);
4c11d7ae 4829 }
bc0c38d1 4830
bc0c38d1
SR
4831 trace_init_cmdlines();
4832
43a15386 4833 register_tracer(&nop_trace);
79fb0768 4834 current_trace = &nop_trace;
60a11774
SR
4835 /* All seems OK, enable tracing */
4836 tracing_disabled = 0;
3928a8a2 4837
3f5a54e3
SR
4838 atomic_notifier_chain_register(&panic_notifier_list,
4839 &trace_panic_notifier);
4840
4841 register_die_notifier(&trace_die_notifier);
2fc1dfbe
FW
4842
4843 return 0;
3f5a54e3 4844
9e01c1b7
RR
4845out_free_cpumask:
4846 free_cpumask_var(tracing_cpumask);
4847out_free_buffer_mask:
4848 free_cpumask_var(tracing_buffer_mask);
4849out:
4850 return ret;
bc0c38d1 4851}
b2821ae6
SR
4852
4853__init static int clear_boot_tracer(void)
4854{
4855 /*
4856 * The default tracer at boot buffer is an init section.
4857 * This function is called in lateinit. If we did not
4858 * find the boot tracer, then clear it out, to prevent
4859 * later registration from accessing the buffer that is
4860 * about to be freed.
4861 */
4862 if (!default_bootup_tracer)
4863 return 0;
4864
4865 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4866 default_bootup_tracer);
4867 default_bootup_tracer = NULL;
4868
4869 return 0;
4870}
4871
b5ad384e
FW
4872early_initcall(tracer_alloc_buffers);
4873fs_initcall(tracer_init_debugfs);
b2821ae6 4874late_initcall(clear_boot_tracer);