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