blob: 1a41023a1f88e03cb6ee79e0b7c46991857e90ab [file] [log] [blame]
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001/*
2 * ring buffer based function tracer
3 *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005 * 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
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010012 * Copyright (C) 2004 Nadia Yvette Chambers
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020013 */
Steven Rostedt2cadf912008-12-01 22:20:19 -050014#include <linux/ring_buffer.h>
Sam Ravnborg273b2812009-10-18 00:52:28 +020015#include <generated/utsrelease.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050016#include <linux/stacktrace.h>
17#include <linux/writeback.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020018#include <linux/kallsyms.h>
19#include <linux/seq_file.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040020#include <linux/notifier.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050021#include <linux/irqflags.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020022#include <linux/debugfs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020023#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020024#include <linux/hardirq.h>
25#include <linux/linkage.h>
26#include <linux/uaccess.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050027#include <linux/kprobes.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020028#include <linux/ftrace.h>
29#include <linux/module.h>
30#include <linux/percpu.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050031#include <linux/splice.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040032#include <linux/kdebug.h>
Frederic Weisbecker5f0c6c02009-03-27 14:22:10 +010033#include <linux/string.h>
Lai Jiangshan7e53bd42010-01-06 20:08:50 +080034#include <linux/rwsem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020036#include <linux/ctype.h>
37#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020038#include <linux/poll.h>
Steven Rostedtb892e5c2012-03-01 22:06:48 -050039#include <linux/nmi.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020040#include <linux/fs.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060041#include <linux/sched/rt.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020042
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020043#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050044#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020045
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010046/*
Steven Rostedt73c51622009-03-11 13:42:01 -040047 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
49 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -050050bool ring_buffer_expanded;
Steven Rostedt73c51622009-03-11 13:42:01 -040051
52/*
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010053 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010054 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010056 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010057 * at the same time, giving false positive or negative results.
58 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010059static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010060
Steven Rostedtb2821ae2009-02-02 21:38:32 -050061/*
62 * If a tracer is running, we do not want to run SELFTEST.
63 */
Li Zefan020e5f82009-07-01 10:47:05 +080064bool __read_mostly tracing_selftest_disabled;
Steven Rostedtb2821ae2009-02-02 21:38:32 -050065
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010066/* For tracers that don't implement custom flags */
67static struct tracer_opt dummy_tracer_opt[] = {
68 { }
69};
70
71static struct tracer_flags dummy_tracer_flags = {
72 .val = 0,
73 .opts = dummy_tracer_opt
74};
75
76static int dummy_set_flag(u32 old_flags, u32 bit, int set)
77{
78 return 0;
79}
Steven Rostedt0f048702008-11-05 16:05:44 -050080
81/*
Steven Rostedt7ffbd482012-10-11 12:14:25 -040082 * To prevent the comm cache from being overwritten when no
83 * tracing is active, only save the comm when a trace event
84 * occurred.
85 */
86static DEFINE_PER_CPU(bool, trace_cmdline_save);
87
88/*
Steven Rostedt0f048702008-11-05 16:05:44 -050089 * Kill all tracing for good (never come back).
90 * It is initialized to 1 but will turn to zero if the initialization
91 * of the tracer is successful. But that is the only place that sets
92 * this back to zero.
93 */
Hannes Eder4fd27352009-02-10 19:44:12 +010094static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -050095
Christoph Lameter9288f992009-10-07 19:17:45 -040096DEFINE_PER_CPU(int, ftrace_cpu_disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -040097
Jason Wessel955b61e2010-08-05 09:22:23 -050098cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +020099
Steven Rostedt944ac422008-10-23 19:26:08 -0400100/*
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
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200111 * /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
Steven Rostedt944ac422008-10-23 19:26:08 -0400114 */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200115
116enum ftrace_dump_mode ftrace_dump_on_oops;
Steven Rostedt944ac422008-10-23 19:26:08 -0400117
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500118static int tracing_set_tracer(const char *buf);
119
Li Zefanee6c2c12009-09-18 14:06:47 +0800120#define MAX_TRACER_SIZE 100
121static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500122static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100123
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500124static bool allocate_snapshot;
125
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200126static int __init set_cmdline_ftrace(char *str)
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100127{
Chen Gang67012ab2013-04-08 12:06:44 +0800128 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500129 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400130 /* We are using ftrace early, expand it */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500131 ring_buffer_expanded = true;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100132 return 1;
133}
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200134__setup("ftrace=", set_cmdline_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100135
Steven Rostedt944ac422008-10-23 19:26:08 -0400136static int __init set_ftrace_dump_on_oops(char *str)
137{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200138 if (*str++ != '=' || !*str) {
139 ftrace_dump_on_oops = DUMP_ALL;
140 return 1;
141 }
142
143 if (!strcmp("orig_cpu", str)) {
144 ftrace_dump_on_oops = DUMP_ORIG;
145 return 1;
146 }
147
148 return 0;
Steven Rostedt944ac422008-10-23 19:26:08 -0400149}
150__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200151
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400152static int __init boot_alloc_snapshot(char *str)
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500153{
154 allocate_snapshot = true;
155 /* We also need the main ring buffer expanded */
156 ring_buffer_expanded = true;
157 return 1;
158}
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400159__setup("alloc_snapshot", boot_alloc_snapshot);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500160
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400161
162static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
163static char *trace_boot_options __initdata;
164
165static int __init set_trace_boot_options(char *str)
166{
Chen Gang67012ab2013-04-08 12:06:44 +0800167 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400168 trace_boot_options = trace_boot_options_buf;
169 return 0;
170}
171__setup("trace_options=", set_trace_boot_options);
172
Lai Jiangshancf8e3472009-03-30 13:48:00 +0800173unsigned long long ns2usecs(cycle_t nsec)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200174{
175 nsec += 500;
176 do_div(nsec, 1000);
177 return nsec;
178}
179
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200180/*
181 * The global_trace is the descriptor that holds the tracing
182 * buffers for the live tracing. For each CPU, it contains
183 * a link list of pages that will store trace entries. The
184 * page descriptor of the pages in the memory is used to hold
185 * the link list by linking the lru item in the page descriptor
186 * to each of the pages in the buffer per CPU.
187 *
188 * For each active CPU there is a data field that holds the
189 * pages for the buffer for that CPU. Each CPU has the same number
190 * of pages allocated for its buffer.
191 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200192static struct trace_array global_trace;
193
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400194LIST_HEAD(ftrace_trace_arrays);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200195
Steven Rostedte77405a2009-09-02 14:17:06 -0400196int filter_current_check_discard(struct ring_buffer *buffer,
197 struct ftrace_event_call *call, void *rec,
Tom Zanussieb02ce02009-04-08 03:15:54 -0500198 struct ring_buffer_event *event)
199{
Steven Rostedte77405a2009-09-02 14:17:06 -0400200 return filter_check_discard(call, rec, buffer, event);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500201}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400202EXPORT_SYMBOL_GPL(filter_current_check_discard);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500203
Steven Rostedt37886f62009-03-17 17:22:06 -0400204cycle_t ftrace_now(int cpu)
205{
206 u64 ts;
207
208 /* Early boot up does not have a buffer yet */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500209 if (!global_trace.trace_buffer.buffer)
Steven Rostedt37886f62009-03-17 17:22:06 -0400210 return trace_clock_local();
211
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500212 ts = ring_buffer_time_stamp(global_trace.trace_buffer.buffer, cpu);
213 ring_buffer_normalize_time_stamp(global_trace.trace_buffer.buffer, cpu, &ts);
Steven Rostedt37886f62009-03-17 17:22:06 -0400214
215 return ts;
216}
217
Steven Rostedt90369902008-11-05 16:05:44 -0500218int tracing_is_enabled(void)
219{
Steven Rostedt0fb96562012-05-11 14:25:30 -0400220 return tracing_is_on();
Steven Rostedt90369902008-11-05 16:05:44 -0500221}
222
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200223/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400224 * trace_buf_size is the size in bytes that is allocated
225 * for a buffer. Note, the number of bytes is always rounded
226 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400227 *
228 * This number is purposely set to a low number of 16384.
229 * If the dump on oops happens, it will be much appreciated
230 * to not have to wait for all that output. Anyway this can be
231 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200232 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400233#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400234
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400235static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200236
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200237/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200238static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200239
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200240/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200241 * trace_types_lock is used to protect the trace_types list.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200242 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200243static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200244
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800245/*
246 * serialize the access of the ring buffer
247 *
248 * ring buffer serializes readers, but it is low level protection.
249 * The validity of the events (which returns by ring_buffer_peek() ..etc)
250 * are not protected by ring buffer.
251 *
252 * The content of events may become garbage if we allow other process consumes
253 * these events concurrently:
254 * A) the page of the consumed events may become a normal page
255 * (not reader page) in ring buffer, and this page will be rewrited
256 * by events producer.
257 * B) The page of the consumed events may become a page for splice_read,
258 * and this page will be returned to system.
259 *
260 * These primitives allow multi process access to different cpu ring buffer
261 * concurrently.
262 *
263 * These primitives don't distinguish read-only and read-consume access.
264 * Multi read-only access are also serialized.
265 */
266
267#ifdef CONFIG_SMP
268static DECLARE_RWSEM(all_cpu_access_lock);
269static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
270
271static inline void trace_access_lock(int cpu)
272{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500273 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800274 /* gain it for accessing the whole ring buffer. */
275 down_write(&all_cpu_access_lock);
276 } else {
277 /* gain it for accessing a cpu ring buffer. */
278
Steven Rostedtae3b5092013-01-23 15:22:59 -0500279 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800280 down_read(&all_cpu_access_lock);
281
282 /* Secondly block other access to this @cpu ring buffer. */
283 mutex_lock(&per_cpu(cpu_access_lock, cpu));
284 }
285}
286
287static inline void trace_access_unlock(int cpu)
288{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500289 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800290 up_write(&all_cpu_access_lock);
291 } else {
292 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
293 up_read(&all_cpu_access_lock);
294 }
295}
296
297static inline void trace_access_lock_init(void)
298{
299 int cpu;
300
301 for_each_possible_cpu(cpu)
302 mutex_init(&per_cpu(cpu_access_lock, cpu));
303}
304
305#else
306
307static DEFINE_MUTEX(access_lock);
308
309static inline void trace_access_lock(int cpu)
310{
311 (void)cpu;
312 mutex_lock(&access_lock);
313}
314
315static inline void trace_access_unlock(int cpu)
316{
317 (void)cpu;
318 mutex_unlock(&access_lock);
319}
320
321static inline void trace_access_lock_init(void)
322{
323}
324
325#endif
326
Steven Rostedtee6bce52008-11-12 17:52:37 -0500327/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500328unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Steven Rostedta2a16d62009-03-24 23:17:58 -0400329 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
Steven Rostedt77271ce2011-11-17 09:34:33 -0500330 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400331 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
Vaibhav Nagarnaike7e2ee82011-05-10 13:27:21 -0700332
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200333/**
Steven Rostedt499e5472012-02-22 15:50:28 -0500334 * tracing_on - enable tracing buffers
335 *
336 * This function enables tracing buffers that may have been
337 * disabled with tracing_off.
338 */
339void tracing_on(void)
340{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500341 if (global_trace.trace_buffer.buffer)
342 ring_buffer_record_on(global_trace.trace_buffer.buffer);
Steven Rostedt499e5472012-02-22 15:50:28 -0500343 /*
344 * This flag is only looked at when buffers haven't been
345 * allocated yet. We don't really care about the race
346 * between setting this flag and actually turning
347 * on the buffer.
348 */
349 global_trace.buffer_disabled = 0;
350}
351EXPORT_SYMBOL_GPL(tracing_on);
352
353/**
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500354 * __trace_puts - write a constant string into the trace buffer.
355 * @ip: The address of the caller
356 * @str: The constant string to write
357 * @size: The size of the string.
358 */
359int __trace_puts(unsigned long ip, const char *str, int size)
360{
361 struct ring_buffer_event *event;
362 struct ring_buffer *buffer;
363 struct print_entry *entry;
364 unsigned long irq_flags;
365 int alloc;
366
367 alloc = sizeof(*entry) + size + 2; /* possible \n added */
368
369 local_save_flags(irq_flags);
370 buffer = global_trace.trace_buffer.buffer;
371 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
372 irq_flags, preempt_count());
373 if (!event)
374 return 0;
375
376 entry = ring_buffer_event_data(event);
377 entry->ip = ip;
378
379 memcpy(&entry->buf, str, size);
380
381 /* Add a newline if necessary */
382 if (entry->buf[size - 1] != '\n') {
383 entry->buf[size] = '\n';
384 entry->buf[size + 1] = '\0';
385 } else
386 entry->buf[size] = '\0';
387
388 __buffer_unlock_commit(buffer, event);
389
390 return size;
391}
392EXPORT_SYMBOL_GPL(__trace_puts);
393
394/**
395 * __trace_bputs - write the pointer to a constant string into trace buffer
396 * @ip: The address of the caller
397 * @str: The constant string to write to the buffer to
398 */
399int __trace_bputs(unsigned long ip, const char *str)
400{
401 struct ring_buffer_event *event;
402 struct ring_buffer *buffer;
403 struct bputs_entry *entry;
404 unsigned long irq_flags;
405 int size = sizeof(struct bputs_entry);
406
407 local_save_flags(irq_flags);
408 buffer = global_trace.trace_buffer.buffer;
409 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
410 irq_flags, preempt_count());
411 if (!event)
412 return 0;
413
414 entry = ring_buffer_event_data(event);
415 entry->ip = ip;
416 entry->str = str;
417
418 __buffer_unlock_commit(buffer, event);
419
420 return 1;
421}
422EXPORT_SYMBOL_GPL(__trace_bputs);
423
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500424#ifdef CONFIG_TRACER_SNAPSHOT
425/**
426 * trace_snapshot - take a snapshot of the current buffer.
427 *
428 * This causes a swap between the snapshot buffer and the current live
429 * tracing buffer. You can use this to take snapshots of the live
430 * trace when some condition is triggered, but continue to trace.
431 *
432 * Note, make sure to allocate the snapshot with either
433 * a tracing_snapshot_alloc(), or by doing it manually
434 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
435 *
436 * If the snapshot buffer is not allocated, it will stop tracing.
437 * Basically making a permanent snapshot.
438 */
439void tracing_snapshot(void)
440{
441 struct trace_array *tr = &global_trace;
442 struct tracer *tracer = tr->current_trace;
443 unsigned long flags;
444
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500445 if (in_nmi()) {
446 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
447 internal_trace_puts("*** snapshot is being ignored ***\n");
448 return;
449 }
450
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500451 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500452 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
453 internal_trace_puts("*** stopping trace here! ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500454 tracing_off();
455 return;
456 }
457
458 /* Note, snapshot can not be used when the tracer uses it */
459 if (tracer->use_max_tr) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500460 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
461 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500462 return;
463 }
464
465 local_irq_save(flags);
466 update_max_tr(tr, current, smp_processor_id());
467 local_irq_restore(flags);
468}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500469EXPORT_SYMBOL_GPL(tracing_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500470
471static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
472 struct trace_buffer *size_buf, int cpu_id);
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400473static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
474
475static int alloc_snapshot(struct trace_array *tr)
476{
477 int ret;
478
479 if (!tr->allocated_snapshot) {
480
481 /* allocate spare buffer */
482 ret = resize_buffer_duplicate_size(&tr->max_buffer,
483 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
484 if (ret < 0)
485 return ret;
486
487 tr->allocated_snapshot = true;
488 }
489
490 return 0;
491}
492
493void free_snapshot(struct trace_array *tr)
494{
495 /*
496 * We don't free the ring buffer. instead, resize it because
497 * The max_tr ring buffer has some state (e.g. ring->clock) and
498 * we want preserve it.
499 */
500 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
501 set_buffer_entries(&tr->max_buffer, 1);
502 tracing_reset_online_cpus(&tr->max_buffer);
503 tr->allocated_snapshot = false;
504}
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500505
506/**
507 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
508 *
509 * This is similar to trace_snapshot(), but it will allocate the
510 * snapshot buffer if it isn't already allocated. Use this only
511 * where it is safe to sleep, as the allocation may sleep.
512 *
513 * This causes a swap between the snapshot buffer and the current live
514 * tracing buffer. You can use this to take snapshots of the live
515 * trace when some condition is triggered, but continue to trace.
516 */
517void tracing_snapshot_alloc(void)
518{
519 struct trace_array *tr = &global_trace;
520 int ret;
521
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400522 ret = alloc_snapshot(tr);
523 if (WARN_ON(ret < 0))
524 return;
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500525
526 tracing_snapshot();
527}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500528EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500529#else
530void tracing_snapshot(void)
531{
532 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
533}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500534EXPORT_SYMBOL_GPL(tracing_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500535void tracing_snapshot_alloc(void)
536{
537 /* Give warning */
538 tracing_snapshot();
539}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500540EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500541#endif /* CONFIG_TRACER_SNAPSHOT */
542
Steven Rostedt499e5472012-02-22 15:50:28 -0500543/**
544 * tracing_off - turn off tracing buffers
545 *
546 * This function stops the tracing buffers from recording data.
547 * It does not disable any overhead the tracers themselves may
548 * be causing. This function simply causes all recording to
549 * the ring buffers to fail.
550 */
551void tracing_off(void)
552{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500553 if (global_trace.trace_buffer.buffer)
554 ring_buffer_record_off(global_trace.trace_buffer.buffer);
Steven Rostedt499e5472012-02-22 15:50:28 -0500555 /*
556 * This flag is only looked at when buffers haven't been
557 * allocated yet. We don't really care about the race
558 * between setting this flag and actually turning
559 * on the buffer.
560 */
561 global_trace.buffer_disabled = 1;
562}
563EXPORT_SYMBOL_GPL(tracing_off);
564
565/**
566 * tracing_is_on - show state of ring buffers enabled
567 */
568int tracing_is_on(void)
569{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500570 if (global_trace.trace_buffer.buffer)
571 return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
Steven Rostedt499e5472012-02-22 15:50:28 -0500572 return !global_trace.buffer_disabled;
573}
574EXPORT_SYMBOL_GPL(tracing_is_on);
575
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400576static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200577{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400578 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200579
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200580 if (!str)
581 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +0800582 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200583 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +0800584 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200585 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400586 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200587 return 1;
588}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400589__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200590
Tim Bird0e950172010-02-25 15:36:43 -0800591static int __init set_tracing_thresh(char *str)
592{
Wang Tianhong87abb3b2012-08-02 14:02:00 +0800593 unsigned long threshold;
Tim Bird0e950172010-02-25 15:36:43 -0800594 int ret;
595
596 if (!str)
597 return 0;
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200598 ret = kstrtoul(str, 0, &threshold);
Tim Bird0e950172010-02-25 15:36:43 -0800599 if (ret < 0)
600 return 0;
Wang Tianhong87abb3b2012-08-02 14:02:00 +0800601 tracing_thresh = threshold * 1000;
Tim Bird0e950172010-02-25 15:36:43 -0800602 return 1;
603}
604__setup("tracing_thresh=", set_tracing_thresh);
605
Steven Rostedt57f50be2008-05-12 21:20:44 +0200606unsigned long nsecs_to_usecs(unsigned long nsecs)
607{
608 return nsecs / 1000;
609}
610
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200611/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200612static const char *trace_options[] = {
613 "print-parent",
614 "sym-offset",
615 "sym-addr",
616 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200617 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200618 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200619 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200620 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200621 "stacktrace",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100622 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500623 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500624 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500625 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200626 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200627 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100628 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200629 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500630 "latency-format",
Steven Rostedtbe6f1642009-03-24 11:06:24 -0400631 "sleep-time",
Steven Rostedta2a16d62009-03-24 23:17:58 -0400632 "graph-time",
Li Zefane870e9a2010-07-02 11:07:32 +0800633 "record-cmd",
David Sharp750912f2010-12-08 13:46:47 -0800634 "overwrite",
Steven Rostedtcf30cf62011-06-14 22:44:07 -0400635 "disable_on_free",
Steven Rostedt77271ce2011-11-17 09:34:33 -0500636 "irq-info",
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -0700637 "markers",
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400638 "function-trace",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200639 NULL
640};
641
Zhaolei5079f322009-08-25 16:12:56 +0800642static struct {
643 u64 (*func)(void);
644 const char *name;
David Sharp8be07092012-11-13 12:18:22 -0800645 int in_ns; /* is this clock in nanoseconds? */
Zhaolei5079f322009-08-25 16:12:56 +0800646} trace_clocks[] = {
David Sharp8be07092012-11-13 12:18:22 -0800647 { trace_clock_local, "local", 1 },
648 { trace_clock_global, "global", 1 },
649 { trace_clock_counter, "counter", 0 },
Steven Rostedt (Red Hat)8aacf012013-03-14 13:13:45 -0400650 { trace_clock_jiffies, "uptime", 1 },
Steven Rostedt (Red Hat)76f11912013-03-14 17:53:25 -0400651 { trace_clock, "perf", 1 },
David Sharp8cbd9cc2012-11-13 12:18:21 -0800652 ARCH_TRACE_CLOCKS
Zhaolei5079f322009-08-25 16:12:56 +0800653};
654
655int trace_clock_id;
656
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200657/*
658 * trace_parser_get_init - gets the buffer for trace parser
659 */
660int trace_parser_get_init(struct trace_parser *parser, int size)
661{
662 memset(parser, 0, sizeof(*parser));
663
664 parser->buffer = kmalloc(size, GFP_KERNEL);
665 if (!parser->buffer)
666 return 1;
667
668 parser->size = size;
669 return 0;
670}
671
672/*
673 * trace_parser_put - frees the buffer for trace parser
674 */
675void trace_parser_put(struct trace_parser *parser)
676{
677 kfree(parser->buffer);
678}
679
680/*
681 * trace_get_user - reads the user input string separated by space
682 * (matched by isspace(ch))
683 *
684 * For each string found the 'struct trace_parser' is updated,
685 * and the function returns.
686 *
687 * Returns number of bytes read.
688 *
689 * See kernel/trace/trace.h for 'struct trace_parser' details.
690 */
691int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
692 size_t cnt, loff_t *ppos)
693{
694 char ch;
695 size_t read = 0;
696 ssize_t ret;
697
698 if (!*ppos)
699 trace_parser_clear(parser);
700
701 ret = get_user(ch, ubuf++);
702 if (ret)
703 goto out;
704
705 read++;
706 cnt--;
707
708 /*
709 * The parser is not finished with the last write,
710 * continue reading the user input without skipping spaces.
711 */
712 if (!parser->cont) {
713 /* skip white space */
714 while (cnt && isspace(ch)) {
715 ret = get_user(ch, ubuf++);
716 if (ret)
717 goto out;
718 read++;
719 cnt--;
720 }
721
722 /* only spaces were written */
723 if (isspace(ch)) {
724 *ppos += read;
725 ret = read;
726 goto out;
727 }
728
729 parser->idx = 0;
730 }
731
732 /* read the non-space input */
733 while (cnt && !isspace(ch)) {
Li Zefan3c235a32009-09-22 13:51:54 +0800734 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200735 parser->buffer[parser->idx++] = ch;
736 else {
737 ret = -EINVAL;
738 goto out;
739 }
740 ret = get_user(ch, ubuf++);
741 if (ret)
742 goto out;
743 read++;
744 cnt--;
745 }
746
747 /* We either got finished input or we have to wait for another call. */
748 if (isspace(ch)) {
749 parser->buffer[parser->idx] = 0;
750 parser->cont = false;
751 } else {
752 parser->cont = true;
753 parser->buffer[parser->idx++] = ch;
754 }
755
756 *ppos += read;
757 ret = read;
758
759out:
760 return ret;
761}
762
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200763ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
764{
765 int len;
766 int ret;
767
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500768 if (!cnt)
769 return 0;
770
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200771 if (s->len <= s->readpos)
772 return -EBUSY;
773
774 len = s->len - s->readpos;
775 if (cnt > len)
776 cnt = len;
777 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500778 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200779 return -EFAULT;
780
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500781 cnt -= ret;
782
Steven Rostedte74da522009-03-04 20:31:11 -0500783 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200784 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200785}
786
Dmitri Vorobievb8b94262009-03-22 19:11:11 +0200787static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200788{
789 int len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200790
791 if (s->len <= s->readpos)
792 return -EBUSY;
793
794 len = s->len - s->readpos;
795 if (cnt > len)
796 cnt = len;
Dan Carpenter5a26c8f2012-04-20 09:31:45 +0300797 memcpy(buf, s->buffer + s->readpos, cnt);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200798
Steven Rostedte74da522009-03-04 20:31:11 -0500799 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200800 return cnt;
801}
802
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400803/*
804 * ftrace_max_lock is used to protect the swapping of buffers
805 * when taking a max snapshot. The buffers themselves are
806 * protected by per_cpu spinlocks. But the action of the swap
807 * needs its own lock.
808 *
Thomas Gleixner445c8952009-12-02 19:49:50 +0100809 * This is defined as a arch_spinlock_t in order to help
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400810 * with performance when lockdep debugging is enabled.
811 *
812 * It is also used in other places outside the update_max_tr
813 * so it needs to be defined outside of the
814 * CONFIG_TRACER_MAX_TRACE.
815 */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100816static arch_spinlock_t ftrace_max_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100817 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400818
Tim Bird0e950172010-02-25 15:36:43 -0800819unsigned long __read_mostly tracing_thresh;
820
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400821#ifdef CONFIG_TRACER_MAX_TRACE
822unsigned long __read_mostly tracing_max_latency;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400823
824/*
825 * Copy the new maximum trace into the separate maximum-trace
826 * structure. (this way the maximum trace is permanently saved,
827 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
828 */
829static void
830__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
831{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500832 struct trace_buffer *trace_buf = &tr->trace_buffer;
833 struct trace_buffer *max_buf = &tr->max_buffer;
834 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
835 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400836
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500837 max_buf->cpu = cpu;
838 max_buf->time_start = data->preempt_timestamp;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400839
Steven Rostedt8248ac02009-09-02 12:27:41 -0400840 max_data->saved_latency = tracing_max_latency;
841 max_data->critical_start = data->critical_start;
842 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400843
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300844 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -0400845 max_data->pid = tsk->pid;
Steven Rostedt (Red Hat)f17a5192013-05-30 21:10:37 -0400846 /*
847 * If tsk == current, then use current_uid(), as that does not use
848 * RCU. The irq tracer can be called out of RCU scope.
849 */
850 if (tsk == current)
851 max_data->uid = current_uid();
852 else
853 max_data->uid = task_uid(tsk);
854
Steven Rostedt8248ac02009-09-02 12:27:41 -0400855 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
856 max_data->policy = tsk->policy;
857 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400858
859 /* record this tasks comm */
860 tracing_record_cmdline(tsk);
861}
862
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200863/**
864 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
865 * @tr: tracer
866 * @tsk: the task with the latency
867 * @cpu: The cpu that initiated the trace.
868 *
869 * Flip the buffers between the @tr and the max_tr and record information
870 * about which task was the cause of this latency.
871 */
Ingo Molnare309b412008-05-12 21:20:51 +0200872void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200873update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
874{
Steven Rostedt (Red Hat)2721e722013-03-12 11:32:32 -0400875 struct ring_buffer *buf;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200876
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400877 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400878 return;
879
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200880 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt34600f02013-01-22 13:35:11 -0500881
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -0500882 if (!tr->allocated_snapshot) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +0900883 /* Only the nop tracer should hit this when disabling */
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400884 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900885 return;
Hiraku Toyookadebdd572012-12-26 11:53:00 +0900886 }
Steven Rostedt34600f02013-01-22 13:35:11 -0500887
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100888 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400889
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500890 buf = tr->trace_buffer.buffer;
891 tr->trace_buffer.buffer = tr->max_buffer.buffer;
892 tr->max_buffer.buffer = buf;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400893
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200894 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100895 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200896}
897
898/**
899 * update_max_tr_single - only copy one trace over, and reset the rest
900 * @tr - tracer
901 * @tsk - task with the latency
902 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200903 *
904 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200905 */
Ingo Molnare309b412008-05-12 21:20:51 +0200906void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200907update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
908{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400909 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200910
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400911 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400912 return;
913
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200914 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt6c244992013-04-29 20:08:14 -0400915 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -0400916 /* Only the nop tracer should hit this when disabling */
Linus Torvalds9e8529a2013-04-29 13:55:38 -0700917 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900918 return;
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -0400919 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900920
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100921 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200922
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500923 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400924
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400925 if (ret == -EBUSY) {
926 /*
927 * We failed to swap the buffer due to a commit taking
928 * place on this CPU. We fail to record, but we reset
929 * the max trace buffer (no one writes directly to it)
930 * and flag that it failed.
931 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500932 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400933 "Failed to swap buffers due to commit in progress\n");
934 }
935
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400936 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200937
938 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100939 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200940}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400941#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200942
Steven Rostedt0d5c6e12012-11-01 20:54:21 -0400943static void default_wait_pipe(struct trace_iterator *iter)
944{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500945 /* Iterators are static, they should be filled or empty */
946 if (trace_buffer_iter(iter, iter->cpu_file))
947 return;
Steven Rostedt0d5c6e12012-11-01 20:54:21 -0400948
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500949 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt0d5c6e12012-11-01 20:54:21 -0400950}
951
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -0500952#ifdef CONFIG_FTRACE_STARTUP_TEST
953static int run_tracer_selftest(struct tracer *type)
954{
955 struct trace_array *tr = &global_trace;
956 struct tracer *saved_tracer = tr->current_trace;
957 int ret;
958
959 if (!type->selftest || tracing_selftest_disabled)
960 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200961
962 /*
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -0500963 * Run a selftest on this tracer.
964 * Here we reset the trace buffer, and set the current
965 * tracer to be this tracer. The tracer can then run some
966 * internal tracing to verify that everything is in order.
967 * If we fail, we do not register this tracer.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200968 */
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -0500969 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200970
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -0500971 tr->current_trace = type;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200972
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -0500973#ifdef CONFIG_TRACER_MAX_TRACE
974 if (type->use_max_tr) {
975 /* If we expanded the buffers, make sure the max is expanded too */
976 if (ring_buffer_expanded)
977 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
978 RING_BUFFER_ALL_CPUS);
979 tr->allocated_snapshot = true;
980 }
981#endif
982
983 /* the test is responsible for initializing and enabling */
984 pr_info("Testing tracer %s: ", type->name);
985 ret = type->selftest(type, tr);
986 /* the test is responsible for resetting too */
987 tr->current_trace = saved_tracer;
988 if (ret) {
989 printk(KERN_CONT "FAILED!\n");
990 /* Add the warning after printing 'FAILED' */
991 WARN_ON(1);
992 return -1;
993 }
994 /* Only reset on passing, to avoid touching corrupted buffers */
995 tracing_reset_online_cpus(&tr->trace_buffer);
996
997#ifdef CONFIG_TRACER_MAX_TRACE
998 if (type->use_max_tr) {
999 tr->allocated_snapshot = false;
1000
1001 /* Shrink the max buffer again */
1002 if (ring_buffer_expanded)
1003 ring_buffer_resize(tr->max_buffer.buffer, 1,
1004 RING_BUFFER_ALL_CPUS);
1005 }
1006#endif
1007
1008 printk(KERN_CONT "PASSED\n");
1009 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001010}
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001011#else
1012static inline int run_tracer_selftest(struct tracer *type)
1013{
1014 return 0;
1015}
1016#endif /* CONFIG_FTRACE_STARTUP_TEST */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001017
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001018/**
1019 * register_tracer - register a tracer with the ftrace system.
1020 * @type - the plugin for the tracer
1021 *
1022 * Register a new plugin tracer.
1023 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001024int register_tracer(struct tracer *type)
1025{
1026 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001027 int ret = 0;
1028
1029 if (!type->name) {
1030 pr_info("Tracer must have a name\n");
1031 return -1;
1032 }
1033
Dan Carpenter24a461d2010-07-10 12:06:44 +02001034 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +08001035 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1036 return -1;
1037 }
1038
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001039 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +01001040
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001041 tracing_selftest_running = true;
1042
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001043 for (t = trace_types; t; t = t->next) {
1044 if (strcmp(type->name, t->name) == 0) {
1045 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +08001046 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001047 type->name);
1048 ret = -1;
1049 goto out;
1050 }
1051 }
1052
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001053 if (!type->set_flag)
1054 type->set_flag = &dummy_set_flag;
1055 if (!type->flags)
1056 type->flags = &dummy_tracer_flags;
1057 else
1058 if (!type->flags->opts)
1059 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001060 if (!type->wait_pipe)
1061 type->wait_pipe = default_wait_pipe;
1062
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001063 ret = run_tracer_selftest(type);
1064 if (ret < 0)
1065 goto out;
Steven Rostedt60a11772008-05-12 21:20:44 +02001066
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001067 type->next = trace_types;
1068 trace_types = type;
Steven Rostedt60a11772008-05-12 21:20:44 +02001069
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001070 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001071 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001072 mutex_unlock(&trace_types_lock);
1073
Steven Rostedtdac74942009-02-05 01:13:38 -05001074 if (ret || !default_bootup_tracer)
1075 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -05001076
Li Zefanee6c2c12009-09-18 14:06:47 +08001077 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -05001078 goto out_unlock;
1079
1080 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1081 /* Do we want this tracer to start on bootup? */
1082 tracing_set_tracer(type->name);
1083 default_bootup_tracer = NULL;
1084 /* disable other selftests, since this will break it. */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05001085 tracing_selftest_disabled = true;
Steven Rostedtdac74942009-02-05 01:13:38 -05001086#ifdef CONFIG_FTRACE_STARTUP_TEST
1087 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1088 type->name);
1089#endif
1090
1091 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001092 return ret;
1093}
1094
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001095void tracing_reset(struct trace_buffer *buf, int cpu)
Steven Rostedtf6339032009-09-04 12:35:16 -04001096{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001097 struct ring_buffer *buffer = buf->buffer;
Steven Rostedtf6339032009-09-04 12:35:16 -04001098
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001099 if (!buffer)
1100 return;
1101
Steven Rostedtf6339032009-09-04 12:35:16 -04001102 ring_buffer_record_disable(buffer);
1103
1104 /* Make sure all commits have finished */
1105 synchronize_sched();
Steven Rostedt68179682012-05-08 20:57:53 -04001106 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -04001107
1108 ring_buffer_record_enable(buffer);
1109}
1110
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001111void tracing_reset_online_cpus(struct trace_buffer *buf)
Pekka J Enberg213cc062008-12-19 12:08:39 +02001112{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001113 struct ring_buffer *buffer = buf->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +02001114 int cpu;
1115
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001116 if (!buffer)
1117 return;
1118
Steven Rostedt621968c2009-09-04 12:02:35 -04001119 ring_buffer_record_disable(buffer);
1120
1121 /* Make sure all commits have finished */
1122 synchronize_sched();
1123
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001124 buf->time_start = ftrace_now(buf->cpu);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001125
1126 for_each_online_cpu(cpu)
Steven Rostedt68179682012-05-08 20:57:53 -04001127 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -04001128
1129 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001130}
1131
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001132void tracing_reset_current(int cpu)
1133{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001134 tracing_reset(&global_trace.trace_buffer, cpu);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001135}
1136
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001137void tracing_reset_all_online_cpus(void)
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001138{
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001139 struct trace_array *tr;
1140
1141 mutex_lock(&trace_types_lock);
1142 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001143 tracing_reset_online_cpus(&tr->trace_buffer);
1144#ifdef CONFIG_TRACER_MAX_TRACE
1145 tracing_reset_online_cpus(&tr->max_buffer);
1146#endif
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001147 }
1148 mutex_unlock(&trace_types_lock);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001149}
1150
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001151#define SAVED_CMDLINES 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001152#define NO_CMDLINE_MAP UINT_MAX
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001153static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1154static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1155static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1156static int cmdline_idx;
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001157static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +02001158
Steven Rostedt25b0b442008-05-12 21:21:00 +02001159/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +01001160static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001161
1162static void trace_init_cmdlines(void)
1163{
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001164 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1165 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001166 cmdline_idx = 0;
1167}
1168
Carsten Emdeb5130b12009-09-13 01:43:07 +02001169int is_tracing_stopped(void)
1170{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001171 return global_trace.stop_count;
Carsten Emdeb5130b12009-09-13 01:43:07 +02001172}
1173
Steven Rostedt0f048702008-11-05 16:05:44 -05001174/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -05001175 * ftrace_off_permanent - disable all ftrace code permanently
1176 *
1177 * This should only be called when a serious anomally has
1178 * been detected. This will turn off the function tracing,
1179 * ring buffers, and other tracing utilites. It takes no
1180 * locks and can be called from any context.
1181 */
1182void ftrace_off_permanent(void)
1183{
1184 tracing_disabled = 1;
1185 ftrace_stop();
1186 tracing_off_permanent();
1187}
1188
1189/**
Steven Rostedt0f048702008-11-05 16:05:44 -05001190 * tracing_start - quick start of the tracer
1191 *
1192 * If tracing is enabled but was stopped by tracing_stop,
1193 * this will start the tracer back up.
1194 */
1195void tracing_start(void)
1196{
1197 struct ring_buffer *buffer;
1198 unsigned long flags;
1199
1200 if (tracing_disabled)
1201 return;
1202
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001203 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1204 if (--global_trace.stop_count) {
1205 if (global_trace.stop_count < 0) {
Steven Rostedtb06a8302009-01-22 14:26:15 -05001206 /* Someone screwed up their debugging */
1207 WARN_ON_ONCE(1);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001208 global_trace.stop_count = 0;
Steven Rostedtb06a8302009-01-22 14:26:15 -05001209 }
Steven Rostedt0f048702008-11-05 16:05:44 -05001210 goto out;
1211 }
1212
Steven Rostedta2f80712010-03-12 19:56:00 -05001213 /* Prevent the buffers from switching */
1214 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -05001215
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001216 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001217 if (buffer)
1218 ring_buffer_record_enable(buffer);
1219
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001220#ifdef CONFIG_TRACER_MAX_TRACE
1221 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001222 if (buffer)
1223 ring_buffer_record_enable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001224#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001225
Steven Rostedta2f80712010-03-12 19:56:00 -05001226 arch_spin_unlock(&ftrace_max_lock);
1227
Steven Rostedt0f048702008-11-05 16:05:44 -05001228 ftrace_start();
1229 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001230 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1231}
1232
1233static void tracing_start_tr(struct trace_array *tr)
1234{
1235 struct ring_buffer *buffer;
1236 unsigned long flags;
1237
1238 if (tracing_disabled)
1239 return;
1240
1241 /* If global, we need to also start the max tracer */
1242 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1243 return tracing_start();
1244
1245 raw_spin_lock_irqsave(&tr->start_lock, flags);
1246
1247 if (--tr->stop_count) {
1248 if (tr->stop_count < 0) {
1249 /* Someone screwed up their debugging */
1250 WARN_ON_ONCE(1);
1251 tr->stop_count = 0;
1252 }
1253 goto out;
1254 }
1255
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001256 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001257 if (buffer)
1258 ring_buffer_record_enable(buffer);
1259
1260 out:
1261 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001262}
1263
1264/**
1265 * tracing_stop - quick stop of the tracer
1266 *
1267 * Light weight way to stop tracing. Use in conjunction with
1268 * tracing_start.
1269 */
1270void tracing_stop(void)
1271{
1272 struct ring_buffer *buffer;
1273 unsigned long flags;
1274
1275 ftrace_stop();
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001276 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1277 if (global_trace.stop_count++)
Steven Rostedt0f048702008-11-05 16:05:44 -05001278 goto out;
1279
Steven Rostedta2f80712010-03-12 19:56:00 -05001280 /* Prevent the buffers from switching */
1281 arch_spin_lock(&ftrace_max_lock);
1282
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001283 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001284 if (buffer)
1285 ring_buffer_record_disable(buffer);
1286
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001287#ifdef CONFIG_TRACER_MAX_TRACE
1288 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001289 if (buffer)
1290 ring_buffer_record_disable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001291#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001292
Steven Rostedta2f80712010-03-12 19:56:00 -05001293 arch_spin_unlock(&ftrace_max_lock);
1294
Steven Rostedt0f048702008-11-05 16:05:44 -05001295 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001296 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1297}
1298
1299static void tracing_stop_tr(struct trace_array *tr)
1300{
1301 struct ring_buffer *buffer;
1302 unsigned long flags;
1303
1304 /* If global, we need to also stop the max tracer */
1305 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1306 return tracing_stop();
1307
1308 raw_spin_lock_irqsave(&tr->start_lock, flags);
1309 if (tr->stop_count++)
1310 goto out;
1311
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001312 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001313 if (buffer)
1314 ring_buffer_record_disable(buffer);
1315
1316 out:
1317 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001318}
1319
Ingo Molnare309b412008-05-12 21:20:51 +02001320void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001321
Ingo Molnare309b412008-05-12 21:20:51 +02001322static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001323{
Carsten Emdea635cf02009-03-18 09:00:41 +01001324 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001325
1326 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1327 return;
1328
1329 /*
1330 * It's not the end of the world if we don't get
1331 * the lock, but we also don't want to spin
1332 * nor do we want to disable interrupts,
1333 * so if we miss here, then better luck next time.
1334 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001335 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001336 return;
1337
1338 idx = map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001339 if (idx == NO_CMDLINE_MAP) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001340 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1341
Carsten Emdea635cf02009-03-18 09:00:41 +01001342 /*
1343 * Check whether the cmdline buffer at idx has a pid
1344 * mapped. We are going to overwrite that entry so we
1345 * need to clear the map_pid_to_cmdline. Otherwise we
1346 * would read the new comm for the old pid.
1347 */
1348 pid = map_cmdline_to_pid[idx];
1349 if (pid != NO_CMDLINE_MAP)
1350 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001351
Carsten Emdea635cf02009-03-18 09:00:41 +01001352 map_cmdline_to_pid[idx] = tsk->pid;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001353 map_pid_to_cmdline[tsk->pid] = idx;
1354
1355 cmdline_idx = idx;
1356 }
1357
1358 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1359
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001360 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001361}
1362
Steven Rostedt4ca530852009-03-16 19:20:15 -04001363void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001364{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001365 unsigned map;
1366
Steven Rostedt4ca530852009-03-16 19:20:15 -04001367 if (!pid) {
1368 strcpy(comm, "<idle>");
1369 return;
1370 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001371
Steven Rostedt74bf4072010-01-25 15:11:53 -05001372 if (WARN_ON_ONCE(pid < 0)) {
1373 strcpy(comm, "<XXX>");
1374 return;
1375 }
1376
Steven Rostedt4ca530852009-03-16 19:20:15 -04001377 if (pid > PID_MAX_DEFAULT) {
1378 strcpy(comm, "<...>");
1379 return;
1380 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001381
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001382 preempt_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001383 arch_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001384 map = map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01001385 if (map != NO_CMDLINE_MAP)
1386 strcpy(comm, saved_cmdlines[map]);
1387 else
1388 strcpy(comm, "<...>");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001389
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001390 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001391 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001392}
1393
Ingo Molnare309b412008-05-12 21:20:51 +02001394void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001395{
Steven Rostedt0fb96562012-05-11 14:25:30 -04001396 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001397 return;
1398
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001399 if (!__this_cpu_read(trace_cmdline_save))
1400 return;
1401
1402 __this_cpu_write(trace_cmdline_save, false);
1403
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001404 trace_save_cmdline(tsk);
1405}
1406
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03001407void
Steven Rostedt38697052008-10-01 13:14:09 -04001408tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1409 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001410{
1411 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001412
Steven Rostedt777e2082008-09-29 23:02:42 -04001413 entry->preempt_count = pc & 0xff;
1414 entry->pid = (tsk) ? tsk->pid : 0;
1415 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04001416#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001417 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04001418#else
1419 TRACE_FLAG_IRQS_NOSUPPORT |
1420#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001421 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1422 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1423 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1424}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02001425EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001426
Steven Rostedte77405a2009-09-02 14:17:06 -04001427struct ring_buffer_event *
1428trace_buffer_lock_reserve(struct ring_buffer *buffer,
1429 int type,
1430 unsigned long len,
1431 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001432{
1433 struct ring_buffer_event *event;
1434
Steven Rostedte77405a2009-09-02 14:17:06 -04001435 event = ring_buffer_lock_reserve(buffer, len);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001436 if (event != NULL) {
1437 struct trace_entry *ent = ring_buffer_event_data(event);
1438
1439 tracing_generic_entry_update(ent, flags, pc);
1440 ent->type = type;
1441 }
1442
1443 return event;
1444}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001445
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001446void
1447__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1448{
1449 __this_cpu_write(trace_cmdline_save, true);
1450 ring_buffer_unlock_commit(buffer, event);
1451}
1452
Steven Rostedte77405a2009-09-02 14:17:06 -04001453static inline void
1454__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1455 struct ring_buffer_event *event,
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001456 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001457{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001458 __buffer_unlock_commit(buffer, event);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001459
Steven Rostedte77405a2009-09-02 14:17:06 -04001460 ftrace_trace_stack(buffer, flags, 6, pc);
1461 ftrace_trace_userstack(buffer, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001462}
1463
Steven Rostedte77405a2009-09-02 14:17:06 -04001464void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1465 struct ring_buffer_event *event,
1466 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001467{
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001468 __trace_buffer_unlock_commit(buffer, event, flags, pc);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001469}
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001470EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001471
Steven Rostedtef5580d2009-02-27 19:38:04 -05001472struct ring_buffer_event *
Steven Rostedtccb469a2012-08-02 10:32:10 -04001473trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1474 struct ftrace_event_file *ftrace_file,
1475 int type, unsigned long len,
1476 unsigned long flags, int pc)
1477{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001478 *current_rb = ftrace_file->tr->trace_buffer.buffer;
Steven Rostedtccb469a2012-08-02 10:32:10 -04001479 return trace_buffer_lock_reserve(*current_rb,
1480 type, len, flags, pc);
1481}
1482EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1483
1484struct ring_buffer_event *
Steven Rostedte77405a2009-09-02 14:17:06 -04001485trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1486 int type, unsigned long len,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001487 unsigned long flags, int pc)
1488{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001489 *current_rb = global_trace.trace_buffer.buffer;
Steven Rostedte77405a2009-09-02 14:17:06 -04001490 return trace_buffer_lock_reserve(*current_rb,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001491 type, len, flags, pc);
1492}
Steven Rostedt94487d62009-05-05 19:22:53 -04001493EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001494
Steven Rostedte77405a2009-09-02 14:17:06 -04001495void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1496 struct ring_buffer_event *event,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001497 unsigned long flags, int pc)
1498{
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001499 __trace_buffer_unlock_commit(buffer, event, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001500}
Steven Rostedt94487d62009-05-05 19:22:53 -04001501EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001502
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001503void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1504 struct ring_buffer_event *event,
1505 unsigned long flags, int pc,
1506 struct pt_regs *regs)
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001507{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001508 __buffer_unlock_commit(buffer, event);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001509
1510 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1511 ftrace_trace_userstack(buffer, flags, pc);
1512}
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001513EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001514
Steven Rostedte77405a2009-09-02 14:17:06 -04001515void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1516 struct ring_buffer_event *event)
Steven Rostedt77d9f462009-04-02 01:16:59 -04001517{
Steven Rostedte77405a2009-09-02 14:17:06 -04001518 ring_buffer_discard_commit(buffer, event);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001519}
Steven Rostedt12acd472009-04-17 16:01:56 -04001520EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001521
Ingo Molnare309b412008-05-12 21:20:51 +02001522void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001523trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001524 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1525 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001526{
Tom Zanussie1112b42009-03-31 00:48:49 -05001527 struct ftrace_event_call *call = &event_function;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001528 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001529 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001530 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001531
Steven Rostedtd7690412008-10-01 00:29:53 -04001532 /* If we are reading the ring buffer, don't trace */
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001533 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04001534 return;
1535
Steven Rostedte77405a2009-09-02 14:17:06 -04001536 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001537 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001538 if (!event)
1539 return;
1540 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001541 entry->ip = ip;
1542 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05001543
Steven Rostedte77405a2009-09-02 14:17:06 -04001544 if (!filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001545 __buffer_unlock_commit(buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001546}
1547
Ingo Molnare309b412008-05-12 21:20:51 +02001548void
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001549ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -04001550 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1551 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001552{
1553 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001554 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001555}
1556
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001557#ifdef CONFIG_STACKTRACE
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001558
1559#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1560struct ftrace_stack {
1561 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1562};
1563
1564static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1565static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1566
Steven Rostedte77405a2009-09-02 14:17:06 -04001567static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05001568 unsigned long flags,
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001569 int skip, int pc, struct pt_regs *regs)
Ingo Molnar86387f72008-05-12 21:20:51 +02001570{
Tom Zanussie1112b42009-03-31 00:48:49 -05001571 struct ftrace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001572 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001573 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02001574 struct stack_trace trace;
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001575 int use_stack;
1576 int size = FTRACE_STACK_ENTRIES;
Ingo Molnar86387f72008-05-12 21:20:51 +02001577
1578 trace.nr_entries = 0;
Ingo Molnar86387f72008-05-12 21:20:51 +02001579 trace.skip = skip;
Ingo Molnar86387f72008-05-12 21:20:51 +02001580
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001581 /*
1582 * Since events can happen in NMIs there's no safe way to
1583 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1584 * or NMI comes in, it will just have to use the default
1585 * FTRACE_STACK_SIZE.
1586 */
1587 preempt_disable_notrace();
1588
Shan Wei82146522012-11-19 13:21:01 +08001589 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001590 /*
1591 * We don't need any atomic variables, just a barrier.
1592 * If an interrupt comes in, we don't care, because it would
1593 * have exited and put the counter back to what we want.
1594 * We just need a barrier to keep gcc from moving things
1595 * around.
1596 */
1597 barrier();
1598 if (use_stack == 1) {
1599 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1600 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1601
1602 if (regs)
1603 save_stack_trace_regs(regs, &trace);
1604 else
1605 save_stack_trace(&trace);
1606
1607 if (trace.nr_entries > size)
1608 size = trace.nr_entries;
1609 } else
1610 /* From now on, use_stack is a boolean */
1611 use_stack = 0;
1612
1613 size *= sizeof(unsigned long);
1614
1615 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1616 sizeof(*entry) + size, flags, pc);
1617 if (!event)
1618 goto out;
1619 entry = ring_buffer_event_data(event);
1620
1621 memset(&entry->caller, 0, size);
1622
1623 if (use_stack)
1624 memcpy(&entry->caller, trace.entries,
1625 trace.nr_entries * sizeof(unsigned long));
1626 else {
1627 trace.max_entries = FTRACE_STACK_ENTRIES;
1628 trace.entries = entry->caller;
1629 if (regs)
1630 save_stack_trace_regs(regs, &trace);
1631 else
1632 save_stack_trace(&trace);
1633 }
1634
1635 entry->size = trace.nr_entries;
1636
Steven Rostedte77405a2009-09-02 14:17:06 -04001637 if (!filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001638 __buffer_unlock_commit(buffer, event);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001639
1640 out:
1641 /* Again, don't let gcc optimize things here */
1642 barrier();
Shan Wei82146522012-11-19 13:21:01 +08001643 __this_cpu_dec(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001644 preempt_enable_notrace();
1645
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001646}
1647
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001648void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1649 int skip, int pc, struct pt_regs *regs)
1650{
1651 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1652 return;
1653
1654 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1655}
1656
Steven Rostedte77405a2009-09-02 14:17:06 -04001657void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1658 int skip, int pc)
Steven Rostedt53614992009-01-15 19:12:40 -05001659{
1660 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1661 return;
1662
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001663 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
Steven Rostedt53614992009-01-15 19:12:40 -05001664}
1665
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001666void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1667 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04001668{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001669 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
Steven Rostedt38697052008-10-01 13:14:09 -04001670}
1671
Steven Rostedt03889382009-12-11 09:48:22 -05001672/**
1673 * trace_dump_stack - record a stack back trace in the trace buffer
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04001674 * @skip: Number of functions to skip (helper handlers)
Steven Rostedt03889382009-12-11 09:48:22 -05001675 */
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04001676void trace_dump_stack(int skip)
Steven Rostedt03889382009-12-11 09:48:22 -05001677{
1678 unsigned long flags;
1679
1680 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05001681 return;
Steven Rostedt03889382009-12-11 09:48:22 -05001682
1683 local_save_flags(flags);
1684
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04001685 /*
1686 * Skip 3 more, seems to get us at the caller of
1687 * this function.
1688 */
1689 skip += 3;
1690 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1691 flags, skip, preempt_count(), NULL);
Steven Rostedt03889382009-12-11 09:48:22 -05001692}
1693
Steven Rostedt91e86e52010-11-10 12:56:12 +01001694static DEFINE_PER_CPU(int, user_stack_count);
1695
Steven Rostedte77405a2009-09-02 14:17:06 -04001696void
1697ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02001698{
Tom Zanussie1112b42009-03-31 00:48:49 -05001699 struct ftrace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02001700 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001701 struct userstack_entry *entry;
1702 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001703
1704 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1705 return;
1706
Steven Rostedtb6345872010-03-12 20:03:30 -05001707 /*
1708 * NMIs can not handle page faults, even with fix ups.
1709 * The save user stack can (and often does) fault.
1710 */
1711 if (unlikely(in_nmi()))
1712 return;
1713
Steven Rostedt91e86e52010-11-10 12:56:12 +01001714 /*
1715 * prevent recursion, since the user stack tracing may
1716 * trigger other kernel events.
1717 */
1718 preempt_disable();
1719 if (__this_cpu_read(user_stack_count))
1720 goto out;
1721
1722 __this_cpu_inc(user_stack_count);
1723
Steven Rostedte77405a2009-09-02 14:17:06 -04001724 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001725 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001726 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08001727 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02001728 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001729
Steven Rostedt48659d32009-09-11 11:36:23 -04001730 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02001731 memset(&entry->caller, 0, sizeof(entry->caller));
1732
1733 trace.nr_entries = 0;
1734 trace.max_entries = FTRACE_STACK_ENTRIES;
1735 trace.skip = 0;
1736 trace.entries = entry->caller;
1737
1738 save_stack_trace_user(&trace);
Steven Rostedte77405a2009-09-02 14:17:06 -04001739 if (!filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001740 __buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001741
Li Zefan1dbd1952010-12-09 15:47:56 +08001742 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01001743 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001744 out:
1745 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02001746}
1747
Hannes Eder4fd27352009-02-10 19:44:12 +01001748#ifdef UNUSED
1749static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001750{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001751 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001752}
Hannes Eder4fd27352009-02-10 19:44:12 +01001753#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001754
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001755#endif /* CONFIG_STACKTRACE */
1756
Steven Rostedt07d777f2011-09-22 14:01:55 -04001757/* created for use with alloc_percpu */
1758struct trace_buffer_struct {
1759 char buffer[TRACE_BUF_SIZE];
1760};
1761
1762static struct trace_buffer_struct *trace_percpu_buffer;
1763static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1764static struct trace_buffer_struct *trace_percpu_irq_buffer;
1765static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1766
1767/*
1768 * The buffer used is dependent on the context. There is a per cpu
1769 * buffer for normal context, softirq contex, hard irq context and
1770 * for NMI context. Thise allows for lockless recording.
1771 *
1772 * Note, if the buffers failed to be allocated, then this returns NULL
1773 */
1774static char *get_trace_buf(void)
1775{
1776 struct trace_buffer_struct *percpu_buffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04001777
1778 /*
1779 * If we have allocated per cpu buffers, then we do not
1780 * need to do any locking.
1781 */
1782 if (in_nmi())
1783 percpu_buffer = trace_percpu_nmi_buffer;
1784 else if (in_irq())
1785 percpu_buffer = trace_percpu_irq_buffer;
1786 else if (in_softirq())
1787 percpu_buffer = trace_percpu_sirq_buffer;
1788 else
1789 percpu_buffer = trace_percpu_buffer;
1790
1791 if (!percpu_buffer)
1792 return NULL;
1793
Shan Weid8a03492012-11-13 09:53:04 +08001794 return this_cpu_ptr(&percpu_buffer->buffer[0]);
Steven Rostedt07d777f2011-09-22 14:01:55 -04001795}
1796
1797static int alloc_percpu_trace_buffer(void)
1798{
1799 struct trace_buffer_struct *buffers;
1800 struct trace_buffer_struct *sirq_buffers;
1801 struct trace_buffer_struct *irq_buffers;
1802 struct trace_buffer_struct *nmi_buffers;
1803
1804 buffers = alloc_percpu(struct trace_buffer_struct);
1805 if (!buffers)
1806 goto err_warn;
1807
1808 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1809 if (!sirq_buffers)
1810 goto err_sirq;
1811
1812 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1813 if (!irq_buffers)
1814 goto err_irq;
1815
1816 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1817 if (!nmi_buffers)
1818 goto err_nmi;
1819
1820 trace_percpu_buffer = buffers;
1821 trace_percpu_sirq_buffer = sirq_buffers;
1822 trace_percpu_irq_buffer = irq_buffers;
1823 trace_percpu_nmi_buffer = nmi_buffers;
1824
1825 return 0;
1826
1827 err_nmi:
1828 free_percpu(irq_buffers);
1829 err_irq:
1830 free_percpu(sirq_buffers);
1831 err_sirq:
1832 free_percpu(buffers);
1833 err_warn:
1834 WARN(1, "Could not allocate percpu trace_printk buffer");
1835 return -ENOMEM;
1836}
1837
Steven Rostedt81698832012-10-11 10:15:05 -04001838static int buffers_allocated;
1839
Steven Rostedt07d777f2011-09-22 14:01:55 -04001840void trace_printk_init_buffers(void)
1841{
Steven Rostedt07d777f2011-09-22 14:01:55 -04001842 if (buffers_allocated)
1843 return;
1844
1845 if (alloc_percpu_trace_buffer())
1846 return;
1847
1848 pr_info("ftrace: Allocated trace_printk buffers\n");
1849
Steven Rostedtb382ede62012-10-10 21:44:34 -04001850 /* Expand the buffers to set size */
1851 tracing_update_buffers();
1852
Steven Rostedt07d777f2011-09-22 14:01:55 -04001853 buffers_allocated = 1;
Steven Rostedt81698832012-10-11 10:15:05 -04001854
1855 /*
1856 * trace_printk_init_buffers() can be called by modules.
1857 * If that happens, then we need to start cmdline recording
1858 * directly here. If the global_trace.buffer is already
1859 * allocated here, then this was called by module code.
1860 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001861 if (global_trace.trace_buffer.buffer)
Steven Rostedt81698832012-10-11 10:15:05 -04001862 tracing_start_cmdline_record();
1863}
1864
1865void trace_printk_start_comm(void)
1866{
1867 /* Start tracing comms if trace printk is set */
1868 if (!buffers_allocated)
1869 return;
1870 tracing_start_cmdline_record();
1871}
1872
1873static void trace_printk_start_stop_comm(int enabled)
1874{
1875 if (!buffers_allocated)
1876 return;
1877
1878 if (enabled)
1879 tracing_start_cmdline_record();
1880 else
1881 tracing_stop_cmdline_record();
Steven Rostedt07d777f2011-09-22 14:01:55 -04001882}
1883
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001884/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001885 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001886 *
1887 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001888int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001889{
Tom Zanussie1112b42009-03-31 00:48:49 -05001890 struct ftrace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001891 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001892 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001893 struct trace_array *tr = &global_trace;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001894 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001895 unsigned long flags;
Steven Rostedt07d777f2011-09-22 14:01:55 -04001896 char *tbuffer;
1897 int len = 0, size, pc;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001898
1899 if (unlikely(tracing_selftest_running || tracing_disabled))
1900 return 0;
1901
1902 /* Don't pollute graph traces with trace_vprintk internals */
1903 pause_graph_tracing();
1904
1905 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04001906 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001907
Steven Rostedt07d777f2011-09-22 14:01:55 -04001908 tbuffer = get_trace_buf();
1909 if (!tbuffer) {
1910 len = 0;
1911 goto out;
1912 }
1913
1914 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1915
1916 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001917 goto out;
1918
Steven Rostedt07d777f2011-09-22 14:01:55 -04001919 local_save_flags(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001920 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001921 buffer = tr->trace_buffer.buffer;
Steven Rostedte77405a2009-09-02 14:17:06 -04001922 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1923 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001924 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04001925 goto out;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001926 entry = ring_buffer_event_data(event);
1927 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001928 entry->fmt = fmt;
1929
Steven Rostedt07d777f2011-09-22 14:01:55 -04001930 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
Steven Rostedtd9313692010-01-06 17:27:11 -05001931 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001932 __buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05001933 ftrace_trace_stack(buffer, flags, 6, pc);
1934 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001935
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001936out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04001937 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001938 unpause_graph_tracing();
1939
1940 return len;
1941}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001942EXPORT_SYMBOL_GPL(trace_vbprintk);
1943
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001944static int
1945__trace_array_vprintk(struct ring_buffer *buffer,
1946 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001947{
Tom Zanussie1112b42009-03-31 00:48:49 -05001948 struct ftrace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001949 struct ring_buffer_event *event;
Steven Rostedt07d777f2011-09-22 14:01:55 -04001950 int len = 0, size, pc;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001951 struct print_entry *entry;
Steven Rostedt07d777f2011-09-22 14:01:55 -04001952 unsigned long flags;
1953 char *tbuffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001954
1955 if (tracing_disabled || tracing_selftest_running)
1956 return 0;
1957
Steven Rostedt07d777f2011-09-22 14:01:55 -04001958 /* Don't pollute graph traces with trace_vprintk internals */
1959 pause_graph_tracing();
1960
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001961 pc = preempt_count();
1962 preempt_disable_notrace();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001963
Steven Rostedt07d777f2011-09-22 14:01:55 -04001964
1965 tbuffer = get_trace_buf();
1966 if (!tbuffer) {
1967 len = 0;
1968 goto out;
1969 }
1970
1971 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1972 if (len > TRACE_BUF_SIZE)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001973 goto out;
1974
Steven Rostedt07d777f2011-09-22 14:01:55 -04001975 local_save_flags(flags);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001976 size = sizeof(*entry) + len + 1;
Steven Rostedte77405a2009-09-02 14:17:06 -04001977 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
Steven Rostedt07d777f2011-09-22 14:01:55 -04001978 flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001979 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04001980 goto out;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001981 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001982 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001983
Steven Rostedt07d777f2011-09-22 14:01:55 -04001984 memcpy(&entry->buf, tbuffer, len);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001985 entry->buf[len] = '\0';
Steven Rostedtd9313692010-01-06 17:27:11 -05001986 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001987 __buffer_unlock_commit(buffer, event);
Steven Rostedt07d777f2011-09-22 14:01:55 -04001988 ftrace_trace_stack(buffer, flags, 6, pc);
Steven Rostedtd9313692010-01-06 17:27:11 -05001989 }
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001990 out:
1991 preempt_enable_notrace();
Steven Rostedt07d777f2011-09-22 14:01:55 -04001992 unpause_graph_tracing();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001993
1994 return len;
1995}
Steven Rostedt659372d2009-09-03 19:11:07 -04001996
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001997int trace_array_vprintk(struct trace_array *tr,
1998 unsigned long ip, const char *fmt, va_list args)
1999{
2000 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2001}
2002
2003int trace_array_printk(struct trace_array *tr,
2004 unsigned long ip, const char *fmt, ...)
2005{
2006 int ret;
2007 va_list ap;
2008
2009 if (!(trace_flags & TRACE_ITER_PRINTK))
2010 return 0;
2011
2012 va_start(ap, fmt);
2013 ret = trace_array_vprintk(tr, ip, fmt, ap);
2014 va_end(ap);
2015 return ret;
2016}
2017
2018int trace_array_printk_buf(struct ring_buffer *buffer,
2019 unsigned long ip, const char *fmt, ...)
2020{
2021 int ret;
2022 va_list ap;
2023
2024 if (!(trace_flags & TRACE_ITER_PRINTK))
2025 return 0;
2026
2027 va_start(ap, fmt);
2028 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2029 va_end(ap);
2030 return ret;
2031}
2032
Steven Rostedt659372d2009-09-03 19:11:07 -04002033int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2034{
Steven Rostedta813a152009-10-09 01:41:35 -04002035 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04002036}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002037EXPORT_SYMBOL_GPL(trace_vprintk);
2038
Robert Richtere2ac8ef2008-11-12 12:59:32 +01002039static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04002040{
Steven Rostedt6d158a82012-06-27 20:46:14 -04002041 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2042
Steven Rostedt5a90f572008-09-03 17:42:51 -04002043 iter->idx++;
Steven Rostedt6d158a82012-06-27 20:46:14 -04002044 if (buf_iter)
2045 ring_buffer_read(buf_iter, NULL);
Steven Rostedt5a90f572008-09-03 17:42:51 -04002046}
2047
Ingo Molnare309b412008-05-12 21:20:51 +02002048static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04002049peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2050 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002051{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002052 struct ring_buffer_event *event;
Steven Rostedt6d158a82012-06-27 20:46:14 -04002053 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002054
Steven Rostedtd7690412008-10-01 00:29:53 -04002055 if (buf_iter)
2056 event = ring_buffer_iter_peek(buf_iter, ts);
2057 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002058 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04002059 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04002060
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002061 if (event) {
2062 iter->ent_size = ring_buffer_event_length(event);
2063 return ring_buffer_event_data(event);
2064 }
2065 iter->ent_size = 0;
2066 return NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002067}
Steven Rostedtd7690412008-10-01 00:29:53 -04002068
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002069static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04002070__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2071 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002072{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002073 struct ring_buffer *buffer = iter->trace_buffer->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002074 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08002075 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002076 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002077 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002078 int next_cpu = -1;
Steven Rostedt12b5da32012-03-27 10:43:28 -04002079 int next_size = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002080 int cpu;
2081
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002082 /*
2083 * If we are in a per_cpu trace file, don't bother by iterating over
2084 * all cpu and peek directly.
2085 */
Steven Rostedtae3b5092013-01-23 15:22:59 -05002086 if (cpu_file > RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002087 if (ring_buffer_empty_cpu(buffer, cpu_file))
2088 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002089 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002090 if (ent_cpu)
2091 *ent_cpu = cpu_file;
2092
2093 return ent;
2094 }
2095
Steven Rostedtab464282008-05-12 21:21:00 +02002096 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002097
2098 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002099 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002100
Steven Rostedtbc21b472010-03-31 19:49:26 -04002101 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002102
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02002103 /*
2104 * Pick the entry with the smallest timestamp:
2105 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002106 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002107 next = ent;
2108 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002109 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002110 next_lost = lost_events;
Steven Rostedt12b5da32012-03-27 10:43:28 -04002111 next_size = iter->ent_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002112 }
2113 }
2114
Steven Rostedt12b5da32012-03-27 10:43:28 -04002115 iter->ent_size = next_size;
2116
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002117 if (ent_cpu)
2118 *ent_cpu = next_cpu;
2119
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002120 if (ent_ts)
2121 *ent_ts = next_ts;
2122
Steven Rostedtbc21b472010-03-31 19:49:26 -04002123 if (missing_events)
2124 *missing_events = next_lost;
2125
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002126 return next;
2127}
2128
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002129/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002130struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2131 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002132{
Steven Rostedtbc21b472010-03-31 19:49:26 -04002133 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002134}
Ingo Molnar8c523a92008-05-12 21:20:46 +02002135
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002136/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05002137void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002138{
Steven Rostedtbc21b472010-03-31 19:49:26 -04002139 iter->ent = __find_next_entry(iter, &iter->cpu,
2140 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002141
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002142 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01002143 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002144
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002145 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002146}
2147
Ingo Molnare309b412008-05-12 21:20:51 +02002148static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002149{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002150 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04002151 &iter->lost_events);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002152}
2153
Ingo Molnare309b412008-05-12 21:20:51 +02002154static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002155{
2156 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002157 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002158 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002159
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002160 WARN_ON_ONCE(iter->leftover);
2161
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002162 (*pos)++;
2163
2164 /* can't go backwards */
2165 if (iter->idx > i)
2166 return NULL;
2167
2168 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05002169 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002170 else
2171 ent = iter;
2172
2173 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05002174 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002175
2176 iter->pos = *pos;
2177
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002178 return ent;
2179}
2180
Jason Wessel955b61e2010-08-05 09:22:23 -05002181void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002182{
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002183 struct ring_buffer_event *event;
2184 struct ring_buffer_iter *buf_iter;
2185 unsigned long entries = 0;
2186 u64 ts;
2187
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002188 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002189
Steven Rostedt6d158a82012-06-27 20:46:14 -04002190 buf_iter = trace_buffer_iter(iter, cpu);
2191 if (!buf_iter)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002192 return;
2193
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002194 ring_buffer_iter_reset(buf_iter);
2195
2196 /*
2197 * We could have the case with the max latency tracers
2198 * that a reset never took place on a cpu. This is evident
2199 * by the timestamp being before the start of the buffer.
2200 */
2201 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002202 if (ts >= iter->trace_buffer->time_start)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002203 break;
2204 entries++;
2205 ring_buffer_read(buf_iter, NULL);
2206 }
2207
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002208 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002209}
2210
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002211/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002212 * The current tracer is copied to avoid a global locking
2213 * all around.
2214 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002215static void *s_start(struct seq_file *m, loff_t *pos)
2216{
2217 struct trace_iterator *iter = m->private;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002218 struct trace_array *tr = iter->tr;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002219 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002220 void *p = NULL;
2221 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002222 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002223
Hiraku Toyooka2fd196e2012-12-26 11:52:52 +09002224 /*
2225 * copy the tracer to avoid using a global lock all around.
2226 * iter->trace is a copy of current_trace, the pointer to the
2227 * name may be used instead of a strcmp(), as iter->trace->name
2228 * will point to the same string as current_trace->name.
2229 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002230 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002231 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2232 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002233 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002234
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002235#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002236 if (iter->snapshot && iter->trace->use_max_tr)
2237 return ERR_PTR(-EBUSY);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002238#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002239
2240 if (!iter->snapshot)
2241 atomic_inc(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002242
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002243 if (*pos != iter->pos) {
2244 iter->ent = NULL;
2245 iter->cpu = 0;
2246 iter->idx = -1;
2247
Steven Rostedtae3b5092013-01-23 15:22:59 -05002248 if (cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002249 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002250 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002251 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002252 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002253
Lai Jiangshanac91d852010-03-02 17:54:50 +08002254 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002255 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2256 ;
2257
2258 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002259 /*
2260 * If we overflowed the seq_file before, then we want
2261 * to just reuse the trace_seq buffer again.
2262 */
2263 if (iter->leftover)
2264 p = iter;
2265 else {
2266 l = *pos - 1;
2267 p = s_next(m, p, &l);
2268 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002269 }
2270
Lai Jiangshan4f535962009-05-18 19:35:34 +08002271 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002272 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002273 return p;
2274}
2275
2276static void s_stop(struct seq_file *m, void *p)
2277{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002278 struct trace_iterator *iter = m->private;
2279
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002280#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002281 if (iter->snapshot && iter->trace->use_max_tr)
2282 return;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002283#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002284
2285 if (!iter->snapshot)
2286 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002287
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002288 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08002289 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002290}
2291
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002292static void
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002293get_total_entries(struct trace_buffer *buf,
2294 unsigned long *total, unsigned long *entries)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002295{
2296 unsigned long count;
2297 int cpu;
2298
2299 *total = 0;
2300 *entries = 0;
2301
2302 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002303 count = ring_buffer_entries_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002304 /*
2305 * If this buffer has skipped entries, then we hold all
2306 * entries for the trace and we need to ignore the
2307 * ones before the time stamp.
2308 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002309 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2310 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002311 /* total is the same as the entries */
2312 *total += count;
2313 } else
2314 *total += count +
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002315 ring_buffer_overrun_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002316 *entries += count;
2317 }
2318}
2319
Ingo Molnare309b412008-05-12 21:20:51 +02002320static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002321{
Michael Ellermana6168352008-08-20 16:36:11 -07002322 seq_puts(m, "# _------=> CPU# \n");
2323 seq_puts(m, "# / _-----=> irqs-off \n");
2324 seq_puts(m, "# | / _----=> need-resched \n");
2325 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2326 seq_puts(m, "# ||| / _--=> preempt-depth \n");
Steven Rostedte6e1e252011-03-09 10:41:56 -05002327 seq_puts(m, "# |||| / delay \n");
2328 seq_puts(m, "# cmd pid ||||| time | caller \n");
2329 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002330}
2331
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002332static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002333{
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002334 unsigned long total;
2335 unsigned long entries;
2336
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002337 get_total_entries(buf, &total, &entries);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002338 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2339 entries, total, num_online_cpus());
2340 seq_puts(m, "#\n");
2341}
2342
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002343static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002344{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002345 print_event_info(buf, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002346 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
Michael Ellermana6168352008-08-20 16:36:11 -07002347 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002348}
2349
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002350static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedt77271ce2011-11-17 09:34:33 -05002351{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002352 print_event_info(buf, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002353 seq_puts(m, "# _-----=> irqs-off\n");
2354 seq_puts(m, "# / _----=> need-resched\n");
2355 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2356 seq_puts(m, "# || / _--=> preempt-depth\n");
2357 seq_puts(m, "# ||| / delay\n");
2358 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2359 seq_puts(m, "# | | | |||| | |\n");
2360}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002361
Jiri Olsa62b915f2010-04-02 19:01:22 +02002362void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002363print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2364{
2365 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002366 struct trace_buffer *buf = iter->trace_buffer;
2367 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002368 struct tracer *type = iter->trace;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002369 unsigned long entries;
2370 unsigned long total;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002371 const char *name = "preemption";
2372
Steven Rostedt (Red Hat)d840f712013-02-01 18:38:47 -05002373 name = type->name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002374
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002375 get_total_entries(buf, &total, &entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002376
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002377 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002378 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002379 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002380 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002381 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002382 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02002383 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002384 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002385 total,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002386 buf->cpu,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002387#if defined(CONFIG_PREEMPT_NONE)
2388 "server",
2389#elif defined(CONFIG_PREEMPT_VOLUNTARY)
2390 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04002391#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002392 "preempt",
2393#else
2394 "unknown",
2395#endif
2396 /* These are reserved for later use */
2397 0, 0, 0, 0);
2398#ifdef CONFIG_SMP
2399 seq_printf(m, " #P:%d)\n", num_online_cpus());
2400#else
2401 seq_puts(m, ")\n");
2402#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002403 seq_puts(m, "# -----------------\n");
2404 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002405 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
Eric W. Biedermand20b92a2012-03-13 16:02:19 -07002406 data->comm, data->pid,
2407 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002408 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002409 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002410
2411 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002412 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02002413 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2414 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002415 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02002416 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2417 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04002418 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002419 }
2420
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002421 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002422}
2423
Steven Rostedta3097202008-11-07 22:36:02 -05002424static void test_cpu_buff_start(struct trace_iterator *iter)
2425{
2426 struct trace_seq *s = &iter->seq;
2427
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002428 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2429 return;
2430
2431 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2432 return;
2433
Rusty Russell44623442009-01-01 10:12:23 +10302434 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05002435 return;
2436
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002437 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002438 return;
2439
Rusty Russell44623442009-01-01 10:12:23 +10302440 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002441
2442 /* Don't print started cpu buffer for the first entry of the trace */
2443 if (iter->idx > 1)
2444 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2445 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05002446}
2447
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002448static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002449{
Steven Rostedt214023c2008-05-12 21:20:46 +02002450 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002451 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002452 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002453 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002454
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002455 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002456
Steven Rostedta3097202008-11-07 22:36:02 -05002457 test_cpu_buff_start(iter);
2458
Steven Rostedtf633cef2008-12-23 23:24:13 -05002459 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002460
2461 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05002462 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2463 if (!trace_print_lat_context(iter))
2464 goto partial;
2465 } else {
2466 if (!trace_print_context(iter))
2467 goto partial;
2468 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002469 }
2470
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002471 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04002472 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002473
2474 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2475 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04002476
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002477 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002478partial:
2479 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002480}
2481
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002482static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002483{
2484 struct trace_seq *s = &iter->seq;
2485 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002486 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002487
2488 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002489
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002490 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002491 if (!trace_seq_printf(s, "%d %d %llu ",
2492 entry->pid, iter->cpu, iter->ts))
2493 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002494 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002495
Steven Rostedtf633cef2008-12-23 23:24:13 -05002496 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002497 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04002498 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002499
2500 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2501 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04002502
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002503 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002504partial:
2505 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002506}
2507
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002508static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002509{
2510 struct trace_seq *s = &iter->seq;
2511 unsigned char newline = '\n';
2512 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002513 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002514
2515 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002516
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002517 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2518 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2519 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2520 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2521 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002522
Steven Rostedtf633cef2008-12-23 23:24:13 -05002523 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002524 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04002525 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002526 if (ret != TRACE_TYPE_HANDLED)
2527 return ret;
2528 }
Steven Rostedt7104f302008-10-01 10:52:51 -04002529
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002530 SEQ_PUT_FIELD_RET(s, newline);
2531
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002532 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002533}
2534
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002535static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002536{
2537 struct trace_seq *s = &iter->seq;
2538 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002539 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002540
2541 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002542
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002543 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2544 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05002545 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002546 SEQ_PUT_FIELD_RET(s, iter->ts);
2547 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002548
Steven Rostedtf633cef2008-12-23 23:24:13 -05002549 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04002550 return event ? event->funcs->binary(iter, 0, event) :
2551 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002552}
2553
Jiri Olsa62b915f2010-04-02 19:01:22 +02002554int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002555{
Steven Rostedt6d158a82012-06-27 20:46:14 -04002556 struct ring_buffer_iter *buf_iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002557 int cpu;
2558
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002559 /* If we are looking at one CPU buffer, only check that one */
Steven Rostedtae3b5092013-01-23 15:22:59 -05002560 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002561 cpu = iter->cpu_file;
Steven Rostedt6d158a82012-06-27 20:46:14 -04002562 buf_iter = trace_buffer_iter(iter, cpu);
2563 if (buf_iter) {
2564 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002565 return 0;
2566 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002567 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002568 return 0;
2569 }
2570 return 1;
2571 }
2572
Steven Rostedtab464282008-05-12 21:21:00 +02002573 for_each_tracing_cpu(cpu) {
Steven Rostedt6d158a82012-06-27 20:46:14 -04002574 buf_iter = trace_buffer_iter(iter, cpu);
2575 if (buf_iter) {
2576 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedtd7690412008-10-01 00:29:53 -04002577 return 0;
2578 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002579 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedtd7690412008-10-01 00:29:53 -04002580 return 0;
2581 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002582 }
Steven Rostedtd7690412008-10-01 00:29:53 -04002583
Frederic Weisbecker797d3712008-09-30 18:13:45 +02002584 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002585}
2586
Lai Jiangshan4f535962009-05-18 19:35:34 +08002587/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05002588enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002589{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002590 enum print_line_t ret;
2591
Jiri Olsaee5e51f2011-03-25 12:05:18 +01002592 if (iter->lost_events &&
2593 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2594 iter->cpu, iter->lost_events))
2595 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002596
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002597 if (iter->trace && iter->trace->print_line) {
2598 ret = iter->trace->print_line(iter);
2599 if (ret != TRACE_TYPE_UNHANDLED)
2600 return ret;
2601 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002602
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -05002603 if (iter->ent->type == TRACE_BPUTS &&
2604 trace_flags & TRACE_ITER_PRINTK &&
2605 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2606 return trace_print_bputs_msg_only(iter);
2607
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002608 if (iter->ent->type == TRACE_BPRINT &&
2609 trace_flags & TRACE_ITER_PRINTK &&
2610 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002611 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002612
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002613 if (iter->ent->type == TRACE_PRINT &&
2614 trace_flags & TRACE_ITER_PRINTK &&
2615 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002616 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002617
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002618 if (trace_flags & TRACE_ITER_BIN)
2619 return print_bin_fmt(iter);
2620
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002621 if (trace_flags & TRACE_ITER_HEX)
2622 return print_hex_fmt(iter);
2623
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002624 if (trace_flags & TRACE_ITER_RAW)
2625 return print_raw_fmt(iter);
2626
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002627 return print_trace_fmt(iter);
2628}
2629
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01002630void trace_latency_header(struct seq_file *m)
2631{
2632 struct trace_iterator *iter = m->private;
2633
2634 /* print nothing if the buffers are empty */
2635 if (trace_empty(iter))
2636 return;
2637
2638 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2639 print_trace_header(m, iter);
2640
2641 if (!(trace_flags & TRACE_ITER_VERBOSE))
2642 print_lat_help_header(m);
2643}
2644
Jiri Olsa62b915f2010-04-02 19:01:22 +02002645void trace_default_header(struct seq_file *m)
2646{
2647 struct trace_iterator *iter = m->private;
2648
Jiri Olsaf56e7f82011-06-03 16:58:49 +02002649 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2650 return;
2651
Jiri Olsa62b915f2010-04-02 19:01:22 +02002652 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2653 /* print nothing if the buffers are empty */
2654 if (trace_empty(iter))
2655 return;
2656 print_trace_header(m, iter);
2657 if (!(trace_flags & TRACE_ITER_VERBOSE))
2658 print_lat_help_header(m);
2659 } else {
Steven Rostedt77271ce2011-11-17 09:34:33 -05002660 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2661 if (trace_flags & TRACE_ITER_IRQ_INFO)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002662 print_func_help_header_irq(iter->trace_buffer, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002663 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002664 print_func_help_header(iter->trace_buffer, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002665 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02002666 }
2667}
2668
Steven Rostedte0a413f2011-09-29 21:26:16 -04002669static void test_ftrace_alive(struct seq_file *m)
2670{
2671 if (!ftrace_is_dead())
2672 return;
2673 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2674 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2675}
2676
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002677#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002678static void show_snapshot_main_help(struct seq_file *m)
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002679{
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002680 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2681 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2682 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2683 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2684 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2685 seq_printf(m, "# is not a '0' or '1')\n");
2686}
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002687
2688static void show_snapshot_percpu_help(struct seq_file *m)
2689{
2690 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2691#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2692 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2693 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2694#else
2695 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2696 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2697#endif
2698 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2699 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2700 seq_printf(m, "# is not a '0' or '1')\n");
2701}
2702
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002703static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2704{
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05002705 if (iter->tr->allocated_snapshot)
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002706 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2707 else
2708 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2709
2710 seq_printf(m, "# Snapshot commands:\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002711 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2712 show_snapshot_main_help(m);
2713 else
2714 show_snapshot_percpu_help(m);
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002715}
2716#else
2717/* Should never be called */
2718static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2719#endif
2720
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002721static int s_show(struct seq_file *m, void *v)
2722{
2723 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002724 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002725
2726 if (iter->ent == NULL) {
2727 if (iter->tr) {
2728 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2729 seq_puts(m, "#\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04002730 test_ftrace_alive(m);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002731 }
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002732 if (iter->snapshot && trace_empty(iter))
2733 print_snapshot_help(m, iter);
2734 else if (iter->trace && iter->trace->print_header)
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002735 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02002736 else
2737 trace_default_header(m);
2738
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002739 } else if (iter->leftover) {
2740 /*
2741 * If we filled the seq_file buffer earlier, we
2742 * want to just show it now.
2743 */
2744 ret = trace_print_seq(m, &iter->seq);
2745
2746 /* ret should this time be zero, but you never know */
2747 iter->leftover = ret;
2748
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002749 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002750 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002751 ret = trace_print_seq(m, &iter->seq);
2752 /*
2753 * If we overflow the seq_file buffer, then it will
2754 * ask us for this data again at start up.
2755 * Use that instead.
2756 * ret is 0 if seq_file write succeeded.
2757 * -1 otherwise.
2758 */
2759 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002760 }
2761
2762 return 0;
2763}
2764
James Morris88e9d342009-09-22 16:43:43 -07002765static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002766 .start = s_start,
2767 .next = s_next,
2768 .stop = s_stop,
2769 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002770};
2771
Ingo Molnare309b412008-05-12 21:20:51 +02002772static struct trace_iterator *
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002773__tracing_open(struct inode *inode, struct file *file, bool snapshot)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002774{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002775 struct trace_cpu *tc = inode->i_private;
2776 struct trace_array *tr = tc->tr;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002777 struct trace_iterator *iter;
Jiri Olsa50e18b92012-04-25 10:23:39 +02002778 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002779
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002780 if (tracing_disabled)
2781 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02002782
Jiri Olsa50e18b92012-04-25 10:23:39 +02002783 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002784 if (!iter)
2785 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002786
Steven Rostedt6d158a82012-06-27 20:46:14 -04002787 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2788 GFP_KERNEL);
Dan Carpenter93574fc2012-07-11 09:35:08 +03002789 if (!iter->buffer_iter)
2790 goto release;
2791
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002792 /*
2793 * We make a copy of the current tracer to avoid concurrent
2794 * changes on it while we are reading.
2795 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002796 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002797 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002798 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002799 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002800
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002801 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002802
Li Zefan79f55992009-06-15 14:58:26 +08002803 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002804 goto fail;
2805
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002806 iter->tr = tr;
2807
2808#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002809 /* Currently only the top directory has a snapshot */
2810 if (tr->current_trace->print_max || snapshot)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002811 iter->trace_buffer = &tr->max_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002812 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002813#endif
2814 iter->trace_buffer = &tr->trace_buffer;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002815 iter->snapshot = snapshot;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002816 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002817 mutex_init(&iter->mutex);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002818 iter->cpu_file = tc->cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002819
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002820 /* Notify the tracer early; before we stop tracing. */
2821 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01002822 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002823
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002824 /* Annotate start of buffers if we had overruns */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002825 if (ring_buffer_overruns(iter->trace_buffer->buffer))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002826 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2827
David Sharp8be07092012-11-13 12:18:22 -08002828 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2829 if (trace_clocks[trace_clock_id].in_ns)
2830 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2831
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002832 /* stop the trace while dumping if we are not opening "snapshot" */
2833 if (!iter->snapshot)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002834 tracing_stop_tr(tr);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002835
Steven Rostedtae3b5092013-01-23 15:22:59 -05002836 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002837 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002838 iter->buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002839 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
David Miller72c9ddf2010-04-20 15:47:11 -07002840 }
2841 ring_buffer_read_prepare_sync();
2842 for_each_tracing_cpu(cpu) {
2843 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002844 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002845 }
2846 } else {
2847 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002848 iter->buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002849 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
David Miller72c9ddf2010-04-20 15:47:11 -07002850 ring_buffer_read_prepare_sync();
2851 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002852 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002853 }
2854
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05002855 tr->ref++;
2856
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002857 mutex_unlock(&trace_types_lock);
2858
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002859 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002860
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002861 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002862 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002863 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04002864 kfree(iter->buffer_iter);
Dan Carpenter93574fc2012-07-11 09:35:08 +03002865release:
Jiri Olsa50e18b92012-04-25 10:23:39 +02002866 seq_release_private(inode, file);
2867 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002868}
2869
2870int tracing_open_generic(struct inode *inode, struct file *filp)
2871{
Steven Rostedt60a11772008-05-12 21:20:44 +02002872 if (tracing_disabled)
2873 return -ENODEV;
2874
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002875 filp->private_data = inode->i_private;
2876 return 0;
2877}
2878
Hannes Eder4fd27352009-02-10 19:44:12 +01002879static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002880{
matt mooney907f2782010-09-27 19:04:53 -07002881 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002882 struct trace_iterator *iter;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002883 struct trace_array *tr;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002884 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002885
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002886 if (!(file->f_mode & FMODE_READ))
2887 return 0;
2888
2889 iter = m->private;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002890 tr = iter->tr;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002891
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002892 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05002893
2894 WARN_ON(!tr->ref);
2895 tr->ref--;
2896
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002897 for_each_tracing_cpu(cpu) {
2898 if (iter->buffer_iter[cpu])
2899 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2900 }
2901
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002902 if (iter->trace && iter->trace->close)
2903 iter->trace->close(iter);
2904
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002905 if (!iter->snapshot)
2906 /* reenable tracing if it was previously enabled */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002907 tracing_start_tr(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002908 mutex_unlock(&trace_types_lock);
2909
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002910 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002911 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002912 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04002913 kfree(iter->buffer_iter);
Jiri Olsa50e18b92012-04-25 10:23:39 +02002914 seq_release_private(inode, file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002915 return 0;
2916}
2917
2918static int tracing_open(struct inode *inode, struct file *file)
2919{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002920 struct trace_iterator *iter;
2921 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002922
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002923 /* If this file was open for write, then erase contents */
2924 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002925 (file->f_flags & O_TRUNC)) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002926 struct trace_cpu *tc = inode->i_private;
2927 struct trace_array *tr = tc->tr;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002928
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002929 if (tc->cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002930 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002931 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002932 tracing_reset(&tr->trace_buffer, tc->cpu);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002933 }
2934
2935 if (file->f_mode & FMODE_READ) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002936 iter = __tracing_open(inode, file, false);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002937 if (IS_ERR(iter))
2938 ret = PTR_ERR(iter);
2939 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2940 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2941 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002942 return ret;
2943}
2944
Ingo Molnare309b412008-05-12 21:20:51 +02002945static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002946t_next(struct seq_file *m, void *v, loff_t *pos)
2947{
Li Zefanf129e962009-06-24 09:53:44 +08002948 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002949
2950 (*pos)++;
2951
2952 if (t)
2953 t = t->next;
2954
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002955 return t;
2956}
2957
2958static void *t_start(struct seq_file *m, loff_t *pos)
2959{
Li Zefanf129e962009-06-24 09:53:44 +08002960 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002961 loff_t l = 0;
2962
2963 mutex_lock(&trace_types_lock);
Li Zefanf129e962009-06-24 09:53:44 +08002964 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002965 ;
2966
2967 return t;
2968}
2969
2970static void t_stop(struct seq_file *m, void *p)
2971{
2972 mutex_unlock(&trace_types_lock);
2973}
2974
2975static int t_show(struct seq_file *m, void *v)
2976{
2977 struct tracer *t = v;
2978
2979 if (!t)
2980 return 0;
2981
2982 seq_printf(m, "%s", t->name);
2983 if (t->next)
2984 seq_putc(m, ' ');
2985 else
2986 seq_putc(m, '\n');
2987
2988 return 0;
2989}
2990
James Morris88e9d342009-09-22 16:43:43 -07002991static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002992 .start = t_start,
2993 .next = t_next,
2994 .stop = t_stop,
2995 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002996};
2997
2998static int show_traces_open(struct inode *inode, struct file *file)
2999{
Steven Rostedt60a11772008-05-12 21:20:44 +02003000 if (tracing_disabled)
3001 return -ENODEV;
3002
Li Zefanf129e962009-06-24 09:53:44 +08003003 return seq_open(file, &show_traces_seq_ops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003004}
3005
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003006static ssize_t
3007tracing_write_stub(struct file *filp, const char __user *ubuf,
3008 size_t count, loff_t *ppos)
3009{
3010 return count;
3011}
3012
Slava Pestov364829b2010-11-24 15:13:16 -08003013static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
3014{
3015 if (file->f_mode & FMODE_READ)
3016 return seq_lseek(file, offset, origin);
3017 else
3018 return 0;
3019}
3020
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003021static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003022 .open = tracing_open,
3023 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003024 .write = tracing_write_stub,
Slava Pestov364829b2010-11-24 15:13:16 -08003025 .llseek = tracing_seek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003026 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003027};
3028
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003029static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02003030 .open = show_traces_open,
3031 .read = seq_read,
3032 .release = seq_release,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003033 .llseek = seq_lseek,
Ingo Molnarc7078de2008-05-12 21:20:52 +02003034};
3035
Ingo Molnar36dfe922008-05-12 21:20:52 +02003036/*
3037 * Only trace on a CPU if the bitmask is set:
3038 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303039static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02003040
3041/*
3042 * The tracer itself will not take this lock, but still we want
3043 * to provide a consistent cpumask to user-space:
3044 */
3045static DEFINE_MUTEX(tracing_cpumask_update_lock);
3046
3047/*
3048 * Temporary storage for the character representation of the
3049 * CPU bitmask (and one more byte for the newline):
3050 */
3051static char mask_str[NR_CPUS + 1];
3052
Ingo Molnarc7078de2008-05-12 21:20:52 +02003053static ssize_t
3054tracing_cpumask_read(struct file *filp, char __user *ubuf,
3055 size_t count, loff_t *ppos)
3056{
Ingo Molnar36dfe922008-05-12 21:20:52 +02003057 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003058
3059 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003060
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303061 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003062 if (count - len < 2) {
3063 count = -EINVAL;
3064 goto out_err;
3065 }
3066 len += sprintf(mask_str + len, "\n");
3067 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3068
3069out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02003070 mutex_unlock(&tracing_cpumask_update_lock);
3071
3072 return count;
3073}
3074
3075static ssize_t
3076tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3077 size_t count, loff_t *ppos)
3078{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003079 struct trace_array *tr = filp->private_data;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303080 cpumask_var_t tracing_cpumask_new;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003081 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303082
3083 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3084 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003085
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303086 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003087 if (err)
3088 goto err_unlock;
3089
Li Zefan215368e2009-06-15 10:56:42 +08003090 mutex_lock(&tracing_cpumask_update_lock);
3091
Steven Rostedta5e25882008-12-02 15:34:05 -05003092 local_irq_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003093 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02003094 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02003095 /*
3096 * Increase/decrease the disabled counter if we are
3097 * about to flip a bit in the cpumask:
3098 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303099 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
3100 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003101 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3102 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003103 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303104 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
3105 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003106 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3107 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003108 }
3109 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003110 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05003111 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02003112
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303113 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003114
Ingo Molnarc7078de2008-05-12 21:20:52 +02003115 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303116 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02003117
Ingo Molnarc7078de2008-05-12 21:20:52 +02003118 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02003119
3120err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08003121 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003122
3123 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003124}
3125
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003126static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02003127 .open = tracing_open_generic,
3128 .read = tracing_cpumask_read,
3129 .write = tracing_cpumask_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003130 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003131};
3132
Li Zefanfdb372e2009-12-08 11:15:59 +08003133static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003134{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003135 struct tracer_opt *trace_opts;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003136 struct trace_array *tr = m->private;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003137 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003138 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003139
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003140 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003141 tracer_flags = tr->current_trace->flags->val;
3142 trace_opts = tr->current_trace->flags->opts;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003143
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003144 for (i = 0; trace_options[i]; i++) {
3145 if (trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08003146 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003147 else
Li Zefanfdb372e2009-12-08 11:15:59 +08003148 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003149 }
3150
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003151 for (i = 0; trace_opts[i].name; i++) {
3152 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08003153 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003154 else
Li Zefanfdb372e2009-12-08 11:15:59 +08003155 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003156 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003157 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003158
Li Zefanfdb372e2009-12-08 11:15:59 +08003159 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003160}
3161
Li Zefan8d18eaa2009-12-08 11:17:06 +08003162static int __set_tracer_option(struct tracer *trace,
3163 struct tracer_flags *tracer_flags,
3164 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003165{
Li Zefan8d18eaa2009-12-08 11:17:06 +08003166 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003167
Li Zefan8d18eaa2009-12-08 11:17:06 +08003168 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003169 if (ret)
3170 return ret;
3171
3172 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08003173 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003174 else
Zhaolei77708412009-08-07 18:53:21 +08003175 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003176 return 0;
3177}
3178
Li Zefan8d18eaa2009-12-08 11:17:06 +08003179/* Try to assign a tracer specific option */
3180static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3181{
3182 struct tracer_flags *tracer_flags = trace->flags;
3183 struct tracer_opt *opts = NULL;
3184 int i;
3185
3186 for (i = 0; tracer_flags->opts[i].name; i++) {
3187 opts = &tracer_flags->opts[i];
3188
3189 if (strcmp(cmp, opts->name) == 0)
3190 return __set_tracer_option(trace, trace->flags,
3191 opts, neg);
3192 }
3193
3194 return -EINVAL;
3195}
3196
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003197/* Some tracers require overwrite to stay enabled */
3198int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3199{
3200 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3201 return -1;
3202
3203 return 0;
3204}
3205
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003206int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003207{
3208 /* do nothing if flag is already set */
3209 if (!!(trace_flags & mask) == !!enabled)
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003210 return 0;
3211
3212 /* Give the tracer a chance to approve the change */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003213 if (tr->current_trace->flag_changed)
3214 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003215 return -EINVAL;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003216
3217 if (enabled)
3218 trace_flags |= mask;
3219 else
3220 trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08003221
3222 if (mask == TRACE_ITER_RECORD_CMD)
3223 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08003224
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003225 if (mask == TRACE_ITER_OVERWRITE) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003226 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003227#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003228 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003229#endif
3230 }
Steven Rostedt81698832012-10-11 10:15:05 -04003231
3232 if (mask == TRACE_ITER_PRINTK)
3233 trace_printk_start_stop_comm(enabled);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003234
3235 return 0;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003236}
3237
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003238static int trace_set_options(struct trace_array *tr, char *option)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003239{
Li Zefan8d18eaa2009-12-08 11:17:06 +08003240 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003241 int neg = 0;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003242 int ret = -ENODEV;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003243 int i;
3244
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003245 cmp = strstrip(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003246
Li Zefan8d18eaa2009-12-08 11:17:06 +08003247 if (strncmp(cmp, "no", 2) == 0) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003248 neg = 1;
3249 cmp += 2;
3250 }
3251
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003252 mutex_lock(&trace_types_lock);
3253
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003254 for (i = 0; trace_options[i]; i++) {
Li Zefan8d18eaa2009-12-08 11:17:06 +08003255 if (strcmp(cmp, trace_options[i]) == 0) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003256 ret = set_tracer_flag(tr, 1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003257 break;
3258 }
3259 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003260
3261 /* If no option could be set, test the specific tracer options */
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003262 if (!trace_options[i])
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003263 ret = set_tracer_option(tr->current_trace, cmp, neg);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003264
3265 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003266
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003267 return ret;
3268}
3269
3270static ssize_t
3271tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3272 size_t cnt, loff_t *ppos)
3273{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003274 struct seq_file *m = filp->private_data;
3275 struct trace_array *tr = m->private;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003276 char buf[64];
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003277 int ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003278
3279 if (cnt >= sizeof(buf))
3280 return -EINVAL;
3281
3282 if (copy_from_user(&buf, ubuf, cnt))
3283 return -EFAULT;
3284
Steven Rostedta8dd2172013-01-09 20:54:17 -05003285 buf[cnt] = 0;
3286
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003287 ret = trace_set_options(tr, buf);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003288 if (ret < 0)
3289 return ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003290
Jiri Olsacf8517c2009-10-23 19:36:16 -04003291 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003292
3293 return cnt;
3294}
3295
Li Zefanfdb372e2009-12-08 11:15:59 +08003296static int tracing_trace_options_open(struct inode *inode, struct file *file)
3297{
3298 if (tracing_disabled)
3299 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003300
3301 return single_open(file, tracing_trace_options_show, inode->i_private);
Li Zefanfdb372e2009-12-08 11:15:59 +08003302}
3303
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003304static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08003305 .open = tracing_trace_options_open,
3306 .read = seq_read,
3307 .llseek = seq_lseek,
3308 .release = single_release,
Steven Rostedtee6bce52008-11-12 17:52:37 -05003309 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003310};
3311
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003312static const char readme_msg[] =
3313 "tracing mini-HOWTO:\n\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003314 "# echo 0 > tracing_on : quick way to disable tracing\n"
3315 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3316 " Important files:\n"
3317 " trace\t\t\t- The static contents of the buffer\n"
3318 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3319 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3320 " current_tracer\t- function and latency tracers\n"
3321 " available_tracers\t- list of configured tracers for current_tracer\n"
3322 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3323 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3324 " trace_clock\t\t-change the clock used to order events\n"
3325 " local: Per cpu clock but may not be synced across CPUs\n"
3326 " global: Synced across CPUs but slows tracing down.\n"
3327 " counter: Not a clock, but just an increment\n"
3328 " uptime: Jiffy counter from time of boot\n"
3329 " perf: Same clock that perf events use\n"
3330#ifdef CONFIG_X86_64
3331 " x86-tsc: TSC cycle counter\n"
3332#endif
3333 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3334 " tracing_cpumask\t- Limit which CPUs to trace\n"
3335 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3336 "\t\t\t Remove sub-buffer with rmdir\n"
3337 " trace_options\t\t- Set format or modify how tracing happens\n"
3338 "\t\t\t Disable an option by adding a suffix 'no' to the option name\n"
3339#ifdef CONFIG_DYNAMIC_FTRACE
3340 "\n available_filter_functions - list of functions that can be filtered on\n"
3341 " set_ftrace_filter\t- echo function name in here to only trace these functions\n"
3342 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3343 " modules: Can select a group via module\n"
3344 " Format: :mod:<module-name>\n"
3345 " example: echo :mod:ext3 > set_ftrace_filter\n"
3346 " triggers: a command to perform when function is hit\n"
3347 " Format: <function>:<trigger>[:count]\n"
3348 " trigger: traceon, traceoff\n"
3349 " enable_event:<system>:<event>\n"
3350 " disable_event:<system>:<event>\n"
3351#ifdef CONFIG_STACKTRACE
3352 " stacktrace\n"
3353#endif
3354#ifdef CONFIG_TRACER_SNAPSHOT
3355 " snapshot\n"
3356#endif
3357 " example: echo do_fault:traceoff > set_ftrace_filter\n"
3358 " echo do_trap:traceoff:3 > set_ftrace_filter\n"
3359 " The first one will disable tracing every time do_fault is hit\n"
3360 " The second will disable tracing at most 3 times when do_trap is hit\n"
3361 " The first time do trap is hit and it disables tracing, the counter\n"
3362 " will decrement to 2. If tracing is already disabled, the counter\n"
3363 " will not decrement. It only decrements when the trigger did work\n"
3364 " To remove trigger without count:\n"
3365 " echo '!<function>:<trigger> > set_ftrace_filter\n"
3366 " To remove trigger with a count:\n"
3367 " echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3368 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3369 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3370 " modules: Can select a group via module command :mod:\n"
3371 " Does not accept triggers\n"
3372#endif /* CONFIG_DYNAMIC_FTRACE */
3373#ifdef CONFIG_FUNCTION_TRACER
3374 " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
3375#endif
3376#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3377 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3378 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3379#endif
3380#ifdef CONFIG_TRACER_SNAPSHOT
3381 "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
3382 "\t\t\t Read the contents for more information\n"
3383#endif
3384#ifdef CONFIG_STACKTRACE
3385 " stack_trace\t\t- Shows the max stack trace when active\n"
3386 " stack_max_size\t- Shows current max stack size that was traced\n"
3387 "\t\t\t Write into this file to reset the max size (trigger a new trace)\n"
3388#ifdef CONFIG_DYNAMIC_FTRACE
3389 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
3390#endif
3391#endif /* CONFIG_STACKTRACE */
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003392;
3393
3394static ssize_t
3395tracing_readme_read(struct file *filp, char __user *ubuf,
3396 size_t cnt, loff_t *ppos)
3397{
3398 return simple_read_from_buffer(ubuf, cnt, ppos,
3399 readme_msg, strlen(readme_msg));
3400}
3401
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003402static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02003403 .open = tracing_open_generic,
3404 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003405 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003406};
3407
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003408static ssize_t
Avadh Patel69abe6a2009-04-10 16:04:48 -04003409tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3410 size_t cnt, loff_t *ppos)
3411{
3412 char *buf_comm;
3413 char *file_buf;
3414 char *buf;
3415 int len = 0;
3416 int pid;
3417 int i;
3418
3419 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3420 if (!file_buf)
3421 return -ENOMEM;
3422
3423 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3424 if (!buf_comm) {
3425 kfree(file_buf);
3426 return -ENOMEM;
3427 }
3428
3429 buf = file_buf;
3430
3431 for (i = 0; i < SAVED_CMDLINES; i++) {
3432 int r;
3433
3434 pid = map_cmdline_to_pid[i];
3435 if (pid == -1 || pid == NO_CMDLINE_MAP)
3436 continue;
3437
3438 trace_find_cmdline(pid, buf_comm);
3439 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3440 buf += r;
3441 len += r;
3442 }
3443
3444 len = simple_read_from_buffer(ubuf, cnt, ppos,
3445 file_buf, len);
3446
3447 kfree(file_buf);
3448 kfree(buf_comm);
3449
3450 return len;
3451}
3452
3453static const struct file_operations tracing_saved_cmdlines_fops = {
3454 .open = tracing_open_generic,
3455 .read = tracing_saved_cmdlines_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003456 .llseek = generic_file_llseek,
Avadh Patel69abe6a2009-04-10 16:04:48 -04003457};
3458
3459static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003460tracing_set_trace_read(struct file *filp, char __user *ubuf,
3461 size_t cnt, loff_t *ppos)
3462{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003463 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08003464 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003465 int r;
3466
3467 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003468 r = sprintf(buf, "%s\n", tr->current_trace->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003469 mutex_unlock(&trace_types_lock);
3470
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003471 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003472}
3473
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02003474int tracer_init(struct tracer *t, struct trace_array *tr)
3475{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003476 tracing_reset_online_cpus(&tr->trace_buffer);
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02003477 return t->init(tr);
3478}
3479
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003480static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003481{
3482 int cpu;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05003483
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003484 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003485 per_cpu_ptr(buf->data, cpu)->entries = val;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003486}
3487
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003488#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookad60da502012-10-17 11:56:16 +09003489/* resize @tr's buffer to the size of @size_tr's entries */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003490static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3491 struct trace_buffer *size_buf, int cpu_id)
Hiraku Toyookad60da502012-10-17 11:56:16 +09003492{
3493 int cpu, ret = 0;
3494
3495 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3496 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003497 ret = ring_buffer_resize(trace_buf->buffer,
3498 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
Hiraku Toyookad60da502012-10-17 11:56:16 +09003499 if (ret < 0)
3500 break;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003501 per_cpu_ptr(trace_buf->data, cpu)->entries =
3502 per_cpu_ptr(size_buf->data, cpu)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09003503 }
3504 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003505 ret = ring_buffer_resize(trace_buf->buffer,
3506 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
Hiraku Toyookad60da502012-10-17 11:56:16 +09003507 if (ret == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003508 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3509 per_cpu_ptr(size_buf->data, cpu_id)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09003510 }
3511
3512 return ret;
3513}
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003514#endif /* CONFIG_TRACER_MAX_TRACE */
Hiraku Toyookad60da502012-10-17 11:56:16 +09003515
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003516static int __tracing_resize_ring_buffer(struct trace_array *tr,
3517 unsigned long size, int cpu)
Steven Rostedt73c51622009-03-11 13:42:01 -04003518{
3519 int ret;
3520
3521 /*
3522 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04003523 * we use the size that was given, and we can forget about
3524 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04003525 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05003526 ring_buffer_expanded = true;
Steven Rostedt73c51622009-03-11 13:42:01 -04003527
Steven Rostedtb382ede62012-10-10 21:44:34 -04003528 /* May be called before buffers are initialized */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003529 if (!tr->trace_buffer.buffer)
Steven Rostedtb382ede62012-10-10 21:44:34 -04003530 return 0;
3531
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003532 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04003533 if (ret < 0)
3534 return ret;
3535
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003536#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003537 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3538 !tr->current_trace->use_max_tr)
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003539 goto out;
3540
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003541 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04003542 if (ret < 0) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003543 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3544 &tr->trace_buffer, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04003545 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04003546 /*
3547 * AARGH! We are left with different
3548 * size max buffer!!!!
3549 * The max buffer is our "snapshot" buffer.
3550 * When a tracer needs a snapshot (one of the
3551 * latency tracers), it swaps the max buffer
3552 * with the saved snap shot. We succeeded to
3553 * update the size of the main buffer, but failed to
3554 * update the size of the max buffer. But when we tried
3555 * to reset the main buffer to the original size, we
3556 * failed there too. This is very unlikely to
3557 * happen, but if it does, warn and kill all
3558 * tracing.
3559 */
Steven Rostedt73c51622009-03-11 13:42:01 -04003560 WARN_ON(1);
3561 tracing_disabled = 1;
3562 }
3563 return ret;
3564 }
3565
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003566 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003567 set_buffer_entries(&tr->max_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003568 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003569 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003570
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003571 out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003572#endif /* CONFIG_TRACER_MAX_TRACE */
3573
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003574 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003575 set_buffer_entries(&tr->trace_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003576 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003577 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
Steven Rostedt73c51622009-03-11 13:42:01 -04003578
3579 return ret;
3580}
3581
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003582static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3583 unsigned long size, int cpu_id)
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003584{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003585 int ret = size;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003586
3587 mutex_lock(&trace_types_lock);
3588
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003589 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3590 /* make sure, this cpu is enabled in the mask */
3591 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3592 ret = -EINVAL;
3593 goto out;
3594 }
3595 }
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003596
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003597 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003598 if (ret < 0)
3599 ret = -ENOMEM;
3600
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003601out:
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003602 mutex_unlock(&trace_types_lock);
3603
3604 return ret;
3605}
3606
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003607
Steven Rostedt1852fcc2009-03-11 14:33:00 -04003608/**
3609 * tracing_update_buffers - used by tracing facility to expand ring buffers
3610 *
3611 * To save on memory when the tracing is never used on a system with it
3612 * configured in. The ring buffers are set to a minimum size. But once
3613 * a user starts to use the tracing facility, then they need to grow
3614 * to their default size.
3615 *
3616 * This function is to be called when a tracer is about to be used.
3617 */
3618int tracing_update_buffers(void)
3619{
3620 int ret = 0;
3621
Steven Rostedt1027fcb2009-03-12 11:33:20 -04003622 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04003623 if (!ring_buffer_expanded)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003624 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003625 RING_BUFFER_ALL_CPUS);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04003626 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04003627
3628 return ret;
3629}
3630
Steven Rostedt577b7852009-02-26 23:43:05 -05003631struct trace_option_dentry;
3632
3633static struct trace_option_dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003634create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
Steven Rostedt577b7852009-02-26 23:43:05 -05003635
3636static void
3637destroy_trace_option_files(struct trace_option_dentry *topts);
3638
Steven Rostedtb2821ae2009-02-02 21:38:32 -05003639static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003640{
Steven Rostedt577b7852009-02-26 23:43:05 -05003641 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003642 struct trace_array *tr = &global_trace;
3643 struct tracer *t;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003644#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05003645 bool had_max_tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003646#endif
Peter Zijlstrad9e54072008-11-01 19:57:37 +01003647 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003648
Steven Rostedt1027fcb2009-03-12 11:33:20 -04003649 mutex_lock(&trace_types_lock);
3650
Steven Rostedt73c51622009-03-11 13:42:01 -04003651 if (!ring_buffer_expanded) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003652 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003653 RING_BUFFER_ALL_CPUS);
Steven Rostedt73c51622009-03-11 13:42:01 -04003654 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01003655 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04003656 ret = 0;
3657 }
3658
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003659 for (t = trace_types; t; t = t->next) {
3660 if (strcmp(t->name, buf) == 0)
3661 break;
3662 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02003663 if (!t) {
3664 ret = -EINVAL;
3665 goto out;
3666 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003667 if (t == tr->current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003668 goto out;
3669
Steven Rostedt9f029e82008-11-12 15:24:24 -05003670 trace_branch_disable();
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003671
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003672 tr->current_trace->enabled = false;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003673
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003674 if (tr->current_trace->reset)
3675 tr->current_trace->reset(tr);
Steven Rostedt34600f02013-01-22 13:35:11 -05003676
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003677 /* Current trace needs to be nop_trace before synchronize_sched */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003678 tr->current_trace = &nop_trace;
Steven Rostedt34600f02013-01-22 13:35:11 -05003679
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05003680#ifdef CONFIG_TRACER_MAX_TRACE
3681 had_max_tr = tr->allocated_snapshot;
Steven Rostedt34600f02013-01-22 13:35:11 -05003682
3683 if (had_max_tr && !t->use_max_tr) {
3684 /*
3685 * We need to make sure that the update_max_tr sees that
3686 * current_trace changed to nop_trace to keep it from
3687 * swapping the buffers after we resize it.
3688 * The update_max_tr is called from interrupts disabled
3689 * so a synchronized_sched() is sufficient.
3690 */
3691 synchronize_sched();
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04003692 free_snapshot(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003693 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003694#endif
Steven Rostedt577b7852009-02-26 23:43:05 -05003695 destroy_trace_option_files(topts);
3696
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003697 topts = create_trace_option_files(tr, t);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003698
3699#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05003700 if (t->use_max_tr && !had_max_tr) {
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04003701 ret = alloc_snapshot(tr);
Hiraku Toyookad60da502012-10-17 11:56:16 +09003702 if (ret < 0)
3703 goto out;
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003704 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003705#endif
Steven Rostedt577b7852009-02-26 23:43:05 -05003706
Frederic Weisbecker1c800252008-11-16 05:57:26 +01003707 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02003708 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01003709 if (ret)
3710 goto out;
3711 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003712
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003713 tr->current_trace = t;
3714 tr->current_trace->enabled = true;
Steven Rostedt9f029e82008-11-12 15:24:24 -05003715 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003716 out:
3717 mutex_unlock(&trace_types_lock);
3718
Peter Zijlstrad9e54072008-11-01 19:57:37 +01003719 return ret;
3720}
3721
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003722static ssize_t
3723tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3724 size_t cnt, loff_t *ppos)
3725{
Li Zefanee6c2c12009-09-18 14:06:47 +08003726 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003727 int i;
3728 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01003729 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003730
Steven Rostedt60063a62008-10-28 10:44:24 -04003731 ret = cnt;
3732
Li Zefanee6c2c12009-09-18 14:06:47 +08003733 if (cnt > MAX_TRACER_SIZE)
3734 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003735
3736 if (copy_from_user(&buf, ubuf, cnt))
3737 return -EFAULT;
3738
3739 buf[cnt] = 0;
3740
3741 /* strip ending whitespace. */
3742 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3743 buf[i] = 0;
3744
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01003745 err = tracing_set_tracer(buf);
3746 if (err)
3747 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003748
Jiri Olsacf8517c2009-10-23 19:36:16 -04003749 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003750
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02003751 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003752}
3753
3754static ssize_t
3755tracing_max_lat_read(struct file *filp, char __user *ubuf,
3756 size_t cnt, loff_t *ppos)
3757{
3758 unsigned long *ptr = filp->private_data;
3759 char buf[64];
3760 int r;
3761
Steven Rostedtcffae432008-05-12 21:21:00 +02003762 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003763 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02003764 if (r > sizeof(buf))
3765 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003766 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003767}
3768
3769static ssize_t
3770tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3771 size_t cnt, loff_t *ppos)
3772{
Hannes Eder5e398412009-02-10 19:44:34 +01003773 unsigned long *ptr = filp->private_data;
Hannes Eder5e398412009-02-10 19:44:34 +01003774 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003775 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003776
Peter Huewe22fe9b52011-06-07 21:58:27 +02003777 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3778 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003779 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003780
3781 *ptr = val * 1000;
3782
3783 return cnt;
3784}
3785
Steven Rostedtb3806b42008-05-12 21:20:46 +02003786static int tracing_open_pipe(struct inode *inode, struct file *filp)
3787{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003788 struct trace_cpu *tc = inode->i_private;
3789 struct trace_array *tr = tc->tr;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003790 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003791 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003792
3793 if (tracing_disabled)
3794 return -ENODEV;
3795
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003796 mutex_lock(&trace_types_lock);
3797
Steven Rostedtb3806b42008-05-12 21:20:46 +02003798 /* create a buffer to store the information to pass to userspace */
3799 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003800 if (!iter) {
3801 ret = -ENOMEM;
3802 goto out;
3803 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003804
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003805 /*
3806 * We make a copy of the current tracer to avoid concurrent
3807 * changes on it while we are reading.
3808 */
3809 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3810 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003811 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003812 goto fail;
3813 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003814 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003815
3816 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3817 ret = -ENOMEM;
3818 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10303819 }
3820
Steven Rostedta3097202008-11-07 22:36:02 -05003821 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10303822 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05003823
Steven Rostedt112f38a72009-06-01 15:16:05 -04003824 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3825 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3826
David Sharp8be07092012-11-13 12:18:22 -08003827 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3828 if (trace_clocks[trace_clock_id].in_ns)
3829 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3830
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003831 iter->cpu_file = tc->cpu;
3832 iter->tr = tc->tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003833 iter->trace_buffer = &tc->tr->trace_buffer;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003834 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003835 filp->private_data = iter;
3836
Steven Rostedt107bad82008-05-12 21:21:01 +02003837 if (iter->trace->pipe_open)
3838 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02003839
Arnd Bergmannb4447862010-07-07 23:40:11 +02003840 nonseekable_open(inode, filp);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003841out:
3842 mutex_unlock(&trace_types_lock);
3843 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003844
3845fail:
3846 kfree(iter->trace);
3847 kfree(iter);
3848 mutex_unlock(&trace_types_lock);
3849 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003850}
3851
3852static int tracing_release_pipe(struct inode *inode, struct file *file)
3853{
3854 struct trace_iterator *iter = file->private_data;
3855
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003856 mutex_lock(&trace_types_lock);
3857
Steven Rostedt29bf4a52009-12-09 12:37:43 -05003858 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05003859 iter->trace->pipe_close(iter);
3860
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003861 mutex_unlock(&trace_types_lock);
3862
Rusty Russell44623442009-01-01 10:12:23 +10303863 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003864 mutex_destroy(&iter->mutex);
3865 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003866 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003867
3868 return 0;
3869}
3870
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003871static unsigned int
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05003872trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003873{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003874 /* Iterators are static, they should be filled or empty */
3875 if (trace_buffer_iter(iter, iter->cpu_file))
3876 return POLLIN | POLLRDNORM;
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003877
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003878 if (trace_flags & TRACE_ITER_BLOCK)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003879 /*
3880 * Always select as readable when in blocking mode
3881 */
3882 return POLLIN | POLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003883 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003884 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003885 filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003886}
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003887
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05003888static unsigned int
3889tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3890{
3891 struct trace_iterator *iter = filp->private_data;
3892
3893 return trace_poll(iter, filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003894}
3895
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003896/*
3897 * This is a make-shift waitqueue.
3898 * A tracer might use this callback on some rare cases:
3899 *
3900 * 1) the current tracer might hold the runqueue lock when it wakes up
3901 * a reader, hence a deadlock (sched, function, and function graph tracers)
3902 * 2) the function tracers, trace all functions, we don't want
3903 * the overhead of calling wake_up and friends
3904 * (and tracing them too)
3905 *
3906 * Anyway, this is really very primitive wakeup.
3907 */
3908void poll_wait_pipe(struct trace_iterator *iter)
3909{
3910 set_current_state(TASK_INTERRUPTIBLE);
3911 /* sleep for 100 msecs, and try again. */
3912 schedule_timeout(HZ / 10);
3913}
3914
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003915/* Must be called with trace_types_lock mutex held. */
3916static int tracing_wait_pipe(struct file *filp)
3917{
3918 struct trace_iterator *iter = filp->private_data;
3919
3920 while (trace_empty(iter)) {
3921
3922 if ((filp->f_flags & O_NONBLOCK)) {
3923 return -EAGAIN;
3924 }
3925
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003926 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003927
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003928 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003929
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003930 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003931
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003932 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003933 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003934
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003935 /*
Liu Bo250bfd32013-01-14 10:54:11 +08003936 * We block until we read something and tracing is disabled.
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003937 * We still block if tracing is disabled, but we have never
3938 * read anything. This allows a user to cat this file, and
3939 * then enable tracing. But after we have read something,
3940 * we give an EOF when tracing is again disabled.
3941 *
3942 * iter->pos will be 0 if we haven't read anything.
3943 */
Liu Bo250bfd32013-01-14 10:54:11 +08003944 if (!tracing_is_enabled() && iter->pos)
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003945 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003946 }
3947
3948 return 1;
3949}
3950
Steven Rostedtb3806b42008-05-12 21:20:46 +02003951/*
3952 * Consumer reader.
3953 */
3954static ssize_t
3955tracing_read_pipe(struct file *filp, char __user *ubuf,
3956 size_t cnt, loff_t *ppos)
3957{
3958 struct trace_iterator *iter = filp->private_data;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003959 struct trace_array *tr = iter->tr;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003960 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003961
3962 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003963 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3964 if (sret != -EBUSY)
3965 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003966
Steven Rostedtf9520752009-03-02 14:04:40 -05003967 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003968
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003969 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02003970 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003971 if (unlikely(iter->trace->name != tr->current_trace->name))
3972 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003973 mutex_unlock(&trace_types_lock);
3974
3975 /*
3976 * Avoid more than one consumer on a single file descriptor
3977 * This is just a matter of traces coherency, the ring buffer itself
3978 * is protected.
3979 */
3980 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003981 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003982 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3983 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02003984 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02003985 }
3986
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003987waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003988 sret = tracing_wait_pipe(filp);
3989 if (sret <= 0)
3990 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003991
3992 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003993 if (trace_empty(iter)) {
3994 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02003995 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003996 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003997
3998 if (cnt >= PAGE_SIZE)
3999 cnt = PAGE_SIZE - 1;
4000
Steven Rostedt53d0aa72008-05-12 21:21:01 +02004001 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02004002 memset(&iter->seq, 0,
4003 sizeof(struct trace_iterator) -
4004 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02004005 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004006
Lai Jiangshan4f535962009-05-18 19:35:34 +08004007 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004008 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05004009 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02004010 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02004011 int len = iter->seq.len;
4012
Ingo Molnarf9896bf2008-05-12 21:20:47 +02004013 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02004014 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02004015 /* don't print partial lines */
4016 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004017 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02004018 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01004019 if (ret != TRACE_TYPE_NO_CONSUME)
4020 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02004021
4022 if (iter->seq.len >= cnt)
4023 break;
Jiri Olsaee5e51f2011-03-25 12:05:18 +01004024
4025 /*
4026 * Setting the full flag means we reached the trace_seq buffer
4027 * size and we should leave by partial output condition above.
4028 * One of the trace_seq_* functions is not used properly.
4029 */
4030 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4031 iter->ent->type);
Steven Rostedtb3806b42008-05-12 21:20:46 +02004032 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004033 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08004034 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02004035
Steven Rostedtb3806b42008-05-12 21:20:46 +02004036 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02004037 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4038 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05004039 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02004040
4041 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004042 * If there was nothing to send to user, in spite of consuming trace
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02004043 * entries, go back to wait for more entries.
4044 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02004045 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02004046 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004047
Steven Rostedt107bad82008-05-12 21:21:01 +02004048out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004049 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02004050
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02004051 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004052}
4053
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004054static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
4055 struct pipe_buffer *buf)
4056{
4057 __free_page(buf->page);
4058}
4059
4060static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4061 unsigned int idx)
4062{
4063 __free_page(spd->pages[idx]);
4064}
4065
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08004066static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05004067 .can_merge = 0,
4068 .map = generic_pipe_buf_map,
4069 .unmap = generic_pipe_buf_unmap,
4070 .confirm = generic_pipe_buf_confirm,
4071 .release = tracing_pipe_buf_release,
4072 .steal = generic_pipe_buf_steal,
4073 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004074};
4075
Steven Rostedt34cd4992009-02-09 12:06:29 -05004076static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01004077tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05004078{
4079 size_t count;
4080 int ret;
4081
4082 /* Seq buffer is page-sized, exactly what we need. */
4083 for (;;) {
4084 count = iter->seq.len;
4085 ret = print_trace_line(iter);
4086 count = iter->seq.len - count;
4087 if (rem < count) {
4088 rem = 0;
4089 iter->seq.len -= count;
4090 break;
4091 }
4092 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4093 iter->seq.len -= count;
4094 break;
4095 }
4096
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08004097 if (ret != TRACE_TYPE_NO_CONSUME)
4098 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05004099 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05004100 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05004101 rem = 0;
4102 iter->ent = NULL;
4103 break;
4104 }
4105 }
4106
4107 return rem;
4108}
4109
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004110static ssize_t tracing_splice_read_pipe(struct file *filp,
4111 loff_t *ppos,
4112 struct pipe_inode_info *pipe,
4113 size_t len,
4114 unsigned int flags)
4115{
Jens Axboe35f3d142010-05-20 10:43:18 +02004116 struct page *pages_def[PIPE_DEF_BUFFERS];
4117 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004118 struct trace_iterator *iter = filp->private_data;
4119 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02004120 .pages = pages_def,
4121 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05004122 .nr_pages = 0, /* This gets updated below. */
Eric Dumazet047fe362012-06-12 15:24:40 +02004123 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt34cd4992009-02-09 12:06:29 -05004124 .flags = flags,
4125 .ops = &tracing_pipe_buf_ops,
4126 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004127 };
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004128 struct trace_array *tr = iter->tr;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004129 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05004130 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004131 unsigned int i;
4132
Jens Axboe35f3d142010-05-20 10:43:18 +02004133 if (splice_grow_spd(pipe, &spd))
4134 return -ENOMEM;
4135
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004136 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004137 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004138 if (unlikely(iter->trace->name != tr->current_trace->name))
4139 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004140 mutex_unlock(&trace_types_lock);
4141
4142 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004143
4144 if (iter->trace->splice_read) {
4145 ret = iter->trace->splice_read(iter, filp,
4146 ppos, pipe, len, flags);
4147 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05004148 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004149 }
4150
4151 ret = tracing_wait_pipe(filp);
4152 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05004153 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004154
Jason Wessel955b61e2010-08-05 09:22:23 -05004155 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004156 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05004157 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004158 }
4159
Lai Jiangshan4f535962009-05-18 19:35:34 +08004160 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004161 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08004162
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004163 /* Fill as many pages as possible. */
Jens Axboe35f3d142010-05-20 10:43:18 +02004164 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4165 spd.pages[i] = alloc_page(GFP_KERNEL);
4166 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05004167 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004168
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01004169 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004170
4171 /* Copy the data into the page, so we can start over. */
4172 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02004173 page_address(spd.pages[i]),
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004174 iter->seq.len);
4175 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02004176 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004177 break;
4178 }
Jens Axboe35f3d142010-05-20 10:43:18 +02004179 spd.partial[i].offset = 0;
4180 spd.partial[i].len = iter->seq.len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004181
Steven Rostedtf9520752009-03-02 14:04:40 -05004182 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004183 }
4184
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004185 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08004186 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004187 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004188
4189 spd.nr_pages = i;
4190
Jens Axboe35f3d142010-05-20 10:43:18 +02004191 ret = splice_to_pipe(pipe, &spd);
4192out:
Eric Dumazet047fe362012-06-12 15:24:40 +02004193 splice_shrink_spd(&spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02004194 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004195
Steven Rostedt34cd4992009-02-09 12:06:29 -05004196out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004197 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02004198 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004199}
4200
Steven Rostedta98a3c32008-05-12 21:20:59 +02004201static ssize_t
4202tracing_entries_read(struct file *filp, char __user *ubuf,
4203 size_t cnt, loff_t *ppos)
4204{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004205 struct trace_cpu *tc = filp->private_data;
4206 struct trace_array *tr = tc->tr;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004207 char buf[64];
4208 int r = 0;
4209 ssize_t ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004210
Steven Rostedtdb526ca2009-03-12 13:53:25 -04004211 mutex_lock(&trace_types_lock);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004212
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004213 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004214 int cpu, buf_size_same;
4215 unsigned long size;
4216
4217 size = 0;
4218 buf_size_same = 1;
4219 /* check if all cpu sizes are same */
4220 for_each_tracing_cpu(cpu) {
4221 /* fill in the size from first enabled cpu */
4222 if (size == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004223 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4224 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004225 buf_size_same = 0;
4226 break;
4227 }
4228 }
4229
4230 if (buf_size_same) {
4231 if (!ring_buffer_expanded)
4232 r = sprintf(buf, "%lu (expanded: %lu)\n",
4233 size >> 10,
4234 trace_buf_size >> 10);
4235 else
4236 r = sprintf(buf, "%lu\n", size >> 10);
4237 } else
4238 r = sprintf(buf, "X\n");
4239 } else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004240 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004241
Steven Rostedtdb526ca2009-03-12 13:53:25 -04004242 mutex_unlock(&trace_types_lock);
4243
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004244 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4245 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004246}
4247
4248static ssize_t
4249tracing_entries_write(struct file *filp, const char __user *ubuf,
4250 size_t cnt, loff_t *ppos)
4251{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004252 struct trace_cpu *tc = filp->private_data;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004253 unsigned long val;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004254 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004255
Peter Huewe22fe9b52011-06-07 21:58:27 +02004256 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4257 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02004258 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004259
4260 /* must have at least 1 entry */
4261 if (!val)
4262 return -EINVAL;
4263
Steven Rostedt1696b2b2008-11-13 00:09:35 -05004264 /* value is in KB */
4265 val <<= 10;
4266
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004267 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004268 if (ret < 0)
4269 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004270
Jiri Olsacf8517c2009-10-23 19:36:16 -04004271 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004272
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004273 return cnt;
4274}
Steven Rostedtbf5e6512008-11-10 21:46:00 -05004275
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004276static ssize_t
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07004277tracing_total_entries_read(struct file *filp, char __user *ubuf,
4278 size_t cnt, loff_t *ppos)
4279{
4280 struct trace_array *tr = filp->private_data;
4281 char buf[64];
4282 int r, cpu;
4283 unsigned long size = 0, expanded_size = 0;
4284
4285 mutex_lock(&trace_types_lock);
4286 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004287 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07004288 if (!ring_buffer_expanded)
4289 expanded_size += trace_buf_size >> 10;
4290 }
4291 if (ring_buffer_expanded)
4292 r = sprintf(buf, "%lu\n", size);
4293 else
4294 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4295 mutex_unlock(&trace_types_lock);
4296
4297 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4298}
4299
4300static ssize_t
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004301tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4302 size_t cnt, loff_t *ppos)
4303{
4304 /*
4305 * There is no need to read what the user has written, this function
4306 * is just to make sure that there is no error when "echo" is used
4307 */
4308
4309 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004310
4311 return cnt;
4312}
4313
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004314static int
4315tracing_free_buffer_release(struct inode *inode, struct file *filp)
4316{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004317 struct trace_array *tr = inode->i_private;
4318
Steven Rostedtcf30cf62011-06-14 22:44:07 -04004319 /* disable tracing ? */
4320 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4321 tracing_off();
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004322 /* resize the ring buffer to 0 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004323 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004324
4325 return 0;
4326}
4327
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004328static ssize_t
4329tracing_mark_write(struct file *filp, const char __user *ubuf,
4330 size_t cnt, loff_t *fpos)
4331{
Steven Rostedtd696b582011-09-22 11:50:27 -04004332 unsigned long addr = (unsigned long)ubuf;
4333 struct ring_buffer_event *event;
4334 struct ring_buffer *buffer;
4335 struct print_entry *entry;
4336 unsigned long irq_flags;
4337 struct page *pages[2];
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004338 void *map_page[2];
Steven Rostedtd696b582011-09-22 11:50:27 -04004339 int nr_pages = 1;
4340 ssize_t written;
Steven Rostedtd696b582011-09-22 11:50:27 -04004341 int offset;
4342 int size;
4343 int len;
4344 int ret;
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004345 int i;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004346
Steven Rostedtc76f0692008-11-07 22:36:02 -05004347 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004348 return -EINVAL;
4349
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -07004350 if (!(trace_flags & TRACE_ITER_MARKERS))
4351 return -EINVAL;
4352
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004353 if (cnt > TRACE_BUF_SIZE)
4354 cnt = TRACE_BUF_SIZE;
4355
Steven Rostedtd696b582011-09-22 11:50:27 -04004356 /*
4357 * Userspace is injecting traces into the kernel trace buffer.
4358 * We want to be as non intrusive as possible.
4359 * To do so, we do not want to allocate any special buffers
4360 * or take any locks, but instead write the userspace data
4361 * straight into the ring buffer.
4362 *
4363 * First we need to pin the userspace buffer into memory,
4364 * which, most likely it is, because it just referenced it.
4365 * But there's no guarantee that it is. By using get_user_pages_fast()
4366 * and kmap_atomic/kunmap_atomic() we can get access to the
4367 * pages directly. We then write the data directly into the
4368 * ring buffer.
4369 */
4370 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004371
Steven Rostedtd696b582011-09-22 11:50:27 -04004372 /* check if we cross pages */
4373 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4374 nr_pages = 2;
4375
4376 offset = addr & (PAGE_SIZE - 1);
4377 addr &= PAGE_MASK;
4378
4379 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4380 if (ret < nr_pages) {
4381 while (--ret >= 0)
4382 put_page(pages[ret]);
4383 written = -EFAULT;
4384 goto out;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004385 }
4386
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004387 for (i = 0; i < nr_pages; i++)
4388 map_page[i] = kmap_atomic(pages[i]);
Steven Rostedtd696b582011-09-22 11:50:27 -04004389
4390 local_save_flags(irq_flags);
4391 size = sizeof(*entry) + cnt + 2; /* possible \n added */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004392 buffer = global_trace.trace_buffer.buffer;
Steven Rostedtd696b582011-09-22 11:50:27 -04004393 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4394 irq_flags, preempt_count());
4395 if (!event) {
4396 /* Ring buffer disabled, return as if not open for write */
4397 written = -EBADF;
4398 goto out_unlock;
4399 }
4400
4401 entry = ring_buffer_event_data(event);
4402 entry->ip = _THIS_IP_;
4403
4404 if (nr_pages == 2) {
4405 len = PAGE_SIZE - offset;
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004406 memcpy(&entry->buf, map_page[0] + offset, len);
4407 memcpy(&entry->buf[len], map_page[1], cnt - len);
Steven Rostedtd696b582011-09-22 11:50:27 -04004408 } else
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004409 memcpy(&entry->buf, map_page[0] + offset, cnt);
Steven Rostedtd696b582011-09-22 11:50:27 -04004410
4411 if (entry->buf[cnt - 1] != '\n') {
4412 entry->buf[cnt] = '\n';
4413 entry->buf[cnt + 1] = '\0';
4414 } else
4415 entry->buf[cnt] = '\0';
4416
Steven Rostedt7ffbd482012-10-11 12:14:25 -04004417 __buffer_unlock_commit(buffer, event);
Steven Rostedtd696b582011-09-22 11:50:27 -04004418
4419 written = cnt;
4420
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02004421 *fpos += written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004422
Steven Rostedtd696b582011-09-22 11:50:27 -04004423 out_unlock:
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004424 for (i = 0; i < nr_pages; i++){
4425 kunmap_atomic(map_page[i]);
4426 put_page(pages[i]);
4427 }
Steven Rostedtd696b582011-09-22 11:50:27 -04004428 out:
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02004429 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004430}
4431
Li Zefan13f16d22009-12-08 11:16:11 +08004432static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08004433{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004434 struct trace_array *tr = m->private;
Zhaolei5079f322009-08-25 16:12:56 +08004435 int i;
4436
4437 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08004438 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08004439 "%s%s%s%s", i ? " " : "",
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004440 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4441 i == tr->clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08004442 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08004443
Li Zefan13f16d22009-12-08 11:16:11 +08004444 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08004445}
4446
4447static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4448 size_t cnt, loff_t *fpos)
4449{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004450 struct seq_file *m = filp->private_data;
4451 struct trace_array *tr = m->private;
Zhaolei5079f322009-08-25 16:12:56 +08004452 char buf[64];
4453 const char *clockstr;
4454 int i;
4455
4456 if (cnt >= sizeof(buf))
4457 return -EINVAL;
4458
4459 if (copy_from_user(&buf, ubuf, cnt))
4460 return -EFAULT;
4461
4462 buf[cnt] = 0;
4463
4464 clockstr = strstrip(buf);
4465
4466 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4467 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4468 break;
4469 }
4470 if (i == ARRAY_SIZE(trace_clocks))
4471 return -EINVAL;
4472
Zhaolei5079f322009-08-25 16:12:56 +08004473 mutex_lock(&trace_types_lock);
4474
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004475 tr->clock_id = i;
4476
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004477 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
Zhaolei5079f322009-08-25 16:12:56 +08004478
David Sharp60303ed2012-10-11 16:27:52 -07004479 /*
4480 * New clock may not be consistent with the previous clock.
4481 * Reset the buffer so that it doesn't have incomparable timestamps.
4482 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004483 tracing_reset_online_cpus(&global_trace.trace_buffer);
4484
4485#ifdef CONFIG_TRACER_MAX_TRACE
4486 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4487 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4488 tracing_reset_online_cpus(&global_trace.max_buffer);
4489#endif
David Sharp60303ed2012-10-11 16:27:52 -07004490
Zhaolei5079f322009-08-25 16:12:56 +08004491 mutex_unlock(&trace_types_lock);
4492
4493 *fpos += cnt;
4494
4495 return cnt;
4496}
4497
Li Zefan13f16d22009-12-08 11:16:11 +08004498static int tracing_clock_open(struct inode *inode, struct file *file)
4499{
4500 if (tracing_disabled)
4501 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004502
4503 return single_open(file, tracing_clock_show, inode->i_private);
Li Zefan13f16d22009-12-08 11:16:11 +08004504}
4505
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004506struct ftrace_buffer_info {
4507 struct trace_iterator iter;
4508 void *spare;
4509 unsigned int read;
4510};
4511
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004512#ifdef CONFIG_TRACER_SNAPSHOT
4513static int tracing_snapshot_open(struct inode *inode, struct file *file)
4514{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004515 struct trace_cpu *tc = inode->i_private;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004516 struct trace_iterator *iter;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004517 struct seq_file *m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004518 int ret = 0;
4519
4520 if (file->f_mode & FMODE_READ) {
4521 iter = __tracing_open(inode, file, true);
4522 if (IS_ERR(iter))
4523 ret = PTR_ERR(iter);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004524 } else {
4525 /* Writes still need the seq_file to hold the private data */
4526 m = kzalloc(sizeof(*m), GFP_KERNEL);
4527 if (!m)
4528 return -ENOMEM;
4529 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4530 if (!iter) {
4531 kfree(m);
4532 return -ENOMEM;
4533 }
4534 iter->tr = tc->tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004535 iter->trace_buffer = &tc->tr->max_buffer;
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004536 iter->cpu_file = tc->cpu;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004537 m->private = iter;
4538 file->private_data = m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004539 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004540
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004541 return ret;
4542}
4543
4544static ssize_t
4545tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4546 loff_t *ppos)
4547{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004548 struct seq_file *m = filp->private_data;
4549 struct trace_iterator *iter = m->private;
4550 struct trace_array *tr = iter->tr;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004551 unsigned long val;
4552 int ret;
4553
4554 ret = tracing_update_buffers();
4555 if (ret < 0)
4556 return ret;
4557
4558 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4559 if (ret)
4560 return ret;
4561
4562 mutex_lock(&trace_types_lock);
4563
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004564 if (tr->current_trace->use_max_tr) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004565 ret = -EBUSY;
4566 goto out;
4567 }
4568
4569 switch (val) {
4570 case 0:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004571 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4572 ret = -EINVAL;
4573 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004574 }
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04004575 if (tr->allocated_snapshot)
4576 free_snapshot(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004577 break;
4578 case 1:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004579/* Only allow per-cpu swap if the ring buffer supports it */
4580#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4581 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4582 ret = -EINVAL;
4583 break;
4584 }
4585#endif
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05004586 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04004587 ret = alloc_snapshot(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004588 if (ret < 0)
4589 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004590 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004591 local_irq_disable();
4592 /* Now, we're going to swap */
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004593 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05004594 update_max_tr(tr, current, smp_processor_id());
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004595 else
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05004596 update_max_tr_single(tr, current, iter->cpu_file);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004597 local_irq_enable();
4598 break;
4599 default:
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05004600 if (tr->allocated_snapshot) {
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004601 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4602 tracing_reset_online_cpus(&tr->max_buffer);
4603 else
4604 tracing_reset(&tr->max_buffer, iter->cpu_file);
4605 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004606 break;
4607 }
4608
4609 if (ret >= 0) {
4610 *ppos += cnt;
4611 ret = cnt;
4612 }
4613out:
4614 mutex_unlock(&trace_types_lock);
4615 return ret;
4616}
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004617
4618static int tracing_snapshot_release(struct inode *inode, struct file *file)
4619{
4620 struct seq_file *m = file->private_data;
4621
4622 if (file->f_mode & FMODE_READ)
4623 return tracing_release(inode, file);
4624
4625 /* If write only, the seq_file is just a stub */
4626 if (m)
4627 kfree(m->private);
4628 kfree(m);
4629
4630 return 0;
4631}
4632
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004633static int tracing_buffers_open(struct inode *inode, struct file *filp);
4634static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4635 size_t count, loff_t *ppos);
4636static int tracing_buffers_release(struct inode *inode, struct file *file);
4637static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4638 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4639
4640static int snapshot_raw_open(struct inode *inode, struct file *filp)
4641{
4642 struct ftrace_buffer_info *info;
4643 int ret;
4644
4645 ret = tracing_buffers_open(inode, filp);
4646 if (ret < 0)
4647 return ret;
4648
4649 info = filp->private_data;
4650
4651 if (info->iter.trace->use_max_tr) {
4652 tracing_buffers_release(inode, filp);
4653 return -EBUSY;
4654 }
4655
4656 info->iter.snapshot = true;
4657 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4658
4659 return ret;
4660}
4661
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004662#endif /* CONFIG_TRACER_SNAPSHOT */
4663
4664
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004665static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004666 .open = tracing_open_generic,
4667 .read = tracing_max_lat_read,
4668 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004669 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004670};
4671
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004672static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004673 .open = tracing_open_generic,
4674 .read = tracing_set_trace_read,
4675 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004676 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004677};
4678
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004679static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004680 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02004681 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004682 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004683 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004684 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004685 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02004686};
4687
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004688static const struct file_operations tracing_entries_fops = {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004689 .open = tracing_open_generic,
Steven Rostedta98a3c32008-05-12 21:20:59 +02004690 .read = tracing_entries_read,
4691 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004692 .llseek = generic_file_llseek,
Steven Rostedta98a3c32008-05-12 21:20:59 +02004693};
4694
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07004695static const struct file_operations tracing_total_entries_fops = {
4696 .open = tracing_open_generic,
4697 .read = tracing_total_entries_read,
4698 .llseek = generic_file_llseek,
4699};
4700
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004701static const struct file_operations tracing_free_buffer_fops = {
4702 .write = tracing_free_buffer_write,
4703 .release = tracing_free_buffer_release,
4704};
4705
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004706static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004707 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004708 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004709 .llseek = generic_file_llseek,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004710};
4711
Zhaolei5079f322009-08-25 16:12:56 +08004712static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08004713 .open = tracing_clock_open,
4714 .read = seq_read,
4715 .llseek = seq_lseek,
4716 .release = single_release,
Zhaolei5079f322009-08-25 16:12:56 +08004717 .write = tracing_clock_write,
4718};
4719
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004720#ifdef CONFIG_TRACER_SNAPSHOT
4721static const struct file_operations snapshot_fops = {
4722 .open = tracing_snapshot_open,
4723 .read = seq_read,
4724 .write = tracing_snapshot_write,
4725 .llseek = tracing_seek,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004726 .release = tracing_snapshot_release,
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004727};
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004728
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004729static const struct file_operations snapshot_raw_fops = {
4730 .open = snapshot_raw_open,
4731 .read = tracing_buffers_read,
4732 .release = tracing_buffers_release,
4733 .splice_read = tracing_buffers_splice_read,
4734 .llseek = no_llseek,
Steven Rostedt2cadf912008-12-01 22:20:19 -05004735};
4736
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004737#endif /* CONFIG_TRACER_SNAPSHOT */
4738
Steven Rostedt2cadf912008-12-01 22:20:19 -05004739static int tracing_buffers_open(struct inode *inode, struct file *filp)
4740{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004741 struct trace_cpu *tc = inode->i_private;
4742 struct trace_array *tr = tc->tr;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004743 struct ftrace_buffer_info *info;
4744
4745 if (tracing_disabled)
4746 return -ENODEV;
4747
4748 info = kzalloc(sizeof(*info), GFP_KERNEL);
4749 if (!info)
4750 return -ENOMEM;
4751
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05004752 mutex_lock(&trace_types_lock);
4753
4754 tr->ref++;
4755
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004756 info->iter.tr = tr;
4757 info->iter.cpu_file = tc->cpu;
Steven Rostedtb6273442013-02-28 13:44:11 -05004758 info->iter.trace = tr->current_trace;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004759 info->iter.trace_buffer = &tr->trace_buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004760 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004761 /* Force reading ring buffer for first read */
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004762 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004763
4764 filp->private_data = info;
4765
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05004766 mutex_unlock(&trace_types_lock);
4767
Lai Jiangshand1e7e022009-04-02 15:16:56 +08004768 return nonseekable_open(inode, filp);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004769}
4770
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004771static unsigned int
4772tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4773{
4774 struct ftrace_buffer_info *info = filp->private_data;
4775 struct trace_iterator *iter = &info->iter;
4776
4777 return trace_poll(iter, filp, poll_table);
4778}
4779
Steven Rostedt2cadf912008-12-01 22:20:19 -05004780static ssize_t
4781tracing_buffers_read(struct file *filp, char __user *ubuf,
4782 size_t count, loff_t *ppos)
4783{
4784 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004785 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004786 ssize_t ret;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004787 ssize_t size;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004788
Steven Rostedt2dc5d122009-03-04 19:10:05 -05004789 if (!count)
4790 return 0;
4791
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004792 mutex_lock(&trace_types_lock);
4793
4794#ifdef CONFIG_TRACER_MAX_TRACE
4795 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4796 size = -EBUSY;
4797 goto out_unlock;
4798 }
4799#endif
4800
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004801 if (!info->spare)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004802 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
4803 iter->cpu_file);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004804 size = -ENOMEM;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004805 if (!info->spare)
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004806 goto out_unlock;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004807
Steven Rostedt2cadf912008-12-01 22:20:19 -05004808 /* Do we have previous read data to read? */
4809 if (info->read < PAGE_SIZE)
4810 goto read;
4811
Steven Rostedtb6273442013-02-28 13:44:11 -05004812 again:
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004813 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004814 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
Steven Rostedt2cadf912008-12-01 22:20:19 -05004815 &info->spare,
4816 count,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004817 iter->cpu_file, 0);
4818 trace_access_unlock(iter->cpu_file);
Steven Rostedtb6273442013-02-28 13:44:11 -05004819
4820 if (ret < 0) {
4821 if (trace_empty(iter)) {
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004822 if ((filp->f_flags & O_NONBLOCK)) {
4823 size = -EAGAIN;
4824 goto out_unlock;
4825 }
4826 mutex_unlock(&trace_types_lock);
Steven Rostedtb6273442013-02-28 13:44:11 -05004827 iter->trace->wait_pipe(iter);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004828 mutex_lock(&trace_types_lock);
4829 if (signal_pending(current)) {
4830 size = -EINTR;
4831 goto out_unlock;
4832 }
Steven Rostedtb6273442013-02-28 13:44:11 -05004833 goto again;
4834 }
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004835 size = 0;
4836 goto out_unlock;
Steven Rostedtb6273442013-02-28 13:44:11 -05004837 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05004838
Steven Rostedt436fc282011-10-14 10:44:25 -04004839 info->read = 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05004840 read:
Steven Rostedt2cadf912008-12-01 22:20:19 -05004841 size = PAGE_SIZE - info->read;
4842 if (size > count)
4843 size = count;
4844
4845 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004846 if (ret == size) {
4847 size = -EFAULT;
4848 goto out_unlock;
4849 }
Steven Rostedt2dc5d122009-03-04 19:10:05 -05004850 size -= ret;
4851
Steven Rostedt2cadf912008-12-01 22:20:19 -05004852 *ppos += size;
4853 info->read += size;
4854
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004855 out_unlock:
4856 mutex_unlock(&trace_types_lock);
4857
Steven Rostedt2cadf912008-12-01 22:20:19 -05004858 return size;
4859}
4860
4861static int tracing_buffers_release(struct inode *inode, struct file *file)
4862{
4863 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004864 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004865
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05004866 mutex_lock(&trace_types_lock);
4867
4868 WARN_ON(!iter->tr->ref);
4869 iter->tr->ref--;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004870
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004871 if (info->spare)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004872 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004873 kfree(info);
4874
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05004875 mutex_unlock(&trace_types_lock);
4876
Steven Rostedt2cadf912008-12-01 22:20:19 -05004877 return 0;
4878}
4879
4880struct buffer_ref {
4881 struct ring_buffer *buffer;
4882 void *page;
4883 int ref;
4884};
4885
4886static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4887 struct pipe_buffer *buf)
4888{
4889 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4890
4891 if (--ref->ref)
4892 return;
4893
4894 ring_buffer_free_read_page(ref->buffer, ref->page);
4895 kfree(ref);
4896 buf->private = 0;
4897}
4898
Steven Rostedt2cadf912008-12-01 22:20:19 -05004899static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4900 struct pipe_buffer *buf)
4901{
4902 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4903
4904 ref->ref++;
4905}
4906
4907/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08004908static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05004909 .can_merge = 0,
4910 .map = generic_pipe_buf_map,
4911 .unmap = generic_pipe_buf_unmap,
4912 .confirm = generic_pipe_buf_confirm,
4913 .release = buffer_pipe_buf_release,
Masami Hiramatsud55cb6c2012-08-09 21:31:10 +09004914 .steal = generic_pipe_buf_steal,
Steven Rostedt2cadf912008-12-01 22:20:19 -05004915 .get = buffer_pipe_buf_get,
4916};
4917
4918/*
4919 * Callback from splice_to_pipe(), if we need to release some pages
4920 * at the end of the spd in case we error'ed out in filling the pipe.
4921 */
4922static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4923{
4924 struct buffer_ref *ref =
4925 (struct buffer_ref *)spd->partial[i].private;
4926
4927 if (--ref->ref)
4928 return;
4929
4930 ring_buffer_free_read_page(ref->buffer, ref->page);
4931 kfree(ref);
4932 spd->partial[i].private = 0;
4933}
4934
4935static ssize_t
4936tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4937 struct pipe_inode_info *pipe, size_t len,
4938 unsigned int flags)
4939{
4940 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004941 struct trace_iterator *iter = &info->iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02004942 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4943 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05004944 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02004945 .pages = pages_def,
4946 .partial = partial_def,
Eric Dumazet047fe362012-06-12 15:24:40 +02004947 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt2cadf912008-12-01 22:20:19 -05004948 .flags = flags,
4949 .ops = &buffer_pipe_buf_ops,
4950 .spd_release = buffer_spd_release,
4951 };
4952 struct buffer_ref *ref;
Steven Rostedt93459c62009-04-29 00:23:13 -04004953 int entries, size, i;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004954 ssize_t ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004955
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004956 mutex_lock(&trace_types_lock);
4957
4958#ifdef CONFIG_TRACER_MAX_TRACE
4959 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4960 ret = -EBUSY;
4961 goto out;
4962 }
4963#endif
4964
4965 if (splice_grow_spd(pipe, &spd)) {
4966 ret = -ENOMEM;
4967 goto out;
4968 }
Jens Axboe35f3d142010-05-20 10:43:18 +02004969
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08004970 if (*ppos & (PAGE_SIZE - 1)) {
Jens Axboe35f3d142010-05-20 10:43:18 +02004971 ret = -EINVAL;
4972 goto out;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08004973 }
4974
4975 if (len & (PAGE_SIZE - 1)) {
Jens Axboe35f3d142010-05-20 10:43:18 +02004976 if (len < PAGE_SIZE) {
4977 ret = -EINVAL;
4978 goto out;
4979 }
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08004980 len &= PAGE_MASK;
4981 }
4982
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004983 again:
4984 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004985 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt93459c62009-04-29 00:23:13 -04004986
Jens Axboe35f3d142010-05-20 10:43:18 +02004987 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05004988 struct page *page;
4989 int r;
4990
4991 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4992 if (!ref)
4993 break;
4994
Steven Rostedt7267fa62009-04-29 00:16:21 -04004995 ref->ref = 1;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004996 ref->buffer = iter->trace_buffer->buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004997 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004998 if (!ref->page) {
4999 kfree(ref);
5000 break;
5001 }
5002
5003 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005004 len, iter->cpu_file, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005005 if (r < 0) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005006 ring_buffer_free_read_page(ref->buffer, ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005007 kfree(ref);
5008 break;
5009 }
5010
5011 /*
5012 * zero out any left over data, this is going to
5013 * user land.
5014 */
5015 size = ring_buffer_page_len(ref->page);
5016 if (size < PAGE_SIZE)
5017 memset(ref->page + size, 0, PAGE_SIZE - size);
5018
5019 page = virt_to_page(ref->page);
5020
5021 spd.pages[i] = page;
5022 spd.partial[i].len = PAGE_SIZE;
5023 spd.partial[i].offset = 0;
5024 spd.partial[i].private = (unsigned long)ref;
5025 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08005026 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04005027
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005028 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005029 }
5030
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005031 trace_access_unlock(iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005032 spd.nr_pages = i;
5033
5034 /* did we read anything? */
5035 if (!spd.nr_pages) {
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005036 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05005037 ret = -EAGAIN;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005038 goto out;
5039 }
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005040 mutex_unlock(&trace_types_lock);
Steven Rostedtb6273442013-02-28 13:44:11 -05005041 iter->trace->wait_pipe(iter);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005042 mutex_lock(&trace_types_lock);
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005043 if (signal_pending(current)) {
5044 ret = -EINTR;
5045 goto out;
5046 }
5047 goto again;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005048 }
5049
5050 ret = splice_to_pipe(pipe, &spd);
Eric Dumazet047fe362012-06-12 15:24:40 +02005051 splice_shrink_spd(&spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02005052out:
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005053 mutex_unlock(&trace_types_lock);
5054
Steven Rostedt2cadf912008-12-01 22:20:19 -05005055 return ret;
5056}
5057
5058static const struct file_operations tracing_buffers_fops = {
5059 .open = tracing_buffers_open,
5060 .read = tracing_buffers_read,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005061 .poll = tracing_buffers_poll,
Steven Rostedt2cadf912008-12-01 22:20:19 -05005062 .release = tracing_buffers_release,
5063 .splice_read = tracing_buffers_splice_read,
5064 .llseek = no_llseek,
5065};
5066
Steven Rostedtc8d77182009-04-29 18:03:45 -04005067static ssize_t
5068tracing_stats_read(struct file *filp, char __user *ubuf,
5069 size_t count, loff_t *ppos)
5070{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005071 struct trace_cpu *tc = filp->private_data;
5072 struct trace_array *tr = tc->tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005073 struct trace_buffer *trace_buf = &tr->trace_buffer;
Steven Rostedtc8d77182009-04-29 18:03:45 -04005074 struct trace_seq *s;
5075 unsigned long cnt;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005076 unsigned long long t;
5077 unsigned long usec_rem;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005078 int cpu = tc->cpu;
Steven Rostedtc8d77182009-04-29 18:03:45 -04005079
Li Zefane4f2d102009-06-15 10:57:28 +08005080 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04005081 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01005082 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04005083
5084 trace_seq_init(s);
5085
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005086 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04005087 trace_seq_printf(s, "entries: %ld\n", cnt);
5088
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005089 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04005090 trace_seq_printf(s, "overrun: %ld\n", cnt);
5091
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005092 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04005093 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5094
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005095 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005096 trace_seq_printf(s, "bytes: %ld\n", cnt);
5097
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005098 if (trace_clocks[trace_clock_id].in_ns) {
5099 /* local or global for trace_clock */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005100 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005101 usec_rem = do_div(t, USEC_PER_SEC);
5102 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5103 t, usec_rem);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005104
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005105 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005106 usec_rem = do_div(t, USEC_PER_SEC);
5107 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5108 } else {
5109 /* counter or tsc mode for trace_clock */
5110 trace_seq_printf(s, "oldest event ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005111 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005112
5113 trace_seq_printf(s, "now ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005114 ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005115 }
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005116
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005117 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
Slava Pestov884bfe82011-07-15 14:23:58 -07005118 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5119
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005120 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05005121 trace_seq_printf(s, "read events: %ld\n", cnt);
5122
Steven Rostedtc8d77182009-04-29 18:03:45 -04005123 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5124
5125 kfree(s);
5126
5127 return count;
5128}
5129
5130static const struct file_operations tracing_stats_fops = {
5131 .open = tracing_open_generic,
5132 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005133 .llseek = generic_file_llseek,
Steven Rostedtc8d77182009-04-29 18:03:45 -04005134};
5135
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005136#ifdef CONFIG_DYNAMIC_FTRACE
5137
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005138int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005139{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005140 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005141}
5142
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005143static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005144tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005145 size_t cnt, loff_t *ppos)
5146{
Steven Rostedta26a2a22008-10-31 00:03:22 -04005147 static char ftrace_dyn_info_buffer[1024];
5148 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005149 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005150 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04005151 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005152 int r;
5153
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005154 mutex_lock(&dyn_info_mutex);
5155 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005156
Steven Rostedta26a2a22008-10-31 00:03:22 -04005157 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005158 buf[r++] = '\n';
5159
5160 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5161
5162 mutex_unlock(&dyn_info_mutex);
5163
5164 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005165}
5166
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005167static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005168 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005169 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005170 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005171};
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04005172#endif /* CONFIG_DYNAMIC_FTRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005173
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04005174#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5175static void
5176ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005177{
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04005178 tracing_snapshot();
5179}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005180
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04005181static void
5182ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5183{
5184 unsigned long *count = (long *)data;
5185
5186 if (!*count)
5187 return;
5188
5189 if (*count != -1)
5190 (*count)--;
5191
5192 tracing_snapshot();
5193}
5194
5195static int
5196ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5197 struct ftrace_probe_ops *ops, void *data)
5198{
5199 long count = (long)data;
5200
5201 seq_printf(m, "%ps:", (void *)ip);
5202
5203 seq_printf(m, "snapshot");
5204
5205 if (count == -1)
5206 seq_printf(m, ":unlimited\n");
5207 else
5208 seq_printf(m, ":count=%ld\n", count);
5209
5210 return 0;
5211}
5212
5213static struct ftrace_probe_ops snapshot_probe_ops = {
5214 .func = ftrace_snapshot,
5215 .print = ftrace_snapshot_print,
5216};
5217
5218static struct ftrace_probe_ops snapshot_count_probe_ops = {
5219 .func = ftrace_count_snapshot,
5220 .print = ftrace_snapshot_print,
5221};
5222
5223static int
5224ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5225 char *glob, char *cmd, char *param, int enable)
5226{
5227 struct ftrace_probe_ops *ops;
5228 void *count = (void *)-1;
5229 char *number;
5230 int ret;
5231
5232 /* hash funcs only work with set_ftrace_filter */
5233 if (!enable)
5234 return -EINVAL;
5235
5236 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5237
5238 if (glob[0] == '!') {
5239 unregister_ftrace_function_probe_func(glob+1, ops);
5240 return 0;
5241 }
5242
5243 if (!param)
5244 goto out_reg;
5245
5246 number = strsep(&param, ":");
5247
5248 if (!strlen(number))
5249 goto out_reg;
5250
5251 /*
5252 * We use the callback data field (which is a pointer)
5253 * as our counter.
5254 */
5255 ret = kstrtoul(number, 0, (unsigned long *)&count);
5256 if (ret)
5257 return ret;
5258
5259 out_reg:
5260 ret = register_ftrace_function_probe(glob, ops, count);
5261
5262 if (ret >= 0)
5263 alloc_snapshot(&global_trace);
5264
5265 return ret < 0 ? ret : 0;
5266}
5267
5268static struct ftrace_func_command ftrace_snapshot_cmd = {
5269 .name = "snapshot",
5270 .func = ftrace_trace_snapshot_callback,
5271};
5272
5273static int register_snapshot_cmd(void)
5274{
5275 return register_ftrace_command(&ftrace_snapshot_cmd);
5276}
5277#else
5278static inline int register_snapshot_cmd(void) { return 0; }
5279#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005280
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005281struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005282{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005283 if (tr->dir)
5284 return tr->dir;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005285
Frederic Weisbecker3e1f60b2009-03-22 23:10:45 +01005286 if (!debugfs_initialized())
5287 return NULL;
5288
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005289 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5290 tr->dir = debugfs_create_dir("tracing", NULL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005291
zhangwei(Jovi)687c8782013-03-11 15:13:29 +08005292 if (!tr->dir)
5293 pr_warn_once("Could not create debugfs directory 'tracing'\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005294
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005295 return tr->dir;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005296}
5297
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005298struct dentry *tracing_init_dentry(void)
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005299{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005300 return tracing_init_dentry_tr(&global_trace);
5301}
5302
5303static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5304{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005305 struct dentry *d_tracer;
5306
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005307 if (tr->percpu_dir)
5308 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005309
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005310 d_tracer = tracing_init_dentry_tr(tr);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005311 if (!d_tracer)
5312 return NULL;
5313
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005314 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005315
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005316 WARN_ONCE(!tr->percpu_dir,
5317 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005318
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005319 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005320}
5321
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005322static void
5323tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005324{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005325 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005326 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005327 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04005328 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005329
Namhyung Kim0a3d7ce2012-04-23 10:11:57 +09005330 if (!d_percpu)
5331 return;
5332
Steven Rostedtdd49a382010-10-20 21:51:26 -04005333 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01005334 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5335 if (!d_cpu) {
5336 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5337 return;
5338 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005339
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01005340 /* per cpu trace_pipe */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005341 trace_create_file("trace_pipe", 0444, d_cpu,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005342 (void *)&data->trace_cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005343
5344 /* per cpu trace */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005345 trace_create_file("trace", 0644, d_cpu,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005346 (void *)&data->trace_cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04005347
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005348 trace_create_file("trace_pipe_raw", 0444, d_cpu,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005349 (void *)&data->trace_cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04005350
5351 trace_create_file("stats", 0444, d_cpu,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005352 (void *)&data->trace_cpu, &tracing_stats_fops);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005353
5354 trace_create_file("buffer_size_kb", 0444, d_cpu,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005355 (void *)&data->trace_cpu, &tracing_entries_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005356
5357#ifdef CONFIG_TRACER_SNAPSHOT
5358 trace_create_file("snapshot", 0644, d_cpu,
5359 (void *)&data->trace_cpu, &snapshot_fops);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005360
5361 trace_create_file("snapshot_raw", 0444, d_cpu,
5362 (void *)&data->trace_cpu, &snapshot_raw_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005363#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005364}
5365
Steven Rostedt60a11772008-05-12 21:20:44 +02005366#ifdef CONFIG_FTRACE_SELFTEST
5367/* Let selftest have access to static functions in this file */
5368#include "trace_selftest.c"
5369#endif
5370
Steven Rostedt577b7852009-02-26 23:43:05 -05005371struct trace_option_dentry {
5372 struct tracer_opt *opt;
5373 struct tracer_flags *flags;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005374 struct trace_array *tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05005375 struct dentry *entry;
5376};
5377
5378static ssize_t
5379trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5380 loff_t *ppos)
5381{
5382 struct trace_option_dentry *topt = filp->private_data;
5383 char *buf;
5384
5385 if (topt->flags->val & topt->opt->bit)
5386 buf = "1\n";
5387 else
5388 buf = "0\n";
5389
5390 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5391}
5392
5393static ssize_t
5394trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5395 loff_t *ppos)
5396{
5397 struct trace_option_dentry *topt = filp->private_data;
5398 unsigned long val;
Steven Rostedt577b7852009-02-26 23:43:05 -05005399 int ret;
5400
Peter Huewe22fe9b52011-06-07 21:58:27 +02005401 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5402 if (ret)
Steven Rostedt577b7852009-02-26 23:43:05 -05005403 return ret;
5404
Li Zefan8d18eaa2009-12-08 11:17:06 +08005405 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05005406 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08005407
5408 if (!!(topt->flags->val & topt->opt->bit) != val) {
5409 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005410 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05005411 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08005412 mutex_unlock(&trace_types_lock);
5413 if (ret)
5414 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05005415 }
5416
5417 *ppos += cnt;
5418
5419 return cnt;
5420}
5421
5422
5423static const struct file_operations trace_options_fops = {
5424 .open = tracing_open_generic,
5425 .read = trace_options_read,
5426 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005427 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05005428};
5429
Steven Rostedta8259072009-02-26 22:19:12 -05005430static ssize_t
5431trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5432 loff_t *ppos)
5433{
5434 long index = (long)filp->private_data;
5435 char *buf;
5436
5437 if (trace_flags & (1 << index))
5438 buf = "1\n";
5439 else
5440 buf = "0\n";
5441
5442 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5443}
5444
5445static ssize_t
5446trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5447 loff_t *ppos)
5448{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005449 struct trace_array *tr = &global_trace;
Steven Rostedta8259072009-02-26 22:19:12 -05005450 long index = (long)filp->private_data;
Steven Rostedta8259072009-02-26 22:19:12 -05005451 unsigned long val;
5452 int ret;
5453
Peter Huewe22fe9b52011-06-07 21:58:27 +02005454 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5455 if (ret)
Steven Rostedta8259072009-02-26 22:19:12 -05005456 return ret;
5457
Zhaoleif2d84b62009-08-07 18:55:48 +08005458 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05005459 return -EINVAL;
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04005460
5461 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005462 ret = set_tracer_flag(tr, 1 << index, val);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04005463 mutex_unlock(&trace_types_lock);
Steven Rostedta8259072009-02-26 22:19:12 -05005464
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005465 if (ret < 0)
5466 return ret;
5467
Steven Rostedta8259072009-02-26 22:19:12 -05005468 *ppos += cnt;
5469
5470 return cnt;
5471}
5472
Steven Rostedta8259072009-02-26 22:19:12 -05005473static const struct file_operations trace_options_core_fops = {
5474 .open = tracing_open_generic,
5475 .read = trace_options_core_read,
5476 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005477 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05005478};
5479
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005480struct dentry *trace_create_file(const char *name,
Al Virof4ae40a62011-07-24 04:33:43 -04005481 umode_t mode,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005482 struct dentry *parent,
5483 void *data,
5484 const struct file_operations *fops)
5485{
5486 struct dentry *ret;
5487
5488 ret = debugfs_create_file(name, mode, parent, data, fops);
5489 if (!ret)
5490 pr_warning("Could not create debugfs '%s' entry\n", name);
5491
5492 return ret;
5493}
5494
5495
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005496static struct dentry *trace_options_init_dentry(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05005497{
5498 struct dentry *d_tracer;
Steven Rostedta8259072009-02-26 22:19:12 -05005499
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005500 if (tr->options)
5501 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05005502
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005503 d_tracer = tracing_init_dentry_tr(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05005504 if (!d_tracer)
5505 return NULL;
5506
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005507 tr->options = debugfs_create_dir("options", d_tracer);
5508 if (!tr->options) {
Steven Rostedta8259072009-02-26 22:19:12 -05005509 pr_warning("Could not create debugfs directory 'options'\n");
5510 return NULL;
5511 }
5512
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005513 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05005514}
5515
Steven Rostedt577b7852009-02-26 23:43:05 -05005516static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005517create_trace_option_file(struct trace_array *tr,
5518 struct trace_option_dentry *topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05005519 struct tracer_flags *flags,
5520 struct tracer_opt *opt)
5521{
5522 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05005523
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005524 t_options = trace_options_init_dentry(tr);
Steven Rostedt577b7852009-02-26 23:43:05 -05005525 if (!t_options)
5526 return;
5527
5528 topt->flags = flags;
5529 topt->opt = opt;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005530 topt->tr = tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05005531
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005532 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05005533 &trace_options_fops);
5534
Steven Rostedt577b7852009-02-26 23:43:05 -05005535}
5536
5537static struct trace_option_dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005538create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
Steven Rostedt577b7852009-02-26 23:43:05 -05005539{
5540 struct trace_option_dentry *topts;
5541 struct tracer_flags *flags;
5542 struct tracer_opt *opts;
5543 int cnt;
5544
5545 if (!tracer)
5546 return NULL;
5547
5548 flags = tracer->flags;
5549
5550 if (!flags || !flags->opts)
5551 return NULL;
5552
5553 opts = flags->opts;
5554
5555 for (cnt = 0; opts[cnt].name; cnt++)
5556 ;
5557
Steven Rostedt0cfe8242009-02-27 10:51:10 -05005558 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05005559 if (!topts)
5560 return NULL;
5561
5562 for (cnt = 0; opts[cnt].name; cnt++)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005563 create_trace_option_file(tr, &topts[cnt], flags,
Steven Rostedt577b7852009-02-26 23:43:05 -05005564 &opts[cnt]);
5565
5566 return topts;
5567}
5568
5569static void
5570destroy_trace_option_files(struct trace_option_dentry *topts)
5571{
5572 int cnt;
5573
5574 if (!topts)
5575 return;
5576
5577 for (cnt = 0; topts[cnt].opt; cnt++) {
5578 if (topts[cnt].entry)
5579 debugfs_remove(topts[cnt].entry);
5580 }
5581
5582 kfree(topts);
5583}
5584
Steven Rostedta8259072009-02-26 22:19:12 -05005585static struct dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005586create_trace_option_core_file(struct trace_array *tr,
5587 const char *option, long index)
Steven Rostedta8259072009-02-26 22:19:12 -05005588{
5589 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05005590
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005591 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05005592 if (!t_options)
5593 return NULL;
5594
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005595 return trace_create_file(option, 0644, t_options, (void *)index,
Steven Rostedta8259072009-02-26 22:19:12 -05005596 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05005597}
5598
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005599static __init void create_trace_options_dir(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05005600{
5601 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05005602 int i;
5603
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005604 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05005605 if (!t_options)
5606 return;
5607
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005608 for (i = 0; trace_options[i]; i++)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005609 create_trace_option_core_file(tr, trace_options[i], i);
Steven Rostedta8259072009-02-26 22:19:12 -05005610}
5611
Steven Rostedt499e5472012-02-22 15:50:28 -05005612static ssize_t
5613rb_simple_read(struct file *filp, char __user *ubuf,
5614 size_t cnt, loff_t *ppos)
5615{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04005616 struct trace_array *tr = filp->private_data;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005617 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt499e5472012-02-22 15:50:28 -05005618 char buf[64];
5619 int r;
5620
5621 if (buffer)
5622 r = ring_buffer_record_is_on(buffer);
5623 else
5624 r = 0;
5625
5626 r = sprintf(buf, "%d\n", r);
5627
5628 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5629}
5630
5631static ssize_t
5632rb_simple_write(struct file *filp, const char __user *ubuf,
5633 size_t cnt, loff_t *ppos)
5634{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04005635 struct trace_array *tr = filp->private_data;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005636 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt499e5472012-02-22 15:50:28 -05005637 unsigned long val;
5638 int ret;
5639
5640 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5641 if (ret)
5642 return ret;
5643
5644 if (buffer) {
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05005645 mutex_lock(&trace_types_lock);
5646 if (val) {
Steven Rostedt499e5472012-02-22 15:50:28 -05005647 ring_buffer_record_on(buffer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005648 if (tr->current_trace->start)
5649 tr->current_trace->start(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05005650 } else {
Steven Rostedt499e5472012-02-22 15:50:28 -05005651 ring_buffer_record_off(buffer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005652 if (tr->current_trace->stop)
5653 tr->current_trace->stop(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05005654 }
5655 mutex_unlock(&trace_types_lock);
Steven Rostedt499e5472012-02-22 15:50:28 -05005656 }
5657
5658 (*ppos)++;
5659
5660 return cnt;
5661}
5662
5663static const struct file_operations rb_simple_fops = {
5664 .open = tracing_open_generic,
5665 .read = rb_simple_read,
5666 .write = rb_simple_write,
5667 .llseek = default_llseek,
5668};
5669
Steven Rostedt277ba042012-08-03 16:10:49 -04005670struct dentry *trace_instance_dir;
5671
5672static void
5673init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5674
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005675static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5676{
5677 int cpu;
5678
5679 for_each_tracing_cpu(cpu) {
5680 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5681 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5682 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5683 }
5684}
5685
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005686static int
5687allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
Steven Rostedt277ba042012-08-03 16:10:49 -04005688{
5689 enum ring_buffer_flags rb_flags;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005690
5691 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5692
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005693 buf->buffer = ring_buffer_alloc(size, rb_flags);
5694 if (!buf->buffer)
5695 return -ENOMEM;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005696
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005697 buf->data = alloc_percpu(struct trace_array_cpu);
5698 if (!buf->data) {
5699 ring_buffer_free(buf->buffer);
5700 return -ENOMEM;
5701 }
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005702
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005703 init_trace_buffers(tr, buf);
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005704
5705 /* Allocate the first page for all buffers */
5706 set_buffer_entries(&tr->trace_buffer,
5707 ring_buffer_size(tr->trace_buffer.buffer, 0));
5708
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005709 return 0;
5710}
5711
5712static int allocate_trace_buffers(struct trace_array *tr, int size)
5713{
5714 int ret;
5715
5716 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5717 if (ret)
5718 return ret;
5719
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005720#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005721 ret = allocate_trace_buffer(tr, &tr->max_buffer,
5722 allocate_snapshot ? size : 1);
5723 if (WARN_ON(ret)) {
5724 ring_buffer_free(tr->trace_buffer.buffer);
5725 free_percpu(tr->trace_buffer.data);
5726 return -ENOMEM;
5727 }
5728 tr->allocated_snapshot = allocate_snapshot;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005729
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005730 /*
5731 * Only the top level trace array gets its snapshot allocated
5732 * from the kernel command line.
5733 */
5734 allocate_snapshot = false;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005735#endif
5736 return 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005737}
5738
5739static int new_instance_create(const char *name)
5740{
Steven Rostedt277ba042012-08-03 16:10:49 -04005741 struct trace_array *tr;
5742 int ret;
Steven Rostedt277ba042012-08-03 16:10:49 -04005743
5744 mutex_lock(&trace_types_lock);
5745
5746 ret = -EEXIST;
5747 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5748 if (tr->name && strcmp(tr->name, name) == 0)
5749 goto out_unlock;
5750 }
5751
5752 ret = -ENOMEM;
5753 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5754 if (!tr)
5755 goto out_unlock;
5756
5757 tr->name = kstrdup(name, GFP_KERNEL);
5758 if (!tr->name)
5759 goto out_free_tr;
5760
5761 raw_spin_lock_init(&tr->start_lock);
5762
5763 tr->current_trace = &nop_trace;
5764
5765 INIT_LIST_HEAD(&tr->systems);
5766 INIT_LIST_HEAD(&tr->events);
5767
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005768 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
Steven Rostedt277ba042012-08-03 16:10:49 -04005769 goto out_free_tr;
5770
Steven Rostedt277ba042012-08-03 16:10:49 -04005771 /* Holder for file callbacks */
5772 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5773 tr->trace_cpu.tr = tr;
5774
5775 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5776 if (!tr->dir)
5777 goto out_free_tr;
5778
5779 ret = event_trace_add_tracer(tr->dir, tr);
5780 if (ret)
5781 goto out_free_tr;
5782
5783 init_tracer_debugfs(tr, tr->dir);
5784
5785 list_add(&tr->list, &ftrace_trace_arrays);
5786
5787 mutex_unlock(&trace_types_lock);
5788
5789 return 0;
5790
5791 out_free_tr:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005792 if (tr->trace_buffer.buffer)
5793 ring_buffer_free(tr->trace_buffer.buffer);
Steven Rostedt277ba042012-08-03 16:10:49 -04005794 kfree(tr->name);
5795 kfree(tr);
5796
5797 out_unlock:
5798 mutex_unlock(&trace_types_lock);
5799
5800 return ret;
5801
5802}
5803
Steven Rostedt0c8916c2012-08-07 16:14:16 -04005804static int instance_delete(const char *name)
5805{
5806 struct trace_array *tr;
5807 int found = 0;
5808 int ret;
5809
5810 mutex_lock(&trace_types_lock);
5811
5812 ret = -ENODEV;
5813 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5814 if (tr->name && strcmp(tr->name, name) == 0) {
5815 found = 1;
5816 break;
5817 }
5818 }
5819 if (!found)
5820 goto out_unlock;
5821
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05005822 ret = -EBUSY;
5823 if (tr->ref)
5824 goto out_unlock;
5825
Steven Rostedt0c8916c2012-08-07 16:14:16 -04005826 list_del(&tr->list);
5827
5828 event_trace_del_tracer(tr);
5829 debugfs_remove_recursive(tr->dir);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005830 free_percpu(tr->trace_buffer.data);
5831 ring_buffer_free(tr->trace_buffer.buffer);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04005832
5833 kfree(tr->name);
5834 kfree(tr);
5835
5836 ret = 0;
5837
5838 out_unlock:
5839 mutex_unlock(&trace_types_lock);
5840
5841 return ret;
5842}
5843
Steven Rostedt277ba042012-08-03 16:10:49 -04005844static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
5845{
5846 struct dentry *parent;
5847 int ret;
5848
5849 /* Paranoid: Make sure the parent is the "instances" directory */
5850 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5851 if (WARN_ON_ONCE(parent != trace_instance_dir))
5852 return -ENOENT;
5853
5854 /*
5855 * The inode mutex is locked, but debugfs_create_dir() will also
5856 * take the mutex. As the instances directory can not be destroyed
5857 * or changed in any other way, it is safe to unlock it, and
5858 * let the dentry try. If two users try to make the same dir at
5859 * the same time, then the new_instance_create() will determine the
5860 * winner.
5861 */
5862 mutex_unlock(&inode->i_mutex);
5863
5864 ret = new_instance_create(dentry->d_iname);
5865
5866 mutex_lock(&inode->i_mutex);
5867
5868 return ret;
5869}
5870
Steven Rostedt0c8916c2012-08-07 16:14:16 -04005871static int instance_rmdir(struct inode *inode, struct dentry *dentry)
5872{
5873 struct dentry *parent;
5874 int ret;
5875
5876 /* Paranoid: Make sure the parent is the "instances" directory */
5877 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5878 if (WARN_ON_ONCE(parent != trace_instance_dir))
5879 return -ENOENT;
5880
5881 /* The caller did a dget() on dentry */
5882 mutex_unlock(&dentry->d_inode->i_mutex);
5883
5884 /*
5885 * The inode mutex is locked, but debugfs_create_dir() will also
5886 * take the mutex. As the instances directory can not be destroyed
5887 * or changed in any other way, it is safe to unlock it, and
5888 * let the dentry try. If two users try to make the same dir at
5889 * the same time, then the instance_delete() will determine the
5890 * winner.
5891 */
5892 mutex_unlock(&inode->i_mutex);
5893
5894 ret = instance_delete(dentry->d_iname);
5895
5896 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
5897 mutex_lock(&dentry->d_inode->i_mutex);
5898
5899 return ret;
5900}
5901
Steven Rostedt277ba042012-08-03 16:10:49 -04005902static const struct inode_operations instance_dir_inode_operations = {
5903 .lookup = simple_lookup,
5904 .mkdir = instance_mkdir,
Steven Rostedt0c8916c2012-08-07 16:14:16 -04005905 .rmdir = instance_rmdir,
Steven Rostedt277ba042012-08-03 16:10:49 -04005906};
5907
5908static __init void create_trace_instances(struct dentry *d_tracer)
5909{
5910 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
5911 if (WARN_ON(!trace_instance_dir))
5912 return;
5913
5914 /* Hijack the dir inode operations, to allow mkdir */
5915 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
5916}
5917
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005918static void
5919init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
5920{
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05005921 int cpu;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005922
5923 trace_create_file("trace_options", 0644, d_tracer,
5924 tr, &tracing_iter_fops);
5925
5926 trace_create_file("trace", 0644, d_tracer,
5927 (void *)&tr->trace_cpu, &tracing_fops);
5928
5929 trace_create_file("trace_pipe", 0444, d_tracer,
5930 (void *)&tr->trace_cpu, &tracing_pipe_fops);
5931
5932 trace_create_file("buffer_size_kb", 0644, d_tracer,
5933 (void *)&tr->trace_cpu, &tracing_entries_fops);
5934
5935 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5936 tr, &tracing_total_entries_fops);
5937
5938 trace_create_file("free_buffer", 0644, d_tracer,
5939 tr, &tracing_free_buffer_fops);
5940
5941 trace_create_file("trace_marker", 0220, d_tracer,
5942 tr, &tracing_mark_fops);
5943
5944 trace_create_file("trace_clock", 0644, d_tracer, tr,
5945 &trace_clock_fops);
5946
5947 trace_create_file("tracing_on", 0644, d_tracer,
5948 tr, &rb_simple_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05005949
5950#ifdef CONFIG_TRACER_SNAPSHOT
5951 trace_create_file("snapshot", 0644, d_tracer,
5952 (void *)&tr->trace_cpu, &snapshot_fops);
5953#endif
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05005954
5955 for_each_tracing_cpu(cpu)
5956 tracing_init_debugfs_percpu(tr, cpu);
5957
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005958}
5959
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01005960static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005961{
5962 struct dentry *d_tracer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005963
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005964 trace_access_lock_init();
5965
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005966 d_tracer = tracing_init_dentry();
Namhyung Kimed6f1c92013-04-10 09:18:12 +09005967 if (!d_tracer)
5968 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005969
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005970 init_tracer_debugfs(&global_trace, d_tracer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005971
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005972 trace_create_file("tracing_cpumask", 0644, d_tracer,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005973 &global_trace, &tracing_cpumask_fops);
Ingo Molnarc7078de2008-05-12 21:20:52 +02005974
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005975 trace_create_file("available_tracers", 0444, d_tracer,
5976 &global_trace, &show_traces_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005977
Li Zefan339ae5d2009-04-17 10:34:30 +08005978 trace_create_file("current_tracer", 0644, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005979 &global_trace, &set_tracer_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005980
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04005981#ifdef CONFIG_TRACER_MAX_TRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005982 trace_create_file("tracing_max_latency", 0644, d_tracer,
5983 &tracing_max_latency, &tracing_max_lat_fops);
Tim Bird0e950172010-02-25 15:36:43 -08005984#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005985
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005986 trace_create_file("tracing_thresh", 0644, d_tracer,
5987 &tracing_thresh, &tracing_max_lat_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005988
Li Zefan339ae5d2009-04-17 10:34:30 +08005989 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005990 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02005991
Avadh Patel69abe6a2009-04-10 16:04:48 -04005992 trace_create_file("saved_cmdlines", 0444, d_tracer,
5993 NULL, &tracing_saved_cmdlines_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005994
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005995#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005996 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5997 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005998#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005999
Steven Rostedt277ba042012-08-03 16:10:49 -04006000 create_trace_instances(d_tracer);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006001
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006002 create_trace_options_dir(&global_trace);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006003
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01006004 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006005}
6006
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006007static int trace_panic_handler(struct notifier_block *this,
6008 unsigned long event, void *unused)
6009{
Steven Rostedt944ac422008-10-23 19:26:08 -04006010 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02006011 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006012 return NOTIFY_OK;
6013}
6014
6015static struct notifier_block trace_panic_notifier = {
6016 .notifier_call = trace_panic_handler,
6017 .next = NULL,
6018 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6019};
6020
6021static int trace_die_handler(struct notifier_block *self,
6022 unsigned long val,
6023 void *data)
6024{
6025 switch (val) {
6026 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04006027 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02006028 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006029 break;
6030 default:
6031 break;
6032 }
6033 return NOTIFY_OK;
6034}
6035
6036static struct notifier_block trace_die_notifier = {
6037 .notifier_call = trace_die_handler,
6038 .priority = 200
6039};
6040
6041/*
6042 * printk is set to max of 1024, we really don't need it that big.
6043 * Nothing should be printing 1000 characters anyway.
6044 */
6045#define TRACE_MAX_PRINT 1000
6046
6047/*
6048 * Define here KERN_TRACE so that we have one place to modify
6049 * it if we decide to change what log level the ftrace dump
6050 * should be at.
6051 */
Steven Rostedt428aee12009-01-14 12:24:42 -05006052#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006053
Jason Wessel955b61e2010-08-05 09:22:23 -05006054void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006055trace_printk_seq(struct trace_seq *s)
6056{
6057 /* Probably should print a warning here. */
zhangwei(Jovi)bd6df182013-03-11 15:13:37 +08006058 if (s->len >= TRACE_MAX_PRINT)
6059 s->len = TRACE_MAX_PRINT;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006060
6061 /* should be zero ended, but we are paranoid. */
6062 s->buffer[s->len] = 0;
6063
6064 printk(KERN_TRACE "%s", s->buffer);
6065
Steven Rostedtf9520752009-03-02 14:04:40 -05006066 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006067}
6068
Jason Wessel955b61e2010-08-05 09:22:23 -05006069void trace_init_global_iter(struct trace_iterator *iter)
6070{
6071 iter->tr = &global_trace;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006072 iter->trace = iter->tr->current_trace;
Steven Rostedtae3b5092013-01-23 15:22:59 -05006073 iter->cpu_file = RING_BUFFER_ALL_CPUS;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006074 iter->trace_buffer = &global_trace.trace_buffer;
Jason Wessel955b61e2010-08-05 09:22:23 -05006075}
6076
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006077void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006078{
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006079 /* use static because iter can be a bit big for the stack */
6080 static struct trace_iterator iter;
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006081 static atomic_t dump_running;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01006082 unsigned int old_userobj;
Steven Rostedtd7690412008-10-01 00:29:53 -04006083 unsigned long flags;
6084 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006085
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006086 /* Only allow one dump user at a time. */
6087 if (atomic_inc_return(&dump_running) != 1) {
6088 atomic_dec(&dump_running);
6089 return;
Steven Rostedte0a413f2011-09-29 21:26:16 -04006090 }
6091
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006092 /*
6093 * Always turn off tracing when we dump.
6094 * We don't need to show trace output of what happens
6095 * between multiple crashes.
6096 *
6097 * If the user does a sysrq-z, then they can re-enable
6098 * tracing with echo 1 > tracing_on.
6099 */
6100 tracing_off();
6101
6102 local_irq_save(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006103
Jovi Zhang38dbe0b2013-01-25 18:03:07 +08006104 /* Simulate the iterator */
Jason Wessel955b61e2010-08-05 09:22:23 -05006105 trace_init_global_iter(&iter);
6106
Steven Rostedtd7690412008-10-01 00:29:53 -04006107 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006108 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04006109 }
6110
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01006111 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6112
Török Edwinb54d3de2008-11-22 13:28:48 +02006113 /* don't look at user memory in panic mode */
6114 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6115
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02006116 switch (oops_dump_mode) {
6117 case DUMP_ALL:
Steven Rostedtae3b5092013-01-23 15:22:59 -05006118 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02006119 break;
6120 case DUMP_ORIG:
6121 iter.cpu_file = raw_smp_processor_id();
6122 break;
6123 case DUMP_NONE:
6124 goto out_enable;
6125 default:
6126 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
Steven Rostedtae3b5092013-01-23 15:22:59 -05006127 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02006128 }
6129
6130 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006131
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006132 /* Did function tracer already get disabled? */
6133 if (ftrace_is_dead()) {
6134 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6135 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6136 }
6137
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006138 /*
6139 * We need to stop all tracing on all CPUS to read the
6140 * the next buffer. This is a bit expensive, but is
6141 * not done often. We fill all what we can read,
6142 * and then release the locks again.
6143 */
6144
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006145 while (!trace_empty(&iter)) {
6146
6147 if (!cnt)
6148 printk(KERN_TRACE "---------------------------------\n");
6149
6150 cnt++;
6151
6152 /* reset all but tr, trace, and overruns */
6153 memset(&iter.seq, 0,
6154 sizeof(struct trace_iterator) -
6155 offsetof(struct trace_iterator, seq));
6156 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6157 iter.pos = -1;
6158
Jason Wessel955b61e2010-08-05 09:22:23 -05006159 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08006160 int ret;
6161
6162 ret = print_trace_line(&iter);
6163 if (ret != TRACE_TYPE_NO_CONSUME)
6164 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006165 }
Steven Rostedtb892e5c2012-03-01 22:06:48 -05006166 touch_nmi_watchdog();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006167
6168 trace_printk_seq(&iter.seq);
6169 }
6170
6171 if (!cnt)
6172 printk(KERN_TRACE " (ftrace buffer empty)\n");
6173 else
6174 printk(KERN_TRACE "---------------------------------\n");
6175
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02006176 out_enable:
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006177 trace_flags |= old_userobj;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01006178
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006179 for_each_tracing_cpu(cpu) {
6180 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01006181 }
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006182 atomic_dec(&dump_running);
Steven Rostedtcd891ae2009-04-28 11:39:34 -04006183 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006184}
Paul E. McKenneya8eecf22011-10-02 11:01:15 -07006185EXPORT_SYMBOL_GPL(ftrace_dump);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01006186
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006187__init static int tracer_alloc_buffers(void)
6188{
Steven Rostedt73c51622009-03-11 13:42:01 -04006189 int ring_buf_size;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10306190 int ret = -ENOMEM;
6191
David Sharp750912f2010-12-08 13:46:47 -08006192
Rusty Russell9e01c1b2009-01-01 10:12:22 +10306193 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6194 goto out;
6195
6196 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
6197 goto out_free_buffer_mask;
6198
Steven Rostedt07d777f2011-09-22 14:01:55 -04006199 /* Only allocate trace_printk buffers if a trace_printk exists */
6200 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
Steven Rostedt81698832012-10-11 10:15:05 -04006201 /* Must be called before global_trace.buffer is allocated */
Steven Rostedt07d777f2011-09-22 14:01:55 -04006202 trace_printk_init_buffers();
6203
Steven Rostedt73c51622009-03-11 13:42:01 -04006204 /* To save memory, keep the ring buffer size to its minimum */
6205 if (ring_buffer_expanded)
6206 ring_buf_size = trace_buf_size;
6207 else
6208 ring_buf_size = 1;
6209
Rusty Russell9e01c1b2009-01-01 10:12:22 +10306210 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6211 cpumask_copy(tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006212
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006213 raw_spin_lock_init(&global_trace.start_lock);
6214
Steven Rostedtab464282008-05-12 21:21:00 +02006215 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006216 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04006217 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6218 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10306219 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04006220 }
Steven Rostedta7603ff2012-08-06 16:24:11 -04006221
Steven Rostedt499e5472012-02-22 15:50:28 -05006222 if (global_trace.buffer_disabled)
6223 tracing_off();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04006224
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006225 trace_init_cmdlines();
6226
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04006227 /*
6228 * register_tracer() might reference current_trace, so it
6229 * needs to be set before we register anything. This is
6230 * just a bootstrap of current_trace anyway.
6231 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006232 global_trace.current_trace = &nop_trace;
6233
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04006234 register_tracer(&nop_trace);
6235
Steven Rostedt60a11772008-05-12 21:20:44 +02006236 /* All seems OK, enable tracing */
6237 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04006238
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006239 atomic_notifier_chain_register(&panic_notifier_list,
6240 &trace_panic_notifier);
6241
6242 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01006243
Steven Rostedtae63b31e2012-05-03 23:09:03 -04006244 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6245
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006246 /* Holder for file callbacks */
6247 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6248 global_trace.trace_cpu.tr = &global_trace;
6249
Steven Rostedtae63b31e2012-05-03 23:09:03 -04006250 INIT_LIST_HEAD(&global_trace.systems);
6251 INIT_LIST_HEAD(&global_trace.events);
6252 list_add(&global_trace.list, &ftrace_trace_arrays);
6253
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04006254 while (trace_boot_options) {
6255 char *option;
6256
6257 option = strsep(&trace_boot_options, ",");
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006258 trace_set_options(&global_trace, option);
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04006259 }
6260
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006261 register_snapshot_cmd();
6262
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01006263 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006264
Rusty Russell9e01c1b2009-01-01 10:12:22 +10306265out_free_cpumask:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006266 free_percpu(global_trace.trace_buffer.data);
6267#ifdef CONFIG_TRACER_MAX_TRACE
6268 free_percpu(global_trace.max_buffer.data);
6269#endif
Rusty Russell9e01c1b2009-01-01 10:12:22 +10306270 free_cpumask_var(tracing_cpumask);
6271out_free_buffer_mask:
6272 free_cpumask_var(tracing_buffer_mask);
6273out:
6274 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006275}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05006276
6277__init static int clear_boot_tracer(void)
6278{
6279 /*
6280 * The default tracer at boot buffer is an init section.
6281 * This function is called in lateinit. If we did not
6282 * find the boot tracer, then clear it out, to prevent
6283 * later registration from accessing the buffer that is
6284 * about to be freed.
6285 */
6286 if (!default_bootup_tracer)
6287 return 0;
6288
6289 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6290 default_bootup_tracer);
6291 default_bootup_tracer = NULL;
6292
6293 return 0;
6294}
6295
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01006296early_initcall(tracer_alloc_buffers);
6297fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05006298late_initcall(clear_boot_tracer);