blob: 50820887dce949718b8faae8fb19469d5b6a78c0 [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 Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050023#include <linux/tracefs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020024#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020025#include <linux/hardirq.h>
26#include <linux/linkage.h>
27#include <linux/uaccess.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050028#include <linux/kprobes.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020029#include <linux/ftrace.h>
30#include <linux/module.h>
31#include <linux/percpu.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050032#include <linux/splice.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040033#include <linux/kdebug.h>
Frederic Weisbecker5f0c6c02009-03-27 14:22:10 +010034#include <linux/string.h>
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -050035#include <linux/mount.h>
Lai Jiangshan7e53bd42010-01-06 20:08:50 +080036#include <linux/rwsem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020038#include <linux/ctype.h>
39#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020040#include <linux/poll.h>
Steven Rostedtb892e5c2012-03-01 22:06:48 -050041#include <linux/nmi.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020042#include <linux/fs.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060043#include <linux/sched/rt.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020044
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020045#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050046#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020047
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010048/*
Steven Rostedt73c51622009-03-11 13:42:01 -040049 * On boot up, the ring buffer is set to the minimum size, so that
50 * we do not waste memory on systems that are not using tracing.
51 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -050052bool ring_buffer_expanded;
Steven Rostedt73c51622009-03-11 13:42:01 -040053
54/*
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010055 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010056 * A selftest will lurk into the ring-buffer to count the
57 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010058 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010059 * at the same time, giving false positive or negative results.
60 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010061static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010062
Steven Rostedtb2821ae2009-02-02 21:38:32 -050063/*
64 * If a tracer is running, we do not want to run SELFTEST.
65 */
Li Zefan020e5f82009-07-01 10:47:05 +080066bool __read_mostly tracing_selftest_disabled;
Steven Rostedtb2821ae2009-02-02 21:38:32 -050067
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -050068/* Pipe tracepoints to printk */
69struct trace_iterator *tracepoint_print_iter;
70int tracepoint_printk;
71
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010072/* For tracers that don't implement custom flags */
73static struct tracer_opt dummy_tracer_opt[] = {
74 { }
75};
76
77static struct tracer_flags dummy_tracer_flags = {
78 .val = 0,
79 .opts = dummy_tracer_opt
80};
81
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -050082static int
83dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010084{
85 return 0;
86}
Steven Rostedt0f048702008-11-05 16:05:44 -050087
88/*
Steven Rostedt7ffbd482012-10-11 12:14:25 -040089 * To prevent the comm cache from being overwritten when no
90 * tracing is active, only save the comm when a trace event
91 * occurred.
92 */
93static DEFINE_PER_CPU(bool, trace_cmdline_save);
94
95/*
Steven Rostedt0f048702008-11-05 16:05:44 -050096 * Kill all tracing for good (never come back).
97 * It is initialized to 1 but will turn to zero if the initialization
98 * of the tracer is successful. But that is the only place that sets
99 * this back to zero.
100 */
Hannes Eder4fd27352009-02-10 19:44:12 +0100101static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -0500102
Christoph Lameter9288f992009-10-07 19:17:45 -0400103DEFINE_PER_CPU(int, ftrace_cpu_disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -0400104
Jason Wessel955b61e2010-08-05 09:22:23 -0500105cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +0200106
Steven Rostedt944ac422008-10-23 19:26:08 -0400107/*
108 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
109 *
110 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
111 * is set, then ftrace_dump is called. This will output the contents
112 * of the ftrace buffers to the console. This is very useful for
113 * capturing traces that lead to crashes and outputing it to a
114 * serial console.
115 *
116 * It is default off, but you can enable it with either specifying
117 * "ftrace_dump_on_oops" in the kernel command line, or setting
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200118 * /proc/sys/kernel/ftrace_dump_on_oops
119 * Set 1 if you want to dump buffers of all CPUs
120 * Set 2 if you want to dump the buffer of the CPU that triggered oops
Steven Rostedt944ac422008-10-23 19:26:08 -0400121 */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200122
123enum ftrace_dump_mode ftrace_dump_on_oops;
Steven Rostedt944ac422008-10-23 19:26:08 -0400124
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400125/* When set, tracing will stop when a WARN*() is hit */
126int __disable_trace_on_warning;
127
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400128#ifdef CONFIG_TRACE_ENUM_MAP_FILE
129/* Map of enums to their values, for "enum_map" file */
130struct trace_enum_map_head {
131 struct module *mod;
132 unsigned long length;
133};
134
135union trace_enum_map_item;
136
137struct trace_enum_map_tail {
138 /*
139 * "end" is first and points to NULL as it must be different
140 * than "mod" or "enum_string"
141 */
142 union trace_enum_map_item *next;
143 const char *end; /* points to NULL */
144};
145
146static DEFINE_MUTEX(trace_enum_mutex);
147
148/*
149 * The trace_enum_maps are saved in an array with two extra elements,
150 * one at the beginning, and one at the end. The beginning item contains
151 * the count of the saved maps (head.length), and the module they
152 * belong to if not built in (head.mod). The ending item contains a
153 * pointer to the next array of saved enum_map items.
154 */
155union trace_enum_map_item {
156 struct trace_enum_map map;
157 struct trace_enum_map_head head;
158 struct trace_enum_map_tail tail;
159};
160
161static union trace_enum_map_item *trace_enum_maps;
162#endif /* CONFIG_TRACE_ENUM_MAP_FILE */
163
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -0500164static int tracing_set_tracer(struct trace_array *tr, const char *buf);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500165
Li Zefanee6c2c12009-09-18 14:06:47 +0800166#define MAX_TRACER_SIZE 100
167static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500168static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100169
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500170static bool allocate_snapshot;
171
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200172static int __init set_cmdline_ftrace(char *str)
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100173{
Chen Gang67012ab2013-04-08 12:06:44 +0800174 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500175 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400176 /* We are using ftrace early, expand it */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500177 ring_buffer_expanded = true;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100178 return 1;
179}
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200180__setup("ftrace=", set_cmdline_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100181
Steven Rostedt944ac422008-10-23 19:26:08 -0400182static int __init set_ftrace_dump_on_oops(char *str)
183{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200184 if (*str++ != '=' || !*str) {
185 ftrace_dump_on_oops = DUMP_ALL;
186 return 1;
187 }
188
189 if (!strcmp("orig_cpu", str)) {
190 ftrace_dump_on_oops = DUMP_ORIG;
191 return 1;
192 }
193
194 return 0;
Steven Rostedt944ac422008-10-23 19:26:08 -0400195}
196__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200197
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400198static int __init stop_trace_on_warning(char *str)
199{
Luis Claudio R. Goncalves933ff9f2014-11-12 21:14:00 -0200200 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
201 __disable_trace_on_warning = 1;
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400202 return 1;
203}
Luis Claudio R. Goncalves933ff9f2014-11-12 21:14:00 -0200204__setup("traceoff_on_warning", stop_trace_on_warning);
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400205
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400206static int __init boot_alloc_snapshot(char *str)
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500207{
208 allocate_snapshot = true;
209 /* We also need the main ring buffer expanded */
210 ring_buffer_expanded = true;
211 return 1;
212}
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400213__setup("alloc_snapshot", boot_alloc_snapshot);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500214
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400215
216static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
217static char *trace_boot_options __initdata;
218
219static int __init set_trace_boot_options(char *str)
220{
Chen Gang67012ab2013-04-08 12:06:44 +0800221 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400222 trace_boot_options = trace_boot_options_buf;
223 return 0;
224}
225__setup("trace_options=", set_trace_boot_options);
226
Steven Rostedte1e232c2014-02-10 23:38:46 -0500227static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
228static char *trace_boot_clock __initdata;
229
230static int __init set_trace_boot_clock(char *str)
231{
232 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
233 trace_boot_clock = trace_boot_clock_buf;
234 return 0;
235}
236__setup("trace_clock=", set_trace_boot_clock);
237
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500238static int __init set_tracepoint_printk(char *str)
239{
240 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
241 tracepoint_printk = 1;
242 return 1;
243}
244__setup("tp_printk", set_tracepoint_printk);
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400245
Lai Jiangshancf8e3472009-03-30 13:48:00 +0800246unsigned long long ns2usecs(cycle_t nsec)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200247{
248 nsec += 500;
249 do_div(nsec, 1000);
250 return nsec;
251}
252
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200253/*
254 * The global_trace is the descriptor that holds the tracing
255 * buffers for the live tracing. For each CPU, it contains
256 * a link list of pages that will store trace entries. The
257 * page descriptor of the pages in the memory is used to hold
258 * the link list by linking the lru item in the page descriptor
259 * to each of the pages in the buffer per CPU.
260 *
261 * For each active CPU there is a data field that holds the
262 * pages for the buffer for that CPU. Each CPU has the same number
263 * of pages allocated for its buffer.
264 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200265static struct trace_array global_trace;
266
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400267LIST_HEAD(ftrace_trace_arrays);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200268
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -0400269int trace_array_get(struct trace_array *this_tr)
270{
271 struct trace_array *tr;
272 int ret = -ENODEV;
273
274 mutex_lock(&trace_types_lock);
275 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
276 if (tr == this_tr) {
277 tr->ref++;
278 ret = 0;
279 break;
280 }
281 }
282 mutex_unlock(&trace_types_lock);
283
284 return ret;
285}
286
287static void __trace_array_put(struct trace_array *this_tr)
288{
289 WARN_ON(!this_tr->ref);
290 this_tr->ref--;
291}
292
293void trace_array_put(struct trace_array *this_tr)
294{
295 mutex_lock(&trace_types_lock);
296 __trace_array_put(this_tr);
297 mutex_unlock(&trace_types_lock);
298}
299
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400300int filter_check_discard(struct trace_event_file *file, void *rec,
Tom Zanussif306cc82013-10-24 08:34:17 -0500301 struct ring_buffer *buffer,
302 struct ring_buffer_event *event)
Tom Zanussieb02ce02009-04-08 03:15:54 -0500303{
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400304 if (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
Tom Zanussif306cc82013-10-24 08:34:17 -0500305 !filter_match_preds(file->filter, rec)) {
306 ring_buffer_discard_commit(buffer, event);
307 return 1;
308 }
309
310 return 0;
Tom Zanussieb02ce02009-04-08 03:15:54 -0500311}
Tom Zanussif306cc82013-10-24 08:34:17 -0500312EXPORT_SYMBOL_GPL(filter_check_discard);
313
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400314int call_filter_check_discard(struct trace_event_call *call, void *rec,
Tom Zanussif306cc82013-10-24 08:34:17 -0500315 struct ring_buffer *buffer,
316 struct ring_buffer_event *event)
317{
318 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
319 !filter_match_preds(call->filter, rec)) {
320 ring_buffer_discard_commit(buffer, event);
321 return 1;
322 }
323
324 return 0;
325}
326EXPORT_SYMBOL_GPL(call_filter_check_discard);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500327
Fabian Frederickad1438a2014-04-17 21:44:42 +0200328static cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
Steven Rostedt37886f62009-03-17 17:22:06 -0400329{
330 u64 ts;
331
332 /* Early boot up does not have a buffer yet */
Alexander Z Lam94571582013-08-02 18:36:16 -0700333 if (!buf->buffer)
Steven Rostedt37886f62009-03-17 17:22:06 -0400334 return trace_clock_local();
335
Alexander Z Lam94571582013-08-02 18:36:16 -0700336 ts = ring_buffer_time_stamp(buf->buffer, cpu);
337 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
Steven Rostedt37886f62009-03-17 17:22:06 -0400338
339 return ts;
340}
341
Alexander Z Lam94571582013-08-02 18:36:16 -0700342cycle_t ftrace_now(int cpu)
343{
344 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
345}
346
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400347/**
348 * tracing_is_enabled - Show if global_trace has been disabled
349 *
350 * Shows if the global trace has been enabled or not. It uses the
351 * mirror flag "buffer_disabled" to be used in fast paths such as for
352 * the irqsoff tracer. But it may be inaccurate due to races. If you
353 * need to know the accurate state, use tracing_is_on() which is a little
354 * slower, but accurate.
355 */
Steven Rostedt90369902008-11-05 16:05:44 -0500356int tracing_is_enabled(void)
357{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400358 /*
359 * For quick access (irqsoff uses this in fast path), just
360 * return the mirror variable of the state of the ring buffer.
361 * It's a little racy, but we don't really care.
362 */
363 smp_rmb();
364 return !global_trace.buffer_disabled;
Steven Rostedt90369902008-11-05 16:05:44 -0500365}
366
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200367/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400368 * trace_buf_size is the size in bytes that is allocated
369 * for a buffer. Note, the number of bytes is always rounded
370 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400371 *
372 * This number is purposely set to a low number of 16384.
373 * If the dump on oops happens, it will be much appreciated
374 * to not have to wait for all that output. Anyway this can be
375 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200376 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400377#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400378
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400379static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200380
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200381/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200382static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200383
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200384/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200385 * trace_types_lock is used to protect the trace_types list.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200386 */
Alexander Z Lama8227412013-07-01 19:37:54 -0700387DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200388
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800389/*
390 * serialize the access of the ring buffer
391 *
392 * ring buffer serializes readers, but it is low level protection.
393 * The validity of the events (which returns by ring_buffer_peek() ..etc)
394 * are not protected by ring buffer.
395 *
396 * The content of events may become garbage if we allow other process consumes
397 * these events concurrently:
398 * A) the page of the consumed events may become a normal page
399 * (not reader page) in ring buffer, and this page will be rewrited
400 * by events producer.
401 * B) The page of the consumed events may become a page for splice_read,
402 * and this page will be returned to system.
403 *
404 * These primitives allow multi process access to different cpu ring buffer
405 * concurrently.
406 *
407 * These primitives don't distinguish read-only and read-consume access.
408 * Multi read-only access are also serialized.
409 */
410
411#ifdef CONFIG_SMP
412static DECLARE_RWSEM(all_cpu_access_lock);
413static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
414
415static inline void trace_access_lock(int cpu)
416{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500417 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800418 /* gain it for accessing the whole ring buffer. */
419 down_write(&all_cpu_access_lock);
420 } else {
421 /* gain it for accessing a cpu ring buffer. */
422
Steven Rostedtae3b5092013-01-23 15:22:59 -0500423 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800424 down_read(&all_cpu_access_lock);
425
426 /* Secondly block other access to this @cpu ring buffer. */
427 mutex_lock(&per_cpu(cpu_access_lock, cpu));
428 }
429}
430
431static inline void trace_access_unlock(int cpu)
432{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500433 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800434 up_write(&all_cpu_access_lock);
435 } else {
436 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
437 up_read(&all_cpu_access_lock);
438 }
439}
440
441static inline void trace_access_lock_init(void)
442{
443 int cpu;
444
445 for_each_possible_cpu(cpu)
446 mutex_init(&per_cpu(cpu_access_lock, cpu));
447}
448
449#else
450
451static DEFINE_MUTEX(access_lock);
452
453static inline void trace_access_lock(int cpu)
454{
455 (void)cpu;
456 mutex_lock(&access_lock);
457}
458
459static inline void trace_access_unlock(int cpu)
460{
461 (void)cpu;
462 mutex_unlock(&access_lock);
463}
464
465static inline void trace_access_lock_init(void)
466{
467}
468
469#endif
470
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400471#ifdef CONFIG_STACKTRACE
472static void __ftrace_trace_stack(struct ring_buffer *buffer,
473 unsigned long flags,
474 int skip, int pc, struct pt_regs *regs);
475#else
476static inline void __ftrace_trace_stack(struct ring_buffer *buffer,
477 unsigned long flags,
478 int skip, int pc, struct pt_regs *regs)
479{
480}
481#endif
482
Steven Rostedtee6bce52008-11-12 17:52:37 -0500483/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500484unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Steven Rostedta2a16d62009-03-24 23:17:58 -0400485 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
Steven Rostedt77271ce2011-11-17 09:34:33 -0500486 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400487 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
Vaibhav Nagarnaike7e2ee82011-05-10 13:27:21 -0700488
Steven Rostedt (Red Hat)5280bce2013-07-02 19:59:57 -0400489static void tracer_tracing_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400490{
491 if (tr->trace_buffer.buffer)
492 ring_buffer_record_on(tr->trace_buffer.buffer);
493 /*
494 * This flag is looked at when buffers haven't been allocated
495 * yet, or by some tracers (like irqsoff), that just want to
496 * know if the ring buffer has been disabled, but it can handle
497 * races of where it gets disabled but we still do a record.
498 * As the check is in the fast path of the tracers, it is more
499 * important to be fast than accurate.
500 */
501 tr->buffer_disabled = 0;
502 /* Make the flag seen by readers */
503 smp_wmb();
504}
505
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200506/**
Steven Rostedt499e5472012-02-22 15:50:28 -0500507 * tracing_on - enable tracing buffers
508 *
509 * This function enables tracing buffers that may have been
510 * disabled with tracing_off.
511 */
512void tracing_on(void)
513{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400514 tracer_tracing_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -0500515}
516EXPORT_SYMBOL_GPL(tracing_on);
517
518/**
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500519 * __trace_puts - write a constant string into the trace buffer.
520 * @ip: The address of the caller
521 * @str: The constant string to write
522 * @size: The size of the string.
523 */
524int __trace_puts(unsigned long ip, const char *str, int size)
525{
526 struct ring_buffer_event *event;
527 struct ring_buffer *buffer;
528 struct print_entry *entry;
529 unsigned long irq_flags;
530 int alloc;
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800531 int pc;
532
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800533 if (!(trace_flags & TRACE_ITER_PRINTK))
534 return 0;
535
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800536 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500537
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500538 if (unlikely(tracing_selftest_running || tracing_disabled))
539 return 0;
540
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500541 alloc = sizeof(*entry) + size + 2; /* possible \n added */
542
543 local_save_flags(irq_flags);
544 buffer = global_trace.trace_buffer.buffer;
545 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800546 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500547 if (!event)
548 return 0;
549
550 entry = ring_buffer_event_data(event);
551 entry->ip = ip;
552
553 memcpy(&entry->buf, str, size);
554
555 /* Add a newline if necessary */
556 if (entry->buf[size - 1] != '\n') {
557 entry->buf[size] = '\n';
558 entry->buf[size + 1] = '\0';
559 } else
560 entry->buf[size] = '\0';
561
562 __buffer_unlock_commit(buffer, event);
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800563 ftrace_trace_stack(buffer, irq_flags, 4, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500564
565 return size;
566}
567EXPORT_SYMBOL_GPL(__trace_puts);
568
569/**
570 * __trace_bputs - write the pointer to a constant string into trace buffer
571 * @ip: The address of the caller
572 * @str: The constant string to write to the buffer to
573 */
574int __trace_bputs(unsigned long ip, const char *str)
575{
576 struct ring_buffer_event *event;
577 struct ring_buffer *buffer;
578 struct bputs_entry *entry;
579 unsigned long irq_flags;
580 int size = sizeof(struct bputs_entry);
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800581 int pc;
582
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800583 if (!(trace_flags & TRACE_ITER_PRINTK))
584 return 0;
585
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800586 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500587
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500588 if (unlikely(tracing_selftest_running || tracing_disabled))
589 return 0;
590
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500591 local_save_flags(irq_flags);
592 buffer = global_trace.trace_buffer.buffer;
593 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800594 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500595 if (!event)
596 return 0;
597
598 entry = ring_buffer_event_data(event);
599 entry->ip = ip;
600 entry->str = str;
601
602 __buffer_unlock_commit(buffer, event);
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800603 ftrace_trace_stack(buffer, irq_flags, 4, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500604
605 return 1;
606}
607EXPORT_SYMBOL_GPL(__trace_bputs);
608
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500609#ifdef CONFIG_TRACER_SNAPSHOT
610/**
611 * trace_snapshot - take a snapshot of the current buffer.
612 *
613 * This causes a swap between the snapshot buffer and the current live
614 * tracing buffer. You can use this to take snapshots of the live
615 * trace when some condition is triggered, but continue to trace.
616 *
617 * Note, make sure to allocate the snapshot with either
618 * a tracing_snapshot_alloc(), or by doing it manually
619 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
620 *
621 * If the snapshot buffer is not allocated, it will stop tracing.
622 * Basically making a permanent snapshot.
623 */
624void tracing_snapshot(void)
625{
626 struct trace_array *tr = &global_trace;
627 struct tracer *tracer = tr->current_trace;
628 unsigned long flags;
629
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500630 if (in_nmi()) {
631 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
632 internal_trace_puts("*** snapshot is being ignored ***\n");
633 return;
634 }
635
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500636 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500637 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
638 internal_trace_puts("*** stopping trace here! ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500639 tracing_off();
640 return;
641 }
642
643 /* Note, snapshot can not be used when the tracer uses it */
644 if (tracer->use_max_tr) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500645 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
646 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500647 return;
648 }
649
650 local_irq_save(flags);
651 update_max_tr(tr, current, smp_processor_id());
652 local_irq_restore(flags);
653}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500654EXPORT_SYMBOL_GPL(tracing_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500655
656static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
657 struct trace_buffer *size_buf, int cpu_id);
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400658static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
659
660static int alloc_snapshot(struct trace_array *tr)
661{
662 int ret;
663
664 if (!tr->allocated_snapshot) {
665
666 /* allocate spare buffer */
667 ret = resize_buffer_duplicate_size(&tr->max_buffer,
668 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
669 if (ret < 0)
670 return ret;
671
672 tr->allocated_snapshot = true;
673 }
674
675 return 0;
676}
677
Fabian Frederickad1438a2014-04-17 21:44:42 +0200678static void free_snapshot(struct trace_array *tr)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400679{
680 /*
681 * We don't free the ring buffer. instead, resize it because
682 * The max_tr ring buffer has some state (e.g. ring->clock) and
683 * we want preserve it.
684 */
685 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
686 set_buffer_entries(&tr->max_buffer, 1);
687 tracing_reset_online_cpus(&tr->max_buffer);
688 tr->allocated_snapshot = false;
689}
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500690
691/**
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500692 * tracing_alloc_snapshot - allocate snapshot buffer.
693 *
694 * This only allocates the snapshot buffer if it isn't already
695 * allocated - it doesn't also take a snapshot.
696 *
697 * This is meant to be used in cases where the snapshot buffer needs
698 * to be set up for events that can't sleep but need to be able to
699 * trigger a snapshot.
700 */
701int tracing_alloc_snapshot(void)
702{
703 struct trace_array *tr = &global_trace;
704 int ret;
705
706 ret = alloc_snapshot(tr);
707 WARN_ON(ret < 0);
708
709 return ret;
710}
711EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
712
713/**
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500714 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
715 *
716 * This is similar to trace_snapshot(), but it will allocate the
717 * snapshot buffer if it isn't already allocated. Use this only
718 * where it is safe to sleep, as the allocation may sleep.
719 *
720 * This causes a swap between the snapshot buffer and the current live
721 * tracing buffer. You can use this to take snapshots of the live
722 * trace when some condition is triggered, but continue to trace.
723 */
724void tracing_snapshot_alloc(void)
725{
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500726 int ret;
727
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500728 ret = tracing_alloc_snapshot();
729 if (ret < 0)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400730 return;
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500731
732 tracing_snapshot();
733}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500734EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500735#else
736void tracing_snapshot(void)
737{
738 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
739}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500740EXPORT_SYMBOL_GPL(tracing_snapshot);
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500741int tracing_alloc_snapshot(void)
742{
743 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
744 return -ENODEV;
745}
746EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500747void tracing_snapshot_alloc(void)
748{
749 /* Give warning */
750 tracing_snapshot();
751}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500752EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500753#endif /* CONFIG_TRACER_SNAPSHOT */
754
Steven Rostedt (Red Hat)5280bce2013-07-02 19:59:57 -0400755static void tracer_tracing_off(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400756{
757 if (tr->trace_buffer.buffer)
758 ring_buffer_record_off(tr->trace_buffer.buffer);
759 /*
760 * This flag is looked at when buffers haven't been allocated
761 * yet, or by some tracers (like irqsoff), that just want to
762 * know if the ring buffer has been disabled, but it can handle
763 * races of where it gets disabled but we still do a record.
764 * As the check is in the fast path of the tracers, it is more
765 * important to be fast than accurate.
766 */
767 tr->buffer_disabled = 1;
768 /* Make the flag seen by readers */
769 smp_wmb();
770}
771
Steven Rostedt499e5472012-02-22 15:50:28 -0500772/**
773 * tracing_off - turn off tracing buffers
774 *
775 * This function stops the tracing buffers from recording data.
776 * It does not disable any overhead the tracers themselves may
777 * be causing. This function simply causes all recording to
778 * the ring buffers to fail.
779 */
780void tracing_off(void)
781{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400782 tracer_tracing_off(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -0500783}
784EXPORT_SYMBOL_GPL(tracing_off);
785
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400786void disable_trace_on_warning(void)
787{
788 if (__disable_trace_on_warning)
789 tracing_off();
790}
791
Steven Rostedt499e5472012-02-22 15:50:28 -0500792/**
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400793 * tracer_tracing_is_on - show real state of ring buffer enabled
794 * @tr : the trace array to know if ring buffer is enabled
795 *
796 * Shows real state of the ring buffer if it is enabled or not.
797 */
Steven Rostedt (Red Hat)5280bce2013-07-02 19:59:57 -0400798static int tracer_tracing_is_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400799{
800 if (tr->trace_buffer.buffer)
801 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
802 return !tr->buffer_disabled;
803}
804
Steven Rostedt499e5472012-02-22 15:50:28 -0500805/**
806 * tracing_is_on - show state of ring buffers enabled
807 */
808int tracing_is_on(void)
809{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400810 return tracer_tracing_is_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -0500811}
812EXPORT_SYMBOL_GPL(tracing_is_on);
813
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400814static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200815{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400816 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200817
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200818 if (!str)
819 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +0800820 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200821 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +0800822 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200823 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400824 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200825 return 1;
826}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400827__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200828
Tim Bird0e950172010-02-25 15:36:43 -0800829static int __init set_tracing_thresh(char *str)
830{
Wang Tianhong87abb3b2012-08-02 14:02:00 +0800831 unsigned long threshold;
Tim Bird0e950172010-02-25 15:36:43 -0800832 int ret;
833
834 if (!str)
835 return 0;
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200836 ret = kstrtoul(str, 0, &threshold);
Tim Bird0e950172010-02-25 15:36:43 -0800837 if (ret < 0)
838 return 0;
Wang Tianhong87abb3b2012-08-02 14:02:00 +0800839 tracing_thresh = threshold * 1000;
Tim Bird0e950172010-02-25 15:36:43 -0800840 return 1;
841}
842__setup("tracing_thresh=", set_tracing_thresh);
843
Steven Rostedt57f50be2008-05-12 21:20:44 +0200844unsigned long nsecs_to_usecs(unsigned long nsecs)
845{
846 return nsecs / 1000;
847}
848
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200849/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200850static const char *trace_options[] = {
851 "print-parent",
852 "sym-offset",
853 "sym-addr",
854 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200855 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200856 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200857 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200858 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200859 "stacktrace",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100860 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500861 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500862 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500863 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200864 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200865 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100866 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200867 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500868 "latency-format",
Steven Rostedtbe6f1642009-03-24 11:06:24 -0400869 "sleep-time",
Steven Rostedta2a16d62009-03-24 23:17:58 -0400870 "graph-time",
Li Zefane870e9a2010-07-02 11:07:32 +0800871 "record-cmd",
David Sharp750912f2010-12-08 13:46:47 -0800872 "overwrite",
Steven Rostedtcf30cf62011-06-14 22:44:07 -0400873 "disable_on_free",
Steven Rostedt77271ce2011-11-17 09:34:33 -0500874 "irq-info",
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -0700875 "markers",
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400876 "function-trace",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200877 NULL
878};
879
Zhaolei5079f322009-08-25 16:12:56 +0800880static struct {
881 u64 (*func)(void);
882 const char *name;
David Sharp8be07092012-11-13 12:18:22 -0800883 int in_ns; /* is this clock in nanoseconds? */
Zhaolei5079f322009-08-25 16:12:56 +0800884} trace_clocks[] = {
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +0000885 { trace_clock_local, "local", 1 },
886 { trace_clock_global, "global", 1 },
887 { trace_clock_counter, "counter", 0 },
Linus Torvaldse7fda6c2014-08-05 17:46:42 -0700888 { trace_clock_jiffies, "uptime", 0 },
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +0000889 { trace_clock, "perf", 1 },
890 { ktime_get_mono_fast_ns, "mono", 1 },
Drew Richardsonaabfa5f2015-05-08 07:30:39 -0700891 { ktime_get_raw_fast_ns, "mono_raw", 1 },
David Sharp8cbd9cc2012-11-13 12:18:21 -0800892 ARCH_TRACE_CLOCKS
Zhaolei5079f322009-08-25 16:12:56 +0800893};
894
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200895/*
896 * trace_parser_get_init - gets the buffer for trace parser
897 */
898int trace_parser_get_init(struct trace_parser *parser, int size)
899{
900 memset(parser, 0, sizeof(*parser));
901
902 parser->buffer = kmalloc(size, GFP_KERNEL);
903 if (!parser->buffer)
904 return 1;
905
906 parser->size = size;
907 return 0;
908}
909
910/*
911 * trace_parser_put - frees the buffer for trace parser
912 */
913void trace_parser_put(struct trace_parser *parser)
914{
915 kfree(parser->buffer);
916}
917
918/*
919 * trace_get_user - reads the user input string separated by space
920 * (matched by isspace(ch))
921 *
922 * For each string found the 'struct trace_parser' is updated,
923 * and the function returns.
924 *
925 * Returns number of bytes read.
926 *
927 * See kernel/trace/trace.h for 'struct trace_parser' details.
928 */
929int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
930 size_t cnt, loff_t *ppos)
931{
932 char ch;
933 size_t read = 0;
934 ssize_t ret;
935
936 if (!*ppos)
937 trace_parser_clear(parser);
938
939 ret = get_user(ch, ubuf++);
940 if (ret)
941 goto out;
942
943 read++;
944 cnt--;
945
946 /*
947 * The parser is not finished with the last write,
948 * continue reading the user input without skipping spaces.
949 */
950 if (!parser->cont) {
951 /* skip white space */
952 while (cnt && isspace(ch)) {
953 ret = get_user(ch, ubuf++);
954 if (ret)
955 goto out;
956 read++;
957 cnt--;
958 }
959
960 /* only spaces were written */
961 if (isspace(ch)) {
962 *ppos += read;
963 ret = read;
964 goto out;
965 }
966
967 parser->idx = 0;
968 }
969
970 /* read the non-space input */
971 while (cnt && !isspace(ch)) {
Li Zefan3c235a32009-09-22 13:51:54 +0800972 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200973 parser->buffer[parser->idx++] = ch;
974 else {
975 ret = -EINVAL;
976 goto out;
977 }
978 ret = get_user(ch, ubuf++);
979 if (ret)
980 goto out;
981 read++;
982 cnt--;
983 }
984
985 /* We either got finished input or we have to wait for another call. */
986 if (isspace(ch)) {
987 parser->buffer[parser->idx] = 0;
988 parser->cont = false;
Steven Rostedt057db842013-10-09 22:23:23 -0400989 } else if (parser->idx < parser->size - 1) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200990 parser->cont = true;
991 parser->buffer[parser->idx++] = ch;
Steven Rostedt057db842013-10-09 22:23:23 -0400992 } else {
993 ret = -EINVAL;
994 goto out;
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200995 }
996
997 *ppos += read;
998 ret = read;
999
1000out:
1001 return ret;
1002}
1003
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001004/* TODO add a seq_buf_to_buffer() */
Dmitri Vorobievb8b94262009-03-22 19:11:11 +02001005static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001006{
1007 int len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001008
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001009 if (trace_seq_used(s) <= s->seq.readpos)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001010 return -EBUSY;
1011
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001012 len = trace_seq_used(s) - s->seq.readpos;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001013 if (cnt > len)
1014 cnt = len;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001015 memcpy(buf, s->buffer + s->seq.readpos, cnt);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001016
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001017 s->seq.readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001018 return cnt;
1019}
1020
Tim Bird0e950172010-02-25 15:36:43 -08001021unsigned long __read_mostly tracing_thresh;
1022
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001023#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001024/*
1025 * Copy the new maximum trace into the separate maximum-trace
1026 * structure. (this way the maximum trace is permanently saved,
1027 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
1028 */
1029static void
1030__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1031{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001032 struct trace_buffer *trace_buf = &tr->trace_buffer;
1033 struct trace_buffer *max_buf = &tr->max_buffer;
1034 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1035 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001036
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001037 max_buf->cpu = cpu;
1038 max_buf->time_start = data->preempt_timestamp;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001039
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001040 max_data->saved_latency = tr->max_latency;
Steven Rostedt8248ac02009-09-02 12:27:41 -04001041 max_data->critical_start = data->critical_start;
1042 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001043
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -03001044 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -04001045 max_data->pid = tsk->pid;
Steven Rostedt (Red Hat)f17a5192013-05-30 21:10:37 -04001046 /*
1047 * If tsk == current, then use current_uid(), as that does not use
1048 * RCU. The irq tracer can be called out of RCU scope.
1049 */
1050 if (tsk == current)
1051 max_data->uid = current_uid();
1052 else
1053 max_data->uid = task_uid(tsk);
1054
Steven Rostedt8248ac02009-09-02 12:27:41 -04001055 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1056 max_data->policy = tsk->policy;
1057 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001058
1059 /* record this tasks comm */
1060 tracing_record_cmdline(tsk);
1061}
1062
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001063/**
1064 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1065 * @tr: tracer
1066 * @tsk: the task with the latency
1067 * @cpu: The cpu that initiated the trace.
1068 *
1069 * Flip the buffers between the @tr and the max_tr and record information
1070 * about which task was the cause of this latency.
1071 */
Ingo Molnare309b412008-05-12 21:20:51 +02001072void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001073update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1074{
Steven Rostedt (Red Hat)2721e722013-03-12 11:32:32 -04001075 struct ring_buffer *buf;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001076
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001077 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001078 return;
1079
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001080 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt34600f02013-01-22 13:35:11 -05001081
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05001082 if (!tr->allocated_snapshot) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001083 /* Only the nop tracer should hit this when disabling */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001084 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001085 return;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001086 }
Steven Rostedt34600f02013-01-22 13:35:11 -05001087
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001088 arch_spin_lock(&tr->max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001089
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001090 buf = tr->trace_buffer.buffer;
1091 tr->trace_buffer.buffer = tr->max_buffer.buffer;
1092 tr->max_buffer.buffer = buf;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001093
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001094 __update_max_tr(tr, tsk, cpu);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001095 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001096}
1097
1098/**
1099 * update_max_tr_single - only copy one trace over, and reset the rest
1100 * @tr - tracer
1101 * @tsk - task with the latency
1102 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001103 *
1104 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001105 */
Ingo Molnare309b412008-05-12 21:20:51 +02001106void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001107update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1108{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001109 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001110
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001111 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001112 return;
1113
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001114 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt6c244992013-04-29 20:08:14 -04001115 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001116 /* Only the nop tracer should hit this when disabling */
Linus Torvalds9e8529a2013-04-29 13:55:38 -07001117 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001118 return;
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001119 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001120
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001121 arch_spin_lock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001122
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001123 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001124
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001125 if (ret == -EBUSY) {
1126 /*
1127 * We failed to swap the buffer due to a commit taking
1128 * place on this CPU. We fail to record, but we reset
1129 * the max trace buffer (no one writes directly to it)
1130 * and flag that it failed.
1131 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001132 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001133 "Failed to swap buffers due to commit in progress\n");
1134 }
1135
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001136 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001137
1138 __update_max_tr(tr, tsk, cpu);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001139 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001140}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001141#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001142
Rabin Vincente30f53a2014-11-10 19:46:34 +01001143static int wait_on_pipe(struct trace_iterator *iter, bool full)
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001144{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001145 /* Iterators are static, they should be filled or empty */
1146 if (trace_buffer_iter(iter, iter->cpu_file))
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04001147 return 0;
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001148
Rabin Vincente30f53a2014-11-10 19:46:34 +01001149 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1150 full);
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001151}
1152
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001153#ifdef CONFIG_FTRACE_STARTUP_TEST
1154static int run_tracer_selftest(struct tracer *type)
1155{
1156 struct trace_array *tr = &global_trace;
1157 struct tracer *saved_tracer = tr->current_trace;
1158 int ret;
1159
1160 if (!type->selftest || tracing_selftest_disabled)
1161 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001162
1163 /*
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001164 * Run a selftest on this tracer.
1165 * Here we reset the trace buffer, and set the current
1166 * tracer to be this tracer. The tracer can then run some
1167 * internal tracing to verify that everything is in order.
1168 * If we fail, we do not register this tracer.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001169 */
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001170 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001171
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001172 tr->current_trace = type;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001173
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001174#ifdef CONFIG_TRACER_MAX_TRACE
1175 if (type->use_max_tr) {
1176 /* If we expanded the buffers, make sure the max is expanded too */
1177 if (ring_buffer_expanded)
1178 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1179 RING_BUFFER_ALL_CPUS);
1180 tr->allocated_snapshot = true;
1181 }
1182#endif
1183
1184 /* the test is responsible for initializing and enabling */
1185 pr_info("Testing tracer %s: ", type->name);
1186 ret = type->selftest(type, tr);
1187 /* the test is responsible for resetting too */
1188 tr->current_trace = saved_tracer;
1189 if (ret) {
1190 printk(KERN_CONT "FAILED!\n");
1191 /* Add the warning after printing 'FAILED' */
1192 WARN_ON(1);
1193 return -1;
1194 }
1195 /* Only reset on passing, to avoid touching corrupted buffers */
1196 tracing_reset_online_cpus(&tr->trace_buffer);
1197
1198#ifdef CONFIG_TRACER_MAX_TRACE
1199 if (type->use_max_tr) {
1200 tr->allocated_snapshot = false;
1201
1202 /* Shrink the max buffer again */
1203 if (ring_buffer_expanded)
1204 ring_buffer_resize(tr->max_buffer.buffer, 1,
1205 RING_BUFFER_ALL_CPUS);
1206 }
1207#endif
1208
1209 printk(KERN_CONT "PASSED\n");
1210 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001211}
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001212#else
1213static inline int run_tracer_selftest(struct tracer *type)
1214{
1215 return 0;
1216}
1217#endif /* CONFIG_FTRACE_STARTUP_TEST */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001218
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001219/**
1220 * register_tracer - register a tracer with the ftrace system.
1221 * @type - the plugin for the tracer
1222 *
1223 * Register a new plugin tracer.
1224 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001225int register_tracer(struct tracer *type)
1226{
1227 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001228 int ret = 0;
1229
1230 if (!type->name) {
1231 pr_info("Tracer must have a name\n");
1232 return -1;
1233 }
1234
Dan Carpenter24a461d2010-07-10 12:06:44 +02001235 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +08001236 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1237 return -1;
1238 }
1239
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001240 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +01001241
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001242 tracing_selftest_running = true;
1243
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001244 for (t = trace_types; t; t = t->next) {
1245 if (strcmp(type->name, t->name) == 0) {
1246 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +08001247 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001248 type->name);
1249 ret = -1;
1250 goto out;
1251 }
1252 }
1253
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001254 if (!type->set_flag)
1255 type->set_flag = &dummy_set_flag;
1256 if (!type->flags)
1257 type->flags = &dummy_tracer_flags;
1258 else
1259 if (!type->flags->opts)
1260 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001261
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001262 ret = run_tracer_selftest(type);
1263 if (ret < 0)
1264 goto out;
Steven Rostedt60a11772008-05-12 21:20:44 +02001265
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001266 type->next = trace_types;
1267 trace_types = type;
Steven Rostedt60a11772008-05-12 21:20:44 +02001268
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001269 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001270 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001271 mutex_unlock(&trace_types_lock);
1272
Steven Rostedtdac74942009-02-05 01:13:38 -05001273 if (ret || !default_bootup_tracer)
1274 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -05001275
Li Zefanee6c2c12009-09-18 14:06:47 +08001276 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -05001277 goto out_unlock;
1278
1279 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1280 /* Do we want this tracer to start on bootup? */
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05001281 tracing_set_tracer(&global_trace, type->name);
Steven Rostedtdac74942009-02-05 01:13:38 -05001282 default_bootup_tracer = NULL;
1283 /* disable other selftests, since this will break it. */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05001284 tracing_selftest_disabled = true;
Steven Rostedtdac74942009-02-05 01:13:38 -05001285#ifdef CONFIG_FTRACE_STARTUP_TEST
1286 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1287 type->name);
1288#endif
1289
1290 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001291 return ret;
1292}
1293
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001294void tracing_reset(struct trace_buffer *buf, int cpu)
Steven Rostedtf6339032009-09-04 12:35:16 -04001295{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001296 struct ring_buffer *buffer = buf->buffer;
Steven Rostedtf6339032009-09-04 12:35:16 -04001297
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001298 if (!buffer)
1299 return;
1300
Steven Rostedtf6339032009-09-04 12:35:16 -04001301 ring_buffer_record_disable(buffer);
1302
1303 /* Make sure all commits have finished */
1304 synchronize_sched();
Steven Rostedt68179682012-05-08 20:57:53 -04001305 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -04001306
1307 ring_buffer_record_enable(buffer);
1308}
1309
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001310void tracing_reset_online_cpus(struct trace_buffer *buf)
Pekka J Enberg213cc062008-12-19 12:08:39 +02001311{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001312 struct ring_buffer *buffer = buf->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +02001313 int cpu;
1314
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001315 if (!buffer)
1316 return;
1317
Steven Rostedt621968c2009-09-04 12:02:35 -04001318 ring_buffer_record_disable(buffer);
1319
1320 /* Make sure all commits have finished */
1321 synchronize_sched();
1322
Alexander Z Lam94571582013-08-02 18:36:16 -07001323 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001324
1325 for_each_online_cpu(cpu)
Steven Rostedt68179682012-05-08 20:57:53 -04001326 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -04001327
1328 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001329}
1330
Steven Rostedt (Red Hat)09d80912013-07-23 22:21:59 -04001331/* Must have trace_types_lock held */
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001332void tracing_reset_all_online_cpus(void)
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001333{
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001334 struct trace_array *tr;
1335
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001336 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001337 tracing_reset_online_cpus(&tr->trace_buffer);
1338#ifdef CONFIG_TRACER_MAX_TRACE
1339 tracing_reset_online_cpus(&tr->max_buffer);
1340#endif
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001341 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001342}
1343
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001344#define SAVED_CMDLINES_DEFAULT 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001345#define NO_CMDLINE_MAP UINT_MAX
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001346static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001347struct saved_cmdlines_buffer {
1348 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1349 unsigned *map_cmdline_to_pid;
1350 unsigned cmdline_num;
1351 int cmdline_idx;
1352 char *saved_cmdlines;
1353};
1354static struct saved_cmdlines_buffer *savedcmd;
Steven Rostedt25b0b442008-05-12 21:21:00 +02001355
Steven Rostedt25b0b442008-05-12 21:21:00 +02001356/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +01001357static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001358
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001359static inline char *get_saved_cmdlines(int idx)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001360{
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001361 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
1362}
1363
1364static inline void set_cmdline(int idx, const char *cmdline)
1365{
1366 memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
1367}
1368
1369static int allocate_cmdlines_buffer(unsigned int val,
1370 struct saved_cmdlines_buffer *s)
1371{
1372 s->map_cmdline_to_pid = kmalloc(val * sizeof(*s->map_cmdline_to_pid),
1373 GFP_KERNEL);
1374 if (!s->map_cmdline_to_pid)
1375 return -ENOMEM;
1376
1377 s->saved_cmdlines = kmalloc(val * TASK_COMM_LEN, GFP_KERNEL);
1378 if (!s->saved_cmdlines) {
1379 kfree(s->map_cmdline_to_pid);
1380 return -ENOMEM;
1381 }
1382
1383 s->cmdline_idx = 0;
1384 s->cmdline_num = val;
1385 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
1386 sizeof(s->map_pid_to_cmdline));
1387 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
1388 val * sizeof(*s->map_cmdline_to_pid));
1389
1390 return 0;
1391}
1392
1393static int trace_create_savedcmd(void)
1394{
1395 int ret;
1396
Namhyung Kima6af8fb2014-06-10 16:11:35 +09001397 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001398 if (!savedcmd)
1399 return -ENOMEM;
1400
1401 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
1402 if (ret < 0) {
1403 kfree(savedcmd);
1404 savedcmd = NULL;
1405 return -ENOMEM;
1406 }
1407
1408 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001409}
1410
Carsten Emdeb5130b12009-09-13 01:43:07 +02001411int is_tracing_stopped(void)
1412{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001413 return global_trace.stop_count;
Carsten Emdeb5130b12009-09-13 01:43:07 +02001414}
1415
Steven Rostedt0f048702008-11-05 16:05:44 -05001416/**
1417 * tracing_start - quick start of the tracer
1418 *
1419 * If tracing is enabled but was stopped by tracing_stop,
1420 * this will start the tracer back up.
1421 */
1422void tracing_start(void)
1423{
1424 struct ring_buffer *buffer;
1425 unsigned long flags;
1426
1427 if (tracing_disabled)
1428 return;
1429
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001430 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1431 if (--global_trace.stop_count) {
1432 if (global_trace.stop_count < 0) {
Steven Rostedtb06a8302009-01-22 14:26:15 -05001433 /* Someone screwed up their debugging */
1434 WARN_ON_ONCE(1);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001435 global_trace.stop_count = 0;
Steven Rostedtb06a8302009-01-22 14:26:15 -05001436 }
Steven Rostedt0f048702008-11-05 16:05:44 -05001437 goto out;
1438 }
1439
Steven Rostedta2f80712010-03-12 19:56:00 -05001440 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001441 arch_spin_lock(&global_trace.max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -05001442
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001443 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001444 if (buffer)
1445 ring_buffer_record_enable(buffer);
1446
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001447#ifdef CONFIG_TRACER_MAX_TRACE
1448 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001449 if (buffer)
1450 ring_buffer_record_enable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001451#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001452
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001453 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001454
Steven Rostedt0f048702008-11-05 16:05:44 -05001455 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001456 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1457}
1458
1459static void tracing_start_tr(struct trace_array *tr)
1460{
1461 struct ring_buffer *buffer;
1462 unsigned long flags;
1463
1464 if (tracing_disabled)
1465 return;
1466
1467 /* If global, we need to also start the max tracer */
1468 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1469 return tracing_start();
1470
1471 raw_spin_lock_irqsave(&tr->start_lock, flags);
1472
1473 if (--tr->stop_count) {
1474 if (tr->stop_count < 0) {
1475 /* Someone screwed up their debugging */
1476 WARN_ON_ONCE(1);
1477 tr->stop_count = 0;
1478 }
1479 goto out;
1480 }
1481
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001482 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001483 if (buffer)
1484 ring_buffer_record_enable(buffer);
1485
1486 out:
1487 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001488}
1489
1490/**
1491 * tracing_stop - quick stop of the tracer
1492 *
1493 * Light weight way to stop tracing. Use in conjunction with
1494 * tracing_start.
1495 */
1496void tracing_stop(void)
1497{
1498 struct ring_buffer *buffer;
1499 unsigned long flags;
1500
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001501 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1502 if (global_trace.stop_count++)
Steven Rostedt0f048702008-11-05 16:05:44 -05001503 goto out;
1504
Steven Rostedta2f80712010-03-12 19:56:00 -05001505 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001506 arch_spin_lock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001507
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001508 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001509 if (buffer)
1510 ring_buffer_record_disable(buffer);
1511
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001512#ifdef CONFIG_TRACER_MAX_TRACE
1513 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001514 if (buffer)
1515 ring_buffer_record_disable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001516#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001517
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001518 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001519
Steven Rostedt0f048702008-11-05 16:05:44 -05001520 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001521 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1522}
1523
1524static void tracing_stop_tr(struct trace_array *tr)
1525{
1526 struct ring_buffer *buffer;
1527 unsigned long flags;
1528
1529 /* If global, we need to also stop the max tracer */
1530 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1531 return tracing_stop();
1532
1533 raw_spin_lock_irqsave(&tr->start_lock, flags);
1534 if (tr->stop_count++)
1535 goto out;
1536
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001537 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001538 if (buffer)
1539 ring_buffer_record_disable(buffer);
1540
1541 out:
1542 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001543}
1544
Ingo Molnare309b412008-05-12 21:20:51 +02001545void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001546
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001547static int trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001548{
Carsten Emdea635cf02009-03-18 09:00:41 +01001549 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001550
1551 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001552 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001553
1554 /*
1555 * It's not the end of the world if we don't get
1556 * the lock, but we also don't want to spin
1557 * nor do we want to disable interrupts,
1558 * so if we miss here, then better luck next time.
1559 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001560 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001561 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001562
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001563 idx = savedcmd->map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001564 if (idx == NO_CMDLINE_MAP) {
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001565 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001566
Carsten Emdea635cf02009-03-18 09:00:41 +01001567 /*
1568 * Check whether the cmdline buffer at idx has a pid
1569 * mapped. We are going to overwrite that entry so we
1570 * need to clear the map_pid_to_cmdline. Otherwise we
1571 * would read the new comm for the old pid.
1572 */
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001573 pid = savedcmd->map_cmdline_to_pid[idx];
Carsten Emdea635cf02009-03-18 09:00:41 +01001574 if (pid != NO_CMDLINE_MAP)
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001575 savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001576
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001577 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
1578 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001579
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001580 savedcmd->cmdline_idx = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001581 }
1582
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001583 set_cmdline(idx, tsk->comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001584
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001585 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001586
1587 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001588}
1589
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04001590static void __trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001591{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001592 unsigned map;
1593
Steven Rostedt4ca530852009-03-16 19:20:15 -04001594 if (!pid) {
1595 strcpy(comm, "<idle>");
1596 return;
1597 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001598
Steven Rostedt74bf4072010-01-25 15:11:53 -05001599 if (WARN_ON_ONCE(pid < 0)) {
1600 strcpy(comm, "<XXX>");
1601 return;
1602 }
1603
Steven Rostedt4ca530852009-03-16 19:20:15 -04001604 if (pid > PID_MAX_DEFAULT) {
1605 strcpy(comm, "<...>");
1606 return;
1607 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001608
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001609 map = savedcmd->map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01001610 if (map != NO_CMDLINE_MAP)
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001611 strcpy(comm, get_saved_cmdlines(map));
Thomas Gleixner50d88752009-03-18 08:58:44 +01001612 else
1613 strcpy(comm, "<...>");
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04001614}
1615
1616void trace_find_cmdline(int pid, char comm[])
1617{
1618 preempt_disable();
1619 arch_spin_lock(&trace_cmdline_lock);
1620
1621 __trace_find_cmdline(pid, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001622
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001623 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001624 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001625}
1626
Ingo Molnare309b412008-05-12 21:20:51 +02001627void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001628{
Steven Rostedt0fb96562012-05-11 14:25:30 -04001629 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001630 return;
1631
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001632 if (!__this_cpu_read(trace_cmdline_save))
1633 return;
1634
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001635 if (trace_save_cmdline(tsk))
1636 __this_cpu_write(trace_cmdline_save, false);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001637}
1638
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03001639void
Steven Rostedt38697052008-10-01 13:14:09 -04001640tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1641 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001642{
1643 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001644
Steven Rostedt777e2082008-09-29 23:02:42 -04001645 entry->preempt_count = pc & 0xff;
1646 entry->pid = (tsk) ? tsk->pid : 0;
1647 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04001648#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001649 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04001650#else
1651 TRACE_FLAG_IRQS_NOSUPPORT |
1652#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001653 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1654 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
Peter Zijlstrae5137b52013-10-04 17:28:26 +02001655 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
1656 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001657}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02001658EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001659
Steven Rostedte77405a2009-09-02 14:17:06 -04001660struct ring_buffer_event *
1661trace_buffer_lock_reserve(struct ring_buffer *buffer,
1662 int type,
1663 unsigned long len,
1664 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001665{
1666 struct ring_buffer_event *event;
1667
Steven Rostedte77405a2009-09-02 14:17:06 -04001668 event = ring_buffer_lock_reserve(buffer, len);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001669 if (event != NULL) {
1670 struct trace_entry *ent = ring_buffer_event_data(event);
1671
1672 tracing_generic_entry_update(ent, flags, pc);
1673 ent->type = type;
1674 }
1675
1676 return event;
1677}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001678
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001679void
1680__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1681{
1682 __this_cpu_write(trace_cmdline_save, true);
1683 ring_buffer_unlock_commit(buffer, event);
1684}
1685
Steven Rostedte77405a2009-09-02 14:17:06 -04001686static inline void
1687__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1688 struct ring_buffer_event *event,
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001689 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001690{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001691 __buffer_unlock_commit(buffer, event);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001692
Steven Rostedte77405a2009-09-02 14:17:06 -04001693 ftrace_trace_stack(buffer, flags, 6, pc);
1694 ftrace_trace_userstack(buffer, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001695}
1696
Steven Rostedte77405a2009-09-02 14:17:06 -04001697void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1698 struct ring_buffer_event *event,
1699 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001700{
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001701 __trace_buffer_unlock_commit(buffer, event, flags, pc);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001702}
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001703EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001704
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04001705static struct ring_buffer *temp_buffer;
1706
Steven Rostedtef5580d2009-02-27 19:38:04 -05001707struct ring_buffer_event *
Steven Rostedtccb469a2012-08-02 10:32:10 -04001708trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001709 struct trace_event_file *trace_file,
Steven Rostedtccb469a2012-08-02 10:32:10 -04001710 int type, unsigned long len,
1711 unsigned long flags, int pc)
1712{
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04001713 struct ring_buffer_event *entry;
1714
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001715 *current_rb = trace_file->tr->trace_buffer.buffer;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04001716 entry = trace_buffer_lock_reserve(*current_rb,
Steven Rostedtccb469a2012-08-02 10:32:10 -04001717 type, len, flags, pc);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04001718 /*
1719 * If tracing is off, but we have triggers enabled
1720 * we still need to look at the event data. Use the temp_buffer
1721 * to store the trace event for the tigger to use. It's recusive
1722 * safe and will not be recorded anywhere.
1723 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001724 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04001725 *current_rb = temp_buffer;
1726 entry = trace_buffer_lock_reserve(*current_rb,
1727 type, len, flags, pc);
1728 }
1729 return entry;
Steven Rostedtccb469a2012-08-02 10:32:10 -04001730}
1731EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1732
1733struct ring_buffer_event *
Steven Rostedte77405a2009-09-02 14:17:06 -04001734trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1735 int type, unsigned long len,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001736 unsigned long flags, int pc)
1737{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001738 *current_rb = global_trace.trace_buffer.buffer;
Steven Rostedte77405a2009-09-02 14:17:06 -04001739 return trace_buffer_lock_reserve(*current_rb,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001740 type, len, flags, pc);
1741}
Steven Rostedt94487d62009-05-05 19:22:53 -04001742EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001743
Steven Rostedte77405a2009-09-02 14:17:06 -04001744void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1745 struct ring_buffer_event *event,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001746 unsigned long flags, int pc)
1747{
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001748 __trace_buffer_unlock_commit(buffer, event, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001749}
Steven Rostedt94487d62009-05-05 19:22:53 -04001750EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001751
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001752void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1753 struct ring_buffer_event *event,
1754 unsigned long flags, int pc,
1755 struct pt_regs *regs)
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001756{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001757 __buffer_unlock_commit(buffer, event);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001758
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -04001759 if (trace_flags & TRACE_ITER_STACKTRACE)
1760 __ftrace_trace_stack(buffer, flags, 0, pc, regs);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001761 ftrace_trace_userstack(buffer, flags, pc);
1762}
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001763EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001764
Steven Rostedte77405a2009-09-02 14:17:06 -04001765void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1766 struct ring_buffer_event *event)
Steven Rostedt77d9f462009-04-02 01:16:59 -04001767{
Steven Rostedte77405a2009-09-02 14:17:06 -04001768 ring_buffer_discard_commit(buffer, event);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001769}
Steven Rostedt12acd472009-04-17 16:01:56 -04001770EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001771
Ingo Molnare309b412008-05-12 21:20:51 +02001772void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001773trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001774 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1775 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001776{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001777 struct trace_event_call *call = &event_function;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001778 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001779 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001780 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001781
Steven Rostedtd7690412008-10-01 00:29:53 -04001782 /* If we are reading the ring buffer, don't trace */
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001783 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04001784 return;
1785
Steven Rostedte77405a2009-09-02 14:17:06 -04001786 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001787 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001788 if (!event)
1789 return;
1790 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001791 entry->ip = ip;
1792 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05001793
Tom Zanussif306cc82013-10-24 08:34:17 -05001794 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001795 __buffer_unlock_commit(buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001796}
1797
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001798#ifdef CONFIG_STACKTRACE
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001799
1800#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1801struct ftrace_stack {
1802 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1803};
1804
1805static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1806static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1807
Steven Rostedte77405a2009-09-02 14:17:06 -04001808static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05001809 unsigned long flags,
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001810 int skip, int pc, struct pt_regs *regs)
Ingo Molnar86387f72008-05-12 21:20:51 +02001811{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001812 struct trace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001813 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001814 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02001815 struct stack_trace trace;
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001816 int use_stack;
1817 int size = FTRACE_STACK_ENTRIES;
Ingo Molnar86387f72008-05-12 21:20:51 +02001818
1819 trace.nr_entries = 0;
Ingo Molnar86387f72008-05-12 21:20:51 +02001820 trace.skip = skip;
Ingo Molnar86387f72008-05-12 21:20:51 +02001821
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001822 /*
1823 * Since events can happen in NMIs there's no safe way to
1824 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1825 * or NMI comes in, it will just have to use the default
1826 * FTRACE_STACK_SIZE.
1827 */
1828 preempt_disable_notrace();
1829
Shan Wei82146522012-11-19 13:21:01 +08001830 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001831 /*
1832 * We don't need any atomic variables, just a barrier.
1833 * If an interrupt comes in, we don't care, because it would
1834 * have exited and put the counter back to what we want.
1835 * We just need a barrier to keep gcc from moving things
1836 * around.
1837 */
1838 barrier();
1839 if (use_stack == 1) {
Christoph Lameterbdffd892014-04-29 14:17:40 -05001840 trace.entries = this_cpu_ptr(ftrace_stack.calls);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001841 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1842
1843 if (regs)
1844 save_stack_trace_regs(regs, &trace);
1845 else
1846 save_stack_trace(&trace);
1847
1848 if (trace.nr_entries > size)
1849 size = trace.nr_entries;
1850 } else
1851 /* From now on, use_stack is a boolean */
1852 use_stack = 0;
1853
1854 size *= sizeof(unsigned long);
1855
1856 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1857 sizeof(*entry) + size, flags, pc);
1858 if (!event)
1859 goto out;
1860 entry = ring_buffer_event_data(event);
1861
1862 memset(&entry->caller, 0, size);
1863
1864 if (use_stack)
1865 memcpy(&entry->caller, trace.entries,
1866 trace.nr_entries * sizeof(unsigned long));
1867 else {
1868 trace.max_entries = FTRACE_STACK_ENTRIES;
1869 trace.entries = entry->caller;
1870 if (regs)
1871 save_stack_trace_regs(regs, &trace);
1872 else
1873 save_stack_trace(&trace);
1874 }
1875
1876 entry->size = trace.nr_entries;
1877
Tom Zanussif306cc82013-10-24 08:34:17 -05001878 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001879 __buffer_unlock_commit(buffer, event);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001880
1881 out:
1882 /* Again, don't let gcc optimize things here */
1883 barrier();
Shan Wei82146522012-11-19 13:21:01 +08001884 __this_cpu_dec(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001885 preempt_enable_notrace();
1886
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001887}
1888
Steven Rostedte77405a2009-09-02 14:17:06 -04001889void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1890 int skip, int pc)
Steven Rostedt53614992009-01-15 19:12:40 -05001891{
1892 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1893 return;
1894
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001895 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
Steven Rostedt53614992009-01-15 19:12:40 -05001896}
1897
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001898void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1899 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04001900{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001901 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
Steven Rostedt38697052008-10-01 13:14:09 -04001902}
1903
Steven Rostedt03889382009-12-11 09:48:22 -05001904/**
1905 * trace_dump_stack - record a stack back trace in the trace buffer
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04001906 * @skip: Number of functions to skip (helper handlers)
Steven Rostedt03889382009-12-11 09:48:22 -05001907 */
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04001908void trace_dump_stack(int skip)
Steven Rostedt03889382009-12-11 09:48:22 -05001909{
1910 unsigned long flags;
1911
1912 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05001913 return;
Steven Rostedt03889382009-12-11 09:48:22 -05001914
1915 local_save_flags(flags);
1916
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04001917 /*
1918 * Skip 3 more, seems to get us at the caller of
1919 * this function.
1920 */
1921 skip += 3;
1922 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1923 flags, skip, preempt_count(), NULL);
Steven Rostedt03889382009-12-11 09:48:22 -05001924}
1925
Steven Rostedt91e86e52010-11-10 12:56:12 +01001926static DEFINE_PER_CPU(int, user_stack_count);
1927
Steven Rostedte77405a2009-09-02 14:17:06 -04001928void
1929ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02001930{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001931 struct trace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02001932 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001933 struct userstack_entry *entry;
1934 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001935
1936 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1937 return;
1938
Steven Rostedtb6345872010-03-12 20:03:30 -05001939 /*
1940 * NMIs can not handle page faults, even with fix ups.
1941 * The save user stack can (and often does) fault.
1942 */
1943 if (unlikely(in_nmi()))
1944 return;
1945
Steven Rostedt91e86e52010-11-10 12:56:12 +01001946 /*
1947 * prevent recursion, since the user stack tracing may
1948 * trigger other kernel events.
1949 */
1950 preempt_disable();
1951 if (__this_cpu_read(user_stack_count))
1952 goto out;
1953
1954 __this_cpu_inc(user_stack_count);
1955
Steven Rostedte77405a2009-09-02 14:17:06 -04001956 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001957 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001958 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08001959 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02001960 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001961
Steven Rostedt48659d32009-09-11 11:36:23 -04001962 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02001963 memset(&entry->caller, 0, sizeof(entry->caller));
1964
1965 trace.nr_entries = 0;
1966 trace.max_entries = FTRACE_STACK_ENTRIES;
1967 trace.skip = 0;
1968 trace.entries = entry->caller;
1969
1970 save_stack_trace_user(&trace);
Tom Zanussif306cc82013-10-24 08:34:17 -05001971 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001972 __buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001973
Li Zefan1dbd1952010-12-09 15:47:56 +08001974 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01001975 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001976 out:
1977 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02001978}
1979
Hannes Eder4fd27352009-02-10 19:44:12 +01001980#ifdef UNUSED
1981static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001982{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001983 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001984}
Hannes Eder4fd27352009-02-10 19:44:12 +01001985#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001986
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001987#endif /* CONFIG_STACKTRACE */
1988
Steven Rostedt07d777f2011-09-22 14:01:55 -04001989/* created for use with alloc_percpu */
1990struct trace_buffer_struct {
1991 char buffer[TRACE_BUF_SIZE];
1992};
1993
1994static struct trace_buffer_struct *trace_percpu_buffer;
1995static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1996static struct trace_buffer_struct *trace_percpu_irq_buffer;
1997static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1998
1999/*
2000 * The buffer used is dependent on the context. There is a per cpu
2001 * buffer for normal context, softirq contex, hard irq context and
2002 * for NMI context. Thise allows for lockless recording.
2003 *
2004 * Note, if the buffers failed to be allocated, then this returns NULL
2005 */
2006static char *get_trace_buf(void)
2007{
2008 struct trace_buffer_struct *percpu_buffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002009
2010 /*
2011 * If we have allocated per cpu buffers, then we do not
2012 * need to do any locking.
2013 */
2014 if (in_nmi())
2015 percpu_buffer = trace_percpu_nmi_buffer;
2016 else if (in_irq())
2017 percpu_buffer = trace_percpu_irq_buffer;
2018 else if (in_softirq())
2019 percpu_buffer = trace_percpu_sirq_buffer;
2020 else
2021 percpu_buffer = trace_percpu_buffer;
2022
2023 if (!percpu_buffer)
2024 return NULL;
2025
Shan Weid8a03492012-11-13 09:53:04 +08002026 return this_cpu_ptr(&percpu_buffer->buffer[0]);
Steven Rostedt07d777f2011-09-22 14:01:55 -04002027}
2028
2029static int alloc_percpu_trace_buffer(void)
2030{
2031 struct trace_buffer_struct *buffers;
2032 struct trace_buffer_struct *sirq_buffers;
2033 struct trace_buffer_struct *irq_buffers;
2034 struct trace_buffer_struct *nmi_buffers;
2035
2036 buffers = alloc_percpu(struct trace_buffer_struct);
2037 if (!buffers)
2038 goto err_warn;
2039
2040 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
2041 if (!sirq_buffers)
2042 goto err_sirq;
2043
2044 irq_buffers = alloc_percpu(struct trace_buffer_struct);
2045 if (!irq_buffers)
2046 goto err_irq;
2047
2048 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
2049 if (!nmi_buffers)
2050 goto err_nmi;
2051
2052 trace_percpu_buffer = buffers;
2053 trace_percpu_sirq_buffer = sirq_buffers;
2054 trace_percpu_irq_buffer = irq_buffers;
2055 trace_percpu_nmi_buffer = nmi_buffers;
2056
2057 return 0;
2058
2059 err_nmi:
2060 free_percpu(irq_buffers);
2061 err_irq:
2062 free_percpu(sirq_buffers);
2063 err_sirq:
2064 free_percpu(buffers);
2065 err_warn:
2066 WARN(1, "Could not allocate percpu trace_printk buffer");
2067 return -ENOMEM;
2068}
2069
Steven Rostedt81698832012-10-11 10:15:05 -04002070static int buffers_allocated;
2071
Steven Rostedt07d777f2011-09-22 14:01:55 -04002072void trace_printk_init_buffers(void)
2073{
Steven Rostedt07d777f2011-09-22 14:01:55 -04002074 if (buffers_allocated)
2075 return;
2076
2077 if (alloc_percpu_trace_buffer())
2078 return;
2079
Steven Rostedt2184db42014-05-28 13:14:40 -04002080 /* trace_printk() is for debug use only. Don't use it in production. */
2081
Borislav Petkov69a1c992015-01-27 17:17:20 +01002082 pr_warning("\n");
2083 pr_warning("**********************************************************\n");
Steven Rostedt2184db42014-05-28 13:14:40 -04002084 pr_warning("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2085 pr_warning("** **\n");
2086 pr_warning("** trace_printk() being used. Allocating extra memory. **\n");
2087 pr_warning("** **\n");
2088 pr_warning("** This means that this is a DEBUG kernel and it is **\n");
Frans Klavereff264e2014-11-07 15:53:44 +01002089 pr_warning("** unsafe for production use. **\n");
Steven Rostedt2184db42014-05-28 13:14:40 -04002090 pr_warning("** **\n");
2091 pr_warning("** If you see this message and you are not debugging **\n");
2092 pr_warning("** the kernel, report this immediately to your vendor! **\n");
2093 pr_warning("** **\n");
2094 pr_warning("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2095 pr_warning("**********************************************************\n");
Steven Rostedt07d777f2011-09-22 14:01:55 -04002096
Steven Rostedtb382ede62012-10-10 21:44:34 -04002097 /* Expand the buffers to set size */
2098 tracing_update_buffers();
2099
Steven Rostedt07d777f2011-09-22 14:01:55 -04002100 buffers_allocated = 1;
Steven Rostedt81698832012-10-11 10:15:05 -04002101
2102 /*
2103 * trace_printk_init_buffers() can be called by modules.
2104 * If that happens, then we need to start cmdline recording
2105 * directly here. If the global_trace.buffer is already
2106 * allocated here, then this was called by module code.
2107 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002108 if (global_trace.trace_buffer.buffer)
Steven Rostedt81698832012-10-11 10:15:05 -04002109 tracing_start_cmdline_record();
2110}
2111
2112void trace_printk_start_comm(void)
2113{
2114 /* Start tracing comms if trace printk is set */
2115 if (!buffers_allocated)
2116 return;
2117 tracing_start_cmdline_record();
2118}
2119
2120static void trace_printk_start_stop_comm(int enabled)
2121{
2122 if (!buffers_allocated)
2123 return;
2124
2125 if (enabled)
2126 tracing_start_cmdline_record();
2127 else
2128 tracing_stop_cmdline_record();
Steven Rostedt07d777f2011-09-22 14:01:55 -04002129}
2130
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002131/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002132 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002133 *
2134 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04002135int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002136{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002137 struct trace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002138 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04002139 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002140 struct trace_array *tr = &global_trace;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002141 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002142 unsigned long flags;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002143 char *tbuffer;
2144 int len = 0, size, pc;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002145
2146 if (unlikely(tracing_selftest_running || tracing_disabled))
2147 return 0;
2148
2149 /* Don't pollute graph traces with trace_vprintk internals */
2150 pause_graph_tracing();
2151
2152 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04002153 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002154
Steven Rostedt07d777f2011-09-22 14:01:55 -04002155 tbuffer = get_trace_buf();
2156 if (!tbuffer) {
2157 len = 0;
2158 goto out;
2159 }
2160
2161 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
2162
2163 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002164 goto out;
2165
Steven Rostedt07d777f2011-09-22 14:01:55 -04002166 local_save_flags(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002167 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002168 buffer = tr->trace_buffer.buffer;
Steven Rostedte77405a2009-09-02 14:17:06 -04002169 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2170 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002171 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002172 goto out;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002173 entry = ring_buffer_event_data(event);
2174 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002175 entry->fmt = fmt;
2176
Steven Rostedt07d777f2011-09-22 14:01:55 -04002177 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
Tom Zanussif306cc82013-10-24 08:34:17 -05002178 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002179 __buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05002180 ftrace_trace_stack(buffer, flags, 6, pc);
2181 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002182
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002183out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002184 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002185 unpause_graph_tracing();
2186
2187 return len;
2188}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002189EXPORT_SYMBOL_GPL(trace_vbprintk);
2190
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002191static int
2192__trace_array_vprintk(struct ring_buffer *buffer,
2193 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002194{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002195 struct trace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002196 struct ring_buffer_event *event;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002197 int len = 0, size, pc;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002198 struct print_entry *entry;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002199 unsigned long flags;
2200 char *tbuffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002201
2202 if (tracing_disabled || tracing_selftest_running)
2203 return 0;
2204
Steven Rostedt07d777f2011-09-22 14:01:55 -04002205 /* Don't pollute graph traces with trace_vprintk internals */
2206 pause_graph_tracing();
2207
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002208 pc = preempt_count();
2209 preempt_disable_notrace();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002210
Steven Rostedt07d777f2011-09-22 14:01:55 -04002211
2212 tbuffer = get_trace_buf();
2213 if (!tbuffer) {
2214 len = 0;
2215 goto out;
2216 }
2217
Dan Carpenter3558a5a2014-11-27 18:57:52 +03002218 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002219
Steven Rostedt07d777f2011-09-22 14:01:55 -04002220 local_save_flags(flags);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002221 size = sizeof(*entry) + len + 1;
Steven Rostedte77405a2009-09-02 14:17:06 -04002222 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
Steven Rostedt07d777f2011-09-22 14:01:55 -04002223 flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002224 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002225 goto out;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002226 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01002227 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002228
Dan Carpenter3558a5a2014-11-27 18:57:52 +03002229 memcpy(&entry->buf, tbuffer, len + 1);
Tom Zanussif306cc82013-10-24 08:34:17 -05002230 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002231 __buffer_unlock_commit(buffer, event);
Steven Rostedt07d777f2011-09-22 14:01:55 -04002232 ftrace_trace_stack(buffer, flags, 6, pc);
Steven Rostedtd9313692010-01-06 17:27:11 -05002233 }
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002234 out:
2235 preempt_enable_notrace();
Steven Rostedt07d777f2011-09-22 14:01:55 -04002236 unpause_graph_tracing();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002237
2238 return len;
2239}
Steven Rostedt659372d2009-09-03 19:11:07 -04002240
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002241int trace_array_vprintk(struct trace_array *tr,
2242 unsigned long ip, const char *fmt, va_list args)
2243{
2244 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2245}
2246
2247int trace_array_printk(struct trace_array *tr,
2248 unsigned long ip, const char *fmt, ...)
2249{
2250 int ret;
2251 va_list ap;
2252
2253 if (!(trace_flags & TRACE_ITER_PRINTK))
2254 return 0;
2255
2256 va_start(ap, fmt);
2257 ret = trace_array_vprintk(tr, ip, fmt, ap);
2258 va_end(ap);
2259 return ret;
2260}
2261
2262int trace_array_printk_buf(struct ring_buffer *buffer,
2263 unsigned long ip, const char *fmt, ...)
2264{
2265 int ret;
2266 va_list ap;
2267
2268 if (!(trace_flags & TRACE_ITER_PRINTK))
2269 return 0;
2270
2271 va_start(ap, fmt);
2272 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2273 va_end(ap);
2274 return ret;
2275}
2276
Steven Rostedt659372d2009-09-03 19:11:07 -04002277int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2278{
Steven Rostedta813a152009-10-09 01:41:35 -04002279 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04002280}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002281EXPORT_SYMBOL_GPL(trace_vprintk);
2282
Robert Richtere2ac8ef2008-11-12 12:59:32 +01002283static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04002284{
Steven Rostedt6d158a82012-06-27 20:46:14 -04002285 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2286
Steven Rostedt5a90f572008-09-03 17:42:51 -04002287 iter->idx++;
Steven Rostedt6d158a82012-06-27 20:46:14 -04002288 if (buf_iter)
2289 ring_buffer_read(buf_iter, NULL);
Steven Rostedt5a90f572008-09-03 17:42:51 -04002290}
2291
Ingo Molnare309b412008-05-12 21:20:51 +02002292static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04002293peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2294 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002295{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002296 struct ring_buffer_event *event;
Steven Rostedt6d158a82012-06-27 20:46:14 -04002297 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002298
Steven Rostedtd7690412008-10-01 00:29:53 -04002299 if (buf_iter)
2300 event = ring_buffer_iter_peek(buf_iter, ts);
2301 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002302 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04002303 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04002304
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002305 if (event) {
2306 iter->ent_size = ring_buffer_event_length(event);
2307 return ring_buffer_event_data(event);
2308 }
2309 iter->ent_size = 0;
2310 return NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002311}
Steven Rostedtd7690412008-10-01 00:29:53 -04002312
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002313static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04002314__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2315 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002316{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002317 struct ring_buffer *buffer = iter->trace_buffer->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002318 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08002319 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002320 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002321 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002322 int next_cpu = -1;
Steven Rostedt12b5da32012-03-27 10:43:28 -04002323 int next_size = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002324 int cpu;
2325
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002326 /*
2327 * If we are in a per_cpu trace file, don't bother by iterating over
2328 * all cpu and peek directly.
2329 */
Steven Rostedtae3b5092013-01-23 15:22:59 -05002330 if (cpu_file > RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002331 if (ring_buffer_empty_cpu(buffer, cpu_file))
2332 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002333 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002334 if (ent_cpu)
2335 *ent_cpu = cpu_file;
2336
2337 return ent;
2338 }
2339
Steven Rostedtab464282008-05-12 21:21:00 +02002340 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002341
2342 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002343 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002344
Steven Rostedtbc21b472010-03-31 19:49:26 -04002345 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002346
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02002347 /*
2348 * Pick the entry with the smallest timestamp:
2349 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002350 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002351 next = ent;
2352 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002353 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002354 next_lost = lost_events;
Steven Rostedt12b5da32012-03-27 10:43:28 -04002355 next_size = iter->ent_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002356 }
2357 }
2358
Steven Rostedt12b5da32012-03-27 10:43:28 -04002359 iter->ent_size = next_size;
2360
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002361 if (ent_cpu)
2362 *ent_cpu = next_cpu;
2363
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002364 if (ent_ts)
2365 *ent_ts = next_ts;
2366
Steven Rostedtbc21b472010-03-31 19:49:26 -04002367 if (missing_events)
2368 *missing_events = next_lost;
2369
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002370 return next;
2371}
2372
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002373/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002374struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2375 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002376{
Steven Rostedtbc21b472010-03-31 19:49:26 -04002377 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002378}
Ingo Molnar8c523a92008-05-12 21:20:46 +02002379
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002380/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05002381void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002382{
Steven Rostedtbc21b472010-03-31 19:49:26 -04002383 iter->ent = __find_next_entry(iter, &iter->cpu,
2384 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002385
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002386 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01002387 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002388
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002389 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002390}
2391
Ingo Molnare309b412008-05-12 21:20:51 +02002392static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002393{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002394 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04002395 &iter->lost_events);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002396}
2397
Ingo Molnare309b412008-05-12 21:20:51 +02002398static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002399{
2400 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002401 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002402 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002403
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002404 WARN_ON_ONCE(iter->leftover);
2405
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002406 (*pos)++;
2407
2408 /* can't go backwards */
2409 if (iter->idx > i)
2410 return NULL;
2411
2412 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05002413 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002414 else
2415 ent = iter;
2416
2417 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05002418 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002419
2420 iter->pos = *pos;
2421
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002422 return ent;
2423}
2424
Jason Wessel955b61e2010-08-05 09:22:23 -05002425void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002426{
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002427 struct ring_buffer_event *event;
2428 struct ring_buffer_iter *buf_iter;
2429 unsigned long entries = 0;
2430 u64 ts;
2431
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002432 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002433
Steven Rostedt6d158a82012-06-27 20:46:14 -04002434 buf_iter = trace_buffer_iter(iter, cpu);
2435 if (!buf_iter)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002436 return;
2437
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002438 ring_buffer_iter_reset(buf_iter);
2439
2440 /*
2441 * We could have the case with the max latency tracers
2442 * that a reset never took place on a cpu. This is evident
2443 * by the timestamp being before the start of the buffer.
2444 */
2445 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002446 if (ts >= iter->trace_buffer->time_start)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002447 break;
2448 entries++;
2449 ring_buffer_read(buf_iter, NULL);
2450 }
2451
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002452 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002453}
2454
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002455/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002456 * The current tracer is copied to avoid a global locking
2457 * all around.
2458 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002459static void *s_start(struct seq_file *m, loff_t *pos)
2460{
2461 struct trace_iterator *iter = m->private;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002462 struct trace_array *tr = iter->tr;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002463 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002464 void *p = NULL;
2465 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002466 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002467
Hiraku Toyooka2fd196e2012-12-26 11:52:52 +09002468 /*
2469 * copy the tracer to avoid using a global lock all around.
2470 * iter->trace is a copy of current_trace, the pointer to the
2471 * name may be used instead of a strcmp(), as iter->trace->name
2472 * will point to the same string as current_trace->name.
2473 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002474 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002475 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2476 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002477 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002478
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002479#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002480 if (iter->snapshot && iter->trace->use_max_tr)
2481 return ERR_PTR(-EBUSY);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002482#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002483
2484 if (!iter->snapshot)
2485 atomic_inc(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002486
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002487 if (*pos != iter->pos) {
2488 iter->ent = NULL;
2489 iter->cpu = 0;
2490 iter->idx = -1;
2491
Steven Rostedtae3b5092013-01-23 15:22:59 -05002492 if (cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002493 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002494 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002495 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002496 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002497
Lai Jiangshanac91d852010-03-02 17:54:50 +08002498 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002499 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2500 ;
2501
2502 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002503 /*
2504 * If we overflowed the seq_file before, then we want
2505 * to just reuse the trace_seq buffer again.
2506 */
2507 if (iter->leftover)
2508 p = iter;
2509 else {
2510 l = *pos - 1;
2511 p = s_next(m, p, &l);
2512 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002513 }
2514
Lai Jiangshan4f535962009-05-18 19:35:34 +08002515 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002516 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002517 return p;
2518}
2519
2520static void s_stop(struct seq_file *m, void *p)
2521{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002522 struct trace_iterator *iter = m->private;
2523
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002524#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002525 if (iter->snapshot && iter->trace->use_max_tr)
2526 return;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002527#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002528
2529 if (!iter->snapshot)
2530 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002531
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002532 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08002533 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002534}
2535
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002536static void
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002537get_total_entries(struct trace_buffer *buf,
2538 unsigned long *total, unsigned long *entries)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002539{
2540 unsigned long count;
2541 int cpu;
2542
2543 *total = 0;
2544 *entries = 0;
2545
2546 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002547 count = ring_buffer_entries_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002548 /*
2549 * If this buffer has skipped entries, then we hold all
2550 * entries for the trace and we need to ignore the
2551 * ones before the time stamp.
2552 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002553 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2554 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002555 /* total is the same as the entries */
2556 *total += count;
2557 } else
2558 *total += count +
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002559 ring_buffer_overrun_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002560 *entries += count;
2561 }
2562}
2563
Ingo Molnare309b412008-05-12 21:20:51 +02002564static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002565{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002566 seq_puts(m, "# _------=> CPU# \n"
2567 "# / _-----=> irqs-off \n"
2568 "# | / _----=> need-resched \n"
2569 "# || / _---=> hardirq/softirq \n"
2570 "# ||| / _--=> preempt-depth \n"
2571 "# |||| / delay \n"
2572 "# cmd pid ||||| time | caller \n"
2573 "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002574}
2575
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002576static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002577{
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002578 unsigned long total;
2579 unsigned long entries;
2580
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002581 get_total_entries(buf, &total, &entries);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002582 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2583 entries, total, num_online_cpus());
2584 seq_puts(m, "#\n");
2585}
2586
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002587static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002588{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002589 print_event_info(buf, m);
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002590 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"
2591 "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002592}
2593
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002594static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedt77271ce2011-11-17 09:34:33 -05002595{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002596 print_event_info(buf, m);
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002597 seq_puts(m, "# _-----=> irqs-off\n"
2598 "# / _----=> need-resched\n"
2599 "# | / _---=> hardirq/softirq\n"
2600 "# || / _--=> preempt-depth\n"
2601 "# ||| / delay\n"
2602 "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
2603 "# | | | |||| | |\n");
Steven Rostedt77271ce2011-11-17 09:34:33 -05002604}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002605
Jiri Olsa62b915f2010-04-02 19:01:22 +02002606void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002607print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2608{
2609 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002610 struct trace_buffer *buf = iter->trace_buffer;
2611 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002612 struct tracer *type = iter->trace;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002613 unsigned long entries;
2614 unsigned long total;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002615 const char *name = "preemption";
2616
Steven Rostedt (Red Hat)d840f712013-02-01 18:38:47 -05002617 name = type->name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002618
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002619 get_total_entries(buf, &total, &entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002620
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002621 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002622 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002623 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002624 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002625 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002626 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02002627 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002628 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002629 total,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002630 buf->cpu,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002631#if defined(CONFIG_PREEMPT_NONE)
2632 "server",
2633#elif defined(CONFIG_PREEMPT_VOLUNTARY)
2634 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04002635#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002636 "preempt",
2637#else
2638 "unknown",
2639#endif
2640 /* These are reserved for later use */
2641 0, 0, 0, 0);
2642#ifdef CONFIG_SMP
2643 seq_printf(m, " #P:%d)\n", num_online_cpus());
2644#else
2645 seq_puts(m, ")\n");
2646#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002647 seq_puts(m, "# -----------------\n");
2648 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002649 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
Eric W. Biedermand20b92a2012-03-13 16:02:19 -07002650 data->comm, data->pid,
2651 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002652 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002653 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002654
2655 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002656 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02002657 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2658 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002659 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02002660 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2661 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04002662 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002663 }
2664
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002665 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002666}
2667
Steven Rostedta3097202008-11-07 22:36:02 -05002668static void test_cpu_buff_start(struct trace_iterator *iter)
2669{
2670 struct trace_seq *s = &iter->seq;
2671
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002672 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2673 return;
2674
2675 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2676 return;
2677
Rusty Russell44623442009-01-01 10:12:23 +10302678 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05002679 return;
2680
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002681 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002682 return;
2683
Rusty Russell44623442009-01-01 10:12:23 +10302684 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002685
2686 /* Don't print started cpu buffer for the first entry of the trace */
2687 if (iter->idx > 1)
2688 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2689 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05002690}
2691
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002692static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002693{
Steven Rostedt214023c2008-05-12 21:20:46 +02002694 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002695 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002696 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002697 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002698
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002699 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002700
Steven Rostedta3097202008-11-07 22:36:02 -05002701 test_cpu_buff_start(iter);
2702
Steven Rostedtf633cef2008-12-23 23:24:13 -05002703 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002704
2705 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002706 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2707 trace_print_lat_context(iter);
2708 else
2709 trace_print_context(iter);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002710 }
2711
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002712 if (trace_seq_has_overflowed(s))
2713 return TRACE_TYPE_PARTIAL_LINE;
2714
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002715 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04002716 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002717
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002718 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04002719
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002720 return trace_handle_return(s);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002721}
2722
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002723static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002724{
2725 struct trace_seq *s = &iter->seq;
2726 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002727 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002728
2729 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002730
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002731 if (trace_flags & TRACE_ITER_CONTEXT_INFO)
2732 trace_seq_printf(s, "%d %d %llu ",
2733 entry->pid, iter->cpu, iter->ts);
2734
2735 if (trace_seq_has_overflowed(s))
2736 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002737
Steven Rostedtf633cef2008-12-23 23:24:13 -05002738 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002739 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04002740 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002741
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002742 trace_seq_printf(s, "%d ?\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04002743
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002744 return trace_handle_return(s);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002745}
2746
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002747static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002748{
2749 struct trace_seq *s = &iter->seq;
2750 unsigned char newline = '\n';
2751 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002752 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002753
2754 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002755
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002756 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002757 SEQ_PUT_HEX_FIELD(s, entry->pid);
2758 SEQ_PUT_HEX_FIELD(s, iter->cpu);
2759 SEQ_PUT_HEX_FIELD(s, iter->ts);
2760 if (trace_seq_has_overflowed(s))
2761 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002762 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002763
Steven Rostedtf633cef2008-12-23 23:24:13 -05002764 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002765 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04002766 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002767 if (ret != TRACE_TYPE_HANDLED)
2768 return ret;
2769 }
Steven Rostedt7104f302008-10-01 10:52:51 -04002770
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002771 SEQ_PUT_FIELD(s, newline);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002772
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002773 return trace_handle_return(s);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002774}
2775
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002776static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002777{
2778 struct trace_seq *s = &iter->seq;
2779 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002780 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002781
2782 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002783
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002784 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002785 SEQ_PUT_FIELD(s, entry->pid);
2786 SEQ_PUT_FIELD(s, iter->cpu);
2787 SEQ_PUT_FIELD(s, iter->ts);
2788 if (trace_seq_has_overflowed(s))
2789 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002790 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002791
Steven Rostedtf633cef2008-12-23 23:24:13 -05002792 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04002793 return event ? event->funcs->binary(iter, 0, event) :
2794 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002795}
2796
Jiri Olsa62b915f2010-04-02 19:01:22 +02002797int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002798{
Steven Rostedt6d158a82012-06-27 20:46:14 -04002799 struct ring_buffer_iter *buf_iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002800 int cpu;
2801
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002802 /* If we are looking at one CPU buffer, only check that one */
Steven Rostedtae3b5092013-01-23 15:22:59 -05002803 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002804 cpu = iter->cpu_file;
Steven Rostedt6d158a82012-06-27 20:46:14 -04002805 buf_iter = trace_buffer_iter(iter, cpu);
2806 if (buf_iter) {
2807 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002808 return 0;
2809 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002810 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002811 return 0;
2812 }
2813 return 1;
2814 }
2815
Steven Rostedtab464282008-05-12 21:21:00 +02002816 for_each_tracing_cpu(cpu) {
Steven Rostedt6d158a82012-06-27 20:46:14 -04002817 buf_iter = trace_buffer_iter(iter, cpu);
2818 if (buf_iter) {
2819 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedtd7690412008-10-01 00:29:53 -04002820 return 0;
2821 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002822 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedtd7690412008-10-01 00:29:53 -04002823 return 0;
2824 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002825 }
Steven Rostedtd7690412008-10-01 00:29:53 -04002826
Frederic Weisbecker797d3712008-09-30 18:13:45 +02002827 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002828}
2829
Lai Jiangshan4f535962009-05-18 19:35:34 +08002830/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05002831enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002832{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002833 enum print_line_t ret;
2834
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05002835 if (iter->lost_events) {
2836 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2837 iter->cpu, iter->lost_events);
2838 if (trace_seq_has_overflowed(&iter->seq))
2839 return TRACE_TYPE_PARTIAL_LINE;
2840 }
Steven Rostedtbc21b472010-03-31 19:49:26 -04002841
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002842 if (iter->trace && iter->trace->print_line) {
2843 ret = iter->trace->print_line(iter);
2844 if (ret != TRACE_TYPE_UNHANDLED)
2845 return ret;
2846 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002847
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -05002848 if (iter->ent->type == TRACE_BPUTS &&
2849 trace_flags & TRACE_ITER_PRINTK &&
2850 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2851 return trace_print_bputs_msg_only(iter);
2852
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002853 if (iter->ent->type == TRACE_BPRINT &&
2854 trace_flags & TRACE_ITER_PRINTK &&
2855 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002856 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002857
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002858 if (iter->ent->type == TRACE_PRINT &&
2859 trace_flags & TRACE_ITER_PRINTK &&
2860 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002861 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002862
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002863 if (trace_flags & TRACE_ITER_BIN)
2864 return print_bin_fmt(iter);
2865
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002866 if (trace_flags & TRACE_ITER_HEX)
2867 return print_hex_fmt(iter);
2868
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002869 if (trace_flags & TRACE_ITER_RAW)
2870 return print_raw_fmt(iter);
2871
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002872 return print_trace_fmt(iter);
2873}
2874
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01002875void trace_latency_header(struct seq_file *m)
2876{
2877 struct trace_iterator *iter = m->private;
2878
2879 /* print nothing if the buffers are empty */
2880 if (trace_empty(iter))
2881 return;
2882
2883 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2884 print_trace_header(m, iter);
2885
2886 if (!(trace_flags & TRACE_ITER_VERBOSE))
2887 print_lat_help_header(m);
2888}
2889
Jiri Olsa62b915f2010-04-02 19:01:22 +02002890void trace_default_header(struct seq_file *m)
2891{
2892 struct trace_iterator *iter = m->private;
2893
Jiri Olsaf56e7f82011-06-03 16:58:49 +02002894 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2895 return;
2896
Jiri Olsa62b915f2010-04-02 19:01:22 +02002897 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2898 /* print nothing if the buffers are empty */
2899 if (trace_empty(iter))
2900 return;
2901 print_trace_header(m, iter);
2902 if (!(trace_flags & TRACE_ITER_VERBOSE))
2903 print_lat_help_header(m);
2904 } else {
Steven Rostedt77271ce2011-11-17 09:34:33 -05002905 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2906 if (trace_flags & TRACE_ITER_IRQ_INFO)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002907 print_func_help_header_irq(iter->trace_buffer, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002908 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002909 print_func_help_header(iter->trace_buffer, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002910 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02002911 }
2912}
2913
Steven Rostedte0a413f2011-09-29 21:26:16 -04002914static void test_ftrace_alive(struct seq_file *m)
2915{
2916 if (!ftrace_is_dead())
2917 return;
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002918 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
2919 "# MAY BE MISSING FUNCTION EVENTS\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04002920}
2921
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002922#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002923static void show_snapshot_main_help(struct seq_file *m)
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002924{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002925 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
2926 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
2927 "# Takes a snapshot of the main buffer.\n"
2928 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
2929 "# (Doesn't have to be '2' works with any number that\n"
2930 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002931}
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002932
2933static void show_snapshot_percpu_help(struct seq_file *m)
2934{
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01002935 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002936#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002937 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
2938 "# Takes a snapshot of the main buffer for this cpu.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002939#else
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002940 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
2941 "# Must use main snapshot file to allocate.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002942#endif
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002943 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
2944 "# (Doesn't have to be '2' works with any number that\n"
2945 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002946}
2947
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002948static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2949{
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05002950 if (iter->tr->allocated_snapshot)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01002951 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002952 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01002953 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002954
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01002955 seq_puts(m, "# Snapshot commands:\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05002956 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2957 show_snapshot_main_help(m);
2958 else
2959 show_snapshot_percpu_help(m);
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002960}
2961#else
2962/* Should never be called */
2963static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2964#endif
2965
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002966static int s_show(struct seq_file *m, void *v)
2967{
2968 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002969 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002970
2971 if (iter->ent == NULL) {
2972 if (iter->tr) {
2973 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2974 seq_puts(m, "#\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04002975 test_ftrace_alive(m);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002976 }
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05002977 if (iter->snapshot && trace_empty(iter))
2978 print_snapshot_help(m, iter);
2979 else if (iter->trace && iter->trace->print_header)
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002980 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02002981 else
2982 trace_default_header(m);
2983
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002984 } else if (iter->leftover) {
2985 /*
2986 * If we filled the seq_file buffer earlier, we
2987 * want to just show it now.
2988 */
2989 ret = trace_print_seq(m, &iter->seq);
2990
2991 /* ret should this time be zero, but you never know */
2992 iter->leftover = ret;
2993
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002994 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002995 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002996 ret = trace_print_seq(m, &iter->seq);
2997 /*
2998 * If we overflow the seq_file buffer, then it will
2999 * ask us for this data again at start up.
3000 * Use that instead.
3001 * ret is 0 if seq_file write succeeded.
3002 * -1 otherwise.
3003 */
3004 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003005 }
3006
3007 return 0;
3008}
3009
Oleg Nesterov649e9c702013-07-23 17:25:54 +02003010/*
3011 * Should be used after trace_array_get(), trace_types_lock
3012 * ensures that i_cdev was already initialized.
3013 */
3014static inline int tracing_get_cpu(struct inode *inode)
3015{
3016 if (inode->i_cdev) /* See trace_create_cpu_file() */
3017 return (long)inode->i_cdev - 1;
3018 return RING_BUFFER_ALL_CPUS;
3019}
3020
James Morris88e9d342009-09-22 16:43:43 -07003021static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003022 .start = s_start,
3023 .next = s_next,
3024 .stop = s_stop,
3025 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003026};
3027
Ingo Molnare309b412008-05-12 21:20:51 +02003028static struct trace_iterator *
Oleg Nesterov6484c712013-07-23 17:26:10 +02003029__tracing_open(struct inode *inode, struct file *file, bool snapshot)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003030{
Oleg Nesterov6484c712013-07-23 17:26:10 +02003031 struct trace_array *tr = inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003032 struct trace_iterator *iter;
Jiri Olsa50e18b92012-04-25 10:23:39 +02003033 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003034
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003035 if (tracing_disabled)
3036 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02003037
Jiri Olsa50e18b92012-04-25 10:23:39 +02003038 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003039 if (!iter)
3040 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003041
Gil Fruchter72917232015-06-09 10:32:35 +03003042 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
Steven Rostedt6d158a82012-06-27 20:46:14 -04003043 GFP_KERNEL);
Dan Carpenter93574fc2012-07-11 09:35:08 +03003044 if (!iter->buffer_iter)
3045 goto release;
3046
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003047 /*
3048 * We make a copy of the current tracer to avoid concurrent
3049 * changes on it while we are reading.
3050 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003051 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003052 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003053 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003054 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003055
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003056 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003057
Li Zefan79f55992009-06-15 14:58:26 +08003058 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003059 goto fail;
3060
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003061 iter->tr = tr;
3062
3063#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003064 /* Currently only the top directory has a snapshot */
3065 if (tr->current_trace->print_max || snapshot)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003066 iter->trace_buffer = &tr->max_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003067 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003068#endif
3069 iter->trace_buffer = &tr->trace_buffer;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003070 iter->snapshot = snapshot;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003071 iter->pos = -1;
Oleg Nesterov6484c712013-07-23 17:26:10 +02003072 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003073 mutex_init(&iter->mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003074
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003075 /* Notify the tracer early; before we stop tracing. */
3076 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01003077 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003078
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003079 /* Annotate start of buffers if we had overruns */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003080 if (ring_buffer_overruns(iter->trace_buffer->buffer))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003081 iter->iter_flags |= TRACE_FILE_ANNOTATE;
3082
David Sharp8be07092012-11-13 12:18:22 -08003083 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09003084 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08003085 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3086
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003087 /* stop the trace while dumping if we are not opening "snapshot" */
3088 if (!iter->snapshot)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003089 tracing_stop_tr(tr);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003090
Steven Rostedtae3b5092013-01-23 15:22:59 -05003091 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003092 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003093 iter->buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003094 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
David Miller72c9ddf2010-04-20 15:47:11 -07003095 }
3096 ring_buffer_read_prepare_sync();
3097 for_each_tracing_cpu(cpu) {
3098 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003099 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003100 }
3101 } else {
3102 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003103 iter->buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003104 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
David Miller72c9ddf2010-04-20 15:47:11 -07003105 ring_buffer_read_prepare_sync();
3106 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003107 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003108 }
3109
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003110 mutex_unlock(&trace_types_lock);
3111
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003112 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003113
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003114 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003115 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003116 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04003117 kfree(iter->buffer_iter);
Dan Carpenter93574fc2012-07-11 09:35:08 +03003118release:
Jiri Olsa50e18b92012-04-25 10:23:39 +02003119 seq_release_private(inode, file);
3120 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003121}
3122
3123int tracing_open_generic(struct inode *inode, struct file *filp)
3124{
Steven Rostedt60a11772008-05-12 21:20:44 +02003125 if (tracing_disabled)
3126 return -ENODEV;
3127
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003128 filp->private_data = inode->i_private;
3129 return 0;
3130}
3131
Geyslan G. Bem2e864212013-10-18 21:15:54 -03003132bool tracing_is_disabled(void)
3133{
3134 return (tracing_disabled) ? true: false;
3135}
3136
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003137/*
3138 * Open and update trace_array ref count.
3139 * Must have the current trace_array passed to it.
3140 */
Steven Rostedt (Red Hat)dcc30222013-07-02 20:30:52 -04003141static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003142{
3143 struct trace_array *tr = inode->i_private;
3144
3145 if (tracing_disabled)
3146 return -ENODEV;
3147
3148 if (trace_array_get(tr) < 0)
3149 return -ENODEV;
3150
3151 filp->private_data = inode->i_private;
3152
3153 return 0;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003154}
3155
Hannes Eder4fd27352009-02-10 19:44:12 +01003156static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003157{
Oleg Nesterov6484c712013-07-23 17:26:10 +02003158 struct trace_array *tr = inode->i_private;
matt mooney907f2782010-09-27 19:04:53 -07003159 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003160 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003161 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003162
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003163 if (!(file->f_mode & FMODE_READ)) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02003164 trace_array_put(tr);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003165 return 0;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003166 }
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003167
Oleg Nesterov6484c712013-07-23 17:26:10 +02003168 /* Writes do not use seq_file */
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003169 iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003170 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05003171
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003172 for_each_tracing_cpu(cpu) {
3173 if (iter->buffer_iter[cpu])
3174 ring_buffer_read_finish(iter->buffer_iter[cpu]);
3175 }
3176
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003177 if (iter->trace && iter->trace->close)
3178 iter->trace->close(iter);
3179
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003180 if (!iter->snapshot)
3181 /* reenable tracing if it was previously enabled */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003182 tracing_start_tr(tr);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07003183
3184 __trace_array_put(tr);
3185
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003186 mutex_unlock(&trace_types_lock);
3187
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003188 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003189 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003190 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04003191 kfree(iter->buffer_iter);
Jiri Olsa50e18b92012-04-25 10:23:39 +02003192 seq_release_private(inode, file);
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003193
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003194 return 0;
3195}
3196
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003197static int tracing_release_generic_tr(struct inode *inode, struct file *file)
3198{
3199 struct trace_array *tr = inode->i_private;
3200
3201 trace_array_put(tr);
3202 return 0;
3203}
3204
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003205static int tracing_single_release_tr(struct inode *inode, struct file *file)
3206{
3207 struct trace_array *tr = inode->i_private;
3208
3209 trace_array_put(tr);
3210
3211 return single_release(inode, file);
3212}
3213
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003214static int tracing_open(struct inode *inode, struct file *file)
3215{
Oleg Nesterov6484c712013-07-23 17:26:10 +02003216 struct trace_array *tr = inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003217 struct trace_iterator *iter;
3218 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003219
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003220 if (trace_array_get(tr) < 0)
3221 return -ENODEV;
3222
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003223 /* If this file was open for write, then erase contents */
Oleg Nesterov6484c712013-07-23 17:26:10 +02003224 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
3225 int cpu = tracing_get_cpu(inode);
3226
3227 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003228 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003229 else
Oleg Nesterov6484c712013-07-23 17:26:10 +02003230 tracing_reset(&tr->trace_buffer, cpu);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003231 }
3232
3233 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02003234 iter = __tracing_open(inode, file, false);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003235 if (IS_ERR(iter))
3236 ret = PTR_ERR(iter);
3237 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
3238 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3239 }
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003240
3241 if (ret < 0)
3242 trace_array_put(tr);
3243
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003244 return ret;
3245}
3246
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003247/*
3248 * Some tracers are not suitable for instance buffers.
3249 * A tracer is always available for the global array (toplevel)
3250 * or if it explicitly states that it is.
3251 */
3252static bool
3253trace_ok_for_array(struct tracer *t, struct trace_array *tr)
3254{
3255 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
3256}
3257
3258/* Find the next tracer that this trace array may use */
3259static struct tracer *
3260get_tracer_for_array(struct trace_array *tr, struct tracer *t)
3261{
3262 while (t && !trace_ok_for_array(t, tr))
3263 t = t->next;
3264
3265 return t;
3266}
3267
Ingo Molnare309b412008-05-12 21:20:51 +02003268static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003269t_next(struct seq_file *m, void *v, loff_t *pos)
3270{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003271 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08003272 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003273
3274 (*pos)++;
3275
3276 if (t)
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003277 t = get_tracer_for_array(tr, t->next);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003278
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003279 return t;
3280}
3281
3282static void *t_start(struct seq_file *m, loff_t *pos)
3283{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003284 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08003285 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003286 loff_t l = 0;
3287
3288 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003289
3290 t = get_tracer_for_array(tr, trace_types);
3291 for (; t && l < *pos; t = t_next(m, t, &l))
3292 ;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003293
3294 return t;
3295}
3296
3297static void t_stop(struct seq_file *m, void *p)
3298{
3299 mutex_unlock(&trace_types_lock);
3300}
3301
3302static int t_show(struct seq_file *m, void *v)
3303{
3304 struct tracer *t = v;
3305
3306 if (!t)
3307 return 0;
3308
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003309 seq_puts(m, t->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003310 if (t->next)
3311 seq_putc(m, ' ');
3312 else
3313 seq_putc(m, '\n');
3314
3315 return 0;
3316}
3317
James Morris88e9d342009-09-22 16:43:43 -07003318static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003319 .start = t_start,
3320 .next = t_next,
3321 .stop = t_stop,
3322 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003323};
3324
3325static int show_traces_open(struct inode *inode, struct file *file)
3326{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003327 struct trace_array *tr = inode->i_private;
3328 struct seq_file *m;
3329 int ret;
3330
Steven Rostedt60a11772008-05-12 21:20:44 +02003331 if (tracing_disabled)
3332 return -ENODEV;
3333
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003334 ret = seq_open(file, &show_traces_seq_ops);
3335 if (ret)
3336 return ret;
3337
3338 m = file->private_data;
3339 m->private = tr;
3340
3341 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003342}
3343
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003344static ssize_t
3345tracing_write_stub(struct file *filp, const char __user *ubuf,
3346 size_t count, loff_t *ppos)
3347{
3348 return count;
3349}
3350
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003351loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
Slava Pestov364829b2010-11-24 15:13:16 -08003352{
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003353 int ret;
3354
Slava Pestov364829b2010-11-24 15:13:16 -08003355 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003356 ret = seq_lseek(file, offset, whence);
Slava Pestov364829b2010-11-24 15:13:16 -08003357 else
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003358 file->f_pos = ret = 0;
3359
3360 return ret;
Slava Pestov364829b2010-11-24 15:13:16 -08003361}
3362
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003363static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003364 .open = tracing_open,
3365 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003366 .write = tracing_write_stub,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003367 .llseek = tracing_lseek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003368 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003369};
3370
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003371static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02003372 .open = show_traces_open,
3373 .read = seq_read,
3374 .release = seq_release,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003375 .llseek = seq_lseek,
Ingo Molnarc7078de2008-05-12 21:20:52 +02003376};
3377
Ingo Molnar36dfe922008-05-12 21:20:52 +02003378/*
Ingo Molnar36dfe922008-05-12 21:20:52 +02003379 * The tracer itself will not take this lock, but still we want
3380 * to provide a consistent cpumask to user-space:
3381 */
3382static DEFINE_MUTEX(tracing_cpumask_update_lock);
3383
3384/*
3385 * Temporary storage for the character representation of the
3386 * CPU bitmask (and one more byte for the newline):
3387 */
3388static char mask_str[NR_CPUS + 1];
3389
Ingo Molnarc7078de2008-05-12 21:20:52 +02003390static ssize_t
3391tracing_cpumask_read(struct file *filp, char __user *ubuf,
3392 size_t count, loff_t *ppos)
3393{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003394 struct trace_array *tr = file_inode(filp)->i_private;
Ingo Molnar36dfe922008-05-12 21:20:52 +02003395 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003396
3397 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003398
Tejun Heo1a402432015-02-13 14:37:39 -08003399 len = snprintf(mask_str, count, "%*pb\n",
3400 cpumask_pr_args(tr->tracing_cpumask));
3401 if (len >= count) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02003402 count = -EINVAL;
3403 goto out_err;
3404 }
Ingo Molnar36dfe922008-05-12 21:20:52 +02003405 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3406
3407out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02003408 mutex_unlock(&tracing_cpumask_update_lock);
3409
3410 return count;
3411}
3412
3413static ssize_t
3414tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3415 size_t count, loff_t *ppos)
3416{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003417 struct trace_array *tr = file_inode(filp)->i_private;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303418 cpumask_var_t tracing_cpumask_new;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003419 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303420
3421 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3422 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003423
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303424 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003425 if (err)
3426 goto err_unlock;
3427
Li Zefan215368e2009-06-15 10:56:42 +08003428 mutex_lock(&tracing_cpumask_update_lock);
3429
Steven Rostedta5e25882008-12-02 15:34:05 -05003430 local_irq_disable();
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05003431 arch_spin_lock(&tr->max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02003432 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02003433 /*
3434 * Increase/decrease the disabled counter if we are
3435 * about to flip a bit in the cpumask:
3436 */
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003437 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303438 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003439 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3440 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003441 }
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003442 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303443 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003444 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3445 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003446 }
3447 }
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05003448 arch_spin_unlock(&tr->max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05003449 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02003450
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003451 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003452
Ingo Molnarc7078de2008-05-12 21:20:52 +02003453 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303454 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02003455
Ingo Molnarc7078de2008-05-12 21:20:52 +02003456 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02003457
3458err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08003459 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003460
3461 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003462}
3463
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003464static const struct file_operations tracing_cpumask_fops = {
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003465 .open = tracing_open_generic_tr,
Ingo Molnarc7078de2008-05-12 21:20:52 +02003466 .read = tracing_cpumask_read,
3467 .write = tracing_cpumask_write,
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003468 .release = tracing_release_generic_tr,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003469 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003470};
3471
Li Zefanfdb372e2009-12-08 11:15:59 +08003472static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003473{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003474 struct tracer_opt *trace_opts;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003475 struct trace_array *tr = m->private;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003476 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003477 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003478
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003479 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003480 tracer_flags = tr->current_trace->flags->val;
3481 trace_opts = tr->current_trace->flags->opts;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003482
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003483 for (i = 0; trace_options[i]; i++) {
3484 if (trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08003485 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003486 else
Li Zefanfdb372e2009-12-08 11:15:59 +08003487 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003488 }
3489
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003490 for (i = 0; trace_opts[i].name; i++) {
3491 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08003492 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003493 else
Li Zefanfdb372e2009-12-08 11:15:59 +08003494 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003495 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003496 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003497
Li Zefanfdb372e2009-12-08 11:15:59 +08003498 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003499}
3500
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003501static int __set_tracer_option(struct trace_array *tr,
Li Zefan8d18eaa2009-12-08 11:17:06 +08003502 struct tracer_flags *tracer_flags,
3503 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003504{
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003505 struct tracer *trace = tr->current_trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08003506 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003507
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003508 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003509 if (ret)
3510 return ret;
3511
3512 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08003513 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003514 else
Zhaolei77708412009-08-07 18:53:21 +08003515 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003516 return 0;
3517}
3518
Li Zefan8d18eaa2009-12-08 11:17:06 +08003519/* Try to assign a tracer specific option */
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003520static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
Li Zefan8d18eaa2009-12-08 11:17:06 +08003521{
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003522 struct tracer *trace = tr->current_trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08003523 struct tracer_flags *tracer_flags = trace->flags;
3524 struct tracer_opt *opts = NULL;
3525 int i;
3526
3527 for (i = 0; tracer_flags->opts[i].name; i++) {
3528 opts = &tracer_flags->opts[i];
3529
3530 if (strcmp(cmp, opts->name) == 0)
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003531 return __set_tracer_option(tr, trace->flags, opts, neg);
Li Zefan8d18eaa2009-12-08 11:17:06 +08003532 }
3533
3534 return -EINVAL;
3535}
3536
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003537/* Some tracers require overwrite to stay enabled */
3538int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3539{
3540 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3541 return -1;
3542
3543 return 0;
3544}
3545
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003546int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003547{
3548 /* do nothing if flag is already set */
3549 if (!!(trace_flags & mask) == !!enabled)
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003550 return 0;
3551
3552 /* Give the tracer a chance to approve the change */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003553 if (tr->current_trace->flag_changed)
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -05003554 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003555 return -EINVAL;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003556
3557 if (enabled)
3558 trace_flags |= mask;
3559 else
3560 trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08003561
3562 if (mask == TRACE_ITER_RECORD_CMD)
3563 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08003564
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003565 if (mask == TRACE_ITER_OVERWRITE) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003566 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003567#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003568 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003569#endif
3570 }
Steven Rostedt81698832012-10-11 10:15:05 -04003571
3572 if (mask == TRACE_ITER_PRINTK)
3573 trace_printk_start_stop_comm(enabled);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003574
3575 return 0;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003576}
3577
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003578static int trace_set_options(struct trace_array *tr, char *option)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003579{
Li Zefan8d18eaa2009-12-08 11:17:06 +08003580 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003581 int neg = 0;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003582 int ret = -ENODEV;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003583 int i;
3584
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003585 cmp = strstrip(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003586
Li Zefan8d18eaa2009-12-08 11:17:06 +08003587 if (strncmp(cmp, "no", 2) == 0) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003588 neg = 1;
3589 cmp += 2;
3590 }
3591
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003592 mutex_lock(&trace_types_lock);
3593
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003594 for (i = 0; trace_options[i]; i++) {
Li Zefan8d18eaa2009-12-08 11:17:06 +08003595 if (strcmp(cmp, trace_options[i]) == 0) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003596 ret = set_tracer_flag(tr, 1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003597 break;
3598 }
3599 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003600
3601 /* If no option could be set, test the specific tracer options */
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003602 if (!trace_options[i])
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003603 ret = set_tracer_option(tr, cmp, neg);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003604
3605 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003606
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003607 return ret;
3608}
3609
3610static ssize_t
3611tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3612 size_t cnt, loff_t *ppos)
3613{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003614 struct seq_file *m = filp->private_data;
3615 struct trace_array *tr = m->private;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003616 char buf[64];
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003617 int ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003618
3619 if (cnt >= sizeof(buf))
3620 return -EINVAL;
3621
3622 if (copy_from_user(&buf, ubuf, cnt))
3623 return -EFAULT;
3624
Steven Rostedta8dd2172013-01-09 20:54:17 -05003625 buf[cnt] = 0;
3626
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003627 ret = trace_set_options(tr, buf);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003628 if (ret < 0)
3629 return ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003630
Jiri Olsacf8517c2009-10-23 19:36:16 -04003631 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003632
3633 return cnt;
3634}
3635
Li Zefanfdb372e2009-12-08 11:15:59 +08003636static int tracing_trace_options_open(struct inode *inode, struct file *file)
3637{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003638 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07003639 int ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003640
Li Zefanfdb372e2009-12-08 11:15:59 +08003641 if (tracing_disabled)
3642 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003643
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003644 if (trace_array_get(tr) < 0)
3645 return -ENODEV;
3646
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07003647 ret = single_open(file, tracing_trace_options_show, inode->i_private);
3648 if (ret < 0)
3649 trace_array_put(tr);
3650
3651 return ret;
Li Zefanfdb372e2009-12-08 11:15:59 +08003652}
3653
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003654static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08003655 .open = tracing_trace_options_open,
3656 .read = seq_read,
3657 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003658 .release = tracing_single_release_tr,
Steven Rostedtee6bce52008-11-12 17:52:37 -05003659 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003660};
3661
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003662static const char readme_msg[] =
3663 "tracing mini-HOWTO:\n\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003664 "# echo 0 > tracing_on : quick way to disable tracing\n"
3665 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3666 " Important files:\n"
3667 " trace\t\t\t- The static contents of the buffer\n"
3668 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3669 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3670 " current_tracer\t- function and latency tracers\n"
3671 " available_tracers\t- list of configured tracers for current_tracer\n"
3672 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3673 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3674 " trace_clock\t\t-change the clock used to order events\n"
3675 " local: Per cpu clock but may not be synced across CPUs\n"
3676 " global: Synced across CPUs but slows tracing down.\n"
3677 " counter: Not a clock, but just an increment\n"
3678 " uptime: Jiffy counter from time of boot\n"
3679 " perf: Same clock that perf events use\n"
3680#ifdef CONFIG_X86_64
3681 " x86-tsc: TSC cycle counter\n"
3682#endif
3683 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3684 " tracing_cpumask\t- Limit which CPUs to trace\n"
3685 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3686 "\t\t\t Remove sub-buffer with rmdir\n"
3687 " trace_options\t\t- Set format or modify how tracing happens\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003688 "\t\t\t Disable an option by adding a suffix 'no' to the\n"
3689 "\t\t\t option name\n"
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09003690 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003691#ifdef CONFIG_DYNAMIC_FTRACE
3692 "\n available_filter_functions - list of functions that can be filtered on\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003693 " set_ftrace_filter\t- echo function name in here to only trace these\n"
3694 "\t\t\t functions\n"
3695 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3696 "\t modules: Can select a group via module\n"
3697 "\t Format: :mod:<module-name>\n"
3698 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
3699 "\t triggers: a command to perform when function is hit\n"
3700 "\t Format: <function>:<trigger>[:count]\n"
3701 "\t trigger: traceon, traceoff\n"
3702 "\t\t enable_event:<system>:<event>\n"
3703 "\t\t disable_event:<system>:<event>\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003704#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003705 "\t\t stacktrace\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003706#endif
3707#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003708 "\t\t snapshot\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003709#endif
Steven Rostedt (Red Hat)17a280e2014-04-10 22:43:37 -04003710 "\t\t dump\n"
3711 "\t\t cpudump\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003712 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
3713 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
3714 "\t The first one will disable tracing every time do_fault is hit\n"
3715 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
3716 "\t The first time do trap is hit and it disables tracing, the\n"
3717 "\t counter will decrement to 2. If tracing is already disabled,\n"
3718 "\t the counter will not decrement. It only decrements when the\n"
3719 "\t trigger did work\n"
3720 "\t To remove trigger without count:\n"
3721 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
3722 "\t To remove trigger with a count:\n"
3723 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003724 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003725 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3726 "\t modules: Can select a group via module command :mod:\n"
3727 "\t Does not accept triggers\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003728#endif /* CONFIG_DYNAMIC_FTRACE */
3729#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003730 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
3731 "\t\t (function)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003732#endif
3733#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3734 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
Namhyung Kimd048a8c72014-06-13 01:23:53 +09003735 " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003736 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3737#endif
3738#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003739 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
3740 "\t\t\t snapshot buffer. Read the contents for more\n"
3741 "\t\t\t information\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003742#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08003743#ifdef CONFIG_STACK_TRACER
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003744 " stack_trace\t\t- Shows the max stack trace when active\n"
3745 " stack_max_size\t- Shows current max stack size that was traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003746 "\t\t\t Write into this file to reset the max size (trigger a\n"
3747 "\t\t\t new trace)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003748#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003749 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
3750 "\t\t\t traces\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04003751#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08003752#endif /* CONFIG_STACK_TRACER */
Tom Zanussi26f25562014-01-17 15:11:44 -06003753 " events/\t\t- Directory containing all trace event subsystems:\n"
3754 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
3755 " events/<system>/\t- Directory containing all trace events for <system>:\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003756 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
3757 "\t\t\t events\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06003758 " filter\t\t- If set, only events passing filter are traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003759 " events/<system>/<event>/\t- Directory containing control files for\n"
3760 "\t\t\t <event>:\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06003761 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
3762 " filter\t\t- If set, only events passing filter are traced\n"
3763 " trigger\t\t- If set, a command to perform when event is hit\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003764 "\t Format: <trigger>[:count][if <filter>]\n"
3765 "\t trigger: traceon, traceoff\n"
3766 "\t enable_event:<system>:<event>\n"
3767 "\t disable_event:<system>:<event>\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06003768#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003769 "\t\t stacktrace\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06003770#endif
3771#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003772 "\t\t snapshot\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06003773#endif
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05003774 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
3775 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
3776 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
3777 "\t events/block/block_unplug/trigger\n"
3778 "\t The first disables tracing every time block_unplug is hit.\n"
3779 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
3780 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
3781 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
3782 "\t Like function triggers, the counter is only decremented if it\n"
3783 "\t enabled or disabled tracing.\n"
3784 "\t To remove a trigger without a count:\n"
3785 "\t echo '!<trigger> > <system>/<event>/trigger\n"
3786 "\t To remove a trigger with a count:\n"
3787 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
3788 "\t Filters can be ignored when removing a trigger.\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003789;
3790
3791static ssize_t
3792tracing_readme_read(struct file *filp, char __user *ubuf,
3793 size_t cnt, loff_t *ppos)
3794{
3795 return simple_read_from_buffer(ubuf, cnt, ppos,
3796 readme_msg, strlen(readme_msg));
3797}
3798
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003799static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02003800 .open = tracing_open_generic,
3801 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003802 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003803};
3804
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003805static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
Avadh Patel69abe6a2009-04-10 16:04:48 -04003806{
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003807 unsigned int *ptr = v;
Avadh Patel69abe6a2009-04-10 16:04:48 -04003808
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003809 if (*pos || m->count)
3810 ptr++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04003811
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003812 (*pos)++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04003813
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09003814 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
3815 ptr++) {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003816 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
Avadh Patel69abe6a2009-04-10 16:04:48 -04003817 continue;
3818
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003819 return ptr;
Avadh Patel69abe6a2009-04-10 16:04:48 -04003820 }
3821
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003822 return NULL;
3823}
Avadh Patel69abe6a2009-04-10 16:04:48 -04003824
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003825static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
3826{
3827 void *v;
3828 loff_t l = 0;
Avadh Patel69abe6a2009-04-10 16:04:48 -04003829
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04003830 preempt_disable();
3831 arch_spin_lock(&trace_cmdline_lock);
3832
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09003833 v = &savedcmd->map_cmdline_to_pid[0];
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003834 while (l <= *pos) {
3835 v = saved_cmdlines_next(m, v, &l);
3836 if (!v)
3837 return NULL;
3838 }
3839
3840 return v;
3841}
3842
3843static void saved_cmdlines_stop(struct seq_file *m, void *v)
3844{
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04003845 arch_spin_unlock(&trace_cmdline_lock);
3846 preempt_enable();
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003847}
3848
3849static int saved_cmdlines_show(struct seq_file *m, void *v)
3850{
3851 char buf[TASK_COMM_LEN];
3852 unsigned int *pid = v;
3853
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04003854 __trace_find_cmdline(*pid, buf);
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003855 seq_printf(m, "%d %s\n", *pid, buf);
3856 return 0;
3857}
3858
3859static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
3860 .start = saved_cmdlines_start,
3861 .next = saved_cmdlines_next,
3862 .stop = saved_cmdlines_stop,
3863 .show = saved_cmdlines_show,
3864};
3865
3866static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
3867{
3868 if (tracing_disabled)
3869 return -ENODEV;
3870
3871 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
Avadh Patel69abe6a2009-04-10 16:04:48 -04003872}
3873
3874static const struct file_operations tracing_saved_cmdlines_fops = {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09003875 .open = tracing_saved_cmdlines_open,
3876 .read = seq_read,
3877 .llseek = seq_lseek,
3878 .release = seq_release,
Avadh Patel69abe6a2009-04-10 16:04:48 -04003879};
3880
3881static ssize_t
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09003882tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
3883 size_t cnt, loff_t *ppos)
3884{
3885 char buf[64];
3886 int r;
3887
3888 arch_spin_lock(&trace_cmdline_lock);
Namhyung Kima6af8fb2014-06-10 16:11:35 +09003889 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09003890 arch_spin_unlock(&trace_cmdline_lock);
3891
3892 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3893}
3894
3895static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
3896{
3897 kfree(s->saved_cmdlines);
3898 kfree(s->map_cmdline_to_pid);
3899 kfree(s);
3900}
3901
3902static int tracing_resize_saved_cmdlines(unsigned int val)
3903{
3904 struct saved_cmdlines_buffer *s, *savedcmd_temp;
3905
Namhyung Kima6af8fb2014-06-10 16:11:35 +09003906 s = kmalloc(sizeof(*s), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09003907 if (!s)
3908 return -ENOMEM;
3909
3910 if (allocate_cmdlines_buffer(val, s) < 0) {
3911 kfree(s);
3912 return -ENOMEM;
3913 }
3914
3915 arch_spin_lock(&trace_cmdline_lock);
3916 savedcmd_temp = savedcmd;
3917 savedcmd = s;
3918 arch_spin_unlock(&trace_cmdline_lock);
3919 free_saved_cmdlines_buffer(savedcmd_temp);
3920
3921 return 0;
3922}
3923
3924static ssize_t
3925tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
3926 size_t cnt, loff_t *ppos)
3927{
3928 unsigned long val;
3929 int ret;
3930
3931 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3932 if (ret)
3933 return ret;
3934
3935 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
3936 if (!val || val > PID_MAX_DEFAULT)
3937 return -EINVAL;
3938
3939 ret = tracing_resize_saved_cmdlines((unsigned int)val);
3940 if (ret < 0)
3941 return ret;
3942
3943 *ppos += cnt;
3944
3945 return cnt;
3946}
3947
3948static const struct file_operations tracing_saved_cmdlines_size_fops = {
3949 .open = tracing_open_generic,
3950 .read = tracing_saved_cmdlines_size_read,
3951 .write = tracing_saved_cmdlines_size_write,
3952};
3953
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04003954#ifdef CONFIG_TRACE_ENUM_MAP_FILE
3955static union trace_enum_map_item *
3956update_enum_map(union trace_enum_map_item *ptr)
3957{
3958 if (!ptr->map.enum_string) {
3959 if (ptr->tail.next) {
3960 ptr = ptr->tail.next;
3961 /* Set ptr to the next real item (skip head) */
3962 ptr++;
3963 } else
3964 return NULL;
3965 }
3966 return ptr;
3967}
3968
3969static void *enum_map_next(struct seq_file *m, void *v, loff_t *pos)
3970{
3971 union trace_enum_map_item *ptr = v;
3972
3973 /*
3974 * Paranoid! If ptr points to end, we don't want to increment past it.
3975 * This really should never happen.
3976 */
3977 ptr = update_enum_map(ptr);
3978 if (WARN_ON_ONCE(!ptr))
3979 return NULL;
3980
3981 ptr++;
3982
3983 (*pos)++;
3984
3985 ptr = update_enum_map(ptr);
3986
3987 return ptr;
3988}
3989
3990static void *enum_map_start(struct seq_file *m, loff_t *pos)
3991{
3992 union trace_enum_map_item *v;
3993 loff_t l = 0;
3994
3995 mutex_lock(&trace_enum_mutex);
3996
3997 v = trace_enum_maps;
3998 if (v)
3999 v++;
4000
4001 while (v && l < *pos) {
4002 v = enum_map_next(m, v, &l);
4003 }
4004
4005 return v;
4006}
4007
4008static void enum_map_stop(struct seq_file *m, void *v)
4009{
4010 mutex_unlock(&trace_enum_mutex);
4011}
4012
4013static int enum_map_show(struct seq_file *m, void *v)
4014{
4015 union trace_enum_map_item *ptr = v;
4016
4017 seq_printf(m, "%s %ld (%s)\n",
4018 ptr->map.enum_string, ptr->map.enum_value,
4019 ptr->map.system);
4020
4021 return 0;
4022}
4023
4024static const struct seq_operations tracing_enum_map_seq_ops = {
4025 .start = enum_map_start,
4026 .next = enum_map_next,
4027 .stop = enum_map_stop,
4028 .show = enum_map_show,
4029};
4030
4031static int tracing_enum_map_open(struct inode *inode, struct file *filp)
4032{
4033 if (tracing_disabled)
4034 return -ENODEV;
4035
4036 return seq_open(filp, &tracing_enum_map_seq_ops);
4037}
4038
4039static const struct file_operations tracing_enum_map_fops = {
4040 .open = tracing_enum_map_open,
4041 .read = seq_read,
4042 .llseek = seq_lseek,
4043 .release = seq_release,
4044};
4045
4046static inline union trace_enum_map_item *
4047trace_enum_jmp_to_tail(union trace_enum_map_item *ptr)
4048{
4049 /* Return tail of array given the head */
4050 return ptr + ptr->head.length + 1;
4051}
4052
4053static void
4054trace_insert_enum_map_file(struct module *mod, struct trace_enum_map **start,
4055 int len)
4056{
4057 struct trace_enum_map **stop;
4058 struct trace_enum_map **map;
4059 union trace_enum_map_item *map_array;
4060 union trace_enum_map_item *ptr;
4061
4062 stop = start + len;
4063
4064 /*
4065 * The trace_enum_maps contains the map plus a head and tail item,
4066 * where the head holds the module and length of array, and the
4067 * tail holds a pointer to the next list.
4068 */
4069 map_array = kmalloc(sizeof(*map_array) * (len + 2), GFP_KERNEL);
4070 if (!map_array) {
4071 pr_warning("Unable to allocate trace enum mapping\n");
4072 return;
4073 }
4074
4075 mutex_lock(&trace_enum_mutex);
4076
4077 if (!trace_enum_maps)
4078 trace_enum_maps = map_array;
4079 else {
4080 ptr = trace_enum_maps;
4081 for (;;) {
4082 ptr = trace_enum_jmp_to_tail(ptr);
4083 if (!ptr->tail.next)
4084 break;
4085 ptr = ptr->tail.next;
4086
4087 }
4088 ptr->tail.next = map_array;
4089 }
4090 map_array->head.mod = mod;
4091 map_array->head.length = len;
4092 map_array++;
4093
4094 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
4095 map_array->map = **map;
4096 map_array++;
4097 }
4098 memset(map_array, 0, sizeof(*map_array));
4099
4100 mutex_unlock(&trace_enum_mutex);
4101}
4102
4103static void trace_create_enum_file(struct dentry *d_tracer)
4104{
4105 trace_create_file("enum_map", 0444, d_tracer,
4106 NULL, &tracing_enum_map_fops);
4107}
4108
4109#else /* CONFIG_TRACE_ENUM_MAP_FILE */
4110static inline void trace_create_enum_file(struct dentry *d_tracer) { }
4111static inline void trace_insert_enum_map_file(struct module *mod,
4112 struct trace_enum_map **start, int len) { }
4113#endif /* !CONFIG_TRACE_ENUM_MAP_FILE */
4114
4115static void trace_insert_enum_map(struct module *mod,
4116 struct trace_enum_map **start, int len)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04004117{
4118 struct trace_enum_map **map;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04004119
4120 if (len <= 0)
4121 return;
4122
4123 map = start;
4124
4125 trace_event_enum_update(map, len);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004126
4127 trace_insert_enum_map_file(mod, start, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04004128}
4129
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004130static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004131tracing_set_trace_read(struct file *filp, char __user *ubuf,
4132 size_t cnt, loff_t *ppos)
4133{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004134 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08004135 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004136 int r;
4137
4138 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004139 r = sprintf(buf, "%s\n", tr->current_trace->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004140 mutex_unlock(&trace_types_lock);
4141
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004142 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004143}
4144
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02004145int tracer_init(struct tracer *t, struct trace_array *tr)
4146{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004147 tracing_reset_online_cpus(&tr->trace_buffer);
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02004148 return t->init(tr);
4149}
4150
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004151static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004152{
4153 int cpu;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05004154
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004155 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004156 per_cpu_ptr(buf->data, cpu)->entries = val;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004157}
4158
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004159#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookad60da502012-10-17 11:56:16 +09004160/* resize @tr's buffer to the size of @size_tr's entries */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004161static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
4162 struct trace_buffer *size_buf, int cpu_id)
Hiraku Toyookad60da502012-10-17 11:56:16 +09004163{
4164 int cpu, ret = 0;
4165
4166 if (cpu_id == RING_BUFFER_ALL_CPUS) {
4167 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004168 ret = ring_buffer_resize(trace_buf->buffer,
4169 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
Hiraku Toyookad60da502012-10-17 11:56:16 +09004170 if (ret < 0)
4171 break;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004172 per_cpu_ptr(trace_buf->data, cpu)->entries =
4173 per_cpu_ptr(size_buf->data, cpu)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09004174 }
4175 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004176 ret = ring_buffer_resize(trace_buf->buffer,
4177 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
Hiraku Toyookad60da502012-10-17 11:56:16 +09004178 if (ret == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004179 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
4180 per_cpu_ptr(size_buf->data, cpu_id)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09004181 }
4182
4183 return ret;
4184}
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004185#endif /* CONFIG_TRACER_MAX_TRACE */
Hiraku Toyookad60da502012-10-17 11:56:16 +09004186
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004187static int __tracing_resize_ring_buffer(struct trace_array *tr,
4188 unsigned long size, int cpu)
Steven Rostedt73c51622009-03-11 13:42:01 -04004189{
4190 int ret;
4191
4192 /*
4193 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04004194 * we use the size that was given, and we can forget about
4195 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04004196 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05004197 ring_buffer_expanded = true;
Steven Rostedt73c51622009-03-11 13:42:01 -04004198
Steven Rostedtb382ede62012-10-10 21:44:34 -04004199 /* May be called before buffers are initialized */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004200 if (!tr->trace_buffer.buffer)
Steven Rostedtb382ede62012-10-10 21:44:34 -04004201 return 0;
4202
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004203 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04004204 if (ret < 0)
4205 return ret;
4206
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004207#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004208 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
4209 !tr->current_trace->use_max_tr)
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004210 goto out;
4211
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004212 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04004213 if (ret < 0) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004214 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
4215 &tr->trace_buffer, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04004216 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04004217 /*
4218 * AARGH! We are left with different
4219 * size max buffer!!!!
4220 * The max buffer is our "snapshot" buffer.
4221 * When a tracer needs a snapshot (one of the
4222 * latency tracers), it swaps the max buffer
4223 * with the saved snap shot. We succeeded to
4224 * update the size of the main buffer, but failed to
4225 * update the size of the max buffer. But when we tried
4226 * to reset the main buffer to the original size, we
4227 * failed there too. This is very unlikely to
4228 * happen, but if it does, warn and kill all
4229 * tracing.
4230 */
Steven Rostedt73c51622009-03-11 13:42:01 -04004231 WARN_ON(1);
4232 tracing_disabled = 1;
4233 }
4234 return ret;
4235 }
4236
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004237 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004238 set_buffer_entries(&tr->max_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004239 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004240 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004241
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004242 out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004243#endif /* CONFIG_TRACER_MAX_TRACE */
4244
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004245 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004246 set_buffer_entries(&tr->trace_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004247 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004248 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
Steven Rostedt73c51622009-03-11 13:42:01 -04004249
4250 return ret;
4251}
4252
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004253static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
4254 unsigned long size, int cpu_id)
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004255{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004256 int ret = size;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004257
4258 mutex_lock(&trace_types_lock);
4259
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004260 if (cpu_id != RING_BUFFER_ALL_CPUS) {
4261 /* make sure, this cpu is enabled in the mask */
4262 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
4263 ret = -EINVAL;
4264 goto out;
4265 }
4266 }
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004267
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004268 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004269 if (ret < 0)
4270 ret = -ENOMEM;
4271
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004272out:
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004273 mutex_unlock(&trace_types_lock);
4274
4275 return ret;
4276}
4277
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004278
Steven Rostedt1852fcc2009-03-11 14:33:00 -04004279/**
4280 * tracing_update_buffers - used by tracing facility to expand ring buffers
4281 *
4282 * To save on memory when the tracing is never used on a system with it
4283 * configured in. The ring buffers are set to a minimum size. But once
4284 * a user starts to use the tracing facility, then they need to grow
4285 * to their default size.
4286 *
4287 * This function is to be called when a tracer is about to be used.
4288 */
4289int tracing_update_buffers(void)
4290{
4291 int ret = 0;
4292
Steven Rostedt1027fcb2009-03-12 11:33:20 -04004293 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04004294 if (!ring_buffer_expanded)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004295 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004296 RING_BUFFER_ALL_CPUS);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04004297 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04004298
4299 return ret;
4300}
4301
Steven Rostedt577b7852009-02-26 23:43:05 -05004302struct trace_option_dentry;
4303
4304static struct trace_option_dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004305create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
Steven Rostedt577b7852009-02-26 23:43:05 -05004306
4307static void
4308destroy_trace_option_files(struct trace_option_dentry *topts);
4309
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05004310/*
4311 * Used to clear out the tracer before deletion of an instance.
4312 * Must have trace_types_lock held.
4313 */
4314static void tracing_set_nop(struct trace_array *tr)
4315{
4316 if (tr->current_trace == &nop_trace)
4317 return;
4318
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05004319 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05004320
4321 if (tr->current_trace->reset)
4322 tr->current_trace->reset(tr);
4323
4324 tr->current_trace = &nop_trace;
4325}
4326
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05004327static void update_tracer_options(struct trace_array *tr, struct tracer *t)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004328{
Steven Rostedt577b7852009-02-26 23:43:05 -05004329 static struct trace_option_dentry *topts;
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05004330
4331 /* Only enable if the directory has been created already. */
4332 if (!tr->dir)
4333 return;
4334
4335 /* Currently, only the top instance has options */
4336 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL))
4337 return;
4338
4339 destroy_trace_option_files(topts);
4340 topts = create_trace_option_files(tr, t);
4341}
4342
4343static int tracing_set_tracer(struct trace_array *tr, const char *buf)
4344{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004345 struct tracer *t;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004346#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05004347 bool had_max_tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004348#endif
Peter Zijlstrad9e54072008-11-01 19:57:37 +01004349 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004350
Steven Rostedt1027fcb2009-03-12 11:33:20 -04004351 mutex_lock(&trace_types_lock);
4352
Steven Rostedt73c51622009-03-11 13:42:01 -04004353 if (!ring_buffer_expanded) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004354 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004355 RING_BUFFER_ALL_CPUS);
Steven Rostedt73c51622009-03-11 13:42:01 -04004356 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01004357 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04004358 ret = 0;
4359 }
4360
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004361 for (t = trace_types; t; t = t->next) {
4362 if (strcmp(t->name, buf) == 0)
4363 break;
4364 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02004365 if (!t) {
4366 ret = -EINVAL;
4367 goto out;
4368 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004369 if (t == tr->current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004370 goto out;
4371
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004372 /* Some tracers are only allowed for the top level buffer */
4373 if (!trace_ok_for_array(t, tr)) {
4374 ret = -EINVAL;
4375 goto out;
4376 }
4377
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05004378 /* If trace pipe files are being read, we can't change the tracer */
4379 if (tr->current_trace->ref) {
4380 ret = -EBUSY;
4381 goto out;
4382 }
4383
Steven Rostedt9f029e82008-11-12 15:24:24 -05004384 trace_branch_disable();
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004385
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05004386 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004387
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004388 if (tr->current_trace->reset)
4389 tr->current_trace->reset(tr);
Steven Rostedt34600f02013-01-22 13:35:11 -05004390
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004391 /* Current trace needs to be nop_trace before synchronize_sched */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004392 tr->current_trace = &nop_trace;
Steven Rostedt34600f02013-01-22 13:35:11 -05004393
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05004394#ifdef CONFIG_TRACER_MAX_TRACE
4395 had_max_tr = tr->allocated_snapshot;
Steven Rostedt34600f02013-01-22 13:35:11 -05004396
4397 if (had_max_tr && !t->use_max_tr) {
4398 /*
4399 * We need to make sure that the update_max_tr sees that
4400 * current_trace changed to nop_trace to keep it from
4401 * swapping the buffers after we resize it.
4402 * The update_max_tr is called from interrupts disabled
4403 * so a synchronized_sched() is sufficient.
4404 */
4405 synchronize_sched();
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04004406 free_snapshot(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004407 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004408#endif
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05004409 update_tracer_options(tr, t);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004410
4411#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05004412 if (t->use_max_tr && !had_max_tr) {
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04004413 ret = alloc_snapshot(tr);
Hiraku Toyookad60da502012-10-17 11:56:16 +09004414 if (ret < 0)
4415 goto out;
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004416 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004417#endif
Steven Rostedt577b7852009-02-26 23:43:05 -05004418
Frederic Weisbecker1c800252008-11-16 05:57:26 +01004419 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02004420 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01004421 if (ret)
4422 goto out;
4423 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004424
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004425 tr->current_trace = t;
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05004426 tr->current_trace->enabled++;
Steven Rostedt9f029e82008-11-12 15:24:24 -05004427 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004428 out:
4429 mutex_unlock(&trace_types_lock);
4430
Peter Zijlstrad9e54072008-11-01 19:57:37 +01004431 return ret;
4432}
4433
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004434static ssize_t
4435tracing_set_trace_write(struct file *filp, const char __user *ubuf,
4436 size_t cnt, loff_t *ppos)
4437{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004438 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08004439 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004440 int i;
4441 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01004442 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004443
Steven Rostedt60063a62008-10-28 10:44:24 -04004444 ret = cnt;
4445
Li Zefanee6c2c12009-09-18 14:06:47 +08004446 if (cnt > MAX_TRACER_SIZE)
4447 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004448
4449 if (copy_from_user(&buf, ubuf, cnt))
4450 return -EFAULT;
4451
4452 buf[cnt] = 0;
4453
4454 /* strip ending whitespace. */
4455 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
4456 buf[i] = 0;
4457
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004458 err = tracing_set_tracer(tr, buf);
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01004459 if (err)
4460 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004461
Jiri Olsacf8517c2009-10-23 19:36:16 -04004462 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004463
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02004464 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004465}
4466
4467static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04004468tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
4469 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004470{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004471 char buf[64];
4472 int r;
4473
Steven Rostedtcffae432008-05-12 21:21:00 +02004474 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004475 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02004476 if (r > sizeof(buf))
4477 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004478 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004479}
4480
4481static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04004482tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
4483 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004484{
Hannes Eder5e398412009-02-10 19:44:34 +01004485 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02004486 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004487
Peter Huewe22fe9b52011-06-07 21:58:27 +02004488 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4489 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02004490 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004491
4492 *ptr = val * 1000;
4493
4494 return cnt;
4495}
4496
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04004497static ssize_t
4498tracing_thresh_read(struct file *filp, char __user *ubuf,
4499 size_t cnt, loff_t *ppos)
4500{
4501 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
4502}
4503
4504static ssize_t
4505tracing_thresh_write(struct file *filp, const char __user *ubuf,
4506 size_t cnt, loff_t *ppos)
4507{
4508 struct trace_array *tr = filp->private_data;
4509 int ret;
4510
4511 mutex_lock(&trace_types_lock);
4512 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
4513 if (ret < 0)
4514 goto out;
4515
4516 if (tr->current_trace->update_thresh) {
4517 ret = tr->current_trace->update_thresh(tr);
4518 if (ret < 0)
4519 goto out;
4520 }
4521
4522 ret = cnt;
4523out:
4524 mutex_unlock(&trace_types_lock);
4525
4526 return ret;
4527}
4528
4529static ssize_t
4530tracing_max_lat_read(struct file *filp, char __user *ubuf,
4531 size_t cnt, loff_t *ppos)
4532{
4533 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos);
4534}
4535
4536static ssize_t
4537tracing_max_lat_write(struct file *filp, const char __user *ubuf,
4538 size_t cnt, loff_t *ppos)
4539{
4540 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos);
4541}
4542
Steven Rostedtb3806b42008-05-12 21:20:46 +02004543static int tracing_open_pipe(struct inode *inode, struct file *filp)
4544{
Oleg Nesterov15544202013-07-23 17:25:57 +02004545 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004546 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004547 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004548
4549 if (tracing_disabled)
4550 return -ENODEV;
4551
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004552 if (trace_array_get(tr) < 0)
4553 return -ENODEV;
4554
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004555 mutex_lock(&trace_types_lock);
4556
Steven Rostedtb3806b42008-05-12 21:20:46 +02004557 /* create a buffer to store the information to pass to userspace */
4558 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004559 if (!iter) {
4560 ret = -ENOMEM;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004561 __trace_array_put(tr);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004562 goto out;
4563 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02004564
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04004565 trace_seq_init(&iter->seq);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05004566 iter->trace = tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004567
4568 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
4569 ret = -ENOMEM;
4570 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10304571 }
4572
Steven Rostedta3097202008-11-07 22:36:02 -05004573 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10304574 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05004575
Steven Rostedt112f38a72009-06-01 15:16:05 -04004576 if (trace_flags & TRACE_ITER_LATENCY_FMT)
4577 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4578
David Sharp8be07092012-11-13 12:18:22 -08004579 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09004580 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08004581 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4582
Oleg Nesterov15544202013-07-23 17:25:57 +02004583 iter->tr = tr;
4584 iter->trace_buffer = &tr->trace_buffer;
4585 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004586 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02004587 filp->private_data = iter;
4588
Steven Rostedt107bad82008-05-12 21:21:01 +02004589 if (iter->trace->pipe_open)
4590 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02004591
Arnd Bergmannb4447862010-07-07 23:40:11 +02004592 nonseekable_open(inode, filp);
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05004593
4594 tr->current_trace->ref++;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004595out:
4596 mutex_unlock(&trace_types_lock);
4597 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004598
4599fail:
4600 kfree(iter->trace);
4601 kfree(iter);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004602 __trace_array_put(tr);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004603 mutex_unlock(&trace_types_lock);
4604 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004605}
4606
4607static int tracing_release_pipe(struct inode *inode, struct file *file)
4608{
4609 struct trace_iterator *iter = file->private_data;
Oleg Nesterov15544202013-07-23 17:25:57 +02004610 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004611
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004612 mutex_lock(&trace_types_lock);
4613
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05004614 tr->current_trace->ref--;
4615
Steven Rostedt29bf4a52009-12-09 12:37:43 -05004616 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05004617 iter->trace->pipe_close(iter);
4618
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004619 mutex_unlock(&trace_types_lock);
4620
Rusty Russell44623442009-01-01 10:12:23 +10304621 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004622 mutex_destroy(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02004623 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02004624
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004625 trace_array_put(tr);
4626
Steven Rostedtb3806b42008-05-12 21:20:46 +02004627 return 0;
4628}
4629
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02004630static unsigned int
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004631trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02004632{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05004633 /* Iterators are static, they should be filled or empty */
4634 if (trace_buffer_iter(iter, iter->cpu_file))
4635 return POLLIN | POLLRDNORM;
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02004636
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05004637 if (trace_flags & TRACE_ITER_BLOCK)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02004638 /*
4639 * Always select as readable when in blocking mode
4640 */
4641 return POLLIN | POLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05004642 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004643 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05004644 filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02004645}
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02004646
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05004647static unsigned int
4648tracing_poll_pipe(struct file *filp, poll_table *poll_table)
4649{
4650 struct trace_iterator *iter = filp->private_data;
4651
4652 return trace_poll(iter, filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02004653}
4654
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05004655/* Must be called with iter->mutex held. */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02004656static int tracing_wait_pipe(struct file *filp)
4657{
4658 struct trace_iterator *iter = filp->private_data;
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04004659 int ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02004660
4661 while (trace_empty(iter)) {
4662
4663 if ((filp->f_flags & O_NONBLOCK)) {
4664 return -EAGAIN;
4665 }
4666
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02004667 /*
Liu Bo250bfd32013-01-14 10:54:11 +08004668 * We block until we read something and tracing is disabled.
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02004669 * We still block if tracing is disabled, but we have never
4670 * read anything. This allows a user to cat this file, and
4671 * then enable tracing. But after we have read something,
4672 * we give an EOF when tracing is again disabled.
4673 *
4674 * iter->pos will be 0 if we haven't read anything.
4675 */
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04004676 if (!tracing_is_on() && iter->pos)
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02004677 break;
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04004678
4679 mutex_unlock(&iter->mutex);
4680
Rabin Vincente30f53a2014-11-10 19:46:34 +01004681 ret = wait_on_pipe(iter, false);
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04004682
4683 mutex_lock(&iter->mutex);
4684
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04004685 if (ret)
4686 return ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02004687 }
4688
4689 return 1;
4690}
4691
Steven Rostedtb3806b42008-05-12 21:20:46 +02004692/*
4693 * Consumer reader.
4694 */
4695static ssize_t
4696tracing_read_pipe(struct file *filp, char __user *ubuf,
4697 size_t cnt, loff_t *ppos)
4698{
4699 struct trace_iterator *iter = filp->private_data;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02004700 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004701
4702 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02004703 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4704 if (sret != -EBUSY)
4705 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004706
Steven Rostedtf9520752009-03-02 14:04:40 -05004707 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02004708
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004709 /*
4710 * Avoid more than one consumer on a single file descriptor
4711 * This is just a matter of traces coherency, the ring buffer itself
4712 * is protected.
4713 */
4714 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02004715 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02004716 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4717 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02004718 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02004719 }
4720
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02004721waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02004722 sret = tracing_wait_pipe(filp);
4723 if (sret <= 0)
4724 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004725
4726 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02004727 if (trace_empty(iter)) {
4728 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02004729 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02004730 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02004731
4732 if (cnt >= PAGE_SIZE)
4733 cnt = PAGE_SIZE - 1;
4734
Steven Rostedt53d0aa72008-05-12 21:21:01 +02004735 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02004736 memset(&iter->seq, 0,
4737 sizeof(struct trace_iterator) -
4738 offsetof(struct trace_iterator, seq));
Andrew Vagined5467d2013-08-02 21:16:43 +04004739 cpumask_clear(iter->started);
Steven Rostedt4823ed72008-05-12 21:21:01 +02004740 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004741
Lai Jiangshan4f535962009-05-18 19:35:34 +08004742 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004743 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05004744 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02004745 enum print_line_t ret;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05004746 int save_len = iter->seq.seq.len;
Steven Rostedt088b1e422008-05-12 21:20:48 +02004747
Ingo Molnarf9896bf2008-05-12 21:20:47 +02004748 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02004749 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02004750 /* don't print partial lines */
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05004751 iter->seq.seq.len = save_len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004752 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02004753 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01004754 if (ret != TRACE_TYPE_NO_CONSUME)
4755 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02004756
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05004757 if (trace_seq_used(&iter->seq) >= cnt)
Steven Rostedtb3806b42008-05-12 21:20:46 +02004758 break;
Jiri Olsaee5e51f2011-03-25 12:05:18 +01004759
4760 /*
4761 * Setting the full flag means we reached the trace_seq buffer
4762 * size and we should leave by partial output condition above.
4763 * One of the trace_seq_* functions is not used properly.
4764 */
4765 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4766 iter->ent->type);
Steven Rostedtb3806b42008-05-12 21:20:46 +02004767 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004768 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08004769 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02004770
Steven Rostedtb3806b42008-05-12 21:20:46 +02004771 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02004772 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05004773 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
Steven Rostedtf9520752009-03-02 14:04:40 -05004774 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02004775
4776 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004777 * If there was nothing to send to user, in spite of consuming trace
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02004778 * entries, go back to wait for more entries.
4779 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02004780 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02004781 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004782
Steven Rostedt107bad82008-05-12 21:21:01 +02004783out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004784 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02004785
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02004786 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004787}
4788
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004789static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4790 unsigned int idx)
4791{
4792 __free_page(spd->pages[idx]);
4793}
4794
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08004795static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05004796 .can_merge = 0,
Steven Rostedt34cd4992009-02-09 12:06:29 -05004797 .confirm = generic_pipe_buf_confirm,
Al Viro92fdd982014-01-17 07:53:39 -05004798 .release = generic_pipe_buf_release,
Steven Rostedt34cd4992009-02-09 12:06:29 -05004799 .steal = generic_pipe_buf_steal,
4800 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004801};
4802
Steven Rostedt34cd4992009-02-09 12:06:29 -05004803static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01004804tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05004805{
4806 size_t count;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05004807 int save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05004808 int ret;
4809
4810 /* Seq buffer is page-sized, exactly what we need. */
4811 for (;;) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05004812 save_len = iter->seq.seq.len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05004813 ret = print_trace_line(iter);
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05004814
4815 if (trace_seq_has_overflowed(&iter->seq)) {
4816 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05004817 break;
4818 }
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05004819
4820 /*
4821 * This should not be hit, because it should only
4822 * be set if the iter->seq overflowed. But check it
4823 * anyway to be safe.
4824 */
Steven Rostedt34cd4992009-02-09 12:06:29 -05004825 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05004826 iter->seq.seq.len = save_len;
4827 break;
4828 }
4829
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05004830 count = trace_seq_used(&iter->seq) - save_len;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05004831 if (rem < count) {
4832 rem = 0;
4833 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05004834 break;
4835 }
4836
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08004837 if (ret != TRACE_TYPE_NO_CONSUME)
4838 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05004839 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05004840 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05004841 rem = 0;
4842 iter->ent = NULL;
4843 break;
4844 }
4845 }
4846
4847 return rem;
4848}
4849
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004850static ssize_t tracing_splice_read_pipe(struct file *filp,
4851 loff_t *ppos,
4852 struct pipe_inode_info *pipe,
4853 size_t len,
4854 unsigned int flags)
4855{
Jens Axboe35f3d142010-05-20 10:43:18 +02004856 struct page *pages_def[PIPE_DEF_BUFFERS];
4857 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004858 struct trace_iterator *iter = filp->private_data;
4859 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02004860 .pages = pages_def,
4861 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05004862 .nr_pages = 0, /* This gets updated below. */
Eric Dumazet047fe362012-06-12 15:24:40 +02004863 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt34cd4992009-02-09 12:06:29 -05004864 .flags = flags,
4865 .ops = &tracing_pipe_buf_ops,
4866 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004867 };
4868 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05004869 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004870 unsigned int i;
4871
Jens Axboe35f3d142010-05-20 10:43:18 +02004872 if (splice_grow_spd(pipe, &spd))
4873 return -ENOMEM;
4874
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004875 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004876
4877 if (iter->trace->splice_read) {
4878 ret = iter->trace->splice_read(iter, filp,
4879 ppos, pipe, len, flags);
4880 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05004881 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004882 }
4883
4884 ret = tracing_wait_pipe(filp);
4885 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05004886 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004887
Jason Wessel955b61e2010-08-05 09:22:23 -05004888 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004889 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05004890 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004891 }
4892
Lai Jiangshan4f535962009-05-18 19:35:34 +08004893 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004894 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08004895
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004896 /* Fill as many pages as possible. */
Al Viroa786c062014-04-11 12:01:03 -04004897 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
Jens Axboe35f3d142010-05-20 10:43:18 +02004898 spd.pages[i] = alloc_page(GFP_KERNEL);
4899 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05004900 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004901
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01004902 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004903
4904 /* Copy the data into the page, so we can start over. */
4905 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02004906 page_address(spd.pages[i]),
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05004907 trace_seq_used(&iter->seq));
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004908 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02004909 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004910 break;
4911 }
Jens Axboe35f3d142010-05-20 10:43:18 +02004912 spd.partial[i].offset = 0;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05004913 spd.partial[i].len = trace_seq_used(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004914
Steven Rostedtf9520752009-03-02 14:04:40 -05004915 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004916 }
4917
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004918 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08004919 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004920 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004921
4922 spd.nr_pages = i;
4923
Jens Axboe35f3d142010-05-20 10:43:18 +02004924 ret = splice_to_pipe(pipe, &spd);
4925out:
Eric Dumazet047fe362012-06-12 15:24:40 +02004926 splice_shrink_spd(&spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02004927 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004928
Steven Rostedt34cd4992009-02-09 12:06:29 -05004929out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004930 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02004931 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004932}
4933
Steven Rostedta98a3c32008-05-12 21:20:59 +02004934static ssize_t
4935tracing_entries_read(struct file *filp, char __user *ubuf,
4936 size_t cnt, loff_t *ppos)
4937{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02004938 struct inode *inode = file_inode(filp);
4939 struct trace_array *tr = inode->i_private;
4940 int cpu = tracing_get_cpu(inode);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004941 char buf[64];
4942 int r = 0;
4943 ssize_t ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004944
Steven Rostedtdb526ca2009-03-12 13:53:25 -04004945 mutex_lock(&trace_types_lock);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004946
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02004947 if (cpu == RING_BUFFER_ALL_CPUS) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004948 int cpu, buf_size_same;
4949 unsigned long size;
4950
4951 size = 0;
4952 buf_size_same = 1;
4953 /* check if all cpu sizes are same */
4954 for_each_tracing_cpu(cpu) {
4955 /* fill in the size from first enabled cpu */
4956 if (size == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004957 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4958 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004959 buf_size_same = 0;
4960 break;
4961 }
4962 }
4963
4964 if (buf_size_same) {
4965 if (!ring_buffer_expanded)
4966 r = sprintf(buf, "%lu (expanded: %lu)\n",
4967 size >> 10,
4968 trace_buf_size >> 10);
4969 else
4970 r = sprintf(buf, "%lu\n", size >> 10);
4971 } else
4972 r = sprintf(buf, "X\n");
4973 } else
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02004974 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004975
Steven Rostedtdb526ca2009-03-12 13:53:25 -04004976 mutex_unlock(&trace_types_lock);
4977
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004978 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4979 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004980}
4981
4982static ssize_t
4983tracing_entries_write(struct file *filp, const char __user *ubuf,
4984 size_t cnt, loff_t *ppos)
4985{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02004986 struct inode *inode = file_inode(filp);
4987 struct trace_array *tr = inode->i_private;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004988 unsigned long val;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004989 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004990
Peter Huewe22fe9b52011-06-07 21:58:27 +02004991 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4992 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02004993 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02004994
4995 /* must have at least 1 entry */
4996 if (!val)
4997 return -EINVAL;
4998
Steven Rostedt1696b2b2008-11-13 00:09:35 -05004999 /* value is in KB */
5000 val <<= 10;
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005001 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005002 if (ret < 0)
5003 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005004
Jiri Olsacf8517c2009-10-23 19:36:16 -04005005 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005006
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005007 return cnt;
5008}
Steven Rostedtbf5e6512008-11-10 21:46:00 -05005009
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005010static ssize_t
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005011tracing_total_entries_read(struct file *filp, char __user *ubuf,
5012 size_t cnt, loff_t *ppos)
5013{
5014 struct trace_array *tr = filp->private_data;
5015 char buf[64];
5016 int r, cpu;
5017 unsigned long size = 0, expanded_size = 0;
5018
5019 mutex_lock(&trace_types_lock);
5020 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005021 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005022 if (!ring_buffer_expanded)
5023 expanded_size += trace_buf_size >> 10;
5024 }
5025 if (ring_buffer_expanded)
5026 r = sprintf(buf, "%lu\n", size);
5027 else
5028 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
5029 mutex_unlock(&trace_types_lock);
5030
5031 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5032}
5033
5034static ssize_t
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005035tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
5036 size_t cnt, loff_t *ppos)
5037{
5038 /*
5039 * There is no need to read what the user has written, this function
5040 * is just to make sure that there is no error when "echo" is used
5041 */
5042
5043 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005044
5045 return cnt;
5046}
5047
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005048static int
5049tracing_free_buffer_release(struct inode *inode, struct file *filp)
5050{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005051 struct trace_array *tr = inode->i_private;
5052
Steven Rostedtcf30cf62011-06-14 22:44:07 -04005053 /* disable tracing ? */
5054 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
Alexander Z Lam711e1242013-08-02 18:36:15 -07005055 tracer_tracing_off(tr);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005056 /* resize the ring buffer to 0 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005057 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005058
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005059 trace_array_put(tr);
5060
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005061 return 0;
5062}
5063
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005064static ssize_t
5065tracing_mark_write(struct file *filp, const char __user *ubuf,
5066 size_t cnt, loff_t *fpos)
5067{
Steven Rostedtd696b582011-09-22 11:50:27 -04005068 unsigned long addr = (unsigned long)ubuf;
Alexander Z Lam2d716192013-07-01 15:31:24 -07005069 struct trace_array *tr = filp->private_data;
Steven Rostedtd696b582011-09-22 11:50:27 -04005070 struct ring_buffer_event *event;
5071 struct ring_buffer *buffer;
5072 struct print_entry *entry;
5073 unsigned long irq_flags;
5074 struct page *pages[2];
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005075 void *map_page[2];
Steven Rostedtd696b582011-09-22 11:50:27 -04005076 int nr_pages = 1;
5077 ssize_t written;
Steven Rostedtd696b582011-09-22 11:50:27 -04005078 int offset;
5079 int size;
5080 int len;
5081 int ret;
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005082 int i;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005083
Steven Rostedtc76f0692008-11-07 22:36:02 -05005084 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005085 return -EINVAL;
5086
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -07005087 if (!(trace_flags & TRACE_ITER_MARKERS))
5088 return -EINVAL;
5089
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005090 if (cnt > TRACE_BUF_SIZE)
5091 cnt = TRACE_BUF_SIZE;
5092
Steven Rostedtd696b582011-09-22 11:50:27 -04005093 /*
5094 * Userspace is injecting traces into the kernel trace buffer.
5095 * We want to be as non intrusive as possible.
5096 * To do so, we do not want to allocate any special buffers
5097 * or take any locks, but instead write the userspace data
5098 * straight into the ring buffer.
5099 *
5100 * First we need to pin the userspace buffer into memory,
5101 * which, most likely it is, because it just referenced it.
5102 * But there's no guarantee that it is. By using get_user_pages_fast()
5103 * and kmap_atomic/kunmap_atomic() we can get access to the
5104 * pages directly. We then write the data directly into the
5105 * ring buffer.
5106 */
5107 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005108
Steven Rostedtd696b582011-09-22 11:50:27 -04005109 /* check if we cross pages */
5110 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
5111 nr_pages = 2;
5112
5113 offset = addr & (PAGE_SIZE - 1);
5114 addr &= PAGE_MASK;
5115
5116 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
5117 if (ret < nr_pages) {
5118 while (--ret >= 0)
5119 put_page(pages[ret]);
5120 written = -EFAULT;
5121 goto out;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005122 }
5123
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005124 for (i = 0; i < nr_pages; i++)
5125 map_page[i] = kmap_atomic(pages[i]);
Steven Rostedtd696b582011-09-22 11:50:27 -04005126
5127 local_save_flags(irq_flags);
5128 size = sizeof(*entry) + cnt + 2; /* possible \n added */
Alexander Z Lam2d716192013-07-01 15:31:24 -07005129 buffer = tr->trace_buffer.buffer;
Steven Rostedtd696b582011-09-22 11:50:27 -04005130 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
5131 irq_flags, preempt_count());
5132 if (!event) {
5133 /* Ring buffer disabled, return as if not open for write */
5134 written = -EBADF;
5135 goto out_unlock;
5136 }
5137
5138 entry = ring_buffer_event_data(event);
5139 entry->ip = _THIS_IP_;
5140
5141 if (nr_pages == 2) {
5142 len = PAGE_SIZE - offset;
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005143 memcpy(&entry->buf, map_page[0] + offset, len);
5144 memcpy(&entry->buf[len], map_page[1], cnt - len);
Steven Rostedtd696b582011-09-22 11:50:27 -04005145 } else
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005146 memcpy(&entry->buf, map_page[0] + offset, cnt);
Steven Rostedtd696b582011-09-22 11:50:27 -04005147
5148 if (entry->buf[cnt - 1] != '\n') {
5149 entry->buf[cnt] = '\n';
5150 entry->buf[cnt + 1] = '\0';
5151 } else
5152 entry->buf[cnt] = '\0';
5153
Steven Rostedt7ffbd482012-10-11 12:14:25 -04005154 __buffer_unlock_commit(buffer, event);
Steven Rostedtd696b582011-09-22 11:50:27 -04005155
5156 written = cnt;
5157
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02005158 *fpos += written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005159
Steven Rostedtd696b582011-09-22 11:50:27 -04005160 out_unlock:
Vikram Mulukutla72158532014-12-17 18:50:56 -08005161 for (i = nr_pages - 1; i >= 0; i--) {
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005162 kunmap_atomic(map_page[i]);
5163 put_page(pages[i]);
5164 }
Steven Rostedtd696b582011-09-22 11:50:27 -04005165 out:
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02005166 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005167}
5168
Li Zefan13f16d22009-12-08 11:16:11 +08005169static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08005170{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005171 struct trace_array *tr = m->private;
Zhaolei5079f322009-08-25 16:12:56 +08005172 int i;
5173
5174 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08005175 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08005176 "%s%s%s%s", i ? " " : "",
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005177 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
5178 i == tr->clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08005179 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08005180
Li Zefan13f16d22009-12-08 11:16:11 +08005181 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08005182}
5183
Steven Rostedte1e232c2014-02-10 23:38:46 -05005184static int tracing_set_clock(struct trace_array *tr, const char *clockstr)
Zhaolei5079f322009-08-25 16:12:56 +08005185{
Zhaolei5079f322009-08-25 16:12:56 +08005186 int i;
5187
Zhaolei5079f322009-08-25 16:12:56 +08005188 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
5189 if (strcmp(trace_clocks[i].name, clockstr) == 0)
5190 break;
5191 }
5192 if (i == ARRAY_SIZE(trace_clocks))
5193 return -EINVAL;
5194
Zhaolei5079f322009-08-25 16:12:56 +08005195 mutex_lock(&trace_types_lock);
5196
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005197 tr->clock_id = i;
5198
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005199 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
Zhaolei5079f322009-08-25 16:12:56 +08005200
David Sharp60303ed2012-10-11 16:27:52 -07005201 /*
5202 * New clock may not be consistent with the previous clock.
5203 * Reset the buffer so that it doesn't have incomparable timestamps.
5204 */
Alexander Z Lam94571582013-08-02 18:36:16 -07005205 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005206
5207#ifdef CONFIG_TRACER_MAX_TRACE
5208 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
5209 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
Alexander Z Lam94571582013-08-02 18:36:16 -07005210 tracing_reset_online_cpus(&tr->max_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005211#endif
David Sharp60303ed2012-10-11 16:27:52 -07005212
Zhaolei5079f322009-08-25 16:12:56 +08005213 mutex_unlock(&trace_types_lock);
5214
Steven Rostedte1e232c2014-02-10 23:38:46 -05005215 return 0;
5216}
5217
5218static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
5219 size_t cnt, loff_t *fpos)
5220{
5221 struct seq_file *m = filp->private_data;
5222 struct trace_array *tr = m->private;
5223 char buf[64];
5224 const char *clockstr;
5225 int ret;
5226
5227 if (cnt >= sizeof(buf))
5228 return -EINVAL;
5229
5230 if (copy_from_user(&buf, ubuf, cnt))
5231 return -EFAULT;
5232
5233 buf[cnt] = 0;
5234
5235 clockstr = strstrip(buf);
5236
5237 ret = tracing_set_clock(tr, clockstr);
5238 if (ret)
5239 return ret;
5240
Zhaolei5079f322009-08-25 16:12:56 +08005241 *fpos += cnt;
5242
5243 return cnt;
5244}
5245
Li Zefan13f16d22009-12-08 11:16:11 +08005246static int tracing_clock_open(struct inode *inode, struct file *file)
5247{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005248 struct trace_array *tr = inode->i_private;
5249 int ret;
5250
Li Zefan13f16d22009-12-08 11:16:11 +08005251 if (tracing_disabled)
5252 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005253
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005254 if (trace_array_get(tr))
5255 return -ENODEV;
5256
5257 ret = single_open(file, tracing_clock_show, inode->i_private);
5258 if (ret < 0)
5259 trace_array_put(tr);
5260
5261 return ret;
Li Zefan13f16d22009-12-08 11:16:11 +08005262}
5263
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005264struct ftrace_buffer_info {
5265 struct trace_iterator iter;
5266 void *spare;
5267 unsigned int read;
5268};
5269
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005270#ifdef CONFIG_TRACER_SNAPSHOT
5271static int tracing_snapshot_open(struct inode *inode, struct file *file)
5272{
Oleg Nesterov6484c712013-07-23 17:26:10 +02005273 struct trace_array *tr = inode->i_private;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005274 struct trace_iterator *iter;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005275 struct seq_file *m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005276 int ret = 0;
5277
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005278 if (trace_array_get(tr) < 0)
5279 return -ENODEV;
5280
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005281 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02005282 iter = __tracing_open(inode, file, true);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005283 if (IS_ERR(iter))
5284 ret = PTR_ERR(iter);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005285 } else {
5286 /* Writes still need the seq_file to hold the private data */
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005287 ret = -ENOMEM;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005288 m = kzalloc(sizeof(*m), GFP_KERNEL);
5289 if (!m)
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005290 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005291 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
5292 if (!iter) {
5293 kfree(m);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005294 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005295 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005296 ret = 0;
5297
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005298 iter->tr = tr;
Oleg Nesterov6484c712013-07-23 17:26:10 +02005299 iter->trace_buffer = &tr->max_buffer;
5300 iter->cpu_file = tracing_get_cpu(inode);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005301 m->private = iter;
5302 file->private_data = m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005303 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005304out:
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005305 if (ret < 0)
5306 trace_array_put(tr);
5307
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005308 return ret;
5309}
5310
5311static ssize_t
5312tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
5313 loff_t *ppos)
5314{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005315 struct seq_file *m = filp->private_data;
5316 struct trace_iterator *iter = m->private;
5317 struct trace_array *tr = iter->tr;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005318 unsigned long val;
5319 int ret;
5320
5321 ret = tracing_update_buffers();
5322 if (ret < 0)
5323 return ret;
5324
5325 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5326 if (ret)
5327 return ret;
5328
5329 mutex_lock(&trace_types_lock);
5330
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005331 if (tr->current_trace->use_max_tr) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005332 ret = -EBUSY;
5333 goto out;
5334 }
5335
5336 switch (val) {
5337 case 0:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005338 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
5339 ret = -EINVAL;
5340 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005341 }
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04005342 if (tr->allocated_snapshot)
5343 free_snapshot(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005344 break;
5345 case 1:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005346/* Only allow per-cpu swap if the ring buffer supports it */
5347#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
5348 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
5349 ret = -EINVAL;
5350 break;
5351 }
5352#endif
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05005353 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04005354 ret = alloc_snapshot(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005355 if (ret < 0)
5356 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005357 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005358 local_irq_disable();
5359 /* Now, we're going to swap */
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005360 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05005361 update_max_tr(tr, current, smp_processor_id());
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005362 else
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05005363 update_max_tr_single(tr, current, iter->cpu_file);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005364 local_irq_enable();
5365 break;
5366 default:
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05005367 if (tr->allocated_snapshot) {
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005368 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
5369 tracing_reset_online_cpus(&tr->max_buffer);
5370 else
5371 tracing_reset(&tr->max_buffer, iter->cpu_file);
5372 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005373 break;
5374 }
5375
5376 if (ret >= 0) {
5377 *ppos += cnt;
5378 ret = cnt;
5379 }
5380out:
5381 mutex_unlock(&trace_types_lock);
5382 return ret;
5383}
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005384
5385static int tracing_snapshot_release(struct inode *inode, struct file *file)
5386{
5387 struct seq_file *m = file->private_data;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005388 int ret;
5389
5390 ret = tracing_release(inode, file);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005391
5392 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005393 return ret;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005394
5395 /* If write only, the seq_file is just a stub */
5396 if (m)
5397 kfree(m->private);
5398 kfree(m);
5399
5400 return 0;
5401}
5402
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005403static int tracing_buffers_open(struct inode *inode, struct file *filp);
5404static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
5405 size_t count, loff_t *ppos);
5406static int tracing_buffers_release(struct inode *inode, struct file *file);
5407static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5408 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
5409
5410static int snapshot_raw_open(struct inode *inode, struct file *filp)
5411{
5412 struct ftrace_buffer_info *info;
5413 int ret;
5414
5415 ret = tracing_buffers_open(inode, filp);
5416 if (ret < 0)
5417 return ret;
5418
5419 info = filp->private_data;
5420
5421 if (info->iter.trace->use_max_tr) {
5422 tracing_buffers_release(inode, filp);
5423 return -EBUSY;
5424 }
5425
5426 info->iter.snapshot = true;
5427 info->iter.trace_buffer = &info->iter.tr->max_buffer;
5428
5429 return ret;
5430}
5431
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005432#endif /* CONFIG_TRACER_SNAPSHOT */
5433
5434
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005435static const struct file_operations tracing_thresh_fops = {
5436 .open = tracing_open_generic,
5437 .read = tracing_thresh_read,
5438 .write = tracing_thresh_write,
5439 .llseek = generic_file_llseek,
5440};
5441
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005442static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005443 .open = tracing_open_generic,
5444 .read = tracing_max_lat_read,
5445 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005446 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005447};
5448
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005449static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005450 .open = tracing_open_generic,
5451 .read = tracing_set_trace_read,
5452 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005453 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005454};
5455
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005456static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005457 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005458 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005459 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005460 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005461 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005462 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02005463};
5464
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005465static const struct file_operations tracing_entries_fops = {
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005466 .open = tracing_open_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02005467 .read = tracing_entries_read,
5468 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005469 .llseek = generic_file_llseek,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005470 .release = tracing_release_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02005471};
5472
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005473static const struct file_operations tracing_total_entries_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005474 .open = tracing_open_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005475 .read = tracing_total_entries_read,
5476 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005477 .release = tracing_release_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005478};
5479
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005480static const struct file_operations tracing_free_buffer_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005481 .open = tracing_open_generic_tr,
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005482 .write = tracing_free_buffer_write,
5483 .release = tracing_free_buffer_release,
5484};
5485
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005486static const struct file_operations tracing_mark_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005487 .open = tracing_open_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005488 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005489 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005490 .release = tracing_release_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005491};
5492
Zhaolei5079f322009-08-25 16:12:56 +08005493static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08005494 .open = tracing_clock_open,
5495 .read = seq_read,
5496 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005497 .release = tracing_single_release_tr,
Zhaolei5079f322009-08-25 16:12:56 +08005498 .write = tracing_clock_write,
5499};
5500
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005501#ifdef CONFIG_TRACER_SNAPSHOT
5502static const struct file_operations snapshot_fops = {
5503 .open = tracing_snapshot_open,
5504 .read = seq_read,
5505 .write = tracing_snapshot_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005506 .llseek = tracing_lseek,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005507 .release = tracing_snapshot_release,
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005508};
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005509
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005510static const struct file_operations snapshot_raw_fops = {
5511 .open = snapshot_raw_open,
5512 .read = tracing_buffers_read,
5513 .release = tracing_buffers_release,
5514 .splice_read = tracing_buffers_splice_read,
5515 .llseek = no_llseek,
Steven Rostedt2cadf912008-12-01 22:20:19 -05005516};
5517
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005518#endif /* CONFIG_TRACER_SNAPSHOT */
5519
Steven Rostedt2cadf912008-12-01 22:20:19 -05005520static int tracing_buffers_open(struct inode *inode, struct file *filp)
5521{
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02005522 struct trace_array *tr = inode->i_private;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005523 struct ftrace_buffer_info *info;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005524 int ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005525
5526 if (tracing_disabled)
5527 return -ENODEV;
5528
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005529 if (trace_array_get(tr) < 0)
5530 return -ENODEV;
5531
Steven Rostedt2cadf912008-12-01 22:20:19 -05005532 info = kzalloc(sizeof(*info), GFP_KERNEL);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005533 if (!info) {
5534 trace_array_put(tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005535 return -ENOMEM;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005536 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05005537
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05005538 mutex_lock(&trace_types_lock);
5539
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005540 info->iter.tr = tr;
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02005541 info->iter.cpu_file = tracing_get_cpu(inode);
Steven Rostedtb6273442013-02-28 13:44:11 -05005542 info->iter.trace = tr->current_trace;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005543 info->iter.trace_buffer = &tr->trace_buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005544 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005545 /* Force reading ring buffer for first read */
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005546 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005547
5548 filp->private_data = info;
5549
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005550 tr->current_trace->ref++;
5551
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05005552 mutex_unlock(&trace_types_lock);
5553
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005554 ret = nonseekable_open(inode, filp);
5555 if (ret < 0)
5556 trace_array_put(tr);
5557
5558 return ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005559}
5560
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005561static unsigned int
5562tracing_buffers_poll(struct file *filp, poll_table *poll_table)
5563{
5564 struct ftrace_buffer_info *info = filp->private_data;
5565 struct trace_iterator *iter = &info->iter;
5566
5567 return trace_poll(iter, filp, poll_table);
5568}
5569
Steven Rostedt2cadf912008-12-01 22:20:19 -05005570static ssize_t
5571tracing_buffers_read(struct file *filp, char __user *ubuf,
5572 size_t count, loff_t *ppos)
5573{
5574 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005575 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005576 ssize_t ret;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005577 ssize_t size;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005578
Steven Rostedt2dc5d122009-03-04 19:10:05 -05005579 if (!count)
5580 return 0;
5581
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005582#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005583 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
5584 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005585#endif
5586
Lai Jiangshanddd538f2009-04-02 15:16:59 +08005587 if (!info->spare)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005588 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
5589 iter->cpu_file);
Lai Jiangshanddd538f2009-04-02 15:16:59 +08005590 if (!info->spare)
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005591 return -ENOMEM;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08005592
Steven Rostedt2cadf912008-12-01 22:20:19 -05005593 /* Do we have previous read data to read? */
5594 if (info->read < PAGE_SIZE)
5595 goto read;
5596
Steven Rostedtb6273442013-02-28 13:44:11 -05005597 again:
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005598 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005599 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
Steven Rostedt2cadf912008-12-01 22:20:19 -05005600 &info->spare,
5601 count,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005602 iter->cpu_file, 0);
5603 trace_access_unlock(iter->cpu_file);
Steven Rostedtb6273442013-02-28 13:44:11 -05005604
5605 if (ret < 0) {
5606 if (trace_empty(iter)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005607 if ((filp->f_flags & O_NONBLOCK))
5608 return -EAGAIN;
5609
Rabin Vincente30f53a2014-11-10 19:46:34 +01005610 ret = wait_on_pipe(iter, false);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005611 if (ret)
5612 return ret;
5613
Steven Rostedtb6273442013-02-28 13:44:11 -05005614 goto again;
5615 }
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005616 return 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05005617 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05005618
Steven Rostedt436fc282011-10-14 10:44:25 -04005619 info->read = 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05005620 read:
Steven Rostedt2cadf912008-12-01 22:20:19 -05005621 size = PAGE_SIZE - info->read;
5622 if (size > count)
5623 size = count;
5624
5625 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005626 if (ret == size)
5627 return -EFAULT;
5628
Steven Rostedt2dc5d122009-03-04 19:10:05 -05005629 size -= ret;
5630
Steven Rostedt2cadf912008-12-01 22:20:19 -05005631 *ppos += size;
5632 info->read += size;
5633
5634 return size;
5635}
5636
5637static int tracing_buffers_release(struct inode *inode, struct file *file)
5638{
5639 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005640 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005641
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05005642 mutex_lock(&trace_types_lock);
5643
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005644 iter->tr->current_trace->ref--;
5645
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005646 __trace_array_put(iter->tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005647
Lai Jiangshanddd538f2009-04-02 15:16:59 +08005648 if (info->spare)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005649 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005650 kfree(info);
5651
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05005652 mutex_unlock(&trace_types_lock);
5653
Steven Rostedt2cadf912008-12-01 22:20:19 -05005654 return 0;
5655}
5656
5657struct buffer_ref {
5658 struct ring_buffer *buffer;
5659 void *page;
5660 int ref;
5661};
5662
5663static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
5664 struct pipe_buffer *buf)
5665{
5666 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5667
5668 if (--ref->ref)
5669 return;
5670
5671 ring_buffer_free_read_page(ref->buffer, ref->page);
5672 kfree(ref);
5673 buf->private = 0;
5674}
5675
Steven Rostedt2cadf912008-12-01 22:20:19 -05005676static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
5677 struct pipe_buffer *buf)
5678{
5679 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5680
5681 ref->ref++;
5682}
5683
5684/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08005685static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05005686 .can_merge = 0,
Steven Rostedt2cadf912008-12-01 22:20:19 -05005687 .confirm = generic_pipe_buf_confirm,
5688 .release = buffer_pipe_buf_release,
Masami Hiramatsud55cb6c2012-08-09 21:31:10 +09005689 .steal = generic_pipe_buf_steal,
Steven Rostedt2cadf912008-12-01 22:20:19 -05005690 .get = buffer_pipe_buf_get,
5691};
5692
5693/*
5694 * Callback from splice_to_pipe(), if we need to release some pages
5695 * at the end of the spd in case we error'ed out in filling the pipe.
5696 */
5697static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
5698{
5699 struct buffer_ref *ref =
5700 (struct buffer_ref *)spd->partial[i].private;
5701
5702 if (--ref->ref)
5703 return;
5704
5705 ring_buffer_free_read_page(ref->buffer, ref->page);
5706 kfree(ref);
5707 spd->partial[i].private = 0;
5708}
5709
5710static ssize_t
5711tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5712 struct pipe_inode_info *pipe, size_t len,
5713 unsigned int flags)
5714{
5715 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005716 struct trace_iterator *iter = &info->iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02005717 struct partial_page partial_def[PIPE_DEF_BUFFERS];
5718 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05005719 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02005720 .pages = pages_def,
5721 .partial = partial_def,
Eric Dumazet047fe362012-06-12 15:24:40 +02005722 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt2cadf912008-12-01 22:20:19 -05005723 .flags = flags,
5724 .ops = &buffer_pipe_buf_ops,
5725 .spd_release = buffer_spd_release,
5726 };
5727 struct buffer_ref *ref;
Steven Rostedt93459c62009-04-29 00:23:13 -04005728 int entries, size, i;
Rabin Vincent07906da2014-11-06 22:26:07 +01005729 ssize_t ret = 0;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005730
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005731#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005732 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
5733 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005734#endif
5735
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005736 if (splice_grow_spd(pipe, &spd))
5737 return -ENOMEM;
Jens Axboe35f3d142010-05-20 10:43:18 +02005738
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005739 if (*ppos & (PAGE_SIZE - 1))
5740 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08005741
5742 if (len & (PAGE_SIZE - 1)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005743 if (len < PAGE_SIZE)
5744 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08005745 len &= PAGE_MASK;
5746 }
5747
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005748 again:
5749 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005750 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt93459c62009-04-29 00:23:13 -04005751
Al Viroa786c062014-04-11 12:01:03 -04005752 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05005753 struct page *page;
5754 int r;
5755
5756 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
Rabin Vincent07906da2014-11-06 22:26:07 +01005757 if (!ref) {
5758 ret = -ENOMEM;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005759 break;
Rabin Vincent07906da2014-11-06 22:26:07 +01005760 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05005761
Steven Rostedt7267fa62009-04-29 00:16:21 -04005762 ref->ref = 1;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005763 ref->buffer = iter->trace_buffer->buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005764 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005765 if (!ref->page) {
Rabin Vincent07906da2014-11-06 22:26:07 +01005766 ret = -ENOMEM;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005767 kfree(ref);
5768 break;
5769 }
5770
5771 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005772 len, iter->cpu_file, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005773 if (r < 0) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07005774 ring_buffer_free_read_page(ref->buffer, ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005775 kfree(ref);
5776 break;
5777 }
5778
5779 /*
5780 * zero out any left over data, this is going to
5781 * user land.
5782 */
5783 size = ring_buffer_page_len(ref->page);
5784 if (size < PAGE_SIZE)
5785 memset(ref->page + size, 0, PAGE_SIZE - size);
5786
5787 page = virt_to_page(ref->page);
5788
5789 spd.pages[i] = page;
5790 spd.partial[i].len = PAGE_SIZE;
5791 spd.partial[i].offset = 0;
5792 spd.partial[i].private = (unsigned long)ref;
5793 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08005794 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04005795
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005796 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005797 }
5798
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005799 trace_access_unlock(iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005800 spd.nr_pages = i;
5801
5802 /* did we read anything? */
5803 if (!spd.nr_pages) {
Rabin Vincent07906da2014-11-06 22:26:07 +01005804 if (ret)
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005805 return ret;
Rabin Vincent07906da2014-11-06 22:26:07 +01005806
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005807 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
5808 return -EAGAIN;
5809
Rabin Vincente30f53a2014-11-10 19:46:34 +01005810 ret = wait_on_pipe(iter, true);
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04005811 if (ret)
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005812 return ret;
Rabin Vincente30f53a2014-11-10 19:46:34 +01005813
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005814 goto again;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005815 }
5816
5817 ret = splice_to_pipe(pipe, &spd);
Eric Dumazet047fe362012-06-12 15:24:40 +02005818 splice_shrink_spd(&spd);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005819
Steven Rostedt2cadf912008-12-01 22:20:19 -05005820 return ret;
5821}
5822
5823static const struct file_operations tracing_buffers_fops = {
5824 .open = tracing_buffers_open,
5825 .read = tracing_buffers_read,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005826 .poll = tracing_buffers_poll,
Steven Rostedt2cadf912008-12-01 22:20:19 -05005827 .release = tracing_buffers_release,
5828 .splice_read = tracing_buffers_splice_read,
5829 .llseek = no_llseek,
5830};
5831
Steven Rostedtc8d77182009-04-29 18:03:45 -04005832static ssize_t
5833tracing_stats_read(struct file *filp, char __user *ubuf,
5834 size_t count, loff_t *ppos)
5835{
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02005836 struct inode *inode = file_inode(filp);
5837 struct trace_array *tr = inode->i_private;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005838 struct trace_buffer *trace_buf = &tr->trace_buffer;
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02005839 int cpu = tracing_get_cpu(inode);
Steven Rostedtc8d77182009-04-29 18:03:45 -04005840 struct trace_seq *s;
5841 unsigned long cnt;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005842 unsigned long long t;
5843 unsigned long usec_rem;
Steven Rostedtc8d77182009-04-29 18:03:45 -04005844
Li Zefane4f2d102009-06-15 10:57:28 +08005845 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04005846 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01005847 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04005848
5849 trace_seq_init(s);
5850
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005851 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04005852 trace_seq_printf(s, "entries: %ld\n", cnt);
5853
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005854 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04005855 trace_seq_printf(s, "overrun: %ld\n", cnt);
5856
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005857 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04005858 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5859
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005860 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005861 trace_seq_printf(s, "bytes: %ld\n", cnt);
5862
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09005863 if (trace_clocks[tr->clock_id].in_ns) {
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005864 /* local or global for trace_clock */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005865 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005866 usec_rem = do_div(t, USEC_PER_SEC);
5867 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5868 t, usec_rem);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005869
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005870 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005871 usec_rem = do_div(t, USEC_PER_SEC);
5872 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5873 } else {
5874 /* counter or tsc mode for trace_clock */
5875 trace_seq_printf(s, "oldest event ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005876 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005877
5878 trace_seq_printf(s, "now ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005879 ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08005880 }
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07005881
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005882 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
Slava Pestov884bfe82011-07-15 14:23:58 -07005883 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5884
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005885 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05005886 trace_seq_printf(s, "read events: %ld\n", cnt);
5887
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005888 count = simple_read_from_buffer(ubuf, count, ppos,
5889 s->buffer, trace_seq_used(s));
Steven Rostedtc8d77182009-04-29 18:03:45 -04005890
5891 kfree(s);
5892
5893 return count;
5894}
5895
5896static const struct file_operations tracing_stats_fops = {
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02005897 .open = tracing_open_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04005898 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005899 .llseek = generic_file_llseek,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02005900 .release = tracing_release_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04005901};
5902
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005903#ifdef CONFIG_DYNAMIC_FTRACE
5904
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005905int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005906{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005907 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005908}
5909
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005910static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005911tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005912 size_t cnt, loff_t *ppos)
5913{
Steven Rostedta26a2a22008-10-31 00:03:22 -04005914 static char ftrace_dyn_info_buffer[1024];
5915 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005916 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005917 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04005918 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005919 int r;
5920
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005921 mutex_lock(&dyn_info_mutex);
5922 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005923
Steven Rostedta26a2a22008-10-31 00:03:22 -04005924 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005925 buf[r++] = '\n';
5926
5927 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5928
5929 mutex_unlock(&dyn_info_mutex);
5930
5931 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005932}
5933
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005934static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005935 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04005936 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005937 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005938};
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04005939#endif /* CONFIG_DYNAMIC_FTRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005940
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04005941#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5942static void
5943ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005944{
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04005945 tracing_snapshot();
5946}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005947
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04005948static void
5949ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5950{
5951 unsigned long *count = (long *)data;
5952
5953 if (!*count)
5954 return;
5955
5956 if (*count != -1)
5957 (*count)--;
5958
5959 tracing_snapshot();
5960}
5961
5962static int
5963ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5964 struct ftrace_probe_ops *ops, void *data)
5965{
5966 long count = (long)data;
5967
5968 seq_printf(m, "%ps:", (void *)ip);
5969
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005970 seq_puts(m, "snapshot");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04005971
5972 if (count == -1)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005973 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04005974 else
5975 seq_printf(m, ":count=%ld\n", count);
5976
5977 return 0;
5978}
5979
5980static struct ftrace_probe_ops snapshot_probe_ops = {
5981 .func = ftrace_snapshot,
5982 .print = ftrace_snapshot_print,
5983};
5984
5985static struct ftrace_probe_ops snapshot_count_probe_ops = {
5986 .func = ftrace_count_snapshot,
5987 .print = ftrace_snapshot_print,
5988};
5989
5990static int
5991ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5992 char *glob, char *cmd, char *param, int enable)
5993{
5994 struct ftrace_probe_ops *ops;
5995 void *count = (void *)-1;
5996 char *number;
5997 int ret;
5998
5999 /* hash funcs only work with set_ftrace_filter */
6000 if (!enable)
6001 return -EINVAL;
6002
6003 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
6004
6005 if (glob[0] == '!') {
6006 unregister_ftrace_function_probe_func(glob+1, ops);
6007 return 0;
6008 }
6009
6010 if (!param)
6011 goto out_reg;
6012
6013 number = strsep(&param, ":");
6014
6015 if (!strlen(number))
6016 goto out_reg;
6017
6018 /*
6019 * We use the callback data field (which is a pointer)
6020 * as our counter.
6021 */
6022 ret = kstrtoul(number, 0, (unsigned long *)&count);
6023 if (ret)
6024 return ret;
6025
6026 out_reg:
6027 ret = register_ftrace_function_probe(glob, ops, count);
6028
6029 if (ret >= 0)
6030 alloc_snapshot(&global_trace);
6031
6032 return ret < 0 ? ret : 0;
6033}
6034
6035static struct ftrace_func_command ftrace_snapshot_cmd = {
6036 .name = "snapshot",
6037 .func = ftrace_trace_snapshot_callback,
6038};
6039
Tom Zanussi38de93a2013-10-24 08:34:18 -05006040static __init int register_snapshot_cmd(void)
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006041{
6042 return register_ftrace_command(&ftrace_snapshot_cmd);
6043}
6044#else
Tom Zanussi38de93a2013-10-24 08:34:18 -05006045static inline __init int register_snapshot_cmd(void) { return 0; }
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006046#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006047
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006048static struct dentry *tracing_get_dentry(struct trace_array *tr)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006049{
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006050 if (WARN_ON(!tr->dir))
6051 return ERR_PTR(-ENODEV);
6052
6053 /* Top directory uses NULL as the parent */
6054 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
6055 return NULL;
6056
6057 /* All sub buffers have a descriptor */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006058 return tr->dir;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006059}
6060
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006061static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
6062{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006063 struct dentry *d_tracer;
6064
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006065 if (tr->percpu_dir)
6066 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006067
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006068 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05006069 if (IS_ERR(d_tracer))
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006070 return NULL;
6071
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006072 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006073
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006074 WARN_ONCE(!tr->percpu_dir,
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006075 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006076
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006077 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006078}
6079
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006080static struct dentry *
6081trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
6082 void *data, long cpu, const struct file_operations *fops)
6083{
6084 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
6085
6086 if (ret) /* See tracing_get_cpu() */
David Howells7682c912015-03-17 22:26:16 +00006087 d_inode(ret)->i_cdev = (void *)(cpu + 1);
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006088 return ret;
6089}
6090
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006091static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006092tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006093{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006094 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006095 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04006096 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006097
Namhyung Kim0a3d7ce2012-04-23 10:11:57 +09006098 if (!d_percpu)
6099 return;
6100
Steven Rostedtdd49a382010-10-20 21:51:26 -04006101 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006102 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01006103 if (!d_cpu) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006104 pr_warning("Could not create tracefs '%s' entry\n", cpu_dir);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01006105 return;
6106 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006107
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01006108 /* per cpu trace_pipe */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006109 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
Oleg Nesterov15544202013-07-23 17:25:57 +02006110 tr, cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006111
6112 /* per cpu trace */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006113 trace_create_cpu_file("trace", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02006114 tr, cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04006115
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006116 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006117 tr, cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006118
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006119 trace_create_cpu_file("stats", 0444, d_cpu,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006120 tr, cpu, &tracing_stats_fops);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006121
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006122 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006123 tr, cpu, &tracing_entries_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006124
6125#ifdef CONFIG_TRACER_SNAPSHOT
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006126 trace_create_cpu_file("snapshot", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02006127 tr, cpu, &snapshot_fops);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006128
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006129 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006130 tr, cpu, &snapshot_raw_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006131#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006132}
6133
Steven Rostedt60a11772008-05-12 21:20:44 +02006134#ifdef CONFIG_FTRACE_SELFTEST
6135/* Let selftest have access to static functions in this file */
6136#include "trace_selftest.c"
6137#endif
6138
Steven Rostedt577b7852009-02-26 23:43:05 -05006139struct trace_option_dentry {
6140 struct tracer_opt *opt;
6141 struct tracer_flags *flags;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006142 struct trace_array *tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05006143 struct dentry *entry;
6144};
6145
6146static ssize_t
6147trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
6148 loff_t *ppos)
6149{
6150 struct trace_option_dentry *topt = filp->private_data;
6151 char *buf;
6152
6153 if (topt->flags->val & topt->opt->bit)
6154 buf = "1\n";
6155 else
6156 buf = "0\n";
6157
6158 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
6159}
6160
6161static ssize_t
6162trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
6163 loff_t *ppos)
6164{
6165 struct trace_option_dentry *topt = filp->private_data;
6166 unsigned long val;
Steven Rostedt577b7852009-02-26 23:43:05 -05006167 int ret;
6168
Peter Huewe22fe9b52011-06-07 21:58:27 +02006169 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6170 if (ret)
Steven Rostedt577b7852009-02-26 23:43:05 -05006171 return ret;
6172
Li Zefan8d18eaa2009-12-08 11:17:06 +08006173 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05006174 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08006175
6176 if (!!(topt->flags->val & topt->opt->bit) != val) {
6177 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05006178 ret = __set_tracer_option(topt->tr, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05006179 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08006180 mutex_unlock(&trace_types_lock);
6181 if (ret)
6182 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05006183 }
6184
6185 *ppos += cnt;
6186
6187 return cnt;
6188}
6189
6190
6191static const struct file_operations trace_options_fops = {
6192 .open = tracing_open_generic,
6193 .read = trace_options_read,
6194 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006195 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05006196};
6197
Steven Rostedta8259072009-02-26 22:19:12 -05006198static ssize_t
6199trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
6200 loff_t *ppos)
6201{
6202 long index = (long)filp->private_data;
6203 char *buf;
6204
6205 if (trace_flags & (1 << index))
6206 buf = "1\n";
6207 else
6208 buf = "0\n";
6209
6210 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
6211}
6212
6213static ssize_t
6214trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
6215 loff_t *ppos)
6216{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006217 struct trace_array *tr = &global_trace;
Steven Rostedta8259072009-02-26 22:19:12 -05006218 long index = (long)filp->private_data;
Steven Rostedta8259072009-02-26 22:19:12 -05006219 unsigned long val;
6220 int ret;
6221
Peter Huewe22fe9b52011-06-07 21:58:27 +02006222 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6223 if (ret)
Steven Rostedta8259072009-02-26 22:19:12 -05006224 return ret;
6225
Zhaoleif2d84b62009-08-07 18:55:48 +08006226 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05006227 return -EINVAL;
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04006228
6229 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006230 ret = set_tracer_flag(tr, 1 << index, val);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04006231 mutex_unlock(&trace_types_lock);
Steven Rostedta8259072009-02-26 22:19:12 -05006232
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04006233 if (ret < 0)
6234 return ret;
6235
Steven Rostedta8259072009-02-26 22:19:12 -05006236 *ppos += cnt;
6237
6238 return cnt;
6239}
6240
Steven Rostedta8259072009-02-26 22:19:12 -05006241static const struct file_operations trace_options_core_fops = {
6242 .open = tracing_open_generic,
6243 .read = trace_options_core_read,
6244 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006245 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05006246};
6247
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006248struct dentry *trace_create_file(const char *name,
Al Virof4ae40a62011-07-24 04:33:43 -04006249 umode_t mode,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006250 struct dentry *parent,
6251 void *data,
6252 const struct file_operations *fops)
6253{
6254 struct dentry *ret;
6255
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006256 ret = tracefs_create_file(name, mode, parent, data, fops);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006257 if (!ret)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006258 pr_warning("Could not create tracefs '%s' entry\n", name);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006259
6260 return ret;
6261}
6262
6263
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006264static struct dentry *trace_options_init_dentry(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05006265{
6266 struct dentry *d_tracer;
Steven Rostedta8259072009-02-26 22:19:12 -05006267
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006268 if (tr->options)
6269 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05006270
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006271 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05006272 if (IS_ERR(d_tracer))
Steven Rostedta8259072009-02-26 22:19:12 -05006273 return NULL;
6274
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006275 tr->options = tracefs_create_dir("options", d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006276 if (!tr->options) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006277 pr_warning("Could not create tracefs directory 'options'\n");
Steven Rostedta8259072009-02-26 22:19:12 -05006278 return NULL;
6279 }
6280
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006281 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05006282}
6283
Steven Rostedt577b7852009-02-26 23:43:05 -05006284static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006285create_trace_option_file(struct trace_array *tr,
6286 struct trace_option_dentry *topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05006287 struct tracer_flags *flags,
6288 struct tracer_opt *opt)
6289{
6290 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05006291
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006292 t_options = trace_options_init_dentry(tr);
Steven Rostedt577b7852009-02-26 23:43:05 -05006293 if (!t_options)
6294 return;
6295
6296 topt->flags = flags;
6297 topt->opt = opt;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006298 topt->tr = tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05006299
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006300 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05006301 &trace_options_fops);
6302
Steven Rostedt577b7852009-02-26 23:43:05 -05006303}
6304
6305static struct trace_option_dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006306create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
Steven Rostedt577b7852009-02-26 23:43:05 -05006307{
6308 struct trace_option_dentry *topts;
6309 struct tracer_flags *flags;
6310 struct tracer_opt *opts;
6311 int cnt;
6312
6313 if (!tracer)
6314 return NULL;
6315
6316 flags = tracer->flags;
6317
6318 if (!flags || !flags->opts)
6319 return NULL;
6320
6321 opts = flags->opts;
6322
6323 for (cnt = 0; opts[cnt].name; cnt++)
6324 ;
6325
Steven Rostedt0cfe8242009-02-27 10:51:10 -05006326 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05006327 if (!topts)
6328 return NULL;
6329
6330 for (cnt = 0; opts[cnt].name; cnt++)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006331 create_trace_option_file(tr, &topts[cnt], flags,
Steven Rostedt577b7852009-02-26 23:43:05 -05006332 &opts[cnt]);
6333
6334 return topts;
6335}
6336
6337static void
6338destroy_trace_option_files(struct trace_option_dentry *topts)
6339{
6340 int cnt;
6341
6342 if (!topts)
6343 return;
6344
Fabian Frederick3f4d8f72014-06-26 19:14:31 +02006345 for (cnt = 0; topts[cnt].opt; cnt++)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006346 tracefs_remove(topts[cnt].entry);
Steven Rostedt577b7852009-02-26 23:43:05 -05006347
6348 kfree(topts);
6349}
6350
Steven Rostedta8259072009-02-26 22:19:12 -05006351static struct dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006352create_trace_option_core_file(struct trace_array *tr,
6353 const char *option, long index)
Steven Rostedta8259072009-02-26 22:19:12 -05006354{
6355 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05006356
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006357 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05006358 if (!t_options)
6359 return NULL;
6360
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006361 return trace_create_file(option, 0644, t_options, (void *)index,
Steven Rostedta8259072009-02-26 22:19:12 -05006362 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05006363}
6364
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006365static __init void create_trace_options_dir(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05006366{
6367 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05006368 int i;
6369
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006370 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05006371 if (!t_options)
6372 return;
6373
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006374 for (i = 0; trace_options[i]; i++)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006375 create_trace_option_core_file(tr, trace_options[i], i);
Steven Rostedta8259072009-02-26 22:19:12 -05006376}
6377
Steven Rostedt499e5472012-02-22 15:50:28 -05006378static ssize_t
6379rb_simple_read(struct file *filp, char __user *ubuf,
6380 size_t cnt, loff_t *ppos)
6381{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04006382 struct trace_array *tr = filp->private_data;
Steven Rostedt499e5472012-02-22 15:50:28 -05006383 char buf[64];
6384 int r;
6385
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04006386 r = tracer_tracing_is_on(tr);
Steven Rostedt499e5472012-02-22 15:50:28 -05006387 r = sprintf(buf, "%d\n", r);
6388
6389 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6390}
6391
6392static ssize_t
6393rb_simple_write(struct file *filp, const char __user *ubuf,
6394 size_t cnt, loff_t *ppos)
6395{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04006396 struct trace_array *tr = filp->private_data;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006397 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt499e5472012-02-22 15:50:28 -05006398 unsigned long val;
6399 int ret;
6400
6401 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6402 if (ret)
6403 return ret;
6404
6405 if (buffer) {
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05006406 mutex_lock(&trace_types_lock);
6407 if (val) {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04006408 tracer_tracing_on(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006409 if (tr->current_trace->start)
6410 tr->current_trace->start(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05006411 } else {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04006412 tracer_tracing_off(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006413 if (tr->current_trace->stop)
6414 tr->current_trace->stop(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05006415 }
6416 mutex_unlock(&trace_types_lock);
Steven Rostedt499e5472012-02-22 15:50:28 -05006417 }
6418
6419 (*ppos)++;
6420
6421 return cnt;
6422}
6423
6424static const struct file_operations rb_simple_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006425 .open = tracing_open_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05006426 .read = rb_simple_read,
6427 .write = rb_simple_write,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006428 .release = tracing_release_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05006429 .llseek = default_llseek,
6430};
6431
Steven Rostedt277ba042012-08-03 16:10:49 -04006432struct dentry *trace_instance_dir;
6433
6434static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006435init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
Steven Rostedt277ba042012-08-03 16:10:49 -04006436
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006437static int
6438allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
Steven Rostedt277ba042012-08-03 16:10:49 -04006439{
6440 enum ring_buffer_flags rb_flags;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006441
6442 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
6443
Steven Rostedt (Red Hat)dced3412014-01-14 10:19:46 -05006444 buf->tr = tr;
6445
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006446 buf->buffer = ring_buffer_alloc(size, rb_flags);
6447 if (!buf->buffer)
6448 return -ENOMEM;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006449
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006450 buf->data = alloc_percpu(struct trace_array_cpu);
6451 if (!buf->data) {
6452 ring_buffer_free(buf->buffer);
6453 return -ENOMEM;
6454 }
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006455
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006456 /* Allocate the first page for all buffers */
6457 set_buffer_entries(&tr->trace_buffer,
6458 ring_buffer_size(tr->trace_buffer.buffer, 0));
6459
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006460 return 0;
6461}
6462
6463static int allocate_trace_buffers(struct trace_array *tr, int size)
6464{
6465 int ret;
6466
6467 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
6468 if (ret)
6469 return ret;
6470
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006471#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006472 ret = allocate_trace_buffer(tr, &tr->max_buffer,
6473 allocate_snapshot ? size : 1);
6474 if (WARN_ON(ret)) {
6475 ring_buffer_free(tr->trace_buffer.buffer);
6476 free_percpu(tr->trace_buffer.data);
6477 return -ENOMEM;
6478 }
6479 tr->allocated_snapshot = allocate_snapshot;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006480
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006481 /*
6482 * Only the top level trace array gets its snapshot allocated
6483 * from the kernel command line.
6484 */
6485 allocate_snapshot = false;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006486#endif
6487 return 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006488}
6489
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04006490static void free_trace_buffer(struct trace_buffer *buf)
6491{
6492 if (buf->buffer) {
6493 ring_buffer_free(buf->buffer);
6494 buf->buffer = NULL;
6495 free_percpu(buf->data);
6496 buf->data = NULL;
6497 }
6498}
6499
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04006500static void free_trace_buffers(struct trace_array *tr)
6501{
6502 if (!tr)
6503 return;
6504
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04006505 free_trace_buffer(&tr->trace_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04006506
6507#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04006508 free_trace_buffer(&tr->max_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04006509#endif
6510}
6511
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05006512static int instance_mkdir(const char *name)
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006513{
Steven Rostedt277ba042012-08-03 16:10:49 -04006514 struct trace_array *tr;
6515 int ret;
Steven Rostedt277ba042012-08-03 16:10:49 -04006516
6517 mutex_lock(&trace_types_lock);
6518
6519 ret = -EEXIST;
6520 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6521 if (tr->name && strcmp(tr->name, name) == 0)
6522 goto out_unlock;
6523 }
6524
6525 ret = -ENOMEM;
6526 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
6527 if (!tr)
6528 goto out_unlock;
6529
6530 tr->name = kstrdup(name, GFP_KERNEL);
6531 if (!tr->name)
6532 goto out_free_tr;
6533
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07006534 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
6535 goto out_free_tr;
6536
6537 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
6538
Steven Rostedt277ba042012-08-03 16:10:49 -04006539 raw_spin_lock_init(&tr->start_lock);
6540
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05006541 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
6542
Steven Rostedt277ba042012-08-03 16:10:49 -04006543 tr->current_trace = &nop_trace;
6544
6545 INIT_LIST_HEAD(&tr->systems);
6546 INIT_LIST_HEAD(&tr->events);
6547
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006548 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
Steven Rostedt277ba042012-08-03 16:10:49 -04006549 goto out_free_tr;
6550
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006551 tr->dir = tracefs_create_dir(name, trace_instance_dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04006552 if (!tr->dir)
6553 goto out_free_tr;
6554
6555 ret = event_trace_add_tracer(tr->dir, tr);
Alexander Z Lam609e85a2013-07-10 17:34:34 -07006556 if (ret) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006557 tracefs_remove_recursive(tr->dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04006558 goto out_free_tr;
Alexander Z Lam609e85a2013-07-10 17:34:34 -07006559 }
Steven Rostedt277ba042012-08-03 16:10:49 -04006560
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006561 init_tracer_tracefs(tr, tr->dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04006562
6563 list_add(&tr->list, &ftrace_trace_arrays);
6564
6565 mutex_unlock(&trace_types_lock);
6566
6567 return 0;
6568
6569 out_free_tr:
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04006570 free_trace_buffers(tr);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07006571 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt277ba042012-08-03 16:10:49 -04006572 kfree(tr->name);
6573 kfree(tr);
6574
6575 out_unlock:
6576 mutex_unlock(&trace_types_lock);
6577
6578 return ret;
6579
6580}
6581
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05006582static int instance_rmdir(const char *name)
Steven Rostedt0c8916c2012-08-07 16:14:16 -04006583{
6584 struct trace_array *tr;
6585 int found = 0;
6586 int ret;
6587
6588 mutex_lock(&trace_types_lock);
6589
6590 ret = -ENODEV;
6591 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6592 if (tr->name && strcmp(tr->name, name) == 0) {
6593 found = 1;
6594 break;
6595 }
6596 }
6597 if (!found)
6598 goto out_unlock;
6599
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006600 ret = -EBUSY;
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05006601 if (tr->ref || (tr->current_trace && tr->current_trace->ref))
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006602 goto out_unlock;
6603
Steven Rostedt0c8916c2012-08-07 16:14:16 -04006604 list_del(&tr->list);
6605
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05006606 tracing_set_nop(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04006607 event_trace_del_tracer(tr);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006608 ftrace_destroy_function_files(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04006609 debugfs_remove_recursive(tr->dir);
Steven Rostedt (Red Hat)a9fcaaa2014-06-06 23:17:28 -04006610 free_trace_buffers(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04006611
6612 kfree(tr->name);
6613 kfree(tr);
6614
6615 ret = 0;
6616
6617 out_unlock:
6618 mutex_unlock(&trace_types_lock);
6619
6620 return ret;
6621}
6622
Steven Rostedt277ba042012-08-03 16:10:49 -04006623static __init void create_trace_instances(struct dentry *d_tracer)
6624{
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05006625 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
6626 instance_mkdir,
6627 instance_rmdir);
Steven Rostedt277ba042012-08-03 16:10:49 -04006628 if (WARN_ON(!trace_instance_dir))
6629 return;
Steven Rostedt277ba042012-08-03 16:10:49 -04006630}
6631
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006632static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006633init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006634{
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05006635 int cpu;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006636
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05006637 trace_create_file("available_tracers", 0444, d_tracer,
6638 tr, &show_traces_fops);
6639
6640 trace_create_file("current_tracer", 0644, d_tracer,
6641 tr, &set_tracer_fops);
6642
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07006643 trace_create_file("tracing_cpumask", 0644, d_tracer,
6644 tr, &tracing_cpumask_fops);
6645
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006646 trace_create_file("trace_options", 0644, d_tracer,
6647 tr, &tracing_iter_fops);
6648
6649 trace_create_file("trace", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02006650 tr, &tracing_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006651
6652 trace_create_file("trace_pipe", 0444, d_tracer,
Oleg Nesterov15544202013-07-23 17:25:57 +02006653 tr, &tracing_pipe_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006654
6655 trace_create_file("buffer_size_kb", 0644, d_tracer,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006656 tr, &tracing_entries_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006657
6658 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
6659 tr, &tracing_total_entries_fops);
6660
Wang YanQing238ae932013-05-26 16:52:01 +08006661 trace_create_file("free_buffer", 0200, d_tracer,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006662 tr, &tracing_free_buffer_fops);
6663
6664 trace_create_file("trace_marker", 0220, d_tracer,
6665 tr, &tracing_mark_fops);
6666
6667 trace_create_file("trace_clock", 0644, d_tracer, tr,
6668 &trace_clock_fops);
6669
6670 trace_create_file("tracing_on", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02006671 tr, &rb_simple_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05006672
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05006673#ifdef CONFIG_TRACER_MAX_TRACE
6674 trace_create_file("tracing_max_latency", 0644, d_tracer,
6675 &tr->max_latency, &tracing_max_lat_fops);
6676#endif
6677
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006678 if (ftrace_create_function_files(tr, d_tracer))
6679 WARN(1, "Could not allocate function filter files");
6680
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05006681#ifdef CONFIG_TRACER_SNAPSHOT
6682 trace_create_file("snapshot", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02006683 tr, &snapshot_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05006684#endif
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05006685
6686 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006687 tracing_init_tracefs_percpu(tr, cpu);
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05006688
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006689}
6690
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05006691static struct vfsmount *trace_automount(void *ingore)
6692{
6693 struct vfsmount *mnt;
6694 struct file_system_type *type;
6695
6696 /*
6697 * To maintain backward compatibility for tools that mount
6698 * debugfs to get to the tracing facility, tracefs is automatically
6699 * mounted to the debugfs/tracing directory.
6700 */
6701 type = get_fs_type("tracefs");
6702 if (!type)
6703 return NULL;
6704 mnt = vfs_kern_mount(type, 0, "tracefs", NULL);
6705 put_filesystem(type);
6706 if (IS_ERR(mnt))
6707 return NULL;
6708 mntget(mnt);
6709
6710 return mnt;
6711}
6712
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006713/**
6714 * tracing_init_dentry - initialize top level trace array
6715 *
6716 * This is called when creating files or directories in the tracing
6717 * directory. It is called via fs_initcall() by any of the boot up code
6718 * and expects to return the dentry of the top level tracing directory.
6719 */
6720struct dentry *tracing_init_dentry(void)
6721{
6722 struct trace_array *tr = &global_trace;
6723
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05006724 /* The top level trace array uses NULL as parent */
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006725 if (tr->dir)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05006726 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006727
6728 if (WARN_ON(!debugfs_initialized()))
6729 return ERR_PTR(-ENODEV);
6730
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05006731 /*
6732 * As there may still be users that expect the tracing
6733 * files to exist in debugfs/tracing, we must automount
6734 * the tracefs file system there, so older tools still
6735 * work with the newer kerenl.
6736 */
6737 tr->dir = debugfs_create_automount("tracing", NULL,
6738 trace_automount, NULL);
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006739 if (!tr->dir) {
6740 pr_warn_once("Could not create debugfs directory 'tracing'\n");
6741 return ERR_PTR(-ENOMEM);
6742 }
6743
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006744 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006745}
6746
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04006747extern struct trace_enum_map *__start_ftrace_enum_maps[];
6748extern struct trace_enum_map *__stop_ftrace_enum_maps[];
6749
6750static void __init trace_enum_init(void)
6751{
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04006752 int len;
6753
6754 len = __stop_ftrace_enum_maps - __start_ftrace_enum_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04006755 trace_insert_enum_map(NULL, __start_ftrace_enum_maps, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04006756}
6757
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04006758#ifdef CONFIG_MODULES
6759static void trace_module_add_enums(struct module *mod)
6760{
6761 if (!mod->num_trace_enums)
6762 return;
6763
6764 /*
6765 * Modules with bad taint do not have events created, do
6766 * not bother with enums either.
6767 */
6768 if (trace_module_has_bad_taint(mod))
6769 return;
6770
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04006771 trace_insert_enum_map(mod, mod->trace_enums, mod->num_trace_enums);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04006772}
6773
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04006774#ifdef CONFIG_TRACE_ENUM_MAP_FILE
6775static void trace_module_remove_enums(struct module *mod)
6776{
6777 union trace_enum_map_item *map;
6778 union trace_enum_map_item **last = &trace_enum_maps;
6779
6780 if (!mod->num_trace_enums)
6781 return;
6782
6783 mutex_lock(&trace_enum_mutex);
6784
6785 map = trace_enum_maps;
6786
6787 while (map) {
6788 if (map->head.mod == mod)
6789 break;
6790 map = trace_enum_jmp_to_tail(map);
6791 last = &map->tail.next;
6792 map = map->tail.next;
6793 }
6794 if (!map)
6795 goto out;
6796
6797 *last = trace_enum_jmp_to_tail(map)->tail.next;
6798 kfree(map);
6799 out:
6800 mutex_unlock(&trace_enum_mutex);
6801}
6802#else
6803static inline void trace_module_remove_enums(struct module *mod) { }
6804#endif /* CONFIG_TRACE_ENUM_MAP_FILE */
6805
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04006806static int trace_module_notify(struct notifier_block *self,
6807 unsigned long val, void *data)
6808{
6809 struct module *mod = data;
6810
6811 switch (val) {
6812 case MODULE_STATE_COMING:
6813 trace_module_add_enums(mod);
6814 break;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04006815 case MODULE_STATE_GOING:
6816 trace_module_remove_enums(mod);
6817 break;
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04006818 }
6819
6820 return 0;
6821}
6822
6823static struct notifier_block trace_module_nb = {
6824 .notifier_call = trace_module_notify,
6825 .priority = 0,
6826};
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04006827#endif /* CONFIG_MODULES */
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04006828
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006829static __init int tracer_init_tracefs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006830{
6831 struct dentry *d_tracer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006832
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006833 trace_access_lock_init();
6834
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006835 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05006836 if (IS_ERR(d_tracer))
Namhyung Kimed6f1c92013-04-10 09:18:12 +09006837 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006838
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006839 init_tracer_tracefs(&global_trace, d_tracer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006840
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006841 trace_create_file("tracing_thresh", 0644, d_tracer,
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04006842 &global_trace, &tracing_thresh_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006843
Li Zefan339ae5d2009-04-17 10:34:30 +08006844 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006845 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02006846
Avadh Patel69abe6a2009-04-10 16:04:48 -04006847 trace_create_file("saved_cmdlines", 0444, d_tracer,
6848 NULL, &tracing_saved_cmdlines_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006849
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09006850 trace_create_file("saved_cmdlines_size", 0644, d_tracer,
6851 NULL, &tracing_saved_cmdlines_size_fops);
6852
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04006853 trace_enum_init();
6854
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04006855 trace_create_enum_file(d_tracer);
6856
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04006857#ifdef CONFIG_MODULES
6858 register_module_notifier(&trace_module_nb);
6859#endif
6860
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006861#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006862 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6863 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006864#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006865
Steven Rostedt277ba042012-08-03 16:10:49 -04006866 create_trace_instances(d_tracer);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006867
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006868 create_trace_options_dir(&global_trace);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006869
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05006870 /* If the tracer was started via cmdline, create options for it here */
6871 if (global_trace.current_trace != &nop_trace)
6872 update_tracer_options(&global_trace, global_trace.current_trace);
6873
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01006874 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006875}
6876
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006877static int trace_panic_handler(struct notifier_block *this,
6878 unsigned long event, void *unused)
6879{
Steven Rostedt944ac422008-10-23 19:26:08 -04006880 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02006881 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006882 return NOTIFY_OK;
6883}
6884
6885static struct notifier_block trace_panic_notifier = {
6886 .notifier_call = trace_panic_handler,
6887 .next = NULL,
6888 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6889};
6890
6891static int trace_die_handler(struct notifier_block *self,
6892 unsigned long val,
6893 void *data)
6894{
6895 switch (val) {
6896 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04006897 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02006898 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006899 break;
6900 default:
6901 break;
6902 }
6903 return NOTIFY_OK;
6904}
6905
6906static struct notifier_block trace_die_notifier = {
6907 .notifier_call = trace_die_handler,
6908 .priority = 200
6909};
6910
6911/*
6912 * printk is set to max of 1024, we really don't need it that big.
6913 * Nothing should be printing 1000 characters anyway.
6914 */
6915#define TRACE_MAX_PRINT 1000
6916
6917/*
6918 * Define here KERN_TRACE so that we have one place to modify
6919 * it if we decide to change what log level the ftrace dump
6920 * should be at.
6921 */
Steven Rostedt428aee12009-01-14 12:24:42 -05006922#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006923
Jason Wessel955b61e2010-08-05 09:22:23 -05006924void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006925trace_printk_seq(struct trace_seq *s)
6926{
6927 /* Probably should print a warning here. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04006928 if (s->seq.len >= TRACE_MAX_PRINT)
6929 s->seq.len = TRACE_MAX_PRINT;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006930
Steven Rostedt (Red Hat)820b75f2014-11-19 10:56:41 -05006931 /*
6932 * More paranoid code. Although the buffer size is set to
6933 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
6934 * an extra layer of protection.
6935 */
6936 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
6937 s->seq.len = s->seq.size - 1;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006938
6939 /* should be zero ended, but we are paranoid. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04006940 s->buffer[s->seq.len] = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006941
6942 printk(KERN_TRACE "%s", s->buffer);
6943
Steven Rostedtf9520752009-03-02 14:04:40 -05006944 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006945}
6946
Jason Wessel955b61e2010-08-05 09:22:23 -05006947void trace_init_global_iter(struct trace_iterator *iter)
6948{
6949 iter->tr = &global_trace;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006950 iter->trace = iter->tr->current_trace;
Steven Rostedtae3b5092013-01-23 15:22:59 -05006951 iter->cpu_file = RING_BUFFER_ALL_CPUS;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006952 iter->trace_buffer = &global_trace.trace_buffer;
Cody P Schaferb2f974d2013-10-23 11:49:57 -07006953
6954 if (iter->trace && iter->trace->open)
6955 iter->trace->open(iter);
6956
6957 /* Annotate start of buffers if we had overruns */
6958 if (ring_buffer_overruns(iter->trace_buffer->buffer))
6959 iter->iter_flags |= TRACE_FILE_ANNOTATE;
6960
6961 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
6962 if (trace_clocks[iter->tr->clock_id].in_ns)
6963 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
Jason Wessel955b61e2010-08-05 09:22:23 -05006964}
6965
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006966void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006967{
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006968 /* use static because iter can be a bit big for the stack */
6969 static struct trace_iterator iter;
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006970 static atomic_t dump_running;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01006971 unsigned int old_userobj;
Steven Rostedtd7690412008-10-01 00:29:53 -04006972 unsigned long flags;
6973 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006974
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006975 /* Only allow one dump user at a time. */
6976 if (atomic_inc_return(&dump_running) != 1) {
6977 atomic_dec(&dump_running);
6978 return;
Steven Rostedte0a413f2011-09-29 21:26:16 -04006979 }
6980
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04006981 /*
6982 * Always turn off tracing when we dump.
6983 * We don't need to show trace output of what happens
6984 * between multiple crashes.
6985 *
6986 * If the user does a sysrq-z, then they can re-enable
6987 * tracing with echo 1 > tracing_on.
6988 */
6989 tracing_off();
6990
6991 local_irq_save(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04006992
Jovi Zhang38dbe0b2013-01-25 18:03:07 +08006993 /* Simulate the iterator */
Jason Wessel955b61e2010-08-05 09:22:23 -05006994 trace_init_global_iter(&iter);
6995
Steven Rostedtd7690412008-10-01 00:29:53 -04006996 for_each_tracing_cpu(cpu) {
Umesh Tiwari5e2d5ef2015-06-22 16:55:06 +05306997 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04006998 }
6999
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007000 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
7001
Török Edwinb54d3de2008-11-22 13:28:48 +02007002 /* don't look at user memory in panic mode */
7003 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
7004
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007005 switch (oops_dump_mode) {
7006 case DUMP_ALL:
Steven Rostedtae3b5092013-01-23 15:22:59 -05007007 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007008 break;
7009 case DUMP_ORIG:
7010 iter.cpu_file = raw_smp_processor_id();
7011 break;
7012 case DUMP_NONE:
7013 goto out_enable;
7014 default:
7015 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
Steven Rostedtae3b5092013-01-23 15:22:59 -05007016 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007017 }
7018
7019 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007020
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007021 /* Did function tracer already get disabled? */
7022 if (ftrace_is_dead()) {
7023 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
7024 printk("# MAY BE MISSING FUNCTION EVENTS\n");
7025 }
7026
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007027 /*
7028 * We need to stop all tracing on all CPUS to read the
7029 * the next buffer. This is a bit expensive, but is
7030 * not done often. We fill all what we can read,
7031 * and then release the locks again.
7032 */
7033
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007034 while (!trace_empty(&iter)) {
7035
7036 if (!cnt)
7037 printk(KERN_TRACE "---------------------------------\n");
7038
7039 cnt++;
7040
7041 /* reset all but tr, trace, and overruns */
7042 memset(&iter.seq, 0,
7043 sizeof(struct trace_iterator) -
7044 offsetof(struct trace_iterator, seq));
7045 iter.iter_flags |= TRACE_FILE_LAT_FMT;
7046 iter.pos = -1;
7047
Jason Wessel955b61e2010-08-05 09:22:23 -05007048 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08007049 int ret;
7050
7051 ret = print_trace_line(&iter);
7052 if (ret != TRACE_TYPE_NO_CONSUME)
7053 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007054 }
Steven Rostedtb892e5c2012-03-01 22:06:48 -05007055 touch_nmi_watchdog();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007056
7057 trace_printk_seq(&iter.seq);
7058 }
7059
7060 if (!cnt)
7061 printk(KERN_TRACE " (ftrace buffer empty)\n");
7062 else
7063 printk(KERN_TRACE "---------------------------------\n");
7064
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007065 out_enable:
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007066 trace_flags |= old_userobj;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007067
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007068 for_each_tracing_cpu(cpu) {
7069 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007070 }
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007071 atomic_dec(&dump_running);
Steven Rostedtcd891ae2009-04-28 11:39:34 -04007072 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007073}
Paul E. McKenneya8eecf22011-10-02 11:01:15 -07007074EXPORT_SYMBOL_GPL(ftrace_dump);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007075
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007076__init static int tracer_alloc_buffers(void)
7077{
Steven Rostedt73c51622009-03-11 13:42:01 -04007078 int ring_buf_size;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307079 int ret = -ENOMEM;
7080
7081 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
7082 goto out;
7083
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007084 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307085 goto out_free_buffer_mask;
7086
Steven Rostedt07d777f2011-09-22 14:01:55 -04007087 /* Only allocate trace_printk buffers if a trace_printk exists */
7088 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
Steven Rostedt81698832012-10-11 10:15:05 -04007089 /* Must be called before global_trace.buffer is allocated */
Steven Rostedt07d777f2011-09-22 14:01:55 -04007090 trace_printk_init_buffers();
7091
Steven Rostedt73c51622009-03-11 13:42:01 -04007092 /* To save memory, keep the ring buffer size to its minimum */
7093 if (ring_buffer_expanded)
7094 ring_buf_size = trace_buf_size;
7095 else
7096 ring_buf_size = 1;
7097
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307098 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007099 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007100
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007101 raw_spin_lock_init(&global_trace.start_lock);
7102
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04007103 /* Used for event triggers */
7104 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
7105 if (!temp_buffer)
7106 goto out_free_cpumask;
7107
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09007108 if (trace_create_savedcmd() < 0)
7109 goto out_free_temp_buffer;
7110
Steven Rostedtab464282008-05-12 21:21:00 +02007111 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007112 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007113 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
7114 WARN_ON(1);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09007115 goto out_free_savedcmd;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007116 }
Steven Rostedta7603ff2012-08-06 16:24:11 -04007117
Steven Rostedt499e5472012-02-22 15:50:28 -05007118 if (global_trace.buffer_disabled)
7119 tracing_off();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007120
Steven Rostedte1e232c2014-02-10 23:38:46 -05007121 if (trace_boot_clock) {
7122 ret = tracing_set_clock(&global_trace, trace_boot_clock);
7123 if (ret < 0)
7124 pr_warning("Trace clock %s not defined, going back to default\n",
7125 trace_boot_clock);
7126 }
7127
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04007128 /*
7129 * register_tracer() might reference current_trace, so it
7130 * needs to be set before we register anything. This is
7131 * just a bootstrap of current_trace anyway.
7132 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007133 global_trace.current_trace = &nop_trace;
7134
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05007135 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
7136
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007137 ftrace_init_global_array_ops(&global_trace);
7138
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04007139 register_tracer(&nop_trace);
7140
Steven Rostedt60a11772008-05-12 21:20:44 +02007141 /* All seems OK, enable tracing */
7142 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007143
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007144 atomic_notifier_chain_register(&panic_notifier_list,
7145 &trace_panic_notifier);
7146
7147 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01007148
Steven Rostedtae63b31e2012-05-03 23:09:03 -04007149 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
7150
7151 INIT_LIST_HEAD(&global_trace.systems);
7152 INIT_LIST_HEAD(&global_trace.events);
7153 list_add(&global_trace.list, &ftrace_trace_arrays);
7154
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04007155 while (trace_boot_options) {
7156 char *option;
7157
7158 option = strsep(&trace_boot_options, ",");
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007159 trace_set_options(&global_trace, option);
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04007160 }
7161
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007162 register_snapshot_cmd();
7163
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01007164 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007165
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09007166out_free_savedcmd:
7167 free_saved_cmdlines_buffer(savedcmd);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04007168out_free_temp_buffer:
7169 ring_buffer_free(temp_buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307170out_free_cpumask:
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007171 free_cpumask_var(global_trace.tracing_cpumask);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307172out_free_buffer_mask:
7173 free_cpumask_var(tracing_buffer_mask);
7174out:
7175 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007176}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05007177
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05007178void __init trace_init(void)
7179{
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05007180 if (tracepoint_printk) {
7181 tracepoint_print_iter =
7182 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
7183 if (WARN_ON(!tracepoint_print_iter))
7184 tracepoint_printk = 0;
7185 }
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05007186 tracer_alloc_buffers();
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04007187 trace_event_init();
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05007188}
7189
Steven Rostedtb2821ae2009-02-02 21:38:32 -05007190__init static int clear_boot_tracer(void)
7191{
7192 /*
7193 * The default tracer at boot buffer is an init section.
7194 * This function is called in lateinit. If we did not
7195 * find the boot tracer, then clear it out, to prevent
7196 * later registration from accessing the buffer that is
7197 * about to be freed.
7198 */
7199 if (!default_bootup_tracer)
7200 return 0;
7201
7202 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
7203 default_bootup_tracer);
7204 default_bootup_tracer = NULL;
7205
7206 return 0;
7207}
7208
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007209fs_initcall(tracer_init_tracefs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05007210late_initcall(clear_boot_tracer);