blob: 5043a0c4dde0ec55e7167d44fb1fcaa1c6db71a9 [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{
Li Zefanee6c2c12009-09-18 14:06:47 +0800128 strncpy(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)55034cd2013-03-07 22:48:09 -0500152static int __init alloc_snapshot(char *str)
153{
154 allocate_snapshot = true;
155 /* We also need the main ring buffer expanded */
156 ring_buffer_expanded = true;
157 return 1;
158}
159__setup("alloc_snapshot", alloc_snapshot);
160
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{
167 strncpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
168 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);
195
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 |
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -0700331 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
Ingo Molnar4e655512008-05-12 21:20:52 +0200332
Steven Rostedt0d5c6e12012-11-01 20:54:21 -0400333/**
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
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500353/**
354 * __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
445 if (!tr->allocated_snapshot) {
446 trace_printk("*** SNAPSHOT NOT ALLOCATED ***\n");
447 trace_printk("*** stopping trace here! ***\n");
448 tracing_off();
449 return;
450 }
451
452 /* Note, snapshot can not be used when the tracer uses it */
453 if (tracer->use_max_tr) {
454 trace_printk("*** LATENCY TRACER ACTIVE ***\n");
455 trace_printk("*** Can not use snapshot (sorry) ***\n");
456 return;
457 }
458
459 local_irq_save(flags);
460 update_max_tr(tr, current, smp_processor_id());
461 local_irq_restore(flags);
462}
463
464static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
465 struct trace_buffer *size_buf, int cpu_id);
466
467/**
468 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
469 *
470 * This is similar to trace_snapshot(), but it will allocate the
471 * snapshot buffer if it isn't already allocated. Use this only
472 * where it is safe to sleep, as the allocation may sleep.
473 *
474 * This causes a swap between the snapshot buffer and the current live
475 * tracing buffer. You can use this to take snapshots of the live
476 * trace when some condition is triggered, but continue to trace.
477 */
478void tracing_snapshot_alloc(void)
479{
480 struct trace_array *tr = &global_trace;
481 int ret;
482
483 if (!tr->allocated_snapshot) {
484
485 /* allocate spare buffer */
486 ret = resize_buffer_duplicate_size(&tr->max_buffer,
487 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
488 if (WARN_ON(ret < 0))
489 return;
490
491 tr->allocated_snapshot = true;
492 }
493
494 tracing_snapshot();
495}
496#else
497void tracing_snapshot(void)
498{
499 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
500}
501void tracing_snapshot_alloc(void)
502{
503 /* Give warning */
504 tracing_snapshot();
505}
506#endif /* CONFIG_TRACER_SNAPSHOT */
507
Steven Rostedt499e5472012-02-22 15:50:28 -0500508/**
509 * tracing_off - turn off tracing buffers
510 *
511 * This function stops the tracing buffers from recording data.
512 * It does not disable any overhead the tracers themselves may
513 * be causing. This function simply causes all recording to
514 * the ring buffers to fail.
515 */
516void tracing_off(void)
517{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500518 if (global_trace.trace_buffer.buffer)
519 ring_buffer_record_off(global_trace.trace_buffer.buffer);
Steven Rostedt499e5472012-02-22 15:50:28 -0500520 /*
521 * This flag is only looked at when buffers haven't been
522 * allocated yet. We don't really care about the race
523 * between setting this flag and actually turning
524 * on the buffer.
525 */
526 global_trace.buffer_disabled = 1;
527}
528EXPORT_SYMBOL_GPL(tracing_off);
529
530/**
531 * tracing_is_on - show state of ring buffers enabled
532 */
533int tracing_is_on(void)
534{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500535 if (global_trace.trace_buffer.buffer)
536 return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
Steven Rostedt499e5472012-02-22 15:50:28 -0500537 return !global_trace.buffer_disabled;
538}
539EXPORT_SYMBOL_GPL(tracing_is_on);
540
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400541static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200542{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400543 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200544
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200545 if (!str)
546 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +0800547 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200548 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +0800549 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200550 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400551 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200552 return 1;
553}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400554__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200555
Tim Bird0e950172010-02-25 15:36:43 -0800556static int __init set_tracing_thresh(char *str)
557{
Wang Tianhong87abb3b2012-08-02 14:02:00 +0800558 unsigned long threshold;
Tim Bird0e950172010-02-25 15:36:43 -0800559 int ret;
560
561 if (!str)
562 return 0;
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200563 ret = kstrtoul(str, 0, &threshold);
Tim Bird0e950172010-02-25 15:36:43 -0800564 if (ret < 0)
565 return 0;
Wang Tianhong87abb3b2012-08-02 14:02:00 +0800566 tracing_thresh = threshold * 1000;
Tim Bird0e950172010-02-25 15:36:43 -0800567 return 1;
568}
569__setup("tracing_thresh=", set_tracing_thresh);
570
Steven Rostedt57f50be2008-05-12 21:20:44 +0200571unsigned long nsecs_to_usecs(unsigned long nsecs)
572{
573 return nsecs / 1000;
574}
575
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200576/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200577static const char *trace_options[] = {
578 "print-parent",
579 "sym-offset",
580 "sym-addr",
581 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200582 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200583 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200584 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200585 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200586 "stacktrace",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100587 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500588 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500589 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500590 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200591 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200592 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100593 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200594 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500595 "latency-format",
Steven Rostedtbe6f1642009-03-24 11:06:24 -0400596 "sleep-time",
Steven Rostedta2a16d62009-03-24 23:17:58 -0400597 "graph-time",
Li Zefane870e9a2010-07-02 11:07:32 +0800598 "record-cmd",
David Sharp750912f2010-12-08 13:46:47 -0800599 "overwrite",
Steven Rostedtcf30cf62011-06-14 22:44:07 -0400600 "disable_on_free",
Steven Rostedt77271ce2011-11-17 09:34:33 -0500601 "irq-info",
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -0700602 "markers",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200603 NULL
604};
605
Zhaolei5079f322009-08-25 16:12:56 +0800606static struct {
607 u64 (*func)(void);
608 const char *name;
David Sharp8be07092012-11-13 12:18:22 -0800609 int in_ns; /* is this clock in nanoseconds? */
Zhaolei5079f322009-08-25 16:12:56 +0800610} trace_clocks[] = {
David Sharp8be07092012-11-13 12:18:22 -0800611 { trace_clock_local, "local", 1 },
612 { trace_clock_global, "global", 1 },
613 { trace_clock_counter, "counter", 0 },
David Sharp8cbd9cc2012-11-13 12:18:21 -0800614 ARCH_TRACE_CLOCKS
Zhaolei5079f322009-08-25 16:12:56 +0800615};
616
617int trace_clock_id;
618
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200619/*
620 * trace_parser_get_init - gets the buffer for trace parser
621 */
622int trace_parser_get_init(struct trace_parser *parser, int size)
623{
624 memset(parser, 0, sizeof(*parser));
625
626 parser->buffer = kmalloc(size, GFP_KERNEL);
627 if (!parser->buffer)
628 return 1;
629
630 parser->size = size;
631 return 0;
632}
633
634/*
635 * trace_parser_put - frees the buffer for trace parser
636 */
637void trace_parser_put(struct trace_parser *parser)
638{
639 kfree(parser->buffer);
640}
641
642/*
643 * trace_get_user - reads the user input string separated by space
644 * (matched by isspace(ch))
645 *
646 * For each string found the 'struct trace_parser' is updated,
647 * and the function returns.
648 *
649 * Returns number of bytes read.
650 *
651 * See kernel/trace/trace.h for 'struct trace_parser' details.
652 */
653int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
654 size_t cnt, loff_t *ppos)
655{
656 char ch;
657 size_t read = 0;
658 ssize_t ret;
659
660 if (!*ppos)
661 trace_parser_clear(parser);
662
663 ret = get_user(ch, ubuf++);
664 if (ret)
665 goto out;
666
667 read++;
668 cnt--;
669
670 /*
671 * The parser is not finished with the last write,
672 * continue reading the user input without skipping spaces.
673 */
674 if (!parser->cont) {
675 /* skip white space */
676 while (cnt && isspace(ch)) {
677 ret = get_user(ch, ubuf++);
678 if (ret)
679 goto out;
680 read++;
681 cnt--;
682 }
683
684 /* only spaces were written */
685 if (isspace(ch)) {
686 *ppos += read;
687 ret = read;
688 goto out;
689 }
690
691 parser->idx = 0;
692 }
693
694 /* read the non-space input */
695 while (cnt && !isspace(ch)) {
Li Zefan3c235a32009-09-22 13:51:54 +0800696 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200697 parser->buffer[parser->idx++] = ch;
698 else {
699 ret = -EINVAL;
700 goto out;
701 }
702 ret = get_user(ch, ubuf++);
703 if (ret)
704 goto out;
705 read++;
706 cnt--;
707 }
708
709 /* We either got finished input or we have to wait for another call. */
710 if (isspace(ch)) {
711 parser->buffer[parser->idx] = 0;
712 parser->cont = false;
713 } else {
714 parser->cont = true;
715 parser->buffer[parser->idx++] = ch;
716 }
717
718 *ppos += read;
719 ret = read;
720
721out:
722 return ret;
723}
724
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200725ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
726{
727 int len;
728 int ret;
729
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500730 if (!cnt)
731 return 0;
732
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200733 if (s->len <= s->readpos)
734 return -EBUSY;
735
736 len = s->len - s->readpos;
737 if (cnt > len)
738 cnt = len;
739 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500740 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200741 return -EFAULT;
742
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500743 cnt -= ret;
744
Steven Rostedte74da522009-03-04 20:31:11 -0500745 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200746 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200747}
748
Dmitri Vorobievb8b94262009-03-22 19:11:11 +0200749static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200750{
751 int len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200752
753 if (s->len <= s->readpos)
754 return -EBUSY;
755
756 len = s->len - s->readpos;
757 if (cnt > len)
758 cnt = len;
Dan Carpenter5a26c8f2012-04-20 09:31:45 +0300759 memcpy(buf, s->buffer + s->readpos, cnt);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200760
Steven Rostedte74da522009-03-04 20:31:11 -0500761 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200762 return cnt;
763}
764
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400765/*
766 * ftrace_max_lock is used to protect the swapping of buffers
767 * when taking a max snapshot. The buffers themselves are
768 * protected by per_cpu spinlocks. But the action of the swap
769 * needs its own lock.
770 *
Thomas Gleixner445c8952009-12-02 19:49:50 +0100771 * This is defined as a arch_spinlock_t in order to help
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400772 * with performance when lockdep debugging is enabled.
773 *
774 * It is also used in other places outside the update_max_tr
775 * so it needs to be defined outside of the
776 * CONFIG_TRACER_MAX_TRACE.
777 */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100778static arch_spinlock_t ftrace_max_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100779 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400780
Tim Bird0e950172010-02-25 15:36:43 -0800781unsigned long __read_mostly tracing_thresh;
782
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400783#ifdef CONFIG_TRACER_MAX_TRACE
784unsigned long __read_mostly tracing_max_latency;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400785
786/*
787 * Copy the new maximum trace into the separate maximum-trace
788 * structure. (this way the maximum trace is permanently saved,
789 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
790 */
791static void
792__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
793{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500794 struct trace_buffer *trace_buf = &tr->trace_buffer;
795 struct trace_buffer *max_buf = &tr->max_buffer;
796 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
797 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400798
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500799 max_buf->cpu = cpu;
800 max_buf->time_start = data->preempt_timestamp;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400801
Steven Rostedt8248ac02009-09-02 12:27:41 -0400802 max_data->saved_latency = tracing_max_latency;
803 max_data->critical_start = data->critical_start;
804 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400805
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300806 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -0400807 max_data->pid = tsk->pid;
808 max_data->uid = task_uid(tsk);
809 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
810 max_data->policy = tsk->policy;
811 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400812
813 /* record this tasks comm */
814 tracing_record_cmdline(tsk);
815}
816
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200817/**
818 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
819 * @tr: tracer
820 * @tsk: the task with the latency
821 * @cpu: The cpu that initiated the trace.
822 *
823 * Flip the buffers between the @tr and the max_tr and record information
824 * about which task was the cause of this latency.
825 */
Ingo Molnare309b412008-05-12 21:20:51 +0200826void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200827update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
828{
Steven Rostedt (Red Hat)2721e722013-03-12 11:32:32 -0400829 struct ring_buffer *buf;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200830
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400831 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400832 return;
833
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200834 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt34600f02013-01-22 13:35:11 -0500835
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -0500836 if (!tr->allocated_snapshot) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +0900837 /* Only the nop tracer should hit this when disabling */
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400838 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900839 return;
Hiraku Toyookadebdd572012-12-26 11:53:00 +0900840 }
Steven Rostedt34600f02013-01-22 13:35:11 -0500841
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100842 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400843
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500844 buf = tr->trace_buffer.buffer;
845 tr->trace_buffer.buffer = tr->max_buffer.buffer;
846 tr->max_buffer.buffer = buf;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400847
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200848 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100849 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200850}
851
852/**
853 * update_max_tr_single - only copy one trace over, and reset the rest
854 * @tr - tracer
855 * @tsk - task with the latency
856 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200857 *
858 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200859 */
Ingo Molnare309b412008-05-12 21:20:51 +0200860void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200861update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
862{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400863 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200864
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400865 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400866 return;
867
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200868 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -0500869 if (WARN_ON_ONCE(!tr->allocated_snapshot))
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900870 return;
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900871
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100872 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200873
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500874 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400875
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400876 if (ret == -EBUSY) {
877 /*
878 * We failed to swap the buffer due to a commit taking
879 * place on this CPU. We fail to record, but we reset
880 * the max trace buffer (no one writes directly to it)
881 * and flag that it failed.
882 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500883 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400884 "Failed to swap buffers due to commit in progress\n");
885 }
886
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400887 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200888
889 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100890 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200891}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400892#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200893
Steven Rostedt0d5c6e12012-11-01 20:54:21 -0400894static void default_wait_pipe(struct trace_iterator *iter)
895{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -0500896 /* Iterators are static, they should be filled or empty */
897 if (trace_buffer_iter(iter, iter->cpu_file))
898 return;
Steven Rostedt0d5c6e12012-11-01 20:54:21 -0400899
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500900 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt0d5c6e12012-11-01 20:54:21 -0400901}
902
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -0500903#ifdef CONFIG_FTRACE_STARTUP_TEST
904static int run_tracer_selftest(struct tracer *type)
905{
906 struct trace_array *tr = &global_trace;
907 struct tracer *saved_tracer = tr->current_trace;
908 int ret;
909
910 if (!type->selftest || tracing_selftest_disabled)
911 return 0;
912
913 /*
914 * Run a selftest on this tracer.
915 * Here we reset the trace buffer, and set the current
916 * tracer to be this tracer. The tracer can then run some
917 * internal tracing to verify that everything is in order.
918 * If we fail, we do not register this tracer.
919 */
920 tracing_reset_online_cpus(&tr->trace_buffer);
921
922 tr->current_trace = type;
923
924#ifdef CONFIG_TRACER_MAX_TRACE
925 if (type->use_max_tr) {
926 /* If we expanded the buffers, make sure the max is expanded too */
927 if (ring_buffer_expanded)
928 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
929 RING_BUFFER_ALL_CPUS);
930 tr->allocated_snapshot = true;
931 }
932#endif
933
934 /* the test is responsible for initializing and enabling */
935 pr_info("Testing tracer %s: ", type->name);
936 ret = type->selftest(type, tr);
937 /* the test is responsible for resetting too */
938 tr->current_trace = saved_tracer;
939 if (ret) {
940 printk(KERN_CONT "FAILED!\n");
941 /* Add the warning after printing 'FAILED' */
942 WARN_ON(1);
943 return -1;
944 }
945 /* Only reset on passing, to avoid touching corrupted buffers */
946 tracing_reset_online_cpus(&tr->trace_buffer);
947
948#ifdef CONFIG_TRACER_MAX_TRACE
949 if (type->use_max_tr) {
950 tr->allocated_snapshot = false;
951
952 /* Shrink the max buffer again */
953 if (ring_buffer_expanded)
954 ring_buffer_resize(tr->max_buffer.buffer, 1,
955 RING_BUFFER_ALL_CPUS);
956 }
957#endif
958
959 printk(KERN_CONT "PASSED\n");
960 return 0;
961}
962#else
963static inline int run_tracer_selftest(struct tracer *type)
964{
965 return 0;
966}
967#endif /* CONFIG_FTRACE_STARTUP_TEST */
968
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200969/**
970 * register_tracer - register a tracer with the ftrace system.
971 * @type - the plugin for the tracer
972 *
973 * Register a new plugin tracer.
974 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200975int register_tracer(struct tracer *type)
976{
977 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200978 int ret = 0;
979
980 if (!type->name) {
981 pr_info("Tracer must have a name\n");
982 return -1;
983 }
984
Dan Carpenter24a461d2010-07-10 12:06:44 +0200985 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +0800986 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
987 return -1;
988 }
989
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200990 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100991
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100992 tracing_selftest_running = true;
993
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200994 for (t = trace_types; t; t = t->next) {
995 if (strcmp(type->name, t->name) == 0) {
996 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +0800997 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200998 type->name);
999 ret = -1;
1000 goto out;
1001 }
1002 }
1003
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001004 if (!type->set_flag)
1005 type->set_flag = &dummy_set_flag;
1006 if (!type->flags)
1007 type->flags = &dummy_tracer_flags;
1008 else
1009 if (!type->flags->opts)
1010 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001011 if (!type->wait_pipe)
1012 type->wait_pipe = default_wait_pipe;
1013
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001014 ret = run_tracer_selftest(type);
1015 if (ret < 0)
1016 goto out;
Steven Rostedt60a11772008-05-12 21:20:44 +02001017
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001018 type->next = trace_types;
1019 trace_types = type;
Steven Rostedt60a11772008-05-12 21:20:44 +02001020
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001021 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001022 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001023 mutex_unlock(&trace_types_lock);
1024
Steven Rostedtdac74942009-02-05 01:13:38 -05001025 if (ret || !default_bootup_tracer)
1026 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -05001027
Li Zefanee6c2c12009-09-18 14:06:47 +08001028 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -05001029 goto out_unlock;
1030
1031 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1032 /* Do we want this tracer to start on bootup? */
1033 tracing_set_tracer(type->name);
1034 default_bootup_tracer = NULL;
1035 /* disable other selftests, since this will break it. */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05001036 tracing_selftest_disabled = true;
Steven Rostedtdac74942009-02-05 01:13:38 -05001037#ifdef CONFIG_FTRACE_STARTUP_TEST
1038 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1039 type->name);
1040#endif
1041
1042 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001043 return ret;
1044}
1045
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001046void tracing_reset(struct trace_buffer *buf, int cpu)
Steven Rostedtf6339032009-09-04 12:35:16 -04001047{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001048 struct ring_buffer *buffer = buf->buffer;
Steven Rostedtf6339032009-09-04 12:35:16 -04001049
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001050 if (!buffer)
1051 return;
1052
Steven Rostedtf6339032009-09-04 12:35:16 -04001053 ring_buffer_record_disable(buffer);
1054
1055 /* Make sure all commits have finished */
1056 synchronize_sched();
Steven Rostedt68179682012-05-08 20:57:53 -04001057 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -04001058
1059 ring_buffer_record_enable(buffer);
1060}
1061
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001062void tracing_reset_online_cpus(struct trace_buffer *buf)
Pekka J Enberg213cc062008-12-19 12:08:39 +02001063{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001064 struct ring_buffer *buffer = buf->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +02001065 int cpu;
1066
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001067 if (!buffer)
1068 return;
1069
Steven Rostedt621968c2009-09-04 12:02:35 -04001070 ring_buffer_record_disable(buffer);
1071
1072 /* Make sure all commits have finished */
1073 synchronize_sched();
1074
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001075 buf->time_start = ftrace_now(buf->cpu);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001076
1077 for_each_online_cpu(cpu)
Steven Rostedt68179682012-05-08 20:57:53 -04001078 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -04001079
1080 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001081}
1082
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001083void tracing_reset_current(int cpu)
1084{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001085 tracing_reset(&global_trace.trace_buffer, cpu);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001086}
1087
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001088void tracing_reset_all_online_cpus(void)
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001089{
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001090 struct trace_array *tr;
1091
1092 mutex_lock(&trace_types_lock);
1093 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001094 tracing_reset_online_cpus(&tr->trace_buffer);
1095#ifdef CONFIG_TRACER_MAX_TRACE
1096 tracing_reset_online_cpus(&tr->max_buffer);
1097#endif
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001098 }
1099 mutex_unlock(&trace_types_lock);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001100}
1101
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001102#define SAVED_CMDLINES 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001103#define NO_CMDLINE_MAP UINT_MAX
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001104static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1105static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1106static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1107static int cmdline_idx;
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001108static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +02001109
Steven Rostedt25b0b442008-05-12 21:21:00 +02001110/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +01001111static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001112
1113static void trace_init_cmdlines(void)
1114{
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001115 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1116 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001117 cmdline_idx = 0;
1118}
1119
Carsten Emdeb5130b12009-09-13 01:43:07 +02001120int is_tracing_stopped(void)
1121{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001122 return global_trace.stop_count;
Carsten Emdeb5130b12009-09-13 01:43:07 +02001123}
1124
Steven Rostedt0f048702008-11-05 16:05:44 -05001125/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -05001126 * ftrace_off_permanent - disable all ftrace code permanently
1127 *
1128 * This should only be called when a serious anomally has
1129 * been detected. This will turn off the function tracing,
1130 * ring buffers, and other tracing utilites. It takes no
1131 * locks and can be called from any context.
1132 */
1133void ftrace_off_permanent(void)
1134{
1135 tracing_disabled = 1;
1136 ftrace_stop();
1137 tracing_off_permanent();
1138}
1139
1140/**
Steven Rostedt0f048702008-11-05 16:05:44 -05001141 * tracing_start - quick start of the tracer
1142 *
1143 * If tracing is enabled but was stopped by tracing_stop,
1144 * this will start the tracer back up.
1145 */
1146void tracing_start(void)
1147{
1148 struct ring_buffer *buffer;
1149 unsigned long flags;
1150
1151 if (tracing_disabled)
1152 return;
1153
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001154 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1155 if (--global_trace.stop_count) {
1156 if (global_trace.stop_count < 0) {
Steven Rostedtb06a8302009-01-22 14:26:15 -05001157 /* Someone screwed up their debugging */
1158 WARN_ON_ONCE(1);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001159 global_trace.stop_count = 0;
Steven Rostedtb06a8302009-01-22 14:26:15 -05001160 }
Steven Rostedt0f048702008-11-05 16:05:44 -05001161 goto out;
1162 }
1163
Steven Rostedta2f80712010-03-12 19:56:00 -05001164 /* Prevent the buffers from switching */
1165 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -05001166
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001167 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001168 if (buffer)
1169 ring_buffer_record_enable(buffer);
1170
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001171#ifdef CONFIG_TRACER_MAX_TRACE
1172 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001173 if (buffer)
1174 ring_buffer_record_enable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001175#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001176
Steven Rostedta2f80712010-03-12 19:56:00 -05001177 arch_spin_unlock(&ftrace_max_lock);
1178
Steven Rostedt0f048702008-11-05 16:05:44 -05001179 ftrace_start();
1180 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001181 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1182}
1183
1184static void tracing_start_tr(struct trace_array *tr)
1185{
1186 struct ring_buffer *buffer;
1187 unsigned long flags;
1188
1189 if (tracing_disabled)
1190 return;
1191
1192 /* If global, we need to also start the max tracer */
1193 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1194 return tracing_start();
1195
1196 raw_spin_lock_irqsave(&tr->start_lock, flags);
1197
1198 if (--tr->stop_count) {
1199 if (tr->stop_count < 0) {
1200 /* Someone screwed up their debugging */
1201 WARN_ON_ONCE(1);
1202 tr->stop_count = 0;
1203 }
1204 goto out;
1205 }
1206
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001207 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001208 if (buffer)
1209 ring_buffer_record_enable(buffer);
1210
1211 out:
1212 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001213}
1214
1215/**
1216 * tracing_stop - quick stop of the tracer
1217 *
1218 * Light weight way to stop tracing. Use in conjunction with
1219 * tracing_start.
1220 */
1221void tracing_stop(void)
1222{
1223 struct ring_buffer *buffer;
1224 unsigned long flags;
1225
1226 ftrace_stop();
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001227 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1228 if (global_trace.stop_count++)
Steven Rostedt0f048702008-11-05 16:05:44 -05001229 goto out;
1230
Steven Rostedta2f80712010-03-12 19:56:00 -05001231 /* Prevent the buffers from switching */
1232 arch_spin_lock(&ftrace_max_lock);
1233
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001234 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001235 if (buffer)
1236 ring_buffer_record_disable(buffer);
1237
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001238#ifdef CONFIG_TRACER_MAX_TRACE
1239 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001240 if (buffer)
1241 ring_buffer_record_disable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001242#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001243
Steven Rostedta2f80712010-03-12 19:56:00 -05001244 arch_spin_unlock(&ftrace_max_lock);
1245
Steven Rostedt0f048702008-11-05 16:05:44 -05001246 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001247 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1248}
1249
1250static void tracing_stop_tr(struct trace_array *tr)
1251{
1252 struct ring_buffer *buffer;
1253 unsigned long flags;
1254
1255 /* If global, we need to also stop the max tracer */
1256 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1257 return tracing_stop();
1258
1259 raw_spin_lock_irqsave(&tr->start_lock, flags);
1260 if (tr->stop_count++)
1261 goto out;
1262
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001263 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001264 if (buffer)
1265 ring_buffer_record_disable(buffer);
1266
1267 out:
1268 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001269}
1270
Ingo Molnare309b412008-05-12 21:20:51 +02001271void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001272
Ingo Molnare309b412008-05-12 21:20:51 +02001273static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001274{
Carsten Emdea635cf02009-03-18 09:00:41 +01001275 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001276
1277 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1278 return;
1279
1280 /*
1281 * It's not the end of the world if we don't get
1282 * the lock, but we also don't want to spin
1283 * nor do we want to disable interrupts,
1284 * so if we miss here, then better luck next time.
1285 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001286 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001287 return;
1288
1289 idx = map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001290 if (idx == NO_CMDLINE_MAP) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001291 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1292
Carsten Emdea635cf02009-03-18 09:00:41 +01001293 /*
1294 * Check whether the cmdline buffer at idx has a pid
1295 * mapped. We are going to overwrite that entry so we
1296 * need to clear the map_pid_to_cmdline. Otherwise we
1297 * would read the new comm for the old pid.
1298 */
1299 pid = map_cmdline_to_pid[idx];
1300 if (pid != NO_CMDLINE_MAP)
1301 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001302
Carsten Emdea635cf02009-03-18 09:00:41 +01001303 map_cmdline_to_pid[idx] = tsk->pid;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001304 map_pid_to_cmdline[tsk->pid] = idx;
1305
1306 cmdline_idx = idx;
1307 }
1308
1309 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1310
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001311 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001312}
1313
Steven Rostedt4ca530852009-03-16 19:20:15 -04001314void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001315{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001316 unsigned map;
1317
Steven Rostedt4ca530852009-03-16 19:20:15 -04001318 if (!pid) {
1319 strcpy(comm, "<idle>");
1320 return;
1321 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001322
Steven Rostedt74bf4072010-01-25 15:11:53 -05001323 if (WARN_ON_ONCE(pid < 0)) {
1324 strcpy(comm, "<XXX>");
1325 return;
1326 }
1327
Steven Rostedt4ca530852009-03-16 19:20:15 -04001328 if (pid > PID_MAX_DEFAULT) {
1329 strcpy(comm, "<...>");
1330 return;
1331 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001332
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001333 preempt_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001334 arch_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001335 map = map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01001336 if (map != NO_CMDLINE_MAP)
1337 strcpy(comm, saved_cmdlines[map]);
1338 else
1339 strcpy(comm, "<...>");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001340
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001341 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001342 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001343}
1344
Ingo Molnare309b412008-05-12 21:20:51 +02001345void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001346{
Steven Rostedt0fb96562012-05-11 14:25:30 -04001347 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001348 return;
1349
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001350 if (!__this_cpu_read(trace_cmdline_save))
1351 return;
1352
1353 __this_cpu_write(trace_cmdline_save, false);
1354
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001355 trace_save_cmdline(tsk);
1356}
1357
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03001358void
Steven Rostedt38697052008-10-01 13:14:09 -04001359tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1360 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001361{
1362 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001363
Steven Rostedt777e2082008-09-29 23:02:42 -04001364 entry->preempt_count = pc & 0xff;
1365 entry->pid = (tsk) ? tsk->pid : 0;
1366 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04001367#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001368 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04001369#else
1370 TRACE_FLAG_IRQS_NOSUPPORT |
1371#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001372 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1373 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1374 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1375}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02001376EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001377
Steven Rostedte77405a2009-09-02 14:17:06 -04001378struct ring_buffer_event *
1379trace_buffer_lock_reserve(struct ring_buffer *buffer,
1380 int type,
1381 unsigned long len,
1382 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001383{
1384 struct ring_buffer_event *event;
1385
Steven Rostedte77405a2009-09-02 14:17:06 -04001386 event = ring_buffer_lock_reserve(buffer, len);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001387 if (event != NULL) {
1388 struct trace_entry *ent = ring_buffer_event_data(event);
1389
1390 tracing_generic_entry_update(ent, flags, pc);
1391 ent->type = type;
1392 }
1393
1394 return event;
1395}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001396
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001397void
1398__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1399{
1400 __this_cpu_write(trace_cmdline_save, true);
1401 ring_buffer_unlock_commit(buffer, event);
1402}
1403
Steven Rostedte77405a2009-09-02 14:17:06 -04001404static inline void
1405__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1406 struct ring_buffer_event *event,
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001407 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001408{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001409 __buffer_unlock_commit(buffer, event);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001410
Steven Rostedte77405a2009-09-02 14:17:06 -04001411 ftrace_trace_stack(buffer, flags, 6, pc);
1412 ftrace_trace_userstack(buffer, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001413}
1414
Steven Rostedte77405a2009-09-02 14:17:06 -04001415void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1416 struct ring_buffer_event *event,
1417 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001418{
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001419 __trace_buffer_unlock_commit(buffer, event, flags, pc);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001420}
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001421EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001422
Steven Rostedtef5580d2009-02-27 19:38:04 -05001423struct ring_buffer_event *
Steven Rostedtccb469a2012-08-02 10:32:10 -04001424trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1425 struct ftrace_event_file *ftrace_file,
1426 int type, unsigned long len,
1427 unsigned long flags, int pc)
1428{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001429 *current_rb = ftrace_file->tr->trace_buffer.buffer;
Steven Rostedtccb469a2012-08-02 10:32:10 -04001430 return trace_buffer_lock_reserve(*current_rb,
1431 type, len, flags, pc);
1432}
1433EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1434
1435struct ring_buffer_event *
Steven Rostedte77405a2009-09-02 14:17:06 -04001436trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1437 int type, unsigned long len,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001438 unsigned long flags, int pc)
1439{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001440 *current_rb = global_trace.trace_buffer.buffer;
Steven Rostedte77405a2009-09-02 14:17:06 -04001441 return trace_buffer_lock_reserve(*current_rb,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001442 type, len, flags, pc);
1443}
Steven Rostedt94487d62009-05-05 19:22:53 -04001444EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001445
Steven Rostedte77405a2009-09-02 14:17:06 -04001446void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1447 struct ring_buffer_event *event,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001448 unsigned long flags, int pc)
1449{
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001450 __trace_buffer_unlock_commit(buffer, event, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001451}
Steven Rostedt94487d62009-05-05 19:22:53 -04001452EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001453
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001454void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1455 struct ring_buffer_event *event,
1456 unsigned long flags, int pc,
1457 struct pt_regs *regs)
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001458{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001459 __buffer_unlock_commit(buffer, event);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001460
1461 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1462 ftrace_trace_userstack(buffer, flags, pc);
1463}
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001464EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001465
Steven Rostedte77405a2009-09-02 14:17:06 -04001466void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1467 struct ring_buffer_event *event)
Steven Rostedt77d9f462009-04-02 01:16:59 -04001468{
Steven Rostedte77405a2009-09-02 14:17:06 -04001469 ring_buffer_discard_commit(buffer, event);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001470}
Steven Rostedt12acd472009-04-17 16:01:56 -04001471EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001472
Ingo Molnare309b412008-05-12 21:20:51 +02001473void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001474trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001475 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1476 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001477{
Tom Zanussie1112b42009-03-31 00:48:49 -05001478 struct ftrace_event_call *call = &event_function;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001479 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001480 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001481 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001482
Steven Rostedtd7690412008-10-01 00:29:53 -04001483 /* If we are reading the ring buffer, don't trace */
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001484 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04001485 return;
1486
Steven Rostedte77405a2009-09-02 14:17:06 -04001487 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001488 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001489 if (!event)
1490 return;
1491 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001492 entry->ip = ip;
1493 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05001494
Steven Rostedte77405a2009-09-02 14:17:06 -04001495 if (!filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001496 __buffer_unlock_commit(buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001497}
1498
Ingo Molnare309b412008-05-12 21:20:51 +02001499void
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001500ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -04001501 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1502 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001503{
1504 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001505 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001506}
1507
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001508#ifdef CONFIG_STACKTRACE
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001509
1510#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1511struct ftrace_stack {
1512 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1513};
1514
1515static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1516static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1517
Steven Rostedte77405a2009-09-02 14:17:06 -04001518static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05001519 unsigned long flags,
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001520 int skip, int pc, struct pt_regs *regs)
Ingo Molnar86387f72008-05-12 21:20:51 +02001521{
Tom Zanussie1112b42009-03-31 00:48:49 -05001522 struct ftrace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001523 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001524 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02001525 struct stack_trace trace;
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001526 int use_stack;
1527 int size = FTRACE_STACK_ENTRIES;
Ingo Molnar86387f72008-05-12 21:20:51 +02001528
1529 trace.nr_entries = 0;
Ingo Molnar86387f72008-05-12 21:20:51 +02001530 trace.skip = skip;
Ingo Molnar86387f72008-05-12 21:20:51 +02001531
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001532 /*
1533 * Since events can happen in NMIs there's no safe way to
1534 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1535 * or NMI comes in, it will just have to use the default
1536 * FTRACE_STACK_SIZE.
1537 */
1538 preempt_disable_notrace();
1539
Shan Wei82146522012-11-19 13:21:01 +08001540 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001541 /*
1542 * We don't need any atomic variables, just a barrier.
1543 * If an interrupt comes in, we don't care, because it would
1544 * have exited and put the counter back to what we want.
1545 * We just need a barrier to keep gcc from moving things
1546 * around.
1547 */
1548 barrier();
1549 if (use_stack == 1) {
1550 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1551 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1552
1553 if (regs)
1554 save_stack_trace_regs(regs, &trace);
1555 else
1556 save_stack_trace(&trace);
1557
1558 if (trace.nr_entries > size)
1559 size = trace.nr_entries;
1560 } else
1561 /* From now on, use_stack is a boolean */
1562 use_stack = 0;
1563
1564 size *= sizeof(unsigned long);
1565
1566 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1567 sizeof(*entry) + size, flags, pc);
1568 if (!event)
1569 goto out;
1570 entry = ring_buffer_event_data(event);
1571
1572 memset(&entry->caller, 0, size);
1573
1574 if (use_stack)
1575 memcpy(&entry->caller, trace.entries,
1576 trace.nr_entries * sizeof(unsigned long));
1577 else {
1578 trace.max_entries = FTRACE_STACK_ENTRIES;
1579 trace.entries = entry->caller;
1580 if (regs)
1581 save_stack_trace_regs(regs, &trace);
1582 else
1583 save_stack_trace(&trace);
1584 }
1585
1586 entry->size = trace.nr_entries;
1587
Steven Rostedte77405a2009-09-02 14:17:06 -04001588 if (!filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001589 __buffer_unlock_commit(buffer, event);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001590
1591 out:
1592 /* Again, don't let gcc optimize things here */
1593 barrier();
Shan Wei82146522012-11-19 13:21:01 +08001594 __this_cpu_dec(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001595 preempt_enable_notrace();
1596
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001597}
1598
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001599void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1600 int skip, int pc, struct pt_regs *regs)
1601{
1602 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1603 return;
1604
1605 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1606}
1607
Steven Rostedte77405a2009-09-02 14:17:06 -04001608void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1609 int skip, int pc)
Steven Rostedt53614992009-01-15 19:12:40 -05001610{
1611 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1612 return;
1613
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001614 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
Steven Rostedt53614992009-01-15 19:12:40 -05001615}
1616
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001617void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1618 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04001619{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001620 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
Steven Rostedt38697052008-10-01 13:14:09 -04001621}
1622
Steven Rostedt03889382009-12-11 09:48:22 -05001623/**
1624 * trace_dump_stack - record a stack back trace in the trace buffer
1625 */
1626void trace_dump_stack(void)
1627{
1628 unsigned long flags;
1629
1630 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05001631 return;
Steven Rostedt03889382009-12-11 09:48:22 -05001632
1633 local_save_flags(flags);
1634
1635 /* skipping 3 traces, seems to get us at the caller of this function */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001636 __ftrace_trace_stack(global_trace.trace_buffer.buffer, flags, 3,
1637 preempt_count(), NULL);
Steven Rostedt03889382009-12-11 09:48:22 -05001638}
1639
Steven Rostedt91e86e52010-11-10 12:56:12 +01001640static DEFINE_PER_CPU(int, user_stack_count);
1641
Steven Rostedte77405a2009-09-02 14:17:06 -04001642void
1643ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02001644{
Tom Zanussie1112b42009-03-31 00:48:49 -05001645 struct ftrace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02001646 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001647 struct userstack_entry *entry;
1648 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001649
1650 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1651 return;
1652
Steven Rostedtb6345872010-03-12 20:03:30 -05001653 /*
1654 * NMIs can not handle page faults, even with fix ups.
1655 * The save user stack can (and often does) fault.
1656 */
1657 if (unlikely(in_nmi()))
1658 return;
1659
Steven Rostedt91e86e52010-11-10 12:56:12 +01001660 /*
1661 * prevent recursion, since the user stack tracing may
1662 * trigger other kernel events.
1663 */
1664 preempt_disable();
1665 if (__this_cpu_read(user_stack_count))
1666 goto out;
1667
1668 __this_cpu_inc(user_stack_count);
1669
Steven Rostedte77405a2009-09-02 14:17:06 -04001670 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001671 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001672 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08001673 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02001674 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001675
Steven Rostedt48659d32009-09-11 11:36:23 -04001676 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02001677 memset(&entry->caller, 0, sizeof(entry->caller));
1678
1679 trace.nr_entries = 0;
1680 trace.max_entries = FTRACE_STACK_ENTRIES;
1681 trace.skip = 0;
1682 trace.entries = entry->caller;
1683
1684 save_stack_trace_user(&trace);
Steven Rostedte77405a2009-09-02 14:17:06 -04001685 if (!filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001686 __buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001687
Li Zefan1dbd1952010-12-09 15:47:56 +08001688 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01001689 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001690 out:
1691 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02001692}
1693
Hannes Eder4fd27352009-02-10 19:44:12 +01001694#ifdef UNUSED
1695static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001696{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001697 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001698}
Hannes Eder4fd27352009-02-10 19:44:12 +01001699#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001700
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001701#endif /* CONFIG_STACKTRACE */
1702
Steven Rostedt07d777f2011-09-22 14:01:55 -04001703/* created for use with alloc_percpu */
1704struct trace_buffer_struct {
1705 char buffer[TRACE_BUF_SIZE];
1706};
1707
1708static struct trace_buffer_struct *trace_percpu_buffer;
1709static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1710static struct trace_buffer_struct *trace_percpu_irq_buffer;
1711static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1712
1713/*
1714 * The buffer used is dependent on the context. There is a per cpu
1715 * buffer for normal context, softirq contex, hard irq context and
1716 * for NMI context. Thise allows for lockless recording.
1717 *
1718 * Note, if the buffers failed to be allocated, then this returns NULL
1719 */
1720static char *get_trace_buf(void)
1721{
1722 struct trace_buffer_struct *percpu_buffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04001723
1724 /*
1725 * If we have allocated per cpu buffers, then we do not
1726 * need to do any locking.
1727 */
1728 if (in_nmi())
1729 percpu_buffer = trace_percpu_nmi_buffer;
1730 else if (in_irq())
1731 percpu_buffer = trace_percpu_irq_buffer;
1732 else if (in_softirq())
1733 percpu_buffer = trace_percpu_sirq_buffer;
1734 else
1735 percpu_buffer = trace_percpu_buffer;
1736
1737 if (!percpu_buffer)
1738 return NULL;
1739
Shan Weid8a03492012-11-13 09:53:04 +08001740 return this_cpu_ptr(&percpu_buffer->buffer[0]);
Steven Rostedt07d777f2011-09-22 14:01:55 -04001741}
1742
1743static int alloc_percpu_trace_buffer(void)
1744{
1745 struct trace_buffer_struct *buffers;
1746 struct trace_buffer_struct *sirq_buffers;
1747 struct trace_buffer_struct *irq_buffers;
1748 struct trace_buffer_struct *nmi_buffers;
1749
1750 buffers = alloc_percpu(struct trace_buffer_struct);
1751 if (!buffers)
1752 goto err_warn;
1753
1754 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1755 if (!sirq_buffers)
1756 goto err_sirq;
1757
1758 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1759 if (!irq_buffers)
1760 goto err_irq;
1761
1762 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1763 if (!nmi_buffers)
1764 goto err_nmi;
1765
1766 trace_percpu_buffer = buffers;
1767 trace_percpu_sirq_buffer = sirq_buffers;
1768 trace_percpu_irq_buffer = irq_buffers;
1769 trace_percpu_nmi_buffer = nmi_buffers;
1770
1771 return 0;
1772
1773 err_nmi:
1774 free_percpu(irq_buffers);
1775 err_irq:
1776 free_percpu(sirq_buffers);
1777 err_sirq:
1778 free_percpu(buffers);
1779 err_warn:
1780 WARN(1, "Could not allocate percpu trace_printk buffer");
1781 return -ENOMEM;
1782}
1783
Steven Rostedt81698832012-10-11 10:15:05 -04001784static int buffers_allocated;
1785
Steven Rostedt07d777f2011-09-22 14:01:55 -04001786void trace_printk_init_buffers(void)
1787{
Steven Rostedt07d777f2011-09-22 14:01:55 -04001788 if (buffers_allocated)
1789 return;
1790
1791 if (alloc_percpu_trace_buffer())
1792 return;
1793
1794 pr_info("ftrace: Allocated trace_printk buffers\n");
1795
Steven Rostedtb382ede62012-10-10 21:44:34 -04001796 /* Expand the buffers to set size */
1797 tracing_update_buffers();
1798
Steven Rostedt07d777f2011-09-22 14:01:55 -04001799 buffers_allocated = 1;
Steven Rostedt81698832012-10-11 10:15:05 -04001800
1801 /*
1802 * trace_printk_init_buffers() can be called by modules.
1803 * If that happens, then we need to start cmdline recording
1804 * directly here. If the global_trace.buffer is already
1805 * allocated here, then this was called by module code.
1806 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001807 if (global_trace.trace_buffer.buffer)
Steven Rostedt81698832012-10-11 10:15:05 -04001808 tracing_start_cmdline_record();
1809}
1810
1811void trace_printk_start_comm(void)
1812{
1813 /* Start tracing comms if trace printk is set */
1814 if (!buffers_allocated)
1815 return;
1816 tracing_start_cmdline_record();
1817}
1818
1819static void trace_printk_start_stop_comm(int enabled)
1820{
1821 if (!buffers_allocated)
1822 return;
1823
1824 if (enabled)
1825 tracing_start_cmdline_record();
1826 else
1827 tracing_stop_cmdline_record();
Steven Rostedt07d777f2011-09-22 14:01:55 -04001828}
1829
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001830/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001831 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001832 *
1833 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001834int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001835{
Tom Zanussie1112b42009-03-31 00:48:49 -05001836 struct ftrace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001837 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001838 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001839 struct trace_array *tr = &global_trace;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001840 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001841 unsigned long flags;
Steven Rostedt07d777f2011-09-22 14:01:55 -04001842 char *tbuffer;
1843 int len = 0, size, pc;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001844
1845 if (unlikely(tracing_selftest_running || tracing_disabled))
1846 return 0;
1847
1848 /* Don't pollute graph traces with trace_vprintk internals */
1849 pause_graph_tracing();
1850
1851 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04001852 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001853
Steven Rostedt07d777f2011-09-22 14:01:55 -04001854 tbuffer = get_trace_buf();
1855 if (!tbuffer) {
1856 len = 0;
1857 goto out;
1858 }
1859
1860 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1861
1862 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001863 goto out;
1864
Steven Rostedt07d777f2011-09-22 14:01:55 -04001865 local_save_flags(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001866 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001867 buffer = tr->trace_buffer.buffer;
Steven Rostedte77405a2009-09-02 14:17:06 -04001868 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1869 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001870 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04001871 goto out;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001872 entry = ring_buffer_event_data(event);
1873 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001874 entry->fmt = fmt;
1875
Steven Rostedt07d777f2011-09-22 14:01:55 -04001876 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
Steven Rostedtd9313692010-01-06 17:27:11 -05001877 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001878 __buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05001879 ftrace_trace_stack(buffer, flags, 6, pc);
1880 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001881
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001882out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04001883 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001884 unpause_graph_tracing();
1885
1886 return len;
1887}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001888EXPORT_SYMBOL_GPL(trace_vbprintk);
1889
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001890static int
1891__trace_array_vprintk(struct ring_buffer *buffer,
1892 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001893{
Tom Zanussie1112b42009-03-31 00:48:49 -05001894 struct ftrace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001895 struct ring_buffer_event *event;
Steven Rostedt07d777f2011-09-22 14:01:55 -04001896 int len = 0, size, pc;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001897 struct print_entry *entry;
Steven Rostedt07d777f2011-09-22 14:01:55 -04001898 unsigned long flags;
1899 char *tbuffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001900
1901 if (tracing_disabled || tracing_selftest_running)
1902 return 0;
1903
Steven Rostedt07d777f2011-09-22 14:01:55 -04001904 /* Don't pollute graph traces with trace_vprintk internals */
1905 pause_graph_tracing();
1906
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001907 pc = preempt_count();
1908 preempt_disable_notrace();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001909
Steven Rostedt07d777f2011-09-22 14:01:55 -04001910
1911 tbuffer = get_trace_buf();
1912 if (!tbuffer) {
1913 len = 0;
1914 goto out;
1915 }
1916
1917 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1918 if (len > TRACE_BUF_SIZE)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001919 goto out;
1920
Steven Rostedt07d777f2011-09-22 14:01:55 -04001921 local_save_flags(flags);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001922 size = sizeof(*entry) + len + 1;
Steven Rostedte77405a2009-09-02 14:17:06 -04001923 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
Steven Rostedt07d777f2011-09-22 14:01:55 -04001924 flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001925 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04001926 goto out;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001927 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001928 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001929
Steven Rostedt07d777f2011-09-22 14:01:55 -04001930 memcpy(&entry->buf, tbuffer, len);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001931 entry->buf[len] = '\0';
Steven Rostedtd9313692010-01-06 17:27:11 -05001932 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001933 __buffer_unlock_commit(buffer, event);
Steven Rostedt07d777f2011-09-22 14:01:55 -04001934 ftrace_trace_stack(buffer, flags, 6, pc);
Steven Rostedtd9313692010-01-06 17:27:11 -05001935 }
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001936 out:
1937 preempt_enable_notrace();
Steven Rostedt07d777f2011-09-22 14:01:55 -04001938 unpause_graph_tracing();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001939
1940 return len;
1941}
Steven Rostedt659372d2009-09-03 19:11:07 -04001942
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001943int trace_array_vprintk(struct trace_array *tr,
1944 unsigned long ip, const char *fmt, va_list args)
1945{
1946 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
1947}
1948
1949int trace_array_printk(struct trace_array *tr,
1950 unsigned long ip, const char *fmt, ...)
1951{
1952 int ret;
1953 va_list ap;
1954
1955 if (!(trace_flags & TRACE_ITER_PRINTK))
1956 return 0;
1957
1958 va_start(ap, fmt);
1959 ret = trace_array_vprintk(tr, ip, fmt, ap);
1960 va_end(ap);
1961 return ret;
1962}
1963
1964int trace_array_printk_buf(struct ring_buffer *buffer,
1965 unsigned long ip, const char *fmt, ...)
1966{
1967 int ret;
1968 va_list ap;
1969
1970 if (!(trace_flags & TRACE_ITER_PRINTK))
1971 return 0;
1972
1973 va_start(ap, fmt);
1974 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
1975 va_end(ap);
1976 return ret;
1977}
1978
Steven Rostedt659372d2009-09-03 19:11:07 -04001979int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1980{
Steven Rostedta813a152009-10-09 01:41:35 -04001981 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04001982}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001983EXPORT_SYMBOL_GPL(trace_vprintk);
1984
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001985static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001986{
Steven Rostedt6d158a82012-06-27 20:46:14 -04001987 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
1988
Steven Rostedt5a90f572008-09-03 17:42:51 -04001989 iter->idx++;
Steven Rostedt6d158a82012-06-27 20:46:14 -04001990 if (buf_iter)
1991 ring_buffer_read(buf_iter, NULL);
Steven Rostedt5a90f572008-09-03 17:42:51 -04001992}
1993
Ingo Molnare309b412008-05-12 21:20:51 +02001994static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04001995peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1996 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001997{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001998 struct ring_buffer_event *event;
Steven Rostedt6d158a82012-06-27 20:46:14 -04001999 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002000
Steven Rostedtd7690412008-10-01 00:29:53 -04002001 if (buf_iter)
2002 event = ring_buffer_iter_peek(buf_iter, ts);
2003 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002004 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04002005 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04002006
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002007 if (event) {
2008 iter->ent_size = ring_buffer_event_length(event);
2009 return ring_buffer_event_data(event);
2010 }
2011 iter->ent_size = 0;
2012 return NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002013}
Steven Rostedtd7690412008-10-01 00:29:53 -04002014
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002015static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04002016__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2017 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002018{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002019 struct ring_buffer *buffer = iter->trace_buffer->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002020 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08002021 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002022 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002023 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002024 int next_cpu = -1;
Steven Rostedt12b5da32012-03-27 10:43:28 -04002025 int next_size = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002026 int cpu;
2027
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002028 /*
2029 * If we are in a per_cpu trace file, don't bother by iterating over
2030 * all cpu and peek directly.
2031 */
Steven Rostedtae3b5092013-01-23 15:22:59 -05002032 if (cpu_file > RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002033 if (ring_buffer_empty_cpu(buffer, cpu_file))
2034 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002035 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002036 if (ent_cpu)
2037 *ent_cpu = cpu_file;
2038
2039 return ent;
2040 }
2041
Steven Rostedtab464282008-05-12 21:21:00 +02002042 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002043
2044 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002045 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002046
Steven Rostedtbc21b472010-03-31 19:49:26 -04002047 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002048
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02002049 /*
2050 * Pick the entry with the smallest timestamp:
2051 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002052 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002053 next = ent;
2054 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002055 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002056 next_lost = lost_events;
Steven Rostedt12b5da32012-03-27 10:43:28 -04002057 next_size = iter->ent_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002058 }
2059 }
2060
Steven Rostedt12b5da32012-03-27 10:43:28 -04002061 iter->ent_size = next_size;
2062
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002063 if (ent_cpu)
2064 *ent_cpu = next_cpu;
2065
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002066 if (ent_ts)
2067 *ent_ts = next_ts;
2068
Steven Rostedtbc21b472010-03-31 19:49:26 -04002069 if (missing_events)
2070 *missing_events = next_lost;
2071
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002072 return next;
2073}
2074
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002075/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002076struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2077 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002078{
Steven Rostedtbc21b472010-03-31 19:49:26 -04002079 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002080}
Ingo Molnar8c523a92008-05-12 21:20:46 +02002081
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002082/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05002083void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002084{
Steven Rostedtbc21b472010-03-31 19:49:26 -04002085 iter->ent = __find_next_entry(iter, &iter->cpu,
2086 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002087
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002088 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01002089 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002090
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002091 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002092}
2093
Ingo Molnare309b412008-05-12 21:20:51 +02002094static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002095{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002096 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04002097 &iter->lost_events);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002098}
2099
Ingo Molnare309b412008-05-12 21:20:51 +02002100static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002101{
2102 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002103 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002104 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002105
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002106 WARN_ON_ONCE(iter->leftover);
2107
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002108 (*pos)++;
2109
2110 /* can't go backwards */
2111 if (iter->idx > i)
2112 return NULL;
2113
2114 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05002115 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002116 else
2117 ent = iter;
2118
2119 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05002120 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002121
2122 iter->pos = *pos;
2123
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002124 return ent;
2125}
2126
Jason Wessel955b61e2010-08-05 09:22:23 -05002127void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002128{
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002129 struct ring_buffer_event *event;
2130 struct ring_buffer_iter *buf_iter;
2131 unsigned long entries = 0;
2132 u64 ts;
2133
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002134 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002135
Steven Rostedt6d158a82012-06-27 20:46:14 -04002136 buf_iter = trace_buffer_iter(iter, cpu);
2137 if (!buf_iter)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002138 return;
2139
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002140 ring_buffer_iter_reset(buf_iter);
2141
2142 /*
2143 * We could have the case with the max latency tracers
2144 * that a reset never took place on a cpu. This is evident
2145 * by the timestamp being before the start of the buffer.
2146 */
2147 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002148 if (ts >= iter->trace_buffer->time_start)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002149 break;
2150 entries++;
2151 ring_buffer_read(buf_iter, NULL);
2152 }
2153
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002154 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002155}
2156
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002157/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002158 * The current tracer is copied to avoid a global locking
2159 * all around.
2160 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002161static void *s_start(struct seq_file *m, loff_t *pos)
2162{
2163 struct trace_iterator *iter = m->private;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002164 struct trace_array *tr = iter->tr;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002165 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002166 void *p = NULL;
2167 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002168 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002169
Hiraku Toyooka2fd196e2012-12-26 11:52:52 +09002170 /*
2171 * copy the tracer to avoid using a global lock all around.
2172 * iter->trace is a copy of current_trace, the pointer to the
2173 * name may be used instead of a strcmp(), as iter->trace->name
2174 * will point to the same string as current_trace->name.
2175 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002176 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002177 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2178 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002179 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002180
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002181#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002182 if (iter->snapshot && iter->trace->use_max_tr)
2183 return ERR_PTR(-EBUSY);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002184#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002185
2186 if (!iter->snapshot)
2187 atomic_inc(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002188
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002189 if (*pos != iter->pos) {
2190 iter->ent = NULL;
2191 iter->cpu = 0;
2192 iter->idx = -1;
2193
Steven Rostedtae3b5092013-01-23 15:22:59 -05002194 if (cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002195 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002196 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002197 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002198 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002199
Lai Jiangshanac91d852010-03-02 17:54:50 +08002200 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002201 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2202 ;
2203
2204 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002205 /*
2206 * If we overflowed the seq_file before, then we want
2207 * to just reuse the trace_seq buffer again.
2208 */
2209 if (iter->leftover)
2210 p = iter;
2211 else {
2212 l = *pos - 1;
2213 p = s_next(m, p, &l);
2214 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002215 }
2216
Lai Jiangshan4f535962009-05-18 19:35:34 +08002217 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002218 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002219 return p;
2220}
2221
2222static void s_stop(struct seq_file *m, void *p)
2223{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002224 struct trace_iterator *iter = m->private;
2225
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002226#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002227 if (iter->snapshot && iter->trace->use_max_tr)
2228 return;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002229#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002230
2231 if (!iter->snapshot)
2232 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002233
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002234 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08002235 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002236}
2237
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002238static void
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002239get_total_entries(struct trace_buffer *buf,
2240 unsigned long *total, unsigned long *entries)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002241{
2242 unsigned long count;
2243 int cpu;
2244
2245 *total = 0;
2246 *entries = 0;
2247
2248 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002249 count = ring_buffer_entries_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002250 /*
2251 * If this buffer has skipped entries, then we hold all
2252 * entries for the trace and we need to ignore the
2253 * ones before the time stamp.
2254 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002255 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2256 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002257 /* total is the same as the entries */
2258 *total += count;
2259 } else
2260 *total += count +
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002261 ring_buffer_overrun_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002262 *entries += count;
2263 }
2264}
2265
Ingo Molnare309b412008-05-12 21:20:51 +02002266static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002267{
Michael Ellermana6168352008-08-20 16:36:11 -07002268 seq_puts(m, "# _------=> CPU# \n");
2269 seq_puts(m, "# / _-----=> irqs-off \n");
2270 seq_puts(m, "# | / _----=> need-resched \n");
2271 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2272 seq_puts(m, "# ||| / _--=> preempt-depth \n");
Steven Rostedte6e1e252011-03-09 10:41:56 -05002273 seq_puts(m, "# |||| / delay \n");
2274 seq_puts(m, "# cmd pid ||||| time | caller \n");
2275 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002276}
2277
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002278static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002279{
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002280 unsigned long total;
2281 unsigned long entries;
2282
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002283 get_total_entries(buf, &total, &entries);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002284 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2285 entries, total, num_online_cpus());
2286 seq_puts(m, "#\n");
2287}
2288
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002289static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002290{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002291 print_event_info(buf, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002292 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
Michael Ellermana6168352008-08-20 16:36:11 -07002293 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002294}
2295
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002296static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedt77271ce2011-11-17 09:34:33 -05002297{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002298 print_event_info(buf, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002299 seq_puts(m, "# _-----=> irqs-off\n");
2300 seq_puts(m, "# / _----=> need-resched\n");
2301 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2302 seq_puts(m, "# || / _--=> preempt-depth\n");
2303 seq_puts(m, "# ||| / delay\n");
2304 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2305 seq_puts(m, "# | | | |||| | |\n");
2306}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002307
Jiri Olsa62b915f2010-04-02 19:01:22 +02002308void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002309print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2310{
2311 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002312 struct trace_buffer *buf = iter->trace_buffer;
2313 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002314 struct tracer *type = iter->trace;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002315 unsigned long entries;
2316 unsigned long total;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002317 const char *name = "preemption";
2318
Steven Rostedt (Red Hat)d840f712013-02-01 18:38:47 -05002319 name = type->name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002320
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002321 get_total_entries(buf, &total, &entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002322
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002323 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002324 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002325 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002326 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002327 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002328 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02002329 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002330 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002331 total,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002332 buf->cpu,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002333#if defined(CONFIG_PREEMPT_NONE)
2334 "server",
2335#elif defined(CONFIG_PREEMPT_VOLUNTARY)
2336 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04002337#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002338 "preempt",
2339#else
2340 "unknown",
2341#endif
2342 /* These are reserved for later use */
2343 0, 0, 0, 0);
2344#ifdef CONFIG_SMP
2345 seq_printf(m, " #P:%d)\n", num_online_cpus());
2346#else
2347 seq_puts(m, ")\n");
2348#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002349 seq_puts(m, "# -----------------\n");
2350 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002351 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
Eric W. Biedermand20b92a2012-03-13 16:02:19 -07002352 data->comm, data->pid,
2353 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002354 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002355 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002356
2357 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002358 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02002359 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2360 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002361 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02002362 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2363 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04002364 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002365 }
2366
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002367 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002368}
2369
Steven Rostedta3097202008-11-07 22:36:02 -05002370static void test_cpu_buff_start(struct trace_iterator *iter)
2371{
2372 struct trace_seq *s = &iter->seq;
2373
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002374 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2375 return;
2376
2377 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2378 return;
2379
Rusty Russell44623442009-01-01 10:12:23 +10302380 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05002381 return;
2382
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002383 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002384 return;
2385
Rusty Russell44623442009-01-01 10:12:23 +10302386 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002387
2388 /* Don't print started cpu buffer for the first entry of the trace */
2389 if (iter->idx > 1)
2390 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2391 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05002392}
2393
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002394static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002395{
Steven Rostedt214023c2008-05-12 21:20:46 +02002396 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002397 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002398 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002399 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002400
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002401 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002402
Steven Rostedta3097202008-11-07 22:36:02 -05002403 test_cpu_buff_start(iter);
2404
Steven Rostedtf633cef2008-12-23 23:24:13 -05002405 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002406
2407 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05002408 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2409 if (!trace_print_lat_context(iter))
2410 goto partial;
2411 } else {
2412 if (!trace_print_context(iter))
2413 goto partial;
2414 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002415 }
2416
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002417 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04002418 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002419
2420 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2421 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04002422
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002423 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002424partial:
2425 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002426}
2427
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002428static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002429{
2430 struct trace_seq *s = &iter->seq;
2431 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002432 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002433
2434 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002435
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002436 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002437 if (!trace_seq_printf(s, "%d %d %llu ",
2438 entry->pid, iter->cpu, iter->ts))
2439 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002440 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002441
Steven Rostedtf633cef2008-12-23 23:24:13 -05002442 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002443 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04002444 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002445
2446 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2447 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04002448
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002449 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002450partial:
2451 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002452}
2453
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002454static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002455{
2456 struct trace_seq *s = &iter->seq;
2457 unsigned char newline = '\n';
2458 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002459 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002460
2461 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002462
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002463 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2464 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2465 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2466 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2467 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002468
Steven Rostedtf633cef2008-12-23 23:24:13 -05002469 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002470 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04002471 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002472 if (ret != TRACE_TYPE_HANDLED)
2473 return ret;
2474 }
Steven Rostedt7104f302008-10-01 10:52:51 -04002475
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002476 SEQ_PUT_FIELD_RET(s, newline);
2477
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002478 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002479}
2480
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002481static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002482{
2483 struct trace_seq *s = &iter->seq;
2484 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002485 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002486
2487 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002488
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002489 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2490 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05002491 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002492 SEQ_PUT_FIELD_RET(s, iter->ts);
2493 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002494
Steven Rostedtf633cef2008-12-23 23:24:13 -05002495 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04002496 return event ? event->funcs->binary(iter, 0, event) :
2497 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002498}
2499
Jiri Olsa62b915f2010-04-02 19:01:22 +02002500int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002501{
Steven Rostedt6d158a82012-06-27 20:46:14 -04002502 struct ring_buffer_iter *buf_iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002503 int cpu;
2504
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002505 /* If we are looking at one CPU buffer, only check that one */
Steven Rostedtae3b5092013-01-23 15:22:59 -05002506 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002507 cpu = iter->cpu_file;
Steven Rostedt6d158a82012-06-27 20:46:14 -04002508 buf_iter = trace_buffer_iter(iter, cpu);
2509 if (buf_iter) {
2510 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002511 return 0;
2512 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002513 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002514 return 0;
2515 }
2516 return 1;
2517 }
2518
Steven Rostedtab464282008-05-12 21:21:00 +02002519 for_each_tracing_cpu(cpu) {
Steven Rostedt6d158a82012-06-27 20:46:14 -04002520 buf_iter = trace_buffer_iter(iter, cpu);
2521 if (buf_iter) {
2522 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedtd7690412008-10-01 00:29:53 -04002523 return 0;
2524 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002525 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedtd7690412008-10-01 00:29:53 -04002526 return 0;
2527 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002528 }
Steven Rostedtd7690412008-10-01 00:29:53 -04002529
Frederic Weisbecker797d3712008-09-30 18:13:45 +02002530 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002531}
2532
Lai Jiangshan4f535962009-05-18 19:35:34 +08002533/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05002534enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002535{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002536 enum print_line_t ret;
2537
Jiri Olsaee5e51f2011-03-25 12:05:18 +01002538 if (iter->lost_events &&
2539 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2540 iter->cpu, iter->lost_events))
2541 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002542
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002543 if (iter->trace && iter->trace->print_line) {
2544 ret = iter->trace->print_line(iter);
2545 if (ret != TRACE_TYPE_UNHANDLED)
2546 return ret;
2547 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002548
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -05002549 if (iter->ent->type == TRACE_BPUTS &&
2550 trace_flags & TRACE_ITER_PRINTK &&
2551 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2552 return trace_print_bputs_msg_only(iter);
2553
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002554 if (iter->ent->type == TRACE_BPRINT &&
2555 trace_flags & TRACE_ITER_PRINTK &&
2556 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002557 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002558
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002559 if (iter->ent->type == TRACE_PRINT &&
2560 trace_flags & TRACE_ITER_PRINTK &&
2561 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002562 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002563
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002564 if (trace_flags & TRACE_ITER_BIN)
2565 return print_bin_fmt(iter);
2566
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002567 if (trace_flags & TRACE_ITER_HEX)
2568 return print_hex_fmt(iter);
2569
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002570 if (trace_flags & TRACE_ITER_RAW)
2571 return print_raw_fmt(iter);
2572
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002573 return print_trace_fmt(iter);
2574}
2575
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01002576void trace_latency_header(struct seq_file *m)
2577{
2578 struct trace_iterator *iter = m->private;
2579
2580 /* print nothing if the buffers are empty */
2581 if (trace_empty(iter))
2582 return;
2583
2584 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2585 print_trace_header(m, iter);
2586
2587 if (!(trace_flags & TRACE_ITER_VERBOSE))
2588 print_lat_help_header(m);
2589}
2590
Jiri Olsa62b915f2010-04-02 19:01:22 +02002591void trace_default_header(struct seq_file *m)
2592{
2593 struct trace_iterator *iter = m->private;
2594
Jiri Olsaf56e7f82011-06-03 16:58:49 +02002595 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2596 return;
2597
Jiri Olsa62b915f2010-04-02 19:01:22 +02002598 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2599 /* print nothing if the buffers are empty */
2600 if (trace_empty(iter))
2601 return;
2602 print_trace_header(m, iter);
2603 if (!(trace_flags & TRACE_ITER_VERBOSE))
2604 print_lat_help_header(m);
2605 } else {
Steven Rostedt77271ce2011-11-17 09:34:33 -05002606 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2607 if (trace_flags & TRACE_ITER_IRQ_INFO)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002608 print_func_help_header_irq(iter->trace_buffer, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002609 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002610 print_func_help_header(iter->trace_buffer, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002611 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02002612 }
2613}
2614
Steven Rostedte0a413f2011-09-29 21:26:16 -04002615static void test_ftrace_alive(struct seq_file *m)
2616{
2617 if (!ftrace_is_dead())
2618 return;
2619 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2620 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2621}
2622
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002623#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002624static void show_snapshot_main_help(struct seq_file *m)
2625{
2626 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2627 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2628 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2629 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2630 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2631 seq_printf(m, "# is not a '0' or '1')\n");
2632}
2633
2634static void show_snapshot_percpu_help(struct seq_file *m)
2635{
2636 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2637#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2638 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2639 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2640#else
2641 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2642 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2643#endif
2644 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2645 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2646 seq_printf(m, "# is not a '0' or '1')\n");
2647}
2648
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002649static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2650{
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05002651 if (iter->tr->allocated_snapshot)
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002652 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2653 else
2654 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2655
2656 seq_printf(m, "# Snapshot commands:\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002657 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2658 show_snapshot_main_help(m);
2659 else
2660 show_snapshot_percpu_help(m);
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002661}
2662#else
2663/* Should never be called */
2664static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2665#endif
2666
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002667static int s_show(struct seq_file *m, void *v)
2668{
2669 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002670 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002671
2672 if (iter->ent == NULL) {
2673 if (iter->tr) {
2674 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2675 seq_puts(m, "#\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04002676 test_ftrace_alive(m);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002677 }
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002678 if (iter->snapshot && trace_empty(iter))
2679 print_snapshot_help(m, iter);
2680 else if (iter->trace && iter->trace->print_header)
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002681 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02002682 else
2683 trace_default_header(m);
2684
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002685 } else if (iter->leftover) {
2686 /*
2687 * If we filled the seq_file buffer earlier, we
2688 * want to just show it now.
2689 */
2690 ret = trace_print_seq(m, &iter->seq);
2691
2692 /* ret should this time be zero, but you never know */
2693 iter->leftover = ret;
2694
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002695 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002696 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002697 ret = trace_print_seq(m, &iter->seq);
2698 /*
2699 * If we overflow the seq_file buffer, then it will
2700 * ask us for this data again at start up.
2701 * Use that instead.
2702 * ret is 0 if seq_file write succeeded.
2703 * -1 otherwise.
2704 */
2705 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002706 }
2707
2708 return 0;
2709}
2710
James Morris88e9d342009-09-22 16:43:43 -07002711static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002712 .start = s_start,
2713 .next = s_next,
2714 .stop = s_stop,
2715 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002716};
2717
Ingo Molnare309b412008-05-12 21:20:51 +02002718static struct trace_iterator *
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002719__tracing_open(struct inode *inode, struct file *file, bool snapshot)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002720{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002721 struct trace_cpu *tc = inode->i_private;
2722 struct trace_array *tr = tc->tr;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002723 struct trace_iterator *iter;
Jiri Olsa50e18b92012-04-25 10:23:39 +02002724 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002725
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002726 if (tracing_disabled)
2727 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02002728
Jiri Olsa50e18b92012-04-25 10:23:39 +02002729 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002730 if (!iter)
2731 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002732
Steven Rostedt6d158a82012-06-27 20:46:14 -04002733 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2734 GFP_KERNEL);
Dan Carpenter93574fc2012-07-11 09:35:08 +03002735 if (!iter->buffer_iter)
2736 goto release;
2737
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002738 /*
2739 * We make a copy of the current tracer to avoid concurrent
2740 * changes on it while we are reading.
2741 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002742 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002743 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002744 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002745 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002746
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002747 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002748
Li Zefan79f55992009-06-15 14:58:26 +08002749 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002750 goto fail;
2751
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002752 iter->tr = tr;
2753
2754#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002755 /* Currently only the top directory has a snapshot */
2756 if (tr->current_trace->print_max || snapshot)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002757 iter->trace_buffer = &tr->max_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002758 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002759#endif
2760 iter->trace_buffer = &tr->trace_buffer;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002761 iter->snapshot = snapshot;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002762 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002763 mutex_init(&iter->mutex);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002764 iter->cpu_file = tc->cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002765
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002766 /* Notify the tracer early; before we stop tracing. */
2767 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01002768 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002769
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002770 /* Annotate start of buffers if we had overruns */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002771 if (ring_buffer_overruns(iter->trace_buffer->buffer))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002772 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2773
David Sharp8be07092012-11-13 12:18:22 -08002774 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2775 if (trace_clocks[trace_clock_id].in_ns)
2776 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2777
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002778 /* stop the trace while dumping if we are not opening "snapshot" */
2779 if (!iter->snapshot)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002780 tracing_stop_tr(tr);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002781
Steven Rostedtae3b5092013-01-23 15:22:59 -05002782 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002783 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002784 iter->buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002785 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
David Miller72c9ddf2010-04-20 15:47:11 -07002786 }
2787 ring_buffer_read_prepare_sync();
2788 for_each_tracing_cpu(cpu) {
2789 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002790 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002791 }
2792 } else {
2793 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002794 iter->buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002795 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
David Miller72c9ddf2010-04-20 15:47:11 -07002796 ring_buffer_read_prepare_sync();
2797 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002798 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002799 }
2800
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05002801 tr->ref++;
2802
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002803 mutex_unlock(&trace_types_lock);
2804
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002805 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002806
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002807 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002808 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002809 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04002810 kfree(iter->buffer_iter);
Dan Carpenter93574fc2012-07-11 09:35:08 +03002811release:
Jiri Olsa50e18b92012-04-25 10:23:39 +02002812 seq_release_private(inode, file);
2813 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002814}
2815
2816int tracing_open_generic(struct inode *inode, struct file *filp)
2817{
Steven Rostedt60a11772008-05-12 21:20:44 +02002818 if (tracing_disabled)
2819 return -ENODEV;
2820
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002821 filp->private_data = inode->i_private;
2822 return 0;
2823}
2824
Hannes Eder4fd27352009-02-10 19:44:12 +01002825static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002826{
matt mooney907f2782010-09-27 19:04:53 -07002827 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002828 struct trace_iterator *iter;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002829 struct trace_array *tr;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002830 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002831
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002832 if (!(file->f_mode & FMODE_READ))
2833 return 0;
2834
2835 iter = m->private;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002836 tr = iter->tr;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002837
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002838 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05002839
2840 WARN_ON(!tr->ref);
2841 tr->ref--;
2842
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002843 for_each_tracing_cpu(cpu) {
2844 if (iter->buffer_iter[cpu])
2845 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2846 }
2847
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002848 if (iter->trace && iter->trace->close)
2849 iter->trace->close(iter);
2850
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002851 if (!iter->snapshot)
2852 /* reenable tracing if it was previously enabled */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002853 tracing_start_tr(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002854 mutex_unlock(&trace_types_lock);
2855
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002856 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002857 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002858 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04002859 kfree(iter->buffer_iter);
Jiri Olsa50e18b92012-04-25 10:23:39 +02002860 seq_release_private(inode, file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002861 return 0;
2862}
2863
2864static int tracing_open(struct inode *inode, struct file *file)
2865{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002866 struct trace_iterator *iter;
2867 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002868
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002869 /* If this file was open for write, then erase contents */
2870 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002871 (file->f_flags & O_TRUNC)) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002872 struct trace_cpu *tc = inode->i_private;
2873 struct trace_array *tr = tc->tr;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002874
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002875 if (tc->cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002876 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002877 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002878 tracing_reset(&tr->trace_buffer, tc->cpu);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002879 }
2880
2881 if (file->f_mode & FMODE_READ) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002882 iter = __tracing_open(inode, file, false);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002883 if (IS_ERR(iter))
2884 ret = PTR_ERR(iter);
2885 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2886 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2887 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002888 return ret;
2889}
2890
Ingo Molnare309b412008-05-12 21:20:51 +02002891static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002892t_next(struct seq_file *m, void *v, loff_t *pos)
2893{
Li Zefanf129e962009-06-24 09:53:44 +08002894 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002895
2896 (*pos)++;
2897
2898 if (t)
2899 t = t->next;
2900
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002901 return t;
2902}
2903
2904static void *t_start(struct seq_file *m, loff_t *pos)
2905{
Li Zefanf129e962009-06-24 09:53:44 +08002906 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002907 loff_t l = 0;
2908
2909 mutex_lock(&trace_types_lock);
Li Zefanf129e962009-06-24 09:53:44 +08002910 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002911 ;
2912
2913 return t;
2914}
2915
2916static void t_stop(struct seq_file *m, void *p)
2917{
2918 mutex_unlock(&trace_types_lock);
2919}
2920
2921static int t_show(struct seq_file *m, void *v)
2922{
2923 struct tracer *t = v;
2924
2925 if (!t)
2926 return 0;
2927
2928 seq_printf(m, "%s", t->name);
2929 if (t->next)
2930 seq_putc(m, ' ');
2931 else
2932 seq_putc(m, '\n');
2933
2934 return 0;
2935}
2936
James Morris88e9d342009-09-22 16:43:43 -07002937static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002938 .start = t_start,
2939 .next = t_next,
2940 .stop = t_stop,
2941 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002942};
2943
2944static int show_traces_open(struct inode *inode, struct file *file)
2945{
Steven Rostedt60a11772008-05-12 21:20:44 +02002946 if (tracing_disabled)
2947 return -ENODEV;
2948
Li Zefanf129e962009-06-24 09:53:44 +08002949 return seq_open(file, &show_traces_seq_ops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002950}
2951
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002952static ssize_t
2953tracing_write_stub(struct file *filp, const char __user *ubuf,
2954 size_t count, loff_t *ppos)
2955{
2956 return count;
2957}
2958
Slava Pestov364829b2010-11-24 15:13:16 -08002959static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2960{
2961 if (file->f_mode & FMODE_READ)
2962 return seq_lseek(file, offset, origin);
2963 else
2964 return 0;
2965}
2966
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002967static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002968 .open = tracing_open,
2969 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002970 .write = tracing_write_stub,
Slava Pestov364829b2010-11-24 15:13:16 -08002971 .llseek = tracing_seek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002972 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002973};
2974
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002975static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002976 .open = show_traces_open,
2977 .read = seq_read,
2978 .release = seq_release,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002979 .llseek = seq_lseek,
Ingo Molnarc7078de2008-05-12 21:20:52 +02002980};
2981
Ingo Molnar36dfe922008-05-12 21:20:52 +02002982/*
2983 * Only trace on a CPU if the bitmask is set:
2984 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302985static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002986
2987/*
2988 * The tracer itself will not take this lock, but still we want
2989 * to provide a consistent cpumask to user-space:
2990 */
2991static DEFINE_MUTEX(tracing_cpumask_update_lock);
2992
2993/*
2994 * Temporary storage for the character representation of the
2995 * CPU bitmask (and one more byte for the newline):
2996 */
2997static char mask_str[NR_CPUS + 1];
2998
Ingo Molnarc7078de2008-05-12 21:20:52 +02002999static ssize_t
3000tracing_cpumask_read(struct file *filp, char __user *ubuf,
3001 size_t count, loff_t *ppos)
3002{
Ingo Molnar36dfe922008-05-12 21:20:52 +02003003 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003004
3005 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003006
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303007 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003008 if (count - len < 2) {
3009 count = -EINVAL;
3010 goto out_err;
3011 }
3012 len += sprintf(mask_str + len, "\n");
3013 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3014
3015out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02003016 mutex_unlock(&tracing_cpumask_update_lock);
3017
3018 return count;
3019}
3020
3021static ssize_t
3022tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3023 size_t count, loff_t *ppos)
3024{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003025 struct trace_array *tr = filp->private_data;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303026 cpumask_var_t tracing_cpumask_new;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003027 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303028
3029 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3030 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003031
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303032 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003033 if (err)
3034 goto err_unlock;
3035
Li Zefan215368e2009-06-15 10:56:42 +08003036 mutex_lock(&tracing_cpumask_update_lock);
3037
Steven Rostedta5e25882008-12-02 15:34:05 -05003038 local_irq_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003039 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02003040 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02003041 /*
3042 * Increase/decrease the disabled counter if we are
3043 * about to flip a bit in the cpumask:
3044 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303045 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
3046 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003047 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3048 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003049 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303050 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
3051 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003052 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3053 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003054 }
3055 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003056 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05003057 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02003058
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303059 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003060
Ingo Molnarc7078de2008-05-12 21:20:52 +02003061 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303062 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02003063
Ingo Molnarc7078de2008-05-12 21:20:52 +02003064 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02003065
3066err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08003067 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003068
3069 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003070}
3071
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003072static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02003073 .open = tracing_open_generic,
3074 .read = tracing_cpumask_read,
3075 .write = tracing_cpumask_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003076 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003077};
3078
Li Zefanfdb372e2009-12-08 11:15:59 +08003079static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003080{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003081 struct tracer_opt *trace_opts;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003082 struct trace_array *tr = m->private;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003083 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003084 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003085
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003086 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003087 tracer_flags = tr->current_trace->flags->val;
3088 trace_opts = tr->current_trace->flags->opts;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003089
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003090 for (i = 0; trace_options[i]; i++) {
3091 if (trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08003092 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003093 else
Li Zefanfdb372e2009-12-08 11:15:59 +08003094 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003095 }
3096
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003097 for (i = 0; trace_opts[i].name; i++) {
3098 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08003099 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003100 else
Li Zefanfdb372e2009-12-08 11:15:59 +08003101 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003102 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003103 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003104
Li Zefanfdb372e2009-12-08 11:15:59 +08003105 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003106}
3107
Li Zefan8d18eaa2009-12-08 11:17:06 +08003108static int __set_tracer_option(struct tracer *trace,
3109 struct tracer_flags *tracer_flags,
3110 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003111{
Li Zefan8d18eaa2009-12-08 11:17:06 +08003112 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003113
Li Zefan8d18eaa2009-12-08 11:17:06 +08003114 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003115 if (ret)
3116 return ret;
3117
3118 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08003119 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003120 else
Zhaolei77708412009-08-07 18:53:21 +08003121 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003122 return 0;
3123}
3124
Li Zefan8d18eaa2009-12-08 11:17:06 +08003125/* Try to assign a tracer specific option */
3126static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3127{
3128 struct tracer_flags *tracer_flags = trace->flags;
3129 struct tracer_opt *opts = NULL;
3130 int i;
3131
3132 for (i = 0; tracer_flags->opts[i].name; i++) {
3133 opts = &tracer_flags->opts[i];
3134
3135 if (strcmp(cmp, opts->name) == 0)
3136 return __set_tracer_option(trace, trace->flags,
3137 opts, neg);
3138 }
3139
3140 return -EINVAL;
3141}
3142
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003143/* Some tracers require overwrite to stay enabled */
3144int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3145{
3146 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3147 return -1;
3148
3149 return 0;
3150}
3151
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003152int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003153{
3154 /* do nothing if flag is already set */
3155 if (!!(trace_flags & mask) == !!enabled)
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003156 return 0;
3157
3158 /* Give the tracer a chance to approve the change */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003159 if (tr->current_trace->flag_changed)
3160 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003161 return -EINVAL;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003162
3163 if (enabled)
3164 trace_flags |= mask;
3165 else
3166 trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08003167
3168 if (mask == TRACE_ITER_RECORD_CMD)
3169 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08003170
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003171 if (mask == TRACE_ITER_OVERWRITE) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003172 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003173#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003174 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003175#endif
3176 }
Steven Rostedt81698832012-10-11 10:15:05 -04003177
3178 if (mask == TRACE_ITER_PRINTK)
3179 trace_printk_start_stop_comm(enabled);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003180
3181 return 0;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003182}
3183
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003184static int trace_set_options(struct trace_array *tr, char *option)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003185{
Li Zefan8d18eaa2009-12-08 11:17:06 +08003186 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003187 int neg = 0;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003188 int ret = -ENODEV;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003189 int i;
3190
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003191 cmp = strstrip(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003192
Li Zefan8d18eaa2009-12-08 11:17:06 +08003193 if (strncmp(cmp, "no", 2) == 0) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003194 neg = 1;
3195 cmp += 2;
3196 }
3197
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003198 mutex_lock(&trace_types_lock);
3199
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003200 for (i = 0; trace_options[i]; i++) {
Li Zefan8d18eaa2009-12-08 11:17:06 +08003201 if (strcmp(cmp, trace_options[i]) == 0) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003202 ret = set_tracer_flag(tr, 1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003203 break;
3204 }
3205 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003206
3207 /* If no option could be set, test the specific tracer options */
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003208 if (!trace_options[i])
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003209 ret = set_tracer_option(tr->current_trace, cmp, neg);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003210
3211 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003212
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003213 return ret;
3214}
3215
3216static ssize_t
3217tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3218 size_t cnt, loff_t *ppos)
3219{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003220 struct seq_file *m = filp->private_data;
3221 struct trace_array *tr = m->private;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003222 char buf[64];
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003223 int ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003224
3225 if (cnt >= sizeof(buf))
3226 return -EINVAL;
3227
3228 if (copy_from_user(&buf, ubuf, cnt))
3229 return -EFAULT;
3230
Steven Rostedta8dd2172013-01-09 20:54:17 -05003231 buf[cnt] = 0;
3232
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003233 ret = trace_set_options(tr, buf);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003234 if (ret < 0)
3235 return ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003236
Jiri Olsacf8517c2009-10-23 19:36:16 -04003237 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003238
3239 return cnt;
3240}
3241
Li Zefanfdb372e2009-12-08 11:15:59 +08003242static int tracing_trace_options_open(struct inode *inode, struct file *file)
3243{
3244 if (tracing_disabled)
3245 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003246
3247 return single_open(file, tracing_trace_options_show, inode->i_private);
Li Zefanfdb372e2009-12-08 11:15:59 +08003248}
3249
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003250static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08003251 .open = tracing_trace_options_open,
3252 .read = seq_read,
3253 .llseek = seq_lseek,
3254 .release = single_release,
Steven Rostedtee6bce52008-11-12 17:52:37 -05003255 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003256};
3257
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003258static const char readme_msg[] =
3259 "tracing mini-HOWTO:\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09003260 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
3261 "# cat /sys/kernel/debug/tracing/available_tracers\n"
Geunsik Lim1e42e832012-02-08 19:05:36 +09003262 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09003263 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05303264 "nop\n"
Geunsik Lim1e42e832012-02-08 19:05:36 +09003265 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09003266 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Geunsik Lim1e42e832012-02-08 19:05:36 +09003267 "wakeup\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09003268 "# cat /sys/kernel/debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003269 "noprint-parent nosym-offset nosym-addr noverbose\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09003270 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
Geunsik Lim9b5f8b32011-08-12 14:30:22 +09003271 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09003272 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
Geunsik Lim9b5f8b32011-08-12 14:30:22 +09003273 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003274;
3275
3276static ssize_t
3277tracing_readme_read(struct file *filp, char __user *ubuf,
3278 size_t cnt, loff_t *ppos)
3279{
3280 return simple_read_from_buffer(ubuf, cnt, ppos,
3281 readme_msg, strlen(readme_msg));
3282}
3283
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003284static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02003285 .open = tracing_open_generic,
3286 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003287 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003288};
3289
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003290static ssize_t
Avadh Patel69abe6a2009-04-10 16:04:48 -04003291tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3292 size_t cnt, loff_t *ppos)
3293{
3294 char *buf_comm;
3295 char *file_buf;
3296 char *buf;
3297 int len = 0;
3298 int pid;
3299 int i;
3300
3301 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3302 if (!file_buf)
3303 return -ENOMEM;
3304
3305 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3306 if (!buf_comm) {
3307 kfree(file_buf);
3308 return -ENOMEM;
3309 }
3310
3311 buf = file_buf;
3312
3313 for (i = 0; i < SAVED_CMDLINES; i++) {
3314 int r;
3315
3316 pid = map_cmdline_to_pid[i];
3317 if (pid == -1 || pid == NO_CMDLINE_MAP)
3318 continue;
3319
3320 trace_find_cmdline(pid, buf_comm);
3321 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3322 buf += r;
3323 len += r;
3324 }
3325
3326 len = simple_read_from_buffer(ubuf, cnt, ppos,
3327 file_buf, len);
3328
3329 kfree(file_buf);
3330 kfree(buf_comm);
3331
3332 return len;
3333}
3334
3335static const struct file_operations tracing_saved_cmdlines_fops = {
3336 .open = tracing_open_generic,
3337 .read = tracing_saved_cmdlines_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003338 .llseek = generic_file_llseek,
Avadh Patel69abe6a2009-04-10 16:04:48 -04003339};
3340
3341static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003342tracing_set_trace_read(struct file *filp, char __user *ubuf,
3343 size_t cnt, loff_t *ppos)
3344{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003345 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08003346 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003347 int r;
3348
3349 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003350 r = sprintf(buf, "%s\n", tr->current_trace->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003351 mutex_unlock(&trace_types_lock);
3352
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003353 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003354}
3355
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02003356int tracer_init(struct tracer *t, struct trace_array *tr)
3357{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003358 tracing_reset_online_cpus(&tr->trace_buffer);
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02003359 return t->init(tr);
3360}
3361
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003362static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003363{
3364 int cpu;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05003365
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003366 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003367 per_cpu_ptr(buf->data, cpu)->entries = val;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003368}
3369
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003370#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookad60da502012-10-17 11:56:16 +09003371/* resize @tr's buffer to the size of @size_tr's entries */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003372static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3373 struct trace_buffer *size_buf, int cpu_id)
Hiraku Toyookad60da502012-10-17 11:56:16 +09003374{
3375 int cpu, ret = 0;
3376
3377 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3378 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003379 ret = ring_buffer_resize(trace_buf->buffer,
3380 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
Hiraku Toyookad60da502012-10-17 11:56:16 +09003381 if (ret < 0)
3382 break;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003383 per_cpu_ptr(trace_buf->data, cpu)->entries =
3384 per_cpu_ptr(size_buf->data, cpu)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09003385 }
3386 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003387 ret = ring_buffer_resize(trace_buf->buffer,
3388 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
Hiraku Toyookad60da502012-10-17 11:56:16 +09003389 if (ret == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003390 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3391 per_cpu_ptr(size_buf->data, cpu_id)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09003392 }
3393
3394 return ret;
3395}
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003396#endif /* CONFIG_TRACER_MAX_TRACE */
Hiraku Toyookad60da502012-10-17 11:56:16 +09003397
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003398static int __tracing_resize_ring_buffer(struct trace_array *tr,
3399 unsigned long size, int cpu)
Steven Rostedt73c51622009-03-11 13:42:01 -04003400{
3401 int ret;
3402
3403 /*
3404 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04003405 * we use the size that was given, and we can forget about
3406 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04003407 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05003408 ring_buffer_expanded = true;
Steven Rostedt73c51622009-03-11 13:42:01 -04003409
Steven Rostedtb382ede62012-10-10 21:44:34 -04003410 /* May be called before buffers are initialized */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003411 if (!tr->trace_buffer.buffer)
Steven Rostedtb382ede62012-10-10 21:44:34 -04003412 return 0;
3413
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003414 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04003415 if (ret < 0)
3416 return ret;
3417
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003418#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003419 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3420 !tr->current_trace->use_max_tr)
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003421 goto out;
3422
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003423 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04003424 if (ret < 0) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003425 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3426 &tr->trace_buffer, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04003427 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04003428 /*
3429 * AARGH! We are left with different
3430 * size max buffer!!!!
3431 * The max buffer is our "snapshot" buffer.
3432 * When a tracer needs a snapshot (one of the
3433 * latency tracers), it swaps the max buffer
3434 * with the saved snap shot. We succeeded to
3435 * update the size of the main buffer, but failed to
3436 * update the size of the max buffer. But when we tried
3437 * to reset the main buffer to the original size, we
3438 * failed there too. This is very unlikely to
3439 * happen, but if it does, warn and kill all
3440 * tracing.
3441 */
Steven Rostedt73c51622009-03-11 13:42:01 -04003442 WARN_ON(1);
3443 tracing_disabled = 1;
3444 }
3445 return ret;
3446 }
3447
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003448 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003449 set_buffer_entries(&tr->max_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003450 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003451 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003452
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003453 out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003454#endif /* CONFIG_TRACER_MAX_TRACE */
3455
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003456 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003457 set_buffer_entries(&tr->trace_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003458 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003459 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
Steven Rostedt73c51622009-03-11 13:42:01 -04003460
3461 return ret;
3462}
3463
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003464static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3465 unsigned long size, int cpu_id)
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003466{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003467 int ret = size;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003468
3469 mutex_lock(&trace_types_lock);
3470
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003471 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3472 /* make sure, this cpu is enabled in the mask */
3473 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3474 ret = -EINVAL;
3475 goto out;
3476 }
3477 }
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003478
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003479 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003480 if (ret < 0)
3481 ret = -ENOMEM;
3482
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003483out:
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003484 mutex_unlock(&trace_types_lock);
3485
3486 return ret;
3487}
3488
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003489
Steven Rostedt1852fcc2009-03-11 14:33:00 -04003490/**
3491 * tracing_update_buffers - used by tracing facility to expand ring buffers
3492 *
3493 * To save on memory when the tracing is never used on a system with it
3494 * configured in. The ring buffers are set to a minimum size. But once
3495 * a user starts to use the tracing facility, then they need to grow
3496 * to their default size.
3497 *
3498 * This function is to be called when a tracer is about to be used.
3499 */
3500int tracing_update_buffers(void)
3501{
3502 int ret = 0;
3503
Steven Rostedt1027fcb2009-03-12 11:33:20 -04003504 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04003505 if (!ring_buffer_expanded)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003506 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003507 RING_BUFFER_ALL_CPUS);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04003508 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04003509
3510 return ret;
3511}
3512
Steven Rostedt577b7852009-02-26 23:43:05 -05003513struct trace_option_dentry;
3514
3515static struct trace_option_dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003516create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
Steven Rostedt577b7852009-02-26 23:43:05 -05003517
3518static void
3519destroy_trace_option_files(struct trace_option_dentry *topts);
3520
Steven Rostedtb2821ae2009-02-02 21:38:32 -05003521static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003522{
Steven Rostedt577b7852009-02-26 23:43:05 -05003523 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003524 struct trace_array *tr = &global_trace;
3525 struct tracer *t;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003526#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05003527 bool had_max_tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003528#endif
Peter Zijlstrad9e54072008-11-01 19:57:37 +01003529 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003530
Steven Rostedt1027fcb2009-03-12 11:33:20 -04003531 mutex_lock(&trace_types_lock);
3532
Steven Rostedt73c51622009-03-11 13:42:01 -04003533 if (!ring_buffer_expanded) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003534 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003535 RING_BUFFER_ALL_CPUS);
Steven Rostedt73c51622009-03-11 13:42:01 -04003536 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01003537 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04003538 ret = 0;
3539 }
3540
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003541 for (t = trace_types; t; t = t->next) {
3542 if (strcmp(t->name, buf) == 0)
3543 break;
3544 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02003545 if (!t) {
3546 ret = -EINVAL;
3547 goto out;
3548 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003549 if (t == tr->current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003550 goto out;
3551
Steven Rostedt9f029e82008-11-12 15:24:24 -05003552 trace_branch_disable();
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003553
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003554 tr->current_trace->enabled = false;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003555
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003556 if (tr->current_trace->reset)
3557 tr->current_trace->reset(tr);
Steven Rostedt34600f02013-01-22 13:35:11 -05003558
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003559 /* Current trace needs to be nop_trace before synchronize_sched */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003560 tr->current_trace = &nop_trace;
Steven Rostedt34600f02013-01-22 13:35:11 -05003561
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05003562#ifdef CONFIG_TRACER_MAX_TRACE
3563 had_max_tr = tr->allocated_snapshot;
3564
Steven Rostedt34600f02013-01-22 13:35:11 -05003565 if (had_max_tr && !t->use_max_tr) {
3566 /*
3567 * We need to make sure that the update_max_tr sees that
3568 * current_trace changed to nop_trace to keep it from
3569 * swapping the buffers after we resize it.
3570 * The update_max_tr is called from interrupts disabled
3571 * so a synchronized_sched() is sufficient.
3572 */
3573 synchronize_sched();
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003574 /*
3575 * We don't free the ring buffer. instead, resize it because
3576 * The max_tr ring buffer has some state (e.g. ring->clock) and
3577 * we want preserve it.
3578 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003579 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
3580 set_buffer_entries(&tr->max_buffer, 1);
3581 tracing_reset_online_cpus(&tr->max_buffer);
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05003582 tr->allocated_snapshot = false;
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003583 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003584#endif
Steven Rostedt577b7852009-02-26 23:43:05 -05003585 destroy_trace_option_files(topts);
3586
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003587 topts = create_trace_option_files(tr, t);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003588
3589#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05003590 if (t->use_max_tr && !had_max_tr) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003591 /* we need to make per cpu buffer sizes equivalent */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003592 ret = resize_buffer_duplicate_size(&tr->max_buffer, &tr->trace_buffer,
Hiraku Toyookad60da502012-10-17 11:56:16 +09003593 RING_BUFFER_ALL_CPUS);
3594 if (ret < 0)
3595 goto out;
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05003596 tr->allocated_snapshot = true;
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003597 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003598#endif
Steven Rostedt577b7852009-02-26 23:43:05 -05003599
Frederic Weisbecker1c800252008-11-16 05:57:26 +01003600 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02003601 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01003602 if (ret)
3603 goto out;
3604 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003605
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003606 tr->current_trace = t;
3607 tr->current_trace->enabled = true;
Steven Rostedt9f029e82008-11-12 15:24:24 -05003608 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003609 out:
3610 mutex_unlock(&trace_types_lock);
3611
Peter Zijlstrad9e54072008-11-01 19:57:37 +01003612 return ret;
3613}
3614
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003615static ssize_t
3616tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3617 size_t cnt, loff_t *ppos)
3618{
Li Zefanee6c2c12009-09-18 14:06:47 +08003619 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003620 int i;
3621 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01003622 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003623
Steven Rostedt60063a62008-10-28 10:44:24 -04003624 ret = cnt;
3625
Li Zefanee6c2c12009-09-18 14:06:47 +08003626 if (cnt > MAX_TRACER_SIZE)
3627 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003628
3629 if (copy_from_user(&buf, ubuf, cnt))
3630 return -EFAULT;
3631
3632 buf[cnt] = 0;
3633
3634 /* strip ending whitespace. */
3635 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3636 buf[i] = 0;
3637
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01003638 err = tracing_set_tracer(buf);
3639 if (err)
3640 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003641
Jiri Olsacf8517c2009-10-23 19:36:16 -04003642 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003643
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02003644 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003645}
3646
3647static ssize_t
3648tracing_max_lat_read(struct file *filp, char __user *ubuf,
3649 size_t cnt, loff_t *ppos)
3650{
3651 unsigned long *ptr = filp->private_data;
3652 char buf[64];
3653 int r;
3654
Steven Rostedtcffae432008-05-12 21:21:00 +02003655 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003656 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02003657 if (r > sizeof(buf))
3658 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003659 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003660}
3661
3662static ssize_t
3663tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3664 size_t cnt, loff_t *ppos)
3665{
Hannes Eder5e398412009-02-10 19:44:34 +01003666 unsigned long *ptr = filp->private_data;
Hannes Eder5e398412009-02-10 19:44:34 +01003667 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003668 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003669
Peter Huewe22fe9b52011-06-07 21:58:27 +02003670 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3671 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003672 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003673
3674 *ptr = val * 1000;
3675
3676 return cnt;
3677}
3678
Steven Rostedtb3806b42008-05-12 21:20:46 +02003679static int tracing_open_pipe(struct inode *inode, struct file *filp)
3680{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003681 struct trace_cpu *tc = inode->i_private;
3682 struct trace_array *tr = tc->tr;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003683 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003684 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003685
3686 if (tracing_disabled)
3687 return -ENODEV;
3688
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003689 mutex_lock(&trace_types_lock);
3690
Steven Rostedtb3806b42008-05-12 21:20:46 +02003691 /* create a buffer to store the information to pass to userspace */
3692 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003693 if (!iter) {
3694 ret = -ENOMEM;
3695 goto out;
3696 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003697
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003698 /*
3699 * We make a copy of the current tracer to avoid concurrent
3700 * changes on it while we are reading.
3701 */
3702 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3703 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003704 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003705 goto fail;
3706 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003707 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003708
3709 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3710 ret = -ENOMEM;
3711 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10303712 }
3713
Steven Rostedta3097202008-11-07 22:36:02 -05003714 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10303715 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05003716
Steven Rostedt112f38a72009-06-01 15:16:05 -04003717 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3718 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3719
David Sharp8be07092012-11-13 12:18:22 -08003720 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3721 if (trace_clocks[trace_clock_id].in_ns)
3722 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3723
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003724 iter->cpu_file = tc->cpu;
3725 iter->tr = tc->tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003726 iter->trace_buffer = &tc->tr->trace_buffer;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003727 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003728 filp->private_data = iter;
3729
Steven Rostedt107bad82008-05-12 21:21:01 +02003730 if (iter->trace->pipe_open)
3731 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02003732
Arnd Bergmannb4447862010-07-07 23:40:11 +02003733 nonseekable_open(inode, filp);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003734out:
3735 mutex_unlock(&trace_types_lock);
3736 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003737
3738fail:
3739 kfree(iter->trace);
3740 kfree(iter);
3741 mutex_unlock(&trace_types_lock);
3742 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003743}
3744
3745static int tracing_release_pipe(struct inode *inode, struct file *file)
3746{
3747 struct trace_iterator *iter = file->private_data;
3748
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003749 mutex_lock(&trace_types_lock);
3750
Steven Rostedt29bf4a52009-12-09 12:37:43 -05003751 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05003752 iter->trace->pipe_close(iter);
3753
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003754 mutex_unlock(&trace_types_lock);
3755
Rusty Russell44623442009-01-01 10:12:23 +10303756 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003757 mutex_destroy(&iter->mutex);
3758 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003759 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003760
3761 return 0;
3762}
3763
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003764static unsigned int
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05003765trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003766{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003767 /* Iterators are static, they should be filled or empty */
3768 if (trace_buffer_iter(iter, iter->cpu_file))
3769 return POLLIN | POLLRDNORM;
3770
3771 if (trace_flags & TRACE_ITER_BLOCK)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003772 /*
3773 * Always select as readable when in blocking mode
3774 */
3775 return POLLIN | POLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003776 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003777 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05003778 filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003779}
3780
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05003781static unsigned int
3782tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3783{
3784 struct trace_iterator *iter = filp->private_data;
3785
3786 return trace_poll(iter, filp, poll_table);
3787}
3788
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003789/*
3790 * This is a make-shift waitqueue.
3791 * A tracer might use this callback on some rare cases:
3792 *
3793 * 1) the current tracer might hold the runqueue lock when it wakes up
3794 * a reader, hence a deadlock (sched, function, and function graph tracers)
3795 * 2) the function tracers, trace all functions, we don't want
3796 * the overhead of calling wake_up and friends
3797 * (and tracing them too)
3798 *
3799 * Anyway, this is really very primitive wakeup.
3800 */
3801void poll_wait_pipe(struct trace_iterator *iter)
3802{
3803 set_current_state(TASK_INTERRUPTIBLE);
3804 /* sleep for 100 msecs, and try again. */
3805 schedule_timeout(HZ / 10);
3806}
3807
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003808/* Must be called with trace_types_lock mutex held. */
3809static int tracing_wait_pipe(struct file *filp)
3810{
3811 struct trace_iterator *iter = filp->private_data;
3812
3813 while (trace_empty(iter)) {
3814
3815 if ((filp->f_flags & O_NONBLOCK)) {
3816 return -EAGAIN;
3817 }
3818
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003819 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003820
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003821 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003822
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003823 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003824
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003825 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003826 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003827
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003828 /*
Liu Bo250bfd32013-01-14 10:54:11 +08003829 * We block until we read something and tracing is disabled.
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003830 * We still block if tracing is disabled, but we have never
3831 * read anything. This allows a user to cat this file, and
3832 * then enable tracing. But after we have read something,
3833 * we give an EOF when tracing is again disabled.
3834 *
3835 * iter->pos will be 0 if we haven't read anything.
3836 */
Liu Bo250bfd32013-01-14 10:54:11 +08003837 if (!tracing_is_enabled() && iter->pos)
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003838 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003839 }
3840
3841 return 1;
3842}
3843
Steven Rostedtb3806b42008-05-12 21:20:46 +02003844/*
3845 * Consumer reader.
3846 */
3847static ssize_t
3848tracing_read_pipe(struct file *filp, char __user *ubuf,
3849 size_t cnt, loff_t *ppos)
3850{
3851 struct trace_iterator *iter = filp->private_data;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003852 struct trace_array *tr = iter->tr;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003853 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003854
3855 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003856 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3857 if (sret != -EBUSY)
3858 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003859
Steven Rostedtf9520752009-03-02 14:04:40 -05003860 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003861
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003862 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02003863 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003864 if (unlikely(iter->trace->name != tr->current_trace->name))
3865 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003866 mutex_unlock(&trace_types_lock);
3867
3868 /*
3869 * Avoid more than one consumer on a single file descriptor
3870 * This is just a matter of traces coherency, the ring buffer itself
3871 * is protected.
3872 */
3873 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003874 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003875 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3876 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02003877 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02003878 }
3879
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003880waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003881 sret = tracing_wait_pipe(filp);
3882 if (sret <= 0)
3883 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003884
3885 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003886 if (trace_empty(iter)) {
3887 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02003888 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003889 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003890
3891 if (cnt >= PAGE_SIZE)
3892 cnt = PAGE_SIZE - 1;
3893
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003894 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003895 memset(&iter->seq, 0,
3896 sizeof(struct trace_iterator) -
3897 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02003898 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003899
Lai Jiangshan4f535962009-05-18 19:35:34 +08003900 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003901 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05003902 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003903 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003904 int len = iter->seq.len;
3905
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003906 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003907 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02003908 /* don't print partial lines */
3909 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003910 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003911 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01003912 if (ret != TRACE_TYPE_NO_CONSUME)
3913 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003914
3915 if (iter->seq.len >= cnt)
3916 break;
Jiri Olsaee5e51f2011-03-25 12:05:18 +01003917
3918 /*
3919 * Setting the full flag means we reached the trace_seq buffer
3920 * size and we should leave by partial output condition above.
3921 * One of the trace_seq_* functions is not used properly.
3922 */
3923 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3924 iter->ent->type);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003925 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003926 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003927 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02003928
Steven Rostedtb3806b42008-05-12 21:20:46 +02003929 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003930 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3931 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05003932 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003933
3934 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003935 * If there was nothing to send to user, in spite of consuming trace
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003936 * entries, go back to wait for more entries.
3937 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003938 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003939 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003940
Steven Rostedt107bad82008-05-12 21:21:01 +02003941out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003942 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003943
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003944 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003945}
3946
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003947static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3948 struct pipe_buffer *buf)
3949{
3950 __free_page(buf->page);
3951}
3952
3953static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3954 unsigned int idx)
3955{
3956 __free_page(spd->pages[idx]);
3957}
3958
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08003959static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003960 .can_merge = 0,
3961 .map = generic_pipe_buf_map,
3962 .unmap = generic_pipe_buf_unmap,
3963 .confirm = generic_pipe_buf_confirm,
3964 .release = tracing_pipe_buf_release,
3965 .steal = generic_pipe_buf_steal,
3966 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003967};
3968
Steven Rostedt34cd4992009-02-09 12:06:29 -05003969static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003970tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003971{
3972 size_t count;
3973 int ret;
3974
3975 /* Seq buffer is page-sized, exactly what we need. */
3976 for (;;) {
3977 count = iter->seq.len;
3978 ret = print_trace_line(iter);
3979 count = iter->seq.len - count;
3980 if (rem < count) {
3981 rem = 0;
3982 iter->seq.len -= count;
3983 break;
3984 }
3985 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3986 iter->seq.len -= count;
3987 break;
3988 }
3989
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08003990 if (ret != TRACE_TYPE_NO_CONSUME)
3991 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05003992 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05003993 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003994 rem = 0;
3995 iter->ent = NULL;
3996 break;
3997 }
3998 }
3999
4000 return rem;
4001}
4002
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004003static ssize_t tracing_splice_read_pipe(struct file *filp,
4004 loff_t *ppos,
4005 struct pipe_inode_info *pipe,
4006 size_t len,
4007 unsigned int flags)
4008{
Jens Axboe35f3d142010-05-20 10:43:18 +02004009 struct page *pages_def[PIPE_DEF_BUFFERS];
4010 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004011 struct trace_iterator *iter = filp->private_data;
4012 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02004013 .pages = pages_def,
4014 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05004015 .nr_pages = 0, /* This gets updated below. */
Eric Dumazet047fe362012-06-12 15:24:40 +02004016 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt34cd4992009-02-09 12:06:29 -05004017 .flags = flags,
4018 .ops = &tracing_pipe_buf_ops,
4019 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004020 };
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004021 struct trace_array *tr = iter->tr;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004022 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05004023 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004024 unsigned int i;
4025
Jens Axboe35f3d142010-05-20 10:43:18 +02004026 if (splice_grow_spd(pipe, &spd))
4027 return -ENOMEM;
4028
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004029 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004030 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004031 if (unlikely(iter->trace->name != tr->current_trace->name))
4032 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004033 mutex_unlock(&trace_types_lock);
4034
4035 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004036
4037 if (iter->trace->splice_read) {
4038 ret = iter->trace->splice_read(iter, filp,
4039 ppos, pipe, len, flags);
4040 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05004041 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004042 }
4043
4044 ret = tracing_wait_pipe(filp);
4045 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05004046 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004047
Jason Wessel955b61e2010-08-05 09:22:23 -05004048 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004049 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05004050 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004051 }
4052
Lai Jiangshan4f535962009-05-18 19:35:34 +08004053 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004054 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08004055
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004056 /* Fill as many pages as possible. */
Jens Axboe35f3d142010-05-20 10:43:18 +02004057 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4058 spd.pages[i] = alloc_page(GFP_KERNEL);
4059 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05004060 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004061
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01004062 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004063
4064 /* Copy the data into the page, so we can start over. */
4065 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02004066 page_address(spd.pages[i]),
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004067 iter->seq.len);
4068 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02004069 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004070 break;
4071 }
Jens Axboe35f3d142010-05-20 10:43:18 +02004072 spd.partial[i].offset = 0;
4073 spd.partial[i].len = iter->seq.len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004074
Steven Rostedtf9520752009-03-02 14:04:40 -05004075 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004076 }
4077
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004078 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08004079 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004080 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004081
4082 spd.nr_pages = i;
4083
Jens Axboe35f3d142010-05-20 10:43:18 +02004084 ret = splice_to_pipe(pipe, &spd);
4085out:
Eric Dumazet047fe362012-06-12 15:24:40 +02004086 splice_shrink_spd(&spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02004087 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004088
Steven Rostedt34cd4992009-02-09 12:06:29 -05004089out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004090 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02004091 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004092}
4093
Steven Rostedta98a3c32008-05-12 21:20:59 +02004094static ssize_t
4095tracing_entries_read(struct file *filp, char __user *ubuf,
4096 size_t cnt, loff_t *ppos)
4097{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004098 struct trace_cpu *tc = filp->private_data;
4099 struct trace_array *tr = tc->tr;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004100 char buf[64];
4101 int r = 0;
4102 ssize_t ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004103
Steven Rostedtdb526ca2009-03-12 13:53:25 -04004104 mutex_lock(&trace_types_lock);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004105
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004106 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004107 int cpu, buf_size_same;
4108 unsigned long size;
4109
4110 size = 0;
4111 buf_size_same = 1;
4112 /* check if all cpu sizes are same */
4113 for_each_tracing_cpu(cpu) {
4114 /* fill in the size from first enabled cpu */
4115 if (size == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004116 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4117 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004118 buf_size_same = 0;
4119 break;
4120 }
4121 }
4122
4123 if (buf_size_same) {
4124 if (!ring_buffer_expanded)
4125 r = sprintf(buf, "%lu (expanded: %lu)\n",
4126 size >> 10,
4127 trace_buf_size >> 10);
4128 else
4129 r = sprintf(buf, "%lu\n", size >> 10);
4130 } else
4131 r = sprintf(buf, "X\n");
4132 } else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004133 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004134
Steven Rostedtdb526ca2009-03-12 13:53:25 -04004135 mutex_unlock(&trace_types_lock);
4136
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004137 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4138 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004139}
4140
4141static ssize_t
4142tracing_entries_write(struct file *filp, const char __user *ubuf,
4143 size_t cnt, loff_t *ppos)
4144{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004145 struct trace_cpu *tc = filp->private_data;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004146 unsigned long val;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004147 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004148
Peter Huewe22fe9b52011-06-07 21:58:27 +02004149 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4150 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02004151 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004152
4153 /* must have at least 1 entry */
4154 if (!val)
4155 return -EINVAL;
4156
Steven Rostedt1696b2b2008-11-13 00:09:35 -05004157 /* value is in KB */
4158 val <<= 10;
4159
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004160 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004161 if (ret < 0)
4162 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004163
Jiri Olsacf8517c2009-10-23 19:36:16 -04004164 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004165
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004166 return cnt;
4167}
Steven Rostedtbf5e6512008-11-10 21:46:00 -05004168
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004169static ssize_t
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07004170tracing_total_entries_read(struct file *filp, char __user *ubuf,
4171 size_t cnt, loff_t *ppos)
4172{
4173 struct trace_array *tr = filp->private_data;
4174 char buf[64];
4175 int r, cpu;
4176 unsigned long size = 0, expanded_size = 0;
4177
4178 mutex_lock(&trace_types_lock);
4179 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004180 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07004181 if (!ring_buffer_expanded)
4182 expanded_size += trace_buf_size >> 10;
4183 }
4184 if (ring_buffer_expanded)
4185 r = sprintf(buf, "%lu\n", size);
4186 else
4187 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4188 mutex_unlock(&trace_types_lock);
4189
4190 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4191}
4192
4193static ssize_t
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004194tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4195 size_t cnt, loff_t *ppos)
4196{
4197 /*
4198 * There is no need to read what the user has written, this function
4199 * is just to make sure that there is no error when "echo" is used
4200 */
4201
4202 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004203
4204 return cnt;
4205}
4206
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004207static int
4208tracing_free_buffer_release(struct inode *inode, struct file *filp)
4209{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004210 struct trace_array *tr = inode->i_private;
4211
Steven Rostedtcf30cf62011-06-14 22:44:07 -04004212 /* disable tracing ? */
4213 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4214 tracing_off();
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004215 /* resize the ring buffer to 0 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004216 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004217
4218 return 0;
4219}
4220
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004221static ssize_t
4222tracing_mark_write(struct file *filp, const char __user *ubuf,
4223 size_t cnt, loff_t *fpos)
4224{
Steven Rostedtd696b582011-09-22 11:50:27 -04004225 unsigned long addr = (unsigned long)ubuf;
4226 struct ring_buffer_event *event;
4227 struct ring_buffer *buffer;
4228 struct print_entry *entry;
4229 unsigned long irq_flags;
4230 struct page *pages[2];
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004231 void *map_page[2];
Steven Rostedtd696b582011-09-22 11:50:27 -04004232 int nr_pages = 1;
4233 ssize_t written;
Steven Rostedtd696b582011-09-22 11:50:27 -04004234 int offset;
4235 int size;
4236 int len;
4237 int ret;
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004238 int i;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004239
Steven Rostedtc76f0692008-11-07 22:36:02 -05004240 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004241 return -EINVAL;
4242
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -07004243 if (!(trace_flags & TRACE_ITER_MARKERS))
4244 return -EINVAL;
4245
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004246 if (cnt > TRACE_BUF_SIZE)
4247 cnt = TRACE_BUF_SIZE;
4248
Steven Rostedtd696b582011-09-22 11:50:27 -04004249 /*
4250 * Userspace is injecting traces into the kernel trace buffer.
4251 * We want to be as non intrusive as possible.
4252 * To do so, we do not want to allocate any special buffers
4253 * or take any locks, but instead write the userspace data
4254 * straight into the ring buffer.
4255 *
4256 * First we need to pin the userspace buffer into memory,
4257 * which, most likely it is, because it just referenced it.
4258 * But there's no guarantee that it is. By using get_user_pages_fast()
4259 * and kmap_atomic/kunmap_atomic() we can get access to the
4260 * pages directly. We then write the data directly into the
4261 * ring buffer.
4262 */
4263 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004264
Steven Rostedtd696b582011-09-22 11:50:27 -04004265 /* check if we cross pages */
4266 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4267 nr_pages = 2;
4268
4269 offset = addr & (PAGE_SIZE - 1);
4270 addr &= PAGE_MASK;
4271
4272 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4273 if (ret < nr_pages) {
4274 while (--ret >= 0)
4275 put_page(pages[ret]);
4276 written = -EFAULT;
4277 goto out;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004278 }
4279
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004280 for (i = 0; i < nr_pages; i++)
4281 map_page[i] = kmap_atomic(pages[i]);
Steven Rostedtd696b582011-09-22 11:50:27 -04004282
4283 local_save_flags(irq_flags);
4284 size = sizeof(*entry) + cnt + 2; /* possible \n added */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004285 buffer = global_trace.trace_buffer.buffer;
Steven Rostedtd696b582011-09-22 11:50:27 -04004286 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4287 irq_flags, preempt_count());
4288 if (!event) {
4289 /* Ring buffer disabled, return as if not open for write */
4290 written = -EBADF;
4291 goto out_unlock;
4292 }
4293
4294 entry = ring_buffer_event_data(event);
4295 entry->ip = _THIS_IP_;
4296
4297 if (nr_pages == 2) {
4298 len = PAGE_SIZE - offset;
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004299 memcpy(&entry->buf, map_page[0] + offset, len);
4300 memcpy(&entry->buf[len], map_page[1], cnt - len);
Steven Rostedtd696b582011-09-22 11:50:27 -04004301 } else
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004302 memcpy(&entry->buf, map_page[0] + offset, cnt);
Steven Rostedtd696b582011-09-22 11:50:27 -04004303
4304 if (entry->buf[cnt - 1] != '\n') {
4305 entry->buf[cnt] = '\n';
4306 entry->buf[cnt + 1] = '\0';
4307 } else
4308 entry->buf[cnt] = '\0';
4309
Steven Rostedt7ffbd482012-10-11 12:14:25 -04004310 __buffer_unlock_commit(buffer, event);
Steven Rostedtd696b582011-09-22 11:50:27 -04004311
4312 written = cnt;
4313
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02004314 *fpos += written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004315
Steven Rostedtd696b582011-09-22 11:50:27 -04004316 out_unlock:
Steven Rostedt6edb2a82012-05-11 23:28:49 -04004317 for (i = 0; i < nr_pages; i++){
4318 kunmap_atomic(map_page[i]);
4319 put_page(pages[i]);
4320 }
Steven Rostedtd696b582011-09-22 11:50:27 -04004321 out:
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02004322 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004323}
4324
Li Zefan13f16d22009-12-08 11:16:11 +08004325static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08004326{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004327 struct trace_array *tr = m->private;
Zhaolei5079f322009-08-25 16:12:56 +08004328 int i;
4329
4330 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08004331 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08004332 "%s%s%s%s", i ? " " : "",
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004333 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4334 i == tr->clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08004335 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08004336
Li Zefan13f16d22009-12-08 11:16:11 +08004337 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08004338}
4339
4340static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4341 size_t cnt, loff_t *fpos)
4342{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004343 struct seq_file *m = filp->private_data;
4344 struct trace_array *tr = m->private;
Zhaolei5079f322009-08-25 16:12:56 +08004345 char buf[64];
4346 const char *clockstr;
4347 int i;
4348
4349 if (cnt >= sizeof(buf))
4350 return -EINVAL;
4351
4352 if (copy_from_user(&buf, ubuf, cnt))
4353 return -EFAULT;
4354
4355 buf[cnt] = 0;
4356
4357 clockstr = strstrip(buf);
4358
4359 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4360 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4361 break;
4362 }
4363 if (i == ARRAY_SIZE(trace_clocks))
4364 return -EINVAL;
4365
Zhaolei5079f322009-08-25 16:12:56 +08004366 mutex_lock(&trace_types_lock);
4367
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004368 tr->clock_id = i;
4369
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004370 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
Zhaolei5079f322009-08-25 16:12:56 +08004371
David Sharp60303ed2012-10-11 16:27:52 -07004372 /*
4373 * New clock may not be consistent with the previous clock.
4374 * Reset the buffer so that it doesn't have incomparable timestamps.
4375 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004376 tracing_reset_online_cpus(&global_trace.trace_buffer);
4377
4378#ifdef CONFIG_TRACER_MAX_TRACE
4379 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4380 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4381 tracing_reset_online_cpus(&global_trace.max_buffer);
4382#endif
David Sharp60303ed2012-10-11 16:27:52 -07004383
Zhaolei5079f322009-08-25 16:12:56 +08004384 mutex_unlock(&trace_types_lock);
4385
4386 *fpos += cnt;
4387
4388 return cnt;
4389}
4390
Li Zefan13f16d22009-12-08 11:16:11 +08004391static int tracing_clock_open(struct inode *inode, struct file *file)
4392{
4393 if (tracing_disabled)
4394 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004395
4396 return single_open(file, tracing_clock_show, inode->i_private);
Li Zefan13f16d22009-12-08 11:16:11 +08004397}
4398
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004399struct ftrace_buffer_info {
4400 struct trace_iterator iter;
4401 void *spare;
4402 unsigned int read;
4403};
4404
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004405#ifdef CONFIG_TRACER_SNAPSHOT
4406static int tracing_snapshot_open(struct inode *inode, struct file *file)
4407{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004408 struct trace_cpu *tc = inode->i_private;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004409 struct trace_iterator *iter;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004410 struct seq_file *m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004411 int ret = 0;
4412
4413 if (file->f_mode & FMODE_READ) {
4414 iter = __tracing_open(inode, file, true);
4415 if (IS_ERR(iter))
4416 ret = PTR_ERR(iter);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004417 } else {
4418 /* Writes still need the seq_file to hold the private data */
4419 m = kzalloc(sizeof(*m), GFP_KERNEL);
4420 if (!m)
4421 return -ENOMEM;
4422 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4423 if (!iter) {
4424 kfree(m);
4425 return -ENOMEM;
4426 }
4427 iter->tr = tc->tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004428 iter->trace_buffer = &tc->tr->max_buffer;
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004429 iter->cpu_file = tc->cpu;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004430 m->private = iter;
4431 file->private_data = m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004432 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004433
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004434 return ret;
4435}
4436
4437static ssize_t
4438tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4439 loff_t *ppos)
4440{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004441 struct seq_file *m = filp->private_data;
4442 struct trace_iterator *iter = m->private;
4443 struct trace_array *tr = iter->tr;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004444 unsigned long val;
4445 int ret;
4446
4447 ret = tracing_update_buffers();
4448 if (ret < 0)
4449 return ret;
4450
4451 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4452 if (ret)
4453 return ret;
4454
4455 mutex_lock(&trace_types_lock);
4456
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004457 if (tr->current_trace->use_max_tr) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004458 ret = -EBUSY;
4459 goto out;
4460 }
4461
4462 switch (val) {
4463 case 0:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004464 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4465 ret = -EINVAL;
4466 break;
4467 }
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05004468 if (tr->allocated_snapshot) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004469 /* free spare buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004470 ring_buffer_resize(tr->max_buffer.buffer, 1,
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004471 RING_BUFFER_ALL_CPUS);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004472 set_buffer_entries(&tr->max_buffer, 1);
4473 tracing_reset_online_cpus(&tr->max_buffer);
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05004474 tr->allocated_snapshot = false;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004475 }
4476 break;
4477 case 1:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004478/* Only allow per-cpu swap if the ring buffer supports it */
4479#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4480 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4481 ret = -EINVAL;
4482 break;
4483 }
4484#endif
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05004485 if (!tr->allocated_snapshot) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004486 /* allocate spare buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004487 ret = resize_buffer_duplicate_size(&tr->max_buffer,
4488 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004489 if (ret < 0)
4490 break;
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05004491 tr->allocated_snapshot = true;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004492 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004493 local_irq_disable();
4494 /* Now, we're going to swap */
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004495 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05004496 update_max_tr(tr, current, smp_processor_id());
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004497 else
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05004498 update_max_tr_single(tr, current, iter->cpu_file);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004499 local_irq_enable();
4500 break;
4501 default:
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05004502 if (tr->allocated_snapshot) {
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004503 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4504 tracing_reset_online_cpus(&tr->max_buffer);
4505 else
4506 tracing_reset(&tr->max_buffer, iter->cpu_file);
4507 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004508 break;
4509 }
4510
4511 if (ret >= 0) {
4512 *ppos += cnt;
4513 ret = cnt;
4514 }
4515out:
4516 mutex_unlock(&trace_types_lock);
4517 return ret;
4518}
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004519
4520static int tracing_snapshot_release(struct inode *inode, struct file *file)
4521{
4522 struct seq_file *m = file->private_data;
4523
4524 if (file->f_mode & FMODE_READ)
4525 return tracing_release(inode, file);
4526
4527 /* If write only, the seq_file is just a stub */
4528 if (m)
4529 kfree(m->private);
4530 kfree(m);
4531
4532 return 0;
4533}
4534
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004535static int tracing_buffers_open(struct inode *inode, struct file *filp);
4536static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4537 size_t count, loff_t *ppos);
4538static int tracing_buffers_release(struct inode *inode, struct file *file);
4539static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4540 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4541
4542static int snapshot_raw_open(struct inode *inode, struct file *filp)
4543{
4544 struct ftrace_buffer_info *info;
4545 int ret;
4546
4547 ret = tracing_buffers_open(inode, filp);
4548 if (ret < 0)
4549 return ret;
4550
4551 info = filp->private_data;
4552
4553 if (info->iter.trace->use_max_tr) {
4554 tracing_buffers_release(inode, filp);
4555 return -EBUSY;
4556 }
4557
4558 info->iter.snapshot = true;
4559 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4560
4561 return ret;
4562}
4563
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004564#endif /* CONFIG_TRACER_SNAPSHOT */
4565
4566
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004567static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004568 .open = tracing_open_generic,
4569 .read = tracing_max_lat_read,
4570 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004571 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004572};
4573
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004574static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004575 .open = tracing_open_generic,
4576 .read = tracing_set_trace_read,
4577 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004578 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004579};
4580
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004581static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004582 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02004583 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004584 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004585 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004586 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004587 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02004588};
4589
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004590static const struct file_operations tracing_entries_fops = {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004591 .open = tracing_open_generic,
Steven Rostedta98a3c32008-05-12 21:20:59 +02004592 .read = tracing_entries_read,
4593 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004594 .llseek = generic_file_llseek,
Steven Rostedta98a3c32008-05-12 21:20:59 +02004595};
4596
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07004597static const struct file_operations tracing_total_entries_fops = {
4598 .open = tracing_open_generic,
4599 .read = tracing_total_entries_read,
4600 .llseek = generic_file_llseek,
4601};
4602
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004603static const struct file_operations tracing_free_buffer_fops = {
4604 .write = tracing_free_buffer_write,
4605 .release = tracing_free_buffer_release,
4606};
4607
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004608static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004609 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004610 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004611 .llseek = generic_file_llseek,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004612};
4613
Zhaolei5079f322009-08-25 16:12:56 +08004614static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08004615 .open = tracing_clock_open,
4616 .read = seq_read,
4617 .llseek = seq_lseek,
4618 .release = single_release,
Zhaolei5079f322009-08-25 16:12:56 +08004619 .write = tracing_clock_write,
4620};
4621
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004622#ifdef CONFIG_TRACER_SNAPSHOT
4623static const struct file_operations snapshot_fops = {
4624 .open = tracing_snapshot_open,
4625 .read = seq_read,
4626 .write = tracing_snapshot_write,
4627 .llseek = tracing_seek,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004628 .release = tracing_snapshot_release,
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004629};
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004630
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004631static const struct file_operations snapshot_raw_fops = {
4632 .open = snapshot_raw_open,
4633 .read = tracing_buffers_read,
4634 .release = tracing_buffers_release,
4635 .splice_read = tracing_buffers_splice_read,
4636 .llseek = no_llseek,
Steven Rostedt2cadf912008-12-01 22:20:19 -05004637};
4638
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004639#endif /* CONFIG_TRACER_SNAPSHOT */
4640
Steven Rostedt2cadf912008-12-01 22:20:19 -05004641static int tracing_buffers_open(struct inode *inode, struct file *filp)
4642{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004643 struct trace_cpu *tc = inode->i_private;
4644 struct trace_array *tr = tc->tr;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004645 struct ftrace_buffer_info *info;
4646
4647 if (tracing_disabled)
4648 return -ENODEV;
4649
4650 info = kzalloc(sizeof(*info), GFP_KERNEL);
4651 if (!info)
4652 return -ENOMEM;
4653
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05004654 mutex_lock(&trace_types_lock);
4655
4656 tr->ref++;
4657
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004658 info->iter.tr = tr;
4659 info->iter.cpu_file = tc->cpu;
Steven Rostedtb6273442013-02-28 13:44:11 -05004660 info->iter.trace = tr->current_trace;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004661 info->iter.trace_buffer = &tr->trace_buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004662 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004663 /* Force reading ring buffer for first read */
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004664 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004665
4666 filp->private_data = info;
4667
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05004668 mutex_unlock(&trace_types_lock);
4669
Lai Jiangshand1e7e022009-04-02 15:16:56 +08004670 return nonseekable_open(inode, filp);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004671}
4672
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004673static unsigned int
4674tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4675{
4676 struct ftrace_buffer_info *info = filp->private_data;
4677 struct trace_iterator *iter = &info->iter;
4678
4679 return trace_poll(iter, filp, poll_table);
4680}
4681
Steven Rostedt2cadf912008-12-01 22:20:19 -05004682static ssize_t
4683tracing_buffers_read(struct file *filp, char __user *ubuf,
4684 size_t count, loff_t *ppos)
4685{
4686 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004687 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004688 ssize_t ret;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004689 ssize_t size;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004690
Steven Rostedt2dc5d122009-03-04 19:10:05 -05004691 if (!count)
4692 return 0;
4693
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004694 mutex_lock(&trace_types_lock);
4695
4696#ifdef CONFIG_TRACER_MAX_TRACE
4697 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4698 size = -EBUSY;
4699 goto out_unlock;
4700 }
4701#endif
4702
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004703 if (!info->spare)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004704 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
4705 iter->cpu_file);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004706 size = -ENOMEM;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004707 if (!info->spare)
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004708 goto out_unlock;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004709
Steven Rostedt2cadf912008-12-01 22:20:19 -05004710 /* Do we have previous read data to read? */
4711 if (info->read < PAGE_SIZE)
4712 goto read;
4713
Steven Rostedtb6273442013-02-28 13:44:11 -05004714 again:
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004715 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004716 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
Steven Rostedt2cadf912008-12-01 22:20:19 -05004717 &info->spare,
4718 count,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004719 iter->cpu_file, 0);
4720 trace_access_unlock(iter->cpu_file);
Steven Rostedtb6273442013-02-28 13:44:11 -05004721
4722 if (ret < 0) {
4723 if (trace_empty(iter)) {
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004724 if ((filp->f_flags & O_NONBLOCK)) {
4725 size = -EAGAIN;
4726 goto out_unlock;
4727 }
4728 mutex_unlock(&trace_types_lock);
Steven Rostedtb6273442013-02-28 13:44:11 -05004729 iter->trace->wait_pipe(iter);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004730 mutex_lock(&trace_types_lock);
4731 if (signal_pending(current)) {
4732 size = -EINTR;
4733 goto out_unlock;
4734 }
Steven Rostedtb6273442013-02-28 13:44:11 -05004735 goto again;
4736 }
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004737 size = 0;
4738 goto out_unlock;
Steven Rostedtb6273442013-02-28 13:44:11 -05004739 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05004740
Steven Rostedt436fc282011-10-14 10:44:25 -04004741 info->read = 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05004742 read:
Steven Rostedt2cadf912008-12-01 22:20:19 -05004743 size = PAGE_SIZE - info->read;
4744 if (size > count)
4745 size = count;
4746
4747 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004748 if (ret == size) {
4749 size = -EFAULT;
4750 goto out_unlock;
4751 }
Steven Rostedt2dc5d122009-03-04 19:10:05 -05004752 size -= ret;
4753
Steven Rostedt2cadf912008-12-01 22:20:19 -05004754 *ppos += size;
4755 info->read += size;
4756
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004757 out_unlock:
4758 mutex_unlock(&trace_types_lock);
4759
Steven Rostedt2cadf912008-12-01 22:20:19 -05004760 return size;
4761}
4762
4763static int tracing_buffers_release(struct inode *inode, struct file *file)
4764{
4765 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004766 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004767
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05004768 mutex_lock(&trace_types_lock);
4769
4770 WARN_ON(!iter->tr->ref);
4771 iter->tr->ref--;
4772
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004773 if (info->spare)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004774 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004775 kfree(info);
4776
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05004777 mutex_unlock(&trace_types_lock);
4778
Steven Rostedt2cadf912008-12-01 22:20:19 -05004779 return 0;
4780}
4781
4782struct buffer_ref {
4783 struct ring_buffer *buffer;
4784 void *page;
4785 int ref;
4786};
4787
4788static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4789 struct pipe_buffer *buf)
4790{
4791 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4792
4793 if (--ref->ref)
4794 return;
4795
4796 ring_buffer_free_read_page(ref->buffer, ref->page);
4797 kfree(ref);
4798 buf->private = 0;
4799}
4800
Steven Rostedt2cadf912008-12-01 22:20:19 -05004801static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4802 struct pipe_buffer *buf)
4803{
4804 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4805
4806 ref->ref++;
4807}
4808
4809/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08004810static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05004811 .can_merge = 0,
4812 .map = generic_pipe_buf_map,
4813 .unmap = generic_pipe_buf_unmap,
4814 .confirm = generic_pipe_buf_confirm,
4815 .release = buffer_pipe_buf_release,
Masami Hiramatsud55cb6c2012-08-09 21:31:10 +09004816 .steal = generic_pipe_buf_steal,
Steven Rostedt2cadf912008-12-01 22:20:19 -05004817 .get = buffer_pipe_buf_get,
4818};
4819
4820/*
4821 * Callback from splice_to_pipe(), if we need to release some pages
4822 * at the end of the spd in case we error'ed out in filling the pipe.
4823 */
4824static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4825{
4826 struct buffer_ref *ref =
4827 (struct buffer_ref *)spd->partial[i].private;
4828
4829 if (--ref->ref)
4830 return;
4831
4832 ring_buffer_free_read_page(ref->buffer, ref->page);
4833 kfree(ref);
4834 spd->partial[i].private = 0;
4835}
4836
4837static ssize_t
4838tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4839 struct pipe_inode_info *pipe, size_t len,
4840 unsigned int flags)
4841{
4842 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004843 struct trace_iterator *iter = &info->iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02004844 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4845 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05004846 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02004847 .pages = pages_def,
4848 .partial = partial_def,
Eric Dumazet047fe362012-06-12 15:24:40 +02004849 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt2cadf912008-12-01 22:20:19 -05004850 .flags = flags,
4851 .ops = &buffer_pipe_buf_ops,
4852 .spd_release = buffer_spd_release,
4853 };
4854 struct buffer_ref *ref;
Steven Rostedt93459c62009-04-29 00:23:13 -04004855 int entries, size, i;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004856 ssize_t ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004857
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004858 mutex_lock(&trace_types_lock);
4859
4860#ifdef CONFIG_TRACER_MAX_TRACE
4861 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4862 ret = -EBUSY;
4863 goto out;
4864 }
4865#endif
4866
4867 if (splice_grow_spd(pipe, &spd)) {
4868 ret = -ENOMEM;
4869 goto out;
4870 }
Jens Axboe35f3d142010-05-20 10:43:18 +02004871
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08004872 if (*ppos & (PAGE_SIZE - 1)) {
Jens Axboe35f3d142010-05-20 10:43:18 +02004873 ret = -EINVAL;
4874 goto out;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08004875 }
4876
4877 if (len & (PAGE_SIZE - 1)) {
Jens Axboe35f3d142010-05-20 10:43:18 +02004878 if (len < PAGE_SIZE) {
4879 ret = -EINVAL;
4880 goto out;
4881 }
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08004882 len &= PAGE_MASK;
4883 }
4884
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004885 again:
4886 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004887 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt93459c62009-04-29 00:23:13 -04004888
Jens Axboe35f3d142010-05-20 10:43:18 +02004889 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05004890 struct page *page;
4891 int r;
4892
4893 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4894 if (!ref)
4895 break;
4896
Steven Rostedt7267fa62009-04-29 00:16:21 -04004897 ref->ref = 1;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004898 ref->buffer = iter->trace_buffer->buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004899 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004900 if (!ref->page) {
4901 kfree(ref);
4902 break;
4903 }
4904
4905 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004906 len, iter->cpu_file, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004907 if (r < 0) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004908 ring_buffer_free_read_page(ref->buffer, ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004909 kfree(ref);
4910 break;
4911 }
4912
4913 /*
4914 * zero out any left over data, this is going to
4915 * user land.
4916 */
4917 size = ring_buffer_page_len(ref->page);
4918 if (size < PAGE_SIZE)
4919 memset(ref->page + size, 0, PAGE_SIZE - size);
4920
4921 page = virt_to_page(ref->page);
4922
4923 spd.pages[i] = page;
4924 spd.partial[i].len = PAGE_SIZE;
4925 spd.partial[i].offset = 0;
4926 spd.partial[i].private = (unsigned long)ref;
4927 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08004928 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04004929
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004930 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004931 }
4932
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004933 trace_access_unlock(iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004934 spd.nr_pages = i;
4935
4936 /* did we read anything? */
4937 if (!spd.nr_pages) {
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004938 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05004939 ret = -EAGAIN;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004940 goto out;
4941 }
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004942 mutex_unlock(&trace_types_lock);
Steven Rostedtb6273442013-02-28 13:44:11 -05004943 iter->trace->wait_pipe(iter);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004944 mutex_lock(&trace_types_lock);
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004945 if (signal_pending(current)) {
4946 ret = -EINTR;
4947 goto out;
4948 }
4949 goto again;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004950 }
4951
4952 ret = splice_to_pipe(pipe, &spd);
Eric Dumazet047fe362012-06-12 15:24:40 +02004953 splice_shrink_spd(&spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02004954out:
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05004955 mutex_unlock(&trace_types_lock);
4956
Steven Rostedt2cadf912008-12-01 22:20:19 -05004957 return ret;
4958}
4959
4960static const struct file_operations tracing_buffers_fops = {
4961 .open = tracing_buffers_open,
4962 .read = tracing_buffers_read,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004963 .poll = tracing_buffers_poll,
Steven Rostedt2cadf912008-12-01 22:20:19 -05004964 .release = tracing_buffers_release,
4965 .splice_read = tracing_buffers_splice_read,
4966 .llseek = no_llseek,
4967};
4968
Steven Rostedtc8d77182009-04-29 18:03:45 -04004969static ssize_t
4970tracing_stats_read(struct file *filp, char __user *ubuf,
4971 size_t count, loff_t *ppos)
4972{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004973 struct trace_cpu *tc = filp->private_data;
4974 struct trace_array *tr = tc->tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004975 struct trace_buffer *trace_buf = &tr->trace_buffer;
Steven Rostedtc8d77182009-04-29 18:03:45 -04004976 struct trace_seq *s;
4977 unsigned long cnt;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004978 unsigned long long t;
4979 unsigned long usec_rem;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004980 int cpu = tc->cpu;
Steven Rostedtc8d77182009-04-29 18:03:45 -04004981
Li Zefane4f2d102009-06-15 10:57:28 +08004982 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04004983 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01004984 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04004985
4986 trace_seq_init(s);
4987
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004988 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04004989 trace_seq_printf(s, "entries: %ld\n", cnt);
4990
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004991 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04004992 trace_seq_printf(s, "overrun: %ld\n", cnt);
4993
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004994 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04004995 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4996
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004997 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004998 trace_seq_printf(s, "bytes: %ld\n", cnt);
4999
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005000 if (trace_clocks[trace_clock_id].in_ns) {
5001 /* local or global for trace_clock */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005002 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005003 usec_rem = do_div(t, USEC_PER_SEC);
5004 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5005 t, usec_rem);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005006
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005007 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005008 usec_rem = do_div(t, USEC_PER_SEC);
5009 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5010 } else {
5011 /* counter or tsc mode for trace_clock */
5012 trace_seq_printf(s, "oldest event ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005013 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005014
5015 trace_seq_printf(s, "now ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005016 ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005017 }
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005018
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005019 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
Slava Pestov884bfe82011-07-15 14:23:58 -07005020 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5021
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005022 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05005023 trace_seq_printf(s, "read events: %ld\n", cnt);
5024
Steven Rostedtc8d77182009-04-29 18:03:45 -04005025 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5026
5027 kfree(s);
5028
5029 return count;
5030}
5031
5032static const struct file_operations tracing_stats_fops = {
5033 .open = tracing_open_generic,
5034 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005035 .llseek = generic_file_llseek,
Steven Rostedtc8d77182009-04-29 18:03:45 -04005036};
5037
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005038#ifdef CONFIG_DYNAMIC_FTRACE
5039
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005040int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005041{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005042 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005043}
5044
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005045static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005046tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005047 size_t cnt, loff_t *ppos)
5048{
Steven Rostedta26a2a22008-10-31 00:03:22 -04005049 static char ftrace_dyn_info_buffer[1024];
5050 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005051 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005052 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04005053 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005054 int r;
5055
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005056 mutex_lock(&dyn_info_mutex);
5057 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005058
Steven Rostedta26a2a22008-10-31 00:03:22 -04005059 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005060 buf[r++] = '\n';
5061
5062 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5063
5064 mutex_unlock(&dyn_info_mutex);
5065
5066 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005067}
5068
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005069static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005070 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005071 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005072 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005073};
5074#endif
5075
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005076struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005077{
5078 static int once;
5079
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005080 if (tr->dir)
5081 return tr->dir;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005082
Frederic Weisbecker3e1f60b2009-03-22 23:10:45 +01005083 if (!debugfs_initialized())
5084 return NULL;
5085
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005086 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5087 tr->dir = debugfs_create_dir("tracing", NULL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005088
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005089 if (!tr->dir && !once) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005090 once = 1;
5091 pr_warning("Could not create debugfs directory 'tracing'\n");
5092 return NULL;
5093 }
5094
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005095 return tr->dir;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005096}
5097
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005098struct dentry *tracing_init_dentry(void)
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005099{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005100 return tracing_init_dentry_tr(&global_trace);
5101}
5102
5103static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5104{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005105 struct dentry *d_tracer;
5106
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005107 if (tr->percpu_dir)
5108 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005109
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005110 d_tracer = tracing_init_dentry_tr(tr);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005111 if (!d_tracer)
5112 return NULL;
5113
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005114 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005115
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005116 WARN_ONCE(!tr->percpu_dir,
5117 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005118
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005119 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005120}
5121
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005122static void
5123tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005124{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005125 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005126 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005127 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04005128 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005129
Namhyung Kim0a3d7ce2012-04-23 10:11:57 +09005130 if (!d_percpu)
5131 return;
5132
Steven Rostedtdd49a382010-10-20 21:51:26 -04005133 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01005134 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5135 if (!d_cpu) {
5136 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5137 return;
5138 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005139
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01005140 /* per cpu trace_pipe */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005141 trace_create_file("trace_pipe", 0444, d_cpu,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005142 (void *)&data->trace_cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005143
5144 /* per cpu trace */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005145 trace_create_file("trace", 0644, d_cpu,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005146 (void *)&data->trace_cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04005147
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005148 trace_create_file("trace_pipe_raw", 0444, d_cpu,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005149 (void *)&data->trace_cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04005150
5151 trace_create_file("stats", 0444, d_cpu,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005152 (void *)&data->trace_cpu, &tracing_stats_fops);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005153
5154 trace_create_file("buffer_size_kb", 0444, d_cpu,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005155 (void *)&data->trace_cpu, &tracing_entries_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005156
5157#ifdef CONFIG_TRACER_SNAPSHOT
5158 trace_create_file("snapshot", 0644, d_cpu,
5159 (void *)&data->trace_cpu, &snapshot_fops);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005160
5161 trace_create_file("snapshot_raw", 0444, d_cpu,
5162 (void *)&data->trace_cpu, &snapshot_raw_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005163#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005164}
5165
Steven Rostedt60a11772008-05-12 21:20:44 +02005166#ifdef CONFIG_FTRACE_SELFTEST
5167/* Let selftest have access to static functions in this file */
5168#include "trace_selftest.c"
5169#endif
5170
Steven Rostedt577b7852009-02-26 23:43:05 -05005171struct trace_option_dentry {
5172 struct tracer_opt *opt;
5173 struct tracer_flags *flags;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005174 struct trace_array *tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05005175 struct dentry *entry;
5176};
5177
5178static ssize_t
5179trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5180 loff_t *ppos)
5181{
5182 struct trace_option_dentry *topt = filp->private_data;
5183 char *buf;
5184
5185 if (topt->flags->val & topt->opt->bit)
5186 buf = "1\n";
5187 else
5188 buf = "0\n";
5189
5190 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5191}
5192
5193static ssize_t
5194trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5195 loff_t *ppos)
5196{
5197 struct trace_option_dentry *topt = filp->private_data;
5198 unsigned long val;
Steven Rostedt577b7852009-02-26 23:43:05 -05005199 int ret;
5200
Peter Huewe22fe9b52011-06-07 21:58:27 +02005201 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5202 if (ret)
Steven Rostedt577b7852009-02-26 23:43:05 -05005203 return ret;
5204
Li Zefan8d18eaa2009-12-08 11:17:06 +08005205 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05005206 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08005207
5208 if (!!(topt->flags->val & topt->opt->bit) != val) {
5209 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005210 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05005211 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08005212 mutex_unlock(&trace_types_lock);
5213 if (ret)
5214 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05005215 }
5216
5217 *ppos += cnt;
5218
5219 return cnt;
5220}
5221
5222
5223static const struct file_operations trace_options_fops = {
5224 .open = tracing_open_generic,
5225 .read = trace_options_read,
5226 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005227 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05005228};
5229
Steven Rostedta8259072009-02-26 22:19:12 -05005230static ssize_t
5231trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5232 loff_t *ppos)
5233{
5234 long index = (long)filp->private_data;
5235 char *buf;
5236
5237 if (trace_flags & (1 << index))
5238 buf = "1\n";
5239 else
5240 buf = "0\n";
5241
5242 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5243}
5244
5245static ssize_t
5246trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5247 loff_t *ppos)
5248{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005249 struct trace_array *tr = &global_trace;
Steven Rostedta8259072009-02-26 22:19:12 -05005250 long index = (long)filp->private_data;
Steven Rostedta8259072009-02-26 22:19:12 -05005251 unsigned long val;
5252 int ret;
5253
Peter Huewe22fe9b52011-06-07 21:58:27 +02005254 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5255 if (ret)
Steven Rostedta8259072009-02-26 22:19:12 -05005256 return ret;
5257
Zhaoleif2d84b62009-08-07 18:55:48 +08005258 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05005259 return -EINVAL;
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04005260
5261 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005262 ret = set_tracer_flag(tr, 1 << index, val);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04005263 mutex_unlock(&trace_types_lock);
Steven Rostedta8259072009-02-26 22:19:12 -05005264
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005265 if (ret < 0)
5266 return ret;
5267
Steven Rostedta8259072009-02-26 22:19:12 -05005268 *ppos += cnt;
5269
5270 return cnt;
5271}
5272
Steven Rostedta8259072009-02-26 22:19:12 -05005273static const struct file_operations trace_options_core_fops = {
5274 .open = tracing_open_generic,
5275 .read = trace_options_core_read,
5276 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005277 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05005278};
5279
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005280struct dentry *trace_create_file(const char *name,
Al Virof4ae40a62011-07-24 04:33:43 -04005281 umode_t mode,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005282 struct dentry *parent,
5283 void *data,
5284 const struct file_operations *fops)
5285{
5286 struct dentry *ret;
5287
5288 ret = debugfs_create_file(name, mode, parent, data, fops);
5289 if (!ret)
5290 pr_warning("Could not create debugfs '%s' entry\n", name);
5291
5292 return ret;
5293}
5294
5295
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005296static struct dentry *trace_options_init_dentry(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05005297{
5298 struct dentry *d_tracer;
Steven Rostedta8259072009-02-26 22:19:12 -05005299
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005300 if (tr->options)
5301 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05005302
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005303 d_tracer = tracing_init_dentry_tr(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05005304 if (!d_tracer)
5305 return NULL;
5306
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005307 tr->options = debugfs_create_dir("options", d_tracer);
5308 if (!tr->options) {
Steven Rostedta8259072009-02-26 22:19:12 -05005309 pr_warning("Could not create debugfs directory 'options'\n");
5310 return NULL;
5311 }
5312
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005313 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05005314}
5315
Steven Rostedt577b7852009-02-26 23:43:05 -05005316static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005317create_trace_option_file(struct trace_array *tr,
5318 struct trace_option_dentry *topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05005319 struct tracer_flags *flags,
5320 struct tracer_opt *opt)
5321{
5322 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05005323
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005324 t_options = trace_options_init_dentry(tr);
Steven Rostedt577b7852009-02-26 23:43:05 -05005325 if (!t_options)
5326 return;
5327
5328 topt->flags = flags;
5329 topt->opt = opt;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005330 topt->tr = tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05005331
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005332 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05005333 &trace_options_fops);
5334
Steven Rostedt577b7852009-02-26 23:43:05 -05005335}
5336
5337static struct trace_option_dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005338create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
Steven Rostedt577b7852009-02-26 23:43:05 -05005339{
5340 struct trace_option_dentry *topts;
5341 struct tracer_flags *flags;
5342 struct tracer_opt *opts;
5343 int cnt;
5344
5345 if (!tracer)
5346 return NULL;
5347
5348 flags = tracer->flags;
5349
5350 if (!flags || !flags->opts)
5351 return NULL;
5352
5353 opts = flags->opts;
5354
5355 for (cnt = 0; opts[cnt].name; cnt++)
5356 ;
5357
Steven Rostedt0cfe8242009-02-27 10:51:10 -05005358 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05005359 if (!topts)
5360 return NULL;
5361
5362 for (cnt = 0; opts[cnt].name; cnt++)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005363 create_trace_option_file(tr, &topts[cnt], flags,
Steven Rostedt577b7852009-02-26 23:43:05 -05005364 &opts[cnt]);
5365
5366 return topts;
5367}
5368
5369static void
5370destroy_trace_option_files(struct trace_option_dentry *topts)
5371{
5372 int cnt;
5373
5374 if (!topts)
5375 return;
5376
5377 for (cnt = 0; topts[cnt].opt; cnt++) {
5378 if (topts[cnt].entry)
5379 debugfs_remove(topts[cnt].entry);
5380 }
5381
5382 kfree(topts);
5383}
5384
Steven Rostedta8259072009-02-26 22:19:12 -05005385static struct dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005386create_trace_option_core_file(struct trace_array *tr,
5387 const char *option, long index)
Steven Rostedta8259072009-02-26 22:19:12 -05005388{
5389 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05005390
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005391 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05005392 if (!t_options)
5393 return NULL;
5394
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005395 return trace_create_file(option, 0644, t_options, (void *)index,
Steven Rostedta8259072009-02-26 22:19:12 -05005396 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05005397}
5398
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005399static __init void create_trace_options_dir(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05005400{
5401 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05005402 int i;
5403
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005404 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05005405 if (!t_options)
5406 return;
5407
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005408 for (i = 0; trace_options[i]; i++)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005409 create_trace_option_core_file(tr, trace_options[i], i);
Steven Rostedta8259072009-02-26 22:19:12 -05005410}
5411
Steven Rostedt499e5472012-02-22 15:50:28 -05005412static ssize_t
5413rb_simple_read(struct file *filp, char __user *ubuf,
5414 size_t cnt, loff_t *ppos)
5415{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04005416 struct trace_array *tr = filp->private_data;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005417 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt499e5472012-02-22 15:50:28 -05005418 char buf[64];
5419 int r;
5420
5421 if (buffer)
5422 r = ring_buffer_record_is_on(buffer);
5423 else
5424 r = 0;
5425
5426 r = sprintf(buf, "%d\n", r);
5427
5428 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5429}
5430
5431static ssize_t
5432rb_simple_write(struct file *filp, const char __user *ubuf,
5433 size_t cnt, loff_t *ppos)
5434{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04005435 struct trace_array *tr = filp->private_data;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005436 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt499e5472012-02-22 15:50:28 -05005437 unsigned long val;
5438 int ret;
5439
5440 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5441 if (ret)
5442 return ret;
5443
5444 if (buffer) {
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05005445 mutex_lock(&trace_types_lock);
5446 if (val) {
Steven Rostedt499e5472012-02-22 15:50:28 -05005447 ring_buffer_record_on(buffer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005448 if (tr->current_trace->start)
5449 tr->current_trace->start(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05005450 } else {
Steven Rostedt499e5472012-02-22 15:50:28 -05005451 ring_buffer_record_off(buffer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005452 if (tr->current_trace->stop)
5453 tr->current_trace->stop(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05005454 }
5455 mutex_unlock(&trace_types_lock);
Steven Rostedt499e5472012-02-22 15:50:28 -05005456 }
5457
5458 (*ppos)++;
5459
5460 return cnt;
5461}
5462
5463static const struct file_operations rb_simple_fops = {
5464 .open = tracing_open_generic,
5465 .read = rb_simple_read,
5466 .write = rb_simple_write,
5467 .llseek = default_llseek,
5468};
5469
Steven Rostedt277ba042012-08-03 16:10:49 -04005470struct dentry *trace_instance_dir;
5471
5472static void
5473init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5474
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005475static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5476{
5477 int cpu;
5478
5479 for_each_tracing_cpu(cpu) {
5480 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5481 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5482 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5483 }
5484}
5485
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005486static int
5487allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
Steven Rostedt277ba042012-08-03 16:10:49 -04005488{
5489 enum ring_buffer_flags rb_flags;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005490
5491 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5492
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005493 buf->buffer = ring_buffer_alloc(size, rb_flags);
5494 if (!buf->buffer)
5495 return -ENOMEM;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005496
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005497 buf->data = alloc_percpu(struct trace_array_cpu);
5498 if (!buf->data) {
5499 ring_buffer_free(buf->buffer);
5500 return -ENOMEM;
5501 }
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005502
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005503 init_trace_buffers(tr, buf);
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005504
5505 /* Allocate the first page for all buffers */
5506 set_buffer_entries(&tr->trace_buffer,
5507 ring_buffer_size(tr->trace_buffer.buffer, 0));
5508
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005509 return 0;
5510}
5511
5512static int allocate_trace_buffers(struct trace_array *tr, int size)
5513{
5514 int ret;
5515
5516 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5517 if (ret)
5518 return ret;
5519
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005520#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005521 ret = allocate_trace_buffer(tr, &tr->max_buffer,
5522 allocate_snapshot ? size : 1);
5523 if (WARN_ON(ret)) {
5524 ring_buffer_free(tr->trace_buffer.buffer);
5525 free_percpu(tr->trace_buffer.data);
5526 return -ENOMEM;
5527 }
5528 tr->allocated_snapshot = allocate_snapshot;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005529
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005530 /*
5531 * Only the top level trace array gets its snapshot allocated
5532 * from the kernel command line.
5533 */
5534 allocate_snapshot = false;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005535#endif
5536 return 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005537}
5538
5539static int new_instance_create(const char *name)
5540{
Steven Rostedt277ba042012-08-03 16:10:49 -04005541 struct trace_array *tr;
5542 int ret;
Steven Rostedt277ba042012-08-03 16:10:49 -04005543
5544 mutex_lock(&trace_types_lock);
5545
5546 ret = -EEXIST;
5547 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5548 if (tr->name && strcmp(tr->name, name) == 0)
5549 goto out_unlock;
5550 }
5551
5552 ret = -ENOMEM;
5553 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5554 if (!tr)
5555 goto out_unlock;
5556
5557 tr->name = kstrdup(name, GFP_KERNEL);
5558 if (!tr->name)
5559 goto out_free_tr;
5560
5561 raw_spin_lock_init(&tr->start_lock);
5562
5563 tr->current_trace = &nop_trace;
5564
5565 INIT_LIST_HEAD(&tr->systems);
5566 INIT_LIST_HEAD(&tr->events);
5567
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005568 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
Steven Rostedt277ba042012-08-03 16:10:49 -04005569 goto out_free_tr;
5570
Steven Rostedt277ba042012-08-03 16:10:49 -04005571 /* Holder for file callbacks */
5572 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5573 tr->trace_cpu.tr = tr;
5574
5575 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5576 if (!tr->dir)
5577 goto out_free_tr;
5578
5579 ret = event_trace_add_tracer(tr->dir, tr);
5580 if (ret)
5581 goto out_free_tr;
5582
5583 init_tracer_debugfs(tr, tr->dir);
5584
5585 list_add(&tr->list, &ftrace_trace_arrays);
5586
5587 mutex_unlock(&trace_types_lock);
5588
5589 return 0;
5590
5591 out_free_tr:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005592 if (tr->trace_buffer.buffer)
5593 ring_buffer_free(tr->trace_buffer.buffer);
Steven Rostedt277ba042012-08-03 16:10:49 -04005594 kfree(tr->name);
5595 kfree(tr);
5596
5597 out_unlock:
5598 mutex_unlock(&trace_types_lock);
5599
5600 return ret;
5601
5602}
5603
Steven Rostedt0c8916c2012-08-07 16:14:16 -04005604static int instance_delete(const char *name)
5605{
5606 struct trace_array *tr;
5607 int found = 0;
5608 int ret;
5609
5610 mutex_lock(&trace_types_lock);
5611
5612 ret = -ENODEV;
5613 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5614 if (tr->name && strcmp(tr->name, name) == 0) {
5615 found = 1;
5616 break;
5617 }
5618 }
5619 if (!found)
5620 goto out_unlock;
5621
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05005622 ret = -EBUSY;
5623 if (tr->ref)
5624 goto out_unlock;
5625
Steven Rostedt0c8916c2012-08-07 16:14:16 -04005626 list_del(&tr->list);
5627
5628 event_trace_del_tracer(tr);
5629 debugfs_remove_recursive(tr->dir);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005630 free_percpu(tr->trace_buffer.data);
5631 ring_buffer_free(tr->trace_buffer.buffer);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04005632
5633 kfree(tr->name);
5634 kfree(tr);
5635
5636 ret = 0;
5637
5638 out_unlock:
5639 mutex_unlock(&trace_types_lock);
5640
5641 return ret;
5642}
5643
Steven Rostedt277ba042012-08-03 16:10:49 -04005644static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
5645{
5646 struct dentry *parent;
5647 int ret;
5648
5649 /* Paranoid: Make sure the parent is the "instances" directory */
5650 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5651 if (WARN_ON_ONCE(parent != trace_instance_dir))
5652 return -ENOENT;
5653
5654 /*
5655 * The inode mutex is locked, but debugfs_create_dir() will also
5656 * take the mutex. As the instances directory can not be destroyed
5657 * or changed in any other way, it is safe to unlock it, and
5658 * let the dentry try. If two users try to make the same dir at
5659 * the same time, then the new_instance_create() will determine the
5660 * winner.
5661 */
5662 mutex_unlock(&inode->i_mutex);
5663
5664 ret = new_instance_create(dentry->d_iname);
5665
5666 mutex_lock(&inode->i_mutex);
5667
5668 return ret;
5669}
5670
Steven Rostedt0c8916c2012-08-07 16:14:16 -04005671static int instance_rmdir(struct inode *inode, struct dentry *dentry)
5672{
5673 struct dentry *parent;
5674 int ret;
5675
5676 /* Paranoid: Make sure the parent is the "instances" directory */
5677 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5678 if (WARN_ON_ONCE(parent != trace_instance_dir))
5679 return -ENOENT;
5680
5681 /* The caller did a dget() on dentry */
5682 mutex_unlock(&dentry->d_inode->i_mutex);
5683
5684 /*
5685 * The inode mutex is locked, but debugfs_create_dir() will also
5686 * take the mutex. As the instances directory can not be destroyed
5687 * or changed in any other way, it is safe to unlock it, and
5688 * let the dentry try. If two users try to make the same dir at
5689 * the same time, then the instance_delete() will determine the
5690 * winner.
5691 */
5692 mutex_unlock(&inode->i_mutex);
5693
5694 ret = instance_delete(dentry->d_iname);
5695
5696 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
5697 mutex_lock(&dentry->d_inode->i_mutex);
5698
5699 return ret;
5700}
5701
Steven Rostedt277ba042012-08-03 16:10:49 -04005702static const struct inode_operations instance_dir_inode_operations = {
5703 .lookup = simple_lookup,
5704 .mkdir = instance_mkdir,
Steven Rostedt0c8916c2012-08-07 16:14:16 -04005705 .rmdir = instance_rmdir,
Steven Rostedt277ba042012-08-03 16:10:49 -04005706};
5707
5708static __init void create_trace_instances(struct dentry *d_tracer)
5709{
5710 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
5711 if (WARN_ON(!trace_instance_dir))
5712 return;
5713
5714 /* Hijack the dir inode operations, to allow mkdir */
5715 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
5716}
5717
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005718static void
5719init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
5720{
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05005721 int cpu;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005722
5723 trace_create_file("trace_options", 0644, d_tracer,
5724 tr, &tracing_iter_fops);
5725
5726 trace_create_file("trace", 0644, d_tracer,
5727 (void *)&tr->trace_cpu, &tracing_fops);
5728
5729 trace_create_file("trace_pipe", 0444, d_tracer,
5730 (void *)&tr->trace_cpu, &tracing_pipe_fops);
5731
5732 trace_create_file("buffer_size_kb", 0644, d_tracer,
5733 (void *)&tr->trace_cpu, &tracing_entries_fops);
5734
5735 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5736 tr, &tracing_total_entries_fops);
5737
5738 trace_create_file("free_buffer", 0644, d_tracer,
5739 tr, &tracing_free_buffer_fops);
5740
5741 trace_create_file("trace_marker", 0220, d_tracer,
5742 tr, &tracing_mark_fops);
5743
5744 trace_create_file("trace_clock", 0644, d_tracer, tr,
5745 &trace_clock_fops);
5746
5747 trace_create_file("tracing_on", 0644, d_tracer,
5748 tr, &rb_simple_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05005749
5750#ifdef CONFIG_TRACER_SNAPSHOT
5751 trace_create_file("snapshot", 0644, d_tracer,
5752 (void *)&tr->trace_cpu, &snapshot_fops);
5753#endif
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05005754
5755 for_each_tracing_cpu(cpu)
5756 tracing_init_debugfs_percpu(tr, cpu);
5757
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005758}
5759
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01005760static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005761{
5762 struct dentry *d_tracer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005763
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005764 trace_access_lock_init();
5765
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005766 d_tracer = tracing_init_dentry();
5767
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005768 init_tracer_debugfs(&global_trace, d_tracer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005769
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005770 trace_create_file("tracing_cpumask", 0644, d_tracer,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005771 &global_trace, &tracing_cpumask_fops);
Ingo Molnarc7078de2008-05-12 21:20:52 +02005772
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005773 trace_create_file("available_tracers", 0444, d_tracer,
5774 &global_trace, &show_traces_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005775
Li Zefan339ae5d2009-04-17 10:34:30 +08005776 trace_create_file("current_tracer", 0644, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005777 &global_trace, &set_tracer_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005778
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04005779#ifdef CONFIG_TRACER_MAX_TRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005780 trace_create_file("tracing_max_latency", 0644, d_tracer,
5781 &tracing_max_latency, &tracing_max_lat_fops);
Tim Bird0e950172010-02-25 15:36:43 -08005782#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005783
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005784 trace_create_file("tracing_thresh", 0644, d_tracer,
5785 &tracing_thresh, &tracing_max_lat_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005786
Li Zefan339ae5d2009-04-17 10:34:30 +08005787 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005788 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02005789
Avadh Patel69abe6a2009-04-10 16:04:48 -04005790 trace_create_file("saved_cmdlines", 0444, d_tracer,
5791 NULL, &tracing_saved_cmdlines_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005792
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005793#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005794 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5795 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005796#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005797
Steven Rostedt277ba042012-08-03 16:10:49 -04005798 create_trace_instances(d_tracer);
5799
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005800 create_trace_options_dir(&global_trace);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005801
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01005802 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005803}
5804
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005805static int trace_panic_handler(struct notifier_block *this,
5806 unsigned long event, void *unused)
5807{
Steven Rostedt944ac422008-10-23 19:26:08 -04005808 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005809 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005810 return NOTIFY_OK;
5811}
5812
5813static struct notifier_block trace_panic_notifier = {
5814 .notifier_call = trace_panic_handler,
5815 .next = NULL,
5816 .priority = 150 /* priority: INT_MAX >= x >= 0 */
5817};
5818
5819static int trace_die_handler(struct notifier_block *self,
5820 unsigned long val,
5821 void *data)
5822{
5823 switch (val) {
5824 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04005825 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005826 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005827 break;
5828 default:
5829 break;
5830 }
5831 return NOTIFY_OK;
5832}
5833
5834static struct notifier_block trace_die_notifier = {
5835 .notifier_call = trace_die_handler,
5836 .priority = 200
5837};
5838
5839/*
5840 * printk is set to max of 1024, we really don't need it that big.
5841 * Nothing should be printing 1000 characters anyway.
5842 */
5843#define TRACE_MAX_PRINT 1000
5844
5845/*
5846 * Define here KERN_TRACE so that we have one place to modify
5847 * it if we decide to change what log level the ftrace dump
5848 * should be at.
5849 */
Steven Rostedt428aee12009-01-14 12:24:42 -05005850#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005851
Jason Wessel955b61e2010-08-05 09:22:23 -05005852void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005853trace_printk_seq(struct trace_seq *s)
5854{
5855 /* Probably should print a warning here. */
5856 if (s->len >= 1000)
5857 s->len = 1000;
5858
5859 /* should be zero ended, but we are paranoid. */
5860 s->buffer[s->len] = 0;
5861
5862 printk(KERN_TRACE "%s", s->buffer);
5863
Steven Rostedtf9520752009-03-02 14:04:40 -05005864 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005865}
5866
Jason Wessel955b61e2010-08-05 09:22:23 -05005867void trace_init_global_iter(struct trace_iterator *iter)
5868{
5869 iter->tr = &global_trace;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005870 iter->trace = iter->tr->current_trace;
Steven Rostedtae3b5092013-01-23 15:22:59 -05005871 iter->cpu_file = RING_BUFFER_ALL_CPUS;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005872 iter->trace_buffer = &global_trace.trace_buffer;
Jason Wessel955b61e2010-08-05 09:22:23 -05005873}
5874
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005875static void
5876__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005877{
Thomas Gleixner445c8952009-12-02 19:49:50 +01005878 static arch_spinlock_t ftrace_dump_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01005879 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005880 /* use static because iter can be a bit big for the stack */
5881 static struct trace_iterator iter;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005882 unsigned int old_userobj;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005883 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04005884 unsigned long flags;
5885 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005886
5887 /* only one dump */
Steven Rostedtcd891ae2009-04-28 11:39:34 -04005888 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01005889 arch_spin_lock(&ftrace_dump_lock);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005890 if (dump_ran)
5891 goto out;
5892
5893 dump_ran = 1;
5894
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05005895 tracing_off();
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005896
Steven Rostedte0a413f2011-09-29 21:26:16 -04005897 /* Did function tracer already get disabled? */
5898 if (ftrace_is_dead()) {
5899 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5900 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5901 }
5902
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005903 if (disable_tracing)
5904 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005905
Jovi Zhang38dbe0b2013-01-25 18:03:07 +08005906 /* Simulate the iterator */
Jason Wessel955b61e2010-08-05 09:22:23 -05005907 trace_init_global_iter(&iter);
5908
Steven Rostedtd7690412008-10-01 00:29:53 -04005909 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005910 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04005911 }
5912
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005913 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5914
Török Edwinb54d3de2008-11-22 13:28:48 +02005915 /* don't look at user memory in panic mode */
5916 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5917
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005918 switch (oops_dump_mode) {
5919 case DUMP_ALL:
Steven Rostedtae3b5092013-01-23 15:22:59 -05005920 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005921 break;
5922 case DUMP_ORIG:
5923 iter.cpu_file = raw_smp_processor_id();
5924 break;
5925 case DUMP_NONE:
5926 goto out_enable;
5927 default:
5928 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
Steven Rostedtae3b5092013-01-23 15:22:59 -05005929 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005930 }
5931
5932 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005933
5934 /*
5935 * We need to stop all tracing on all CPUS to read the
5936 * the next buffer. This is a bit expensive, but is
5937 * not done often. We fill all what we can read,
5938 * and then release the locks again.
5939 */
5940
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005941 while (!trace_empty(&iter)) {
5942
5943 if (!cnt)
5944 printk(KERN_TRACE "---------------------------------\n");
5945
5946 cnt++;
5947
5948 /* reset all but tr, trace, and overruns */
5949 memset(&iter.seq, 0,
5950 sizeof(struct trace_iterator) -
5951 offsetof(struct trace_iterator, seq));
5952 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5953 iter.pos = -1;
5954
Jason Wessel955b61e2010-08-05 09:22:23 -05005955 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08005956 int ret;
5957
5958 ret = print_trace_line(&iter);
5959 if (ret != TRACE_TYPE_NO_CONSUME)
5960 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005961 }
Steven Rostedtb892e5c2012-03-01 22:06:48 -05005962 touch_nmi_watchdog();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005963
5964 trace_printk_seq(&iter.seq);
5965 }
5966
5967 if (!cnt)
5968 printk(KERN_TRACE " (ftrace buffer empty)\n");
5969 else
5970 printk(KERN_TRACE "---------------------------------\n");
5971
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005972 out_enable:
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005973 /* Re-enable tracing if requested */
5974 if (!disable_tracing) {
5975 trace_flags |= old_userobj;
5976
5977 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005978 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005979 }
5980 tracing_on();
5981 }
5982
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005983 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01005984 arch_spin_unlock(&ftrace_dump_lock);
Steven Rostedtcd891ae2009-04-28 11:39:34 -04005985 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005986}
5987
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005988/* By default: disable tracing after the dump */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005989void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005990{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005991 __ftrace_dump(true, oops_dump_mode);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005992}
Paul E. McKenneya8eecf22011-10-02 11:01:15 -07005993EXPORT_SYMBOL_GPL(ftrace_dump);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005994
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005995__init static int tracer_alloc_buffers(void)
5996{
Steven Rostedt73c51622009-03-11 13:42:01 -04005997 int ring_buf_size;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305998 int ret = -ENOMEM;
5999
David Sharp750912f2010-12-08 13:46:47 -08006000
Rusty Russell9e01c1b2009-01-01 10:12:22 +10306001 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6002 goto out;
6003
6004 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
6005 goto out_free_buffer_mask;
6006
Steven Rostedt07d777f2011-09-22 14:01:55 -04006007 /* Only allocate trace_printk buffers if a trace_printk exists */
6008 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
Steven Rostedt81698832012-10-11 10:15:05 -04006009 /* Must be called before global_trace.buffer is allocated */
Steven Rostedt07d777f2011-09-22 14:01:55 -04006010 trace_printk_init_buffers();
6011
Steven Rostedt73c51622009-03-11 13:42:01 -04006012 /* To save memory, keep the ring buffer size to its minimum */
6013 if (ring_buffer_expanded)
6014 ring_buf_size = trace_buf_size;
6015 else
6016 ring_buf_size = 1;
6017
Rusty Russell9e01c1b2009-01-01 10:12:22 +10306018 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6019 cpumask_copy(tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006020
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006021 raw_spin_lock_init(&global_trace.start_lock);
6022
Steven Rostedtab464282008-05-12 21:21:00 +02006023 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006024 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04006025 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6026 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10306027 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04006028 }
Steven Rostedta7603ff2012-08-06 16:24:11 -04006029
Steven Rostedt499e5472012-02-22 15:50:28 -05006030 if (global_trace.buffer_disabled)
6031 tracing_off();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04006032
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006033 trace_init_cmdlines();
6034
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02006035 register_tracer(&nop_trace);
Steven Rostedt (Red Hat)d840f712013-02-01 18:38:47 -05006036
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006037 global_trace.current_trace = &nop_trace;
6038
Steven Rostedt60a11772008-05-12 21:20:44 +02006039 /* All seems OK, enable tracing */
6040 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04006041
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006042 atomic_notifier_chain_register(&panic_notifier_list,
6043 &trace_panic_notifier);
6044
6045 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01006046
Steven Rostedtae63b31e2012-05-03 23:09:03 -04006047 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6048
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006049 /* Holder for file callbacks */
6050 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6051 global_trace.trace_cpu.tr = &global_trace;
6052
Steven Rostedtae63b31e2012-05-03 23:09:03 -04006053 INIT_LIST_HEAD(&global_trace.systems);
6054 INIT_LIST_HEAD(&global_trace.events);
6055 list_add(&global_trace.list, &ftrace_trace_arrays);
6056
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04006057 while (trace_boot_options) {
6058 char *option;
6059
6060 option = strsep(&trace_boot_options, ",");
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006061 trace_set_options(&global_trace, option);
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04006062 }
6063
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01006064 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006065
Rusty Russell9e01c1b2009-01-01 10:12:22 +10306066out_free_cpumask:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006067 free_percpu(global_trace.trace_buffer.data);
6068#ifdef CONFIG_TRACER_MAX_TRACE
6069 free_percpu(global_trace.max_buffer.data);
6070#endif
Rusty Russell9e01c1b2009-01-01 10:12:22 +10306071 free_cpumask_var(tracing_cpumask);
6072out_free_buffer_mask:
6073 free_cpumask_var(tracing_buffer_mask);
6074out:
6075 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006076}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05006077
6078__init static int clear_boot_tracer(void)
6079{
6080 /*
6081 * The default tracer at boot buffer is an init section.
6082 * This function is called in lateinit. If we did not
6083 * find the boot tracer, then clear it out, to prevent
6084 * later registration from accessing the buffer that is
6085 * about to be freed.
6086 */
6087 if (!default_bootup_tracer)
6088 return 0;
6089
6090 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6091 default_bootup_tracer);
6092 default_bootup_tracer = NULL;
6093
6094 return 0;
6095}
6096
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01006097early_initcall(tracer_alloc_buffers);
6098fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05006099late_initcall(clear_boot_tracer);