Merge tag 'v3.10.108' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / trace / trace.c
CommitLineData
bc0c38d1
SR
1/*
2 * ring buffer based function tracer
3 *
2b6080f2 4 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
bc0c38d1
SR
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
6d49e352 12 * Copyright (C) 2004 Nadia Yvette Chambers
bc0c38d1 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>
b892e5c8 39#include <linux/nmi.h>
bc0c38d1 40#include <linux/fs.h>
6fa3eb70 41
8bd75c77 42#include <linux/sched/rt.h>
86387f7e 43
bc0c38d1 44#include "trace.h"
f0868d1e 45#include "trace_output.h"
bc0c38d1 46
6fa3eb70
S
47#ifdef CONFIG_MTK_SCHED_TRACERS
48#include <linux/mtk_ftrace.h>
49#define CREATE_TRACE_POINTS
50#include <trace/events/mtk_events.h>
51EXPORT_TRACEPOINT_SYMBOL(gpu_freq);
52#endif
53
54#ifdef CONFIG_MTK_EXTMEM
55#include <linux/vmalloc.h>
56#endif
57
73c5162a
SR
58/*
59 * On boot up, the ring buffer is set to the minimum size, so that
60 * we do not waste memory on systems that are not using tracing.
61 */
55034cd6 62bool ring_buffer_expanded;
73c5162a 63
8e1b82e0
FW
64/*
65 * We need to change this state when a selftest is running.
ff32504f
FW
66 * A selftest will lurk into the ring-buffer to count the
67 * entries inserted during the selftest although some concurrent
5e1607a0 68 * insertions into the ring-buffer such as trace_printk could occurred
ff32504f
FW
69 * at the same time, giving false positive or negative results.
70 */
8e1b82e0 71static bool __read_mostly tracing_selftest_running;
ff32504f 72
b2821ae6
SR
73/*
74 * If a tracer is running, we do not want to run SELFTEST.
75 */
020e5f85 76bool __read_mostly tracing_selftest_disabled;
b2821ae6 77
adf9f195
FW
78/* For tracers that don't implement custom flags */
79static struct tracer_opt dummy_tracer_opt[] = {
80 { }
81};
82
83static struct tracer_flags dummy_tracer_flags = {
84 .val = 0,
85 .opts = dummy_tracer_opt
86};
87
88static int dummy_set_flag(u32 old_flags, u32 bit, int set)
89{
90 return 0;
91}
0f048701 92
7ffbd48d
SR
93/*
94 * To prevent the comm cache from being overwritten when no
95 * tracing is active, only save the comm when a trace event
96 * occurred.
97 */
98static DEFINE_PER_CPU(bool, trace_cmdline_save);
99
0f048701
SR
100/*
101 * Kill all tracing for good (never come back).
102 * It is initialized to 1 but will turn to zero if the initialization
103 * of the tracer is successful. But that is the only place that sets
104 * this back to zero.
105 */
4fd27358 106static int tracing_disabled = 1;
0f048701 107
9288f99a 108DEFINE_PER_CPU(int, ftrace_cpu_disabled);
d769041f 109
955b61e5 110cpumask_var_t __read_mostly tracing_buffer_mask;
ab46428c 111
944ac425
SR
112/*
113 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
114 *
115 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
116 * is set, then ftrace_dump is called. This will output the contents
117 * of the ftrace buffers to the console. This is very useful for
118 * capturing traces that lead to crashes and outputing it to a
119 * serial console.
120 *
121 * It is default off, but you can enable it with either specifying
122 * "ftrace_dump_on_oops" in the kernel command line, or setting
cecbca96
FW
123 * /proc/sys/kernel/ftrace_dump_on_oops
124 * Set 1 if you want to dump buffers of all CPUs
125 * Set 2 if you want to dump the buffer of the CPU that triggered oops
944ac425 126 */
cecbca96
FW
127
128enum ftrace_dump_mode ftrace_dump_on_oops;
944ac425 129
b2821ae6
SR
130static int tracing_set_tracer(const char *buf);
131
ee6c2c1b
LZ
132#define MAX_TRACER_SIZE 100
133static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
b2821ae6 134static char *default_bootup_tracer;
d9e54076 135
55034cd6
SRRH
136static bool allocate_snapshot;
137
1beee96b 138static int __init set_cmdline_ftrace(char *str)
d9e54076 139{
67012ab1 140 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
b2821ae6 141 default_bootup_tracer = bootup_tracer_buf;
73c5162a 142 /* We are using ftrace early, expand it */
55034cd6 143 ring_buffer_expanded = true;
d9e54076
PZ
144 return 1;
145}
1beee96b 146__setup("ftrace=", set_cmdline_ftrace);
d9e54076 147
944ac425
SR
148static int __init set_ftrace_dump_on_oops(char *str)
149{
cecbca96
FW
150 if (*str++ != '=' || !*str) {
151 ftrace_dump_on_oops = DUMP_ALL;
152 return 1;
153 }
154
155 if (!strcmp("orig_cpu", str)) {
156 ftrace_dump_on_oops = DUMP_ORIG;
157 return 1;
158 }
159
160 return 0;
944ac425
SR
161}
162__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
60a11774 163
3209cff4 164static int __init boot_alloc_snapshot(char *str)
55034cd6
SRRH
165{
166 allocate_snapshot = true;
167 /* We also need the main ring buffer expanded */
168 ring_buffer_expanded = true;
169 return 1;
170}
3209cff4 171__setup("alloc_snapshot", boot_alloc_snapshot);
55034cd6 172
7bcfaf54
SR
173
174static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
175static char *trace_boot_options __initdata;
176
177static int __init set_trace_boot_options(char *str)
178{
67012ab1 179 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
7bcfaf54
SR
180 trace_boot_options = trace_boot_options_buf;
181 return 0;
182}
183__setup("trace_options=", set_trace_boot_options);
184
cf8e3474 185unsigned long long ns2usecs(cycle_t nsec)
bc0c38d1
SR
186{
187 nsec += 500;
188 do_div(nsec, 1000);
189 return nsec;
190}
191
4fcdae83
SR
192/*
193 * The global_trace is the descriptor that holds the tracing
194 * buffers for the live tracing. For each CPU, it contains
195 * a link list of pages that will store trace entries. The
196 * page descriptor of the pages in the memory is used to hold
197 * the link list by linking the lru item in the page descriptor
198 * to each of the pages in the buffer per CPU.
199 *
200 * For each active CPU there is a data field that holds the
201 * pages for the buffer for that CPU. Each CPU has the same number
202 * of pages allocated for its buffer.
203 */
bc0c38d1
SR
204static struct trace_array global_trace;
205
ae63b31e 206LIST_HEAD(ftrace_trace_arrays);
bc0c38d1 207
59d8f488
SRRH
208int trace_array_get(struct trace_array *this_tr)
209{
210 struct trace_array *tr;
211 int ret = -ENODEV;
212
213 mutex_lock(&trace_types_lock);
214 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
215 if (tr == this_tr) {
216 tr->ref++;
217 ret = 0;
218 break;
219 }
220 }
221 mutex_unlock(&trace_types_lock);
222
223 return ret;
224}
225
226static void __trace_array_put(struct trace_array *this_tr)
227{
228 WARN_ON(!this_tr->ref);
229 this_tr->ref--;
230}
231
232void trace_array_put(struct trace_array *this_tr)
233{
234 mutex_lock(&trace_types_lock);
235 __trace_array_put(this_tr);
236 mutex_unlock(&trace_types_lock);
237}
238
e77405ad
SR
239int filter_current_check_discard(struct ring_buffer *buffer,
240 struct ftrace_event_call *call, void *rec,
eb02ce01
TZ
241 struct ring_buffer_event *event)
242{
e77405ad 243 return filter_check_discard(call, rec, buffer, event);
eb02ce01 244}
17c873ec 245EXPORT_SYMBOL_GPL(filter_current_check_discard);
eb02ce01 246
e88512e7 247cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
37886f6a
SR
248{
249 u64 ts;
250
251 /* Early boot up does not have a buffer yet */
e88512e7 252 if (!buf->buffer)
37886f6a
SR
253 return trace_clock_local();
254
e88512e7
AL
255 ts = ring_buffer_time_stamp(buf->buffer, cpu);
256 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
37886f6a
SR
257
258 return ts;
259}
bc0c38d1 260
e88512e7
AL
261cycle_t ftrace_now(int cpu)
262{
263 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
264}
265
2fd821ee
SRRH
266/**
267 * tracing_is_enabled - Show if global_trace has been disabled
268 *
269 * Shows if the global trace has been enabled or not. It uses the
270 * mirror flag "buffer_disabled" to be used in fast paths such as for
271 * the irqsoff tracer. But it may be inaccurate due to races. If you
272 * need to know the accurate state, use tracing_is_on() which is a little
273 * slower, but accurate.
274 */
9036990d
SR
275int tracing_is_enabled(void)
276{
2fd821ee
SRRH
277 /*
278 * For quick access (irqsoff uses this in fast path), just
279 * return the mirror variable of the state of the ring buffer.
280 * It's a little racy, but we don't really care.
281 */
282 smp_rmb();
283 return !global_trace.buffer_disabled;
9036990d
SR
284}
285
4fcdae83 286/*
3928a8a2
SR
287 * trace_buf_size is the size in bytes that is allocated
288 * for a buffer. Note, the number of bytes is always rounded
289 * to page size.
3f5a54e3
SR
290 *
291 * This number is purposely set to a low number of 16384.
292 * If the dump on oops happens, it will be much appreciated
293 * to not have to wait for all that output. Anyway this can be
294 * boot time and run time configurable.
4fcdae83 295 */
3928a8a2 296#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
3f5a54e3 297
3928a8a2 298static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
bc0c38d1 299
6fa3eb70
S
300#ifdef CONFIG_MTK_SCHED_TRACERS
301#define CPUX_TRACE_BUF_SIZE_DEFAULT 4194304UL
302#define CPU0_to_CPUX_RATIO (1.2)
303extern unsigned int get_max_DRAM_size (void);
304static unsigned long trace_buf_size_cpu0 = (CPUX_TRACE_BUF_SIZE_DEFAULT * CPU0_to_CPUX_RATIO);
305static unsigned long trace_buf_size_cpuX = CPUX_TRACE_BUF_SIZE_DEFAULT;
306static unsigned int trace_buf_size_updated_from_cmdline = 0;
307#endif
308
4fcdae83 309/* trace_types holds a link list of available tracers. */
bc0c38d1 310static struct tracer *trace_types __read_mostly;
4fcdae83 311
4fcdae83
SR
312/*
313 * trace_types_lock is used to protect the trace_types list.
4fcdae83 314 */
9713f785 315DEFINE_MUTEX(trace_types_lock);
4fcdae83 316
7e53bd42
LJ
317/*
318 * serialize the access of the ring buffer
319 *
320 * ring buffer serializes readers, but it is low level protection.
321 * The validity of the events (which returns by ring_buffer_peek() ..etc)
322 * are not protected by ring buffer.
323 *
324 * The content of events may become garbage if we allow other process consumes
325 * these events concurrently:
326 * A) the page of the consumed events may become a normal page
327 * (not reader page) in ring buffer, and this page will be rewrited
328 * by events producer.
329 * B) The page of the consumed events may become a page for splice_read,
330 * and this page will be returned to system.
331 *
332 * These primitives allow multi process access to different cpu ring buffer
333 * concurrently.
334 *
335 * These primitives don't distinguish read-only and read-consume access.
336 * Multi read-only access are also serialized.
337 */
338
339#ifdef CONFIG_SMP
340static DECLARE_RWSEM(all_cpu_access_lock);
341static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
342
343static inline void trace_access_lock(int cpu)
344{
ae3b5093 345 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
346 /* gain it for accessing the whole ring buffer. */
347 down_write(&all_cpu_access_lock);
348 } else {
349 /* gain it for accessing a cpu ring buffer. */
350
ae3b5093 351 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
7e53bd42
LJ
352 down_read(&all_cpu_access_lock);
353
354 /* Secondly block other access to this @cpu ring buffer. */
355 mutex_lock(&per_cpu(cpu_access_lock, cpu));
356 }
357}
358
359static inline void trace_access_unlock(int cpu)
360{
ae3b5093 361 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
362 up_write(&all_cpu_access_lock);
363 } else {
364 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
365 up_read(&all_cpu_access_lock);
366 }
367}
368
369static inline void trace_access_lock_init(void)
370{
371 int cpu;
372
373 for_each_possible_cpu(cpu)
374 mutex_init(&per_cpu(cpu_access_lock, cpu));
375}
376
377#else
378
379static DEFINE_MUTEX(access_lock);
380
381static inline void trace_access_lock(int cpu)
382{
383 (void)cpu;
384 mutex_lock(&access_lock);
385}
386
387static inline void trace_access_unlock(int cpu)
388{
389 (void)cpu;
390 mutex_unlock(&access_lock);
391}
392
393static inline void trace_access_lock_init(void)
394{
395}
396
397#endif
398
ee6bce52 399/* trace_flags holds trace_options default values */
6fa3eb70
S
400#ifdef CONFIG_MTK_FTRACE_DEFAULT_ENABLE
401unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
402 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
403 TRACE_ITER_GRAPH_TIME | TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS |
404 TRACE_ITER_FUNCTION;
405#else
12ef7d44 406unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
a2a16d6a 407 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
77271ce4 408 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
328df475 409 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
6fa3eb70 410#endif
e7e2ee89 411
2fd821ee
SRRH
412void tracer_tracing_on(struct trace_array *tr)
413{
414 if (tr->trace_buffer.buffer)
415 ring_buffer_record_on(tr->trace_buffer.buffer);
416 /*
417 * This flag is looked at when buffers haven't been allocated
418 * yet, or by some tracers (like irqsoff), that just want to
419 * know if the ring buffer has been disabled, but it can handle
420 * races of where it gets disabled but we still do a record.
421 * As the check is in the fast path of the tracers, it is more
422 * important to be fast than accurate.
423 */
424 tr->buffer_disabled = 0;
425 /* Make the flag seen by readers */
426 smp_wmb();
427}
428
499e5470
SR
429/**
430 * tracing_on - enable tracing buffers
431 *
432 * This function enables tracing buffers that may have been
433 * disabled with tracing_off.
434 */
435void tracing_on(void)
436{
2fd821ee 437 tracer_tracing_on(&global_trace);
6fa3eb70
S
438#ifdef CONFIG_MTK_SCHED_TRACERS
439 trace_tracing_on(1, CALLER_ADDR0);
440#endif
499e5470
SR
441}
442EXPORT_SYMBOL_GPL(tracing_on);
443
09ae7234
SRRH
444/**
445 * __trace_puts - write a constant string into the trace buffer.
446 * @ip: The address of the caller
447 * @str: The constant string to write
448 * @size: The size of the string.
449 */
450int __trace_puts(unsigned long ip, const char *str, int size)
451{
452 struct ring_buffer_event *event;
453 struct ring_buffer *buffer;
454 struct print_entry *entry;
455 unsigned long irq_flags;
456 int alloc;
e250100b
J
457 int pc;
458
459 pc = preempt_count();
09ae7234 460
f74bb740
SRRH
461 if (unlikely(tracing_selftest_running || tracing_disabled))
462 return 0;
463
09ae7234
SRRH
464 alloc = sizeof(*entry) + size + 2; /* possible \n added */
465
466 local_save_flags(irq_flags);
467 buffer = global_trace.trace_buffer.buffer;
468 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
e250100b 469 irq_flags, pc);
09ae7234
SRRH
470 if (!event)
471 return 0;
472
473 entry = ring_buffer_event_data(event);
474 entry->ip = ip;
475
476 memcpy(&entry->buf, str, size);
477
478 /* Add a newline if necessary */
479 if (entry->buf[size - 1] != '\n') {
480 entry->buf[size] = '\n';
481 entry->buf[size + 1] = '\0';
482 } else
483 entry->buf[size] = '\0';
484
485 __buffer_unlock_commit(buffer, event);
e250100b 486 ftrace_trace_stack(buffer, irq_flags, 4, pc);
09ae7234
SRRH
487
488 return size;
489}
490EXPORT_SYMBOL_GPL(__trace_puts);
491
492/**
493 * __trace_bputs - write the pointer to a constant string into trace buffer
494 * @ip: The address of the caller
495 * @str: The constant string to write to the buffer to
496 */
497int __trace_bputs(unsigned long ip, const char *str)
498{
499 struct ring_buffer_event *event;
500 struct ring_buffer *buffer;
501 struct bputs_entry *entry;
502 unsigned long irq_flags;
503 int size = sizeof(struct bputs_entry);
e250100b
J
504 int pc;
505
506 pc = preempt_count();
09ae7234 507
f74bb740
SRRH
508 if (unlikely(tracing_selftest_running || tracing_disabled))
509 return 0;
510
09ae7234
SRRH
511 local_save_flags(irq_flags);
512 buffer = global_trace.trace_buffer.buffer;
513 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
e250100b 514 irq_flags, pc);
09ae7234
SRRH
515 if (!event)
516 return 0;
517
518 entry = ring_buffer_event_data(event);
519 entry->ip = ip;
520 entry->str = str;
521
522 __buffer_unlock_commit(buffer, event);
e250100b 523 ftrace_trace_stack(buffer, irq_flags, 4, pc);
09ae7234
SRRH
524
525 return 1;
526}
527EXPORT_SYMBOL_GPL(__trace_bputs);
528
ad909e21
SRRH
529#ifdef CONFIG_TRACER_SNAPSHOT
530/**
531 * trace_snapshot - take a snapshot of the current buffer.
532 *
533 * This causes a swap between the snapshot buffer and the current live
534 * tracing buffer. You can use this to take snapshots of the live
535 * trace when some condition is triggered, but continue to trace.
536 *
537 * Note, make sure to allocate the snapshot with either
538 * a tracing_snapshot_alloc(), or by doing it manually
539 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
540 *
541 * If the snapshot buffer is not allocated, it will stop tracing.
542 * Basically making a permanent snapshot.
543 */
544void tracing_snapshot(void)
545{
546 struct trace_array *tr = &global_trace;
547 struct tracer *tracer = tr->current_trace;
548 unsigned long flags;
549
1b22e382
SRRH
550 if (in_nmi()) {
551 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
552 internal_trace_puts("*** snapshot is being ignored ***\n");
553 return;
554 }
555
ad909e21 556 if (!tr->allocated_snapshot) {
ca268da6
SRRH
557 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
558 internal_trace_puts("*** stopping trace here! ***\n");
ad909e21
SRRH
559 tracing_off();
560 return;
561 }
562
563 /* Note, snapshot can not be used when the tracer uses it */
564 if (tracer->use_max_tr) {
ca268da6
SRRH
565 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
566 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
ad909e21
SRRH
567 return;
568 }
569
570 local_irq_save(flags);
571 update_max_tr(tr, current, smp_processor_id());
572 local_irq_restore(flags);
573}
1b22e382 574EXPORT_SYMBOL_GPL(tracing_snapshot);
ad909e21
SRRH
575
576static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
577 struct trace_buffer *size_buf, int cpu_id);
3209cff4
SRRH
578static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
579
580static int alloc_snapshot(struct trace_array *tr)
581{
582 int ret;
583
584 if (!tr->allocated_snapshot) {
585
586 /* allocate spare buffer */
587 ret = resize_buffer_duplicate_size(&tr->max_buffer,
588 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
589 if (ret < 0)
590 return ret;
591
592 tr->allocated_snapshot = true;
593 }
594
595 return 0;
596}
597
598void free_snapshot(struct trace_array *tr)
599{
600 /*
601 * We don't free the ring buffer. instead, resize it because
602 * The max_tr ring buffer has some state (e.g. ring->clock) and
603 * we want preserve it.
604 */
605 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
606 set_buffer_entries(&tr->max_buffer, 1);
607 tracing_reset_online_cpus(&tr->max_buffer);
608 tr->allocated_snapshot = false;
609}
ad909e21
SRRH
610
611/**
612 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
613 *
614 * This is similar to trace_snapshot(), but it will allocate the
615 * snapshot buffer if it isn't already allocated. Use this only
616 * where it is safe to sleep, as the allocation may sleep.
617 *
618 * This causes a swap between the snapshot buffer and the current live
619 * tracing buffer. You can use this to take snapshots of the live
620 * trace when some condition is triggered, but continue to trace.
621 */
622void tracing_snapshot_alloc(void)
623{
624 struct trace_array *tr = &global_trace;
625 int ret;
626
3209cff4
SRRH
627 ret = alloc_snapshot(tr);
628 if (WARN_ON(ret < 0))
629 return;
ad909e21
SRRH
630
631 tracing_snapshot();
632}
1b22e382 633EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
ad909e21
SRRH
634#else
635void tracing_snapshot(void)
636{
637 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
638}
1b22e382 639EXPORT_SYMBOL_GPL(tracing_snapshot);
ad909e21
SRRH
640void tracing_snapshot_alloc(void)
641{
642 /* Give warning */
643 tracing_snapshot();
644}
1b22e382 645EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
ad909e21
SRRH
646#endif /* CONFIG_TRACER_SNAPSHOT */
647
2fd821ee
SRRH
648void tracer_tracing_off(struct trace_array *tr)
649{
650 if (tr->trace_buffer.buffer)
651 ring_buffer_record_off(tr->trace_buffer.buffer);
652 /*
653 * This flag is looked at when buffers haven't been allocated
654 * yet, or by some tracers (like irqsoff), that just want to
655 * know if the ring buffer has been disabled, but it can handle
656 * races of where it gets disabled but we still do a record.
657 * As the check is in the fast path of the tracers, it is more
658 * important to be fast than accurate.
659 */
660 tr->buffer_disabled = 1;
661 /* Make the flag seen by readers */
662 smp_wmb();
663}
664
499e5470
SR
665/**
666 * tracing_off - turn off tracing buffers
667 *
668 * This function stops the tracing buffers from recording data.
669 * It does not disable any overhead the tracers themselves may
670 * be causing. This function simply causes all recording to
671 * the ring buffers to fail.
672 */
673void tracing_off(void)
674{
6fa3eb70
S
675#ifdef CONFIG_MTK_SCHED_TRACERS
676 trace_tracing_on(0, CALLER_ADDR0);
677#endif
2fd821ee 678 tracer_tracing_off(&global_trace);
499e5470
SR
679}
680EXPORT_SYMBOL_GPL(tracing_off);
681
2fd821ee
SRRH
682/**
683 * tracer_tracing_is_on - show real state of ring buffer enabled
684 * @tr : the trace array to know if ring buffer is enabled
685 *
686 * Shows real state of the ring buffer if it is enabled or not.
687 */
688int tracer_tracing_is_on(struct trace_array *tr)
689{
690 if (tr->trace_buffer.buffer)
691 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
692 return !tr->buffer_disabled;
693}
694
499e5470
SR
695/**
696 * tracing_is_on - show state of ring buffers enabled
697 */
698int tracing_is_on(void)
699{
2fd821ee 700 return tracer_tracing_is_on(&global_trace);
499e5470
SR
701}
702EXPORT_SYMBOL_GPL(tracing_is_on);
703
3928a8a2 704static int __init set_buf_size(char *str)
bc0c38d1 705{
3928a8a2 706 unsigned long buf_size;
c6caeeb1 707
bc0c38d1
SR
708 if (!str)
709 return 0;
9d612bef 710 buf_size = memparse(str, &str);
c6caeeb1 711 /* nr_entries can not be zero */
9d612bef 712 if (buf_size == 0)
c6caeeb1 713 return 0;
3928a8a2 714 trace_buf_size = buf_size;
6fa3eb70
S
715#ifdef CONFIG_MTK_SCHED_TRACERS
716 trace_buf_size_cpu0 =
717 trace_buf_size_cpuX = buf_size ;
718 trace_buf_size_updated_from_cmdline = 1;
719#endif
bc0c38d1
SR
720 return 1;
721}
3928a8a2 722__setup("trace_buf_size=", set_buf_size);
bc0c38d1 723
0e950173
TB
724static int __init set_tracing_thresh(char *str)
725{
87abb3b1 726 unsigned long threshold;
0e950173
TB
727 int ret;
728
729 if (!str)
730 return 0;
bcd83ea6 731 ret = kstrtoul(str, 0, &threshold);
0e950173
TB
732 if (ret < 0)
733 return 0;
87abb3b1 734 tracing_thresh = threshold * 1000;
0e950173
TB
735 return 1;
736}
737__setup("tracing_thresh=", set_tracing_thresh);
738
57f50be1
SR
739unsigned long nsecs_to_usecs(unsigned long nsecs)
740{
741 return nsecs / 1000;
742}
743
4fcdae83 744/* These must match the bit postions in trace_iterator_flags */
bc0c38d1
SR
745static const char *trace_options[] = {
746 "print-parent",
747 "sym-offset",
748 "sym-addr",
749 "verbose",
f9896bf3 750 "raw",
5e3ca0ec 751 "hex",
cb0f12aa 752 "bin",
2a2cc8f7 753 "block",
86387f7e 754 "stacktrace",
5e1607a0 755 "trace_printk",
b2a866f9 756 "ftrace_preempt",
9f029e83 757 "branch",
12ef7d44 758 "annotate",
02b67518 759 "userstacktrace",
b54d3de9 760 "sym-userobj",
66896a85 761 "printk-msg-only",
c4a8e8be 762 "context-info",
c032ef64 763 "latency-format",
be6f164a 764 "sleep-time",
a2a16d6a 765 "graph-time",
e870e9a1 766 "record-cmd",
750912fa 767 "overwrite",
cf30cf67 768 "disable_on_free",
77271ce4 769 "irq-info",
5224c3a3 770 "markers",
328df475 771 "function-trace",
6fa3eb70 772 "print-tgid",
bc0c38d1
SR
773 NULL
774};
775
5079f326
Z
776static struct {
777 u64 (*func)(void);
778 const char *name;
8be0709f 779 int in_ns; /* is this clock in nanoseconds? */
5079f326 780} trace_clocks[] = {
8be0709f
DS
781 { trace_clock_local, "local", 1 },
782 { trace_clock_global, "global", 1 },
783 { trace_clock_counter, "counter", 0 },
efd39f77 784 { trace_clock_jiffies, "uptime", 0 },
76f11917 785 { trace_clock, "perf", 1 },
8cbd9cc6 786 ARCH_TRACE_CLOCKS
5079f326
Z
787};
788
b63f39ea 789/*
790 * trace_parser_get_init - gets the buffer for trace parser
791 */
792int trace_parser_get_init(struct trace_parser *parser, int size)
793{
794 memset(parser, 0, sizeof(*parser));
795
796 parser->buffer = kmalloc(size, GFP_KERNEL);
797 if (!parser->buffer)
798 return 1;
799
800 parser->size = size;
801 return 0;
802}
803
804/*
805 * trace_parser_put - frees the buffer for trace parser
806 */
807void trace_parser_put(struct trace_parser *parser)
808{
809 kfree(parser->buffer);
810}
811
812/*
813 * trace_get_user - reads the user input string separated by space
814 * (matched by isspace(ch))
815 *
816 * For each string found the 'struct trace_parser' is updated,
817 * and the function returns.
818 *
819 * Returns number of bytes read.
820 *
821 * See kernel/trace/trace.h for 'struct trace_parser' details.
822 */
823int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
824 size_t cnt, loff_t *ppos)
825{
826 char ch;
827 size_t read = 0;
828 ssize_t ret;
829
830 if (!*ppos)
831 trace_parser_clear(parser);
832
833 ret = get_user(ch, ubuf++);
834 if (ret)
835 goto out;
836
837 read++;
838 cnt--;
839
840 /*
841 * The parser is not finished with the last write,
842 * continue reading the user input without skipping spaces.
843 */
844 if (!parser->cont) {
845 /* skip white space */
846 while (cnt && isspace(ch)) {
847 ret = get_user(ch, ubuf++);
848 if (ret)
849 goto out;
850 read++;
851 cnt--;
852 }
853
854 /* only spaces were written */
855 if (isspace(ch)) {
856 *ppos += read;
857 ret = read;
858 goto out;
859 }
860
861 parser->idx = 0;
862 }
863
864 /* read the non-space input */
865 while (cnt && !isspace(ch)) {
3c235a33 866 if (parser->idx < parser->size - 1)
b63f39ea 867 parser->buffer[parser->idx++] = ch;
868 else {
869 ret = -EINVAL;
870 goto out;
871 }
872 ret = get_user(ch, ubuf++);
873 if (ret)
874 goto out;
875 read++;
876 cnt--;
877 }
878
879 /* We either got finished input or we have to wait for another call. */
880 if (isspace(ch)) {
881 parser->buffer[parser->idx] = 0;
882 parser->cont = false;
84300636 883 } else if (parser->idx < parser->size - 1) {
b63f39ea 884 parser->cont = true;
885 parser->buffer[parser->idx++] = ch;
84300636
SR
886 } else {
887 ret = -EINVAL;
888 goto out;
b63f39ea 889 }
890
891 *ppos += read;
892 ret = read;
893
894out:
895 return ret;
896}
897
6c6c2796
PP
898ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
899{
900 int len;
901 int ret;
902
2dc5d12b
SR
903 if (!cnt)
904 return 0;
905
6c6c2796
PP
906 if (s->len <= s->readpos)
907 return -EBUSY;
908
909 len = s->len - s->readpos;
910 if (cnt > len)
911 cnt = len;
912 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
2dc5d12b 913 if (ret == cnt)
6c6c2796
PP
914 return -EFAULT;
915
2dc5d12b
SR
916 cnt -= ret;
917
e74da523 918 s->readpos += cnt;
6c6c2796 919 return cnt;
214023c3
SR
920}
921
b8b94265 922static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
3c56819b
EGM
923{
924 int len;
3c56819b
EGM
925
926 if (s->len <= s->readpos)
927 return -EBUSY;
928
929 len = s->len - s->readpos;
930 if (cnt > len)
931 cnt = len;
5a26c8f0 932 memcpy(buf, s->buffer + s->readpos, cnt);
3c56819b 933
e74da523 934 s->readpos += cnt;
3c56819b
EGM
935 return cnt;
936}
937
5d4a9dba
SR
938/*
939 * ftrace_max_lock is used to protect the swapping of buffers
940 * when taking a max snapshot. The buffers themselves are
941 * protected by per_cpu spinlocks. But the action of the swap
942 * needs its own lock.
943 *
445c8951 944 * This is defined as a arch_spinlock_t in order to help
5d4a9dba
SR
945 * with performance when lockdep debugging is enabled.
946 *
947 * It is also used in other places outside the update_max_tr
948 * so it needs to be defined outside of the
949 * CONFIG_TRACER_MAX_TRACE.
950 */
445c8951 951static arch_spinlock_t ftrace_max_lock =
edc35bd7 952 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5d4a9dba 953
0e950173
TB
954unsigned long __read_mostly tracing_thresh;
955
5d4a9dba
SR
956#ifdef CONFIG_TRACER_MAX_TRACE
957unsigned long __read_mostly tracing_max_latency;
5d4a9dba
SR
958
959/*
960 * Copy the new maximum trace into the separate maximum-trace
961 * structure. (this way the maximum trace is permanently saved,
962 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
963 */
964static void
965__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
966{
12883efb
SRRH
967 struct trace_buffer *trace_buf = &tr->trace_buffer;
968 struct trace_buffer *max_buf = &tr->max_buffer;
969 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
970 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
5d4a9dba 971
12883efb
SRRH
972 max_buf->cpu = cpu;
973 max_buf->time_start = data->preempt_timestamp;
5d4a9dba 974
8248ac05
SR
975 max_data->saved_latency = tracing_max_latency;
976 max_data->critical_start = data->critical_start;
977 max_data->critical_end = data->critical_end;
5d4a9dba 978
1acaa1b2 979 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
8248ac05 980 max_data->pid = tsk->pid;
f17a5194
SRRH
981 /*
982 * If tsk == current, then use current_uid(), as that does not use
983 * RCU. The irq tracer can be called out of RCU scope.
984 */
985 if (tsk == current)
986 max_data->uid = current_uid();
987 else
988 max_data->uid = task_uid(tsk);
989
8248ac05
SR
990 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
991 max_data->policy = tsk->policy;
992 max_data->rt_priority = tsk->rt_priority;
5d4a9dba
SR
993
994 /* record this tasks comm */
995 tracing_record_cmdline(tsk);
996}
997
4fcdae83
SR
998/**
999 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1000 * @tr: tracer
1001 * @tsk: the task with the latency
1002 * @cpu: The cpu that initiated the trace.
1003 *
1004 * Flip the buffers between the @tr and the max_tr and record information
1005 * about which task was the cause of this latency.
1006 */
e309b41d 1007void
bc0c38d1
SR
1008update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1009{
2721e72d 1010 struct ring_buffer *buf;
bc0c38d1 1011
2b6080f2 1012 if (tr->stop_count)
b8de7bd1
SR
1013 return;
1014
4c11d7ae 1015 WARN_ON_ONCE(!irqs_disabled());
34600f0e 1016
45ad21ca 1017 if (!tr->allocated_snapshot) {
debdd57f 1018 /* Only the nop tracer should hit this when disabling */
2b6080f2 1019 WARN_ON_ONCE(tr->current_trace != &nop_trace);
34600f0e 1020 return;
debdd57f 1021 }
34600f0e 1022
0199c4e6 1023 arch_spin_lock(&ftrace_max_lock);
3928a8a2 1024
12883efb
SRRH
1025 buf = tr->trace_buffer.buffer;
1026 tr->trace_buffer.buffer = tr->max_buffer.buffer;
1027 tr->max_buffer.buffer = buf;
3928a8a2 1028
bc0c38d1 1029 __update_max_tr(tr, tsk, cpu);
0199c4e6 1030 arch_spin_unlock(&ftrace_max_lock);
bc0c38d1
SR
1031}
1032
1033/**
1034 * update_max_tr_single - only copy one trace over, and reset the rest
1035 * @tr - tracer
1036 * @tsk - task with the latency
1037 * @cpu - the cpu of the buffer to copy.
4fcdae83
SR
1038 *
1039 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
bc0c38d1 1040 */
e309b41d 1041void
bc0c38d1
SR
1042update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1043{
3928a8a2 1044 int ret;
bc0c38d1 1045
2b6080f2 1046 if (tr->stop_count)
b8de7bd1
SR
1047 return;
1048
4c11d7ae 1049 WARN_ON_ONCE(!irqs_disabled());
6c24499f 1050 if (!tr->allocated_snapshot) {
2930e04d 1051 /* Only the nop tracer should hit this when disabling */
9e8529af 1052 WARN_ON_ONCE(tr->current_trace != &nop_trace);
ef710e10 1053 return;
2930e04d 1054 }
ef710e10 1055
0199c4e6 1056 arch_spin_lock(&ftrace_max_lock);
bc0c38d1 1057
12883efb 1058 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
3928a8a2 1059
e8165dbb
SR
1060 if (ret == -EBUSY) {
1061 /*
1062 * We failed to swap the buffer due to a commit taking
1063 * place on this CPU. We fail to record, but we reset
1064 * the max trace buffer (no one writes directly to it)
1065 * and flag that it failed.
1066 */
12883efb 1067 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
e8165dbb
SR
1068 "Failed to swap buffers due to commit in progress\n");
1069 }
1070
e8165dbb 1071 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
bc0c38d1
SR
1072
1073 __update_max_tr(tr, tsk, cpu);
0199c4e6 1074 arch_spin_unlock(&ftrace_max_lock);
bc0c38d1 1075}
5d4a9dba 1076#endif /* CONFIG_TRACER_MAX_TRACE */
bc0c38d1 1077
561237e4 1078static int default_wait_pipe(struct trace_iterator *iter)
0d5c6e1c 1079{
15693458
SRRH
1080 /* Iterators are static, they should be filled or empty */
1081 if (trace_buffer_iter(iter, iter->cpu_file))
561237e4 1082 return 0;
0d5c6e1c 1083
561237e4 1084 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
0d5c6e1c
SR
1085}
1086
f4e781c0
SRRH
1087#ifdef CONFIG_FTRACE_STARTUP_TEST
1088static int run_tracer_selftest(struct tracer *type)
1089{
1090 struct trace_array *tr = &global_trace;
1091 struct tracer *saved_tracer = tr->current_trace;
1092 int ret;
0d5c6e1c 1093
f4e781c0
SRRH
1094 if (!type->selftest || tracing_selftest_disabled)
1095 return 0;
0d5c6e1c
SR
1096
1097 /*
f4e781c0
SRRH
1098 * Run a selftest on this tracer.
1099 * Here we reset the trace buffer, and set the current
1100 * tracer to be this tracer. The tracer can then run some
1101 * internal tracing to verify that everything is in order.
1102 * If we fail, we do not register this tracer.
0d5c6e1c 1103 */
f4e781c0 1104 tracing_reset_online_cpus(&tr->trace_buffer);
0d5c6e1c 1105
f4e781c0
SRRH
1106 tr->current_trace = type;
1107
1108#ifdef CONFIG_TRACER_MAX_TRACE
1109 if (type->use_max_tr) {
1110 /* If we expanded the buffers, make sure the max is expanded too */
1111 if (ring_buffer_expanded)
1112 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1113 RING_BUFFER_ALL_CPUS);
1114 tr->allocated_snapshot = true;
1115 }
1116#endif
1117
1118 /* the test is responsible for initializing and enabling */
1119 pr_info("Testing tracer %s: ", type->name);
1120 ret = type->selftest(type, tr);
1121 /* the test is responsible for resetting too */
1122 tr->current_trace = saved_tracer;
1123 if (ret) {
1124 printk(KERN_CONT "FAILED!\n");
1125 /* Add the warning after printing 'FAILED' */
1126 WARN_ON(1);
1127 return -1;
1128 }
1129 /* Only reset on passing, to avoid touching corrupted buffers */
1130 tracing_reset_online_cpus(&tr->trace_buffer);
1131
1132#ifdef CONFIG_TRACER_MAX_TRACE
1133 if (type->use_max_tr) {
1134 tr->allocated_snapshot = false;
0d5c6e1c 1135
f4e781c0
SRRH
1136 /* Shrink the max buffer again */
1137 if (ring_buffer_expanded)
1138 ring_buffer_resize(tr->max_buffer.buffer, 1,
1139 RING_BUFFER_ALL_CPUS);
1140 }
1141#endif
1142
1143 printk(KERN_CONT "PASSED\n");
1144 return 0;
1145}
1146#else
1147static inline int run_tracer_selftest(struct tracer *type)
1148{
1149 return 0;
0d5c6e1c 1150}
f4e781c0 1151#endif /* CONFIG_FTRACE_STARTUP_TEST */
0d5c6e1c 1152
4fcdae83
SR
1153/**
1154 * register_tracer - register a tracer with the ftrace system.
1155 * @type - the plugin for the tracer
1156 *
1157 * Register a new plugin tracer.
1158 */
bc0c38d1
SR
1159int register_tracer(struct tracer *type)
1160{
1161 struct tracer *t;
bc0c38d1
SR
1162 int ret = 0;
1163
1164 if (!type->name) {
1165 pr_info("Tracer must have a name\n");
1166 return -1;
1167 }
1168
24a461d5 1169 if (strlen(type->name) >= MAX_TRACER_SIZE) {
ee6c2c1b
LZ
1170 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1171 return -1;
1172 }
1173
bc0c38d1 1174 mutex_lock(&trace_types_lock);
86fa2f60 1175
8e1b82e0
FW
1176 tracing_selftest_running = true;
1177
bc0c38d1
SR
1178 for (t = trace_types; t; t = t->next) {
1179 if (strcmp(type->name, t->name) == 0) {
1180 /* already found */
ee6c2c1b 1181 pr_info("Tracer %s already registered\n",
bc0c38d1
SR
1182 type->name);
1183 ret = -1;
1184 goto out;
1185 }
1186 }
1187
adf9f195
FW
1188 if (!type->set_flag)
1189 type->set_flag = &dummy_set_flag;
1190 if (!type->flags)
1191 type->flags = &dummy_tracer_flags;
1192 else
1193 if (!type->flags->opts)
1194 type->flags->opts = dummy_tracer_opt;
6eaaa5d5
FW
1195 if (!type->wait_pipe)
1196 type->wait_pipe = default_wait_pipe;
1197
f4e781c0
SRRH
1198 ret = run_tracer_selftest(type);
1199 if (ret < 0)
1200 goto out;
60a11774 1201
bc0c38d1
SR
1202 type->next = trace_types;
1203 trace_types = type;
60a11774 1204
bc0c38d1 1205 out:
8e1b82e0 1206 tracing_selftest_running = false;
bc0c38d1
SR
1207 mutex_unlock(&trace_types_lock);
1208
dac74940
SR
1209 if (ret || !default_bootup_tracer)
1210 goto out_unlock;
1211
ee6c2c1b 1212 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
dac74940
SR
1213 goto out_unlock;
1214
1215 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1216 /* Do we want this tracer to start on bootup? */
1217 tracing_set_tracer(type->name);
1218 default_bootup_tracer = NULL;
1219 /* disable other selftests, since this will break it. */
55034cd6 1220 tracing_selftest_disabled = true;
b2821ae6 1221#ifdef CONFIG_FTRACE_STARTUP_TEST
dac74940
SR
1222 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1223 type->name);
b2821ae6 1224#endif
b2821ae6 1225
dac74940 1226 out_unlock:
bc0c38d1
SR
1227 return ret;
1228}
1229
12883efb 1230void tracing_reset(struct trace_buffer *buf, int cpu)
f633903a 1231{
12883efb 1232 struct ring_buffer *buffer = buf->buffer;
f633903a 1233
a5416411
HT
1234 if (!buffer)
1235 return;
1236
f633903a
SR
1237 ring_buffer_record_disable(buffer);
1238
1239 /* Make sure all commits have finished */
1240 synchronize_sched();
68179686 1241 ring_buffer_reset_cpu(buffer, cpu);
f633903a 1242
6fa3eb70 1243 printk(KERN_INFO "[ftrace]cpu %d trace reset\n", cpu);
f633903a
SR
1244 ring_buffer_record_enable(buffer);
1245}
1246
12883efb 1247void tracing_reset_online_cpus(struct trace_buffer *buf)
213cc060 1248{
12883efb 1249 struct ring_buffer *buffer = buf->buffer;
213cc060
PE
1250 int cpu;
1251
a5416411
HT
1252 if (!buffer)
1253 return;
1254
621968cd
SR
1255 ring_buffer_record_disable(buffer);
1256
1257 /* Make sure all commits have finished */
1258 synchronize_sched();
1259
e88512e7 1260 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
213cc060
PE
1261
1262 for_each_online_cpu(cpu)
68179686 1263 ring_buffer_reset_cpu(buffer, cpu);
621968cd 1264
6fa3eb70 1265 printk(KERN_INFO "[ftrace]all cpu trace reset\n");
621968cd 1266 ring_buffer_record_enable(buffer);
213cc060
PE
1267}
1268
c62771c8 1269/* Must have trace_types_lock held */
873c642f 1270void tracing_reset_all_online_cpus(void)
9456f0fa 1271{
873c642f
SRRH
1272 struct trace_array *tr;
1273
873c642f 1274 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
12883efb
SRRH
1275 tracing_reset_online_cpus(&tr->trace_buffer);
1276#ifdef CONFIG_TRACER_MAX_TRACE
1277 tracing_reset_online_cpus(&tr->max_buffer);
1278#endif
873c642f 1279 }
9456f0fa
SR
1280}
1281
bc0c38d1 1282#define SAVED_CMDLINES 128
2c7eea4c 1283#define NO_CMDLINE_MAP UINT_MAX
6fa3eb70
S
1284#ifdef CONFIG_MTK_EXTMEM
1285extern void* extmem_malloc_page_align(size_t bytes);
1286#define SIZEOF_MAP_PID_TO_CMDLINE ((PID_MAX_DEFAULT+1)*sizeof(unsigned))
1287#define SIZEOF_MAP_CMDLINE_TO_PID (SAVED_CMDLINES*sizeof(unsigned))
1288static unsigned* map_pid_to_cmdline = NULL;
1289static unsigned* map_cmdline_to_pid = NULL;
1290#else
bc0c38d1
SR
1291static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1292static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
6fa3eb70 1293#endif
bc0c38d1 1294static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
6fa3eb70 1295static unsigned saved_tgids[SAVED_CMDLINES];
bc0c38d1 1296static int cmdline_idx;
edc35bd7 1297static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
25b0b44a 1298
25b0b44a 1299/* temporary disable recording */
4fd27358 1300static atomic_t trace_record_cmdline_disabled __read_mostly;
bc0c38d1
SR
1301
1302static void trace_init_cmdlines(void)
1303{
6fa3eb70
S
1304#ifdef CONFIG_MTK_EXTMEM
1305 map_pid_to_cmdline = (unsigned *) extmem_malloc_page_align(SIZEOF_MAP_PID_TO_CMDLINE);
1306 if(map_pid_to_cmdline == NULL) {
1307 pr_err("%s[%s] ext memory alloc failed!!!\n", __FILE__, __FUNCTION__);
1308 map_pid_to_cmdline = (unsigned *)vmalloc(SIZEOF_MAP_PID_TO_CMDLINE);
1309 }
1310 map_cmdline_to_pid = (unsigned *) extmem_malloc_page_align(SIZEOF_MAP_CMDLINE_TO_PID);
1311 if(map_pid_to_cmdline == NULL) {
1312 pr_warning("%s[%s] ext memory alloc failed!!!\n", __FILE__, __FUNCTION__);
1313 map_cmdline_to_pid = (unsigned *)vmalloc(SIZEOF_MAP_CMDLINE_TO_PID);
1314 }
1315 memset(map_pid_to_cmdline, NO_CMDLINE_MAP, SIZEOF_MAP_PID_TO_CMDLINE);
1316 memset(map_cmdline_to_pid, NO_CMDLINE_MAP, SIZEOF_MAP_CMDLINE_TO_PID);
1317#else
2c7eea4c
TG
1318 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1319 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
6fa3eb70 1320#endif
bc0c38d1
SR
1321 cmdline_idx = 0;
1322}
1323
b5130b1e
CE
1324int is_tracing_stopped(void)
1325{
2b6080f2 1326 return global_trace.stop_count;
b5130b1e
CE
1327}
1328
69bb54ec
SR
1329/**
1330 * ftrace_off_permanent - disable all ftrace code permanently
1331 *
1332 * This should only be called when a serious anomally has
1333 * been detected. This will turn off the function tracing,
1334 * ring buffers, and other tracing utilites. It takes no
1335 * locks and can be called from any context.
1336 */
1337void ftrace_off_permanent(void)
1338{
1339 tracing_disabled = 1;
1340 ftrace_stop();
1341 tracing_off_permanent();
1342}
1343
0f048701
SR
1344/**
1345 * tracing_start - quick start of the tracer
1346 *
1347 * If tracing is enabled but was stopped by tracing_stop,
1348 * this will start the tracer back up.
1349 */
1350void tracing_start(void)
1351{
1352 struct ring_buffer *buffer;
1353 unsigned long flags;
6fa3eb70 1354 int reset_ftrace = 0;
0f048701
SR
1355
1356 if (tracing_disabled)
1357 return;
1358
2b6080f2
SR
1359 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1360 if (--global_trace.stop_count) {
1361 if (global_trace.stop_count < 0) {
b06a8301
SR
1362 /* Someone screwed up their debugging */
1363 WARN_ON_ONCE(1);
2b6080f2 1364 global_trace.stop_count = 0;
6fa3eb70 1365 reset_ftrace = 1;
b06a8301 1366 }
0f048701 1367 goto out;
6fa3eb70
S
1368 }else
1369 reset_ftrace = 1;
1370
0f048701 1371
a2f80714
SR
1372 /* Prevent the buffers from switching */
1373 arch_spin_lock(&ftrace_max_lock);
0f048701 1374
12883efb 1375 buffer = global_trace.trace_buffer.buffer;
0f048701
SR
1376 if (buffer)
1377 ring_buffer_record_enable(buffer);
1378
12883efb
SRRH
1379#ifdef CONFIG_TRACER_MAX_TRACE
1380 buffer = global_trace.max_buffer.buffer;
0f048701
SR
1381 if (buffer)
1382 ring_buffer_record_enable(buffer);
12883efb 1383#endif
0f048701 1384
a2f80714
SR
1385 arch_spin_unlock(&ftrace_max_lock);
1386
0f048701 1387 out:
2b6080f2 1388 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
6fa3eb70
S
1389
1390#ifdef CONFIG_MTK_SCHED_TRACERS
1391 // reset ring buffer when all readers left
1392 if(reset_ftrace == 1 && global_trace.stop_count == 0)
1393 tracing_reset_online_cpus(&global_trace.trace_buffer);
1394#endif
2b6080f2
SR
1395}
1396
1397static void tracing_start_tr(struct trace_array *tr)
1398{
1399 struct ring_buffer *buffer;
1400 unsigned long flags;
1401
1402 if (tracing_disabled)
1403 return;
1404
1405 /* If global, we need to also start the max tracer */
1406 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1407 return tracing_start();
1408
1409 raw_spin_lock_irqsave(&tr->start_lock, flags);
1410
1411 if (--tr->stop_count) {
1412 if (tr->stop_count < 0) {
1413 /* Someone screwed up their debugging */
1414 WARN_ON_ONCE(1);
1415 tr->stop_count = 0;
1416 }
1417 goto out;
1418 }
1419
12883efb 1420 buffer = tr->trace_buffer.buffer;
2b6080f2
SR
1421 if (buffer)
1422 ring_buffer_record_enable(buffer);
1423
1424 out:
1425 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
0f048701
SR
1426}
1427
1428/**
1429 * tracing_stop - quick stop of the tracer
1430 *
1431 * Light weight way to stop tracing. Use in conjunction with
1432 * tracing_start.
1433 */
1434void tracing_stop(void)
1435{
1436 struct ring_buffer *buffer;
1437 unsigned long flags;
1438
2b6080f2
SR
1439 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1440 if (global_trace.stop_count++)
0f048701
SR
1441 goto out;
1442
a2f80714
SR
1443 /* Prevent the buffers from switching */
1444 arch_spin_lock(&ftrace_max_lock);
1445
12883efb 1446 buffer = global_trace.trace_buffer.buffer;
0f048701
SR
1447 if (buffer)
1448 ring_buffer_record_disable(buffer);
1449
12883efb
SRRH
1450#ifdef CONFIG_TRACER_MAX_TRACE
1451 buffer = global_trace.max_buffer.buffer;
0f048701
SR
1452 if (buffer)
1453 ring_buffer_record_disable(buffer);
12883efb 1454#endif
0f048701 1455
a2f80714
SR
1456 arch_spin_unlock(&ftrace_max_lock);
1457
0f048701 1458 out:
2b6080f2
SR
1459 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1460}
1461
1462static void tracing_stop_tr(struct trace_array *tr)
1463{
1464 struct ring_buffer *buffer;
1465 unsigned long flags;
1466
1467 /* If global, we need to also stop the max tracer */
1468 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1469 return tracing_stop();
1470
1471 raw_spin_lock_irqsave(&tr->start_lock, flags);
1472 if (tr->stop_count++)
1473 goto out;
1474
12883efb 1475 buffer = tr->trace_buffer.buffer;
2b6080f2
SR
1476 if (buffer)
1477 ring_buffer_record_disable(buffer);
1478
1479 out:
1480 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
0f048701
SR
1481}
1482
e309b41d 1483void trace_stop_cmdline_recording(void);
bc0c38d1 1484
c5bce736 1485static int trace_save_cmdline(struct task_struct *tsk)
bc0c38d1 1486{
a635cf04 1487 unsigned pid, idx;
bc0c38d1
SR
1488
1489 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
c5bce736 1490 return 0;
bc0c38d1
SR
1491
1492 /*
1493 * It's not the end of the world if we don't get
1494 * the lock, but we also don't want to spin
1495 * nor do we want to disable interrupts,
1496 * so if we miss here, then better luck next time.
1497 */
0199c4e6 1498 if (!arch_spin_trylock(&trace_cmdline_lock))
c5bce736 1499 return 0;
bc0c38d1
SR
1500
1501 idx = map_pid_to_cmdline[tsk->pid];
2c7eea4c 1502 if (idx == NO_CMDLINE_MAP) {
bc0c38d1
SR
1503 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1504
a635cf04
CE
1505 /*
1506 * Check whether the cmdline buffer at idx has a pid
1507 * mapped. We are going to overwrite that entry so we
1508 * need to clear the map_pid_to_cmdline. Otherwise we
1509 * would read the new comm for the old pid.
1510 */
1511 pid = map_cmdline_to_pid[idx];
1512 if (pid != NO_CMDLINE_MAP)
1513 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
bc0c38d1 1514
a635cf04 1515 map_cmdline_to_pid[idx] = tsk->pid;
bc0c38d1
SR
1516 map_pid_to_cmdline[tsk->pid] = idx;
1517
1518 cmdline_idx = idx;
1519 }
1520
1521 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
6fa3eb70 1522 saved_tgids[idx] = tsk->tgid;
bc0c38d1 1523
0199c4e6 1524 arch_spin_unlock(&trace_cmdline_lock);
c5bce736
SRRH
1525
1526 return 1;
bc0c38d1
SR
1527}
1528
4ca53085 1529void trace_find_cmdline(int pid, char comm[])
bc0c38d1 1530{
bc0c38d1
SR
1531 unsigned map;
1532
4ca53085
SR
1533 if (!pid) {
1534 strcpy(comm, "<idle>");
1535 return;
1536 }
bc0c38d1 1537
74bf4076
SR
1538 if (WARN_ON_ONCE(pid < 0)) {
1539 strcpy(comm, "<XXX>");
1540 return;
1541 }
1542
4ca53085
SR
1543 if (pid > PID_MAX_DEFAULT) {
1544 strcpy(comm, "<...>");
1545 return;
1546 }
bc0c38d1 1547
5b6045a9 1548 preempt_disable();
0199c4e6 1549 arch_spin_lock(&trace_cmdline_lock);
bc0c38d1 1550 map = map_pid_to_cmdline[pid];
50d88758
TG
1551 if (map != NO_CMDLINE_MAP)
1552 strcpy(comm, saved_cmdlines[map]);
1553 else
1554 strcpy(comm, "<...>");
bc0c38d1 1555
0199c4e6 1556 arch_spin_unlock(&trace_cmdline_lock);
5b6045a9 1557 preempt_enable();
bc0c38d1
SR
1558}
1559
6fa3eb70
S
1560int trace_find_tgid(int pid)
1561{
1562 unsigned map;
1563 int tgid;
1564
1565 preempt_disable();
1566 arch_spin_lock(&trace_cmdline_lock);
1567 map = map_pid_to_cmdline[pid];
1568 if (map != NO_CMDLINE_MAP)
1569 tgid = saved_tgids[map];
1570 else
1571 tgid = -1;
1572
1573 arch_spin_unlock(&trace_cmdline_lock);
1574 preempt_enable();
1575
1576 return tgid;
1577}
1578
e309b41d 1579void tracing_record_cmdline(struct task_struct *tsk)
bc0c38d1 1580{
0fb9656d 1581 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
bc0c38d1
SR
1582 return;
1583
7ffbd48d
SR
1584 if (!__this_cpu_read(trace_cmdline_save))
1585 return;
1586
c5bce736
SRRH
1587 if (trace_save_cmdline(tsk))
1588 __this_cpu_write(trace_cmdline_save, false);
bc0c38d1
SR
1589}
1590
45dcd8b8 1591void
38697053
SR
1592tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1593 int pc)
bc0c38d1
SR
1594{
1595 struct task_struct *tsk = current;
bc0c38d1 1596
777e208d
SR
1597 entry->preempt_count = pc & 0xff;
1598 entry->pid = (tsk) ? tsk->pid : 0;
1599 entry->flags =
9244489a 1600#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
2e2ca155 1601 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
9244489a
SR
1602#else
1603 TRACE_FLAG_IRQS_NOSUPPORT |
1604#endif
bc0c38d1
SR
1605 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1606 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1607 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1608}
f413cdb8 1609EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
bc0c38d1 1610
e77405ad
SR
1611struct ring_buffer_event *
1612trace_buffer_lock_reserve(struct ring_buffer *buffer,
1613 int type,
1614 unsigned long len,
1615 unsigned long flags, int pc)
51a763dd
ACM
1616{
1617 struct ring_buffer_event *event;
1618
e77405ad 1619 event = ring_buffer_lock_reserve(buffer, len);
51a763dd
ACM
1620 if (event != NULL) {
1621 struct trace_entry *ent = ring_buffer_event_data(event);
1622
1623 tracing_generic_entry_update(ent, flags, pc);
1624 ent->type = type;
1625 }
1626
1627 return event;
1628}
51a763dd 1629
7ffbd48d
SR
1630void
1631__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1632{
1633 __this_cpu_write(trace_cmdline_save, true);
1634 ring_buffer_unlock_commit(buffer, event);
1635}
1636
e77405ad
SR
1637static inline void
1638__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1639 struct ring_buffer_event *event,
0d5c6e1c 1640 unsigned long flags, int pc)
51a763dd 1641{
7ffbd48d 1642 __buffer_unlock_commit(buffer, event);
51a763dd 1643
e77405ad
SR
1644 ftrace_trace_stack(buffer, flags, 6, pc);
1645 ftrace_trace_userstack(buffer, flags, pc);
07edf712
FW
1646}
1647
e77405ad
SR
1648void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1649 struct ring_buffer_event *event,
1650 unsigned long flags, int pc)
07edf712 1651{
0d5c6e1c 1652 __trace_buffer_unlock_commit(buffer, event, flags, pc);
51a763dd 1653}
0d5c6e1c 1654EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
51a763dd 1655
ccb469a1
SR
1656struct ring_buffer_event *
1657trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1658 struct ftrace_event_file *ftrace_file,
1659 int type, unsigned long len,
1660 unsigned long flags, int pc)
1661{
12883efb 1662 *current_rb = ftrace_file->tr->trace_buffer.buffer;
ccb469a1
SR
1663 return trace_buffer_lock_reserve(*current_rb,
1664 type, len, flags, pc);
1665}
1666EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1667
ef5580d0 1668struct ring_buffer_event *
e77405ad
SR
1669trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1670 int type, unsigned long len,
ef5580d0
SR
1671 unsigned long flags, int pc)
1672{
12883efb 1673 *current_rb = global_trace.trace_buffer.buffer;
e77405ad 1674 return trace_buffer_lock_reserve(*current_rb,
ef5580d0
SR
1675 type, len, flags, pc);
1676}
94487d6d 1677EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
ef5580d0 1678
e77405ad
SR
1679void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1680 struct ring_buffer_event *event,
ef5580d0
SR
1681 unsigned long flags, int pc)
1682{
0d5c6e1c 1683 __trace_buffer_unlock_commit(buffer, event, flags, pc);
07edf712 1684}
94487d6d 1685EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
07edf712 1686
0d5c6e1c
SR
1687void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1688 struct ring_buffer_event *event,
1689 unsigned long flags, int pc,
1690 struct pt_regs *regs)
1fd8df2c 1691{
7ffbd48d 1692 __buffer_unlock_commit(buffer, event);
1fd8df2c
MH
1693
1694 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1695 ftrace_trace_userstack(buffer, flags, pc);
1696}
0d5c6e1c 1697EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1fd8df2c 1698
e77405ad
SR
1699void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1700 struct ring_buffer_event *event)
77d9f465 1701{
e77405ad 1702 ring_buffer_discard_commit(buffer, event);
ef5580d0 1703}
12acd473 1704EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
ef5580d0 1705
e309b41d 1706void
7be42151 1707trace_function(struct trace_array *tr,
38697053
SR
1708 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1709 int pc)
bc0c38d1 1710{
e1112b4d 1711 struct ftrace_event_call *call = &event_function;
12883efb 1712 struct ring_buffer *buffer = tr->trace_buffer.buffer;
3928a8a2 1713 struct ring_buffer_event *event;
777e208d 1714 struct ftrace_entry *entry;
bc0c38d1 1715
d769041f 1716 /* If we are reading the ring buffer, don't trace */
dd17c8f7 1717 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
d769041f
SR
1718 return;
1719
e77405ad 1720 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
51a763dd 1721 flags, pc);
3928a8a2
SR
1722 if (!event)
1723 return;
1724 entry = ring_buffer_event_data(event);
777e208d
SR
1725 entry->ip = ip;
1726 entry->parent_ip = parent_ip;
e1112b4d 1727
e77405ad 1728 if (!filter_check_discard(call, entry, buffer, event))
7ffbd48d 1729 __buffer_unlock_commit(buffer, event);
bc0c38d1
SR
1730}
1731
e309b41d 1732void
2e0f5761 1733ftrace(struct trace_array *tr, struct trace_array_cpu *data,
38697053
SR
1734 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1735 int pc)
2e0f5761
IM
1736{
1737 if (likely(!atomic_read(&data->disabled)))
7be42151 1738 trace_function(tr, ip, parent_ip, flags, pc);
2e0f5761
IM
1739}
1740
c0a0d0d3 1741#ifdef CONFIG_STACKTRACE
4a9bd3f1
SR
1742
1743#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1744struct ftrace_stack {
1745 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1746};
1747
1748static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1749static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1750
e77405ad 1751static void __ftrace_trace_stack(struct ring_buffer *buffer,
53614991 1752 unsigned long flags,
1fd8df2c 1753 int skip, int pc, struct pt_regs *regs)
86387f7e 1754{
e1112b4d 1755 struct ftrace_event_call *call = &event_kernel_stack;
3928a8a2 1756 struct ring_buffer_event *event;
777e208d 1757 struct stack_entry *entry;
86387f7e 1758 struct stack_trace trace;
4a9bd3f1
SR
1759 int use_stack;
1760 int size = FTRACE_STACK_ENTRIES;
1761
1762 trace.nr_entries = 0;
1763 trace.skip = skip;
1764
1765 /*
1766 * Since events can happen in NMIs there's no safe way to
1767 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1768 * or NMI comes in, it will just have to use the default
1769 * FTRACE_STACK_SIZE.
1770 */
1771 preempt_disable_notrace();
1772
82146529 1773 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
4a9bd3f1
SR
1774 /*
1775 * We don't need any atomic variables, just a barrier.
1776 * If an interrupt comes in, we don't care, because it would
1777 * have exited and put the counter back to what we want.
1778 * We just need a barrier to keep gcc from moving things
1779 * around.
1780 */
1781 barrier();
1782 if (use_stack == 1) {
1783 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1784 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1785
1786 if (regs)
1787 save_stack_trace_regs(regs, &trace);
1788 else
1789 save_stack_trace(&trace);
1790
1791 if (trace.nr_entries > size)
1792 size = trace.nr_entries;
1793 } else
1794 /* From now on, use_stack is a boolean */
1795 use_stack = 0;
1796
1797 size *= sizeof(unsigned long);
86387f7e 1798
e77405ad 1799 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
4a9bd3f1 1800 sizeof(*entry) + size, flags, pc);
3928a8a2 1801 if (!event)
4a9bd3f1
SR
1802 goto out;
1803 entry = ring_buffer_event_data(event);
86387f7e 1804
4a9bd3f1
SR
1805 memset(&entry->caller, 0, size);
1806
1807 if (use_stack)
1808 memcpy(&entry->caller, trace.entries,
1809 trace.nr_entries * sizeof(unsigned long));
1810 else {
1811 trace.max_entries = FTRACE_STACK_ENTRIES;
1812 trace.entries = entry->caller;
1813 if (regs)
1814 save_stack_trace_regs(regs, &trace);
1815 else
1816 save_stack_trace(&trace);
1817 }
1818
1819 entry->size = trace.nr_entries;
86387f7e 1820
e77405ad 1821 if (!filter_check_discard(call, entry, buffer, event))
7ffbd48d 1822 __buffer_unlock_commit(buffer, event);
4a9bd3f1
SR
1823
1824 out:
1825 /* Again, don't let gcc optimize things here */
1826 barrier();
82146529 1827 __this_cpu_dec(ftrace_stack_reserve);
4a9bd3f1
SR
1828 preempt_enable_notrace();
1829
f0a920d5
IM
1830}
1831
1fd8df2c
MH
1832void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1833 int skip, int pc, struct pt_regs *regs)
1834{
1835 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1836 return;
1837
1838 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1839}
1840
e77405ad
SR
1841void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1842 int skip, int pc)
53614991
SR
1843{
1844 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1845 return;
1846
1fd8df2c 1847 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
53614991
SR
1848}
1849
c0a0d0d3
FW
1850void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1851 int pc)
38697053 1852{
12883efb 1853 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
38697053
SR
1854}
1855
03889384
SR
1856/**
1857 * trace_dump_stack - record a stack back trace in the trace buffer
c142be8e 1858 * @skip: Number of functions to skip (helper handlers)
03889384 1859 */
c142be8e 1860void trace_dump_stack(int skip)
03889384
SR
1861{
1862 unsigned long flags;
1863
1864 if (tracing_disabled || tracing_selftest_running)
e36c5458 1865 return;
03889384
SR
1866
1867 local_save_flags(flags);
1868
c142be8e
SRRH
1869 /*
1870 * Skip 3 more, seems to get us at the caller of
1871 * this function.
1872 */
1873 skip += 3;
1874 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1875 flags, skip, preempt_count(), NULL);
03889384
SR
1876}
1877
91e86e56
SR
1878static DEFINE_PER_CPU(int, user_stack_count);
1879
e77405ad
SR
1880void
1881ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
02b67518 1882{
e1112b4d 1883 struct ftrace_event_call *call = &event_user_stack;
8d7c6a96 1884 struct ring_buffer_event *event;
02b67518
TE
1885 struct userstack_entry *entry;
1886 struct stack_trace trace;
02b67518
TE
1887
1888 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1889 return;
1890
b6345879
SR
1891 /*
1892 * NMIs can not handle page faults, even with fix ups.
1893 * The save user stack can (and often does) fault.
1894 */
1895 if (unlikely(in_nmi()))
1896 return;
02b67518 1897
91e86e56
SR
1898 /*
1899 * prevent recursion, since the user stack tracing may
1900 * trigger other kernel events.
1901 */
1902 preempt_disable();
1903 if (__this_cpu_read(user_stack_count))
1904 goto out;
1905
1906 __this_cpu_inc(user_stack_count);
1907
e77405ad 1908 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
51a763dd 1909 sizeof(*entry), flags, pc);
02b67518 1910 if (!event)
1dbd1951 1911 goto out_drop_count;
02b67518 1912 entry = ring_buffer_event_data(event);
02b67518 1913
48659d31 1914 entry->tgid = current->tgid;
02b67518
TE
1915 memset(&entry->caller, 0, sizeof(entry->caller));
1916
1917 trace.nr_entries = 0;
1918 trace.max_entries = FTRACE_STACK_ENTRIES;
1919 trace.skip = 0;
1920 trace.entries = entry->caller;
1921
1922 save_stack_trace_user(&trace);
e77405ad 1923 if (!filter_check_discard(call, entry, buffer, event))
7ffbd48d 1924 __buffer_unlock_commit(buffer, event);
91e86e56 1925
1dbd1951 1926 out_drop_count:
91e86e56 1927 __this_cpu_dec(user_stack_count);
91e86e56
SR
1928 out:
1929 preempt_enable();
02b67518
TE
1930}
1931
4fd27358
HE
1932#ifdef UNUSED
1933static void __trace_userstack(struct trace_array *tr, unsigned long flags)
02b67518 1934{
7be42151 1935 ftrace_trace_userstack(tr, flags, preempt_count());
02b67518 1936}
4fd27358 1937#endif /* UNUSED */
02b67518 1938
c0a0d0d3
FW
1939#endif /* CONFIG_STACKTRACE */
1940
07d777fe
SR
1941/* created for use with alloc_percpu */
1942struct trace_buffer_struct {
1943 char buffer[TRACE_BUF_SIZE];
1944};
1945
1946static struct trace_buffer_struct *trace_percpu_buffer;
1947static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1948static struct trace_buffer_struct *trace_percpu_irq_buffer;
1949static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1950
1951/*
1952 * The buffer used is dependent on the context. There is a per cpu
1953 * buffer for normal context, softirq contex, hard irq context and
1954 * for NMI context. Thise allows for lockless recording.
1955 *
1956 * Note, if the buffers failed to be allocated, then this returns NULL
1957 */
1958static char *get_trace_buf(void)
1959{
1960 struct trace_buffer_struct *percpu_buffer;
07d777fe
SR
1961
1962 /*
1963 * If we have allocated per cpu buffers, then we do not
1964 * need to do any locking.
1965 */
1966 if (in_nmi())
1967 percpu_buffer = trace_percpu_nmi_buffer;
1968 else if (in_irq())
1969 percpu_buffer = trace_percpu_irq_buffer;
1970 else if (in_softirq())
1971 percpu_buffer = trace_percpu_sirq_buffer;
1972 else
1973 percpu_buffer = trace_percpu_buffer;
1974
1975 if (!percpu_buffer)
1976 return NULL;
1977
d8a0349c 1978 return this_cpu_ptr(&percpu_buffer->buffer[0]);
07d777fe
SR
1979}
1980
1981static int alloc_percpu_trace_buffer(void)
1982{
1983 struct trace_buffer_struct *buffers;
1984 struct trace_buffer_struct *sirq_buffers;
1985 struct trace_buffer_struct *irq_buffers;
1986 struct trace_buffer_struct *nmi_buffers;
1987
1988 buffers = alloc_percpu(struct trace_buffer_struct);
1989 if (!buffers)
1990 goto err_warn;
1991
1992 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1993 if (!sirq_buffers)
1994 goto err_sirq;
1995
1996 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1997 if (!irq_buffers)
1998 goto err_irq;
1999
2000 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
2001 if (!nmi_buffers)
2002 goto err_nmi;
2003
2004 trace_percpu_buffer = buffers;
2005 trace_percpu_sirq_buffer = sirq_buffers;
2006 trace_percpu_irq_buffer = irq_buffers;
2007 trace_percpu_nmi_buffer = nmi_buffers;
2008
2009 return 0;
2010
2011 err_nmi:
2012 free_percpu(irq_buffers);
2013 err_irq:
2014 free_percpu(sirq_buffers);
2015 err_sirq:
2016 free_percpu(buffers);
2017 err_warn:
2018 WARN(1, "Could not allocate percpu trace_printk buffer");
2019 return -ENOMEM;
2020}
2021
81698831
SR
2022static int buffers_allocated;
2023
07d777fe
SR
2024void trace_printk_init_buffers(void)
2025{
07d777fe
SR
2026 if (buffers_allocated)
2027 return;
2028
2029 if (alloc_percpu_trace_buffer())
2030 return;
2031
2032 pr_info("ftrace: Allocated trace_printk buffers\n");
2033
b382ede6 2034 /* Expand the buffers to set size */
6fa3eb70
S
2035 /* M: avoid to expand buffer because of trace_printk in kernel */
2036 /* tracing_update_buffers(); */
b382ede6 2037
07d777fe 2038 buffers_allocated = 1;
81698831
SR
2039
2040 /*
2041 * trace_printk_init_buffers() can be called by modules.
2042 * If that happens, then we need to start cmdline recording
2043 * directly here. If the global_trace.buffer is already
2044 * allocated here, then this was called by module code.
2045 */
12883efb 2046 if (global_trace.trace_buffer.buffer)
81698831
SR
2047 tracing_start_cmdline_record();
2048}
2049
2050void trace_printk_start_comm(void)
2051{
2052 /* Start tracing comms if trace printk is set */
2053 if (!buffers_allocated)
2054 return;
2055 tracing_start_cmdline_record();
2056}
2057
2058static void trace_printk_start_stop_comm(int enabled)
2059{
2060 if (!buffers_allocated)
2061 return;
2062
2063 if (enabled)
2064 tracing_start_cmdline_record();
2065 else
2066 tracing_stop_cmdline_record();
07d777fe
SR
2067}
2068
769b0441 2069/**
48ead020 2070 * trace_vbprintk - write binary msg to tracing buffer
769b0441
FW
2071 *
2072 */
40ce74f1 2073int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
769b0441 2074{
e1112b4d 2075 struct ftrace_event_call *call = &event_bprint;
769b0441 2076 struct ring_buffer_event *event;
e77405ad 2077 struct ring_buffer *buffer;
769b0441 2078 struct trace_array *tr = &global_trace;
48ead020 2079 struct bprint_entry *entry;
769b0441 2080 unsigned long flags;
07d777fe
SR
2081 char *tbuffer;
2082 int len = 0, size, pc;
769b0441
FW
2083
2084 if (unlikely(tracing_selftest_running || tracing_disabled))
2085 return 0;
2086
2087 /* Don't pollute graph traces with trace_vprintk internals */
2088 pause_graph_tracing();
2089
2090 pc = preempt_count();
5168ae50 2091 preempt_disable_notrace();
769b0441 2092
07d777fe
SR
2093 tbuffer = get_trace_buf();
2094 if (!tbuffer) {
2095 len = 0;
769b0441 2096 goto out;
07d777fe 2097 }
769b0441 2098
07d777fe 2099 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
769b0441 2100
07d777fe
SR
2101 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
2102 goto out;
769b0441 2103
07d777fe 2104 local_save_flags(flags);
769b0441 2105 size = sizeof(*entry) + sizeof(u32) * len;
12883efb 2106 buffer = tr->trace_buffer.buffer;
e77405ad
SR
2107 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2108 flags, pc);
769b0441 2109 if (!event)
07d777fe 2110 goto out;
769b0441
FW
2111 entry = ring_buffer_event_data(event);
2112 entry->ip = ip;
769b0441
FW
2113 entry->fmt = fmt;
2114
07d777fe 2115 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
d931369b 2116 if (!filter_check_discard(call, entry, buffer, event)) {
7ffbd48d 2117 __buffer_unlock_commit(buffer, event);
d931369b
SR
2118 ftrace_trace_stack(buffer, flags, 6, pc);
2119 }
769b0441 2120
769b0441 2121out:
5168ae50 2122 preempt_enable_notrace();
769b0441
FW
2123 unpause_graph_tracing();
2124
2125 return len;
2126}
48ead020
FW
2127EXPORT_SYMBOL_GPL(trace_vbprintk);
2128
12883efb
SRRH
2129static int
2130__trace_array_vprintk(struct ring_buffer *buffer,
2131 unsigned long ip, const char *fmt, va_list args)
48ead020 2132{
e1112b4d 2133 struct ftrace_event_call *call = &event_print;
48ead020 2134 struct ring_buffer_event *event;
07d777fe 2135 int len = 0, size, pc;
48ead020 2136 struct print_entry *entry;
07d777fe
SR
2137 unsigned long flags;
2138 char *tbuffer;
48ead020
FW
2139
2140 if (tracing_disabled || tracing_selftest_running)
2141 return 0;
2142
07d777fe
SR
2143 /* Don't pollute graph traces with trace_vprintk internals */
2144 pause_graph_tracing();
2145
48ead020
FW
2146 pc = preempt_count();
2147 preempt_disable_notrace();
48ead020 2148
07d777fe
SR
2149
2150 tbuffer = get_trace_buf();
2151 if (!tbuffer) {
2152 len = 0;
48ead020 2153 goto out;
07d777fe 2154 }
48ead020 2155
07d777fe
SR
2156 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
2157 if (len > TRACE_BUF_SIZE)
2158 goto out;
48ead020 2159
07d777fe 2160 local_save_flags(flags);
48ead020 2161 size = sizeof(*entry) + len + 1;
e77405ad 2162 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
07d777fe 2163 flags, pc);
48ead020 2164 if (!event)
07d777fe 2165 goto out;
48ead020 2166 entry = ring_buffer_event_data(event);
c13d2f7c 2167 entry->ip = ip;
48ead020 2168
07d777fe 2169 memcpy(&entry->buf, tbuffer, len);
c13d2f7c 2170 entry->buf[len] = '\0';
d931369b 2171 if (!filter_check_discard(call, entry, buffer, event)) {
7ffbd48d 2172 __buffer_unlock_commit(buffer, event);
07d777fe 2173 ftrace_trace_stack(buffer, flags, 6, pc);
d931369b 2174 }
48ead020
FW
2175 out:
2176 preempt_enable_notrace();
07d777fe 2177 unpause_graph_tracing();
48ead020
FW
2178
2179 return len;
2180}
659372d3 2181
12883efb
SRRH
2182int trace_array_vprintk(struct trace_array *tr,
2183 unsigned long ip, const char *fmt, va_list args)
2184{
2185 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2186}
2187
2188int trace_array_printk(struct trace_array *tr,
2189 unsigned long ip, const char *fmt, ...)
2190{
2191 int ret;
2192 va_list ap;
2193
2194 if (!(trace_flags & TRACE_ITER_PRINTK))
2195 return 0;
2196
2197 va_start(ap, fmt);
2198 ret = trace_array_vprintk(tr, ip, fmt, ap);
2199 va_end(ap);
2200 return ret;
2201}
2202
2203int trace_array_printk_buf(struct ring_buffer *buffer,
2204 unsigned long ip, const char *fmt, ...)
2205{
2206 int ret;
2207 va_list ap;
2208
2209 if (!(trace_flags & TRACE_ITER_PRINTK))
2210 return 0;
2211
2212 va_start(ap, fmt);
2213 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2214 va_end(ap);
2215 return ret;
2216}
2217
659372d3
SR
2218int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2219{
a813a159 2220 return trace_array_vprintk(&global_trace, ip, fmt, args);
659372d3 2221}
769b0441
FW
2222EXPORT_SYMBOL_GPL(trace_vprintk);
2223
e2ac8ef5 2224static void trace_iterator_increment(struct trace_iterator *iter)
5a90f577 2225{
6d158a81
SR
2226 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2227
5a90f577 2228 iter->idx++;
6d158a81
SR
2229 if (buf_iter)
2230 ring_buffer_read(buf_iter, NULL);
5a90f577
SR
2231}
2232
e309b41d 2233static struct trace_entry *
bc21b478
SR
2234peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2235 unsigned long *lost_events)
dd0e545f 2236{
3928a8a2 2237 struct ring_buffer_event *event;
6d158a81 2238 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
dd0e545f 2239
d769041f
SR
2240 if (buf_iter)
2241 event = ring_buffer_iter_peek(buf_iter, ts);
2242 else
12883efb 2243 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
bc21b478 2244 lost_events);
d769041f 2245
4a9bd3f1
SR
2246 if (event) {
2247 iter->ent_size = ring_buffer_event_length(event);
2248 return ring_buffer_event_data(event);
2249 }
2250 iter->ent_size = 0;
2251 return NULL;
dd0e545f 2252}
d769041f 2253
dd0e545f 2254static struct trace_entry *
bc21b478
SR
2255__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2256 unsigned long *missing_events, u64 *ent_ts)
bc0c38d1 2257{
12883efb 2258 struct ring_buffer *buffer = iter->trace_buffer->buffer;
bc0c38d1 2259 struct trace_entry *ent, *next = NULL;
aa27497c 2260 unsigned long lost_events = 0, next_lost = 0;
b04cc6b1 2261 int cpu_file = iter->cpu_file;
3928a8a2 2262 u64 next_ts = 0, ts;
bc0c38d1 2263 int next_cpu = -1;
12b5da34 2264 int next_size = 0;
bc0c38d1
SR
2265 int cpu;
2266
b04cc6b1
FW
2267 /*
2268 * If we are in a per_cpu trace file, don't bother by iterating over
2269 * all cpu and peek directly.
2270 */
ae3b5093 2271 if (cpu_file > RING_BUFFER_ALL_CPUS) {
b04cc6b1
FW
2272 if (ring_buffer_empty_cpu(buffer, cpu_file))
2273 return NULL;
bc21b478 2274 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
b04cc6b1
FW
2275 if (ent_cpu)
2276 *ent_cpu = cpu_file;
2277
2278 return ent;
2279 }
2280
ab46428c 2281 for_each_tracing_cpu(cpu) {
dd0e545f 2282
3928a8a2
SR
2283 if (ring_buffer_empty_cpu(buffer, cpu))
2284 continue;
dd0e545f 2285
bc21b478 2286 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
dd0e545f 2287
cdd31cd2
IM
2288 /*
2289 * Pick the entry with the smallest timestamp:
2290 */
3928a8a2 2291 if (ent && (!next || ts < next_ts)) {
bc0c38d1
SR
2292 next = ent;
2293 next_cpu = cpu;
3928a8a2 2294 next_ts = ts;
bc21b478 2295 next_lost = lost_events;
12b5da34 2296 next_size = iter->ent_size;
bc0c38d1
SR
2297 }
2298 }
2299
12b5da34
SR
2300 iter->ent_size = next_size;
2301
bc0c38d1
SR
2302 if (ent_cpu)
2303 *ent_cpu = next_cpu;
2304
3928a8a2
SR
2305 if (ent_ts)
2306 *ent_ts = next_ts;
2307
bc21b478
SR
2308 if (missing_events)
2309 *missing_events = next_lost;
2310
bc0c38d1
SR
2311 return next;
2312}
2313
dd0e545f 2314/* Find the next real entry, without updating the iterator itself */
c4a8e8be
FW
2315struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2316 int *ent_cpu, u64 *ent_ts)
bc0c38d1 2317{
bc21b478 2318 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
dd0e545f
SR
2319}
2320
2321/* Find the next real entry, and increment the iterator to the next entry */
955b61e5 2322void *trace_find_next_entry_inc(struct trace_iterator *iter)
dd0e545f 2323{
bc21b478
SR
2324 iter->ent = __find_next_entry(iter, &iter->cpu,
2325 &iter->lost_events, &iter->ts);
dd0e545f 2326
3928a8a2 2327 if (iter->ent)
e2ac8ef5 2328 trace_iterator_increment(iter);
dd0e545f 2329
3928a8a2 2330 return iter->ent ? iter : NULL;
b3806b43 2331}
bc0c38d1 2332
e309b41d 2333static void trace_consume(struct trace_iterator *iter)
b3806b43 2334{
12883efb 2335 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
bc21b478 2336 &iter->lost_events);
bc0c38d1
SR
2337}
2338
e309b41d 2339static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
2340{
2341 struct trace_iterator *iter = m->private;
bc0c38d1 2342 int i = (int)*pos;
4e3c3333 2343 void *ent;
bc0c38d1 2344
a63ce5b3
SR
2345 WARN_ON_ONCE(iter->leftover);
2346
bc0c38d1
SR
2347 (*pos)++;
2348
2349 /* can't go backwards */
2350 if (iter->idx > i)
2351 return NULL;
2352
2353 if (iter->idx < 0)
955b61e5 2354 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
2355 else
2356 ent = iter;
2357
2358 while (ent && iter->idx < i)
955b61e5 2359 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
2360
2361 iter->pos = *pos;
2362
bc0c38d1
SR
2363 return ent;
2364}
2365
955b61e5 2366void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2f26ebd5 2367{
2f26ebd5
SR
2368 struct ring_buffer_event *event;
2369 struct ring_buffer_iter *buf_iter;
2370 unsigned long entries = 0;
2371 u64 ts;
2372
12883efb 2373 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2f26ebd5 2374
6d158a81
SR
2375 buf_iter = trace_buffer_iter(iter, cpu);
2376 if (!buf_iter)
2f26ebd5
SR
2377 return;
2378
2f26ebd5
SR
2379 ring_buffer_iter_reset(buf_iter);
2380
2381 /*
2382 * We could have the case with the max latency tracers
2383 * that a reset never took place on a cpu. This is evident
2384 * by the timestamp being before the start of the buffer.
2385 */
2386 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
12883efb 2387 if (ts >= iter->trace_buffer->time_start)
2f26ebd5
SR
2388 break;
2389 entries++;
2390 ring_buffer_read(buf_iter, NULL);
2391 }
2392
12883efb 2393 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2f26ebd5
SR
2394}
2395
d7350c3f 2396/*
d7350c3f
FW
2397 * The current tracer is copied to avoid a global locking
2398 * all around.
2399 */
bc0c38d1
SR
2400static void *s_start(struct seq_file *m, loff_t *pos)
2401{
2402 struct trace_iterator *iter = m->private;
2b6080f2 2403 struct trace_array *tr = iter->tr;
b04cc6b1 2404 int cpu_file = iter->cpu_file;
bc0c38d1
SR
2405 void *p = NULL;
2406 loff_t l = 0;
3928a8a2 2407 int cpu;
bc0c38d1 2408
2fd196ec
HT
2409 /*
2410 * copy the tracer to avoid using a global lock all around.
2411 * iter->trace is a copy of current_trace, the pointer to the
2412 * name may be used instead of a strcmp(), as iter->trace->name
2413 * will point to the same string as current_trace->name.
2414 */
bc0c38d1 2415 mutex_lock(&trace_types_lock);
2b6080f2
SR
2416 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2417 *iter->trace = *tr->current_trace;
d7350c3f 2418 mutex_unlock(&trace_types_lock);
bc0c38d1 2419
12883efb 2420#ifdef CONFIG_TRACER_MAX_TRACE
debdd57f
HT
2421 if (iter->snapshot && iter->trace->use_max_tr)
2422 return ERR_PTR(-EBUSY);
12883efb 2423#endif
debdd57f
HT
2424
2425 if (!iter->snapshot)
2426 atomic_inc(&trace_record_cmdline_disabled);
bc0c38d1 2427
bc0c38d1
SR
2428 if (*pos != iter->pos) {
2429 iter->ent = NULL;
2430 iter->cpu = 0;
2431 iter->idx = -1;
2432
ae3b5093 2433 if (cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 2434 for_each_tracing_cpu(cpu)
2f26ebd5 2435 tracing_iter_reset(iter, cpu);
b04cc6b1 2436 } else
2f26ebd5 2437 tracing_iter_reset(iter, cpu_file);
bc0c38d1 2438
ac91d854 2439 iter->leftover = 0;
bc0c38d1
SR
2440 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2441 ;
2442
2443 } else {
a63ce5b3
SR
2444 /*
2445 * If we overflowed the seq_file before, then we want
2446 * to just reuse the trace_seq buffer again.
2447 */
2448 if (iter->leftover)
2449 p = iter;
2450 else {
2451 l = *pos - 1;
2452 p = s_next(m, p, &l);
2453 }
bc0c38d1
SR
2454 }
2455
4f535968 2456 trace_event_read_lock();
7e53bd42 2457 trace_access_lock(cpu_file);
bc0c38d1
SR
2458 return p;
2459}
2460
2461static void s_stop(struct seq_file *m, void *p)
2462{
7e53bd42
LJ
2463 struct trace_iterator *iter = m->private;
2464
12883efb 2465#ifdef CONFIG_TRACER_MAX_TRACE
debdd57f
HT
2466 if (iter->snapshot && iter->trace->use_max_tr)
2467 return;
12883efb 2468#endif
debdd57f
HT
2469
2470 if (!iter->snapshot)
2471 atomic_dec(&trace_record_cmdline_disabled);
12883efb 2472
7e53bd42 2473 trace_access_unlock(iter->cpu_file);
4f535968 2474 trace_event_read_unlock();
bc0c38d1
SR
2475}
2476
39eaf7ef 2477static void
12883efb
SRRH
2478get_total_entries(struct trace_buffer *buf,
2479 unsigned long *total, unsigned long *entries)
39eaf7ef
SR
2480{
2481 unsigned long count;
2482 int cpu;
2483
2484 *total = 0;
2485 *entries = 0;
2486
2487 for_each_tracing_cpu(cpu) {
12883efb 2488 count = ring_buffer_entries_cpu(buf->buffer, cpu);
39eaf7ef
SR
2489 /*
2490 * If this buffer has skipped entries, then we hold all
2491 * entries for the trace and we need to ignore the
2492 * ones before the time stamp.
2493 */
12883efb
SRRH
2494 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2495 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
39eaf7ef
SR
2496 /* total is the same as the entries */
2497 *total += count;
2498 } else
2499 *total += count +
12883efb 2500 ring_buffer_overrun_cpu(buf->buffer, cpu);
39eaf7ef
SR
2501 *entries += count;
2502 }
2503}
2504
e309b41d 2505static void print_lat_help_header(struct seq_file *m)
bc0c38d1 2506{
a6168353
ME
2507 seq_puts(m, "# _------=> CPU# \n");
2508 seq_puts(m, "# / _-----=> irqs-off \n");
2509 seq_puts(m, "# | / _----=> need-resched \n");
2510 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2511 seq_puts(m, "# ||| / _--=> preempt-depth \n");
e6e1e259
SR
2512 seq_puts(m, "# |||| / delay \n");
2513 seq_puts(m, "# cmd pid ||||| time | caller \n");
2514 seq_puts(m, "# \\ / ||||| \\ | / \n");
bc0c38d1
SR
2515}
2516
12883efb 2517static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
bc0c38d1 2518{
39eaf7ef
SR
2519 unsigned long total;
2520 unsigned long entries;
2521
12883efb 2522 get_total_entries(buf, &total, &entries);
39eaf7ef
SR
2523 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2524 entries, total, num_online_cpus());
6fa3eb70
S
2525#ifdef CONFIG_MTK_SCHED_TRACERS
2526 print_enabled_events(m);
2527#endif
39eaf7ef
SR
2528 seq_puts(m, "#\n");
2529}
2530
12883efb 2531static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
39eaf7ef 2532{
12883efb 2533 print_event_info(buf, m);
77271ce4 2534 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
a6168353 2535 seq_puts(m, "# | | | | |\n");
bc0c38d1
SR
2536}
2537
6fa3eb70
S
2538static void print_func_help_header_tgid(struct trace_buffer *buf, struct seq_file *m)
2539{
2540 print_event_info(buf, m);
2541 seq_puts(m, "# TASK-PID TGID CPU# TIMESTAMP FUNCTION\n");
2542 seq_puts(m, "# | | | | | |\n");
2543}
2544
12883efb 2545static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
77271ce4 2546{
12883efb 2547 print_event_info(buf, m);
77271ce4
SR
2548 seq_puts(m, "# _-----=> irqs-off\n");
2549 seq_puts(m, "# / _----=> need-resched\n");
2550 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2551 seq_puts(m, "# || / _--=> preempt-depth\n");
2552 seq_puts(m, "# ||| / delay\n");
2553 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2554 seq_puts(m, "# | | | |||| | |\n");
2555}
bc0c38d1 2556
6fa3eb70
S
2557static void print_func_help_header_irq_tgid(struct trace_buffer *buf, struct seq_file *m)
2558{
2559 print_event_info(buf, m);
2560 seq_puts(m, "# _-----=> irqs-off\n");
2561 seq_puts(m, "# / _----=> need-resched\n");
2562 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2563 seq_puts(m, "# || / _--=> preempt-depth\n");
2564 seq_puts(m, "# ||| / delay\n");
2565 seq_puts(m, "# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION\n");
2566 seq_puts(m, "# | | | | |||| | |\n");
2567}
2568
62b915f1 2569void
bc0c38d1
SR
2570print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2571{
2572 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
12883efb
SRRH
2573 struct trace_buffer *buf = iter->trace_buffer;
2574 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2b6080f2 2575 struct tracer *type = iter->trace;
39eaf7ef
SR
2576 unsigned long entries;
2577 unsigned long total;
bc0c38d1
SR
2578 const char *name = "preemption";
2579
d840f718 2580 name = type->name;
bc0c38d1 2581
12883efb 2582 get_total_entries(buf, &total, &entries);
bc0c38d1 2583
888b55dc 2584 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
bc0c38d1 2585 name, UTS_RELEASE);
888b55dc 2586 seq_puts(m, "# -----------------------------------"
bc0c38d1 2587 "---------------------------------\n");
888b55dc 2588 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
bc0c38d1 2589 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 2590 nsecs_to_usecs(data->saved_latency),
bc0c38d1 2591 entries,
4c11d7ae 2592 total,
12883efb 2593 buf->cpu,
bc0c38d1
SR
2594#if defined(CONFIG_PREEMPT_NONE)
2595 "server",
2596#elif defined(CONFIG_PREEMPT_VOLUNTARY)
2597 "desktop",
b5c21b45 2598#elif defined(CONFIG_PREEMPT)
bc0c38d1
SR
2599 "preempt",
2600#else
2601 "unknown",
2602#endif
2603 /* These are reserved for later use */
2604 0, 0, 0, 0);
2605#ifdef CONFIG_SMP
2606 seq_printf(m, " #P:%d)\n", num_online_cpus());
2607#else
2608 seq_puts(m, ")\n");
2609#endif
888b55dc
KM
2610 seq_puts(m, "# -----------------\n");
2611 seq_printf(m, "# | task: %.16s-%d "
bc0c38d1 2612 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
d20b92ab
EB
2613 data->comm, data->pid,
2614 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
bc0c38d1 2615 data->policy, data->rt_priority);
888b55dc 2616 seq_puts(m, "# -----------------\n");
bc0c38d1
SR
2617
2618 if (data->critical_start) {
888b55dc 2619 seq_puts(m, "# => started at: ");
214023c3
SR
2620 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2621 trace_print_seq(m, &iter->seq);
888b55dc 2622 seq_puts(m, "\n# => ended at: ");
214023c3
SR
2623 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2624 trace_print_seq(m, &iter->seq);
8248ac05 2625 seq_puts(m, "\n#\n");
bc0c38d1
SR
2626 }
2627
888b55dc 2628 seq_puts(m, "#\n");
bc0c38d1
SR
2629}
2630
a309720c
SR
2631static void test_cpu_buff_start(struct trace_iterator *iter)
2632{
2633 struct trace_seq *s = &iter->seq;
2634
12ef7d44
SR
2635 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2636 return;
2637
2638 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2639 return;
2640
4462344e 2641 if (cpumask_test_cpu(iter->cpu, iter->started))
a309720c
SR
2642 return;
2643
12883efb 2644 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2f26ebd5
SR
2645 return;
2646
4462344e 2647 cpumask_set_cpu(iter->cpu, iter->started);
b0dfa978
FW
2648
2649 /* Don't print started cpu buffer for the first entry of the trace */
2650 if (iter->idx > 1)
2651 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2652 iter->cpu);
a309720c
SR
2653}
2654
2c4f035f 2655static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 2656{
214023c3 2657 struct trace_seq *s = &iter->seq;
bc0c38d1 2658 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 2659 struct trace_entry *entry;
f633cef0 2660 struct trace_event *event;
bc0c38d1 2661
4e3c3333 2662 entry = iter->ent;
dd0e545f 2663
a309720c
SR
2664 test_cpu_buff_start(iter);
2665
c4a8e8be 2666 event = ftrace_find_event(entry->type);
bc0c38d1 2667
c4a8e8be 2668 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
27d48be8
SR
2669 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2670 if (!trace_print_lat_context(iter))
2671 goto partial;
2672 } else {
2673 if (!trace_print_context(iter))
2674 goto partial;
2675 }
c4a8e8be 2676 }
bc0c38d1 2677
268ccda0 2678 if (event)
a9a57763 2679 return event->funcs->trace(iter, sym_flags, event);
d9793bd8
ACM
2680
2681 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2682 goto partial;
02b67518 2683
2c4f035f 2684 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
2685partial:
2686 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1
SR
2687}
2688
2c4f035f 2689static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3
IM
2690{
2691 struct trace_seq *s = &iter->seq;
2692 struct trace_entry *entry;
f633cef0 2693 struct trace_event *event;
f9896bf3
IM
2694
2695 entry = iter->ent;
dd0e545f 2696
c4a8e8be 2697 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
d9793bd8
ACM
2698 if (!trace_seq_printf(s, "%d %d %llu ",
2699 entry->pid, iter->cpu, iter->ts))
2700 goto partial;
c4a8e8be 2701 }
f9896bf3 2702
f633cef0 2703 event = ftrace_find_event(entry->type);
268ccda0 2704 if (event)
a9a57763 2705 return event->funcs->raw(iter, 0, event);
d9793bd8
ACM
2706
2707 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2708 goto partial;
777e208d 2709
2c4f035f 2710 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
2711partial:
2712 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3
IM
2713}
2714
2c4f035f 2715static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec
IM
2716{
2717 struct trace_seq *s = &iter->seq;
2718 unsigned char newline = '\n';
2719 struct trace_entry *entry;
f633cef0 2720 struct trace_event *event;
5e3ca0ec
IM
2721
2722 entry = iter->ent;
dd0e545f 2723
c4a8e8be
FW
2724 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2725 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2726 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2727 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2728 }
5e3ca0ec 2729
f633cef0 2730 event = ftrace_find_event(entry->type);
268ccda0 2731 if (event) {
a9a57763 2732 enum print_line_t ret = event->funcs->hex(iter, 0, event);
d9793bd8
ACM
2733 if (ret != TRACE_TYPE_HANDLED)
2734 return ret;
2735 }
7104f300 2736
5e3ca0ec
IM
2737 SEQ_PUT_FIELD_RET(s, newline);
2738
2c4f035f 2739 return TRACE_TYPE_HANDLED;
5e3ca0ec
IM
2740}
2741
2c4f035f 2742static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa
IM
2743{
2744 struct trace_seq *s = &iter->seq;
2745 struct trace_entry *entry;
f633cef0 2746 struct trace_event *event;
cb0f12aa
IM
2747
2748 entry = iter->ent;
dd0e545f 2749
c4a8e8be
FW
2750 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2751 SEQ_PUT_FIELD_RET(s, entry->pid);
1830b52d 2752 SEQ_PUT_FIELD_RET(s, iter->cpu);
c4a8e8be
FW
2753 SEQ_PUT_FIELD_RET(s, iter->ts);
2754 }
cb0f12aa 2755
f633cef0 2756 event = ftrace_find_event(entry->type);
a9a57763
SR
2757 return event ? event->funcs->binary(iter, 0, event) :
2758 TRACE_TYPE_HANDLED;
cb0f12aa
IM
2759}
2760
62b915f1 2761int trace_empty(struct trace_iterator *iter)
bc0c38d1 2762{
6d158a81 2763 struct ring_buffer_iter *buf_iter;
bc0c38d1
SR
2764 int cpu;
2765
9aba60fe 2766 /* If we are looking at one CPU buffer, only check that one */
ae3b5093 2767 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
9aba60fe 2768 cpu = iter->cpu_file;
6d158a81
SR
2769 buf_iter = trace_buffer_iter(iter, cpu);
2770 if (buf_iter) {
2771 if (!ring_buffer_iter_empty(buf_iter))
9aba60fe
SR
2772 return 0;
2773 } else {
12883efb 2774 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
9aba60fe
SR
2775 return 0;
2776 }
2777 return 1;
2778 }
2779
ab46428c 2780 for_each_tracing_cpu(cpu) {
6d158a81
SR
2781 buf_iter = trace_buffer_iter(iter, cpu);
2782 if (buf_iter) {
2783 if (!ring_buffer_iter_empty(buf_iter))
d769041f
SR
2784 return 0;
2785 } else {
12883efb 2786 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
d769041f
SR
2787 return 0;
2788 }
bc0c38d1 2789 }
d769041f 2790
797d3712 2791 return 1;
bc0c38d1
SR
2792}
2793
4f535968 2794/* Called with trace_event_read_lock() held. */
955b61e5 2795enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 2796{
2c4f035f
FW
2797 enum print_line_t ret;
2798
ee5e51f5
JO
2799 if (iter->lost_events &&
2800 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2801 iter->cpu, iter->lost_events))
2802 return TRACE_TYPE_PARTIAL_LINE;
bc21b478 2803
2c4f035f
FW
2804 if (iter->trace && iter->trace->print_line) {
2805 ret = iter->trace->print_line(iter);
2806 if (ret != TRACE_TYPE_UNHANDLED)
2807 return ret;
2808 }
72829bc3 2809
09ae7234
SRRH
2810 if (iter->ent->type == TRACE_BPUTS &&
2811 trace_flags & TRACE_ITER_PRINTK &&
2812 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2813 return trace_print_bputs_msg_only(iter);
2814
48ead020
FW
2815 if (iter->ent->type == TRACE_BPRINT &&
2816 trace_flags & TRACE_ITER_PRINTK &&
2817 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2818 return trace_print_bprintk_msg_only(iter);
48ead020 2819
66896a85
FW
2820 if (iter->ent->type == TRACE_PRINT &&
2821 trace_flags & TRACE_ITER_PRINTK &&
2822 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2823 return trace_print_printk_msg_only(iter);
66896a85 2824
cb0f12aa
IM
2825 if (trace_flags & TRACE_ITER_BIN)
2826 return print_bin_fmt(iter);
2827
5e3ca0ec
IM
2828 if (trace_flags & TRACE_ITER_HEX)
2829 return print_hex_fmt(iter);
2830
f9896bf3
IM
2831 if (trace_flags & TRACE_ITER_RAW)
2832 return print_raw_fmt(iter);
2833
f9896bf3
IM
2834 return print_trace_fmt(iter);
2835}
2836
7e9a49ef
JO
2837void trace_latency_header(struct seq_file *m)
2838{
2839 struct trace_iterator *iter = m->private;
2840
2841 /* print nothing if the buffers are empty */
2842 if (trace_empty(iter))
2843 return;
2844
2845 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2846 print_trace_header(m, iter);
2847
2848 if (!(trace_flags & TRACE_ITER_VERBOSE))
2849 print_lat_help_header(m);
2850}
2851
62b915f1
JO
2852void trace_default_header(struct seq_file *m)
2853{
2854 struct trace_iterator *iter = m->private;
2855
f56e7f8e
JO
2856 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2857 return;
2858
62b915f1
JO
2859 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2860 /* print nothing if the buffers are empty */
2861 if (trace_empty(iter))
2862 return;
2863 print_trace_header(m, iter);
2864 if (!(trace_flags & TRACE_ITER_VERBOSE))
2865 print_lat_help_header(m);
2866 } else {
77271ce4
SR
2867 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2868 if (trace_flags & TRACE_ITER_IRQ_INFO)
6fa3eb70
S
2869 if (trace_flags & TRACE_ITER_TGID)
2870 print_func_help_header_irq_tgid(iter->trace_buffer, m);
2871 else
2872 print_func_help_header_irq(iter->trace_buffer, m);
77271ce4 2873 else
6fa3eb70
S
2874 if (trace_flags & TRACE_ITER_TGID)
2875 print_func_help_header_tgid(iter->trace_buffer, m);
2876 else
2877 print_func_help_header(iter->trace_buffer, m);
77271ce4 2878 }
62b915f1
JO
2879 }
2880}
2881
e0a413f6
SR
2882static void test_ftrace_alive(struct seq_file *m)
2883{
2884 if (!ftrace_is_dead())
2885 return;
2886 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2887 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2888}
2889
d8741e2e 2890#ifdef CONFIG_TRACER_MAX_TRACE
f1affcaa 2891static void show_snapshot_main_help(struct seq_file *m)
d8741e2e 2892{
d8741e2e
SRRH
2893 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2894 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2895 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2896 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2897 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2898 seq_printf(m, "# is not a '0' or '1')\n");
2899}
f1affcaa
SRRH
2900
2901static void show_snapshot_percpu_help(struct seq_file *m)
2902{
2903 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2904#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2905 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2906 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2907#else
2908 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2909 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2910#endif
2911 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2912 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2913 seq_printf(m, "# is not a '0' or '1')\n");
2914}
2915
d8741e2e
SRRH
2916static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2917{
45ad21ca 2918 if (iter->tr->allocated_snapshot)
d8741e2e
SRRH
2919 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2920 else
2921 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2922
2923 seq_printf(m, "# Snapshot commands:\n");
f1affcaa
SRRH
2924 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2925 show_snapshot_main_help(m);
2926 else
2927 show_snapshot_percpu_help(m);
d8741e2e
SRRH
2928}
2929#else
2930/* Should never be called */
2931static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2932#endif
2933
bc0c38d1
SR
2934static int s_show(struct seq_file *m, void *v)
2935{
2936 struct trace_iterator *iter = v;
a63ce5b3 2937 int ret;
bc0c38d1
SR
2938
2939 if (iter->ent == NULL) {
2940 if (iter->tr) {
2941 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2942 seq_puts(m, "#\n");
e0a413f6 2943 test_ftrace_alive(m);
bc0c38d1 2944 }
d8741e2e
SRRH
2945 if (iter->snapshot && trace_empty(iter))
2946 print_snapshot_help(m, iter);
2947 else if (iter->trace && iter->trace->print_header)
8bba1bf5 2948 iter->trace->print_header(m);
62b915f1
JO
2949 else
2950 trace_default_header(m);
2951
a63ce5b3
SR
2952 } else if (iter->leftover) {
2953 /*
2954 * If we filled the seq_file buffer earlier, we
2955 * want to just show it now.
2956 */
2957 ret = trace_print_seq(m, &iter->seq);
2958
2959 /* ret should this time be zero, but you never know */
2960 iter->leftover = ret;
2961
bc0c38d1 2962 } else {
f9896bf3 2963 print_trace_line(iter);
a63ce5b3
SR
2964 ret = trace_print_seq(m, &iter->seq);
2965 /*
2966 * If we overflow the seq_file buffer, then it will
2967 * ask us for this data again at start up.
2968 * Use that instead.
2969 * ret is 0 if seq_file write succeeded.
2970 * -1 otherwise.
2971 */
2972 iter->leftover = ret;
bc0c38d1
SR
2973 }
2974
2975 return 0;
2976}
2977
e2d4dbe0
ON
2978/*
2979 * Should be used after trace_array_get(), trace_types_lock
2980 * ensures that i_cdev was already initialized.
2981 */
2982static inline int tracing_get_cpu(struct inode *inode)
2983{
2984 if (inode->i_cdev) /* See trace_create_cpu_file() */
2985 return (long)inode->i_cdev - 1;
2986 return RING_BUFFER_ALL_CPUS;
2987}
2988
88e9d34c 2989static const struct seq_operations tracer_seq_ops = {
4bf39a94
IM
2990 .start = s_start,
2991 .next = s_next,
2992 .stop = s_stop,
2993 .show = s_show,
bc0c38d1
SR
2994};
2995
e309b41d 2996static struct trace_iterator *
7272711a 2997__tracing_open(struct inode *inode, struct file *file, bool snapshot)
bc0c38d1 2998{
7272711a 2999 struct trace_array *tr = inode->i_private;
bc0c38d1 3000 struct trace_iterator *iter;
50e18b94 3001 int cpu;
bc0c38d1 3002
85a2f9b4
SR
3003 if (tracing_disabled)
3004 return ERR_PTR(-ENODEV);
60a11774 3005
50e18b94 3006 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
85a2f9b4
SR
3007 if (!iter)
3008 return ERR_PTR(-ENOMEM);
bc0c38d1 3009
6d158a81
SR
3010 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
3011 GFP_KERNEL);
93574fcc
DC
3012 if (!iter->buffer_iter)
3013 goto release;
3014
d7350c3f
FW
3015 /*
3016 * We make a copy of the current tracer to avoid concurrent
3017 * changes on it while we are reading.
3018 */
bc0c38d1 3019 mutex_lock(&trace_types_lock);
d7350c3f 3020 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
85a2f9b4 3021 if (!iter->trace)
d7350c3f 3022 goto fail;
85a2f9b4 3023
2b6080f2 3024 *iter->trace = *tr->current_trace;
d7350c3f 3025
79f55997 3026 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
b0dfa978
FW
3027 goto fail;
3028
12883efb
SRRH
3029 iter->tr = tr;
3030
3031#ifdef CONFIG_TRACER_MAX_TRACE
2b6080f2
SR
3032 /* Currently only the top directory has a snapshot */
3033 if (tr->current_trace->print_max || snapshot)
12883efb 3034 iter->trace_buffer = &tr->max_buffer;
bc0c38d1 3035 else
12883efb
SRRH
3036#endif
3037 iter->trace_buffer = &tr->trace_buffer;
debdd57f 3038 iter->snapshot = snapshot;
bc0c38d1 3039 iter->pos = -1;
7272711a 3040 iter->cpu_file = tracing_get_cpu(inode);
d7350c3f 3041 mutex_init(&iter->mutex);
bc0c38d1 3042
8bba1bf5
MM
3043 /* Notify the tracer early; before we stop tracing. */
3044 if (iter->trace && iter->trace->open)
a93751ca 3045 iter->trace->open(iter);
8bba1bf5 3046
12ef7d44 3047 /* Annotate start of buffers if we had overruns */
12883efb 3048 if (ring_buffer_overruns(iter->trace_buffer->buffer))
12ef7d44
SR
3049 iter->iter_flags |= TRACE_FILE_ANNOTATE;
3050
8be0709f 3051 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
58e8eedf 3052 if (trace_clocks[tr->clock_id].in_ns)
8be0709f
DS
3053 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3054
debdd57f
HT
3055 /* stop the trace while dumping if we are not opening "snapshot" */
3056 if (!iter->snapshot)
2b6080f2 3057 tracing_stop_tr(tr);
2f26ebd5 3058
ae3b5093 3059 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 3060 for_each_tracing_cpu(cpu) {
b04cc6b1 3061 iter->buffer_iter[cpu] =
12883efb 3062 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
72c9ddfd
DM
3063 }
3064 ring_buffer_read_prepare_sync();
3065 for_each_tracing_cpu(cpu) {
3066 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 3067 tracing_iter_reset(iter, cpu);
b04cc6b1
FW
3068 }
3069 } else {
3070 cpu = iter->cpu_file;
3928a8a2 3071 iter->buffer_iter[cpu] =
12883efb 3072 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
72c9ddfd
DM
3073 ring_buffer_read_prepare_sync();
3074 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 3075 tracing_iter_reset(iter, cpu);
3928a8a2
SR
3076 }
3077
bc0c38d1
SR
3078 mutex_unlock(&trace_types_lock);
3079
bc0c38d1 3080 return iter;
3928a8a2 3081
d7350c3f 3082 fail:
3928a8a2 3083 mutex_unlock(&trace_types_lock);
d7350c3f 3084 kfree(iter->trace);
6d158a81 3085 kfree(iter->buffer_iter);
93574fcc 3086release:
50e18b94
JO
3087 seq_release_private(inode, file);
3088 return ERR_PTR(-ENOMEM);
bc0c38d1
SR
3089}
3090
3091int tracing_open_generic(struct inode *inode, struct file *filp)
3092{
60a11774
SR
3093 if (tracing_disabled)
3094 return -ENODEV;
3095
bc0c38d1
SR
3096 filp->private_data = inode->i_private;
3097 return 0;
3098}
3099
6492334c
SRRH
3100/*
3101 * Open and update trace_array ref count.
3102 * Must have the current trace_array passed to it.
3103 */
3104int tracing_open_generic_tr(struct inode *inode, struct file *filp)
3105{
3106 struct trace_array *tr = inode->i_private;
3107
3108 if (tracing_disabled)
3109 return -ENODEV;
3110
3111 if (trace_array_get(tr) < 0)
3112 return -ENODEV;
3113
3114 filp->private_data = inode->i_private;
3115
3116 return 0;
6492334c
SRRH
3117}
3118
4fd27358 3119static int tracing_release(struct inode *inode, struct file *file)
bc0c38d1 3120{
7272711a 3121 struct trace_array *tr = inode->i_private;
907f2784 3122 struct seq_file *m = file->private_data;
4acd4d00 3123 struct trace_iterator *iter;
3928a8a2 3124 int cpu;
bc0c38d1 3125
59d8f488 3126 if (!(file->f_mode & FMODE_READ)) {
7272711a 3127 trace_array_put(tr);
4acd4d00 3128 return 0;
59d8f488 3129 }
4acd4d00 3130
7272711a 3131 /* Writes do not use seq_file */
4acd4d00 3132 iter = m->private;
bc0c38d1 3133 mutex_lock(&trace_types_lock);
a695cb58 3134
3928a8a2
SR
3135 for_each_tracing_cpu(cpu) {
3136 if (iter->buffer_iter[cpu])
3137 ring_buffer_read_finish(iter->buffer_iter[cpu]);
3138 }
3139
bc0c38d1
SR
3140 if (iter->trace && iter->trace->close)
3141 iter->trace->close(iter);
3142
6fa3eb70 3143 printk(KERN_INFO "[ftrace]end reading trace file\n");
debdd57f
HT
3144 if (!iter->snapshot)
3145 /* reenable tracing if it was previously enabled */
2b6080f2 3146 tracing_start_tr(tr);
e45ccd09
AL
3147
3148 __trace_array_put(tr);
3149
bc0c38d1
SR
3150 mutex_unlock(&trace_types_lock);
3151
d7350c3f 3152 mutex_destroy(&iter->mutex);
b0dfa978 3153 free_cpumask_var(iter->started);
d7350c3f 3154 kfree(iter->trace);
6d158a81 3155 kfree(iter->buffer_iter);
50e18b94 3156 seq_release_private(inode, file);
59d8f488 3157
bc0c38d1
SR
3158 return 0;
3159}
3160
6492334c
SRRH
3161static int tracing_release_generic_tr(struct inode *inode, struct file *file)
3162{
3163 struct trace_array *tr = inode->i_private;
3164
3165 trace_array_put(tr);
3166 return 0;
3167}
3168
6492334c
SRRH
3169static int tracing_single_release_tr(struct inode *inode, struct file *file)
3170{
3171 struct trace_array *tr = inode->i_private;
3172
3173 trace_array_put(tr);
3174
3175 return single_release(inode, file);
3176}
3177
bc0c38d1
SR
3178static int tracing_open(struct inode *inode, struct file *file)
3179{
7272711a 3180 struct trace_array *tr = inode->i_private;
85a2f9b4
SR
3181 struct trace_iterator *iter;
3182 int ret = 0;
bc0c38d1 3183
59d8f488
SRRH
3184 if (trace_array_get(tr) < 0)
3185 return -ENODEV;
3186
4acd4d00 3187 /* If this file was open for write, then erase contents */
7272711a
ON
3188 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
3189 int cpu = tracing_get_cpu(inode);
7210062e
BY
3190 struct trace_buffer *trace_buf = &tr->trace_buffer;
3191
3192#ifdef CONFIG_TRACER_MAX_TRACE
3193 if (tr->current_trace->print_max)
3194 trace_buf = &tr->max_buffer;
3195#endif
7272711a
ON
3196
3197 if (cpu == RING_BUFFER_ALL_CPUS)
7210062e 3198 tracing_reset_online_cpus(trace_buf);
4acd4d00 3199 else
7210062e 3200 tracing_reset(trace_buf, cpu);
4acd4d00 3201 }
bc0c38d1 3202
4acd4d00 3203 if (file->f_mode & FMODE_READ) {
6fa3eb70 3204 printk(KERN_INFO "[ftrace]start reading trace file\n");
7272711a 3205 iter = __tracing_open(inode, file, false);
4acd4d00
SR
3206 if (IS_ERR(iter))
3207 ret = PTR_ERR(iter);
3208 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
3209 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3210 }
59d8f488
SRRH
3211
3212 if (ret < 0)
3213 trace_array_put(tr);
3214
bc0c38d1
SR
3215 return ret;
3216}
3217
e309b41d 3218static void *
bc0c38d1
SR
3219t_next(struct seq_file *m, void *v, loff_t *pos)
3220{
f129e965 3221 struct tracer *t = v;
bc0c38d1
SR
3222
3223 (*pos)++;
3224
3225 if (t)
3226 t = t->next;
3227
bc0c38d1
SR
3228 return t;
3229}
3230
3231static void *t_start(struct seq_file *m, loff_t *pos)
3232{
f129e965 3233 struct tracer *t;
bc0c38d1
SR
3234 loff_t l = 0;
3235
3236 mutex_lock(&trace_types_lock);
f129e965 3237 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
bc0c38d1
SR
3238 ;
3239
3240 return t;
3241}
3242
3243static void t_stop(struct seq_file *m, void *p)
3244{
3245 mutex_unlock(&trace_types_lock);
3246}
3247
3248static int t_show(struct seq_file *m, void *v)
3249{
3250 struct tracer *t = v;
3251
3252 if (!t)
3253 return 0;
3254
3255 seq_printf(m, "%s", t->name);
3256 if (t->next)
3257 seq_putc(m, ' ');
3258 else
3259 seq_putc(m, '\n');
3260
3261 return 0;
3262}
3263
88e9d34c 3264static const struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
3265 .start = t_start,
3266 .next = t_next,
3267 .stop = t_stop,
3268 .show = t_show,
bc0c38d1
SR
3269};
3270
3271static int show_traces_open(struct inode *inode, struct file *file)
3272{
60a11774
SR
3273 if (tracing_disabled)
3274 return -ENODEV;
3275
f129e965 3276 return seq_open(file, &show_traces_seq_ops);
bc0c38d1
SR
3277}
3278
4acd4d00
SR
3279static ssize_t
3280tracing_write_stub(struct file *filp, const char __user *ubuf,
3281 size_t count, loff_t *ppos)
3282{
3283 return count;
3284}
3285
364829b1
SP
3286static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
3287{
3288 if (file->f_mode & FMODE_READ)
3289 return seq_lseek(file, offset, origin);
3290 else
3291 return 0;
3292}
3293
5e2336a0 3294static const struct file_operations tracing_fops = {
4bf39a94
IM
3295 .open = tracing_open,
3296 .read = seq_read,
4acd4d00 3297 .write = tracing_write_stub,
364829b1 3298 .llseek = tracing_seek,
4bf39a94 3299 .release = tracing_release,
bc0c38d1
SR
3300};
3301
5e2336a0 3302static const struct file_operations show_traces_fops = {
c7078de1
IM
3303 .open = show_traces_open,
3304 .read = seq_read,
3305 .release = seq_release,
b444786f 3306 .llseek = seq_lseek,
c7078de1
IM
3307};
3308
36dfe925
IM
3309/*
3310 * Only trace on a CPU if the bitmask is set:
3311 */
9e01c1b7 3312static cpumask_var_t tracing_cpumask;
36dfe925
IM
3313
3314/*
3315 * The tracer itself will not take this lock, but still we want
3316 * to provide a consistent cpumask to user-space:
3317 */
3318static DEFINE_MUTEX(tracing_cpumask_update_lock);
3319
3320/*
3321 * Temporary storage for the character representation of the
3322 * CPU bitmask (and one more byte for the newline):
3323 */
3324static char mask_str[NR_CPUS + 1];
3325
c7078de1
IM
3326static ssize_t
3327tracing_cpumask_read(struct file *filp, char __user *ubuf,
3328 size_t count, loff_t *ppos)
3329{
36dfe925 3330 int len;
c7078de1
IM
3331
3332 mutex_lock(&tracing_cpumask_update_lock);
36dfe925 3333
9e01c1b7 3334 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
36dfe925
IM
3335 if (count - len < 2) {
3336 count = -EINVAL;
3337 goto out_err;
3338 }
3339 len += sprintf(mask_str + len, "\n");
3340 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3341
3342out_err:
c7078de1
IM
3343 mutex_unlock(&tracing_cpumask_update_lock);
3344
3345 return count;
3346}
3347
3348static ssize_t
3349tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3350 size_t count, loff_t *ppos)
3351{
2b6080f2 3352 struct trace_array *tr = filp->private_data;
9e01c1b7 3353 cpumask_var_t tracing_cpumask_new;
2b6080f2 3354 int err, cpu;
9e01c1b7
RR
3355
3356 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3357 return -ENOMEM;
c7078de1 3358
9e01c1b7 3359 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 3360 if (err)
36dfe925
IM
3361 goto err_unlock;
3362
215368e8
LZ
3363 mutex_lock(&tracing_cpumask_update_lock);
3364
a5e25883 3365 local_irq_disable();
0199c4e6 3366 arch_spin_lock(&ftrace_max_lock);
ab46428c 3367 for_each_tracing_cpu(cpu) {
36dfe925
IM
3368 /*
3369 * Increase/decrease the disabled counter if we are
3370 * about to flip a bit in the cpumask:
3371 */
9e01c1b7
RR
3372 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
3373 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
12883efb
SRRH
3374 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3375 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
36dfe925 3376 }
9e01c1b7
RR
3377 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
3378 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
12883efb
SRRH
3379 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3380 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
36dfe925
IM
3381 }
3382 }
0199c4e6 3383 arch_spin_unlock(&ftrace_max_lock);
a5e25883 3384 local_irq_enable();
36dfe925 3385
9e01c1b7 3386 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
36dfe925
IM
3387
3388 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 3389 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
3390
3391 return count;
36dfe925
IM
3392
3393err_unlock:
215368e8 3394 free_cpumask_var(tracing_cpumask_new);
36dfe925
IM
3395
3396 return err;
c7078de1
IM
3397}
3398
5e2336a0 3399static const struct file_operations tracing_cpumask_fops = {
c7078de1
IM
3400 .open = tracing_open_generic,
3401 .read = tracing_cpumask_read,
3402 .write = tracing_cpumask_write,
b444786f 3403 .llseek = generic_file_llseek,
bc0c38d1
SR
3404};
3405
fdb372ed 3406static int tracing_trace_options_show(struct seq_file *m, void *v)
bc0c38d1 3407{
d8e83d26 3408 struct tracer_opt *trace_opts;
2b6080f2 3409 struct trace_array *tr = m->private;
d8e83d26 3410 u32 tracer_flags;
d8e83d26 3411 int i;
adf9f195 3412
d8e83d26 3413 mutex_lock(&trace_types_lock);
2b6080f2
SR
3414 tracer_flags = tr->current_trace->flags->val;
3415 trace_opts = tr->current_trace->flags->opts;
d8e83d26 3416
bc0c38d1
SR
3417 for (i = 0; trace_options[i]; i++) {
3418 if (trace_flags & (1 << i))
fdb372ed 3419 seq_printf(m, "%s\n", trace_options[i]);
bc0c38d1 3420 else
fdb372ed 3421 seq_printf(m, "no%s\n", trace_options[i]);
bc0c38d1
SR
3422 }
3423
adf9f195
FW
3424 for (i = 0; trace_opts[i].name; i++) {
3425 if (tracer_flags & trace_opts[i].bit)
fdb372ed 3426 seq_printf(m, "%s\n", trace_opts[i].name);
adf9f195 3427 else
fdb372ed 3428 seq_printf(m, "no%s\n", trace_opts[i].name);
adf9f195 3429 }
d8e83d26 3430 mutex_unlock(&trace_types_lock);
adf9f195 3431
fdb372ed 3432 return 0;
bc0c38d1 3433}
bc0c38d1 3434
8d18eaaf
LZ
3435static int __set_tracer_option(struct tracer *trace,
3436 struct tracer_flags *tracer_flags,
3437 struct tracer_opt *opts, int neg)
3438{
3439 int ret;
bc0c38d1 3440
8d18eaaf
LZ
3441 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3442 if (ret)
3443 return ret;
3444
3445 if (neg)
3446 tracer_flags->val &= ~opts->bit;
3447 else
3448 tracer_flags->val |= opts->bit;
3449 return 0;
bc0c38d1
SR
3450}
3451
adf9f195
FW
3452/* Try to assign a tracer specific option */
3453static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3454{
7770841e 3455 struct tracer_flags *tracer_flags = trace->flags;
adf9f195 3456 struct tracer_opt *opts = NULL;
8d18eaaf 3457 int i;
adf9f195 3458
7770841e
Z
3459 for (i = 0; tracer_flags->opts[i].name; i++) {
3460 opts = &tracer_flags->opts[i];
adf9f195 3461
8d18eaaf
LZ
3462 if (strcmp(cmp, opts->name) == 0)
3463 return __set_tracer_option(trace, trace->flags,
3464 opts, neg);
adf9f195 3465 }
adf9f195 3466
8d18eaaf 3467 return -EINVAL;
adf9f195
FW
3468}
3469
613f04a0
SRRH
3470/* Some tracers require overwrite to stay enabled */
3471int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3472{
3473 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3474 return -1;
3475
3476 return 0;
3477}
3478
2b6080f2 3479int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
af4617bd
SR
3480{
3481 /* do nothing if flag is already set */
3482 if (!!(trace_flags & mask) == !!enabled)
613f04a0
SRRH
3483 return 0;
3484
3485 /* Give the tracer a chance to approve the change */
2b6080f2
SR
3486 if (tr->current_trace->flag_changed)
3487 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
613f04a0 3488 return -EINVAL;
af4617bd
SR
3489
3490 if (enabled)
3491 trace_flags |= mask;
3492 else
3493 trace_flags &= ~mask;
e870e9a1
LZ
3494
3495 if (mask == TRACE_ITER_RECORD_CMD)
3496 trace_event_enable_cmd_record(enabled);
750912fa 3497
80902822 3498 if (mask == TRACE_ITER_OVERWRITE) {
12883efb 3499 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
80902822 3500#ifdef CONFIG_TRACER_MAX_TRACE
12883efb 3501 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
80902822
SRRH
3502#endif
3503 }
81698831
SR
3504
3505 if (mask == TRACE_ITER_PRINTK)
3506 trace_printk_start_stop_comm(enabled);
613f04a0
SRRH
3507
3508 return 0;
af4617bd
SR
3509}
3510
2b6080f2 3511static int trace_set_options(struct trace_array *tr, char *option)
bc0c38d1 3512{
8d18eaaf 3513 char *cmp;
bc0c38d1 3514 int neg = 0;
613f04a0 3515 int ret = -ENODEV;
bc0c38d1
SR
3516 int i;
3517
7bcfaf54 3518 cmp = strstrip(option);
bc0c38d1 3519
8d18eaaf 3520 if (strncmp(cmp, "no", 2) == 0) {
bc0c38d1
SR
3521 neg = 1;
3522 cmp += 2;
3523 }
3524
69d34da2
SRRH
3525 mutex_lock(&trace_types_lock);
3526
bc0c38d1 3527 for (i = 0; trace_options[i]; i++) {
8d18eaaf 3528 if (strcmp(cmp, trace_options[i]) == 0) {
2b6080f2 3529 ret = set_tracer_flag(tr, 1 << i, !neg);
bc0c38d1
SR
3530 break;
3531 }
3532 }
adf9f195
FW
3533
3534 /* If no option could be set, test the specific tracer options */
69d34da2 3535 if (!trace_options[i])
2b6080f2 3536 ret = set_tracer_option(tr->current_trace, cmp, neg);
69d34da2
SRRH
3537
3538 mutex_unlock(&trace_types_lock);
bc0c38d1 3539
7bcfaf54
SR
3540 return ret;
3541}
3542
3543static ssize_t
3544tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3545 size_t cnt, loff_t *ppos)
3546{
2b6080f2
SR
3547 struct seq_file *m = filp->private_data;
3548 struct trace_array *tr = m->private;
7bcfaf54 3549 char buf[64];
613f04a0 3550 int ret;
7bcfaf54
SR
3551
3552 if (cnt >= sizeof(buf))
3553 return -EINVAL;
3554
3555 if (copy_from_user(&buf, ubuf, cnt))
3556 return -EFAULT;
3557
a8dd2176
SR
3558 buf[cnt] = 0;
3559
2b6080f2 3560 ret = trace_set_options(tr, buf);
613f04a0
SRRH
3561 if (ret < 0)
3562 return ret;
7bcfaf54 3563
cf8517cf 3564 *ppos += cnt;
bc0c38d1
SR
3565
3566 return cnt;
3567}
3568
fdb372ed
LZ
3569static int tracing_trace_options_open(struct inode *inode, struct file *file)
3570{
6492334c 3571 struct trace_array *tr = inode->i_private;
e45ccd09 3572 int ret;
6492334c 3573
fdb372ed
LZ
3574 if (tracing_disabled)
3575 return -ENODEV;
2b6080f2 3576
6492334c
SRRH
3577 if (trace_array_get(tr) < 0)
3578 return -ENODEV;
3579
e45ccd09
AL
3580 ret = single_open(file, tracing_trace_options_show, inode->i_private);
3581 if (ret < 0)
3582 trace_array_put(tr);
3583
3584 return ret;
fdb372ed
LZ
3585}
3586
5e2336a0 3587static const struct file_operations tracing_iter_fops = {
fdb372ed
LZ
3588 .open = tracing_trace_options_open,
3589 .read = seq_read,
3590 .llseek = seq_lseek,
6492334c 3591 .release = tracing_single_release_tr,
ee6bce52 3592 .write = tracing_trace_options_write,
bc0c38d1
SR
3593};
3594
7bd2f24c
IM
3595static const char readme_msg[] =
3596 "tracing mini-HOWTO:\n\n"
22f45649
SRRH
3597 "# echo 0 > tracing_on : quick way to disable tracing\n"
3598 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3599 " Important files:\n"
3600 " trace\t\t\t- The static contents of the buffer\n"
3601 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3602 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3603 " current_tracer\t- function and latency tracers\n"
3604 " available_tracers\t- list of configured tracers for current_tracer\n"
3605 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3606 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3607 " trace_clock\t\t-change the clock used to order events\n"
3608 " local: Per cpu clock but may not be synced across CPUs\n"
3609 " global: Synced across CPUs but slows tracing down.\n"
3610 " counter: Not a clock, but just an increment\n"
3611 " uptime: Jiffy counter from time of boot\n"
3612 " perf: Same clock that perf events use\n"
3613#ifdef CONFIG_X86_64
3614 " x86-tsc: TSC cycle counter\n"
3615#endif
3616 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3617 " tracing_cpumask\t- Limit which CPUs to trace\n"
3618 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3619 "\t\t\t Remove sub-buffer with rmdir\n"
3620 " trace_options\t\t- Set format or modify how tracing happens\n"
3621 "\t\t\t Disable an option by adding a suffix 'no' to the option name\n"
3622#ifdef CONFIG_DYNAMIC_FTRACE
3623 "\n available_filter_functions - list of functions that can be filtered on\n"
3624 " set_ftrace_filter\t- echo function name in here to only trace these functions\n"
3625 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3626 " modules: Can select a group via module\n"
3627 " Format: :mod:<module-name>\n"
3628 " example: echo :mod:ext3 > set_ftrace_filter\n"
3629 " triggers: a command to perform when function is hit\n"
3630 " Format: <function>:<trigger>[:count]\n"
3631 " trigger: traceon, traceoff\n"
3632 " enable_event:<system>:<event>\n"
3633 " disable_event:<system>:<event>\n"
3634#ifdef CONFIG_STACKTRACE
3635 " stacktrace\n"
3636#endif
3637#ifdef CONFIG_TRACER_SNAPSHOT
3638 " snapshot\n"
3639#endif
3640 " example: echo do_fault:traceoff > set_ftrace_filter\n"
3641 " echo do_trap:traceoff:3 > set_ftrace_filter\n"
3642 " The first one will disable tracing every time do_fault is hit\n"
3643 " The second will disable tracing at most 3 times when do_trap is hit\n"
3644 " The first time do trap is hit and it disables tracing, the counter\n"
3645 " will decrement to 2. If tracing is already disabled, the counter\n"
3646 " will not decrement. It only decrements when the trigger did work\n"
3647 " To remove trigger without count:\n"
3648 " echo '!<function>:<trigger> > set_ftrace_filter\n"
3649 " To remove trigger with a count:\n"
3650 " echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3651 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3652 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3653 " modules: Can select a group via module command :mod:\n"
3654 " Does not accept triggers\n"
3655#endif /* CONFIG_DYNAMIC_FTRACE */
3656#ifdef CONFIG_FUNCTION_TRACER
3657 " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
3658#endif
3659#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3660 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3661 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3662#endif
3663#ifdef CONFIG_TRACER_SNAPSHOT
3664 "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
3665 "\t\t\t Read the contents for more information\n"
3666#endif
3667#ifdef CONFIG_STACKTRACE
3668 " stack_trace\t\t- Shows the max stack trace when active\n"
3669 " stack_max_size\t- Shows current max stack size that was traced\n"
3670 "\t\t\t Write into this file to reset the max size (trigger a new trace)\n"
3671#ifdef CONFIG_DYNAMIC_FTRACE
3672 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
3673#endif
3674#endif /* CONFIG_STACKTRACE */
7bd2f24c
IM
3675;
3676
3677static ssize_t
3678tracing_readme_read(struct file *filp, char __user *ubuf,
3679 size_t cnt, loff_t *ppos)
3680{
3681 return simple_read_from_buffer(ubuf, cnt, ppos,
3682 readme_msg, strlen(readme_msg));
3683}
3684
5e2336a0 3685static const struct file_operations tracing_readme_fops = {
c7078de1
IM
3686 .open = tracing_open_generic,
3687 .read = tracing_readme_read,
b444786f 3688 .llseek = generic_file_llseek,
7bd2f24c
IM
3689};
3690
69abe6a5
AP
3691static ssize_t
3692tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3693 size_t cnt, loff_t *ppos)
3694{
3695 char *buf_comm;
3696 char *file_buf;
3697 char *buf;
3698 int len = 0;
3699 int pid;
3700 int i;
3701
3702 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3703 if (!file_buf)
3704 return -ENOMEM;
3705
3706 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3707 if (!buf_comm) {
3708 kfree(file_buf);
3709 return -ENOMEM;
3710 }
3711
3712 buf = file_buf;
3713
3714 for (i = 0; i < SAVED_CMDLINES; i++) {
3715 int r;
3716
3717 pid = map_cmdline_to_pid[i];
3718 if (pid == -1 || pid == NO_CMDLINE_MAP)
3719 continue;
3720
3721 trace_find_cmdline(pid, buf_comm);
3722 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3723 buf += r;
3724 len += r;
3725 }
3726
3727 len = simple_read_from_buffer(ubuf, cnt, ppos,
3728 file_buf, len);
3729
3730 kfree(file_buf);
3731 kfree(buf_comm);
3732
3733 return len;
3734}
3735
3736static const struct file_operations tracing_saved_cmdlines_fops = {
6fa3eb70
S
3737 .open = tracing_open_generic,
3738 .read = tracing_saved_cmdlines_read,
3739 .llseek = generic_file_llseek,
3740};
3741
3742static ssize_t
3743tracing_saved_tgids_read(struct file *file, char __user *ubuf,
3744 size_t cnt, loff_t *ppos)
3745{
3746 char *file_buf;
3747 char *buf;
3748 int len = 0;
3749 int pid;
3750 int i;
3751
3752 file_buf = kmalloc(SAVED_CMDLINES*(16+1+16), GFP_KERNEL);
3753 if (!file_buf)
3754 return -ENOMEM;
3755
3756 buf = file_buf;
3757
3758 for (i = 0; i < SAVED_CMDLINES; i++) {
3759 int tgid;
3760 int r;
3761
3762 pid = map_cmdline_to_pid[i];
3763 if (pid == -1 || pid == NO_CMDLINE_MAP)
3764 continue;
3765
3766 tgid = trace_find_tgid(pid);
3767 r = sprintf(buf, "%d %d\n", pid, tgid);
3768 buf += r;
3769 len += r;
3770 }
3771
3772 len = simple_read_from_buffer(ubuf, cnt, ppos,
3773 file_buf, len);
3774
3775 kfree(file_buf);
3776
3777 return len;
3778}
3779
3780static const struct file_operations tracing_saved_tgids_fops = {
3781 .open = tracing_open_generic,
3782 .read = tracing_saved_tgids_read,
3783 .llseek = generic_file_llseek,
69abe6a5
AP
3784};
3785
bc0c38d1
SR
3786static ssize_t
3787tracing_set_trace_read(struct file *filp, char __user *ubuf,
3788 size_t cnt, loff_t *ppos)
3789{
2b6080f2 3790 struct trace_array *tr = filp->private_data;
ee6c2c1b 3791 char buf[MAX_TRACER_SIZE+2];
bc0c38d1
SR
3792 int r;
3793
3794 mutex_lock(&trace_types_lock);
2b6080f2 3795 r = sprintf(buf, "%s\n", tr->current_trace->name);
bc0c38d1
SR
3796 mutex_unlock(&trace_types_lock);
3797
4bf39a94 3798 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
3799}
3800
b6f11df2
ACM
3801int tracer_init(struct tracer *t, struct trace_array *tr)
3802{
12883efb 3803 tracing_reset_online_cpus(&tr->trace_buffer);
b6f11df2
ACM
3804 return t->init(tr);
3805}
3806
12883efb 3807static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
438ced17
VN
3808{
3809 int cpu;
737223fb 3810
438ced17 3811 for_each_tracing_cpu(cpu)
12883efb 3812 per_cpu_ptr(buf->data, cpu)->entries = val;
438ced17
VN
3813}
3814
12883efb 3815#ifdef CONFIG_TRACER_MAX_TRACE
d60da506 3816/* resize @tr's buffer to the size of @size_tr's entries */
12883efb
SRRH
3817static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3818 struct trace_buffer *size_buf, int cpu_id)
d60da506
HT
3819{
3820 int cpu, ret = 0;
3821
3822 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3823 for_each_tracing_cpu(cpu) {
12883efb
SRRH
3824 ret = ring_buffer_resize(trace_buf->buffer,
3825 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
d60da506
HT
3826 if (ret < 0)
3827 break;
12883efb
SRRH
3828 per_cpu_ptr(trace_buf->data, cpu)->entries =
3829 per_cpu_ptr(size_buf->data, cpu)->entries;
d60da506
HT
3830 }
3831 } else {
12883efb
SRRH
3832 ret = ring_buffer_resize(trace_buf->buffer,
3833 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
d60da506 3834 if (ret == 0)
12883efb
SRRH
3835 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3836 per_cpu_ptr(size_buf->data, cpu_id)->entries;
d60da506
HT
3837 }
3838
3839 return ret;
3840}
12883efb 3841#endif /* CONFIG_TRACER_MAX_TRACE */
d60da506 3842
2b6080f2
SR
3843static int __tracing_resize_ring_buffer(struct trace_array *tr,
3844 unsigned long size, int cpu)
73c5162a
SR
3845{
3846 int ret;
3847
3848 /*
3849 * If kernel or user changes the size of the ring buffer
a123c52b
SR
3850 * we use the size that was given, and we can forget about
3851 * expanding it later.
73c5162a 3852 */
55034cd6 3853 ring_buffer_expanded = true;
73c5162a 3854
b382ede6 3855 /* May be called before buffers are initialized */
12883efb 3856 if (!tr->trace_buffer.buffer)
b382ede6
SR
3857 return 0;
3858
12883efb 3859 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
73c5162a
SR
3860 if (ret < 0)
3861 return ret;
3862
12883efb 3863#ifdef CONFIG_TRACER_MAX_TRACE
2b6080f2
SR
3864 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3865 !tr->current_trace->use_max_tr)
ef710e10
KM
3866 goto out;
3867
12883efb 3868 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
73c5162a 3869 if (ret < 0) {
12883efb
SRRH
3870 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3871 &tr->trace_buffer, cpu);
73c5162a 3872 if (r < 0) {
a123c52b
SR
3873 /*
3874 * AARGH! We are left with different
3875 * size max buffer!!!!
3876 * The max buffer is our "snapshot" buffer.
3877 * When a tracer needs a snapshot (one of the
3878 * latency tracers), it swaps the max buffer
3879 * with the saved snap shot. We succeeded to
3880 * update the size of the main buffer, but failed to
3881 * update the size of the max buffer. But when we tried
3882 * to reset the main buffer to the original size, we
3883 * failed there too. This is very unlikely to
3884 * happen, but if it does, warn and kill all
3885 * tracing.
3886 */
73c5162a
SR
3887 WARN_ON(1);
3888 tracing_disabled = 1;
3889 }
3890 return ret;
3891 }
3892
438ced17 3893 if (cpu == RING_BUFFER_ALL_CPUS)
12883efb 3894 set_buffer_entries(&tr->max_buffer, size);
438ced17 3895 else
12883efb 3896 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
438ced17 3897
ef710e10 3898 out:
12883efb
SRRH
3899#endif /* CONFIG_TRACER_MAX_TRACE */
3900
438ced17 3901 if (cpu == RING_BUFFER_ALL_CPUS)
12883efb 3902 set_buffer_entries(&tr->trace_buffer, size);
438ced17 3903 else
12883efb 3904 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
73c5162a
SR
3905
3906 return ret;
3907}
3908
6fa3eb70 3909ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
2b6080f2 3910 unsigned long size, int cpu_id)
4f271a2a 3911{
83f40318 3912 int ret = size;
4f271a2a
VN
3913
3914 mutex_lock(&trace_types_lock);
3915
438ced17
VN
3916 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3917 /* make sure, this cpu is enabled in the mask */
3918 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3919 ret = -EINVAL;
3920 goto out;
3921 }
3922 }
4f271a2a 3923
2b6080f2 3924 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
4f271a2a
VN
3925 if (ret < 0)
3926 ret = -ENOMEM;
3927
438ced17 3928out:
4f271a2a
VN
3929 mutex_unlock(&trace_types_lock);
3930
3931 return ret;
3932}
3933
1852fcce
SR
3934/**
3935 * tracing_update_buffers - used by tracing facility to expand ring buffers
3936 *
3937 * To save on memory when the tracing is never used on a system with it
3938 * configured in. The ring buffers are set to a minimum size. But once
3939 * a user starts to use the tracing facility, then they need to grow
3940 * to their default size.
3941 *
3942 * This function is to be called when a tracer is about to be used.
3943 */
3944int tracing_update_buffers(void)
3945{
3946 int ret = 0;
6fa3eb70
S
3947#ifdef CONFIG_MTK_FTRACE_DEFAULT_ENABLE
3948 int i = 0;
3949#endif
1852fcce 3950
1027fcb2 3951 mutex_lock(&trace_types_lock);
1852fcce 3952 if (!ring_buffer_expanded)
6fa3eb70
S
3953#ifdef CONFIG_MTK_FTRACE_DEFAULT_ENABLE
3954 {
3955 if(get_max_DRAM_size() >= 0x40000000 && !trace_buf_size_updated_from_cmdline){
3956 trace_buf_size_cpu0 = (CPUX_TRACE_BUF_SIZE_DEFAULT * CPU0_to_CPUX_RATIO * 1.25);
3957 trace_buf_size_cpuX = (CPUX_TRACE_BUF_SIZE_DEFAULT * 1.25);
3958 }
3959
3960 for_each_tracing_cpu(i){
3961 ret = __tracing_resize_ring_buffer(&global_trace, (i==0?trace_buf_size_cpu0:trace_buf_size_cpuX), i);
3962 if(ret < 0){
3963 printk("KERN_INFO [ftrace]fail to update cpu%d ring buffer to %lu KB \n",
3964 i, (i==0?(trace_buf_size_cpu0>>10):(trace_buf_size_cpuX>>10)));
3965 break;
3966 }
3967 }
3968 }
3969#else
2b6080f2 3970 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
438ced17 3971 RING_BUFFER_ALL_CPUS);
6fa3eb70 3972#endif
1027fcb2 3973 mutex_unlock(&trace_types_lock);
1852fcce
SR
3974
3975 return ret;
3976}
3977
577b785f
SR
3978struct trace_option_dentry;
3979
3980static struct trace_option_dentry *
2b6080f2 3981create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
577b785f
SR
3982
3983static void
3984destroy_trace_option_files(struct trace_option_dentry *topts);
3985
b2821ae6 3986static int tracing_set_tracer(const char *buf)
bc0c38d1 3987{
577b785f 3988 static struct trace_option_dentry *topts;
bc0c38d1
SR
3989 struct trace_array *tr = &global_trace;
3990 struct tracer *t;
12883efb 3991#ifdef CONFIG_TRACER_MAX_TRACE
34600f0e 3992 bool had_max_tr;
12883efb 3993#endif
d9e54076 3994 int ret = 0;
bc0c38d1 3995
1027fcb2
SR
3996 mutex_lock(&trace_types_lock);
3997
73c5162a 3998 if (!ring_buffer_expanded) {
2b6080f2 3999 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
438ced17 4000 RING_BUFFER_ALL_CPUS);
73c5162a 4001 if (ret < 0)
59f586db 4002 goto out;
73c5162a
SR
4003 ret = 0;
4004 }
4005
bc0c38d1
SR
4006 for (t = trace_types; t; t = t->next) {
4007 if (strcmp(t->name, buf) == 0)
4008 break;
4009 }
c2931e05
FW
4010 if (!t) {
4011 ret = -EINVAL;
4012 goto out;
4013 }
2b6080f2 4014 if (t == tr->current_trace)
bc0c38d1
SR
4015 goto out;
4016
9f029e83 4017 trace_branch_disable();
613f04a0 4018
2b6080f2 4019 tr->current_trace->enabled = false;
613f04a0 4020
2b6080f2
SR
4021 if (tr->current_trace->reset)
4022 tr->current_trace->reset(tr);
34600f0e 4023
12883efb 4024 /* Current trace needs to be nop_trace before synchronize_sched */
2b6080f2 4025 tr->current_trace = &nop_trace;
34600f0e 4026
45ad21ca
SRRH
4027#ifdef CONFIG_TRACER_MAX_TRACE
4028 had_max_tr = tr->allocated_snapshot;
34600f0e
SR
4029
4030 if (had_max_tr && !t->use_max_tr) {
4031 /*
4032 * We need to make sure that the update_max_tr sees that
4033 * current_trace changed to nop_trace to keep it from
4034 * swapping the buffers after we resize it.
4035 * The update_max_tr is called from interrupts disabled
4036 * so a synchronized_sched() is sufficient.
4037 */
4038 synchronize_sched();
3209cff4 4039 free_snapshot(tr);
ef710e10 4040 }
12883efb 4041#endif
577b785f
SR
4042 destroy_trace_option_files(topts);
4043
2b6080f2 4044 topts = create_trace_option_files(tr, t);
12883efb
SRRH
4045
4046#ifdef CONFIG_TRACER_MAX_TRACE
34600f0e 4047 if (t->use_max_tr && !had_max_tr) {
3209cff4 4048 ret = alloc_snapshot(tr);
d60da506
HT
4049 if (ret < 0)
4050 goto out;
ef710e10 4051 }
12883efb 4052#endif
577b785f 4053
1c80025a 4054 if (t->init) {
b6f11df2 4055 ret = tracer_init(t, tr);
1c80025a
FW
4056 if (ret)
4057 goto out;
4058 }
bc0c38d1 4059
2b6080f2
SR
4060 tr->current_trace = t;
4061 tr->current_trace->enabled = true;
9f029e83 4062 trace_branch_enable(tr);
bc0c38d1
SR
4063 out:
4064 mutex_unlock(&trace_types_lock);
4065
d9e54076
PZ
4066 return ret;
4067}
4068
4069static ssize_t
4070tracing_set_trace_write(struct file *filp, const char __user *ubuf,
4071 size_t cnt, loff_t *ppos)
4072{
ee6c2c1b 4073 char buf[MAX_TRACER_SIZE+1];
d9e54076
PZ
4074 int i;
4075 size_t ret;
e6e7a65a
FW
4076 int err;
4077
4078 ret = cnt;
d9e54076 4079
ee6c2c1b
LZ
4080 if (cnt > MAX_TRACER_SIZE)
4081 cnt = MAX_TRACER_SIZE;
d9e54076
PZ
4082
4083 if (copy_from_user(&buf, ubuf, cnt))
4084 return -EFAULT;
4085
4086 buf[cnt] = 0;
4087
4088 /* strip ending whitespace. */
4089 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
4090 buf[i] = 0;
4091
6fa3eb70 4092 printk(KERN_INFO "[ftrace]set current_tracer to '%s'\n", buf);
e6e7a65a
FW
4093 err = tracing_set_tracer(buf);
4094 if (err)
4095 return err;
d9e54076 4096
cf8517cf 4097 *ppos += ret;
bc0c38d1 4098
c2931e05 4099 return ret;
bc0c38d1
SR
4100}
4101
4102static ssize_t
4103tracing_max_lat_read(struct file *filp, char __user *ubuf,
4104 size_t cnt, loff_t *ppos)
4105{
4106 unsigned long *ptr = filp->private_data;
4107 char buf[64];
4108 int r;
4109
cffae437 4110 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 4111 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
4112 if (r > sizeof(buf))
4113 r = sizeof(buf);
4bf39a94 4114 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
4115}
4116
4117static ssize_t
4118tracing_max_lat_write(struct file *filp, const char __user *ubuf,
4119 size_t cnt, loff_t *ppos)
4120{
5e39841c 4121 unsigned long *ptr = filp->private_data;
5e39841c 4122 unsigned long val;
c6caeeb1 4123 int ret;
bc0c38d1 4124
22fe9b54
PH
4125 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4126 if (ret)
c6caeeb1 4127 return ret;
bc0c38d1
SR
4128
4129 *ptr = val * 1000;
4130
4131 return cnt;
4132}
4133
b3806b43
SR
4134static int tracing_open_pipe(struct inode *inode, struct file *filp)
4135{
23b625fa 4136 struct trace_array *tr = inode->i_private;
b3806b43 4137 struct trace_iterator *iter;
b04cc6b1 4138 int ret = 0;
b3806b43
SR
4139
4140 if (tracing_disabled)
4141 return -ENODEV;
4142
6492334c
SRRH
4143 if (trace_array_get(tr) < 0)
4144 return -ENODEV;
4145
b04cc6b1
FW
4146 mutex_lock(&trace_types_lock);
4147
b3806b43
SR
4148 /* create a buffer to store the information to pass to userspace */
4149 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
b04cc6b1
FW
4150 if (!iter) {
4151 ret = -ENOMEM;
e45ccd09 4152 __trace_array_put(tr);
b04cc6b1
FW
4153 goto out;
4154 }
b3806b43 4155
d7350c3f
FW
4156 /*
4157 * We make a copy of the current tracer to avoid concurrent
4158 * changes on it while we are reading.
4159 */
4160 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
4161 if (!iter->trace) {
4162 ret = -ENOMEM;
4163 goto fail;
4164 }
2b6080f2 4165 *iter->trace = *tr->current_trace;
d7350c3f 4166
4462344e 4167 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
b04cc6b1 4168 ret = -ENOMEM;
d7350c3f 4169 goto fail;
4462344e
RR
4170 }
4171
a309720c 4172 /* trace pipe does not show start of buffer */
4462344e 4173 cpumask_setall(iter->started);
a309720c 4174
112f38a7
SR
4175 if (trace_flags & TRACE_ITER_LATENCY_FMT)
4176 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4177
8be0709f 4178 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
58e8eedf 4179 if (trace_clocks[tr->clock_id].in_ns)
8be0709f
DS
4180 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4181
23b625fa
ON
4182 iter->tr = tr;
4183 iter->trace_buffer = &tr->trace_buffer;
4184 iter->cpu_file = tracing_get_cpu(inode);
d7350c3f 4185 mutex_init(&iter->mutex);
b3806b43
SR
4186 filp->private_data = iter;
4187
107bad8b
SR
4188 if (iter->trace->pipe_open)
4189 iter->trace->pipe_open(iter);
107bad8b 4190
b444786f 4191 nonseekable_open(inode, filp);
b04cc6b1
FW
4192out:
4193 mutex_unlock(&trace_types_lock);
4194 return ret;
d7350c3f
FW
4195
4196fail:
4197 kfree(iter->trace);
4198 kfree(iter);
6492334c 4199 __trace_array_put(tr);
d7350c3f
FW
4200 mutex_unlock(&trace_types_lock);
4201 return ret;
b3806b43
SR
4202}
4203
4204static int tracing_release_pipe(struct inode *inode, struct file *file)
4205{
4206 struct trace_iterator *iter = file->private_data;
23b625fa 4207 struct trace_array *tr = inode->i_private;
b3806b43 4208
b04cc6b1
FW
4209 mutex_lock(&trace_types_lock);
4210
29bf4a5e 4211 if (iter->trace->pipe_close)
c521efd1
SR
4212 iter->trace->pipe_close(iter);
4213
b04cc6b1
FW
4214 mutex_unlock(&trace_types_lock);
4215
4462344e 4216 free_cpumask_var(iter->started);
d7350c3f
FW
4217 mutex_destroy(&iter->mutex);
4218 kfree(iter->trace);
b3806b43 4219 kfree(iter);
b3806b43 4220
6492334c
SRRH
4221 trace_array_put(tr);
4222
b3806b43
SR
4223 return 0;
4224}
4225
2a2cc8f7 4226static unsigned int
cc60cdc9 4227trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
2a2cc8f7 4228{
15693458
SRRH
4229 /* Iterators are static, they should be filled or empty */
4230 if (trace_buffer_iter(iter, iter->cpu_file))
4231 return POLLIN | POLLRDNORM;
2a2cc8f7 4232
15693458 4233 if (trace_flags & TRACE_ITER_BLOCK)
2a2cc8f7
SSP
4234 /*
4235 * Always select as readable when in blocking mode
4236 */
4237 return POLLIN | POLLRDNORM;
15693458 4238 else
12883efb 4239 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
15693458 4240 filp, poll_table);
2a2cc8f7 4241}
2a2cc8f7 4242
cc60cdc9
SR
4243static unsigned int
4244tracing_poll_pipe(struct file *filp, poll_table *poll_table)
4245{
4246 struct trace_iterator *iter = filp->private_data;
4247
4248 return trace_poll(iter, filp, poll_table);
2a2cc8f7
SSP
4249}
4250
6eaaa5d5
FW
4251/*
4252 * This is a make-shift waitqueue.
4253 * A tracer might use this callback on some rare cases:
4254 *
4255 * 1) the current tracer might hold the runqueue lock when it wakes up
4256 * a reader, hence a deadlock (sched, function, and function graph tracers)
4257 * 2) the function tracers, trace all functions, we don't want
4258 * the overhead of calling wake_up and friends
4259 * (and tracing them too)
4260 *
4261 * Anyway, this is really very primitive wakeup.
4262 */
561237e4 4263int poll_wait_pipe(struct trace_iterator *iter)
6eaaa5d5
FW
4264{
4265 set_current_state(TASK_INTERRUPTIBLE);
4266 /* sleep for 100 msecs, and try again. */
4267 schedule_timeout(HZ / 10);
561237e4 4268 return 0;
6eaaa5d5
FW
4269}
4270
ff98781b
EGM
4271/* Must be called with trace_types_lock mutex held. */
4272static int tracing_wait_pipe(struct file *filp)
b3806b43
SR
4273{
4274 struct trace_iterator *iter = filp->private_data;
561237e4 4275 int ret;
b3806b43 4276
b3806b43 4277 while (trace_empty(iter)) {
2dc8f095 4278
107bad8b 4279 if ((filp->f_flags & O_NONBLOCK)) {
ff98781b 4280 return -EAGAIN;
107bad8b 4281 }
2dc8f095 4282
d7350c3f 4283 mutex_unlock(&iter->mutex);
107bad8b 4284
561237e4 4285 ret = iter->trace->wait_pipe(iter);
b3806b43 4286
d7350c3f 4287 mutex_lock(&iter->mutex);
107bad8b 4288
561237e4
SRRH
4289 if (ret)
4290 return ret;
4291
6eaaa5d5 4292 if (signal_pending(current))
ff98781b 4293 return -EINTR;
b3806b43
SR
4294
4295 /*
250bfd3d 4296 * We block until we read something and tracing is disabled.
b3806b43
SR
4297 * We still block if tracing is disabled, but we have never
4298 * read anything. This allows a user to cat this file, and
4299 * then enable tracing. But after we have read something,
4300 * we give an EOF when tracing is again disabled.
4301 *
4302 * iter->pos will be 0 if we haven't read anything.
4303 */
2fd821ee 4304 if (!tracing_is_on() && iter->pos)
b3806b43 4305 break;
b3806b43
SR
4306 }
4307
ff98781b
EGM
4308 return 1;
4309}
4310
4311/*
4312 * Consumer reader.
4313 */
4314static ssize_t
4315tracing_read_pipe(struct file *filp, char __user *ubuf,
4316 size_t cnt, loff_t *ppos)
4317{
4318 struct trace_iterator *iter = filp->private_data;
2b6080f2 4319 struct trace_array *tr = iter->tr;
ff98781b
EGM
4320 ssize_t sret;
4321
d7350c3f 4322 /* copy the tracer to avoid using a global lock all around */
ff98781b 4323 mutex_lock(&trace_types_lock);
2b6080f2
SR
4324 if (unlikely(iter->trace->name != tr->current_trace->name))
4325 *iter->trace = *tr->current_trace;
d7350c3f
FW
4326 mutex_unlock(&trace_types_lock);
4327
4328 /*
4329 * Avoid more than one consumer on a single file descriptor
4330 * This is just a matter of traces coherency, the ring buffer itself
4331 * is protected.
4332 */
4333 mutex_lock(&iter->mutex);
0d75118c
SRRH
4334
4335 /* return any leftover data */
4336 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4337 if (sret != -EBUSY)
4338 goto out;
4339
4340 trace_seq_init(&iter->seq);
4341
ff98781b
EGM
4342 if (iter->trace->read) {
4343 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4344 if (sret)
4345 goto out;
4346 }
4347
4348waitagain:
4349 sret = tracing_wait_pipe(filp);
4350 if (sret <= 0)
4351 goto out;
4352
b3806b43 4353 /* stop when tracing is finished */
ff98781b
EGM
4354 if (trace_empty(iter)) {
4355 sret = 0;
107bad8b 4356 goto out;
ff98781b 4357 }
b3806b43
SR
4358
4359 if (cnt >= PAGE_SIZE)
4360 cnt = PAGE_SIZE - 1;
4361
53d0aa77 4362 /* reset all but tr, trace, and overruns */
53d0aa77
SR
4363 memset(&iter->seq, 0,
4364 sizeof(struct trace_iterator) -
4365 offsetof(struct trace_iterator, seq));
2d7ddf0a 4366 cpumask_clear(iter->started);
4823ed7e 4367 iter->pos = -1;
b3806b43 4368
4f535968 4369 trace_event_read_lock();
7e53bd42 4370 trace_access_lock(iter->cpu_file);
955b61e5 4371 while (trace_find_next_entry_inc(iter) != NULL) {
2c4f035f 4372 enum print_line_t ret;
088b1e42
SR
4373 int len = iter->seq.len;
4374
f9896bf3 4375 ret = print_trace_line(iter);
2c4f035f 4376 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42
SR
4377 /* don't print partial lines */
4378 iter->seq.len = len;
b3806b43 4379 break;
088b1e42 4380 }
b91facc3
FW
4381 if (ret != TRACE_TYPE_NO_CONSUME)
4382 trace_consume(iter);
b3806b43
SR
4383
4384 if (iter->seq.len >= cnt)
4385 break;
ee5e51f5
JO
4386
4387 /*
4388 * Setting the full flag means we reached the trace_seq buffer
4389 * size and we should leave by partial output condition above.
4390 * One of the trace_seq_* functions is not used properly.
4391 */
4392 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4393 iter->ent->type);
b3806b43 4394 }
7e53bd42 4395 trace_access_unlock(iter->cpu_file);
4f535968 4396 trace_event_read_unlock();
b3806b43 4397
b3806b43 4398 /* Now copy what we have to the user */
6c6c2796
PP
4399 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4400 if (iter->seq.readpos >= iter->seq.len)
f9520750 4401 trace_seq_init(&iter->seq);
9ff4b974
PP
4402
4403 /*
25985edc 4404 * If there was nothing to send to user, in spite of consuming trace
9ff4b974
PP
4405 * entries, go back to wait for more entries.
4406 */
6c6c2796 4407 if (sret == -EBUSY)
9ff4b974 4408 goto waitagain;
b3806b43 4409
107bad8b 4410out:
d7350c3f 4411 mutex_unlock(&iter->mutex);
107bad8b 4412
6c6c2796 4413 return sret;
b3806b43
SR
4414}
4415
3c56819b
EGM
4416static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
4417 struct pipe_buffer *buf)
4418{
4419 __free_page(buf->page);
4420}
4421
4422static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4423 unsigned int idx)
4424{
4425 __free_page(spd->pages[idx]);
4426}
4427
28dfef8f 4428static const struct pipe_buf_operations tracing_pipe_buf_ops = {
34cd4998
SR
4429 .can_merge = 0,
4430 .map = generic_pipe_buf_map,
4431 .unmap = generic_pipe_buf_unmap,
4432 .confirm = generic_pipe_buf_confirm,
4433 .release = tracing_pipe_buf_release,
4434 .steal = generic_pipe_buf_steal,
4435 .get = generic_pipe_buf_get,
3c56819b
EGM
4436};
4437
34cd4998 4438static size_t
fa7c7f6e 4439tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
34cd4998
SR
4440{
4441 size_t count;
4442 int ret;
4443
4444 /* Seq buffer is page-sized, exactly what we need. */
4445 for (;;) {
4446 count = iter->seq.len;
4447 ret = print_trace_line(iter);
4448 count = iter->seq.len - count;
4449 if (rem < count) {
4450 rem = 0;
4451 iter->seq.len -= count;
4452 break;
4453 }
4454 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4455 iter->seq.len -= count;
4456 break;
4457 }
4458
74e7ff8c
LJ
4459 if (ret != TRACE_TYPE_NO_CONSUME)
4460 trace_consume(iter);
34cd4998 4461 rem -= count;
955b61e5 4462 if (!trace_find_next_entry_inc(iter)) {
34cd4998
SR
4463 rem = 0;
4464 iter->ent = NULL;
4465 break;
4466 }
4467 }
4468
4469 return rem;
4470}
4471
3c56819b
EGM
4472static ssize_t tracing_splice_read_pipe(struct file *filp,
4473 loff_t *ppos,
4474 struct pipe_inode_info *pipe,
4475 size_t len,
4476 unsigned int flags)
4477{
35f3d14d
JA
4478 struct page *pages_def[PIPE_DEF_BUFFERS];
4479 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3c56819b
EGM
4480 struct trace_iterator *iter = filp->private_data;
4481 struct splice_pipe_desc spd = {
35f3d14d
JA
4482 .pages = pages_def,
4483 .partial = partial_def,
34cd4998 4484 .nr_pages = 0, /* This gets updated below. */
047fe360 4485 .nr_pages_max = PIPE_DEF_BUFFERS,
34cd4998
SR
4486 .flags = flags,
4487 .ops = &tracing_pipe_buf_ops,
4488 .spd_release = tracing_spd_release_pipe,
3c56819b 4489 };
2b6080f2 4490 struct trace_array *tr = iter->tr;
3c56819b 4491 ssize_t ret;
34cd4998 4492 size_t rem;
3c56819b
EGM
4493 unsigned int i;
4494
35f3d14d
JA
4495 if (splice_grow_spd(pipe, &spd))
4496 return -ENOMEM;
4497
d7350c3f 4498 /* copy the tracer to avoid using a global lock all around */
3c56819b 4499 mutex_lock(&trace_types_lock);
2b6080f2
SR
4500 if (unlikely(iter->trace->name != tr->current_trace->name))
4501 *iter->trace = *tr->current_trace;
d7350c3f
FW
4502 mutex_unlock(&trace_types_lock);
4503
4504 mutex_lock(&iter->mutex);
3c56819b
EGM
4505
4506 if (iter->trace->splice_read) {
4507 ret = iter->trace->splice_read(iter, filp,
4508 ppos, pipe, len, flags);
4509 if (ret)
34cd4998 4510 goto out_err;
3c56819b
EGM
4511 }
4512
4513 ret = tracing_wait_pipe(filp);
4514 if (ret <= 0)
34cd4998 4515 goto out_err;
3c56819b 4516
955b61e5 4517 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3c56819b 4518 ret = -EFAULT;
34cd4998 4519 goto out_err;
3c56819b
EGM
4520 }
4521
4f535968 4522 trace_event_read_lock();
7e53bd42 4523 trace_access_lock(iter->cpu_file);
4f535968 4524
3c56819b 4525 /* Fill as many pages as possible. */
35f3d14d
JA
4526 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4527 spd.pages[i] = alloc_page(GFP_KERNEL);
4528 if (!spd.pages[i])
34cd4998 4529 break;
3c56819b 4530
fa7c7f6e 4531 rem = tracing_fill_pipe_page(rem, iter);
3c56819b
EGM
4532
4533 /* Copy the data into the page, so we can start over. */
4534 ret = trace_seq_to_buffer(&iter->seq,
35f3d14d 4535 page_address(spd.pages[i]),
3c56819b
EGM
4536 iter->seq.len);
4537 if (ret < 0) {
35f3d14d 4538 __free_page(spd.pages[i]);
3c56819b
EGM
4539 break;
4540 }
35f3d14d
JA
4541 spd.partial[i].offset = 0;
4542 spd.partial[i].len = iter->seq.len;
3c56819b 4543
f9520750 4544 trace_seq_init(&iter->seq);
3c56819b
EGM
4545 }
4546
7e53bd42 4547 trace_access_unlock(iter->cpu_file);
4f535968 4548 trace_event_read_unlock();
d7350c3f 4549 mutex_unlock(&iter->mutex);
3c56819b
EGM
4550
4551 spd.nr_pages = i;
4552
3f3f1fce
SRRH
4553 if (i)
4554 ret = splice_to_pipe(pipe, &spd);
4555 else
4556 ret = 0;
35f3d14d 4557out:
047fe360 4558 splice_shrink_spd(&spd);
35f3d14d 4559 return ret;
3c56819b 4560
34cd4998 4561out_err:
d7350c3f 4562 mutex_unlock(&iter->mutex);
35f3d14d 4563 goto out;
3c56819b
EGM
4564}
4565
a98a3c3f
SR
4566static ssize_t
4567tracing_entries_read(struct file *filp, char __user *ubuf,
4568 size_t cnt, loff_t *ppos)
4569{
e5dd03c4
ON
4570 struct inode *inode = file_inode(filp);
4571 struct trace_array *tr = inode->i_private;
4572 int cpu = tracing_get_cpu(inode);
438ced17
VN
4573 char buf[64];
4574 int r = 0;
4575 ssize_t ret;
a98a3c3f 4576
db526ca3 4577 mutex_lock(&trace_types_lock);
438ced17 4578
e5dd03c4 4579 if (cpu == RING_BUFFER_ALL_CPUS) {
438ced17
VN
4580 int cpu, buf_size_same;
4581 unsigned long size;
4582
4583 size = 0;
4584 buf_size_same = 1;
4585 /* check if all cpu sizes are same */
4586 for_each_tracing_cpu(cpu) {
4587 /* fill in the size from first enabled cpu */
4588 if (size == 0)
12883efb
SRRH
4589 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4590 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
438ced17
VN
4591 buf_size_same = 0;
4592 break;
4593 }
4594 }
4595
4596 if (buf_size_same) {
4597 if (!ring_buffer_expanded)
4598 r = sprintf(buf, "%lu (expanded: %lu)\n",
4599 size >> 10,
4600 trace_buf_size >> 10);
4601 else
4602 r = sprintf(buf, "%lu\n", size >> 10);
4603 } else
4604 r = sprintf(buf, "X\n");
4605 } else
e5dd03c4 4606 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
438ced17 4607
db526ca3
SR
4608 mutex_unlock(&trace_types_lock);
4609
438ced17
VN
4610 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4611 return ret;
a98a3c3f
SR
4612}
4613
4614static ssize_t
4615tracing_entries_write(struct file *filp, const char __user *ubuf,
4616 size_t cnt, loff_t *ppos)
4617{
e5dd03c4
ON
4618 struct inode *inode = file_inode(filp);
4619 struct trace_array *tr = inode->i_private;
a98a3c3f 4620 unsigned long val;
6fa3eb70 4621 int do_drop_cache = 0;
4f271a2a 4622 int ret;
a98a3c3f 4623
22fe9b54
PH
4624 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4625 if (ret)
c6caeeb1 4626 return ret;
a98a3c3f
SR
4627
4628 /* must have at least 1 entry */
4629 if (!val)
4630 return -EINVAL;
4631
1696b2b0
SR
4632 /* value is in KB */
4633 val <<= 10;
6fa3eb70 4634resize_ring_buffer:
e5dd03c4 4635 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
6fa3eb70
S
4636 if (ret == -ENOMEM && !do_drop_cache) {
4637 do_drop_cache++;
4638 drop_pagecache();
4639 goto resize_ring_buffer;
4640 } else if (ret < 0)
4f271a2a 4641 return ret;
a98a3c3f 4642
cf8517cf 4643 *ppos += cnt;
a98a3c3f 4644
4f271a2a
VN
4645 return cnt;
4646}
bf5e6519 4647
f81ab074
VN
4648static ssize_t
4649tracing_total_entries_read(struct file *filp, char __user *ubuf,
4650 size_t cnt, loff_t *ppos)
4651{
4652 struct trace_array *tr = filp->private_data;
4653 char buf[64];
4654 int r, cpu;
4655 unsigned long size = 0, expanded_size = 0;
4656
4657 mutex_lock(&trace_types_lock);
4658 for_each_tracing_cpu(cpu) {
12883efb 4659 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
f81ab074
VN
4660 if (!ring_buffer_expanded)
4661 expanded_size += trace_buf_size >> 10;
4662 }
4663 if (ring_buffer_expanded)
4664 r = sprintf(buf, "%lu\n", size);
4665 else
4666 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4667 mutex_unlock(&trace_types_lock);
4668
4669 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4670}
4671
4f271a2a
VN
4672static ssize_t
4673tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4674 size_t cnt, loff_t *ppos)
4675{
4676 /*
4677 * There is no need to read what the user has written, this function
4678 * is just to make sure that there is no error when "echo" is used
4679 */
4680
4681 *ppos += cnt;
a98a3c3f
SR
4682
4683 return cnt;
4684}
4685
4f271a2a
VN
4686static int
4687tracing_free_buffer_release(struct inode *inode, struct file *filp)
4688{
2b6080f2
SR
4689 struct trace_array *tr = inode->i_private;
4690
cf30cf67
SR
4691 /* disable tracing ? */
4692 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
e25d4586 4693 tracer_tracing_off(tr);
4f271a2a 4694 /* resize the ring buffer to 0 */
2b6080f2 4695 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4f271a2a 4696
6492334c
SRRH
4697 trace_array_put(tr);
4698
4f271a2a
VN
4699 return 0;
4700}
4701
5bf9a1ee
PP
4702static ssize_t
4703tracing_mark_write(struct file *filp, const char __user *ubuf,
4704 size_t cnt, loff_t *fpos)
4705{
d696b58c 4706 unsigned long addr = (unsigned long)ubuf;
b7f15519 4707 struct trace_array *tr = filp->private_data;
d696b58c
SR
4708 struct ring_buffer_event *event;
4709 struct ring_buffer *buffer;
4710 struct print_entry *entry;
4711 unsigned long irq_flags;
4712 struct page *pages[2];
6edb2a8a 4713 void *map_page[2];
d696b58c
SR
4714 int nr_pages = 1;
4715 ssize_t written;
d696b58c
SR
4716 int offset;
4717 int size;
4718 int len;
4719 int ret;
6edb2a8a 4720 int i;
5bf9a1ee 4721
c76f0694 4722 if (tracing_disabled)
5bf9a1ee
PP
4723 return -EINVAL;
4724
5224c3a3
MSB
4725 if (!(trace_flags & TRACE_ITER_MARKERS))
4726 return -EINVAL;
4727
5bf9a1ee
PP
4728 if (cnt > TRACE_BUF_SIZE)
4729 cnt = TRACE_BUF_SIZE;
4730
d696b58c
SR
4731 /*
4732 * Userspace is injecting traces into the kernel trace buffer.
4733 * We want to be as non intrusive as possible.
4734 * To do so, we do not want to allocate any special buffers
4735 * or take any locks, but instead write the userspace data
4736 * straight into the ring buffer.
4737 *
4738 * First we need to pin the userspace buffer into memory,
4739 * which, most likely it is, because it just referenced it.
4740 * But there's no guarantee that it is. By using get_user_pages_fast()
4741 * and kmap_atomic/kunmap_atomic() we can get access to the
4742 * pages directly. We then write the data directly into the
4743 * ring buffer.
4744 */
4745 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
5bf9a1ee 4746
d696b58c
SR
4747 /* check if we cross pages */
4748 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4749 nr_pages = 2;
4750
4751 offset = addr & (PAGE_SIZE - 1);
4752 addr &= PAGE_MASK;
4753
4754 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4755 if (ret < nr_pages) {
4756 while (--ret >= 0)
4757 put_page(pages[ret]);
4758 written = -EFAULT;
4759 goto out;
5bf9a1ee 4760 }
d696b58c 4761
6edb2a8a
SR
4762 for (i = 0; i < nr_pages; i++)
4763 map_page[i] = kmap_atomic(pages[i]);
d696b58c
SR
4764
4765 local_save_flags(irq_flags);
4766 size = sizeof(*entry) + cnt + 2; /* possible \n added */
b7f15519 4767 buffer = tr->trace_buffer.buffer;
d696b58c
SR
4768 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4769 irq_flags, preempt_count());
4770 if (!event) {
4771 /* Ring buffer disabled, return as if not open for write */
4772 written = -EBADF;
4773 goto out_unlock;
5bf9a1ee 4774 }
d696b58c
SR
4775
4776 entry = ring_buffer_event_data(event);
4777 entry->ip = _THIS_IP_;
4778
4779 if (nr_pages == 2) {
4780 len = PAGE_SIZE - offset;
6edb2a8a
SR
4781 memcpy(&entry->buf, map_page[0] + offset, len);
4782 memcpy(&entry->buf[len], map_page[1], cnt - len);
c13d2f7c 4783 } else
6edb2a8a 4784 memcpy(&entry->buf, map_page[0] + offset, cnt);
5bf9a1ee 4785
d696b58c
SR
4786 if (entry->buf[cnt - 1] != '\n') {
4787 entry->buf[cnt] = '\n';
4788 entry->buf[cnt + 1] = '\0';
4789 } else
4790 entry->buf[cnt] = '\0';
4791
7ffbd48d 4792 __buffer_unlock_commit(buffer, event);
5bf9a1ee 4793
d696b58c 4794 written = cnt;
5bf9a1ee 4795
d696b58c 4796 *fpos += written;
1aa54bca 4797
d696b58c 4798 out_unlock:
0d998434 4799 for (i = nr_pages - 1; i >= 0; i--) {
6edb2a8a
SR
4800 kunmap_atomic(map_page[i]);
4801 put_page(pages[i]);
4802 }
d696b58c 4803 out:
1aa54bca 4804 return written;
5bf9a1ee
PP
4805}
4806
13f16d20 4807static int tracing_clock_show(struct seq_file *m, void *v)
5079f326 4808{
2b6080f2 4809 struct trace_array *tr = m->private;
5079f326
Z
4810 int i;
4811
4812 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
13f16d20 4813 seq_printf(m,
5079f326 4814 "%s%s%s%s", i ? " " : "",
2b6080f2
SR
4815 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4816 i == tr->clock_id ? "]" : "");
13f16d20 4817 seq_putc(m, '\n');
5079f326 4818
13f16d20 4819 return 0;
5079f326
Z
4820}
4821
4822static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4823 size_t cnt, loff_t *fpos)
4824{
2b6080f2
SR
4825 struct seq_file *m = filp->private_data;
4826 struct trace_array *tr = m->private;
5079f326
Z
4827 char buf[64];
4828 const char *clockstr;
4829 int i;
4830
4831 if (cnt >= sizeof(buf))
4832 return -EINVAL;
4833
4834 if (copy_from_user(&buf, ubuf, cnt))
4835 return -EFAULT;
4836
4837 buf[cnt] = 0;
4838
4839 clockstr = strstrip(buf);
4840
4841 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4842 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4843 break;
4844 }
4845 if (i == ARRAY_SIZE(trace_clocks))
4846 return -EINVAL;
4847
5079f326
Z
4848 mutex_lock(&trace_types_lock);
4849
2b6080f2
SR
4850 tr->clock_id = i;
4851
12883efb 4852 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
5079f326 4853
60303ed3
DS
4854 /*
4855 * New clock may not be consistent with the previous clock.
4856 * Reset the buffer so that it doesn't have incomparable timestamps.
4857 */
e88512e7 4858 tracing_reset_online_cpus(&tr->trace_buffer);
12883efb
SRRH
4859
4860#ifdef CONFIG_TRACER_MAX_TRACE
ec8c69ea 4861 if (tr->max_buffer.buffer)
12883efb 4862 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
e88512e7 4863 tracing_reset_online_cpus(&tr->max_buffer);
12883efb 4864#endif
60303ed3 4865
5079f326
Z
4866 mutex_unlock(&trace_types_lock);
4867
4868 *fpos += cnt;
4869
4870 return cnt;
4871}
4872
13f16d20
LZ
4873static int tracing_clock_open(struct inode *inode, struct file *file)
4874{
6492334c
SRRH
4875 struct trace_array *tr = inode->i_private;
4876 int ret;
4877
13f16d20
LZ
4878 if (tracing_disabled)
4879 return -ENODEV;
2b6080f2 4880
6492334c
SRRH
4881 if (trace_array_get(tr))
4882 return -ENODEV;
4883
4884 ret = single_open(file, tracing_clock_show, inode->i_private);
4885 if (ret < 0)
4886 trace_array_put(tr);
4887
4888 return ret;
13f16d20
LZ
4889}
4890
6de58e62
SRRH
4891struct ftrace_buffer_info {
4892 struct trace_iterator iter;
4893 void *spare;
4894 unsigned int read;
4895};
4896
debdd57f
HT
4897#ifdef CONFIG_TRACER_SNAPSHOT
4898static int tracing_snapshot_open(struct inode *inode, struct file *file)
4899{
7272711a 4900 struct trace_array *tr = inode->i_private;
debdd57f 4901 struct trace_iterator *iter;
2b6080f2 4902 struct seq_file *m;
debdd57f
HT
4903 int ret = 0;
4904
59d8f488
SRRH
4905 if (trace_array_get(tr) < 0)
4906 return -ENODEV;
4907
debdd57f 4908 if (file->f_mode & FMODE_READ) {
7272711a 4909 iter = __tracing_open(inode, file, true);
debdd57f
HT
4910 if (IS_ERR(iter))
4911 ret = PTR_ERR(iter);
2b6080f2
SR
4912 } else {
4913 /* Writes still need the seq_file to hold the private data */
e45ccd09 4914 ret = -ENOMEM;
2b6080f2
SR
4915 m = kzalloc(sizeof(*m), GFP_KERNEL);
4916 if (!m)
e45ccd09 4917 goto out;
2b6080f2
SR
4918 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4919 if (!iter) {
4920 kfree(m);
e45ccd09 4921 goto out;
2b6080f2 4922 }
e45ccd09
AL
4923 ret = 0;
4924
59d8f488 4925 iter->tr = tr;
7272711a
ON
4926 iter->trace_buffer = &tr->max_buffer;
4927 iter->cpu_file = tracing_get_cpu(inode);
2b6080f2
SR
4928 m->private = iter;
4929 file->private_data = m;
debdd57f 4930 }
e45ccd09 4931out:
59d8f488
SRRH
4932 if (ret < 0)
4933 trace_array_put(tr);
4934
debdd57f
HT
4935 return ret;
4936}
4937
4938static ssize_t
4939tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4940 loff_t *ppos)
4941{
2b6080f2
SR
4942 struct seq_file *m = filp->private_data;
4943 struct trace_iterator *iter = m->private;
4944 struct trace_array *tr = iter->tr;
debdd57f
HT
4945 unsigned long val;
4946 int ret;
4947
4948 ret = tracing_update_buffers();
4949 if (ret < 0)
4950 return ret;
4951
4952 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4953 if (ret)
4954 return ret;
4955
4956 mutex_lock(&trace_types_lock);
4957
2b6080f2 4958 if (tr->current_trace->use_max_tr) {
debdd57f
HT
4959 ret = -EBUSY;
4960 goto out;
4961 }
4962
4963 switch (val) {
4964 case 0:
f1affcaa
SRRH
4965 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4966 ret = -EINVAL;
4967 break;
debdd57f 4968 }
3209cff4
SRRH
4969 if (tr->allocated_snapshot)
4970 free_snapshot(tr);
debdd57f
HT
4971 break;
4972 case 1:
f1affcaa
SRRH
4973/* Only allow per-cpu swap if the ring buffer supports it */
4974#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4975 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4976 ret = -EINVAL;
4977 break;
4978 }
4979#endif
45ad21ca 4980 if (!tr->allocated_snapshot) {
3209cff4 4981 ret = alloc_snapshot(tr);
debdd57f
HT
4982 if (ret < 0)
4983 break;
debdd57f 4984 }
debdd57f
HT
4985 local_irq_disable();
4986 /* Now, we're going to swap */
f1affcaa 4987 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
ce9bae55 4988 update_max_tr(tr, current, smp_processor_id());
f1affcaa 4989 else
ce9bae55 4990 update_max_tr_single(tr, current, iter->cpu_file);
debdd57f
HT
4991 local_irq_enable();
4992 break;
4993 default:
45ad21ca 4994 if (tr->allocated_snapshot) {
f1affcaa
SRRH
4995 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4996 tracing_reset_online_cpus(&tr->max_buffer);
4997 else
4998 tracing_reset(&tr->max_buffer, iter->cpu_file);
4999 }
debdd57f
HT
5000 break;
5001 }
5002
5003 if (ret >= 0) {
5004 *ppos += cnt;
5005 ret = cnt;
5006 }
5007out:
5008 mutex_unlock(&trace_types_lock);
5009 return ret;
5010}
2b6080f2
SR
5011
5012static int tracing_snapshot_release(struct inode *inode, struct file *file)
5013{
5014 struct seq_file *m = file->private_data;
59d8f488
SRRH
5015 int ret;
5016
5017 ret = tracing_release(inode, file);
2b6080f2
SR
5018
5019 if (file->f_mode & FMODE_READ)
59d8f488 5020 return ret;
2b6080f2
SR
5021
5022 /* If write only, the seq_file is just a stub */
5023 if (m)
5024 kfree(m->private);
5025 kfree(m);
5026
5027 return 0;
5028}
5029
6de58e62
SRRH
5030static int tracing_buffers_open(struct inode *inode, struct file *filp);
5031static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
5032 size_t count, loff_t *ppos);
5033static int tracing_buffers_release(struct inode *inode, struct file *file);
5034static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5035 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
5036
5037static int snapshot_raw_open(struct inode *inode, struct file *filp)
5038{
5039 struct ftrace_buffer_info *info;
5040 int ret;
5041
5042 ret = tracing_buffers_open(inode, filp);
5043 if (ret < 0)
5044 return ret;
5045
5046 info = filp->private_data;
5047
5048 if (info->iter.trace->use_max_tr) {
5049 tracing_buffers_release(inode, filp);
5050 return -EBUSY;
5051 }
5052
5053 info->iter.snapshot = true;
5054 info->iter.trace_buffer = &info->iter.tr->max_buffer;
5055
5056 return ret;
5057}
5058
debdd57f
HT
5059#endif /* CONFIG_TRACER_SNAPSHOT */
5060
5061
5e2336a0 5062static const struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
5063 .open = tracing_open_generic,
5064 .read = tracing_max_lat_read,
5065 .write = tracing_max_lat_write,
b444786f 5066 .llseek = generic_file_llseek,
bc0c38d1
SR
5067};
5068
5e2336a0 5069static const struct file_operations set_tracer_fops = {
4bf39a94
IM
5070 .open = tracing_open_generic,
5071 .read = tracing_set_trace_read,
5072 .write = tracing_set_trace_write,
b444786f 5073 .llseek = generic_file_llseek,
bc0c38d1
SR
5074};
5075
5e2336a0 5076static const struct file_operations tracing_pipe_fops = {
4bf39a94 5077 .open = tracing_open_pipe,
2a2cc8f7 5078 .poll = tracing_poll_pipe,
4bf39a94 5079 .read = tracing_read_pipe,
3c56819b 5080 .splice_read = tracing_splice_read_pipe,
4bf39a94 5081 .release = tracing_release_pipe,
b444786f 5082 .llseek = no_llseek,
b3806b43
SR
5083};
5084
5e2336a0 5085static const struct file_operations tracing_entries_fops = {
e5dd03c4 5086 .open = tracing_open_generic_tr,
a98a3c3f
SR
5087 .read = tracing_entries_read,
5088 .write = tracing_entries_write,
b444786f 5089 .llseek = generic_file_llseek,
e5dd03c4 5090 .release = tracing_release_generic_tr,
a98a3c3f
SR
5091};
5092
f81ab074 5093static const struct file_operations tracing_total_entries_fops = {
6492334c 5094 .open = tracing_open_generic_tr,
f81ab074
VN
5095 .read = tracing_total_entries_read,
5096 .llseek = generic_file_llseek,
6492334c 5097 .release = tracing_release_generic_tr,
f81ab074
VN
5098};
5099
4f271a2a 5100static const struct file_operations tracing_free_buffer_fops = {
6492334c 5101 .open = tracing_open_generic_tr,
4f271a2a
VN
5102 .write = tracing_free_buffer_write,
5103 .release = tracing_free_buffer_release,
5104};
5105
5e2336a0 5106static const struct file_operations tracing_mark_fops = {
6492334c 5107 .open = tracing_open_generic_tr,
5bf9a1ee 5108 .write = tracing_mark_write,
b444786f 5109 .llseek = generic_file_llseek,
6492334c 5110 .release = tracing_release_generic_tr,
5bf9a1ee
PP
5111};
5112
5079f326 5113static const struct file_operations trace_clock_fops = {
13f16d20
LZ
5114 .open = tracing_clock_open,
5115 .read = seq_read,
5116 .llseek = seq_lseek,
6492334c 5117 .release = tracing_single_release_tr,
5079f326
Z
5118 .write = tracing_clock_write,
5119};
5120
debdd57f
HT
5121#ifdef CONFIG_TRACER_SNAPSHOT
5122static const struct file_operations snapshot_fops = {
5123 .open = tracing_snapshot_open,
5124 .read = seq_read,
5125 .write = tracing_snapshot_write,
5126 .llseek = tracing_seek,
2b6080f2 5127 .release = tracing_snapshot_release,
debdd57f 5128};
debdd57f 5129
6de58e62
SRRH
5130static const struct file_operations snapshot_raw_fops = {
5131 .open = snapshot_raw_open,
5132 .read = tracing_buffers_read,
5133 .release = tracing_buffers_release,
5134 .splice_read = tracing_buffers_splice_read,
5135 .llseek = no_llseek,
2cadf913
SR
5136};
5137
6de58e62
SRRH
5138#endif /* CONFIG_TRACER_SNAPSHOT */
5139
2cadf913
SR
5140static int tracing_buffers_open(struct inode *inode, struct file *filp)
5141{
92b6279e 5142 struct trace_array *tr = inode->i_private;
2cadf913 5143 struct ftrace_buffer_info *info;
6492334c 5144 int ret;
2cadf913
SR
5145
5146 if (tracing_disabled)
5147 return -ENODEV;
5148
6492334c
SRRH
5149 if (trace_array_get(tr) < 0)
5150 return -ENODEV;
5151
2cadf913 5152 info = kzalloc(sizeof(*info), GFP_KERNEL);
6492334c
SRRH
5153 if (!info) {
5154 trace_array_put(tr);
2cadf913 5155 return -ENOMEM;
6492334c 5156 }
2cadf913 5157
a695cb58
SRRH
5158 mutex_lock(&trace_types_lock);
5159
cc60cdc9 5160 info->iter.tr = tr;
92b6279e 5161 info->iter.cpu_file = tracing_get_cpu(inode);
b627344f 5162 info->iter.trace = tr->current_trace;
12883efb 5163 info->iter.trace_buffer = &tr->trace_buffer;
cc60cdc9 5164 info->spare = NULL;
2cadf913 5165 /* Force reading ring buffer for first read */
cc60cdc9 5166 info->read = (unsigned int)-1;
2cadf913
SR
5167
5168 filp->private_data = info;
5169
a695cb58
SRRH
5170 mutex_unlock(&trace_types_lock);
5171
6492334c
SRRH
5172 ret = nonseekable_open(inode, filp);
5173 if (ret < 0)
5174 trace_array_put(tr);
5175
5176 return ret;
2cadf913
SR
5177}
5178
cc60cdc9
SR
5179static unsigned int
5180tracing_buffers_poll(struct file *filp, poll_table *poll_table)
5181{
5182 struct ftrace_buffer_info *info = filp->private_data;
5183 struct trace_iterator *iter = &info->iter;
5184
5185 return trace_poll(iter, filp, poll_table);
5186}
5187
2cadf913
SR
5188static ssize_t
5189tracing_buffers_read(struct file *filp, char __user *ubuf,
5190 size_t count, loff_t *ppos)
5191{
5192 struct ftrace_buffer_info *info = filp->private_data;
cc60cdc9 5193 struct trace_iterator *iter = &info->iter;
2cadf913 5194 ssize_t ret;
6de58e62 5195 ssize_t size;
2cadf913 5196
2dc5d12b
SR
5197 if (!count)
5198 return 0;
5199
6de58e62
SRRH
5200 mutex_lock(&trace_types_lock);
5201
5202#ifdef CONFIG_TRACER_MAX_TRACE
5203 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5204 size = -EBUSY;
5205 goto out_unlock;
5206 }
5207#endif
5208
ddd538f3 5209 if (!info->spare)
12883efb
SRRH
5210 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
5211 iter->cpu_file);
6de58e62 5212 size = -ENOMEM;
ddd538f3 5213 if (!info->spare)
6de58e62 5214 goto out_unlock;
ddd538f3 5215
2cadf913
SR
5216 /* Do we have previous read data to read? */
5217 if (info->read < PAGE_SIZE)
5218 goto read;
5219
b627344f 5220 again:
cc60cdc9 5221 trace_access_lock(iter->cpu_file);
12883efb 5222 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
2cadf913
SR
5223 &info->spare,
5224 count,
cc60cdc9
SR
5225 iter->cpu_file, 0);
5226 trace_access_unlock(iter->cpu_file);
2cadf913 5227
b627344f
SR
5228 if (ret < 0) {
5229 if (trace_empty(iter)) {
6de58e62
SRRH
5230 if ((filp->f_flags & O_NONBLOCK)) {
5231 size = -EAGAIN;
5232 goto out_unlock;
5233 }
5234 mutex_unlock(&trace_types_lock);
561237e4 5235 ret = iter->trace->wait_pipe(iter);
6de58e62 5236 mutex_lock(&trace_types_lock);
561237e4
SRRH
5237 if (ret) {
5238 size = ret;
5239 goto out_unlock;
5240 }
6de58e62
SRRH
5241 if (signal_pending(current)) {
5242 size = -EINTR;
5243 goto out_unlock;
5244 }
b627344f
SR
5245 goto again;
5246 }
6de58e62
SRRH
5247 size = 0;
5248 goto out_unlock;
b627344f 5249 }
436fc280 5250
436fc280 5251 info->read = 0;
b627344f 5252 read:
2cadf913
SR
5253 size = PAGE_SIZE - info->read;
5254 if (size > count)
5255 size = count;
5256
5257 ret = copy_to_user(ubuf, info->spare + info->read, size);
6de58e62
SRRH
5258 if (ret == size) {
5259 size = -EFAULT;
5260 goto out_unlock;
5261 }
2dc5d12b
SR
5262 size -= ret;
5263
2cadf913
SR
5264 *ppos += size;
5265 info->read += size;
5266
6de58e62
SRRH
5267 out_unlock:
5268 mutex_unlock(&trace_types_lock);
5269
2cadf913
SR
5270 return size;
5271}
5272
5273static int tracing_buffers_release(struct inode *inode, struct file *file)
5274{
5275 struct ftrace_buffer_info *info = file->private_data;
cc60cdc9 5276 struct trace_iterator *iter = &info->iter;
2cadf913 5277
a695cb58
SRRH
5278 mutex_lock(&trace_types_lock);
5279
59d8f488 5280 __trace_array_put(iter->tr);
2cadf913 5281
ddd538f3 5282 if (info->spare)
12883efb 5283 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
2cadf913
SR
5284 kfree(info);
5285
a695cb58
SRRH
5286 mutex_unlock(&trace_types_lock);
5287
2cadf913
SR
5288 return 0;
5289}
5290
5291struct buffer_ref {
5292 struct ring_buffer *buffer;
5293 void *page;
5294 int ref;
5295};
5296
5297static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
5298 struct pipe_buffer *buf)
5299{
5300 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5301
5302 if (--ref->ref)
5303 return;
5304
5305 ring_buffer_free_read_page(ref->buffer, ref->page);
5306 kfree(ref);
5307 buf->private = 0;
5308}
5309
2cadf913
SR
5310static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
5311 struct pipe_buffer *buf)
5312{
5313 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5314
5315 ref->ref++;
5316}
5317
5318/* Pipe buffer operations for a buffer. */
28dfef8f 5319static const struct pipe_buf_operations buffer_pipe_buf_ops = {
2cadf913
SR
5320 .can_merge = 0,
5321 .map = generic_pipe_buf_map,
5322 .unmap = generic_pipe_buf_unmap,
5323 .confirm = generic_pipe_buf_confirm,
5324 .release = buffer_pipe_buf_release,
d55cb6cf 5325 .steal = generic_pipe_buf_steal,
2cadf913
SR
5326 .get = buffer_pipe_buf_get,
5327};
5328
5329/*
5330 * Callback from splice_to_pipe(), if we need to release some pages
5331 * at the end of the spd in case we error'ed out in filling the pipe.
5332 */
5333static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
5334{
5335 struct buffer_ref *ref =
5336 (struct buffer_ref *)spd->partial[i].private;
5337
5338 if (--ref->ref)
5339 return;
5340
5341 ring_buffer_free_read_page(ref->buffer, ref->page);
5342 kfree(ref);
5343 spd->partial[i].private = 0;
5344}
5345
5346static ssize_t
5347tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5348 struct pipe_inode_info *pipe, size_t len,
5349 unsigned int flags)
5350{
5351 struct ftrace_buffer_info *info = file->private_data;
cc60cdc9 5352 struct trace_iterator *iter = &info->iter;
35f3d14d
JA
5353 struct partial_page partial_def[PIPE_DEF_BUFFERS];
5354 struct page *pages_def[PIPE_DEF_BUFFERS];
2cadf913 5355 struct splice_pipe_desc spd = {
35f3d14d
JA
5356 .pages = pages_def,
5357 .partial = partial_def,
047fe360 5358 .nr_pages_max = PIPE_DEF_BUFFERS,
2cadf913
SR
5359 .flags = flags,
5360 .ops = &buffer_pipe_buf_ops,
5361 .spd_release = buffer_spd_release,
5362 };
5363 struct buffer_ref *ref;
93459c6c 5364 int entries, size, i;
6de58e62 5365 ssize_t ret;
2cadf913 5366
6de58e62
SRRH
5367 mutex_lock(&trace_types_lock);
5368
5369#ifdef CONFIG_TRACER_MAX_TRACE
5370 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5371 ret = -EBUSY;
5372 goto out;
5373 }
5374#endif
5375
93cfb3c9 5376 if (*ppos & (PAGE_SIZE - 1)) {
35f3d14d
JA
5377 ret = -EINVAL;
5378 goto out;
93cfb3c9
LJ
5379 }
5380
5381 if (len & (PAGE_SIZE - 1)) {
35f3d14d
JA
5382 if (len < PAGE_SIZE) {
5383 ret = -EINVAL;
5384 goto out;
5385 }
93cfb3c9
LJ
5386 len &= PAGE_MASK;
5387 }
5388
7bb0f7e6
AV
5389 if (splice_grow_spd(pipe, &spd)) {
5390 ret = -ENOMEM;
5391 goto out;
5392 }
5393
cc60cdc9
SR
5394 again:
5395 trace_access_lock(iter->cpu_file);
12883efb 5396 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
93459c6c 5397
35f3d14d 5398 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
2cadf913
SR
5399 struct page *page;
5400 int r;
5401
5402 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5403 if (!ref)
5404 break;
5405
7267fa68 5406 ref->ref = 1;
12883efb 5407 ref->buffer = iter->trace_buffer->buffer;
cc60cdc9 5408 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
2cadf913
SR
5409 if (!ref->page) {
5410 kfree(ref);
5411 break;
5412 }
5413
5414 r = ring_buffer_read_page(ref->buffer, &ref->page,
cc60cdc9 5415 len, iter->cpu_file, 1);
2cadf913 5416 if (r < 0) {
7ea59064 5417 ring_buffer_free_read_page(ref->buffer, ref->page);
2cadf913
SR
5418 kfree(ref);
5419 break;
5420 }
5421
5422 /*
5423 * zero out any left over data, this is going to
5424 * user land.
5425 */
5426 size = ring_buffer_page_len(ref->page);
5427 if (size < PAGE_SIZE)
5428 memset(ref->page + size, 0, PAGE_SIZE - size);
5429
5430 page = virt_to_page(ref->page);
5431
5432 spd.pages[i] = page;
5433 spd.partial[i].len = PAGE_SIZE;
5434 spd.partial[i].offset = 0;
5435 spd.partial[i].private = (unsigned long)ref;
5436 spd.nr_pages++;
93cfb3c9 5437 *ppos += PAGE_SIZE;
93459c6c 5438
12883efb 5439 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
2cadf913
SR
5440 }
5441
cc60cdc9 5442 trace_access_unlock(iter->cpu_file);
2cadf913
SR
5443 spd.nr_pages = i;
5444
5445 /* did we read anything? */
5446 if (!spd.nr_pages) {
cc60cdc9 5447 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
2cadf913 5448 ret = -EAGAIN;
7bb0f7e6 5449 goto out_shrink;
cc60cdc9 5450 }
6de58e62 5451 mutex_unlock(&trace_types_lock);
561237e4 5452 ret = iter->trace->wait_pipe(iter);
6de58e62 5453 mutex_lock(&trace_types_lock);
561237e4 5454 if (ret)
7bb0f7e6 5455 goto out_shrink;
cc60cdc9
SR
5456 if (signal_pending(current)) {
5457 ret = -EINTR;
7bb0f7e6 5458 goto out_shrink;
cc60cdc9
SR
5459 }
5460 goto again;
2cadf913
SR
5461 }
5462
5463 ret = splice_to_pipe(pipe, &spd);
7bb0f7e6 5464out_shrink:
047fe360 5465 splice_shrink_spd(&spd);
35f3d14d 5466out:
6de58e62
SRRH
5467 mutex_unlock(&trace_types_lock);
5468
2cadf913
SR
5469 return ret;
5470}
5471
5472static const struct file_operations tracing_buffers_fops = {
5473 .open = tracing_buffers_open,
5474 .read = tracing_buffers_read,
cc60cdc9 5475 .poll = tracing_buffers_poll,
2cadf913
SR
5476 .release = tracing_buffers_release,
5477 .splice_read = tracing_buffers_splice_read,
5478 .llseek = no_llseek,
5479};
5480
c8d77183
SR
5481static ssize_t
5482tracing_stats_read(struct file *filp, char __user *ubuf,
5483 size_t count, loff_t *ppos)
5484{
6dafb0f1
ON
5485 struct inode *inode = file_inode(filp);
5486 struct trace_array *tr = inode->i_private;
12883efb 5487 struct trace_buffer *trace_buf = &tr->trace_buffer;
6dafb0f1 5488 int cpu = tracing_get_cpu(inode);
c8d77183
SR
5489 struct trace_seq *s;
5490 unsigned long cnt;
c64e148a
VN
5491 unsigned long long t;
5492 unsigned long usec_rem;
c8d77183 5493
e4f2d10f 5494 s = kmalloc(sizeof(*s), GFP_KERNEL);
c8d77183 5495 if (!s)
a646365c 5496 return -ENOMEM;
c8d77183
SR
5497
5498 trace_seq_init(s);
5499
12883efb 5500 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
c8d77183
SR
5501 trace_seq_printf(s, "entries: %ld\n", cnt);
5502
12883efb 5503 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
c8d77183
SR
5504 trace_seq_printf(s, "overrun: %ld\n", cnt);
5505
12883efb 5506 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
c8d77183
SR
5507 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5508
12883efb 5509 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
c64e148a
VN
5510 trace_seq_printf(s, "bytes: %ld\n", cnt);
5511
58e8eedf 5512 if (trace_clocks[tr->clock_id].in_ns) {
11043d8b 5513 /* local or global for trace_clock */
12883efb 5514 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
11043d8b
YY
5515 usec_rem = do_div(t, USEC_PER_SEC);
5516 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5517 t, usec_rem);
5518
12883efb 5519 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
11043d8b
YY
5520 usec_rem = do_div(t, USEC_PER_SEC);
5521 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5522 } else {
5523 /* counter or tsc mode for trace_clock */
5524 trace_seq_printf(s, "oldest event ts: %llu\n",
12883efb 5525 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
c64e148a 5526
11043d8b 5527 trace_seq_printf(s, "now ts: %llu\n",
12883efb 5528 ring_buffer_time_stamp(trace_buf->buffer, cpu));
11043d8b 5529 }
c64e148a 5530
12883efb 5531 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
884bfe89
SP
5532 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5533
12883efb 5534 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
ad964704
SRRH
5535 trace_seq_printf(s, "read events: %ld\n", cnt);
5536
c8d77183
SR
5537 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5538
5539 kfree(s);
5540
5541 return count;
5542}
5543
5544static const struct file_operations tracing_stats_fops = {
6dafb0f1 5545 .open = tracing_open_generic_tr,
c8d77183 5546 .read = tracing_stats_read,
b444786f 5547 .llseek = generic_file_llseek,
6dafb0f1 5548 .release = tracing_release_generic_tr,
c8d77183
SR
5549};
5550
bc0c38d1
SR
5551#ifdef CONFIG_DYNAMIC_FTRACE
5552
b807c3d0
SR
5553int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5554{
5555 return 0;
5556}
5557
bc0c38d1 5558static ssize_t
b807c3d0 5559tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
5560 size_t cnt, loff_t *ppos)
5561{
a26a2a27
SR
5562 static char ftrace_dyn_info_buffer[1024];
5563 static DEFINE_MUTEX(dyn_info_mutex);
bc0c38d1 5564 unsigned long *p = filp->private_data;
b807c3d0 5565 char *buf = ftrace_dyn_info_buffer;
a26a2a27 5566 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
bc0c38d1
SR
5567 int r;
5568
b807c3d0
SR
5569 mutex_lock(&dyn_info_mutex);
5570 r = sprintf(buf, "%ld ", *p);
4bf39a94 5571
a26a2a27 5572 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
b807c3d0
SR
5573 buf[r++] = '\n';
5574
5575 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5576
5577 mutex_unlock(&dyn_info_mutex);
5578
5579 return r;
bc0c38d1
SR
5580}
5581
5e2336a0 5582static const struct file_operations tracing_dyn_info_fops = {
4bf39a94 5583 .open = tracing_open_generic,
b807c3d0 5584 .read = tracing_read_dyn_info,
b444786f 5585 .llseek = generic_file_llseek,
bc0c38d1 5586};
77fd5c15 5587#endif /* CONFIG_DYNAMIC_FTRACE */
bc0c38d1 5588
77fd5c15
SRRH
5589#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5590static void
5591ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5592{
5593 tracing_snapshot();
5594}
bc0c38d1 5595
77fd5c15
SRRH
5596static void
5597ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
bc0c38d1 5598{
77fd5c15
SRRH
5599 unsigned long *count = (long *)data;
5600
5601 if (!*count)
5602 return;
bc0c38d1 5603
77fd5c15
SRRH
5604 if (*count != -1)
5605 (*count)--;
5606
5607 tracing_snapshot();
5608}
5609
5610static int
5611ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5612 struct ftrace_probe_ops *ops, void *data)
5613{
5614 long count = (long)data;
5615
5616 seq_printf(m, "%ps:", (void *)ip);
5617
5618 seq_printf(m, "snapshot");
5619
5620 if (count == -1)
5621 seq_printf(m, ":unlimited\n");
5622 else
5623 seq_printf(m, ":count=%ld\n", count);
5624
5625 return 0;
5626}
5627
5628static struct ftrace_probe_ops snapshot_probe_ops = {
5629 .func = ftrace_snapshot,
5630 .print = ftrace_snapshot_print,
5631};
5632
5633static struct ftrace_probe_ops snapshot_count_probe_ops = {
5634 .func = ftrace_count_snapshot,
5635 .print = ftrace_snapshot_print,
5636};
5637
5638static int
5639ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5640 char *glob, char *cmd, char *param, int enable)
5641{
5642 struct ftrace_probe_ops *ops;
5643 void *count = (void *)-1;
5644 char *number;
5645 int ret;
5646
5647 /* hash funcs only work with set_ftrace_filter */
5648 if (!enable)
5649 return -EINVAL;
5650
5651 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5652
5653 if (glob[0] == '!') {
5654 unregister_ftrace_function_probe_func(glob+1, ops);
5655 return 0;
5656 }
5657
5658 if (!param)
5659 goto out_reg;
5660
5661 number = strsep(&param, ":");
5662
5663 if (!strlen(number))
5664 goto out_reg;
5665
5666 /*
5667 * We use the callback data field (which is a pointer)
5668 * as our counter.
5669 */
5670 ret = kstrtoul(number, 0, (unsigned long *)&count);
5671 if (ret)
5672 return ret;
5673
5674 out_reg:
95948b1e
SRV
5675 ret = alloc_snapshot(&global_trace);
5676 if (ret < 0)
5677 goto out;
77fd5c15 5678
95948b1e 5679 ret = register_ftrace_function_probe(glob, ops, count);
77fd5c15 5680
95948b1e 5681 out:
77fd5c15
SRRH
5682 return ret < 0 ? ret : 0;
5683}
5684
5685static struct ftrace_func_command ftrace_snapshot_cmd = {
5686 .name = "snapshot",
5687 .func = ftrace_trace_snapshot_callback,
5688};
5689
5690static int register_snapshot_cmd(void)
5691{
5692 return register_ftrace_command(&ftrace_snapshot_cmd);
5693}
5694#else
5695static inline int register_snapshot_cmd(void) { return 0; }
5696#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
bc0c38d1 5697
2b6080f2 5698struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
bc0c38d1 5699{
2b6080f2
SR
5700 if (tr->dir)
5701 return tr->dir;
bc0c38d1 5702
3e1f60b8
FW
5703 if (!debugfs_initialized())
5704 return NULL;
5705
2b6080f2
SR
5706 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5707 tr->dir = debugfs_create_dir("tracing", NULL);
bc0c38d1 5708
687c878a
J
5709 if (!tr->dir)
5710 pr_warn_once("Could not create debugfs directory 'tracing'\n");
bc0c38d1 5711
2b6080f2 5712 return tr->dir;
bc0c38d1
SR
5713}
5714
2b6080f2
SR
5715struct dentry *tracing_init_dentry(void)
5716{
5717 return tracing_init_dentry_tr(&global_trace);
5718}
b04cc6b1 5719
2b6080f2 5720static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
b04cc6b1 5721{
b04cc6b1
FW
5722 struct dentry *d_tracer;
5723
2b6080f2
SR
5724 if (tr->percpu_dir)
5725 return tr->percpu_dir;
b04cc6b1 5726
2b6080f2 5727 d_tracer = tracing_init_dentry_tr(tr);
b04cc6b1
FW
5728 if (!d_tracer)
5729 return NULL;
5730
2b6080f2 5731 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
b04cc6b1 5732
2b6080f2
SR
5733 WARN_ONCE(!tr->percpu_dir,
5734 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
b04cc6b1 5735
2b6080f2 5736 return tr->percpu_dir;
b04cc6b1
FW
5737}
5738
e2d4dbe0
ON
5739static struct dentry *
5740trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
5741 void *data, long cpu, const struct file_operations *fops)
5742{
5743 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
5744
5745 if (ret) /* See tracing_get_cpu() */
5746 ret->d_inode->i_cdev = (void *)(cpu + 1);
5747 return ret;
5748}
5749
2b6080f2
SR
5750static void
5751tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
b04cc6b1 5752{
2b6080f2 5753 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5452af66 5754 struct dentry *d_cpu;
dd49a38c 5755 char cpu_dir[30]; /* 30 characters should be more than enough */
b04cc6b1 5756
0a3d7ce7
NK
5757 if (!d_percpu)
5758 return;
5759
dd49a38c 5760 snprintf(cpu_dir, 30, "cpu%ld", cpu);
8656e7a2
FW
5761 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5762 if (!d_cpu) {
5763 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5764 return;
5765 }
b04cc6b1 5766
8656e7a2 5767 /* per cpu trace_pipe */
e2d4dbe0 5768 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
23b625fa 5769 tr, cpu, &tracing_pipe_fops);
b04cc6b1
FW
5770
5771 /* per cpu trace */
e2d4dbe0 5772 trace_create_cpu_file("trace", 0644, d_cpu,
7272711a 5773 tr, cpu, &tracing_fops);
7f96f93f 5774
e2d4dbe0 5775 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
92b6279e 5776 tr, cpu, &tracing_buffers_fops);
7f96f93f 5777
e2d4dbe0 5778 trace_create_cpu_file("stats", 0444, d_cpu,
6dafb0f1 5779 tr, cpu, &tracing_stats_fops);
438ced17 5780
e2d4dbe0 5781 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
e5dd03c4 5782 tr, cpu, &tracing_entries_fops);
f1affcaa
SRRH
5783
5784#ifdef CONFIG_TRACER_SNAPSHOT
e2d4dbe0 5785 trace_create_cpu_file("snapshot", 0644, d_cpu,
7272711a 5786 tr, cpu, &snapshot_fops);
6de58e62 5787
e2d4dbe0 5788 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
92b6279e 5789 tr, cpu, &snapshot_raw_fops);
f1affcaa 5790#endif
b04cc6b1
FW
5791}
5792
60a11774
SR
5793#ifdef CONFIG_FTRACE_SELFTEST
5794/* Let selftest have access to static functions in this file */
5795#include "trace_selftest.c"
5796#endif
5797
577b785f
SR
5798struct trace_option_dentry {
5799 struct tracer_opt *opt;
5800 struct tracer_flags *flags;
2b6080f2 5801 struct trace_array *tr;
577b785f
SR
5802 struct dentry *entry;
5803};
5804
5805static ssize_t
5806trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5807 loff_t *ppos)
5808{
5809 struct trace_option_dentry *topt = filp->private_data;
5810 char *buf;
5811
5812 if (topt->flags->val & topt->opt->bit)
5813 buf = "1\n";
5814 else
5815 buf = "0\n";
5816
5817 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5818}
5819
5820static ssize_t
5821trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5822 loff_t *ppos)
5823{
5824 struct trace_option_dentry *topt = filp->private_data;
5825 unsigned long val;
577b785f
SR
5826 int ret;
5827
22fe9b54
PH
5828 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5829 if (ret)
577b785f
SR
5830 return ret;
5831
8d18eaaf
LZ
5832 if (val != 0 && val != 1)
5833 return -EINVAL;
577b785f 5834
8d18eaaf 5835 if (!!(topt->flags->val & topt->opt->bit) != val) {
577b785f 5836 mutex_lock(&trace_types_lock);
2b6080f2 5837 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
c757bea9 5838 topt->opt, !val);
577b785f
SR
5839 mutex_unlock(&trace_types_lock);
5840 if (ret)
5841 return ret;
577b785f
SR
5842 }
5843
5844 *ppos += cnt;
5845
5846 return cnt;
5847}
5848
5849
5850static const struct file_operations trace_options_fops = {
5851 .open = tracing_open_generic,
5852 .read = trace_options_read,
5853 .write = trace_options_write,
b444786f 5854 .llseek = generic_file_llseek,
577b785f
SR
5855};
5856
a8259075
SR
5857static ssize_t
5858trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5859 loff_t *ppos)
5860{
5861 long index = (long)filp->private_data;
5862 char *buf;
5863
5864 if (trace_flags & (1 << index))
5865 buf = "1\n";
5866 else
5867 buf = "0\n";
5868
5869 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5870}
5871
5872static ssize_t
5873trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5874 loff_t *ppos)
5875{
2b6080f2 5876 struct trace_array *tr = &global_trace;
a8259075 5877 long index = (long)filp->private_data;
a8259075
SR
5878 unsigned long val;
5879 int ret;
5880
22fe9b54
PH
5881 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5882 if (ret)
a8259075
SR
5883 return ret;
5884
f2d84b65 5885 if (val != 0 && val != 1)
a8259075 5886 return -EINVAL;
69d34da2
SRRH
5887
5888 mutex_lock(&trace_types_lock);
2b6080f2 5889 ret = set_tracer_flag(tr, 1 << index, val);
69d34da2 5890 mutex_unlock(&trace_types_lock);
a8259075 5891
613f04a0
SRRH
5892 if (ret < 0)
5893 return ret;
5894
a8259075
SR
5895 *ppos += cnt;
5896
5897 return cnt;
5898}
5899
a8259075
SR
5900static const struct file_operations trace_options_core_fops = {
5901 .open = tracing_open_generic,
5902 .read = trace_options_core_read,
5903 .write = trace_options_core_write,
b444786f 5904 .llseek = generic_file_llseek,
a8259075
SR
5905};
5906
5452af66 5907struct dentry *trace_create_file(const char *name,
f4ae40a6 5908 umode_t mode,
5452af66
FW
5909 struct dentry *parent,
5910 void *data,
5911 const struct file_operations *fops)
5912{
5913 struct dentry *ret;
5914
5915 ret = debugfs_create_file(name, mode, parent, data, fops);
5916 if (!ret)
5917 pr_warning("Could not create debugfs '%s' entry\n", name);
5918
5919 return ret;
5920}
5921
5922
2b6080f2 5923static struct dentry *trace_options_init_dentry(struct trace_array *tr)
a8259075
SR
5924{
5925 struct dentry *d_tracer;
a8259075 5926
2b6080f2
SR
5927 if (tr->options)
5928 return tr->options;
a8259075 5929
2b6080f2 5930 d_tracer = tracing_init_dentry_tr(tr);
a8259075
SR
5931 if (!d_tracer)
5932 return NULL;
5933
2b6080f2
SR
5934 tr->options = debugfs_create_dir("options", d_tracer);
5935 if (!tr->options) {
a8259075
SR
5936 pr_warning("Could not create debugfs directory 'options'\n");
5937 return NULL;
5938 }
5939
2b6080f2 5940 return tr->options;
a8259075
SR
5941}
5942
577b785f 5943static void
2b6080f2
SR
5944create_trace_option_file(struct trace_array *tr,
5945 struct trace_option_dentry *topt,
577b785f
SR
5946 struct tracer_flags *flags,
5947 struct tracer_opt *opt)
5948{
5949 struct dentry *t_options;
577b785f 5950
2b6080f2 5951 t_options = trace_options_init_dentry(tr);
577b785f
SR
5952 if (!t_options)
5953 return;
5954
5955 topt->flags = flags;
5956 topt->opt = opt;
2b6080f2 5957 topt->tr = tr;
577b785f 5958
5452af66 5959 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
577b785f
SR
5960 &trace_options_fops);
5961
577b785f
SR
5962}
5963
5964static struct trace_option_dentry *
2b6080f2 5965create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
577b785f
SR
5966{
5967 struct trace_option_dentry *topts;
5968 struct tracer_flags *flags;
5969 struct tracer_opt *opts;
5970 int cnt;
5971
5972 if (!tracer)
5973 return NULL;
5974
5975 flags = tracer->flags;
5976
5977 if (!flags || !flags->opts)
5978 return NULL;
5979
5980 opts = flags->opts;
5981
5982 for (cnt = 0; opts[cnt].name; cnt++)
5983 ;
5984
0cfe8245 5985 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
577b785f
SR
5986 if (!topts)
5987 return NULL;
5988
5989 for (cnt = 0; opts[cnt].name; cnt++)
2b6080f2 5990 create_trace_option_file(tr, &topts[cnt], flags,
577b785f
SR
5991 &opts[cnt]);
5992
5993 return topts;
5994}
5995
5996static void
5997destroy_trace_option_files(struct trace_option_dentry *topts)
5998{
5999 int cnt;
6000
6001 if (!topts)
6002 return;
6003
6004 for (cnt = 0; topts[cnt].opt; cnt++) {
6005 if (topts[cnt].entry)
6006 debugfs_remove(topts[cnt].entry);
6007 }
6008
6009 kfree(topts);
6010}
6011
a8259075 6012static struct dentry *
2b6080f2
SR
6013create_trace_option_core_file(struct trace_array *tr,
6014 const char *option, long index)
a8259075
SR
6015{
6016 struct dentry *t_options;
a8259075 6017
2b6080f2 6018 t_options = trace_options_init_dentry(tr);
a8259075
SR
6019 if (!t_options)
6020 return NULL;
6021
5452af66 6022 return trace_create_file(option, 0644, t_options, (void *)index,
a8259075 6023 &trace_options_core_fops);
a8259075
SR
6024}
6025
2b6080f2 6026static __init void create_trace_options_dir(struct trace_array *tr)
a8259075
SR
6027{
6028 struct dentry *t_options;
a8259075
SR
6029 int i;
6030
2b6080f2 6031 t_options = trace_options_init_dentry(tr);
a8259075
SR
6032 if (!t_options)
6033 return;
6034
5452af66 6035 for (i = 0; trace_options[i]; i++)
2b6080f2 6036 create_trace_option_core_file(tr, trace_options[i], i);
a8259075
SR
6037}
6038
499e5470
SR
6039static ssize_t
6040rb_simple_read(struct file *filp, char __user *ubuf,
6041 size_t cnt, loff_t *ppos)
6042{
348f0fc2 6043 struct trace_array *tr = filp->private_data;
499e5470
SR
6044 char buf[64];
6045 int r;
6046
2fd821ee 6047 r = tracer_tracing_is_on(tr);
499e5470
SR
6048 r = sprintf(buf, "%d\n", r);
6049
6050 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6051}
6052
6053static ssize_t
6054rb_simple_write(struct file *filp, const char __user *ubuf,
6055 size_t cnt, loff_t *ppos)
6056{
348f0fc2 6057 struct trace_array *tr = filp->private_data;
12883efb 6058 struct ring_buffer *buffer = tr->trace_buffer.buffer;
499e5470
SR
6059 unsigned long val;
6060 int ret;
6061
6062 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6063 if (ret)
6064 return ret;
6065
6066 if (buffer) {
6fa3eb70
S
6067 if(ring_buffer_record_is_on(buffer) ^ val)
6068 printk(KERN_INFO "[ftrace]tracing_on is toggled to %lu\n", val);
2df8f8a6
SR
6069 mutex_lock(&trace_types_lock);
6070 if (val) {
2fd821ee 6071 tracer_tracing_on(tr);
6fa3eb70
S
6072#ifdef CONFIG_MTK_SCHED_TRACERS
6073 trace_tracing_on(val, CALLER_ADDR0);
6074#endif
2b6080f2
SR
6075 if (tr->current_trace->start)
6076 tr->current_trace->start(tr);
2df8f8a6 6077 } else {
6fa3eb70
S
6078#ifdef CONFIG_MTK_SCHED_TRACERS
6079 trace_tracing_on(val, CALLER_ADDR0);
6080#endif
2fd821ee 6081 tracer_tracing_off(tr);
2b6080f2
SR
6082 if (tr->current_trace->stop)
6083 tr->current_trace->stop(tr);
2df8f8a6
SR
6084 }
6085 mutex_unlock(&trace_types_lock);
499e5470
SR
6086 }
6087
6088 (*ppos)++;
6089
6090 return cnt;
6091}
6092
6093static const struct file_operations rb_simple_fops = {
6492334c 6094 .open = tracing_open_generic_tr,
499e5470
SR
6095 .read = rb_simple_read,
6096 .write = rb_simple_write,
6492334c 6097 .release = tracing_release_generic_tr,
499e5470
SR
6098 .llseek = default_llseek,
6099};
6100
6fa3eb70
S
6101#ifdef CONFIG_MTK_KERNEL_MARKER
6102static int mt_kernel_marker_enabled = 1;
6103static ssize_t
6104mt_kernel_marker_enabled_simple_read(struct file *filp, char __user *ubuf,
6105 size_t cnt, loff_t *ppos)
6106{
6107 char buf[64];
6108 int r;
6109
6110 r = sprintf(buf, "%d\n", mt_kernel_marker_enabled);
6111
6112 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6113}
6114static ssize_t
6115mt_kernel_marker_enabled_simple_write(struct file *filp, const char __user *ubuf,
6116 size_t cnt, loff_t *ppos)
6117{
6118 unsigned long val;
6119 int ret;
6120
6121 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6122 if (ret)
6123 return ret;
6124
6125 mt_kernel_marker_enabled = !!val;
6126
6127 (*ppos)++;
6128
6129 return cnt;
6130}
6131static const struct file_operations kernel_marker_simple_fops = {
6132 .open = tracing_open_generic,
6133 .read = mt_kernel_marker_enabled_simple_read,
6134 .write = mt_kernel_marker_enabled_simple_write,
6135 .llseek = default_llseek,
6136};
6137#endif
277ba044
SR
6138struct dentry *trace_instance_dir;
6139
6140static void
6141init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
6142
737223fb
SRRH
6143static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
6144{
6145 int cpu;
6146
6147 for_each_tracing_cpu(cpu) {
6148 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
6149 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
6150 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
6151 }
6152}
6153
55034cd6
SRRH
6154static int
6155allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
277ba044
SR
6156{
6157 enum ring_buffer_flags rb_flags;
737223fb
SRRH
6158
6159 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
6160
fb23eaf4
SRRH
6161 buf->tr = tr;
6162
55034cd6
SRRH
6163 buf->buffer = ring_buffer_alloc(size, rb_flags);
6164 if (!buf->buffer)
6165 return -ENOMEM;
737223fb 6166
55034cd6
SRRH
6167 buf->data = alloc_percpu(struct trace_array_cpu);
6168 if (!buf->data) {
6169 ring_buffer_free(buf->buffer);
6170 return -ENOMEM;
6171 }
737223fb 6172
55034cd6 6173 init_trace_buffers(tr, buf);
737223fb
SRRH
6174
6175 /* Allocate the first page for all buffers */
6176 set_buffer_entries(&tr->trace_buffer,
6177 ring_buffer_size(tr->trace_buffer.buffer, 0));
6178
55034cd6
SRRH
6179 return 0;
6180}
737223fb 6181
55034cd6
SRRH
6182static int allocate_trace_buffers(struct trace_array *tr, int size)
6183{
6184 int ret;
737223fb 6185
55034cd6
SRRH
6186 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
6187 if (ret)
6188 return ret;
737223fb 6189
55034cd6
SRRH
6190#ifdef CONFIG_TRACER_MAX_TRACE
6191 ret = allocate_trace_buffer(tr, &tr->max_buffer,
6192 allocate_snapshot ? size : 1);
6193 if (WARN_ON(ret)) {
737223fb 6194 ring_buffer_free(tr->trace_buffer.buffer);
55034cd6
SRRH
6195 free_percpu(tr->trace_buffer.data);
6196 return -ENOMEM;
6197 }
6198 tr->allocated_snapshot = allocate_snapshot;
737223fb 6199
55034cd6
SRRH
6200 /*
6201 * Only the top level trace array gets its snapshot allocated
6202 * from the kernel command line.
6203 */
6204 allocate_snapshot = false;
737223fb 6205#endif
55034cd6 6206 return 0;
737223fb
SRRH
6207}
6208
6209static int new_instance_create(const char *name)
6210{
277ba044
SR
6211 struct trace_array *tr;
6212 int ret;
277ba044
SR
6213
6214 mutex_lock(&trace_types_lock);
6215
6216 ret = -EEXIST;
6217 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6218 if (tr->name && strcmp(tr->name, name) == 0)
6219 goto out_unlock;
6220 }
6221
6222 ret = -ENOMEM;
6223 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
6224 if (!tr)
6225 goto out_unlock;
6226
6227 tr->name = kstrdup(name, GFP_KERNEL);
6228 if (!tr->name)
6229 goto out_free_tr;
6230
6231 raw_spin_lock_init(&tr->start_lock);
6232
6233 tr->current_trace = &nop_trace;
6234
6235 INIT_LIST_HEAD(&tr->systems);
6236 INIT_LIST_HEAD(&tr->events);
6237
737223fb 6238 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
277ba044
SR
6239 goto out_free_tr;
6240
277ba044
SR
6241 /* Holder for file callbacks */
6242 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6243 tr->trace_cpu.tr = tr;
6244
6245 tr->dir = debugfs_create_dir(name, trace_instance_dir);
6246 if (!tr->dir)
6247 goto out_free_tr;
6248
6249 ret = event_trace_add_tracer(tr->dir, tr);
a33eb1f5
AL
6250 if (ret) {
6251 debugfs_remove_recursive(tr->dir);
277ba044 6252 goto out_free_tr;
a33eb1f5 6253 }
277ba044
SR
6254
6255 init_tracer_debugfs(tr, tr->dir);
6256
6257 list_add(&tr->list, &ftrace_trace_arrays);
6258
6259 mutex_unlock(&trace_types_lock);
6260
6261 return 0;
6262
6263 out_free_tr:
12883efb
SRRH
6264 if (tr->trace_buffer.buffer)
6265 ring_buffer_free(tr->trace_buffer.buffer);
277ba044
SR
6266 kfree(tr->name);
6267 kfree(tr);
6268
6269 out_unlock:
6270 mutex_unlock(&trace_types_lock);
6271
6272 return ret;
6273
6274}
6275
0c8916c3
SR
6276static int instance_delete(const char *name)
6277{
6278 struct trace_array *tr;
6279 int found = 0;
6280 int ret;
6281
6282 mutex_lock(&trace_types_lock);
6283
6284 ret = -ENODEV;
6285 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6286 if (tr->name && strcmp(tr->name, name) == 0) {
6287 found = 1;
6288 break;
6289 }
6290 }
6291 if (!found)
6292 goto out_unlock;
6293
a695cb58
SRRH
6294 ret = -EBUSY;
6295 if (tr->ref)
6296 goto out_unlock;
6297
0c8916c3
SR
6298 list_del(&tr->list);
6299
6300 event_trace_del_tracer(tr);
6301 debugfs_remove_recursive(tr->dir);
12883efb
SRRH
6302 free_percpu(tr->trace_buffer.data);
6303 ring_buffer_free(tr->trace_buffer.buffer);
0c8916c3
SR
6304
6305 kfree(tr->name);
6306 kfree(tr);
6307
6308 ret = 0;
6309
6310 out_unlock:
6311 mutex_unlock(&trace_types_lock);
6312
6313 return ret;
6314}
6315
277ba044
SR
6316static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
6317{
6318 struct dentry *parent;
6319 int ret;
6320
6321 /* Paranoid: Make sure the parent is the "instances" directory */
6637ecd3 6322 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
277ba044
SR
6323 if (WARN_ON_ONCE(parent != trace_instance_dir))
6324 return -ENOENT;
6325
6326 /*
6327 * The inode mutex is locked, but debugfs_create_dir() will also
6328 * take the mutex. As the instances directory can not be destroyed
6329 * or changed in any other way, it is safe to unlock it, and
6330 * let the dentry try. If two users try to make the same dir at
6331 * the same time, then the new_instance_create() will determine the
6332 * winner.
6333 */
6334 mutex_unlock(&inode->i_mutex);
6335
6336 ret = new_instance_create(dentry->d_iname);
6337
6338 mutex_lock(&inode->i_mutex);
6339
6340 return ret;
6341}
6342
0c8916c3
SR
6343static int instance_rmdir(struct inode *inode, struct dentry *dentry)
6344{
6345 struct dentry *parent;
6346 int ret;
6347
6348 /* Paranoid: Make sure the parent is the "instances" directory */
6637ecd3 6349 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
0c8916c3
SR
6350 if (WARN_ON_ONCE(parent != trace_instance_dir))
6351 return -ENOENT;
6352
6353 /* The caller did a dget() on dentry */
6354 mutex_unlock(&dentry->d_inode->i_mutex);
6355
6356 /*
6357 * The inode mutex is locked, but debugfs_create_dir() will also
6358 * take the mutex. As the instances directory can not be destroyed
6359 * or changed in any other way, it is safe to unlock it, and
6360 * let the dentry try. If two users try to make the same dir at
6361 * the same time, then the instance_delete() will determine the
6362 * winner.
6363 */
6364 mutex_unlock(&inode->i_mutex);
6365
6366 ret = instance_delete(dentry->d_iname);
6367
6368 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
6369 mutex_lock(&dentry->d_inode->i_mutex);
6370
6371 return ret;
6372}
6373
277ba044
SR
6374static const struct inode_operations instance_dir_inode_operations = {
6375 .lookup = simple_lookup,
6376 .mkdir = instance_mkdir,
0c8916c3 6377 .rmdir = instance_rmdir,
277ba044
SR
6378};
6379
6380static __init void create_trace_instances(struct dentry *d_tracer)
6381{
6382 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
6383 if (WARN_ON(!trace_instance_dir))
6384 return;
6385
6386 /* Hijack the dir inode operations, to allow mkdir */
6387 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
6388}
6389
2b6080f2
SR
6390static void
6391init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
6392{
121aaee7 6393 int cpu;
2b6080f2
SR
6394
6395 trace_create_file("trace_options", 0644, d_tracer,
6396 tr, &tracing_iter_fops);
6397
6398 trace_create_file("trace", 0644, d_tracer,
7272711a 6399 tr, &tracing_fops);
2b6080f2
SR
6400
6401 trace_create_file("trace_pipe", 0444, d_tracer,
23b625fa 6402 tr, &tracing_pipe_fops);
2b6080f2
SR
6403
6404 trace_create_file("buffer_size_kb", 0644, d_tracer,
e5dd03c4 6405 tr, &tracing_entries_fops);
2b6080f2
SR
6406
6407 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
6408 tr, &tracing_total_entries_fops);
6409
6410 trace_create_file("free_buffer", 0644, d_tracer,
6411 tr, &tracing_free_buffer_fops);
6412
6413 trace_create_file("trace_marker", 0220, d_tracer,
6414 tr, &tracing_mark_fops);
6415
6fa3eb70
S
6416 trace_create_file("saved_tgids", 0444, d_tracer,
6417 tr, &tracing_saved_tgids_fops);
6418
2b6080f2
SR
6419 trace_create_file("trace_clock", 0644, d_tracer, tr,
6420 &trace_clock_fops);
6421
6422 trace_create_file("tracing_on", 0644, d_tracer,
7272711a 6423 tr, &rb_simple_fops);
ce9bae55
SRRH
6424
6425#ifdef CONFIG_TRACER_SNAPSHOT
6426 trace_create_file("snapshot", 0644, d_tracer,
7272711a 6427 tr, &snapshot_fops);
ce9bae55 6428#endif
121aaee7
SRRH
6429
6430 for_each_tracing_cpu(cpu)
6431 tracing_init_debugfs_percpu(tr, cpu);
6432
2b6080f2
SR
6433}
6434
b5ad384e 6435static __init int tracer_init_debugfs(void)
bc0c38d1
SR
6436{
6437 struct dentry *d_tracer;
bc0c38d1 6438
7e53bd42
LJ
6439 trace_access_lock_init();
6440
bc0c38d1 6441 d_tracer = tracing_init_dentry();
ed6f1c99
NK
6442 if (!d_tracer)
6443 return 0;
bc0c38d1 6444
2b6080f2 6445 init_tracer_debugfs(&global_trace, d_tracer);
bc0c38d1 6446
5452af66 6447 trace_create_file("tracing_cpumask", 0644, d_tracer,
2b6080f2 6448 &global_trace, &tracing_cpumask_fops);
a8259075 6449
5452af66
FW
6450 trace_create_file("available_tracers", 0444, d_tracer,
6451 &global_trace, &show_traces_fops);
6452
339ae5d3 6453 trace_create_file("current_tracer", 0644, d_tracer,
5452af66
FW
6454 &global_trace, &set_tracer_fops);
6455
5d4a9dba 6456#ifdef CONFIG_TRACER_MAX_TRACE
5452af66
FW
6457 trace_create_file("tracing_max_latency", 0644, d_tracer,
6458 &tracing_max_latency, &tracing_max_lat_fops);
0e950173 6459#endif
5452af66
FW
6460
6461 trace_create_file("tracing_thresh", 0644, d_tracer,
6462 &tracing_thresh, &tracing_max_lat_fops);
a8259075 6463
339ae5d3 6464 trace_create_file("README", 0444, d_tracer,
5452af66
FW
6465 NULL, &tracing_readme_fops);
6466
69abe6a5
AP
6467 trace_create_file("saved_cmdlines", 0444, d_tracer,
6468 NULL, &tracing_saved_cmdlines_fops);
5bf9a1ee 6469
bc0c38d1 6470#ifdef CONFIG_DYNAMIC_FTRACE
5452af66
FW
6471 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6472 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
bc0c38d1 6473#endif
b04cc6b1 6474
277ba044 6475 create_trace_instances(d_tracer);
5452af66 6476
2b6080f2 6477 create_trace_options_dir(&global_trace);
b04cc6b1 6478
b5ad384e 6479 return 0;
bc0c38d1
SR
6480}
6481
3f5a54e3
SR
6482static int trace_panic_handler(struct notifier_block *this,
6483 unsigned long event, void *unused)
6484{
944ac425 6485 if (ftrace_dump_on_oops)
cecbca96 6486 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
6487 return NOTIFY_OK;
6488}
6489
6490static struct notifier_block trace_panic_notifier = {
6491 .notifier_call = trace_panic_handler,
6492 .next = NULL,
6493 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6494};
6495
6496static int trace_die_handler(struct notifier_block *self,
6497 unsigned long val,
6498 void *data)
6499{
6500 switch (val) {
6501 case DIE_OOPS:
944ac425 6502 if (ftrace_dump_on_oops)
cecbca96 6503 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
6504 break;
6505 default:
6506 break;
6507 }
6508 return NOTIFY_OK;
6509}
6510
6511static struct notifier_block trace_die_notifier = {
6512 .notifier_call = trace_die_handler,
6513 .priority = 200
6514};
6515
6516/*
6517 * printk is set to max of 1024, we really don't need it that big.
6518 * Nothing should be printing 1000 characters anyway.
6519 */
6520#define TRACE_MAX_PRINT 1000
6521
6522/*
6523 * Define here KERN_TRACE so that we have one place to modify
6524 * it if we decide to change what log level the ftrace dump
6525 * should be at.
6526 */
428aee14 6527#define KERN_TRACE KERN_EMERG
3f5a54e3 6528
955b61e5 6529void
3f5a54e3
SR
6530trace_printk_seq(struct trace_seq *s)
6531{
6532 /* Probably should print a warning here. */
bd6df187
J
6533 if (s->len >= TRACE_MAX_PRINT)
6534 s->len = TRACE_MAX_PRINT;
3f5a54e3
SR
6535
6536 /* should be zero ended, but we are paranoid. */
6537 s->buffer[s->len] = 0;
6538
6539 printk(KERN_TRACE "%s", s->buffer);
6540
f9520750 6541 trace_seq_init(s);
3f5a54e3
SR
6542}
6543
955b61e5
JW
6544void trace_init_global_iter(struct trace_iterator *iter)
6545{
6546 iter->tr = &global_trace;
2b6080f2 6547 iter->trace = iter->tr->current_trace;
ae3b5093 6548 iter->cpu_file = RING_BUFFER_ALL_CPUS;
12883efb 6549 iter->trace_buffer = &global_trace.trace_buffer;
955b61e5
JW
6550}
6551
7fe70b57 6552void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
3f5a54e3 6553{
3f5a54e3
SR
6554 /* use static because iter can be a bit big for the stack */
6555 static struct trace_iterator iter;
7fe70b57 6556 static atomic_t dump_running;
cf586b61 6557 unsigned int old_userobj;
d769041f
SR
6558 unsigned long flags;
6559 int cnt = 0, cpu;
3f5a54e3 6560
7fe70b57
SRRH
6561 /* Only allow one dump user at a time. */
6562 if (atomic_inc_return(&dump_running) != 1) {
6563 atomic_dec(&dump_running);
6564 return;
6565 }
3f5a54e3 6566
7fe70b57
SRRH
6567 /*
6568 * Always turn off tracing when we dump.
6569 * We don't need to show trace output of what happens
6570 * between multiple crashes.
6571 *
6572 * If the user does a sysrq-z, then they can re-enable
6573 * tracing with echo 1 > tracing_on.
6574 */
0ee6b6cf 6575 tracing_off();
cf586b61 6576
7fe70b57 6577 local_irq_save(flags);
3f5a54e3 6578
38dbe0b1 6579 /* Simulate the iterator */
955b61e5
JW
6580 trace_init_global_iter(&iter);
6581
d769041f 6582 for_each_tracing_cpu(cpu) {
12883efb 6583 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
d769041f
SR
6584 }
6585
cf586b61
FW
6586 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6587
b54d3de9
TE
6588 /* don't look at user memory in panic mode */
6589 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6590
cecbca96
FW
6591 switch (oops_dump_mode) {
6592 case DUMP_ALL:
ae3b5093 6593 iter.cpu_file = RING_BUFFER_ALL_CPUS;
cecbca96
FW
6594 break;
6595 case DUMP_ORIG:
6596 iter.cpu_file = raw_smp_processor_id();
6597 break;
6598 case DUMP_NONE:
6599 goto out_enable;
6600 default:
6601 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
ae3b5093 6602 iter.cpu_file = RING_BUFFER_ALL_CPUS;
cecbca96
FW
6603 }
6604
6605 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3f5a54e3 6606
7fe70b57
SRRH
6607 /* Did function tracer already get disabled? */
6608 if (ftrace_is_dead()) {
6609 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6610 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6611 }
6612
3f5a54e3
SR
6613 /*
6614 * We need to stop all tracing on all CPUS to read the
6615 * the next buffer. This is a bit expensive, but is
6616 * not done often. We fill all what we can read,
6617 * and then release the locks again.
6618 */
6619
3f5a54e3
SR
6620 while (!trace_empty(&iter)) {
6621
6622 if (!cnt)
6623 printk(KERN_TRACE "---------------------------------\n");
6624
6625 cnt++;
6626
6627 /* reset all but tr, trace, and overruns */
6628 memset(&iter.seq, 0,
6629 sizeof(struct trace_iterator) -
6630 offsetof(struct trace_iterator, seq));
6631 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6632 iter.pos = -1;
6633
955b61e5 6634 if (trace_find_next_entry_inc(&iter) != NULL) {
74e7ff8c
LJ
6635 int ret;
6636
6637 ret = print_trace_line(&iter);
6638 if (ret != TRACE_TYPE_NO_CONSUME)
6639 trace_consume(&iter);
3f5a54e3 6640 }
b892e5c8 6641 touch_nmi_watchdog();
3f5a54e3
SR
6642
6643 trace_printk_seq(&iter.seq);
6644 }
6645
6646 if (!cnt)
6647 printk(KERN_TRACE " (ftrace buffer empty)\n");
6648 else
6649 printk(KERN_TRACE "---------------------------------\n");
6650
cecbca96 6651 out_enable:
7fe70b57 6652 trace_flags |= old_userobj;
cf586b61 6653
7fe70b57
SRRH
6654 for_each_tracing_cpu(cpu) {
6655 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
cf586b61 6656 }
7fe70b57 6657 atomic_dec(&dump_running);
cd891ae0 6658 local_irq_restore(flags);
3f5a54e3 6659}
a8eecf22 6660EXPORT_SYMBOL_GPL(ftrace_dump);
cf586b61 6661
3928a8a2 6662__init static int tracer_alloc_buffers(void)
bc0c38d1 6663{
73c5162a 6664 int ring_buf_size;
9e01c1b7 6665 int ret = -ENOMEM;
4c11d7ae 6666
750912fa 6667
9e01c1b7
RR
6668 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6669 goto out;
6670
6671 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
6672 goto out_free_buffer_mask;
4c11d7ae 6673
07d777fe
SR
6674 /* Only allocate trace_printk buffers if a trace_printk exists */
6675 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
81698831 6676 /* Must be called before global_trace.buffer is allocated */
6fa3eb70 6677 trace_printk_init_buffers();
07d777fe 6678
73c5162a
SR
6679 /* To save memory, keep the ring buffer size to its minimum */
6680 if (ring_buffer_expanded)
6681 ring_buf_size = trace_buf_size;
6682 else
6683 ring_buf_size = 1;
6684
9e01c1b7
RR
6685 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6686 cpumask_copy(tracing_cpumask, cpu_all_mask);
6687
2b6080f2
SR
6688 raw_spin_lock_init(&global_trace.start_lock);
6689
9e01c1b7 6690 /* TODO: make the number of buffers hot pluggable with CPUS */
737223fb 6691 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
3928a8a2
SR
6692 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6693 WARN_ON(1);
9e01c1b7 6694 goto out_free_cpumask;
4c11d7ae 6695 }
a7603ff4 6696
499e5470
SR
6697 if (global_trace.buffer_disabled)
6698 tracing_off();
4c11d7ae 6699
bc0c38d1
SR
6700 trace_init_cmdlines();
6701
ca164318
SRRH
6702 /*
6703 * register_tracer() might reference current_trace, so it
6704 * needs to be set before we register anything. This is
6705 * just a bootstrap of current_trace anyway.
6706 */
2b6080f2
SR
6707 global_trace.current_trace = &nop_trace;
6708
ca164318
SRRH
6709 register_tracer(&nop_trace);
6710
60a11774
SR
6711 /* All seems OK, enable tracing */
6712 tracing_disabled = 0;
3928a8a2 6713
3f5a54e3
SR
6714 atomic_notifier_chain_register(&panic_notifier_list,
6715 &trace_panic_notifier);
6716
6717 register_die_notifier(&trace_die_notifier);
2fc1dfbe 6718
ae63b31e
SR
6719 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6720
2b6080f2
SR
6721 /* Holder for file callbacks */
6722 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6723 global_trace.trace_cpu.tr = &global_trace;
6724
ae63b31e
SR
6725 INIT_LIST_HEAD(&global_trace.systems);
6726 INIT_LIST_HEAD(&global_trace.events);
6727 list_add(&global_trace.list, &ftrace_trace_arrays);
6728
7bcfaf54
SR
6729 while (trace_boot_options) {
6730 char *option;
6731
6732 option = strsep(&trace_boot_options, ",");
2b6080f2 6733 trace_set_options(&global_trace, option);
7bcfaf54
SR
6734 }
6735
77fd5c15
SRRH
6736 register_snapshot_cmd();
6737
2fc1dfbe 6738 return 0;
3f5a54e3 6739
9e01c1b7 6740out_free_cpumask:
12883efb
SRRH
6741 free_percpu(global_trace.trace_buffer.data);
6742#ifdef CONFIG_TRACER_MAX_TRACE
6743 free_percpu(global_trace.max_buffer.data);
6744#endif
9e01c1b7
RR
6745 free_cpumask_var(tracing_cpumask);
6746out_free_buffer_mask:
6747 free_cpumask_var(tracing_buffer_mask);
6748out:
6749 return ret;
bc0c38d1 6750}
b2821ae6
SR
6751
6752__init static int clear_boot_tracer(void)
6753{
6754 /*
6755 * The default tracer at boot buffer is an init section.
6756 * This function is called in lateinit. If we did not
6757 * find the boot tracer, then clear it out, to prevent
6758 * later registration from accessing the buffer that is
6759 * about to be freed.
6760 */
6761 if (!default_bootup_tracer)
6762 return 0;
6763
6764 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6765 default_bootup_tracer);
6766 default_bootup_tracer = NULL;
6767
6768 return 0;
6769}
6770
b5ad384e
FW
6771early_initcall(tracer_alloc_buffers);
6772fs_initcall(tracer_init_debugfs);
b2821ae6 6773late_initcall(clear_boot_tracer);