blob: d382fd1aa4a67587932dd59e9c582034adf93684 [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002/*
3 * ring buffer based function tracer
4 *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 *
8 * Originally taken from the RT patch by:
9 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 *
11 * Based on code from the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020014 */
Steven Rostedt2cadf912008-12-01 22:20:19 -050015#include <linux/ring_buffer.h>
Sam Ravnborg273b2812009-10-18 00:52:28 +020016#include <generated/utsrelease.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050017#include <linux/stacktrace.h>
18#include <linux/writeback.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020019#include <linux/kallsyms.h>
20#include <linux/seq_file.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040021#include <linux/notifier.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050022#include <linux/irqflags.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020023#include <linux/debugfs.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050024#include <linux/tracefs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020025#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020026#include <linux/hardirq.h>
27#include <linux/linkage.h>
28#include <linux/uaccess.h>
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -040029#include <linux/vmalloc.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020030#include <linux/ftrace.h>
31#include <linux/module.h>
32#include <linux/percpu.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050033#include <linux/splice.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040034#include <linux/kdebug.h>
Frederic Weisbecker5f0c6c02009-03-27 14:22:10 +010035#include <linux/string.h>
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -050036#include <linux/mount.h>
Lai Jiangshan7e53bd42010-01-06 20:08:50 +080037#include <linux/rwsem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020039#include <linux/ctype.h>
40#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020041#include <linux/poll.h>
Steven Rostedtb892e5c2012-03-01 22:06:48 -050042#include <linux/nmi.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020043#include <linux/fs.h>
Chunyan Zhang478409d2016-11-21 15:57:18 +080044#include <linux/trace.h>
Chris Wilson3fd49c92018-03-30 16:01:31 +010045#include <linux/sched/clock.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060046#include <linux/sched/rt.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020047
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020048#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050049#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020050
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010051/*
Steven Rostedt73c51622009-03-11 13:42:01 -040052 * On boot up, the ring buffer is set to the minimum size, so that
53 * we do not waste memory on systems that are not using tracing.
54 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -050055bool ring_buffer_expanded;
Steven Rostedt73c51622009-03-11 13:42:01 -040056
57/*
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010058 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010059 * A selftest will lurk into the ring-buffer to count the
60 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010061 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010062 * at the same time, giving false positive or negative results.
63 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010064static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010065
Steven Rostedtb2821ae2009-02-02 21:38:32 -050066/*
67 * If a tracer is running, we do not want to run SELFTEST.
68 */
Li Zefan020e5f82009-07-01 10:47:05 +080069bool __read_mostly tracing_selftest_disabled;
Steven Rostedtb2821ae2009-02-02 21:38:32 -050070
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -050071/* Pipe tracepoints to printk */
72struct trace_iterator *tracepoint_print_iter;
73int tracepoint_printk;
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -050074static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -050075
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010076/* For tracers that don't implement custom flags */
77static struct tracer_opt dummy_tracer_opt[] = {
78 { }
79};
80
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -050081static int
82dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010083{
84 return 0;
85}
Steven Rostedt0f048702008-11-05 16:05:44 -050086
87/*
Steven Rostedt7ffbd482012-10-11 12:14:25 -040088 * To prevent the comm cache from being overwritten when no
89 * tracing is active, only save the comm when a trace event
90 * occurred.
91 */
Joel Fernandesd914ba32017-06-26 19:01:55 -070092static DEFINE_PER_CPU(bool, trace_taskinfo_save);
Steven Rostedt7ffbd482012-10-11 12:14:25 -040093
94/*
Steven Rostedt0f048702008-11-05 16:05:44 -050095 * Kill all tracing for good (never come back).
96 * It is initialized to 1 but will turn to zero if the initialization
97 * of the tracer is successful. But that is the only place that sets
98 * this back to zero.
99 */
Hannes Eder4fd27352009-02-10 19:44:12 +0100100static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -0500101
Jason Wessel955b61e2010-08-05 09:22:23 -0500102cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +0200103
Steven Rostedt944ac422008-10-23 19:26:08 -0400104/*
105 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
106 *
107 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
108 * is set, then ftrace_dump is called. This will output the contents
109 * of the ftrace buffers to the console. This is very useful for
110 * capturing traces that lead to crashes and outputing it to a
111 * serial console.
112 *
113 * It is default off, but you can enable it with either specifying
114 * "ftrace_dump_on_oops" in the kernel command line, or setting
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200115 * /proc/sys/kernel/ftrace_dump_on_oops
116 * Set 1 if you want to dump buffers of all CPUs
117 * Set 2 if you want to dump the buffer of the CPU that triggered oops
Steven Rostedt944ac422008-10-23 19:26:08 -0400118 */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200119
120enum ftrace_dump_mode ftrace_dump_on_oops;
Steven Rostedt944ac422008-10-23 19:26:08 -0400121
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400122/* When set, tracing will stop when a WARN*() is hit */
123int __disable_trace_on_warning;
124
Jeremy Linton681bec02017-05-31 16:56:53 -0500125#ifdef CONFIG_TRACE_EVAL_MAP_FILE
126/* Map of enums to their values, for "eval_map" file */
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500127struct trace_eval_map_head {
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400128 struct module *mod;
129 unsigned long length;
130};
131
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500132union trace_eval_map_item;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400133
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500134struct trace_eval_map_tail {
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400135 /*
136 * "end" is first and points to NULL as it must be different
Jeremy Linton00f4b652017-05-31 16:56:43 -0500137 * than "mod" or "eval_string"
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400138 */
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500139 union trace_eval_map_item *next;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400140 const char *end; /* points to NULL */
141};
142
Jeremy Linton1793ed92017-05-31 16:56:46 -0500143static DEFINE_MUTEX(trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400144
145/*
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500146 * The trace_eval_maps are saved in an array with two extra elements,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400147 * one at the beginning, and one at the end. The beginning item contains
148 * the count of the saved maps (head.length), and the module they
149 * belong to if not built in (head.mod). The ending item contains a
Jeremy Linton681bec02017-05-31 16:56:53 -0500150 * pointer to the next array of saved eval_map items.
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400151 */
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500152union trace_eval_map_item {
Jeremy Linton00f4b652017-05-31 16:56:43 -0500153 struct trace_eval_map map;
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500154 struct trace_eval_map_head head;
155 struct trace_eval_map_tail tail;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400156};
157
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500158static union trace_eval_map_item *trace_eval_maps;
Jeremy Linton681bec02017-05-31 16:56:53 -0500159#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400160
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -0500161static int tracing_set_tracer(struct trace_array *tr, const char *buf);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500162
Li Zefanee6c2c12009-09-18 14:06:47 +0800163#define MAX_TRACER_SIZE 100
164static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500165static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100166
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500167static bool allocate_snapshot;
168
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200169static int __init set_cmdline_ftrace(char *str)
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100170{
Chen Gang67012ab2013-04-08 12:06:44 +0800171 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500172 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400173 /* We are using ftrace early, expand it */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500174 ring_buffer_expanded = true;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100175 return 1;
176}
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200177__setup("ftrace=", set_cmdline_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100178
Steven Rostedt944ac422008-10-23 19:26:08 -0400179static int __init set_ftrace_dump_on_oops(char *str)
180{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200181 if (*str++ != '=' || !*str) {
182 ftrace_dump_on_oops = DUMP_ALL;
183 return 1;
184 }
185
186 if (!strcmp("orig_cpu", str)) {
187 ftrace_dump_on_oops = DUMP_ORIG;
188 return 1;
189 }
190
191 return 0;
Steven Rostedt944ac422008-10-23 19:26:08 -0400192}
193__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200194
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400195static int __init stop_trace_on_warning(char *str)
196{
Luis Claudio R. Goncalves933ff9f2014-11-12 21:14:00 -0200197 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
198 __disable_trace_on_warning = 1;
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400199 return 1;
200}
Luis Claudio R. Goncalves933ff9f2014-11-12 21:14:00 -0200201__setup("traceoff_on_warning", stop_trace_on_warning);
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400202
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400203static int __init boot_alloc_snapshot(char *str)
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500204{
205 allocate_snapshot = true;
206 /* We also need the main ring buffer expanded */
207 ring_buffer_expanded = true;
208 return 1;
209}
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400210__setup("alloc_snapshot", boot_alloc_snapshot);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500211
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400212
213static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400214
215static int __init set_trace_boot_options(char *str)
216{
Chen Gang67012ab2013-04-08 12:06:44 +0800217 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400218 return 0;
219}
220__setup("trace_options=", set_trace_boot_options);
221
Steven Rostedte1e232c2014-02-10 23:38:46 -0500222static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
223static char *trace_boot_clock __initdata;
224
225static int __init set_trace_boot_clock(char *str)
226{
227 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
228 trace_boot_clock = trace_boot_clock_buf;
229 return 0;
230}
231__setup("trace_clock=", set_trace_boot_clock);
232
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500233static int __init set_tracepoint_printk(char *str)
234{
235 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
236 tracepoint_printk = 1;
237 return 1;
238}
239__setup("tp_printk", set_tracepoint_printk);
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400240
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100241unsigned long long ns2usecs(u64 nsec)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200242{
243 nsec += 500;
244 do_div(nsec, 1000);
245 return nsec;
246}
247
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400248/* trace_flags holds trace_options default values */
249#define TRACE_DEFAULT_FLAGS \
250 (FUNCTION_DEFAULT_FLAGS | \
251 TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \
252 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \
253 TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \
254 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS)
255
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -0400256/* trace_options that are only supported by global_trace */
257#define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \
258 TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
259
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -0400260/* trace_flags that are default zero for instances */
261#define ZEROED_TRACE_FLAGS \
Namhyung Kim1e104862017-04-17 11:44:28 +0900262 (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK)
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -0400263
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200264/*
Joel Fernandes67d04bb2017-02-16 20:10:58 -0800265 * The global_trace is the descriptor that holds the top-level tracing
266 * buffers for the live tracing.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200267 */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400268static struct trace_array global_trace = {
269 .trace_flags = TRACE_DEFAULT_FLAGS,
270};
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200271
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400272LIST_HEAD(ftrace_trace_arrays);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200273
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -0400274int trace_array_get(struct trace_array *this_tr)
275{
276 struct trace_array *tr;
277 int ret = -ENODEV;
278
279 mutex_lock(&trace_types_lock);
280 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
281 if (tr == this_tr) {
282 tr->ref++;
283 ret = 0;
284 break;
285 }
286 }
287 mutex_unlock(&trace_types_lock);
288
289 return ret;
290}
291
292static void __trace_array_put(struct trace_array *this_tr)
293{
294 WARN_ON(!this_tr->ref);
295 this_tr->ref--;
296}
297
298void trace_array_put(struct trace_array *this_tr)
299{
300 mutex_lock(&trace_types_lock);
301 __trace_array_put(this_tr);
302 mutex_unlock(&trace_types_lock);
303}
304
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400305int call_filter_check_discard(struct trace_event_call *call, void *rec,
Tom Zanussif306cc82013-10-24 08:34:17 -0500306 struct ring_buffer *buffer,
307 struct ring_buffer_event *event)
308{
309 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
310 !filter_match_preds(call->filter, rec)) {
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -0400311 __trace_event_discard_commit(buffer, event);
Tom Zanussif306cc82013-10-24 08:34:17 -0500312 return 1;
313 }
314
315 return 0;
316}
Tom Zanussieb02ce02009-04-08 03:15:54 -0500317
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400318void trace_free_pid_list(struct trace_pid_list *pid_list)
319{
320 vfree(pid_list->pids);
321 kfree(pid_list);
322}
323
Steven Rostedtd8275c42016-04-14 12:15:22 -0400324/**
325 * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
326 * @filtered_pids: The list of pids to check
327 * @search_pid: The PID to find in @filtered_pids
328 *
329 * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis.
330 */
331bool
332trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
333{
334 /*
335 * If pid_max changed after filtered_pids was created, we
336 * by default ignore all pids greater than the previous pid_max.
337 */
338 if (search_pid >= filtered_pids->pid_max)
339 return false;
340
341 return test_bit(search_pid, filtered_pids->pids);
342}
343
344/**
345 * trace_ignore_this_task - should a task be ignored for tracing
346 * @filtered_pids: The list of pids to check
347 * @task: The task that should be ignored if not filtered
348 *
349 * Checks if @task should be traced or not from @filtered_pids.
350 * Returns true if @task should *NOT* be traced.
351 * Returns false if @task should be traced.
352 */
353bool
354trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task)
355{
356 /*
357 * Return false, because if filtered_pids does not exist,
358 * all pids are good to trace.
359 */
360 if (!filtered_pids)
361 return false;
362
363 return !trace_find_filtered_pid(filtered_pids, task->pid);
364}
365
366/**
Chunyu Hu5a93bae2017-10-19 14:32:33 +0800367 * trace_pid_filter_add_remove_task - Add or remove a task from a pid_list
Steven Rostedtd8275c42016-04-14 12:15:22 -0400368 * @pid_list: The list to modify
369 * @self: The current task for fork or NULL for exit
370 * @task: The task to add or remove
371 *
372 * If adding a task, if @self is defined, the task is only added if @self
373 * is also included in @pid_list. This happens on fork and tasks should
374 * only be added when the parent is listed. If @self is NULL, then the
375 * @task pid will be removed from the list, which would happen on exit
376 * of a task.
377 */
378void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
379 struct task_struct *self,
380 struct task_struct *task)
381{
382 if (!pid_list)
383 return;
384
385 /* For forks, we only add if the forking task is listed */
386 if (self) {
387 if (!trace_find_filtered_pid(pid_list, self->pid))
388 return;
389 }
390
391 /* Sorry, but we don't support pid_max changing after setting */
392 if (task->pid >= pid_list->pid_max)
393 return;
394
395 /* "self" is set for forks, and NULL for exits */
396 if (self)
397 set_bit(task->pid, pid_list->pids);
398 else
399 clear_bit(task->pid, pid_list->pids);
400}
401
Steven Rostedt (Red Hat)5cc89762016-04-20 15:19:54 -0400402/**
403 * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
404 * @pid_list: The pid list to show
405 * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
406 * @pos: The position of the file
407 *
408 * This is used by the seq_file "next" operation to iterate the pids
409 * listed in a trace_pid_list structure.
410 *
411 * Returns the pid+1 as we want to display pid of zero, but NULL would
412 * stop the iteration.
413 */
414void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
415{
416 unsigned long pid = (unsigned long)v;
417
418 (*pos)++;
419
420 /* pid already is +1 of the actual prevous bit */
421 pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
422
423 /* Return pid + 1 to allow zero to be represented */
424 if (pid < pid_list->pid_max)
425 return (void *)(pid + 1);
426
427 return NULL;
428}
429
430/**
431 * trace_pid_start - Used for seq_file to start reading pid lists
432 * @pid_list: The pid list to show
433 * @pos: The position of the file
434 *
435 * This is used by seq_file "start" operation to start the iteration
436 * of listing pids.
437 *
438 * Returns the pid+1 as we want to display pid of zero, but NULL would
439 * stop the iteration.
440 */
441void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
442{
443 unsigned long pid;
444 loff_t l = 0;
445
446 pid = find_first_bit(pid_list->pids, pid_list->pid_max);
447 if (pid >= pid_list->pid_max)
448 return NULL;
449
450 /* Return pid + 1 so that zero can be the exit value */
451 for (pid++; pid && l < *pos;
452 pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
453 ;
454 return (void *)pid;
455}
456
457/**
458 * trace_pid_show - show the current pid in seq_file processing
459 * @m: The seq_file structure to write into
460 * @v: A void pointer of the pid (+1) value to display
461 *
462 * Can be directly used by seq_file operations to display the current
463 * pid value.
464 */
465int trace_pid_show(struct seq_file *m, void *v)
466{
467 unsigned long pid = (unsigned long)v - 1;
468
469 seq_printf(m, "%lu\n", pid);
470 return 0;
471}
472
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400473/* 128 should be much more than enough */
474#define PID_BUF_SIZE 127
475
476int trace_pid_write(struct trace_pid_list *filtered_pids,
477 struct trace_pid_list **new_pid_list,
478 const char __user *ubuf, size_t cnt)
479{
480 struct trace_pid_list *pid_list;
481 struct trace_parser parser;
482 unsigned long val;
483 int nr_pids = 0;
484 ssize_t read = 0;
485 ssize_t ret = 0;
486 loff_t pos;
487 pid_t pid;
488
489 if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
490 return -ENOMEM;
491
492 /*
493 * Always recreate a new array. The write is an all or nothing
494 * operation. Always create a new array when adding new pids by
495 * the user. If the operation fails, then the current list is
496 * not modified.
497 */
498 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
499 if (!pid_list)
500 return -ENOMEM;
501
502 pid_list->pid_max = READ_ONCE(pid_max);
503
504 /* Only truncating will shrink pid_max */
505 if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
506 pid_list->pid_max = filtered_pids->pid_max;
507
508 pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
509 if (!pid_list->pids) {
510 kfree(pid_list);
511 return -ENOMEM;
512 }
513
514 if (filtered_pids) {
515 /* copy the current bits to the new max */
Wei Yongjun67f20b02016-07-04 15:10:04 +0000516 for_each_set_bit(pid, filtered_pids->pids,
517 filtered_pids->pid_max) {
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400518 set_bit(pid, pid_list->pids);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400519 nr_pids++;
520 }
521 }
522
523 while (cnt > 0) {
524
525 pos = 0;
526
527 ret = trace_get_user(&parser, ubuf, cnt, &pos);
528 if (ret < 0 || !trace_parser_loaded(&parser))
529 break;
530
531 read += ret;
532 ubuf += ret;
533 cnt -= ret;
534
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400535 ret = -EINVAL;
536 if (kstrtoul(parser.buffer, 0, &val))
537 break;
538 if (val >= pid_list->pid_max)
539 break;
540
541 pid = (pid_t)val;
542
543 set_bit(pid, pid_list->pids);
544 nr_pids++;
545
546 trace_parser_clear(&parser);
547 ret = 0;
548 }
549 trace_parser_put(&parser);
550
551 if (ret < 0) {
552 trace_free_pid_list(pid_list);
553 return ret;
554 }
555
556 if (!nr_pids) {
557 /* Cleared the list of pids */
558 trace_free_pid_list(pid_list);
559 read = ret;
560 pid_list = NULL;
561 }
562
563 *new_pid_list = pid_list;
564
565 return read;
566}
567
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100568static u64 buffer_ftrace_now(struct trace_buffer *buf, int cpu)
Steven Rostedt37886f62009-03-17 17:22:06 -0400569{
570 u64 ts;
571
572 /* Early boot up does not have a buffer yet */
Alexander Z Lam94571582013-08-02 18:36:16 -0700573 if (!buf->buffer)
Steven Rostedt37886f62009-03-17 17:22:06 -0400574 return trace_clock_local();
575
Alexander Z Lam94571582013-08-02 18:36:16 -0700576 ts = ring_buffer_time_stamp(buf->buffer, cpu);
577 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
Steven Rostedt37886f62009-03-17 17:22:06 -0400578
579 return ts;
580}
581
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100582u64 ftrace_now(int cpu)
Alexander Z Lam94571582013-08-02 18:36:16 -0700583{
584 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
585}
586
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400587/**
588 * tracing_is_enabled - Show if global_trace has been disabled
589 *
590 * Shows if the global trace has been enabled or not. It uses the
591 * mirror flag "buffer_disabled" to be used in fast paths such as for
592 * the irqsoff tracer. But it may be inaccurate due to races. If you
593 * need to know the accurate state, use tracing_is_on() which is a little
594 * slower, but accurate.
595 */
Steven Rostedt90369902008-11-05 16:05:44 -0500596int tracing_is_enabled(void)
597{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400598 /*
599 * For quick access (irqsoff uses this in fast path), just
600 * return the mirror variable of the state of the ring buffer.
601 * It's a little racy, but we don't really care.
602 */
603 smp_rmb();
604 return !global_trace.buffer_disabled;
Steven Rostedt90369902008-11-05 16:05:44 -0500605}
606
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200607/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400608 * trace_buf_size is the size in bytes that is allocated
609 * for a buffer. Note, the number of bytes is always rounded
610 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400611 *
612 * This number is purposely set to a low number of 16384.
613 * If the dump on oops happens, it will be much appreciated
614 * to not have to wait for all that output. Anyway this can be
615 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200616 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400617#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400618
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400619static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200620
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200621/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200622static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200623
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200624/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200625 * trace_types_lock is used to protect the trace_types list.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200626 */
Alexander Z Lama8227412013-07-01 19:37:54 -0700627DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200628
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800629/*
630 * serialize the access of the ring buffer
631 *
632 * ring buffer serializes readers, but it is low level protection.
633 * The validity of the events (which returns by ring_buffer_peek() ..etc)
634 * are not protected by ring buffer.
635 *
636 * The content of events may become garbage if we allow other process consumes
637 * these events concurrently:
638 * A) the page of the consumed events may become a normal page
639 * (not reader page) in ring buffer, and this page will be rewrited
640 * by events producer.
641 * B) The page of the consumed events may become a page for splice_read,
642 * and this page will be returned to system.
643 *
644 * These primitives allow multi process access to different cpu ring buffer
645 * concurrently.
646 *
647 * These primitives don't distinguish read-only and read-consume access.
648 * Multi read-only access are also serialized.
649 */
650
651#ifdef CONFIG_SMP
652static DECLARE_RWSEM(all_cpu_access_lock);
653static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
654
655static inline void trace_access_lock(int cpu)
656{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500657 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800658 /* gain it for accessing the whole ring buffer. */
659 down_write(&all_cpu_access_lock);
660 } else {
661 /* gain it for accessing a cpu ring buffer. */
662
Steven Rostedtae3b5092013-01-23 15:22:59 -0500663 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800664 down_read(&all_cpu_access_lock);
665
666 /* Secondly block other access to this @cpu ring buffer. */
667 mutex_lock(&per_cpu(cpu_access_lock, cpu));
668 }
669}
670
671static inline void trace_access_unlock(int cpu)
672{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500673 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800674 up_write(&all_cpu_access_lock);
675 } else {
676 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
677 up_read(&all_cpu_access_lock);
678 }
679}
680
681static inline void trace_access_lock_init(void)
682{
683 int cpu;
684
685 for_each_possible_cpu(cpu)
686 mutex_init(&per_cpu(cpu_access_lock, cpu));
687}
688
689#else
690
691static DEFINE_MUTEX(access_lock);
692
693static inline void trace_access_lock(int cpu)
694{
695 (void)cpu;
696 mutex_lock(&access_lock);
697}
698
699static inline void trace_access_unlock(int cpu)
700{
701 (void)cpu;
702 mutex_unlock(&access_lock);
703}
704
705static inline void trace_access_lock_init(void)
706{
707}
708
709#endif
710
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400711#ifdef CONFIG_STACKTRACE
712static void __ftrace_trace_stack(struct ring_buffer *buffer,
713 unsigned long flags,
714 int skip, int pc, struct pt_regs *regs);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400715static inline void ftrace_trace_stack(struct trace_array *tr,
716 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -0400717 unsigned long flags,
718 int skip, int pc, struct pt_regs *regs);
Steven Rostedt (Red Hat)ca475e82015-09-28 09:41:11 -0400719
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400720#else
721static inline void __ftrace_trace_stack(struct ring_buffer *buffer,
722 unsigned long flags,
723 int skip, int pc, struct pt_regs *regs)
724{
725}
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400726static inline void ftrace_trace_stack(struct trace_array *tr,
727 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -0400728 unsigned long flags,
729 int skip, int pc, struct pt_regs *regs)
Steven Rostedt (Red Hat)ca475e82015-09-28 09:41:11 -0400730{
731}
732
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400733#endif
734
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500735static __always_inline void
736trace_event_setup(struct ring_buffer_event *event,
737 int type, unsigned long flags, int pc)
738{
739 struct trace_entry *ent = ring_buffer_event_data(event);
740
741 tracing_generic_entry_update(ent, flags, pc);
742 ent->type = type;
743}
744
745static __always_inline struct ring_buffer_event *
746__trace_buffer_lock_reserve(struct ring_buffer *buffer,
747 int type,
748 unsigned long len,
749 unsigned long flags, int pc)
750{
751 struct ring_buffer_event *event;
752
753 event = ring_buffer_lock_reserve(buffer, len);
754 if (event != NULL)
755 trace_event_setup(event, type, flags, pc);
756
757 return event;
758}
759
Steven Rostedt (VMware)2290f2c2017-04-20 11:46:03 -0400760void tracer_tracing_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400761{
762 if (tr->trace_buffer.buffer)
763 ring_buffer_record_on(tr->trace_buffer.buffer);
764 /*
765 * This flag is looked at when buffers haven't been allocated
766 * yet, or by some tracers (like irqsoff), that just want to
767 * know if the ring buffer has been disabled, but it can handle
768 * races of where it gets disabled but we still do a record.
769 * As the check is in the fast path of the tracers, it is more
770 * important to be fast than accurate.
771 */
772 tr->buffer_disabled = 0;
773 /* Make the flag seen by readers */
774 smp_wmb();
775}
776
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200777/**
Steven Rostedt499e5472012-02-22 15:50:28 -0500778 * tracing_on - enable tracing buffers
779 *
780 * This function enables tracing buffers that may have been
781 * disabled with tracing_off.
782 */
783void tracing_on(void)
784{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400785 tracer_tracing_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -0500786}
787EXPORT_SYMBOL_GPL(tracing_on);
788
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500789
790static __always_inline void
791__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
792{
Joel Fernandesd914ba32017-06-26 19:01:55 -0700793 __this_cpu_write(trace_taskinfo_save, true);
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500794
795 /* If this is the temp buffer, we need to commit fully */
796 if (this_cpu_read(trace_buffered_event) == event) {
797 /* Length is in event->array[0] */
798 ring_buffer_write(buffer, event->array[0], &event->array[1]);
799 /* Release the temp buffer */
800 this_cpu_dec(trace_buffered_event_cnt);
801 } else
802 ring_buffer_unlock_commit(buffer, event);
803}
804
Steven Rostedt499e5472012-02-22 15:50:28 -0500805/**
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500806 * __trace_puts - write a constant string into the trace buffer.
807 * @ip: The address of the caller
808 * @str: The constant string to write
809 * @size: The size of the string.
810 */
811int __trace_puts(unsigned long ip, const char *str, int size)
812{
813 struct ring_buffer_event *event;
814 struct ring_buffer *buffer;
815 struct print_entry *entry;
816 unsigned long irq_flags;
817 int alloc;
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800818 int pc;
819
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400820 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800821 return 0;
822
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800823 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500824
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500825 if (unlikely(tracing_selftest_running || tracing_disabled))
826 return 0;
827
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500828 alloc = sizeof(*entry) + size + 2; /* possible \n added */
829
830 local_save_flags(irq_flags);
831 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500832 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
833 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500834 if (!event)
835 return 0;
836
837 entry = ring_buffer_event_data(event);
838 entry->ip = ip;
839
840 memcpy(&entry->buf, str, size);
841
842 /* Add a newline if necessary */
843 if (entry->buf[size - 1] != '\n') {
844 entry->buf[size] = '\n';
845 entry->buf[size + 1] = '\0';
846 } else
847 entry->buf[size] = '\0';
848
849 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400850 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500851
852 return size;
853}
854EXPORT_SYMBOL_GPL(__trace_puts);
855
856/**
857 * __trace_bputs - write the pointer to a constant string into trace buffer
858 * @ip: The address of the caller
859 * @str: The constant string to write to the buffer to
860 */
861int __trace_bputs(unsigned long ip, const char *str)
862{
863 struct ring_buffer_event *event;
864 struct ring_buffer *buffer;
865 struct bputs_entry *entry;
866 unsigned long irq_flags;
867 int size = sizeof(struct bputs_entry);
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800868 int pc;
869
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400870 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800871 return 0;
872
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800873 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500874
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500875 if (unlikely(tracing_selftest_running || tracing_disabled))
876 return 0;
877
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500878 local_save_flags(irq_flags);
879 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500880 event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
881 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500882 if (!event)
883 return 0;
884
885 entry = ring_buffer_event_data(event);
886 entry->ip = ip;
887 entry->str = str;
888
889 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400890 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500891
892 return 1;
893}
894EXPORT_SYMBOL_GPL(__trace_bputs);
895
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500896#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -0400897void tracing_snapshot_instance(struct trace_array *tr)
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500898{
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500899 struct tracer *tracer = tr->current_trace;
900 unsigned long flags;
901
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500902 if (in_nmi()) {
903 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
904 internal_trace_puts("*** snapshot is being ignored ***\n");
905 return;
906 }
907
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500908 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500909 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
910 internal_trace_puts("*** stopping trace here! ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500911 tracing_off();
912 return;
913 }
914
915 /* Note, snapshot can not be used when the tracer uses it */
916 if (tracer->use_max_tr) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500917 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
918 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500919 return;
920 }
921
922 local_irq_save(flags);
923 update_max_tr(tr, current, smp_processor_id());
924 local_irq_restore(flags);
925}
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -0400926
927/**
Chunyu Hu5a93bae2017-10-19 14:32:33 +0800928 * tracing_snapshot - take a snapshot of the current buffer.
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -0400929 *
930 * This causes a swap between the snapshot buffer and the current live
931 * tracing buffer. You can use this to take snapshots of the live
932 * trace when some condition is triggered, but continue to trace.
933 *
934 * Note, make sure to allocate the snapshot with either
935 * a tracing_snapshot_alloc(), or by doing it manually
936 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
937 *
938 * If the snapshot buffer is not allocated, it will stop tracing.
939 * Basically making a permanent snapshot.
940 */
941void tracing_snapshot(void)
942{
943 struct trace_array *tr = &global_trace;
944
945 tracing_snapshot_instance(tr);
946}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500947EXPORT_SYMBOL_GPL(tracing_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500948
949static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
950 struct trace_buffer *size_buf, int cpu_id);
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400951static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
952
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -0400953int tracing_alloc_snapshot_instance(struct trace_array *tr)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400954{
955 int ret;
956
957 if (!tr->allocated_snapshot) {
958
959 /* allocate spare buffer */
960 ret = resize_buffer_duplicate_size(&tr->max_buffer,
961 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
962 if (ret < 0)
963 return ret;
964
965 tr->allocated_snapshot = true;
966 }
967
968 return 0;
969}
970
Fabian Frederickad1438a2014-04-17 21:44:42 +0200971static void free_snapshot(struct trace_array *tr)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400972{
973 /*
974 * We don't free the ring buffer. instead, resize it because
975 * The max_tr ring buffer has some state (e.g. ring->clock) and
976 * we want preserve it.
977 */
978 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
979 set_buffer_entries(&tr->max_buffer, 1);
980 tracing_reset_online_cpus(&tr->max_buffer);
981 tr->allocated_snapshot = false;
982}
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500983
984/**
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500985 * tracing_alloc_snapshot - allocate snapshot buffer.
986 *
987 * This only allocates the snapshot buffer if it isn't already
988 * allocated - it doesn't also take a snapshot.
989 *
990 * This is meant to be used in cases where the snapshot buffer needs
991 * to be set up for events that can't sleep but need to be able to
992 * trigger a snapshot.
993 */
994int tracing_alloc_snapshot(void)
995{
996 struct trace_array *tr = &global_trace;
997 int ret;
998
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -0400999 ret = tracing_alloc_snapshot_instance(tr);
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001000 WARN_ON(ret < 0);
1001
1002 return ret;
1003}
1004EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1005
1006/**
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001007 * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001008 *
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001009 * This is similar to tracing_snapshot(), but it will allocate the
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001010 * snapshot buffer if it isn't already allocated. Use this only
1011 * where it is safe to sleep, as the allocation may sleep.
1012 *
1013 * This causes a swap between the snapshot buffer and the current live
1014 * tracing buffer. You can use this to take snapshots of the live
1015 * trace when some condition is triggered, but continue to trace.
1016 */
1017void tracing_snapshot_alloc(void)
1018{
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001019 int ret;
1020
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001021 ret = tracing_alloc_snapshot();
1022 if (ret < 0)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001023 return;
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001024
1025 tracing_snapshot();
1026}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001027EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001028#else
1029void tracing_snapshot(void)
1030{
1031 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1032}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001033EXPORT_SYMBOL_GPL(tracing_snapshot);
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001034int tracing_alloc_snapshot(void)
1035{
1036 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1037 return -ENODEV;
1038}
1039EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001040void tracing_snapshot_alloc(void)
1041{
1042 /* Give warning */
1043 tracing_snapshot();
1044}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001045EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001046#endif /* CONFIG_TRACER_SNAPSHOT */
1047
Steven Rostedt (VMware)2290f2c2017-04-20 11:46:03 -04001048void tracer_tracing_off(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001049{
1050 if (tr->trace_buffer.buffer)
1051 ring_buffer_record_off(tr->trace_buffer.buffer);
1052 /*
1053 * This flag is looked at when buffers haven't been allocated
1054 * yet, or by some tracers (like irqsoff), that just want to
1055 * know if the ring buffer has been disabled, but it can handle
1056 * races of where it gets disabled but we still do a record.
1057 * As the check is in the fast path of the tracers, it is more
1058 * important to be fast than accurate.
1059 */
1060 tr->buffer_disabled = 1;
1061 /* Make the flag seen by readers */
1062 smp_wmb();
1063}
1064
Steven Rostedt499e5472012-02-22 15:50:28 -05001065/**
1066 * tracing_off - turn off tracing buffers
1067 *
1068 * This function stops the tracing buffers from recording data.
1069 * It does not disable any overhead the tracers themselves may
1070 * be causing. This function simply causes all recording to
1071 * the ring buffers to fail.
1072 */
1073void tracing_off(void)
1074{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001075 tracer_tracing_off(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001076}
1077EXPORT_SYMBOL_GPL(tracing_off);
1078
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -04001079void disable_trace_on_warning(void)
1080{
1081 if (__disable_trace_on_warning)
1082 tracing_off();
1083}
1084
Steven Rostedt499e5472012-02-22 15:50:28 -05001085/**
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001086 * tracer_tracing_is_on - show real state of ring buffer enabled
1087 * @tr : the trace array to know if ring buffer is enabled
1088 *
1089 * Shows real state of the ring buffer if it is enabled or not.
1090 */
Steven Rostedt (VMware)ec573502018-08-01 16:08:57 -04001091bool tracer_tracing_is_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001092{
1093 if (tr->trace_buffer.buffer)
1094 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
1095 return !tr->buffer_disabled;
1096}
1097
Steven Rostedt499e5472012-02-22 15:50:28 -05001098/**
1099 * tracing_is_on - show state of ring buffers enabled
1100 */
1101int tracing_is_on(void)
1102{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001103 return tracer_tracing_is_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001104}
1105EXPORT_SYMBOL_GPL(tracing_is_on);
1106
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001107static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001108{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001109 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001110
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001111 if (!str)
1112 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +08001113 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001114 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +08001115 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001116 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001117 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001118 return 1;
1119}
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001120__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001121
Tim Bird0e950172010-02-25 15:36:43 -08001122static int __init set_tracing_thresh(char *str)
1123{
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001124 unsigned long threshold;
Tim Bird0e950172010-02-25 15:36:43 -08001125 int ret;
1126
1127 if (!str)
1128 return 0;
Daniel Walterbcd83ea2012-09-26 22:08:38 +02001129 ret = kstrtoul(str, 0, &threshold);
Tim Bird0e950172010-02-25 15:36:43 -08001130 if (ret < 0)
1131 return 0;
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001132 tracing_thresh = threshold * 1000;
Tim Bird0e950172010-02-25 15:36:43 -08001133 return 1;
1134}
1135__setup("tracing_thresh=", set_tracing_thresh);
1136
Steven Rostedt57f50be2008-05-12 21:20:44 +02001137unsigned long nsecs_to_usecs(unsigned long nsecs)
1138{
1139 return nsecs / 1000;
1140}
1141
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001142/*
1143 * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
Jeremy Lintonf57a4142017-05-31 16:56:48 -05001144 * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001145 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
Jeremy Lintonf57a4142017-05-31 16:56:48 -05001146 * of strings in the order that the evals (enum) were defined.
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001147 */
1148#undef C
1149#define C(a, b) b
1150
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001151/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001152static const char *trace_options[] = {
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001153 TRACE_FLAGS
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001154 NULL
1155};
1156
Zhaolei5079f322009-08-25 16:12:56 +08001157static struct {
1158 u64 (*func)(void);
1159 const char *name;
David Sharp8be07092012-11-13 12:18:22 -08001160 int in_ns; /* is this clock in nanoseconds? */
Zhaolei5079f322009-08-25 16:12:56 +08001161} trace_clocks[] = {
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001162 { trace_clock_local, "local", 1 },
1163 { trace_clock_global, "global", 1 },
1164 { trace_clock_counter, "counter", 0 },
Linus Torvaldse7fda6c2014-08-05 17:46:42 -07001165 { trace_clock_jiffies, "uptime", 0 },
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001166 { trace_clock, "perf", 1 },
1167 { ktime_get_mono_fast_ns, "mono", 1 },
Drew Richardsonaabfa5f2015-05-08 07:30:39 -07001168 { ktime_get_raw_fast_ns, "mono_raw", 1 },
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +02001169 { ktime_get_boot_fast_ns, "boot", 1 },
David Sharp8cbd9cc2012-11-13 12:18:21 -08001170 ARCH_TRACE_CLOCKS
Zhaolei5079f322009-08-25 16:12:56 +08001171};
1172
Tom Zanussi860f9f62018-01-15 20:51:48 -06001173bool trace_clock_in_ns(struct trace_array *tr)
1174{
1175 if (trace_clocks[tr->clock_id].in_ns)
1176 return true;
1177
1178 return false;
1179}
1180
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001181/*
1182 * trace_parser_get_init - gets the buffer for trace parser
1183 */
1184int trace_parser_get_init(struct trace_parser *parser, int size)
1185{
1186 memset(parser, 0, sizeof(*parser));
1187
1188 parser->buffer = kmalloc(size, GFP_KERNEL);
1189 if (!parser->buffer)
1190 return 1;
1191
1192 parser->size = size;
1193 return 0;
1194}
1195
1196/*
1197 * trace_parser_put - frees the buffer for trace parser
1198 */
1199void trace_parser_put(struct trace_parser *parser)
1200{
1201 kfree(parser->buffer);
Steven Rostedt (VMware)0e684b62017-02-02 17:58:18 -05001202 parser->buffer = NULL;
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001203}
1204
1205/*
1206 * trace_get_user - reads the user input string separated by space
1207 * (matched by isspace(ch))
1208 *
1209 * For each string found the 'struct trace_parser' is updated,
1210 * and the function returns.
1211 *
1212 * Returns number of bytes read.
1213 *
1214 * See kernel/trace/trace.h for 'struct trace_parser' details.
1215 */
1216int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1217 size_t cnt, loff_t *ppos)
1218{
1219 char ch;
1220 size_t read = 0;
1221 ssize_t ret;
1222
1223 if (!*ppos)
1224 trace_parser_clear(parser);
1225
1226 ret = get_user(ch, ubuf++);
1227 if (ret)
1228 goto out;
1229
1230 read++;
1231 cnt--;
1232
1233 /*
1234 * The parser is not finished with the last write,
1235 * continue reading the user input without skipping spaces.
1236 */
1237 if (!parser->cont) {
1238 /* skip white space */
1239 while (cnt && isspace(ch)) {
1240 ret = get_user(ch, ubuf++);
1241 if (ret)
1242 goto out;
1243 read++;
1244 cnt--;
1245 }
1246
Changbin Du76638d92018-01-16 17:02:29 +08001247 parser->idx = 0;
1248
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001249 /* only spaces were written */
Changbin Du921a7ac2018-01-16 17:02:28 +08001250 if (isspace(ch) || !ch) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001251 *ppos += read;
1252 ret = read;
1253 goto out;
1254 }
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001255 }
1256
1257 /* read the non-space input */
Changbin Du921a7ac2018-01-16 17:02:28 +08001258 while (cnt && !isspace(ch) && ch) {
Li Zefan3c235a32009-09-22 13:51:54 +08001259 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001260 parser->buffer[parser->idx++] = ch;
1261 else {
1262 ret = -EINVAL;
1263 goto out;
1264 }
1265 ret = get_user(ch, ubuf++);
1266 if (ret)
1267 goto out;
1268 read++;
1269 cnt--;
1270 }
1271
1272 /* We either got finished input or we have to wait for another call. */
Changbin Du921a7ac2018-01-16 17:02:28 +08001273 if (isspace(ch) || !ch) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001274 parser->buffer[parser->idx] = 0;
1275 parser->cont = false;
Steven Rostedt057db842013-10-09 22:23:23 -04001276 } else if (parser->idx < parser->size - 1) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001277 parser->cont = true;
1278 parser->buffer[parser->idx++] = ch;
Changbin Duf4d07062018-01-16 17:02:30 +08001279 /* Make sure the parsed string always terminates with '\0'. */
1280 parser->buffer[parser->idx] = 0;
Steven Rostedt057db842013-10-09 22:23:23 -04001281 } else {
1282 ret = -EINVAL;
1283 goto out;
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001284 }
1285
1286 *ppos += read;
1287 ret = read;
1288
1289out:
1290 return ret;
1291}
1292
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001293/* TODO add a seq_buf_to_buffer() */
Dmitri Vorobievb8b94262009-03-22 19:11:11 +02001294static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001295{
1296 int len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001297
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001298 if (trace_seq_used(s) <= s->seq.readpos)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001299 return -EBUSY;
1300
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001301 len = trace_seq_used(s) - s->seq.readpos;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001302 if (cnt > len)
1303 cnt = len;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001304 memcpy(buf, s->buffer + s->seq.readpos, cnt);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001305
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001306 s->seq.readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001307 return cnt;
1308}
1309
Tim Bird0e950172010-02-25 15:36:43 -08001310unsigned long __read_mostly tracing_thresh;
1311
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001312#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001313/*
1314 * Copy the new maximum trace into the separate maximum-trace
1315 * structure. (this way the maximum trace is permanently saved,
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001316 * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001317 */
1318static void
1319__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1320{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001321 struct trace_buffer *trace_buf = &tr->trace_buffer;
1322 struct trace_buffer *max_buf = &tr->max_buffer;
1323 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1324 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001325
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001326 max_buf->cpu = cpu;
1327 max_buf->time_start = data->preempt_timestamp;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001328
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001329 max_data->saved_latency = tr->max_latency;
Steven Rostedt8248ac02009-09-02 12:27:41 -04001330 max_data->critical_start = data->critical_start;
1331 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001332
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -03001333 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -04001334 max_data->pid = tsk->pid;
Steven Rostedt (Red Hat)f17a5192013-05-30 21:10:37 -04001335 /*
1336 * If tsk == current, then use current_uid(), as that does not use
1337 * RCU. The irq tracer can be called out of RCU scope.
1338 */
1339 if (tsk == current)
1340 max_data->uid = current_uid();
1341 else
1342 max_data->uid = task_uid(tsk);
1343
Steven Rostedt8248ac02009-09-02 12:27:41 -04001344 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1345 max_data->policy = tsk->policy;
1346 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001347
1348 /* record this tasks comm */
1349 tracing_record_cmdline(tsk);
1350}
1351
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001352/**
1353 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1354 * @tr: tracer
1355 * @tsk: the task with the latency
1356 * @cpu: The cpu that initiated the trace.
1357 *
1358 * Flip the buffers between the @tr and the max_tr and record information
1359 * about which task was the cause of this latency.
1360 */
Ingo Molnare309b412008-05-12 21:20:51 +02001361void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001362update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1363{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001364 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001365 return;
1366
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001367 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt34600f02013-01-22 13:35:11 -05001368
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05001369 if (!tr->allocated_snapshot) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001370 /* Only the nop tracer should hit this when disabling */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001371 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001372 return;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001373 }
Steven Rostedt34600f02013-01-22 13:35:11 -05001374
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001375 arch_spin_lock(&tr->max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001376
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09001377 /* Inherit the recordable setting from trace_buffer */
1378 if (ring_buffer_record_is_set_on(tr->trace_buffer.buffer))
1379 ring_buffer_record_on(tr->max_buffer.buffer);
1380 else
1381 ring_buffer_record_off(tr->max_buffer.buffer);
1382
Gustavo A. R. Silva08ae88f2018-02-09 11:53:16 -06001383 swap(tr->trace_buffer.buffer, tr->max_buffer.buffer);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001384
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001385 __update_max_tr(tr, tsk, cpu);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001386 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001387}
1388
1389/**
1390 * update_max_tr_single - only copy one trace over, and reset the rest
1391 * @tr - tracer
1392 * @tsk - task with the latency
1393 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001394 *
1395 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001396 */
Ingo Molnare309b412008-05-12 21:20:51 +02001397void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001398update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1399{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001400 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001401
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001402 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001403 return;
1404
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001405 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt6c244992013-04-29 20:08:14 -04001406 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001407 /* Only the nop tracer should hit this when disabling */
Linus Torvalds9e8529a2013-04-29 13:55:38 -07001408 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001409 return;
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001410 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001411
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001412 arch_spin_lock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001413
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001414 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001415
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001416 if (ret == -EBUSY) {
1417 /*
1418 * We failed to swap the buffer due to a commit taking
1419 * place on this CPU. We fail to record, but we reset
1420 * the max trace buffer (no one writes directly to it)
1421 * and flag that it failed.
1422 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001423 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001424 "Failed to swap buffers due to commit in progress\n");
1425 }
1426
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001427 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001428
1429 __update_max_tr(tr, tsk, cpu);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001430 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001431}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001432#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001433
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05001434static int wait_on_pipe(struct trace_iterator *iter, int full)
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001435{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001436 /* Iterators are static, they should be filled or empty */
1437 if (trace_buffer_iter(iter, iter->cpu_file))
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04001438 return 0;
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001439
Rabin Vincente30f53a2014-11-10 19:46:34 +01001440 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1441 full);
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001442}
1443
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001444#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001445static bool selftests_can_run;
1446
1447struct trace_selftests {
1448 struct list_head list;
1449 struct tracer *type;
1450};
1451
1452static LIST_HEAD(postponed_selftests);
1453
1454static int save_selftest(struct tracer *type)
1455{
1456 struct trace_selftests *selftest;
1457
1458 selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
1459 if (!selftest)
1460 return -ENOMEM;
1461
1462 selftest->type = type;
1463 list_add(&selftest->list, &postponed_selftests);
1464 return 0;
1465}
1466
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001467static int run_tracer_selftest(struct tracer *type)
1468{
1469 struct trace_array *tr = &global_trace;
1470 struct tracer *saved_tracer = tr->current_trace;
1471 int ret;
1472
1473 if (!type->selftest || tracing_selftest_disabled)
1474 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001475
1476 /*
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001477 * If a tracer registers early in boot up (before scheduling is
1478 * initialized and such), then do not run its selftests yet.
1479 * Instead, run it a little later in the boot process.
1480 */
1481 if (!selftests_can_run)
1482 return save_selftest(type);
1483
1484 /*
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001485 * Run a selftest on this tracer.
1486 * Here we reset the trace buffer, and set the current
1487 * tracer to be this tracer. The tracer can then run some
1488 * internal tracing to verify that everything is in order.
1489 * If we fail, we do not register this tracer.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001490 */
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001491 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001492
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001493 tr->current_trace = type;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001494
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001495#ifdef CONFIG_TRACER_MAX_TRACE
1496 if (type->use_max_tr) {
1497 /* If we expanded the buffers, make sure the max is expanded too */
1498 if (ring_buffer_expanded)
1499 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1500 RING_BUFFER_ALL_CPUS);
1501 tr->allocated_snapshot = true;
1502 }
1503#endif
1504
1505 /* the test is responsible for initializing and enabling */
1506 pr_info("Testing tracer %s: ", type->name);
1507 ret = type->selftest(type, tr);
1508 /* the test is responsible for resetting too */
1509 tr->current_trace = saved_tracer;
1510 if (ret) {
1511 printk(KERN_CONT "FAILED!\n");
1512 /* Add the warning after printing 'FAILED' */
1513 WARN_ON(1);
1514 return -1;
1515 }
1516 /* Only reset on passing, to avoid touching corrupted buffers */
1517 tracing_reset_online_cpus(&tr->trace_buffer);
1518
1519#ifdef CONFIG_TRACER_MAX_TRACE
1520 if (type->use_max_tr) {
1521 tr->allocated_snapshot = false;
1522
1523 /* Shrink the max buffer again */
1524 if (ring_buffer_expanded)
1525 ring_buffer_resize(tr->max_buffer.buffer, 1,
1526 RING_BUFFER_ALL_CPUS);
1527 }
1528#endif
1529
1530 printk(KERN_CONT "PASSED\n");
1531 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001532}
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001533
1534static __init int init_trace_selftests(void)
1535{
1536 struct trace_selftests *p, *n;
1537 struct tracer *t, **last;
1538 int ret;
1539
1540 selftests_can_run = true;
1541
1542 mutex_lock(&trace_types_lock);
1543
1544 if (list_empty(&postponed_selftests))
1545 goto out;
1546
1547 pr_info("Running postponed tracer tests:\n");
1548
1549 list_for_each_entry_safe(p, n, &postponed_selftests, list) {
1550 ret = run_tracer_selftest(p->type);
1551 /* If the test fails, then warn and remove from available_tracers */
1552 if (ret < 0) {
1553 WARN(1, "tracer: %s failed selftest, disabling\n",
1554 p->type->name);
1555 last = &trace_types;
1556 for (t = trace_types; t; t = t->next) {
1557 if (t == p->type) {
1558 *last = t->next;
1559 break;
1560 }
1561 last = &t->next;
1562 }
1563 }
1564 list_del(&p->list);
1565 kfree(p);
1566 }
1567
1568 out:
1569 mutex_unlock(&trace_types_lock);
1570
1571 return 0;
1572}
Steven Rostedtb9ef0322017-05-17 11:14:35 -04001573core_initcall(init_trace_selftests);
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001574#else
1575static inline int run_tracer_selftest(struct tracer *type)
1576{
1577 return 0;
1578}
1579#endif /* CONFIG_FTRACE_STARTUP_TEST */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001580
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001581static void add_tracer_options(struct trace_array *tr, struct tracer *t);
1582
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001583static void __init apply_trace_boot_options(void);
1584
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001585/**
1586 * register_tracer - register a tracer with the ftrace system.
1587 * @type - the plugin for the tracer
1588 *
1589 * Register a new plugin tracer.
1590 */
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001591int __init register_tracer(struct tracer *type)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001592{
1593 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001594 int ret = 0;
1595
1596 if (!type->name) {
1597 pr_info("Tracer must have a name\n");
1598 return -1;
1599 }
1600
Dan Carpenter24a461d2010-07-10 12:06:44 +02001601 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +08001602 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1603 return -1;
1604 }
1605
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001606 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +01001607
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001608 tracing_selftest_running = true;
1609
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001610 for (t = trace_types; t; t = t->next) {
1611 if (strcmp(type->name, t->name) == 0) {
1612 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +08001613 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001614 type->name);
1615 ret = -1;
1616 goto out;
1617 }
1618 }
1619
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001620 if (!type->set_flag)
1621 type->set_flag = &dummy_set_flag;
Chunyu Hud39cdd22016-03-08 21:37:01 +08001622 if (!type->flags) {
1623 /*allocate a dummy tracer_flags*/
1624 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
Chunyu Huc8ca0032016-03-14 20:35:41 +08001625 if (!type->flags) {
1626 ret = -ENOMEM;
1627 goto out;
1628 }
Chunyu Hud39cdd22016-03-08 21:37:01 +08001629 type->flags->val = 0;
1630 type->flags->opts = dummy_tracer_opt;
1631 } else
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001632 if (!type->flags->opts)
1633 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001634
Chunyu Hud39cdd22016-03-08 21:37:01 +08001635 /* store the tracer for __set_tracer_option */
1636 type->flags->trace = type;
1637
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001638 ret = run_tracer_selftest(type);
1639 if (ret < 0)
1640 goto out;
Steven Rostedt60a11772008-05-12 21:20:44 +02001641
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001642 type->next = trace_types;
1643 trace_types = type;
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001644 add_tracer_options(&global_trace, type);
Steven Rostedt60a11772008-05-12 21:20:44 +02001645
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001646 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001647 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001648 mutex_unlock(&trace_types_lock);
1649
Steven Rostedtdac74942009-02-05 01:13:38 -05001650 if (ret || !default_bootup_tracer)
1651 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -05001652
Li Zefanee6c2c12009-09-18 14:06:47 +08001653 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -05001654 goto out_unlock;
1655
1656 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1657 /* Do we want this tracer to start on bootup? */
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05001658 tracing_set_tracer(&global_trace, type->name);
Steven Rostedtdac74942009-02-05 01:13:38 -05001659 default_bootup_tracer = NULL;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001660
1661 apply_trace_boot_options();
1662
Steven Rostedtdac74942009-02-05 01:13:38 -05001663 /* disable other selftests, since this will break it. */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05001664 tracing_selftest_disabled = true;
Steven Rostedtdac74942009-02-05 01:13:38 -05001665#ifdef CONFIG_FTRACE_STARTUP_TEST
1666 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1667 type->name);
1668#endif
1669
1670 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001671 return ret;
1672}
1673
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001674void tracing_reset(struct trace_buffer *buf, int cpu)
Steven Rostedtf6339032009-09-04 12:35:16 -04001675{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001676 struct ring_buffer *buffer = buf->buffer;
Steven Rostedtf6339032009-09-04 12:35:16 -04001677
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001678 if (!buffer)
1679 return;
1680
Steven Rostedtf6339032009-09-04 12:35:16 -04001681 ring_buffer_record_disable(buffer);
1682
1683 /* Make sure all commits have finished */
1684 synchronize_sched();
Steven Rostedt68179682012-05-08 20:57:53 -04001685 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -04001686
1687 ring_buffer_record_enable(buffer);
1688}
1689
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001690void tracing_reset_online_cpus(struct trace_buffer *buf)
Pekka J Enberg213cc062008-12-19 12:08:39 +02001691{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001692 struct ring_buffer *buffer = buf->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +02001693 int cpu;
1694
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001695 if (!buffer)
1696 return;
1697
Steven Rostedt621968c2009-09-04 12:02:35 -04001698 ring_buffer_record_disable(buffer);
1699
1700 /* Make sure all commits have finished */
1701 synchronize_sched();
1702
Alexander Z Lam94571582013-08-02 18:36:16 -07001703 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001704
1705 for_each_online_cpu(cpu)
Steven Rostedt68179682012-05-08 20:57:53 -04001706 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -04001707
1708 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001709}
1710
Steven Rostedt (Red Hat)09d80912013-07-23 22:21:59 -04001711/* Must have trace_types_lock held */
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001712void tracing_reset_all_online_cpus(void)
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001713{
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001714 struct trace_array *tr;
1715
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001716 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
Steven Rostedt (VMware)065e63f2017-08-31 17:03:47 -04001717 if (!tr->clear_trace)
1718 continue;
1719 tr->clear_trace = false;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001720 tracing_reset_online_cpus(&tr->trace_buffer);
1721#ifdef CONFIG_TRACER_MAX_TRACE
1722 tracing_reset_online_cpus(&tr->max_buffer);
1723#endif
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001724 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001725}
1726
Joel Fernandesd914ba32017-06-26 19:01:55 -07001727static int *tgid_map;
1728
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001729#define SAVED_CMDLINES_DEFAULT 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001730#define NO_CMDLINE_MAP UINT_MAX
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001731static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001732struct saved_cmdlines_buffer {
1733 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1734 unsigned *map_cmdline_to_pid;
1735 unsigned cmdline_num;
1736 int cmdline_idx;
1737 char *saved_cmdlines;
1738};
1739static struct saved_cmdlines_buffer *savedcmd;
Steven Rostedt25b0b442008-05-12 21:21:00 +02001740
Steven Rostedt25b0b442008-05-12 21:21:00 +02001741/* temporary disable recording */
Joel Fernandesd914ba32017-06-26 19:01:55 -07001742static atomic_t trace_record_taskinfo_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001743
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001744static inline char *get_saved_cmdlines(int idx)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001745{
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001746 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
1747}
1748
1749static inline void set_cmdline(int idx, const char *cmdline)
1750{
1751 memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
1752}
1753
1754static int allocate_cmdlines_buffer(unsigned int val,
1755 struct saved_cmdlines_buffer *s)
1756{
Kees Cook6da2ec52018-06-12 13:55:00 -07001757 s->map_cmdline_to_pid = kmalloc_array(val,
1758 sizeof(*s->map_cmdline_to_pid),
1759 GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001760 if (!s->map_cmdline_to_pid)
1761 return -ENOMEM;
1762
Kees Cook6da2ec52018-06-12 13:55:00 -07001763 s->saved_cmdlines = kmalloc_array(TASK_COMM_LEN, val, GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001764 if (!s->saved_cmdlines) {
1765 kfree(s->map_cmdline_to_pid);
1766 return -ENOMEM;
1767 }
1768
1769 s->cmdline_idx = 0;
1770 s->cmdline_num = val;
1771 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
1772 sizeof(s->map_pid_to_cmdline));
1773 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
1774 val * sizeof(*s->map_cmdline_to_pid));
1775
1776 return 0;
1777}
1778
1779static int trace_create_savedcmd(void)
1780{
1781 int ret;
1782
Namhyung Kima6af8fb2014-06-10 16:11:35 +09001783 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001784 if (!savedcmd)
1785 return -ENOMEM;
1786
1787 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
1788 if (ret < 0) {
1789 kfree(savedcmd);
1790 savedcmd = NULL;
1791 return -ENOMEM;
1792 }
1793
1794 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001795}
1796
Carsten Emdeb5130b12009-09-13 01:43:07 +02001797int is_tracing_stopped(void)
1798{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001799 return global_trace.stop_count;
Carsten Emdeb5130b12009-09-13 01:43:07 +02001800}
1801
Steven Rostedt0f048702008-11-05 16:05:44 -05001802/**
1803 * tracing_start - quick start of the tracer
1804 *
1805 * If tracing is enabled but was stopped by tracing_stop,
1806 * this will start the tracer back up.
1807 */
1808void tracing_start(void)
1809{
1810 struct ring_buffer *buffer;
1811 unsigned long flags;
1812
1813 if (tracing_disabled)
1814 return;
1815
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001816 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1817 if (--global_trace.stop_count) {
1818 if (global_trace.stop_count < 0) {
Steven Rostedtb06a8302009-01-22 14:26:15 -05001819 /* Someone screwed up their debugging */
1820 WARN_ON_ONCE(1);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001821 global_trace.stop_count = 0;
Steven Rostedtb06a8302009-01-22 14:26:15 -05001822 }
Steven Rostedt0f048702008-11-05 16:05:44 -05001823 goto out;
1824 }
1825
Steven Rostedta2f80712010-03-12 19:56:00 -05001826 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001827 arch_spin_lock(&global_trace.max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -05001828
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001829 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001830 if (buffer)
1831 ring_buffer_record_enable(buffer);
1832
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001833#ifdef CONFIG_TRACER_MAX_TRACE
1834 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001835 if (buffer)
1836 ring_buffer_record_enable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001837#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001838
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001839 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001840
Steven Rostedt0f048702008-11-05 16:05:44 -05001841 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001842 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1843}
1844
1845static void tracing_start_tr(struct trace_array *tr)
1846{
1847 struct ring_buffer *buffer;
1848 unsigned long flags;
1849
1850 if (tracing_disabled)
1851 return;
1852
1853 /* If global, we need to also start the max tracer */
1854 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1855 return tracing_start();
1856
1857 raw_spin_lock_irqsave(&tr->start_lock, flags);
1858
1859 if (--tr->stop_count) {
1860 if (tr->stop_count < 0) {
1861 /* Someone screwed up their debugging */
1862 WARN_ON_ONCE(1);
1863 tr->stop_count = 0;
1864 }
1865 goto out;
1866 }
1867
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001868 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001869 if (buffer)
1870 ring_buffer_record_enable(buffer);
1871
1872 out:
1873 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001874}
1875
1876/**
1877 * tracing_stop - quick stop of the tracer
1878 *
1879 * Light weight way to stop tracing. Use in conjunction with
1880 * tracing_start.
1881 */
1882void tracing_stop(void)
1883{
1884 struct ring_buffer *buffer;
1885 unsigned long flags;
1886
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001887 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1888 if (global_trace.stop_count++)
Steven Rostedt0f048702008-11-05 16:05:44 -05001889 goto out;
1890
Steven Rostedta2f80712010-03-12 19:56:00 -05001891 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001892 arch_spin_lock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001893
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001894 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001895 if (buffer)
1896 ring_buffer_record_disable(buffer);
1897
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001898#ifdef CONFIG_TRACER_MAX_TRACE
1899 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001900 if (buffer)
1901 ring_buffer_record_disable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001902#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001903
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001904 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001905
Steven Rostedt0f048702008-11-05 16:05:44 -05001906 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001907 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1908}
1909
1910static void tracing_stop_tr(struct trace_array *tr)
1911{
1912 struct ring_buffer *buffer;
1913 unsigned long flags;
1914
1915 /* If global, we need to also stop the max tracer */
1916 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1917 return tracing_stop();
1918
1919 raw_spin_lock_irqsave(&tr->start_lock, flags);
1920 if (tr->stop_count++)
1921 goto out;
1922
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001923 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001924 if (buffer)
1925 ring_buffer_record_disable(buffer);
1926
1927 out:
1928 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001929}
1930
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001931static int trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001932{
Carsten Emdea635cf02009-03-18 09:00:41 +01001933 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001934
Joel Fernandeseaf260a2017-07-06 16:00:21 -07001935 /* treat recording of idle task as a success */
1936 if (!tsk->pid)
1937 return 1;
1938
1939 if (unlikely(tsk->pid > PID_MAX_DEFAULT))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001940 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001941
1942 /*
1943 * It's not the end of the world if we don't get
1944 * the lock, but we also don't want to spin
1945 * nor do we want to disable interrupts,
1946 * so if we miss here, then better luck next time.
1947 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001948 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001949 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001950
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001951 idx = savedcmd->map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001952 if (idx == NO_CMDLINE_MAP) {
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001953 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001954
Carsten Emdea635cf02009-03-18 09:00:41 +01001955 /*
1956 * Check whether the cmdline buffer at idx has a pid
1957 * mapped. We are going to overwrite that entry so we
1958 * need to clear the map_pid_to_cmdline. Otherwise we
1959 * would read the new comm for the old pid.
1960 */
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001961 pid = savedcmd->map_cmdline_to_pid[idx];
Carsten Emdea635cf02009-03-18 09:00:41 +01001962 if (pid != NO_CMDLINE_MAP)
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001963 savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001964
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001965 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
1966 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001967
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001968 savedcmd->cmdline_idx = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001969 }
1970
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001971 set_cmdline(idx, tsk->comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001972
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001973 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001974
1975 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001976}
1977
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04001978static void __trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001979{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001980 unsigned map;
1981
Steven Rostedt4ca530852009-03-16 19:20:15 -04001982 if (!pid) {
1983 strcpy(comm, "<idle>");
1984 return;
1985 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001986
Steven Rostedt74bf4072010-01-25 15:11:53 -05001987 if (WARN_ON_ONCE(pid < 0)) {
1988 strcpy(comm, "<XXX>");
1989 return;
1990 }
1991
Steven Rostedt4ca530852009-03-16 19:20:15 -04001992 if (pid > PID_MAX_DEFAULT) {
1993 strcpy(comm, "<...>");
1994 return;
1995 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001996
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001997 map = savedcmd->map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01001998 if (map != NO_CMDLINE_MAP)
Amey Telawanee09e2862017-05-03 15:41:14 +05301999 strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
Thomas Gleixner50d88752009-03-18 08:58:44 +01002000 else
2001 strcpy(comm, "<...>");
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04002002}
2003
2004void trace_find_cmdline(int pid, char comm[])
2005{
2006 preempt_disable();
2007 arch_spin_lock(&trace_cmdline_lock);
2008
2009 __trace_find_cmdline(pid, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002010
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002011 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02002012 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002013}
2014
Joel Fernandesd914ba32017-06-26 19:01:55 -07002015int trace_find_tgid(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002016{
Joel Fernandesd914ba32017-06-26 19:01:55 -07002017 if (unlikely(!tgid_map || !pid || pid > PID_MAX_DEFAULT))
2018 return 0;
2019
2020 return tgid_map[pid];
2021}
2022
2023static int trace_save_tgid(struct task_struct *tsk)
2024{
Joel Fernandesbd45d342017-07-06 16:00:22 -07002025 /* treat recording of idle task as a success */
2026 if (!tsk->pid)
2027 return 1;
2028
2029 if (unlikely(!tgid_map || tsk->pid > PID_MAX_DEFAULT))
Joel Fernandesd914ba32017-06-26 19:01:55 -07002030 return 0;
2031
2032 tgid_map[tsk->pid] = tsk->tgid;
2033 return 1;
2034}
2035
2036static bool tracing_record_taskinfo_skip(int flags)
2037{
2038 if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID))))
2039 return true;
2040 if (atomic_read(&trace_record_taskinfo_disabled) || !tracing_is_on())
2041 return true;
2042 if (!__this_cpu_read(trace_taskinfo_save))
2043 return true;
2044 return false;
2045}
2046
2047/**
2048 * tracing_record_taskinfo - record the task info of a task
2049 *
2050 * @task - task to record
2051 * @flags - TRACE_RECORD_CMDLINE for recording comm
2052 * - TRACE_RECORD_TGID for recording tgid
2053 */
2054void tracing_record_taskinfo(struct task_struct *task, int flags)
2055{
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002056 bool done;
2057
Joel Fernandesd914ba32017-06-26 19:01:55 -07002058 if (tracing_record_taskinfo_skip(flags))
2059 return;
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002060
2061 /*
2062 * Record as much task information as possible. If some fail, continue
2063 * to try to record the others.
2064 */
2065 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task);
2066 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task);
2067
2068 /* If recording any information failed, retry again soon. */
2069 if (!done)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002070 return;
2071
Joel Fernandesd914ba32017-06-26 19:01:55 -07002072 __this_cpu_write(trace_taskinfo_save, false);
2073}
2074
2075/**
2076 * tracing_record_taskinfo_sched_switch - record task info for sched_switch
2077 *
2078 * @prev - previous task during sched_switch
2079 * @next - next task during sched_switch
2080 * @flags - TRACE_RECORD_CMDLINE for recording comm
2081 * TRACE_RECORD_TGID for recording tgid
2082 */
2083void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
2084 struct task_struct *next, int flags)
2085{
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002086 bool done;
2087
Joel Fernandesd914ba32017-06-26 19:01:55 -07002088 if (tracing_record_taskinfo_skip(flags))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002089 return;
2090
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002091 /*
2092 * Record as much task information as possible. If some fail, continue
2093 * to try to record the others.
2094 */
2095 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev);
2096 done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next);
2097 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev);
2098 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next);
Joel Fernandesd914ba32017-06-26 19:01:55 -07002099
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002100 /* If recording any information failed, retry again soon. */
2101 if (!done)
Joel Fernandesd914ba32017-06-26 19:01:55 -07002102 return;
2103
2104 __this_cpu_write(trace_taskinfo_save, false);
2105}
2106
2107/* Helpers to record a specific task information */
2108void tracing_record_cmdline(struct task_struct *task)
2109{
2110 tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE);
2111}
2112
2113void tracing_record_tgid(struct task_struct *task)
2114{
2115 tracing_record_taskinfo(task, TRACE_RECORD_TGID);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002116}
2117
Steven Rostedt (VMware)af0009f2017-03-16 11:01:06 -04002118/*
2119 * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
2120 * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
2121 * simplifies those functions and keeps them in sync.
2122 */
2123enum print_line_t trace_handle_return(struct trace_seq *s)
2124{
2125 return trace_seq_has_overflowed(s) ?
2126 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2127}
2128EXPORT_SYMBOL_GPL(trace_handle_return);
2129
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03002130void
Steven Rostedt38697052008-10-01 13:14:09 -04002131tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
2132 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002133{
2134 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002135
Steven Rostedt777e2082008-09-29 23:02:42 -04002136 entry->preempt_count = pc & 0xff;
2137 entry->pid = (tsk) ? tsk->pid : 0;
2138 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04002139#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04002140 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04002141#else
2142 TRACE_FLAG_IRQS_NOSUPPORT |
2143#endif
Peter Zijlstra7e6867b2016-03-18 16:28:04 +01002144 ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002145 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
Pavankumar Kondetic59f29c2016-12-09 21:50:17 +05302146 ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
Peter Zijlstrae5137b52013-10-04 17:28:26 +02002147 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
2148 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002149}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002150EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002151
Steven Rostedte77405a2009-09-02 14:17:06 -04002152struct ring_buffer_event *
2153trace_buffer_lock_reserve(struct ring_buffer *buffer,
2154 int type,
2155 unsigned long len,
2156 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002157{
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002158 return __trace_buffer_lock_reserve(buffer, type, len, flags, pc);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002159}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002160
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002161DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2162DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2163static int trace_buffered_event_ref;
2164
2165/**
2166 * trace_buffered_event_enable - enable buffering events
2167 *
2168 * When events are being filtered, it is quicker to use a temporary
2169 * buffer to write the event data into if there's a likely chance
2170 * that it will not be committed. The discard of the ring buffer
2171 * is not as fast as committing, and is much slower than copying
2172 * a commit.
2173 *
2174 * When an event is to be filtered, allocate per cpu buffers to
2175 * write the event data into, and if the event is filtered and discarded
2176 * it is simply dropped, otherwise, the entire data is to be committed
2177 * in one shot.
2178 */
2179void trace_buffered_event_enable(void)
2180{
2181 struct ring_buffer_event *event;
2182 struct page *page;
2183 int cpu;
2184
2185 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2186
2187 if (trace_buffered_event_ref++)
2188 return;
2189
2190 for_each_tracing_cpu(cpu) {
2191 page = alloc_pages_node(cpu_to_node(cpu),
2192 GFP_KERNEL | __GFP_NORETRY, 0);
2193 if (!page)
2194 goto failed;
2195
2196 event = page_address(page);
2197 memset(event, 0, sizeof(*event));
2198
2199 per_cpu(trace_buffered_event, cpu) = event;
2200
2201 preempt_disable();
2202 if (cpu == smp_processor_id() &&
2203 this_cpu_read(trace_buffered_event) !=
2204 per_cpu(trace_buffered_event, cpu))
2205 WARN_ON_ONCE(1);
2206 preempt_enable();
2207 }
2208
2209 return;
2210 failed:
2211 trace_buffered_event_disable();
2212}
2213
2214static void enable_trace_buffered_event(void *data)
2215{
2216 /* Probably not needed, but do it anyway */
2217 smp_rmb();
2218 this_cpu_dec(trace_buffered_event_cnt);
2219}
2220
2221static void disable_trace_buffered_event(void *data)
2222{
2223 this_cpu_inc(trace_buffered_event_cnt);
2224}
2225
2226/**
2227 * trace_buffered_event_disable - disable buffering events
2228 *
2229 * When a filter is removed, it is faster to not use the buffered
2230 * events, and to commit directly into the ring buffer. Free up
2231 * the temp buffers when there are no more users. This requires
2232 * special synchronization with current events.
2233 */
2234void trace_buffered_event_disable(void)
2235{
2236 int cpu;
2237
2238 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2239
2240 if (WARN_ON_ONCE(!trace_buffered_event_ref))
2241 return;
2242
2243 if (--trace_buffered_event_ref)
2244 return;
2245
2246 preempt_disable();
2247 /* For each CPU, set the buffer as used. */
2248 smp_call_function_many(tracing_buffer_mask,
2249 disable_trace_buffered_event, NULL, 1);
2250 preempt_enable();
2251
2252 /* Wait for all current users to finish */
2253 synchronize_sched();
2254
2255 for_each_tracing_cpu(cpu) {
2256 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2257 per_cpu(trace_buffered_event, cpu) = NULL;
2258 }
2259 /*
2260 * Make sure trace_buffered_event is NULL before clearing
2261 * trace_buffered_event_cnt.
2262 */
2263 smp_wmb();
2264
2265 preempt_disable();
2266 /* Do the work on each cpu */
2267 smp_call_function_many(tracing_buffer_mask,
2268 enable_trace_buffered_event, NULL, 1);
2269 preempt_enable();
2270}
2271
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002272static struct ring_buffer *temp_buffer;
2273
Steven Rostedtef5580d2009-02-27 19:38:04 -05002274struct ring_buffer_event *
Steven Rostedtccb469a2012-08-02 10:32:10 -04002275trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002276 struct trace_event_file *trace_file,
Steven Rostedtccb469a2012-08-02 10:32:10 -04002277 int type, unsigned long len,
2278 unsigned long flags, int pc)
2279{
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002280 struct ring_buffer_event *entry;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002281 int val;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002282
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002283 *current_rb = trace_file->tr->trace_buffer.buffer;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002284
Tom Zanussi00b41452018-01-15 20:51:39 -06002285 if (!ring_buffer_time_stamp_abs(*current_rb) && (trace_file->flags &
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002286 (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
2287 (entry = this_cpu_read(trace_buffered_event))) {
2288 /* Try to use the per cpu buffer first */
2289 val = this_cpu_inc_return(trace_buffered_event_cnt);
2290 if (val == 1) {
2291 trace_event_setup(entry, type, flags, pc);
2292 entry->array[0] = len;
2293 return entry;
2294 }
2295 this_cpu_dec(trace_buffered_event_cnt);
2296 }
2297
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002298 entry = __trace_buffer_lock_reserve(*current_rb,
2299 type, len, flags, pc);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002300 /*
2301 * If tracing is off, but we have triggers enabled
2302 * we still need to look at the event data. Use the temp_buffer
2303 * to store the trace event for the tigger to use. It's recusive
2304 * safe and will not be recorded anywhere.
2305 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002306 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002307 *current_rb = temp_buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002308 entry = __trace_buffer_lock_reserve(*current_rb,
2309 type, len, flags, pc);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002310 }
2311 return entry;
Steven Rostedtccb469a2012-08-02 10:32:10 -04002312}
2313EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2314
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002315static DEFINE_SPINLOCK(tracepoint_iter_lock);
2316static DEFINE_MUTEX(tracepoint_printk_mutex);
2317
2318static void output_printk(struct trace_event_buffer *fbuffer)
2319{
2320 struct trace_event_call *event_call;
2321 struct trace_event *event;
2322 unsigned long flags;
2323 struct trace_iterator *iter = tracepoint_print_iter;
2324
2325 /* We should never get here if iter is NULL */
2326 if (WARN_ON_ONCE(!iter))
2327 return;
2328
2329 event_call = fbuffer->trace_file->event_call;
2330 if (!event_call || !event_call->event.funcs ||
2331 !event_call->event.funcs->trace)
2332 return;
2333
2334 event = &fbuffer->trace_file->event_call->event;
2335
2336 spin_lock_irqsave(&tracepoint_iter_lock, flags);
2337 trace_seq_init(&iter->seq);
2338 iter->ent = fbuffer->entry;
2339 event_call->event.funcs->trace(iter, 0, event);
2340 trace_seq_putc(&iter->seq, 0);
2341 printk("%s", iter->seq.buffer);
2342
2343 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2344}
2345
2346int tracepoint_printk_sysctl(struct ctl_table *table, int write,
2347 void __user *buffer, size_t *lenp,
2348 loff_t *ppos)
2349{
2350 int save_tracepoint_printk;
2351 int ret;
2352
2353 mutex_lock(&tracepoint_printk_mutex);
2354 save_tracepoint_printk = tracepoint_printk;
2355
2356 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2357
2358 /*
2359 * This will force exiting early, as tracepoint_printk
2360 * is always zero when tracepoint_printk_iter is not allocated
2361 */
2362 if (!tracepoint_print_iter)
2363 tracepoint_printk = 0;
2364
2365 if (save_tracepoint_printk == tracepoint_printk)
2366 goto out;
2367
2368 if (tracepoint_printk)
2369 static_key_enable(&tracepoint_printk_key.key);
2370 else
2371 static_key_disable(&tracepoint_printk_key.key);
2372
2373 out:
2374 mutex_unlock(&tracepoint_printk_mutex);
2375
2376 return ret;
2377}
2378
2379void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2380{
2381 if (static_key_false(&tracepoint_printk_key.key))
2382 output_printk(fbuffer);
2383
2384 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
2385 fbuffer->event, fbuffer->entry,
2386 fbuffer->flags, fbuffer->pc);
2387}
2388EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2389
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002390/*
2391 * Skip 3:
2392 *
2393 * trace_buffer_unlock_commit_regs()
2394 * trace_event_buffer_commit()
2395 * trace_event_raw_event_xxx()
Rohit Visavalia13cf9122018-01-29 15:11:26 +05302396 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002397# define STACK_SKIP 3
2398
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04002399void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2400 struct ring_buffer *buffer,
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04002401 struct ring_buffer_event *event,
2402 unsigned long flags, int pc,
2403 struct pt_regs *regs)
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002404{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002405 __buffer_unlock_commit(buffer, event);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002406
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002407 /*
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002408 * If regs is not set, then skip the necessary functions.
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002409 * Note, we can still get here via blktrace, wakeup tracer
2410 * and mmiotrace, but that's ok if they lose a function or
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002411 * two. They are not that meaningful.
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002412 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002413 ftrace_trace_stack(tr, buffer, flags, regs ? 0 : STACK_SKIP, pc, regs);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002414 ftrace_trace_userstack(buffer, flags, pc);
2415}
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002416
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -05002417/*
2418 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2419 */
2420void
2421trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer,
2422 struct ring_buffer_event *event)
2423{
2424 __buffer_unlock_commit(buffer, event);
2425}
2426
Chunyan Zhang478409d2016-11-21 15:57:18 +08002427static void
2428trace_process_export(struct trace_export *export,
2429 struct ring_buffer_event *event)
2430{
2431 struct trace_entry *entry;
2432 unsigned int size = 0;
2433
2434 entry = ring_buffer_event_data(event);
2435 size = ring_buffer_event_length(event);
Felipe Balbia773d412017-06-02 13:20:25 +03002436 export->write(export, entry, size);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002437}
2438
2439static DEFINE_MUTEX(ftrace_export_lock);
2440
2441static struct trace_export __rcu *ftrace_exports_list __read_mostly;
2442
2443static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled);
2444
2445static inline void ftrace_exports_enable(void)
2446{
2447 static_branch_enable(&ftrace_exports_enabled);
2448}
2449
2450static inline void ftrace_exports_disable(void)
2451{
2452 static_branch_disable(&ftrace_exports_enabled);
2453}
2454
2455void ftrace_exports(struct ring_buffer_event *event)
2456{
2457 struct trace_export *export;
2458
2459 preempt_disable_notrace();
2460
2461 export = rcu_dereference_raw_notrace(ftrace_exports_list);
2462 while (export) {
2463 trace_process_export(export, event);
2464 export = rcu_dereference_raw_notrace(export->next);
2465 }
2466
2467 preempt_enable_notrace();
2468}
2469
2470static inline void
2471add_trace_export(struct trace_export **list, struct trace_export *export)
2472{
2473 rcu_assign_pointer(export->next, *list);
2474 /*
2475 * We are entering export into the list but another
2476 * CPU might be walking that list. We need to make sure
2477 * the export->next pointer is valid before another CPU sees
2478 * the export pointer included into the list.
2479 */
2480 rcu_assign_pointer(*list, export);
2481}
2482
2483static inline int
2484rm_trace_export(struct trace_export **list, struct trace_export *export)
2485{
2486 struct trace_export **p;
2487
2488 for (p = list; *p != NULL; p = &(*p)->next)
2489 if (*p == export)
2490 break;
2491
2492 if (*p != export)
2493 return -1;
2494
2495 rcu_assign_pointer(*p, (*p)->next);
2496
2497 return 0;
2498}
2499
2500static inline void
2501add_ftrace_export(struct trace_export **list, struct trace_export *export)
2502{
2503 if (*list == NULL)
2504 ftrace_exports_enable();
2505
2506 add_trace_export(list, export);
2507}
2508
2509static inline int
2510rm_ftrace_export(struct trace_export **list, struct trace_export *export)
2511{
2512 int ret;
2513
2514 ret = rm_trace_export(list, export);
2515 if (*list == NULL)
2516 ftrace_exports_disable();
2517
2518 return ret;
2519}
2520
2521int register_ftrace_export(struct trace_export *export)
2522{
2523 if (WARN_ON_ONCE(!export->write))
2524 return -1;
2525
2526 mutex_lock(&ftrace_export_lock);
2527
2528 add_ftrace_export(&ftrace_exports_list, export);
2529
2530 mutex_unlock(&ftrace_export_lock);
2531
2532 return 0;
2533}
2534EXPORT_SYMBOL_GPL(register_ftrace_export);
2535
2536int unregister_ftrace_export(struct trace_export *export)
2537{
2538 int ret;
2539
2540 mutex_lock(&ftrace_export_lock);
2541
2542 ret = rm_ftrace_export(&ftrace_exports_list, export);
2543
2544 mutex_unlock(&ftrace_export_lock);
2545
2546 return ret;
2547}
2548EXPORT_SYMBOL_GPL(unregister_ftrace_export);
2549
Ingo Molnare309b412008-05-12 21:20:51 +02002550void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05002551trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04002552 unsigned long ip, unsigned long parent_ip, unsigned long flags,
2553 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002554{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002555 struct trace_event_call *call = &event_function;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002556 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002557 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04002558 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002559
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002560 event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
2561 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002562 if (!event)
2563 return;
2564 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04002565 entry->ip = ip;
2566 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05002567
Chunyan Zhang478409d2016-11-21 15:57:18 +08002568 if (!call_filter_check_discard(call, entry, buffer, event)) {
2569 if (static_branch_unlikely(&ftrace_exports_enabled))
2570 ftrace_exports(event);
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002571 __buffer_unlock_commit(buffer, event);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002572 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002573}
2574
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002575#ifdef CONFIG_STACKTRACE
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002576
2577#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
2578struct ftrace_stack {
2579 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
2580};
2581
2582static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
2583static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2584
Steven Rostedte77405a2009-09-02 14:17:06 -04002585static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05002586 unsigned long flags,
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002587 int skip, int pc, struct pt_regs *regs)
Ingo Molnar86387f72008-05-12 21:20:51 +02002588{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002589 struct trace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002590 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04002591 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02002592 struct stack_trace trace;
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002593 int use_stack;
2594 int size = FTRACE_STACK_ENTRIES;
Ingo Molnar86387f72008-05-12 21:20:51 +02002595
2596 trace.nr_entries = 0;
Ingo Molnar86387f72008-05-12 21:20:51 +02002597 trace.skip = skip;
Ingo Molnar86387f72008-05-12 21:20:51 +02002598
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002599 /*
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002600 * Add one, for this function and the call to save_stack_trace()
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002601 * If regs is set, then these functions will not be in the way.
2602 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002603#ifndef CONFIG_UNWINDER_ORC
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002604 if (!regs)
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002605 trace.skip++;
2606#endif
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002607
2608 /*
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002609 * Since events can happen in NMIs there's no safe way to
2610 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2611 * or NMI comes in, it will just have to use the default
2612 * FTRACE_STACK_SIZE.
2613 */
2614 preempt_disable_notrace();
2615
Shan Wei82146522012-11-19 13:21:01 +08002616 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002617 /*
2618 * We don't need any atomic variables, just a barrier.
2619 * If an interrupt comes in, we don't care, because it would
2620 * have exited and put the counter back to what we want.
2621 * We just need a barrier to keep gcc from moving things
2622 * around.
2623 */
2624 barrier();
2625 if (use_stack == 1) {
Christoph Lameterbdffd892014-04-29 14:17:40 -05002626 trace.entries = this_cpu_ptr(ftrace_stack.calls);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002627 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
2628
2629 if (regs)
2630 save_stack_trace_regs(regs, &trace);
2631 else
2632 save_stack_trace(&trace);
2633
2634 if (trace.nr_entries > size)
2635 size = trace.nr_entries;
2636 } else
2637 /* From now on, use_stack is a boolean */
2638 use_stack = 0;
2639
2640 size *= sizeof(unsigned long);
2641
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002642 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
2643 sizeof(*entry) + size, flags, pc);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002644 if (!event)
2645 goto out;
2646 entry = ring_buffer_event_data(event);
2647
2648 memset(&entry->caller, 0, size);
2649
2650 if (use_stack)
2651 memcpy(&entry->caller, trace.entries,
2652 trace.nr_entries * sizeof(unsigned long));
2653 else {
2654 trace.max_entries = FTRACE_STACK_ENTRIES;
2655 trace.entries = entry->caller;
2656 if (regs)
2657 save_stack_trace_regs(regs, &trace);
2658 else
2659 save_stack_trace(&trace);
2660 }
2661
2662 entry->size = trace.nr_entries;
2663
Tom Zanussif306cc82013-10-24 08:34:17 -05002664 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002665 __buffer_unlock_commit(buffer, event);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002666
2667 out:
2668 /* Again, don't let gcc optimize things here */
2669 barrier();
Shan Wei82146522012-11-19 13:21:01 +08002670 __this_cpu_dec(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002671 preempt_enable_notrace();
2672
Ingo Molnarf0a920d2008-05-12 21:20:47 +02002673}
2674
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002675static inline void ftrace_trace_stack(struct trace_array *tr,
2676 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002677 unsigned long flags,
2678 int skip, int pc, struct pt_regs *regs)
Steven Rostedt53614992009-01-15 19:12:40 -05002679{
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002680 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
Steven Rostedt53614992009-01-15 19:12:40 -05002681 return;
2682
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002683 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
Steven Rostedt53614992009-01-15 19:12:40 -05002684}
2685
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002686void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
2687 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04002688{
Steven Rostedt (VMware)a33d7d92017-05-12 13:15:45 -04002689 struct ring_buffer *buffer = tr->trace_buffer.buffer;
2690
2691 if (rcu_is_watching()) {
2692 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2693 return;
2694 }
2695
2696 /*
2697 * When an NMI triggers, RCU is enabled via rcu_nmi_enter(),
2698 * but if the above rcu_is_watching() failed, then the NMI
2699 * triggered someplace critical, and rcu_irq_enter() should
2700 * not be called from NMI.
2701 */
2702 if (unlikely(in_nmi()))
2703 return;
2704
Steven Rostedt (VMware)a33d7d92017-05-12 13:15:45 -04002705 rcu_irq_enter_irqson();
2706 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2707 rcu_irq_exit_irqson();
Steven Rostedt38697052008-10-01 13:14:09 -04002708}
2709
Steven Rostedt03889382009-12-11 09:48:22 -05002710/**
2711 * trace_dump_stack - record a stack back trace in the trace buffer
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002712 * @skip: Number of functions to skip (helper handlers)
Steven Rostedt03889382009-12-11 09:48:22 -05002713 */
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002714void trace_dump_stack(int skip)
Steven Rostedt03889382009-12-11 09:48:22 -05002715{
2716 unsigned long flags;
2717
2718 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05002719 return;
Steven Rostedt03889382009-12-11 09:48:22 -05002720
2721 local_save_flags(flags);
2722
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002723#ifndef CONFIG_UNWINDER_ORC
2724 /* Skip 1 to skip this function. */
2725 skip++;
2726#endif
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002727 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
2728 flags, skip, preempt_count(), NULL);
Steven Rostedt03889382009-12-11 09:48:22 -05002729}
Nikolay Borisovda387e52018-10-17 09:51:43 +03002730EXPORT_SYMBOL_GPL(trace_dump_stack);
Steven Rostedt03889382009-12-11 09:48:22 -05002731
Steven Rostedt91e86e52010-11-10 12:56:12 +01002732static DEFINE_PER_CPU(int, user_stack_count);
2733
Steven Rostedte77405a2009-09-02 14:17:06 -04002734void
2735ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02002736{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002737 struct trace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02002738 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02002739 struct userstack_entry *entry;
2740 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02002741
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04002742 if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE))
Török Edwin02b67512008-11-22 13:28:47 +02002743 return;
2744
Steven Rostedtb6345872010-03-12 20:03:30 -05002745 /*
2746 * NMIs can not handle page faults, even with fix ups.
2747 * The save user stack can (and often does) fault.
2748 */
2749 if (unlikely(in_nmi()))
2750 return;
2751
Steven Rostedt91e86e52010-11-10 12:56:12 +01002752 /*
2753 * prevent recursion, since the user stack tracing may
2754 * trigger other kernel events.
2755 */
2756 preempt_disable();
2757 if (__this_cpu_read(user_stack_count))
2758 goto out;
2759
2760 __this_cpu_inc(user_stack_count);
2761
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002762 event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
2763 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02002764 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08002765 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02002766 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02002767
Steven Rostedt48659d32009-09-11 11:36:23 -04002768 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02002769 memset(&entry->caller, 0, sizeof(entry->caller));
2770
2771 trace.nr_entries = 0;
2772 trace.max_entries = FTRACE_STACK_ENTRIES;
2773 trace.skip = 0;
2774 trace.entries = entry->caller;
2775
2776 save_stack_trace_user(&trace);
Tom Zanussif306cc82013-10-24 08:34:17 -05002777 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002778 __buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01002779
Li Zefan1dbd1952010-12-09 15:47:56 +08002780 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01002781 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01002782 out:
2783 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02002784}
2785
Hannes Eder4fd27352009-02-10 19:44:12 +01002786#ifdef UNUSED
2787static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02002788{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05002789 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02002790}
Hannes Eder4fd27352009-02-10 19:44:12 +01002791#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02002792
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002793#endif /* CONFIG_STACKTRACE */
2794
Steven Rostedt07d777f2011-09-22 14:01:55 -04002795/* created for use with alloc_percpu */
2796struct trace_buffer_struct {
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002797 int nesting;
2798 char buffer[4][TRACE_BUF_SIZE];
Steven Rostedt07d777f2011-09-22 14:01:55 -04002799};
2800
2801static struct trace_buffer_struct *trace_percpu_buffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002802
2803/*
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002804 * Thise allows for lockless recording. If we're nested too deeply, then
2805 * this returns NULL.
Steven Rostedt07d777f2011-09-22 14:01:55 -04002806 */
2807static char *get_trace_buf(void)
2808{
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002809 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
Steven Rostedt07d777f2011-09-22 14:01:55 -04002810
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002811 if (!buffer || buffer->nesting >= 4)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002812 return NULL;
2813
Steven Rostedt (VMware)3d9622c2017-09-05 11:32:01 -04002814 buffer->nesting++;
2815
2816 /* Interrupts must see nesting incremented before we use the buffer */
2817 barrier();
2818 return &buffer->buffer[buffer->nesting][0];
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002819}
2820
2821static void put_trace_buf(void)
2822{
Steven Rostedt (VMware)3d9622c2017-09-05 11:32:01 -04002823 /* Don't let the decrement of nesting leak before this */
2824 barrier();
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002825 this_cpu_dec(trace_percpu_buffer->nesting);
Steven Rostedt07d777f2011-09-22 14:01:55 -04002826}
2827
2828static int alloc_percpu_trace_buffer(void)
2829{
2830 struct trace_buffer_struct *buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002831
2832 buffers = alloc_percpu(struct trace_buffer_struct);
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002833 if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
2834 return -ENOMEM;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002835
2836 trace_percpu_buffer = buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002837 return 0;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002838}
2839
Steven Rostedt81698832012-10-11 10:15:05 -04002840static int buffers_allocated;
2841
Steven Rostedt07d777f2011-09-22 14:01:55 -04002842void trace_printk_init_buffers(void)
2843{
Steven Rostedt07d777f2011-09-22 14:01:55 -04002844 if (buffers_allocated)
2845 return;
2846
2847 if (alloc_percpu_trace_buffer())
2848 return;
2849
Steven Rostedt2184db42014-05-28 13:14:40 -04002850 /* trace_printk() is for debug use only. Don't use it in production. */
2851
Joe Perchesa395d6a2016-03-22 14:28:09 -07002852 pr_warn("\n");
2853 pr_warn("**********************************************************\n");
2854 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2855 pr_warn("** **\n");
2856 pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
2857 pr_warn("** **\n");
2858 pr_warn("** This means that this is a DEBUG kernel and it is **\n");
2859 pr_warn("** unsafe for production use. **\n");
2860 pr_warn("** **\n");
2861 pr_warn("** If you see this message and you are not debugging **\n");
2862 pr_warn("** the kernel, report this immediately to your vendor! **\n");
2863 pr_warn("** **\n");
2864 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2865 pr_warn("**********************************************************\n");
Steven Rostedt07d777f2011-09-22 14:01:55 -04002866
Steven Rostedtb382ede62012-10-10 21:44:34 -04002867 /* Expand the buffers to set size */
2868 tracing_update_buffers();
2869
Steven Rostedt07d777f2011-09-22 14:01:55 -04002870 buffers_allocated = 1;
Steven Rostedt81698832012-10-11 10:15:05 -04002871
2872 /*
2873 * trace_printk_init_buffers() can be called by modules.
2874 * If that happens, then we need to start cmdline recording
2875 * directly here. If the global_trace.buffer is already
2876 * allocated here, then this was called by module code.
2877 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002878 if (global_trace.trace_buffer.buffer)
Steven Rostedt81698832012-10-11 10:15:05 -04002879 tracing_start_cmdline_record();
2880}
2881
2882void trace_printk_start_comm(void)
2883{
2884 /* Start tracing comms if trace printk is set */
2885 if (!buffers_allocated)
2886 return;
2887 tracing_start_cmdline_record();
2888}
2889
2890static void trace_printk_start_stop_comm(int enabled)
2891{
2892 if (!buffers_allocated)
2893 return;
2894
2895 if (enabled)
2896 tracing_start_cmdline_record();
2897 else
2898 tracing_stop_cmdline_record();
Steven Rostedt07d777f2011-09-22 14:01:55 -04002899}
2900
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002901/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002902 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002903 *
2904 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04002905int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002906{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002907 struct trace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002908 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04002909 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002910 struct trace_array *tr = &global_trace;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002911 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002912 unsigned long flags;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002913 char *tbuffer;
2914 int len = 0, size, pc;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002915
2916 if (unlikely(tracing_selftest_running || tracing_disabled))
2917 return 0;
2918
2919 /* Don't pollute graph traces with trace_vprintk internals */
2920 pause_graph_tracing();
2921
2922 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04002923 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002924
Steven Rostedt07d777f2011-09-22 14:01:55 -04002925 tbuffer = get_trace_buf();
2926 if (!tbuffer) {
2927 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002928 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002929 }
2930
2931 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
2932
2933 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002934 goto out;
2935
Steven Rostedt07d777f2011-09-22 14:01:55 -04002936 local_save_flags(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002937 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002938 buffer = tr->trace_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002939 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2940 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002941 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002942 goto out;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002943 entry = ring_buffer_event_data(event);
2944 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002945 entry->fmt = fmt;
2946
Steven Rostedt07d777f2011-09-22 14:01:55 -04002947 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
Tom Zanussif306cc82013-10-24 08:34:17 -05002948 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002949 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002950 ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05002951 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002952
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002953out:
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002954 put_trace_buf();
2955
2956out_nobuffer:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002957 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002958 unpause_graph_tracing();
2959
2960 return len;
2961}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002962EXPORT_SYMBOL_GPL(trace_vbprintk);
2963
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01002964__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002965static int
2966__trace_array_vprintk(struct ring_buffer *buffer,
2967 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002968{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002969 struct trace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002970 struct ring_buffer_event *event;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002971 int len = 0, size, pc;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002972 struct print_entry *entry;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002973 unsigned long flags;
2974 char *tbuffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002975
2976 if (tracing_disabled || tracing_selftest_running)
2977 return 0;
2978
Steven Rostedt07d777f2011-09-22 14:01:55 -04002979 /* Don't pollute graph traces with trace_vprintk internals */
2980 pause_graph_tracing();
2981
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002982 pc = preempt_count();
2983 preempt_disable_notrace();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002984
Steven Rostedt07d777f2011-09-22 14:01:55 -04002985
2986 tbuffer = get_trace_buf();
2987 if (!tbuffer) {
2988 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002989 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002990 }
2991
Dan Carpenter3558a5a2014-11-27 18:57:52 +03002992 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002993
Steven Rostedt07d777f2011-09-22 14:01:55 -04002994 local_save_flags(flags);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002995 size = sizeof(*entry) + len + 1;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002996 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
2997 flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002998 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002999 goto out;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003000 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01003001 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003002
Dan Carpenter3558a5a2014-11-27 18:57:52 +03003003 memcpy(&entry->buf, tbuffer, len + 1);
Tom Zanussif306cc82013-10-24 08:34:17 -05003004 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04003005 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04003006 ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05003007 }
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003008
3009out:
3010 put_trace_buf();
3011
3012out_nobuffer:
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003013 preempt_enable_notrace();
Steven Rostedt07d777f2011-09-22 14:01:55 -04003014 unpause_graph_tracing();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003015
3016 return len;
3017}
Steven Rostedt659372d2009-09-03 19:11:07 -04003018
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003019__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003020int trace_array_vprintk(struct trace_array *tr,
3021 unsigned long ip, const char *fmt, va_list args)
3022{
3023 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
3024}
3025
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003026__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003027int trace_array_printk(struct trace_array *tr,
3028 unsigned long ip, const char *fmt, ...)
3029{
3030 int ret;
3031 va_list ap;
3032
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003033 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003034 return 0;
3035
3036 va_start(ap, fmt);
3037 ret = trace_array_vprintk(tr, ip, fmt, ap);
3038 va_end(ap);
3039 return ret;
3040}
3041
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003042__printf(3, 4)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003043int trace_array_printk_buf(struct ring_buffer *buffer,
3044 unsigned long ip, const char *fmt, ...)
3045{
3046 int ret;
3047 va_list ap;
3048
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003049 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003050 return 0;
3051
3052 va_start(ap, fmt);
3053 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3054 va_end(ap);
3055 return ret;
3056}
3057
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003058__printf(2, 0)
Steven Rostedt659372d2009-09-03 19:11:07 -04003059int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3060{
Steven Rostedta813a152009-10-09 01:41:35 -04003061 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04003062}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003063EXPORT_SYMBOL_GPL(trace_vprintk);
3064
Robert Richtere2ac8ef2008-11-12 12:59:32 +01003065static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04003066{
Steven Rostedt6d158a82012-06-27 20:46:14 -04003067 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3068
Steven Rostedt5a90f572008-09-03 17:42:51 -04003069 iter->idx++;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003070 if (buf_iter)
3071 ring_buffer_read(buf_iter, NULL);
Steven Rostedt5a90f572008-09-03 17:42:51 -04003072}
3073
Ingo Molnare309b412008-05-12 21:20:51 +02003074static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04003075peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3076 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003077{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003078 struct ring_buffer_event *event;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003079 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003080
Steven Rostedtd7690412008-10-01 00:29:53 -04003081 if (buf_iter)
3082 event = ring_buffer_iter_peek(buf_iter, ts);
3083 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003084 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04003085 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04003086
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04003087 if (event) {
3088 iter->ent_size = ring_buffer_event_length(event);
3089 return ring_buffer_event_data(event);
3090 }
3091 iter->ent_size = 0;
3092 return NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003093}
Steven Rostedtd7690412008-10-01 00:29:53 -04003094
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003095static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04003096__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3097 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003098{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003099 struct ring_buffer *buffer = iter->trace_buffer->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003100 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08003101 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003102 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003103 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003104 int next_cpu = -1;
Steven Rostedt12b5da32012-03-27 10:43:28 -04003105 int next_size = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003106 int cpu;
3107
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003108 /*
3109 * If we are in a per_cpu trace file, don't bother by iterating over
3110 * all cpu and peek directly.
3111 */
Steven Rostedtae3b5092013-01-23 15:22:59 -05003112 if (cpu_file > RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003113 if (ring_buffer_empty_cpu(buffer, cpu_file))
3114 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04003115 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003116 if (ent_cpu)
3117 *ent_cpu = cpu_file;
3118
3119 return ent;
3120 }
3121
Steven Rostedtab464282008-05-12 21:21:00 +02003122 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003123
3124 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003125 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003126
Steven Rostedtbc21b472010-03-31 19:49:26 -04003127 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003128
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02003129 /*
3130 * Pick the entry with the smallest timestamp:
3131 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003132 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003133 next = ent;
3134 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003135 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04003136 next_lost = lost_events;
Steven Rostedt12b5da32012-03-27 10:43:28 -04003137 next_size = iter->ent_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003138 }
3139 }
3140
Steven Rostedt12b5da32012-03-27 10:43:28 -04003141 iter->ent_size = next_size;
3142
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003143 if (ent_cpu)
3144 *ent_cpu = next_cpu;
3145
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003146 if (ent_ts)
3147 *ent_ts = next_ts;
3148
Steven Rostedtbc21b472010-03-31 19:49:26 -04003149 if (missing_events)
3150 *missing_events = next_lost;
3151
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003152 return next;
3153}
3154
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003155/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003156struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3157 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02003158{
Steven Rostedtbc21b472010-03-31 19:49:26 -04003159 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003160}
Ingo Molnar8c523a92008-05-12 21:20:46 +02003161
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003162/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05003163void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003164{
Steven Rostedtbc21b472010-03-31 19:49:26 -04003165 iter->ent = __find_next_entry(iter, &iter->cpu,
3166 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003167
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003168 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01003169 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003170
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003171 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003172}
3173
Ingo Molnare309b412008-05-12 21:20:51 +02003174static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02003175{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003176 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04003177 &iter->lost_events);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003178}
3179
Ingo Molnare309b412008-05-12 21:20:51 +02003180static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003181{
3182 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003183 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003184 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003185
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003186 WARN_ON_ONCE(iter->leftover);
3187
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003188 (*pos)++;
3189
3190 /* can't go backwards */
3191 if (iter->idx > i)
3192 return NULL;
3193
3194 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05003195 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003196 else
3197 ent = iter;
3198
3199 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05003200 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003201
3202 iter->pos = *pos;
3203
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003204 return ent;
3205}
3206
Jason Wessel955b61e2010-08-05 09:22:23 -05003207void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003208{
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003209 struct ring_buffer_event *event;
3210 struct ring_buffer_iter *buf_iter;
3211 unsigned long entries = 0;
3212 u64 ts;
3213
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003214 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003215
Steven Rostedt6d158a82012-06-27 20:46:14 -04003216 buf_iter = trace_buffer_iter(iter, cpu);
3217 if (!buf_iter)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003218 return;
3219
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003220 ring_buffer_iter_reset(buf_iter);
3221
3222 /*
3223 * We could have the case with the max latency tracers
3224 * that a reset never took place on a cpu. This is evident
3225 * by the timestamp being before the start of the buffer.
3226 */
3227 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003228 if (ts >= iter->trace_buffer->time_start)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003229 break;
3230 entries++;
3231 ring_buffer_read(buf_iter, NULL);
3232 }
3233
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003234 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003235}
3236
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003237/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003238 * The current tracer is copied to avoid a global locking
3239 * all around.
3240 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003241static void *s_start(struct seq_file *m, loff_t *pos)
3242{
3243 struct trace_iterator *iter = m->private;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003244 struct trace_array *tr = iter->tr;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003245 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003246 void *p = NULL;
3247 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003248 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003249
Hiraku Toyooka2fd196e2012-12-26 11:52:52 +09003250 /*
3251 * copy the tracer to avoid using a global lock all around.
3252 * iter->trace is a copy of current_trace, the pointer to the
3253 * name may be used instead of a strcmp(), as iter->trace->name
3254 * will point to the same string as current_trace->name.
3255 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003256 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003257 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
3258 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003259 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003260
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003261#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003262 if (iter->snapshot && iter->trace->use_max_tr)
3263 return ERR_PTR(-EBUSY);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003264#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003265
3266 if (!iter->snapshot)
Joel Fernandesd914ba32017-06-26 19:01:55 -07003267 atomic_inc(&trace_record_taskinfo_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003268
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003269 if (*pos != iter->pos) {
3270 iter->ent = NULL;
3271 iter->cpu = 0;
3272 iter->idx = -1;
3273
Steven Rostedtae3b5092013-01-23 15:22:59 -05003274 if (cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003275 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003276 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003277 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003278 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003279
Lai Jiangshanac91d852010-03-02 17:54:50 +08003280 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003281 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
3282 ;
3283
3284 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003285 /*
3286 * If we overflowed the seq_file before, then we want
3287 * to just reuse the trace_seq buffer again.
3288 */
3289 if (iter->leftover)
3290 p = iter;
3291 else {
3292 l = *pos - 1;
3293 p = s_next(m, p, &l);
3294 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003295 }
3296
Lai Jiangshan4f535962009-05-18 19:35:34 +08003297 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003298 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003299 return p;
3300}
3301
3302static void s_stop(struct seq_file *m, void *p)
3303{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003304 struct trace_iterator *iter = m->private;
3305
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003306#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003307 if (iter->snapshot && iter->trace->use_max_tr)
3308 return;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003309#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003310
3311 if (!iter->snapshot)
Joel Fernandesd914ba32017-06-26 19:01:55 -07003312 atomic_dec(&trace_record_taskinfo_disabled);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003313
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003314 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003315 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003316}
3317
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003318static void
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003319get_total_entries(struct trace_buffer *buf,
3320 unsigned long *total, unsigned long *entries)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003321{
3322 unsigned long count;
3323 int cpu;
3324
3325 *total = 0;
3326 *entries = 0;
3327
3328 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003329 count = ring_buffer_entries_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003330 /*
3331 * If this buffer has skipped entries, then we hold all
3332 * entries for the trace and we need to ignore the
3333 * ones before the time stamp.
3334 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003335 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3336 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003337 /* total is the same as the entries */
3338 *total += count;
3339 } else
3340 *total += count +
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003341 ring_buffer_overrun_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003342 *entries += count;
3343 }
3344}
3345
Ingo Molnare309b412008-05-12 21:20:51 +02003346static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003347{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003348 seq_puts(m, "# _------=> CPU# \n"
3349 "# / _-----=> irqs-off \n"
3350 "# | / _----=> need-resched \n"
3351 "# || / _---=> hardirq/softirq \n"
3352 "# ||| / _--=> preempt-depth \n"
3353 "# |||| / delay \n"
3354 "# cmd pid ||||| time | caller \n"
3355 "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003356}
3357
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003358static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003359{
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003360 unsigned long total;
3361 unsigned long entries;
3362
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003363 get_total_entries(buf, &total, &entries);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003364 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
3365 entries, total, num_online_cpus());
3366 seq_puts(m, "#\n");
3367}
3368
Joel Fernandes441dae82017-06-25 22:38:43 -07003369static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m,
3370 unsigned int flags)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003371{
Joel Fernandes441dae82017-06-25 22:38:43 -07003372 bool tgid = flags & TRACE_ITER_RECORD_TGID;
3373
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003374 print_event_info(buf, m);
Joel Fernandes441dae82017-06-25 22:38:43 -07003375
Joel Fernandes (Google)f8494fa2018-06-25 17:08:22 -07003376 seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? "TGID " : "");
3377 seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003378}
3379
Joel Fernandes441dae82017-06-25 22:38:43 -07003380static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m,
3381 unsigned int flags)
Steven Rostedt77271ce2011-11-17 09:34:33 -05003382{
Joel Fernandes441dae82017-06-25 22:38:43 -07003383 bool tgid = flags & TRACE_ITER_RECORD_TGID;
Steven Rostedt (VMware)b11fb732017-07-11 15:43:24 -04003384 const char tgid_space[] = " ";
3385 const char space[] = " ";
Joel Fernandes441dae82017-06-25 22:38:43 -07003386
Steven Rostedt (VMware)b11fb732017-07-11 15:43:24 -04003387 seq_printf(m, "# %s _-----=> irqs-off\n",
3388 tgid ? tgid_space : space);
3389 seq_printf(m, "# %s / _----=> need-resched\n",
3390 tgid ? tgid_space : space);
3391 seq_printf(m, "# %s| / _---=> hardirq/softirq\n",
3392 tgid ? tgid_space : space);
3393 seq_printf(m, "# %s|| / _--=> preempt-depth\n",
3394 tgid ? tgid_space : space);
3395 seq_printf(m, "# %s||| / delay\n",
3396 tgid ? tgid_space : space);
Joel Fernandes (Google)f8494fa2018-06-25 17:08:22 -07003397 seq_printf(m, "# TASK-PID %sCPU# |||| TIMESTAMP FUNCTION\n",
Steven Rostedt (VMware)b11fb732017-07-11 15:43:24 -04003398 tgid ? " TGID " : space);
Joel Fernandes (Google)f8494fa2018-06-25 17:08:22 -07003399 seq_printf(m, "# | | %s | |||| | |\n",
Steven Rostedt (VMware)b11fb732017-07-11 15:43:24 -04003400 tgid ? " | " : space);
Steven Rostedt77271ce2011-11-17 09:34:33 -05003401}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003402
Jiri Olsa62b915f2010-04-02 19:01:22 +02003403void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003404print_trace_header(struct seq_file *m, struct trace_iterator *iter)
3405{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003406 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003407 struct trace_buffer *buf = iter->trace_buffer;
3408 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003409 struct tracer *type = iter->trace;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003410 unsigned long entries;
3411 unsigned long total;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003412 const char *name = "preemption";
3413
Steven Rostedt (Red Hat)d840f712013-02-01 18:38:47 -05003414 name = type->name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003415
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003416 get_total_entries(buf, &total, &entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003417
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003418 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003419 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003420 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003421 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003422 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003423 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02003424 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003425 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003426 total,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003427 buf->cpu,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003428#if defined(CONFIG_PREEMPT_NONE)
3429 "server",
3430#elif defined(CONFIG_PREEMPT_VOLUNTARY)
3431 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04003432#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003433 "preempt",
3434#else
3435 "unknown",
3436#endif
3437 /* These are reserved for later use */
3438 0, 0, 0, 0);
3439#ifdef CONFIG_SMP
3440 seq_printf(m, " #P:%d)\n", num_online_cpus());
3441#else
3442 seq_puts(m, ")\n");
3443#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003444 seq_puts(m, "# -----------------\n");
3445 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003446 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
Eric W. Biedermand20b92a2012-03-13 16:02:19 -07003447 data->comm, data->pid,
3448 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003449 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003450 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003451
3452 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003453 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02003454 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
3455 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003456 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02003457 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
3458 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04003459 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003460 }
3461
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003462 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003463}
3464
Steven Rostedta3097202008-11-07 22:36:02 -05003465static void test_cpu_buff_start(struct trace_iterator *iter)
3466{
3467 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003468 struct trace_array *tr = iter->tr;
Steven Rostedta3097202008-11-07 22:36:02 -05003469
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003470 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003471 return;
3472
3473 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
3474 return;
3475
Matthias Kaehlcke4dbbe2d2017-04-21 16:41:10 -07003476 if (cpumask_available(iter->started) &&
3477 cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05003478 return;
3479
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003480 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003481 return;
3482
Matthias Kaehlcke4dbbe2d2017-04-21 16:41:10 -07003483 if (cpumask_available(iter->started))
Sasha Levin919cd972015-09-04 12:45:56 -04003484 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003485
3486 /* Don't print started cpu buffer for the first entry of the trace */
3487 if (iter->idx > 1)
3488 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
3489 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05003490}
3491
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003492static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003493{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003494 struct trace_array *tr = iter->tr;
Steven Rostedt214023c2008-05-12 21:20:46 +02003495 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003496 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003497 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003498 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003499
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003500 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003501
Steven Rostedta3097202008-11-07 22:36:02 -05003502 test_cpu_buff_start(iter);
3503
Steven Rostedtf633cef2008-12-23 23:24:13 -05003504 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003505
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003506 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003507 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3508 trace_print_lat_context(iter);
3509 else
3510 trace_print_context(iter);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003511 }
3512
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003513 if (trace_seq_has_overflowed(s))
3514 return TRACE_TYPE_PARTIAL_LINE;
3515
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003516 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003517 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003518
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003519 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003520
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003521 return trace_handle_return(s);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003522}
3523
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003524static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003525{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003526 struct trace_array *tr = iter->tr;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003527 struct trace_seq *s = &iter->seq;
3528 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003529 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003530
3531 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003532
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003533 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003534 trace_seq_printf(s, "%d %d %llu ",
3535 entry->pid, iter->cpu, iter->ts);
3536
3537 if (trace_seq_has_overflowed(s))
3538 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003539
Steven Rostedtf633cef2008-12-23 23:24:13 -05003540 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003541 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003542 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003543
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003544 trace_seq_printf(s, "%d ?\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003545
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003546 return trace_handle_return(s);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003547}
3548
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003549static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003550{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003551 struct trace_array *tr = iter->tr;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003552 struct trace_seq *s = &iter->seq;
3553 unsigned char newline = '\n';
3554 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003555 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003556
3557 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003558
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003559 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003560 SEQ_PUT_HEX_FIELD(s, entry->pid);
3561 SEQ_PUT_HEX_FIELD(s, iter->cpu);
3562 SEQ_PUT_HEX_FIELD(s, iter->ts);
3563 if (trace_seq_has_overflowed(s))
3564 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003565 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003566
Steven Rostedtf633cef2008-12-23 23:24:13 -05003567 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003568 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04003569 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003570 if (ret != TRACE_TYPE_HANDLED)
3571 return ret;
3572 }
Steven Rostedt7104f302008-10-01 10:52:51 -04003573
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003574 SEQ_PUT_FIELD(s, newline);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003575
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003576 return trace_handle_return(s);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003577}
3578
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003579static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003580{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003581 struct trace_array *tr = iter->tr;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003582 struct trace_seq *s = &iter->seq;
3583 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003584 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003585
3586 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003587
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003588 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003589 SEQ_PUT_FIELD(s, entry->pid);
3590 SEQ_PUT_FIELD(s, iter->cpu);
3591 SEQ_PUT_FIELD(s, iter->ts);
3592 if (trace_seq_has_overflowed(s))
3593 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003594 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003595
Steven Rostedtf633cef2008-12-23 23:24:13 -05003596 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04003597 return event ? event->funcs->binary(iter, 0, event) :
3598 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003599}
3600
Jiri Olsa62b915f2010-04-02 19:01:22 +02003601int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003602{
Steven Rostedt6d158a82012-06-27 20:46:14 -04003603 struct ring_buffer_iter *buf_iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003604 int cpu;
3605
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003606 /* If we are looking at one CPU buffer, only check that one */
Steven Rostedtae3b5092013-01-23 15:22:59 -05003607 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003608 cpu = iter->cpu_file;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003609 buf_iter = trace_buffer_iter(iter, cpu);
3610 if (buf_iter) {
3611 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003612 return 0;
3613 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003614 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003615 return 0;
3616 }
3617 return 1;
3618 }
3619
Steven Rostedtab464282008-05-12 21:21:00 +02003620 for_each_tracing_cpu(cpu) {
Steven Rostedt6d158a82012-06-27 20:46:14 -04003621 buf_iter = trace_buffer_iter(iter, cpu);
3622 if (buf_iter) {
3623 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedtd7690412008-10-01 00:29:53 -04003624 return 0;
3625 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003626 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedtd7690412008-10-01 00:29:53 -04003627 return 0;
3628 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003629 }
Steven Rostedtd7690412008-10-01 00:29:53 -04003630
Frederic Weisbecker797d3712008-09-30 18:13:45 +02003631 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003632}
3633
Lai Jiangshan4f535962009-05-18 19:35:34 +08003634/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05003635enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003636{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003637 struct trace_array *tr = iter->tr;
3638 unsigned long trace_flags = tr->trace_flags;
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003639 enum print_line_t ret;
3640
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003641 if (iter->lost_events) {
3642 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3643 iter->cpu, iter->lost_events);
3644 if (trace_seq_has_overflowed(&iter->seq))
3645 return TRACE_TYPE_PARTIAL_LINE;
3646 }
Steven Rostedtbc21b472010-03-31 19:49:26 -04003647
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003648 if (iter->trace && iter->trace->print_line) {
3649 ret = iter->trace->print_line(iter);
3650 if (ret != TRACE_TYPE_UNHANDLED)
3651 return ret;
3652 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02003653
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -05003654 if (iter->ent->type == TRACE_BPUTS &&
3655 trace_flags & TRACE_ITER_PRINTK &&
3656 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3657 return trace_print_bputs_msg_only(iter);
3658
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003659 if (iter->ent->type == TRACE_BPRINT &&
3660 trace_flags & TRACE_ITER_PRINTK &&
3661 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04003662 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003663
Frederic Weisbecker66896a82008-12-13 20:18:13 +01003664 if (iter->ent->type == TRACE_PRINT &&
3665 trace_flags & TRACE_ITER_PRINTK &&
3666 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04003667 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01003668
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003669 if (trace_flags & TRACE_ITER_BIN)
3670 return print_bin_fmt(iter);
3671
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003672 if (trace_flags & TRACE_ITER_HEX)
3673 return print_hex_fmt(iter);
3674
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003675 if (trace_flags & TRACE_ITER_RAW)
3676 return print_raw_fmt(iter);
3677
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003678 return print_trace_fmt(iter);
3679}
3680
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003681void trace_latency_header(struct seq_file *m)
3682{
3683 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003684 struct trace_array *tr = iter->tr;
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003685
3686 /* print nothing if the buffers are empty */
3687 if (trace_empty(iter))
3688 return;
3689
3690 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3691 print_trace_header(m, iter);
3692
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003693 if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003694 print_lat_help_header(m);
3695}
3696
Jiri Olsa62b915f2010-04-02 19:01:22 +02003697void trace_default_header(struct seq_file *m)
3698{
3699 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003700 struct trace_array *tr = iter->tr;
3701 unsigned long trace_flags = tr->trace_flags;
Jiri Olsa62b915f2010-04-02 19:01:22 +02003702
Jiri Olsaf56e7f82011-06-03 16:58:49 +02003703 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
3704 return;
3705
Jiri Olsa62b915f2010-04-02 19:01:22 +02003706 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
3707 /* print nothing if the buffers are empty */
3708 if (trace_empty(iter))
3709 return;
3710 print_trace_header(m, iter);
3711 if (!(trace_flags & TRACE_ITER_VERBOSE))
3712 print_lat_help_header(m);
3713 } else {
Steven Rostedt77271ce2011-11-17 09:34:33 -05003714 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
3715 if (trace_flags & TRACE_ITER_IRQ_INFO)
Joel Fernandes441dae82017-06-25 22:38:43 -07003716 print_func_help_header_irq(iter->trace_buffer,
3717 m, trace_flags);
Steven Rostedt77271ce2011-11-17 09:34:33 -05003718 else
Joel Fernandes441dae82017-06-25 22:38:43 -07003719 print_func_help_header(iter->trace_buffer, m,
3720 trace_flags);
Steven Rostedt77271ce2011-11-17 09:34:33 -05003721 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02003722 }
3723}
3724
Steven Rostedte0a413f2011-09-29 21:26:16 -04003725static void test_ftrace_alive(struct seq_file *m)
3726{
3727 if (!ftrace_is_dead())
3728 return;
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003729 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
3730 "# MAY BE MISSING FUNCTION EVENTS\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04003731}
3732
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003733#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003734static void show_snapshot_main_help(struct seq_file *m)
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003735{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003736 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
3737 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3738 "# Takes a snapshot of the main buffer.\n"
3739 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
3740 "# (Doesn't have to be '2' works with any number that\n"
3741 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003742}
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003743
3744static void show_snapshot_percpu_help(struct seq_file *m)
3745{
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003746 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003747#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003748 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3749 "# Takes a snapshot of the main buffer for this cpu.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003750#else
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003751 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
3752 "# Must use main snapshot file to allocate.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003753#endif
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003754 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
3755 "# (Doesn't have to be '2' works with any number that\n"
3756 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003757}
3758
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003759static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
3760{
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05003761 if (iter->tr->allocated_snapshot)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003762 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003763 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003764 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003765
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003766 seq_puts(m, "# Snapshot commands:\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003767 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
3768 show_snapshot_main_help(m);
3769 else
3770 show_snapshot_percpu_help(m);
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003771}
3772#else
3773/* Should never be called */
3774static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
3775#endif
3776
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003777static int s_show(struct seq_file *m, void *v)
3778{
3779 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003780 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003781
3782 if (iter->ent == NULL) {
3783 if (iter->tr) {
3784 seq_printf(m, "# tracer: %s\n", iter->trace->name);
3785 seq_puts(m, "#\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04003786 test_ftrace_alive(m);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003787 }
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003788 if (iter->snapshot && trace_empty(iter))
3789 print_snapshot_help(m, iter);
3790 else if (iter->trace && iter->trace->print_header)
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003791 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02003792 else
3793 trace_default_header(m);
3794
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003795 } else if (iter->leftover) {
3796 /*
3797 * If we filled the seq_file buffer earlier, we
3798 * want to just show it now.
3799 */
3800 ret = trace_print_seq(m, &iter->seq);
3801
3802 /* ret should this time be zero, but you never know */
3803 iter->leftover = ret;
3804
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003805 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003806 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003807 ret = trace_print_seq(m, &iter->seq);
3808 /*
3809 * If we overflow the seq_file buffer, then it will
3810 * ask us for this data again at start up.
3811 * Use that instead.
3812 * ret is 0 if seq_file write succeeded.
3813 * -1 otherwise.
3814 */
3815 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003816 }
3817
3818 return 0;
3819}
3820
Oleg Nesterov649e9c702013-07-23 17:25:54 +02003821/*
3822 * Should be used after trace_array_get(), trace_types_lock
3823 * ensures that i_cdev was already initialized.
3824 */
3825static inline int tracing_get_cpu(struct inode *inode)
3826{
3827 if (inode->i_cdev) /* See trace_create_cpu_file() */
3828 return (long)inode->i_cdev - 1;
3829 return RING_BUFFER_ALL_CPUS;
3830}
3831
James Morris88e9d342009-09-22 16:43:43 -07003832static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003833 .start = s_start,
3834 .next = s_next,
3835 .stop = s_stop,
3836 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003837};
3838
Ingo Molnare309b412008-05-12 21:20:51 +02003839static struct trace_iterator *
Oleg Nesterov6484c712013-07-23 17:26:10 +02003840__tracing_open(struct inode *inode, struct file *file, bool snapshot)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003841{
Oleg Nesterov6484c712013-07-23 17:26:10 +02003842 struct trace_array *tr = inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003843 struct trace_iterator *iter;
Jiri Olsa50e18b92012-04-25 10:23:39 +02003844 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003845
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003846 if (tracing_disabled)
3847 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02003848
Jiri Olsa50e18b92012-04-25 10:23:39 +02003849 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003850 if (!iter)
3851 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003852
Gil Fruchter72917232015-06-09 10:32:35 +03003853 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
Steven Rostedt6d158a82012-06-27 20:46:14 -04003854 GFP_KERNEL);
Dan Carpenter93574fc2012-07-11 09:35:08 +03003855 if (!iter->buffer_iter)
3856 goto release;
3857
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003858 /*
3859 * We make a copy of the current tracer to avoid concurrent
3860 * changes on it while we are reading.
3861 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003862 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003863 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003864 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003865 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003866
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003867 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003868
Li Zefan79f55992009-06-15 14:58:26 +08003869 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003870 goto fail;
3871
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003872 iter->tr = tr;
3873
3874#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003875 /* Currently only the top directory has a snapshot */
3876 if (tr->current_trace->print_max || snapshot)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003877 iter->trace_buffer = &tr->max_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003878 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003879#endif
3880 iter->trace_buffer = &tr->trace_buffer;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003881 iter->snapshot = snapshot;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003882 iter->pos = -1;
Oleg Nesterov6484c712013-07-23 17:26:10 +02003883 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003884 mutex_init(&iter->mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003885
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003886 /* Notify the tracer early; before we stop tracing. */
3887 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01003888 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003889
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003890 /* Annotate start of buffers if we had overruns */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003891 if (ring_buffer_overruns(iter->trace_buffer->buffer))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003892 iter->iter_flags |= TRACE_FILE_ANNOTATE;
3893
David Sharp8be07092012-11-13 12:18:22 -08003894 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09003895 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08003896 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3897
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003898 /* stop the trace while dumping if we are not opening "snapshot" */
3899 if (!iter->snapshot)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003900 tracing_stop_tr(tr);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003901
Steven Rostedtae3b5092013-01-23 15:22:59 -05003902 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003903 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003904 iter->buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003905 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
David Miller72c9ddf2010-04-20 15:47:11 -07003906 }
3907 ring_buffer_read_prepare_sync();
3908 for_each_tracing_cpu(cpu) {
3909 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003910 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003911 }
3912 } else {
3913 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003914 iter->buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003915 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
David Miller72c9ddf2010-04-20 15:47:11 -07003916 ring_buffer_read_prepare_sync();
3917 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003918 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003919 }
3920
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003921 mutex_unlock(&trace_types_lock);
3922
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003923 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003924
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003925 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003926 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003927 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04003928 kfree(iter->buffer_iter);
Dan Carpenter93574fc2012-07-11 09:35:08 +03003929release:
Jiri Olsa50e18b92012-04-25 10:23:39 +02003930 seq_release_private(inode, file);
3931 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003932}
3933
3934int tracing_open_generic(struct inode *inode, struct file *filp)
3935{
Steven Rostedt60a11772008-05-12 21:20:44 +02003936 if (tracing_disabled)
3937 return -ENODEV;
3938
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003939 filp->private_data = inode->i_private;
3940 return 0;
3941}
3942
Geyslan G. Bem2e864212013-10-18 21:15:54 -03003943bool tracing_is_disabled(void)
3944{
3945 return (tracing_disabled) ? true: false;
3946}
3947
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003948/*
3949 * Open and update trace_array ref count.
3950 * Must have the current trace_array passed to it.
3951 */
Steven Rostedt (Red Hat)dcc30222013-07-02 20:30:52 -04003952static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003953{
3954 struct trace_array *tr = inode->i_private;
3955
3956 if (tracing_disabled)
3957 return -ENODEV;
3958
3959 if (trace_array_get(tr) < 0)
3960 return -ENODEV;
3961
3962 filp->private_data = inode->i_private;
3963
3964 return 0;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003965}
3966
Hannes Eder4fd27352009-02-10 19:44:12 +01003967static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003968{
Oleg Nesterov6484c712013-07-23 17:26:10 +02003969 struct trace_array *tr = inode->i_private;
matt mooney907f2782010-09-27 19:04:53 -07003970 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003971 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003972 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003973
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003974 if (!(file->f_mode & FMODE_READ)) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02003975 trace_array_put(tr);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003976 return 0;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003977 }
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003978
Oleg Nesterov6484c712013-07-23 17:26:10 +02003979 /* Writes do not use seq_file */
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003980 iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003981 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05003982
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003983 for_each_tracing_cpu(cpu) {
3984 if (iter->buffer_iter[cpu])
3985 ring_buffer_read_finish(iter->buffer_iter[cpu]);
3986 }
3987
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003988 if (iter->trace && iter->trace->close)
3989 iter->trace->close(iter);
3990
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003991 if (!iter->snapshot)
3992 /* reenable tracing if it was previously enabled */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003993 tracing_start_tr(tr);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07003994
3995 __trace_array_put(tr);
3996
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003997 mutex_unlock(&trace_types_lock);
3998
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003999 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02004000 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004001 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04004002 kfree(iter->buffer_iter);
Jiri Olsa50e18b92012-04-25 10:23:39 +02004003 seq_release_private(inode, file);
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004004
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004005 return 0;
4006}
4007
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004008static int tracing_release_generic_tr(struct inode *inode, struct file *file)
4009{
4010 struct trace_array *tr = inode->i_private;
4011
4012 trace_array_put(tr);
4013 return 0;
4014}
4015
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004016static int tracing_single_release_tr(struct inode *inode, struct file *file)
4017{
4018 struct trace_array *tr = inode->i_private;
4019
4020 trace_array_put(tr);
4021
4022 return single_release(inode, file);
4023}
4024
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004025static int tracing_open(struct inode *inode, struct file *file)
4026{
Oleg Nesterov6484c712013-07-23 17:26:10 +02004027 struct trace_array *tr = inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004028 struct trace_iterator *iter;
4029 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004030
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004031 if (trace_array_get(tr) < 0)
4032 return -ENODEV;
4033
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004034 /* If this file was open for write, then erase contents */
Oleg Nesterov6484c712013-07-23 17:26:10 +02004035 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
4036 int cpu = tracing_get_cpu(inode);
Bo Yan8dd33bc2017-09-18 10:03:35 -07004037 struct trace_buffer *trace_buf = &tr->trace_buffer;
4038
4039#ifdef CONFIG_TRACER_MAX_TRACE
4040 if (tr->current_trace->print_max)
4041 trace_buf = &tr->max_buffer;
4042#endif
Oleg Nesterov6484c712013-07-23 17:26:10 +02004043
4044 if (cpu == RING_BUFFER_ALL_CPUS)
Bo Yan8dd33bc2017-09-18 10:03:35 -07004045 tracing_reset_online_cpus(trace_buf);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004046 else
Bo Yan8dd33bc2017-09-18 10:03:35 -07004047 tracing_reset(trace_buf, cpu);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004048 }
4049
4050 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02004051 iter = __tracing_open(inode, file, false);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004052 if (IS_ERR(iter))
4053 ret = PTR_ERR(iter);
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004054 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004055 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4056 }
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004057
4058 if (ret < 0)
4059 trace_array_put(tr);
4060
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004061 return ret;
4062}
4063
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004064/*
4065 * Some tracers are not suitable for instance buffers.
4066 * A tracer is always available for the global array (toplevel)
4067 * or if it explicitly states that it is.
4068 */
4069static bool
4070trace_ok_for_array(struct tracer *t, struct trace_array *tr)
4071{
4072 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
4073}
4074
4075/* Find the next tracer that this trace array may use */
4076static struct tracer *
4077get_tracer_for_array(struct trace_array *tr, struct tracer *t)
4078{
4079 while (t && !trace_ok_for_array(t, tr))
4080 t = t->next;
4081
4082 return t;
4083}
4084
Ingo Molnare309b412008-05-12 21:20:51 +02004085static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004086t_next(struct seq_file *m, void *v, loff_t *pos)
4087{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004088 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08004089 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004090
4091 (*pos)++;
4092
4093 if (t)
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004094 t = get_tracer_for_array(tr, t->next);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004095
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004096 return t;
4097}
4098
4099static void *t_start(struct seq_file *m, loff_t *pos)
4100{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004101 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08004102 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004103 loff_t l = 0;
4104
4105 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004106
4107 t = get_tracer_for_array(tr, trace_types);
4108 for (; t && l < *pos; t = t_next(m, t, &l))
4109 ;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004110
4111 return t;
4112}
4113
4114static void t_stop(struct seq_file *m, void *p)
4115{
4116 mutex_unlock(&trace_types_lock);
4117}
4118
4119static int t_show(struct seq_file *m, void *v)
4120{
4121 struct tracer *t = v;
4122
4123 if (!t)
4124 return 0;
4125
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004126 seq_puts(m, t->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004127 if (t->next)
4128 seq_putc(m, ' ');
4129 else
4130 seq_putc(m, '\n');
4131
4132 return 0;
4133}
4134
James Morris88e9d342009-09-22 16:43:43 -07004135static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004136 .start = t_start,
4137 .next = t_next,
4138 .stop = t_stop,
4139 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004140};
4141
4142static int show_traces_open(struct inode *inode, struct file *file)
4143{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004144 struct trace_array *tr = inode->i_private;
4145 struct seq_file *m;
4146 int ret;
4147
Steven Rostedt60a11772008-05-12 21:20:44 +02004148 if (tracing_disabled)
4149 return -ENODEV;
4150
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004151 ret = seq_open(file, &show_traces_seq_ops);
4152 if (ret)
4153 return ret;
4154
4155 m = file->private_data;
4156 m->private = tr;
4157
4158 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004159}
4160
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004161static ssize_t
4162tracing_write_stub(struct file *filp, const char __user *ubuf,
4163 size_t count, loff_t *ppos)
4164{
4165 return count;
4166}
4167
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004168loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
Slava Pestov364829b2010-11-24 15:13:16 -08004169{
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004170 int ret;
4171
Slava Pestov364829b2010-11-24 15:13:16 -08004172 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004173 ret = seq_lseek(file, offset, whence);
Slava Pestov364829b2010-11-24 15:13:16 -08004174 else
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004175 file->f_pos = ret = 0;
4176
4177 return ret;
Slava Pestov364829b2010-11-24 15:13:16 -08004178}
4179
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004180static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004181 .open = tracing_open,
4182 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004183 .write = tracing_write_stub,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004184 .llseek = tracing_lseek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004185 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004186};
4187
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004188static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02004189 .open = show_traces_open,
4190 .read = seq_read,
4191 .release = seq_release,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004192 .llseek = seq_lseek,
Ingo Molnarc7078de2008-05-12 21:20:52 +02004193};
4194
4195static ssize_t
4196tracing_cpumask_read(struct file *filp, char __user *ubuf,
4197 size_t count, loff_t *ppos)
4198{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004199 struct trace_array *tr = file_inode(filp)->i_private;
Changbin Du90e406f2017-11-30 11:39:43 +08004200 char *mask_str;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004201 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02004202
Changbin Du90e406f2017-11-30 11:39:43 +08004203 len = snprintf(NULL, 0, "%*pb\n",
4204 cpumask_pr_args(tr->tracing_cpumask)) + 1;
4205 mask_str = kmalloc(len, GFP_KERNEL);
4206 if (!mask_str)
4207 return -ENOMEM;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004208
Changbin Du90e406f2017-11-30 11:39:43 +08004209 len = snprintf(mask_str, len, "%*pb\n",
Tejun Heo1a402432015-02-13 14:37:39 -08004210 cpumask_pr_args(tr->tracing_cpumask));
4211 if (len >= count) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02004212 count = -EINVAL;
4213 goto out_err;
4214 }
Changbin Du90e406f2017-11-30 11:39:43 +08004215 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004216
4217out_err:
Changbin Du90e406f2017-11-30 11:39:43 +08004218 kfree(mask_str);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004219
4220 return count;
4221}
4222
4223static ssize_t
4224tracing_cpumask_write(struct file *filp, const char __user *ubuf,
4225 size_t count, loff_t *ppos)
4226{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004227 struct trace_array *tr = file_inode(filp)->i_private;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304228 cpumask_var_t tracing_cpumask_new;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004229 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304230
4231 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
4232 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02004233
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304234 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004235 if (err)
4236 goto err_unlock;
4237
Steven Rostedta5e25882008-12-02 15:34:05 -05004238 local_irq_disable();
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05004239 arch_spin_lock(&tr->max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02004240 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02004241 /*
4242 * Increase/decrease the disabled counter if we are
4243 * about to flip a bit in the cpumask:
4244 */
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004245 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304246 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004247 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4248 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004249 }
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004250 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304251 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004252 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4253 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004254 }
4255 }
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05004256 arch_spin_unlock(&tr->max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05004257 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02004258
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004259 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304260 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004261
Ingo Molnarc7078de2008-05-12 21:20:52 +02004262 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004263
4264err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08004265 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004266
4267 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02004268}
4269
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004270static const struct file_operations tracing_cpumask_fops = {
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004271 .open = tracing_open_generic_tr,
Ingo Molnarc7078de2008-05-12 21:20:52 +02004272 .read = tracing_cpumask_read,
4273 .write = tracing_cpumask_write,
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004274 .release = tracing_release_generic_tr,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004275 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004276};
4277
Li Zefanfdb372e2009-12-08 11:15:59 +08004278static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004279{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004280 struct tracer_opt *trace_opts;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004281 struct trace_array *tr = m->private;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004282 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004283 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004284
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004285 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004286 tracer_flags = tr->current_trace->flags->val;
4287 trace_opts = tr->current_trace->flags->opts;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004288
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004289 for (i = 0; trace_options[i]; i++) {
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004290 if (tr->trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08004291 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004292 else
Li Zefanfdb372e2009-12-08 11:15:59 +08004293 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004294 }
4295
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004296 for (i = 0; trace_opts[i].name; i++) {
4297 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08004298 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004299 else
Li Zefanfdb372e2009-12-08 11:15:59 +08004300 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004301 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004302 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004303
Li Zefanfdb372e2009-12-08 11:15:59 +08004304 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004305}
4306
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004307static int __set_tracer_option(struct trace_array *tr,
Li Zefan8d18eaa2009-12-08 11:17:06 +08004308 struct tracer_flags *tracer_flags,
4309 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004310{
Chunyu Hud39cdd22016-03-08 21:37:01 +08004311 struct tracer *trace = tracer_flags->trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004312 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004313
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004314 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004315 if (ret)
4316 return ret;
4317
4318 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08004319 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004320 else
Zhaolei77708412009-08-07 18:53:21 +08004321 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004322 return 0;
4323}
4324
Li Zefan8d18eaa2009-12-08 11:17:06 +08004325/* Try to assign a tracer specific option */
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004326static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
Li Zefan8d18eaa2009-12-08 11:17:06 +08004327{
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004328 struct tracer *trace = tr->current_trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004329 struct tracer_flags *tracer_flags = trace->flags;
4330 struct tracer_opt *opts = NULL;
4331 int i;
4332
4333 for (i = 0; tracer_flags->opts[i].name; i++) {
4334 opts = &tracer_flags->opts[i];
4335
4336 if (strcmp(cmp, opts->name) == 0)
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004337 return __set_tracer_option(tr, trace->flags, opts, neg);
Li Zefan8d18eaa2009-12-08 11:17:06 +08004338 }
4339
4340 return -EINVAL;
4341}
4342
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004343/* Some tracers require overwrite to stay enabled */
4344int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
4345{
4346 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
4347 return -1;
4348
4349 return 0;
4350}
4351
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004352int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004353{
4354 /* do nothing if flag is already set */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004355 if (!!(tr->trace_flags & mask) == !!enabled)
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004356 return 0;
4357
4358 /* Give the tracer a chance to approve the change */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004359 if (tr->current_trace->flag_changed)
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -05004360 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004361 return -EINVAL;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004362
4363 if (enabled)
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004364 tr->trace_flags |= mask;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004365 else
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004366 tr->trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08004367
4368 if (mask == TRACE_ITER_RECORD_CMD)
4369 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08004370
Joel Fernandesd914ba32017-06-26 19:01:55 -07004371 if (mask == TRACE_ITER_RECORD_TGID) {
4372 if (!tgid_map)
Kees Cook6396bb22018-06-12 14:03:40 -07004373 tgid_map = kcalloc(PID_MAX_DEFAULT + 1,
4374 sizeof(*tgid_map),
Joel Fernandesd914ba32017-06-26 19:01:55 -07004375 GFP_KERNEL);
4376 if (!tgid_map) {
4377 tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
4378 return -ENOMEM;
4379 }
4380
4381 trace_event_enable_tgid_record(enabled);
4382 }
4383
Steven Rostedtc37775d2016-04-13 16:59:18 -04004384 if (mask == TRACE_ITER_EVENT_FORK)
4385 trace_event_follow_fork(tr, enabled);
4386
Namhyung Kim1e104862017-04-17 11:44:28 +09004387 if (mask == TRACE_ITER_FUNC_FORK)
4388 ftrace_pid_follow_fork(tr, enabled);
4389
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004390 if (mask == TRACE_ITER_OVERWRITE) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004391 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004392#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004393 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004394#endif
4395 }
Steven Rostedt81698832012-10-11 10:15:05 -04004396
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04004397 if (mask == TRACE_ITER_PRINTK) {
Steven Rostedt81698832012-10-11 10:15:05 -04004398 trace_printk_start_stop_comm(enabled);
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04004399 trace_printk_control(enabled);
4400 }
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004401
4402 return 0;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004403}
4404
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004405static int trace_set_options(struct trace_array *tr, char *option)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004406{
Li Zefan8d18eaa2009-12-08 11:17:06 +08004407 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004408 int neg = 0;
Yisheng Xie591a0332018-05-17 16:36:03 +08004409 int ret;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004410 size_t orig_len = strlen(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004411
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004412 cmp = strstrip(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004413
Li Zefan8d18eaa2009-12-08 11:17:06 +08004414 if (strncmp(cmp, "no", 2) == 0) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004415 neg = 1;
4416 cmp += 2;
4417 }
4418
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04004419 mutex_lock(&trace_types_lock);
4420
Yisheng Xie591a0332018-05-17 16:36:03 +08004421 ret = match_string(trace_options, -1, cmp);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004422 /* If no option could be set, test the specific tracer options */
Yisheng Xie591a0332018-05-17 16:36:03 +08004423 if (ret < 0)
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004424 ret = set_tracer_option(tr, cmp, neg);
Yisheng Xie591a0332018-05-17 16:36:03 +08004425 else
4426 ret = set_tracer_flag(tr, 1 << ret, !neg);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04004427
4428 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004429
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004430 /*
4431 * If the first trailing whitespace is replaced with '\0' by strstrip,
4432 * turn it back into a space.
4433 */
4434 if (orig_len > strlen(option))
4435 option[strlen(option)] = ' ';
4436
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004437 return ret;
4438}
4439
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004440static void __init apply_trace_boot_options(void)
4441{
4442 char *buf = trace_boot_options_buf;
4443 char *option;
4444
4445 while (true) {
4446 option = strsep(&buf, ",");
4447
4448 if (!option)
4449 break;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004450
Steven Rostedt (Red Hat)43ed3842015-11-03 22:15:14 -05004451 if (*option)
4452 trace_set_options(&global_trace, option);
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004453
4454 /* Put back the comma to allow this to be called again */
4455 if (buf)
4456 *(buf - 1) = ',';
4457 }
4458}
4459
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004460static ssize_t
4461tracing_trace_options_write(struct file *filp, const char __user *ubuf,
4462 size_t cnt, loff_t *ppos)
4463{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004464 struct seq_file *m = filp->private_data;
4465 struct trace_array *tr = m->private;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004466 char buf[64];
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004467 int ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004468
4469 if (cnt >= sizeof(buf))
4470 return -EINVAL;
4471
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08004472 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004473 return -EFAULT;
4474
Steven Rostedta8dd2172013-01-09 20:54:17 -05004475 buf[cnt] = 0;
4476
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004477 ret = trace_set_options(tr, buf);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004478 if (ret < 0)
4479 return ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004480
Jiri Olsacf8517c2009-10-23 19:36:16 -04004481 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004482
4483 return cnt;
4484}
4485
Li Zefanfdb372e2009-12-08 11:15:59 +08004486static int tracing_trace_options_open(struct inode *inode, struct file *file)
4487{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004488 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004489 int ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004490
Li Zefanfdb372e2009-12-08 11:15:59 +08004491 if (tracing_disabled)
4492 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004493
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004494 if (trace_array_get(tr) < 0)
4495 return -ENODEV;
4496
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004497 ret = single_open(file, tracing_trace_options_show, inode->i_private);
4498 if (ret < 0)
4499 trace_array_put(tr);
4500
4501 return ret;
Li Zefanfdb372e2009-12-08 11:15:59 +08004502}
4503
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004504static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08004505 .open = tracing_trace_options_open,
4506 .read = seq_read,
4507 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004508 .release = tracing_single_release_tr,
Steven Rostedtee6bce52008-11-12 17:52:37 -05004509 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004510};
4511
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004512static const char readme_msg[] =
4513 "tracing mini-HOWTO:\n\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004514 "# echo 0 > tracing_on : quick way to disable tracing\n"
4515 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
4516 " Important files:\n"
4517 " trace\t\t\t- The static contents of the buffer\n"
4518 "\t\t\t To clear the buffer write into this file: echo > trace\n"
4519 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
4520 " current_tracer\t- function and latency tracers\n"
4521 " available_tracers\t- list of configured tracers for current_tracer\n"
4522 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
4523 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
4524 " trace_clock\t\t-change the clock used to order events\n"
4525 " local: Per cpu clock but may not be synced across CPUs\n"
4526 " global: Synced across CPUs but slows tracing down.\n"
4527 " counter: Not a clock, but just an increment\n"
4528 " uptime: Jiffy counter from time of boot\n"
4529 " perf: Same clock that perf events use\n"
4530#ifdef CONFIG_X86_64
4531 " x86-tsc: TSC cycle counter\n"
4532#endif
Tom Zanussi2c1ea602018-01-15 20:51:41 -06004533 "\n timestamp_mode\t-view the mode used to timestamp events\n"
4534 " delta: Delta difference against a buffer-wide timestamp\n"
4535 " absolute: Absolute (standalone) timestamp\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004536 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
Steven Rostedtfa32e852016-07-06 15:25:08 -04004537 "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004538 " tracing_cpumask\t- Limit which CPUs to trace\n"
4539 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
4540 "\t\t\t Remove sub-buffer with rmdir\n"
4541 " trace_options\t\t- Set format or modify how tracing happens\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004542 "\t\t\t Disable an option by adding a suffix 'no' to the\n"
4543 "\t\t\t option name\n"
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004544 " 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 -04004545#ifdef CONFIG_DYNAMIC_FTRACE
4546 "\n available_filter_functions - list of functions that can be filtered on\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004547 " set_ftrace_filter\t- echo function name in here to only trace these\n"
4548 "\t\t\t functions\n"
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09004549 "\t accepts: func_full_name or glob-matching-pattern\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004550 "\t modules: Can select a group via module\n"
4551 "\t Format: :mod:<module-name>\n"
4552 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
4553 "\t triggers: a command to perform when function is hit\n"
4554 "\t Format: <function>:<trigger>[:count]\n"
4555 "\t trigger: traceon, traceoff\n"
4556 "\t\t enable_event:<system>:<event>\n"
4557 "\t\t disable_event:<system>:<event>\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004558#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004559 "\t\t stacktrace\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004560#endif
4561#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004562 "\t\t snapshot\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004563#endif
Steven Rostedt (Red Hat)17a280e2014-04-10 22:43:37 -04004564 "\t\t dump\n"
4565 "\t\t cpudump\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004566 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
4567 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
4568 "\t The first one will disable tracing every time do_fault is hit\n"
4569 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
4570 "\t The first time do trap is hit and it disables tracing, the\n"
4571 "\t counter will decrement to 2. If tracing is already disabled,\n"
4572 "\t the counter will not decrement. It only decrements when the\n"
4573 "\t trigger did work\n"
4574 "\t To remove trigger without count:\n"
4575 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
4576 "\t To remove trigger with a count:\n"
4577 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004578 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004579 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
4580 "\t modules: Can select a group via module command :mod:\n"
4581 "\t Does not accept triggers\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004582#endif /* CONFIG_DYNAMIC_FTRACE */
4583#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004584 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
4585 "\t\t (function)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004586#endif
4587#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4588 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
Namhyung Kimd048a8c72014-06-13 01:23:53 +09004589 " 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 -04004590 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
4591#endif
4592#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004593 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
4594 "\t\t\t snapshot buffer. Read the contents for more\n"
4595 "\t\t\t information\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004596#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004597#ifdef CONFIG_STACK_TRACER
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004598 " stack_trace\t\t- Shows the max stack trace when active\n"
4599 " stack_max_size\t- Shows current max stack size that was traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004600 "\t\t\t Write into this file to reset the max size (trigger a\n"
4601 "\t\t\t new trace)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004602#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004603 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
4604 "\t\t\t traces\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004605#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004606#endif /* CONFIG_STACK_TRACER */
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004607#ifdef CONFIG_KPROBE_EVENTS
Masami Hiramatsu86425622016-08-18 17:58:15 +09004608 " kprobe_events\t\t- Add/remove/show the kernel dynamic events\n"
4609 "\t\t\t Write into this file to define/undefine new trace events.\n"
4610#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004611#ifdef CONFIG_UPROBE_EVENTS
Masami Hiramatsu86425622016-08-18 17:58:15 +09004612 " uprobe_events\t\t- Add/remove/show the userspace dynamic events\n"
4613 "\t\t\t Write into this file to define/undefine new trace events.\n"
4614#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004615#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
Masami Hiramatsu86425622016-08-18 17:58:15 +09004616 "\t accepts: event-definitions (one definition per line)\n"
Masami Hiramatsuc3ca46e2017-05-23 15:05:50 +09004617 "\t Format: p[:[<group>/]<event>] <place> [<args>]\n"
4618 "\t r[maxactive][:[<group>/]<event>] <place> [<args>]\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004619 "\t -:[<group>/]<event>\n"
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004620#ifdef CONFIG_KPROBE_EVENTS
Masami Hiramatsu86425622016-08-18 17:58:15 +09004621 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
Naveen N. Rao35b6f552017-02-22 19:23:39 +05304622 "place (kretprobe): [<module>:]<symbol>[+<offset>]|<memaddr>\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004623#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004624#ifdef CONFIG_UPROBE_EVENTS
Ravi Bangoria1cc33162018-08-20 10:12:47 +05304625 " place (uprobe): <path>:<offset>[(ref_ctr_offset)]\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004626#endif
4627 "\t args: <name>=fetcharg[:type]\n"
4628 "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
Masami Hiramatsua1303af2018-04-25 21:21:26 +09004629#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
4630 "\t $stack<index>, $stack, $retval, $comm, $arg<N>\n"
4631#else
Masami Hiramatsu86425622016-08-18 17:58:15 +09004632 "\t $stack<index>, $stack, $retval, $comm\n"
Masami Hiramatsua1303af2018-04-25 21:21:26 +09004633#endif
Masami Hiramatsu60c2e0c2018-04-25 21:20:28 +09004634 "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
Masami Hiramatsu40b53b72018-04-25 21:21:55 +09004635 "\t b<bit-width>@<bit-offset>/<container-size>,\n"
4636 "\t <type>\\[<array-size>\\]\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004637#endif
Tom Zanussi26f25562014-01-17 15:11:44 -06004638 " events/\t\t- Directory containing all trace event subsystems:\n"
4639 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
4640 " events/<system>/\t- Directory containing all trace events for <system>:\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004641 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
4642 "\t\t\t events\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004643 " filter\t\t- If set, only events passing filter are traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004644 " events/<system>/<event>/\t- Directory containing control files for\n"
4645 "\t\t\t <event>:\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004646 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
4647 " filter\t\t- If set, only events passing filter are traced\n"
4648 " trigger\t\t- If set, a command to perform when event is hit\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004649 "\t Format: <trigger>[:count][if <filter>]\n"
4650 "\t trigger: traceon, traceoff\n"
4651 "\t enable_event:<system>:<event>\n"
4652 "\t disable_event:<system>:<event>\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06004653#ifdef CONFIG_HIST_TRIGGERS
4654 "\t enable_hist:<system>:<event>\n"
4655 "\t disable_hist:<system>:<event>\n"
4656#endif
Tom Zanussi26f25562014-01-17 15:11:44 -06004657#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004658 "\t\t stacktrace\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004659#endif
4660#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004661 "\t\t snapshot\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004662#endif
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004663#ifdef CONFIG_HIST_TRIGGERS
4664 "\t\t hist (see below)\n"
4665#endif
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004666 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
4667 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
4668 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
4669 "\t events/block/block_unplug/trigger\n"
4670 "\t The first disables tracing every time block_unplug is hit.\n"
4671 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
4672 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
4673 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
4674 "\t Like function triggers, the counter is only decremented if it\n"
4675 "\t enabled or disabled tracing.\n"
4676 "\t To remove a trigger without a count:\n"
4677 "\t echo '!<trigger> > <system>/<event>/trigger\n"
4678 "\t To remove a trigger with a count:\n"
4679 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
4680 "\t Filters can be ignored when removing a trigger.\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004681#ifdef CONFIG_HIST_TRIGGERS
4682 " hist trigger\t- If set, event hits are aggregated into a hash table\n"
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004683 "\t Format: hist:keys=<field1[,field2,...]>\n"
Tom Zanussif2606832016-03-03 12:54:43 -06004684 "\t [:values=<field1[,field2,...]>]\n"
Tom Zanussie62347d2016-03-03 12:54:45 -06004685 "\t [:sort=<field1[,field2,...]>]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004686 "\t [:size=#entries]\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06004687 "\t [:pause][:continue][:clear]\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004688 "\t [:name=histname1]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004689 "\t [if <filter>]\n\n"
4690 "\t When a matching event is hit, an entry is added to a hash\n"
Tom Zanussif2606832016-03-03 12:54:43 -06004691 "\t table using the key(s) and value(s) named, and the value of a\n"
4692 "\t sum called 'hitcount' is incremented. Keys and values\n"
4693 "\t correspond to fields in the event's format description. Keys\n"
Tom Zanussi69a02002016-03-03 12:54:52 -06004694 "\t can be any field, or the special string 'stacktrace'.\n"
4695 "\t Compound keys consisting of up to two fields can be specified\n"
4696 "\t by the 'keys' keyword. Values must correspond to numeric\n"
4697 "\t fields. Sort keys consisting of up to two fields can be\n"
4698 "\t specified using the 'sort' keyword. The sort direction can\n"
4699 "\t be modified by appending '.descending' or '.ascending' to a\n"
4700 "\t sort field. The 'size' parameter can be used to specify more\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004701 "\t or fewer than the default 2048 entries for the hashtable size.\n"
4702 "\t If a hist trigger is given a name using the 'name' parameter,\n"
4703 "\t its histogram data will be shared with other triggers of the\n"
4704 "\t same name, and trigger hits will update this common data.\n\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004705 "\t Reading the 'hist' file for the event will dump the hash\n"
Tom Zanussi52a7f162016-03-03 12:54:57 -06004706 "\t table in its entirety to stdout. If there are multiple hist\n"
4707 "\t triggers attached to an event, there will be a table for each\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004708 "\t trigger in the output. The table displayed for a named\n"
4709 "\t trigger will be the same as any other instance having the\n"
4710 "\t same name. The default format used to display a given field\n"
4711 "\t can be modified by appending any of the following modifiers\n"
4712 "\t to the field name, as applicable:\n\n"
Tom Zanussic6afad42016-03-03 12:54:49 -06004713 "\t .hex display a number as a hex value\n"
4714 "\t .sym display an address as a symbol\n"
Tom Zanussi6b4827a2016-03-03 12:54:50 -06004715 "\t .sym-offset display an address as a symbol and offset\n"
Tom Zanussi31696192016-03-03 12:54:51 -06004716 "\t .execname display a common_pid as a program name\n"
Tom Zanussi860f9f62018-01-15 20:51:48 -06004717 "\t .syscall display a syscall id as a syscall name\n"
4718 "\t .log2 display log2 value rather than raw number\n"
4719 "\t .usecs display a common_timestamp in microseconds\n\n"
Tom Zanussi83e99912016-03-03 12:54:46 -06004720 "\t The 'pause' parameter can be used to pause an existing hist\n"
4721 "\t trigger or to start a hist trigger but not log any events\n"
4722 "\t until told to do so. 'continue' can be used to start or\n"
4723 "\t restart a paused hist trigger.\n\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06004724 "\t The 'clear' parameter will clear the contents of a running\n"
4725 "\t hist trigger and leave its current paused/active state\n"
4726 "\t unchanged.\n\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06004727 "\t The enable_hist and disable_hist triggers can be used to\n"
4728 "\t have one event conditionally start and stop another event's\n"
4729 "\t already-attached hist trigger. The syntax is analagous to\n"
4730 "\t the enable_event and disable_event triggers.\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004731#endif
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004732;
4733
4734static ssize_t
4735tracing_readme_read(struct file *filp, char __user *ubuf,
4736 size_t cnt, loff_t *ppos)
4737{
4738 return simple_read_from_buffer(ubuf, cnt, ppos,
4739 readme_msg, strlen(readme_msg));
4740}
4741
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004742static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02004743 .open = tracing_open_generic,
4744 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004745 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004746};
4747
Michael Sartain99c621d2017-07-05 22:07:15 -06004748static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos)
4749{
4750 int *ptr = v;
4751
4752 if (*pos || m->count)
4753 ptr++;
4754
4755 (*pos)++;
4756
4757 for (; ptr <= &tgid_map[PID_MAX_DEFAULT]; ptr++) {
4758 if (trace_find_tgid(*ptr))
4759 return ptr;
4760 }
4761
4762 return NULL;
4763}
4764
4765static void *saved_tgids_start(struct seq_file *m, loff_t *pos)
4766{
4767 void *v;
4768 loff_t l = 0;
4769
4770 if (!tgid_map)
4771 return NULL;
4772
4773 v = &tgid_map[0];
4774 while (l <= *pos) {
4775 v = saved_tgids_next(m, v, &l);
4776 if (!v)
4777 return NULL;
4778 }
4779
4780 return v;
4781}
4782
4783static void saved_tgids_stop(struct seq_file *m, void *v)
4784{
4785}
4786
4787static int saved_tgids_show(struct seq_file *m, void *v)
4788{
4789 int pid = (int *)v - tgid_map;
4790
4791 seq_printf(m, "%d %d\n", pid, trace_find_tgid(pid));
4792 return 0;
4793}
4794
4795static const struct seq_operations tracing_saved_tgids_seq_ops = {
4796 .start = saved_tgids_start,
4797 .stop = saved_tgids_stop,
4798 .next = saved_tgids_next,
4799 .show = saved_tgids_show,
4800};
4801
4802static int tracing_saved_tgids_open(struct inode *inode, struct file *filp)
4803{
4804 if (tracing_disabled)
4805 return -ENODEV;
4806
4807 return seq_open(filp, &tracing_saved_tgids_seq_ops);
4808}
4809
4810
4811static const struct file_operations tracing_saved_tgids_fops = {
4812 .open = tracing_saved_tgids_open,
4813 .read = seq_read,
4814 .llseek = seq_lseek,
4815 .release = seq_release,
4816};
4817
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004818static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
Avadh Patel69abe6a2009-04-10 16:04:48 -04004819{
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004820 unsigned int *ptr = v;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004821
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004822 if (*pos || m->count)
4823 ptr++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004824
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004825 (*pos)++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004826
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004827 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
4828 ptr++) {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004829 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
Avadh Patel69abe6a2009-04-10 16:04:48 -04004830 continue;
4831
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004832 return ptr;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004833 }
4834
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004835 return NULL;
4836}
Avadh Patel69abe6a2009-04-10 16:04:48 -04004837
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004838static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
4839{
4840 void *v;
4841 loff_t l = 0;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004842
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04004843 preempt_disable();
4844 arch_spin_lock(&trace_cmdline_lock);
4845
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004846 v = &savedcmd->map_cmdline_to_pid[0];
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004847 while (l <= *pos) {
4848 v = saved_cmdlines_next(m, v, &l);
4849 if (!v)
4850 return NULL;
4851 }
4852
4853 return v;
4854}
4855
4856static void saved_cmdlines_stop(struct seq_file *m, void *v)
4857{
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04004858 arch_spin_unlock(&trace_cmdline_lock);
4859 preempt_enable();
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004860}
4861
4862static int saved_cmdlines_show(struct seq_file *m, void *v)
4863{
4864 char buf[TASK_COMM_LEN];
4865 unsigned int *pid = v;
4866
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04004867 __trace_find_cmdline(*pid, buf);
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004868 seq_printf(m, "%d %s\n", *pid, buf);
4869 return 0;
4870}
4871
4872static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
4873 .start = saved_cmdlines_start,
4874 .next = saved_cmdlines_next,
4875 .stop = saved_cmdlines_stop,
4876 .show = saved_cmdlines_show,
4877};
4878
4879static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
4880{
4881 if (tracing_disabled)
4882 return -ENODEV;
4883
4884 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
Avadh Patel69abe6a2009-04-10 16:04:48 -04004885}
4886
4887static const struct file_operations tracing_saved_cmdlines_fops = {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004888 .open = tracing_saved_cmdlines_open,
4889 .read = seq_read,
4890 .llseek = seq_lseek,
4891 .release = seq_release,
Avadh Patel69abe6a2009-04-10 16:04:48 -04004892};
4893
4894static ssize_t
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004895tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
4896 size_t cnt, loff_t *ppos)
4897{
4898 char buf[64];
4899 int r;
4900
4901 arch_spin_lock(&trace_cmdline_lock);
Namhyung Kima6af8fb2014-06-10 16:11:35 +09004902 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004903 arch_spin_unlock(&trace_cmdline_lock);
4904
4905 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4906}
4907
4908static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
4909{
4910 kfree(s->saved_cmdlines);
4911 kfree(s->map_cmdline_to_pid);
4912 kfree(s);
4913}
4914
4915static int tracing_resize_saved_cmdlines(unsigned int val)
4916{
4917 struct saved_cmdlines_buffer *s, *savedcmd_temp;
4918
Namhyung Kima6af8fb2014-06-10 16:11:35 +09004919 s = kmalloc(sizeof(*s), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004920 if (!s)
4921 return -ENOMEM;
4922
4923 if (allocate_cmdlines_buffer(val, s) < 0) {
4924 kfree(s);
4925 return -ENOMEM;
4926 }
4927
4928 arch_spin_lock(&trace_cmdline_lock);
4929 savedcmd_temp = savedcmd;
4930 savedcmd = s;
4931 arch_spin_unlock(&trace_cmdline_lock);
4932 free_saved_cmdlines_buffer(savedcmd_temp);
4933
4934 return 0;
4935}
4936
4937static ssize_t
4938tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
4939 size_t cnt, loff_t *ppos)
4940{
4941 unsigned long val;
4942 int ret;
4943
4944 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4945 if (ret)
4946 return ret;
4947
4948 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
4949 if (!val || val > PID_MAX_DEFAULT)
4950 return -EINVAL;
4951
4952 ret = tracing_resize_saved_cmdlines((unsigned int)val);
4953 if (ret < 0)
4954 return ret;
4955
4956 *ppos += cnt;
4957
4958 return cnt;
4959}
4960
4961static const struct file_operations tracing_saved_cmdlines_size_fops = {
4962 .open = tracing_open_generic,
4963 .read = tracing_saved_cmdlines_size_read,
4964 .write = tracing_saved_cmdlines_size_write,
4965};
4966
Jeremy Linton681bec02017-05-31 16:56:53 -05004967#ifdef CONFIG_TRACE_EVAL_MAP_FILE
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05004968static union trace_eval_map_item *
Jeremy Lintonf57a4142017-05-31 16:56:48 -05004969update_eval_map(union trace_eval_map_item *ptr)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004970{
Jeremy Linton00f4b652017-05-31 16:56:43 -05004971 if (!ptr->map.eval_string) {
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004972 if (ptr->tail.next) {
4973 ptr = ptr->tail.next;
4974 /* Set ptr to the next real item (skip head) */
4975 ptr++;
4976 } else
4977 return NULL;
4978 }
4979 return ptr;
4980}
4981
Jeremy Lintonf57a4142017-05-31 16:56:48 -05004982static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004983{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05004984 union trace_eval_map_item *ptr = v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004985
4986 /*
4987 * Paranoid! If ptr points to end, we don't want to increment past it.
4988 * This really should never happen.
4989 */
Jeremy Lintonf57a4142017-05-31 16:56:48 -05004990 ptr = update_eval_map(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004991 if (WARN_ON_ONCE(!ptr))
4992 return NULL;
4993
4994 ptr++;
4995
4996 (*pos)++;
4997
Jeremy Lintonf57a4142017-05-31 16:56:48 -05004998 ptr = update_eval_map(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004999
5000 return ptr;
5001}
5002
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005003static void *eval_map_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005004{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005005 union trace_eval_map_item *v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005006 loff_t l = 0;
5007
Jeremy Linton1793ed92017-05-31 16:56:46 -05005008 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005009
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005010 v = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005011 if (v)
5012 v++;
5013
5014 while (v && l < *pos) {
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005015 v = eval_map_next(m, v, &l);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005016 }
5017
5018 return v;
5019}
5020
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005021static void eval_map_stop(struct seq_file *m, void *v)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005022{
Jeremy Linton1793ed92017-05-31 16:56:46 -05005023 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005024}
5025
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005026static int eval_map_show(struct seq_file *m, void *v)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005027{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005028 union trace_eval_map_item *ptr = v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005029
5030 seq_printf(m, "%s %ld (%s)\n",
Jeremy Linton00f4b652017-05-31 16:56:43 -05005031 ptr->map.eval_string, ptr->map.eval_value,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005032 ptr->map.system);
5033
5034 return 0;
5035}
5036
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005037static const struct seq_operations tracing_eval_map_seq_ops = {
5038 .start = eval_map_start,
5039 .next = eval_map_next,
5040 .stop = eval_map_stop,
5041 .show = eval_map_show,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005042};
5043
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005044static int tracing_eval_map_open(struct inode *inode, struct file *filp)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005045{
5046 if (tracing_disabled)
5047 return -ENODEV;
5048
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005049 return seq_open(filp, &tracing_eval_map_seq_ops);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005050}
5051
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005052static const struct file_operations tracing_eval_map_fops = {
5053 .open = tracing_eval_map_open,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005054 .read = seq_read,
5055 .llseek = seq_lseek,
5056 .release = seq_release,
5057};
5058
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005059static inline union trace_eval_map_item *
Jeremy Linton5f60b352017-05-31 16:56:47 -05005060trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005061{
5062 /* Return tail of array given the head */
5063 return ptr + ptr->head.length + 1;
5064}
5065
5066static void
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005067trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005068 int len)
5069{
Jeremy Linton00f4b652017-05-31 16:56:43 -05005070 struct trace_eval_map **stop;
5071 struct trace_eval_map **map;
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005072 union trace_eval_map_item *map_array;
5073 union trace_eval_map_item *ptr;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005074
5075 stop = start + len;
5076
5077 /*
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005078 * The trace_eval_maps contains the map plus a head and tail item,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005079 * where the head holds the module and length of array, and the
5080 * tail holds a pointer to the next list.
5081 */
Kees Cook6da2ec52018-06-12 13:55:00 -07005082 map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005083 if (!map_array) {
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005084 pr_warn("Unable to allocate trace eval mapping\n");
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005085 return;
5086 }
5087
Jeremy Linton1793ed92017-05-31 16:56:46 -05005088 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005089
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005090 if (!trace_eval_maps)
5091 trace_eval_maps = map_array;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005092 else {
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005093 ptr = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005094 for (;;) {
Jeremy Linton5f60b352017-05-31 16:56:47 -05005095 ptr = trace_eval_jmp_to_tail(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005096 if (!ptr->tail.next)
5097 break;
5098 ptr = ptr->tail.next;
5099
5100 }
5101 ptr->tail.next = map_array;
5102 }
5103 map_array->head.mod = mod;
5104 map_array->head.length = len;
5105 map_array++;
5106
5107 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
5108 map_array->map = **map;
5109 map_array++;
5110 }
5111 memset(map_array, 0, sizeof(*map_array));
5112
Jeremy Linton1793ed92017-05-31 16:56:46 -05005113 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005114}
5115
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005116static void trace_create_eval_file(struct dentry *d_tracer)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005117{
Jeremy Linton681bec02017-05-31 16:56:53 -05005118 trace_create_file("eval_map", 0444, d_tracer,
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005119 NULL, &tracing_eval_map_fops);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005120}
5121
Jeremy Linton681bec02017-05-31 16:56:53 -05005122#else /* CONFIG_TRACE_EVAL_MAP_FILE */
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005123static inline void trace_create_eval_file(struct dentry *d_tracer) { }
5124static inline void trace_insert_eval_map_file(struct module *mod,
Jeremy Linton00f4b652017-05-31 16:56:43 -05005125 struct trace_eval_map **start, int len) { }
Jeremy Linton681bec02017-05-31 16:56:53 -05005126#endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005127
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005128static void trace_insert_eval_map(struct module *mod,
Jeremy Linton00f4b652017-05-31 16:56:43 -05005129 struct trace_eval_map **start, int len)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005130{
Jeremy Linton00f4b652017-05-31 16:56:43 -05005131 struct trace_eval_map **map;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005132
5133 if (len <= 0)
5134 return;
5135
5136 map = start;
5137
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005138 trace_event_eval_update(map, len);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005139
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005140 trace_insert_eval_map_file(mod, start, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005141}
5142
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005143static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005144tracing_set_trace_read(struct file *filp, char __user *ubuf,
5145 size_t cnt, loff_t *ppos)
5146{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005147 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08005148 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005149 int r;
5150
5151 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005152 r = sprintf(buf, "%s\n", tr->current_trace->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005153 mutex_unlock(&trace_types_lock);
5154
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005155 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005156}
5157
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005158int tracer_init(struct tracer *t, struct trace_array *tr)
5159{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005160 tracing_reset_online_cpus(&tr->trace_buffer);
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005161 return t->init(tr);
5162}
5163
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005164static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005165{
5166 int cpu;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005167
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005168 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005169 per_cpu_ptr(buf->data, cpu)->entries = val;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005170}
5171
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005172#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookad60da502012-10-17 11:56:16 +09005173/* resize @tr's buffer to the size of @size_tr's entries */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005174static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
5175 struct trace_buffer *size_buf, int cpu_id)
Hiraku Toyookad60da502012-10-17 11:56:16 +09005176{
5177 int cpu, ret = 0;
5178
5179 if (cpu_id == RING_BUFFER_ALL_CPUS) {
5180 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005181 ret = ring_buffer_resize(trace_buf->buffer,
5182 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005183 if (ret < 0)
5184 break;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005185 per_cpu_ptr(trace_buf->data, cpu)->entries =
5186 per_cpu_ptr(size_buf->data, cpu)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09005187 }
5188 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005189 ret = ring_buffer_resize(trace_buf->buffer,
5190 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005191 if (ret == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005192 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
5193 per_cpu_ptr(size_buf->data, cpu_id)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09005194 }
5195
5196 return ret;
5197}
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005198#endif /* CONFIG_TRACER_MAX_TRACE */
Hiraku Toyookad60da502012-10-17 11:56:16 +09005199
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005200static int __tracing_resize_ring_buffer(struct trace_array *tr,
5201 unsigned long size, int cpu)
Steven Rostedt73c51622009-03-11 13:42:01 -04005202{
5203 int ret;
5204
5205 /*
5206 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04005207 * we use the size that was given, and we can forget about
5208 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04005209 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005210 ring_buffer_expanded = true;
Steven Rostedt73c51622009-03-11 13:42:01 -04005211
Steven Rostedtb382ede62012-10-10 21:44:34 -04005212 /* May be called before buffers are initialized */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005213 if (!tr->trace_buffer.buffer)
Steven Rostedtb382ede62012-10-10 21:44:34 -04005214 return 0;
5215
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005216 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005217 if (ret < 0)
5218 return ret;
5219
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005220#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005221 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
5222 !tr->current_trace->use_max_tr)
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005223 goto out;
5224
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005225 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005226 if (ret < 0) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005227 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
5228 &tr->trace_buffer, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005229 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04005230 /*
5231 * AARGH! We are left with different
5232 * size max buffer!!!!
5233 * The max buffer is our "snapshot" buffer.
5234 * When a tracer needs a snapshot (one of the
5235 * latency tracers), it swaps the max buffer
5236 * with the saved snap shot. We succeeded to
5237 * update the size of the main buffer, but failed to
5238 * update the size of the max buffer. But when we tried
5239 * to reset the main buffer to the original size, we
5240 * failed there too. This is very unlikely to
5241 * happen, but if it does, warn and kill all
5242 * tracing.
5243 */
Steven Rostedt73c51622009-03-11 13:42:01 -04005244 WARN_ON(1);
5245 tracing_disabled = 1;
5246 }
5247 return ret;
5248 }
5249
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005250 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005251 set_buffer_entries(&tr->max_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005252 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005253 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005254
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005255 out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005256#endif /* CONFIG_TRACER_MAX_TRACE */
5257
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005258 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005259 set_buffer_entries(&tr->trace_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005260 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005261 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
Steven Rostedt73c51622009-03-11 13:42:01 -04005262
5263 return ret;
5264}
5265
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005266static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
5267 unsigned long size, int cpu_id)
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005268{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07005269 int ret = size;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005270
5271 mutex_lock(&trace_types_lock);
5272
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005273 if (cpu_id != RING_BUFFER_ALL_CPUS) {
5274 /* make sure, this cpu is enabled in the mask */
5275 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
5276 ret = -EINVAL;
5277 goto out;
5278 }
5279 }
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005280
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005281 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005282 if (ret < 0)
5283 ret = -ENOMEM;
5284
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005285out:
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005286 mutex_unlock(&trace_types_lock);
5287
5288 return ret;
5289}
5290
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005291
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005292/**
5293 * tracing_update_buffers - used by tracing facility to expand ring buffers
5294 *
5295 * To save on memory when the tracing is never used on a system with it
5296 * configured in. The ring buffers are set to a minimum size. But once
5297 * a user starts to use the tracing facility, then they need to grow
5298 * to their default size.
5299 *
5300 * This function is to be called when a tracer is about to be used.
5301 */
5302int tracing_update_buffers(void)
5303{
5304 int ret = 0;
5305
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005306 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005307 if (!ring_buffer_expanded)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005308 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005309 RING_BUFFER_ALL_CPUS);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005310 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005311
5312 return ret;
5313}
5314
Steven Rostedt577b7852009-02-26 23:43:05 -05005315struct trace_option_dentry;
5316
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04005317static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005318create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
Steven Rostedt577b7852009-02-26 23:43:05 -05005319
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05005320/*
5321 * Used to clear out the tracer before deletion of an instance.
5322 * Must have trace_types_lock held.
5323 */
5324static void tracing_set_nop(struct trace_array *tr)
5325{
5326 if (tr->current_trace == &nop_trace)
5327 return;
5328
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005329 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05005330
5331 if (tr->current_trace->reset)
5332 tr->current_trace->reset(tr);
5333
5334 tr->current_trace = &nop_trace;
5335}
5336
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04005337static void add_tracer_options(struct trace_array *tr, struct tracer *t)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005338{
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05005339 /* Only enable if the directory has been created already. */
5340 if (!tr->dir)
5341 return;
5342
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04005343 create_trace_option_files(tr, t);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05005344}
5345
5346static int tracing_set_tracer(struct trace_array *tr, const char *buf)
5347{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005348 struct tracer *t;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005349#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05005350 bool had_max_tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005351#endif
Peter Zijlstrad9e54072008-11-01 19:57:37 +01005352 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005353
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005354 mutex_lock(&trace_types_lock);
5355
Steven Rostedt73c51622009-03-11 13:42:01 -04005356 if (!ring_buffer_expanded) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005357 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005358 RING_BUFFER_ALL_CPUS);
Steven Rostedt73c51622009-03-11 13:42:01 -04005359 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01005360 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04005361 ret = 0;
5362 }
5363
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005364 for (t = trace_types; t; t = t->next) {
5365 if (strcmp(t->name, buf) == 0)
5366 break;
5367 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02005368 if (!t) {
5369 ret = -EINVAL;
5370 goto out;
5371 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005372 if (t == tr->current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005373 goto out;
5374
Ziqian SUN (Zamir)c7b3ae02017-09-11 14:26:35 +08005375 /* Some tracers won't work on kernel command line */
5376 if (system_state < SYSTEM_RUNNING && t->noboot) {
5377 pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
5378 t->name);
5379 goto out;
5380 }
5381
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005382 /* Some tracers are only allowed for the top level buffer */
5383 if (!trace_ok_for_array(t, tr)) {
5384 ret = -EINVAL;
5385 goto out;
5386 }
5387
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005388 /* If trace pipe files are being read, we can't change the tracer */
5389 if (tr->current_trace->ref) {
5390 ret = -EBUSY;
5391 goto out;
5392 }
5393
Steven Rostedt9f029e82008-11-12 15:24:24 -05005394 trace_branch_disable();
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005395
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005396 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005397
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005398 if (tr->current_trace->reset)
5399 tr->current_trace->reset(tr);
Steven Rostedt34600f02013-01-22 13:35:11 -05005400
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005401 /* Current trace needs to be nop_trace before synchronize_sched */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005402 tr->current_trace = &nop_trace;
Steven Rostedt34600f02013-01-22 13:35:11 -05005403
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05005404#ifdef CONFIG_TRACER_MAX_TRACE
5405 had_max_tr = tr->allocated_snapshot;
Steven Rostedt34600f02013-01-22 13:35:11 -05005406
5407 if (had_max_tr && !t->use_max_tr) {
5408 /*
5409 * We need to make sure that the update_max_tr sees that
5410 * current_trace changed to nop_trace to keep it from
5411 * swapping the buffers after we resize it.
5412 * The update_max_tr is called from interrupts disabled
5413 * so a synchronized_sched() is sufficient.
5414 */
5415 synchronize_sched();
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04005416 free_snapshot(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005417 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005418#endif
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005419
5420#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05005421 if (t->use_max_tr && !had_max_tr) {
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04005422 ret = tracing_alloc_snapshot_instance(tr);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005423 if (ret < 0)
5424 goto out;
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005425 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005426#endif
Steven Rostedt577b7852009-02-26 23:43:05 -05005427
Frederic Weisbecker1c800252008-11-16 05:57:26 +01005428 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005429 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01005430 if (ret)
5431 goto out;
5432 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005433
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005434 tr->current_trace = t;
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005435 tr->current_trace->enabled++;
Steven Rostedt9f029e82008-11-12 15:24:24 -05005436 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005437 out:
5438 mutex_unlock(&trace_types_lock);
5439
Peter Zijlstrad9e54072008-11-01 19:57:37 +01005440 return ret;
5441}
5442
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005443static ssize_t
5444tracing_set_trace_write(struct file *filp, const char __user *ubuf,
5445 size_t cnt, loff_t *ppos)
5446{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005447 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08005448 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005449 int i;
5450 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01005451 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005452
Steven Rostedt60063a62008-10-28 10:44:24 -04005453 ret = cnt;
5454
Li Zefanee6c2c12009-09-18 14:06:47 +08005455 if (cnt > MAX_TRACER_SIZE)
5456 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005457
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08005458 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005459 return -EFAULT;
5460
5461 buf[cnt] = 0;
5462
5463 /* strip ending whitespace. */
5464 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
5465 buf[i] = 0;
5466
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005467 err = tracing_set_tracer(tr, buf);
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01005468 if (err)
5469 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005470
Jiri Olsacf8517c2009-10-23 19:36:16 -04005471 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005472
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02005473 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005474}
5475
5476static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005477tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
5478 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005479{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005480 char buf[64];
5481 int r;
5482
Steven Rostedtcffae432008-05-12 21:21:00 +02005483 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005484 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02005485 if (r > sizeof(buf))
5486 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005487 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005488}
5489
5490static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005491tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
5492 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005493{
Hannes Eder5e398412009-02-10 19:44:34 +01005494 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005495 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005496
Peter Huewe22fe9b52011-06-07 21:58:27 +02005497 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5498 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005499 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005500
5501 *ptr = val * 1000;
5502
5503 return cnt;
5504}
5505
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005506static ssize_t
5507tracing_thresh_read(struct file *filp, char __user *ubuf,
5508 size_t cnt, loff_t *ppos)
5509{
5510 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
5511}
5512
5513static ssize_t
5514tracing_thresh_write(struct file *filp, const char __user *ubuf,
5515 size_t cnt, loff_t *ppos)
5516{
5517 struct trace_array *tr = filp->private_data;
5518 int ret;
5519
5520 mutex_lock(&trace_types_lock);
5521 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
5522 if (ret < 0)
5523 goto out;
5524
5525 if (tr->current_trace->update_thresh) {
5526 ret = tr->current_trace->update_thresh(tr);
5527 if (ret < 0)
5528 goto out;
5529 }
5530
5531 ret = cnt;
5532out:
5533 mutex_unlock(&trace_types_lock);
5534
5535 return ret;
5536}
5537
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04005538#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Chen Gange428abb2015-11-10 05:15:15 +08005539
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005540static ssize_t
5541tracing_max_lat_read(struct file *filp, char __user *ubuf,
5542 size_t cnt, loff_t *ppos)
5543{
5544 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos);
5545}
5546
5547static ssize_t
5548tracing_max_lat_write(struct file *filp, const char __user *ubuf,
5549 size_t cnt, loff_t *ppos)
5550{
5551 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos);
5552}
5553
Chen Gange428abb2015-11-10 05:15:15 +08005554#endif
5555
Steven Rostedtb3806b42008-05-12 21:20:46 +02005556static int tracing_open_pipe(struct inode *inode, struct file *filp)
5557{
Oleg Nesterov15544202013-07-23 17:25:57 +02005558 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005559 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005560 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005561
5562 if (tracing_disabled)
5563 return -ENODEV;
5564
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005565 if (trace_array_get(tr) < 0)
5566 return -ENODEV;
5567
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005568 mutex_lock(&trace_types_lock);
5569
Steven Rostedtb3806b42008-05-12 21:20:46 +02005570 /* create a buffer to store the information to pass to userspace */
5571 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005572 if (!iter) {
5573 ret = -ENOMEM;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005574 __trace_array_put(tr);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005575 goto out;
5576 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02005577
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04005578 trace_seq_init(&iter->seq);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005579 iter->trace = tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005580
5581 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
5582 ret = -ENOMEM;
5583 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10305584 }
5585
Steven Rostedta3097202008-11-07 22:36:02 -05005586 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10305587 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05005588
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005589 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt112f38a72009-06-01 15:16:05 -04005590 iter->iter_flags |= TRACE_FILE_LAT_FMT;
5591
David Sharp8be07092012-11-13 12:18:22 -08005592 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09005593 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08005594 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
5595
Oleg Nesterov15544202013-07-23 17:25:57 +02005596 iter->tr = tr;
5597 iter->trace_buffer = &tr->trace_buffer;
5598 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005599 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005600 filp->private_data = iter;
5601
Steven Rostedt107bad82008-05-12 21:21:01 +02005602 if (iter->trace->pipe_open)
5603 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02005604
Arnd Bergmannb4447862010-07-07 23:40:11 +02005605 nonseekable_open(inode, filp);
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005606
5607 tr->current_trace->ref++;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005608out:
5609 mutex_unlock(&trace_types_lock);
5610 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005611
5612fail:
5613 kfree(iter->trace);
5614 kfree(iter);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005615 __trace_array_put(tr);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005616 mutex_unlock(&trace_types_lock);
5617 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005618}
5619
5620static int tracing_release_pipe(struct inode *inode, struct file *file)
5621{
5622 struct trace_iterator *iter = file->private_data;
Oleg Nesterov15544202013-07-23 17:25:57 +02005623 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005624
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005625 mutex_lock(&trace_types_lock);
5626
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005627 tr->current_trace->ref--;
5628
Steven Rostedt29bf4a52009-12-09 12:37:43 -05005629 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05005630 iter->trace->pipe_close(iter);
5631
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005632 mutex_unlock(&trace_types_lock);
5633
Rusty Russell44623442009-01-01 10:12:23 +10305634 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005635 mutex_destroy(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005636 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005637
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005638 trace_array_put(tr);
5639
Steven Rostedtb3806b42008-05-12 21:20:46 +02005640 return 0;
5641}
5642
Al Viro9dd95742017-07-03 00:42:43 -04005643static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005644trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005645{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005646 struct trace_array *tr = iter->tr;
5647
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005648 /* Iterators are static, they should be filled or empty */
5649 if (trace_buffer_iter(iter, iter->cpu_file))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08005650 return EPOLLIN | EPOLLRDNORM;
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005651
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005652 if (tr->trace_flags & TRACE_ITER_BLOCK)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005653 /*
5654 * Always select as readable when in blocking mode
5655 */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08005656 return EPOLLIN | EPOLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005657 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005658 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005659 filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005660}
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005661
Al Viro9dd95742017-07-03 00:42:43 -04005662static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005663tracing_poll_pipe(struct file *filp, poll_table *poll_table)
5664{
5665 struct trace_iterator *iter = filp->private_data;
5666
5667 return trace_poll(iter, filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005668}
5669
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005670/* Must be called with iter->mutex held. */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005671static int tracing_wait_pipe(struct file *filp)
5672{
5673 struct trace_iterator *iter = filp->private_data;
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04005674 int ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005675
5676 while (trace_empty(iter)) {
5677
5678 if ((filp->f_flags & O_NONBLOCK)) {
5679 return -EAGAIN;
5680 }
5681
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005682 /*
Liu Bo250bfd32013-01-14 10:54:11 +08005683 * We block until we read something and tracing is disabled.
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005684 * We still block if tracing is disabled, but we have never
5685 * read anything. This allows a user to cat this file, and
5686 * then enable tracing. But after we have read something,
5687 * we give an EOF when tracing is again disabled.
5688 *
5689 * iter->pos will be 0 if we haven't read anything.
5690 */
Tahsin Erdogan75df6e62017-09-17 03:23:48 -07005691 if (!tracer_tracing_is_on(iter->tr) && iter->pos)
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005692 break;
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04005693
5694 mutex_unlock(&iter->mutex);
5695
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05005696 ret = wait_on_pipe(iter, 0);
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04005697
5698 mutex_lock(&iter->mutex);
5699
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04005700 if (ret)
5701 return ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005702 }
5703
5704 return 1;
5705}
5706
Steven Rostedtb3806b42008-05-12 21:20:46 +02005707/*
5708 * Consumer reader.
5709 */
5710static ssize_t
5711tracing_read_pipe(struct file *filp, char __user *ubuf,
5712 size_t cnt, loff_t *ppos)
5713{
5714 struct trace_iterator *iter = filp->private_data;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005715 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005716
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005717 /*
5718 * Avoid more than one consumer on a single file descriptor
5719 * This is just a matter of traces coherency, the ring buffer itself
5720 * is protected.
5721 */
5722 mutex_lock(&iter->mutex);
Steven Rostedt (Red Hat)12458002016-09-23 22:57:13 -04005723
5724 /* return any leftover data */
5725 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
5726 if (sret != -EBUSY)
5727 goto out;
5728
5729 trace_seq_init(&iter->seq);
5730
Steven Rostedt107bad82008-05-12 21:21:01 +02005731 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005732 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
5733 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02005734 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02005735 }
5736
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005737waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005738 sret = tracing_wait_pipe(filp);
5739 if (sret <= 0)
5740 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005741
5742 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005743 if (trace_empty(iter)) {
5744 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02005745 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005746 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02005747
5748 if (cnt >= PAGE_SIZE)
5749 cnt = PAGE_SIZE - 1;
5750
Steven Rostedt53d0aa72008-05-12 21:21:01 +02005751 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02005752 memset(&iter->seq, 0,
5753 sizeof(struct trace_iterator) -
5754 offsetof(struct trace_iterator, seq));
Andrew Vagined5467d2013-08-02 21:16:43 +04005755 cpumask_clear(iter->started);
Steven Rostedt4823ed72008-05-12 21:21:01 +02005756 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005757
Lai Jiangshan4f535962009-05-18 19:35:34 +08005758 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005759 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05005760 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02005761 enum print_line_t ret;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005762 int save_len = iter->seq.seq.len;
Steven Rostedt088b1e422008-05-12 21:20:48 +02005763
Ingo Molnarf9896bf2008-05-12 21:20:47 +02005764 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02005765 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02005766 /* don't print partial lines */
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005767 iter->seq.seq.len = save_len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005768 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02005769 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01005770 if (ret != TRACE_TYPE_NO_CONSUME)
5771 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005772
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005773 if (trace_seq_used(&iter->seq) >= cnt)
Steven Rostedtb3806b42008-05-12 21:20:46 +02005774 break;
Jiri Olsaee5e51f2011-03-25 12:05:18 +01005775
5776 /*
5777 * Setting the full flag means we reached the trace_seq buffer
5778 * size and we should leave by partial output condition above.
5779 * One of the trace_seq_* functions is not used properly.
5780 */
5781 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
5782 iter->ent->type);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005783 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005784 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08005785 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02005786
Steven Rostedtb3806b42008-05-12 21:20:46 +02005787 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005788 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005789 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
Steven Rostedtf9520752009-03-02 14:04:40 -05005790 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005791
5792 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005793 * If there was nothing to send to user, in spite of consuming trace
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005794 * entries, go back to wait for more entries.
5795 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005796 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005797 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005798
Steven Rostedt107bad82008-05-12 21:21:01 +02005799out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005800 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02005801
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005802 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005803}
5804
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005805static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
5806 unsigned int idx)
5807{
5808 __free_page(spd->pages[idx]);
5809}
5810
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08005811static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05005812 .can_merge = 0,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005813 .confirm = generic_pipe_buf_confirm,
Al Viro92fdd982014-01-17 07:53:39 -05005814 .release = generic_pipe_buf_release,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005815 .steal = generic_pipe_buf_steal,
5816 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005817};
5818
Steven Rostedt34cd4992009-02-09 12:06:29 -05005819static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01005820tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05005821{
5822 size_t count;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005823 int save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005824 int ret;
5825
5826 /* Seq buffer is page-sized, exactly what we need. */
5827 for (;;) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005828 save_len = iter->seq.seq.len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005829 ret = print_trace_line(iter);
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005830
5831 if (trace_seq_has_overflowed(&iter->seq)) {
5832 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005833 break;
5834 }
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005835
5836 /*
5837 * This should not be hit, because it should only
5838 * be set if the iter->seq overflowed. But check it
5839 * anyway to be safe.
5840 */
Steven Rostedt34cd4992009-02-09 12:06:29 -05005841 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005842 iter->seq.seq.len = save_len;
5843 break;
5844 }
5845
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005846 count = trace_seq_used(&iter->seq) - save_len;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005847 if (rem < count) {
5848 rem = 0;
5849 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005850 break;
5851 }
5852
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08005853 if (ret != TRACE_TYPE_NO_CONSUME)
5854 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05005855 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05005856 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05005857 rem = 0;
5858 iter->ent = NULL;
5859 break;
5860 }
5861 }
5862
5863 return rem;
5864}
5865
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005866static ssize_t tracing_splice_read_pipe(struct file *filp,
5867 loff_t *ppos,
5868 struct pipe_inode_info *pipe,
5869 size_t len,
5870 unsigned int flags)
5871{
Jens Axboe35f3d142010-05-20 10:43:18 +02005872 struct page *pages_def[PIPE_DEF_BUFFERS];
5873 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005874 struct trace_iterator *iter = filp->private_data;
5875 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02005876 .pages = pages_def,
5877 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005878 .nr_pages = 0, /* This gets updated below. */
Eric Dumazet047fe362012-06-12 15:24:40 +02005879 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005880 .ops = &tracing_pipe_buf_ops,
5881 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005882 };
5883 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005884 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005885 unsigned int i;
5886
Jens Axboe35f3d142010-05-20 10:43:18 +02005887 if (splice_grow_spd(pipe, &spd))
5888 return -ENOMEM;
5889
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005890 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005891
5892 if (iter->trace->splice_read) {
5893 ret = iter->trace->splice_read(iter, filp,
5894 ppos, pipe, len, flags);
5895 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05005896 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005897 }
5898
5899 ret = tracing_wait_pipe(filp);
5900 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05005901 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005902
Jason Wessel955b61e2010-08-05 09:22:23 -05005903 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005904 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005905 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005906 }
5907
Lai Jiangshan4f535962009-05-18 19:35:34 +08005908 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005909 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08005910
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005911 /* Fill as many pages as possible. */
Al Viroa786c062014-04-11 12:01:03 -04005912 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
Jens Axboe35f3d142010-05-20 10:43:18 +02005913 spd.pages[i] = alloc_page(GFP_KERNEL);
5914 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05005915 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005916
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01005917 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005918
5919 /* Copy the data into the page, so we can start over. */
5920 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02005921 page_address(spd.pages[i]),
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005922 trace_seq_used(&iter->seq));
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005923 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02005924 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005925 break;
5926 }
Jens Axboe35f3d142010-05-20 10:43:18 +02005927 spd.partial[i].offset = 0;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005928 spd.partial[i].len = trace_seq_used(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005929
Steven Rostedtf9520752009-03-02 14:04:40 -05005930 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005931 }
5932
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005933 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08005934 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005935 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005936
5937 spd.nr_pages = i;
5938
Steven Rostedt (Red Hat)a29054d92016-03-18 15:46:48 -04005939 if (i)
5940 ret = splice_to_pipe(pipe, &spd);
5941 else
5942 ret = 0;
Jens Axboe35f3d142010-05-20 10:43:18 +02005943out:
Eric Dumazet047fe362012-06-12 15:24:40 +02005944 splice_shrink_spd(&spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02005945 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005946
Steven Rostedt34cd4992009-02-09 12:06:29 -05005947out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005948 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02005949 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005950}
5951
Steven Rostedta98a3c32008-05-12 21:20:59 +02005952static ssize_t
5953tracing_entries_read(struct file *filp, char __user *ubuf,
5954 size_t cnt, loff_t *ppos)
5955{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005956 struct inode *inode = file_inode(filp);
5957 struct trace_array *tr = inode->i_private;
5958 int cpu = tracing_get_cpu(inode);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005959 char buf[64];
5960 int r = 0;
5961 ssize_t ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005962
Steven Rostedtdb526ca2009-03-12 13:53:25 -04005963 mutex_lock(&trace_types_lock);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005964
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005965 if (cpu == RING_BUFFER_ALL_CPUS) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005966 int cpu, buf_size_same;
5967 unsigned long size;
5968
5969 size = 0;
5970 buf_size_same = 1;
5971 /* check if all cpu sizes are same */
5972 for_each_tracing_cpu(cpu) {
5973 /* fill in the size from first enabled cpu */
5974 if (size == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005975 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
5976 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005977 buf_size_same = 0;
5978 break;
5979 }
5980 }
5981
5982 if (buf_size_same) {
5983 if (!ring_buffer_expanded)
5984 r = sprintf(buf, "%lu (expanded: %lu)\n",
5985 size >> 10,
5986 trace_buf_size >> 10);
5987 else
5988 r = sprintf(buf, "%lu\n", size >> 10);
5989 } else
5990 r = sprintf(buf, "X\n");
5991 } else
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005992 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005993
Steven Rostedtdb526ca2009-03-12 13:53:25 -04005994 mutex_unlock(&trace_types_lock);
5995
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005996 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5997 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005998}
5999
6000static ssize_t
6001tracing_entries_write(struct file *filp, const char __user *ubuf,
6002 size_t cnt, loff_t *ppos)
6003{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006004 struct inode *inode = file_inode(filp);
6005 struct trace_array *tr = inode->i_private;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006006 unsigned long val;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006007 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006008
Peter Huewe22fe9b52011-06-07 21:58:27 +02006009 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6010 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02006011 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006012
6013 /* must have at least 1 entry */
6014 if (!val)
6015 return -EINVAL;
6016
Steven Rostedt1696b2b2008-11-13 00:09:35 -05006017 /* value is in KB */
6018 val <<= 10;
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006019 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006020 if (ret < 0)
6021 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006022
Jiri Olsacf8517c2009-10-23 19:36:16 -04006023 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006024
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006025 return cnt;
6026}
Steven Rostedtbf5e6512008-11-10 21:46:00 -05006027
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006028static ssize_t
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006029tracing_total_entries_read(struct file *filp, char __user *ubuf,
6030 size_t cnt, loff_t *ppos)
6031{
6032 struct trace_array *tr = filp->private_data;
6033 char buf[64];
6034 int r, cpu;
6035 unsigned long size = 0, expanded_size = 0;
6036
6037 mutex_lock(&trace_types_lock);
6038 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006039 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006040 if (!ring_buffer_expanded)
6041 expanded_size += trace_buf_size >> 10;
6042 }
6043 if (ring_buffer_expanded)
6044 r = sprintf(buf, "%lu\n", size);
6045 else
6046 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
6047 mutex_unlock(&trace_types_lock);
6048
6049 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6050}
6051
6052static ssize_t
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006053tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
6054 size_t cnt, loff_t *ppos)
6055{
6056 /*
6057 * There is no need to read what the user has written, this function
6058 * is just to make sure that there is no error when "echo" is used
6059 */
6060
6061 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006062
6063 return cnt;
6064}
6065
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006066static int
6067tracing_free_buffer_release(struct inode *inode, struct file *filp)
6068{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006069 struct trace_array *tr = inode->i_private;
6070
Steven Rostedtcf30cf62011-06-14 22:44:07 -04006071 /* disable tracing ? */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006072 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
Alexander Z Lam711e1242013-08-02 18:36:15 -07006073 tracer_tracing_off(tr);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006074 /* resize the ring buffer to 0 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006075 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006076
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006077 trace_array_put(tr);
6078
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006079 return 0;
6080}
6081
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006082static ssize_t
6083tracing_mark_write(struct file *filp, const char __user *ubuf,
6084 size_t cnt, loff_t *fpos)
6085{
Alexander Z Lam2d716192013-07-01 15:31:24 -07006086 struct trace_array *tr = filp->private_data;
Steven Rostedtd696b582011-09-22 11:50:27 -04006087 struct ring_buffer_event *event;
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006088 enum event_trigger_type tt = ETT_NONE;
Steven Rostedtd696b582011-09-22 11:50:27 -04006089 struct ring_buffer *buffer;
6090 struct print_entry *entry;
6091 unsigned long irq_flags;
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006092 const char faulted[] = "<faulted>";
Steven Rostedtd696b582011-09-22 11:50:27 -04006093 ssize_t written;
Steven Rostedtd696b582011-09-22 11:50:27 -04006094 int size;
6095 int len;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006096
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006097/* Used in tracing_mark_raw_write() as well */
6098#define FAULTED_SIZE (sizeof(faulted) - 1) /* '\0' is already accounted for */
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006099
Steven Rostedtc76f0692008-11-07 22:36:02 -05006100 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006101 return -EINVAL;
6102
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006103 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -07006104 return -EINVAL;
6105
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006106 if (cnt > TRACE_BUF_SIZE)
6107 cnt = TRACE_BUF_SIZE;
6108
Steven Rostedtd696b582011-09-22 11:50:27 -04006109 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006110
Steven Rostedtd696b582011-09-22 11:50:27 -04006111 local_save_flags(irq_flags);
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006112 size = sizeof(*entry) + cnt + 2; /* add '\0' and possible '\n' */
6113
6114 /* If less than "<faulted>", then make sure we can still add that */
6115 if (cnt < FAULTED_SIZE)
6116 size += FAULTED_SIZE - cnt;
6117
Alexander Z Lam2d716192013-07-01 15:31:24 -07006118 buffer = tr->trace_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05006119 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
6120 irq_flags, preempt_count());
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006121 if (unlikely(!event))
Steven Rostedtd696b582011-09-22 11:50:27 -04006122 /* Ring buffer disabled, return as if not open for write */
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006123 return -EBADF;
Steven Rostedtd696b582011-09-22 11:50:27 -04006124
6125 entry = ring_buffer_event_data(event);
6126 entry->ip = _THIS_IP_;
6127
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006128 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
6129 if (len) {
6130 memcpy(&entry->buf, faulted, FAULTED_SIZE);
6131 cnt = FAULTED_SIZE;
6132 written = -EFAULT;
Steven Rostedtd696b582011-09-22 11:50:27 -04006133 } else
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006134 written = cnt;
6135 len = cnt;
Steven Rostedtd696b582011-09-22 11:50:27 -04006136
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006137 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
6138 /* do not add \n before testing triggers, but add \0 */
6139 entry->buf[cnt] = '\0';
6140 tt = event_triggers_call(tr->trace_marker_file, entry, event);
6141 }
6142
Steven Rostedtd696b582011-09-22 11:50:27 -04006143 if (entry->buf[cnt - 1] != '\n') {
6144 entry->buf[cnt] = '\n';
6145 entry->buf[cnt + 1] = '\0';
6146 } else
6147 entry->buf[cnt] = '\0';
6148
Steven Rostedt7ffbd482012-10-11 12:14:25 -04006149 __buffer_unlock_commit(buffer, event);
Steven Rostedtd696b582011-09-22 11:50:27 -04006150
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006151 if (tt)
6152 event_triggers_post_call(tr->trace_marker_file, tt);
6153
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006154 if (written > 0)
6155 *fpos += written;
Steven Rostedtd696b582011-09-22 11:50:27 -04006156
Steven Rostedtfa32e852016-07-06 15:25:08 -04006157 return written;
6158}
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006159
Steven Rostedtfa32e852016-07-06 15:25:08 -04006160/* Limit it for now to 3K (including tag) */
6161#define RAW_DATA_MAX_SIZE (1024*3)
6162
6163static ssize_t
6164tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
6165 size_t cnt, loff_t *fpos)
6166{
6167 struct trace_array *tr = filp->private_data;
6168 struct ring_buffer_event *event;
6169 struct ring_buffer *buffer;
6170 struct raw_data_entry *entry;
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006171 const char faulted[] = "<faulted>";
Steven Rostedtfa32e852016-07-06 15:25:08 -04006172 unsigned long irq_flags;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006173 ssize_t written;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006174 int size;
6175 int len;
6176
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006177#define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
6178
Steven Rostedtfa32e852016-07-06 15:25:08 -04006179 if (tracing_disabled)
6180 return -EINVAL;
6181
6182 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
6183 return -EINVAL;
6184
6185 /* The marker must at least have a tag id */
6186 if (cnt < sizeof(unsigned int) || cnt > RAW_DATA_MAX_SIZE)
6187 return -EINVAL;
6188
6189 if (cnt > TRACE_BUF_SIZE)
6190 cnt = TRACE_BUF_SIZE;
6191
6192 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
6193
Steven Rostedtfa32e852016-07-06 15:25:08 -04006194 local_save_flags(irq_flags);
6195 size = sizeof(*entry) + cnt;
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006196 if (cnt < FAULT_SIZE_ID)
6197 size += FAULT_SIZE_ID - cnt;
6198
Steven Rostedtfa32e852016-07-06 15:25:08 -04006199 buffer = tr->trace_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05006200 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
6201 irq_flags, preempt_count());
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006202 if (!event)
Steven Rostedtfa32e852016-07-06 15:25:08 -04006203 /* Ring buffer disabled, return as if not open for write */
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006204 return -EBADF;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006205
6206 entry = ring_buffer_event_data(event);
6207
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006208 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
6209 if (len) {
6210 entry->id = -1;
6211 memcpy(&entry->buf, faulted, FAULTED_SIZE);
6212 written = -EFAULT;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006213 } else
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006214 written = cnt;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006215
6216 __buffer_unlock_commit(buffer, event);
6217
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006218 if (written > 0)
6219 *fpos += written;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006220
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02006221 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006222}
6223
Li Zefan13f16d22009-12-08 11:16:11 +08006224static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08006225{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006226 struct trace_array *tr = m->private;
Zhaolei5079f322009-08-25 16:12:56 +08006227 int i;
6228
6229 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08006230 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08006231 "%s%s%s%s", i ? " " : "",
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006232 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
6233 i == tr->clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08006234 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08006235
Li Zefan13f16d22009-12-08 11:16:11 +08006236 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08006237}
6238
Tom Zanussid71bd342018-01-15 20:52:07 -06006239int tracing_set_clock(struct trace_array *tr, const char *clockstr)
Zhaolei5079f322009-08-25 16:12:56 +08006240{
Zhaolei5079f322009-08-25 16:12:56 +08006241 int i;
6242
Zhaolei5079f322009-08-25 16:12:56 +08006243 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
6244 if (strcmp(trace_clocks[i].name, clockstr) == 0)
6245 break;
6246 }
6247 if (i == ARRAY_SIZE(trace_clocks))
6248 return -EINVAL;
6249
Zhaolei5079f322009-08-25 16:12:56 +08006250 mutex_lock(&trace_types_lock);
6251
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006252 tr->clock_id = i;
6253
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006254 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
Zhaolei5079f322009-08-25 16:12:56 +08006255
David Sharp60303ed2012-10-11 16:27:52 -07006256 /*
6257 * New clock may not be consistent with the previous clock.
6258 * Reset the buffer so that it doesn't have incomparable timestamps.
6259 */
Alexander Z Lam94571582013-08-02 18:36:16 -07006260 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006261
6262#ifdef CONFIG_TRACER_MAX_TRACE
Baohong Liu170b3b12017-09-05 16:57:19 -05006263 if (tr->max_buffer.buffer)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006264 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
Alexander Z Lam94571582013-08-02 18:36:16 -07006265 tracing_reset_online_cpus(&tr->max_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006266#endif
David Sharp60303ed2012-10-11 16:27:52 -07006267
Zhaolei5079f322009-08-25 16:12:56 +08006268 mutex_unlock(&trace_types_lock);
6269
Steven Rostedte1e232c2014-02-10 23:38:46 -05006270 return 0;
6271}
6272
6273static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
6274 size_t cnt, loff_t *fpos)
6275{
6276 struct seq_file *m = filp->private_data;
6277 struct trace_array *tr = m->private;
6278 char buf[64];
6279 const char *clockstr;
6280 int ret;
6281
6282 if (cnt >= sizeof(buf))
6283 return -EINVAL;
6284
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08006285 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedte1e232c2014-02-10 23:38:46 -05006286 return -EFAULT;
6287
6288 buf[cnt] = 0;
6289
6290 clockstr = strstrip(buf);
6291
6292 ret = tracing_set_clock(tr, clockstr);
6293 if (ret)
6294 return ret;
6295
Zhaolei5079f322009-08-25 16:12:56 +08006296 *fpos += cnt;
6297
6298 return cnt;
6299}
6300
Li Zefan13f16d22009-12-08 11:16:11 +08006301static int tracing_clock_open(struct inode *inode, struct file *file)
6302{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006303 struct trace_array *tr = inode->i_private;
6304 int ret;
6305
Li Zefan13f16d22009-12-08 11:16:11 +08006306 if (tracing_disabled)
6307 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006308
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006309 if (trace_array_get(tr))
6310 return -ENODEV;
6311
6312 ret = single_open(file, tracing_clock_show, inode->i_private);
6313 if (ret < 0)
6314 trace_array_put(tr);
6315
6316 return ret;
Li Zefan13f16d22009-12-08 11:16:11 +08006317}
6318
Tom Zanussi2c1ea602018-01-15 20:51:41 -06006319static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
6320{
6321 struct trace_array *tr = m->private;
6322
6323 mutex_lock(&trace_types_lock);
6324
6325 if (ring_buffer_time_stamp_abs(tr->trace_buffer.buffer))
6326 seq_puts(m, "delta [absolute]\n");
6327 else
6328 seq_puts(m, "[delta] absolute\n");
6329
6330 mutex_unlock(&trace_types_lock);
6331
6332 return 0;
6333}
6334
6335static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
6336{
6337 struct trace_array *tr = inode->i_private;
6338 int ret;
6339
6340 if (tracing_disabled)
6341 return -ENODEV;
6342
6343 if (trace_array_get(tr))
6344 return -ENODEV;
6345
6346 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
6347 if (ret < 0)
6348 trace_array_put(tr);
6349
6350 return ret;
6351}
6352
Tom Zanussi00b41452018-01-15 20:51:39 -06006353int tracing_set_time_stamp_abs(struct trace_array *tr, bool abs)
6354{
6355 int ret = 0;
6356
6357 mutex_lock(&trace_types_lock);
6358
6359 if (abs && tr->time_stamp_abs_ref++)
6360 goto out;
6361
6362 if (!abs) {
6363 if (WARN_ON_ONCE(!tr->time_stamp_abs_ref)) {
6364 ret = -EINVAL;
6365 goto out;
6366 }
6367
6368 if (--tr->time_stamp_abs_ref)
6369 goto out;
6370 }
6371
6372 ring_buffer_set_time_stamp_abs(tr->trace_buffer.buffer, abs);
6373
6374#ifdef CONFIG_TRACER_MAX_TRACE
6375 if (tr->max_buffer.buffer)
6376 ring_buffer_set_time_stamp_abs(tr->max_buffer.buffer, abs);
6377#endif
6378 out:
6379 mutex_unlock(&trace_types_lock);
6380
6381 return ret;
6382}
6383
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006384struct ftrace_buffer_info {
6385 struct trace_iterator iter;
6386 void *spare;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006387 unsigned int spare_cpu;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006388 unsigned int read;
6389};
6390
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006391#ifdef CONFIG_TRACER_SNAPSHOT
6392static int tracing_snapshot_open(struct inode *inode, struct file *file)
6393{
Oleg Nesterov6484c712013-07-23 17:26:10 +02006394 struct trace_array *tr = inode->i_private;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006395 struct trace_iterator *iter;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006396 struct seq_file *m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006397 int ret = 0;
6398
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006399 if (trace_array_get(tr) < 0)
6400 return -ENODEV;
6401
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006402 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02006403 iter = __tracing_open(inode, file, true);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006404 if (IS_ERR(iter))
6405 ret = PTR_ERR(iter);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006406 } else {
6407 /* Writes still need the seq_file to hold the private data */
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006408 ret = -ENOMEM;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006409 m = kzalloc(sizeof(*m), GFP_KERNEL);
6410 if (!m)
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006411 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006412 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
6413 if (!iter) {
6414 kfree(m);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006415 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006416 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006417 ret = 0;
6418
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006419 iter->tr = tr;
Oleg Nesterov6484c712013-07-23 17:26:10 +02006420 iter->trace_buffer = &tr->max_buffer;
6421 iter->cpu_file = tracing_get_cpu(inode);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006422 m->private = iter;
6423 file->private_data = m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006424 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006425out:
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006426 if (ret < 0)
6427 trace_array_put(tr);
6428
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006429 return ret;
6430}
6431
6432static ssize_t
6433tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
6434 loff_t *ppos)
6435{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006436 struct seq_file *m = filp->private_data;
6437 struct trace_iterator *iter = m->private;
6438 struct trace_array *tr = iter->tr;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006439 unsigned long val;
6440 int ret;
6441
6442 ret = tracing_update_buffers();
6443 if (ret < 0)
6444 return ret;
6445
6446 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6447 if (ret)
6448 return ret;
6449
6450 mutex_lock(&trace_types_lock);
6451
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006452 if (tr->current_trace->use_max_tr) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006453 ret = -EBUSY;
6454 goto out;
6455 }
6456
6457 switch (val) {
6458 case 0:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006459 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6460 ret = -EINVAL;
6461 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006462 }
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04006463 if (tr->allocated_snapshot)
6464 free_snapshot(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006465 break;
6466 case 1:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006467/* Only allow per-cpu swap if the ring buffer supports it */
6468#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
6469 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6470 ret = -EINVAL;
6471 break;
6472 }
6473#endif
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05006474 if (!tr->allocated_snapshot) {
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04006475 ret = tracing_alloc_snapshot_instance(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006476 if (ret < 0)
6477 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006478 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006479 local_irq_disable();
6480 /* Now, we're going to swap */
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006481 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05006482 update_max_tr(tr, current, smp_processor_id());
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006483 else
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05006484 update_max_tr_single(tr, current, iter->cpu_file);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006485 local_irq_enable();
6486 break;
6487 default:
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05006488 if (tr->allocated_snapshot) {
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006489 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
6490 tracing_reset_online_cpus(&tr->max_buffer);
6491 else
6492 tracing_reset(&tr->max_buffer, iter->cpu_file);
6493 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006494 break;
6495 }
6496
6497 if (ret >= 0) {
6498 *ppos += cnt;
6499 ret = cnt;
6500 }
6501out:
6502 mutex_unlock(&trace_types_lock);
6503 return ret;
6504}
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006505
6506static int tracing_snapshot_release(struct inode *inode, struct file *file)
6507{
6508 struct seq_file *m = file->private_data;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006509 int ret;
6510
6511 ret = tracing_release(inode, file);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006512
6513 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006514 return ret;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006515
6516 /* If write only, the seq_file is just a stub */
6517 if (m)
6518 kfree(m->private);
6519 kfree(m);
6520
6521 return 0;
6522}
6523
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006524static int tracing_buffers_open(struct inode *inode, struct file *filp);
6525static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
6526 size_t count, loff_t *ppos);
6527static int tracing_buffers_release(struct inode *inode, struct file *file);
6528static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
6529 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
6530
6531static int snapshot_raw_open(struct inode *inode, struct file *filp)
6532{
6533 struct ftrace_buffer_info *info;
6534 int ret;
6535
6536 ret = tracing_buffers_open(inode, filp);
6537 if (ret < 0)
6538 return ret;
6539
6540 info = filp->private_data;
6541
6542 if (info->iter.trace->use_max_tr) {
6543 tracing_buffers_release(inode, filp);
6544 return -EBUSY;
6545 }
6546
6547 info->iter.snapshot = true;
6548 info->iter.trace_buffer = &info->iter.tr->max_buffer;
6549
6550 return ret;
6551}
6552
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006553#endif /* CONFIG_TRACER_SNAPSHOT */
6554
6555
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04006556static const struct file_operations tracing_thresh_fops = {
6557 .open = tracing_open_generic,
6558 .read = tracing_thresh_read,
6559 .write = tracing_thresh_write,
6560 .llseek = generic_file_llseek,
6561};
6562
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04006563#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006564static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006565 .open = tracing_open_generic,
6566 .read = tracing_max_lat_read,
6567 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006568 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006569};
Chen Gange428abb2015-11-10 05:15:15 +08006570#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006571
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006572static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006573 .open = tracing_open_generic,
6574 .read = tracing_set_trace_read,
6575 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006576 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006577};
6578
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006579static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006580 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006581 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006582 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006583 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006584 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006585 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02006586};
6587
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006588static const struct file_operations tracing_entries_fops = {
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006589 .open = tracing_open_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02006590 .read = tracing_entries_read,
6591 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006592 .llseek = generic_file_llseek,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006593 .release = tracing_release_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02006594};
6595
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006596static const struct file_operations tracing_total_entries_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006597 .open = tracing_open_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006598 .read = tracing_total_entries_read,
6599 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006600 .release = tracing_release_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006601};
6602
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006603static const struct file_operations tracing_free_buffer_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006604 .open = tracing_open_generic_tr,
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006605 .write = tracing_free_buffer_write,
6606 .release = tracing_free_buffer_release,
6607};
6608
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006609static const struct file_operations tracing_mark_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006610 .open = tracing_open_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006611 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006612 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006613 .release = tracing_release_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006614};
6615
Steven Rostedtfa32e852016-07-06 15:25:08 -04006616static const struct file_operations tracing_mark_raw_fops = {
6617 .open = tracing_open_generic_tr,
6618 .write = tracing_mark_raw_write,
6619 .llseek = generic_file_llseek,
6620 .release = tracing_release_generic_tr,
6621};
6622
Zhaolei5079f322009-08-25 16:12:56 +08006623static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08006624 .open = tracing_clock_open,
6625 .read = seq_read,
6626 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006627 .release = tracing_single_release_tr,
Zhaolei5079f322009-08-25 16:12:56 +08006628 .write = tracing_clock_write,
6629};
6630
Tom Zanussi2c1ea602018-01-15 20:51:41 -06006631static const struct file_operations trace_time_stamp_mode_fops = {
6632 .open = tracing_time_stamp_mode_open,
6633 .read = seq_read,
6634 .llseek = seq_lseek,
6635 .release = tracing_single_release_tr,
6636};
6637
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006638#ifdef CONFIG_TRACER_SNAPSHOT
6639static const struct file_operations snapshot_fops = {
6640 .open = tracing_snapshot_open,
6641 .read = seq_read,
6642 .write = tracing_snapshot_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006643 .llseek = tracing_lseek,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006644 .release = tracing_snapshot_release,
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006645};
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006646
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006647static const struct file_operations snapshot_raw_fops = {
6648 .open = snapshot_raw_open,
6649 .read = tracing_buffers_read,
6650 .release = tracing_buffers_release,
6651 .splice_read = tracing_buffers_splice_read,
6652 .llseek = no_llseek,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006653};
6654
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006655#endif /* CONFIG_TRACER_SNAPSHOT */
6656
Steven Rostedt2cadf912008-12-01 22:20:19 -05006657static int tracing_buffers_open(struct inode *inode, struct file *filp)
6658{
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006659 struct trace_array *tr = inode->i_private;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006660 struct ftrace_buffer_info *info;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006661 int ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006662
6663 if (tracing_disabled)
6664 return -ENODEV;
6665
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006666 if (trace_array_get(tr) < 0)
6667 return -ENODEV;
6668
Steven Rostedt2cadf912008-12-01 22:20:19 -05006669 info = kzalloc(sizeof(*info), GFP_KERNEL);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006670 if (!info) {
6671 trace_array_put(tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006672 return -ENOMEM;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006673 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05006674
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006675 mutex_lock(&trace_types_lock);
6676
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006677 info->iter.tr = tr;
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006678 info->iter.cpu_file = tracing_get_cpu(inode);
Steven Rostedtb6273442013-02-28 13:44:11 -05006679 info->iter.trace = tr->current_trace;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006680 info->iter.trace_buffer = &tr->trace_buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006681 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006682 /* Force reading ring buffer for first read */
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006683 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006684
6685 filp->private_data = info;
6686
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05006687 tr->current_trace->ref++;
6688
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006689 mutex_unlock(&trace_types_lock);
6690
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006691 ret = nonseekable_open(inode, filp);
6692 if (ret < 0)
6693 trace_array_put(tr);
6694
6695 return ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006696}
6697
Al Viro9dd95742017-07-03 00:42:43 -04006698static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006699tracing_buffers_poll(struct file *filp, poll_table *poll_table)
6700{
6701 struct ftrace_buffer_info *info = filp->private_data;
6702 struct trace_iterator *iter = &info->iter;
6703
6704 return trace_poll(iter, filp, poll_table);
6705}
6706
Steven Rostedt2cadf912008-12-01 22:20:19 -05006707static ssize_t
6708tracing_buffers_read(struct file *filp, char __user *ubuf,
6709 size_t count, loff_t *ppos)
6710{
6711 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006712 struct trace_iterator *iter = &info->iter;
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04006713 ssize_t ret = 0;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006714 ssize_t size;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006715
Steven Rostedt2dc5d122009-03-04 19:10:05 -05006716 if (!count)
6717 return 0;
6718
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006719#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006720 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
6721 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006722#endif
6723
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006724 if (!info->spare) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006725 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
6726 iter->cpu_file);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04006727 if (IS_ERR(info->spare)) {
6728 ret = PTR_ERR(info->spare);
6729 info->spare = NULL;
6730 } else {
6731 info->spare_cpu = iter->cpu_file;
6732 }
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006733 }
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006734 if (!info->spare)
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04006735 return ret;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006736
Steven Rostedt2cadf912008-12-01 22:20:19 -05006737 /* Do we have previous read data to read? */
6738 if (info->read < PAGE_SIZE)
6739 goto read;
6740
Steven Rostedtb6273442013-02-28 13:44:11 -05006741 again:
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006742 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006743 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006744 &info->spare,
6745 count,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006746 iter->cpu_file, 0);
6747 trace_access_unlock(iter->cpu_file);
Steven Rostedtb6273442013-02-28 13:44:11 -05006748
6749 if (ret < 0) {
6750 if (trace_empty(iter)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006751 if ((filp->f_flags & O_NONBLOCK))
6752 return -EAGAIN;
6753
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05006754 ret = wait_on_pipe(iter, 0);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006755 if (ret)
6756 return ret;
6757
Steven Rostedtb6273442013-02-28 13:44:11 -05006758 goto again;
6759 }
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006760 return 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05006761 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05006762
Steven Rostedt436fc282011-10-14 10:44:25 -04006763 info->read = 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05006764 read:
Steven Rostedt2cadf912008-12-01 22:20:19 -05006765 size = PAGE_SIZE - info->read;
6766 if (size > count)
6767 size = count;
6768
6769 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006770 if (ret == size)
6771 return -EFAULT;
6772
Steven Rostedt2dc5d122009-03-04 19:10:05 -05006773 size -= ret;
6774
Steven Rostedt2cadf912008-12-01 22:20:19 -05006775 *ppos += size;
6776 info->read += size;
6777
6778 return size;
6779}
6780
6781static int tracing_buffers_release(struct inode *inode, struct file *file)
6782{
6783 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006784 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006785
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006786 mutex_lock(&trace_types_lock);
6787
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05006788 iter->tr->current_trace->ref--;
6789
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006790 __trace_array_put(iter->tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006791
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006792 if (info->spare)
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006793 ring_buffer_free_read_page(iter->trace_buffer->buffer,
6794 info->spare_cpu, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006795 kfree(info);
6796
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006797 mutex_unlock(&trace_types_lock);
6798
Steven Rostedt2cadf912008-12-01 22:20:19 -05006799 return 0;
6800}
6801
6802struct buffer_ref {
6803 struct ring_buffer *buffer;
6804 void *page;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006805 int cpu;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006806 int ref;
6807};
6808
6809static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
6810 struct pipe_buffer *buf)
6811{
6812 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
6813
6814 if (--ref->ref)
6815 return;
6816
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006817 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006818 kfree(ref);
6819 buf->private = 0;
6820}
6821
Steven Rostedt2cadf912008-12-01 22:20:19 -05006822static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
6823 struct pipe_buffer *buf)
6824{
6825 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
6826
6827 ref->ref++;
6828}
6829
6830/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08006831static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05006832 .can_merge = 0,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006833 .confirm = generic_pipe_buf_confirm,
6834 .release = buffer_pipe_buf_release,
Masami Hiramatsud55cb6c2012-08-09 21:31:10 +09006835 .steal = generic_pipe_buf_steal,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006836 .get = buffer_pipe_buf_get,
6837};
6838
6839/*
6840 * Callback from splice_to_pipe(), if we need to release some pages
6841 * at the end of the spd in case we error'ed out in filling the pipe.
6842 */
6843static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
6844{
6845 struct buffer_ref *ref =
6846 (struct buffer_ref *)spd->partial[i].private;
6847
6848 if (--ref->ref)
6849 return;
6850
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006851 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006852 kfree(ref);
6853 spd->partial[i].private = 0;
6854}
6855
6856static ssize_t
6857tracing_buffers_splice_read(struct file *file, loff_t *ppos,
6858 struct pipe_inode_info *pipe, size_t len,
6859 unsigned int flags)
6860{
6861 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006862 struct trace_iterator *iter = &info->iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02006863 struct partial_page partial_def[PIPE_DEF_BUFFERS];
6864 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05006865 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02006866 .pages = pages_def,
6867 .partial = partial_def,
Eric Dumazet047fe362012-06-12 15:24:40 +02006868 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006869 .ops = &buffer_pipe_buf_ops,
6870 .spd_release = buffer_spd_release,
6871 };
6872 struct buffer_ref *ref;
Steven Rostedt (VMware)6b7e6332017-12-22 20:38:57 -05006873 int entries, i;
Rabin Vincent07906da2014-11-06 22:26:07 +01006874 ssize_t ret = 0;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006875
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006876#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006877 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
6878 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006879#endif
6880
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006881 if (*ppos & (PAGE_SIZE - 1))
6882 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08006883
6884 if (len & (PAGE_SIZE - 1)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006885 if (len < PAGE_SIZE)
6886 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08006887 len &= PAGE_MASK;
6888 }
6889
Al Viro1ae22932016-09-17 18:31:46 -04006890 if (splice_grow_spd(pipe, &spd))
6891 return -ENOMEM;
6892
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006893 again:
6894 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006895 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt93459c62009-04-29 00:23:13 -04006896
Al Viroa786c062014-04-11 12:01:03 -04006897 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05006898 struct page *page;
6899 int r;
6900
6901 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
Rabin Vincent07906da2014-11-06 22:26:07 +01006902 if (!ref) {
6903 ret = -ENOMEM;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006904 break;
Rabin Vincent07906da2014-11-06 22:26:07 +01006905 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05006906
Steven Rostedt7267fa62009-04-29 00:16:21 -04006907 ref->ref = 1;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006908 ref->buffer = iter->trace_buffer->buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006909 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04006910 if (IS_ERR(ref->page)) {
6911 ret = PTR_ERR(ref->page);
6912 ref->page = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006913 kfree(ref);
6914 break;
6915 }
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006916 ref->cpu = iter->cpu_file;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006917
6918 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006919 len, iter->cpu_file, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006920 if (r < 0) {
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006921 ring_buffer_free_read_page(ref->buffer, ref->cpu,
6922 ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006923 kfree(ref);
6924 break;
6925 }
6926
Steven Rostedt2cadf912008-12-01 22:20:19 -05006927 page = virt_to_page(ref->page);
6928
6929 spd.pages[i] = page;
6930 spd.partial[i].len = PAGE_SIZE;
6931 spd.partial[i].offset = 0;
6932 spd.partial[i].private = (unsigned long)ref;
6933 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08006934 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04006935
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006936 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006937 }
6938
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006939 trace_access_unlock(iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006940 spd.nr_pages = i;
6941
6942 /* did we read anything? */
6943 if (!spd.nr_pages) {
Rabin Vincent07906da2014-11-06 22:26:07 +01006944 if (ret)
Al Viro1ae22932016-09-17 18:31:46 -04006945 goto out;
Rabin Vincent07906da2014-11-06 22:26:07 +01006946
Al Viro1ae22932016-09-17 18:31:46 -04006947 ret = -EAGAIN;
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006948 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
Al Viro1ae22932016-09-17 18:31:46 -04006949 goto out;
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006950
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05006951 ret = wait_on_pipe(iter, iter->tr->buffer_percent);
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04006952 if (ret)
Al Viro1ae22932016-09-17 18:31:46 -04006953 goto out;
Rabin Vincente30f53a2014-11-10 19:46:34 +01006954
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006955 goto again;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006956 }
6957
6958 ret = splice_to_pipe(pipe, &spd);
Al Viro1ae22932016-09-17 18:31:46 -04006959out:
Eric Dumazet047fe362012-06-12 15:24:40 +02006960 splice_shrink_spd(&spd);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006961
Steven Rostedt2cadf912008-12-01 22:20:19 -05006962 return ret;
6963}
6964
6965static const struct file_operations tracing_buffers_fops = {
6966 .open = tracing_buffers_open,
6967 .read = tracing_buffers_read,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006968 .poll = tracing_buffers_poll,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006969 .release = tracing_buffers_release,
6970 .splice_read = tracing_buffers_splice_read,
6971 .llseek = no_llseek,
6972};
6973
Steven Rostedtc8d77182009-04-29 18:03:45 -04006974static ssize_t
6975tracing_stats_read(struct file *filp, char __user *ubuf,
6976 size_t count, loff_t *ppos)
6977{
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006978 struct inode *inode = file_inode(filp);
6979 struct trace_array *tr = inode->i_private;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006980 struct trace_buffer *trace_buf = &tr->trace_buffer;
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006981 int cpu = tracing_get_cpu(inode);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006982 struct trace_seq *s;
6983 unsigned long cnt;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07006984 unsigned long long t;
6985 unsigned long usec_rem;
Steven Rostedtc8d77182009-04-29 18:03:45 -04006986
Li Zefane4f2d102009-06-15 10:57:28 +08006987 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006988 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01006989 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04006990
6991 trace_seq_init(s);
6992
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006993 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006994 trace_seq_printf(s, "entries: %ld\n", cnt);
6995
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006996 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006997 trace_seq_printf(s, "overrun: %ld\n", cnt);
6998
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006999 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007000 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
7001
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007002 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007003 trace_seq_printf(s, "bytes: %ld\n", cnt);
7004
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09007005 if (trace_clocks[tr->clock_id].in_ns) {
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007006 /* local or global for trace_clock */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007007 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007008 usec_rem = do_div(t, USEC_PER_SEC);
7009 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
7010 t, usec_rem);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007011
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007012 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007013 usec_rem = do_div(t, USEC_PER_SEC);
7014 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
7015 } else {
7016 /* counter or tsc mode for trace_clock */
7017 trace_seq_printf(s, "oldest event ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007018 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007019
7020 trace_seq_printf(s, "now ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007021 ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007022 }
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007023
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007024 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
Slava Pestov884bfe82011-07-15 14:23:58 -07007025 trace_seq_printf(s, "dropped events: %ld\n", cnt);
7026
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007027 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05007028 trace_seq_printf(s, "read events: %ld\n", cnt);
7029
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05007030 count = simple_read_from_buffer(ubuf, count, ppos,
7031 s->buffer, trace_seq_used(s));
Steven Rostedtc8d77182009-04-29 18:03:45 -04007032
7033 kfree(s);
7034
7035 return count;
7036}
7037
7038static const struct file_operations tracing_stats_fops = {
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007039 .open = tracing_open_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04007040 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007041 .llseek = generic_file_llseek,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007042 .release = tracing_release_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04007043};
7044
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007045#ifdef CONFIG_DYNAMIC_FTRACE
7046
7047static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007048tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007049 size_t cnt, loff_t *ppos)
7050{
7051 unsigned long *p = filp->private_data;
Steven Rostedt (VMware)6a9c9812017-06-27 11:02:49 -04007052 char buf[64]; /* Not too big for a shallow stack */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007053 int r;
7054
Steven Rostedt (VMware)6a9c9812017-06-27 11:02:49 -04007055 r = scnprintf(buf, 63, "%ld", *p);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007056 buf[r++] = '\n';
7057
Steven Rostedt (VMware)6a9c9812017-06-27 11:02:49 -04007058 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007059}
7060
Steven Rostedt5e2336a2009-03-05 21:44:55 -05007061static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02007062 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007063 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007064 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007065};
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007066#endif /* CONFIG_DYNAMIC_FTRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007067
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007068#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
7069static void
Steven Rostedt (VMware)bca6c8d2017-04-03 18:18:47 -04007070ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007071 struct trace_array *tr, struct ftrace_probe_ops *ops,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007072 void *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007073{
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -04007074 tracing_snapshot_instance(tr);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007075}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007076
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007077static void
Steven Rostedt (VMware)bca6c8d2017-04-03 18:18:47 -04007078ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007079 struct trace_array *tr, struct ftrace_probe_ops *ops,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007080 void *data)
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007081{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007082 struct ftrace_func_mapper *mapper = data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007083 long *count = NULL;
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007084
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007085 if (mapper)
7086 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007087
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007088 if (count) {
7089
7090 if (*count <= 0)
7091 return;
7092
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007093 (*count)--;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007094 }
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007095
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -04007096 tracing_snapshot_instance(tr);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007097}
7098
7099static int
7100ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
7101 struct ftrace_probe_ops *ops, void *data)
7102{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007103 struct ftrace_func_mapper *mapper = data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007104 long *count = NULL;
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007105
7106 seq_printf(m, "%ps:", (void *)ip);
7107
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01007108 seq_puts(m, "snapshot");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007109
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007110 if (mapper)
7111 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
7112
7113 if (count)
7114 seq_printf(m, ":count=%ld\n", *count);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007115 else
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007116 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007117
7118 return 0;
7119}
7120
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007121static int
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007122ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007123 unsigned long ip, void *init_data, void **data)
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007124{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007125 struct ftrace_func_mapper *mapper = *data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007126
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007127 if (!mapper) {
7128 mapper = allocate_ftrace_func_mapper();
7129 if (!mapper)
7130 return -ENOMEM;
7131 *data = mapper;
7132 }
7133
7134 return ftrace_func_mapper_add_ip(mapper, ip, init_data);
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007135}
7136
7137static void
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007138ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007139 unsigned long ip, void *data)
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007140{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007141 struct ftrace_func_mapper *mapper = data;
7142
7143 if (!ip) {
7144 if (!mapper)
7145 return;
7146 free_ftrace_func_mapper(mapper, NULL);
7147 return;
7148 }
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007149
7150 ftrace_func_mapper_remove_ip(mapper, ip);
7151}
7152
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007153static struct ftrace_probe_ops snapshot_probe_ops = {
7154 .func = ftrace_snapshot,
7155 .print = ftrace_snapshot_print,
7156};
7157
7158static struct ftrace_probe_ops snapshot_count_probe_ops = {
7159 .func = ftrace_count_snapshot,
7160 .print = ftrace_snapshot_print,
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007161 .init = ftrace_snapshot_init,
7162 .free = ftrace_snapshot_free,
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007163};
7164
7165static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007166ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007167 char *glob, char *cmd, char *param, int enable)
7168{
7169 struct ftrace_probe_ops *ops;
7170 void *count = (void *)-1;
7171 char *number;
7172 int ret;
7173
Steven Rostedt (VMware)0f179762017-06-29 10:05:45 -04007174 if (!tr)
7175 return -ENODEV;
7176
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007177 /* hash funcs only work with set_ftrace_filter */
7178 if (!enable)
7179 return -EINVAL;
7180
7181 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
7182
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04007183 if (glob[0] == '!')
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04007184 return unregister_ftrace_function_probe_func(glob+1, tr, ops);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007185
7186 if (!param)
7187 goto out_reg;
7188
7189 number = strsep(&param, ":");
7190
7191 if (!strlen(number))
7192 goto out_reg;
7193
7194 /*
7195 * We use the callback data field (which is a pointer)
7196 * as our counter.
7197 */
7198 ret = kstrtoul(number, 0, (unsigned long *)&count);
7199 if (ret)
7200 return ret;
7201
7202 out_reg:
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04007203 ret = tracing_alloc_snapshot_instance(tr);
Steven Rostedt (VMware)df62db52017-04-19 12:07:08 -04007204 if (ret < 0)
7205 goto out;
7206
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007207 ret = register_ftrace_function_probe(glob, tr, ops, count);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007208
Steven Rostedt (VMware)df62db52017-04-19 12:07:08 -04007209 out:
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007210 return ret < 0 ? ret : 0;
7211}
7212
7213static struct ftrace_func_command ftrace_snapshot_cmd = {
7214 .name = "snapshot",
7215 .func = ftrace_trace_snapshot_callback,
7216};
7217
Tom Zanussi38de93a2013-10-24 08:34:18 -05007218static __init int register_snapshot_cmd(void)
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007219{
7220 return register_ftrace_command(&ftrace_snapshot_cmd);
7221}
7222#else
Tom Zanussi38de93a2013-10-24 08:34:18 -05007223static inline __init int register_snapshot_cmd(void) { return 0; }
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007224#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007225
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007226static struct dentry *tracing_get_dentry(struct trace_array *tr)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007227{
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007228 if (WARN_ON(!tr->dir))
7229 return ERR_PTR(-ENODEV);
7230
7231 /* Top directory uses NULL as the parent */
7232 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
7233 return NULL;
7234
7235 /* All sub buffers have a descriptor */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007236 return tr->dir;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007237}
7238
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007239static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
7240{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007241 struct dentry *d_tracer;
7242
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007243 if (tr->percpu_dir)
7244 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007245
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007246 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05007247 if (IS_ERR(d_tracer))
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007248 return NULL;
7249
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007250 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007251
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007252 WARN_ONCE(!tr->percpu_dir,
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007253 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007254
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007255 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007256}
7257
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007258static struct dentry *
7259trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
7260 void *data, long cpu, const struct file_operations *fops)
7261{
7262 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
7263
7264 if (ret) /* See tracing_get_cpu() */
David Howells7682c912015-03-17 22:26:16 +00007265 d_inode(ret)->i_cdev = (void *)(cpu + 1);
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007266 return ret;
7267}
7268
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007269static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007270tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007271{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007272 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007273 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04007274 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007275
Namhyung Kim0a3d7ce2012-04-23 10:11:57 +09007276 if (!d_percpu)
7277 return;
7278
Steven Rostedtdd49a382010-10-20 21:51:26 -04007279 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007280 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007281 if (!d_cpu) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07007282 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007283 return;
7284 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007285
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007286 /* per cpu trace_pipe */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007287 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
Oleg Nesterov15544202013-07-23 17:25:57 +02007288 tr, cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007289
7290 /* per cpu trace */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007291 trace_create_cpu_file("trace", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007292 tr, cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04007293
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007294 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007295 tr, cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007296
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007297 trace_create_cpu_file("stats", 0444, d_cpu,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007298 tr, cpu, &tracing_stats_fops);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08007299
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007300 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02007301 tr, cpu, &tracing_entries_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05007302
7303#ifdef CONFIG_TRACER_SNAPSHOT
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007304 trace_create_cpu_file("snapshot", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007305 tr, cpu, &snapshot_fops);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007306
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007307 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007308 tr, cpu, &snapshot_raw_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05007309#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007310}
7311
Steven Rostedt60a11772008-05-12 21:20:44 +02007312#ifdef CONFIG_FTRACE_SELFTEST
7313/* Let selftest have access to static functions in this file */
7314#include "trace_selftest.c"
7315#endif
7316
Steven Rostedt577b7852009-02-26 23:43:05 -05007317static ssize_t
7318trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
7319 loff_t *ppos)
7320{
7321 struct trace_option_dentry *topt = filp->private_data;
7322 char *buf;
7323
7324 if (topt->flags->val & topt->opt->bit)
7325 buf = "1\n";
7326 else
7327 buf = "0\n";
7328
7329 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
7330}
7331
7332static ssize_t
7333trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
7334 loff_t *ppos)
7335{
7336 struct trace_option_dentry *topt = filp->private_data;
7337 unsigned long val;
Steven Rostedt577b7852009-02-26 23:43:05 -05007338 int ret;
7339
Peter Huewe22fe9b52011-06-07 21:58:27 +02007340 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7341 if (ret)
Steven Rostedt577b7852009-02-26 23:43:05 -05007342 return ret;
7343
Li Zefan8d18eaa2009-12-08 11:17:06 +08007344 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05007345 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08007346
7347 if (!!(topt->flags->val & topt->opt->bit) != val) {
7348 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05007349 ret = __set_tracer_option(topt->tr, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05007350 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08007351 mutex_unlock(&trace_types_lock);
7352 if (ret)
7353 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05007354 }
7355
7356 *ppos += cnt;
7357
7358 return cnt;
7359}
7360
7361
7362static const struct file_operations trace_options_fops = {
7363 .open = tracing_open_generic,
7364 .read = trace_options_read,
7365 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007366 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05007367};
7368
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007369/*
7370 * In order to pass in both the trace_array descriptor as well as the index
7371 * to the flag that the trace option file represents, the trace_array
7372 * has a character array of trace_flags_index[], which holds the index
7373 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
7374 * The address of this character array is passed to the flag option file
7375 * read/write callbacks.
7376 *
7377 * In order to extract both the index and the trace_array descriptor,
7378 * get_tr_index() uses the following algorithm.
7379 *
7380 * idx = *ptr;
7381 *
7382 * As the pointer itself contains the address of the index (remember
7383 * index[1] == 1).
7384 *
7385 * Then to get the trace_array descriptor, by subtracting that index
7386 * from the ptr, we get to the start of the index itself.
7387 *
7388 * ptr - idx == &index[0]
7389 *
7390 * Then a simple container_of() from that pointer gets us to the
7391 * trace_array descriptor.
7392 */
7393static void get_tr_index(void *data, struct trace_array **ptr,
7394 unsigned int *pindex)
7395{
7396 *pindex = *(unsigned char *)data;
7397
7398 *ptr = container_of(data - *pindex, struct trace_array,
7399 trace_flags_index);
7400}
7401
Steven Rostedta8259072009-02-26 22:19:12 -05007402static ssize_t
7403trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
7404 loff_t *ppos)
7405{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007406 void *tr_index = filp->private_data;
7407 struct trace_array *tr;
7408 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05007409 char *buf;
7410
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007411 get_tr_index(tr_index, &tr, &index);
7412
7413 if (tr->trace_flags & (1 << index))
Steven Rostedta8259072009-02-26 22:19:12 -05007414 buf = "1\n";
7415 else
7416 buf = "0\n";
7417
7418 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
7419}
7420
7421static ssize_t
7422trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
7423 loff_t *ppos)
7424{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007425 void *tr_index = filp->private_data;
7426 struct trace_array *tr;
7427 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05007428 unsigned long val;
7429 int ret;
7430
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007431 get_tr_index(tr_index, &tr, &index);
7432
Peter Huewe22fe9b52011-06-07 21:58:27 +02007433 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7434 if (ret)
Steven Rostedta8259072009-02-26 22:19:12 -05007435 return ret;
7436
Zhaoleif2d84b62009-08-07 18:55:48 +08007437 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05007438 return -EINVAL;
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04007439
7440 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007441 ret = set_tracer_flag(tr, 1 << index, val);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04007442 mutex_unlock(&trace_types_lock);
Steven Rostedta8259072009-02-26 22:19:12 -05007443
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04007444 if (ret < 0)
7445 return ret;
7446
Steven Rostedta8259072009-02-26 22:19:12 -05007447 *ppos += cnt;
7448
7449 return cnt;
7450}
7451
Steven Rostedta8259072009-02-26 22:19:12 -05007452static const struct file_operations trace_options_core_fops = {
7453 .open = tracing_open_generic,
7454 .read = trace_options_core_read,
7455 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007456 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05007457};
7458
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007459struct dentry *trace_create_file(const char *name,
Al Virof4ae40a62011-07-24 04:33:43 -04007460 umode_t mode,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007461 struct dentry *parent,
7462 void *data,
7463 const struct file_operations *fops)
7464{
7465 struct dentry *ret;
7466
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007467 ret = tracefs_create_file(name, mode, parent, data, fops);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007468 if (!ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -07007469 pr_warn("Could not create tracefs '%s' entry\n", name);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007470
7471 return ret;
7472}
7473
7474
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007475static struct dentry *trace_options_init_dentry(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05007476{
7477 struct dentry *d_tracer;
Steven Rostedta8259072009-02-26 22:19:12 -05007478
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007479 if (tr->options)
7480 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05007481
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007482 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05007483 if (IS_ERR(d_tracer))
Steven Rostedta8259072009-02-26 22:19:12 -05007484 return NULL;
7485
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007486 tr->options = tracefs_create_dir("options", d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007487 if (!tr->options) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07007488 pr_warn("Could not create tracefs directory 'options'\n");
Steven Rostedta8259072009-02-26 22:19:12 -05007489 return NULL;
7490 }
7491
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007492 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05007493}
7494
Steven Rostedt577b7852009-02-26 23:43:05 -05007495static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007496create_trace_option_file(struct trace_array *tr,
7497 struct trace_option_dentry *topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05007498 struct tracer_flags *flags,
7499 struct tracer_opt *opt)
7500{
7501 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05007502
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007503 t_options = trace_options_init_dentry(tr);
Steven Rostedt577b7852009-02-26 23:43:05 -05007504 if (!t_options)
7505 return;
7506
7507 topt->flags = flags;
7508 topt->opt = opt;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007509 topt->tr = tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05007510
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007511 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05007512 &trace_options_fops);
7513
Steven Rostedt577b7852009-02-26 23:43:05 -05007514}
7515
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007516static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007517create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
Steven Rostedt577b7852009-02-26 23:43:05 -05007518{
7519 struct trace_option_dentry *topts;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007520 struct trace_options *tr_topts;
Steven Rostedt577b7852009-02-26 23:43:05 -05007521 struct tracer_flags *flags;
7522 struct tracer_opt *opts;
7523 int cnt;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007524 int i;
Steven Rostedt577b7852009-02-26 23:43:05 -05007525
7526 if (!tracer)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007527 return;
Steven Rostedt577b7852009-02-26 23:43:05 -05007528
7529 flags = tracer->flags;
7530
7531 if (!flags || !flags->opts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007532 return;
7533
7534 /*
7535 * If this is an instance, only create flags for tracers
7536 * the instance may have.
7537 */
7538 if (!trace_ok_for_array(tracer, tr))
7539 return;
7540
7541 for (i = 0; i < tr->nr_topts; i++) {
Chunyu Hud39cdd22016-03-08 21:37:01 +08007542 /* Make sure there's no duplicate flags. */
7543 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007544 return;
7545 }
Steven Rostedt577b7852009-02-26 23:43:05 -05007546
7547 opts = flags->opts;
7548
7549 for (cnt = 0; opts[cnt].name; cnt++)
7550 ;
7551
Steven Rostedt0cfe8242009-02-27 10:51:10 -05007552 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05007553 if (!topts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007554 return;
7555
7556 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
7557 GFP_KERNEL);
7558 if (!tr_topts) {
7559 kfree(topts);
7560 return;
7561 }
7562
7563 tr->topts = tr_topts;
7564 tr->topts[tr->nr_topts].tracer = tracer;
7565 tr->topts[tr->nr_topts].topts = topts;
7566 tr->nr_topts++;
Steven Rostedt577b7852009-02-26 23:43:05 -05007567
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04007568 for (cnt = 0; opts[cnt].name; cnt++) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007569 create_trace_option_file(tr, &topts[cnt], flags,
Steven Rostedt577b7852009-02-26 23:43:05 -05007570 &opts[cnt]);
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04007571 WARN_ONCE(topts[cnt].entry == NULL,
7572 "Failed to create trace option: %s",
7573 opts[cnt].name);
7574 }
Steven Rostedt577b7852009-02-26 23:43:05 -05007575}
7576
Steven Rostedta8259072009-02-26 22:19:12 -05007577static struct dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007578create_trace_option_core_file(struct trace_array *tr,
7579 const char *option, long index)
Steven Rostedta8259072009-02-26 22:19:12 -05007580{
7581 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05007582
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007583 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05007584 if (!t_options)
7585 return NULL;
7586
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007587 return trace_create_file(option, 0644, t_options,
7588 (void *)&tr->trace_flags_index[index],
7589 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05007590}
7591
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04007592static void create_trace_options_dir(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05007593{
7594 struct dentry *t_options;
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04007595 bool top_level = tr == &global_trace;
Steven Rostedta8259072009-02-26 22:19:12 -05007596 int i;
7597
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007598 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05007599 if (!t_options)
7600 return;
7601
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04007602 for (i = 0; trace_options[i]; i++) {
7603 if (top_level ||
7604 !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
7605 create_trace_option_core_file(tr, trace_options[i], i);
7606 }
Steven Rostedta8259072009-02-26 22:19:12 -05007607}
7608
Steven Rostedt499e5472012-02-22 15:50:28 -05007609static ssize_t
7610rb_simple_read(struct file *filp, char __user *ubuf,
7611 size_t cnt, loff_t *ppos)
7612{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04007613 struct trace_array *tr = filp->private_data;
Steven Rostedt499e5472012-02-22 15:50:28 -05007614 char buf[64];
7615 int r;
7616
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04007617 r = tracer_tracing_is_on(tr);
Steven Rostedt499e5472012-02-22 15:50:28 -05007618 r = sprintf(buf, "%d\n", r);
7619
7620 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7621}
7622
7623static ssize_t
7624rb_simple_write(struct file *filp, const char __user *ubuf,
7625 size_t cnt, loff_t *ppos)
7626{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04007627 struct trace_array *tr = filp->private_data;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007628 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt499e5472012-02-22 15:50:28 -05007629 unsigned long val;
7630 int ret;
7631
7632 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7633 if (ret)
7634 return ret;
7635
7636 if (buffer) {
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05007637 mutex_lock(&trace_types_lock);
Steven Rostedt (VMware)f1436412018-08-01 15:40:57 -04007638 if (!!val == tracer_tracing_is_on(tr)) {
7639 val = 0; /* do nothing */
7640 } else if (val) {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04007641 tracer_tracing_on(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007642 if (tr->current_trace->start)
7643 tr->current_trace->start(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05007644 } else {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04007645 tracer_tracing_off(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007646 if (tr->current_trace->stop)
7647 tr->current_trace->stop(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05007648 }
7649 mutex_unlock(&trace_types_lock);
Steven Rostedt499e5472012-02-22 15:50:28 -05007650 }
7651
7652 (*ppos)++;
7653
7654 return cnt;
7655}
7656
7657static const struct file_operations rb_simple_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007658 .open = tracing_open_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05007659 .read = rb_simple_read,
7660 .write = rb_simple_write,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007661 .release = tracing_release_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05007662 .llseek = default_llseek,
7663};
7664
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05007665static ssize_t
7666buffer_percent_read(struct file *filp, char __user *ubuf,
7667 size_t cnt, loff_t *ppos)
7668{
7669 struct trace_array *tr = filp->private_data;
7670 char buf[64];
7671 int r;
7672
7673 r = tr->buffer_percent;
7674 r = sprintf(buf, "%d\n", r);
7675
7676 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7677}
7678
7679static ssize_t
7680buffer_percent_write(struct file *filp, const char __user *ubuf,
7681 size_t cnt, loff_t *ppos)
7682{
7683 struct trace_array *tr = filp->private_data;
7684 unsigned long val;
7685 int ret;
7686
7687 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7688 if (ret)
7689 return ret;
7690
7691 if (val > 100)
7692 return -EINVAL;
7693
7694 if (!val)
7695 val = 1;
7696
7697 tr->buffer_percent = val;
7698
7699 (*ppos)++;
7700
7701 return cnt;
7702}
7703
7704static const struct file_operations buffer_percent_fops = {
7705 .open = tracing_open_generic_tr,
7706 .read = buffer_percent_read,
7707 .write = buffer_percent_write,
7708 .release = tracing_release_generic_tr,
7709 .llseek = default_llseek,
7710};
7711
Steven Rostedt277ba042012-08-03 16:10:49 -04007712struct dentry *trace_instance_dir;
7713
7714static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007715init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
Steven Rostedt277ba042012-08-03 16:10:49 -04007716
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007717static int
7718allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
Steven Rostedt277ba042012-08-03 16:10:49 -04007719{
7720 enum ring_buffer_flags rb_flags;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007721
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007722 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007723
Steven Rostedt (Red Hat)dced3412014-01-14 10:19:46 -05007724 buf->tr = tr;
7725
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007726 buf->buffer = ring_buffer_alloc(size, rb_flags);
7727 if (!buf->buffer)
7728 return -ENOMEM;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007729
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007730 buf->data = alloc_percpu(struct trace_array_cpu);
7731 if (!buf->data) {
7732 ring_buffer_free(buf->buffer);
Steven Rostedt (VMware)4397f042017-12-26 20:07:34 -05007733 buf->buffer = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007734 return -ENOMEM;
7735 }
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007736
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007737 /* Allocate the first page for all buffers */
7738 set_buffer_entries(&tr->trace_buffer,
7739 ring_buffer_size(tr->trace_buffer.buffer, 0));
7740
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007741 return 0;
7742}
7743
7744static int allocate_trace_buffers(struct trace_array *tr, int size)
7745{
7746 int ret;
7747
7748 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
7749 if (ret)
7750 return ret;
7751
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007752#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007753 ret = allocate_trace_buffer(tr, &tr->max_buffer,
7754 allocate_snapshot ? size : 1);
7755 if (WARN_ON(ret)) {
7756 ring_buffer_free(tr->trace_buffer.buffer);
Jing Xia24f2aaf2017-12-26 15:12:53 +08007757 tr->trace_buffer.buffer = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007758 free_percpu(tr->trace_buffer.data);
Jing Xia24f2aaf2017-12-26 15:12:53 +08007759 tr->trace_buffer.data = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007760 return -ENOMEM;
7761 }
7762 tr->allocated_snapshot = allocate_snapshot;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007763
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007764 /*
7765 * Only the top level trace array gets its snapshot allocated
7766 * from the kernel command line.
7767 */
7768 allocate_snapshot = false;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007769#endif
7770 return 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007771}
7772
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04007773static void free_trace_buffer(struct trace_buffer *buf)
7774{
7775 if (buf->buffer) {
7776 ring_buffer_free(buf->buffer);
7777 buf->buffer = NULL;
7778 free_percpu(buf->data);
7779 buf->data = NULL;
7780 }
7781}
7782
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04007783static void free_trace_buffers(struct trace_array *tr)
7784{
7785 if (!tr)
7786 return;
7787
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04007788 free_trace_buffer(&tr->trace_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04007789
7790#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04007791 free_trace_buffer(&tr->max_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04007792#endif
7793}
7794
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007795static void init_trace_flags_index(struct trace_array *tr)
7796{
7797 int i;
7798
7799 /* Used by the trace options files */
7800 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
7801 tr->trace_flags_index[i] = i;
7802}
7803
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007804static void __update_tracer_options(struct trace_array *tr)
7805{
7806 struct tracer *t;
7807
7808 for (t = trace_types; t; t = t->next)
7809 add_tracer_options(tr, t);
7810}
7811
7812static void update_tracer_options(struct trace_array *tr)
7813{
7814 mutex_lock(&trace_types_lock);
7815 __update_tracer_options(tr);
7816 mutex_unlock(&trace_types_lock);
7817}
7818
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05007819static int instance_mkdir(const char *name)
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007820{
Steven Rostedt277ba042012-08-03 16:10:49 -04007821 struct trace_array *tr;
7822 int ret;
Steven Rostedt277ba042012-08-03 16:10:49 -04007823
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04007824 mutex_lock(&event_mutex);
Steven Rostedt277ba042012-08-03 16:10:49 -04007825 mutex_lock(&trace_types_lock);
7826
7827 ret = -EEXIST;
7828 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7829 if (tr->name && strcmp(tr->name, name) == 0)
7830 goto out_unlock;
7831 }
7832
7833 ret = -ENOMEM;
7834 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
7835 if (!tr)
7836 goto out_unlock;
7837
7838 tr->name = kstrdup(name, GFP_KERNEL);
7839 if (!tr->name)
7840 goto out_free_tr;
7841
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007842 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
7843 goto out_free_tr;
7844
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04007845 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007846
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007847 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
7848
Steven Rostedt277ba042012-08-03 16:10:49 -04007849 raw_spin_lock_init(&tr->start_lock);
7850
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05007851 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
7852
Steven Rostedt277ba042012-08-03 16:10:49 -04007853 tr->current_trace = &nop_trace;
7854
7855 INIT_LIST_HEAD(&tr->systems);
7856 INIT_LIST_HEAD(&tr->events);
Tom Zanussi067fe032018-01-15 20:51:56 -06007857 INIT_LIST_HEAD(&tr->hist_vars);
Steven Rostedt277ba042012-08-03 16:10:49 -04007858
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007859 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
Steven Rostedt277ba042012-08-03 16:10:49 -04007860 goto out_free_tr;
7861
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007862 tr->dir = tracefs_create_dir(name, trace_instance_dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04007863 if (!tr->dir)
7864 goto out_free_tr;
7865
7866 ret = event_trace_add_tracer(tr->dir, tr);
Alexander Z Lam609e85a2013-07-10 17:34:34 -07007867 if (ret) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007868 tracefs_remove_recursive(tr->dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04007869 goto out_free_tr;
Alexander Z Lam609e85a2013-07-10 17:34:34 -07007870 }
Steven Rostedt277ba042012-08-03 16:10:49 -04007871
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007872 ftrace_init_trace_array(tr);
7873
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007874 init_tracer_tracefs(tr, tr->dir);
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007875 init_trace_flags_index(tr);
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007876 __update_tracer_options(tr);
Steven Rostedt277ba042012-08-03 16:10:49 -04007877
7878 list_add(&tr->list, &ftrace_trace_arrays);
7879
7880 mutex_unlock(&trace_types_lock);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04007881 mutex_unlock(&event_mutex);
Steven Rostedt277ba042012-08-03 16:10:49 -04007882
7883 return 0;
7884
7885 out_free_tr:
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04007886 free_trace_buffers(tr);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007887 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt277ba042012-08-03 16:10:49 -04007888 kfree(tr->name);
7889 kfree(tr);
7890
7891 out_unlock:
7892 mutex_unlock(&trace_types_lock);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04007893 mutex_unlock(&event_mutex);
Steven Rostedt277ba042012-08-03 16:10:49 -04007894
7895 return ret;
7896
7897}
7898
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05007899static int instance_rmdir(const char *name)
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007900{
7901 struct trace_array *tr;
7902 int found = 0;
7903 int ret;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007904 int i;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007905
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04007906 mutex_lock(&event_mutex);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007907 mutex_lock(&trace_types_lock);
7908
7909 ret = -ENODEV;
7910 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7911 if (tr->name && strcmp(tr->name, name) == 0) {
7912 found = 1;
7913 break;
7914 }
7915 }
7916 if (!found)
7917 goto out_unlock;
7918
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007919 ret = -EBUSY;
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05007920 if (tr->ref || (tr->current_trace && tr->current_trace->ref))
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007921 goto out_unlock;
7922
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007923 list_del(&tr->list);
7924
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04007925 /* Disable all the flags that were enabled coming in */
7926 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
7927 if ((1 << i) & ZEROED_TRACE_FLAGS)
7928 set_tracer_flag(tr, 1 << i, 0);
7929 }
7930
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05007931 tracing_set_nop(tr);
Naveen N. Raoa0e63692017-05-16 23:21:26 +05307932 clear_ftrace_function_probes(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007933 event_trace_del_tracer(tr);
Namhyung Kimd879d0b2017-04-17 11:44:27 +09007934 ftrace_clear_pids(tr);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05007935 ftrace_destroy_function_files(tr);
Jiaxing Wang681a4a22015-10-18 19:58:08 +08007936 tracefs_remove_recursive(tr->dir);
Steven Rostedt (Red Hat)a9fcaaa2014-06-06 23:17:28 -04007937 free_trace_buffers(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007938
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007939 for (i = 0; i < tr->nr_topts; i++) {
7940 kfree(tr->topts[i].topts);
7941 }
7942 kfree(tr->topts);
7943
Chunyu Hudb9108e02017-07-20 18:36:09 +08007944 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007945 kfree(tr->name);
7946 kfree(tr);
7947
7948 ret = 0;
7949
7950 out_unlock:
7951 mutex_unlock(&trace_types_lock);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04007952 mutex_unlock(&event_mutex);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007953
7954 return ret;
7955}
7956
Steven Rostedt277ba042012-08-03 16:10:49 -04007957static __init void create_trace_instances(struct dentry *d_tracer)
7958{
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05007959 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
7960 instance_mkdir,
7961 instance_rmdir);
Steven Rostedt277ba042012-08-03 16:10:49 -04007962 if (WARN_ON(!trace_instance_dir))
7963 return;
Steven Rostedt277ba042012-08-03 16:10:49 -04007964}
7965
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007966static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007967init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007968{
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04007969 struct trace_event_file *file;
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05007970 int cpu;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007971
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05007972 trace_create_file("available_tracers", 0444, d_tracer,
7973 tr, &show_traces_fops);
7974
7975 trace_create_file("current_tracer", 0644, d_tracer,
7976 tr, &set_tracer_fops);
7977
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007978 trace_create_file("tracing_cpumask", 0644, d_tracer,
7979 tr, &tracing_cpumask_fops);
7980
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007981 trace_create_file("trace_options", 0644, d_tracer,
7982 tr, &tracing_iter_fops);
7983
7984 trace_create_file("trace", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007985 tr, &tracing_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007986
7987 trace_create_file("trace_pipe", 0444, d_tracer,
Oleg Nesterov15544202013-07-23 17:25:57 +02007988 tr, &tracing_pipe_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007989
7990 trace_create_file("buffer_size_kb", 0644, d_tracer,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02007991 tr, &tracing_entries_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007992
7993 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
7994 tr, &tracing_total_entries_fops);
7995
Wang YanQing238ae932013-05-26 16:52:01 +08007996 trace_create_file("free_buffer", 0200, d_tracer,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007997 tr, &tracing_free_buffer_fops);
7998
7999 trace_create_file("trace_marker", 0220, d_tracer,
8000 tr, &tracing_mark_fops);
8001
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04008002 file = __find_event_file(tr, "ftrace", "print");
8003 if (file && file->dir)
8004 trace_create_file("trigger", 0644, file->dir, file,
8005 &event_trigger_fops);
8006 tr->trace_marker_file = file;
8007
Steven Rostedtfa32e852016-07-06 15:25:08 -04008008 trace_create_file("trace_marker_raw", 0220, d_tracer,
8009 tr, &tracing_mark_raw_fops);
8010
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008011 trace_create_file("trace_clock", 0644, d_tracer, tr,
8012 &trace_clock_fops);
8013
8014 trace_create_file("tracing_on", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02008015 tr, &rb_simple_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008016
Tom Zanussi2c1ea602018-01-15 20:51:41 -06008017 trace_create_file("timestamp_mode", 0444, d_tracer, tr,
8018 &trace_time_stamp_mode_fops);
8019
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05008020 tr->buffer_percent = 1;
8021
8022 trace_create_file("buffer_percent", 0444, d_tracer,
8023 tr, &buffer_percent_fops);
8024
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04008025 create_trace_options_dir(tr);
8026
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04008027#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05008028 trace_create_file("tracing_max_latency", 0644, d_tracer,
8029 &tr->max_latency, &tracing_max_lat_fops);
8030#endif
8031
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05008032 if (ftrace_create_function_files(tr, d_tracer))
8033 WARN(1, "Could not allocate function filter files");
8034
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008035#ifdef CONFIG_TRACER_SNAPSHOT
8036 trace_create_file("snapshot", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02008037 tr, &snapshot_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008038#endif
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008039
8040 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008041 tracing_init_tracefs_percpu(tr, cpu);
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008042
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04008043 ftrace_init_tracefs(tr, d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008044}
8045
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13008046static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008047{
8048 struct vfsmount *mnt;
8049 struct file_system_type *type;
8050
8051 /*
8052 * To maintain backward compatibility for tools that mount
8053 * debugfs to get to the tracing facility, tracefs is automatically
8054 * mounted to the debugfs/tracing directory.
8055 */
8056 type = get_fs_type("tracefs");
8057 if (!type)
8058 return NULL;
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13008059 mnt = vfs_submount(mntpt, type, "tracefs", NULL);
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008060 put_filesystem(type);
8061 if (IS_ERR(mnt))
8062 return NULL;
8063 mntget(mnt);
8064
8065 return mnt;
8066}
8067
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008068/**
8069 * tracing_init_dentry - initialize top level trace array
8070 *
8071 * This is called when creating files or directories in the tracing
8072 * directory. It is called via fs_initcall() by any of the boot up code
8073 * and expects to return the dentry of the top level tracing directory.
8074 */
8075struct dentry *tracing_init_dentry(void)
8076{
8077 struct trace_array *tr = &global_trace;
8078
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008079 /* The top level trace array uses NULL as parent */
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008080 if (tr->dir)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008081 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008082
Jiaxing Wang8b129192015-11-06 16:04:16 +08008083 if (WARN_ON(!tracefs_initialized()) ||
8084 (IS_ENABLED(CONFIG_DEBUG_FS) &&
8085 WARN_ON(!debugfs_initialized())))
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008086 return ERR_PTR(-ENODEV);
8087
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008088 /*
8089 * As there may still be users that expect the tracing
8090 * files to exist in debugfs/tracing, we must automount
8091 * the tracefs file system there, so older tools still
8092 * work with the newer kerenl.
8093 */
8094 tr->dir = debugfs_create_automount("tracing", NULL,
8095 trace_automount, NULL);
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008096 if (!tr->dir) {
8097 pr_warn_once("Could not create debugfs directory 'tracing'\n");
8098 return ERR_PTR(-ENOMEM);
8099 }
8100
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008101 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008102}
8103
Jeremy Linton00f4b652017-05-31 16:56:43 -05008104extern struct trace_eval_map *__start_ftrace_eval_maps[];
8105extern struct trace_eval_map *__stop_ftrace_eval_maps[];
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008106
Jeremy Linton5f60b352017-05-31 16:56:47 -05008107static void __init trace_eval_init(void)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008108{
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008109 int len;
8110
Jeremy Linton02fd7f62017-05-31 16:56:42 -05008111 len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008112 trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008113}
8114
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008115#ifdef CONFIG_MODULES
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008116static void trace_module_add_evals(struct module *mod)
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008117{
Jeremy Linton99be6472017-05-31 16:56:44 -05008118 if (!mod->num_trace_evals)
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008119 return;
8120
8121 /*
8122 * Modules with bad taint do not have events created, do
8123 * not bother with enums either.
8124 */
8125 if (trace_module_has_bad_taint(mod))
8126 return;
8127
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008128 trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008129}
8130
Jeremy Linton681bec02017-05-31 16:56:53 -05008131#ifdef CONFIG_TRACE_EVAL_MAP_FILE
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008132static void trace_module_remove_evals(struct module *mod)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008133{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05008134 union trace_eval_map_item *map;
8135 union trace_eval_map_item **last = &trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008136
Jeremy Linton99be6472017-05-31 16:56:44 -05008137 if (!mod->num_trace_evals)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008138 return;
8139
Jeremy Linton1793ed92017-05-31 16:56:46 -05008140 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008141
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05008142 map = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008143
8144 while (map) {
8145 if (map->head.mod == mod)
8146 break;
Jeremy Linton5f60b352017-05-31 16:56:47 -05008147 map = trace_eval_jmp_to_tail(map);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008148 last = &map->tail.next;
8149 map = map->tail.next;
8150 }
8151 if (!map)
8152 goto out;
8153
Jeremy Linton5f60b352017-05-31 16:56:47 -05008154 *last = trace_eval_jmp_to_tail(map)->tail.next;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008155 kfree(map);
8156 out:
Jeremy Linton1793ed92017-05-31 16:56:46 -05008157 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008158}
8159#else
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008160static inline void trace_module_remove_evals(struct module *mod) { }
Jeremy Linton681bec02017-05-31 16:56:53 -05008161#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008162
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008163static int trace_module_notify(struct notifier_block *self,
8164 unsigned long val, void *data)
8165{
8166 struct module *mod = data;
8167
8168 switch (val) {
8169 case MODULE_STATE_COMING:
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008170 trace_module_add_evals(mod);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008171 break;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008172 case MODULE_STATE_GOING:
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008173 trace_module_remove_evals(mod);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008174 break;
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008175 }
8176
8177 return 0;
8178}
8179
8180static struct notifier_block trace_module_nb = {
8181 .notifier_call = trace_module_notify,
8182 .priority = 0,
8183};
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008184#endif /* CONFIG_MODULES */
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008185
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008186static __init int tracer_init_tracefs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008187{
8188 struct dentry *d_tracer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008189
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08008190 trace_access_lock_init();
8191
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008192 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05008193 if (IS_ERR(d_tracer))
Namhyung Kimed6f1c92013-04-10 09:18:12 +09008194 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008195
Steven Rostedt (VMware)58b92542018-05-08 15:09:27 -04008196 event_trace_init();
8197
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008198 init_tracer_tracefs(&global_trace, d_tracer);
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04008199 ftrace_init_tracefs_toplevel(&global_trace, d_tracer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008200
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008201 trace_create_file("tracing_thresh", 0644, d_tracer,
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04008202 &global_trace, &tracing_thresh_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008203
Li Zefan339ae5d2009-04-17 10:34:30 +08008204 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008205 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02008206
Avadh Patel69abe6a2009-04-10 16:04:48 -04008207 trace_create_file("saved_cmdlines", 0444, d_tracer,
8208 NULL, &tracing_saved_cmdlines_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03008209
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09008210 trace_create_file("saved_cmdlines_size", 0644, d_tracer,
8211 NULL, &tracing_saved_cmdlines_size_fops);
8212
Michael Sartain99c621d2017-07-05 22:07:15 -06008213 trace_create_file("saved_tgids", 0444, d_tracer,
8214 NULL, &tracing_saved_tgids_fops);
8215
Jeremy Linton5f60b352017-05-31 16:56:47 -05008216 trace_eval_init();
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008217
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008218 trace_create_eval_file(d_tracer);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008219
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008220#ifdef CONFIG_MODULES
8221 register_module_notifier(&trace_module_nb);
8222#endif
8223
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008224#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008225 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
8226 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008227#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01008228
Steven Rostedt277ba042012-08-03 16:10:49 -04008229 create_trace_instances(d_tracer);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09008230
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008231 update_tracer_options(&global_trace);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05008232
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01008233 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008234}
8235
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008236static int trace_panic_handler(struct notifier_block *this,
8237 unsigned long event, void *unused)
8238{
Steven Rostedt944ac422008-10-23 19:26:08 -04008239 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008240 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008241 return NOTIFY_OK;
8242}
8243
8244static struct notifier_block trace_panic_notifier = {
8245 .notifier_call = trace_panic_handler,
8246 .next = NULL,
8247 .priority = 150 /* priority: INT_MAX >= x >= 0 */
8248};
8249
8250static int trace_die_handler(struct notifier_block *self,
8251 unsigned long val,
8252 void *data)
8253{
8254 switch (val) {
8255 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04008256 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008257 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008258 break;
8259 default:
8260 break;
8261 }
8262 return NOTIFY_OK;
8263}
8264
8265static struct notifier_block trace_die_notifier = {
8266 .notifier_call = trace_die_handler,
8267 .priority = 200
8268};
8269
8270/*
8271 * printk is set to max of 1024, we really don't need it that big.
8272 * Nothing should be printing 1000 characters anyway.
8273 */
8274#define TRACE_MAX_PRINT 1000
8275
8276/*
8277 * Define here KERN_TRACE so that we have one place to modify
8278 * it if we decide to change what log level the ftrace dump
8279 * should be at.
8280 */
Steven Rostedt428aee12009-01-14 12:24:42 -05008281#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008282
Jason Wessel955b61e2010-08-05 09:22:23 -05008283void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008284trace_printk_seq(struct trace_seq *s)
8285{
8286 /* Probably should print a warning here. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04008287 if (s->seq.len >= TRACE_MAX_PRINT)
8288 s->seq.len = TRACE_MAX_PRINT;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008289
Steven Rostedt (Red Hat)820b75f2014-11-19 10:56:41 -05008290 /*
8291 * More paranoid code. Although the buffer size is set to
8292 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
8293 * an extra layer of protection.
8294 */
8295 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
8296 s->seq.len = s->seq.size - 1;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008297
8298 /* should be zero ended, but we are paranoid. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04008299 s->buffer[s->seq.len] = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008300
8301 printk(KERN_TRACE "%s", s->buffer);
8302
Steven Rostedtf9520752009-03-02 14:04:40 -05008303 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008304}
8305
Jason Wessel955b61e2010-08-05 09:22:23 -05008306void trace_init_global_iter(struct trace_iterator *iter)
8307{
8308 iter->tr = &global_trace;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008309 iter->trace = iter->tr->current_trace;
Steven Rostedtae3b5092013-01-23 15:22:59 -05008310 iter->cpu_file = RING_BUFFER_ALL_CPUS;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05008311 iter->trace_buffer = &global_trace.trace_buffer;
Cody P Schaferb2f974d2013-10-23 11:49:57 -07008312
8313 if (iter->trace && iter->trace->open)
8314 iter->trace->open(iter);
8315
8316 /* Annotate start of buffers if we had overruns */
8317 if (ring_buffer_overruns(iter->trace_buffer->buffer))
8318 iter->iter_flags |= TRACE_FILE_ANNOTATE;
8319
8320 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
8321 if (trace_clocks[iter->tr->clock_id].in_ns)
8322 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
Jason Wessel955b61e2010-08-05 09:22:23 -05008323}
8324
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008325void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008326{
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008327 /* use static because iter can be a bit big for the stack */
8328 static struct trace_iterator iter;
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008329 static atomic_t dump_running;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008330 struct trace_array *tr = &global_trace;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01008331 unsigned int old_userobj;
Steven Rostedtd7690412008-10-01 00:29:53 -04008332 unsigned long flags;
8333 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008334
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008335 /* Only allow one dump user at a time. */
8336 if (atomic_inc_return(&dump_running) != 1) {
8337 atomic_dec(&dump_running);
8338 return;
Steven Rostedte0a413f2011-09-29 21:26:16 -04008339 }
8340
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008341 /*
8342 * Always turn off tracing when we dump.
8343 * We don't need to show trace output of what happens
8344 * between multiple crashes.
8345 *
8346 * If the user does a sysrq-z, then they can re-enable
8347 * tracing with echo 1 > tracing_on.
8348 */
8349 tracing_off();
8350
8351 local_irq_save(flags);
Petr Mladek03fc7f92018-06-27 16:20:28 +02008352 printk_nmi_direct_enter();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008353
Jovi Zhang38dbe0b2013-01-25 18:03:07 +08008354 /* Simulate the iterator */
Jason Wessel955b61e2010-08-05 09:22:23 -05008355 trace_init_global_iter(&iter);
8356
Steven Rostedtd7690412008-10-01 00:29:53 -04008357 for_each_tracing_cpu(cpu) {
Umesh Tiwari5e2d5ef2015-06-22 16:55:06 +05308358 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04008359 }
8360
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008361 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01008362
Török Edwinb54d3de2008-11-22 13:28:48 +02008363 /* don't look at user memory in panic mode */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008364 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
Török Edwinb54d3de2008-11-22 13:28:48 +02008365
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008366 switch (oops_dump_mode) {
8367 case DUMP_ALL:
Steven Rostedtae3b5092013-01-23 15:22:59 -05008368 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008369 break;
8370 case DUMP_ORIG:
8371 iter.cpu_file = raw_smp_processor_id();
8372 break;
8373 case DUMP_NONE:
8374 goto out_enable;
8375 default:
8376 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
Steven Rostedtae3b5092013-01-23 15:22:59 -05008377 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008378 }
8379
8380 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008381
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008382 /* Did function tracer already get disabled? */
8383 if (ftrace_is_dead()) {
8384 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
8385 printk("# MAY BE MISSING FUNCTION EVENTS\n");
8386 }
8387
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008388 /*
8389 * We need to stop all tracing on all CPUS to read the
8390 * the next buffer. This is a bit expensive, but is
8391 * not done often. We fill all what we can read,
8392 * and then release the locks again.
8393 */
8394
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008395 while (!trace_empty(&iter)) {
8396
8397 if (!cnt)
8398 printk(KERN_TRACE "---------------------------------\n");
8399
8400 cnt++;
8401
8402 /* reset all but tr, trace, and overruns */
8403 memset(&iter.seq, 0,
8404 sizeof(struct trace_iterator) -
8405 offsetof(struct trace_iterator, seq));
8406 iter.iter_flags |= TRACE_FILE_LAT_FMT;
8407 iter.pos = -1;
8408
Jason Wessel955b61e2010-08-05 09:22:23 -05008409 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08008410 int ret;
8411
8412 ret = print_trace_line(&iter);
8413 if (ret != TRACE_TYPE_NO_CONSUME)
8414 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008415 }
Steven Rostedtb892e5c2012-03-01 22:06:48 -05008416 touch_nmi_watchdog();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008417
8418 trace_printk_seq(&iter.seq);
8419 }
8420
8421 if (!cnt)
8422 printk(KERN_TRACE " (ftrace buffer empty)\n");
8423 else
8424 printk(KERN_TRACE "---------------------------------\n");
8425
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008426 out_enable:
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008427 tr->trace_flags |= old_userobj;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01008428
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008429 for_each_tracing_cpu(cpu) {
8430 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01008431 }
Petr Mladek03fc7f92018-06-27 16:20:28 +02008432 atomic_dec(&dump_running);
8433 printk_nmi_direct_exit();
Steven Rostedtcd891ae2009-04-28 11:39:34 -04008434 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008435}
Paul E. McKenneya8eecf22011-10-02 11:01:15 -07008436EXPORT_SYMBOL_GPL(ftrace_dump);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01008437
Tom Zanussi7e465ba2017-09-22 14:58:20 -05008438int trace_run_command(const char *buf, int (*createfn)(int, char **))
8439{
8440 char **argv;
8441 int argc, ret;
8442
8443 argc = 0;
8444 ret = 0;
8445 argv = argv_split(GFP_KERNEL, buf, &argc);
8446 if (!argv)
8447 return -ENOMEM;
8448
8449 if (argc)
8450 ret = createfn(argc, argv);
8451
8452 argv_free(argv);
8453
8454 return ret;
8455}
8456
8457#define WRITE_BUFSIZE 4096
8458
8459ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
8460 size_t count, loff_t *ppos,
8461 int (*createfn)(int, char **))
8462{
8463 char *kbuf, *buf, *tmp;
8464 int ret = 0;
8465 size_t done = 0;
8466 size_t size;
8467
8468 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
8469 if (!kbuf)
8470 return -ENOMEM;
8471
8472 while (done < count) {
8473 size = count - done;
8474
8475 if (size >= WRITE_BUFSIZE)
8476 size = WRITE_BUFSIZE - 1;
8477
8478 if (copy_from_user(kbuf, buffer + done, size)) {
8479 ret = -EFAULT;
8480 goto out;
8481 }
8482 kbuf[size] = '\0';
8483 buf = kbuf;
8484 do {
8485 tmp = strchr(buf, '\n');
8486 if (tmp) {
8487 *tmp = '\0';
8488 size = tmp - buf + 1;
8489 } else {
8490 size = strlen(buf);
8491 if (done + size < count) {
8492 if (buf != kbuf)
8493 break;
8494 /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
8495 pr_warn("Line length is too long: Should be less than %d\n",
8496 WRITE_BUFSIZE - 2);
8497 ret = -EINVAL;
8498 goto out;
8499 }
8500 }
8501 done += size;
8502
8503 /* Remove comments */
8504 tmp = strchr(buf, '#');
8505
8506 if (tmp)
8507 *tmp = '\0';
8508
8509 ret = trace_run_command(buf, createfn);
8510 if (ret)
8511 goto out;
8512 buf += size;
8513
8514 } while (done < count);
8515 }
8516 ret = done;
8517
8518out:
8519 kfree(kbuf);
8520
8521 return ret;
8522}
8523
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008524__init static int tracer_alloc_buffers(void)
8525{
Steven Rostedt73c51622009-03-11 13:42:01 -04008526 int ring_buf_size;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308527 int ret = -ENOMEM;
8528
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04008529 /*
8530 * Make sure we don't accidently add more trace options
8531 * than we have bits for.
8532 */
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008533 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04008534
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308535 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
8536 goto out;
8537
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008538 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308539 goto out_free_buffer_mask;
8540
Steven Rostedt07d777f2011-09-22 14:01:55 -04008541 /* Only allocate trace_printk buffers if a trace_printk exists */
8542 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
Steven Rostedt81698832012-10-11 10:15:05 -04008543 /* Must be called before global_trace.buffer is allocated */
Steven Rostedt07d777f2011-09-22 14:01:55 -04008544 trace_printk_init_buffers();
8545
Steven Rostedt73c51622009-03-11 13:42:01 -04008546 /* To save memory, keep the ring buffer size to its minimum */
8547 if (ring_buffer_expanded)
8548 ring_buf_size = trace_buf_size;
8549 else
8550 ring_buf_size = 1;
8551
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308552 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008553 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008554
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008555 raw_spin_lock_init(&global_trace.start_lock);
8556
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01008557 /*
8558 * The prepare callbacks allocates some memory for the ring buffer. We
8559 * don't free the buffer if the if the CPU goes down. If we were to free
8560 * the buffer, then the user would lose any trace that was in the
8561 * buffer. The memory will be removed once the "instance" is removed.
8562 */
8563 ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
8564 "trace/RB:preapre", trace_rb_cpu_prepare,
8565 NULL);
8566 if (ret < 0)
8567 goto out_free_cpumask;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04008568 /* Used for event triggers */
Dan Carpenter147d88e02017-08-01 14:02:01 +03008569 ret = -ENOMEM;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04008570 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
8571 if (!temp_buffer)
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01008572 goto out_rm_hp_state;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04008573
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09008574 if (trace_create_savedcmd() < 0)
8575 goto out_free_temp_buffer;
8576
Steven Rostedtab464282008-05-12 21:21:00 +02008577 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008578 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04008579 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
8580 WARN_ON(1);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09008581 goto out_free_savedcmd;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04008582 }
Steven Rostedta7603ff2012-08-06 16:24:11 -04008583
Steven Rostedt499e5472012-02-22 15:50:28 -05008584 if (global_trace.buffer_disabled)
8585 tracing_off();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04008586
Steven Rostedte1e232c2014-02-10 23:38:46 -05008587 if (trace_boot_clock) {
8588 ret = tracing_set_clock(&global_trace, trace_boot_clock);
8589 if (ret < 0)
Joe Perchesa395d6a2016-03-22 14:28:09 -07008590 pr_warn("Trace clock %s not defined, going back to default\n",
8591 trace_boot_clock);
Steven Rostedte1e232c2014-02-10 23:38:46 -05008592 }
8593
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04008594 /*
8595 * register_tracer() might reference current_trace, so it
8596 * needs to be set before we register anything. This is
8597 * just a bootstrap of current_trace anyway.
8598 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008599 global_trace.current_trace = &nop_trace;
8600
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05008601 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
8602
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05008603 ftrace_init_global_array_ops(&global_trace);
8604
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008605 init_trace_flags_index(&global_trace);
8606
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04008607 register_tracer(&nop_trace);
8608
Steven Rostedt (VMware)dbeafd02017-03-03 13:48:42 -05008609 /* Function tracing may start here (via kernel command line) */
8610 init_function_trace();
8611
Steven Rostedt60a11772008-05-12 21:20:44 +02008612 /* All seems OK, enable tracing */
8613 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04008614
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008615 atomic_notifier_chain_register(&panic_notifier_list,
8616 &trace_panic_notifier);
8617
8618 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01008619
Steven Rostedtae63b31e2012-05-03 23:09:03 -04008620 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
8621
8622 INIT_LIST_HEAD(&global_trace.systems);
8623 INIT_LIST_HEAD(&global_trace.events);
Tom Zanussi067fe032018-01-15 20:51:56 -06008624 INIT_LIST_HEAD(&global_trace.hist_vars);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04008625 list_add(&global_trace.list, &ftrace_trace_arrays);
8626
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08008627 apply_trace_boot_options();
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04008628
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04008629 register_snapshot_cmd();
8630
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01008631 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008632
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09008633out_free_savedcmd:
8634 free_saved_cmdlines_buffer(savedcmd);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04008635out_free_temp_buffer:
8636 ring_buffer_free(temp_buffer);
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01008637out_rm_hp_state:
8638 cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308639out_free_cpumask:
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008640 free_cpumask_var(global_trace.tracing_cpumask);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308641out_free_buffer_mask:
8642 free_cpumask_var(tracing_buffer_mask);
8643out:
8644 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008645}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05008646
Steven Rostedt (VMware)e725c732017-03-03 13:37:33 -05008647void __init early_trace_init(void)
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05008648{
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05008649 if (tracepoint_printk) {
8650 tracepoint_print_iter =
8651 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
8652 if (WARN_ON(!tracepoint_print_iter))
8653 tracepoint_printk = 0;
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05008654 else
8655 static_key_enable(&tracepoint_printk_key.key);
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05008656 }
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05008657 tracer_alloc_buffers();
Steven Rostedt (VMware)e725c732017-03-03 13:37:33 -05008658}
8659
8660void __init trace_init(void)
8661{
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008662 trace_event_init();
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05008663}
8664
Steven Rostedtb2821ae2009-02-02 21:38:32 -05008665__init static int clear_boot_tracer(void)
8666{
8667 /*
8668 * The default tracer at boot buffer is an init section.
8669 * This function is called in lateinit. If we did not
8670 * find the boot tracer, then clear it out, to prevent
8671 * later registration from accessing the buffer that is
8672 * about to be freed.
8673 */
8674 if (!default_bootup_tracer)
8675 return 0;
8676
8677 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
8678 default_bootup_tracer);
8679 default_bootup_tracer = NULL;
8680
8681 return 0;
8682}
8683
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008684fs_initcall(tracer_init_tracefs);
Steven Rostedt (VMware)4bb0f0e2017-08-01 12:01:52 -04008685late_initcall_sync(clear_boot_tracer);
Chris Wilson3fd49c92018-03-30 16:01:31 +01008686
8687#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
8688__init static int tracing_set_default_clock(void)
8689{
8690 /* sched_clock_stable() is determined in late_initcall */
Chris Wilson5125eee2018-04-04 22:24:50 +01008691 if (!trace_boot_clock && !sched_clock_stable()) {
Chris Wilson3fd49c92018-03-30 16:01:31 +01008692 printk(KERN_WARNING
8693 "Unstable clock detected, switching default tracing clock to \"global\"\n"
8694 "If you want to keep using the local clock, then add:\n"
8695 " \"trace_clock=local\"\n"
8696 "on the kernel command line\n");
8697 tracing_set_clock(&global_trace, "global");
8698 }
8699
8700 return 0;
8701}
8702late_initcall_sync(tracing_set_default_clock);
8703#endif