]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/trace/trace.c
perf-probe: Add user memory access attribute support
[mirror_ubuntu-jammy-kernel.git] / kernel / trace / trace.c
CommitLineData
bcea3f96 1// SPDX-License-Identifier: GPL-2.0
bc0c38d1
SR
2/*
3 * ring buffer based function tracer
4 *
2b6080f2 5 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
bc0c38d1
SR
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 *
8 * Originally taken from the RT patch by:
9 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 *
11 * Based on code from the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
6d49e352 13 * Copyright (C) 2004 Nadia Yvette Chambers
bc0c38d1 14 */
2cadf913 15#include <linux/ring_buffer.h>
273b281f 16#include <generated/utsrelease.h>
2cadf913
SR
17#include <linux/stacktrace.h>
18#include <linux/writeback.h>
bc0c38d1
SR
19#include <linux/kallsyms.h>
20#include <linux/seq_file.h>
3f5a54e3 21#include <linux/notifier.h>
2cadf913 22#include <linux/irqflags.h>
bc0c38d1 23#include <linux/debugfs.h>
8434dc93 24#include <linux/tracefs.h>
4c11d7ae 25#include <linux/pagemap.h>
bc0c38d1
SR
26#include <linux/hardirq.h>
27#include <linux/linkage.h>
28#include <linux/uaccess.h>
76c813e2 29#include <linux/vmalloc.h>
bc0c38d1
SR
30#include <linux/ftrace.h>
31#include <linux/module.h>
32#include <linux/percpu.h>
2cadf913 33#include <linux/splice.h>
3f5a54e3 34#include <linux/kdebug.h>
5f0c6c03 35#include <linux/string.h>
f76180bc 36#include <linux/mount.h>
7e53bd42 37#include <linux/rwsem.h>
5a0e3ad6 38#include <linux/slab.h>
bc0c38d1
SR
39#include <linux/ctype.h>
40#include <linux/init.h>
2a2cc8f7 41#include <linux/poll.h>
b892e5c8 42#include <linux/nmi.h>
bc0c38d1 43#include <linux/fs.h>
478409dd 44#include <linux/trace.h>
3fd49c9e 45#include <linux/sched/clock.h>
8bd75c77 46#include <linux/sched/rt.h>
86387f7e 47
bc0c38d1 48#include "trace.h"
f0868d1e 49#include "trace_output.h"
bc0c38d1 50
73c5162a
SR
51/*
52 * On boot up, the ring buffer is set to the minimum size, so that
53 * we do not waste memory on systems that are not using tracing.
54 */
55034cd6 55bool ring_buffer_expanded;
73c5162a 56
8e1b82e0
FW
57/*
58 * We need to change this state when a selftest is running.
ff32504f
FW
59 * A selftest will lurk into the ring-buffer to count the
60 * entries inserted during the selftest although some concurrent
5e1607a0 61 * insertions into the ring-buffer such as trace_printk could occurred
ff32504f
FW
62 * at the same time, giving false positive or negative results.
63 */
8e1b82e0 64static bool __read_mostly tracing_selftest_running;
ff32504f 65
b2821ae6
SR
66/*
67 * If a tracer is running, we do not want to run SELFTEST.
68 */
020e5f85 69bool __read_mostly tracing_selftest_disabled;
b2821ae6 70
0daa2302
SRRH
71/* Pipe tracepoints to printk */
72struct trace_iterator *tracepoint_print_iter;
73int tracepoint_printk;
42391745 74static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
0daa2302 75
adf9f195
FW
76/* For tracers that don't implement custom flags */
77static struct tracer_opt dummy_tracer_opt[] = {
78 { }
79};
80
8c1a49ae
SRRH
81static int
82dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
adf9f195
FW
83{
84 return 0;
85}
0f048701 86
7ffbd48d
SR
87/*
88 * To prevent the comm cache from being overwritten when no
89 * tracing is active, only save the comm when a trace event
90 * occurred.
91 */
d914ba37 92static DEFINE_PER_CPU(bool, trace_taskinfo_save);
7ffbd48d 93
0f048701
SR
94/*
95 * Kill all tracing for good (never come back).
96 * It is initialized to 1 but will turn to zero if the initialization
97 * of the tracer is successful. But that is the only place that sets
98 * this back to zero.
99 */
4fd27358 100static int tracing_disabled = 1;
0f048701 101
955b61e5 102cpumask_var_t __read_mostly tracing_buffer_mask;
ab46428c 103
944ac425
SR
104/*
105 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
106 *
107 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
108 * is set, then ftrace_dump is called. This will output the contents
109 * of the ftrace buffers to the console. This is very useful for
110 * capturing traces that lead to crashes and outputing it to a
111 * serial console.
112 *
113 * It is default off, but you can enable it with either specifying
114 * "ftrace_dump_on_oops" in the kernel command line, or setting
cecbca96
FW
115 * /proc/sys/kernel/ftrace_dump_on_oops
116 * Set 1 if you want to dump buffers of all CPUs
117 * Set 2 if you want to dump the buffer of the CPU that triggered oops
944ac425 118 */
cecbca96
FW
119
120enum ftrace_dump_mode ftrace_dump_on_oops;
944ac425 121
de7edd31
SRRH
122/* When set, tracing will stop when a WARN*() is hit */
123int __disable_trace_on_warning;
124
681bec03
JL
125#ifdef CONFIG_TRACE_EVAL_MAP_FILE
126/* Map of enums to their values, for "eval_map" file */
23bf8cb8 127struct trace_eval_map_head {
9828413d
SRRH
128 struct module *mod;
129 unsigned long length;
130};
131
23bf8cb8 132union trace_eval_map_item;
9828413d 133
23bf8cb8 134struct trace_eval_map_tail {
9828413d
SRRH
135 /*
136 * "end" is first and points to NULL as it must be different
00f4b652 137 * than "mod" or "eval_string"
9828413d 138 */
23bf8cb8 139 union trace_eval_map_item *next;
9828413d
SRRH
140 const char *end; /* points to NULL */
141};
142
1793ed93 143static DEFINE_MUTEX(trace_eval_mutex);
9828413d
SRRH
144
145/*
23bf8cb8 146 * The trace_eval_maps are saved in an array with two extra elements,
9828413d
SRRH
147 * one at the beginning, and one at the end. The beginning item contains
148 * the count of the saved maps (head.length), and the module they
149 * belong to if not built in (head.mod). The ending item contains a
681bec03 150 * pointer to the next array of saved eval_map items.
9828413d 151 */
23bf8cb8 152union trace_eval_map_item {
00f4b652 153 struct trace_eval_map map;
23bf8cb8
JL
154 struct trace_eval_map_head head;
155 struct trace_eval_map_tail tail;
9828413d
SRRH
156};
157
23bf8cb8 158static union trace_eval_map_item *trace_eval_maps;
681bec03 159#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
9828413d 160
607e2ea1 161static int tracing_set_tracer(struct trace_array *tr, const char *buf);
c438f140
TG
162static void ftrace_trace_userstack(struct ring_buffer *buffer,
163 unsigned long flags, int pc);
b2821ae6 164
ee6c2c1b
LZ
165#define MAX_TRACER_SIZE 100
166static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
b2821ae6 167static char *default_bootup_tracer;
d9e54076 168
55034cd6
SRRH
169static bool allocate_snapshot;
170
1beee96b 171static int __init set_cmdline_ftrace(char *str)
d9e54076 172{
67012ab1 173 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
b2821ae6 174 default_bootup_tracer = bootup_tracer_buf;
73c5162a 175 /* We are using ftrace early, expand it */
55034cd6 176 ring_buffer_expanded = true;
d9e54076
PZ
177 return 1;
178}
1beee96b 179__setup("ftrace=", set_cmdline_ftrace);
d9e54076 180
944ac425
SR
181static int __init set_ftrace_dump_on_oops(char *str)
182{
cecbca96
FW
183 if (*str++ != '=' || !*str) {
184 ftrace_dump_on_oops = DUMP_ALL;
185 return 1;
186 }
187
188 if (!strcmp("orig_cpu", str)) {
189 ftrace_dump_on_oops = DUMP_ORIG;
190 return 1;
191 }
192
193 return 0;
944ac425
SR
194}
195__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
60a11774 196
de7edd31
SRRH
197static int __init stop_trace_on_warning(char *str)
198{
933ff9f2
LCG
199 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
200 __disable_trace_on_warning = 1;
de7edd31
SRRH
201 return 1;
202}
933ff9f2 203__setup("traceoff_on_warning", stop_trace_on_warning);
de7edd31 204
3209cff4 205static int __init boot_alloc_snapshot(char *str)
55034cd6
SRRH
206{
207 allocate_snapshot = true;
208 /* We also need the main ring buffer expanded */
209 ring_buffer_expanded = true;
210 return 1;
211}
3209cff4 212__setup("alloc_snapshot", boot_alloc_snapshot);
55034cd6 213
7bcfaf54
SR
214
215static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
7bcfaf54
SR
216
217static int __init set_trace_boot_options(char *str)
218{
67012ab1 219 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
7bcfaf54
SR
220 return 0;
221}
222__setup("trace_options=", set_trace_boot_options);
223
e1e232ca
SR
224static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
225static char *trace_boot_clock __initdata;
226
227static int __init set_trace_boot_clock(char *str)
228{
229 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
230 trace_boot_clock = trace_boot_clock_buf;
231 return 0;
232}
233__setup("trace_clock=", set_trace_boot_clock);
234
0daa2302
SRRH
235static int __init set_tracepoint_printk(char *str)
236{
237 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
238 tracepoint_printk = 1;
239 return 1;
240}
241__setup("tp_printk", set_tracepoint_printk);
de7edd31 242
a5a1d1c2 243unsigned long long ns2usecs(u64 nsec)
bc0c38d1
SR
244{
245 nsec += 500;
246 do_div(nsec, 1000);
247 return nsec;
248}
249
983f938a
SRRH
250/* trace_flags holds trace_options default values */
251#define TRACE_DEFAULT_FLAGS \
252 (FUNCTION_DEFAULT_FLAGS | \
253 TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \
254 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \
255 TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \
256 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS)
257
16270145
SRRH
258/* trace_options that are only supported by global_trace */
259#define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \
260 TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
261
20550622
SRRH
262/* trace_flags that are default zero for instances */
263#define ZEROED_TRACE_FLAGS \
1e10486f 264 (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK)
16270145 265
4fcdae83 266/*
67d04bb2
JF
267 * The global_trace is the descriptor that holds the top-level tracing
268 * buffers for the live tracing.
4fcdae83 269 */
983f938a
SRRH
270static struct trace_array global_trace = {
271 .trace_flags = TRACE_DEFAULT_FLAGS,
272};
bc0c38d1 273
ae63b31e 274LIST_HEAD(ftrace_trace_arrays);
bc0c38d1 275
ff451961
SRRH
276int trace_array_get(struct trace_array *this_tr)
277{
278 struct trace_array *tr;
279 int ret = -ENODEV;
280
281 mutex_lock(&trace_types_lock);
282 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
283 if (tr == this_tr) {
284 tr->ref++;
285 ret = 0;
286 break;
287 }
288 }
289 mutex_unlock(&trace_types_lock);
290
291 return ret;
292}
293
294static void __trace_array_put(struct trace_array *this_tr)
295{
296 WARN_ON(!this_tr->ref);
297 this_tr->ref--;
298}
299
300void trace_array_put(struct trace_array *this_tr)
301{
302 mutex_lock(&trace_types_lock);
303 __trace_array_put(this_tr);
304 mutex_unlock(&trace_types_lock);
305}
306
2425bcb9 307int call_filter_check_discard(struct trace_event_call *call, void *rec,
f306cc82
TZ
308 struct ring_buffer *buffer,
309 struct ring_buffer_event *event)
310{
311 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
312 !filter_match_preds(call->filter, rec)) {
0fc1b09f 313 __trace_event_discard_commit(buffer, event);
f306cc82
TZ
314 return 1;
315 }
316
317 return 0;
eb02ce01
TZ
318}
319
76c813e2
SRRH
320void trace_free_pid_list(struct trace_pid_list *pid_list)
321{
322 vfree(pid_list->pids);
323 kfree(pid_list);
324}
325
d8275c45
SR
326/**
327 * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
328 * @filtered_pids: The list of pids to check
329 * @search_pid: The PID to find in @filtered_pids
330 *
331 * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis.
332 */
333bool
334trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
335{
336 /*
337 * If pid_max changed after filtered_pids was created, we
338 * by default ignore all pids greater than the previous pid_max.
339 */
340 if (search_pid >= filtered_pids->pid_max)
341 return false;
342
343 return test_bit(search_pid, filtered_pids->pids);
344}
345
346/**
347 * trace_ignore_this_task - should a task be ignored for tracing
348 * @filtered_pids: The list of pids to check
349 * @task: The task that should be ignored if not filtered
350 *
351 * Checks if @task should be traced or not from @filtered_pids.
352 * Returns true if @task should *NOT* be traced.
353 * Returns false if @task should be traced.
354 */
355bool
356trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task)
357{
358 /*
359 * Return false, because if filtered_pids does not exist,
360 * all pids are good to trace.
361 */
362 if (!filtered_pids)
363 return false;
364
365 return !trace_find_filtered_pid(filtered_pids, task->pid);
366}
367
368/**
5a93bae2 369 * trace_pid_filter_add_remove_task - Add or remove a task from a pid_list
d8275c45
SR
370 * @pid_list: The list to modify
371 * @self: The current task for fork or NULL for exit
372 * @task: The task to add or remove
373 *
374 * If adding a task, if @self is defined, the task is only added if @self
375 * is also included in @pid_list. This happens on fork and tasks should
376 * only be added when the parent is listed. If @self is NULL, then the
377 * @task pid will be removed from the list, which would happen on exit
378 * of a task.
379 */
380void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
381 struct task_struct *self,
382 struct task_struct *task)
383{
384 if (!pid_list)
385 return;
386
387 /* For forks, we only add if the forking task is listed */
388 if (self) {
389 if (!trace_find_filtered_pid(pid_list, self->pid))
390 return;
391 }
392
393 /* Sorry, but we don't support pid_max changing after setting */
394 if (task->pid >= pid_list->pid_max)
395 return;
396
397 /* "self" is set for forks, and NULL for exits */
398 if (self)
399 set_bit(task->pid, pid_list->pids);
400 else
401 clear_bit(task->pid, pid_list->pids);
402}
403
5cc8976b
SRRH
404/**
405 * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
406 * @pid_list: The pid list to show
407 * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
408 * @pos: The position of the file
409 *
410 * This is used by the seq_file "next" operation to iterate the pids
411 * listed in a trace_pid_list structure.
412 *
413 * Returns the pid+1 as we want to display pid of zero, but NULL would
414 * stop the iteration.
415 */
416void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
417{
418 unsigned long pid = (unsigned long)v;
419
420 (*pos)++;
421
422 /* pid already is +1 of the actual prevous bit */
423 pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
424
425 /* Return pid + 1 to allow zero to be represented */
426 if (pid < pid_list->pid_max)
427 return (void *)(pid + 1);
428
429 return NULL;
430}
431
432/**
433 * trace_pid_start - Used for seq_file to start reading pid lists
434 * @pid_list: The pid list to show
435 * @pos: The position of the file
436 *
437 * This is used by seq_file "start" operation to start the iteration
438 * of listing pids.
439 *
440 * Returns the pid+1 as we want to display pid of zero, but NULL would
441 * stop the iteration.
442 */
443void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
444{
445 unsigned long pid;
446 loff_t l = 0;
447
448 pid = find_first_bit(pid_list->pids, pid_list->pid_max);
449 if (pid >= pid_list->pid_max)
450 return NULL;
451
452 /* Return pid + 1 so that zero can be the exit value */
453 for (pid++; pid && l < *pos;
454 pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
455 ;
456 return (void *)pid;
457}
458
459/**
460 * trace_pid_show - show the current pid in seq_file processing
461 * @m: The seq_file structure to write into
462 * @v: A void pointer of the pid (+1) value to display
463 *
464 * Can be directly used by seq_file operations to display the current
465 * pid value.
466 */
467int trace_pid_show(struct seq_file *m, void *v)
468{
469 unsigned long pid = (unsigned long)v - 1;
470
471 seq_printf(m, "%lu\n", pid);
472 return 0;
473}
474
76c813e2
SRRH
475/* 128 should be much more than enough */
476#define PID_BUF_SIZE 127
477
478int trace_pid_write(struct trace_pid_list *filtered_pids,
479 struct trace_pid_list **new_pid_list,
480 const char __user *ubuf, size_t cnt)
481{
482 struct trace_pid_list *pid_list;
483 struct trace_parser parser;
484 unsigned long val;
485 int nr_pids = 0;
486 ssize_t read = 0;
487 ssize_t ret = 0;
488 loff_t pos;
489 pid_t pid;
490
491 if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
492 return -ENOMEM;
493
494 /*
495 * Always recreate a new array. The write is an all or nothing
496 * operation. Always create a new array when adding new pids by
497 * the user. If the operation fails, then the current list is
498 * not modified.
499 */
500 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
91862cc7
WW
501 if (!pid_list) {
502 trace_parser_put(&parser);
76c813e2 503 return -ENOMEM;
91862cc7 504 }
76c813e2
SRRH
505
506 pid_list->pid_max = READ_ONCE(pid_max);
507
508 /* Only truncating will shrink pid_max */
509 if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
510 pid_list->pid_max = filtered_pids->pid_max;
511
512 pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
513 if (!pid_list->pids) {
91862cc7 514 trace_parser_put(&parser);
76c813e2
SRRH
515 kfree(pid_list);
516 return -ENOMEM;
517 }
518
519 if (filtered_pids) {
520 /* copy the current bits to the new max */
67f20b08
WY
521 for_each_set_bit(pid, filtered_pids->pids,
522 filtered_pids->pid_max) {
76c813e2 523 set_bit(pid, pid_list->pids);
76c813e2
SRRH
524 nr_pids++;
525 }
526 }
527
528 while (cnt > 0) {
529
530 pos = 0;
531
532 ret = trace_get_user(&parser, ubuf, cnt, &pos);
533 if (ret < 0 || !trace_parser_loaded(&parser))
534 break;
535
536 read += ret;
537 ubuf += ret;
538 cnt -= ret;
539
76c813e2
SRRH
540 ret = -EINVAL;
541 if (kstrtoul(parser.buffer, 0, &val))
542 break;
543 if (val >= pid_list->pid_max)
544 break;
545
546 pid = (pid_t)val;
547
548 set_bit(pid, pid_list->pids);
549 nr_pids++;
550
551 trace_parser_clear(&parser);
552 ret = 0;
553 }
554 trace_parser_put(&parser);
555
556 if (ret < 0) {
557 trace_free_pid_list(pid_list);
558 return ret;
559 }
560
561 if (!nr_pids) {
562 /* Cleared the list of pids */
563 trace_free_pid_list(pid_list);
564 read = ret;
565 pid_list = NULL;
566 }
567
568 *new_pid_list = pid_list;
569
570 return read;
571}
572
a5a1d1c2 573static u64 buffer_ftrace_now(struct trace_buffer *buf, int cpu)
37886f6a
SR
574{
575 u64 ts;
576
577 /* Early boot up does not have a buffer yet */
9457158b 578 if (!buf->buffer)
37886f6a
SR
579 return trace_clock_local();
580
9457158b
AL
581 ts = ring_buffer_time_stamp(buf->buffer, cpu);
582 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
37886f6a
SR
583
584 return ts;
585}
bc0c38d1 586
a5a1d1c2 587u64 ftrace_now(int cpu)
9457158b
AL
588{
589 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
590}
591
10246fa3
SRRH
592/**
593 * tracing_is_enabled - Show if global_trace has been disabled
594 *
595 * Shows if the global trace has been enabled or not. It uses the
596 * mirror flag "buffer_disabled" to be used in fast paths such as for
597 * the irqsoff tracer. But it may be inaccurate due to races. If you
598 * need to know the accurate state, use tracing_is_on() which is a little
599 * slower, but accurate.
600 */
9036990d
SR
601int tracing_is_enabled(void)
602{
10246fa3
SRRH
603 /*
604 * For quick access (irqsoff uses this in fast path), just
605 * return the mirror variable of the state of the ring buffer.
606 * It's a little racy, but we don't really care.
607 */
608 smp_rmb();
609 return !global_trace.buffer_disabled;
9036990d
SR
610}
611
4fcdae83 612/*
3928a8a2
SR
613 * trace_buf_size is the size in bytes that is allocated
614 * for a buffer. Note, the number of bytes is always rounded
615 * to page size.
3f5a54e3
SR
616 *
617 * This number is purposely set to a low number of 16384.
618 * If the dump on oops happens, it will be much appreciated
619 * to not have to wait for all that output. Anyway this can be
620 * boot time and run time configurable.
4fcdae83 621 */
3928a8a2 622#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
3f5a54e3 623
3928a8a2 624static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
bc0c38d1 625
4fcdae83 626/* trace_types holds a link list of available tracers. */
bc0c38d1 627static struct tracer *trace_types __read_mostly;
4fcdae83 628
4fcdae83
SR
629/*
630 * trace_types_lock is used to protect the trace_types list.
4fcdae83 631 */
a8227415 632DEFINE_MUTEX(trace_types_lock);
4fcdae83 633
7e53bd42
LJ
634/*
635 * serialize the access of the ring buffer
636 *
637 * ring buffer serializes readers, but it is low level protection.
638 * The validity of the events (which returns by ring_buffer_peek() ..etc)
639 * are not protected by ring buffer.
640 *
641 * The content of events may become garbage if we allow other process consumes
642 * these events concurrently:
643 * A) the page of the consumed events may become a normal page
644 * (not reader page) in ring buffer, and this page will be rewrited
645 * by events producer.
646 * B) The page of the consumed events may become a page for splice_read,
647 * and this page will be returned to system.
648 *
649 * These primitives allow multi process access to different cpu ring buffer
650 * concurrently.
651 *
652 * These primitives don't distinguish read-only and read-consume access.
653 * Multi read-only access are also serialized.
654 */
655
656#ifdef CONFIG_SMP
657static DECLARE_RWSEM(all_cpu_access_lock);
658static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
659
660static inline void trace_access_lock(int cpu)
661{
ae3b5093 662 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
663 /* gain it for accessing the whole ring buffer. */
664 down_write(&all_cpu_access_lock);
665 } else {
666 /* gain it for accessing a cpu ring buffer. */
667
ae3b5093 668 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
7e53bd42
LJ
669 down_read(&all_cpu_access_lock);
670
671 /* Secondly block other access to this @cpu ring buffer. */
672 mutex_lock(&per_cpu(cpu_access_lock, cpu));
673 }
674}
675
676static inline void trace_access_unlock(int cpu)
677{
ae3b5093 678 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
679 up_write(&all_cpu_access_lock);
680 } else {
681 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
682 up_read(&all_cpu_access_lock);
683 }
684}
685
686static inline void trace_access_lock_init(void)
687{
688 int cpu;
689
690 for_each_possible_cpu(cpu)
691 mutex_init(&per_cpu(cpu_access_lock, cpu));
692}
693
694#else
695
696static DEFINE_MUTEX(access_lock);
697
698static inline void trace_access_lock(int cpu)
699{
700 (void)cpu;
701 mutex_lock(&access_lock);
702}
703
704static inline void trace_access_unlock(int cpu)
705{
706 (void)cpu;
707 mutex_unlock(&access_lock);
708}
709
710static inline void trace_access_lock_init(void)
711{
712}
713
714#endif
715
d78a4614
SRRH
716#ifdef CONFIG_STACKTRACE
717static void __ftrace_trace_stack(struct ring_buffer *buffer,
718 unsigned long flags,
719 int skip, int pc, struct pt_regs *regs);
2d34f489
SRRH
720static inline void ftrace_trace_stack(struct trace_array *tr,
721 struct ring_buffer *buffer,
73dddbb5
SRRH
722 unsigned long flags,
723 int skip, int pc, struct pt_regs *regs);
ca475e83 724
d78a4614
SRRH
725#else
726static inline void __ftrace_trace_stack(struct ring_buffer *buffer,
727 unsigned long flags,
728 int skip, int pc, struct pt_regs *regs)
729{
730}
2d34f489
SRRH
731static inline void ftrace_trace_stack(struct trace_array *tr,
732 struct ring_buffer *buffer,
73dddbb5
SRRH
733 unsigned long flags,
734 int skip, int pc, struct pt_regs *regs)
ca475e83
SRRH
735{
736}
737
d78a4614
SRRH
738#endif
739
3e9a8aad
SRRH
740static __always_inline void
741trace_event_setup(struct ring_buffer_event *event,
742 int type, unsigned long flags, int pc)
743{
744 struct trace_entry *ent = ring_buffer_event_data(event);
745
746 tracing_generic_entry_update(ent, flags, pc);
747 ent->type = type;
748}
749
750static __always_inline struct ring_buffer_event *
751__trace_buffer_lock_reserve(struct ring_buffer *buffer,
752 int type,
753 unsigned long len,
754 unsigned long flags, int pc)
755{
756 struct ring_buffer_event *event;
757
758 event = ring_buffer_lock_reserve(buffer, len);
759 if (event != NULL)
760 trace_event_setup(event, type, flags, pc);
761
762 return event;
763}
764
2290f2c5 765void tracer_tracing_on(struct trace_array *tr)
10246fa3
SRRH
766{
767 if (tr->trace_buffer.buffer)
768 ring_buffer_record_on(tr->trace_buffer.buffer);
769 /*
770 * This flag is looked at when buffers haven't been allocated
771 * yet, or by some tracers (like irqsoff), that just want to
772 * know if the ring buffer has been disabled, but it can handle
773 * races of where it gets disabled but we still do a record.
774 * As the check is in the fast path of the tracers, it is more
775 * important to be fast than accurate.
776 */
777 tr->buffer_disabled = 0;
778 /* Make the flag seen by readers */
779 smp_wmb();
780}
781
499e5470
SR
782/**
783 * tracing_on - enable tracing buffers
784 *
785 * This function enables tracing buffers that may have been
786 * disabled with tracing_off.
787 */
788void tracing_on(void)
789{
10246fa3 790 tracer_tracing_on(&global_trace);
499e5470
SR
791}
792EXPORT_SYMBOL_GPL(tracing_on);
793
52ffabe3
SRRH
794
795static __always_inline void
796__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
797{
d914ba37 798 __this_cpu_write(trace_taskinfo_save, true);
52ffabe3
SRRH
799
800 /* If this is the temp buffer, we need to commit fully */
801 if (this_cpu_read(trace_buffered_event) == event) {
802 /* Length is in event->array[0] */
803 ring_buffer_write(buffer, event->array[0], &event->array[1]);
804 /* Release the temp buffer */
805 this_cpu_dec(trace_buffered_event_cnt);
806 } else
807 ring_buffer_unlock_commit(buffer, event);
808}
809
09ae7234
SRRH
810/**
811 * __trace_puts - write a constant string into the trace buffer.
812 * @ip: The address of the caller
813 * @str: The constant string to write
814 * @size: The size of the string.
815 */
816int __trace_puts(unsigned long ip, const char *str, int size)
817{
818 struct ring_buffer_event *event;
819 struct ring_buffer *buffer;
820 struct print_entry *entry;
821 unsigned long irq_flags;
822 int alloc;
8abfb872
J
823 int pc;
824
983f938a 825 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
f0160a5a
J
826 return 0;
827
8abfb872 828 pc = preempt_count();
09ae7234 829
3132e107
SRRH
830 if (unlikely(tracing_selftest_running || tracing_disabled))
831 return 0;
832
09ae7234
SRRH
833 alloc = sizeof(*entry) + size + 2; /* possible \n added */
834
835 local_save_flags(irq_flags);
836 buffer = global_trace.trace_buffer.buffer;
3e9a8aad
SRRH
837 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
838 irq_flags, pc);
09ae7234
SRRH
839 if (!event)
840 return 0;
841
842 entry = ring_buffer_event_data(event);
843 entry->ip = ip;
844
845 memcpy(&entry->buf, str, size);
846
847 /* Add a newline if necessary */
848 if (entry->buf[size - 1] != '\n') {
849 entry->buf[size] = '\n';
850 entry->buf[size + 1] = '\0';
851 } else
852 entry->buf[size] = '\0';
853
854 __buffer_unlock_commit(buffer, event);
2d34f489 855 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
09ae7234
SRRH
856
857 return size;
858}
859EXPORT_SYMBOL_GPL(__trace_puts);
860
861/**
862 * __trace_bputs - write the pointer to a constant string into trace buffer
863 * @ip: The address of the caller
864 * @str: The constant string to write to the buffer to
865 */
866int __trace_bputs(unsigned long ip, const char *str)
867{
868 struct ring_buffer_event *event;
869 struct ring_buffer *buffer;
870 struct bputs_entry *entry;
871 unsigned long irq_flags;
872 int size = sizeof(struct bputs_entry);
8abfb872
J
873 int pc;
874
983f938a 875 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
f0160a5a
J
876 return 0;
877
8abfb872 878 pc = preempt_count();
09ae7234 879
3132e107
SRRH
880 if (unlikely(tracing_selftest_running || tracing_disabled))
881 return 0;
882
09ae7234
SRRH
883 local_save_flags(irq_flags);
884 buffer = global_trace.trace_buffer.buffer;
3e9a8aad
SRRH
885 event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
886 irq_flags, pc);
09ae7234
SRRH
887 if (!event)
888 return 0;
889
890 entry = ring_buffer_event_data(event);
891 entry->ip = ip;
892 entry->str = str;
893
894 __buffer_unlock_commit(buffer, event);
2d34f489 895 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
09ae7234
SRRH
896
897 return 1;
898}
899EXPORT_SYMBOL_GPL(__trace_bputs);
900
ad909e21 901#ifdef CONFIG_TRACER_SNAPSHOT
a35873a0 902void tracing_snapshot_instance_cond(struct trace_array *tr, void *cond_data)
ad909e21 903{
ad909e21
SRRH
904 struct tracer *tracer = tr->current_trace;
905 unsigned long flags;
906
1b22e382
SRRH
907 if (in_nmi()) {
908 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
909 internal_trace_puts("*** snapshot is being ignored ***\n");
910 return;
911 }
912
ad909e21 913 if (!tr->allocated_snapshot) {
ca268da6
SRRH
914 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
915 internal_trace_puts("*** stopping trace here! ***\n");
ad909e21
SRRH
916 tracing_off();
917 return;
918 }
919
920 /* Note, snapshot can not be used when the tracer uses it */
921 if (tracer->use_max_tr) {
ca268da6
SRRH
922 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
923 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
ad909e21
SRRH
924 return;
925 }
926
927 local_irq_save(flags);
a35873a0 928 update_max_tr(tr, current, smp_processor_id(), cond_data);
ad909e21
SRRH
929 local_irq_restore(flags);
930}
cab50379 931
a35873a0
TZ
932void tracing_snapshot_instance(struct trace_array *tr)
933{
934 tracing_snapshot_instance_cond(tr, NULL);
935}
936
cab50379 937/**
5a93bae2 938 * tracing_snapshot - take a snapshot of the current buffer.
cab50379
SRV
939 *
940 * This causes a swap between the snapshot buffer and the current live
941 * tracing buffer. You can use this to take snapshots of the live
942 * trace when some condition is triggered, but continue to trace.
943 *
944 * Note, make sure to allocate the snapshot with either
945 * a tracing_snapshot_alloc(), or by doing it manually
946 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
947 *
948 * If the snapshot buffer is not allocated, it will stop tracing.
949 * Basically making a permanent snapshot.
950 */
951void tracing_snapshot(void)
952{
953 struct trace_array *tr = &global_trace;
954
955 tracing_snapshot_instance(tr);
956}
1b22e382 957EXPORT_SYMBOL_GPL(tracing_snapshot);
ad909e21 958
a35873a0
TZ
959/**
960 * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
961 * @tr: The tracing instance to snapshot
962 * @cond_data: The data to be tested conditionally, and possibly saved
963 *
964 * This is the same as tracing_snapshot() except that the snapshot is
965 * conditional - the snapshot will only happen if the
966 * cond_snapshot.update() implementation receiving the cond_data
967 * returns true, which means that the trace array's cond_snapshot
968 * update() operation used the cond_data to determine whether the
969 * snapshot should be taken, and if it was, presumably saved it along
970 * with the snapshot.
971 */
972void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
973{
974 tracing_snapshot_instance_cond(tr, cond_data);
975}
976EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
977
978/**
979 * tracing_snapshot_cond_data - get the user data associated with a snapshot
980 * @tr: The tracing instance
981 *
982 * When the user enables a conditional snapshot using
983 * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
984 * with the snapshot. This accessor is used to retrieve it.
985 *
986 * Should not be called from cond_snapshot.update(), since it takes
987 * the tr->max_lock lock, which the code calling
988 * cond_snapshot.update() has already done.
989 *
990 * Returns the cond_data associated with the trace array's snapshot.
991 */
992void *tracing_cond_snapshot_data(struct trace_array *tr)
993{
994 void *cond_data = NULL;
995
996 arch_spin_lock(&tr->max_lock);
997
998 if (tr->cond_snapshot)
999 cond_data = tr->cond_snapshot->cond_data;
1000
1001 arch_spin_unlock(&tr->max_lock);
1002
1003 return cond_data;
1004}
1005EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1006
ad909e21
SRRH
1007static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
1008 struct trace_buffer *size_buf, int cpu_id);
3209cff4
SRRH
1009static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
1010
2824f503 1011int tracing_alloc_snapshot_instance(struct trace_array *tr)
3209cff4
SRRH
1012{
1013 int ret;
1014
1015 if (!tr->allocated_snapshot) {
1016
1017 /* allocate spare buffer */
1018 ret = resize_buffer_duplicate_size(&tr->max_buffer,
1019 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
1020 if (ret < 0)
1021 return ret;
1022
1023 tr->allocated_snapshot = true;
1024 }
1025
1026 return 0;
1027}
1028
ad1438a0 1029static void free_snapshot(struct trace_array *tr)
3209cff4
SRRH
1030{
1031 /*
1032 * We don't free the ring buffer. instead, resize it because
1033 * The max_tr ring buffer has some state (e.g. ring->clock) and
1034 * we want preserve it.
1035 */
1036 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
1037 set_buffer_entries(&tr->max_buffer, 1);
1038 tracing_reset_online_cpus(&tr->max_buffer);
1039 tr->allocated_snapshot = false;
1040}
ad909e21 1041
93e31ffb
TZ
1042/**
1043 * tracing_alloc_snapshot - allocate snapshot buffer.
1044 *
1045 * This only allocates the snapshot buffer if it isn't already
1046 * allocated - it doesn't also take a snapshot.
1047 *
1048 * This is meant to be used in cases where the snapshot buffer needs
1049 * to be set up for events that can't sleep but need to be able to
1050 * trigger a snapshot.
1051 */
1052int tracing_alloc_snapshot(void)
1053{
1054 struct trace_array *tr = &global_trace;
1055 int ret;
1056
2824f503 1057 ret = tracing_alloc_snapshot_instance(tr);
93e31ffb
TZ
1058 WARN_ON(ret < 0);
1059
1060 return ret;
1061}
1062EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1063
ad909e21 1064/**
5a93bae2 1065 * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
ad909e21 1066 *
5a93bae2 1067 * This is similar to tracing_snapshot(), but it will allocate the
ad909e21
SRRH
1068 * snapshot buffer if it isn't already allocated. Use this only
1069 * where it is safe to sleep, as the allocation may sleep.
1070 *
1071 * This causes a swap between the snapshot buffer and the current live
1072 * tracing buffer. You can use this to take snapshots of the live
1073 * trace when some condition is triggered, but continue to trace.
1074 */
1075void tracing_snapshot_alloc(void)
1076{
ad909e21
SRRH
1077 int ret;
1078
93e31ffb
TZ
1079 ret = tracing_alloc_snapshot();
1080 if (ret < 0)
3209cff4 1081 return;
ad909e21
SRRH
1082
1083 tracing_snapshot();
1084}
1b22e382 1085EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
a35873a0
TZ
1086
1087/**
1088 * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
1089 * @tr: The tracing instance
1090 * @cond_data: User data to associate with the snapshot
1091 * @update: Implementation of the cond_snapshot update function
1092 *
1093 * Check whether the conditional snapshot for the given instance has
1094 * already been enabled, or if the current tracer is already using a
1095 * snapshot; if so, return -EBUSY, else create a cond_snapshot and
1096 * save the cond_data and update function inside.
1097 *
1098 * Returns 0 if successful, error otherwise.
1099 */
1100int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
1101 cond_update_fn_t update)
1102{
1103 struct cond_snapshot *cond_snapshot;
1104 int ret = 0;
1105
1106 cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
1107 if (!cond_snapshot)
1108 return -ENOMEM;
1109
1110 cond_snapshot->cond_data = cond_data;
1111 cond_snapshot->update = update;
1112
1113 mutex_lock(&trace_types_lock);
1114
1115 ret = tracing_alloc_snapshot_instance(tr);
1116 if (ret)
1117 goto fail_unlock;
1118
1119 if (tr->current_trace->use_max_tr) {
1120 ret = -EBUSY;
1121 goto fail_unlock;
1122 }
1123
1c347a94
SRV
1124 /*
1125 * The cond_snapshot can only change to NULL without the
1126 * trace_types_lock. We don't care if we race with it going
1127 * to NULL, but we want to make sure that it's not set to
1128 * something other than NULL when we get here, which we can
1129 * do safely with only holding the trace_types_lock and not
1130 * having to take the max_lock.
1131 */
a35873a0
TZ
1132 if (tr->cond_snapshot) {
1133 ret = -EBUSY;
1134 goto fail_unlock;
1135 }
1136
1137 arch_spin_lock(&tr->max_lock);
1138 tr->cond_snapshot = cond_snapshot;
1139 arch_spin_unlock(&tr->max_lock);
1140
1141 mutex_unlock(&trace_types_lock);
1142
1143 return ret;
1144
1145 fail_unlock:
1146 mutex_unlock(&trace_types_lock);
1147 kfree(cond_snapshot);
1148 return ret;
1149}
1150EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1151
1152/**
1153 * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
1154 * @tr: The tracing instance
1155 *
1156 * Check whether the conditional snapshot for the given instance is
1157 * enabled; if so, free the cond_snapshot associated with it,
1158 * otherwise return -EINVAL.
1159 *
1160 * Returns 0 if successful, error otherwise.
1161 */
1162int tracing_snapshot_cond_disable(struct trace_array *tr)
1163{
1164 int ret = 0;
1165
1166 arch_spin_lock(&tr->max_lock);
1167
1168 if (!tr->cond_snapshot)
1169 ret = -EINVAL;
1170 else {
1171 kfree(tr->cond_snapshot);
1172 tr->cond_snapshot = NULL;
1173 }
1174
1175 arch_spin_unlock(&tr->max_lock);
1176
1177 return ret;
1178}
1179EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
ad909e21
SRRH
1180#else
1181void tracing_snapshot(void)
1182{
1183 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1184}
1b22e382 1185EXPORT_SYMBOL_GPL(tracing_snapshot);
a35873a0
TZ
1186void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1187{
1188 WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
1189}
1190EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
93e31ffb
TZ
1191int tracing_alloc_snapshot(void)
1192{
1193 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1194 return -ENODEV;
1195}
1196EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
ad909e21
SRRH
1197void tracing_snapshot_alloc(void)
1198{
1199 /* Give warning */
1200 tracing_snapshot();
1201}
1b22e382 1202EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
a35873a0
TZ
1203void *tracing_cond_snapshot_data(struct trace_array *tr)
1204{
1205 return NULL;
1206}
1207EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1208int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
1209{
1210 return -ENODEV;
1211}
1212EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1213int tracing_snapshot_cond_disable(struct trace_array *tr)
1214{
1215 return false;
1216}
1217EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
ad909e21
SRRH
1218#endif /* CONFIG_TRACER_SNAPSHOT */
1219
2290f2c5 1220void tracer_tracing_off(struct trace_array *tr)
10246fa3
SRRH
1221{
1222 if (tr->trace_buffer.buffer)
1223 ring_buffer_record_off(tr->trace_buffer.buffer);
1224 /*
1225 * This flag is looked at when buffers haven't been allocated
1226 * yet, or by some tracers (like irqsoff), that just want to
1227 * know if the ring buffer has been disabled, but it can handle
1228 * races of where it gets disabled but we still do a record.
1229 * As the check is in the fast path of the tracers, it is more
1230 * important to be fast than accurate.
1231 */
1232 tr->buffer_disabled = 1;
1233 /* Make the flag seen by readers */
1234 smp_wmb();
1235}
1236
499e5470
SR
1237/**
1238 * tracing_off - turn off tracing buffers
1239 *
1240 * This function stops the tracing buffers from recording data.
1241 * It does not disable any overhead the tracers themselves may
1242 * be causing. This function simply causes all recording to
1243 * the ring buffers to fail.
1244 */
1245void tracing_off(void)
1246{
10246fa3 1247 tracer_tracing_off(&global_trace);
499e5470
SR
1248}
1249EXPORT_SYMBOL_GPL(tracing_off);
1250
de7edd31
SRRH
1251void disable_trace_on_warning(void)
1252{
1253 if (__disable_trace_on_warning)
1254 tracing_off();
1255}
1256
10246fa3
SRRH
1257/**
1258 * tracer_tracing_is_on - show real state of ring buffer enabled
1259 * @tr : the trace array to know if ring buffer is enabled
1260 *
1261 * Shows real state of the ring buffer if it is enabled or not.
1262 */
ec573508 1263bool tracer_tracing_is_on(struct trace_array *tr)
10246fa3
SRRH
1264{
1265 if (tr->trace_buffer.buffer)
1266 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
1267 return !tr->buffer_disabled;
1268}
1269
499e5470
SR
1270/**
1271 * tracing_is_on - show state of ring buffers enabled
1272 */
1273int tracing_is_on(void)
1274{
10246fa3 1275 return tracer_tracing_is_on(&global_trace);
499e5470
SR
1276}
1277EXPORT_SYMBOL_GPL(tracing_is_on);
1278
3928a8a2 1279static int __init set_buf_size(char *str)
bc0c38d1 1280{
3928a8a2 1281 unsigned long buf_size;
c6caeeb1 1282
bc0c38d1
SR
1283 if (!str)
1284 return 0;
9d612bef 1285 buf_size = memparse(str, &str);
c6caeeb1 1286 /* nr_entries can not be zero */
9d612bef 1287 if (buf_size == 0)
c6caeeb1 1288 return 0;
3928a8a2 1289 trace_buf_size = buf_size;
bc0c38d1
SR
1290 return 1;
1291}
3928a8a2 1292__setup("trace_buf_size=", set_buf_size);
bc0c38d1 1293
0e950173
TB
1294static int __init set_tracing_thresh(char *str)
1295{
87abb3b1 1296 unsigned long threshold;
0e950173
TB
1297 int ret;
1298
1299 if (!str)
1300 return 0;
bcd83ea6 1301 ret = kstrtoul(str, 0, &threshold);
0e950173
TB
1302 if (ret < 0)
1303 return 0;
87abb3b1 1304 tracing_thresh = threshold * 1000;
0e950173
TB
1305 return 1;
1306}
1307__setup("tracing_thresh=", set_tracing_thresh);
1308
57f50be1
SR
1309unsigned long nsecs_to_usecs(unsigned long nsecs)
1310{
1311 return nsecs / 1000;
1312}
1313
a3418a36
SRRH
1314/*
1315 * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
f57a4143 1316 * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
a3418a36 1317 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
f57a4143 1318 * of strings in the order that the evals (enum) were defined.
a3418a36
SRRH
1319 */
1320#undef C
1321#define C(a, b) b
1322
4fcdae83 1323/* These must match the bit postions in trace_iterator_flags */
bc0c38d1 1324static const char *trace_options[] = {
a3418a36 1325 TRACE_FLAGS
bc0c38d1
SR
1326 NULL
1327};
1328
5079f326
Z
1329static struct {
1330 u64 (*func)(void);
1331 const char *name;
8be0709f 1332 int in_ns; /* is this clock in nanoseconds? */
5079f326 1333} trace_clocks[] = {
1b3e5c09
TG
1334 { trace_clock_local, "local", 1 },
1335 { trace_clock_global, "global", 1 },
1336 { trace_clock_counter, "counter", 0 },
e7fda6c4 1337 { trace_clock_jiffies, "uptime", 0 },
1b3e5c09
TG
1338 { trace_clock, "perf", 1 },
1339 { ktime_get_mono_fast_ns, "mono", 1 },
aabfa5f2 1340 { ktime_get_raw_fast_ns, "mono_raw", 1 },
a3ed0e43 1341 { ktime_get_boot_fast_ns, "boot", 1 },
8cbd9cc6 1342 ARCH_TRACE_CLOCKS
5079f326
Z
1343};
1344
860f9f6b
TZ
1345bool trace_clock_in_ns(struct trace_array *tr)
1346{
1347 if (trace_clocks[tr->clock_id].in_ns)
1348 return true;
1349
1350 return false;
1351}
1352
b63f39ea 1353/*
1354 * trace_parser_get_init - gets the buffer for trace parser
1355 */
1356int trace_parser_get_init(struct trace_parser *parser, int size)
1357{
1358 memset(parser, 0, sizeof(*parser));
1359
1360 parser->buffer = kmalloc(size, GFP_KERNEL);
1361 if (!parser->buffer)
1362 return 1;
1363
1364 parser->size = size;
1365 return 0;
1366}
1367
1368/*
1369 * trace_parser_put - frees the buffer for trace parser
1370 */
1371void trace_parser_put(struct trace_parser *parser)
1372{
1373 kfree(parser->buffer);
0e684b65 1374 parser->buffer = NULL;
b63f39ea 1375}
1376
1377/*
1378 * trace_get_user - reads the user input string separated by space
1379 * (matched by isspace(ch))
1380 *
1381 * For each string found the 'struct trace_parser' is updated,
1382 * and the function returns.
1383 *
1384 * Returns number of bytes read.
1385 *
1386 * See kernel/trace/trace.h for 'struct trace_parser' details.
1387 */
1388int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1389 size_t cnt, loff_t *ppos)
1390{
1391 char ch;
1392 size_t read = 0;
1393 ssize_t ret;
1394
1395 if (!*ppos)
1396 trace_parser_clear(parser);
1397
1398 ret = get_user(ch, ubuf++);
1399 if (ret)
1400 goto out;
1401
1402 read++;
1403 cnt--;
1404
1405 /*
1406 * The parser is not finished with the last write,
1407 * continue reading the user input without skipping spaces.
1408 */
1409 if (!parser->cont) {
1410 /* skip white space */
1411 while (cnt && isspace(ch)) {
1412 ret = get_user(ch, ubuf++);
1413 if (ret)
1414 goto out;
1415 read++;
1416 cnt--;
1417 }
1418
76638d96
CD
1419 parser->idx = 0;
1420
b63f39ea 1421 /* only spaces were written */
921a7acd 1422 if (isspace(ch) || !ch) {
b63f39ea 1423 *ppos += read;
1424 ret = read;
1425 goto out;
1426 }
b63f39ea 1427 }
1428
1429 /* read the non-space input */
921a7acd 1430 while (cnt && !isspace(ch) && ch) {
3c235a33 1431 if (parser->idx < parser->size - 1)
b63f39ea 1432 parser->buffer[parser->idx++] = ch;
1433 else {
1434 ret = -EINVAL;
1435 goto out;
1436 }
1437 ret = get_user(ch, ubuf++);
1438 if (ret)
1439 goto out;
1440 read++;
1441 cnt--;
1442 }
1443
1444 /* We either got finished input or we have to wait for another call. */
921a7acd 1445 if (isspace(ch) || !ch) {
b63f39ea 1446 parser->buffer[parser->idx] = 0;
1447 parser->cont = false;
057db848 1448 } else if (parser->idx < parser->size - 1) {
b63f39ea 1449 parser->cont = true;
1450 parser->buffer[parser->idx++] = ch;
f4d0706c
CD
1451 /* Make sure the parsed string always terminates with '\0'. */
1452 parser->buffer[parser->idx] = 0;
057db848
SR
1453 } else {
1454 ret = -EINVAL;
1455 goto out;
b63f39ea 1456 }
1457
1458 *ppos += read;
1459 ret = read;
1460
1461out:
1462 return ret;
1463}
1464
3a161d99 1465/* TODO add a seq_buf_to_buffer() */
b8b94265 1466static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
3c56819b
EGM
1467{
1468 int len;
3c56819b 1469
5ac48378 1470 if (trace_seq_used(s) <= s->seq.readpos)
3c56819b
EGM
1471 return -EBUSY;
1472
5ac48378 1473 len = trace_seq_used(s) - s->seq.readpos;
3c56819b
EGM
1474 if (cnt > len)
1475 cnt = len;
3a161d99 1476 memcpy(buf, s->buffer + s->seq.readpos, cnt);
3c56819b 1477
3a161d99 1478 s->seq.readpos += cnt;
3c56819b
EGM
1479 return cnt;
1480}
1481
0e950173
TB
1482unsigned long __read_mostly tracing_thresh;
1483
5d4a9dba 1484#ifdef CONFIG_TRACER_MAX_TRACE
5d4a9dba
SR
1485/*
1486 * Copy the new maximum trace into the separate maximum-trace
1487 * structure. (this way the maximum trace is permanently saved,
5a93bae2 1488 * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
5d4a9dba
SR
1489 */
1490static void
1491__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1492{
12883efb
SRRH
1493 struct trace_buffer *trace_buf = &tr->trace_buffer;
1494 struct trace_buffer *max_buf = &tr->max_buffer;
1495 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1496 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
5d4a9dba 1497
12883efb
SRRH
1498 max_buf->cpu = cpu;
1499 max_buf->time_start = data->preempt_timestamp;
5d4a9dba 1500
6d9b3fa5 1501 max_data->saved_latency = tr->max_latency;
8248ac05
SR
1502 max_data->critical_start = data->critical_start;
1503 max_data->critical_end = data->critical_end;
5d4a9dba 1504
85f726a3 1505 strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
8248ac05 1506 max_data->pid = tsk->pid;
f17a5194
SRRH
1507 /*
1508 * If tsk == current, then use current_uid(), as that does not use
1509 * RCU. The irq tracer can be called out of RCU scope.
1510 */
1511 if (tsk == current)
1512 max_data->uid = current_uid();
1513 else
1514 max_data->uid = task_uid(tsk);
1515
8248ac05
SR
1516 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1517 max_data->policy = tsk->policy;
1518 max_data->rt_priority = tsk->rt_priority;
5d4a9dba
SR
1519
1520 /* record this tasks comm */
1521 tracing_record_cmdline(tsk);
1522}
1523
4fcdae83
SR
1524/**
1525 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1526 * @tr: tracer
1527 * @tsk: the task with the latency
1528 * @cpu: The cpu that initiated the trace.
a35873a0 1529 * @cond_data: User data associated with a conditional snapshot
4fcdae83
SR
1530 *
1531 * Flip the buffers between the @tr and the max_tr and record information
1532 * about which task was the cause of this latency.
1533 */
e309b41d 1534void
a35873a0
TZ
1535update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
1536 void *cond_data)
bc0c38d1 1537{
2b6080f2 1538 if (tr->stop_count)
b8de7bd1
SR
1539 return;
1540
4c11d7ae 1541 WARN_ON_ONCE(!irqs_disabled());
34600f0e 1542
45ad21ca 1543 if (!tr->allocated_snapshot) {
debdd57f 1544 /* Only the nop tracer should hit this when disabling */
2b6080f2 1545 WARN_ON_ONCE(tr->current_trace != &nop_trace);
34600f0e 1546 return;
debdd57f 1547 }
34600f0e 1548
0b9b12c1 1549 arch_spin_lock(&tr->max_lock);
3928a8a2 1550
73c8d894
MH
1551 /* Inherit the recordable setting from trace_buffer */
1552 if (ring_buffer_record_is_set_on(tr->trace_buffer.buffer))
1553 ring_buffer_record_on(tr->max_buffer.buffer);
1554 else
1555 ring_buffer_record_off(tr->max_buffer.buffer);
1556
a35873a0
TZ
1557#ifdef CONFIG_TRACER_SNAPSHOT
1558 if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data))
1559 goto out_unlock;
1560#endif
08ae88f8 1561 swap(tr->trace_buffer.buffer, tr->max_buffer.buffer);
3928a8a2 1562
bc0c38d1 1563 __update_max_tr(tr, tsk, cpu);
a35873a0
TZ
1564
1565 out_unlock:
0b9b12c1 1566 arch_spin_unlock(&tr->max_lock);
bc0c38d1
SR
1567}
1568
1569/**
1570 * update_max_tr_single - only copy one trace over, and reset the rest
1571 * @tr - tracer
1572 * @tsk - task with the latency
1573 * @cpu - the cpu of the buffer to copy.
4fcdae83
SR
1574 *
1575 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
bc0c38d1 1576 */
e309b41d 1577void
bc0c38d1
SR
1578update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1579{
3928a8a2 1580 int ret;
bc0c38d1 1581
2b6080f2 1582 if (tr->stop_count)
b8de7bd1
SR
1583 return;
1584
4c11d7ae 1585 WARN_ON_ONCE(!irqs_disabled());
6c24499f 1586 if (!tr->allocated_snapshot) {
2930e04d 1587 /* Only the nop tracer should hit this when disabling */
9e8529af 1588 WARN_ON_ONCE(tr->current_trace != &nop_trace);
ef710e10 1589 return;
2930e04d 1590 }
ef710e10 1591
0b9b12c1 1592 arch_spin_lock(&tr->max_lock);
bc0c38d1 1593
12883efb 1594 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
3928a8a2 1595
e8165dbb
SR
1596 if (ret == -EBUSY) {
1597 /*
1598 * We failed to swap the buffer due to a commit taking
1599 * place on this CPU. We fail to record, but we reset
1600 * the max trace buffer (no one writes directly to it)
1601 * and flag that it failed.
1602 */
12883efb 1603 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
e8165dbb
SR
1604 "Failed to swap buffers due to commit in progress\n");
1605 }
1606
e8165dbb 1607 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
bc0c38d1
SR
1608
1609 __update_max_tr(tr, tsk, cpu);
0b9b12c1 1610 arch_spin_unlock(&tr->max_lock);
bc0c38d1 1611}
5d4a9dba 1612#endif /* CONFIG_TRACER_MAX_TRACE */
bc0c38d1 1613
2c2b0a78 1614static int wait_on_pipe(struct trace_iterator *iter, int full)
0d5c6e1c 1615{
15693458
SRRH
1616 /* Iterators are static, they should be filled or empty */
1617 if (trace_buffer_iter(iter, iter->cpu_file))
8b8b3683 1618 return 0;
0d5c6e1c 1619
e30f53aa
RV
1620 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1621 full);
0d5c6e1c
SR
1622}
1623
f4e781c0 1624#ifdef CONFIG_FTRACE_STARTUP_TEST
9afecfbb
SRV
1625static bool selftests_can_run;
1626
1627struct trace_selftests {
1628 struct list_head list;
1629 struct tracer *type;
1630};
1631
1632static LIST_HEAD(postponed_selftests);
1633
1634static int save_selftest(struct tracer *type)
1635{
1636 struct trace_selftests *selftest;
1637
1638 selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
1639 if (!selftest)
1640 return -ENOMEM;
1641
1642 selftest->type = type;
1643 list_add(&selftest->list, &postponed_selftests);
1644 return 0;
1645}
1646
f4e781c0
SRRH
1647static int run_tracer_selftest(struct tracer *type)
1648{
1649 struct trace_array *tr = &global_trace;
1650 struct tracer *saved_tracer = tr->current_trace;
1651 int ret;
0d5c6e1c 1652
f4e781c0
SRRH
1653 if (!type->selftest || tracing_selftest_disabled)
1654 return 0;
0d5c6e1c 1655
9afecfbb
SRV
1656 /*
1657 * If a tracer registers early in boot up (before scheduling is
1658 * initialized and such), then do not run its selftests yet.
1659 * Instead, run it a little later in the boot process.
1660 */
1661 if (!selftests_can_run)
1662 return save_selftest(type);
1663
0d5c6e1c 1664 /*
f4e781c0
SRRH
1665 * Run a selftest on this tracer.
1666 * Here we reset the trace buffer, and set the current
1667 * tracer to be this tracer. The tracer can then run some
1668 * internal tracing to verify that everything is in order.
1669 * If we fail, we do not register this tracer.
0d5c6e1c 1670 */
f4e781c0 1671 tracing_reset_online_cpus(&tr->trace_buffer);
0d5c6e1c 1672
f4e781c0
SRRH
1673 tr->current_trace = type;
1674
1675#ifdef CONFIG_TRACER_MAX_TRACE
1676 if (type->use_max_tr) {
1677 /* If we expanded the buffers, make sure the max is expanded too */
1678 if (ring_buffer_expanded)
1679 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1680 RING_BUFFER_ALL_CPUS);
1681 tr->allocated_snapshot = true;
1682 }
1683#endif
1684
1685 /* the test is responsible for initializing and enabling */
1686 pr_info("Testing tracer %s: ", type->name);
1687 ret = type->selftest(type, tr);
1688 /* the test is responsible for resetting too */
1689 tr->current_trace = saved_tracer;
1690 if (ret) {
1691 printk(KERN_CONT "FAILED!\n");
1692 /* Add the warning after printing 'FAILED' */
1693 WARN_ON(1);
1694 return -1;
1695 }
1696 /* Only reset on passing, to avoid touching corrupted buffers */
1697 tracing_reset_online_cpus(&tr->trace_buffer);
1698
1699#ifdef CONFIG_TRACER_MAX_TRACE
1700 if (type->use_max_tr) {
1701 tr->allocated_snapshot = false;
0d5c6e1c 1702
f4e781c0
SRRH
1703 /* Shrink the max buffer again */
1704 if (ring_buffer_expanded)
1705 ring_buffer_resize(tr->max_buffer.buffer, 1,
1706 RING_BUFFER_ALL_CPUS);
1707 }
1708#endif
1709
1710 printk(KERN_CONT "PASSED\n");
1711 return 0;
1712}
9afecfbb
SRV
1713
1714static __init int init_trace_selftests(void)
1715{
1716 struct trace_selftests *p, *n;
1717 struct tracer *t, **last;
1718 int ret;
1719
1720 selftests_can_run = true;
1721
1722 mutex_lock(&trace_types_lock);
1723
1724 if (list_empty(&postponed_selftests))
1725 goto out;
1726
1727 pr_info("Running postponed tracer tests:\n");
1728
1729 list_for_each_entry_safe(p, n, &postponed_selftests, list) {
6fc2171c
AR
1730 /* This loop can take minutes when sanitizers are enabled, so
1731 * lets make sure we allow RCU processing.
1732 */
1733 cond_resched();
9afecfbb
SRV
1734 ret = run_tracer_selftest(p->type);
1735 /* If the test fails, then warn and remove from available_tracers */
1736 if (ret < 0) {
1737 WARN(1, "tracer: %s failed selftest, disabling\n",
1738 p->type->name);
1739 last = &trace_types;
1740 for (t = trace_types; t; t = t->next) {
1741 if (t == p->type) {
1742 *last = t->next;
1743 break;
1744 }
1745 last = &t->next;
1746 }
1747 }
1748 list_del(&p->list);
1749 kfree(p);
1750 }
1751
1752 out:
1753 mutex_unlock(&trace_types_lock);
1754
1755 return 0;
1756}
b9ef0326 1757core_initcall(init_trace_selftests);
f4e781c0
SRRH
1758#else
1759static inline int run_tracer_selftest(struct tracer *type)
1760{
1761 return 0;
0d5c6e1c 1762}
f4e781c0 1763#endif /* CONFIG_FTRACE_STARTUP_TEST */
0d5c6e1c 1764
41d9c0be
SRRH
1765static void add_tracer_options(struct trace_array *tr, struct tracer *t);
1766
a4d1e688
JW
1767static void __init apply_trace_boot_options(void);
1768
4fcdae83
SR
1769/**
1770 * register_tracer - register a tracer with the ftrace system.
1771 * @type - the plugin for the tracer
1772 *
1773 * Register a new plugin tracer.
1774 */
a4d1e688 1775int __init register_tracer(struct tracer *type)
bc0c38d1
SR
1776{
1777 struct tracer *t;
bc0c38d1
SR
1778 int ret = 0;
1779
1780 if (!type->name) {
1781 pr_info("Tracer must have a name\n");
1782 return -1;
1783 }
1784
24a461d5 1785 if (strlen(type->name) >= MAX_TRACER_SIZE) {
ee6c2c1b
LZ
1786 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1787 return -1;
1788 }
1789
bc0c38d1 1790 mutex_lock(&trace_types_lock);
86fa2f60 1791
8e1b82e0
FW
1792 tracing_selftest_running = true;
1793
bc0c38d1
SR
1794 for (t = trace_types; t; t = t->next) {
1795 if (strcmp(type->name, t->name) == 0) {
1796 /* already found */
ee6c2c1b 1797 pr_info("Tracer %s already registered\n",
bc0c38d1
SR
1798 type->name);
1799 ret = -1;
1800 goto out;
1801 }
1802 }
1803
adf9f195
FW
1804 if (!type->set_flag)
1805 type->set_flag = &dummy_set_flag;
d39cdd20
CH
1806 if (!type->flags) {
1807 /*allocate a dummy tracer_flags*/
1808 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
c8ca003b
CH
1809 if (!type->flags) {
1810 ret = -ENOMEM;
1811 goto out;
1812 }
d39cdd20
CH
1813 type->flags->val = 0;
1814 type->flags->opts = dummy_tracer_opt;
1815 } else
adf9f195
FW
1816 if (!type->flags->opts)
1817 type->flags->opts = dummy_tracer_opt;
6eaaa5d5 1818
d39cdd20
CH
1819 /* store the tracer for __set_tracer_option */
1820 type->flags->trace = type;
1821
f4e781c0
SRRH
1822 ret = run_tracer_selftest(type);
1823 if (ret < 0)
1824 goto out;
60a11774 1825
bc0c38d1
SR
1826 type->next = trace_types;
1827 trace_types = type;
41d9c0be 1828 add_tracer_options(&global_trace, type);
60a11774 1829
bc0c38d1 1830 out:
8e1b82e0 1831 tracing_selftest_running = false;
bc0c38d1
SR
1832 mutex_unlock(&trace_types_lock);
1833
dac74940
SR
1834 if (ret || !default_bootup_tracer)
1835 goto out_unlock;
1836
ee6c2c1b 1837 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
dac74940
SR
1838 goto out_unlock;
1839
1840 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1841 /* Do we want this tracer to start on bootup? */
607e2ea1 1842 tracing_set_tracer(&global_trace, type->name);
dac74940 1843 default_bootup_tracer = NULL;
a4d1e688
JW
1844
1845 apply_trace_boot_options();
1846
dac74940 1847 /* disable other selftests, since this will break it. */
55034cd6 1848 tracing_selftest_disabled = true;
b2821ae6 1849#ifdef CONFIG_FTRACE_STARTUP_TEST
dac74940
SR
1850 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1851 type->name);
b2821ae6 1852#endif
b2821ae6 1853
dac74940 1854 out_unlock:
bc0c38d1
SR
1855 return ret;
1856}
1857
12883efb 1858void tracing_reset(struct trace_buffer *buf, int cpu)
f633903a 1859{
12883efb 1860 struct ring_buffer *buffer = buf->buffer;
f633903a 1861
a5416411
HT
1862 if (!buffer)
1863 return;
1864
f633903a
SR
1865 ring_buffer_record_disable(buffer);
1866
1867 /* Make sure all commits have finished */
74401729 1868 synchronize_rcu();
68179686 1869 ring_buffer_reset_cpu(buffer, cpu);
f633903a
SR
1870
1871 ring_buffer_record_enable(buffer);
1872}
1873
12883efb 1874void tracing_reset_online_cpus(struct trace_buffer *buf)
213cc060 1875{
12883efb 1876 struct ring_buffer *buffer = buf->buffer;
213cc060
PE
1877 int cpu;
1878
a5416411
HT
1879 if (!buffer)
1880 return;
1881
621968cd
SR
1882 ring_buffer_record_disable(buffer);
1883
1884 /* Make sure all commits have finished */
74401729 1885 synchronize_rcu();
621968cd 1886
9457158b 1887 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
213cc060
PE
1888
1889 for_each_online_cpu(cpu)
68179686 1890 ring_buffer_reset_cpu(buffer, cpu);
621968cd
SR
1891
1892 ring_buffer_record_enable(buffer);
213cc060
PE
1893}
1894
09d8091c 1895/* Must have trace_types_lock held */
873c642f 1896void tracing_reset_all_online_cpus(void)
9456f0fa 1897{
873c642f
SRRH
1898 struct trace_array *tr;
1899
873c642f 1900 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
065e63f9
SRV
1901 if (!tr->clear_trace)
1902 continue;
1903 tr->clear_trace = false;
12883efb
SRRH
1904 tracing_reset_online_cpus(&tr->trace_buffer);
1905#ifdef CONFIG_TRACER_MAX_TRACE
1906 tracing_reset_online_cpus(&tr->max_buffer);
1907#endif
873c642f 1908 }
9456f0fa
SR
1909}
1910
d914ba37
JF
1911static int *tgid_map;
1912
939c7a4f 1913#define SAVED_CMDLINES_DEFAULT 128
2c7eea4c 1914#define NO_CMDLINE_MAP UINT_MAX
edc35bd7 1915static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
939c7a4f
YY
1916struct saved_cmdlines_buffer {
1917 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1918 unsigned *map_cmdline_to_pid;
1919 unsigned cmdline_num;
1920 int cmdline_idx;
1921 char *saved_cmdlines;
1922};
1923static struct saved_cmdlines_buffer *savedcmd;
25b0b44a 1924
25b0b44a 1925/* temporary disable recording */
d914ba37 1926static atomic_t trace_record_taskinfo_disabled __read_mostly;
bc0c38d1 1927
939c7a4f
YY
1928static inline char *get_saved_cmdlines(int idx)
1929{
1930 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
1931}
1932
1933static inline void set_cmdline(int idx, const char *cmdline)
bc0c38d1 1934{
85f726a3 1935 strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
939c7a4f
YY
1936}
1937
1938static int allocate_cmdlines_buffer(unsigned int val,
1939 struct saved_cmdlines_buffer *s)
1940{
6da2ec56
KC
1941 s->map_cmdline_to_pid = kmalloc_array(val,
1942 sizeof(*s->map_cmdline_to_pid),
1943 GFP_KERNEL);
939c7a4f
YY
1944 if (!s->map_cmdline_to_pid)
1945 return -ENOMEM;
1946
6da2ec56 1947 s->saved_cmdlines = kmalloc_array(TASK_COMM_LEN, val, GFP_KERNEL);
939c7a4f
YY
1948 if (!s->saved_cmdlines) {
1949 kfree(s->map_cmdline_to_pid);
1950 return -ENOMEM;
1951 }
1952
1953 s->cmdline_idx = 0;
1954 s->cmdline_num = val;
1955 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
1956 sizeof(s->map_pid_to_cmdline));
1957 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
1958 val * sizeof(*s->map_cmdline_to_pid));
1959
1960 return 0;
1961}
1962
1963static int trace_create_savedcmd(void)
1964{
1965 int ret;
1966
a6af8fbf 1967 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
939c7a4f
YY
1968 if (!savedcmd)
1969 return -ENOMEM;
1970
1971 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
1972 if (ret < 0) {
1973 kfree(savedcmd);
1974 savedcmd = NULL;
1975 return -ENOMEM;
1976 }
1977
1978 return 0;
bc0c38d1
SR
1979}
1980
b5130b1e
CE
1981int is_tracing_stopped(void)
1982{
2b6080f2 1983 return global_trace.stop_count;
b5130b1e
CE
1984}
1985
0f048701
SR
1986/**
1987 * tracing_start - quick start of the tracer
1988 *
1989 * If tracing is enabled but was stopped by tracing_stop,
1990 * this will start the tracer back up.
1991 */
1992void tracing_start(void)
1993{
1994 struct ring_buffer *buffer;
1995 unsigned long flags;
1996
1997 if (tracing_disabled)
1998 return;
1999
2b6080f2
SR
2000 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
2001 if (--global_trace.stop_count) {
2002 if (global_trace.stop_count < 0) {
b06a8301
SR
2003 /* Someone screwed up their debugging */
2004 WARN_ON_ONCE(1);
2b6080f2 2005 global_trace.stop_count = 0;
b06a8301 2006 }
0f048701
SR
2007 goto out;
2008 }
2009
a2f80714 2010 /* Prevent the buffers from switching */
0b9b12c1 2011 arch_spin_lock(&global_trace.max_lock);
0f048701 2012
12883efb 2013 buffer = global_trace.trace_buffer.buffer;
0f048701
SR
2014 if (buffer)
2015 ring_buffer_record_enable(buffer);
2016
12883efb
SRRH
2017#ifdef CONFIG_TRACER_MAX_TRACE
2018 buffer = global_trace.max_buffer.buffer;
0f048701
SR
2019 if (buffer)
2020 ring_buffer_record_enable(buffer);
12883efb 2021#endif
0f048701 2022
0b9b12c1 2023 arch_spin_unlock(&global_trace.max_lock);
a2f80714 2024
0f048701 2025 out:
2b6080f2
SR
2026 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2027}
2028
2029static void tracing_start_tr(struct trace_array *tr)
2030{
2031 struct ring_buffer *buffer;
2032 unsigned long flags;
2033
2034 if (tracing_disabled)
2035 return;
2036
2037 /* If global, we need to also start the max tracer */
2038 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2039 return tracing_start();
2040
2041 raw_spin_lock_irqsave(&tr->start_lock, flags);
2042
2043 if (--tr->stop_count) {
2044 if (tr->stop_count < 0) {
2045 /* Someone screwed up their debugging */
2046 WARN_ON_ONCE(1);
2047 tr->stop_count = 0;
2048 }
2049 goto out;
2050 }
2051
12883efb 2052 buffer = tr->trace_buffer.buffer;
2b6080f2
SR
2053 if (buffer)
2054 ring_buffer_record_enable(buffer);
2055
2056 out:
2057 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
0f048701
SR
2058}
2059
2060/**
2061 * tracing_stop - quick stop of the tracer
2062 *
2063 * Light weight way to stop tracing. Use in conjunction with
2064 * tracing_start.
2065 */
2066void tracing_stop(void)
2067{
2068 struct ring_buffer *buffer;
2069 unsigned long flags;
2070
2b6080f2
SR
2071 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
2072 if (global_trace.stop_count++)
0f048701
SR
2073 goto out;
2074
a2f80714 2075 /* Prevent the buffers from switching */
0b9b12c1 2076 arch_spin_lock(&global_trace.max_lock);
a2f80714 2077
12883efb 2078 buffer = global_trace.trace_buffer.buffer;
0f048701
SR
2079 if (buffer)
2080 ring_buffer_record_disable(buffer);
2081
12883efb
SRRH
2082#ifdef CONFIG_TRACER_MAX_TRACE
2083 buffer = global_trace.max_buffer.buffer;
0f048701
SR
2084 if (buffer)
2085 ring_buffer_record_disable(buffer);
12883efb 2086#endif
0f048701 2087
0b9b12c1 2088 arch_spin_unlock(&global_trace.max_lock);
a2f80714 2089
0f048701 2090 out:
2b6080f2
SR
2091 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2092}
2093
2094static void tracing_stop_tr(struct trace_array *tr)
2095{
2096 struct ring_buffer *buffer;
2097 unsigned long flags;
2098
2099 /* If global, we need to also stop the max tracer */
2100 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2101 return tracing_stop();
2102
2103 raw_spin_lock_irqsave(&tr->start_lock, flags);
2104 if (tr->stop_count++)
2105 goto out;
2106
12883efb 2107 buffer = tr->trace_buffer.buffer;
2b6080f2
SR
2108 if (buffer)
2109 ring_buffer_record_disable(buffer);
2110
2111 out:
2112 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
0f048701
SR
2113}
2114
379cfdac 2115static int trace_save_cmdline(struct task_struct *tsk)
bc0c38d1 2116{
a635cf04 2117 unsigned pid, idx;
bc0c38d1 2118
eaf260ac
JF
2119 /* treat recording of idle task as a success */
2120 if (!tsk->pid)
2121 return 1;
2122
2123 if (unlikely(tsk->pid > PID_MAX_DEFAULT))
379cfdac 2124 return 0;
bc0c38d1
SR
2125
2126 /*
2127 * It's not the end of the world if we don't get
2128 * the lock, but we also don't want to spin
2129 * nor do we want to disable interrupts,
2130 * so if we miss here, then better luck next time.
2131 */
0199c4e6 2132 if (!arch_spin_trylock(&trace_cmdline_lock))
379cfdac 2133 return 0;
bc0c38d1 2134
939c7a4f 2135 idx = savedcmd->map_pid_to_cmdline[tsk->pid];
2c7eea4c 2136 if (idx == NO_CMDLINE_MAP) {
939c7a4f 2137 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
bc0c38d1 2138
a635cf04
CE
2139 /*
2140 * Check whether the cmdline buffer at idx has a pid
2141 * mapped. We are going to overwrite that entry so we
2142 * need to clear the map_pid_to_cmdline. Otherwise we
2143 * would read the new comm for the old pid.
2144 */
939c7a4f 2145 pid = savedcmd->map_cmdline_to_pid[idx];
a635cf04 2146 if (pid != NO_CMDLINE_MAP)
939c7a4f 2147 savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
bc0c38d1 2148
939c7a4f
YY
2149 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
2150 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
bc0c38d1 2151
939c7a4f 2152 savedcmd->cmdline_idx = idx;
bc0c38d1
SR
2153 }
2154
939c7a4f 2155 set_cmdline(idx, tsk->comm);
bc0c38d1 2156
0199c4e6 2157 arch_spin_unlock(&trace_cmdline_lock);
379cfdac
SRRH
2158
2159 return 1;
bc0c38d1
SR
2160}
2161
4c27e756 2162static void __trace_find_cmdline(int pid, char comm[])
bc0c38d1 2163{
bc0c38d1
SR
2164 unsigned map;
2165
4ca53085
SR
2166 if (!pid) {
2167 strcpy(comm, "<idle>");
2168 return;
2169 }
bc0c38d1 2170
74bf4076
SR
2171 if (WARN_ON_ONCE(pid < 0)) {
2172 strcpy(comm, "<XXX>");
2173 return;
2174 }
2175
4ca53085
SR
2176 if (pid > PID_MAX_DEFAULT) {
2177 strcpy(comm, "<...>");
2178 return;
2179 }
bc0c38d1 2180
939c7a4f 2181 map = savedcmd->map_pid_to_cmdline[pid];
50d88758 2182 if (map != NO_CMDLINE_MAP)
e09e2867 2183 strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
50d88758
TG
2184 else
2185 strcpy(comm, "<...>");
4c27e756
SRRH
2186}
2187
2188void trace_find_cmdline(int pid, char comm[])
2189{
2190 preempt_disable();
2191 arch_spin_lock(&trace_cmdline_lock);
2192
2193 __trace_find_cmdline(pid, comm);
bc0c38d1 2194
0199c4e6 2195 arch_spin_unlock(&trace_cmdline_lock);
5b6045a9 2196 preempt_enable();
bc0c38d1
SR
2197}
2198
d914ba37
JF
2199int trace_find_tgid(int pid)
2200{
2201 if (unlikely(!tgid_map || !pid || pid > PID_MAX_DEFAULT))
2202 return 0;
2203
2204 return tgid_map[pid];
2205}
2206
2207static int trace_save_tgid(struct task_struct *tsk)
2208{
bd45d34d
JF
2209 /* treat recording of idle task as a success */
2210 if (!tsk->pid)
2211 return 1;
2212
2213 if (unlikely(!tgid_map || tsk->pid > PID_MAX_DEFAULT))
d914ba37
JF
2214 return 0;
2215
2216 tgid_map[tsk->pid] = tsk->tgid;
2217 return 1;
2218}
2219
2220static bool tracing_record_taskinfo_skip(int flags)
2221{
2222 if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID))))
2223 return true;
2224 if (atomic_read(&trace_record_taskinfo_disabled) || !tracing_is_on())
2225 return true;
2226 if (!__this_cpu_read(trace_taskinfo_save))
2227 return true;
2228 return false;
2229}
2230
2231/**
2232 * tracing_record_taskinfo - record the task info of a task
2233 *
2234 * @task - task to record
2235 * @flags - TRACE_RECORD_CMDLINE for recording comm
2236 * - TRACE_RECORD_TGID for recording tgid
2237 */
2238void tracing_record_taskinfo(struct task_struct *task, int flags)
2239{
29b1a8ad
JF
2240 bool done;
2241
d914ba37
JF
2242 if (tracing_record_taskinfo_skip(flags))
2243 return;
29b1a8ad
JF
2244
2245 /*
2246 * Record as much task information as possible. If some fail, continue
2247 * to try to record the others.
2248 */
2249 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task);
2250 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task);
2251
2252 /* If recording any information failed, retry again soon. */
2253 if (!done)
d914ba37
JF
2254 return;
2255
2256 __this_cpu_write(trace_taskinfo_save, false);
2257}
2258
2259/**
2260 * tracing_record_taskinfo_sched_switch - record task info for sched_switch
2261 *
2262 * @prev - previous task during sched_switch
2263 * @next - next task during sched_switch
2264 * @flags - TRACE_RECORD_CMDLINE for recording comm
2265 * TRACE_RECORD_TGID for recording tgid
2266 */
2267void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
2268 struct task_struct *next, int flags)
bc0c38d1 2269{
29b1a8ad
JF
2270 bool done;
2271
d914ba37
JF
2272 if (tracing_record_taskinfo_skip(flags))
2273 return;
2274
29b1a8ad
JF
2275 /*
2276 * Record as much task information as possible. If some fail, continue
2277 * to try to record the others.
2278 */
2279 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev);
2280 done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next);
2281 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev);
2282 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next);
bc0c38d1 2283
29b1a8ad
JF
2284 /* If recording any information failed, retry again soon. */
2285 if (!done)
7ffbd48d
SR
2286 return;
2287
d914ba37
JF
2288 __this_cpu_write(trace_taskinfo_save, false);
2289}
2290
2291/* Helpers to record a specific task information */
2292void tracing_record_cmdline(struct task_struct *task)
2293{
2294 tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE);
2295}
2296
2297void tracing_record_tgid(struct task_struct *task)
2298{
2299 tracing_record_taskinfo(task, TRACE_RECORD_TGID);
bc0c38d1
SR
2300}
2301
af0009fc
SRV
2302/*
2303 * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
2304 * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
2305 * simplifies those functions and keeps them in sync.
2306 */
2307enum print_line_t trace_handle_return(struct trace_seq *s)
2308{
2309 return trace_seq_has_overflowed(s) ?
2310 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2311}
2312EXPORT_SYMBOL_GPL(trace_handle_return);
2313
45dcd8b8 2314void
38697053
SR
2315tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
2316 int pc)
bc0c38d1
SR
2317{
2318 struct task_struct *tsk = current;
bc0c38d1 2319
777e208d
SR
2320 entry->preempt_count = pc & 0xff;
2321 entry->pid = (tsk) ? tsk->pid : 0;
2322 entry->flags =
9244489a 2323#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
2e2ca155 2324 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
9244489a
SR
2325#else
2326 TRACE_FLAG_IRQS_NOSUPPORT |
2327#endif
7e6867bf 2328 ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
bc0c38d1 2329 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
c59f29cb 2330 ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
e5137b50
PZ
2331 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
2332 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
bc0c38d1 2333}
f413cdb8 2334EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
bc0c38d1 2335
e77405ad
SR
2336struct ring_buffer_event *
2337trace_buffer_lock_reserve(struct ring_buffer *buffer,
2338 int type,
2339 unsigned long len,
2340 unsigned long flags, int pc)
51a763dd 2341{
3e9a8aad 2342 return __trace_buffer_lock_reserve(buffer, type, len, flags, pc);
0fc1b09f
SRRH
2343}
2344
2345DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2346DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2347static int trace_buffered_event_ref;
2348
2349/**
2350 * trace_buffered_event_enable - enable buffering events
2351 *
2352 * When events are being filtered, it is quicker to use a temporary
2353 * buffer to write the event data into if there's a likely chance
2354 * that it will not be committed. The discard of the ring buffer
2355 * is not as fast as committing, and is much slower than copying
2356 * a commit.
2357 *
2358 * When an event is to be filtered, allocate per cpu buffers to
2359 * write the event data into, and if the event is filtered and discarded
2360 * it is simply dropped, otherwise, the entire data is to be committed
2361 * in one shot.
2362 */
2363void trace_buffered_event_enable(void)
2364{
2365 struct ring_buffer_event *event;
2366 struct page *page;
2367 int cpu;
51a763dd 2368
0fc1b09f
SRRH
2369 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2370
2371 if (trace_buffered_event_ref++)
2372 return;
2373
2374 for_each_tracing_cpu(cpu) {
2375 page = alloc_pages_node(cpu_to_node(cpu),
2376 GFP_KERNEL | __GFP_NORETRY, 0);
2377 if (!page)
2378 goto failed;
2379
2380 event = page_address(page);
2381 memset(event, 0, sizeof(*event));
2382
2383 per_cpu(trace_buffered_event, cpu) = event;
2384
2385 preempt_disable();
2386 if (cpu == smp_processor_id() &&
2387 this_cpu_read(trace_buffered_event) !=
2388 per_cpu(trace_buffered_event, cpu))
2389 WARN_ON_ONCE(1);
2390 preempt_enable();
51a763dd
ACM
2391 }
2392
0fc1b09f
SRRH
2393 return;
2394 failed:
2395 trace_buffered_event_disable();
2396}
2397
2398static void enable_trace_buffered_event(void *data)
2399{
2400 /* Probably not needed, but do it anyway */
2401 smp_rmb();
2402 this_cpu_dec(trace_buffered_event_cnt);
2403}
2404
2405static void disable_trace_buffered_event(void *data)
2406{
2407 this_cpu_inc(trace_buffered_event_cnt);
2408}
2409
2410/**
2411 * trace_buffered_event_disable - disable buffering events
2412 *
2413 * When a filter is removed, it is faster to not use the buffered
2414 * events, and to commit directly into the ring buffer. Free up
2415 * the temp buffers when there are no more users. This requires
2416 * special synchronization with current events.
2417 */
2418void trace_buffered_event_disable(void)
2419{
2420 int cpu;
2421
2422 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2423
2424 if (WARN_ON_ONCE(!trace_buffered_event_ref))
2425 return;
2426
2427 if (--trace_buffered_event_ref)
2428 return;
2429
2430 preempt_disable();
2431 /* For each CPU, set the buffer as used. */
2432 smp_call_function_many(tracing_buffer_mask,
2433 disable_trace_buffered_event, NULL, 1);
2434 preempt_enable();
2435
2436 /* Wait for all current users to finish */
74401729 2437 synchronize_rcu();
0fc1b09f
SRRH
2438
2439 for_each_tracing_cpu(cpu) {
2440 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2441 per_cpu(trace_buffered_event, cpu) = NULL;
2442 }
2443 /*
2444 * Make sure trace_buffered_event is NULL before clearing
2445 * trace_buffered_event_cnt.
2446 */
2447 smp_wmb();
2448
2449 preempt_disable();
2450 /* Do the work on each cpu */
2451 smp_call_function_many(tracing_buffer_mask,
2452 enable_trace_buffered_event, NULL, 1);
2453 preempt_enable();
51a763dd 2454}
51a763dd 2455
2c4a33ab
SRRH
2456static struct ring_buffer *temp_buffer;
2457
ccb469a1
SR
2458struct ring_buffer_event *
2459trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
7f1d2f82 2460 struct trace_event_file *trace_file,
ccb469a1
SR
2461 int type, unsigned long len,
2462 unsigned long flags, int pc)
2463{
2c4a33ab 2464 struct ring_buffer_event *entry;
0fc1b09f 2465 int val;
2c4a33ab 2466
7f1d2f82 2467 *current_rb = trace_file->tr->trace_buffer.buffer;
0fc1b09f 2468
00b41452 2469 if (!ring_buffer_time_stamp_abs(*current_rb) && (trace_file->flags &
0fc1b09f
SRRH
2470 (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
2471 (entry = this_cpu_read(trace_buffered_event))) {
2472 /* Try to use the per cpu buffer first */
2473 val = this_cpu_inc_return(trace_buffered_event_cnt);
2474 if (val == 1) {
2475 trace_event_setup(entry, type, flags, pc);
2476 entry->array[0] = len;
2477 return entry;
2478 }
2479 this_cpu_dec(trace_buffered_event_cnt);
2480 }
2481
3e9a8aad
SRRH
2482 entry = __trace_buffer_lock_reserve(*current_rb,
2483 type, len, flags, pc);
2c4a33ab
SRRH
2484 /*
2485 * If tracing is off, but we have triggers enabled
2486 * we still need to look at the event data. Use the temp_buffer
2487 * to store the trace event for the tigger to use. It's recusive
2488 * safe and will not be recorded anywhere.
2489 */
5d6ad960 2490 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
2c4a33ab 2491 *current_rb = temp_buffer;
3e9a8aad
SRRH
2492 entry = __trace_buffer_lock_reserve(*current_rb,
2493 type, len, flags, pc);
2c4a33ab
SRRH
2494 }
2495 return entry;
ccb469a1
SR
2496}
2497EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2498
42391745
SRRH
2499static DEFINE_SPINLOCK(tracepoint_iter_lock);
2500static DEFINE_MUTEX(tracepoint_printk_mutex);
2501
2502static void output_printk(struct trace_event_buffer *fbuffer)
2503{
2504 struct trace_event_call *event_call;
2505 struct trace_event *event;
2506 unsigned long flags;
2507 struct trace_iterator *iter = tracepoint_print_iter;
2508
2509 /* We should never get here if iter is NULL */
2510 if (WARN_ON_ONCE(!iter))
2511 return;
2512
2513 event_call = fbuffer->trace_file->event_call;
2514 if (!event_call || !event_call->event.funcs ||
2515 !event_call->event.funcs->trace)
2516 return;
2517
2518 event = &fbuffer->trace_file->event_call->event;
2519
2520 spin_lock_irqsave(&tracepoint_iter_lock, flags);
2521 trace_seq_init(&iter->seq);
2522 iter->ent = fbuffer->entry;
2523 event_call->event.funcs->trace(iter, 0, event);
2524 trace_seq_putc(&iter->seq, 0);
2525 printk("%s", iter->seq.buffer);
2526
2527 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2528}
2529
2530int tracepoint_printk_sysctl(struct ctl_table *table, int write,
2531 void __user *buffer, size_t *lenp,
2532 loff_t *ppos)
2533{
2534 int save_tracepoint_printk;
2535 int ret;
2536
2537 mutex_lock(&tracepoint_printk_mutex);
2538 save_tracepoint_printk = tracepoint_printk;
2539
2540 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2541
2542 /*
2543 * This will force exiting early, as tracepoint_printk
2544 * is always zero when tracepoint_printk_iter is not allocated
2545 */
2546 if (!tracepoint_print_iter)
2547 tracepoint_printk = 0;
2548
2549 if (save_tracepoint_printk == tracepoint_printk)
2550 goto out;
2551
2552 if (tracepoint_printk)
2553 static_key_enable(&tracepoint_printk_key.key);
2554 else
2555 static_key_disable(&tracepoint_printk_key.key);
2556
2557 out:
2558 mutex_unlock(&tracepoint_printk_mutex);
2559
2560 return ret;
2561}
2562
2563void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2564{
2565 if (static_key_false(&tracepoint_printk_key.key))
2566 output_printk(fbuffer);
2567
2568 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
2569 fbuffer->event, fbuffer->entry,
2570 fbuffer->flags, fbuffer->pc);
2571}
2572EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2573
2ee5b92a
SRV
2574/*
2575 * Skip 3:
2576 *
2577 * trace_buffer_unlock_commit_regs()
2578 * trace_event_buffer_commit()
2579 * trace_event_raw_event_xxx()
13cf912b 2580 */
2ee5b92a
SRV
2581# define STACK_SKIP 3
2582
b7f0c959
SRRH
2583void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2584 struct ring_buffer *buffer,
0d5c6e1c
SR
2585 struct ring_buffer_event *event,
2586 unsigned long flags, int pc,
2587 struct pt_regs *regs)
1fd8df2c 2588{
7ffbd48d 2589 __buffer_unlock_commit(buffer, event);
1fd8df2c 2590
be54f69c 2591 /*
2ee5b92a 2592 * If regs is not set, then skip the necessary functions.
be54f69c
SRRH
2593 * Note, we can still get here via blktrace, wakeup tracer
2594 * and mmiotrace, but that's ok if they lose a function or
2ee5b92a 2595 * two. They are not that meaningful.
be54f69c 2596 */
2ee5b92a 2597 ftrace_trace_stack(tr, buffer, flags, regs ? 0 : STACK_SKIP, pc, regs);
1fd8df2c
MH
2598 ftrace_trace_userstack(buffer, flags, pc);
2599}
1fd8df2c 2600
52ffabe3
SRRH
2601/*
2602 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2603 */
2604void
2605trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer,
2606 struct ring_buffer_event *event)
2607{
2608 __buffer_unlock_commit(buffer, event);
2609}
2610
478409dd
CZ
2611static void
2612trace_process_export(struct trace_export *export,
2613 struct ring_buffer_event *event)
2614{
2615 struct trace_entry *entry;
2616 unsigned int size = 0;
2617
2618 entry = ring_buffer_event_data(event);
2619 size = ring_buffer_event_length(event);
a773d419 2620 export->write(export, entry, size);
478409dd
CZ
2621}
2622
2623static DEFINE_MUTEX(ftrace_export_lock);
2624
2625static struct trace_export __rcu *ftrace_exports_list __read_mostly;
2626
2627static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled);
2628
2629static inline void ftrace_exports_enable(void)
2630{
2631 static_branch_enable(&ftrace_exports_enabled);
2632}
2633
2634static inline void ftrace_exports_disable(void)
2635{
2636 static_branch_disable(&ftrace_exports_enabled);
2637}
2638
1cce377d 2639static void ftrace_exports(struct ring_buffer_event *event)
478409dd
CZ
2640{
2641 struct trace_export *export;
2642
2643 preempt_disable_notrace();
2644
2645 export = rcu_dereference_raw_notrace(ftrace_exports_list);
2646 while (export) {
2647 trace_process_export(export, event);
2648 export = rcu_dereference_raw_notrace(export->next);
2649 }
2650
2651 preempt_enable_notrace();
2652}
2653
2654static inline void
2655add_trace_export(struct trace_export **list, struct trace_export *export)
2656{
2657 rcu_assign_pointer(export->next, *list);
2658 /*
2659 * We are entering export into the list but another
2660 * CPU might be walking that list. We need to make sure
2661 * the export->next pointer is valid before another CPU sees
2662 * the export pointer included into the list.
2663 */
2664 rcu_assign_pointer(*list, export);
2665}
2666
2667static inline int
2668rm_trace_export(struct trace_export **list, struct trace_export *export)
2669{
2670 struct trace_export **p;
2671
2672 for (p = list; *p != NULL; p = &(*p)->next)
2673 if (*p == export)
2674 break;
2675
2676 if (*p != export)
2677 return -1;
2678
2679 rcu_assign_pointer(*p, (*p)->next);
2680
2681 return 0;
2682}
2683
2684static inline void
2685add_ftrace_export(struct trace_export **list, struct trace_export *export)
2686{
2687 if (*list == NULL)
2688 ftrace_exports_enable();
2689
2690 add_trace_export(list, export);
2691}
2692
2693static inline int
2694rm_ftrace_export(struct trace_export **list, struct trace_export *export)
2695{
2696 int ret;
2697
2698 ret = rm_trace_export(list, export);
2699 if (*list == NULL)
2700 ftrace_exports_disable();
2701
2702 return ret;
2703}
2704
2705int register_ftrace_export(struct trace_export *export)
2706{
2707 if (WARN_ON_ONCE(!export->write))
2708 return -1;
2709
2710 mutex_lock(&ftrace_export_lock);
2711
2712 add_ftrace_export(&ftrace_exports_list, export);
2713
2714 mutex_unlock(&ftrace_export_lock);
2715
2716 return 0;
2717}
2718EXPORT_SYMBOL_GPL(register_ftrace_export);
2719
2720int unregister_ftrace_export(struct trace_export *export)
2721{
2722 int ret;
2723
2724 mutex_lock(&ftrace_export_lock);
2725
2726 ret = rm_ftrace_export(&ftrace_exports_list, export);
2727
2728 mutex_unlock(&ftrace_export_lock);
2729
2730 return ret;
2731}
2732EXPORT_SYMBOL_GPL(unregister_ftrace_export);
2733
e309b41d 2734void
7be42151 2735trace_function(struct trace_array *tr,
38697053
SR
2736 unsigned long ip, unsigned long parent_ip, unsigned long flags,
2737 int pc)
bc0c38d1 2738{
2425bcb9 2739 struct trace_event_call *call = &event_function;
12883efb 2740 struct ring_buffer *buffer = tr->trace_buffer.buffer;
3928a8a2 2741 struct ring_buffer_event *event;
777e208d 2742 struct ftrace_entry *entry;
bc0c38d1 2743
3e9a8aad
SRRH
2744 event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
2745 flags, pc);
3928a8a2
SR
2746 if (!event)
2747 return;
2748 entry = ring_buffer_event_data(event);
777e208d
SR
2749 entry->ip = ip;
2750 entry->parent_ip = parent_ip;
e1112b4d 2751
478409dd
CZ
2752 if (!call_filter_check_discard(call, entry, buffer, event)) {
2753 if (static_branch_unlikely(&ftrace_exports_enabled))
2754 ftrace_exports(event);
7ffbd48d 2755 __buffer_unlock_commit(buffer, event);
478409dd 2756 }
bc0c38d1
SR
2757}
2758
c0a0d0d3 2759#ifdef CONFIG_STACKTRACE
4a9bd3f1 2760
2a820bf7
TG
2761/* Allow 4 levels of nesting: normal, softirq, irq, NMI */
2762#define FTRACE_KSTACK_NESTING 4
2763
2764#define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING)
2765
4a9bd3f1 2766struct ftrace_stack {
2a820bf7 2767 unsigned long calls[FTRACE_KSTACK_ENTRIES];
4a9bd3f1
SR
2768};
2769
2a820bf7
TG
2770
2771struct ftrace_stacks {
2772 struct ftrace_stack stacks[FTRACE_KSTACK_NESTING];
4a9bd3f1
SR
2773};
2774
2a820bf7 2775static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
4a9bd3f1
SR
2776static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2777
e77405ad 2778static void __ftrace_trace_stack(struct ring_buffer *buffer,
53614991 2779 unsigned long flags,
1fd8df2c 2780 int skip, int pc, struct pt_regs *regs)
86387f7e 2781{
2425bcb9 2782 struct trace_event_call *call = &event_kernel_stack;
3928a8a2 2783 struct ring_buffer_event *event;
ee6dd0db 2784 unsigned int size, nr_entries;
2a820bf7 2785 struct ftrace_stack *fstack;
777e208d 2786 struct stack_entry *entry;
2a820bf7 2787 int stackidx;
4a9bd3f1 2788
be54f69c 2789 /*
2ee5b92a 2790 * Add one, for this function and the call to save_stack_trace()
be54f69c
SRRH
2791 * If regs is set, then these functions will not be in the way.
2792 */
2ee5b92a 2793#ifndef CONFIG_UNWINDER_ORC
be54f69c 2794 if (!regs)
ee6dd0db 2795 skip++;
2ee5b92a 2796#endif
be54f69c 2797
4a9bd3f1
SR
2798 /*
2799 * Since events can happen in NMIs there's no safe way to
2800 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2801 * or NMI comes in, it will just have to use the default
2802 * FTRACE_STACK_SIZE.
2803 */
2804 preempt_disable_notrace();
2805
2a820bf7
TG
2806 stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
2807
2808 /* This should never happen. If it does, yell once and skip */
2809 if (WARN_ON_ONCE(stackidx > FTRACE_KSTACK_NESTING))
2810 goto out;
2811
4a9bd3f1 2812 /*
2a820bf7
TG
2813 * The above __this_cpu_inc_return() is 'atomic' cpu local. An
2814 * interrupt will either see the value pre increment or post
2815 * increment. If the interrupt happens pre increment it will have
2816 * restored the counter when it returns. We just need a barrier to
2817 * keep gcc from moving things around.
4a9bd3f1
SR
2818 */
2819 barrier();
4a9bd3f1 2820
2a820bf7 2821 fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
ee6dd0db 2822 size = ARRAY_SIZE(fstack->calls);
4a9bd3f1 2823
ee6dd0db
TG
2824 if (regs) {
2825 nr_entries = stack_trace_save_regs(regs, fstack->calls,
2826 size, skip);
2827 } else {
2828 nr_entries = stack_trace_save(fstack->calls, size, skip);
2829 }
86387f7e 2830
ee6dd0db 2831 size = nr_entries * sizeof(unsigned long);
3e9a8aad
SRRH
2832 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
2833 sizeof(*entry) + size, flags, pc);
3928a8a2 2834 if (!event)
4a9bd3f1
SR
2835 goto out;
2836 entry = ring_buffer_event_data(event);
86387f7e 2837
ee6dd0db
TG
2838 memcpy(&entry->caller, fstack->calls, size);
2839 entry->size = nr_entries;
86387f7e 2840
f306cc82 2841 if (!call_filter_check_discard(call, entry, buffer, event))
7ffbd48d 2842 __buffer_unlock_commit(buffer, event);
4a9bd3f1
SR
2843
2844 out:
2845 /* Again, don't let gcc optimize things here */
2846 barrier();
82146529 2847 __this_cpu_dec(ftrace_stack_reserve);
4a9bd3f1
SR
2848 preempt_enable_notrace();
2849
f0a920d5
IM
2850}
2851
2d34f489
SRRH
2852static inline void ftrace_trace_stack(struct trace_array *tr,
2853 struct ring_buffer *buffer,
73dddbb5
SRRH
2854 unsigned long flags,
2855 int skip, int pc, struct pt_regs *regs)
53614991 2856{
2d34f489 2857 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
53614991
SR
2858 return;
2859
73dddbb5 2860 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
53614991
SR
2861}
2862
c0a0d0d3
FW
2863void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
2864 int pc)
38697053 2865{
a33d7d94
SRV
2866 struct ring_buffer *buffer = tr->trace_buffer.buffer;
2867
2868 if (rcu_is_watching()) {
2869 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2870 return;
2871 }
2872
2873 /*
2874 * When an NMI triggers, RCU is enabled via rcu_nmi_enter(),
2875 * but if the above rcu_is_watching() failed, then the NMI
2876 * triggered someplace critical, and rcu_irq_enter() should
2877 * not be called from NMI.
2878 */
2879 if (unlikely(in_nmi()))
2880 return;
2881
a33d7d94
SRV
2882 rcu_irq_enter_irqson();
2883 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2884 rcu_irq_exit_irqson();
38697053
SR
2885}
2886
03889384
SR
2887/**
2888 * trace_dump_stack - record a stack back trace in the trace buffer
c142be8e 2889 * @skip: Number of functions to skip (helper handlers)
03889384 2890 */
c142be8e 2891void trace_dump_stack(int skip)
03889384
SR
2892{
2893 unsigned long flags;
2894
2895 if (tracing_disabled || tracing_selftest_running)
e36c5458 2896 return;
03889384
SR
2897
2898 local_save_flags(flags);
2899
2ee5b92a
SRV
2900#ifndef CONFIG_UNWINDER_ORC
2901 /* Skip 1 to skip this function. */
2902 skip++;
2903#endif
c142be8e
SRRH
2904 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
2905 flags, skip, preempt_count(), NULL);
03889384 2906}
da387e5c 2907EXPORT_SYMBOL_GPL(trace_dump_stack);
03889384 2908
c438f140 2909#ifdef CONFIG_USER_STACKTRACE_SUPPORT
91e86e56
SR
2910static DEFINE_PER_CPU(int, user_stack_count);
2911
c438f140 2912static void
e77405ad 2913ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
02b67518 2914{
2425bcb9 2915 struct trace_event_call *call = &event_user_stack;
8d7c6a96 2916 struct ring_buffer_event *event;
02b67518 2917 struct userstack_entry *entry;
02b67518 2918
983f938a 2919 if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE))
02b67518
TE
2920 return;
2921
b6345879
SR
2922 /*
2923 * NMIs can not handle page faults, even with fix ups.
2924 * The save user stack can (and often does) fault.
2925 */
2926 if (unlikely(in_nmi()))
2927 return;
02b67518 2928
91e86e56
SR
2929 /*
2930 * prevent recursion, since the user stack tracing may
2931 * trigger other kernel events.
2932 */
2933 preempt_disable();
2934 if (__this_cpu_read(user_stack_count))
2935 goto out;
2936
2937 __this_cpu_inc(user_stack_count);
2938
3e9a8aad
SRRH
2939 event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
2940 sizeof(*entry), flags, pc);
02b67518 2941 if (!event)
1dbd1951 2942 goto out_drop_count;
02b67518 2943 entry = ring_buffer_event_data(event);
02b67518 2944
48659d31 2945 entry->tgid = current->tgid;
02b67518
TE
2946 memset(&entry->caller, 0, sizeof(entry->caller));
2947
ee6dd0db 2948 stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
f306cc82 2949 if (!call_filter_check_discard(call, entry, buffer, event))
7ffbd48d 2950 __buffer_unlock_commit(buffer, event);
91e86e56 2951
1dbd1951 2952 out_drop_count:
91e86e56 2953 __this_cpu_dec(user_stack_count);
91e86e56
SR
2954 out:
2955 preempt_enable();
02b67518 2956}
c438f140
TG
2957#else /* CONFIG_USER_STACKTRACE_SUPPORT */
2958static void ftrace_trace_userstack(struct ring_buffer *buffer,
2959 unsigned long flags, int pc)
02b67518 2960{
02b67518 2961}
c438f140 2962#endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
02b67518 2963
c0a0d0d3
FW
2964#endif /* CONFIG_STACKTRACE */
2965
07d777fe
SR
2966/* created for use with alloc_percpu */
2967struct trace_buffer_struct {
e2ace001
AL
2968 int nesting;
2969 char buffer[4][TRACE_BUF_SIZE];
07d777fe
SR
2970};
2971
2972static struct trace_buffer_struct *trace_percpu_buffer;
07d777fe
SR
2973
2974/*
e2ace001
AL
2975 * Thise allows for lockless recording. If we're nested too deeply, then
2976 * this returns NULL.
07d777fe
SR
2977 */
2978static char *get_trace_buf(void)
2979{
e2ace001 2980 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
07d777fe 2981
e2ace001 2982 if (!buffer || buffer->nesting >= 4)
07d777fe
SR
2983 return NULL;
2984
3d9622c1
SRV
2985 buffer->nesting++;
2986
2987 /* Interrupts must see nesting incremented before we use the buffer */
2988 barrier();
2989 return &buffer->buffer[buffer->nesting][0];
e2ace001
AL
2990}
2991
2992static void put_trace_buf(void)
2993{
3d9622c1
SRV
2994 /* Don't let the decrement of nesting leak before this */
2995 barrier();
e2ace001 2996 this_cpu_dec(trace_percpu_buffer->nesting);
07d777fe
SR
2997}
2998
2999static int alloc_percpu_trace_buffer(void)
3000{
3001 struct trace_buffer_struct *buffers;
07d777fe
SR
3002
3003 buffers = alloc_percpu(struct trace_buffer_struct);
e2ace001
AL
3004 if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
3005 return -ENOMEM;
07d777fe
SR
3006
3007 trace_percpu_buffer = buffers;
07d777fe 3008 return 0;
07d777fe
SR
3009}
3010
81698831
SR
3011static int buffers_allocated;
3012
07d777fe
SR
3013void trace_printk_init_buffers(void)
3014{
07d777fe
SR
3015 if (buffers_allocated)
3016 return;
3017
3018 if (alloc_percpu_trace_buffer())
3019 return;
3020
2184db46
SR
3021 /* trace_printk() is for debug use only. Don't use it in production. */
3022
a395d6a7
JP
3023 pr_warn("\n");
3024 pr_warn("**********************************************************\n");
3025 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3026 pr_warn("** **\n");
3027 pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
3028 pr_warn("** **\n");
3029 pr_warn("** This means that this is a DEBUG kernel and it is **\n");
3030 pr_warn("** unsafe for production use. **\n");
3031 pr_warn("** **\n");
3032 pr_warn("** If you see this message and you are not debugging **\n");
3033 pr_warn("** the kernel, report this immediately to your vendor! **\n");
3034 pr_warn("** **\n");
3035 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3036 pr_warn("**********************************************************\n");
07d777fe 3037
b382ede6
SR
3038 /* Expand the buffers to set size */
3039 tracing_update_buffers();
3040
07d777fe 3041 buffers_allocated = 1;
81698831
SR
3042
3043 /*
3044 * trace_printk_init_buffers() can be called by modules.
3045 * If that happens, then we need to start cmdline recording
3046 * directly here. If the global_trace.buffer is already
3047 * allocated here, then this was called by module code.
3048 */
12883efb 3049 if (global_trace.trace_buffer.buffer)
81698831
SR
3050 tracing_start_cmdline_record();
3051}
f45d1225 3052EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
81698831
SR
3053
3054void trace_printk_start_comm(void)
3055{
3056 /* Start tracing comms if trace printk is set */
3057 if (!buffers_allocated)
3058 return;
3059 tracing_start_cmdline_record();
3060}
3061
3062static void trace_printk_start_stop_comm(int enabled)
3063{
3064 if (!buffers_allocated)
3065 return;
3066
3067 if (enabled)
3068 tracing_start_cmdline_record();
3069 else
3070 tracing_stop_cmdline_record();
07d777fe
SR
3071}
3072
769b0441 3073/**
48ead020 3074 * trace_vbprintk - write binary msg to tracing buffer
769b0441
FW
3075 *
3076 */
40ce74f1 3077int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
769b0441 3078{
2425bcb9 3079 struct trace_event_call *call = &event_bprint;
769b0441 3080 struct ring_buffer_event *event;
e77405ad 3081 struct ring_buffer *buffer;
769b0441 3082 struct trace_array *tr = &global_trace;
48ead020 3083 struct bprint_entry *entry;
769b0441 3084 unsigned long flags;
07d777fe
SR
3085 char *tbuffer;
3086 int len = 0, size, pc;
769b0441
FW
3087
3088 if (unlikely(tracing_selftest_running || tracing_disabled))
3089 return 0;
3090
3091 /* Don't pollute graph traces with trace_vprintk internals */
3092 pause_graph_tracing();
3093
3094 pc = preempt_count();
5168ae50 3095 preempt_disable_notrace();
769b0441 3096
07d777fe
SR
3097 tbuffer = get_trace_buf();
3098 if (!tbuffer) {
3099 len = 0;
e2ace001 3100 goto out_nobuffer;
07d777fe 3101 }
769b0441 3102
07d777fe 3103 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
769b0441 3104
07d777fe
SR
3105 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
3106 goto out;
769b0441 3107
07d777fe 3108 local_save_flags(flags);
769b0441 3109 size = sizeof(*entry) + sizeof(u32) * len;
12883efb 3110 buffer = tr->trace_buffer.buffer;
3e9a8aad
SRRH
3111 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
3112 flags, pc);
769b0441 3113 if (!event)
07d777fe 3114 goto out;
769b0441
FW
3115 entry = ring_buffer_event_data(event);
3116 entry->ip = ip;
769b0441
FW
3117 entry->fmt = fmt;
3118
07d777fe 3119 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
f306cc82 3120 if (!call_filter_check_discard(call, entry, buffer, event)) {
7ffbd48d 3121 __buffer_unlock_commit(buffer, event);
2d34f489 3122 ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL);
d931369b 3123 }
769b0441 3124
769b0441 3125out:
e2ace001
AL
3126 put_trace_buf();
3127
3128out_nobuffer:
5168ae50 3129 preempt_enable_notrace();
769b0441
FW
3130 unpause_graph_tracing();
3131
3132 return len;
3133}
48ead020
FW
3134EXPORT_SYMBOL_GPL(trace_vbprintk);
3135
26b68dd2 3136__printf(3, 0)
12883efb
SRRH
3137static int
3138__trace_array_vprintk(struct ring_buffer *buffer,
3139 unsigned long ip, const char *fmt, va_list args)
48ead020 3140{
2425bcb9 3141 struct trace_event_call *call = &event_print;
48ead020 3142 struct ring_buffer_event *event;
07d777fe 3143 int len = 0, size, pc;
48ead020 3144 struct print_entry *entry;
07d777fe
SR
3145 unsigned long flags;
3146 char *tbuffer;
48ead020
FW
3147
3148 if (tracing_disabled || tracing_selftest_running)
3149 return 0;
3150
07d777fe
SR
3151 /* Don't pollute graph traces with trace_vprintk internals */
3152 pause_graph_tracing();
3153
48ead020
FW
3154 pc = preempt_count();
3155 preempt_disable_notrace();
48ead020 3156
07d777fe
SR
3157
3158 tbuffer = get_trace_buf();
3159 if (!tbuffer) {
3160 len = 0;
e2ace001 3161 goto out_nobuffer;
07d777fe 3162 }
48ead020 3163
3558a5ac 3164 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
48ead020 3165
07d777fe 3166 local_save_flags(flags);
48ead020 3167 size = sizeof(*entry) + len + 1;
3e9a8aad
SRRH
3168 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3169 flags, pc);
48ead020 3170 if (!event)
07d777fe 3171 goto out;
48ead020 3172 entry = ring_buffer_event_data(event);
c13d2f7c 3173 entry->ip = ip;
48ead020 3174
3558a5ac 3175 memcpy(&entry->buf, tbuffer, len + 1);
f306cc82 3176 if (!call_filter_check_discard(call, entry, buffer, event)) {
7ffbd48d 3177 __buffer_unlock_commit(buffer, event);
2d34f489 3178 ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
d931369b 3179 }
e2ace001
AL
3180
3181out:
3182 put_trace_buf();
3183
3184out_nobuffer:
48ead020 3185 preempt_enable_notrace();
07d777fe 3186 unpause_graph_tracing();
48ead020
FW
3187
3188 return len;
3189}
659372d3 3190
26b68dd2 3191__printf(3, 0)
12883efb
SRRH
3192int trace_array_vprintk(struct trace_array *tr,
3193 unsigned long ip, const char *fmt, va_list args)
3194{
3195 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
3196}
3197
26b68dd2 3198__printf(3, 0)
12883efb
SRRH
3199int trace_array_printk(struct trace_array *tr,
3200 unsigned long ip, const char *fmt, ...)
3201{
3202 int ret;
3203 va_list ap;
3204
983f938a 3205 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
12883efb
SRRH
3206 return 0;
3207
3208 va_start(ap, fmt);
3209 ret = trace_array_vprintk(tr, ip, fmt, ap);
3210 va_end(ap);
3211 return ret;
3212}
f45d1225 3213EXPORT_SYMBOL_GPL(trace_array_printk);
12883efb 3214
26b68dd2 3215__printf(3, 4)
12883efb
SRRH
3216int trace_array_printk_buf(struct ring_buffer *buffer,
3217 unsigned long ip, const char *fmt, ...)
3218{
3219 int ret;
3220 va_list ap;
3221
983f938a 3222 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
12883efb
SRRH
3223 return 0;
3224
3225 va_start(ap, fmt);
3226 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3227 va_end(ap);
3228 return ret;
3229}
3230
26b68dd2 3231__printf(2, 0)
659372d3
SR
3232int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3233{
a813a159 3234 return trace_array_vprintk(&global_trace, ip, fmt, args);
659372d3 3235}
769b0441
FW
3236EXPORT_SYMBOL_GPL(trace_vprintk);
3237
e2ac8ef5 3238static void trace_iterator_increment(struct trace_iterator *iter)
5a90f577 3239{
6d158a81
SR
3240 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3241
5a90f577 3242 iter->idx++;
6d158a81
SR
3243 if (buf_iter)
3244 ring_buffer_read(buf_iter, NULL);
5a90f577
SR
3245}
3246
e309b41d 3247static struct trace_entry *
bc21b478
SR
3248peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3249 unsigned long *lost_events)
dd0e545f 3250{
3928a8a2 3251 struct ring_buffer_event *event;
6d158a81 3252 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
dd0e545f 3253
d769041f
SR
3254 if (buf_iter)
3255 event = ring_buffer_iter_peek(buf_iter, ts);
3256 else
12883efb 3257 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
bc21b478 3258 lost_events);
d769041f 3259
4a9bd3f1
SR
3260 if (event) {
3261 iter->ent_size = ring_buffer_event_length(event);
3262 return ring_buffer_event_data(event);
3263 }
3264 iter->ent_size = 0;
3265 return NULL;
dd0e545f 3266}
d769041f 3267
dd0e545f 3268static struct trace_entry *
bc21b478
SR
3269__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3270 unsigned long *missing_events, u64 *ent_ts)
bc0c38d1 3271{
12883efb 3272 struct ring_buffer *buffer = iter->trace_buffer->buffer;
bc0c38d1 3273 struct trace_entry *ent, *next = NULL;
aa27497c 3274 unsigned long lost_events = 0, next_lost = 0;
b04cc6b1 3275 int cpu_file = iter->cpu_file;
3928a8a2 3276 u64 next_ts = 0, ts;
bc0c38d1 3277 int next_cpu = -1;
12b5da34 3278 int next_size = 0;
bc0c38d1
SR
3279 int cpu;
3280
b04cc6b1
FW
3281 /*
3282 * If we are in a per_cpu trace file, don't bother by iterating over
3283 * all cpu and peek directly.
3284 */
ae3b5093 3285 if (cpu_file > RING_BUFFER_ALL_CPUS) {
b04cc6b1
FW
3286 if (ring_buffer_empty_cpu(buffer, cpu_file))
3287 return NULL;
bc21b478 3288 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
b04cc6b1
FW
3289 if (ent_cpu)
3290 *ent_cpu = cpu_file;
3291
3292 return ent;
3293 }
3294
ab46428c 3295 for_each_tracing_cpu(cpu) {
dd0e545f 3296
3928a8a2
SR
3297 if (ring_buffer_empty_cpu(buffer, cpu))
3298 continue;
dd0e545f 3299
bc21b478 3300 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
dd0e545f 3301
cdd31cd2
IM
3302 /*
3303 * Pick the entry with the smallest timestamp:
3304 */
3928a8a2 3305 if (ent && (!next || ts < next_ts)) {
bc0c38d1
SR
3306 next = ent;
3307 next_cpu = cpu;
3928a8a2 3308 next_ts = ts;
bc21b478 3309 next_lost = lost_events;
12b5da34 3310 next_size = iter->ent_size;
bc0c38d1
SR
3311 }
3312 }
3313
12b5da34
SR
3314 iter->ent_size = next_size;
3315
bc0c38d1
SR
3316 if (ent_cpu)
3317 *ent_cpu = next_cpu;
3318
3928a8a2
SR
3319 if (ent_ts)
3320 *ent_ts = next_ts;
3321
bc21b478
SR
3322 if (missing_events)
3323 *missing_events = next_lost;
3324
bc0c38d1
SR
3325 return next;
3326}
3327
dd0e545f 3328/* Find the next real entry, without updating the iterator itself */
c4a8e8be
FW
3329struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3330 int *ent_cpu, u64 *ent_ts)
bc0c38d1 3331{
bc21b478 3332 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
dd0e545f
SR
3333}
3334
3335/* Find the next real entry, and increment the iterator to the next entry */
955b61e5 3336void *trace_find_next_entry_inc(struct trace_iterator *iter)
dd0e545f 3337{
bc21b478
SR
3338 iter->ent = __find_next_entry(iter, &iter->cpu,
3339 &iter->lost_events, &iter->ts);
dd0e545f 3340
3928a8a2 3341 if (iter->ent)
e2ac8ef5 3342 trace_iterator_increment(iter);
dd0e545f 3343
3928a8a2 3344 return iter->ent ? iter : NULL;
b3806b43 3345}
bc0c38d1 3346
e309b41d 3347static void trace_consume(struct trace_iterator *iter)
b3806b43 3348{
12883efb 3349 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
bc21b478 3350 &iter->lost_events);
bc0c38d1
SR
3351}
3352
e309b41d 3353static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
3354{
3355 struct trace_iterator *iter = m->private;
bc0c38d1 3356 int i = (int)*pos;
4e3c3333 3357 void *ent;
bc0c38d1 3358
a63ce5b3
SR
3359 WARN_ON_ONCE(iter->leftover);
3360
bc0c38d1
SR
3361 (*pos)++;
3362
3363 /* can't go backwards */
3364 if (iter->idx > i)
3365 return NULL;
3366
3367 if (iter->idx < 0)
955b61e5 3368 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
3369 else
3370 ent = iter;
3371
3372 while (ent && iter->idx < i)
955b61e5 3373 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
3374
3375 iter->pos = *pos;
3376
bc0c38d1
SR
3377 return ent;
3378}
3379
955b61e5 3380void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2f26ebd5 3381{
2f26ebd5
SR
3382 struct ring_buffer_event *event;
3383 struct ring_buffer_iter *buf_iter;
3384 unsigned long entries = 0;
3385 u64 ts;
3386
12883efb 3387 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2f26ebd5 3388
6d158a81
SR
3389 buf_iter = trace_buffer_iter(iter, cpu);
3390 if (!buf_iter)
2f26ebd5
SR
3391 return;
3392
2f26ebd5
SR
3393 ring_buffer_iter_reset(buf_iter);
3394
3395 /*
3396 * We could have the case with the max latency tracers
3397 * that a reset never took place on a cpu. This is evident
3398 * by the timestamp being before the start of the buffer.
3399 */
3400 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
12883efb 3401 if (ts >= iter->trace_buffer->time_start)
2f26ebd5
SR
3402 break;
3403 entries++;
3404 ring_buffer_read(buf_iter, NULL);
3405 }
3406
12883efb 3407 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2f26ebd5
SR
3408}
3409
d7350c3f 3410/*
d7350c3f
FW
3411 * The current tracer is copied to avoid a global locking
3412 * all around.
3413 */
bc0c38d1
SR
3414static void *s_start(struct seq_file *m, loff_t *pos)
3415{
3416 struct trace_iterator *iter = m->private;
2b6080f2 3417 struct trace_array *tr = iter->tr;
b04cc6b1 3418 int cpu_file = iter->cpu_file;
bc0c38d1
SR
3419 void *p = NULL;
3420 loff_t l = 0;
3928a8a2 3421 int cpu;
bc0c38d1 3422
2fd196ec
HT
3423 /*
3424 * copy the tracer to avoid using a global lock all around.
3425 * iter->trace is a copy of current_trace, the pointer to the
3426 * name may be used instead of a strcmp(), as iter->trace->name
3427 * will point to the same string as current_trace->name.
3428 */
bc0c38d1 3429 mutex_lock(&trace_types_lock);
2b6080f2
SR
3430 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
3431 *iter->trace = *tr->current_trace;
d7350c3f 3432 mutex_unlock(&trace_types_lock);
bc0c38d1 3433
12883efb 3434#ifdef CONFIG_TRACER_MAX_TRACE
debdd57f
HT
3435 if (iter->snapshot && iter->trace->use_max_tr)
3436 return ERR_PTR(-EBUSY);
12883efb 3437#endif
debdd57f
HT
3438
3439 if (!iter->snapshot)
d914ba37 3440 atomic_inc(&trace_record_taskinfo_disabled);
bc0c38d1 3441
bc0c38d1
SR
3442 if (*pos != iter->pos) {
3443 iter->ent = NULL;
3444 iter->cpu = 0;
3445 iter->idx = -1;
3446
ae3b5093 3447 if (cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 3448 for_each_tracing_cpu(cpu)
2f26ebd5 3449 tracing_iter_reset(iter, cpu);
b04cc6b1 3450 } else
2f26ebd5 3451 tracing_iter_reset(iter, cpu_file);
bc0c38d1 3452
ac91d854 3453 iter->leftover = 0;
bc0c38d1
SR
3454 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
3455 ;
3456
3457 } else {
a63ce5b3
SR
3458 /*
3459 * If we overflowed the seq_file before, then we want
3460 * to just reuse the trace_seq buffer again.
3461 */
3462 if (iter->leftover)
3463 p = iter;
3464 else {
3465 l = *pos - 1;
3466 p = s_next(m, p, &l);
3467 }
bc0c38d1
SR
3468 }
3469
4f535968 3470 trace_event_read_lock();
7e53bd42 3471 trace_access_lock(cpu_file);
bc0c38d1
SR
3472 return p;
3473}
3474
3475static void s_stop(struct seq_file *m, void *p)
3476{
7e53bd42
LJ
3477 struct trace_iterator *iter = m->private;
3478
12883efb 3479#ifdef CONFIG_TRACER_MAX_TRACE
debdd57f
HT
3480 if (iter->snapshot && iter->trace->use_max_tr)
3481 return;
12883efb 3482#endif
debdd57f
HT
3483
3484 if (!iter->snapshot)
d914ba37 3485 atomic_dec(&trace_record_taskinfo_disabled);
12883efb 3486
7e53bd42 3487 trace_access_unlock(iter->cpu_file);
4f535968 3488 trace_event_read_unlock();
bc0c38d1
SR
3489}
3490
ecffc8a8
DA
3491static void
3492get_total_entries_cpu(struct trace_buffer *buf, unsigned long *total,
3493 unsigned long *entries, int cpu)
3494{
3495 unsigned long count;
3496
3497 count = ring_buffer_entries_cpu(buf->buffer, cpu);
3498 /*
3499 * If this buffer has skipped entries, then we hold all
3500 * entries for the trace and we need to ignore the
3501 * ones before the time stamp.
3502 */
3503 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3504 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
3505 /* total is the same as the entries */
3506 *total = count;
3507 } else
3508 *total = count +
3509 ring_buffer_overrun_cpu(buf->buffer, cpu);
3510 *entries = count;
3511}
3512
39eaf7ef 3513static void
12883efb
SRRH
3514get_total_entries(struct trace_buffer *buf,
3515 unsigned long *total, unsigned long *entries)
39eaf7ef 3516{
ecffc8a8 3517 unsigned long t, e;
39eaf7ef
SR
3518 int cpu;
3519
3520 *total = 0;
3521 *entries = 0;
3522
3523 for_each_tracing_cpu(cpu) {
ecffc8a8
DA
3524 get_total_entries_cpu(buf, &t, &e, cpu);
3525 *total += t;
3526 *entries += e;
39eaf7ef
SR
3527 }
3528}
3529
ecffc8a8
DA
3530unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
3531{
3532 unsigned long total, entries;
3533
3534 if (!tr)
3535 tr = &global_trace;
3536
3537 get_total_entries_cpu(&tr->trace_buffer, &total, &entries, cpu);
3538
3539 return entries;
3540}
3541
3542unsigned long trace_total_entries(struct trace_array *tr)
3543{
3544 unsigned long total, entries;
3545
3546 if (!tr)
3547 tr = &global_trace;
3548
3549 get_total_entries(&tr->trace_buffer, &total, &entries);
3550
3551 return entries;
3552}
3553
e309b41d 3554static void print_lat_help_header(struct seq_file *m)
bc0c38d1 3555{
d79ac28f
RV
3556 seq_puts(m, "# _------=> CPU# \n"
3557 "# / _-----=> irqs-off \n"
3558 "# | / _----=> need-resched \n"
3559 "# || / _---=> hardirq/softirq \n"
3560 "# ||| / _--=> preempt-depth \n"
3561 "# |||| / delay \n"
3562 "# cmd pid ||||| time | caller \n"
3563 "# \\ / ||||| \\ | / \n");
bc0c38d1
SR
3564}
3565
12883efb 3566static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
bc0c38d1 3567{
39eaf7ef
SR
3568 unsigned long total;
3569 unsigned long entries;
3570
12883efb 3571 get_total_entries(buf, &total, &entries);
39eaf7ef
SR
3572 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
3573 entries, total, num_online_cpus());
3574 seq_puts(m, "#\n");
3575}
3576
441dae8f
JF
3577static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m,
3578 unsigned int flags)
39eaf7ef 3579{
441dae8f
JF
3580 bool tgid = flags & TRACE_ITER_RECORD_TGID;
3581
12883efb 3582 print_event_info(buf, m);
441dae8f 3583
f8494fa3
JFG
3584 seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? "TGID " : "");
3585 seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
bc0c38d1
SR
3586}
3587
441dae8f
JF
3588static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m,
3589 unsigned int flags)
77271ce4 3590{
441dae8f 3591 bool tgid = flags & TRACE_ITER_RECORD_TGID;
0f5e5a3a
RV
3592 const char *space = " ";
3593 int prec = tgid ? 10 : 2;
b11fb737 3594
9e738215
QP
3595 print_event_info(buf, m);
3596
0f5e5a3a
RV
3597 seq_printf(m, "# %.*s _-----=> irqs-off\n", prec, space);
3598 seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space);
3599 seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space);
3600 seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space);
3601 seq_printf(m, "# %.*s||| / delay\n", prec, space);
3602 seq_printf(m, "# TASK-PID %.*sCPU# |||| TIMESTAMP FUNCTION\n", prec, " TGID ");
3603 seq_printf(m, "# | | %.*s | |||| | |\n", prec, " | ");
77271ce4 3604}
bc0c38d1 3605
62b915f1 3606void
bc0c38d1
SR
3607print_trace_header(struct seq_file *m, struct trace_iterator *iter)
3608{
983f938a 3609 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
12883efb
SRRH
3610 struct trace_buffer *buf = iter->trace_buffer;
3611 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2b6080f2 3612 struct tracer *type = iter->trace;
39eaf7ef
SR
3613 unsigned long entries;
3614 unsigned long total;
bc0c38d1
SR
3615 const char *name = "preemption";
3616
d840f718 3617 name = type->name;
bc0c38d1 3618
12883efb 3619 get_total_entries(buf, &total, &entries);
bc0c38d1 3620
888b55dc 3621 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
bc0c38d1 3622 name, UTS_RELEASE);
888b55dc 3623 seq_puts(m, "# -----------------------------------"
bc0c38d1 3624 "---------------------------------\n");
888b55dc 3625 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
bc0c38d1 3626 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 3627 nsecs_to_usecs(data->saved_latency),
bc0c38d1 3628 entries,
4c11d7ae 3629 total,
12883efb 3630 buf->cpu,
bc0c38d1
SR
3631#if defined(CONFIG_PREEMPT_NONE)
3632 "server",
3633#elif defined(CONFIG_PREEMPT_VOLUNTARY)
3634 "desktop",
b5c21b45 3635#elif defined(CONFIG_PREEMPT)
bc0c38d1
SR
3636 "preempt",
3637#else
3638 "unknown",
3639#endif
3640 /* These are reserved for later use */
3641 0, 0, 0, 0);
3642#ifdef CONFIG_SMP
3643 seq_printf(m, " #P:%d)\n", num_online_cpus());
3644#else
3645 seq_puts(m, ")\n");
3646#endif
888b55dc
KM
3647 seq_puts(m, "# -----------------\n");
3648 seq_printf(m, "# | task: %.16s-%d "
bc0c38d1 3649 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
d20b92ab
EB
3650 data->comm, data->pid,
3651 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
bc0c38d1 3652 data->policy, data->rt_priority);
888b55dc 3653 seq_puts(m, "# -----------------\n");
bc0c38d1
SR
3654
3655 if (data->critical_start) {
888b55dc 3656 seq_puts(m, "# => started at: ");
214023c3
SR
3657 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
3658 trace_print_seq(m, &iter->seq);
888b55dc 3659 seq_puts(m, "\n# => ended at: ");
214023c3
SR
3660 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
3661 trace_print_seq(m, &iter->seq);
8248ac05 3662 seq_puts(m, "\n#\n");
bc0c38d1
SR
3663 }
3664
888b55dc 3665 seq_puts(m, "#\n");
bc0c38d1
SR
3666}
3667
a309720c
SR
3668static void test_cpu_buff_start(struct trace_iterator *iter)
3669{
3670 struct trace_seq *s = &iter->seq;
983f938a 3671 struct trace_array *tr = iter->tr;
a309720c 3672
983f938a 3673 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
12ef7d44
SR
3674 return;
3675
3676 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
3677 return;
3678
4dbbe2d8
MK
3679 if (cpumask_available(iter->started) &&
3680 cpumask_test_cpu(iter->cpu, iter->started))
a309720c
SR
3681 return;
3682
12883efb 3683 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2f26ebd5
SR
3684 return;
3685
4dbbe2d8 3686 if (cpumask_available(iter->started))
919cd979 3687 cpumask_set_cpu(iter->cpu, iter->started);
b0dfa978
FW
3688
3689 /* Don't print started cpu buffer for the first entry of the trace */
3690 if (iter->idx > 1)
3691 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
3692 iter->cpu);
a309720c
SR
3693}
3694
2c4f035f 3695static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 3696{
983f938a 3697 struct trace_array *tr = iter->tr;
214023c3 3698 struct trace_seq *s = &iter->seq;
983f938a 3699 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 3700 struct trace_entry *entry;
f633cef0 3701 struct trace_event *event;
bc0c38d1 3702
4e3c3333 3703 entry = iter->ent;
dd0e545f 3704
a309720c
SR
3705 test_cpu_buff_start(iter);
3706
c4a8e8be 3707 event = ftrace_find_event(entry->type);
bc0c38d1 3708
983f938a 3709 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
19a7fe20
SRRH
3710 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3711 trace_print_lat_context(iter);
3712 else
3713 trace_print_context(iter);
c4a8e8be 3714 }
bc0c38d1 3715
19a7fe20
SRRH
3716 if (trace_seq_has_overflowed(s))
3717 return TRACE_TYPE_PARTIAL_LINE;
3718
268ccda0 3719 if (event)
a9a57763 3720 return event->funcs->trace(iter, sym_flags, event);
d9793bd8 3721
19a7fe20 3722 trace_seq_printf(s, "Unknown type %d\n", entry->type);
02b67518 3723
19a7fe20 3724 return trace_handle_return(s);
bc0c38d1
SR
3725}
3726
2c4f035f 3727static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3 3728{
983f938a 3729 struct trace_array *tr = iter->tr;
f9896bf3
IM
3730 struct trace_seq *s = &iter->seq;
3731 struct trace_entry *entry;
f633cef0 3732 struct trace_event *event;
f9896bf3
IM
3733
3734 entry = iter->ent;
dd0e545f 3735
983f938a 3736 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
19a7fe20
SRRH
3737 trace_seq_printf(s, "%d %d %llu ",
3738 entry->pid, iter->cpu, iter->ts);
3739
3740 if (trace_seq_has_overflowed(s))
3741 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3 3742
f633cef0 3743 event = ftrace_find_event(entry->type);
268ccda0 3744 if (event)
a9a57763 3745 return event->funcs->raw(iter, 0, event);
d9793bd8 3746
19a7fe20 3747 trace_seq_printf(s, "%d ?\n", entry->type);
777e208d 3748
19a7fe20 3749 return trace_handle_return(s);
f9896bf3
IM
3750}
3751
2c4f035f 3752static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec 3753{
983f938a 3754 struct trace_array *tr = iter->tr;
5e3ca0ec
IM
3755 struct trace_seq *s = &iter->seq;
3756 unsigned char newline = '\n';
3757 struct trace_entry *entry;
f633cef0 3758 struct trace_event *event;
5e3ca0ec
IM
3759
3760 entry = iter->ent;
dd0e545f 3761
983f938a 3762 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
19a7fe20
SRRH
3763 SEQ_PUT_HEX_FIELD(s, entry->pid);
3764 SEQ_PUT_HEX_FIELD(s, iter->cpu);
3765 SEQ_PUT_HEX_FIELD(s, iter->ts);
3766 if (trace_seq_has_overflowed(s))
3767 return TRACE_TYPE_PARTIAL_LINE;
c4a8e8be 3768 }
5e3ca0ec 3769
f633cef0 3770 event = ftrace_find_event(entry->type);
268ccda0 3771 if (event) {
a9a57763 3772 enum print_line_t ret = event->funcs->hex(iter, 0, event);
d9793bd8
ACM
3773 if (ret != TRACE_TYPE_HANDLED)
3774 return ret;
3775 }
7104f300 3776
19a7fe20 3777 SEQ_PUT_FIELD(s, newline);
5e3ca0ec 3778
19a7fe20 3779 return trace_handle_return(s);
5e3ca0ec
IM
3780}
3781
2c4f035f 3782static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa 3783{
983f938a 3784 struct trace_array *tr = iter->tr;
cb0f12aa
IM
3785 struct trace_seq *s = &iter->seq;
3786 struct trace_entry *entry;
f633cef0 3787 struct trace_event *event;
cb0f12aa
IM
3788
3789 entry = iter->ent;
dd0e545f 3790
983f938a 3791 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
19a7fe20
SRRH
3792 SEQ_PUT_FIELD(s, entry->pid);
3793 SEQ_PUT_FIELD(s, iter->cpu);
3794 SEQ_PUT_FIELD(s, iter->ts);
3795 if (trace_seq_has_overflowed(s))
3796 return TRACE_TYPE_PARTIAL_LINE;
c4a8e8be 3797 }
cb0f12aa 3798
f633cef0 3799 event = ftrace_find_event(entry->type);
a9a57763
SR
3800 return event ? event->funcs->binary(iter, 0, event) :
3801 TRACE_TYPE_HANDLED;
cb0f12aa
IM
3802}
3803
62b915f1 3804int trace_empty(struct trace_iterator *iter)
bc0c38d1 3805{
6d158a81 3806 struct ring_buffer_iter *buf_iter;
bc0c38d1
SR
3807 int cpu;
3808
9aba60fe 3809 /* If we are looking at one CPU buffer, only check that one */
ae3b5093 3810 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
9aba60fe 3811 cpu = iter->cpu_file;
6d158a81
SR
3812 buf_iter = trace_buffer_iter(iter, cpu);
3813 if (buf_iter) {
3814 if (!ring_buffer_iter_empty(buf_iter))
9aba60fe
SR
3815 return 0;
3816 } else {
12883efb 3817 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
9aba60fe
SR
3818 return 0;
3819 }
3820 return 1;
3821 }
3822
ab46428c 3823 for_each_tracing_cpu(cpu) {
6d158a81
SR
3824 buf_iter = trace_buffer_iter(iter, cpu);
3825 if (buf_iter) {
3826 if (!ring_buffer_iter_empty(buf_iter))
d769041f
SR
3827 return 0;
3828 } else {
12883efb 3829 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
d769041f
SR
3830 return 0;
3831 }
bc0c38d1 3832 }
d769041f 3833
797d3712 3834 return 1;
bc0c38d1
SR
3835}
3836
4f535968 3837/* Called with trace_event_read_lock() held. */
955b61e5 3838enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 3839{
983f938a
SRRH
3840 struct trace_array *tr = iter->tr;
3841 unsigned long trace_flags = tr->trace_flags;
2c4f035f
FW
3842 enum print_line_t ret;
3843
19a7fe20
SRRH
3844 if (iter->lost_events) {
3845 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3846 iter->cpu, iter->lost_events);
3847 if (trace_seq_has_overflowed(&iter->seq))
3848 return TRACE_TYPE_PARTIAL_LINE;
3849 }
bc21b478 3850
2c4f035f
FW
3851 if (iter->trace && iter->trace->print_line) {
3852 ret = iter->trace->print_line(iter);
3853 if (ret != TRACE_TYPE_UNHANDLED)
3854 return ret;
3855 }
72829bc3 3856
09ae7234
SRRH
3857 if (iter->ent->type == TRACE_BPUTS &&
3858 trace_flags & TRACE_ITER_PRINTK &&
3859 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3860 return trace_print_bputs_msg_only(iter);
3861
48ead020
FW
3862 if (iter->ent->type == TRACE_BPRINT &&
3863 trace_flags & TRACE_ITER_PRINTK &&
3864 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 3865 return trace_print_bprintk_msg_only(iter);
48ead020 3866
66896a85
FW
3867 if (iter->ent->type == TRACE_PRINT &&
3868 trace_flags & TRACE_ITER_PRINTK &&
3869 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 3870 return trace_print_printk_msg_only(iter);
66896a85 3871
cb0f12aa
IM
3872 if (trace_flags & TRACE_ITER_BIN)
3873 return print_bin_fmt(iter);
3874
5e3ca0ec
IM
3875 if (trace_flags & TRACE_ITER_HEX)
3876 return print_hex_fmt(iter);
3877
f9896bf3
IM
3878 if (trace_flags & TRACE_ITER_RAW)
3879 return print_raw_fmt(iter);
3880
f9896bf3
IM
3881 return print_trace_fmt(iter);
3882}
3883
7e9a49ef
JO
3884void trace_latency_header(struct seq_file *m)
3885{
3886 struct trace_iterator *iter = m->private;
983f938a 3887 struct trace_array *tr = iter->tr;
7e9a49ef
JO
3888
3889 /* print nothing if the buffers are empty */
3890 if (trace_empty(iter))
3891 return;
3892
3893 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3894 print_trace_header(m, iter);
3895
983f938a 3896 if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
7e9a49ef
JO
3897 print_lat_help_header(m);
3898}
3899
62b915f1
JO
3900void trace_default_header(struct seq_file *m)
3901{
3902 struct trace_iterator *iter = m->private;
983f938a
SRRH
3903 struct trace_array *tr = iter->tr;
3904 unsigned long trace_flags = tr->trace_flags;
62b915f1 3905
f56e7f8e
JO
3906 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
3907 return;
3908
62b915f1
JO
3909 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
3910 /* print nothing if the buffers are empty */
3911 if (trace_empty(iter))
3912 return;
3913 print_trace_header(m, iter);
3914 if (!(trace_flags & TRACE_ITER_VERBOSE))
3915 print_lat_help_header(m);
3916 } else {
77271ce4
SR
3917 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
3918 if (trace_flags & TRACE_ITER_IRQ_INFO)
441dae8f
JF
3919 print_func_help_header_irq(iter->trace_buffer,
3920 m, trace_flags);
77271ce4 3921 else
441dae8f
JF
3922 print_func_help_header(iter->trace_buffer, m,
3923 trace_flags);
77271ce4 3924 }
62b915f1
JO
3925 }
3926}
3927
e0a413f6
SR
3928static void test_ftrace_alive(struct seq_file *m)
3929{
3930 if (!ftrace_is_dead())
3931 return;
d79ac28f
RV
3932 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
3933 "# MAY BE MISSING FUNCTION EVENTS\n");
e0a413f6
SR
3934}
3935
d8741e2e 3936#ifdef CONFIG_TRACER_MAX_TRACE
f1affcaa 3937static void show_snapshot_main_help(struct seq_file *m)
d8741e2e 3938{
d79ac28f
RV
3939 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
3940 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3941 "# Takes a snapshot of the main buffer.\n"
3942 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
3943 "# (Doesn't have to be '2' works with any number that\n"
3944 "# is not a '0' or '1')\n");
d8741e2e 3945}
f1affcaa
SRRH
3946
3947static void show_snapshot_percpu_help(struct seq_file *m)
3948{
fa6f0cc7 3949 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
f1affcaa 3950#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
d79ac28f
RV
3951 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3952 "# Takes a snapshot of the main buffer for this cpu.\n");
f1affcaa 3953#else
d79ac28f
RV
3954 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
3955 "# Must use main snapshot file to allocate.\n");
f1affcaa 3956#endif
d79ac28f
RV
3957 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
3958 "# (Doesn't have to be '2' works with any number that\n"
3959 "# is not a '0' or '1')\n");
f1affcaa
SRRH
3960}
3961
d8741e2e
SRRH
3962static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
3963{
45ad21ca 3964 if (iter->tr->allocated_snapshot)
fa6f0cc7 3965 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
d8741e2e 3966 else
fa6f0cc7 3967 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
d8741e2e 3968
fa6f0cc7 3969 seq_puts(m, "# Snapshot commands:\n");
f1affcaa
SRRH
3970 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
3971 show_snapshot_main_help(m);
3972 else
3973 show_snapshot_percpu_help(m);
d8741e2e
SRRH
3974}
3975#else
3976/* Should never be called */
3977static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
3978#endif
3979
bc0c38d1
SR
3980static int s_show(struct seq_file *m, void *v)
3981{
3982 struct trace_iterator *iter = v;
a63ce5b3 3983 int ret;
bc0c38d1
SR
3984
3985 if (iter->ent == NULL) {
3986 if (iter->tr) {
3987 seq_printf(m, "# tracer: %s\n", iter->trace->name);
3988 seq_puts(m, "#\n");
e0a413f6 3989 test_ftrace_alive(m);
bc0c38d1 3990 }
d8741e2e
SRRH
3991 if (iter->snapshot && trace_empty(iter))
3992 print_snapshot_help(m, iter);
3993 else if (iter->trace && iter->trace->print_header)
8bba1bf5 3994 iter->trace->print_header(m);
62b915f1
JO
3995 else
3996 trace_default_header(m);
3997
a63ce5b3
SR
3998 } else if (iter->leftover) {
3999 /*
4000 * If we filled the seq_file buffer earlier, we
4001 * want to just show it now.
4002 */
4003 ret = trace_print_seq(m, &iter->seq);
4004
4005 /* ret should this time be zero, but you never know */
4006 iter->leftover = ret;
4007
bc0c38d1 4008 } else {
f9896bf3 4009 print_trace_line(iter);
a63ce5b3
SR
4010 ret = trace_print_seq(m, &iter->seq);
4011 /*
4012 * If we overflow the seq_file buffer, then it will
4013 * ask us for this data again at start up.
4014 * Use that instead.
4015 * ret is 0 if seq_file write succeeded.
4016 * -1 otherwise.
4017 */
4018 iter->leftover = ret;
bc0c38d1
SR
4019 }
4020
4021 return 0;
4022}
4023
649e9c70
ON
4024/*
4025 * Should be used after trace_array_get(), trace_types_lock
4026 * ensures that i_cdev was already initialized.
4027 */
4028static inline int tracing_get_cpu(struct inode *inode)
4029{
4030 if (inode->i_cdev) /* See trace_create_cpu_file() */
4031 return (long)inode->i_cdev - 1;
4032 return RING_BUFFER_ALL_CPUS;
4033}
4034
88e9d34c 4035static const struct seq_operations tracer_seq_ops = {
4bf39a94
IM
4036 .start = s_start,
4037 .next = s_next,
4038 .stop = s_stop,
4039 .show = s_show,
bc0c38d1
SR
4040};
4041
e309b41d 4042static struct trace_iterator *
6484c71c 4043__tracing_open(struct inode *inode, struct file *file, bool snapshot)
bc0c38d1 4044{
6484c71c 4045 struct trace_array *tr = inode->i_private;
bc0c38d1 4046 struct trace_iterator *iter;
50e18b94 4047 int cpu;
bc0c38d1 4048
85a2f9b4
SR
4049 if (tracing_disabled)
4050 return ERR_PTR(-ENODEV);
60a11774 4051
50e18b94 4052 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
85a2f9b4
SR
4053 if (!iter)
4054 return ERR_PTR(-ENOMEM);
bc0c38d1 4055
72917235 4056 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
6d158a81 4057 GFP_KERNEL);
93574fcc
DC
4058 if (!iter->buffer_iter)
4059 goto release;
4060
d7350c3f
FW
4061 /*
4062 * We make a copy of the current tracer to avoid concurrent
4063 * changes on it while we are reading.
4064 */
bc0c38d1 4065 mutex_lock(&trace_types_lock);
d7350c3f 4066 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
85a2f9b4 4067 if (!iter->trace)
d7350c3f 4068 goto fail;
85a2f9b4 4069
2b6080f2 4070 *iter->trace = *tr->current_trace;
d7350c3f 4071
79f55997 4072 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
b0dfa978
FW
4073 goto fail;
4074
12883efb
SRRH
4075 iter->tr = tr;
4076
4077#ifdef CONFIG_TRACER_MAX_TRACE
2b6080f2
SR
4078 /* Currently only the top directory has a snapshot */
4079 if (tr->current_trace->print_max || snapshot)
12883efb 4080 iter->trace_buffer = &tr->max_buffer;
bc0c38d1 4081 else
12883efb
SRRH
4082#endif
4083 iter->trace_buffer = &tr->trace_buffer;
debdd57f 4084 iter->snapshot = snapshot;
bc0c38d1 4085 iter->pos = -1;
6484c71c 4086 iter->cpu_file = tracing_get_cpu(inode);
d7350c3f 4087 mutex_init(&iter->mutex);
bc0c38d1 4088
8bba1bf5
MM
4089 /* Notify the tracer early; before we stop tracing. */
4090 if (iter->trace && iter->trace->open)
a93751ca 4091 iter->trace->open(iter);
8bba1bf5 4092
12ef7d44 4093 /* Annotate start of buffers if we had overruns */
12883efb 4094 if (ring_buffer_overruns(iter->trace_buffer->buffer))
12ef7d44
SR
4095 iter->iter_flags |= TRACE_FILE_ANNOTATE;
4096
8be0709f 4097 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
58e8eedf 4098 if (trace_clocks[tr->clock_id].in_ns)
8be0709f
DS
4099 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4100
debdd57f
HT
4101 /* stop the trace while dumping if we are not opening "snapshot" */
4102 if (!iter->snapshot)
2b6080f2 4103 tracing_stop_tr(tr);
2f26ebd5 4104
ae3b5093 4105 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 4106 for_each_tracing_cpu(cpu) {
b04cc6b1 4107 iter->buffer_iter[cpu] =
31b265b3
DA
4108 ring_buffer_read_prepare(iter->trace_buffer->buffer,
4109 cpu, GFP_KERNEL);
72c9ddfd
DM
4110 }
4111 ring_buffer_read_prepare_sync();
4112 for_each_tracing_cpu(cpu) {
4113 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 4114 tracing_iter_reset(iter, cpu);
b04cc6b1
FW
4115 }
4116 } else {
4117 cpu = iter->cpu_file;
3928a8a2 4118 iter->buffer_iter[cpu] =
31b265b3
DA
4119 ring_buffer_read_prepare(iter->trace_buffer->buffer,
4120 cpu, GFP_KERNEL);
72c9ddfd
DM
4121 ring_buffer_read_prepare_sync();
4122 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 4123 tracing_iter_reset(iter, cpu);
3928a8a2
SR
4124 }
4125
bc0c38d1
SR
4126 mutex_unlock(&trace_types_lock);
4127
bc0c38d1 4128 return iter;
3928a8a2 4129
d7350c3f 4130 fail:
3928a8a2 4131 mutex_unlock(&trace_types_lock);
d7350c3f 4132 kfree(iter->trace);
6d158a81 4133 kfree(iter->buffer_iter);
93574fcc 4134release:
50e18b94
JO
4135 seq_release_private(inode, file);
4136 return ERR_PTR(-ENOMEM);
bc0c38d1
SR
4137}
4138
4139int tracing_open_generic(struct inode *inode, struct file *filp)
4140{
60a11774
SR
4141 if (tracing_disabled)
4142 return -ENODEV;
4143
bc0c38d1
SR
4144 filp->private_data = inode->i_private;
4145 return 0;
4146}
4147
2e86421d
GB
4148bool tracing_is_disabled(void)
4149{
4150 return (tracing_disabled) ? true: false;
4151}
4152
7b85af63
SRRH
4153/*
4154 * Open and update trace_array ref count.
4155 * Must have the current trace_array passed to it.
4156 */
dcc30223 4157static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
7b85af63
SRRH
4158{
4159 struct trace_array *tr = inode->i_private;
4160
4161 if (tracing_disabled)
4162 return -ENODEV;
4163
4164 if (trace_array_get(tr) < 0)
4165 return -ENODEV;
4166
4167 filp->private_data = inode->i_private;
4168
4169 return 0;
7b85af63
SRRH
4170}
4171
4fd27358 4172static int tracing_release(struct inode *inode, struct file *file)
bc0c38d1 4173{
6484c71c 4174 struct trace_array *tr = inode->i_private;
907f2784 4175 struct seq_file *m = file->private_data;
4acd4d00 4176 struct trace_iterator *iter;
3928a8a2 4177 int cpu;
bc0c38d1 4178
ff451961 4179 if (!(file->f_mode & FMODE_READ)) {
6484c71c 4180 trace_array_put(tr);
4acd4d00 4181 return 0;
ff451961 4182 }
4acd4d00 4183
6484c71c 4184 /* Writes do not use seq_file */
4acd4d00 4185 iter = m->private;
bc0c38d1 4186 mutex_lock(&trace_types_lock);
a695cb58 4187
3928a8a2
SR
4188 for_each_tracing_cpu(cpu) {
4189 if (iter->buffer_iter[cpu])
4190 ring_buffer_read_finish(iter->buffer_iter[cpu]);
4191 }
4192
bc0c38d1
SR
4193 if (iter->trace && iter->trace->close)
4194 iter->trace->close(iter);
4195
debdd57f
HT
4196 if (!iter->snapshot)
4197 /* reenable tracing if it was previously enabled */
2b6080f2 4198 tracing_start_tr(tr);
f77d09a3
AL
4199
4200 __trace_array_put(tr);
4201
bc0c38d1
SR
4202 mutex_unlock(&trace_types_lock);
4203
d7350c3f 4204 mutex_destroy(&iter->mutex);
b0dfa978 4205 free_cpumask_var(iter->started);
d7350c3f 4206 kfree(iter->trace);
6d158a81 4207 kfree(iter->buffer_iter);
50e18b94 4208 seq_release_private(inode, file);
ff451961 4209
bc0c38d1
SR
4210 return 0;
4211}
4212
7b85af63
SRRH
4213static int tracing_release_generic_tr(struct inode *inode, struct file *file)
4214{
4215 struct trace_array *tr = inode->i_private;
4216
4217 trace_array_put(tr);
bc0c38d1
SR
4218 return 0;
4219}
4220
7b85af63
SRRH
4221static int tracing_single_release_tr(struct inode *inode, struct file *file)
4222{
4223 struct trace_array *tr = inode->i_private;
4224
4225 trace_array_put(tr);
4226
4227 return single_release(inode, file);
4228}
4229
bc0c38d1
SR
4230static int tracing_open(struct inode *inode, struct file *file)
4231{
6484c71c 4232 struct trace_array *tr = inode->i_private;
85a2f9b4
SR
4233 struct trace_iterator *iter;
4234 int ret = 0;
bc0c38d1 4235
ff451961
SRRH
4236 if (trace_array_get(tr) < 0)
4237 return -ENODEV;
4238
4acd4d00 4239 /* If this file was open for write, then erase contents */
6484c71c
ON
4240 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
4241 int cpu = tracing_get_cpu(inode);
8dd33bcb
BY
4242 struct trace_buffer *trace_buf = &tr->trace_buffer;
4243
4244#ifdef CONFIG_TRACER_MAX_TRACE
4245 if (tr->current_trace->print_max)
4246 trace_buf = &tr->max_buffer;
4247#endif
6484c71c
ON
4248
4249 if (cpu == RING_BUFFER_ALL_CPUS)
8dd33bcb 4250 tracing_reset_online_cpus(trace_buf);
4acd4d00 4251 else
8dd33bcb 4252 tracing_reset(trace_buf, cpu);
4acd4d00 4253 }
bc0c38d1 4254
4acd4d00 4255 if (file->f_mode & FMODE_READ) {
6484c71c 4256 iter = __tracing_open(inode, file, false);
4acd4d00
SR
4257 if (IS_ERR(iter))
4258 ret = PTR_ERR(iter);
983f938a 4259 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
4acd4d00
SR
4260 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4261 }
ff451961
SRRH
4262
4263 if (ret < 0)
4264 trace_array_put(tr);
4265
bc0c38d1
SR
4266 return ret;
4267}
4268
607e2ea1
SRRH
4269/*
4270 * Some tracers are not suitable for instance buffers.
4271 * A tracer is always available for the global array (toplevel)
4272 * or if it explicitly states that it is.
4273 */
4274static bool
4275trace_ok_for_array(struct tracer *t, struct trace_array *tr)
4276{
4277 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
4278}
4279
4280/* Find the next tracer that this trace array may use */
4281static struct tracer *
4282get_tracer_for_array(struct trace_array *tr, struct tracer *t)
4283{
4284 while (t && !trace_ok_for_array(t, tr))
4285 t = t->next;
4286
4287 return t;
4288}
4289
e309b41d 4290static void *
bc0c38d1
SR
4291t_next(struct seq_file *m, void *v, loff_t *pos)
4292{
607e2ea1 4293 struct trace_array *tr = m->private;
f129e965 4294 struct tracer *t = v;
bc0c38d1
SR
4295
4296 (*pos)++;
4297
4298 if (t)
607e2ea1 4299 t = get_tracer_for_array(tr, t->next);
bc0c38d1 4300
bc0c38d1
SR
4301 return t;
4302}
4303
4304static void *t_start(struct seq_file *m, loff_t *pos)
4305{
607e2ea1 4306 struct trace_array *tr = m->private;
f129e965 4307 struct tracer *t;
bc0c38d1
SR
4308 loff_t l = 0;
4309
4310 mutex_lock(&trace_types_lock);
607e2ea1
SRRH
4311
4312 t = get_tracer_for_array(tr, trace_types);
4313 for (; t && l < *pos; t = t_next(m, t, &l))
4314 ;
bc0c38d1
SR
4315
4316 return t;
4317}
4318
4319static void t_stop(struct seq_file *m, void *p)
4320{
4321 mutex_unlock(&trace_types_lock);
4322}
4323
4324static int t_show(struct seq_file *m, void *v)
4325{
4326 struct tracer *t = v;
4327
4328 if (!t)
4329 return 0;
4330
fa6f0cc7 4331 seq_puts(m, t->name);
bc0c38d1
SR
4332 if (t->next)
4333 seq_putc(m, ' ');
4334 else
4335 seq_putc(m, '\n');
4336
4337 return 0;
4338}
4339
88e9d34c 4340static const struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
4341 .start = t_start,
4342 .next = t_next,
4343 .stop = t_stop,
4344 .show = t_show,
bc0c38d1
SR
4345};
4346
4347static int show_traces_open(struct inode *inode, struct file *file)
4348{
607e2ea1
SRRH
4349 struct trace_array *tr = inode->i_private;
4350 struct seq_file *m;
4351 int ret;
4352
60a11774
SR
4353 if (tracing_disabled)
4354 return -ENODEV;
4355
607e2ea1
SRRH
4356 ret = seq_open(file, &show_traces_seq_ops);
4357 if (ret)
4358 return ret;
4359
4360 m = file->private_data;
4361 m->private = tr;
4362
4363 return 0;
bc0c38d1
SR
4364}
4365
4acd4d00
SR
4366static ssize_t
4367tracing_write_stub(struct file *filp, const char __user *ubuf,
4368 size_t count, loff_t *ppos)
4369{
4370 return count;
4371}
4372
098c879e 4373loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
364829b1 4374{
098c879e
SRRH
4375 int ret;
4376
364829b1 4377 if (file->f_mode & FMODE_READ)
098c879e 4378 ret = seq_lseek(file, offset, whence);
364829b1 4379 else
098c879e
SRRH
4380 file->f_pos = ret = 0;
4381
4382 return ret;
364829b1
SP
4383}
4384
5e2336a0 4385static const struct file_operations tracing_fops = {
4bf39a94
IM
4386 .open = tracing_open,
4387 .read = seq_read,
4acd4d00 4388 .write = tracing_write_stub,
098c879e 4389 .llseek = tracing_lseek,
4bf39a94 4390 .release = tracing_release,
bc0c38d1
SR
4391};
4392
5e2336a0 4393static const struct file_operations show_traces_fops = {
c7078de1
IM
4394 .open = show_traces_open,
4395 .read = seq_read,
4396 .release = seq_release,
b444786f 4397 .llseek = seq_lseek,
c7078de1
IM
4398};
4399
4400static ssize_t
4401tracing_cpumask_read(struct file *filp, char __user *ubuf,
4402 size_t count, loff_t *ppos)
4403{
ccfe9e42 4404 struct trace_array *tr = file_inode(filp)->i_private;
90e406f9 4405 char *mask_str;
36dfe925 4406 int len;
c7078de1 4407
90e406f9
CD
4408 len = snprintf(NULL, 0, "%*pb\n",
4409 cpumask_pr_args(tr->tracing_cpumask)) + 1;
4410 mask_str = kmalloc(len, GFP_KERNEL);
4411 if (!mask_str)
4412 return -ENOMEM;
36dfe925 4413
90e406f9 4414 len = snprintf(mask_str, len, "%*pb\n",
1a40243b
TH
4415 cpumask_pr_args(tr->tracing_cpumask));
4416 if (len >= count) {
36dfe925
IM
4417 count = -EINVAL;
4418 goto out_err;
4419 }
90e406f9 4420 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
36dfe925
IM
4421
4422out_err:
90e406f9 4423 kfree(mask_str);
c7078de1
IM
4424
4425 return count;
4426}
4427
4428static ssize_t
4429tracing_cpumask_write(struct file *filp, const char __user *ubuf,
4430 size_t count, loff_t *ppos)
4431{
ccfe9e42 4432 struct trace_array *tr = file_inode(filp)->i_private;
9e01c1b7 4433 cpumask_var_t tracing_cpumask_new;
2b6080f2 4434 int err, cpu;
9e01c1b7
RR
4435
4436 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
4437 return -ENOMEM;
c7078de1 4438
9e01c1b7 4439 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 4440 if (err)
36dfe925
IM
4441 goto err_unlock;
4442
a5e25883 4443 local_irq_disable();
0b9b12c1 4444 arch_spin_lock(&tr->max_lock);
ab46428c 4445 for_each_tracing_cpu(cpu) {
36dfe925
IM
4446 /*
4447 * Increase/decrease the disabled counter if we are
4448 * about to flip a bit in the cpumask:
4449 */
ccfe9e42 4450 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
9e01c1b7 4451 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
12883efb
SRRH
4452 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4453 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
36dfe925 4454 }
ccfe9e42 4455 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
9e01c1b7 4456 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
12883efb
SRRH
4457 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4458 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
36dfe925
IM
4459 }
4460 }
0b9b12c1 4461 arch_spin_unlock(&tr->max_lock);
a5e25883 4462 local_irq_enable();
36dfe925 4463
ccfe9e42 4464 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
9e01c1b7 4465 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
4466
4467 return count;
36dfe925
IM
4468
4469err_unlock:
215368e8 4470 free_cpumask_var(tracing_cpumask_new);
36dfe925
IM
4471
4472 return err;
c7078de1
IM
4473}
4474
5e2336a0 4475static const struct file_operations tracing_cpumask_fops = {
ccfe9e42 4476 .open = tracing_open_generic_tr,
c7078de1
IM
4477 .read = tracing_cpumask_read,
4478 .write = tracing_cpumask_write,
ccfe9e42 4479 .release = tracing_release_generic_tr,
b444786f 4480 .llseek = generic_file_llseek,
bc0c38d1
SR
4481};
4482
fdb372ed 4483static int tracing_trace_options_show(struct seq_file *m, void *v)
bc0c38d1 4484{
d8e83d26 4485 struct tracer_opt *trace_opts;
2b6080f2 4486 struct trace_array *tr = m->private;
d8e83d26 4487 u32 tracer_flags;
d8e83d26 4488 int i;
adf9f195 4489
d8e83d26 4490 mutex_lock(&trace_types_lock);
2b6080f2
SR
4491 tracer_flags = tr->current_trace->flags->val;
4492 trace_opts = tr->current_trace->flags->opts;
d8e83d26 4493
bc0c38d1 4494 for (i = 0; trace_options[i]; i++) {
983f938a 4495 if (tr->trace_flags & (1 << i))
fdb372ed 4496 seq_printf(m, "%s\n", trace_options[i]);
bc0c38d1 4497 else
fdb372ed 4498 seq_printf(m, "no%s\n", trace_options[i]);
bc0c38d1
SR
4499 }
4500
adf9f195
FW
4501 for (i = 0; trace_opts[i].name; i++) {
4502 if (tracer_flags & trace_opts[i].bit)
fdb372ed 4503 seq_printf(m, "%s\n", trace_opts[i].name);
adf9f195 4504 else
fdb372ed 4505 seq_printf(m, "no%s\n", trace_opts[i].name);
adf9f195 4506 }
d8e83d26 4507 mutex_unlock(&trace_types_lock);
adf9f195 4508
fdb372ed 4509 return 0;
bc0c38d1 4510}
bc0c38d1 4511
8c1a49ae 4512static int __set_tracer_option(struct trace_array *tr,
8d18eaaf
LZ
4513 struct tracer_flags *tracer_flags,
4514 struct tracer_opt *opts, int neg)
4515{
d39cdd20 4516 struct tracer *trace = tracer_flags->trace;
8d18eaaf 4517 int ret;
bc0c38d1 4518
8c1a49ae 4519 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
8d18eaaf
LZ
4520 if (ret)
4521 return ret;
4522
4523 if (neg)
4524 tracer_flags->val &= ~opts->bit;
4525 else
4526 tracer_flags->val |= opts->bit;
4527 return 0;
bc0c38d1
SR
4528}
4529
adf9f195 4530/* Try to assign a tracer specific option */
8c1a49ae 4531static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
adf9f195 4532{
8c1a49ae 4533 struct tracer *trace = tr->current_trace;
7770841e 4534 struct tracer_flags *tracer_flags = trace->flags;
adf9f195 4535 struct tracer_opt *opts = NULL;
8d18eaaf 4536 int i;
adf9f195 4537
7770841e
Z
4538 for (i = 0; tracer_flags->opts[i].name; i++) {
4539 opts = &tracer_flags->opts[i];
adf9f195 4540
8d18eaaf 4541 if (strcmp(cmp, opts->name) == 0)
8c1a49ae 4542 return __set_tracer_option(tr, trace->flags, opts, neg);
adf9f195 4543 }
adf9f195 4544
8d18eaaf 4545 return -EINVAL;
adf9f195
FW
4546}
4547
613f04a0
SRRH
4548/* Some tracers require overwrite to stay enabled */
4549int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
4550{
4551 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
4552 return -1;
4553
4554 return 0;
4555}
4556
2b6080f2 4557int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
af4617bd
SR
4558{
4559 /* do nothing if flag is already set */
983f938a 4560 if (!!(tr->trace_flags & mask) == !!enabled)
613f04a0
SRRH
4561 return 0;
4562
4563 /* Give the tracer a chance to approve the change */
2b6080f2 4564 if (tr->current_trace->flag_changed)
bf6065b5 4565 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
613f04a0 4566 return -EINVAL;
af4617bd
SR
4567
4568 if (enabled)
983f938a 4569 tr->trace_flags |= mask;
af4617bd 4570 else
983f938a 4571 tr->trace_flags &= ~mask;
e870e9a1
LZ
4572
4573 if (mask == TRACE_ITER_RECORD_CMD)
4574 trace_event_enable_cmd_record(enabled);
750912fa 4575
d914ba37
JF
4576 if (mask == TRACE_ITER_RECORD_TGID) {
4577 if (!tgid_map)
6396bb22
KC
4578 tgid_map = kcalloc(PID_MAX_DEFAULT + 1,
4579 sizeof(*tgid_map),
d914ba37
JF
4580 GFP_KERNEL);
4581 if (!tgid_map) {
4582 tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
4583 return -ENOMEM;
4584 }
4585
4586 trace_event_enable_tgid_record(enabled);
4587 }
4588
c37775d5
SR
4589 if (mask == TRACE_ITER_EVENT_FORK)
4590 trace_event_follow_fork(tr, enabled);
4591
1e10486f
NK
4592 if (mask == TRACE_ITER_FUNC_FORK)
4593 ftrace_pid_follow_fork(tr, enabled);
4594
80902822 4595 if (mask == TRACE_ITER_OVERWRITE) {
12883efb 4596 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
80902822 4597#ifdef CONFIG_TRACER_MAX_TRACE
12883efb 4598 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
80902822
SRRH
4599#endif
4600 }
81698831 4601
b9f9108c 4602 if (mask == TRACE_ITER_PRINTK) {
81698831 4603 trace_printk_start_stop_comm(enabled);
b9f9108c
SRRH
4604 trace_printk_control(enabled);
4605 }
613f04a0
SRRH
4606
4607 return 0;
af4617bd
SR
4608}
4609
2b6080f2 4610static int trace_set_options(struct trace_array *tr, char *option)
bc0c38d1 4611{
8d18eaaf 4612 char *cmp;
bc0c38d1 4613 int neg = 0;
591a033d 4614 int ret;
a4d1e688 4615 size_t orig_len = strlen(option);
3d739c1f 4616 int len;
bc0c38d1 4617
7bcfaf54 4618 cmp = strstrip(option);
bc0c38d1 4619
3d739c1f
SRV
4620 len = str_has_prefix(cmp, "no");
4621 if (len)
bc0c38d1 4622 neg = 1;
3d739c1f
SRV
4623
4624 cmp += len;
bc0c38d1 4625
69d34da2
SRRH
4626 mutex_lock(&trace_types_lock);
4627
591a033d 4628 ret = match_string(trace_options, -1, cmp);
adf9f195 4629 /* If no option could be set, test the specific tracer options */
591a033d 4630 if (ret < 0)
8c1a49ae 4631 ret = set_tracer_option(tr, cmp, neg);
591a033d
YX
4632 else
4633 ret = set_tracer_flag(tr, 1 << ret, !neg);
69d34da2
SRRH
4634
4635 mutex_unlock(&trace_types_lock);
bc0c38d1 4636
a4d1e688
JW
4637 /*
4638 * If the first trailing whitespace is replaced with '\0' by strstrip,
4639 * turn it back into a space.
4640 */
4641 if (orig_len > strlen(option))
4642 option[strlen(option)] = ' ';
4643
7bcfaf54
SR
4644 return ret;
4645}
4646
a4d1e688
JW
4647static void __init apply_trace_boot_options(void)
4648{
4649 char *buf = trace_boot_options_buf;
4650 char *option;
4651
4652 while (true) {
4653 option = strsep(&buf, ",");
4654
4655 if (!option)
4656 break;
a4d1e688 4657
43ed3843
SRRH
4658 if (*option)
4659 trace_set_options(&global_trace, option);
a4d1e688
JW
4660
4661 /* Put back the comma to allow this to be called again */
4662 if (buf)
4663 *(buf - 1) = ',';
4664 }
4665}
4666
7bcfaf54
SR
4667static ssize_t
4668tracing_trace_options_write(struct file *filp, const char __user *ubuf,
4669 size_t cnt, loff_t *ppos)
4670{
2b6080f2
SR
4671 struct seq_file *m = filp->private_data;
4672 struct trace_array *tr = m->private;
7bcfaf54 4673 char buf[64];
613f04a0 4674 int ret;
7bcfaf54
SR
4675
4676 if (cnt >= sizeof(buf))
4677 return -EINVAL;
4678
4afe6495 4679 if (copy_from_user(buf, ubuf, cnt))
7bcfaf54
SR
4680 return -EFAULT;
4681
a8dd2176
SR
4682 buf[cnt] = 0;
4683
2b6080f2 4684 ret = trace_set_options(tr, buf);
613f04a0
SRRH
4685 if (ret < 0)
4686 return ret;
7bcfaf54 4687
cf8517cf 4688 *ppos += cnt;
bc0c38d1
SR
4689
4690 return cnt;
4691}
4692
fdb372ed
LZ
4693static int tracing_trace_options_open(struct inode *inode, struct file *file)
4694{
7b85af63 4695 struct trace_array *tr = inode->i_private;
f77d09a3 4696 int ret;
7b85af63 4697
fdb372ed
LZ
4698 if (tracing_disabled)
4699 return -ENODEV;
2b6080f2 4700
7b85af63
SRRH
4701 if (trace_array_get(tr) < 0)
4702 return -ENODEV;
4703
f77d09a3
AL
4704 ret = single_open(file, tracing_trace_options_show, inode->i_private);
4705 if (ret < 0)
4706 trace_array_put(tr);
4707
4708 return ret;
fdb372ed
LZ
4709}
4710
5e2336a0 4711static const struct file_operations tracing_iter_fops = {
fdb372ed
LZ
4712 .open = tracing_trace_options_open,
4713 .read = seq_read,
4714 .llseek = seq_lseek,
7b85af63 4715 .release = tracing_single_release_tr,
ee6bce52 4716 .write = tracing_trace_options_write,
bc0c38d1
SR
4717};
4718
7bd2f24c
IM
4719static const char readme_msg[] =
4720 "tracing mini-HOWTO:\n\n"
22f45649
SRRH
4721 "# echo 0 > tracing_on : quick way to disable tracing\n"
4722 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
4723 " Important files:\n"
4724 " trace\t\t\t- The static contents of the buffer\n"
4725 "\t\t\t To clear the buffer write into this file: echo > trace\n"
4726 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
4727 " current_tracer\t- function and latency tracers\n"
4728 " available_tracers\t- list of configured tracers for current_tracer\n"
a8d65579 4729 " error_log\t- error log for failed commands (that support it)\n"
22f45649
SRRH
4730 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
4731 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
4732 " trace_clock\t\t-change the clock used to order events\n"
4733 " local: Per cpu clock but may not be synced across CPUs\n"
4734 " global: Synced across CPUs but slows tracing down.\n"
4735 " counter: Not a clock, but just an increment\n"
4736 " uptime: Jiffy counter from time of boot\n"
4737 " perf: Same clock that perf events use\n"
4738#ifdef CONFIG_X86_64
4739 " x86-tsc: TSC cycle counter\n"
4740#endif
2c1ea60b
TZ
4741 "\n timestamp_mode\t-view the mode used to timestamp events\n"
4742 " delta: Delta difference against a buffer-wide timestamp\n"
4743 " absolute: Absolute (standalone) timestamp\n"
22f45649 4744 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
fa32e855 4745 "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n"
22f45649
SRRH
4746 " tracing_cpumask\t- Limit which CPUs to trace\n"
4747 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
4748 "\t\t\t Remove sub-buffer with rmdir\n"
4749 " trace_options\t\t- Set format or modify how tracing happens\n"
b9416997 4750 "\t\t\t Disable an option by prefixing 'no' to the\n"
71485c45 4751 "\t\t\t option name\n"
939c7a4f 4752 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
22f45649
SRRH
4753#ifdef CONFIG_DYNAMIC_FTRACE
4754 "\n available_filter_functions - list of functions that can be filtered on\n"
71485c45
SRRH
4755 " set_ftrace_filter\t- echo function name in here to only trace these\n"
4756 "\t\t\t functions\n"
60f1d5e3 4757 "\t accepts: func_full_name or glob-matching-pattern\n"
71485c45
SRRH
4758 "\t modules: Can select a group via module\n"
4759 "\t Format: :mod:<module-name>\n"
4760 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
4761 "\t triggers: a command to perform when function is hit\n"
4762 "\t Format: <function>:<trigger>[:count]\n"
4763 "\t trigger: traceon, traceoff\n"
4764 "\t\t enable_event:<system>:<event>\n"
4765 "\t\t disable_event:<system>:<event>\n"
22f45649 4766#ifdef CONFIG_STACKTRACE
71485c45 4767 "\t\t stacktrace\n"
22f45649
SRRH
4768#endif
4769#ifdef CONFIG_TRACER_SNAPSHOT
71485c45 4770 "\t\t snapshot\n"
22f45649 4771#endif
17a280ea
SRRH
4772 "\t\t dump\n"
4773 "\t\t cpudump\n"
71485c45
SRRH
4774 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
4775 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
4776 "\t The first one will disable tracing every time do_fault is hit\n"
4777 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
4778 "\t The first time do trap is hit and it disables tracing, the\n"
4779 "\t counter will decrement to 2. If tracing is already disabled,\n"
4780 "\t the counter will not decrement. It only decrements when the\n"
4781 "\t trigger did work\n"
4782 "\t To remove trigger without count:\n"
4783 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
4784 "\t To remove trigger with a count:\n"
4785 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
22f45649 4786 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
71485c45
SRRH
4787 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
4788 "\t modules: Can select a group via module command :mod:\n"
4789 "\t Does not accept triggers\n"
22f45649
SRRH
4790#endif /* CONFIG_DYNAMIC_FTRACE */
4791#ifdef CONFIG_FUNCTION_TRACER
71485c45
SRRH
4792 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
4793 "\t\t (function)\n"
22f45649
SRRH
4794#endif
4795#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4796 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
d048a8c7 4797 " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
22f45649
SRRH
4798 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
4799#endif
4800#ifdef CONFIG_TRACER_SNAPSHOT
71485c45
SRRH
4801 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
4802 "\t\t\t snapshot buffer. Read the contents for more\n"
4803 "\t\t\t information\n"
22f45649 4804#endif
991821c8 4805#ifdef CONFIG_STACK_TRACER
22f45649
SRRH
4806 " stack_trace\t\t- Shows the max stack trace when active\n"
4807 " stack_max_size\t- Shows current max stack size that was traced\n"
71485c45
SRRH
4808 "\t\t\t Write into this file to reset the max size (trigger a\n"
4809 "\t\t\t new trace)\n"
22f45649 4810#ifdef CONFIG_DYNAMIC_FTRACE
71485c45
SRRH
4811 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
4812 "\t\t\t traces\n"
22f45649 4813#endif
991821c8 4814#endif /* CONFIG_STACK_TRACER */
5448d44c
MH
4815#ifdef CONFIG_DYNAMIC_EVENTS
4816 " dynamic_events\t\t- Add/remove/show the generic dynamic events\n"
4817 "\t\t\t Write into this file to define/undefine new trace events.\n"
4818#endif
6b0b7551 4819#ifdef CONFIG_KPROBE_EVENTS
86425625
MH
4820 " kprobe_events\t\t- Add/remove/show the kernel dynamic events\n"
4821 "\t\t\t Write into this file to define/undefine new trace events.\n"
4822#endif
6b0b7551 4823#ifdef CONFIG_UPROBE_EVENTS
86425625
MH
4824 " uprobe_events\t\t- Add/remove/show the userspace dynamic events\n"
4825 "\t\t\t Write into this file to define/undefine new trace events.\n"
4826#endif
6b0b7551 4827#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
86425625 4828 "\t accepts: event-definitions (one definition per line)\n"
c3ca46ef
MH
4829 "\t Format: p[:[<group>/]<event>] <place> [<args>]\n"
4830 "\t r[maxactive][:[<group>/]<event>] <place> [<args>]\n"
7bbab38d
MH
4831#ifdef CONFIG_HIST_TRIGGERS
4832 "\t s:[synthetic/]<event> <field> [<field>]\n"
4833#endif
86425625 4834 "\t -:[<group>/]<event>\n"
6b0b7551 4835#ifdef CONFIG_KPROBE_EVENTS
86425625 4836 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
35b6f55a 4837 "place (kretprobe): [<module>:]<symbol>[+<offset>]|<memaddr>\n"
86425625 4838#endif
6b0b7551 4839#ifdef CONFIG_UPROBE_EVENTS
1cc33161 4840 " place (uprobe): <path>:<offset>[(ref_ctr_offset)]\n"
86425625
MH
4841#endif
4842 "\t args: <name>=fetcharg[:type]\n"
4843 "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
a1303af5 4844#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
e65f7ae7 4845 "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
a1303af5 4846#else
e65f7ae7 4847 "\t $stack<index>, $stack, $retval, $comm,\n"
a1303af5 4848#endif
e65f7ae7 4849 "\t +|-[u]<offset>(<fetcharg>)\n"
60c2e0ce 4850 "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
88903c46 4851 "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
40b53b77 4852 "\t <type>\\[<array-size>\\]\n"
7bbab38d
MH
4853#ifdef CONFIG_HIST_TRIGGERS
4854 "\t field: <stype> <name>;\n"
4855 "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
4856 "\t [unsigned] char/int/long\n"
4857#endif
86425625 4858#endif
26f25564
TZ
4859 " events/\t\t- Directory containing all trace event subsystems:\n"
4860 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
4861 " events/<system>/\t- Directory containing all trace events for <system>:\n"
71485c45
SRRH
4862 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
4863 "\t\t\t events\n"
26f25564 4864 " filter\t\t- If set, only events passing filter are traced\n"
71485c45
SRRH
4865 " events/<system>/<event>/\t- Directory containing control files for\n"
4866 "\t\t\t <event>:\n"
26f25564
TZ
4867 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
4868 " filter\t\t- If set, only events passing filter are traced\n"
4869 " trigger\t\t- If set, a command to perform when event is hit\n"
71485c45
SRRH
4870 "\t Format: <trigger>[:count][if <filter>]\n"
4871 "\t trigger: traceon, traceoff\n"
4872 "\t enable_event:<system>:<event>\n"
4873 "\t disable_event:<system>:<event>\n"
d0bad49b
TZ
4874#ifdef CONFIG_HIST_TRIGGERS
4875 "\t enable_hist:<system>:<event>\n"
4876 "\t disable_hist:<system>:<event>\n"
4877#endif
26f25564 4878#ifdef CONFIG_STACKTRACE
71485c45 4879 "\t\t stacktrace\n"
26f25564
TZ
4880#endif
4881#ifdef CONFIG_TRACER_SNAPSHOT
71485c45 4882 "\t\t snapshot\n"
7ef224d1
TZ
4883#endif
4884#ifdef CONFIG_HIST_TRIGGERS
4885 "\t\t hist (see below)\n"
26f25564 4886#endif
71485c45
SRRH
4887 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
4888 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
4889 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
4890 "\t events/block/block_unplug/trigger\n"
4891 "\t The first disables tracing every time block_unplug is hit.\n"
4892 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
4893 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
4894 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
4895 "\t Like function triggers, the counter is only decremented if it\n"
4896 "\t enabled or disabled tracing.\n"
4897 "\t To remove a trigger without a count:\n"
4898 "\t echo '!<trigger> > <system>/<event>/trigger\n"
4899 "\t To remove a trigger with a count:\n"
4900 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
4901 "\t Filters can be ignored when removing a trigger.\n"
7ef224d1
TZ
4902#ifdef CONFIG_HIST_TRIGGERS
4903 " hist trigger\t- If set, event hits are aggregated into a hash table\n"
76a3b0c8 4904 "\t Format: hist:keys=<field1[,field2,...]>\n"
f2606835 4905 "\t [:values=<field1[,field2,...]>]\n"
e62347d2 4906 "\t [:sort=<field1[,field2,...]>]\n"
7ef224d1 4907 "\t [:size=#entries]\n"
e86ae9ba 4908 "\t [:pause][:continue][:clear]\n"
5463bfda 4909 "\t [:name=histname1]\n"
c3e49506 4910 "\t [:<handler>.<action>]\n"
7ef224d1
TZ
4911 "\t [if <filter>]\n\n"
4912 "\t When a matching event is hit, an entry is added to a hash\n"
f2606835
TZ
4913 "\t table using the key(s) and value(s) named, and the value of a\n"
4914 "\t sum called 'hitcount' is incremented. Keys and values\n"
4915 "\t correspond to fields in the event's format description. Keys\n"
69a0200c
TZ
4916 "\t can be any field, or the special string 'stacktrace'.\n"
4917 "\t Compound keys consisting of up to two fields can be specified\n"
4918 "\t by the 'keys' keyword. Values must correspond to numeric\n"
4919 "\t fields. Sort keys consisting of up to two fields can be\n"
4920 "\t specified using the 'sort' keyword. The sort direction can\n"
4921 "\t be modified by appending '.descending' or '.ascending' to a\n"
4922 "\t sort field. The 'size' parameter can be used to specify more\n"
5463bfda
TZ
4923 "\t or fewer than the default 2048 entries for the hashtable size.\n"
4924 "\t If a hist trigger is given a name using the 'name' parameter,\n"
4925 "\t its histogram data will be shared with other triggers of the\n"
4926 "\t same name, and trigger hits will update this common data.\n\n"
7ef224d1 4927 "\t Reading the 'hist' file for the event will dump the hash\n"
52a7f16d
TZ
4928 "\t table in its entirety to stdout. If there are multiple hist\n"
4929 "\t triggers attached to an event, there will be a table for each\n"
5463bfda
TZ
4930 "\t trigger in the output. The table displayed for a named\n"
4931 "\t trigger will be the same as any other instance having the\n"
4932 "\t same name. The default format used to display a given field\n"
4933 "\t can be modified by appending any of the following modifiers\n"
4934 "\t to the field name, as applicable:\n\n"
c6afad49
TZ
4935 "\t .hex display a number as a hex value\n"
4936 "\t .sym display an address as a symbol\n"
6b4827ad 4937 "\t .sym-offset display an address as a symbol and offset\n"
31696198 4938 "\t .execname display a common_pid as a program name\n"
860f9f6b
TZ
4939 "\t .syscall display a syscall id as a syscall name\n"
4940 "\t .log2 display log2 value rather than raw number\n"
4941 "\t .usecs display a common_timestamp in microseconds\n\n"
83e99914
TZ
4942 "\t The 'pause' parameter can be used to pause an existing hist\n"
4943 "\t trigger or to start a hist trigger but not log any events\n"
4944 "\t until told to do so. 'continue' can be used to start or\n"
4945 "\t restart a paused hist trigger.\n\n"
e86ae9ba
TZ
4946 "\t The 'clear' parameter will clear the contents of a running\n"
4947 "\t hist trigger and leave its current paused/active state\n"
4948 "\t unchanged.\n\n"
d0bad49b
TZ
4949 "\t The enable_hist and disable_hist triggers can be used to\n"
4950 "\t have one event conditionally start and stop another event's\n"
9e5a36a3 4951 "\t already-attached hist trigger. The syntax is analogous to\n"
c3e49506
TZ
4952 "\t the enable_event and disable_event triggers.\n\n"
4953 "\t Hist trigger handlers and actions are executed whenever a\n"
4954 "\t a histogram entry is added or updated. They take the form:\n\n"
4955 "\t <handler>.<action>\n\n"
4956 "\t The available handlers are:\n\n"
4957 "\t onmatch(matching.event) - invoke on addition or update\n"
dff81f55
TZ
4958 "\t onmax(var) - invoke if var exceeds current max\n"
4959 "\t onchange(var) - invoke action if var changes\n\n"
c3e49506 4960 "\t The available actions are:\n\n"
e91eefd7 4961 "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
c3e49506 4962 "\t save(field,...) - save current event fields\n"
a3785b7e
TZ
4963#ifdef CONFIG_TRACER_SNAPSHOT
4964 "\t snapshot() - snapshot the trace buffer\n"
4965#endif
7ef224d1 4966#endif
7bd2f24c
IM
4967;
4968
4969static ssize_t
4970tracing_readme_read(struct file *filp, char __user *ubuf,
4971 size_t cnt, loff_t *ppos)
4972{
4973 return simple_read_from_buffer(ubuf, cnt, ppos,
4974 readme_msg, strlen(readme_msg));
4975}
4976
5e2336a0 4977static const struct file_operations tracing_readme_fops = {
c7078de1
IM
4978 .open = tracing_open_generic,
4979 .read = tracing_readme_read,
b444786f 4980 .llseek = generic_file_llseek,
7bd2f24c
IM
4981};
4982
99c621d7
MS
4983static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos)
4984{
4985 int *ptr = v;
4986
4987 if (*pos || m->count)
4988 ptr++;
4989
4990 (*pos)++;
4991
4992 for (; ptr <= &tgid_map[PID_MAX_DEFAULT]; ptr++) {
4993 if (trace_find_tgid(*ptr))
4994 return ptr;
4995 }
4996
4997 return NULL;
4998}
4999
5000static void *saved_tgids_start(struct seq_file *m, loff_t *pos)
5001{
5002 void *v;
5003 loff_t l = 0;
5004
5005 if (!tgid_map)
5006 return NULL;
5007
5008 v = &tgid_map[0];
5009 while (l <= *pos) {
5010 v = saved_tgids_next(m, v, &l);
5011 if (!v)
5012 return NULL;
5013 }
5014
5015 return v;
5016}
5017
5018static void saved_tgids_stop(struct seq_file *m, void *v)
5019{
5020}
5021
5022static int saved_tgids_show(struct seq_file *m, void *v)
5023{
5024 int pid = (int *)v - tgid_map;
5025
5026 seq_printf(m, "%d %d\n", pid, trace_find_tgid(pid));
5027 return 0;
5028}
5029
5030static const struct seq_operations tracing_saved_tgids_seq_ops = {
5031 .start = saved_tgids_start,
5032 .stop = saved_tgids_stop,
5033 .next = saved_tgids_next,
5034 .show = saved_tgids_show,
5035};
5036
5037static int tracing_saved_tgids_open(struct inode *inode, struct file *filp)
5038{
5039 if (tracing_disabled)
5040 return -ENODEV;
5041
5042 return seq_open(filp, &tracing_saved_tgids_seq_ops);
5043}
5044
5045
5046static const struct file_operations tracing_saved_tgids_fops = {
5047 .open = tracing_saved_tgids_open,
5048 .read = seq_read,
5049 .llseek = seq_lseek,
5050 .release = seq_release,
5051};
5052
42584c81
YY
5053static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
5054{
5055 unsigned int *ptr = v;
69abe6a5 5056
42584c81
YY
5057 if (*pos || m->count)
5058 ptr++;
69abe6a5 5059
42584c81 5060 (*pos)++;
69abe6a5 5061
939c7a4f
YY
5062 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
5063 ptr++) {
42584c81
YY
5064 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
5065 continue;
69abe6a5 5066
42584c81
YY
5067 return ptr;
5068 }
69abe6a5 5069
42584c81
YY
5070 return NULL;
5071}
5072
5073static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
5074{
5075 void *v;
5076 loff_t l = 0;
69abe6a5 5077
4c27e756
SRRH
5078 preempt_disable();
5079 arch_spin_lock(&trace_cmdline_lock);
5080
939c7a4f 5081 v = &savedcmd->map_cmdline_to_pid[0];
42584c81
YY
5082 while (l <= *pos) {
5083 v = saved_cmdlines_next(m, v, &l);
5084 if (!v)
5085 return NULL;
69abe6a5
AP
5086 }
5087
42584c81
YY
5088 return v;
5089}
5090
5091static void saved_cmdlines_stop(struct seq_file *m, void *v)
5092{
4c27e756
SRRH
5093 arch_spin_unlock(&trace_cmdline_lock);
5094 preempt_enable();
42584c81 5095}
69abe6a5 5096
42584c81
YY
5097static int saved_cmdlines_show(struct seq_file *m, void *v)
5098{
5099 char buf[TASK_COMM_LEN];
5100 unsigned int *pid = v;
69abe6a5 5101
4c27e756 5102 __trace_find_cmdline(*pid, buf);
42584c81
YY
5103 seq_printf(m, "%d %s\n", *pid, buf);
5104 return 0;
5105}
5106
5107static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
5108 .start = saved_cmdlines_start,
5109 .next = saved_cmdlines_next,
5110 .stop = saved_cmdlines_stop,
5111 .show = saved_cmdlines_show,
5112};
5113
5114static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
5115{
5116 if (tracing_disabled)
5117 return -ENODEV;
5118
5119 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
69abe6a5
AP
5120}
5121
5122static const struct file_operations tracing_saved_cmdlines_fops = {
42584c81
YY
5123 .open = tracing_saved_cmdlines_open,
5124 .read = seq_read,
5125 .llseek = seq_lseek,
5126 .release = seq_release,
69abe6a5
AP
5127};
5128
939c7a4f
YY
5129static ssize_t
5130tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
5131 size_t cnt, loff_t *ppos)
5132{
5133 char buf[64];
5134 int r;
5135
5136 arch_spin_lock(&trace_cmdline_lock);
a6af8fbf 5137 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
939c7a4f
YY
5138 arch_spin_unlock(&trace_cmdline_lock);
5139
5140 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5141}
5142
5143static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
5144{
5145 kfree(s->saved_cmdlines);
5146 kfree(s->map_cmdline_to_pid);
5147 kfree(s);
5148}
5149
5150static int tracing_resize_saved_cmdlines(unsigned int val)
5151{
5152 struct saved_cmdlines_buffer *s, *savedcmd_temp;
5153
a6af8fbf 5154 s = kmalloc(sizeof(*s), GFP_KERNEL);
939c7a4f
YY
5155 if (!s)
5156 return -ENOMEM;
5157
5158 if (allocate_cmdlines_buffer(val, s) < 0) {
5159 kfree(s);
5160 return -ENOMEM;
5161 }
5162
5163 arch_spin_lock(&trace_cmdline_lock);
5164 savedcmd_temp = savedcmd;
5165 savedcmd = s;
5166 arch_spin_unlock(&trace_cmdline_lock);
5167 free_saved_cmdlines_buffer(savedcmd_temp);
5168
5169 return 0;
5170}
5171
5172static ssize_t
5173tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
5174 size_t cnt, loff_t *ppos)
5175{
5176 unsigned long val;
5177 int ret;
5178
5179 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5180 if (ret)
5181 return ret;
5182
5183 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
5184 if (!val || val > PID_MAX_DEFAULT)
5185 return -EINVAL;
5186
5187 ret = tracing_resize_saved_cmdlines((unsigned int)val);
5188 if (ret < 0)
5189 return ret;
5190
5191 *ppos += cnt;
5192
5193 return cnt;
5194}
5195
5196static const struct file_operations tracing_saved_cmdlines_size_fops = {
5197 .open = tracing_open_generic,
5198 .read = tracing_saved_cmdlines_size_read,
5199 .write = tracing_saved_cmdlines_size_write,
5200};
5201
681bec03 5202#ifdef CONFIG_TRACE_EVAL_MAP_FILE
23bf8cb8 5203static union trace_eval_map_item *
f57a4143 5204update_eval_map(union trace_eval_map_item *ptr)
9828413d 5205{
00f4b652 5206 if (!ptr->map.eval_string) {
9828413d
SRRH
5207 if (ptr->tail.next) {
5208 ptr = ptr->tail.next;
5209 /* Set ptr to the next real item (skip head) */
5210 ptr++;
5211 } else
5212 return NULL;
5213 }
5214 return ptr;
5215}
5216
f57a4143 5217static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
9828413d 5218{
23bf8cb8 5219 union trace_eval_map_item *ptr = v;
9828413d
SRRH
5220
5221 /*
5222 * Paranoid! If ptr points to end, we don't want to increment past it.
5223 * This really should never happen.
5224 */
f57a4143 5225 ptr = update_eval_map(ptr);
9828413d
SRRH
5226 if (WARN_ON_ONCE(!ptr))
5227 return NULL;
5228
5229 ptr++;
5230
5231 (*pos)++;
5232
f57a4143 5233 ptr = update_eval_map(ptr);
9828413d
SRRH
5234
5235 return ptr;
5236}
5237
f57a4143 5238static void *eval_map_start(struct seq_file *m, loff_t *pos)
9828413d 5239{
23bf8cb8 5240 union trace_eval_map_item *v;
9828413d
SRRH
5241 loff_t l = 0;
5242
1793ed93 5243 mutex_lock(&trace_eval_mutex);
9828413d 5244
23bf8cb8 5245 v = trace_eval_maps;
9828413d
SRRH
5246 if (v)
5247 v++;
5248
5249 while (v && l < *pos) {
f57a4143 5250 v = eval_map_next(m, v, &l);
9828413d
SRRH
5251 }
5252
5253 return v;
5254}
5255
f57a4143 5256static void eval_map_stop(struct seq_file *m, void *v)
9828413d 5257{
1793ed93 5258 mutex_unlock(&trace_eval_mutex);
9828413d
SRRH
5259}
5260
f57a4143 5261static int eval_map_show(struct seq_file *m, void *v)
9828413d 5262{
23bf8cb8 5263 union trace_eval_map_item *ptr = v;
9828413d
SRRH
5264
5265 seq_printf(m, "%s %ld (%s)\n",
00f4b652 5266 ptr->map.eval_string, ptr->map.eval_value,
9828413d
SRRH
5267 ptr->map.system);
5268
5269 return 0;
5270}
5271
f57a4143
JL
5272static const struct seq_operations tracing_eval_map_seq_ops = {
5273 .start = eval_map_start,
5274 .next = eval_map_next,
5275 .stop = eval_map_stop,
5276 .show = eval_map_show,
9828413d
SRRH
5277};
5278
f57a4143 5279static int tracing_eval_map_open(struct inode *inode, struct file *filp)
9828413d
SRRH
5280{
5281 if (tracing_disabled)
5282 return -ENODEV;
5283
f57a4143 5284 return seq_open(filp, &tracing_eval_map_seq_ops);
9828413d
SRRH
5285}
5286
f57a4143
JL
5287static const struct file_operations tracing_eval_map_fops = {
5288 .open = tracing_eval_map_open,
9828413d
SRRH
5289 .read = seq_read,
5290 .llseek = seq_lseek,
5291 .release = seq_release,
5292};
5293
23bf8cb8 5294static inline union trace_eval_map_item *
5f60b351 5295trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
9828413d
SRRH
5296{
5297 /* Return tail of array given the head */
5298 return ptr + ptr->head.length + 1;
5299}
5300
5301static void
f57a4143 5302trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
9828413d
SRRH
5303 int len)
5304{
00f4b652
JL
5305 struct trace_eval_map **stop;
5306 struct trace_eval_map **map;
23bf8cb8
JL
5307 union trace_eval_map_item *map_array;
5308 union trace_eval_map_item *ptr;
9828413d
SRRH
5309
5310 stop = start + len;
5311
5312 /*
23bf8cb8 5313 * The trace_eval_maps contains the map plus a head and tail item,
9828413d
SRRH
5314 * where the head holds the module and length of array, and the
5315 * tail holds a pointer to the next list.
5316 */
6da2ec56 5317 map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
9828413d 5318 if (!map_array) {
f57a4143 5319 pr_warn("Unable to allocate trace eval mapping\n");
9828413d
SRRH
5320 return;
5321 }
5322
1793ed93 5323 mutex_lock(&trace_eval_mutex);
9828413d 5324
23bf8cb8
JL
5325 if (!trace_eval_maps)
5326 trace_eval_maps = map_array;
9828413d 5327 else {
23bf8cb8 5328 ptr = trace_eval_maps;
9828413d 5329 for (;;) {
5f60b351 5330 ptr = trace_eval_jmp_to_tail(ptr);
9828413d
SRRH
5331 if (!ptr->tail.next)
5332 break;
5333 ptr = ptr->tail.next;
5334
5335 }
5336 ptr->tail.next = map_array;
5337 }
5338 map_array->head.mod = mod;
5339 map_array->head.length = len;
5340 map_array++;
5341
5342 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
5343 map_array->map = **map;
5344 map_array++;
5345 }
5346 memset(map_array, 0, sizeof(*map_array));
5347
1793ed93 5348 mutex_unlock(&trace_eval_mutex);
9828413d
SRRH
5349}
5350
f57a4143 5351static void trace_create_eval_file(struct dentry *d_tracer)
9828413d 5352{
681bec03 5353 trace_create_file("eval_map", 0444, d_tracer,
f57a4143 5354 NULL, &tracing_eval_map_fops);
9828413d
SRRH
5355}
5356
681bec03 5357#else /* CONFIG_TRACE_EVAL_MAP_FILE */
f57a4143
JL
5358static inline void trace_create_eval_file(struct dentry *d_tracer) { }
5359static inline void trace_insert_eval_map_file(struct module *mod,
00f4b652 5360 struct trace_eval_map **start, int len) { }
681bec03 5361#endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
9828413d 5362
f57a4143 5363static void trace_insert_eval_map(struct module *mod,
00f4b652 5364 struct trace_eval_map **start, int len)
0c564a53 5365{
00f4b652 5366 struct trace_eval_map **map;
0c564a53
SRRH
5367
5368 if (len <= 0)
5369 return;
5370
5371 map = start;
5372
f57a4143 5373 trace_event_eval_update(map, len);
9828413d 5374
f57a4143 5375 trace_insert_eval_map_file(mod, start, len);
0c564a53
SRRH
5376}
5377
bc0c38d1
SR
5378static ssize_t
5379tracing_set_trace_read(struct file *filp, char __user *ubuf,
5380 size_t cnt, loff_t *ppos)
5381{
2b6080f2 5382 struct trace_array *tr = filp->private_data;
ee6c2c1b 5383 char buf[MAX_TRACER_SIZE+2];
bc0c38d1
SR
5384 int r;
5385
5386 mutex_lock(&trace_types_lock);
2b6080f2 5387 r = sprintf(buf, "%s\n", tr->current_trace->name);
bc0c38d1
SR
5388 mutex_unlock(&trace_types_lock);
5389
4bf39a94 5390 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
5391}
5392
b6f11df2
ACM
5393int tracer_init(struct tracer *t, struct trace_array *tr)
5394{
12883efb 5395 tracing_reset_online_cpus(&tr->trace_buffer);
b6f11df2
ACM
5396 return t->init(tr);
5397}
5398
12883efb 5399static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
438ced17
VN
5400{
5401 int cpu;
737223fb 5402
438ced17 5403 for_each_tracing_cpu(cpu)
12883efb 5404 per_cpu_ptr(buf->data, cpu)->entries = val;
438ced17
VN
5405}
5406
12883efb 5407#ifdef CONFIG_TRACER_MAX_TRACE
d60da506 5408/* resize @tr's buffer to the size of @size_tr's entries */
12883efb
SRRH
5409static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
5410 struct trace_buffer *size_buf, int cpu_id)
d60da506
HT
5411{
5412 int cpu, ret = 0;
5413
5414 if (cpu_id == RING_BUFFER_ALL_CPUS) {
5415 for_each_tracing_cpu(cpu) {
12883efb
SRRH
5416 ret = ring_buffer_resize(trace_buf->buffer,
5417 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
d60da506
HT
5418 if (ret < 0)
5419 break;
12883efb
SRRH
5420 per_cpu_ptr(trace_buf->data, cpu)->entries =
5421 per_cpu_ptr(size_buf->data, cpu)->entries;
d60da506
HT
5422 }
5423 } else {
12883efb
SRRH
5424 ret = ring_buffer_resize(trace_buf->buffer,
5425 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
d60da506 5426 if (ret == 0)
12883efb
SRRH
5427 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
5428 per_cpu_ptr(size_buf->data, cpu_id)->entries;
d60da506
HT
5429 }
5430
5431 return ret;
5432}
12883efb 5433#endif /* CONFIG_TRACER_MAX_TRACE */
d60da506 5434
2b6080f2
SR
5435static int __tracing_resize_ring_buffer(struct trace_array *tr,
5436 unsigned long size, int cpu)
73c5162a
SR
5437{
5438 int ret;
5439
5440 /*
5441 * If kernel or user changes the size of the ring buffer
a123c52b
SR
5442 * we use the size that was given, and we can forget about
5443 * expanding it later.
73c5162a 5444 */
55034cd6 5445 ring_buffer_expanded = true;
73c5162a 5446
b382ede6 5447 /* May be called before buffers are initialized */
12883efb 5448 if (!tr->trace_buffer.buffer)
b382ede6
SR
5449 return 0;
5450
12883efb 5451 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
73c5162a
SR
5452 if (ret < 0)
5453 return ret;
5454
12883efb 5455#ifdef CONFIG_TRACER_MAX_TRACE
2b6080f2
SR
5456 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
5457 !tr->current_trace->use_max_tr)
ef710e10
KM
5458 goto out;
5459
12883efb 5460 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
73c5162a 5461 if (ret < 0) {
12883efb
SRRH
5462 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
5463 &tr->trace_buffer, cpu);
73c5162a 5464 if (r < 0) {
a123c52b
SR
5465 /*
5466 * AARGH! We are left with different
5467 * size max buffer!!!!
5468 * The max buffer is our "snapshot" buffer.
5469 * When a tracer needs a snapshot (one of the
5470 * latency tracers), it swaps the max buffer
5471 * with the saved snap shot. We succeeded to
5472 * update the size of the main buffer, but failed to
5473 * update the size of the max buffer. But when we tried
5474 * to reset the main buffer to the original size, we
5475 * failed there too. This is very unlikely to
5476 * happen, but if it does, warn and kill all
5477 * tracing.
5478 */
73c5162a
SR
5479 WARN_ON(1);
5480 tracing_disabled = 1;
5481 }
5482 return ret;
5483 }
5484
438ced17 5485 if (cpu == RING_BUFFER_ALL_CPUS)
12883efb 5486 set_buffer_entries(&tr->max_buffer, size);
438ced17 5487 else
12883efb 5488 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
438ced17 5489
ef710e10 5490 out:
12883efb
SRRH
5491#endif /* CONFIG_TRACER_MAX_TRACE */
5492
438ced17 5493 if (cpu == RING_BUFFER_ALL_CPUS)
12883efb 5494 set_buffer_entries(&tr->trace_buffer, size);
438ced17 5495 else
12883efb 5496 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
73c5162a
SR
5497
5498 return ret;
5499}
5500
2b6080f2
SR
5501static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
5502 unsigned long size, int cpu_id)
4f271a2a 5503{
83f40318 5504 int ret = size;
4f271a2a
VN
5505
5506 mutex_lock(&trace_types_lock);
5507
438ced17
VN
5508 if (cpu_id != RING_BUFFER_ALL_CPUS) {
5509 /* make sure, this cpu is enabled in the mask */
5510 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
5511 ret = -EINVAL;
5512 goto out;
5513 }
5514 }
4f271a2a 5515
2b6080f2 5516 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
4f271a2a
VN
5517 if (ret < 0)
5518 ret = -ENOMEM;
5519
438ced17 5520out:
4f271a2a
VN
5521 mutex_unlock(&trace_types_lock);
5522
5523 return ret;
5524}
5525
ef710e10 5526
1852fcce
SR
5527/**
5528 * tracing_update_buffers - used by tracing facility to expand ring buffers
5529 *
5530 * To save on memory when the tracing is never used on a system with it
5531 * configured in. The ring buffers are set to a minimum size. But once
5532 * a user starts to use the tracing facility, then they need to grow
5533 * to their default size.
5534 *
5535 * This function is to be called when a tracer is about to be used.
5536 */
5537int tracing_update_buffers(void)
5538{
5539 int ret = 0;
5540
1027fcb2 5541 mutex_lock(&trace_types_lock);
1852fcce 5542 if (!ring_buffer_expanded)
2b6080f2 5543 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
438ced17 5544 RING_BUFFER_ALL_CPUS);
1027fcb2 5545 mutex_unlock(&trace_types_lock);
1852fcce
SR
5546
5547 return ret;
5548}
5549
577b785f
SR
5550struct trace_option_dentry;
5551
37aea98b 5552static void
2b6080f2 5553create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
577b785f 5554
6b450d25
SRRH
5555/*
5556 * Used to clear out the tracer before deletion of an instance.
5557 * Must have trace_types_lock held.
5558 */
5559static void tracing_set_nop(struct trace_array *tr)
5560{
5561 if (tr->current_trace == &nop_trace)
5562 return;
5563
50512ab5 5564 tr->current_trace->enabled--;
6b450d25
SRRH
5565
5566 if (tr->current_trace->reset)
5567 tr->current_trace->reset(tr);
5568
5569 tr->current_trace = &nop_trace;
5570}
5571
41d9c0be 5572static void add_tracer_options(struct trace_array *tr, struct tracer *t)
bc0c38d1 5573{
09d23a1d
SRRH
5574 /* Only enable if the directory has been created already. */
5575 if (!tr->dir)
5576 return;
5577
37aea98b 5578 create_trace_option_files(tr, t);
09d23a1d
SRRH
5579}
5580
5581static int tracing_set_tracer(struct trace_array *tr, const char *buf)
5582{
bc0c38d1 5583 struct tracer *t;
12883efb 5584#ifdef CONFIG_TRACER_MAX_TRACE
34600f0e 5585 bool had_max_tr;
12883efb 5586#endif
d9e54076 5587 int ret = 0;
bc0c38d1 5588
1027fcb2
SR
5589 mutex_lock(&trace_types_lock);
5590
73c5162a 5591 if (!ring_buffer_expanded) {
2b6080f2 5592 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
438ced17 5593 RING_BUFFER_ALL_CPUS);
73c5162a 5594 if (ret < 0)
59f586db 5595 goto out;
73c5162a
SR
5596 ret = 0;
5597 }
5598
bc0c38d1
SR
5599 for (t = trace_types; t; t = t->next) {
5600 if (strcmp(t->name, buf) == 0)
5601 break;
5602 }
c2931e05
FW
5603 if (!t) {
5604 ret = -EINVAL;
5605 goto out;
5606 }
2b6080f2 5607 if (t == tr->current_trace)
bc0c38d1
SR
5608 goto out;
5609
a35873a0
TZ
5610#ifdef CONFIG_TRACER_SNAPSHOT
5611 if (t->use_max_tr) {
5612 arch_spin_lock(&tr->max_lock);
5613 if (tr->cond_snapshot)
5614 ret = -EBUSY;
5615 arch_spin_unlock(&tr->max_lock);
5616 if (ret)
5617 goto out;
5618 }
5619#endif
c7b3ae0b
ZSZ
5620 /* Some tracers won't work on kernel command line */
5621 if (system_state < SYSTEM_RUNNING && t->noboot) {
5622 pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
5623 t->name);
5624 goto out;
5625 }
5626
607e2ea1
SRRH
5627 /* Some tracers are only allowed for the top level buffer */
5628 if (!trace_ok_for_array(t, tr)) {
5629 ret = -EINVAL;
5630 goto out;
5631 }
5632
cf6ab6d9
SRRH
5633 /* If trace pipe files are being read, we can't change the tracer */
5634 if (tr->current_trace->ref) {
5635 ret = -EBUSY;
5636 goto out;
5637 }
5638
9f029e83 5639 trace_branch_disable();
613f04a0 5640
50512ab5 5641 tr->current_trace->enabled--;
613f04a0 5642
2b6080f2
SR
5643 if (tr->current_trace->reset)
5644 tr->current_trace->reset(tr);
34600f0e 5645
74401729 5646 /* Current trace needs to be nop_trace before synchronize_rcu */
2b6080f2 5647 tr->current_trace = &nop_trace;
34600f0e 5648
45ad21ca
SRRH
5649#ifdef CONFIG_TRACER_MAX_TRACE
5650 had_max_tr = tr->allocated_snapshot;
34600f0e
SR
5651
5652 if (had_max_tr && !t->use_max_tr) {
5653 /*
5654 * We need to make sure that the update_max_tr sees that
5655 * current_trace changed to nop_trace to keep it from
5656 * swapping the buffers after we resize it.
5657 * The update_max_tr is called from interrupts disabled
5658 * so a synchronized_sched() is sufficient.
5659 */
74401729 5660 synchronize_rcu();
3209cff4 5661 free_snapshot(tr);
ef710e10 5662 }
12883efb 5663#endif
12883efb
SRRH
5664
5665#ifdef CONFIG_TRACER_MAX_TRACE
34600f0e 5666 if (t->use_max_tr && !had_max_tr) {
2824f503 5667 ret = tracing_alloc_snapshot_instance(tr);
d60da506
HT
5668 if (ret < 0)
5669 goto out;
ef710e10 5670 }
12883efb 5671#endif
577b785f 5672
1c80025a 5673 if (t->init) {
b6f11df2 5674 ret = tracer_init(t, tr);
1c80025a
FW
5675 if (ret)
5676 goto out;
5677 }
bc0c38d1 5678
2b6080f2 5679 tr->current_trace = t;
50512ab5 5680 tr->current_trace->enabled++;
9f029e83 5681 trace_branch_enable(tr);
bc0c38d1
SR
5682 out:
5683 mutex_unlock(&trace_types_lock);
5684
d9e54076
PZ
5685 return ret;
5686}
5687
5688static ssize_t
5689tracing_set_trace_write(struct file *filp, const char __user *ubuf,
5690 size_t cnt, loff_t *ppos)
5691{
607e2ea1 5692 struct trace_array *tr = filp->private_data;
ee6c2c1b 5693 char buf[MAX_TRACER_SIZE+1];
d9e54076
PZ
5694 int i;
5695 size_t ret;
e6e7a65a
FW
5696 int err;
5697
5698 ret = cnt;
d9e54076 5699
ee6c2c1b
LZ
5700 if (cnt > MAX_TRACER_SIZE)
5701 cnt = MAX_TRACER_SIZE;
d9e54076 5702
4afe6495 5703 if (copy_from_user(buf, ubuf, cnt))
d9e54076
PZ
5704 return -EFAULT;
5705
5706 buf[cnt] = 0;
5707
5708 /* strip ending whitespace. */
5709 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
5710 buf[i] = 0;
5711
607e2ea1 5712 err = tracing_set_tracer(tr, buf);
e6e7a65a
FW
5713 if (err)
5714 return err;
d9e54076 5715
cf8517cf 5716 *ppos += ret;
bc0c38d1 5717
c2931e05 5718 return ret;
bc0c38d1
SR
5719}
5720
5721static ssize_t
6508fa76
SF
5722tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
5723 size_t cnt, loff_t *ppos)
bc0c38d1 5724{
bc0c38d1
SR
5725 char buf[64];
5726 int r;
5727
cffae437 5728 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 5729 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
5730 if (r > sizeof(buf))
5731 r = sizeof(buf);
4bf39a94 5732 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
5733}
5734
5735static ssize_t
6508fa76
SF
5736tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
5737 size_t cnt, loff_t *ppos)
bc0c38d1 5738{
5e39841c 5739 unsigned long val;
c6caeeb1 5740 int ret;
bc0c38d1 5741
22fe9b54
PH
5742 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5743 if (ret)
c6caeeb1 5744 return ret;
bc0c38d1
SR
5745
5746 *ptr = val * 1000;
5747
5748 return cnt;
5749}
5750
6508fa76
SF
5751static ssize_t
5752tracing_thresh_read(struct file *filp, char __user *ubuf,
5753 size_t cnt, loff_t *ppos)
5754{
5755 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
5756}
5757
5758static ssize_t
5759tracing_thresh_write(struct file *filp, const char __user *ubuf,
5760 size_t cnt, loff_t *ppos)
5761{
5762 struct trace_array *tr = filp->private_data;
5763 int ret;
5764
5765 mutex_lock(&trace_types_lock);
5766 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
5767 if (ret < 0)
5768 goto out;
5769
5770 if (tr->current_trace->update_thresh) {
5771 ret = tr->current_trace->update_thresh(tr);
5772 if (ret < 0)
5773 goto out;
5774 }
5775
5776 ret = cnt;
5777out:
5778 mutex_unlock(&trace_types_lock);
5779
5780 return ret;
5781}
5782
f971cc9a 5783#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
e428abbb 5784
6508fa76
SF
5785static ssize_t
5786tracing_max_lat_read(struct file *filp, char __user *ubuf,
5787 size_t cnt, loff_t *ppos)
5788{
5789 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos);
5790}
5791
5792static ssize_t
5793tracing_max_lat_write(struct file *filp, const char __user *ubuf,
5794 size_t cnt, loff_t *ppos)
5795{
5796 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos);
5797}
5798
e428abbb
CG
5799#endif
5800
b3806b43
SR
5801static int tracing_open_pipe(struct inode *inode, struct file *filp)
5802{
15544209 5803 struct trace_array *tr = inode->i_private;
b3806b43 5804 struct trace_iterator *iter;
b04cc6b1 5805 int ret = 0;
b3806b43
SR
5806
5807 if (tracing_disabled)
5808 return -ENODEV;
5809
7b85af63
SRRH
5810 if (trace_array_get(tr) < 0)
5811 return -ENODEV;
5812
b04cc6b1
FW
5813 mutex_lock(&trace_types_lock);
5814
b3806b43
SR
5815 /* create a buffer to store the information to pass to userspace */
5816 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
b04cc6b1
FW
5817 if (!iter) {
5818 ret = -ENOMEM;
f77d09a3 5819 __trace_array_put(tr);
b04cc6b1
FW
5820 goto out;
5821 }
b3806b43 5822
3a161d99 5823 trace_seq_init(&iter->seq);
d716ff71 5824 iter->trace = tr->current_trace;
d7350c3f 5825
4462344e 5826 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
b04cc6b1 5827 ret = -ENOMEM;
d7350c3f 5828 goto fail;
4462344e
RR
5829 }
5830
a309720c 5831 /* trace pipe does not show start of buffer */
4462344e 5832 cpumask_setall(iter->started);
a309720c 5833
983f938a 5834 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
112f38a7
SR
5835 iter->iter_flags |= TRACE_FILE_LAT_FMT;
5836
8be0709f 5837 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
58e8eedf 5838 if (trace_clocks[tr->clock_id].in_ns)
8be0709f
DS
5839 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
5840
15544209
ON
5841 iter->tr = tr;
5842 iter->trace_buffer = &tr->trace_buffer;
5843 iter->cpu_file = tracing_get_cpu(inode);
d7350c3f 5844 mutex_init(&iter->mutex);
b3806b43
SR
5845 filp->private_data = iter;
5846
107bad8b
SR
5847 if (iter->trace->pipe_open)
5848 iter->trace->pipe_open(iter);
107bad8b 5849
b444786f 5850 nonseekable_open(inode, filp);
cf6ab6d9
SRRH
5851
5852 tr->current_trace->ref++;
b04cc6b1
FW
5853out:
5854 mutex_unlock(&trace_types_lock);
5855 return ret;
d7350c3f
FW
5856
5857fail:
d7350c3f 5858 kfree(iter);
7b85af63 5859 __trace_array_put(tr);
d7350c3f
FW
5860 mutex_unlock(&trace_types_lock);
5861 return ret;
b3806b43
SR
5862}
5863
5864static int tracing_release_pipe(struct inode *inode, struct file *file)
5865{
5866 struct trace_iterator *iter = file->private_data;
15544209 5867 struct trace_array *tr = inode->i_private;
b3806b43 5868
b04cc6b1
FW
5869 mutex_lock(&trace_types_lock);
5870
cf6ab6d9
SRRH
5871 tr->current_trace->ref--;
5872
29bf4a5e 5873 if (iter->trace->pipe_close)
c521efd1
SR
5874 iter->trace->pipe_close(iter);
5875
b04cc6b1
FW
5876 mutex_unlock(&trace_types_lock);
5877
4462344e 5878 free_cpumask_var(iter->started);
d7350c3f 5879 mutex_destroy(&iter->mutex);
b3806b43 5880 kfree(iter);
b3806b43 5881
7b85af63
SRRH
5882 trace_array_put(tr);
5883
b3806b43
SR
5884 return 0;
5885}
5886
9dd95748 5887static __poll_t
cc60cdc9 5888trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
2a2cc8f7 5889{
983f938a
SRRH
5890 struct trace_array *tr = iter->tr;
5891
15693458
SRRH
5892 /* Iterators are static, they should be filled or empty */
5893 if (trace_buffer_iter(iter, iter->cpu_file))
a9a08845 5894 return EPOLLIN | EPOLLRDNORM;
2a2cc8f7 5895
983f938a 5896 if (tr->trace_flags & TRACE_ITER_BLOCK)
2a2cc8f7
SSP
5897 /*
5898 * Always select as readable when in blocking mode
5899 */
a9a08845 5900 return EPOLLIN | EPOLLRDNORM;
15693458 5901 else
12883efb 5902 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
15693458 5903 filp, poll_table);
2a2cc8f7 5904}
2a2cc8f7 5905
9dd95748 5906static __poll_t
cc60cdc9
SR
5907tracing_poll_pipe(struct file *filp, poll_table *poll_table)
5908{
5909 struct trace_iterator *iter = filp->private_data;
5910
5911 return trace_poll(iter, filp, poll_table);
2a2cc8f7
SSP
5912}
5913
d716ff71 5914/* Must be called with iter->mutex held. */
ff98781b 5915static int tracing_wait_pipe(struct file *filp)
b3806b43
SR
5916{
5917 struct trace_iterator *iter = filp->private_data;
8b8b3683 5918 int ret;
b3806b43 5919
b3806b43 5920 while (trace_empty(iter)) {
2dc8f095 5921
107bad8b 5922 if ((filp->f_flags & O_NONBLOCK)) {
ff98781b 5923 return -EAGAIN;
107bad8b 5924 }
2dc8f095 5925
b3806b43 5926 /*
250bfd3d 5927 * We block until we read something and tracing is disabled.
b3806b43
SR
5928 * We still block if tracing is disabled, but we have never
5929 * read anything. This allows a user to cat this file, and
5930 * then enable tracing. But after we have read something,
5931 * we give an EOF when tracing is again disabled.
5932 *
5933 * iter->pos will be 0 if we haven't read anything.
5934 */
75df6e68 5935 if (!tracer_tracing_is_on(iter->tr) && iter->pos)
b3806b43 5936 break;
f4874261
SRRH
5937
5938 mutex_unlock(&iter->mutex);
5939
2c2b0a78 5940 ret = wait_on_pipe(iter, 0);
f4874261
SRRH
5941
5942 mutex_lock(&iter->mutex);
5943
8b8b3683
SRRH
5944 if (ret)
5945 return ret;
b3806b43
SR
5946 }
5947
ff98781b
EGM
5948 return 1;
5949}
5950
5951/*
5952 * Consumer reader.
5953 */
5954static ssize_t
5955tracing_read_pipe(struct file *filp, char __user *ubuf,
5956 size_t cnt, loff_t *ppos)
5957{
5958 struct trace_iterator *iter = filp->private_data;
5959 ssize_t sret;
5960
d7350c3f
FW
5961 /*
5962 * Avoid more than one consumer on a single file descriptor
5963 * This is just a matter of traces coherency, the ring buffer itself
5964 * is protected.
5965 */
5966 mutex_lock(&iter->mutex);
1245800c
SRRH
5967
5968 /* return any leftover data */
5969 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
5970 if (sret != -EBUSY)
5971 goto out;
5972
5973 trace_seq_init(&iter->seq);
5974
ff98781b
EGM
5975 if (iter->trace->read) {
5976 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
5977 if (sret)
5978 goto out;
5979 }
5980
5981waitagain:
5982 sret = tracing_wait_pipe(filp);
5983 if (sret <= 0)
5984 goto out;
5985
b3806b43 5986 /* stop when tracing is finished */
ff98781b
EGM
5987 if (trace_empty(iter)) {
5988 sret = 0;
107bad8b 5989 goto out;
ff98781b 5990 }
b3806b43
SR
5991
5992 if (cnt >= PAGE_SIZE)
5993 cnt = PAGE_SIZE - 1;
5994
53d0aa77 5995 /* reset all but tr, trace, and overruns */
53d0aa77
SR
5996 memset(&iter->seq, 0,
5997 sizeof(struct trace_iterator) -
5998 offsetof(struct trace_iterator, seq));
ed5467da 5999 cpumask_clear(iter->started);
4823ed7e 6000 iter->pos = -1;
b3806b43 6001
4f535968 6002 trace_event_read_lock();
7e53bd42 6003 trace_access_lock(iter->cpu_file);
955b61e5 6004 while (trace_find_next_entry_inc(iter) != NULL) {
2c4f035f 6005 enum print_line_t ret;
5ac48378 6006 int save_len = iter->seq.seq.len;
088b1e42 6007
f9896bf3 6008 ret = print_trace_line(iter);
2c4f035f 6009 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42 6010 /* don't print partial lines */
5ac48378 6011 iter->seq.seq.len = save_len;
b3806b43 6012 break;
088b1e42 6013 }
b91facc3
FW
6014 if (ret != TRACE_TYPE_NO_CONSUME)
6015 trace_consume(iter);
b3806b43 6016
5ac48378 6017 if (trace_seq_used(&iter->seq) >= cnt)
b3806b43 6018 break;
ee5e51f5
JO
6019
6020 /*
6021 * Setting the full flag means we reached the trace_seq buffer
6022 * size and we should leave by partial output condition above.
6023 * One of the trace_seq_* functions is not used properly.
6024 */
6025 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
6026 iter->ent->type);
b3806b43 6027 }
7e53bd42 6028 trace_access_unlock(iter->cpu_file);
4f535968 6029 trace_event_read_unlock();
b3806b43 6030
b3806b43 6031 /* Now copy what we have to the user */
6c6c2796 6032 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
5ac48378 6033 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
f9520750 6034 trace_seq_init(&iter->seq);
9ff4b974
PP
6035
6036 /*
25985edc 6037 * If there was nothing to send to user, in spite of consuming trace
9ff4b974
PP
6038 * entries, go back to wait for more entries.
6039 */
6c6c2796 6040 if (sret == -EBUSY)
9ff4b974 6041 goto waitagain;
b3806b43 6042
107bad8b 6043out:
d7350c3f 6044 mutex_unlock(&iter->mutex);
107bad8b 6045
6c6c2796 6046 return sret;
b3806b43
SR
6047}
6048
3c56819b
EGM
6049static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
6050 unsigned int idx)
6051{
6052 __free_page(spd->pages[idx]);
6053}
6054
28dfef8f 6055static const struct pipe_buf_operations tracing_pipe_buf_ops = {
34cd4998 6056 .confirm = generic_pipe_buf_confirm,
92fdd98c 6057 .release = generic_pipe_buf_release,
34cd4998
SR
6058 .steal = generic_pipe_buf_steal,
6059 .get = generic_pipe_buf_get,
3c56819b
EGM
6060};
6061
34cd4998 6062static size_t
fa7c7f6e 6063tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
34cd4998
SR
6064{
6065 size_t count;
74f06bb7 6066 int save_len;
34cd4998
SR
6067 int ret;
6068
6069 /* Seq buffer is page-sized, exactly what we need. */
6070 for (;;) {
74f06bb7 6071 save_len = iter->seq.seq.len;
34cd4998 6072 ret = print_trace_line(iter);
74f06bb7
SRRH
6073
6074 if (trace_seq_has_overflowed(&iter->seq)) {
6075 iter->seq.seq.len = save_len;
34cd4998
SR
6076 break;
6077 }
74f06bb7
SRRH
6078
6079 /*
6080 * This should not be hit, because it should only
6081 * be set if the iter->seq overflowed. But check it
6082 * anyway to be safe.
6083 */
34cd4998 6084 if (ret == TRACE_TYPE_PARTIAL_LINE) {
74f06bb7
SRRH
6085 iter->seq.seq.len = save_len;
6086 break;
6087 }
6088
5ac48378 6089 count = trace_seq_used(&iter->seq) - save_len;
74f06bb7
SRRH
6090 if (rem < count) {
6091 rem = 0;
6092 iter->seq.seq.len = save_len;
34cd4998
SR
6093 break;
6094 }
6095
74e7ff8c
LJ
6096 if (ret != TRACE_TYPE_NO_CONSUME)
6097 trace_consume(iter);
34cd4998 6098 rem -= count;
955b61e5 6099 if (!trace_find_next_entry_inc(iter)) {
34cd4998
SR
6100 rem = 0;
6101 iter->ent = NULL;
6102 break;
6103 }
6104 }
6105
6106 return rem;
6107}
6108
3c56819b
EGM
6109static ssize_t tracing_splice_read_pipe(struct file *filp,
6110 loff_t *ppos,
6111 struct pipe_inode_info *pipe,
6112 size_t len,
6113 unsigned int flags)
6114{
35f3d14d
JA
6115 struct page *pages_def[PIPE_DEF_BUFFERS];
6116 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3c56819b
EGM
6117 struct trace_iterator *iter = filp->private_data;
6118 struct splice_pipe_desc spd = {
35f3d14d
JA
6119 .pages = pages_def,
6120 .partial = partial_def,
34cd4998 6121 .nr_pages = 0, /* This gets updated below. */
047fe360 6122 .nr_pages_max = PIPE_DEF_BUFFERS,
34cd4998
SR
6123 .ops = &tracing_pipe_buf_ops,
6124 .spd_release = tracing_spd_release_pipe,
3c56819b
EGM
6125 };
6126 ssize_t ret;
34cd4998 6127 size_t rem;
3c56819b
EGM
6128 unsigned int i;
6129
35f3d14d
JA
6130 if (splice_grow_spd(pipe, &spd))
6131 return -ENOMEM;
6132
d7350c3f 6133 mutex_lock(&iter->mutex);
3c56819b
EGM
6134
6135 if (iter->trace->splice_read) {
6136 ret = iter->trace->splice_read(iter, filp,
6137 ppos, pipe, len, flags);
6138 if (ret)
34cd4998 6139 goto out_err;
3c56819b
EGM
6140 }
6141
6142 ret = tracing_wait_pipe(filp);
6143 if (ret <= 0)
34cd4998 6144 goto out_err;
3c56819b 6145
955b61e5 6146 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3c56819b 6147 ret = -EFAULT;
34cd4998 6148 goto out_err;
3c56819b
EGM
6149 }
6150
4f535968 6151 trace_event_read_lock();
7e53bd42 6152 trace_access_lock(iter->cpu_file);
4f535968 6153
3c56819b 6154 /* Fill as many pages as possible. */
a786c06d 6155 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
35f3d14d
JA
6156 spd.pages[i] = alloc_page(GFP_KERNEL);
6157 if (!spd.pages[i])
34cd4998 6158 break;
3c56819b 6159
fa7c7f6e 6160 rem = tracing_fill_pipe_page(rem, iter);
3c56819b
EGM
6161
6162 /* Copy the data into the page, so we can start over. */
6163 ret = trace_seq_to_buffer(&iter->seq,
35f3d14d 6164 page_address(spd.pages[i]),
5ac48378 6165 trace_seq_used(&iter->seq));
3c56819b 6166 if (ret < 0) {
35f3d14d 6167 __free_page(spd.pages[i]);
3c56819b
EGM
6168 break;
6169 }
35f3d14d 6170 spd.partial[i].offset = 0;
5ac48378 6171 spd.partial[i].len = trace_seq_used(&iter->seq);
3c56819b 6172
f9520750 6173 trace_seq_init(&iter->seq);
3c56819b
EGM
6174 }
6175
7e53bd42 6176 trace_access_unlock(iter->cpu_file);
4f535968 6177 trace_event_read_unlock();
d7350c3f 6178 mutex_unlock(&iter->mutex);
3c56819b
EGM
6179
6180 spd.nr_pages = i;
6181
a29054d9
SRRH
6182 if (i)
6183 ret = splice_to_pipe(pipe, &spd);
6184 else
6185 ret = 0;
35f3d14d 6186out:
047fe360 6187 splice_shrink_spd(&spd);
35f3d14d 6188 return ret;
3c56819b 6189
34cd4998 6190out_err:
d7350c3f 6191 mutex_unlock(&iter->mutex);
35f3d14d 6192 goto out;
3c56819b
EGM
6193}
6194
a98a3c3f
SR
6195static ssize_t
6196tracing_entries_read(struct file *filp, char __user *ubuf,
6197 size_t cnt, loff_t *ppos)
6198{
0bc392ee
ON
6199 struct inode *inode = file_inode(filp);
6200 struct trace_array *tr = inode->i_private;
6201 int cpu = tracing_get_cpu(inode);
438ced17
VN
6202 char buf[64];
6203 int r = 0;
6204 ssize_t ret;
a98a3c3f 6205
db526ca3 6206 mutex_lock(&trace_types_lock);
438ced17 6207
0bc392ee 6208 if (cpu == RING_BUFFER_ALL_CPUS) {
438ced17
VN
6209 int cpu, buf_size_same;
6210 unsigned long size;
6211
6212 size = 0;
6213 buf_size_same = 1;
6214 /* check if all cpu sizes are same */
6215 for_each_tracing_cpu(cpu) {
6216 /* fill in the size from first enabled cpu */
6217 if (size == 0)
12883efb
SRRH
6218 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
6219 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
438ced17
VN
6220 buf_size_same = 0;
6221 break;
6222 }
6223 }
6224
6225 if (buf_size_same) {
6226 if (!ring_buffer_expanded)
6227 r = sprintf(buf, "%lu (expanded: %lu)\n",
6228 size >> 10,
6229 trace_buf_size >> 10);
6230 else
6231 r = sprintf(buf, "%lu\n", size >> 10);
6232 } else
6233 r = sprintf(buf, "X\n");
6234 } else
0bc392ee 6235 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
438ced17 6236
db526ca3
SR
6237 mutex_unlock(&trace_types_lock);
6238
438ced17
VN
6239 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6240 return ret;
a98a3c3f
SR
6241}
6242
6243static ssize_t
6244tracing_entries_write(struct file *filp, const char __user *ubuf,
6245 size_t cnt, loff_t *ppos)
6246{
0bc392ee
ON
6247 struct inode *inode = file_inode(filp);
6248 struct trace_array *tr = inode->i_private;
a98a3c3f 6249 unsigned long val;
4f271a2a 6250 int ret;
a98a3c3f 6251
22fe9b54
PH
6252 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6253 if (ret)
c6caeeb1 6254 return ret;
a98a3c3f
SR
6255
6256 /* must have at least 1 entry */
6257 if (!val)
6258 return -EINVAL;
6259
1696b2b0
SR
6260 /* value is in KB */
6261 val <<= 10;
0bc392ee 6262 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
4f271a2a
VN
6263 if (ret < 0)
6264 return ret;
a98a3c3f 6265
cf8517cf 6266 *ppos += cnt;
a98a3c3f 6267
4f271a2a
VN
6268 return cnt;
6269}
bf5e6519 6270
f81ab074
VN
6271static ssize_t
6272tracing_total_entries_read(struct file *filp, char __user *ubuf,
6273 size_t cnt, loff_t *ppos)
6274{
6275 struct trace_array *tr = filp->private_data;
6276 char buf[64];
6277 int r, cpu;
6278 unsigned long size = 0, expanded_size = 0;
6279
6280 mutex_lock(&trace_types_lock);
6281 for_each_tracing_cpu(cpu) {
12883efb 6282 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
f81ab074
VN
6283 if (!ring_buffer_expanded)
6284 expanded_size += trace_buf_size >> 10;
6285 }
6286 if (ring_buffer_expanded)
6287 r = sprintf(buf, "%lu\n", size);
6288 else
6289 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
6290 mutex_unlock(&trace_types_lock);
6291
6292 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6293}
6294
4f271a2a
VN
6295static ssize_t
6296tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
6297 size_t cnt, loff_t *ppos)
6298{
6299 /*
6300 * There is no need to read what the user has written, this function
6301 * is just to make sure that there is no error when "echo" is used
6302 */
6303
6304 *ppos += cnt;
a98a3c3f
SR
6305
6306 return cnt;
6307}
6308
4f271a2a
VN
6309static int
6310tracing_free_buffer_release(struct inode *inode, struct file *filp)
6311{
2b6080f2
SR
6312 struct trace_array *tr = inode->i_private;
6313
cf30cf67 6314 /* disable tracing ? */
983f938a 6315 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
711e1243 6316 tracer_tracing_off(tr);
4f271a2a 6317 /* resize the ring buffer to 0 */
2b6080f2 6318 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4f271a2a 6319
7b85af63
SRRH
6320 trace_array_put(tr);
6321
4f271a2a
VN
6322 return 0;
6323}
6324
5bf9a1ee
PP
6325static ssize_t
6326tracing_mark_write(struct file *filp, const char __user *ubuf,
6327 size_t cnt, loff_t *fpos)
6328{
2d71619c 6329 struct trace_array *tr = filp->private_data;
d696b58c 6330 struct ring_buffer_event *event;
3dd80953 6331 enum event_trigger_type tt = ETT_NONE;
d696b58c
SR
6332 struct ring_buffer *buffer;
6333 struct print_entry *entry;
6334 unsigned long irq_flags;
d696b58c 6335 ssize_t written;
d696b58c
SR
6336 int size;
6337 int len;
fa32e855 6338
656c7f0d 6339/* Used in tracing_mark_raw_write() as well */
0f5e5a3a
RV
6340#define FAULTED_STR "<faulted>"
6341#define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
5bf9a1ee 6342
c76f0694 6343 if (tracing_disabled)
5bf9a1ee
PP
6344 return -EINVAL;
6345
983f938a 6346 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
5224c3a3
MSB
6347 return -EINVAL;
6348
5bf9a1ee
PP
6349 if (cnt > TRACE_BUF_SIZE)
6350 cnt = TRACE_BUF_SIZE;
6351
d696b58c 6352 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
5bf9a1ee 6353
d696b58c 6354 local_save_flags(irq_flags);
656c7f0d 6355 size = sizeof(*entry) + cnt + 2; /* add '\0' and possible '\n' */
d696b58c 6356
656c7f0d
SRRH
6357 /* If less than "<faulted>", then make sure we can still add that */
6358 if (cnt < FAULTED_SIZE)
6359 size += FAULTED_SIZE - cnt;
d696b58c 6360
2d71619c 6361 buffer = tr->trace_buffer.buffer;
3e9a8aad
SRRH
6362 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
6363 irq_flags, preempt_count());
656c7f0d 6364 if (unlikely(!event))
d696b58c 6365 /* Ring buffer disabled, return as if not open for write */
656c7f0d 6366 return -EBADF;
d696b58c
SR
6367
6368 entry = ring_buffer_event_data(event);
6369 entry->ip = _THIS_IP_;
6370
656c7f0d
SRRH
6371 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
6372 if (len) {
0f5e5a3a 6373 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
656c7f0d
SRRH
6374 cnt = FAULTED_SIZE;
6375 written = -EFAULT;
c13d2f7c 6376 } else
656c7f0d
SRRH
6377 written = cnt;
6378 len = cnt;
5bf9a1ee 6379
3dd80953
SRV
6380 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
6381 /* do not add \n before testing triggers, but add \0 */
6382 entry->buf[cnt] = '\0';
6383 tt = event_triggers_call(tr->trace_marker_file, entry, event);
6384 }
6385
d696b58c
SR
6386 if (entry->buf[cnt - 1] != '\n') {
6387 entry->buf[cnt] = '\n';
6388 entry->buf[cnt + 1] = '\0';
6389 } else
6390 entry->buf[cnt] = '\0';
6391
7ffbd48d 6392 __buffer_unlock_commit(buffer, event);
5bf9a1ee 6393
3dd80953
SRV
6394 if (tt)
6395 event_triggers_post_call(tr->trace_marker_file, tt);
6396
656c7f0d
SRRH
6397 if (written > 0)
6398 *fpos += written;
5bf9a1ee 6399
fa32e855
SR
6400 return written;
6401}
6402
6403/* Limit it for now to 3K (including tag) */
6404#define RAW_DATA_MAX_SIZE (1024*3)
6405
6406static ssize_t
6407tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
6408 size_t cnt, loff_t *fpos)
6409{
6410 struct trace_array *tr = filp->private_data;
6411 struct ring_buffer_event *event;
6412 struct ring_buffer *buffer;
6413 struct raw_data_entry *entry;
6414 unsigned long irq_flags;
fa32e855 6415 ssize_t written;
fa32e855
SR
6416 int size;
6417 int len;
6418
656c7f0d
SRRH
6419#define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
6420
fa32e855
SR
6421 if (tracing_disabled)
6422 return -EINVAL;
6423
6424 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
6425 return -EINVAL;
6426
6427 /* The marker must at least have a tag id */
6428 if (cnt < sizeof(unsigned int) || cnt > RAW_DATA_MAX_SIZE)
6429 return -EINVAL;
6430
6431 if (cnt > TRACE_BUF_SIZE)
6432 cnt = TRACE_BUF_SIZE;
6433
6434 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
6435
fa32e855
SR
6436 local_save_flags(irq_flags);
6437 size = sizeof(*entry) + cnt;
656c7f0d
SRRH
6438 if (cnt < FAULT_SIZE_ID)
6439 size += FAULT_SIZE_ID - cnt;
6440
fa32e855 6441 buffer = tr->trace_buffer.buffer;
3e9a8aad
SRRH
6442 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
6443 irq_flags, preempt_count());
656c7f0d 6444 if (!event)
fa32e855 6445 /* Ring buffer disabled, return as if not open for write */
656c7f0d 6446 return -EBADF;
fa32e855
SR
6447
6448 entry = ring_buffer_event_data(event);
6449
656c7f0d
SRRH
6450 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
6451 if (len) {
6452 entry->id = -1;
0f5e5a3a 6453 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
656c7f0d 6454 written = -EFAULT;
fa32e855 6455 } else
656c7f0d 6456 written = cnt;
fa32e855
SR
6457
6458 __buffer_unlock_commit(buffer, event);
6459
656c7f0d
SRRH
6460 if (written > 0)
6461 *fpos += written;
1aa54bca
MS
6462
6463 return written;
5bf9a1ee
PP
6464}
6465
13f16d20 6466static int tracing_clock_show(struct seq_file *m, void *v)
5079f326 6467{
2b6080f2 6468 struct trace_array *tr = m->private;
5079f326
Z
6469 int i;
6470
6471 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
13f16d20 6472 seq_printf(m,
5079f326 6473 "%s%s%s%s", i ? " " : "",
2b6080f2
SR
6474 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
6475 i == tr->clock_id ? "]" : "");
13f16d20 6476 seq_putc(m, '\n');
5079f326 6477
13f16d20 6478 return 0;
5079f326
Z
6479}
6480
d71bd34d 6481int tracing_set_clock(struct trace_array *tr, const char *clockstr)
5079f326 6482{
5079f326
Z
6483 int i;
6484
5079f326
Z
6485 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
6486 if (strcmp(trace_clocks[i].name, clockstr) == 0)
6487 break;
6488 }
6489 if (i == ARRAY_SIZE(trace_clocks))
6490 return -EINVAL;
6491
5079f326
Z
6492 mutex_lock(&trace_types_lock);
6493
2b6080f2
SR
6494 tr->clock_id = i;
6495
12883efb 6496 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
5079f326 6497
60303ed3
DS
6498 /*
6499 * New clock may not be consistent with the previous clock.
6500 * Reset the buffer so that it doesn't have incomparable timestamps.
6501 */
9457158b 6502 tracing_reset_online_cpus(&tr->trace_buffer);
12883efb
SRRH
6503
6504#ifdef CONFIG_TRACER_MAX_TRACE
170b3b10 6505 if (tr->max_buffer.buffer)
12883efb 6506 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
9457158b 6507 tracing_reset_online_cpus(&tr->max_buffer);
12883efb 6508#endif
60303ed3 6509
5079f326
Z
6510 mutex_unlock(&trace_types_lock);
6511
e1e232ca
SR
6512 return 0;
6513}
6514
6515static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
6516 size_t cnt, loff_t *fpos)
6517{
6518 struct seq_file *m = filp->private_data;
6519 struct trace_array *tr = m->private;
6520 char buf[64];
6521 const char *clockstr;
6522 int ret;
6523
6524 if (cnt >= sizeof(buf))
6525 return -EINVAL;
6526
4afe6495 6527 if (copy_from_user(buf, ubuf, cnt))
e1e232ca
SR
6528 return -EFAULT;
6529
6530 buf[cnt] = 0;
6531
6532 clockstr = strstrip(buf);
6533
6534 ret = tracing_set_clock(tr, clockstr);
6535 if (ret)
6536 return ret;
6537
5079f326
Z
6538 *fpos += cnt;
6539
6540 return cnt;
6541}
6542
13f16d20
LZ
6543static int tracing_clock_open(struct inode *inode, struct file *file)
6544{
7b85af63
SRRH
6545 struct trace_array *tr = inode->i_private;
6546 int ret;
6547
13f16d20
LZ
6548 if (tracing_disabled)
6549 return -ENODEV;
2b6080f2 6550
7b85af63
SRRH
6551 if (trace_array_get(tr))
6552 return -ENODEV;
6553
6554 ret = single_open(file, tracing_clock_show, inode->i_private);
6555 if (ret < 0)
6556 trace_array_put(tr);
6557
6558 return ret;
13f16d20
LZ
6559}
6560
2c1ea60b
TZ
6561static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
6562{
6563 struct trace_array *tr = m->private;
6564
6565 mutex_lock(&trace_types_lock);
6566
6567 if (ring_buffer_time_stamp_abs(tr->trace_buffer.buffer))
6568 seq_puts(m, "delta [absolute]\n");
6569 else
6570 seq_puts(m, "[delta] absolute\n");
6571
6572 mutex_unlock(&trace_types_lock);
6573
6574 return 0;
6575}
6576
6577static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
6578{
6579 struct trace_array *tr = inode->i_private;
6580 int ret;
6581
6582 if (tracing_disabled)
6583 return -ENODEV;
6584
6585 if (trace_array_get(tr))
6586 return -ENODEV;
6587
6588 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
6589 if (ret < 0)
6590 trace_array_put(tr);
6591
6592 return ret;
6593}
6594
00b41452
TZ
6595int tracing_set_time_stamp_abs(struct trace_array *tr, bool abs)
6596{
6597 int ret = 0;
6598
6599 mutex_lock(&trace_types_lock);
6600
6601 if (abs && tr->time_stamp_abs_ref++)
6602 goto out;
6603
6604 if (!abs) {
6605 if (WARN_ON_ONCE(!tr->time_stamp_abs_ref)) {
6606 ret = -EINVAL;
6607 goto out;
6608 }
6609
6610 if (--tr->time_stamp_abs_ref)
6611 goto out;
6612 }
6613
6614 ring_buffer_set_time_stamp_abs(tr->trace_buffer.buffer, abs);
6615
6616#ifdef CONFIG_TRACER_MAX_TRACE
6617 if (tr->max_buffer.buffer)
6618 ring_buffer_set_time_stamp_abs(tr->max_buffer.buffer, abs);
6619#endif
6620 out:
6621 mutex_unlock(&trace_types_lock);
6622
6623 return ret;
6624}
6625
6de58e62
SRRH
6626struct ftrace_buffer_info {
6627 struct trace_iterator iter;
6628 void *spare;
73a757e6 6629 unsigned int spare_cpu;
6de58e62
SRRH
6630 unsigned int read;
6631};
6632
debdd57f
HT
6633#ifdef CONFIG_TRACER_SNAPSHOT
6634static int tracing_snapshot_open(struct inode *inode, struct file *file)
6635{
6484c71c 6636 struct trace_array *tr = inode->i_private;
debdd57f 6637 struct trace_iterator *iter;
2b6080f2 6638 struct seq_file *m;
debdd57f
HT
6639 int ret = 0;
6640
ff451961
SRRH
6641 if (trace_array_get(tr) < 0)
6642 return -ENODEV;
6643
debdd57f 6644 if (file->f_mode & FMODE_READ) {
6484c71c 6645 iter = __tracing_open(inode, file, true);
debdd57f
HT
6646 if (IS_ERR(iter))
6647 ret = PTR_ERR(iter);
2b6080f2
SR
6648 } else {
6649 /* Writes still need the seq_file to hold the private data */
f77d09a3 6650 ret = -ENOMEM;
2b6080f2
SR
6651 m = kzalloc(sizeof(*m), GFP_KERNEL);
6652 if (!m)
f77d09a3 6653 goto out;
2b6080f2
SR
6654 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
6655 if (!iter) {
6656 kfree(m);
f77d09a3 6657 goto out;
2b6080f2 6658 }
f77d09a3
AL
6659 ret = 0;
6660
ff451961 6661 iter->tr = tr;
6484c71c
ON
6662 iter->trace_buffer = &tr->max_buffer;
6663 iter->cpu_file = tracing_get_cpu(inode);
2b6080f2
SR
6664 m->private = iter;
6665 file->private_data = m;
debdd57f 6666 }
f77d09a3 6667out:
ff451961
SRRH
6668 if (ret < 0)
6669 trace_array_put(tr);
6670
debdd57f
HT
6671 return ret;
6672}
6673
6674static ssize_t
6675tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
6676 loff_t *ppos)
6677{
2b6080f2
SR
6678 struct seq_file *m = filp->private_data;
6679 struct trace_iterator *iter = m->private;
6680 struct trace_array *tr = iter->tr;
debdd57f
HT
6681 unsigned long val;
6682 int ret;
6683
6684 ret = tracing_update_buffers();
6685 if (ret < 0)
6686 return ret;
6687
6688 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6689 if (ret)
6690 return ret;
6691
6692 mutex_lock(&trace_types_lock);
6693
2b6080f2 6694 if (tr->current_trace->use_max_tr) {
debdd57f
HT
6695 ret = -EBUSY;
6696 goto out;
6697 }
6698
a35873a0
TZ
6699 arch_spin_lock(&tr->max_lock);
6700 if (tr->cond_snapshot)
6701 ret = -EBUSY;
6702 arch_spin_unlock(&tr->max_lock);
6703 if (ret)
6704 goto out;
6705
debdd57f
HT
6706 switch (val) {
6707 case 0:
f1affcaa
SRRH
6708 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6709 ret = -EINVAL;
6710 break;
debdd57f 6711 }
3209cff4
SRRH
6712 if (tr->allocated_snapshot)
6713 free_snapshot(tr);
debdd57f
HT
6714 break;
6715 case 1:
f1affcaa
SRRH
6716/* Only allow per-cpu swap if the ring buffer supports it */
6717#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
6718 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6719 ret = -EINVAL;
6720 break;
6721 }
6722#endif
45ad21ca 6723 if (!tr->allocated_snapshot) {
2824f503 6724 ret = tracing_alloc_snapshot_instance(tr);
debdd57f
HT
6725 if (ret < 0)
6726 break;
debdd57f 6727 }
debdd57f
HT
6728 local_irq_disable();
6729 /* Now, we're going to swap */
f1affcaa 6730 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
a35873a0 6731 update_max_tr(tr, current, smp_processor_id(), NULL);
f1affcaa 6732 else
ce9bae55 6733 update_max_tr_single(tr, current, iter->cpu_file);
debdd57f
HT
6734 local_irq_enable();
6735 break;
6736 default:
45ad21ca 6737 if (tr->allocated_snapshot) {
f1affcaa
SRRH
6738 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
6739 tracing_reset_online_cpus(&tr->max_buffer);
6740 else
6741 tracing_reset(&tr->max_buffer, iter->cpu_file);
6742 }
debdd57f
HT
6743 break;
6744 }
6745
6746 if (ret >= 0) {
6747 *ppos += cnt;
6748 ret = cnt;
6749 }
6750out:
6751 mutex_unlock(&trace_types_lock);
6752 return ret;
6753}
2b6080f2
SR
6754
6755static int tracing_snapshot_release(struct inode *inode, struct file *file)
6756{
6757 struct seq_file *m = file->private_data;
ff451961
SRRH
6758 int ret;
6759
6760 ret = tracing_release(inode, file);
2b6080f2
SR
6761
6762 if (file->f_mode & FMODE_READ)
ff451961 6763 return ret;
2b6080f2
SR
6764
6765 /* If write only, the seq_file is just a stub */
6766 if (m)
6767 kfree(m->private);
6768 kfree(m);
6769
6770 return 0;
6771}
6772
6de58e62
SRRH
6773static int tracing_buffers_open(struct inode *inode, struct file *filp);
6774static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
6775 size_t count, loff_t *ppos);
6776static int tracing_buffers_release(struct inode *inode, struct file *file);
6777static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
6778 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
6779
6780static int snapshot_raw_open(struct inode *inode, struct file *filp)
6781{
6782 struct ftrace_buffer_info *info;
6783 int ret;
6784
6785 ret = tracing_buffers_open(inode, filp);
6786 if (ret < 0)
6787 return ret;
6788
6789 info = filp->private_data;
6790
6791 if (info->iter.trace->use_max_tr) {
6792 tracing_buffers_release(inode, filp);
6793 return -EBUSY;
6794 }
6795
6796 info->iter.snapshot = true;
6797 info->iter.trace_buffer = &info->iter.tr->max_buffer;
6798
6799 return ret;
6800}
6801
debdd57f
HT
6802#endif /* CONFIG_TRACER_SNAPSHOT */
6803
6804
6508fa76
SF
6805static const struct file_operations tracing_thresh_fops = {
6806 .open = tracing_open_generic,
6807 .read = tracing_thresh_read,
6808 .write = tracing_thresh_write,
6809 .llseek = generic_file_llseek,
6810};
6811
f971cc9a 6812#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
5e2336a0 6813static const struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
6814 .open = tracing_open_generic,
6815 .read = tracing_max_lat_read,
6816 .write = tracing_max_lat_write,
b444786f 6817 .llseek = generic_file_llseek,
bc0c38d1 6818};
e428abbb 6819#endif
bc0c38d1 6820
5e2336a0 6821static const struct file_operations set_tracer_fops = {
4bf39a94
IM
6822 .open = tracing_open_generic,
6823 .read = tracing_set_trace_read,
6824 .write = tracing_set_trace_write,
b444786f 6825 .llseek = generic_file_llseek,
bc0c38d1
SR
6826};
6827
5e2336a0 6828static const struct file_operations tracing_pipe_fops = {
4bf39a94 6829 .open = tracing_open_pipe,
2a2cc8f7 6830 .poll = tracing_poll_pipe,
4bf39a94 6831 .read = tracing_read_pipe,
3c56819b 6832 .splice_read = tracing_splice_read_pipe,
4bf39a94 6833 .release = tracing_release_pipe,
b444786f 6834 .llseek = no_llseek,
b3806b43
SR
6835};
6836
5e2336a0 6837static const struct file_operations tracing_entries_fops = {
0bc392ee 6838 .open = tracing_open_generic_tr,
a98a3c3f
SR
6839 .read = tracing_entries_read,
6840 .write = tracing_entries_write,
b444786f 6841 .llseek = generic_file_llseek,
0bc392ee 6842 .release = tracing_release_generic_tr,
a98a3c3f
SR
6843};
6844
f81ab074 6845static const struct file_operations tracing_total_entries_fops = {
7b85af63 6846 .open = tracing_open_generic_tr,
f81ab074
VN
6847 .read = tracing_total_entries_read,
6848 .llseek = generic_file_llseek,
7b85af63 6849 .release = tracing_release_generic_tr,
f81ab074
VN
6850};
6851
4f271a2a 6852static const struct file_operations tracing_free_buffer_fops = {
7b85af63 6853 .open = tracing_open_generic_tr,
4f271a2a
VN
6854 .write = tracing_free_buffer_write,
6855 .release = tracing_free_buffer_release,
6856};
6857
5e2336a0 6858static const struct file_operations tracing_mark_fops = {
7b85af63 6859 .open = tracing_open_generic_tr,
5bf9a1ee 6860 .write = tracing_mark_write,
b444786f 6861 .llseek = generic_file_llseek,
7b85af63 6862 .release = tracing_release_generic_tr,
5bf9a1ee
PP
6863};
6864
fa32e855
SR
6865static const struct file_operations tracing_mark_raw_fops = {
6866 .open = tracing_open_generic_tr,
6867 .write = tracing_mark_raw_write,
6868 .llseek = generic_file_llseek,
6869 .release = tracing_release_generic_tr,
6870};
6871
5079f326 6872static const struct file_operations trace_clock_fops = {
13f16d20
LZ
6873 .open = tracing_clock_open,
6874 .read = seq_read,
6875 .llseek = seq_lseek,
7b85af63 6876 .release = tracing_single_release_tr,
5079f326
Z
6877 .write = tracing_clock_write,
6878};
6879
2c1ea60b
TZ
6880static const struct file_operations trace_time_stamp_mode_fops = {
6881 .open = tracing_time_stamp_mode_open,
6882 .read = seq_read,
6883 .llseek = seq_lseek,
6884 .release = tracing_single_release_tr,
6885};
6886
debdd57f
HT
6887#ifdef CONFIG_TRACER_SNAPSHOT
6888static const struct file_operations snapshot_fops = {
6889 .open = tracing_snapshot_open,
6890 .read = seq_read,
6891 .write = tracing_snapshot_write,
098c879e 6892 .llseek = tracing_lseek,
2b6080f2 6893 .release = tracing_snapshot_release,
debdd57f 6894};
debdd57f 6895
6de58e62
SRRH
6896static const struct file_operations snapshot_raw_fops = {
6897 .open = snapshot_raw_open,
6898 .read = tracing_buffers_read,
6899 .release = tracing_buffers_release,
6900 .splice_read = tracing_buffers_splice_read,
6901 .llseek = no_llseek,
2cadf913
SR
6902};
6903
6de58e62
SRRH
6904#endif /* CONFIG_TRACER_SNAPSHOT */
6905
8a062902
TZ
6906#define TRACING_LOG_ERRS_MAX 8
6907#define TRACING_LOG_LOC_MAX 128
6908
6909#define CMD_PREFIX " Command: "
6910
6911struct err_info {
6912 const char **errs; /* ptr to loc-specific array of err strings */
6913 u8 type; /* index into errs -> specific err string */
6914 u8 pos; /* MAX_FILTER_STR_VAL = 256 */
6915 u64 ts;
6916};
6917
6918struct tracing_log_err {
6919 struct list_head list;
6920 struct err_info info;
6921 char loc[TRACING_LOG_LOC_MAX]; /* err location */
6922 char cmd[MAX_FILTER_STR_VAL]; /* what caused err */
6923};
6924
8a062902
TZ
6925static DEFINE_MUTEX(tracing_err_log_lock);
6926
2f754e77 6927struct tracing_log_err *get_tracing_log_err(struct trace_array *tr)
8a062902
TZ
6928{
6929 struct tracing_log_err *err;
6930
2f754e77 6931 if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
8a062902
TZ
6932 err = kzalloc(sizeof(*err), GFP_KERNEL);
6933 if (!err)
6934 err = ERR_PTR(-ENOMEM);
2f754e77 6935 tr->n_err_log_entries++;
8a062902
TZ
6936
6937 return err;
6938 }
6939
2f754e77 6940 err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
8a062902
TZ
6941 list_del(&err->list);
6942
6943 return err;
6944}
6945
6946/**
6947 * err_pos - find the position of a string within a command for error careting
6948 * @cmd: The tracing command that caused the error
6949 * @str: The string to position the caret at within @cmd
6950 *
6951 * Finds the position of the first occurence of @str within @cmd. The
6952 * return value can be passed to tracing_log_err() for caret placement
6953 * within @cmd.
6954 *
6955 * Returns the index within @cmd of the first occurence of @str or 0
6956 * if @str was not found.
6957 */
6958unsigned int err_pos(char *cmd, const char *str)
6959{
6960 char *found;
6961
6962 if (WARN_ON(!strlen(cmd)))
6963 return 0;
6964
6965 found = strstr(cmd, str);
6966 if (found)
6967 return found - cmd;
6968
6969 return 0;
6970}
6971
6972/**
6973 * tracing_log_err - write an error to the tracing error log
2f754e77 6974 * @tr: The associated trace array for the error (NULL for top level array)
8a062902
TZ
6975 * @loc: A string describing where the error occurred
6976 * @cmd: The tracing command that caused the error
6977 * @errs: The array of loc-specific static error strings
6978 * @type: The index into errs[], which produces the specific static err string
6979 * @pos: The position the caret should be placed in the cmd
6980 *
6981 * Writes an error into tracing/error_log of the form:
6982 *
6983 * <loc>: error: <text>
6984 * Command: <cmd>
6985 * ^
6986 *
6987 * tracing/error_log is a small log file containing the last
6988 * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated
6989 * unless there has been a tracing error, and the error log can be
6990 * cleared and have its memory freed by writing the empty string in
6991 * truncation mode to it i.e. echo > tracing/error_log.
6992 *
6993 * NOTE: the @errs array along with the @type param are used to
6994 * produce a static error string - this string is not copied and saved
6995 * when the error is logged - only a pointer to it is saved. See
6996 * existing callers for examples of how static strings are typically
6997 * defined for use with tracing_log_err().
6998 */
2f754e77
SRV
6999void tracing_log_err(struct trace_array *tr,
7000 const char *loc, const char *cmd,
8a062902
TZ
7001 const char **errs, u8 type, u8 pos)
7002{
7003 struct tracing_log_err *err;
7004
2f754e77
SRV
7005 if (!tr)
7006 tr = &global_trace;
7007
8a062902 7008 mutex_lock(&tracing_err_log_lock);
2f754e77 7009 err = get_tracing_log_err(tr);
8a062902
TZ
7010 if (PTR_ERR(err) == -ENOMEM) {
7011 mutex_unlock(&tracing_err_log_lock);
7012 return;
7013 }
7014
7015 snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
7016 snprintf(err->cmd, MAX_FILTER_STR_VAL,"\n" CMD_PREFIX "%s\n", cmd);
7017
7018 err->info.errs = errs;
7019 err->info.type = type;
7020 err->info.pos = pos;
7021 err->info.ts = local_clock();
7022
2f754e77 7023 list_add_tail(&err->list, &tr->err_log);
8a062902
TZ
7024 mutex_unlock(&tracing_err_log_lock);
7025}
7026
2f754e77 7027static void clear_tracing_err_log(struct trace_array *tr)
8a062902
TZ
7028{
7029 struct tracing_log_err *err, *next;
7030
7031 mutex_lock(&tracing_err_log_lock);
2f754e77 7032 list_for_each_entry_safe(err, next, &tr->err_log, list) {
8a062902
TZ
7033 list_del(&err->list);
7034 kfree(err);
7035 }
7036
2f754e77 7037 tr->n_err_log_entries = 0;
8a062902
TZ
7038 mutex_unlock(&tracing_err_log_lock);
7039}
7040
7041static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
7042{
2f754e77
SRV
7043 struct trace_array *tr = m->private;
7044
8a062902
TZ
7045 mutex_lock(&tracing_err_log_lock);
7046
2f754e77 7047 return seq_list_start(&tr->err_log, *pos);
8a062902
TZ
7048}
7049
7050static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
7051{
2f754e77
SRV
7052 struct trace_array *tr = m->private;
7053
7054 return seq_list_next(v, &tr->err_log, pos);
8a062902
TZ
7055}
7056
7057static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
7058{
7059 mutex_unlock(&tracing_err_log_lock);
7060}
7061
7062static void tracing_err_log_show_pos(struct seq_file *m, u8 pos)
7063{
7064 u8 i;
7065
7066 for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
7067 seq_putc(m, ' ');
7068 for (i = 0; i < pos; i++)
7069 seq_putc(m, ' ');
7070 seq_puts(m, "^\n");
7071}
7072
7073static int tracing_err_log_seq_show(struct seq_file *m, void *v)
7074{
7075 struct tracing_log_err *err = v;
7076
7077 if (err) {
7078 const char *err_text = err->info.errs[err->info.type];
7079 u64 sec = err->info.ts;
7080 u32 nsec;
7081
7082 nsec = do_div(sec, NSEC_PER_SEC);
7083 seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000,
7084 err->loc, err_text);
7085 seq_printf(m, "%s", err->cmd);
7086 tracing_err_log_show_pos(m, err->info.pos);
7087 }
7088
7089 return 0;
7090}
7091
7092static const struct seq_operations tracing_err_log_seq_ops = {
7093 .start = tracing_err_log_seq_start,
7094 .next = tracing_err_log_seq_next,
7095 .stop = tracing_err_log_seq_stop,
7096 .show = tracing_err_log_seq_show
7097};
7098
7099static int tracing_err_log_open(struct inode *inode, struct file *file)
7100{
2f754e77 7101 struct trace_array *tr = inode->i_private;
8a062902
TZ
7102 int ret = 0;
7103
2f754e77
SRV
7104 if (trace_array_get(tr) < 0)
7105 return -ENODEV;
7106
8a062902
TZ
7107 /* If this file was opened for write, then erase contents */
7108 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
2f754e77 7109 clear_tracing_err_log(tr);
8a062902 7110
2f754e77 7111 if (file->f_mode & FMODE_READ) {
8a062902 7112 ret = seq_open(file, &tracing_err_log_seq_ops);
2f754e77
SRV
7113 if (!ret) {
7114 struct seq_file *m = file->private_data;
7115 m->private = tr;
7116 } else {
7117 trace_array_put(tr);
7118 }
7119 }
8a062902
TZ
7120 return ret;
7121}
7122
7123static ssize_t tracing_err_log_write(struct file *file,
7124 const char __user *buffer,
7125 size_t count, loff_t *ppos)
7126{
7127 return count;
7128}
7129
7130static const struct file_operations tracing_err_log_fops = {
7131 .open = tracing_err_log_open,
7132 .write = tracing_err_log_write,
7133 .read = seq_read,
7134 .llseek = seq_lseek,
2f754e77 7135 .release = tracing_release_generic_tr,
8a062902
TZ
7136};
7137
2cadf913
SR
7138static int tracing_buffers_open(struct inode *inode, struct file *filp)
7139{
46ef2be0 7140 struct trace_array *tr = inode->i_private;
2cadf913 7141 struct ftrace_buffer_info *info;
7b85af63 7142 int ret;
2cadf913
SR
7143
7144 if (tracing_disabled)
7145 return -ENODEV;
7146
7b85af63
SRRH
7147 if (trace_array_get(tr) < 0)
7148 return -ENODEV;
7149
2cadf913 7150 info = kzalloc(sizeof(*info), GFP_KERNEL);
7b85af63
SRRH
7151 if (!info) {
7152 trace_array_put(tr);
2cadf913 7153 return -ENOMEM;
7b85af63 7154 }
2cadf913 7155
a695cb58
SRRH
7156 mutex_lock(&trace_types_lock);
7157
cc60cdc9 7158 info->iter.tr = tr;
46ef2be0 7159 info->iter.cpu_file = tracing_get_cpu(inode);
b627344f 7160 info->iter.trace = tr->current_trace;
12883efb 7161 info->iter.trace_buffer = &tr->trace_buffer;
cc60cdc9 7162 info->spare = NULL;
2cadf913 7163 /* Force reading ring buffer for first read */
cc60cdc9 7164 info->read = (unsigned int)-1;
2cadf913
SR
7165
7166 filp->private_data = info;
7167
cf6ab6d9
SRRH
7168 tr->current_trace->ref++;
7169
a695cb58
SRRH
7170 mutex_unlock(&trace_types_lock);
7171
7b85af63
SRRH
7172 ret = nonseekable_open(inode, filp);
7173 if (ret < 0)
7174 trace_array_put(tr);
7175
7176 return ret;
2cadf913
SR
7177}
7178
9dd95748 7179static __poll_t
cc60cdc9
SR
7180tracing_buffers_poll(struct file *filp, poll_table *poll_table)
7181{
7182 struct ftrace_buffer_info *info = filp->private_data;
7183 struct trace_iterator *iter = &info->iter;
7184
7185 return trace_poll(iter, filp, poll_table);
7186}
7187
2cadf913
SR
7188static ssize_t
7189tracing_buffers_read(struct file *filp, char __user *ubuf,
7190 size_t count, loff_t *ppos)
7191{
7192 struct ftrace_buffer_info *info = filp->private_data;
cc60cdc9 7193 struct trace_iterator *iter = &info->iter;
a7e52ad7 7194 ssize_t ret = 0;
6de58e62 7195 ssize_t size;
2cadf913 7196
2dc5d12b
SR
7197 if (!count)
7198 return 0;
7199
6de58e62 7200#ifdef CONFIG_TRACER_MAX_TRACE
d716ff71
SRRH
7201 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
7202 return -EBUSY;
6de58e62
SRRH
7203#endif
7204
73a757e6 7205 if (!info->spare) {
12883efb
SRRH
7206 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
7207 iter->cpu_file);
a7e52ad7
SRV
7208 if (IS_ERR(info->spare)) {
7209 ret = PTR_ERR(info->spare);
7210 info->spare = NULL;
7211 } else {
7212 info->spare_cpu = iter->cpu_file;
7213 }
73a757e6 7214 }
ddd538f3 7215 if (!info->spare)
a7e52ad7 7216 return ret;
ddd538f3 7217
2cadf913
SR
7218 /* Do we have previous read data to read? */
7219 if (info->read < PAGE_SIZE)
7220 goto read;
7221
b627344f 7222 again:
cc60cdc9 7223 trace_access_lock(iter->cpu_file);
12883efb 7224 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
2cadf913
SR
7225 &info->spare,
7226 count,
cc60cdc9
SR
7227 iter->cpu_file, 0);
7228 trace_access_unlock(iter->cpu_file);
2cadf913 7229
b627344f
SR
7230 if (ret < 0) {
7231 if (trace_empty(iter)) {
d716ff71
SRRH
7232 if ((filp->f_flags & O_NONBLOCK))
7233 return -EAGAIN;
7234
2c2b0a78 7235 ret = wait_on_pipe(iter, 0);
d716ff71
SRRH
7236 if (ret)
7237 return ret;
7238
b627344f
SR
7239 goto again;
7240 }
d716ff71 7241 return 0;
b627344f 7242 }
436fc280 7243
436fc280 7244 info->read = 0;
b627344f 7245 read:
2cadf913
SR
7246 size = PAGE_SIZE - info->read;
7247 if (size > count)
7248 size = count;
7249
7250 ret = copy_to_user(ubuf, info->spare + info->read, size);
d716ff71
SRRH
7251 if (ret == size)
7252 return -EFAULT;
7253
2dc5d12b
SR
7254 size -= ret;
7255
2cadf913
SR
7256 *ppos += size;
7257 info->read += size;
7258
7259 return size;
7260}
7261
7262static int tracing_buffers_release(struct inode *inode, struct file *file)
7263{
7264 struct ftrace_buffer_info *info = file->private_data;
cc60cdc9 7265 struct trace_iterator *iter = &info->iter;
2cadf913 7266
a695cb58
SRRH
7267 mutex_lock(&trace_types_lock);
7268
cf6ab6d9
SRRH
7269 iter->tr->current_trace->ref--;
7270
ff451961 7271 __trace_array_put(iter->tr);
2cadf913 7272
ddd538f3 7273 if (info->spare)
73a757e6
SRV
7274 ring_buffer_free_read_page(iter->trace_buffer->buffer,
7275 info->spare_cpu, info->spare);
2cadf913
SR
7276 kfree(info);
7277
a695cb58
SRRH
7278 mutex_unlock(&trace_types_lock);
7279
2cadf913
SR
7280 return 0;
7281}
7282
7283struct buffer_ref {
7284 struct ring_buffer *buffer;
7285 void *page;
73a757e6 7286 int cpu;
b9872226 7287 refcount_t refcount;
2cadf913
SR
7288};
7289
b9872226
JH
7290static void buffer_ref_release(struct buffer_ref *ref)
7291{
7292 if (!refcount_dec_and_test(&ref->refcount))
7293 return;
7294 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
7295 kfree(ref);
7296}
7297
2cadf913
SR
7298static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
7299 struct pipe_buffer *buf)
7300{
7301 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7302
b9872226 7303 buffer_ref_release(ref);
2cadf913
SR
7304 buf->private = 0;
7305}
7306
15fab63e 7307static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe,
2cadf913
SR
7308 struct pipe_buffer *buf)
7309{
7310 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7311
e9e1a2e7 7312 if (refcount_read(&ref->refcount) > INT_MAX/2)
15fab63e
MW
7313 return false;
7314
b9872226 7315 refcount_inc(&ref->refcount);
15fab63e 7316 return true;
2cadf913
SR
7317}
7318
7319/* Pipe buffer operations for a buffer. */
28dfef8f 7320static const struct pipe_buf_operations buffer_pipe_buf_ops = {
2cadf913
SR
7321 .confirm = generic_pipe_buf_confirm,
7322 .release = buffer_pipe_buf_release,
b9872226 7323 .steal = generic_pipe_buf_nosteal,
2cadf913
SR
7324 .get = buffer_pipe_buf_get,
7325};
7326
7327/*
7328 * Callback from splice_to_pipe(), if we need to release some pages
7329 * at the end of the spd in case we error'ed out in filling the pipe.
7330 */
7331static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
7332{
7333 struct buffer_ref *ref =
7334 (struct buffer_ref *)spd->partial[i].private;
7335
b9872226 7336 buffer_ref_release(ref);
2cadf913
SR
7337 spd->partial[i].private = 0;
7338}
7339
7340static ssize_t
7341tracing_buffers_splice_read(struct file *file, loff_t *ppos,
7342 struct pipe_inode_info *pipe, size_t len,
7343 unsigned int flags)
7344{
7345 struct ftrace_buffer_info *info = file->private_data;
cc60cdc9 7346 struct trace_iterator *iter = &info->iter;
35f3d14d
JA
7347 struct partial_page partial_def[PIPE_DEF_BUFFERS];
7348 struct page *pages_def[PIPE_DEF_BUFFERS];
2cadf913 7349 struct splice_pipe_desc spd = {
35f3d14d
JA
7350 .pages = pages_def,
7351 .partial = partial_def,
047fe360 7352 .nr_pages_max = PIPE_DEF_BUFFERS,
2cadf913
SR
7353 .ops = &buffer_pipe_buf_ops,
7354 .spd_release = buffer_spd_release,
7355 };
7356 struct buffer_ref *ref;
6b7e633f 7357 int entries, i;
07906da7 7358 ssize_t ret = 0;
2cadf913 7359
6de58e62 7360#ifdef CONFIG_TRACER_MAX_TRACE
d716ff71
SRRH
7361 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
7362 return -EBUSY;
6de58e62
SRRH
7363#endif
7364
d716ff71
SRRH
7365 if (*ppos & (PAGE_SIZE - 1))
7366 return -EINVAL;
93cfb3c9
LJ
7367
7368 if (len & (PAGE_SIZE - 1)) {
d716ff71
SRRH
7369 if (len < PAGE_SIZE)
7370 return -EINVAL;
93cfb3c9
LJ
7371 len &= PAGE_MASK;
7372 }
7373
1ae2293d
AV
7374 if (splice_grow_spd(pipe, &spd))
7375 return -ENOMEM;
7376
cc60cdc9
SR
7377 again:
7378 trace_access_lock(iter->cpu_file);
12883efb 7379 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
93459c6c 7380
a786c06d 7381 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
2cadf913
SR
7382 struct page *page;
7383 int r;
7384
7385 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
07906da7
RV
7386 if (!ref) {
7387 ret = -ENOMEM;
2cadf913 7388 break;
07906da7 7389 }
2cadf913 7390
b9872226 7391 refcount_set(&ref->refcount, 1);
12883efb 7392 ref->buffer = iter->trace_buffer->buffer;
cc60cdc9 7393 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
a7e52ad7
SRV
7394 if (IS_ERR(ref->page)) {
7395 ret = PTR_ERR(ref->page);
7396 ref->page = NULL;
2cadf913
SR
7397 kfree(ref);
7398 break;
7399 }
73a757e6 7400 ref->cpu = iter->cpu_file;
2cadf913
SR
7401
7402 r = ring_buffer_read_page(ref->buffer, &ref->page,
cc60cdc9 7403 len, iter->cpu_file, 1);
2cadf913 7404 if (r < 0) {
73a757e6
SRV
7405 ring_buffer_free_read_page(ref->buffer, ref->cpu,
7406 ref->page);
2cadf913
SR
7407 kfree(ref);
7408 break;
7409 }
7410
2cadf913
SR
7411 page = virt_to_page(ref->page);
7412
7413 spd.pages[i] = page;
7414 spd.partial[i].len = PAGE_SIZE;
7415 spd.partial[i].offset = 0;
7416 spd.partial[i].private = (unsigned long)ref;
7417 spd.nr_pages++;
93cfb3c9 7418 *ppos += PAGE_SIZE;
93459c6c 7419
12883efb 7420 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
2cadf913
SR
7421 }
7422
cc60cdc9 7423 trace_access_unlock(iter->cpu_file);
2cadf913
SR
7424 spd.nr_pages = i;
7425
7426 /* did we read anything? */
7427 if (!spd.nr_pages) {
07906da7 7428 if (ret)
1ae2293d 7429 goto out;
d716ff71 7430
1ae2293d 7431 ret = -EAGAIN;
d716ff71 7432 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
1ae2293d 7433 goto out;
07906da7 7434
03329f99 7435 ret = wait_on_pipe(iter, iter->tr->buffer_percent);
8b8b3683 7436 if (ret)
1ae2293d 7437 goto out;
e30f53aa 7438
cc60cdc9 7439 goto again;
2cadf913
SR
7440 }
7441
7442 ret = splice_to_pipe(pipe, &spd);
1ae2293d 7443out:
047fe360 7444 splice_shrink_spd(&spd);
6de58e62 7445
2cadf913
SR
7446 return ret;
7447}
7448
7449static const struct file_operations tracing_buffers_fops = {
7450 .open = tracing_buffers_open,
7451 .read = tracing_buffers_read,
cc60cdc9 7452 .poll = tracing_buffers_poll,
2cadf913
SR
7453 .release = tracing_buffers_release,
7454 .splice_read = tracing_buffers_splice_read,
7455 .llseek = no_llseek,
7456};
7457
c8d77183
SR
7458static ssize_t
7459tracing_stats_read(struct file *filp, char __user *ubuf,
7460 size_t count, loff_t *ppos)
7461{
4d3435b8
ON
7462 struct inode *inode = file_inode(filp);
7463 struct trace_array *tr = inode->i_private;
12883efb 7464 struct trace_buffer *trace_buf = &tr->trace_buffer;
4d3435b8 7465 int cpu = tracing_get_cpu(inode);
c8d77183
SR
7466 struct trace_seq *s;
7467 unsigned long cnt;
c64e148a
VN
7468 unsigned long long t;
7469 unsigned long usec_rem;
c8d77183 7470
e4f2d10f 7471 s = kmalloc(sizeof(*s), GFP_KERNEL);
c8d77183 7472 if (!s)
a646365c 7473 return -ENOMEM;
c8d77183
SR
7474
7475 trace_seq_init(s);
7476
12883efb 7477 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
c8d77183
SR
7478 trace_seq_printf(s, "entries: %ld\n", cnt);
7479
12883efb 7480 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
c8d77183
SR
7481 trace_seq_printf(s, "overrun: %ld\n", cnt);
7482
12883efb 7483 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
c8d77183
SR
7484 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
7485
12883efb 7486 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
c64e148a
VN
7487 trace_seq_printf(s, "bytes: %ld\n", cnt);
7488
58e8eedf 7489 if (trace_clocks[tr->clock_id].in_ns) {
11043d8b 7490 /* local or global for trace_clock */
12883efb 7491 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
11043d8b
YY
7492 usec_rem = do_div(t, USEC_PER_SEC);
7493 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
7494 t, usec_rem);
7495
12883efb 7496 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
11043d8b
YY
7497 usec_rem = do_div(t, USEC_PER_SEC);
7498 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
7499 } else {
7500 /* counter or tsc mode for trace_clock */
7501 trace_seq_printf(s, "oldest event ts: %llu\n",
12883efb 7502 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
c64e148a 7503
11043d8b 7504 trace_seq_printf(s, "now ts: %llu\n",
12883efb 7505 ring_buffer_time_stamp(trace_buf->buffer, cpu));
11043d8b 7506 }
c64e148a 7507
12883efb 7508 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
884bfe89
SP
7509 trace_seq_printf(s, "dropped events: %ld\n", cnt);
7510
12883efb 7511 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
ad964704
SRRH
7512 trace_seq_printf(s, "read events: %ld\n", cnt);
7513
5ac48378
SRRH
7514 count = simple_read_from_buffer(ubuf, count, ppos,
7515 s->buffer, trace_seq_used(s));
c8d77183
SR
7516
7517 kfree(s);
7518
7519 return count;
7520}
7521
7522static const struct file_operations tracing_stats_fops = {
4d3435b8 7523 .open = tracing_open_generic_tr,
c8d77183 7524 .read = tracing_stats_read,
b444786f 7525 .llseek = generic_file_llseek,
4d3435b8 7526 .release = tracing_release_generic_tr,
c8d77183
SR
7527};
7528
bc0c38d1
SR
7529#ifdef CONFIG_DYNAMIC_FTRACE
7530
7531static ssize_t
b807c3d0 7532tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
7533 size_t cnt, loff_t *ppos)
7534{
7535 unsigned long *p = filp->private_data;
6a9c981b 7536 char buf[64]; /* Not too big for a shallow stack */
bc0c38d1
SR
7537 int r;
7538
6a9c981b 7539 r = scnprintf(buf, 63, "%ld", *p);
b807c3d0
SR
7540 buf[r++] = '\n';
7541
6a9c981b 7542 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
7543}
7544
5e2336a0 7545static const struct file_operations tracing_dyn_info_fops = {
4bf39a94 7546 .open = tracing_open_generic,
b807c3d0 7547 .read = tracing_read_dyn_info,
b444786f 7548 .llseek = generic_file_llseek,
bc0c38d1 7549};
77fd5c15 7550#endif /* CONFIG_DYNAMIC_FTRACE */
bc0c38d1 7551
77fd5c15
SRRH
7552#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
7553static void
bca6c8d0 7554ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
b5f081b5 7555 struct trace_array *tr, struct ftrace_probe_ops *ops,
6e444319 7556 void *data)
77fd5c15 7557{
cab50379 7558 tracing_snapshot_instance(tr);
77fd5c15 7559}
bc0c38d1 7560
77fd5c15 7561static void
bca6c8d0 7562ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
b5f081b5 7563 struct trace_array *tr, struct ftrace_probe_ops *ops,
6e444319 7564 void *data)
bc0c38d1 7565{
6e444319 7566 struct ftrace_func_mapper *mapper = data;
1a93f8bd 7567 long *count = NULL;
77fd5c15 7568
1a93f8bd
SRV
7569 if (mapper)
7570 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
7571
7572 if (count) {
7573
7574 if (*count <= 0)
7575 return;
bc0c38d1 7576
77fd5c15 7577 (*count)--;
1a93f8bd 7578 }
77fd5c15 7579
cab50379 7580 tracing_snapshot_instance(tr);
77fd5c15
SRRH
7581}
7582
7583static int
7584ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
7585 struct ftrace_probe_ops *ops, void *data)
7586{
6e444319 7587 struct ftrace_func_mapper *mapper = data;
1a93f8bd 7588 long *count = NULL;
77fd5c15
SRRH
7589
7590 seq_printf(m, "%ps:", (void *)ip);
7591
fa6f0cc7 7592 seq_puts(m, "snapshot");
77fd5c15 7593
1a93f8bd
SRV
7594 if (mapper)
7595 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
7596
7597 if (count)
7598 seq_printf(m, ":count=%ld\n", *count);
77fd5c15 7599 else
1a93f8bd 7600 seq_puts(m, ":unlimited\n");
77fd5c15
SRRH
7601
7602 return 0;
7603}
7604
1a93f8bd 7605static int
b5f081b5 7606ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
6e444319 7607 unsigned long ip, void *init_data, void **data)
1a93f8bd 7608{
6e444319
SRV
7609 struct ftrace_func_mapper *mapper = *data;
7610
7611 if (!mapper) {
7612 mapper = allocate_ftrace_func_mapper();
7613 if (!mapper)
7614 return -ENOMEM;
7615 *data = mapper;
7616 }
1a93f8bd 7617
6e444319 7618 return ftrace_func_mapper_add_ip(mapper, ip, init_data);
1a93f8bd
SRV
7619}
7620
7621static void
b5f081b5 7622ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
6e444319 7623 unsigned long ip, void *data)
1a93f8bd 7624{
6e444319
SRV
7625 struct ftrace_func_mapper *mapper = data;
7626
7627 if (!ip) {
7628 if (!mapper)
7629 return;
7630 free_ftrace_func_mapper(mapper, NULL);
7631 return;
7632 }
1a93f8bd
SRV
7633
7634 ftrace_func_mapper_remove_ip(mapper, ip);
7635}
7636
77fd5c15
SRRH
7637static struct ftrace_probe_ops snapshot_probe_ops = {
7638 .func = ftrace_snapshot,
7639 .print = ftrace_snapshot_print,
7640};
7641
7642static struct ftrace_probe_ops snapshot_count_probe_ops = {
7643 .func = ftrace_count_snapshot,
7644 .print = ftrace_snapshot_print,
1a93f8bd
SRV
7645 .init = ftrace_snapshot_init,
7646 .free = ftrace_snapshot_free,
77fd5c15
SRRH
7647};
7648
7649static int
04ec7bb6 7650ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
77fd5c15
SRRH
7651 char *glob, char *cmd, char *param, int enable)
7652{
7653 struct ftrace_probe_ops *ops;
7654 void *count = (void *)-1;
7655 char *number;
7656 int ret;
7657
0f179765
SRV
7658 if (!tr)
7659 return -ENODEV;
7660
77fd5c15
SRRH
7661 /* hash funcs only work with set_ftrace_filter */
7662 if (!enable)
7663 return -EINVAL;
7664
7665 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
7666
d3d532d7 7667 if (glob[0] == '!')
7b60f3d8 7668 return unregister_ftrace_function_probe_func(glob+1, tr, ops);
77fd5c15
SRRH
7669
7670 if (!param)
7671 goto out_reg;
7672
7673 number = strsep(&param, ":");
7674
7675 if (!strlen(number))
7676 goto out_reg;
7677
7678 /*
7679 * We use the callback data field (which is a pointer)
7680 * as our counter.
7681 */
7682 ret = kstrtoul(number, 0, (unsigned long *)&count);
7683 if (ret)
7684 return ret;
7685
7686 out_reg:
2824f503 7687 ret = tracing_alloc_snapshot_instance(tr);
df62db5b
SRV
7688 if (ret < 0)
7689 goto out;
77fd5c15 7690
4c174688 7691 ret = register_ftrace_function_probe(glob, tr, ops, count);
77fd5c15 7692
df62db5b 7693 out:
77fd5c15
SRRH
7694 return ret < 0 ? ret : 0;
7695}
7696
7697static struct ftrace_func_command ftrace_snapshot_cmd = {
7698 .name = "snapshot",
7699 .func = ftrace_trace_snapshot_callback,
7700};
7701
38de93ab 7702static __init int register_snapshot_cmd(void)
77fd5c15
SRRH
7703{
7704 return register_ftrace_command(&ftrace_snapshot_cmd);
7705}
7706#else
38de93ab 7707static inline __init int register_snapshot_cmd(void) { return 0; }
77fd5c15 7708#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
bc0c38d1 7709
7eeafbca 7710static struct dentry *tracing_get_dentry(struct trace_array *tr)
bc0c38d1 7711{
8434dc93
SRRH
7712 if (WARN_ON(!tr->dir))
7713 return ERR_PTR(-ENODEV);
7714
7715 /* Top directory uses NULL as the parent */
7716 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
7717 return NULL;
7718
7719 /* All sub buffers have a descriptor */
2b6080f2 7720 return tr->dir;
bc0c38d1
SR
7721}
7722
2b6080f2 7723static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
b04cc6b1 7724{
b04cc6b1
FW
7725 struct dentry *d_tracer;
7726
2b6080f2
SR
7727 if (tr->percpu_dir)
7728 return tr->percpu_dir;
b04cc6b1 7729
7eeafbca 7730 d_tracer = tracing_get_dentry(tr);
14a5ae40 7731 if (IS_ERR(d_tracer))
b04cc6b1
FW
7732 return NULL;
7733
8434dc93 7734 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
b04cc6b1 7735
2b6080f2 7736 WARN_ONCE(!tr->percpu_dir,
8434dc93 7737 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
b04cc6b1 7738
2b6080f2 7739 return tr->percpu_dir;
b04cc6b1
FW
7740}
7741
649e9c70
ON
7742static struct dentry *
7743trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
7744 void *data, long cpu, const struct file_operations *fops)
7745{
7746 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
7747
7748 if (ret) /* See tracing_get_cpu() */
7682c918 7749 d_inode(ret)->i_cdev = (void *)(cpu + 1);
649e9c70
ON
7750 return ret;
7751}
7752
2b6080f2 7753static void
8434dc93 7754tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
b04cc6b1 7755{
2b6080f2 7756 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5452af66 7757 struct dentry *d_cpu;
dd49a38c 7758 char cpu_dir[30]; /* 30 characters should be more than enough */
b04cc6b1 7759
0a3d7ce7
NK
7760 if (!d_percpu)
7761 return;
7762
dd49a38c 7763 snprintf(cpu_dir, 30, "cpu%ld", cpu);
8434dc93 7764 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
8656e7a2 7765 if (!d_cpu) {
a395d6a7 7766 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
8656e7a2
FW
7767 return;
7768 }
b04cc6b1 7769
8656e7a2 7770 /* per cpu trace_pipe */
649e9c70 7771 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
15544209 7772 tr, cpu, &tracing_pipe_fops);
b04cc6b1
FW
7773
7774 /* per cpu trace */
649e9c70 7775 trace_create_cpu_file("trace", 0644, d_cpu,
6484c71c 7776 tr, cpu, &tracing_fops);
7f96f93f 7777
649e9c70 7778 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
46ef2be0 7779 tr, cpu, &tracing_buffers_fops);
7f96f93f 7780
649e9c70 7781 trace_create_cpu_file("stats", 0444, d_cpu,
4d3435b8 7782 tr, cpu, &tracing_stats_fops);
438ced17 7783
649e9c70 7784 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
0bc392ee 7785 tr, cpu, &tracing_entries_fops);
f1affcaa
SRRH
7786
7787#ifdef CONFIG_TRACER_SNAPSHOT
649e9c70 7788 trace_create_cpu_file("snapshot", 0644, d_cpu,
6484c71c 7789 tr, cpu, &snapshot_fops);
6de58e62 7790
649e9c70 7791 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
46ef2be0 7792 tr, cpu, &snapshot_raw_fops);
f1affcaa 7793#endif
b04cc6b1
FW
7794}
7795
60a11774
SR
7796#ifdef CONFIG_FTRACE_SELFTEST
7797/* Let selftest have access to static functions in this file */
7798#include "trace_selftest.c"
7799#endif
7800
577b785f
SR
7801static ssize_t
7802trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
7803 loff_t *ppos)
7804{
7805 struct trace_option_dentry *topt = filp->private_data;
7806 char *buf;
7807
7808 if (topt->flags->val & topt->opt->bit)
7809 buf = "1\n";
7810 else
7811 buf = "0\n";
7812
7813 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
7814}
7815
7816static ssize_t
7817trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
7818 loff_t *ppos)
7819{
7820 struct trace_option_dentry *topt = filp->private_data;
7821 unsigned long val;
577b785f
SR
7822 int ret;
7823
22fe9b54
PH
7824 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7825 if (ret)
577b785f
SR
7826 return ret;
7827
8d18eaaf
LZ
7828 if (val != 0 && val != 1)
7829 return -EINVAL;
577b785f 7830
8d18eaaf 7831 if (!!(topt->flags->val & topt->opt->bit) != val) {
577b785f 7832 mutex_lock(&trace_types_lock);
8c1a49ae 7833 ret = __set_tracer_option(topt->tr, topt->flags,
c757bea9 7834 topt->opt, !val);
577b785f
SR
7835 mutex_unlock(&trace_types_lock);
7836 if (ret)
7837 return ret;
577b785f
SR
7838 }
7839
7840 *ppos += cnt;
7841
7842 return cnt;
7843}
7844
7845
7846static const struct file_operations trace_options_fops = {
7847 .open = tracing_open_generic,
7848 .read = trace_options_read,
7849 .write = trace_options_write,
b444786f 7850 .llseek = generic_file_llseek,
577b785f
SR
7851};
7852
9a38a885
SRRH
7853/*
7854 * In order to pass in both the trace_array descriptor as well as the index
7855 * to the flag that the trace option file represents, the trace_array
7856 * has a character array of trace_flags_index[], which holds the index
7857 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
7858 * The address of this character array is passed to the flag option file
7859 * read/write callbacks.
7860 *
7861 * In order to extract both the index and the trace_array descriptor,
7862 * get_tr_index() uses the following algorithm.
7863 *
7864 * idx = *ptr;
7865 *
7866 * As the pointer itself contains the address of the index (remember
7867 * index[1] == 1).
7868 *
7869 * Then to get the trace_array descriptor, by subtracting that index
7870 * from the ptr, we get to the start of the index itself.
7871 *
7872 * ptr - idx == &index[0]
7873 *
7874 * Then a simple container_of() from that pointer gets us to the
7875 * trace_array descriptor.
7876 */
7877static void get_tr_index(void *data, struct trace_array **ptr,
7878 unsigned int *pindex)
7879{
7880 *pindex = *(unsigned char *)data;
7881
7882 *ptr = container_of(data - *pindex, struct trace_array,
7883 trace_flags_index);
7884}
7885
a8259075
SR
7886static ssize_t
7887trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
7888 loff_t *ppos)
7889{
9a38a885
SRRH
7890 void *tr_index = filp->private_data;
7891 struct trace_array *tr;
7892 unsigned int index;
a8259075
SR
7893 char *buf;
7894
9a38a885
SRRH
7895 get_tr_index(tr_index, &tr, &index);
7896
7897 if (tr->trace_flags & (1 << index))
a8259075
SR
7898 buf = "1\n";
7899 else
7900 buf = "0\n";
7901
7902 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
7903}
7904
7905static ssize_t
7906trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
7907 loff_t *ppos)
7908{
9a38a885
SRRH
7909 void *tr_index = filp->private_data;
7910 struct trace_array *tr;
7911 unsigned int index;
a8259075
SR
7912 unsigned long val;
7913 int ret;
7914
9a38a885
SRRH
7915 get_tr_index(tr_index, &tr, &index);
7916
22fe9b54
PH
7917 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7918 if (ret)
a8259075
SR
7919 return ret;
7920
f2d84b65 7921 if (val != 0 && val != 1)
a8259075 7922 return -EINVAL;
69d34da2
SRRH
7923
7924 mutex_lock(&trace_types_lock);
2b6080f2 7925 ret = set_tracer_flag(tr, 1 << index, val);
69d34da2 7926 mutex_unlock(&trace_types_lock);
a8259075 7927
613f04a0
SRRH
7928 if (ret < 0)
7929 return ret;
7930
a8259075
SR
7931 *ppos += cnt;
7932
7933 return cnt;
7934}
7935
a8259075
SR
7936static const struct file_operations trace_options_core_fops = {
7937 .open = tracing_open_generic,
7938 .read = trace_options_core_read,
7939 .write = trace_options_core_write,
b444786f 7940 .llseek = generic_file_llseek,
a8259075
SR
7941};
7942
5452af66 7943struct dentry *trace_create_file(const char *name,
f4ae40a6 7944 umode_t mode,
5452af66
FW
7945 struct dentry *parent,
7946 void *data,
7947 const struct file_operations *fops)
7948{
7949 struct dentry *ret;
7950
8434dc93 7951 ret = tracefs_create_file(name, mode, parent, data, fops);
5452af66 7952 if (!ret)
a395d6a7 7953 pr_warn("Could not create tracefs '%s' entry\n", name);
5452af66
FW
7954
7955 return ret;
7956}
7957
7958
2b6080f2 7959static struct dentry *trace_options_init_dentry(struct trace_array *tr)
a8259075
SR
7960{
7961 struct dentry *d_tracer;
a8259075 7962
2b6080f2
SR
7963 if (tr->options)
7964 return tr->options;
a8259075 7965
7eeafbca 7966 d_tracer = tracing_get_dentry(tr);
14a5ae40 7967 if (IS_ERR(d_tracer))
a8259075
SR
7968 return NULL;
7969
8434dc93 7970 tr->options = tracefs_create_dir("options", d_tracer);
2b6080f2 7971 if (!tr->options) {
a395d6a7 7972 pr_warn("Could not create tracefs directory 'options'\n");
a8259075
SR
7973 return NULL;
7974 }
7975
2b6080f2 7976 return tr->options;
a8259075
SR
7977}
7978
577b785f 7979static void
2b6080f2
SR
7980create_trace_option_file(struct trace_array *tr,
7981 struct trace_option_dentry *topt,
577b785f
SR
7982 struct tracer_flags *flags,
7983 struct tracer_opt *opt)
7984{
7985 struct dentry *t_options;
577b785f 7986
2b6080f2 7987 t_options = trace_options_init_dentry(tr);
577b785f
SR
7988 if (!t_options)
7989 return;
7990
7991 topt->flags = flags;
7992 topt->opt = opt;
2b6080f2 7993 topt->tr = tr;
577b785f 7994
5452af66 7995 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
577b785f
SR
7996 &trace_options_fops);
7997
577b785f
SR
7998}
7999
37aea98b 8000static void
2b6080f2 8001create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
577b785f
SR
8002{
8003 struct trace_option_dentry *topts;
37aea98b 8004 struct trace_options *tr_topts;
577b785f
SR
8005 struct tracer_flags *flags;
8006 struct tracer_opt *opts;
8007 int cnt;
37aea98b 8008 int i;
577b785f
SR
8009
8010 if (!tracer)
37aea98b 8011 return;
577b785f
SR
8012
8013 flags = tracer->flags;
8014
8015 if (!flags || !flags->opts)
37aea98b
SRRH
8016 return;
8017
8018 /*
8019 * If this is an instance, only create flags for tracers
8020 * the instance may have.
8021 */
8022 if (!trace_ok_for_array(tracer, tr))
8023 return;
8024
8025 for (i = 0; i < tr->nr_topts; i++) {
d39cdd20
CH
8026 /* Make sure there's no duplicate flags. */
8027 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
37aea98b
SRRH
8028 return;
8029 }
577b785f
SR
8030
8031 opts = flags->opts;
8032
8033 for (cnt = 0; opts[cnt].name; cnt++)
8034 ;
8035
0cfe8245 8036 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
577b785f 8037 if (!topts)
37aea98b
SRRH
8038 return;
8039
8040 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
8041 GFP_KERNEL);
8042 if (!tr_topts) {
8043 kfree(topts);
8044 return;
8045 }
8046
8047 tr->topts = tr_topts;
8048 tr->topts[tr->nr_topts].tracer = tracer;
8049 tr->topts[tr->nr_topts].topts = topts;
8050 tr->nr_topts++;
577b785f 8051
41d9c0be 8052 for (cnt = 0; opts[cnt].name; cnt++) {
2b6080f2 8053 create_trace_option_file(tr, &topts[cnt], flags,
577b785f 8054 &opts[cnt]);
41d9c0be
SRRH
8055 WARN_ONCE(topts[cnt].entry == NULL,
8056 "Failed to create trace option: %s",
8057 opts[cnt].name);
8058 }
577b785f
SR
8059}
8060
a8259075 8061static struct dentry *
2b6080f2
SR
8062create_trace_option_core_file(struct trace_array *tr,
8063 const char *option, long index)
a8259075
SR
8064{
8065 struct dentry *t_options;
a8259075 8066
2b6080f2 8067 t_options = trace_options_init_dentry(tr);
a8259075
SR
8068 if (!t_options)
8069 return NULL;
8070
9a38a885
SRRH
8071 return trace_create_file(option, 0644, t_options,
8072 (void *)&tr->trace_flags_index[index],
8073 &trace_options_core_fops);
a8259075
SR
8074}
8075
16270145 8076static void create_trace_options_dir(struct trace_array *tr)
a8259075
SR
8077{
8078 struct dentry *t_options;
16270145 8079 bool top_level = tr == &global_trace;
a8259075
SR
8080 int i;
8081
2b6080f2 8082 t_options = trace_options_init_dentry(tr);
a8259075
SR
8083 if (!t_options)
8084 return;
8085
16270145
SRRH
8086 for (i = 0; trace_options[i]; i++) {
8087 if (top_level ||
8088 !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
8089 create_trace_option_core_file(tr, trace_options[i], i);
8090 }
a8259075
SR
8091}
8092
499e5470
SR
8093static ssize_t
8094rb_simple_read(struct file *filp, char __user *ubuf,
8095 size_t cnt, loff_t *ppos)
8096{
348f0fc2 8097 struct trace_array *tr = filp->private_data;
499e5470
SR
8098 char buf[64];
8099 int r;
8100
10246fa3 8101 r = tracer_tracing_is_on(tr);
499e5470
SR
8102 r = sprintf(buf, "%d\n", r);
8103
8104 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8105}
8106
8107static ssize_t
8108rb_simple_write(struct file *filp, const char __user *ubuf,
8109 size_t cnt, loff_t *ppos)
8110{
348f0fc2 8111 struct trace_array *tr = filp->private_data;
12883efb 8112 struct ring_buffer *buffer = tr->trace_buffer.buffer;
499e5470
SR
8113 unsigned long val;
8114 int ret;
8115
8116 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8117 if (ret)
8118 return ret;
8119
8120 if (buffer) {
2df8f8a6 8121 mutex_lock(&trace_types_lock);
f143641b
SRV
8122 if (!!val == tracer_tracing_is_on(tr)) {
8123 val = 0; /* do nothing */
8124 } else if (val) {
10246fa3 8125 tracer_tracing_on(tr);
2b6080f2
SR
8126 if (tr->current_trace->start)
8127 tr->current_trace->start(tr);
2df8f8a6 8128 } else {
10246fa3 8129 tracer_tracing_off(tr);
2b6080f2
SR
8130 if (tr->current_trace->stop)
8131 tr->current_trace->stop(tr);
2df8f8a6
SR
8132 }
8133 mutex_unlock(&trace_types_lock);
499e5470
SR
8134 }
8135
8136 (*ppos)++;
8137
8138 return cnt;
8139}
8140
8141static const struct file_operations rb_simple_fops = {
7b85af63 8142 .open = tracing_open_generic_tr,
499e5470
SR
8143 .read = rb_simple_read,
8144 .write = rb_simple_write,
7b85af63 8145 .release = tracing_release_generic_tr,
499e5470
SR
8146 .llseek = default_llseek,
8147};
8148
03329f99
SRV
8149static ssize_t
8150buffer_percent_read(struct file *filp, char __user *ubuf,
8151 size_t cnt, loff_t *ppos)
8152{
8153 struct trace_array *tr = filp->private_data;
8154 char buf[64];
8155 int r;
8156
8157 r = tr->buffer_percent;
8158 r = sprintf(buf, "%d\n", r);
8159
8160 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8161}
8162
8163static ssize_t
8164buffer_percent_write(struct file *filp, const char __user *ubuf,
8165 size_t cnt, loff_t *ppos)
8166{
8167 struct trace_array *tr = filp->private_data;
8168 unsigned long val;
8169 int ret;
8170
8171 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8172 if (ret)
8173 return ret;
8174
8175 if (val > 100)
8176 return -EINVAL;
8177
8178 if (!val)
8179 val = 1;
8180
8181 tr->buffer_percent = val;
8182
8183 (*ppos)++;
8184
8185 return cnt;
8186}
8187
8188static const struct file_operations buffer_percent_fops = {
8189 .open = tracing_open_generic_tr,
8190 .read = buffer_percent_read,
8191 .write = buffer_percent_write,
8192 .release = tracing_release_generic_tr,
8193 .llseek = default_llseek,
8194};
8195
277ba044
SR
8196struct dentry *trace_instance_dir;
8197
8198static void
8434dc93 8199init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
277ba044 8200
55034cd6
SRRH
8201static int
8202allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
277ba044
SR
8203{
8204 enum ring_buffer_flags rb_flags;
737223fb 8205
983f938a 8206 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
737223fb 8207
dced341b
SRRH
8208 buf->tr = tr;
8209
55034cd6
SRRH
8210 buf->buffer = ring_buffer_alloc(size, rb_flags);
8211 if (!buf->buffer)
8212 return -ENOMEM;
737223fb 8213
55034cd6
SRRH
8214 buf->data = alloc_percpu(struct trace_array_cpu);
8215 if (!buf->data) {
8216 ring_buffer_free(buf->buffer);
4397f045 8217 buf->buffer = NULL;
55034cd6
SRRH
8218 return -ENOMEM;
8219 }
737223fb 8220
737223fb
SRRH
8221 /* Allocate the first page for all buffers */
8222 set_buffer_entries(&tr->trace_buffer,
8223 ring_buffer_size(tr->trace_buffer.buffer, 0));
8224
55034cd6
SRRH
8225 return 0;
8226}
737223fb 8227
55034cd6
SRRH
8228static int allocate_trace_buffers(struct trace_array *tr, int size)
8229{
8230 int ret;
737223fb 8231
55034cd6
SRRH
8232 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
8233 if (ret)
8234 return ret;
737223fb 8235
55034cd6
SRRH
8236#ifdef CONFIG_TRACER_MAX_TRACE
8237 ret = allocate_trace_buffer(tr, &tr->max_buffer,
8238 allocate_snapshot ? size : 1);
8239 if (WARN_ON(ret)) {
737223fb 8240 ring_buffer_free(tr->trace_buffer.buffer);
24f2aaf9 8241 tr->trace_buffer.buffer = NULL;
55034cd6 8242 free_percpu(tr->trace_buffer.data);
24f2aaf9 8243 tr->trace_buffer.data = NULL;
55034cd6
SRRH
8244 return -ENOMEM;
8245 }
8246 tr->allocated_snapshot = allocate_snapshot;
737223fb 8247
55034cd6
SRRH
8248 /*
8249 * Only the top level trace array gets its snapshot allocated
8250 * from the kernel command line.
8251 */
8252 allocate_snapshot = false;
737223fb 8253#endif
55034cd6 8254 return 0;
737223fb
SRRH
8255}
8256
f0b70cc4
SRRH
8257static void free_trace_buffer(struct trace_buffer *buf)
8258{
8259 if (buf->buffer) {
8260 ring_buffer_free(buf->buffer);
8261 buf->buffer = NULL;
8262 free_percpu(buf->data);
8263 buf->data = NULL;
8264 }
8265}
8266
23aaa3c1
SRRH
8267static void free_trace_buffers(struct trace_array *tr)
8268{
8269 if (!tr)
8270 return;
8271
f0b70cc4 8272 free_trace_buffer(&tr->trace_buffer);
23aaa3c1
SRRH
8273
8274#ifdef CONFIG_TRACER_MAX_TRACE
f0b70cc4 8275 free_trace_buffer(&tr->max_buffer);
23aaa3c1
SRRH
8276#endif
8277}
8278
9a38a885
SRRH
8279static void init_trace_flags_index(struct trace_array *tr)
8280{
8281 int i;
8282
8283 /* Used by the trace options files */
8284 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
8285 tr->trace_flags_index[i] = i;
8286}
8287
37aea98b
SRRH
8288static void __update_tracer_options(struct trace_array *tr)
8289{
8290 struct tracer *t;
8291
8292 for (t = trace_types; t; t = t->next)
8293 add_tracer_options(tr, t);
8294}
8295
8296static void update_tracer_options(struct trace_array *tr)
8297{
8298 mutex_lock(&trace_types_lock);
8299 __update_tracer_options(tr);
8300 mutex_unlock(&trace_types_lock);
8301}
8302
f45d1225 8303struct trace_array *trace_array_create(const char *name)
737223fb 8304{
277ba044
SR
8305 struct trace_array *tr;
8306 int ret;
277ba044 8307
12ecef0c 8308 mutex_lock(&event_mutex);
277ba044
SR
8309 mutex_lock(&trace_types_lock);
8310
8311 ret = -EEXIST;
8312 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8313 if (tr->name && strcmp(tr->name, name) == 0)
8314 goto out_unlock;
8315 }
8316
8317 ret = -ENOMEM;
8318 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
8319 if (!tr)
8320 goto out_unlock;
8321
8322 tr->name = kstrdup(name, GFP_KERNEL);
8323 if (!tr->name)
8324 goto out_free_tr;
8325
ccfe9e42
AL
8326 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
8327 goto out_free_tr;
8328
20550622 8329 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
983f938a 8330
ccfe9e42
AL
8331 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
8332
277ba044
SR
8333 raw_spin_lock_init(&tr->start_lock);
8334
0b9b12c1
SRRH
8335 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
8336
277ba044
SR
8337 tr->current_trace = &nop_trace;
8338
8339 INIT_LIST_HEAD(&tr->systems);
8340 INIT_LIST_HEAD(&tr->events);
067fe038 8341 INIT_LIST_HEAD(&tr->hist_vars);
2f754e77 8342 INIT_LIST_HEAD(&tr->err_log);
277ba044 8343
737223fb 8344 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
277ba044
SR
8345 goto out_free_tr;
8346
8434dc93 8347 tr->dir = tracefs_create_dir(name, trace_instance_dir);
277ba044
SR
8348 if (!tr->dir)
8349 goto out_free_tr;
8350
8351 ret = event_trace_add_tracer(tr->dir, tr);
609e85a7 8352 if (ret) {
8434dc93 8353 tracefs_remove_recursive(tr->dir);
277ba044 8354 goto out_free_tr;
609e85a7 8355 }
277ba044 8356
04ec7bb6
SRV
8357 ftrace_init_trace_array(tr);
8358
8434dc93 8359 init_tracer_tracefs(tr, tr->dir);
9a38a885 8360 init_trace_flags_index(tr);
37aea98b 8361 __update_tracer_options(tr);
277ba044
SR
8362
8363 list_add(&tr->list, &ftrace_trace_arrays);
8364
8365 mutex_unlock(&trace_types_lock);
12ecef0c 8366 mutex_unlock(&event_mutex);
277ba044 8367
f45d1225 8368 return tr;
277ba044
SR
8369
8370 out_free_tr:
23aaa3c1 8371 free_trace_buffers(tr);
ccfe9e42 8372 free_cpumask_var(tr->tracing_cpumask);
277ba044
SR
8373 kfree(tr->name);
8374 kfree(tr);
8375
8376 out_unlock:
8377 mutex_unlock(&trace_types_lock);
12ecef0c 8378 mutex_unlock(&event_mutex);
277ba044 8379
f45d1225
DI
8380 return ERR_PTR(ret);
8381}
8382EXPORT_SYMBOL_GPL(trace_array_create);
277ba044 8383
f45d1225
DI
8384static int instance_mkdir(const char *name)
8385{
8386 return PTR_ERR_OR_ZERO(trace_array_create(name));
277ba044
SR
8387}
8388
f45d1225 8389static int __remove_instance(struct trace_array *tr)
0c8916c3 8390{
37aea98b 8391 int i;
0c8916c3 8392
cf6ab6d9 8393 if (tr->ref || (tr->current_trace && tr->current_trace->ref))
f45d1225 8394 return -EBUSY;
a695cb58 8395
0c8916c3
SR
8396 list_del(&tr->list);
8397
20550622
SRRH
8398 /* Disable all the flags that were enabled coming in */
8399 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
8400 if ((1 << i) & ZEROED_TRACE_FLAGS)
8401 set_tracer_flag(tr, 1 << i, 0);
8402 }
8403
6b450d25 8404 tracing_set_nop(tr);
a0e6369e 8405 clear_ftrace_function_probes(tr);
0c8916c3 8406 event_trace_del_tracer(tr);
d879d0b8 8407 ftrace_clear_pids(tr);
591dffda 8408 ftrace_destroy_function_files(tr);
681a4a2f 8409 tracefs_remove_recursive(tr->dir);
a9fcaaac 8410 free_trace_buffers(tr);
0c8916c3 8411
37aea98b
SRRH
8412 for (i = 0; i < tr->nr_topts; i++) {
8413 kfree(tr->topts[i].topts);
8414 }
8415 kfree(tr->topts);
8416
db9108e0 8417 free_cpumask_var(tr->tracing_cpumask);
0c8916c3
SR
8418 kfree(tr->name);
8419 kfree(tr);
f45d1225 8420 tr = NULL;
0c8916c3 8421
f45d1225
DI
8422 return 0;
8423}
8424
8425int trace_array_destroy(struct trace_array *tr)
8426{
8427 int ret;
8428
8429 if (!tr)
8430 return -EINVAL;
8431
8432 mutex_lock(&event_mutex);
8433 mutex_lock(&trace_types_lock);
8434
8435 ret = __remove_instance(tr);
8436
8437 mutex_unlock(&trace_types_lock);
8438 mutex_unlock(&event_mutex);
8439
8440 return ret;
8441}
8442EXPORT_SYMBOL_GPL(trace_array_destroy);
8443
8444static int instance_rmdir(const char *name)
8445{
8446 struct trace_array *tr;
8447 int ret;
8448
8449 mutex_lock(&event_mutex);
8450 mutex_lock(&trace_types_lock);
8451
8452 ret = -ENODEV;
8453 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8454 if (tr->name && strcmp(tr->name, name) == 0) {
8455 ret = __remove_instance(tr);
8456 break;
8457 }
8458 }
0c8916c3 8459
0c8916c3 8460 mutex_unlock(&trace_types_lock);
12ecef0c 8461 mutex_unlock(&event_mutex);
0c8916c3
SR
8462
8463 return ret;
8464}
8465
277ba044
SR
8466static __init void create_trace_instances(struct dentry *d_tracer)
8467{
eae47358
SRRH
8468 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
8469 instance_mkdir,
8470 instance_rmdir);
277ba044
SR
8471 if (WARN_ON(!trace_instance_dir))
8472 return;
277ba044
SR
8473}
8474
2b6080f2 8475static void
8434dc93 8476init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
2b6080f2 8477{
3dd80953 8478 struct trace_event_file *file;
121aaee7 8479 int cpu;
2b6080f2 8480
607e2ea1
SRRH
8481 trace_create_file("available_tracers", 0444, d_tracer,
8482 tr, &show_traces_fops);
8483
8484 trace_create_file("current_tracer", 0644, d_tracer,
8485 tr, &set_tracer_fops);
8486
ccfe9e42
AL
8487 trace_create_file("tracing_cpumask", 0644, d_tracer,
8488 tr, &tracing_cpumask_fops);
8489
2b6080f2
SR
8490 trace_create_file("trace_options", 0644, d_tracer,
8491 tr, &tracing_iter_fops);
8492
8493 trace_create_file("trace", 0644, d_tracer,
6484c71c 8494 tr, &tracing_fops);
2b6080f2
SR
8495
8496 trace_create_file("trace_pipe", 0444, d_tracer,
15544209 8497 tr, &tracing_pipe_fops);
2b6080f2
SR
8498
8499 trace_create_file("buffer_size_kb", 0644, d_tracer,
0bc392ee 8500 tr, &tracing_entries_fops);
2b6080f2
SR
8501
8502 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
8503 tr, &tracing_total_entries_fops);
8504
238ae93d 8505 trace_create_file("free_buffer", 0200, d_tracer,
2b6080f2
SR
8506 tr, &tracing_free_buffer_fops);
8507
8508 trace_create_file("trace_marker", 0220, d_tracer,
8509 tr, &tracing_mark_fops);
8510
3dd80953
SRV
8511 file = __find_event_file(tr, "ftrace", "print");
8512 if (file && file->dir)
8513 trace_create_file("trigger", 0644, file->dir, file,
8514 &event_trigger_fops);
8515 tr->trace_marker_file = file;
8516
fa32e855
SR
8517 trace_create_file("trace_marker_raw", 0220, d_tracer,
8518 tr, &tracing_mark_raw_fops);
8519
2b6080f2
SR
8520 trace_create_file("trace_clock", 0644, d_tracer, tr,
8521 &trace_clock_fops);
8522
8523 trace_create_file("tracing_on", 0644, d_tracer,
6484c71c 8524 tr, &rb_simple_fops);
ce9bae55 8525
2c1ea60b
TZ
8526 trace_create_file("timestamp_mode", 0444, d_tracer, tr,
8527 &trace_time_stamp_mode_fops);
8528
a7b1d74e 8529 tr->buffer_percent = 50;
03329f99
SRV
8530
8531 trace_create_file("buffer_percent", 0444, d_tracer,
8532 tr, &buffer_percent_fops);
8533
16270145
SRRH
8534 create_trace_options_dir(tr);
8535
f971cc9a 8536#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
6d9b3fa5
SRRH
8537 trace_create_file("tracing_max_latency", 0644, d_tracer,
8538 &tr->max_latency, &tracing_max_lat_fops);
8539#endif
8540
591dffda
SRRH
8541 if (ftrace_create_function_files(tr, d_tracer))
8542 WARN(1, "Could not allocate function filter files");
8543
ce9bae55
SRRH
8544#ifdef CONFIG_TRACER_SNAPSHOT
8545 trace_create_file("snapshot", 0644, d_tracer,
6484c71c 8546 tr, &snapshot_fops);
ce9bae55 8547#endif
121aaee7 8548
8a062902
TZ
8549 trace_create_file("error_log", 0644, d_tracer,
8550 tr, &tracing_err_log_fops);
8551
121aaee7 8552 for_each_tracing_cpu(cpu)
8434dc93 8553 tracing_init_tracefs_percpu(tr, cpu);
121aaee7 8554
345ddcc8 8555 ftrace_init_tracefs(tr, d_tracer);
2b6080f2
SR
8556}
8557
93faccbb 8558static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
f76180bc
SRRH
8559{
8560 struct vfsmount *mnt;
8561 struct file_system_type *type;
8562
8563 /*
8564 * To maintain backward compatibility for tools that mount
8565 * debugfs to get to the tracing facility, tracefs is automatically
8566 * mounted to the debugfs/tracing directory.
8567 */
8568 type = get_fs_type("tracefs");
8569 if (!type)
8570 return NULL;
93faccbb 8571 mnt = vfs_submount(mntpt, type, "tracefs", NULL);
f76180bc
SRRH
8572 put_filesystem(type);
8573 if (IS_ERR(mnt))
8574 return NULL;
8575 mntget(mnt);
8576
8577 return mnt;
8578}
8579
7eeafbca
SRRH
8580/**
8581 * tracing_init_dentry - initialize top level trace array
8582 *
8583 * This is called when creating files or directories in the tracing
8584 * directory. It is called via fs_initcall() by any of the boot up code
8585 * and expects to return the dentry of the top level tracing directory.
8586 */
8587struct dentry *tracing_init_dentry(void)
8588{
8589 struct trace_array *tr = &global_trace;
8590
f76180bc 8591 /* The top level trace array uses NULL as parent */
7eeafbca 8592 if (tr->dir)
f76180bc 8593 return NULL;
7eeafbca 8594
8b129199
JW
8595 if (WARN_ON(!tracefs_initialized()) ||
8596 (IS_ENABLED(CONFIG_DEBUG_FS) &&
8597 WARN_ON(!debugfs_initialized())))
7eeafbca
SRRH
8598 return ERR_PTR(-ENODEV);
8599
f76180bc
SRRH
8600 /*
8601 * As there may still be users that expect the tracing
8602 * files to exist in debugfs/tracing, we must automount
8603 * the tracefs file system there, so older tools still
8604 * work with the newer kerenl.
8605 */
8606 tr->dir = debugfs_create_automount("tracing", NULL,
8607 trace_automount, NULL);
7eeafbca
SRRH
8608 if (!tr->dir) {
8609 pr_warn_once("Could not create debugfs directory 'tracing'\n");
8610 return ERR_PTR(-ENOMEM);
8611 }
8612
8434dc93 8613 return NULL;
7eeafbca
SRRH
8614}
8615
00f4b652
JL
8616extern struct trace_eval_map *__start_ftrace_eval_maps[];
8617extern struct trace_eval_map *__stop_ftrace_eval_maps[];
0c564a53 8618
5f60b351 8619static void __init trace_eval_init(void)
0c564a53 8620{
3673b8e4
SRRH
8621 int len;
8622
02fd7f68 8623 len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
f57a4143 8624 trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
3673b8e4
SRRH
8625}
8626
8627#ifdef CONFIG_MODULES
f57a4143 8628static void trace_module_add_evals(struct module *mod)
3673b8e4 8629{
99be647c 8630 if (!mod->num_trace_evals)
3673b8e4
SRRH
8631 return;
8632
8633 /*
8634 * Modules with bad taint do not have events created, do
8635 * not bother with enums either.
8636 */
8637 if (trace_module_has_bad_taint(mod))
8638 return;
8639
f57a4143 8640 trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
3673b8e4
SRRH
8641}
8642
681bec03 8643#ifdef CONFIG_TRACE_EVAL_MAP_FILE
f57a4143 8644static void trace_module_remove_evals(struct module *mod)
9828413d 8645{
23bf8cb8
JL
8646 union trace_eval_map_item *map;
8647 union trace_eval_map_item **last = &trace_eval_maps;
9828413d 8648
99be647c 8649 if (!mod->num_trace_evals)
9828413d
SRRH
8650 return;
8651
1793ed93 8652 mutex_lock(&trace_eval_mutex);
9828413d 8653
23bf8cb8 8654 map = trace_eval_maps;
9828413d
SRRH
8655
8656 while (map) {
8657 if (map->head.mod == mod)
8658 break;
5f60b351 8659 map = trace_eval_jmp_to_tail(map);
9828413d
SRRH
8660 last = &map->tail.next;
8661 map = map->tail.next;
8662 }
8663 if (!map)
8664 goto out;
8665
5f60b351 8666 *last = trace_eval_jmp_to_tail(map)->tail.next;
9828413d
SRRH
8667 kfree(map);
8668 out:
1793ed93 8669 mutex_unlock(&trace_eval_mutex);
9828413d
SRRH
8670}
8671#else
f57a4143 8672static inline void trace_module_remove_evals(struct module *mod) { }
681bec03 8673#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
9828413d 8674
3673b8e4
SRRH
8675static int trace_module_notify(struct notifier_block *self,
8676 unsigned long val, void *data)
8677{
8678 struct module *mod = data;
8679
8680 switch (val) {
8681 case MODULE_STATE_COMING:
f57a4143 8682 trace_module_add_evals(mod);
3673b8e4 8683 break;
9828413d 8684 case MODULE_STATE_GOING:
f57a4143 8685 trace_module_remove_evals(mod);
9828413d 8686 break;
3673b8e4
SRRH
8687 }
8688
8689 return 0;
0c564a53
SRRH
8690}
8691
3673b8e4
SRRH
8692static struct notifier_block trace_module_nb = {
8693 .notifier_call = trace_module_notify,
8694 .priority = 0,
8695};
9828413d 8696#endif /* CONFIG_MODULES */
3673b8e4 8697
8434dc93 8698static __init int tracer_init_tracefs(void)
bc0c38d1
SR
8699{
8700 struct dentry *d_tracer;
bc0c38d1 8701
7e53bd42
LJ
8702 trace_access_lock_init();
8703
bc0c38d1 8704 d_tracer = tracing_init_dentry();
14a5ae40 8705 if (IS_ERR(d_tracer))
ed6f1c99 8706 return 0;
bc0c38d1 8707
58b92547
SRV
8708 event_trace_init();
8709
8434dc93 8710 init_tracer_tracefs(&global_trace, d_tracer);
501c2375 8711 ftrace_init_tracefs_toplevel(&global_trace, d_tracer);
bc0c38d1 8712
5452af66 8713 trace_create_file("tracing_thresh", 0644, d_tracer,
6508fa76 8714 &global_trace, &tracing_thresh_fops);
a8259075 8715
339ae5d3 8716 trace_create_file("README", 0444, d_tracer,
5452af66
FW
8717 NULL, &tracing_readme_fops);
8718
69abe6a5
AP
8719 trace_create_file("saved_cmdlines", 0444, d_tracer,
8720 NULL, &tracing_saved_cmdlines_fops);
5bf9a1ee 8721
939c7a4f
YY
8722 trace_create_file("saved_cmdlines_size", 0644, d_tracer,
8723 NULL, &tracing_saved_cmdlines_size_fops);
8724
99c621d7
MS
8725 trace_create_file("saved_tgids", 0444, d_tracer,
8726 NULL, &tracing_saved_tgids_fops);
8727
5f60b351 8728 trace_eval_init();
0c564a53 8729
f57a4143 8730 trace_create_eval_file(d_tracer);
9828413d 8731
3673b8e4
SRRH
8732#ifdef CONFIG_MODULES
8733 register_module_notifier(&trace_module_nb);
8734#endif
8735
bc0c38d1 8736#ifdef CONFIG_DYNAMIC_FTRACE
5452af66
FW
8737 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
8738 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
bc0c38d1 8739#endif
b04cc6b1 8740
277ba044 8741 create_trace_instances(d_tracer);
5452af66 8742
37aea98b 8743 update_tracer_options(&global_trace);
09d23a1d 8744
b5ad384e 8745 return 0;
bc0c38d1
SR
8746}
8747
3f5a54e3
SR
8748static int trace_panic_handler(struct notifier_block *this,
8749 unsigned long event, void *unused)
8750{
944ac425 8751 if (ftrace_dump_on_oops)
cecbca96 8752 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
8753 return NOTIFY_OK;
8754}
8755
8756static struct notifier_block trace_panic_notifier = {
8757 .notifier_call = trace_panic_handler,
8758 .next = NULL,
8759 .priority = 150 /* priority: INT_MAX >= x >= 0 */
8760};
8761
8762static int trace_die_handler(struct notifier_block *self,
8763 unsigned long val,
8764 void *data)
8765{
8766 switch (val) {
8767 case DIE_OOPS:
944ac425 8768 if (ftrace_dump_on_oops)
cecbca96 8769 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
8770 break;
8771 default:
8772 break;
8773 }
8774 return NOTIFY_OK;
8775}
8776
8777static struct notifier_block trace_die_notifier = {
8778 .notifier_call = trace_die_handler,
8779 .priority = 200
8780};
8781
8782/*
8783 * printk is set to max of 1024, we really don't need it that big.
8784 * Nothing should be printing 1000 characters anyway.
8785 */
8786#define TRACE_MAX_PRINT 1000
8787
8788/*
8789 * Define here KERN_TRACE so that we have one place to modify
8790 * it if we decide to change what log level the ftrace dump
8791 * should be at.
8792 */
428aee14 8793#define KERN_TRACE KERN_EMERG
3f5a54e3 8794
955b61e5 8795void
3f5a54e3
SR
8796trace_printk_seq(struct trace_seq *s)
8797{
8798 /* Probably should print a warning here. */
3a161d99
SRRH
8799 if (s->seq.len >= TRACE_MAX_PRINT)
8800 s->seq.len = TRACE_MAX_PRINT;
3f5a54e3 8801
820b75f6
SRRH
8802 /*
8803 * More paranoid code. Although the buffer size is set to
8804 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
8805 * an extra layer of protection.
8806 */
8807 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
8808 s->seq.len = s->seq.size - 1;
3f5a54e3
SR
8809
8810 /* should be zero ended, but we are paranoid. */
3a161d99 8811 s->buffer[s->seq.len] = 0;
3f5a54e3
SR
8812
8813 printk(KERN_TRACE "%s", s->buffer);
8814
f9520750 8815 trace_seq_init(s);
3f5a54e3
SR
8816}
8817
955b61e5
JW
8818void trace_init_global_iter(struct trace_iterator *iter)
8819{
8820 iter->tr = &global_trace;
2b6080f2 8821 iter->trace = iter->tr->current_trace;
ae3b5093 8822 iter->cpu_file = RING_BUFFER_ALL_CPUS;
12883efb 8823 iter->trace_buffer = &global_trace.trace_buffer;
b2f974d6
CS
8824
8825 if (iter->trace && iter->trace->open)
8826 iter->trace->open(iter);
8827
8828 /* Annotate start of buffers if we had overruns */
8829 if (ring_buffer_overruns(iter->trace_buffer->buffer))
8830 iter->iter_flags |= TRACE_FILE_ANNOTATE;
8831
8832 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
8833 if (trace_clocks[iter->tr->clock_id].in_ns)
8834 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
955b61e5
JW
8835}
8836
7fe70b57 8837void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
3f5a54e3 8838{
3f5a54e3
SR
8839 /* use static because iter can be a bit big for the stack */
8840 static struct trace_iterator iter;
7fe70b57 8841 static atomic_t dump_running;
983f938a 8842 struct trace_array *tr = &global_trace;
cf586b61 8843 unsigned int old_userobj;
d769041f
SR
8844 unsigned long flags;
8845 int cnt = 0, cpu;
3f5a54e3 8846
7fe70b57
SRRH
8847 /* Only allow one dump user at a time. */
8848 if (atomic_inc_return(&dump_running) != 1) {
8849 atomic_dec(&dump_running);
8850 return;
8851 }
3f5a54e3 8852
7fe70b57
SRRH
8853 /*
8854 * Always turn off tracing when we dump.
8855 * We don't need to show trace output of what happens
8856 * between multiple crashes.
8857 *
8858 * If the user does a sysrq-z, then they can re-enable
8859 * tracing with echo 1 > tracing_on.
8860 */
0ee6b6cf 8861 tracing_off();
cf586b61 8862
7fe70b57 8863 local_irq_save(flags);
03fc7f9c 8864 printk_nmi_direct_enter();
3f5a54e3 8865
38dbe0b1 8866 /* Simulate the iterator */
955b61e5
JW
8867 trace_init_global_iter(&iter);
8868
d769041f 8869 for_each_tracing_cpu(cpu) {
5e2d5ef8 8870 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
d769041f
SR
8871 }
8872
983f938a 8873 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
cf586b61 8874
b54d3de9 8875 /* don't look at user memory in panic mode */
983f938a 8876 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
b54d3de9 8877
cecbca96
FW
8878 switch (oops_dump_mode) {
8879 case DUMP_ALL:
ae3b5093 8880 iter.cpu_file = RING_BUFFER_ALL_CPUS;
cecbca96
FW
8881 break;
8882 case DUMP_ORIG:
8883 iter.cpu_file = raw_smp_processor_id();
8884 break;
8885 case DUMP_NONE:
8886 goto out_enable;
8887 default:
8888 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
ae3b5093 8889 iter.cpu_file = RING_BUFFER_ALL_CPUS;
cecbca96
FW
8890 }
8891
8892 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3f5a54e3 8893
7fe70b57
SRRH
8894 /* Did function tracer already get disabled? */
8895 if (ftrace_is_dead()) {
8896 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
8897 printk("# MAY BE MISSING FUNCTION EVENTS\n");
8898 }
8899
3f5a54e3
SR
8900 /*
8901 * We need to stop all tracing on all CPUS to read the
8902 * the next buffer. This is a bit expensive, but is
8903 * not done often. We fill all what we can read,
8904 * and then release the locks again.
8905 */
8906
3f5a54e3
SR
8907 while (!trace_empty(&iter)) {
8908
8909 if (!cnt)
8910 printk(KERN_TRACE "---------------------------------\n");
8911
8912 cnt++;
8913
0c97bf86 8914 trace_iterator_reset(&iter);
3f5a54e3 8915 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3f5a54e3 8916
955b61e5 8917 if (trace_find_next_entry_inc(&iter) != NULL) {
74e7ff8c
LJ
8918 int ret;
8919
8920 ret = print_trace_line(&iter);
8921 if (ret != TRACE_TYPE_NO_CONSUME)
8922 trace_consume(&iter);
3f5a54e3 8923 }
b892e5c8 8924 touch_nmi_watchdog();
3f5a54e3
SR
8925
8926 trace_printk_seq(&iter.seq);
8927 }
8928
8929 if (!cnt)
8930 printk(KERN_TRACE " (ftrace buffer empty)\n");
8931 else
8932 printk(KERN_TRACE "---------------------------------\n");
8933
cecbca96 8934 out_enable:
983f938a 8935 tr->trace_flags |= old_userobj;
cf586b61 8936
7fe70b57
SRRH
8937 for_each_tracing_cpu(cpu) {
8938 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
cf586b61 8939 }
03fc7f9c
PM
8940 atomic_dec(&dump_running);
8941 printk_nmi_direct_exit();
cd891ae0 8942 local_irq_restore(flags);
3f5a54e3 8943}
a8eecf22 8944EXPORT_SYMBOL_GPL(ftrace_dump);
cf586b61 8945
7e465baa
TZ
8946int trace_run_command(const char *buf, int (*createfn)(int, char **))
8947{
8948 char **argv;
8949 int argc, ret;
8950
8951 argc = 0;
8952 ret = 0;
8953 argv = argv_split(GFP_KERNEL, buf, &argc);
8954 if (!argv)
8955 return -ENOMEM;
8956
8957 if (argc)
8958 ret = createfn(argc, argv);
8959
8960 argv_free(argv);
8961
8962 return ret;
8963}
8964
8965#define WRITE_BUFSIZE 4096
8966
8967ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
8968 size_t count, loff_t *ppos,
8969 int (*createfn)(int, char **))
8970{
8971 char *kbuf, *buf, *tmp;
8972 int ret = 0;
8973 size_t done = 0;
8974 size_t size;
8975
8976 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
8977 if (!kbuf)
8978 return -ENOMEM;
8979
8980 while (done < count) {
8981 size = count - done;
8982
8983 if (size >= WRITE_BUFSIZE)
8984 size = WRITE_BUFSIZE - 1;
8985
8986 if (copy_from_user(kbuf, buffer + done, size)) {
8987 ret = -EFAULT;
8988 goto out;
8989 }
8990 kbuf[size] = '\0';
8991 buf = kbuf;
8992 do {
8993 tmp = strchr(buf, '\n');
8994 if (tmp) {
8995 *tmp = '\0';
8996 size = tmp - buf + 1;
8997 } else {
8998 size = strlen(buf);
8999 if (done + size < count) {
9000 if (buf != kbuf)
9001 break;
9002 /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
9003 pr_warn("Line length is too long: Should be less than %d\n",
9004 WRITE_BUFSIZE - 2);
9005 ret = -EINVAL;
9006 goto out;
9007 }
9008 }
9009 done += size;
9010
9011 /* Remove comments */
9012 tmp = strchr(buf, '#');
9013
9014 if (tmp)
9015 *tmp = '\0';
9016
9017 ret = trace_run_command(buf, createfn);
9018 if (ret)
9019 goto out;
9020 buf += size;
9021
9022 } while (done < count);
9023 }
9024 ret = done;
9025
9026out:
9027 kfree(kbuf);
9028
9029 return ret;
9030}
9031
3928a8a2 9032__init static int tracer_alloc_buffers(void)
bc0c38d1 9033{
73c5162a 9034 int ring_buf_size;
9e01c1b7 9035 int ret = -ENOMEM;
4c11d7ae 9036
b5e87c05
SRRH
9037 /*
9038 * Make sure we don't accidently add more trace options
9039 * than we have bits for.
9040 */
9a38a885 9041 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
b5e87c05 9042
9e01c1b7
RR
9043 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
9044 goto out;
9045
ccfe9e42 9046 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
9e01c1b7 9047 goto out_free_buffer_mask;
4c11d7ae 9048
07d777fe
SR
9049 /* Only allocate trace_printk buffers if a trace_printk exists */
9050 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
81698831 9051 /* Must be called before global_trace.buffer is allocated */
07d777fe
SR
9052 trace_printk_init_buffers();
9053
73c5162a
SR
9054 /* To save memory, keep the ring buffer size to its minimum */
9055 if (ring_buffer_expanded)
9056 ring_buf_size = trace_buf_size;
9057 else
9058 ring_buf_size = 1;
9059
9e01c1b7 9060 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
ccfe9e42 9061 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
9e01c1b7 9062
2b6080f2
SR
9063 raw_spin_lock_init(&global_trace.start_lock);
9064
b32614c0
SAS
9065 /*
9066 * The prepare callbacks allocates some memory for the ring buffer. We
9067 * don't free the buffer if the if the CPU goes down. If we were to free
9068 * the buffer, then the user would lose any trace that was in the
9069 * buffer. The memory will be removed once the "instance" is removed.
9070 */
9071 ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
9072 "trace/RB:preapre", trace_rb_cpu_prepare,
9073 NULL);
9074 if (ret < 0)
9075 goto out_free_cpumask;
2c4a33ab 9076 /* Used for event triggers */
147d88e0 9077 ret = -ENOMEM;
2c4a33ab
SRRH
9078 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
9079 if (!temp_buffer)
b32614c0 9080 goto out_rm_hp_state;
2c4a33ab 9081
939c7a4f
YY
9082 if (trace_create_savedcmd() < 0)
9083 goto out_free_temp_buffer;
9084
9e01c1b7 9085 /* TODO: make the number of buffers hot pluggable with CPUS */
737223fb 9086 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
3928a8a2
SR
9087 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
9088 WARN_ON(1);
939c7a4f 9089 goto out_free_savedcmd;
4c11d7ae 9090 }
a7603ff4 9091
499e5470
SR
9092 if (global_trace.buffer_disabled)
9093 tracing_off();
4c11d7ae 9094
e1e232ca
SR
9095 if (trace_boot_clock) {
9096 ret = tracing_set_clock(&global_trace, trace_boot_clock);
9097 if (ret < 0)
a395d6a7
JP
9098 pr_warn("Trace clock %s not defined, going back to default\n",
9099 trace_boot_clock);
e1e232ca
SR
9100 }
9101
ca164318
SRRH
9102 /*
9103 * register_tracer() might reference current_trace, so it
9104 * needs to be set before we register anything. This is
9105 * just a bootstrap of current_trace anyway.
9106 */
2b6080f2
SR
9107 global_trace.current_trace = &nop_trace;
9108
0b9b12c1
SRRH
9109 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
9110
4104d326
SRRH
9111 ftrace_init_global_array_ops(&global_trace);
9112
9a38a885
SRRH
9113 init_trace_flags_index(&global_trace);
9114
ca164318
SRRH
9115 register_tracer(&nop_trace);
9116
dbeafd0d
SRV
9117 /* Function tracing may start here (via kernel command line) */
9118 init_function_trace();
9119
60a11774
SR
9120 /* All seems OK, enable tracing */
9121 tracing_disabled = 0;
3928a8a2 9122
3f5a54e3
SR
9123 atomic_notifier_chain_register(&panic_notifier_list,
9124 &trace_panic_notifier);
9125
9126 register_die_notifier(&trace_die_notifier);
2fc1dfbe 9127
ae63b31e
SR
9128 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
9129
9130 INIT_LIST_HEAD(&global_trace.systems);
9131 INIT_LIST_HEAD(&global_trace.events);
067fe038 9132 INIT_LIST_HEAD(&global_trace.hist_vars);
2f754e77 9133 INIT_LIST_HEAD(&global_trace.err_log);
ae63b31e
SR
9134 list_add(&global_trace.list, &ftrace_trace_arrays);
9135
a4d1e688 9136 apply_trace_boot_options();
7bcfaf54 9137
77fd5c15
SRRH
9138 register_snapshot_cmd();
9139
2fc1dfbe 9140 return 0;
3f5a54e3 9141
939c7a4f
YY
9142out_free_savedcmd:
9143 free_saved_cmdlines_buffer(savedcmd);
2c4a33ab
SRRH
9144out_free_temp_buffer:
9145 ring_buffer_free(temp_buffer);
b32614c0
SAS
9146out_rm_hp_state:
9147 cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
9e01c1b7 9148out_free_cpumask:
ccfe9e42 9149 free_cpumask_var(global_trace.tracing_cpumask);
9e01c1b7
RR
9150out_free_buffer_mask:
9151 free_cpumask_var(tracing_buffer_mask);
9152out:
9153 return ret;
bc0c38d1 9154}
b2821ae6 9155
e725c731 9156void __init early_trace_init(void)
5f893b26 9157{
0daa2302
SRRH
9158 if (tracepoint_printk) {
9159 tracepoint_print_iter =
9160 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
9161 if (WARN_ON(!tracepoint_print_iter))
9162 tracepoint_printk = 0;
42391745
SRRH
9163 else
9164 static_key_enable(&tracepoint_printk_key.key);
0daa2302 9165 }
5f893b26 9166 tracer_alloc_buffers();
e725c731
SRV
9167}
9168
9169void __init trace_init(void)
9170{
0c564a53 9171 trace_event_init();
5f893b26
SRRH
9172}
9173
b2821ae6
SR
9174__init static int clear_boot_tracer(void)
9175{
9176 /*
9177 * The default tracer at boot buffer is an init section.
9178 * This function is called in lateinit. If we did not
9179 * find the boot tracer, then clear it out, to prevent
9180 * later registration from accessing the buffer that is
9181 * about to be freed.
9182 */
9183 if (!default_bootup_tracer)
9184 return 0;
9185
9186 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
9187 default_bootup_tracer);
9188 default_bootup_tracer = NULL;
9189
9190 return 0;
9191}
9192
8434dc93 9193fs_initcall(tracer_init_tracefs);
4bb0f0e7 9194late_initcall_sync(clear_boot_tracer);
3fd49c9e
CW
9195
9196#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
9197__init static int tracing_set_default_clock(void)
9198{
9199 /* sched_clock_stable() is determined in late_initcall */
5125eee4 9200 if (!trace_boot_clock && !sched_clock_stable()) {
3fd49c9e
CW
9201 printk(KERN_WARNING
9202 "Unstable clock detected, switching default tracing clock to \"global\"\n"
9203 "If you want to keep using the local clock, then add:\n"
9204 " \"trace_clock=local\"\n"
9205 "on the kernel command line\n");
9206 tracing_set_clock(&global_trace, "global");
9207 }
9208
9209 return 0;
9210}
9211late_initcall_sync(tracing_set_default_clock);
9212#endif