blob: 46f68fad637393fc589a62c27f3b56ad216ca367 [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);
Wenwen Wang91862cc2019-04-19 21:22:59 -0500499 if (!pid_list) {
500 trace_parser_put(&parser);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400501 return -ENOMEM;
Wenwen Wang91862cc2019-04-19 21:22:59 -0500502 }
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400503
504 pid_list->pid_max = READ_ONCE(pid_max);
505
506 /* Only truncating will shrink pid_max */
507 if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
508 pid_list->pid_max = filtered_pids->pid_max;
509
510 pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
511 if (!pid_list->pids) {
Wenwen Wang91862cc2019-04-19 21:22:59 -0500512 trace_parser_put(&parser);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400513 kfree(pid_list);
514 return -ENOMEM;
515 }
516
517 if (filtered_pids) {
518 /* copy the current bits to the new max */
Wei Yongjun67f20b02016-07-04 15:10:04 +0000519 for_each_set_bit(pid, filtered_pids->pids,
520 filtered_pids->pid_max) {
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400521 set_bit(pid, pid_list->pids);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400522 nr_pids++;
523 }
524 }
525
526 while (cnt > 0) {
527
528 pos = 0;
529
530 ret = trace_get_user(&parser, ubuf, cnt, &pos);
531 if (ret < 0 || !trace_parser_loaded(&parser))
532 break;
533
534 read += ret;
535 ubuf += ret;
536 cnt -= ret;
537
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400538 ret = -EINVAL;
539 if (kstrtoul(parser.buffer, 0, &val))
540 break;
541 if (val >= pid_list->pid_max)
542 break;
543
544 pid = (pid_t)val;
545
546 set_bit(pid, pid_list->pids);
547 nr_pids++;
548
549 trace_parser_clear(&parser);
550 ret = 0;
551 }
552 trace_parser_put(&parser);
553
554 if (ret < 0) {
555 trace_free_pid_list(pid_list);
556 return ret;
557 }
558
559 if (!nr_pids) {
560 /* Cleared the list of pids */
561 trace_free_pid_list(pid_list);
562 read = ret;
563 pid_list = NULL;
564 }
565
566 *new_pid_list = pid_list;
567
568 return read;
569}
570
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100571static u64 buffer_ftrace_now(struct trace_buffer *buf, int cpu)
Steven Rostedt37886f62009-03-17 17:22:06 -0400572{
573 u64 ts;
574
575 /* Early boot up does not have a buffer yet */
Alexander Z Lam94571582013-08-02 18:36:16 -0700576 if (!buf->buffer)
Steven Rostedt37886f62009-03-17 17:22:06 -0400577 return trace_clock_local();
578
Alexander Z Lam94571582013-08-02 18:36:16 -0700579 ts = ring_buffer_time_stamp(buf->buffer, cpu);
580 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
Steven Rostedt37886f62009-03-17 17:22:06 -0400581
582 return ts;
583}
584
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100585u64 ftrace_now(int cpu)
Alexander Z Lam94571582013-08-02 18:36:16 -0700586{
587 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
588}
589
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400590/**
591 * tracing_is_enabled - Show if global_trace has been disabled
592 *
593 * Shows if the global trace has been enabled or not. It uses the
594 * mirror flag "buffer_disabled" to be used in fast paths such as for
595 * the irqsoff tracer. But it may be inaccurate due to races. If you
596 * need to know the accurate state, use tracing_is_on() which is a little
597 * slower, but accurate.
598 */
Steven Rostedt90369902008-11-05 16:05:44 -0500599int tracing_is_enabled(void)
600{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400601 /*
602 * For quick access (irqsoff uses this in fast path), just
603 * return the mirror variable of the state of the ring buffer.
604 * It's a little racy, but we don't really care.
605 */
606 smp_rmb();
607 return !global_trace.buffer_disabled;
Steven Rostedt90369902008-11-05 16:05:44 -0500608}
609
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200610/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400611 * trace_buf_size is the size in bytes that is allocated
612 * for a buffer. Note, the number of bytes is always rounded
613 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400614 *
615 * This number is purposely set to a low number of 16384.
616 * If the dump on oops happens, it will be much appreciated
617 * to not have to wait for all that output. Anyway this can be
618 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200619 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400620#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400621
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400622static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200623
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200624/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200625static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200626
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200627/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200628 * trace_types_lock is used to protect the trace_types list.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200629 */
Alexander Z Lama8227412013-07-01 19:37:54 -0700630DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200631
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800632/*
633 * serialize the access of the ring buffer
634 *
635 * ring buffer serializes readers, but it is low level protection.
636 * The validity of the events (which returns by ring_buffer_peek() ..etc)
637 * are not protected by ring buffer.
638 *
639 * The content of events may become garbage if we allow other process consumes
640 * these events concurrently:
641 * A) the page of the consumed events may become a normal page
642 * (not reader page) in ring buffer, and this page will be rewrited
643 * by events producer.
644 * B) The page of the consumed events may become a page for splice_read,
645 * and this page will be returned to system.
646 *
647 * These primitives allow multi process access to different cpu ring buffer
648 * concurrently.
649 *
650 * These primitives don't distinguish read-only and read-consume access.
651 * Multi read-only access are also serialized.
652 */
653
654#ifdef CONFIG_SMP
655static DECLARE_RWSEM(all_cpu_access_lock);
656static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
657
658static inline void trace_access_lock(int cpu)
659{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500660 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800661 /* gain it for accessing the whole ring buffer. */
662 down_write(&all_cpu_access_lock);
663 } else {
664 /* gain it for accessing a cpu ring buffer. */
665
Steven Rostedtae3b5092013-01-23 15:22:59 -0500666 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800667 down_read(&all_cpu_access_lock);
668
669 /* Secondly block other access to this @cpu ring buffer. */
670 mutex_lock(&per_cpu(cpu_access_lock, cpu));
671 }
672}
673
674static inline void trace_access_unlock(int cpu)
675{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500676 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800677 up_write(&all_cpu_access_lock);
678 } else {
679 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
680 up_read(&all_cpu_access_lock);
681 }
682}
683
684static inline void trace_access_lock_init(void)
685{
686 int cpu;
687
688 for_each_possible_cpu(cpu)
689 mutex_init(&per_cpu(cpu_access_lock, cpu));
690}
691
692#else
693
694static DEFINE_MUTEX(access_lock);
695
696static inline void trace_access_lock(int cpu)
697{
698 (void)cpu;
699 mutex_lock(&access_lock);
700}
701
702static inline void trace_access_unlock(int cpu)
703{
704 (void)cpu;
705 mutex_unlock(&access_lock);
706}
707
708static inline void trace_access_lock_init(void)
709{
710}
711
712#endif
713
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400714#ifdef CONFIG_STACKTRACE
715static void __ftrace_trace_stack(struct ring_buffer *buffer,
716 unsigned long flags,
717 int skip, int pc, struct pt_regs *regs);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400718static inline void ftrace_trace_stack(struct trace_array *tr,
719 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -0400720 unsigned long flags,
721 int skip, int pc, struct pt_regs *regs);
Steven Rostedt (Red Hat)ca475e82015-09-28 09:41:11 -0400722
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400723#else
724static inline void __ftrace_trace_stack(struct ring_buffer *buffer,
725 unsigned long flags,
726 int skip, int pc, struct pt_regs *regs)
727{
728}
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400729static inline void ftrace_trace_stack(struct trace_array *tr,
730 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -0400731 unsigned long flags,
732 int skip, int pc, struct pt_regs *regs)
Steven Rostedt (Red Hat)ca475e82015-09-28 09:41:11 -0400733{
734}
735
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400736#endif
737
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500738static __always_inline void
739trace_event_setup(struct ring_buffer_event *event,
740 int type, unsigned long flags, int pc)
741{
742 struct trace_entry *ent = ring_buffer_event_data(event);
743
744 tracing_generic_entry_update(ent, flags, pc);
745 ent->type = type;
746}
747
748static __always_inline struct ring_buffer_event *
749__trace_buffer_lock_reserve(struct ring_buffer *buffer,
750 int type,
751 unsigned long len,
752 unsigned long flags, int pc)
753{
754 struct ring_buffer_event *event;
755
756 event = ring_buffer_lock_reserve(buffer, len);
757 if (event != NULL)
758 trace_event_setup(event, type, flags, pc);
759
760 return event;
761}
762
Steven Rostedt (VMware)2290f2c2017-04-20 11:46:03 -0400763void tracer_tracing_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400764{
765 if (tr->trace_buffer.buffer)
766 ring_buffer_record_on(tr->trace_buffer.buffer);
767 /*
768 * This flag is looked at when buffers haven't been allocated
769 * yet, or by some tracers (like irqsoff), that just want to
770 * know if the ring buffer has been disabled, but it can handle
771 * races of where it gets disabled but we still do a record.
772 * As the check is in the fast path of the tracers, it is more
773 * important to be fast than accurate.
774 */
775 tr->buffer_disabled = 0;
776 /* Make the flag seen by readers */
777 smp_wmb();
778}
779
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200780/**
Steven Rostedt499e5472012-02-22 15:50:28 -0500781 * tracing_on - enable tracing buffers
782 *
783 * This function enables tracing buffers that may have been
784 * disabled with tracing_off.
785 */
786void tracing_on(void)
787{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400788 tracer_tracing_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -0500789}
790EXPORT_SYMBOL_GPL(tracing_on);
791
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500792
793static __always_inline void
794__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
795{
Joel Fernandesd914ba32017-06-26 19:01:55 -0700796 __this_cpu_write(trace_taskinfo_save, true);
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500797
798 /* If this is the temp buffer, we need to commit fully */
799 if (this_cpu_read(trace_buffered_event) == event) {
800 /* Length is in event->array[0] */
801 ring_buffer_write(buffer, event->array[0], &event->array[1]);
802 /* Release the temp buffer */
803 this_cpu_dec(trace_buffered_event_cnt);
804 } else
805 ring_buffer_unlock_commit(buffer, event);
806}
807
Steven Rostedt499e5472012-02-22 15:50:28 -0500808/**
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500809 * __trace_puts - write a constant string into the trace buffer.
810 * @ip: The address of the caller
811 * @str: The constant string to write
812 * @size: The size of the string.
813 */
814int __trace_puts(unsigned long ip, const char *str, int size)
815{
816 struct ring_buffer_event *event;
817 struct ring_buffer *buffer;
818 struct print_entry *entry;
819 unsigned long irq_flags;
820 int alloc;
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800821 int pc;
822
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400823 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800824 return 0;
825
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800826 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500827
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500828 if (unlikely(tracing_selftest_running || tracing_disabled))
829 return 0;
830
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500831 alloc = sizeof(*entry) + size + 2; /* possible \n added */
832
833 local_save_flags(irq_flags);
834 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500835 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
836 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500837 if (!event)
838 return 0;
839
840 entry = ring_buffer_event_data(event);
841 entry->ip = ip;
842
843 memcpy(&entry->buf, str, size);
844
845 /* Add a newline if necessary */
846 if (entry->buf[size - 1] != '\n') {
847 entry->buf[size] = '\n';
848 entry->buf[size + 1] = '\0';
849 } else
850 entry->buf[size] = '\0';
851
852 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400853 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500854
855 return size;
856}
857EXPORT_SYMBOL_GPL(__trace_puts);
858
859/**
860 * __trace_bputs - write the pointer to a constant string into trace buffer
861 * @ip: The address of the caller
862 * @str: The constant string to write to the buffer to
863 */
864int __trace_bputs(unsigned long ip, const char *str)
865{
866 struct ring_buffer_event *event;
867 struct ring_buffer *buffer;
868 struct bputs_entry *entry;
869 unsigned long irq_flags;
870 int size = sizeof(struct bputs_entry);
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800871 int pc;
872
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400873 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800874 return 0;
875
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800876 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500877
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500878 if (unlikely(tracing_selftest_running || tracing_disabled))
879 return 0;
880
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500881 local_save_flags(irq_flags);
882 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500883 event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
884 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500885 if (!event)
886 return 0;
887
888 entry = ring_buffer_event_data(event);
889 entry->ip = ip;
890 entry->str = str;
891
892 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400893 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500894
895 return 1;
896}
897EXPORT_SYMBOL_GPL(__trace_bputs);
898
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500899#ifdef CONFIG_TRACER_SNAPSHOT
Tom Zanussia35873a2019-02-13 17:42:45 -0600900void tracing_snapshot_instance_cond(struct trace_array *tr, void *cond_data)
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500901{
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500902 struct tracer *tracer = tr->current_trace;
903 unsigned long flags;
904
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500905 if (in_nmi()) {
906 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
907 internal_trace_puts("*** snapshot is being ignored ***\n");
908 return;
909 }
910
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500911 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500912 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
913 internal_trace_puts("*** stopping trace here! ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500914 tracing_off();
915 return;
916 }
917
918 /* Note, snapshot can not be used when the tracer uses it */
919 if (tracer->use_max_tr) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500920 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
921 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500922 return;
923 }
924
925 local_irq_save(flags);
Tom Zanussia35873a2019-02-13 17:42:45 -0600926 update_max_tr(tr, current, smp_processor_id(), cond_data);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500927 local_irq_restore(flags);
928}
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -0400929
Tom Zanussia35873a2019-02-13 17:42:45 -0600930void tracing_snapshot_instance(struct trace_array *tr)
931{
932 tracing_snapshot_instance_cond(tr, NULL);
933}
934
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -0400935/**
Chunyu Hu5a93bae2017-10-19 14:32:33 +0800936 * tracing_snapshot - take a snapshot of the current buffer.
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -0400937 *
938 * This causes a swap between the snapshot buffer and the current live
939 * tracing buffer. You can use this to take snapshots of the live
940 * trace when some condition is triggered, but continue to trace.
941 *
942 * Note, make sure to allocate the snapshot with either
943 * a tracing_snapshot_alloc(), or by doing it manually
944 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
945 *
946 * If the snapshot buffer is not allocated, it will stop tracing.
947 * Basically making a permanent snapshot.
948 */
949void tracing_snapshot(void)
950{
951 struct trace_array *tr = &global_trace;
952
953 tracing_snapshot_instance(tr);
954}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500955EXPORT_SYMBOL_GPL(tracing_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500956
Tom Zanussia35873a2019-02-13 17:42:45 -0600957/**
958 * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
959 * @tr: The tracing instance to snapshot
960 * @cond_data: The data to be tested conditionally, and possibly saved
961 *
962 * This is the same as tracing_snapshot() except that the snapshot is
963 * conditional - the snapshot will only happen if the
964 * cond_snapshot.update() implementation receiving the cond_data
965 * returns true, which means that the trace array's cond_snapshot
966 * update() operation used the cond_data to determine whether the
967 * snapshot should be taken, and if it was, presumably saved it along
968 * with the snapshot.
969 */
970void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
971{
972 tracing_snapshot_instance_cond(tr, cond_data);
973}
974EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
975
976/**
977 * tracing_snapshot_cond_data - get the user data associated with a snapshot
978 * @tr: The tracing instance
979 *
980 * When the user enables a conditional snapshot using
981 * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
982 * with the snapshot. This accessor is used to retrieve it.
983 *
984 * Should not be called from cond_snapshot.update(), since it takes
985 * the tr->max_lock lock, which the code calling
986 * cond_snapshot.update() has already done.
987 *
988 * Returns the cond_data associated with the trace array's snapshot.
989 */
990void *tracing_cond_snapshot_data(struct trace_array *tr)
991{
992 void *cond_data = NULL;
993
994 arch_spin_lock(&tr->max_lock);
995
996 if (tr->cond_snapshot)
997 cond_data = tr->cond_snapshot->cond_data;
998
999 arch_spin_unlock(&tr->max_lock);
1000
1001 return cond_data;
1002}
1003EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1004
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001005static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
1006 struct trace_buffer *size_buf, int cpu_id);
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001007static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
1008
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04001009int tracing_alloc_snapshot_instance(struct trace_array *tr)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001010{
1011 int ret;
1012
1013 if (!tr->allocated_snapshot) {
1014
1015 /* allocate spare buffer */
1016 ret = resize_buffer_duplicate_size(&tr->max_buffer,
1017 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
1018 if (ret < 0)
1019 return ret;
1020
1021 tr->allocated_snapshot = true;
1022 }
1023
1024 return 0;
1025}
1026
Fabian Frederickad1438a2014-04-17 21:44:42 +02001027static void free_snapshot(struct trace_array *tr)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001028{
1029 /*
1030 * We don't free the ring buffer. instead, resize it because
1031 * The max_tr ring buffer has some state (e.g. ring->clock) and
1032 * we want preserve it.
1033 */
1034 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
1035 set_buffer_entries(&tr->max_buffer, 1);
1036 tracing_reset_online_cpus(&tr->max_buffer);
1037 tr->allocated_snapshot = false;
1038}
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001039
1040/**
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001041 * tracing_alloc_snapshot - allocate snapshot buffer.
1042 *
1043 * This only allocates the snapshot buffer if it isn't already
1044 * allocated - it doesn't also take a snapshot.
1045 *
1046 * This is meant to be used in cases where the snapshot buffer needs
1047 * to be set up for events that can't sleep but need to be able to
1048 * trigger a snapshot.
1049 */
1050int tracing_alloc_snapshot(void)
1051{
1052 struct trace_array *tr = &global_trace;
1053 int ret;
1054
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04001055 ret = tracing_alloc_snapshot_instance(tr);
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001056 WARN_ON(ret < 0);
1057
1058 return ret;
1059}
1060EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1061
1062/**
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001063 * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001064 *
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001065 * This is similar to tracing_snapshot(), but it will allocate the
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001066 * snapshot buffer if it isn't already allocated. Use this only
1067 * where it is safe to sleep, as the allocation may sleep.
1068 *
1069 * This causes a swap between the snapshot buffer and the current live
1070 * tracing buffer. You can use this to take snapshots of the live
1071 * trace when some condition is triggered, but continue to trace.
1072 */
1073void tracing_snapshot_alloc(void)
1074{
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001075 int ret;
1076
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001077 ret = tracing_alloc_snapshot();
1078 if (ret < 0)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001079 return;
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001080
1081 tracing_snapshot();
1082}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001083EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Tom Zanussia35873a2019-02-13 17:42:45 -06001084
1085/**
1086 * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
1087 * @tr: The tracing instance
1088 * @cond_data: User data to associate with the snapshot
1089 * @update: Implementation of the cond_snapshot update function
1090 *
1091 * Check whether the conditional snapshot for the given instance has
1092 * already been enabled, or if the current tracer is already using a
1093 * snapshot; if so, return -EBUSY, else create a cond_snapshot and
1094 * save the cond_data and update function inside.
1095 *
1096 * Returns 0 if successful, error otherwise.
1097 */
1098int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
1099 cond_update_fn_t update)
1100{
1101 struct cond_snapshot *cond_snapshot;
1102 int ret = 0;
1103
1104 cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
1105 if (!cond_snapshot)
1106 return -ENOMEM;
1107
1108 cond_snapshot->cond_data = cond_data;
1109 cond_snapshot->update = update;
1110
1111 mutex_lock(&trace_types_lock);
1112
1113 ret = tracing_alloc_snapshot_instance(tr);
1114 if (ret)
1115 goto fail_unlock;
1116
1117 if (tr->current_trace->use_max_tr) {
1118 ret = -EBUSY;
1119 goto fail_unlock;
1120 }
1121
Steven Rostedt (VMware)1c347a92019-02-14 18:45:21 -05001122 /*
1123 * The cond_snapshot can only change to NULL without the
1124 * trace_types_lock. We don't care if we race with it going
1125 * to NULL, but we want to make sure that it's not set to
1126 * something other than NULL when we get here, which we can
1127 * do safely with only holding the trace_types_lock and not
1128 * having to take the max_lock.
1129 */
Tom Zanussia35873a2019-02-13 17:42:45 -06001130 if (tr->cond_snapshot) {
1131 ret = -EBUSY;
1132 goto fail_unlock;
1133 }
1134
1135 arch_spin_lock(&tr->max_lock);
1136 tr->cond_snapshot = cond_snapshot;
1137 arch_spin_unlock(&tr->max_lock);
1138
1139 mutex_unlock(&trace_types_lock);
1140
1141 return ret;
1142
1143 fail_unlock:
1144 mutex_unlock(&trace_types_lock);
1145 kfree(cond_snapshot);
1146 return ret;
1147}
1148EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1149
1150/**
1151 * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
1152 * @tr: The tracing instance
1153 *
1154 * Check whether the conditional snapshot for the given instance is
1155 * enabled; if so, free the cond_snapshot associated with it,
1156 * otherwise return -EINVAL.
1157 *
1158 * Returns 0 if successful, error otherwise.
1159 */
1160int tracing_snapshot_cond_disable(struct trace_array *tr)
1161{
1162 int ret = 0;
1163
1164 arch_spin_lock(&tr->max_lock);
1165
1166 if (!tr->cond_snapshot)
1167 ret = -EINVAL;
1168 else {
1169 kfree(tr->cond_snapshot);
1170 tr->cond_snapshot = NULL;
1171 }
1172
1173 arch_spin_unlock(&tr->max_lock);
1174
1175 return ret;
1176}
1177EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001178#else
1179void tracing_snapshot(void)
1180{
1181 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1182}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001183EXPORT_SYMBOL_GPL(tracing_snapshot);
Tom Zanussia35873a2019-02-13 17:42:45 -06001184void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1185{
1186 WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
1187}
1188EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001189int tracing_alloc_snapshot(void)
1190{
1191 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1192 return -ENODEV;
1193}
1194EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001195void tracing_snapshot_alloc(void)
1196{
1197 /* Give warning */
1198 tracing_snapshot();
1199}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001200EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Tom Zanussia35873a2019-02-13 17:42:45 -06001201void *tracing_cond_snapshot_data(struct trace_array *tr)
1202{
1203 return NULL;
1204}
1205EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1206int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
1207{
1208 return -ENODEV;
1209}
1210EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1211int tracing_snapshot_cond_disable(struct trace_array *tr)
1212{
1213 return false;
1214}
1215EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001216#endif /* CONFIG_TRACER_SNAPSHOT */
1217
Steven Rostedt (VMware)2290f2c2017-04-20 11:46:03 -04001218void tracer_tracing_off(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001219{
1220 if (tr->trace_buffer.buffer)
1221 ring_buffer_record_off(tr->trace_buffer.buffer);
1222 /*
1223 * This flag is looked at when buffers haven't been allocated
1224 * yet, or by some tracers (like irqsoff), that just want to
1225 * know if the ring buffer has been disabled, but it can handle
1226 * races of where it gets disabled but we still do a record.
1227 * As the check is in the fast path of the tracers, it is more
1228 * important to be fast than accurate.
1229 */
1230 tr->buffer_disabled = 1;
1231 /* Make the flag seen by readers */
1232 smp_wmb();
1233}
1234
Steven Rostedt499e5472012-02-22 15:50:28 -05001235/**
1236 * tracing_off - turn off tracing buffers
1237 *
1238 * This function stops the tracing buffers from recording data.
1239 * It does not disable any overhead the tracers themselves may
1240 * be causing. This function simply causes all recording to
1241 * the ring buffers to fail.
1242 */
1243void tracing_off(void)
1244{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001245 tracer_tracing_off(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001246}
1247EXPORT_SYMBOL_GPL(tracing_off);
1248
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -04001249void disable_trace_on_warning(void)
1250{
1251 if (__disable_trace_on_warning)
1252 tracing_off();
1253}
1254
Steven Rostedt499e5472012-02-22 15:50:28 -05001255/**
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001256 * tracer_tracing_is_on - show real state of ring buffer enabled
1257 * @tr : the trace array to know if ring buffer is enabled
1258 *
1259 * Shows real state of the ring buffer if it is enabled or not.
1260 */
Steven Rostedt (VMware)ec573502018-08-01 16:08:57 -04001261bool tracer_tracing_is_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001262{
1263 if (tr->trace_buffer.buffer)
1264 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
1265 return !tr->buffer_disabled;
1266}
1267
Steven Rostedt499e5472012-02-22 15:50:28 -05001268/**
1269 * tracing_is_on - show state of ring buffers enabled
1270 */
1271int tracing_is_on(void)
1272{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001273 return tracer_tracing_is_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001274}
1275EXPORT_SYMBOL_GPL(tracing_is_on);
1276
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001277static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001278{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001279 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001280
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001281 if (!str)
1282 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +08001283 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001284 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +08001285 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001286 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001287 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001288 return 1;
1289}
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001290__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001291
Tim Bird0e950172010-02-25 15:36:43 -08001292static int __init set_tracing_thresh(char *str)
1293{
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001294 unsigned long threshold;
Tim Bird0e950172010-02-25 15:36:43 -08001295 int ret;
1296
1297 if (!str)
1298 return 0;
Daniel Walterbcd83ea2012-09-26 22:08:38 +02001299 ret = kstrtoul(str, 0, &threshold);
Tim Bird0e950172010-02-25 15:36:43 -08001300 if (ret < 0)
1301 return 0;
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001302 tracing_thresh = threshold * 1000;
Tim Bird0e950172010-02-25 15:36:43 -08001303 return 1;
1304}
1305__setup("tracing_thresh=", set_tracing_thresh);
1306
Steven Rostedt57f50be2008-05-12 21:20:44 +02001307unsigned long nsecs_to_usecs(unsigned long nsecs)
1308{
1309 return nsecs / 1000;
1310}
1311
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001312/*
1313 * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
Jeremy Lintonf57a4142017-05-31 16:56:48 -05001314 * 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 -04001315 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
Jeremy Lintonf57a4142017-05-31 16:56:48 -05001316 * of strings in the order that the evals (enum) were defined.
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001317 */
1318#undef C
1319#define C(a, b) b
1320
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001321/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001322static const char *trace_options[] = {
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001323 TRACE_FLAGS
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001324 NULL
1325};
1326
Zhaolei5079f322009-08-25 16:12:56 +08001327static struct {
1328 u64 (*func)(void);
1329 const char *name;
David Sharp8be07092012-11-13 12:18:22 -08001330 int in_ns; /* is this clock in nanoseconds? */
Zhaolei5079f322009-08-25 16:12:56 +08001331} trace_clocks[] = {
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001332 { trace_clock_local, "local", 1 },
1333 { trace_clock_global, "global", 1 },
1334 { trace_clock_counter, "counter", 0 },
Linus Torvaldse7fda6c2014-08-05 17:46:42 -07001335 { trace_clock_jiffies, "uptime", 0 },
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001336 { trace_clock, "perf", 1 },
1337 { ktime_get_mono_fast_ns, "mono", 1 },
Drew Richardsonaabfa5f2015-05-08 07:30:39 -07001338 { ktime_get_raw_fast_ns, "mono_raw", 1 },
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +02001339 { ktime_get_boot_fast_ns, "boot", 1 },
David Sharp8cbd9cc2012-11-13 12:18:21 -08001340 ARCH_TRACE_CLOCKS
Zhaolei5079f322009-08-25 16:12:56 +08001341};
1342
Tom Zanussi860f9f62018-01-15 20:51:48 -06001343bool trace_clock_in_ns(struct trace_array *tr)
1344{
1345 if (trace_clocks[tr->clock_id].in_ns)
1346 return true;
1347
1348 return false;
1349}
1350
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001351/*
1352 * trace_parser_get_init - gets the buffer for trace parser
1353 */
1354int trace_parser_get_init(struct trace_parser *parser, int size)
1355{
1356 memset(parser, 0, sizeof(*parser));
1357
1358 parser->buffer = kmalloc(size, GFP_KERNEL);
1359 if (!parser->buffer)
1360 return 1;
1361
1362 parser->size = size;
1363 return 0;
1364}
1365
1366/*
1367 * trace_parser_put - frees the buffer for trace parser
1368 */
1369void trace_parser_put(struct trace_parser *parser)
1370{
1371 kfree(parser->buffer);
Steven Rostedt (VMware)0e684b62017-02-02 17:58:18 -05001372 parser->buffer = NULL;
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001373}
1374
1375/*
1376 * trace_get_user - reads the user input string separated by space
1377 * (matched by isspace(ch))
1378 *
1379 * For each string found the 'struct trace_parser' is updated,
1380 * and the function returns.
1381 *
1382 * Returns number of bytes read.
1383 *
1384 * See kernel/trace/trace.h for 'struct trace_parser' details.
1385 */
1386int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1387 size_t cnt, loff_t *ppos)
1388{
1389 char ch;
1390 size_t read = 0;
1391 ssize_t ret;
1392
1393 if (!*ppos)
1394 trace_parser_clear(parser);
1395
1396 ret = get_user(ch, ubuf++);
1397 if (ret)
1398 goto out;
1399
1400 read++;
1401 cnt--;
1402
1403 /*
1404 * The parser is not finished with the last write,
1405 * continue reading the user input without skipping spaces.
1406 */
1407 if (!parser->cont) {
1408 /* skip white space */
1409 while (cnt && isspace(ch)) {
1410 ret = get_user(ch, ubuf++);
1411 if (ret)
1412 goto out;
1413 read++;
1414 cnt--;
1415 }
1416
Changbin Du76638d92018-01-16 17:02:29 +08001417 parser->idx = 0;
1418
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001419 /* only spaces were written */
Changbin Du921a7ac2018-01-16 17:02:28 +08001420 if (isspace(ch) || !ch) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001421 *ppos += read;
1422 ret = read;
1423 goto out;
1424 }
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001425 }
1426
1427 /* read the non-space input */
Changbin Du921a7ac2018-01-16 17:02:28 +08001428 while (cnt && !isspace(ch) && ch) {
Li Zefan3c235a32009-09-22 13:51:54 +08001429 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001430 parser->buffer[parser->idx++] = ch;
1431 else {
1432 ret = -EINVAL;
1433 goto out;
1434 }
1435 ret = get_user(ch, ubuf++);
1436 if (ret)
1437 goto out;
1438 read++;
1439 cnt--;
1440 }
1441
1442 /* We either got finished input or we have to wait for another call. */
Changbin Du921a7ac2018-01-16 17:02:28 +08001443 if (isspace(ch) || !ch) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001444 parser->buffer[parser->idx] = 0;
1445 parser->cont = false;
Steven Rostedt057db842013-10-09 22:23:23 -04001446 } else if (parser->idx < parser->size - 1) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001447 parser->cont = true;
1448 parser->buffer[parser->idx++] = ch;
Changbin Duf4d07062018-01-16 17:02:30 +08001449 /* Make sure the parsed string always terminates with '\0'. */
1450 parser->buffer[parser->idx] = 0;
Steven Rostedt057db842013-10-09 22:23:23 -04001451 } else {
1452 ret = -EINVAL;
1453 goto out;
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001454 }
1455
1456 *ppos += read;
1457 ret = read;
1458
1459out:
1460 return ret;
1461}
1462
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001463/* TODO add a seq_buf_to_buffer() */
Dmitri Vorobievb8b94262009-03-22 19:11:11 +02001464static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001465{
1466 int len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001467
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001468 if (trace_seq_used(s) <= s->seq.readpos)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001469 return -EBUSY;
1470
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001471 len = trace_seq_used(s) - s->seq.readpos;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001472 if (cnt > len)
1473 cnt = len;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001474 memcpy(buf, s->buffer + s->seq.readpos, cnt);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001475
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001476 s->seq.readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001477 return cnt;
1478}
1479
Tim Bird0e950172010-02-25 15:36:43 -08001480unsigned long __read_mostly tracing_thresh;
1481
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001482#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001483/*
1484 * Copy the new maximum trace into the separate maximum-trace
1485 * structure. (this way the maximum trace is permanently saved,
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001486 * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001487 */
1488static void
1489__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1490{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001491 struct trace_buffer *trace_buf = &tr->trace_buffer;
1492 struct trace_buffer *max_buf = &tr->max_buffer;
1493 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1494 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001495
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001496 max_buf->cpu = cpu;
1497 max_buf->time_start = data->preempt_timestamp;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001498
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001499 max_data->saved_latency = tr->max_latency;
Steven Rostedt8248ac02009-09-02 12:27:41 -04001500 max_data->critical_start = data->critical_start;
1501 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001502
Tom Zanussi85f726a2019-03-05 10:12:00 -06001503 strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -04001504 max_data->pid = tsk->pid;
Steven Rostedt (Red Hat)f17a5192013-05-30 21:10:37 -04001505 /*
1506 * If tsk == current, then use current_uid(), as that does not use
1507 * RCU. The irq tracer can be called out of RCU scope.
1508 */
1509 if (tsk == current)
1510 max_data->uid = current_uid();
1511 else
1512 max_data->uid = task_uid(tsk);
1513
Steven Rostedt8248ac02009-09-02 12:27:41 -04001514 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1515 max_data->policy = tsk->policy;
1516 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001517
1518 /* record this tasks comm */
1519 tracing_record_cmdline(tsk);
1520}
1521
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001522/**
1523 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1524 * @tr: tracer
1525 * @tsk: the task with the latency
1526 * @cpu: The cpu that initiated the trace.
Tom Zanussia35873a2019-02-13 17:42:45 -06001527 * @cond_data: User data associated with a conditional snapshot
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001528 *
1529 * Flip the buffers between the @tr and the max_tr and record information
1530 * about which task was the cause of this latency.
1531 */
Ingo Molnare309b412008-05-12 21:20:51 +02001532void
Tom Zanussia35873a2019-02-13 17:42:45 -06001533update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
1534 void *cond_data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001535{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001536 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001537 return;
1538
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001539 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt34600f02013-01-22 13:35:11 -05001540
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05001541 if (!tr->allocated_snapshot) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001542 /* Only the nop tracer should hit this when disabling */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001543 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001544 return;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001545 }
Steven Rostedt34600f02013-01-22 13:35:11 -05001546
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001547 arch_spin_lock(&tr->max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001548
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09001549 /* Inherit the recordable setting from trace_buffer */
1550 if (ring_buffer_record_is_set_on(tr->trace_buffer.buffer))
1551 ring_buffer_record_on(tr->max_buffer.buffer);
1552 else
1553 ring_buffer_record_off(tr->max_buffer.buffer);
1554
Tom Zanussia35873a2019-02-13 17:42:45 -06001555#ifdef CONFIG_TRACER_SNAPSHOT
1556 if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data))
1557 goto out_unlock;
1558#endif
Gustavo A. R. Silva08ae88f2018-02-09 11:53:16 -06001559 swap(tr->trace_buffer.buffer, tr->max_buffer.buffer);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001560
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001561 __update_max_tr(tr, tsk, cpu);
Tom Zanussia35873a2019-02-13 17:42:45 -06001562
1563 out_unlock:
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001564 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001565}
1566
1567/**
1568 * update_max_tr_single - only copy one trace over, and reset the rest
1569 * @tr - tracer
1570 * @tsk - task with the latency
1571 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001572 *
1573 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001574 */
Ingo Molnare309b412008-05-12 21:20:51 +02001575void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001576update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1577{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001578 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001579
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001580 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001581 return;
1582
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001583 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt6c244992013-04-29 20:08:14 -04001584 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001585 /* Only the nop tracer should hit this when disabling */
Linus Torvalds9e8529a2013-04-29 13:55:38 -07001586 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001587 return;
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001588 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001589
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001590 arch_spin_lock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001591
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001592 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001593
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001594 if (ret == -EBUSY) {
1595 /*
1596 * We failed to swap the buffer due to a commit taking
1597 * place on this CPU. We fail to record, but we reset
1598 * the max trace buffer (no one writes directly to it)
1599 * and flag that it failed.
1600 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001601 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001602 "Failed to swap buffers due to commit in progress\n");
1603 }
1604
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001605 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001606
1607 __update_max_tr(tr, tsk, cpu);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001608 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001609}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001610#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001611
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05001612static int wait_on_pipe(struct trace_iterator *iter, int full)
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001613{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001614 /* Iterators are static, they should be filled or empty */
1615 if (trace_buffer_iter(iter, iter->cpu_file))
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04001616 return 0;
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001617
Rabin Vincente30f53a2014-11-10 19:46:34 +01001618 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1619 full);
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001620}
1621
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001622#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001623static bool selftests_can_run;
1624
1625struct trace_selftests {
1626 struct list_head list;
1627 struct tracer *type;
1628};
1629
1630static LIST_HEAD(postponed_selftests);
1631
1632static int save_selftest(struct tracer *type)
1633{
1634 struct trace_selftests *selftest;
1635
1636 selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
1637 if (!selftest)
1638 return -ENOMEM;
1639
1640 selftest->type = type;
1641 list_add(&selftest->list, &postponed_selftests);
1642 return 0;
1643}
1644
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001645static int run_tracer_selftest(struct tracer *type)
1646{
1647 struct trace_array *tr = &global_trace;
1648 struct tracer *saved_tracer = tr->current_trace;
1649 int ret;
1650
1651 if (!type->selftest || tracing_selftest_disabled)
1652 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001653
1654 /*
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001655 * If a tracer registers early in boot up (before scheduling is
1656 * initialized and such), then do not run its selftests yet.
1657 * Instead, run it a little later in the boot process.
1658 */
1659 if (!selftests_can_run)
1660 return save_selftest(type);
1661
1662 /*
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001663 * Run a selftest on this tracer.
1664 * Here we reset the trace buffer, and set the current
1665 * tracer to be this tracer. The tracer can then run some
1666 * internal tracing to verify that everything is in order.
1667 * If we fail, we do not register this tracer.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001668 */
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001669 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001670
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001671 tr->current_trace = type;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001672
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001673#ifdef CONFIG_TRACER_MAX_TRACE
1674 if (type->use_max_tr) {
1675 /* If we expanded the buffers, make sure the max is expanded too */
1676 if (ring_buffer_expanded)
1677 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1678 RING_BUFFER_ALL_CPUS);
1679 tr->allocated_snapshot = true;
1680 }
1681#endif
1682
1683 /* the test is responsible for initializing and enabling */
1684 pr_info("Testing tracer %s: ", type->name);
1685 ret = type->selftest(type, tr);
1686 /* the test is responsible for resetting too */
1687 tr->current_trace = saved_tracer;
1688 if (ret) {
1689 printk(KERN_CONT "FAILED!\n");
1690 /* Add the warning after printing 'FAILED' */
1691 WARN_ON(1);
1692 return -1;
1693 }
1694 /* Only reset on passing, to avoid touching corrupted buffers */
1695 tracing_reset_online_cpus(&tr->trace_buffer);
1696
1697#ifdef CONFIG_TRACER_MAX_TRACE
1698 if (type->use_max_tr) {
1699 tr->allocated_snapshot = false;
1700
1701 /* Shrink the max buffer again */
1702 if (ring_buffer_expanded)
1703 ring_buffer_resize(tr->max_buffer.buffer, 1,
1704 RING_BUFFER_ALL_CPUS);
1705 }
1706#endif
1707
1708 printk(KERN_CONT "PASSED\n");
1709 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001710}
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001711
1712static __init int init_trace_selftests(void)
1713{
1714 struct trace_selftests *p, *n;
1715 struct tracer *t, **last;
1716 int ret;
1717
1718 selftests_can_run = true;
1719
1720 mutex_lock(&trace_types_lock);
1721
1722 if (list_empty(&postponed_selftests))
1723 goto out;
1724
1725 pr_info("Running postponed tracer tests:\n");
1726
1727 list_for_each_entry_safe(p, n, &postponed_selftests, list) {
1728 ret = run_tracer_selftest(p->type);
1729 /* If the test fails, then warn and remove from available_tracers */
1730 if (ret < 0) {
1731 WARN(1, "tracer: %s failed selftest, disabling\n",
1732 p->type->name);
1733 last = &trace_types;
1734 for (t = trace_types; t; t = t->next) {
1735 if (t == p->type) {
1736 *last = t->next;
1737 break;
1738 }
1739 last = &t->next;
1740 }
1741 }
1742 list_del(&p->list);
1743 kfree(p);
1744 }
1745
1746 out:
1747 mutex_unlock(&trace_types_lock);
1748
1749 return 0;
1750}
Steven Rostedtb9ef0322017-05-17 11:14:35 -04001751core_initcall(init_trace_selftests);
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001752#else
1753static inline int run_tracer_selftest(struct tracer *type)
1754{
1755 return 0;
1756}
1757#endif /* CONFIG_FTRACE_STARTUP_TEST */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001758
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001759static void add_tracer_options(struct trace_array *tr, struct tracer *t);
1760
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001761static void __init apply_trace_boot_options(void);
1762
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001763/**
1764 * register_tracer - register a tracer with the ftrace system.
1765 * @type - the plugin for the tracer
1766 *
1767 * Register a new plugin tracer.
1768 */
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001769int __init register_tracer(struct tracer *type)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001770{
1771 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001772 int ret = 0;
1773
1774 if (!type->name) {
1775 pr_info("Tracer must have a name\n");
1776 return -1;
1777 }
1778
Dan Carpenter24a461d2010-07-10 12:06:44 +02001779 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +08001780 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1781 return -1;
1782 }
1783
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001784 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +01001785
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001786 tracing_selftest_running = true;
1787
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001788 for (t = trace_types; t; t = t->next) {
1789 if (strcmp(type->name, t->name) == 0) {
1790 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +08001791 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001792 type->name);
1793 ret = -1;
1794 goto out;
1795 }
1796 }
1797
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001798 if (!type->set_flag)
1799 type->set_flag = &dummy_set_flag;
Chunyu Hud39cdd22016-03-08 21:37:01 +08001800 if (!type->flags) {
1801 /*allocate a dummy tracer_flags*/
1802 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
Chunyu Huc8ca0032016-03-14 20:35:41 +08001803 if (!type->flags) {
1804 ret = -ENOMEM;
1805 goto out;
1806 }
Chunyu Hud39cdd22016-03-08 21:37:01 +08001807 type->flags->val = 0;
1808 type->flags->opts = dummy_tracer_opt;
1809 } else
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001810 if (!type->flags->opts)
1811 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001812
Chunyu Hud39cdd22016-03-08 21:37:01 +08001813 /* store the tracer for __set_tracer_option */
1814 type->flags->trace = type;
1815
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001816 ret = run_tracer_selftest(type);
1817 if (ret < 0)
1818 goto out;
Steven Rostedt60a11772008-05-12 21:20:44 +02001819
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001820 type->next = trace_types;
1821 trace_types = type;
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001822 add_tracer_options(&global_trace, type);
Steven Rostedt60a11772008-05-12 21:20:44 +02001823
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001824 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001825 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001826 mutex_unlock(&trace_types_lock);
1827
Steven Rostedtdac74942009-02-05 01:13:38 -05001828 if (ret || !default_bootup_tracer)
1829 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -05001830
Li Zefanee6c2c12009-09-18 14:06:47 +08001831 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -05001832 goto out_unlock;
1833
1834 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1835 /* Do we want this tracer to start on bootup? */
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05001836 tracing_set_tracer(&global_trace, type->name);
Steven Rostedtdac74942009-02-05 01:13:38 -05001837 default_bootup_tracer = NULL;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001838
1839 apply_trace_boot_options();
1840
Steven Rostedtdac74942009-02-05 01:13:38 -05001841 /* disable other selftests, since this will break it. */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05001842 tracing_selftest_disabled = true;
Steven Rostedtdac74942009-02-05 01:13:38 -05001843#ifdef CONFIG_FTRACE_STARTUP_TEST
1844 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1845 type->name);
1846#endif
1847
1848 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001849 return ret;
1850}
1851
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001852void tracing_reset(struct trace_buffer *buf, int cpu)
Steven Rostedtf6339032009-09-04 12:35:16 -04001853{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001854 struct ring_buffer *buffer = buf->buffer;
Steven Rostedtf6339032009-09-04 12:35:16 -04001855
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001856 if (!buffer)
1857 return;
1858
Steven Rostedtf6339032009-09-04 12:35:16 -04001859 ring_buffer_record_disable(buffer);
1860
1861 /* Make sure all commits have finished */
Paul E. McKenney74401722018-11-06 18:44:52 -08001862 synchronize_rcu();
Steven Rostedt68179682012-05-08 20:57:53 -04001863 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -04001864
1865 ring_buffer_record_enable(buffer);
1866}
1867
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001868void tracing_reset_online_cpus(struct trace_buffer *buf)
Pekka J Enberg213cc062008-12-19 12:08:39 +02001869{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001870 struct ring_buffer *buffer = buf->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +02001871 int cpu;
1872
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001873 if (!buffer)
1874 return;
1875
Steven Rostedt621968c2009-09-04 12:02:35 -04001876 ring_buffer_record_disable(buffer);
1877
1878 /* Make sure all commits have finished */
Paul E. McKenney74401722018-11-06 18:44:52 -08001879 synchronize_rcu();
Steven Rostedt621968c2009-09-04 12:02:35 -04001880
Alexander Z Lam94571582013-08-02 18:36:16 -07001881 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001882
1883 for_each_online_cpu(cpu)
Steven Rostedt68179682012-05-08 20:57:53 -04001884 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -04001885
1886 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001887}
1888
Steven Rostedt (Red Hat)09d80912013-07-23 22:21:59 -04001889/* Must have trace_types_lock held */
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001890void tracing_reset_all_online_cpus(void)
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001891{
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001892 struct trace_array *tr;
1893
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001894 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
Steven Rostedt (VMware)065e63f2017-08-31 17:03:47 -04001895 if (!tr->clear_trace)
1896 continue;
1897 tr->clear_trace = false;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001898 tracing_reset_online_cpus(&tr->trace_buffer);
1899#ifdef CONFIG_TRACER_MAX_TRACE
1900 tracing_reset_online_cpus(&tr->max_buffer);
1901#endif
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001902 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001903}
1904
Joel Fernandesd914ba32017-06-26 19:01:55 -07001905static int *tgid_map;
1906
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001907#define SAVED_CMDLINES_DEFAULT 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001908#define NO_CMDLINE_MAP UINT_MAX
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001909static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001910struct saved_cmdlines_buffer {
1911 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1912 unsigned *map_cmdline_to_pid;
1913 unsigned cmdline_num;
1914 int cmdline_idx;
1915 char *saved_cmdlines;
1916};
1917static struct saved_cmdlines_buffer *savedcmd;
Steven Rostedt25b0b442008-05-12 21:21:00 +02001918
Steven Rostedt25b0b442008-05-12 21:21:00 +02001919/* temporary disable recording */
Joel Fernandesd914ba32017-06-26 19:01:55 -07001920static atomic_t trace_record_taskinfo_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001921
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001922static inline char *get_saved_cmdlines(int idx)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001923{
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001924 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
1925}
1926
1927static inline void set_cmdline(int idx, const char *cmdline)
1928{
Tom Zanussi85f726a2019-03-05 10:12:00 -06001929 strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001930}
1931
1932static int allocate_cmdlines_buffer(unsigned int val,
1933 struct saved_cmdlines_buffer *s)
1934{
Kees Cook6da2ec52018-06-12 13:55:00 -07001935 s->map_cmdline_to_pid = kmalloc_array(val,
1936 sizeof(*s->map_cmdline_to_pid),
1937 GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001938 if (!s->map_cmdline_to_pid)
1939 return -ENOMEM;
1940
Kees Cook6da2ec52018-06-12 13:55:00 -07001941 s->saved_cmdlines = kmalloc_array(TASK_COMM_LEN, val, GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001942 if (!s->saved_cmdlines) {
1943 kfree(s->map_cmdline_to_pid);
1944 return -ENOMEM;
1945 }
1946
1947 s->cmdline_idx = 0;
1948 s->cmdline_num = val;
1949 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
1950 sizeof(s->map_pid_to_cmdline));
1951 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
1952 val * sizeof(*s->map_cmdline_to_pid));
1953
1954 return 0;
1955}
1956
1957static int trace_create_savedcmd(void)
1958{
1959 int ret;
1960
Namhyung Kima6af8fb2014-06-10 16:11:35 +09001961 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001962 if (!savedcmd)
1963 return -ENOMEM;
1964
1965 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
1966 if (ret < 0) {
1967 kfree(savedcmd);
1968 savedcmd = NULL;
1969 return -ENOMEM;
1970 }
1971
1972 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001973}
1974
Carsten Emdeb5130b12009-09-13 01:43:07 +02001975int is_tracing_stopped(void)
1976{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001977 return global_trace.stop_count;
Carsten Emdeb5130b12009-09-13 01:43:07 +02001978}
1979
Steven Rostedt0f048702008-11-05 16:05:44 -05001980/**
1981 * tracing_start - quick start of the tracer
1982 *
1983 * If tracing is enabled but was stopped by tracing_stop,
1984 * this will start the tracer back up.
1985 */
1986void tracing_start(void)
1987{
1988 struct ring_buffer *buffer;
1989 unsigned long flags;
1990
1991 if (tracing_disabled)
1992 return;
1993
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001994 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1995 if (--global_trace.stop_count) {
1996 if (global_trace.stop_count < 0) {
Steven Rostedtb06a8302009-01-22 14:26:15 -05001997 /* Someone screwed up their debugging */
1998 WARN_ON_ONCE(1);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001999 global_trace.stop_count = 0;
Steven Rostedtb06a8302009-01-22 14:26:15 -05002000 }
Steven Rostedt0f048702008-11-05 16:05:44 -05002001 goto out;
2002 }
2003
Steven Rostedta2f80712010-03-12 19:56:00 -05002004 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002005 arch_spin_lock(&global_trace.max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -05002006
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002007 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002008 if (buffer)
2009 ring_buffer_record_enable(buffer);
2010
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002011#ifdef CONFIG_TRACER_MAX_TRACE
2012 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002013 if (buffer)
2014 ring_buffer_record_enable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002015#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05002016
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002017 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05002018
Steven Rostedt0f048702008-11-05 16:05:44 -05002019 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002020 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2021}
2022
2023static void tracing_start_tr(struct trace_array *tr)
2024{
2025 struct ring_buffer *buffer;
2026 unsigned long flags;
2027
2028 if (tracing_disabled)
2029 return;
2030
2031 /* If global, we need to also start the max tracer */
2032 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2033 return tracing_start();
2034
2035 raw_spin_lock_irqsave(&tr->start_lock, flags);
2036
2037 if (--tr->stop_count) {
2038 if (tr->stop_count < 0) {
2039 /* Someone screwed up their debugging */
2040 WARN_ON_ONCE(1);
2041 tr->stop_count = 0;
2042 }
2043 goto out;
2044 }
2045
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002046 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002047 if (buffer)
2048 ring_buffer_record_enable(buffer);
2049
2050 out:
2051 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05002052}
2053
2054/**
2055 * tracing_stop - quick stop of the tracer
2056 *
2057 * Light weight way to stop tracing. Use in conjunction with
2058 * tracing_start.
2059 */
2060void tracing_stop(void)
2061{
2062 struct ring_buffer *buffer;
2063 unsigned long flags;
2064
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002065 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
2066 if (global_trace.stop_count++)
Steven Rostedt0f048702008-11-05 16:05:44 -05002067 goto out;
2068
Steven Rostedta2f80712010-03-12 19:56:00 -05002069 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002070 arch_spin_lock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05002071
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002072 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002073 if (buffer)
2074 ring_buffer_record_disable(buffer);
2075
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002076#ifdef CONFIG_TRACER_MAX_TRACE
2077 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002078 if (buffer)
2079 ring_buffer_record_disable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002080#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05002081
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002082 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05002083
Steven Rostedt0f048702008-11-05 16:05:44 -05002084 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002085 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2086}
2087
2088static void tracing_stop_tr(struct trace_array *tr)
2089{
2090 struct ring_buffer *buffer;
2091 unsigned long flags;
2092
2093 /* If global, we need to also stop the max tracer */
2094 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2095 return tracing_stop();
2096
2097 raw_spin_lock_irqsave(&tr->start_lock, flags);
2098 if (tr->stop_count++)
2099 goto out;
2100
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002101 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002102 if (buffer)
2103 ring_buffer_record_disable(buffer);
2104
2105 out:
2106 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05002107}
2108
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002109static int trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002110{
Carsten Emdea635cf02009-03-18 09:00:41 +01002111 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002112
Joel Fernandeseaf260a2017-07-06 16:00:21 -07002113 /* treat recording of idle task as a success */
2114 if (!tsk->pid)
2115 return 1;
2116
2117 if (unlikely(tsk->pid > PID_MAX_DEFAULT))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002118 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002119
2120 /*
2121 * It's not the end of the world if we don't get
2122 * the lock, but we also don't want to spin
2123 * nor do we want to disable interrupts,
2124 * so if we miss here, then better luck next time.
2125 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002126 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002127 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002128
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002129 idx = savedcmd->map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01002130 if (idx == NO_CMDLINE_MAP) {
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002131 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002132
Carsten Emdea635cf02009-03-18 09:00:41 +01002133 /*
2134 * Check whether the cmdline buffer at idx has a pid
2135 * mapped. We are going to overwrite that entry so we
2136 * need to clear the map_pid_to_cmdline. Otherwise we
2137 * would read the new comm for the old pid.
2138 */
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002139 pid = savedcmd->map_cmdline_to_pid[idx];
Carsten Emdea635cf02009-03-18 09:00:41 +01002140 if (pid != NO_CMDLINE_MAP)
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002141 savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002142
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002143 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
2144 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002145
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002146 savedcmd->cmdline_idx = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002147 }
2148
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002149 set_cmdline(idx, tsk->comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002150
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002151 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002152
2153 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002154}
2155
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04002156static void __trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002157{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002158 unsigned map;
2159
Steven Rostedt4ca530852009-03-16 19:20:15 -04002160 if (!pid) {
2161 strcpy(comm, "<idle>");
2162 return;
2163 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002164
Steven Rostedt74bf4072010-01-25 15:11:53 -05002165 if (WARN_ON_ONCE(pid < 0)) {
2166 strcpy(comm, "<XXX>");
2167 return;
2168 }
2169
Steven Rostedt4ca530852009-03-16 19:20:15 -04002170 if (pid > PID_MAX_DEFAULT) {
2171 strcpy(comm, "<...>");
2172 return;
2173 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002174
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002175 map = savedcmd->map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01002176 if (map != NO_CMDLINE_MAP)
Amey Telawanee09e2862017-05-03 15:41:14 +05302177 strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
Thomas Gleixner50d88752009-03-18 08:58:44 +01002178 else
2179 strcpy(comm, "<...>");
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04002180}
2181
2182void trace_find_cmdline(int pid, char comm[])
2183{
2184 preempt_disable();
2185 arch_spin_lock(&trace_cmdline_lock);
2186
2187 __trace_find_cmdline(pid, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002188
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002189 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02002190 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002191}
2192
Joel Fernandesd914ba32017-06-26 19:01:55 -07002193int trace_find_tgid(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002194{
Joel Fernandesd914ba32017-06-26 19:01:55 -07002195 if (unlikely(!tgid_map || !pid || pid > PID_MAX_DEFAULT))
2196 return 0;
2197
2198 return tgid_map[pid];
2199}
2200
2201static int trace_save_tgid(struct task_struct *tsk)
2202{
Joel Fernandesbd45d342017-07-06 16:00:22 -07002203 /* treat recording of idle task as a success */
2204 if (!tsk->pid)
2205 return 1;
2206
2207 if (unlikely(!tgid_map || tsk->pid > PID_MAX_DEFAULT))
Joel Fernandesd914ba32017-06-26 19:01:55 -07002208 return 0;
2209
2210 tgid_map[tsk->pid] = tsk->tgid;
2211 return 1;
2212}
2213
2214static bool tracing_record_taskinfo_skip(int flags)
2215{
2216 if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID))))
2217 return true;
2218 if (atomic_read(&trace_record_taskinfo_disabled) || !tracing_is_on())
2219 return true;
2220 if (!__this_cpu_read(trace_taskinfo_save))
2221 return true;
2222 return false;
2223}
2224
2225/**
2226 * tracing_record_taskinfo - record the task info of a task
2227 *
2228 * @task - task to record
2229 * @flags - TRACE_RECORD_CMDLINE for recording comm
2230 * - TRACE_RECORD_TGID for recording tgid
2231 */
2232void tracing_record_taskinfo(struct task_struct *task, int flags)
2233{
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002234 bool done;
2235
Joel Fernandesd914ba32017-06-26 19:01:55 -07002236 if (tracing_record_taskinfo_skip(flags))
2237 return;
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002238
2239 /*
2240 * Record as much task information as possible. If some fail, continue
2241 * to try to record the others.
2242 */
2243 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task);
2244 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task);
2245
2246 /* If recording any information failed, retry again soon. */
2247 if (!done)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002248 return;
2249
Joel Fernandesd914ba32017-06-26 19:01:55 -07002250 __this_cpu_write(trace_taskinfo_save, false);
2251}
2252
2253/**
2254 * tracing_record_taskinfo_sched_switch - record task info for sched_switch
2255 *
2256 * @prev - previous task during sched_switch
2257 * @next - next task during sched_switch
2258 * @flags - TRACE_RECORD_CMDLINE for recording comm
2259 * TRACE_RECORD_TGID for recording tgid
2260 */
2261void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
2262 struct task_struct *next, int flags)
2263{
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002264 bool done;
2265
Joel Fernandesd914ba32017-06-26 19:01:55 -07002266 if (tracing_record_taskinfo_skip(flags))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002267 return;
2268
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002269 /*
2270 * Record as much task information as possible. If some fail, continue
2271 * to try to record the others.
2272 */
2273 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev);
2274 done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next);
2275 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev);
2276 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next);
Joel Fernandesd914ba32017-06-26 19:01:55 -07002277
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002278 /* If recording any information failed, retry again soon. */
2279 if (!done)
Joel Fernandesd914ba32017-06-26 19:01:55 -07002280 return;
2281
2282 __this_cpu_write(trace_taskinfo_save, false);
2283}
2284
2285/* Helpers to record a specific task information */
2286void tracing_record_cmdline(struct task_struct *task)
2287{
2288 tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE);
2289}
2290
2291void tracing_record_tgid(struct task_struct *task)
2292{
2293 tracing_record_taskinfo(task, TRACE_RECORD_TGID);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002294}
2295
Steven Rostedt (VMware)af0009f2017-03-16 11:01:06 -04002296/*
2297 * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
2298 * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
2299 * simplifies those functions and keeps them in sync.
2300 */
2301enum print_line_t trace_handle_return(struct trace_seq *s)
2302{
2303 return trace_seq_has_overflowed(s) ?
2304 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2305}
2306EXPORT_SYMBOL_GPL(trace_handle_return);
2307
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03002308void
Steven Rostedt38697052008-10-01 13:14:09 -04002309tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
2310 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002311{
2312 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002313
Steven Rostedt777e2082008-09-29 23:02:42 -04002314 entry->preempt_count = pc & 0xff;
2315 entry->pid = (tsk) ? tsk->pid : 0;
2316 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04002317#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04002318 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04002319#else
2320 TRACE_FLAG_IRQS_NOSUPPORT |
2321#endif
Peter Zijlstra7e6867b2016-03-18 16:28:04 +01002322 ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002323 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
Pavankumar Kondetic59f29c2016-12-09 21:50:17 +05302324 ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
Peter Zijlstrae5137b52013-10-04 17:28:26 +02002325 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
2326 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002327}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002328EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002329
Steven Rostedte77405a2009-09-02 14:17:06 -04002330struct ring_buffer_event *
2331trace_buffer_lock_reserve(struct ring_buffer *buffer,
2332 int type,
2333 unsigned long len,
2334 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002335{
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002336 return __trace_buffer_lock_reserve(buffer, type, len, flags, pc);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002337}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002338
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002339DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2340DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2341static int trace_buffered_event_ref;
2342
2343/**
2344 * trace_buffered_event_enable - enable buffering events
2345 *
2346 * When events are being filtered, it is quicker to use a temporary
2347 * buffer to write the event data into if there's a likely chance
2348 * that it will not be committed. The discard of the ring buffer
2349 * is not as fast as committing, and is much slower than copying
2350 * a commit.
2351 *
2352 * When an event is to be filtered, allocate per cpu buffers to
2353 * write the event data into, and if the event is filtered and discarded
2354 * it is simply dropped, otherwise, the entire data is to be committed
2355 * in one shot.
2356 */
2357void trace_buffered_event_enable(void)
2358{
2359 struct ring_buffer_event *event;
2360 struct page *page;
2361 int cpu;
2362
2363 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2364
2365 if (trace_buffered_event_ref++)
2366 return;
2367
2368 for_each_tracing_cpu(cpu) {
2369 page = alloc_pages_node(cpu_to_node(cpu),
2370 GFP_KERNEL | __GFP_NORETRY, 0);
2371 if (!page)
2372 goto failed;
2373
2374 event = page_address(page);
2375 memset(event, 0, sizeof(*event));
2376
2377 per_cpu(trace_buffered_event, cpu) = event;
2378
2379 preempt_disable();
2380 if (cpu == smp_processor_id() &&
2381 this_cpu_read(trace_buffered_event) !=
2382 per_cpu(trace_buffered_event, cpu))
2383 WARN_ON_ONCE(1);
2384 preempt_enable();
2385 }
2386
2387 return;
2388 failed:
2389 trace_buffered_event_disable();
2390}
2391
2392static void enable_trace_buffered_event(void *data)
2393{
2394 /* Probably not needed, but do it anyway */
2395 smp_rmb();
2396 this_cpu_dec(trace_buffered_event_cnt);
2397}
2398
2399static void disable_trace_buffered_event(void *data)
2400{
2401 this_cpu_inc(trace_buffered_event_cnt);
2402}
2403
2404/**
2405 * trace_buffered_event_disable - disable buffering events
2406 *
2407 * When a filter is removed, it is faster to not use the buffered
2408 * events, and to commit directly into the ring buffer. Free up
2409 * the temp buffers when there are no more users. This requires
2410 * special synchronization with current events.
2411 */
2412void trace_buffered_event_disable(void)
2413{
2414 int cpu;
2415
2416 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2417
2418 if (WARN_ON_ONCE(!trace_buffered_event_ref))
2419 return;
2420
2421 if (--trace_buffered_event_ref)
2422 return;
2423
2424 preempt_disable();
2425 /* For each CPU, set the buffer as used. */
2426 smp_call_function_many(tracing_buffer_mask,
2427 disable_trace_buffered_event, NULL, 1);
2428 preempt_enable();
2429
2430 /* Wait for all current users to finish */
Paul E. McKenney74401722018-11-06 18:44:52 -08002431 synchronize_rcu();
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002432
2433 for_each_tracing_cpu(cpu) {
2434 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2435 per_cpu(trace_buffered_event, cpu) = NULL;
2436 }
2437 /*
2438 * Make sure trace_buffered_event is NULL before clearing
2439 * trace_buffered_event_cnt.
2440 */
2441 smp_wmb();
2442
2443 preempt_disable();
2444 /* Do the work on each cpu */
2445 smp_call_function_many(tracing_buffer_mask,
2446 enable_trace_buffered_event, NULL, 1);
2447 preempt_enable();
2448}
2449
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002450static struct ring_buffer *temp_buffer;
2451
Steven Rostedtef5580d2009-02-27 19:38:04 -05002452struct ring_buffer_event *
Steven Rostedtccb469a2012-08-02 10:32:10 -04002453trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002454 struct trace_event_file *trace_file,
Steven Rostedtccb469a2012-08-02 10:32:10 -04002455 int type, unsigned long len,
2456 unsigned long flags, int pc)
2457{
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002458 struct ring_buffer_event *entry;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002459 int val;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002460
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002461 *current_rb = trace_file->tr->trace_buffer.buffer;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002462
Tom Zanussi00b41452018-01-15 20:51:39 -06002463 if (!ring_buffer_time_stamp_abs(*current_rb) && (trace_file->flags &
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002464 (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
2465 (entry = this_cpu_read(trace_buffered_event))) {
2466 /* Try to use the per cpu buffer first */
2467 val = this_cpu_inc_return(trace_buffered_event_cnt);
2468 if (val == 1) {
2469 trace_event_setup(entry, type, flags, pc);
2470 entry->array[0] = len;
2471 return entry;
2472 }
2473 this_cpu_dec(trace_buffered_event_cnt);
2474 }
2475
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002476 entry = __trace_buffer_lock_reserve(*current_rb,
2477 type, len, flags, pc);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002478 /*
2479 * If tracing is off, but we have triggers enabled
2480 * we still need to look at the event data. Use the temp_buffer
2481 * to store the trace event for the tigger to use. It's recusive
2482 * safe and will not be recorded anywhere.
2483 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002484 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002485 *current_rb = temp_buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002486 entry = __trace_buffer_lock_reserve(*current_rb,
2487 type, len, flags, pc);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002488 }
2489 return entry;
Steven Rostedtccb469a2012-08-02 10:32:10 -04002490}
2491EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2492
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002493static DEFINE_SPINLOCK(tracepoint_iter_lock);
2494static DEFINE_MUTEX(tracepoint_printk_mutex);
2495
2496static void output_printk(struct trace_event_buffer *fbuffer)
2497{
2498 struct trace_event_call *event_call;
2499 struct trace_event *event;
2500 unsigned long flags;
2501 struct trace_iterator *iter = tracepoint_print_iter;
2502
2503 /* We should never get here if iter is NULL */
2504 if (WARN_ON_ONCE(!iter))
2505 return;
2506
2507 event_call = fbuffer->trace_file->event_call;
2508 if (!event_call || !event_call->event.funcs ||
2509 !event_call->event.funcs->trace)
2510 return;
2511
2512 event = &fbuffer->trace_file->event_call->event;
2513
2514 spin_lock_irqsave(&tracepoint_iter_lock, flags);
2515 trace_seq_init(&iter->seq);
2516 iter->ent = fbuffer->entry;
2517 event_call->event.funcs->trace(iter, 0, event);
2518 trace_seq_putc(&iter->seq, 0);
2519 printk("%s", iter->seq.buffer);
2520
2521 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2522}
2523
2524int tracepoint_printk_sysctl(struct ctl_table *table, int write,
2525 void __user *buffer, size_t *lenp,
2526 loff_t *ppos)
2527{
2528 int save_tracepoint_printk;
2529 int ret;
2530
2531 mutex_lock(&tracepoint_printk_mutex);
2532 save_tracepoint_printk = tracepoint_printk;
2533
2534 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2535
2536 /*
2537 * This will force exiting early, as tracepoint_printk
2538 * is always zero when tracepoint_printk_iter is not allocated
2539 */
2540 if (!tracepoint_print_iter)
2541 tracepoint_printk = 0;
2542
2543 if (save_tracepoint_printk == tracepoint_printk)
2544 goto out;
2545
2546 if (tracepoint_printk)
2547 static_key_enable(&tracepoint_printk_key.key);
2548 else
2549 static_key_disable(&tracepoint_printk_key.key);
2550
2551 out:
2552 mutex_unlock(&tracepoint_printk_mutex);
2553
2554 return ret;
2555}
2556
2557void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2558{
2559 if (static_key_false(&tracepoint_printk_key.key))
2560 output_printk(fbuffer);
2561
2562 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
2563 fbuffer->event, fbuffer->entry,
2564 fbuffer->flags, fbuffer->pc);
2565}
2566EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2567
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002568/*
2569 * Skip 3:
2570 *
2571 * trace_buffer_unlock_commit_regs()
2572 * trace_event_buffer_commit()
2573 * trace_event_raw_event_xxx()
Rohit Visavalia13cf9122018-01-29 15:11:26 +05302574 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002575# define STACK_SKIP 3
2576
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04002577void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2578 struct ring_buffer *buffer,
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04002579 struct ring_buffer_event *event,
2580 unsigned long flags, int pc,
2581 struct pt_regs *regs)
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002582{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002583 __buffer_unlock_commit(buffer, event);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002584
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002585 /*
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002586 * If regs is not set, then skip the necessary functions.
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002587 * Note, we can still get here via blktrace, wakeup tracer
2588 * and mmiotrace, but that's ok if they lose a function or
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002589 * two. They are not that meaningful.
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002590 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002591 ftrace_trace_stack(tr, buffer, flags, regs ? 0 : STACK_SKIP, pc, regs);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002592 ftrace_trace_userstack(buffer, flags, pc);
2593}
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002594
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -05002595/*
2596 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2597 */
2598void
2599trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer,
2600 struct ring_buffer_event *event)
2601{
2602 __buffer_unlock_commit(buffer, event);
2603}
2604
Chunyan Zhang478409d2016-11-21 15:57:18 +08002605static void
2606trace_process_export(struct trace_export *export,
2607 struct ring_buffer_event *event)
2608{
2609 struct trace_entry *entry;
2610 unsigned int size = 0;
2611
2612 entry = ring_buffer_event_data(event);
2613 size = ring_buffer_event_length(event);
Felipe Balbia773d412017-06-02 13:20:25 +03002614 export->write(export, entry, size);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002615}
2616
2617static DEFINE_MUTEX(ftrace_export_lock);
2618
2619static struct trace_export __rcu *ftrace_exports_list __read_mostly;
2620
2621static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled);
2622
2623static inline void ftrace_exports_enable(void)
2624{
2625 static_branch_enable(&ftrace_exports_enabled);
2626}
2627
2628static inline void ftrace_exports_disable(void)
2629{
2630 static_branch_disable(&ftrace_exports_enabled);
2631}
2632
Mathieu Malaterre1cce3772018-05-16 21:30:12 +02002633static void ftrace_exports(struct ring_buffer_event *event)
Chunyan Zhang478409d2016-11-21 15:57:18 +08002634{
2635 struct trace_export *export;
2636
2637 preempt_disable_notrace();
2638
2639 export = rcu_dereference_raw_notrace(ftrace_exports_list);
2640 while (export) {
2641 trace_process_export(export, event);
2642 export = rcu_dereference_raw_notrace(export->next);
2643 }
2644
2645 preempt_enable_notrace();
2646}
2647
2648static inline void
2649add_trace_export(struct trace_export **list, struct trace_export *export)
2650{
2651 rcu_assign_pointer(export->next, *list);
2652 /*
2653 * We are entering export into the list but another
2654 * CPU might be walking that list. We need to make sure
2655 * the export->next pointer is valid before another CPU sees
2656 * the export pointer included into the list.
2657 */
2658 rcu_assign_pointer(*list, export);
2659}
2660
2661static inline int
2662rm_trace_export(struct trace_export **list, struct trace_export *export)
2663{
2664 struct trace_export **p;
2665
2666 for (p = list; *p != NULL; p = &(*p)->next)
2667 if (*p == export)
2668 break;
2669
2670 if (*p != export)
2671 return -1;
2672
2673 rcu_assign_pointer(*p, (*p)->next);
2674
2675 return 0;
2676}
2677
2678static inline void
2679add_ftrace_export(struct trace_export **list, struct trace_export *export)
2680{
2681 if (*list == NULL)
2682 ftrace_exports_enable();
2683
2684 add_trace_export(list, export);
2685}
2686
2687static inline int
2688rm_ftrace_export(struct trace_export **list, struct trace_export *export)
2689{
2690 int ret;
2691
2692 ret = rm_trace_export(list, export);
2693 if (*list == NULL)
2694 ftrace_exports_disable();
2695
2696 return ret;
2697}
2698
2699int register_ftrace_export(struct trace_export *export)
2700{
2701 if (WARN_ON_ONCE(!export->write))
2702 return -1;
2703
2704 mutex_lock(&ftrace_export_lock);
2705
2706 add_ftrace_export(&ftrace_exports_list, export);
2707
2708 mutex_unlock(&ftrace_export_lock);
2709
2710 return 0;
2711}
2712EXPORT_SYMBOL_GPL(register_ftrace_export);
2713
2714int unregister_ftrace_export(struct trace_export *export)
2715{
2716 int ret;
2717
2718 mutex_lock(&ftrace_export_lock);
2719
2720 ret = rm_ftrace_export(&ftrace_exports_list, export);
2721
2722 mutex_unlock(&ftrace_export_lock);
2723
2724 return ret;
2725}
2726EXPORT_SYMBOL_GPL(unregister_ftrace_export);
2727
Ingo Molnare309b412008-05-12 21:20:51 +02002728void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05002729trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04002730 unsigned long ip, unsigned long parent_ip, unsigned long flags,
2731 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002732{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002733 struct trace_event_call *call = &event_function;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002734 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002735 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04002736 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002737
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002738 event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
2739 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002740 if (!event)
2741 return;
2742 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04002743 entry->ip = ip;
2744 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05002745
Chunyan Zhang478409d2016-11-21 15:57:18 +08002746 if (!call_filter_check_discard(call, entry, buffer, event)) {
2747 if (static_branch_unlikely(&ftrace_exports_enabled))
2748 ftrace_exports(event);
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002749 __buffer_unlock_commit(buffer, event);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002750 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002751}
2752
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002753#ifdef CONFIG_STACKTRACE
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002754
2755#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
2756struct ftrace_stack {
2757 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
2758};
2759
2760static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
2761static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2762
Steven Rostedte77405a2009-09-02 14:17:06 -04002763static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05002764 unsigned long flags,
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002765 int skip, int pc, struct pt_regs *regs)
Ingo Molnar86387f72008-05-12 21:20:51 +02002766{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002767 struct trace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002768 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04002769 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02002770 struct stack_trace trace;
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002771 int use_stack;
2772 int size = FTRACE_STACK_ENTRIES;
Ingo Molnar86387f72008-05-12 21:20:51 +02002773
2774 trace.nr_entries = 0;
Ingo Molnar86387f72008-05-12 21:20:51 +02002775 trace.skip = skip;
Ingo Molnar86387f72008-05-12 21:20:51 +02002776
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002777 /*
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002778 * Add one, for this function and the call to save_stack_trace()
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002779 * If regs is set, then these functions will not be in the way.
2780 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002781#ifndef CONFIG_UNWINDER_ORC
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002782 if (!regs)
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002783 trace.skip++;
2784#endif
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002785
2786 /*
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002787 * Since events can happen in NMIs there's no safe way to
2788 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2789 * or NMI comes in, it will just have to use the default
2790 * FTRACE_STACK_SIZE.
2791 */
2792 preempt_disable_notrace();
2793
Shan Wei82146522012-11-19 13:21:01 +08002794 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002795 /*
2796 * We don't need any atomic variables, just a barrier.
2797 * If an interrupt comes in, we don't care, because it would
2798 * have exited and put the counter back to what we want.
2799 * We just need a barrier to keep gcc from moving things
2800 * around.
2801 */
2802 barrier();
2803 if (use_stack == 1) {
Christoph Lameterbdffd892014-04-29 14:17:40 -05002804 trace.entries = this_cpu_ptr(ftrace_stack.calls);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002805 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
2806
2807 if (regs)
2808 save_stack_trace_regs(regs, &trace);
2809 else
2810 save_stack_trace(&trace);
2811
2812 if (trace.nr_entries > size)
2813 size = trace.nr_entries;
2814 } else
2815 /* From now on, use_stack is a boolean */
2816 use_stack = 0;
2817
2818 size *= sizeof(unsigned long);
2819
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002820 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
2821 sizeof(*entry) + size, flags, pc);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002822 if (!event)
2823 goto out;
2824 entry = ring_buffer_event_data(event);
2825
2826 memset(&entry->caller, 0, size);
2827
2828 if (use_stack)
2829 memcpy(&entry->caller, trace.entries,
2830 trace.nr_entries * sizeof(unsigned long));
2831 else {
2832 trace.max_entries = FTRACE_STACK_ENTRIES;
2833 trace.entries = entry->caller;
2834 if (regs)
2835 save_stack_trace_regs(regs, &trace);
2836 else
2837 save_stack_trace(&trace);
2838 }
2839
2840 entry->size = trace.nr_entries;
2841
Tom Zanussif306cc82013-10-24 08:34:17 -05002842 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002843 __buffer_unlock_commit(buffer, event);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002844
2845 out:
2846 /* Again, don't let gcc optimize things here */
2847 barrier();
Shan Wei82146522012-11-19 13:21:01 +08002848 __this_cpu_dec(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002849 preempt_enable_notrace();
2850
Ingo Molnarf0a920d2008-05-12 21:20:47 +02002851}
2852
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002853static inline void ftrace_trace_stack(struct trace_array *tr,
2854 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002855 unsigned long flags,
2856 int skip, int pc, struct pt_regs *regs)
Steven Rostedt53614992009-01-15 19:12:40 -05002857{
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002858 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
Steven Rostedt53614992009-01-15 19:12:40 -05002859 return;
2860
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002861 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
Steven Rostedt53614992009-01-15 19:12:40 -05002862}
2863
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002864void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
2865 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04002866{
Steven Rostedt (VMware)a33d7d92017-05-12 13:15:45 -04002867 struct ring_buffer *buffer = tr->trace_buffer.buffer;
2868
2869 if (rcu_is_watching()) {
2870 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2871 return;
2872 }
2873
2874 /*
2875 * When an NMI triggers, RCU is enabled via rcu_nmi_enter(),
2876 * but if the above rcu_is_watching() failed, then the NMI
2877 * triggered someplace critical, and rcu_irq_enter() should
2878 * not be called from NMI.
2879 */
2880 if (unlikely(in_nmi()))
2881 return;
2882
Steven Rostedt (VMware)a33d7d92017-05-12 13:15:45 -04002883 rcu_irq_enter_irqson();
2884 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2885 rcu_irq_exit_irqson();
Steven Rostedt38697052008-10-01 13:14:09 -04002886}
2887
Steven Rostedt03889382009-12-11 09:48:22 -05002888/**
2889 * trace_dump_stack - record a stack back trace in the trace buffer
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002890 * @skip: Number of functions to skip (helper handlers)
Steven Rostedt03889382009-12-11 09:48:22 -05002891 */
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002892void trace_dump_stack(int skip)
Steven Rostedt03889382009-12-11 09:48:22 -05002893{
2894 unsigned long flags;
2895
2896 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05002897 return;
Steven Rostedt03889382009-12-11 09:48:22 -05002898
2899 local_save_flags(flags);
2900
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002901#ifndef CONFIG_UNWINDER_ORC
2902 /* Skip 1 to skip this function. */
2903 skip++;
2904#endif
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002905 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
2906 flags, skip, preempt_count(), NULL);
Steven Rostedt03889382009-12-11 09:48:22 -05002907}
Nikolay Borisovda387e52018-10-17 09:51:43 +03002908EXPORT_SYMBOL_GPL(trace_dump_stack);
Steven Rostedt03889382009-12-11 09:48:22 -05002909
Steven Rostedt91e86e52010-11-10 12:56:12 +01002910static DEFINE_PER_CPU(int, user_stack_count);
2911
Steven Rostedte77405a2009-09-02 14:17:06 -04002912void
2913ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02002914{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002915 struct trace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02002916 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02002917 struct userstack_entry *entry;
2918 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02002919
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04002920 if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE))
Török Edwin02b67512008-11-22 13:28:47 +02002921 return;
2922
Steven Rostedtb6345872010-03-12 20:03:30 -05002923 /*
2924 * NMIs can not handle page faults, even with fix ups.
2925 * The save user stack can (and often does) fault.
2926 */
2927 if (unlikely(in_nmi()))
2928 return;
2929
Steven Rostedt91e86e52010-11-10 12:56:12 +01002930 /*
2931 * prevent recursion, since the user stack tracing may
2932 * trigger other kernel events.
2933 */
2934 preempt_disable();
2935 if (__this_cpu_read(user_stack_count))
2936 goto out;
2937
2938 __this_cpu_inc(user_stack_count);
2939
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002940 event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
2941 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02002942 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08002943 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02002944 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02002945
Steven Rostedt48659d32009-09-11 11:36:23 -04002946 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02002947 memset(&entry->caller, 0, sizeof(entry->caller));
2948
2949 trace.nr_entries = 0;
2950 trace.max_entries = FTRACE_STACK_ENTRIES;
2951 trace.skip = 0;
2952 trace.entries = entry->caller;
2953
2954 save_stack_trace_user(&trace);
Tom Zanussif306cc82013-10-24 08:34:17 -05002955 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002956 __buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01002957
Li Zefan1dbd1952010-12-09 15:47:56 +08002958 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01002959 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01002960 out:
2961 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02002962}
2963
Hannes Eder4fd27352009-02-10 19:44:12 +01002964#ifdef UNUSED
2965static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02002966{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05002967 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02002968}
Hannes Eder4fd27352009-02-10 19:44:12 +01002969#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02002970
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002971#endif /* CONFIG_STACKTRACE */
2972
Steven Rostedt07d777f2011-09-22 14:01:55 -04002973/* created for use with alloc_percpu */
2974struct trace_buffer_struct {
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002975 int nesting;
2976 char buffer[4][TRACE_BUF_SIZE];
Steven Rostedt07d777f2011-09-22 14:01:55 -04002977};
2978
2979static struct trace_buffer_struct *trace_percpu_buffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002980
2981/*
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002982 * Thise allows for lockless recording. If we're nested too deeply, then
2983 * this returns NULL.
Steven Rostedt07d777f2011-09-22 14:01:55 -04002984 */
2985static char *get_trace_buf(void)
2986{
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002987 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
Steven Rostedt07d777f2011-09-22 14:01:55 -04002988
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002989 if (!buffer || buffer->nesting >= 4)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002990 return NULL;
2991
Steven Rostedt (VMware)3d9622c2017-09-05 11:32:01 -04002992 buffer->nesting++;
2993
2994 /* Interrupts must see nesting incremented before we use the buffer */
2995 barrier();
2996 return &buffer->buffer[buffer->nesting][0];
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002997}
2998
2999static void put_trace_buf(void)
3000{
Steven Rostedt (VMware)3d9622c2017-09-05 11:32:01 -04003001 /* Don't let the decrement of nesting leak before this */
3002 barrier();
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003003 this_cpu_dec(trace_percpu_buffer->nesting);
Steven Rostedt07d777f2011-09-22 14:01:55 -04003004}
3005
3006static int alloc_percpu_trace_buffer(void)
3007{
3008 struct trace_buffer_struct *buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003009
3010 buffers = alloc_percpu(struct trace_buffer_struct);
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003011 if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
3012 return -ENOMEM;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003013
3014 trace_percpu_buffer = buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003015 return 0;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003016}
3017
Steven Rostedt81698832012-10-11 10:15:05 -04003018static int buffers_allocated;
3019
Steven Rostedt07d777f2011-09-22 14:01:55 -04003020void trace_printk_init_buffers(void)
3021{
Steven Rostedt07d777f2011-09-22 14:01:55 -04003022 if (buffers_allocated)
3023 return;
3024
3025 if (alloc_percpu_trace_buffer())
3026 return;
3027
Steven Rostedt2184db42014-05-28 13:14:40 -04003028 /* trace_printk() is for debug use only. Don't use it in production. */
3029
Joe Perchesa395d6a2016-03-22 14:28:09 -07003030 pr_warn("\n");
3031 pr_warn("**********************************************************\n");
3032 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3033 pr_warn("** **\n");
3034 pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
3035 pr_warn("** **\n");
3036 pr_warn("** This means that this is a DEBUG kernel and it is **\n");
3037 pr_warn("** unsafe for production use. **\n");
3038 pr_warn("** **\n");
3039 pr_warn("** If you see this message and you are not debugging **\n");
3040 pr_warn("** the kernel, report this immediately to your vendor! **\n");
3041 pr_warn("** **\n");
3042 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3043 pr_warn("**********************************************************\n");
Steven Rostedt07d777f2011-09-22 14:01:55 -04003044
Steven Rostedtb382ede62012-10-10 21:44:34 -04003045 /* Expand the buffers to set size */
3046 tracing_update_buffers();
3047
Steven Rostedt07d777f2011-09-22 14:01:55 -04003048 buffers_allocated = 1;
Steven Rostedt81698832012-10-11 10:15:05 -04003049
3050 /*
3051 * trace_printk_init_buffers() can be called by modules.
3052 * If that happens, then we need to start cmdline recording
3053 * directly here. If the global_trace.buffer is already
3054 * allocated here, then this was called by module code.
3055 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003056 if (global_trace.trace_buffer.buffer)
Steven Rostedt81698832012-10-11 10:15:05 -04003057 tracing_start_cmdline_record();
3058}
3059
3060void trace_printk_start_comm(void)
3061{
3062 /* Start tracing comms if trace printk is set */
3063 if (!buffers_allocated)
3064 return;
3065 tracing_start_cmdline_record();
3066}
3067
3068static void trace_printk_start_stop_comm(int enabled)
3069{
3070 if (!buffers_allocated)
3071 return;
3072
3073 if (enabled)
3074 tracing_start_cmdline_record();
3075 else
3076 tracing_stop_cmdline_record();
Steven Rostedt07d777f2011-09-22 14:01:55 -04003077}
3078
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003079/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003080 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003081 *
3082 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04003083int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003084{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003085 struct trace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003086 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04003087 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003088 struct trace_array *tr = &global_trace;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003089 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003090 unsigned long flags;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003091 char *tbuffer;
3092 int len = 0, size, pc;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003093
3094 if (unlikely(tracing_selftest_running || tracing_disabled))
3095 return 0;
3096
3097 /* Don't pollute graph traces with trace_vprintk internals */
3098 pause_graph_tracing();
3099
3100 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04003101 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003102
Steven Rostedt07d777f2011-09-22 14:01:55 -04003103 tbuffer = get_trace_buf();
3104 if (!tbuffer) {
3105 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003106 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003107 }
3108
3109 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
3110
3111 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003112 goto out;
3113
Steven Rostedt07d777f2011-09-22 14:01:55 -04003114 local_save_flags(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003115 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003116 buffer = tr->trace_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05003117 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
3118 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003119 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04003120 goto out;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003121 entry = ring_buffer_event_data(event);
3122 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003123 entry->fmt = fmt;
3124
Steven Rostedt07d777f2011-09-22 14:01:55 -04003125 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
Tom Zanussif306cc82013-10-24 08:34:17 -05003126 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04003127 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04003128 ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05003129 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003130
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003131out:
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003132 put_trace_buf();
3133
3134out_nobuffer:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003135 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003136 unpause_graph_tracing();
3137
3138 return len;
3139}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003140EXPORT_SYMBOL_GPL(trace_vbprintk);
3141
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003142__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003143static int
3144__trace_array_vprintk(struct ring_buffer *buffer,
3145 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003146{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003147 struct trace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003148 struct ring_buffer_event *event;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003149 int len = 0, size, pc;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003150 struct print_entry *entry;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003151 unsigned long flags;
3152 char *tbuffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003153
3154 if (tracing_disabled || tracing_selftest_running)
3155 return 0;
3156
Steven Rostedt07d777f2011-09-22 14:01:55 -04003157 /* Don't pollute graph traces with trace_vprintk internals */
3158 pause_graph_tracing();
3159
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003160 pc = preempt_count();
3161 preempt_disable_notrace();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003162
Steven Rostedt07d777f2011-09-22 14:01:55 -04003163
3164 tbuffer = get_trace_buf();
3165 if (!tbuffer) {
3166 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003167 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003168 }
3169
Dan Carpenter3558a5a2014-11-27 18:57:52 +03003170 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003171
Steven Rostedt07d777f2011-09-22 14:01:55 -04003172 local_save_flags(flags);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003173 size = sizeof(*entry) + len + 1;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05003174 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3175 flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003176 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04003177 goto out;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003178 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01003179 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003180
Dan Carpenter3558a5a2014-11-27 18:57:52 +03003181 memcpy(&entry->buf, tbuffer, len + 1);
Tom Zanussif306cc82013-10-24 08:34:17 -05003182 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04003183 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04003184 ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05003185 }
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003186
3187out:
3188 put_trace_buf();
3189
3190out_nobuffer:
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003191 preempt_enable_notrace();
Steven Rostedt07d777f2011-09-22 14:01:55 -04003192 unpause_graph_tracing();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003193
3194 return len;
3195}
Steven Rostedt659372d2009-09-03 19:11:07 -04003196
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003197__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003198int trace_array_vprintk(struct trace_array *tr,
3199 unsigned long ip, const char *fmt, va_list args)
3200{
3201 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
3202}
3203
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003204__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003205int trace_array_printk(struct trace_array *tr,
3206 unsigned long ip, const char *fmt, ...)
3207{
3208 int ret;
3209 va_list ap;
3210
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003211 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003212 return 0;
3213
3214 va_start(ap, fmt);
3215 ret = trace_array_vprintk(tr, ip, fmt, ap);
3216 va_end(ap);
3217 return ret;
3218}
3219
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003220__printf(3, 4)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003221int trace_array_printk_buf(struct ring_buffer *buffer,
3222 unsigned long ip, const char *fmt, ...)
3223{
3224 int ret;
3225 va_list ap;
3226
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003227 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003228 return 0;
3229
3230 va_start(ap, fmt);
3231 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3232 va_end(ap);
3233 return ret;
3234}
3235
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003236__printf(2, 0)
Steven Rostedt659372d2009-09-03 19:11:07 -04003237int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3238{
Steven Rostedta813a152009-10-09 01:41:35 -04003239 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04003240}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003241EXPORT_SYMBOL_GPL(trace_vprintk);
3242
Robert Richtere2ac8ef2008-11-12 12:59:32 +01003243static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04003244{
Steven Rostedt6d158a82012-06-27 20:46:14 -04003245 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3246
Steven Rostedt5a90f572008-09-03 17:42:51 -04003247 iter->idx++;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003248 if (buf_iter)
3249 ring_buffer_read(buf_iter, NULL);
Steven Rostedt5a90f572008-09-03 17:42:51 -04003250}
3251
Ingo Molnare309b412008-05-12 21:20:51 +02003252static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04003253peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3254 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003255{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003256 struct ring_buffer_event *event;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003257 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003258
Steven Rostedtd7690412008-10-01 00:29:53 -04003259 if (buf_iter)
3260 event = ring_buffer_iter_peek(buf_iter, ts);
3261 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003262 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04003263 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04003264
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04003265 if (event) {
3266 iter->ent_size = ring_buffer_event_length(event);
3267 return ring_buffer_event_data(event);
3268 }
3269 iter->ent_size = 0;
3270 return NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003271}
Steven Rostedtd7690412008-10-01 00:29:53 -04003272
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003273static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04003274__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3275 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003276{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003277 struct ring_buffer *buffer = iter->trace_buffer->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003278 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08003279 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003280 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003281 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003282 int next_cpu = -1;
Steven Rostedt12b5da32012-03-27 10:43:28 -04003283 int next_size = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003284 int cpu;
3285
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003286 /*
3287 * If we are in a per_cpu trace file, don't bother by iterating over
3288 * all cpu and peek directly.
3289 */
Steven Rostedtae3b5092013-01-23 15:22:59 -05003290 if (cpu_file > RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003291 if (ring_buffer_empty_cpu(buffer, cpu_file))
3292 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04003293 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003294 if (ent_cpu)
3295 *ent_cpu = cpu_file;
3296
3297 return ent;
3298 }
3299
Steven Rostedtab464282008-05-12 21:21:00 +02003300 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003301
3302 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003303 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003304
Steven Rostedtbc21b472010-03-31 19:49:26 -04003305 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003306
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02003307 /*
3308 * Pick the entry with the smallest timestamp:
3309 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003310 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003311 next = ent;
3312 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003313 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04003314 next_lost = lost_events;
Steven Rostedt12b5da32012-03-27 10:43:28 -04003315 next_size = iter->ent_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003316 }
3317 }
3318
Steven Rostedt12b5da32012-03-27 10:43:28 -04003319 iter->ent_size = next_size;
3320
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003321 if (ent_cpu)
3322 *ent_cpu = next_cpu;
3323
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003324 if (ent_ts)
3325 *ent_ts = next_ts;
3326
Steven Rostedtbc21b472010-03-31 19:49:26 -04003327 if (missing_events)
3328 *missing_events = next_lost;
3329
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003330 return next;
3331}
3332
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003333/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003334struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3335 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02003336{
Steven Rostedtbc21b472010-03-31 19:49:26 -04003337 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003338}
Ingo Molnar8c523a92008-05-12 21:20:46 +02003339
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003340/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05003341void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003342{
Steven Rostedtbc21b472010-03-31 19:49:26 -04003343 iter->ent = __find_next_entry(iter, &iter->cpu,
3344 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003345
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003346 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01003347 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003348
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003349 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003350}
3351
Ingo Molnare309b412008-05-12 21:20:51 +02003352static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02003353{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003354 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04003355 &iter->lost_events);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003356}
3357
Ingo Molnare309b412008-05-12 21:20:51 +02003358static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003359{
3360 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003361 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003362 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003363
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003364 WARN_ON_ONCE(iter->leftover);
3365
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003366 (*pos)++;
3367
3368 /* can't go backwards */
3369 if (iter->idx > i)
3370 return NULL;
3371
3372 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05003373 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003374 else
3375 ent = iter;
3376
3377 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05003378 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003379
3380 iter->pos = *pos;
3381
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003382 return ent;
3383}
3384
Jason Wessel955b61e2010-08-05 09:22:23 -05003385void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003386{
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003387 struct ring_buffer_event *event;
3388 struct ring_buffer_iter *buf_iter;
3389 unsigned long entries = 0;
3390 u64 ts;
3391
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003392 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003393
Steven Rostedt6d158a82012-06-27 20:46:14 -04003394 buf_iter = trace_buffer_iter(iter, cpu);
3395 if (!buf_iter)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003396 return;
3397
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003398 ring_buffer_iter_reset(buf_iter);
3399
3400 /*
3401 * We could have the case with the max latency tracers
3402 * that a reset never took place on a cpu. This is evident
3403 * by the timestamp being before the start of the buffer.
3404 */
3405 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003406 if (ts >= iter->trace_buffer->time_start)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003407 break;
3408 entries++;
3409 ring_buffer_read(buf_iter, NULL);
3410 }
3411
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003412 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003413}
3414
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003415/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003416 * The current tracer is copied to avoid a global locking
3417 * all around.
3418 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003419static void *s_start(struct seq_file *m, loff_t *pos)
3420{
3421 struct trace_iterator *iter = m->private;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003422 struct trace_array *tr = iter->tr;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003423 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003424 void *p = NULL;
3425 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003426 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003427
Hiraku Toyooka2fd196e2012-12-26 11:52:52 +09003428 /*
3429 * copy the tracer to avoid using a global lock all around.
3430 * iter->trace is a copy of current_trace, the pointer to the
3431 * name may be used instead of a strcmp(), as iter->trace->name
3432 * will point to the same string as current_trace->name.
3433 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003434 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003435 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
3436 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003437 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003438
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003439#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003440 if (iter->snapshot && iter->trace->use_max_tr)
3441 return ERR_PTR(-EBUSY);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003442#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003443
3444 if (!iter->snapshot)
Joel Fernandesd914ba32017-06-26 19:01:55 -07003445 atomic_inc(&trace_record_taskinfo_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003446
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003447 if (*pos != iter->pos) {
3448 iter->ent = NULL;
3449 iter->cpu = 0;
3450 iter->idx = -1;
3451
Steven Rostedtae3b5092013-01-23 15:22:59 -05003452 if (cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003453 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003454 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003455 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003456 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003457
Lai Jiangshanac91d852010-03-02 17:54:50 +08003458 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003459 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
3460 ;
3461
3462 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003463 /*
3464 * If we overflowed the seq_file before, then we want
3465 * to just reuse the trace_seq buffer again.
3466 */
3467 if (iter->leftover)
3468 p = iter;
3469 else {
3470 l = *pos - 1;
3471 p = s_next(m, p, &l);
3472 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003473 }
3474
Lai Jiangshan4f535962009-05-18 19:35:34 +08003475 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003476 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003477 return p;
3478}
3479
3480static void s_stop(struct seq_file *m, void *p)
3481{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003482 struct trace_iterator *iter = m->private;
3483
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003484#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003485 if (iter->snapshot && iter->trace->use_max_tr)
3486 return;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003487#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003488
3489 if (!iter->snapshot)
Joel Fernandesd914ba32017-06-26 19:01:55 -07003490 atomic_dec(&trace_record_taskinfo_disabled);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003491
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003492 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003493 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003494}
3495
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003496static void
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003497get_total_entries(struct trace_buffer *buf,
3498 unsigned long *total, unsigned long *entries)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003499{
3500 unsigned long count;
3501 int cpu;
3502
3503 *total = 0;
3504 *entries = 0;
3505
3506 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003507 count = ring_buffer_entries_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003508 /*
3509 * If this buffer has skipped entries, then we hold all
3510 * entries for the trace and we need to ignore the
3511 * ones before the time stamp.
3512 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003513 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3514 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003515 /* total is the same as the entries */
3516 *total += count;
3517 } else
3518 *total += count +
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003519 ring_buffer_overrun_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003520 *entries += count;
3521 }
3522}
3523
Ingo Molnare309b412008-05-12 21:20:51 +02003524static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003525{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003526 seq_puts(m, "# _------=> CPU# \n"
3527 "# / _-----=> irqs-off \n"
3528 "# | / _----=> need-resched \n"
3529 "# || / _---=> hardirq/softirq \n"
3530 "# ||| / _--=> preempt-depth \n"
3531 "# |||| / delay \n"
3532 "# cmd pid ||||| time | caller \n"
3533 "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003534}
3535
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003536static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003537{
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003538 unsigned long total;
3539 unsigned long entries;
3540
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003541 get_total_entries(buf, &total, &entries);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003542 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
3543 entries, total, num_online_cpus());
3544 seq_puts(m, "#\n");
3545}
3546
Joel Fernandes441dae82017-06-25 22:38:43 -07003547static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m,
3548 unsigned int flags)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003549{
Joel Fernandes441dae82017-06-25 22:38:43 -07003550 bool tgid = flags & TRACE_ITER_RECORD_TGID;
3551
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003552 print_event_info(buf, m);
Joel Fernandes441dae82017-06-25 22:38:43 -07003553
Joel Fernandes (Google)f8494fa2018-06-25 17:08:22 -07003554 seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? "TGID " : "");
3555 seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003556}
3557
Joel Fernandes441dae82017-06-25 22:38:43 -07003558static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m,
3559 unsigned int flags)
Steven Rostedt77271ce2011-11-17 09:34:33 -05003560{
Joel Fernandes441dae82017-06-25 22:38:43 -07003561 bool tgid = flags & TRACE_ITER_RECORD_TGID;
Steven Rostedt (VMware)b11fb732017-07-11 15:43:24 -04003562 const char tgid_space[] = " ";
3563 const char space[] = " ";
Joel Fernandes441dae82017-06-25 22:38:43 -07003564
Quentin Perret9e738212019-02-14 15:29:50 +00003565 print_event_info(buf, m);
3566
Steven Rostedt (VMware)b11fb732017-07-11 15:43:24 -04003567 seq_printf(m, "# %s _-----=> irqs-off\n",
3568 tgid ? tgid_space : space);
3569 seq_printf(m, "# %s / _----=> need-resched\n",
3570 tgid ? tgid_space : space);
3571 seq_printf(m, "# %s| / _---=> hardirq/softirq\n",
3572 tgid ? tgid_space : space);
3573 seq_printf(m, "# %s|| / _--=> preempt-depth\n",
3574 tgid ? tgid_space : space);
3575 seq_printf(m, "# %s||| / delay\n",
3576 tgid ? tgid_space : space);
Joel Fernandes (Google)f8494fa2018-06-25 17:08:22 -07003577 seq_printf(m, "# TASK-PID %sCPU# |||| TIMESTAMP FUNCTION\n",
Steven Rostedt (VMware)b11fb732017-07-11 15:43:24 -04003578 tgid ? " TGID " : space);
Joel Fernandes (Google)f8494fa2018-06-25 17:08:22 -07003579 seq_printf(m, "# | | %s | |||| | |\n",
Steven Rostedt (VMware)b11fb732017-07-11 15:43:24 -04003580 tgid ? " | " : space);
Steven Rostedt77271ce2011-11-17 09:34:33 -05003581}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003582
Jiri Olsa62b915f2010-04-02 19:01:22 +02003583void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003584print_trace_header(struct seq_file *m, struct trace_iterator *iter)
3585{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003586 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003587 struct trace_buffer *buf = iter->trace_buffer;
3588 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003589 struct tracer *type = iter->trace;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003590 unsigned long entries;
3591 unsigned long total;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003592 const char *name = "preemption";
3593
Steven Rostedt (Red Hat)d840f712013-02-01 18:38:47 -05003594 name = type->name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003595
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003596 get_total_entries(buf, &total, &entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003597
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003598 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003599 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003600 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003601 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003602 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003603 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02003604 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003605 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003606 total,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003607 buf->cpu,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003608#if defined(CONFIG_PREEMPT_NONE)
3609 "server",
3610#elif defined(CONFIG_PREEMPT_VOLUNTARY)
3611 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04003612#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003613 "preempt",
3614#else
3615 "unknown",
3616#endif
3617 /* These are reserved for later use */
3618 0, 0, 0, 0);
3619#ifdef CONFIG_SMP
3620 seq_printf(m, " #P:%d)\n", num_online_cpus());
3621#else
3622 seq_puts(m, ")\n");
3623#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003624 seq_puts(m, "# -----------------\n");
3625 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003626 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
Eric W. Biedermand20b92a2012-03-13 16:02:19 -07003627 data->comm, data->pid,
3628 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003629 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003630 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003631
3632 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003633 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02003634 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
3635 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003636 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02003637 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
3638 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04003639 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003640 }
3641
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003642 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003643}
3644
Steven Rostedta3097202008-11-07 22:36:02 -05003645static void test_cpu_buff_start(struct trace_iterator *iter)
3646{
3647 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003648 struct trace_array *tr = iter->tr;
Steven Rostedta3097202008-11-07 22:36:02 -05003649
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003650 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003651 return;
3652
3653 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
3654 return;
3655
Matthias Kaehlcke4dbbe2d2017-04-21 16:41:10 -07003656 if (cpumask_available(iter->started) &&
3657 cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05003658 return;
3659
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003660 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003661 return;
3662
Matthias Kaehlcke4dbbe2d2017-04-21 16:41:10 -07003663 if (cpumask_available(iter->started))
Sasha Levin919cd972015-09-04 12:45:56 -04003664 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003665
3666 /* Don't print started cpu buffer for the first entry of the trace */
3667 if (iter->idx > 1)
3668 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
3669 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05003670}
3671
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003672static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003673{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003674 struct trace_array *tr = iter->tr;
Steven Rostedt214023c2008-05-12 21:20:46 +02003675 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003676 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003677 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003678 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003679
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003680 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003681
Steven Rostedta3097202008-11-07 22:36:02 -05003682 test_cpu_buff_start(iter);
3683
Steven Rostedtf633cef2008-12-23 23:24:13 -05003684 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003685
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003686 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003687 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3688 trace_print_lat_context(iter);
3689 else
3690 trace_print_context(iter);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003691 }
3692
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003693 if (trace_seq_has_overflowed(s))
3694 return TRACE_TYPE_PARTIAL_LINE;
3695
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003696 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003697 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003698
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003699 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003700
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003701 return trace_handle_return(s);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003702}
3703
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003704static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003705{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003706 struct trace_array *tr = iter->tr;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003707 struct trace_seq *s = &iter->seq;
3708 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003709 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003710
3711 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003712
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003713 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003714 trace_seq_printf(s, "%d %d %llu ",
3715 entry->pid, iter->cpu, iter->ts);
3716
3717 if (trace_seq_has_overflowed(s))
3718 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003719
Steven Rostedtf633cef2008-12-23 23:24:13 -05003720 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003721 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003722 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003723
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003724 trace_seq_printf(s, "%d ?\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003725
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003726 return trace_handle_return(s);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003727}
3728
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003729static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003730{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003731 struct trace_array *tr = iter->tr;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003732 struct trace_seq *s = &iter->seq;
3733 unsigned char newline = '\n';
3734 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003735 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003736
3737 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003738
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003739 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003740 SEQ_PUT_HEX_FIELD(s, entry->pid);
3741 SEQ_PUT_HEX_FIELD(s, iter->cpu);
3742 SEQ_PUT_HEX_FIELD(s, iter->ts);
3743 if (trace_seq_has_overflowed(s))
3744 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003745 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003746
Steven Rostedtf633cef2008-12-23 23:24:13 -05003747 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003748 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04003749 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003750 if (ret != TRACE_TYPE_HANDLED)
3751 return ret;
3752 }
Steven Rostedt7104f302008-10-01 10:52:51 -04003753
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003754 SEQ_PUT_FIELD(s, newline);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003755
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003756 return trace_handle_return(s);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003757}
3758
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003759static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003760{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003761 struct trace_array *tr = iter->tr;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003762 struct trace_seq *s = &iter->seq;
3763 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003764 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003765
3766 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003767
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003768 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003769 SEQ_PUT_FIELD(s, entry->pid);
3770 SEQ_PUT_FIELD(s, iter->cpu);
3771 SEQ_PUT_FIELD(s, iter->ts);
3772 if (trace_seq_has_overflowed(s))
3773 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003774 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003775
Steven Rostedtf633cef2008-12-23 23:24:13 -05003776 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04003777 return event ? event->funcs->binary(iter, 0, event) :
3778 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003779}
3780
Jiri Olsa62b915f2010-04-02 19:01:22 +02003781int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003782{
Steven Rostedt6d158a82012-06-27 20:46:14 -04003783 struct ring_buffer_iter *buf_iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003784 int cpu;
3785
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003786 /* If we are looking at one CPU buffer, only check that one */
Steven Rostedtae3b5092013-01-23 15:22:59 -05003787 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003788 cpu = iter->cpu_file;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003789 buf_iter = trace_buffer_iter(iter, cpu);
3790 if (buf_iter) {
3791 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003792 return 0;
3793 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003794 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003795 return 0;
3796 }
3797 return 1;
3798 }
3799
Steven Rostedtab464282008-05-12 21:21:00 +02003800 for_each_tracing_cpu(cpu) {
Steven Rostedt6d158a82012-06-27 20:46:14 -04003801 buf_iter = trace_buffer_iter(iter, cpu);
3802 if (buf_iter) {
3803 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedtd7690412008-10-01 00:29:53 -04003804 return 0;
3805 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003806 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedtd7690412008-10-01 00:29:53 -04003807 return 0;
3808 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003809 }
Steven Rostedtd7690412008-10-01 00:29:53 -04003810
Frederic Weisbecker797d3712008-09-30 18:13:45 +02003811 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003812}
3813
Lai Jiangshan4f535962009-05-18 19:35:34 +08003814/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05003815enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003816{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003817 struct trace_array *tr = iter->tr;
3818 unsigned long trace_flags = tr->trace_flags;
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003819 enum print_line_t ret;
3820
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003821 if (iter->lost_events) {
3822 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3823 iter->cpu, iter->lost_events);
3824 if (trace_seq_has_overflowed(&iter->seq))
3825 return TRACE_TYPE_PARTIAL_LINE;
3826 }
Steven Rostedtbc21b472010-03-31 19:49:26 -04003827
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003828 if (iter->trace && iter->trace->print_line) {
3829 ret = iter->trace->print_line(iter);
3830 if (ret != TRACE_TYPE_UNHANDLED)
3831 return ret;
3832 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02003833
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -05003834 if (iter->ent->type == TRACE_BPUTS &&
3835 trace_flags & TRACE_ITER_PRINTK &&
3836 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3837 return trace_print_bputs_msg_only(iter);
3838
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003839 if (iter->ent->type == TRACE_BPRINT &&
3840 trace_flags & TRACE_ITER_PRINTK &&
3841 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04003842 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003843
Frederic Weisbecker66896a82008-12-13 20:18:13 +01003844 if (iter->ent->type == TRACE_PRINT &&
3845 trace_flags & TRACE_ITER_PRINTK &&
3846 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04003847 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01003848
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003849 if (trace_flags & TRACE_ITER_BIN)
3850 return print_bin_fmt(iter);
3851
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003852 if (trace_flags & TRACE_ITER_HEX)
3853 return print_hex_fmt(iter);
3854
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003855 if (trace_flags & TRACE_ITER_RAW)
3856 return print_raw_fmt(iter);
3857
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003858 return print_trace_fmt(iter);
3859}
3860
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003861void trace_latency_header(struct seq_file *m)
3862{
3863 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003864 struct trace_array *tr = iter->tr;
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003865
3866 /* print nothing if the buffers are empty */
3867 if (trace_empty(iter))
3868 return;
3869
3870 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3871 print_trace_header(m, iter);
3872
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003873 if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003874 print_lat_help_header(m);
3875}
3876
Jiri Olsa62b915f2010-04-02 19:01:22 +02003877void trace_default_header(struct seq_file *m)
3878{
3879 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003880 struct trace_array *tr = iter->tr;
3881 unsigned long trace_flags = tr->trace_flags;
Jiri Olsa62b915f2010-04-02 19:01:22 +02003882
Jiri Olsaf56e7f82011-06-03 16:58:49 +02003883 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
3884 return;
3885
Jiri Olsa62b915f2010-04-02 19:01:22 +02003886 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
3887 /* print nothing if the buffers are empty */
3888 if (trace_empty(iter))
3889 return;
3890 print_trace_header(m, iter);
3891 if (!(trace_flags & TRACE_ITER_VERBOSE))
3892 print_lat_help_header(m);
3893 } else {
Steven Rostedt77271ce2011-11-17 09:34:33 -05003894 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
3895 if (trace_flags & TRACE_ITER_IRQ_INFO)
Joel Fernandes441dae82017-06-25 22:38:43 -07003896 print_func_help_header_irq(iter->trace_buffer,
3897 m, trace_flags);
Steven Rostedt77271ce2011-11-17 09:34:33 -05003898 else
Joel Fernandes441dae82017-06-25 22:38:43 -07003899 print_func_help_header(iter->trace_buffer, m,
3900 trace_flags);
Steven Rostedt77271ce2011-11-17 09:34:33 -05003901 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02003902 }
3903}
3904
Steven Rostedte0a413f2011-09-29 21:26:16 -04003905static void test_ftrace_alive(struct seq_file *m)
3906{
3907 if (!ftrace_is_dead())
3908 return;
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003909 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
3910 "# MAY BE MISSING FUNCTION EVENTS\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04003911}
3912
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003913#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003914static void show_snapshot_main_help(struct seq_file *m)
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003915{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003916 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
3917 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3918 "# Takes a snapshot of the main buffer.\n"
3919 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
3920 "# (Doesn't have to be '2' works with any number that\n"
3921 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003922}
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003923
3924static void show_snapshot_percpu_help(struct seq_file *m)
3925{
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003926 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003927#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003928 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3929 "# Takes a snapshot of the main buffer for this cpu.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003930#else
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003931 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
3932 "# Must use main snapshot file to allocate.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003933#endif
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003934 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
3935 "# (Doesn't have to be '2' works with any number that\n"
3936 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003937}
3938
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003939static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
3940{
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05003941 if (iter->tr->allocated_snapshot)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003942 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003943 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003944 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003945
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003946 seq_puts(m, "# Snapshot commands:\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003947 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
3948 show_snapshot_main_help(m);
3949 else
3950 show_snapshot_percpu_help(m);
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003951}
3952#else
3953/* Should never be called */
3954static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
3955#endif
3956
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003957static int s_show(struct seq_file *m, void *v)
3958{
3959 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003960 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003961
3962 if (iter->ent == NULL) {
3963 if (iter->tr) {
3964 seq_printf(m, "# tracer: %s\n", iter->trace->name);
3965 seq_puts(m, "#\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04003966 test_ftrace_alive(m);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003967 }
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003968 if (iter->snapshot && trace_empty(iter))
3969 print_snapshot_help(m, iter);
3970 else if (iter->trace && iter->trace->print_header)
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003971 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02003972 else
3973 trace_default_header(m);
3974
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003975 } else if (iter->leftover) {
3976 /*
3977 * If we filled the seq_file buffer earlier, we
3978 * want to just show it now.
3979 */
3980 ret = trace_print_seq(m, &iter->seq);
3981
3982 /* ret should this time be zero, but you never know */
3983 iter->leftover = ret;
3984
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003985 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003986 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003987 ret = trace_print_seq(m, &iter->seq);
3988 /*
3989 * If we overflow the seq_file buffer, then it will
3990 * ask us for this data again at start up.
3991 * Use that instead.
3992 * ret is 0 if seq_file write succeeded.
3993 * -1 otherwise.
3994 */
3995 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003996 }
3997
3998 return 0;
3999}
4000
Oleg Nesterov649e9c702013-07-23 17:25:54 +02004001/*
4002 * Should be used after trace_array_get(), trace_types_lock
4003 * ensures that i_cdev was already initialized.
4004 */
4005static inline int tracing_get_cpu(struct inode *inode)
4006{
4007 if (inode->i_cdev) /* See trace_create_cpu_file() */
4008 return (long)inode->i_cdev - 1;
4009 return RING_BUFFER_ALL_CPUS;
4010}
4011
James Morris88e9d342009-09-22 16:43:43 -07004012static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004013 .start = s_start,
4014 .next = s_next,
4015 .stop = s_stop,
4016 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004017};
4018
Ingo Molnare309b412008-05-12 21:20:51 +02004019static struct trace_iterator *
Oleg Nesterov6484c712013-07-23 17:26:10 +02004020__tracing_open(struct inode *inode, struct file *file, bool snapshot)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004021{
Oleg Nesterov6484c712013-07-23 17:26:10 +02004022 struct trace_array *tr = inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004023 struct trace_iterator *iter;
Jiri Olsa50e18b92012-04-25 10:23:39 +02004024 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004025
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004026 if (tracing_disabled)
4027 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02004028
Jiri Olsa50e18b92012-04-25 10:23:39 +02004029 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004030 if (!iter)
4031 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004032
Gil Fruchter72917232015-06-09 10:32:35 +03004033 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
Steven Rostedt6d158a82012-06-27 20:46:14 -04004034 GFP_KERNEL);
Dan Carpenter93574fc2012-07-11 09:35:08 +03004035 if (!iter->buffer_iter)
4036 goto release;
4037
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004038 /*
4039 * We make a copy of the current tracer to avoid concurrent
4040 * changes on it while we are reading.
4041 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004042 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004043 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004044 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004045 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004046
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004047 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004048
Li Zefan79f55992009-06-15 14:58:26 +08004049 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02004050 goto fail;
4051
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004052 iter->tr = tr;
4053
4054#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004055 /* Currently only the top directory has a snapshot */
4056 if (tr->current_trace->print_max || snapshot)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004057 iter->trace_buffer = &tr->max_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004058 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004059#endif
4060 iter->trace_buffer = &tr->trace_buffer;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004061 iter->snapshot = snapshot;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004062 iter->pos = -1;
Oleg Nesterov6484c712013-07-23 17:26:10 +02004063 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004064 mutex_init(&iter->mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004065
Markus Metzger8bba1bf2008-11-25 09:12:31 +01004066 /* Notify the tracer early; before we stop tracing. */
4067 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01004068 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01004069
Steven Rostedt12ef7d42008-11-12 17:52:38 -05004070 /* Annotate start of buffers if we had overruns */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004071 if (ring_buffer_overruns(iter->trace_buffer->buffer))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05004072 iter->iter_flags |= TRACE_FILE_ANNOTATE;
4073
David Sharp8be07092012-11-13 12:18:22 -08004074 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09004075 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08004076 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4077
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004078 /* stop the trace while dumping if we are not opening "snapshot" */
4079 if (!iter->snapshot)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004080 tracing_stop_tr(tr);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04004081
Steven Rostedtae3b5092013-01-23 15:22:59 -05004082 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004083 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004084 iter->buffer_iter[cpu] =
Douglas Anderson31b265b2019-03-08 11:32:04 -08004085 ring_buffer_read_prepare(iter->trace_buffer->buffer,
4086 cpu, GFP_KERNEL);
David Miller72c9ddf2010-04-20 15:47:11 -07004087 }
4088 ring_buffer_read_prepare_sync();
4089 for_each_tracing_cpu(cpu) {
4090 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04004091 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004092 }
4093 } else {
4094 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004095 iter->buffer_iter[cpu] =
Douglas Anderson31b265b2019-03-08 11:32:04 -08004096 ring_buffer_read_prepare(iter->trace_buffer->buffer,
4097 cpu, GFP_KERNEL);
David Miller72c9ddf2010-04-20 15:47:11 -07004098 ring_buffer_read_prepare_sync();
4099 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04004100 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004101 }
4102
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004103 mutex_unlock(&trace_types_lock);
4104
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004105 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004106
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004107 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004108 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004109 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04004110 kfree(iter->buffer_iter);
Dan Carpenter93574fc2012-07-11 09:35:08 +03004111release:
Jiri Olsa50e18b92012-04-25 10:23:39 +02004112 seq_release_private(inode, file);
4113 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004114}
4115
4116int tracing_open_generic(struct inode *inode, struct file *filp)
4117{
Steven Rostedt60a11772008-05-12 21:20:44 +02004118 if (tracing_disabled)
4119 return -ENODEV;
4120
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004121 filp->private_data = inode->i_private;
4122 return 0;
4123}
4124
Geyslan G. Bem2e864212013-10-18 21:15:54 -03004125bool tracing_is_disabled(void)
4126{
4127 return (tracing_disabled) ? true: false;
4128}
4129
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004130/*
4131 * Open and update trace_array ref count.
4132 * Must have the current trace_array passed to it.
4133 */
Steven Rostedt (Red Hat)dcc30222013-07-02 20:30:52 -04004134static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004135{
4136 struct trace_array *tr = inode->i_private;
4137
4138 if (tracing_disabled)
4139 return -ENODEV;
4140
4141 if (trace_array_get(tr) < 0)
4142 return -ENODEV;
4143
4144 filp->private_data = inode->i_private;
4145
4146 return 0;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004147}
4148
Hannes Eder4fd27352009-02-10 19:44:12 +01004149static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004150{
Oleg Nesterov6484c712013-07-23 17:26:10 +02004151 struct trace_array *tr = inode->i_private;
matt mooney907f2782010-09-27 19:04:53 -07004152 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004153 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004154 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004155
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004156 if (!(file->f_mode & FMODE_READ)) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02004157 trace_array_put(tr);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004158 return 0;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004159 }
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004160
Oleg Nesterov6484c712013-07-23 17:26:10 +02004161 /* Writes do not use seq_file */
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004162 iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004163 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05004164
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004165 for_each_tracing_cpu(cpu) {
4166 if (iter->buffer_iter[cpu])
4167 ring_buffer_read_finish(iter->buffer_iter[cpu]);
4168 }
4169
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004170 if (iter->trace && iter->trace->close)
4171 iter->trace->close(iter);
4172
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004173 if (!iter->snapshot)
4174 /* reenable tracing if it was previously enabled */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004175 tracing_start_tr(tr);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004176
4177 __trace_array_put(tr);
4178
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004179 mutex_unlock(&trace_types_lock);
4180
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004181 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02004182 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004183 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04004184 kfree(iter->buffer_iter);
Jiri Olsa50e18b92012-04-25 10:23:39 +02004185 seq_release_private(inode, file);
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004186
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004187 return 0;
4188}
4189
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004190static int tracing_release_generic_tr(struct inode *inode, struct file *file)
4191{
4192 struct trace_array *tr = inode->i_private;
4193
4194 trace_array_put(tr);
4195 return 0;
4196}
4197
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004198static int tracing_single_release_tr(struct inode *inode, struct file *file)
4199{
4200 struct trace_array *tr = inode->i_private;
4201
4202 trace_array_put(tr);
4203
4204 return single_release(inode, file);
4205}
4206
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004207static int tracing_open(struct inode *inode, struct file *file)
4208{
Oleg Nesterov6484c712013-07-23 17:26:10 +02004209 struct trace_array *tr = inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004210 struct trace_iterator *iter;
4211 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004212
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004213 if (trace_array_get(tr) < 0)
4214 return -ENODEV;
4215
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004216 /* If this file was open for write, then erase contents */
Oleg Nesterov6484c712013-07-23 17:26:10 +02004217 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
4218 int cpu = tracing_get_cpu(inode);
Bo Yan8dd33bc2017-09-18 10:03:35 -07004219 struct trace_buffer *trace_buf = &tr->trace_buffer;
4220
4221#ifdef CONFIG_TRACER_MAX_TRACE
4222 if (tr->current_trace->print_max)
4223 trace_buf = &tr->max_buffer;
4224#endif
Oleg Nesterov6484c712013-07-23 17:26:10 +02004225
4226 if (cpu == RING_BUFFER_ALL_CPUS)
Bo Yan8dd33bc2017-09-18 10:03:35 -07004227 tracing_reset_online_cpus(trace_buf);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004228 else
Bo Yan8dd33bc2017-09-18 10:03:35 -07004229 tracing_reset(trace_buf, cpu);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004230 }
4231
4232 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02004233 iter = __tracing_open(inode, file, false);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004234 if (IS_ERR(iter))
4235 ret = PTR_ERR(iter);
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004236 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004237 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4238 }
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004239
4240 if (ret < 0)
4241 trace_array_put(tr);
4242
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004243 return ret;
4244}
4245
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004246/*
4247 * Some tracers are not suitable for instance buffers.
4248 * A tracer is always available for the global array (toplevel)
4249 * or if it explicitly states that it is.
4250 */
4251static bool
4252trace_ok_for_array(struct tracer *t, struct trace_array *tr)
4253{
4254 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
4255}
4256
4257/* Find the next tracer that this trace array may use */
4258static struct tracer *
4259get_tracer_for_array(struct trace_array *tr, struct tracer *t)
4260{
4261 while (t && !trace_ok_for_array(t, tr))
4262 t = t->next;
4263
4264 return t;
4265}
4266
Ingo Molnare309b412008-05-12 21:20:51 +02004267static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004268t_next(struct seq_file *m, void *v, loff_t *pos)
4269{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004270 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08004271 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004272
4273 (*pos)++;
4274
4275 if (t)
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004276 t = get_tracer_for_array(tr, t->next);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004277
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004278 return t;
4279}
4280
4281static void *t_start(struct seq_file *m, loff_t *pos)
4282{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004283 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08004284 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004285 loff_t l = 0;
4286
4287 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004288
4289 t = get_tracer_for_array(tr, trace_types);
4290 for (; t && l < *pos; t = t_next(m, t, &l))
4291 ;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004292
4293 return t;
4294}
4295
4296static void t_stop(struct seq_file *m, void *p)
4297{
4298 mutex_unlock(&trace_types_lock);
4299}
4300
4301static int t_show(struct seq_file *m, void *v)
4302{
4303 struct tracer *t = v;
4304
4305 if (!t)
4306 return 0;
4307
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004308 seq_puts(m, t->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004309 if (t->next)
4310 seq_putc(m, ' ');
4311 else
4312 seq_putc(m, '\n');
4313
4314 return 0;
4315}
4316
James Morris88e9d342009-09-22 16:43:43 -07004317static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004318 .start = t_start,
4319 .next = t_next,
4320 .stop = t_stop,
4321 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004322};
4323
4324static int show_traces_open(struct inode *inode, struct file *file)
4325{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004326 struct trace_array *tr = inode->i_private;
4327 struct seq_file *m;
4328 int ret;
4329
Steven Rostedt60a11772008-05-12 21:20:44 +02004330 if (tracing_disabled)
4331 return -ENODEV;
4332
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004333 ret = seq_open(file, &show_traces_seq_ops);
4334 if (ret)
4335 return ret;
4336
4337 m = file->private_data;
4338 m->private = tr;
4339
4340 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004341}
4342
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004343static ssize_t
4344tracing_write_stub(struct file *filp, const char __user *ubuf,
4345 size_t count, loff_t *ppos)
4346{
4347 return count;
4348}
4349
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004350loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
Slava Pestov364829b2010-11-24 15:13:16 -08004351{
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004352 int ret;
4353
Slava Pestov364829b2010-11-24 15:13:16 -08004354 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004355 ret = seq_lseek(file, offset, whence);
Slava Pestov364829b2010-11-24 15:13:16 -08004356 else
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004357 file->f_pos = ret = 0;
4358
4359 return ret;
Slava Pestov364829b2010-11-24 15:13:16 -08004360}
4361
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004362static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004363 .open = tracing_open,
4364 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004365 .write = tracing_write_stub,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004366 .llseek = tracing_lseek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004367 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004368};
4369
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004370static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02004371 .open = show_traces_open,
4372 .read = seq_read,
4373 .release = seq_release,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004374 .llseek = seq_lseek,
Ingo Molnarc7078de2008-05-12 21:20:52 +02004375};
4376
4377static ssize_t
4378tracing_cpumask_read(struct file *filp, char __user *ubuf,
4379 size_t count, loff_t *ppos)
4380{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004381 struct trace_array *tr = file_inode(filp)->i_private;
Changbin Du90e406f2017-11-30 11:39:43 +08004382 char *mask_str;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004383 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02004384
Changbin Du90e406f2017-11-30 11:39:43 +08004385 len = snprintf(NULL, 0, "%*pb\n",
4386 cpumask_pr_args(tr->tracing_cpumask)) + 1;
4387 mask_str = kmalloc(len, GFP_KERNEL);
4388 if (!mask_str)
4389 return -ENOMEM;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004390
Changbin Du90e406f2017-11-30 11:39:43 +08004391 len = snprintf(mask_str, len, "%*pb\n",
Tejun Heo1a402432015-02-13 14:37:39 -08004392 cpumask_pr_args(tr->tracing_cpumask));
4393 if (len >= count) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02004394 count = -EINVAL;
4395 goto out_err;
4396 }
Changbin Du90e406f2017-11-30 11:39:43 +08004397 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004398
4399out_err:
Changbin Du90e406f2017-11-30 11:39:43 +08004400 kfree(mask_str);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004401
4402 return count;
4403}
4404
4405static ssize_t
4406tracing_cpumask_write(struct file *filp, const char __user *ubuf,
4407 size_t count, loff_t *ppos)
4408{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004409 struct trace_array *tr = file_inode(filp)->i_private;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304410 cpumask_var_t tracing_cpumask_new;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004411 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304412
4413 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
4414 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02004415
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304416 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004417 if (err)
4418 goto err_unlock;
4419
Steven Rostedta5e25882008-12-02 15:34:05 -05004420 local_irq_disable();
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05004421 arch_spin_lock(&tr->max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02004422 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02004423 /*
4424 * Increase/decrease the disabled counter if we are
4425 * about to flip a bit in the cpumask:
4426 */
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004427 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304428 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004429 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4430 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004431 }
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004432 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304433 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004434 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4435 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004436 }
4437 }
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05004438 arch_spin_unlock(&tr->max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05004439 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02004440
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004441 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304442 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004443
Ingo Molnarc7078de2008-05-12 21:20:52 +02004444 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004445
4446err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08004447 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004448
4449 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02004450}
4451
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004452static const struct file_operations tracing_cpumask_fops = {
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004453 .open = tracing_open_generic_tr,
Ingo Molnarc7078de2008-05-12 21:20:52 +02004454 .read = tracing_cpumask_read,
4455 .write = tracing_cpumask_write,
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004456 .release = tracing_release_generic_tr,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004457 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004458};
4459
Li Zefanfdb372e2009-12-08 11:15:59 +08004460static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004461{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004462 struct tracer_opt *trace_opts;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004463 struct trace_array *tr = m->private;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004464 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004465 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004466
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004467 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004468 tracer_flags = tr->current_trace->flags->val;
4469 trace_opts = tr->current_trace->flags->opts;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004470
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004471 for (i = 0; trace_options[i]; i++) {
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004472 if (tr->trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08004473 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004474 else
Li Zefanfdb372e2009-12-08 11:15:59 +08004475 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004476 }
4477
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004478 for (i = 0; trace_opts[i].name; i++) {
4479 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08004480 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004481 else
Li Zefanfdb372e2009-12-08 11:15:59 +08004482 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004483 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004484 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004485
Li Zefanfdb372e2009-12-08 11:15:59 +08004486 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004487}
4488
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004489static int __set_tracer_option(struct trace_array *tr,
Li Zefan8d18eaa2009-12-08 11:17:06 +08004490 struct tracer_flags *tracer_flags,
4491 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004492{
Chunyu Hud39cdd22016-03-08 21:37:01 +08004493 struct tracer *trace = tracer_flags->trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004494 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004495
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004496 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004497 if (ret)
4498 return ret;
4499
4500 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08004501 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004502 else
Zhaolei77708412009-08-07 18:53:21 +08004503 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004504 return 0;
4505}
4506
Li Zefan8d18eaa2009-12-08 11:17:06 +08004507/* Try to assign a tracer specific option */
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004508static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
Li Zefan8d18eaa2009-12-08 11:17:06 +08004509{
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004510 struct tracer *trace = tr->current_trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004511 struct tracer_flags *tracer_flags = trace->flags;
4512 struct tracer_opt *opts = NULL;
4513 int i;
4514
4515 for (i = 0; tracer_flags->opts[i].name; i++) {
4516 opts = &tracer_flags->opts[i];
4517
4518 if (strcmp(cmp, opts->name) == 0)
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004519 return __set_tracer_option(tr, trace->flags, opts, neg);
Li Zefan8d18eaa2009-12-08 11:17:06 +08004520 }
4521
4522 return -EINVAL;
4523}
4524
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004525/* Some tracers require overwrite to stay enabled */
4526int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
4527{
4528 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
4529 return -1;
4530
4531 return 0;
4532}
4533
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004534int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004535{
4536 /* do nothing if flag is already set */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004537 if (!!(tr->trace_flags & mask) == !!enabled)
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004538 return 0;
4539
4540 /* Give the tracer a chance to approve the change */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004541 if (tr->current_trace->flag_changed)
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -05004542 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004543 return -EINVAL;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004544
4545 if (enabled)
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004546 tr->trace_flags |= mask;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004547 else
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004548 tr->trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08004549
4550 if (mask == TRACE_ITER_RECORD_CMD)
4551 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08004552
Joel Fernandesd914ba32017-06-26 19:01:55 -07004553 if (mask == TRACE_ITER_RECORD_TGID) {
4554 if (!tgid_map)
Kees Cook6396bb22018-06-12 14:03:40 -07004555 tgid_map = kcalloc(PID_MAX_DEFAULT + 1,
4556 sizeof(*tgid_map),
Joel Fernandesd914ba32017-06-26 19:01:55 -07004557 GFP_KERNEL);
4558 if (!tgid_map) {
4559 tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
4560 return -ENOMEM;
4561 }
4562
4563 trace_event_enable_tgid_record(enabled);
4564 }
4565
Steven Rostedtc37775d2016-04-13 16:59:18 -04004566 if (mask == TRACE_ITER_EVENT_FORK)
4567 trace_event_follow_fork(tr, enabled);
4568
Namhyung Kim1e104862017-04-17 11:44:28 +09004569 if (mask == TRACE_ITER_FUNC_FORK)
4570 ftrace_pid_follow_fork(tr, enabled);
4571
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004572 if (mask == TRACE_ITER_OVERWRITE) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004573 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004574#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004575 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004576#endif
4577 }
Steven Rostedt81698832012-10-11 10:15:05 -04004578
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04004579 if (mask == TRACE_ITER_PRINTK) {
Steven Rostedt81698832012-10-11 10:15:05 -04004580 trace_printk_start_stop_comm(enabled);
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04004581 trace_printk_control(enabled);
4582 }
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004583
4584 return 0;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004585}
4586
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004587static int trace_set_options(struct trace_array *tr, char *option)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004588{
Li Zefan8d18eaa2009-12-08 11:17:06 +08004589 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004590 int neg = 0;
Yisheng Xie591a0332018-05-17 16:36:03 +08004591 int ret;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004592 size_t orig_len = strlen(option);
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -05004593 int len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004594
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004595 cmp = strstrip(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004596
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -05004597 len = str_has_prefix(cmp, "no");
4598 if (len)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004599 neg = 1;
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -05004600
4601 cmp += len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004602
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04004603 mutex_lock(&trace_types_lock);
4604
Yisheng Xie591a0332018-05-17 16:36:03 +08004605 ret = match_string(trace_options, -1, cmp);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004606 /* If no option could be set, test the specific tracer options */
Yisheng Xie591a0332018-05-17 16:36:03 +08004607 if (ret < 0)
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004608 ret = set_tracer_option(tr, cmp, neg);
Yisheng Xie591a0332018-05-17 16:36:03 +08004609 else
4610 ret = set_tracer_flag(tr, 1 << ret, !neg);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04004611
4612 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004613
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004614 /*
4615 * If the first trailing whitespace is replaced with '\0' by strstrip,
4616 * turn it back into a space.
4617 */
4618 if (orig_len > strlen(option))
4619 option[strlen(option)] = ' ';
4620
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004621 return ret;
4622}
4623
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004624static void __init apply_trace_boot_options(void)
4625{
4626 char *buf = trace_boot_options_buf;
4627 char *option;
4628
4629 while (true) {
4630 option = strsep(&buf, ",");
4631
4632 if (!option)
4633 break;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004634
Steven Rostedt (Red Hat)43ed3842015-11-03 22:15:14 -05004635 if (*option)
4636 trace_set_options(&global_trace, option);
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004637
4638 /* Put back the comma to allow this to be called again */
4639 if (buf)
4640 *(buf - 1) = ',';
4641 }
4642}
4643
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004644static ssize_t
4645tracing_trace_options_write(struct file *filp, const char __user *ubuf,
4646 size_t cnt, loff_t *ppos)
4647{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004648 struct seq_file *m = filp->private_data;
4649 struct trace_array *tr = m->private;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004650 char buf[64];
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004651 int ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004652
4653 if (cnt >= sizeof(buf))
4654 return -EINVAL;
4655
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08004656 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004657 return -EFAULT;
4658
Steven Rostedta8dd2172013-01-09 20:54:17 -05004659 buf[cnt] = 0;
4660
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004661 ret = trace_set_options(tr, buf);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004662 if (ret < 0)
4663 return ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004664
Jiri Olsacf8517c2009-10-23 19:36:16 -04004665 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004666
4667 return cnt;
4668}
4669
Li Zefanfdb372e2009-12-08 11:15:59 +08004670static int tracing_trace_options_open(struct inode *inode, struct file *file)
4671{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004672 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004673 int ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004674
Li Zefanfdb372e2009-12-08 11:15:59 +08004675 if (tracing_disabled)
4676 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004677
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004678 if (trace_array_get(tr) < 0)
4679 return -ENODEV;
4680
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004681 ret = single_open(file, tracing_trace_options_show, inode->i_private);
4682 if (ret < 0)
4683 trace_array_put(tr);
4684
4685 return ret;
Li Zefanfdb372e2009-12-08 11:15:59 +08004686}
4687
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004688static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08004689 .open = tracing_trace_options_open,
4690 .read = seq_read,
4691 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004692 .release = tracing_single_release_tr,
Steven Rostedtee6bce52008-11-12 17:52:37 -05004693 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004694};
4695
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004696static const char readme_msg[] =
4697 "tracing mini-HOWTO:\n\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004698 "# echo 0 > tracing_on : quick way to disable tracing\n"
4699 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
4700 " Important files:\n"
4701 " trace\t\t\t- The static contents of the buffer\n"
4702 "\t\t\t To clear the buffer write into this file: echo > trace\n"
4703 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
4704 " current_tracer\t- function and latency tracers\n"
4705 " available_tracers\t- list of configured tracers for current_tracer\n"
4706 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
4707 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
4708 " trace_clock\t\t-change the clock used to order events\n"
4709 " local: Per cpu clock but may not be synced across CPUs\n"
4710 " global: Synced across CPUs but slows tracing down.\n"
4711 " counter: Not a clock, but just an increment\n"
4712 " uptime: Jiffy counter from time of boot\n"
4713 " perf: Same clock that perf events use\n"
4714#ifdef CONFIG_X86_64
4715 " x86-tsc: TSC cycle counter\n"
4716#endif
Tom Zanussi2c1ea602018-01-15 20:51:41 -06004717 "\n timestamp_mode\t-view the mode used to timestamp events\n"
4718 " delta: Delta difference against a buffer-wide timestamp\n"
4719 " absolute: Absolute (standalone) timestamp\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004720 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
Steven Rostedtfa32e852016-07-06 15:25:08 -04004721 "\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 -04004722 " tracing_cpumask\t- Limit which CPUs to trace\n"
4723 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
4724 "\t\t\t Remove sub-buffer with rmdir\n"
4725 " trace_options\t\t- Set format or modify how tracing happens\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004726 "\t\t\t Disable an option by adding a suffix 'no' to the\n"
4727 "\t\t\t option name\n"
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004728 " 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 -04004729#ifdef CONFIG_DYNAMIC_FTRACE
4730 "\n available_filter_functions - list of functions that can be filtered on\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004731 " set_ftrace_filter\t- echo function name in here to only trace these\n"
4732 "\t\t\t functions\n"
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09004733 "\t accepts: func_full_name or glob-matching-pattern\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004734 "\t modules: Can select a group via module\n"
4735 "\t Format: :mod:<module-name>\n"
4736 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
4737 "\t triggers: a command to perform when function is hit\n"
4738 "\t Format: <function>:<trigger>[:count]\n"
4739 "\t trigger: traceon, traceoff\n"
4740 "\t\t enable_event:<system>:<event>\n"
4741 "\t\t disable_event:<system>:<event>\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004742#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004743 "\t\t stacktrace\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004744#endif
4745#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004746 "\t\t snapshot\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004747#endif
Steven Rostedt (Red Hat)17a280e2014-04-10 22:43:37 -04004748 "\t\t dump\n"
4749 "\t\t cpudump\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004750 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
4751 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
4752 "\t The first one will disable tracing every time do_fault is hit\n"
4753 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
4754 "\t The first time do trap is hit and it disables tracing, the\n"
4755 "\t counter will decrement to 2. If tracing is already disabled,\n"
4756 "\t the counter will not decrement. It only decrements when the\n"
4757 "\t trigger did work\n"
4758 "\t To remove trigger without count:\n"
4759 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
4760 "\t To remove trigger with a count:\n"
4761 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004762 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004763 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
4764 "\t modules: Can select a group via module command :mod:\n"
4765 "\t Does not accept triggers\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004766#endif /* CONFIG_DYNAMIC_FTRACE */
4767#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004768 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
4769 "\t\t (function)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004770#endif
4771#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4772 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
Namhyung Kimd048a8c72014-06-13 01:23:53 +09004773 " 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 -04004774 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
4775#endif
4776#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004777 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
4778 "\t\t\t snapshot buffer. Read the contents for more\n"
4779 "\t\t\t information\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004780#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004781#ifdef CONFIG_STACK_TRACER
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004782 " stack_trace\t\t- Shows the max stack trace when active\n"
4783 " stack_max_size\t- Shows current max stack size that was traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004784 "\t\t\t Write into this file to reset the max size (trigger a\n"
4785 "\t\t\t new trace)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004786#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004787 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
4788 "\t\t\t traces\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004789#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004790#endif /* CONFIG_STACK_TRACER */
Masami Hiramatsu5448d442018-11-05 18:02:08 +09004791#ifdef CONFIG_DYNAMIC_EVENTS
4792 " dynamic_events\t\t- Add/remove/show the generic dynamic events\n"
4793 "\t\t\t Write into this file to define/undefine new trace events.\n"
4794#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004795#ifdef CONFIG_KPROBE_EVENTS
Masami Hiramatsu86425622016-08-18 17:58:15 +09004796 " kprobe_events\t\t- Add/remove/show the kernel dynamic events\n"
4797 "\t\t\t Write into this file to define/undefine new trace events.\n"
4798#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004799#ifdef CONFIG_UPROBE_EVENTS
Masami Hiramatsu86425622016-08-18 17:58:15 +09004800 " uprobe_events\t\t- Add/remove/show the userspace dynamic events\n"
4801 "\t\t\t Write into this file to define/undefine new trace events.\n"
4802#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004803#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
Masami Hiramatsu86425622016-08-18 17:58:15 +09004804 "\t accepts: event-definitions (one definition per line)\n"
Masami Hiramatsuc3ca46e2017-05-23 15:05:50 +09004805 "\t Format: p[:[<group>/]<event>] <place> [<args>]\n"
4806 "\t r[maxactive][:[<group>/]<event>] <place> [<args>]\n"
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09004807#ifdef CONFIG_HIST_TRIGGERS
4808 "\t s:[synthetic/]<event> <field> [<field>]\n"
4809#endif
Masami Hiramatsu86425622016-08-18 17:58:15 +09004810 "\t -:[<group>/]<event>\n"
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004811#ifdef CONFIG_KPROBE_EVENTS
Masami Hiramatsu86425622016-08-18 17:58:15 +09004812 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
Naveen N. Rao35b6f552017-02-22 19:23:39 +05304813 "place (kretprobe): [<module>:]<symbol>[+<offset>]|<memaddr>\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004814#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004815#ifdef CONFIG_UPROBE_EVENTS
Ravi Bangoria1cc33162018-08-20 10:12:47 +05304816 " place (uprobe): <path>:<offset>[(ref_ctr_offset)]\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004817#endif
4818 "\t args: <name>=fetcharg[:type]\n"
4819 "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
Masami Hiramatsua1303af2018-04-25 21:21:26 +09004820#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
4821 "\t $stack<index>, $stack, $retval, $comm, $arg<N>\n"
4822#else
Masami Hiramatsu86425622016-08-18 17:58:15 +09004823 "\t $stack<index>, $stack, $retval, $comm\n"
Masami Hiramatsua1303af2018-04-25 21:21:26 +09004824#endif
Masami Hiramatsu60c2e0c2018-04-25 21:20:28 +09004825 "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
Masami Hiramatsu40b53b72018-04-25 21:21:55 +09004826 "\t b<bit-width>@<bit-offset>/<container-size>,\n"
4827 "\t <type>\\[<array-size>\\]\n"
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09004828#ifdef CONFIG_HIST_TRIGGERS
4829 "\t field: <stype> <name>;\n"
4830 "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
4831 "\t [unsigned] char/int/long\n"
4832#endif
Masami Hiramatsu86425622016-08-18 17:58:15 +09004833#endif
Tom Zanussi26f25562014-01-17 15:11:44 -06004834 " events/\t\t- Directory containing all trace event subsystems:\n"
4835 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
4836 " events/<system>/\t- Directory containing all trace events for <system>:\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004837 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
4838 "\t\t\t events\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004839 " filter\t\t- If set, only events passing filter are traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004840 " events/<system>/<event>/\t- Directory containing control files for\n"
4841 "\t\t\t <event>:\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004842 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
4843 " filter\t\t- If set, only events passing filter are traced\n"
4844 " trigger\t\t- If set, a command to perform when event is hit\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004845 "\t Format: <trigger>[:count][if <filter>]\n"
4846 "\t trigger: traceon, traceoff\n"
4847 "\t enable_event:<system>:<event>\n"
4848 "\t disable_event:<system>:<event>\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06004849#ifdef CONFIG_HIST_TRIGGERS
4850 "\t enable_hist:<system>:<event>\n"
4851 "\t disable_hist:<system>:<event>\n"
4852#endif
Tom Zanussi26f25562014-01-17 15:11:44 -06004853#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004854 "\t\t stacktrace\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004855#endif
4856#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004857 "\t\t snapshot\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004858#endif
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004859#ifdef CONFIG_HIST_TRIGGERS
4860 "\t\t hist (see below)\n"
4861#endif
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004862 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
4863 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
4864 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
4865 "\t events/block/block_unplug/trigger\n"
4866 "\t The first disables tracing every time block_unplug is hit.\n"
4867 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
4868 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
4869 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
4870 "\t Like function triggers, the counter is only decremented if it\n"
4871 "\t enabled or disabled tracing.\n"
4872 "\t To remove a trigger without a count:\n"
4873 "\t echo '!<trigger> > <system>/<event>/trigger\n"
4874 "\t To remove a trigger with a count:\n"
4875 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
4876 "\t Filters can be ignored when removing a trigger.\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004877#ifdef CONFIG_HIST_TRIGGERS
4878 " hist trigger\t- If set, event hits are aggregated into a hash table\n"
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004879 "\t Format: hist:keys=<field1[,field2,...]>\n"
Tom Zanussif2606832016-03-03 12:54:43 -06004880 "\t [:values=<field1[,field2,...]>]\n"
Tom Zanussie62347d2016-03-03 12:54:45 -06004881 "\t [:sort=<field1[,field2,...]>]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004882 "\t [:size=#entries]\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06004883 "\t [:pause][:continue][:clear]\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004884 "\t [:name=histname1]\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06004885 "\t [:<handler>.<action>]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004886 "\t [if <filter>]\n\n"
4887 "\t When a matching event is hit, an entry is added to a hash\n"
Tom Zanussif2606832016-03-03 12:54:43 -06004888 "\t table using the key(s) and value(s) named, and the value of a\n"
4889 "\t sum called 'hitcount' is incremented. Keys and values\n"
4890 "\t correspond to fields in the event's format description. Keys\n"
Tom Zanussi69a02002016-03-03 12:54:52 -06004891 "\t can be any field, or the special string 'stacktrace'.\n"
4892 "\t Compound keys consisting of up to two fields can be specified\n"
4893 "\t by the 'keys' keyword. Values must correspond to numeric\n"
4894 "\t fields. Sort keys consisting of up to two fields can be\n"
4895 "\t specified using the 'sort' keyword. The sort direction can\n"
4896 "\t be modified by appending '.descending' or '.ascending' to a\n"
4897 "\t sort field. The 'size' parameter can be used to specify more\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004898 "\t or fewer than the default 2048 entries for the hashtable size.\n"
4899 "\t If a hist trigger is given a name using the 'name' parameter,\n"
4900 "\t its histogram data will be shared with other triggers of the\n"
4901 "\t same name, and trigger hits will update this common data.\n\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004902 "\t Reading the 'hist' file for the event will dump the hash\n"
Tom Zanussi52a7f162016-03-03 12:54:57 -06004903 "\t table in its entirety to stdout. If there are multiple hist\n"
4904 "\t triggers attached to an event, there will be a table for each\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004905 "\t trigger in the output. The table displayed for a named\n"
4906 "\t trigger will be the same as any other instance having the\n"
4907 "\t same name. The default format used to display a given field\n"
4908 "\t can be modified by appending any of the following modifiers\n"
4909 "\t to the field name, as applicable:\n\n"
Tom Zanussic6afad42016-03-03 12:54:49 -06004910 "\t .hex display a number as a hex value\n"
4911 "\t .sym display an address as a symbol\n"
Tom Zanussi6b4827a2016-03-03 12:54:50 -06004912 "\t .sym-offset display an address as a symbol and offset\n"
Tom Zanussi31696192016-03-03 12:54:51 -06004913 "\t .execname display a common_pid as a program name\n"
Tom Zanussi860f9f62018-01-15 20:51:48 -06004914 "\t .syscall display a syscall id as a syscall name\n"
4915 "\t .log2 display log2 value rather than raw number\n"
4916 "\t .usecs display a common_timestamp in microseconds\n\n"
Tom Zanussi83e99912016-03-03 12:54:46 -06004917 "\t The 'pause' parameter can be used to pause an existing hist\n"
4918 "\t trigger or to start a hist trigger but not log any events\n"
4919 "\t until told to do so. 'continue' can be used to start or\n"
4920 "\t restart a paused hist trigger.\n\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06004921 "\t The 'clear' parameter will clear the contents of a running\n"
4922 "\t hist trigger and leave its current paused/active state\n"
4923 "\t unchanged.\n\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06004924 "\t The enable_hist and disable_hist triggers can be used to\n"
4925 "\t have one event conditionally start and stop another event's\n"
Colin Ian King9e5a36a2019-02-17 22:32:22 +00004926 "\t already-attached hist trigger. The syntax is analogous to\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06004927 "\t the enable_event and disable_event triggers.\n\n"
4928 "\t Hist trigger handlers and actions are executed whenever a\n"
4929 "\t a histogram entry is added or updated. They take the form:\n\n"
4930 "\t <handler>.<action>\n\n"
4931 "\t The available handlers are:\n\n"
4932 "\t onmatch(matching.event) - invoke on addition or update\n"
Tom Zanussidff81f52019-02-13 17:42:48 -06004933 "\t onmax(var) - invoke if var exceeds current max\n"
4934 "\t onchange(var) - invoke action if var changes\n\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06004935 "\t The available actions are:\n\n"
Tom Zanussie91eefd72019-02-13 17:42:50 -06004936 "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06004937 "\t save(field,...) - save current event fields\n"
Tom Zanussia3785b72019-02-13 17:42:46 -06004938#ifdef CONFIG_TRACER_SNAPSHOT
4939 "\t snapshot() - snapshot the trace buffer\n"
4940#endif
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004941#endif
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004942;
4943
4944static ssize_t
4945tracing_readme_read(struct file *filp, char __user *ubuf,
4946 size_t cnt, loff_t *ppos)
4947{
4948 return simple_read_from_buffer(ubuf, cnt, ppos,
4949 readme_msg, strlen(readme_msg));
4950}
4951
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004952static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02004953 .open = tracing_open_generic,
4954 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004955 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004956};
4957
Michael Sartain99c621d2017-07-05 22:07:15 -06004958static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos)
4959{
4960 int *ptr = v;
4961
4962 if (*pos || m->count)
4963 ptr++;
4964
4965 (*pos)++;
4966
4967 for (; ptr <= &tgid_map[PID_MAX_DEFAULT]; ptr++) {
4968 if (trace_find_tgid(*ptr))
4969 return ptr;
4970 }
4971
4972 return NULL;
4973}
4974
4975static void *saved_tgids_start(struct seq_file *m, loff_t *pos)
4976{
4977 void *v;
4978 loff_t l = 0;
4979
4980 if (!tgid_map)
4981 return NULL;
4982
4983 v = &tgid_map[0];
4984 while (l <= *pos) {
4985 v = saved_tgids_next(m, v, &l);
4986 if (!v)
4987 return NULL;
4988 }
4989
4990 return v;
4991}
4992
4993static void saved_tgids_stop(struct seq_file *m, void *v)
4994{
4995}
4996
4997static int saved_tgids_show(struct seq_file *m, void *v)
4998{
4999 int pid = (int *)v - tgid_map;
5000
5001 seq_printf(m, "%d %d\n", pid, trace_find_tgid(pid));
5002 return 0;
5003}
5004
5005static const struct seq_operations tracing_saved_tgids_seq_ops = {
5006 .start = saved_tgids_start,
5007 .stop = saved_tgids_stop,
5008 .next = saved_tgids_next,
5009 .show = saved_tgids_show,
5010};
5011
5012static int tracing_saved_tgids_open(struct inode *inode, struct file *filp)
5013{
5014 if (tracing_disabled)
5015 return -ENODEV;
5016
5017 return seq_open(filp, &tracing_saved_tgids_seq_ops);
5018}
5019
5020
5021static const struct file_operations tracing_saved_tgids_fops = {
5022 .open = tracing_saved_tgids_open,
5023 .read = seq_read,
5024 .llseek = seq_lseek,
5025 .release = seq_release,
5026};
5027
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005028static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
Avadh Patel69abe6a2009-04-10 16:04:48 -04005029{
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005030 unsigned int *ptr = v;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005031
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005032 if (*pos || m->count)
5033 ptr++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005034
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005035 (*pos)++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005036
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005037 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
5038 ptr++) {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005039 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
Avadh Patel69abe6a2009-04-10 16:04:48 -04005040 continue;
5041
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005042 return ptr;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005043 }
5044
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005045 return NULL;
5046}
Avadh Patel69abe6a2009-04-10 16:04:48 -04005047
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005048static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
5049{
5050 void *v;
5051 loff_t l = 0;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005052
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04005053 preempt_disable();
5054 arch_spin_lock(&trace_cmdline_lock);
5055
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005056 v = &savedcmd->map_cmdline_to_pid[0];
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005057 while (l <= *pos) {
5058 v = saved_cmdlines_next(m, v, &l);
5059 if (!v)
5060 return NULL;
5061 }
5062
5063 return v;
5064}
5065
5066static void saved_cmdlines_stop(struct seq_file *m, void *v)
5067{
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04005068 arch_spin_unlock(&trace_cmdline_lock);
5069 preempt_enable();
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005070}
5071
5072static int saved_cmdlines_show(struct seq_file *m, void *v)
5073{
5074 char buf[TASK_COMM_LEN];
5075 unsigned int *pid = v;
5076
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04005077 __trace_find_cmdline(*pid, buf);
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005078 seq_printf(m, "%d %s\n", *pid, buf);
5079 return 0;
5080}
5081
5082static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
5083 .start = saved_cmdlines_start,
5084 .next = saved_cmdlines_next,
5085 .stop = saved_cmdlines_stop,
5086 .show = saved_cmdlines_show,
5087};
5088
5089static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
5090{
5091 if (tracing_disabled)
5092 return -ENODEV;
5093
5094 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
Avadh Patel69abe6a2009-04-10 16:04:48 -04005095}
5096
5097static const struct file_operations tracing_saved_cmdlines_fops = {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005098 .open = tracing_saved_cmdlines_open,
5099 .read = seq_read,
5100 .llseek = seq_lseek,
5101 .release = seq_release,
Avadh Patel69abe6a2009-04-10 16:04:48 -04005102};
5103
5104static ssize_t
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005105tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
5106 size_t cnt, loff_t *ppos)
5107{
5108 char buf[64];
5109 int r;
5110
5111 arch_spin_lock(&trace_cmdline_lock);
Namhyung Kima6af8fb2014-06-10 16:11:35 +09005112 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005113 arch_spin_unlock(&trace_cmdline_lock);
5114
5115 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5116}
5117
5118static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
5119{
5120 kfree(s->saved_cmdlines);
5121 kfree(s->map_cmdline_to_pid);
5122 kfree(s);
5123}
5124
5125static int tracing_resize_saved_cmdlines(unsigned int val)
5126{
5127 struct saved_cmdlines_buffer *s, *savedcmd_temp;
5128
Namhyung Kima6af8fb2014-06-10 16:11:35 +09005129 s = kmalloc(sizeof(*s), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005130 if (!s)
5131 return -ENOMEM;
5132
5133 if (allocate_cmdlines_buffer(val, s) < 0) {
5134 kfree(s);
5135 return -ENOMEM;
5136 }
5137
5138 arch_spin_lock(&trace_cmdline_lock);
5139 savedcmd_temp = savedcmd;
5140 savedcmd = s;
5141 arch_spin_unlock(&trace_cmdline_lock);
5142 free_saved_cmdlines_buffer(savedcmd_temp);
5143
5144 return 0;
5145}
5146
5147static ssize_t
5148tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
5149 size_t cnt, loff_t *ppos)
5150{
5151 unsigned long val;
5152 int ret;
5153
5154 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5155 if (ret)
5156 return ret;
5157
5158 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
5159 if (!val || val > PID_MAX_DEFAULT)
5160 return -EINVAL;
5161
5162 ret = tracing_resize_saved_cmdlines((unsigned int)val);
5163 if (ret < 0)
5164 return ret;
5165
5166 *ppos += cnt;
5167
5168 return cnt;
5169}
5170
5171static const struct file_operations tracing_saved_cmdlines_size_fops = {
5172 .open = tracing_open_generic,
5173 .read = tracing_saved_cmdlines_size_read,
5174 .write = tracing_saved_cmdlines_size_write,
5175};
5176
Jeremy Linton681bec02017-05-31 16:56:53 -05005177#ifdef CONFIG_TRACE_EVAL_MAP_FILE
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005178static union trace_eval_map_item *
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005179update_eval_map(union trace_eval_map_item *ptr)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005180{
Jeremy Linton00f4b652017-05-31 16:56:43 -05005181 if (!ptr->map.eval_string) {
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005182 if (ptr->tail.next) {
5183 ptr = ptr->tail.next;
5184 /* Set ptr to the next real item (skip head) */
5185 ptr++;
5186 } else
5187 return NULL;
5188 }
5189 return ptr;
5190}
5191
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005192static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005193{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005194 union trace_eval_map_item *ptr = v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005195
5196 /*
5197 * Paranoid! If ptr points to end, we don't want to increment past it.
5198 * This really should never happen.
5199 */
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005200 ptr = update_eval_map(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005201 if (WARN_ON_ONCE(!ptr))
5202 return NULL;
5203
5204 ptr++;
5205
5206 (*pos)++;
5207
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005208 ptr = update_eval_map(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005209
5210 return ptr;
5211}
5212
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005213static void *eval_map_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005214{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005215 union trace_eval_map_item *v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005216 loff_t l = 0;
5217
Jeremy Linton1793ed92017-05-31 16:56:46 -05005218 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005219
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005220 v = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005221 if (v)
5222 v++;
5223
5224 while (v && l < *pos) {
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005225 v = eval_map_next(m, v, &l);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005226 }
5227
5228 return v;
5229}
5230
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005231static void eval_map_stop(struct seq_file *m, void *v)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005232{
Jeremy Linton1793ed92017-05-31 16:56:46 -05005233 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005234}
5235
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005236static int eval_map_show(struct seq_file *m, void *v)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005237{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005238 union trace_eval_map_item *ptr = v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005239
5240 seq_printf(m, "%s %ld (%s)\n",
Jeremy Linton00f4b652017-05-31 16:56:43 -05005241 ptr->map.eval_string, ptr->map.eval_value,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005242 ptr->map.system);
5243
5244 return 0;
5245}
5246
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005247static const struct seq_operations tracing_eval_map_seq_ops = {
5248 .start = eval_map_start,
5249 .next = eval_map_next,
5250 .stop = eval_map_stop,
5251 .show = eval_map_show,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005252};
5253
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005254static int tracing_eval_map_open(struct inode *inode, struct file *filp)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005255{
5256 if (tracing_disabled)
5257 return -ENODEV;
5258
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005259 return seq_open(filp, &tracing_eval_map_seq_ops);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005260}
5261
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005262static const struct file_operations tracing_eval_map_fops = {
5263 .open = tracing_eval_map_open,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005264 .read = seq_read,
5265 .llseek = seq_lseek,
5266 .release = seq_release,
5267};
5268
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005269static inline union trace_eval_map_item *
Jeremy Linton5f60b352017-05-31 16:56:47 -05005270trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005271{
5272 /* Return tail of array given the head */
5273 return ptr + ptr->head.length + 1;
5274}
5275
5276static void
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005277trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005278 int len)
5279{
Jeremy Linton00f4b652017-05-31 16:56:43 -05005280 struct trace_eval_map **stop;
5281 struct trace_eval_map **map;
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005282 union trace_eval_map_item *map_array;
5283 union trace_eval_map_item *ptr;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005284
5285 stop = start + len;
5286
5287 /*
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005288 * The trace_eval_maps contains the map plus a head and tail item,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005289 * where the head holds the module and length of array, and the
5290 * tail holds a pointer to the next list.
5291 */
Kees Cook6da2ec52018-06-12 13:55:00 -07005292 map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005293 if (!map_array) {
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005294 pr_warn("Unable to allocate trace eval mapping\n");
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005295 return;
5296 }
5297
Jeremy Linton1793ed92017-05-31 16:56:46 -05005298 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005299
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005300 if (!trace_eval_maps)
5301 trace_eval_maps = map_array;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005302 else {
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005303 ptr = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005304 for (;;) {
Jeremy Linton5f60b352017-05-31 16:56:47 -05005305 ptr = trace_eval_jmp_to_tail(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005306 if (!ptr->tail.next)
5307 break;
5308 ptr = ptr->tail.next;
5309
5310 }
5311 ptr->tail.next = map_array;
5312 }
5313 map_array->head.mod = mod;
5314 map_array->head.length = len;
5315 map_array++;
5316
5317 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
5318 map_array->map = **map;
5319 map_array++;
5320 }
5321 memset(map_array, 0, sizeof(*map_array));
5322
Jeremy Linton1793ed92017-05-31 16:56:46 -05005323 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005324}
5325
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005326static void trace_create_eval_file(struct dentry *d_tracer)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005327{
Jeremy Linton681bec02017-05-31 16:56:53 -05005328 trace_create_file("eval_map", 0444, d_tracer,
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005329 NULL, &tracing_eval_map_fops);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005330}
5331
Jeremy Linton681bec02017-05-31 16:56:53 -05005332#else /* CONFIG_TRACE_EVAL_MAP_FILE */
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005333static inline void trace_create_eval_file(struct dentry *d_tracer) { }
5334static inline void trace_insert_eval_map_file(struct module *mod,
Jeremy Linton00f4b652017-05-31 16:56:43 -05005335 struct trace_eval_map **start, int len) { }
Jeremy Linton681bec02017-05-31 16:56:53 -05005336#endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005337
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005338static void trace_insert_eval_map(struct module *mod,
Jeremy Linton00f4b652017-05-31 16:56:43 -05005339 struct trace_eval_map **start, int len)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005340{
Jeremy Linton00f4b652017-05-31 16:56:43 -05005341 struct trace_eval_map **map;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005342
5343 if (len <= 0)
5344 return;
5345
5346 map = start;
5347
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005348 trace_event_eval_update(map, len);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005349
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005350 trace_insert_eval_map_file(mod, start, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005351}
5352
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005353static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005354tracing_set_trace_read(struct file *filp, char __user *ubuf,
5355 size_t cnt, loff_t *ppos)
5356{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005357 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08005358 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005359 int r;
5360
5361 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005362 r = sprintf(buf, "%s\n", tr->current_trace->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005363 mutex_unlock(&trace_types_lock);
5364
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005365 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005366}
5367
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005368int tracer_init(struct tracer *t, struct trace_array *tr)
5369{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005370 tracing_reset_online_cpus(&tr->trace_buffer);
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005371 return t->init(tr);
5372}
5373
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005374static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005375{
5376 int cpu;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005377
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005378 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005379 per_cpu_ptr(buf->data, cpu)->entries = val;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005380}
5381
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005382#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookad60da502012-10-17 11:56:16 +09005383/* resize @tr's buffer to the size of @size_tr's entries */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005384static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
5385 struct trace_buffer *size_buf, int cpu_id)
Hiraku Toyookad60da502012-10-17 11:56:16 +09005386{
5387 int cpu, ret = 0;
5388
5389 if (cpu_id == RING_BUFFER_ALL_CPUS) {
5390 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005391 ret = ring_buffer_resize(trace_buf->buffer,
5392 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005393 if (ret < 0)
5394 break;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005395 per_cpu_ptr(trace_buf->data, cpu)->entries =
5396 per_cpu_ptr(size_buf->data, cpu)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09005397 }
5398 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005399 ret = ring_buffer_resize(trace_buf->buffer,
5400 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005401 if (ret == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005402 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
5403 per_cpu_ptr(size_buf->data, cpu_id)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09005404 }
5405
5406 return ret;
5407}
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005408#endif /* CONFIG_TRACER_MAX_TRACE */
Hiraku Toyookad60da502012-10-17 11:56:16 +09005409
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005410static int __tracing_resize_ring_buffer(struct trace_array *tr,
5411 unsigned long size, int cpu)
Steven Rostedt73c51622009-03-11 13:42:01 -04005412{
5413 int ret;
5414
5415 /*
5416 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04005417 * we use the size that was given, and we can forget about
5418 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04005419 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005420 ring_buffer_expanded = true;
Steven Rostedt73c51622009-03-11 13:42:01 -04005421
Steven Rostedtb382ede62012-10-10 21:44:34 -04005422 /* May be called before buffers are initialized */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005423 if (!tr->trace_buffer.buffer)
Steven Rostedtb382ede62012-10-10 21:44:34 -04005424 return 0;
5425
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005426 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005427 if (ret < 0)
5428 return ret;
5429
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005430#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005431 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
5432 !tr->current_trace->use_max_tr)
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005433 goto out;
5434
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005435 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005436 if (ret < 0) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005437 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
5438 &tr->trace_buffer, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005439 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04005440 /*
5441 * AARGH! We are left with different
5442 * size max buffer!!!!
5443 * The max buffer is our "snapshot" buffer.
5444 * When a tracer needs a snapshot (one of the
5445 * latency tracers), it swaps the max buffer
5446 * with the saved snap shot. We succeeded to
5447 * update the size of the main buffer, but failed to
5448 * update the size of the max buffer. But when we tried
5449 * to reset the main buffer to the original size, we
5450 * failed there too. This is very unlikely to
5451 * happen, but if it does, warn and kill all
5452 * tracing.
5453 */
Steven Rostedt73c51622009-03-11 13:42:01 -04005454 WARN_ON(1);
5455 tracing_disabled = 1;
5456 }
5457 return ret;
5458 }
5459
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005460 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005461 set_buffer_entries(&tr->max_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005462 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005463 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005464
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005465 out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005466#endif /* CONFIG_TRACER_MAX_TRACE */
5467
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005468 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005469 set_buffer_entries(&tr->trace_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005470 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005471 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
Steven Rostedt73c51622009-03-11 13:42:01 -04005472
5473 return ret;
5474}
5475
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005476static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
5477 unsigned long size, int cpu_id)
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005478{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07005479 int ret = size;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005480
5481 mutex_lock(&trace_types_lock);
5482
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005483 if (cpu_id != RING_BUFFER_ALL_CPUS) {
5484 /* make sure, this cpu is enabled in the mask */
5485 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
5486 ret = -EINVAL;
5487 goto out;
5488 }
5489 }
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005490
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005491 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005492 if (ret < 0)
5493 ret = -ENOMEM;
5494
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005495out:
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005496 mutex_unlock(&trace_types_lock);
5497
5498 return ret;
5499}
5500
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005501
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005502/**
5503 * tracing_update_buffers - used by tracing facility to expand ring buffers
5504 *
5505 * To save on memory when the tracing is never used on a system with it
5506 * configured in. The ring buffers are set to a minimum size. But once
5507 * a user starts to use the tracing facility, then they need to grow
5508 * to their default size.
5509 *
5510 * This function is to be called when a tracer is about to be used.
5511 */
5512int tracing_update_buffers(void)
5513{
5514 int ret = 0;
5515
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005516 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005517 if (!ring_buffer_expanded)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005518 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005519 RING_BUFFER_ALL_CPUS);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005520 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005521
5522 return ret;
5523}
5524
Steven Rostedt577b7852009-02-26 23:43:05 -05005525struct trace_option_dentry;
5526
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04005527static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005528create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
Steven Rostedt577b7852009-02-26 23:43:05 -05005529
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05005530/*
5531 * Used to clear out the tracer before deletion of an instance.
5532 * Must have trace_types_lock held.
5533 */
5534static void tracing_set_nop(struct trace_array *tr)
5535{
5536 if (tr->current_trace == &nop_trace)
5537 return;
5538
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005539 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05005540
5541 if (tr->current_trace->reset)
5542 tr->current_trace->reset(tr);
5543
5544 tr->current_trace = &nop_trace;
5545}
5546
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04005547static void add_tracer_options(struct trace_array *tr, struct tracer *t)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005548{
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05005549 /* Only enable if the directory has been created already. */
5550 if (!tr->dir)
5551 return;
5552
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04005553 create_trace_option_files(tr, t);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05005554}
5555
5556static int tracing_set_tracer(struct trace_array *tr, const char *buf)
5557{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005558 struct tracer *t;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005559#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05005560 bool had_max_tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005561#endif
Peter Zijlstrad9e54072008-11-01 19:57:37 +01005562 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005563
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005564 mutex_lock(&trace_types_lock);
5565
Steven Rostedt73c51622009-03-11 13:42:01 -04005566 if (!ring_buffer_expanded) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005567 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005568 RING_BUFFER_ALL_CPUS);
Steven Rostedt73c51622009-03-11 13:42:01 -04005569 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01005570 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04005571 ret = 0;
5572 }
5573
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005574 for (t = trace_types; t; t = t->next) {
5575 if (strcmp(t->name, buf) == 0)
5576 break;
5577 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02005578 if (!t) {
5579 ret = -EINVAL;
5580 goto out;
5581 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005582 if (t == tr->current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005583 goto out;
5584
Tom Zanussia35873a2019-02-13 17:42:45 -06005585#ifdef CONFIG_TRACER_SNAPSHOT
5586 if (t->use_max_tr) {
5587 arch_spin_lock(&tr->max_lock);
5588 if (tr->cond_snapshot)
5589 ret = -EBUSY;
5590 arch_spin_unlock(&tr->max_lock);
5591 if (ret)
5592 goto out;
5593 }
5594#endif
Ziqian SUN (Zamir)c7b3ae02017-09-11 14:26:35 +08005595 /* Some tracers won't work on kernel command line */
5596 if (system_state < SYSTEM_RUNNING && t->noboot) {
5597 pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
5598 t->name);
5599 goto out;
5600 }
5601
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005602 /* Some tracers are only allowed for the top level buffer */
5603 if (!trace_ok_for_array(t, tr)) {
5604 ret = -EINVAL;
5605 goto out;
5606 }
5607
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005608 /* If trace pipe files are being read, we can't change the tracer */
5609 if (tr->current_trace->ref) {
5610 ret = -EBUSY;
5611 goto out;
5612 }
5613
Steven Rostedt9f029e82008-11-12 15:24:24 -05005614 trace_branch_disable();
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005615
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005616 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005617
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005618 if (tr->current_trace->reset)
5619 tr->current_trace->reset(tr);
Steven Rostedt34600f02013-01-22 13:35:11 -05005620
Paul E. McKenney74401722018-11-06 18:44:52 -08005621 /* Current trace needs to be nop_trace before synchronize_rcu */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005622 tr->current_trace = &nop_trace;
Steven Rostedt34600f02013-01-22 13:35:11 -05005623
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05005624#ifdef CONFIG_TRACER_MAX_TRACE
5625 had_max_tr = tr->allocated_snapshot;
Steven Rostedt34600f02013-01-22 13:35:11 -05005626
5627 if (had_max_tr && !t->use_max_tr) {
5628 /*
5629 * We need to make sure that the update_max_tr sees that
5630 * current_trace changed to nop_trace to keep it from
5631 * swapping the buffers after we resize it.
5632 * The update_max_tr is called from interrupts disabled
5633 * so a synchronized_sched() is sufficient.
5634 */
Paul E. McKenney74401722018-11-06 18:44:52 -08005635 synchronize_rcu();
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04005636 free_snapshot(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005637 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005638#endif
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005639
5640#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05005641 if (t->use_max_tr && !had_max_tr) {
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04005642 ret = tracing_alloc_snapshot_instance(tr);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005643 if (ret < 0)
5644 goto out;
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005645 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005646#endif
Steven Rostedt577b7852009-02-26 23:43:05 -05005647
Frederic Weisbecker1c800252008-11-16 05:57:26 +01005648 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005649 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01005650 if (ret)
5651 goto out;
5652 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005653
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005654 tr->current_trace = t;
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005655 tr->current_trace->enabled++;
Steven Rostedt9f029e82008-11-12 15:24:24 -05005656 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005657 out:
5658 mutex_unlock(&trace_types_lock);
5659
Peter Zijlstrad9e54072008-11-01 19:57:37 +01005660 return ret;
5661}
5662
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005663static ssize_t
5664tracing_set_trace_write(struct file *filp, const char __user *ubuf,
5665 size_t cnt, loff_t *ppos)
5666{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005667 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08005668 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005669 int i;
5670 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01005671 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005672
Steven Rostedt60063a62008-10-28 10:44:24 -04005673 ret = cnt;
5674
Li Zefanee6c2c12009-09-18 14:06:47 +08005675 if (cnt > MAX_TRACER_SIZE)
5676 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005677
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08005678 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005679 return -EFAULT;
5680
5681 buf[cnt] = 0;
5682
5683 /* strip ending whitespace. */
5684 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
5685 buf[i] = 0;
5686
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005687 err = tracing_set_tracer(tr, buf);
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01005688 if (err)
5689 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005690
Jiri Olsacf8517c2009-10-23 19:36:16 -04005691 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005692
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02005693 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005694}
5695
5696static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005697tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
5698 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005699{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005700 char buf[64];
5701 int r;
5702
Steven Rostedtcffae432008-05-12 21:21:00 +02005703 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005704 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02005705 if (r > sizeof(buf))
5706 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005707 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005708}
5709
5710static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005711tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
5712 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005713{
Hannes Eder5e398412009-02-10 19:44:34 +01005714 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005715 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005716
Peter Huewe22fe9b52011-06-07 21:58:27 +02005717 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5718 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005719 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005720
5721 *ptr = val * 1000;
5722
5723 return cnt;
5724}
5725
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005726static ssize_t
5727tracing_thresh_read(struct file *filp, char __user *ubuf,
5728 size_t cnt, loff_t *ppos)
5729{
5730 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
5731}
5732
5733static ssize_t
5734tracing_thresh_write(struct file *filp, const char __user *ubuf,
5735 size_t cnt, loff_t *ppos)
5736{
5737 struct trace_array *tr = filp->private_data;
5738 int ret;
5739
5740 mutex_lock(&trace_types_lock);
5741 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
5742 if (ret < 0)
5743 goto out;
5744
5745 if (tr->current_trace->update_thresh) {
5746 ret = tr->current_trace->update_thresh(tr);
5747 if (ret < 0)
5748 goto out;
5749 }
5750
5751 ret = cnt;
5752out:
5753 mutex_unlock(&trace_types_lock);
5754
5755 return ret;
5756}
5757
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04005758#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Chen Gange428abb2015-11-10 05:15:15 +08005759
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005760static ssize_t
5761tracing_max_lat_read(struct file *filp, char __user *ubuf,
5762 size_t cnt, loff_t *ppos)
5763{
5764 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos);
5765}
5766
5767static ssize_t
5768tracing_max_lat_write(struct file *filp, const char __user *ubuf,
5769 size_t cnt, loff_t *ppos)
5770{
5771 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos);
5772}
5773
Chen Gange428abb2015-11-10 05:15:15 +08005774#endif
5775
Steven Rostedtb3806b42008-05-12 21:20:46 +02005776static int tracing_open_pipe(struct inode *inode, struct file *filp)
5777{
Oleg Nesterov15544202013-07-23 17:25:57 +02005778 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005779 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005780 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005781
5782 if (tracing_disabled)
5783 return -ENODEV;
5784
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005785 if (trace_array_get(tr) < 0)
5786 return -ENODEV;
5787
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005788 mutex_lock(&trace_types_lock);
5789
Steven Rostedtb3806b42008-05-12 21:20:46 +02005790 /* create a buffer to store the information to pass to userspace */
5791 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005792 if (!iter) {
5793 ret = -ENOMEM;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005794 __trace_array_put(tr);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005795 goto out;
5796 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02005797
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04005798 trace_seq_init(&iter->seq);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005799 iter->trace = tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005800
5801 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
5802 ret = -ENOMEM;
5803 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10305804 }
5805
Steven Rostedta3097202008-11-07 22:36:02 -05005806 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10305807 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05005808
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005809 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt112f38a72009-06-01 15:16:05 -04005810 iter->iter_flags |= TRACE_FILE_LAT_FMT;
5811
David Sharp8be07092012-11-13 12:18:22 -08005812 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09005813 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08005814 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
5815
Oleg Nesterov15544202013-07-23 17:25:57 +02005816 iter->tr = tr;
5817 iter->trace_buffer = &tr->trace_buffer;
5818 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005819 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005820 filp->private_data = iter;
5821
Steven Rostedt107bad82008-05-12 21:21:01 +02005822 if (iter->trace->pipe_open)
5823 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02005824
Arnd Bergmannb4447862010-07-07 23:40:11 +02005825 nonseekable_open(inode, filp);
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005826
5827 tr->current_trace->ref++;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005828out:
5829 mutex_unlock(&trace_types_lock);
5830 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005831
5832fail:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005833 kfree(iter);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005834 __trace_array_put(tr);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005835 mutex_unlock(&trace_types_lock);
5836 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005837}
5838
5839static int tracing_release_pipe(struct inode *inode, struct file *file)
5840{
5841 struct trace_iterator *iter = file->private_data;
Oleg Nesterov15544202013-07-23 17:25:57 +02005842 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005843
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005844 mutex_lock(&trace_types_lock);
5845
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005846 tr->current_trace->ref--;
5847
Steven Rostedt29bf4a52009-12-09 12:37:43 -05005848 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05005849 iter->trace->pipe_close(iter);
5850
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005851 mutex_unlock(&trace_types_lock);
5852
Rusty Russell44623442009-01-01 10:12:23 +10305853 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005854 mutex_destroy(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005855 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005856
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005857 trace_array_put(tr);
5858
Steven Rostedtb3806b42008-05-12 21:20:46 +02005859 return 0;
5860}
5861
Al Viro9dd95742017-07-03 00:42:43 -04005862static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005863trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005864{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005865 struct trace_array *tr = iter->tr;
5866
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005867 /* Iterators are static, they should be filled or empty */
5868 if (trace_buffer_iter(iter, iter->cpu_file))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08005869 return EPOLLIN | EPOLLRDNORM;
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005870
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005871 if (tr->trace_flags & TRACE_ITER_BLOCK)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005872 /*
5873 * Always select as readable when in blocking mode
5874 */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08005875 return EPOLLIN | EPOLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005876 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005877 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005878 filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005879}
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005880
Al Viro9dd95742017-07-03 00:42:43 -04005881static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005882tracing_poll_pipe(struct file *filp, poll_table *poll_table)
5883{
5884 struct trace_iterator *iter = filp->private_data;
5885
5886 return trace_poll(iter, filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005887}
5888
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005889/* Must be called with iter->mutex held. */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005890static int tracing_wait_pipe(struct file *filp)
5891{
5892 struct trace_iterator *iter = filp->private_data;
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04005893 int ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005894
5895 while (trace_empty(iter)) {
5896
5897 if ((filp->f_flags & O_NONBLOCK)) {
5898 return -EAGAIN;
5899 }
5900
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005901 /*
Liu Bo250bfd32013-01-14 10:54:11 +08005902 * We block until we read something and tracing is disabled.
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005903 * We still block if tracing is disabled, but we have never
5904 * read anything. This allows a user to cat this file, and
5905 * then enable tracing. But after we have read something,
5906 * we give an EOF when tracing is again disabled.
5907 *
5908 * iter->pos will be 0 if we haven't read anything.
5909 */
Tahsin Erdogan75df6e62017-09-17 03:23:48 -07005910 if (!tracer_tracing_is_on(iter->tr) && iter->pos)
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005911 break;
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04005912
5913 mutex_unlock(&iter->mutex);
5914
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05005915 ret = wait_on_pipe(iter, 0);
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04005916
5917 mutex_lock(&iter->mutex);
5918
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04005919 if (ret)
5920 return ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005921 }
5922
5923 return 1;
5924}
5925
Steven Rostedtb3806b42008-05-12 21:20:46 +02005926/*
5927 * Consumer reader.
5928 */
5929static ssize_t
5930tracing_read_pipe(struct file *filp, char __user *ubuf,
5931 size_t cnt, loff_t *ppos)
5932{
5933 struct trace_iterator *iter = filp->private_data;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005934 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005935
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005936 /*
5937 * Avoid more than one consumer on a single file descriptor
5938 * This is just a matter of traces coherency, the ring buffer itself
5939 * is protected.
5940 */
5941 mutex_lock(&iter->mutex);
Steven Rostedt (Red Hat)12458002016-09-23 22:57:13 -04005942
5943 /* return any leftover data */
5944 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
5945 if (sret != -EBUSY)
5946 goto out;
5947
5948 trace_seq_init(&iter->seq);
5949
Steven Rostedt107bad82008-05-12 21:21:01 +02005950 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005951 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
5952 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02005953 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02005954 }
5955
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005956waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005957 sret = tracing_wait_pipe(filp);
5958 if (sret <= 0)
5959 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005960
5961 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005962 if (trace_empty(iter)) {
5963 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02005964 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005965 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02005966
5967 if (cnt >= PAGE_SIZE)
5968 cnt = PAGE_SIZE - 1;
5969
Steven Rostedt53d0aa72008-05-12 21:21:01 +02005970 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02005971 memset(&iter->seq, 0,
5972 sizeof(struct trace_iterator) -
5973 offsetof(struct trace_iterator, seq));
Andrew Vagined5467d2013-08-02 21:16:43 +04005974 cpumask_clear(iter->started);
Steven Rostedt4823ed72008-05-12 21:21:01 +02005975 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005976
Lai Jiangshan4f535962009-05-18 19:35:34 +08005977 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005978 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05005979 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02005980 enum print_line_t ret;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005981 int save_len = iter->seq.seq.len;
Steven Rostedt088b1e422008-05-12 21:20:48 +02005982
Ingo Molnarf9896bf2008-05-12 21:20:47 +02005983 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02005984 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02005985 /* don't print partial lines */
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005986 iter->seq.seq.len = save_len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005987 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02005988 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01005989 if (ret != TRACE_TYPE_NO_CONSUME)
5990 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005991
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005992 if (trace_seq_used(&iter->seq) >= cnt)
Steven Rostedtb3806b42008-05-12 21:20:46 +02005993 break;
Jiri Olsaee5e51f2011-03-25 12:05:18 +01005994
5995 /*
5996 * Setting the full flag means we reached the trace_seq buffer
5997 * size and we should leave by partial output condition above.
5998 * One of the trace_seq_* functions is not used properly.
5999 */
6000 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
6001 iter->ent->type);
Steven Rostedtb3806b42008-05-12 21:20:46 +02006002 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006003 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08006004 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02006005
Steven Rostedtb3806b42008-05-12 21:20:46 +02006006 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006007 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006008 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
Steven Rostedtf9520752009-03-02 14:04:40 -05006009 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02006010
6011 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006012 * If there was nothing to send to user, in spite of consuming trace
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02006013 * entries, go back to wait for more entries.
6014 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006015 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02006016 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006017
Steven Rostedt107bad82008-05-12 21:21:01 +02006018out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006019 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02006020
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006021 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006022}
6023
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006024static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
6025 unsigned int idx)
6026{
6027 __free_page(spd->pages[idx]);
6028}
6029
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08006030static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05006031 .confirm = generic_pipe_buf_confirm,
Al Viro92fdd982014-01-17 07:53:39 -05006032 .release = generic_pipe_buf_release,
Steven Rostedt34cd4992009-02-09 12:06:29 -05006033 .steal = generic_pipe_buf_steal,
6034 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006035};
6036
Steven Rostedt34cd4992009-02-09 12:06:29 -05006037static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01006038tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05006039{
6040 size_t count;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006041 int save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006042 int ret;
6043
6044 /* Seq buffer is page-sized, exactly what we need. */
6045 for (;;) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006046 save_len = iter->seq.seq.len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006047 ret = print_trace_line(iter);
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006048
6049 if (trace_seq_has_overflowed(&iter->seq)) {
6050 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006051 break;
6052 }
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006053
6054 /*
6055 * This should not be hit, because it should only
6056 * be set if the iter->seq overflowed. But check it
6057 * anyway to be safe.
6058 */
Steven Rostedt34cd4992009-02-09 12:06:29 -05006059 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006060 iter->seq.seq.len = save_len;
6061 break;
6062 }
6063
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006064 count = trace_seq_used(&iter->seq) - save_len;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006065 if (rem < count) {
6066 rem = 0;
6067 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006068 break;
6069 }
6070
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08006071 if (ret != TRACE_TYPE_NO_CONSUME)
6072 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05006073 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05006074 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05006075 rem = 0;
6076 iter->ent = NULL;
6077 break;
6078 }
6079 }
6080
6081 return rem;
6082}
6083
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006084static ssize_t tracing_splice_read_pipe(struct file *filp,
6085 loff_t *ppos,
6086 struct pipe_inode_info *pipe,
6087 size_t len,
6088 unsigned int flags)
6089{
Jens Axboe35f3d142010-05-20 10:43:18 +02006090 struct page *pages_def[PIPE_DEF_BUFFERS];
6091 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006092 struct trace_iterator *iter = filp->private_data;
6093 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02006094 .pages = pages_def,
6095 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05006096 .nr_pages = 0, /* This gets updated below. */
Eric Dumazet047fe362012-06-12 15:24:40 +02006097 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt34cd4992009-02-09 12:06:29 -05006098 .ops = &tracing_pipe_buf_ops,
6099 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006100 };
6101 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006102 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006103 unsigned int i;
6104
Jens Axboe35f3d142010-05-20 10:43:18 +02006105 if (splice_grow_spd(pipe, &spd))
6106 return -ENOMEM;
6107
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006108 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006109
6110 if (iter->trace->splice_read) {
6111 ret = iter->trace->splice_read(iter, filp,
6112 ppos, pipe, len, flags);
6113 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05006114 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006115 }
6116
6117 ret = tracing_wait_pipe(filp);
6118 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05006119 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006120
Jason Wessel955b61e2010-08-05 09:22:23 -05006121 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006122 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006123 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006124 }
6125
Lai Jiangshan4f535962009-05-18 19:35:34 +08006126 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006127 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08006128
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006129 /* Fill as many pages as possible. */
Al Viroa786c062014-04-11 12:01:03 -04006130 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
Jens Axboe35f3d142010-05-20 10:43:18 +02006131 spd.pages[i] = alloc_page(GFP_KERNEL);
6132 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05006133 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006134
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01006135 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006136
6137 /* Copy the data into the page, so we can start over. */
6138 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02006139 page_address(spd.pages[i]),
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006140 trace_seq_used(&iter->seq));
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006141 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02006142 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006143 break;
6144 }
Jens Axboe35f3d142010-05-20 10:43:18 +02006145 spd.partial[i].offset = 0;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006146 spd.partial[i].len = trace_seq_used(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006147
Steven Rostedtf9520752009-03-02 14:04:40 -05006148 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006149 }
6150
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006151 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08006152 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006153 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006154
6155 spd.nr_pages = i;
6156
Steven Rostedt (Red Hat)a29054d92016-03-18 15:46:48 -04006157 if (i)
6158 ret = splice_to_pipe(pipe, &spd);
6159 else
6160 ret = 0;
Jens Axboe35f3d142010-05-20 10:43:18 +02006161out:
Eric Dumazet047fe362012-06-12 15:24:40 +02006162 splice_shrink_spd(&spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02006163 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006164
Steven Rostedt34cd4992009-02-09 12:06:29 -05006165out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006166 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02006167 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006168}
6169
Steven Rostedta98a3c32008-05-12 21:20:59 +02006170static ssize_t
6171tracing_entries_read(struct file *filp, char __user *ubuf,
6172 size_t cnt, loff_t *ppos)
6173{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006174 struct inode *inode = file_inode(filp);
6175 struct trace_array *tr = inode->i_private;
6176 int cpu = tracing_get_cpu(inode);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006177 char buf[64];
6178 int r = 0;
6179 ssize_t ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006180
Steven Rostedtdb526ca2009-03-12 13:53:25 -04006181 mutex_lock(&trace_types_lock);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006182
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006183 if (cpu == RING_BUFFER_ALL_CPUS) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006184 int cpu, buf_size_same;
6185 unsigned long size;
6186
6187 size = 0;
6188 buf_size_same = 1;
6189 /* check if all cpu sizes are same */
6190 for_each_tracing_cpu(cpu) {
6191 /* fill in the size from first enabled cpu */
6192 if (size == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006193 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
6194 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006195 buf_size_same = 0;
6196 break;
6197 }
6198 }
6199
6200 if (buf_size_same) {
6201 if (!ring_buffer_expanded)
6202 r = sprintf(buf, "%lu (expanded: %lu)\n",
6203 size >> 10,
6204 trace_buf_size >> 10);
6205 else
6206 r = sprintf(buf, "%lu\n", size >> 10);
6207 } else
6208 r = sprintf(buf, "X\n");
6209 } else
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006210 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006211
Steven Rostedtdb526ca2009-03-12 13:53:25 -04006212 mutex_unlock(&trace_types_lock);
6213
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006214 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6215 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006216}
6217
6218static ssize_t
6219tracing_entries_write(struct file *filp, const char __user *ubuf,
6220 size_t cnt, loff_t *ppos)
6221{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006222 struct inode *inode = file_inode(filp);
6223 struct trace_array *tr = inode->i_private;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006224 unsigned long val;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006225 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006226
Peter Huewe22fe9b52011-06-07 21:58:27 +02006227 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6228 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02006229 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006230
6231 /* must have at least 1 entry */
6232 if (!val)
6233 return -EINVAL;
6234
Steven Rostedt1696b2b2008-11-13 00:09:35 -05006235 /* value is in KB */
6236 val <<= 10;
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006237 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006238 if (ret < 0)
6239 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006240
Jiri Olsacf8517c2009-10-23 19:36:16 -04006241 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006242
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006243 return cnt;
6244}
Steven Rostedtbf5e6512008-11-10 21:46:00 -05006245
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006246static ssize_t
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006247tracing_total_entries_read(struct file *filp, char __user *ubuf,
6248 size_t cnt, loff_t *ppos)
6249{
6250 struct trace_array *tr = filp->private_data;
6251 char buf[64];
6252 int r, cpu;
6253 unsigned long size = 0, expanded_size = 0;
6254
6255 mutex_lock(&trace_types_lock);
6256 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006257 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006258 if (!ring_buffer_expanded)
6259 expanded_size += trace_buf_size >> 10;
6260 }
6261 if (ring_buffer_expanded)
6262 r = sprintf(buf, "%lu\n", size);
6263 else
6264 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
6265 mutex_unlock(&trace_types_lock);
6266
6267 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6268}
6269
6270static ssize_t
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006271tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
6272 size_t cnt, loff_t *ppos)
6273{
6274 /*
6275 * There is no need to read what the user has written, this function
6276 * is just to make sure that there is no error when "echo" is used
6277 */
6278
6279 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006280
6281 return cnt;
6282}
6283
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006284static int
6285tracing_free_buffer_release(struct inode *inode, struct file *filp)
6286{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006287 struct trace_array *tr = inode->i_private;
6288
Steven Rostedtcf30cf62011-06-14 22:44:07 -04006289 /* disable tracing ? */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006290 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
Alexander Z Lam711e1242013-08-02 18:36:15 -07006291 tracer_tracing_off(tr);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006292 /* resize the ring buffer to 0 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006293 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006294
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006295 trace_array_put(tr);
6296
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006297 return 0;
6298}
6299
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006300static ssize_t
6301tracing_mark_write(struct file *filp, const char __user *ubuf,
6302 size_t cnt, loff_t *fpos)
6303{
Alexander Z Lam2d716192013-07-01 15:31:24 -07006304 struct trace_array *tr = filp->private_data;
Steven Rostedtd696b582011-09-22 11:50:27 -04006305 struct ring_buffer_event *event;
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006306 enum event_trigger_type tt = ETT_NONE;
Steven Rostedtd696b582011-09-22 11:50:27 -04006307 struct ring_buffer *buffer;
6308 struct print_entry *entry;
6309 unsigned long irq_flags;
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006310 const char faulted[] = "<faulted>";
Steven Rostedtd696b582011-09-22 11:50:27 -04006311 ssize_t written;
Steven Rostedtd696b582011-09-22 11:50:27 -04006312 int size;
6313 int len;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006314
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006315/* Used in tracing_mark_raw_write() as well */
6316#define FAULTED_SIZE (sizeof(faulted) - 1) /* '\0' is already accounted for */
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006317
Steven Rostedtc76f0692008-11-07 22:36:02 -05006318 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006319 return -EINVAL;
6320
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006321 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -07006322 return -EINVAL;
6323
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006324 if (cnt > TRACE_BUF_SIZE)
6325 cnt = TRACE_BUF_SIZE;
6326
Steven Rostedtd696b582011-09-22 11:50:27 -04006327 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006328
Steven Rostedtd696b582011-09-22 11:50:27 -04006329 local_save_flags(irq_flags);
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006330 size = sizeof(*entry) + cnt + 2; /* add '\0' and possible '\n' */
6331
6332 /* If less than "<faulted>", then make sure we can still add that */
6333 if (cnt < FAULTED_SIZE)
6334 size += FAULTED_SIZE - cnt;
6335
Alexander Z Lam2d716192013-07-01 15:31:24 -07006336 buffer = tr->trace_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05006337 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
6338 irq_flags, preempt_count());
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006339 if (unlikely(!event))
Steven Rostedtd696b582011-09-22 11:50:27 -04006340 /* Ring buffer disabled, return as if not open for write */
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006341 return -EBADF;
Steven Rostedtd696b582011-09-22 11:50:27 -04006342
6343 entry = ring_buffer_event_data(event);
6344 entry->ip = _THIS_IP_;
6345
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006346 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
6347 if (len) {
6348 memcpy(&entry->buf, faulted, FAULTED_SIZE);
6349 cnt = FAULTED_SIZE;
6350 written = -EFAULT;
Steven Rostedtd696b582011-09-22 11:50:27 -04006351 } else
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006352 written = cnt;
6353 len = cnt;
Steven Rostedtd696b582011-09-22 11:50:27 -04006354
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006355 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
6356 /* do not add \n before testing triggers, but add \0 */
6357 entry->buf[cnt] = '\0';
6358 tt = event_triggers_call(tr->trace_marker_file, entry, event);
6359 }
6360
Steven Rostedtd696b582011-09-22 11:50:27 -04006361 if (entry->buf[cnt - 1] != '\n') {
6362 entry->buf[cnt] = '\n';
6363 entry->buf[cnt + 1] = '\0';
6364 } else
6365 entry->buf[cnt] = '\0';
6366
Steven Rostedt7ffbd482012-10-11 12:14:25 -04006367 __buffer_unlock_commit(buffer, event);
Steven Rostedtd696b582011-09-22 11:50:27 -04006368
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006369 if (tt)
6370 event_triggers_post_call(tr->trace_marker_file, tt);
6371
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006372 if (written > 0)
6373 *fpos += written;
Steven Rostedtd696b582011-09-22 11:50:27 -04006374
Steven Rostedtfa32e852016-07-06 15:25:08 -04006375 return written;
6376}
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006377
Steven Rostedtfa32e852016-07-06 15:25:08 -04006378/* Limit it for now to 3K (including tag) */
6379#define RAW_DATA_MAX_SIZE (1024*3)
6380
6381static ssize_t
6382tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
6383 size_t cnt, loff_t *fpos)
6384{
6385 struct trace_array *tr = filp->private_data;
6386 struct ring_buffer_event *event;
6387 struct ring_buffer *buffer;
6388 struct raw_data_entry *entry;
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006389 const char faulted[] = "<faulted>";
Steven Rostedtfa32e852016-07-06 15:25:08 -04006390 unsigned long irq_flags;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006391 ssize_t written;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006392 int size;
6393 int len;
6394
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006395#define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
6396
Steven Rostedtfa32e852016-07-06 15:25:08 -04006397 if (tracing_disabled)
6398 return -EINVAL;
6399
6400 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
6401 return -EINVAL;
6402
6403 /* The marker must at least have a tag id */
6404 if (cnt < sizeof(unsigned int) || cnt > RAW_DATA_MAX_SIZE)
6405 return -EINVAL;
6406
6407 if (cnt > TRACE_BUF_SIZE)
6408 cnt = TRACE_BUF_SIZE;
6409
6410 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
6411
Steven Rostedtfa32e852016-07-06 15:25:08 -04006412 local_save_flags(irq_flags);
6413 size = sizeof(*entry) + cnt;
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006414 if (cnt < FAULT_SIZE_ID)
6415 size += FAULT_SIZE_ID - cnt;
6416
Steven Rostedtfa32e852016-07-06 15:25:08 -04006417 buffer = tr->trace_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05006418 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
6419 irq_flags, preempt_count());
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006420 if (!event)
Steven Rostedtfa32e852016-07-06 15:25:08 -04006421 /* Ring buffer disabled, return as if not open for write */
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006422 return -EBADF;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006423
6424 entry = ring_buffer_event_data(event);
6425
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006426 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
6427 if (len) {
6428 entry->id = -1;
6429 memcpy(&entry->buf, faulted, FAULTED_SIZE);
6430 written = -EFAULT;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006431 } else
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006432 written = cnt;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006433
6434 __buffer_unlock_commit(buffer, event);
6435
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006436 if (written > 0)
6437 *fpos += written;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006438
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02006439 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006440}
6441
Li Zefan13f16d22009-12-08 11:16:11 +08006442static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08006443{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006444 struct trace_array *tr = m->private;
Zhaolei5079f322009-08-25 16:12:56 +08006445 int i;
6446
6447 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08006448 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08006449 "%s%s%s%s", i ? " " : "",
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006450 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
6451 i == tr->clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08006452 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08006453
Li Zefan13f16d22009-12-08 11:16:11 +08006454 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08006455}
6456
Tom Zanussid71bd342018-01-15 20:52:07 -06006457int tracing_set_clock(struct trace_array *tr, const char *clockstr)
Zhaolei5079f322009-08-25 16:12:56 +08006458{
Zhaolei5079f322009-08-25 16:12:56 +08006459 int i;
6460
Zhaolei5079f322009-08-25 16:12:56 +08006461 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
6462 if (strcmp(trace_clocks[i].name, clockstr) == 0)
6463 break;
6464 }
6465 if (i == ARRAY_SIZE(trace_clocks))
6466 return -EINVAL;
6467
Zhaolei5079f322009-08-25 16:12:56 +08006468 mutex_lock(&trace_types_lock);
6469
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006470 tr->clock_id = i;
6471
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006472 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
Zhaolei5079f322009-08-25 16:12:56 +08006473
David Sharp60303ed2012-10-11 16:27:52 -07006474 /*
6475 * New clock may not be consistent with the previous clock.
6476 * Reset the buffer so that it doesn't have incomparable timestamps.
6477 */
Alexander Z Lam94571582013-08-02 18:36:16 -07006478 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006479
6480#ifdef CONFIG_TRACER_MAX_TRACE
Baohong Liu170b3b12017-09-05 16:57:19 -05006481 if (tr->max_buffer.buffer)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006482 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
Alexander Z Lam94571582013-08-02 18:36:16 -07006483 tracing_reset_online_cpus(&tr->max_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006484#endif
David Sharp60303ed2012-10-11 16:27:52 -07006485
Zhaolei5079f322009-08-25 16:12:56 +08006486 mutex_unlock(&trace_types_lock);
6487
Steven Rostedte1e232c2014-02-10 23:38:46 -05006488 return 0;
6489}
6490
6491static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
6492 size_t cnt, loff_t *fpos)
6493{
6494 struct seq_file *m = filp->private_data;
6495 struct trace_array *tr = m->private;
6496 char buf[64];
6497 const char *clockstr;
6498 int ret;
6499
6500 if (cnt >= sizeof(buf))
6501 return -EINVAL;
6502
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08006503 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedte1e232c2014-02-10 23:38:46 -05006504 return -EFAULT;
6505
6506 buf[cnt] = 0;
6507
6508 clockstr = strstrip(buf);
6509
6510 ret = tracing_set_clock(tr, clockstr);
6511 if (ret)
6512 return ret;
6513
Zhaolei5079f322009-08-25 16:12:56 +08006514 *fpos += cnt;
6515
6516 return cnt;
6517}
6518
Li Zefan13f16d22009-12-08 11:16:11 +08006519static int tracing_clock_open(struct inode *inode, struct file *file)
6520{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006521 struct trace_array *tr = inode->i_private;
6522 int ret;
6523
Li Zefan13f16d22009-12-08 11:16:11 +08006524 if (tracing_disabled)
6525 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006526
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006527 if (trace_array_get(tr))
6528 return -ENODEV;
6529
6530 ret = single_open(file, tracing_clock_show, inode->i_private);
6531 if (ret < 0)
6532 trace_array_put(tr);
6533
6534 return ret;
Li Zefan13f16d22009-12-08 11:16:11 +08006535}
6536
Tom Zanussi2c1ea602018-01-15 20:51:41 -06006537static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
6538{
6539 struct trace_array *tr = m->private;
6540
6541 mutex_lock(&trace_types_lock);
6542
6543 if (ring_buffer_time_stamp_abs(tr->trace_buffer.buffer))
6544 seq_puts(m, "delta [absolute]\n");
6545 else
6546 seq_puts(m, "[delta] absolute\n");
6547
6548 mutex_unlock(&trace_types_lock);
6549
6550 return 0;
6551}
6552
6553static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
6554{
6555 struct trace_array *tr = inode->i_private;
6556 int ret;
6557
6558 if (tracing_disabled)
6559 return -ENODEV;
6560
6561 if (trace_array_get(tr))
6562 return -ENODEV;
6563
6564 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
6565 if (ret < 0)
6566 trace_array_put(tr);
6567
6568 return ret;
6569}
6570
Tom Zanussi00b41452018-01-15 20:51:39 -06006571int tracing_set_time_stamp_abs(struct trace_array *tr, bool abs)
6572{
6573 int ret = 0;
6574
6575 mutex_lock(&trace_types_lock);
6576
6577 if (abs && tr->time_stamp_abs_ref++)
6578 goto out;
6579
6580 if (!abs) {
6581 if (WARN_ON_ONCE(!tr->time_stamp_abs_ref)) {
6582 ret = -EINVAL;
6583 goto out;
6584 }
6585
6586 if (--tr->time_stamp_abs_ref)
6587 goto out;
6588 }
6589
6590 ring_buffer_set_time_stamp_abs(tr->trace_buffer.buffer, abs);
6591
6592#ifdef CONFIG_TRACER_MAX_TRACE
6593 if (tr->max_buffer.buffer)
6594 ring_buffer_set_time_stamp_abs(tr->max_buffer.buffer, abs);
6595#endif
6596 out:
6597 mutex_unlock(&trace_types_lock);
6598
6599 return ret;
6600}
6601
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006602struct ftrace_buffer_info {
6603 struct trace_iterator iter;
6604 void *spare;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006605 unsigned int spare_cpu;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006606 unsigned int read;
6607};
6608
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006609#ifdef CONFIG_TRACER_SNAPSHOT
6610static int tracing_snapshot_open(struct inode *inode, struct file *file)
6611{
Oleg Nesterov6484c712013-07-23 17:26:10 +02006612 struct trace_array *tr = inode->i_private;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006613 struct trace_iterator *iter;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006614 struct seq_file *m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006615 int ret = 0;
6616
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006617 if (trace_array_get(tr) < 0)
6618 return -ENODEV;
6619
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006620 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02006621 iter = __tracing_open(inode, file, true);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006622 if (IS_ERR(iter))
6623 ret = PTR_ERR(iter);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006624 } else {
6625 /* Writes still need the seq_file to hold the private data */
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006626 ret = -ENOMEM;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006627 m = kzalloc(sizeof(*m), GFP_KERNEL);
6628 if (!m)
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006629 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006630 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
6631 if (!iter) {
6632 kfree(m);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006633 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006634 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006635 ret = 0;
6636
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006637 iter->tr = tr;
Oleg Nesterov6484c712013-07-23 17:26:10 +02006638 iter->trace_buffer = &tr->max_buffer;
6639 iter->cpu_file = tracing_get_cpu(inode);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006640 m->private = iter;
6641 file->private_data = m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006642 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006643out:
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006644 if (ret < 0)
6645 trace_array_put(tr);
6646
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006647 return ret;
6648}
6649
6650static ssize_t
6651tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
6652 loff_t *ppos)
6653{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006654 struct seq_file *m = filp->private_data;
6655 struct trace_iterator *iter = m->private;
6656 struct trace_array *tr = iter->tr;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006657 unsigned long val;
6658 int ret;
6659
6660 ret = tracing_update_buffers();
6661 if (ret < 0)
6662 return ret;
6663
6664 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6665 if (ret)
6666 return ret;
6667
6668 mutex_lock(&trace_types_lock);
6669
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006670 if (tr->current_trace->use_max_tr) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006671 ret = -EBUSY;
6672 goto out;
6673 }
6674
Tom Zanussia35873a2019-02-13 17:42:45 -06006675 arch_spin_lock(&tr->max_lock);
6676 if (tr->cond_snapshot)
6677 ret = -EBUSY;
6678 arch_spin_unlock(&tr->max_lock);
6679 if (ret)
6680 goto out;
6681
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006682 switch (val) {
6683 case 0:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006684 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6685 ret = -EINVAL;
6686 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006687 }
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04006688 if (tr->allocated_snapshot)
6689 free_snapshot(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006690 break;
6691 case 1:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006692/* Only allow per-cpu swap if the ring buffer supports it */
6693#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
6694 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6695 ret = -EINVAL;
6696 break;
6697 }
6698#endif
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05006699 if (!tr->allocated_snapshot) {
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04006700 ret = tracing_alloc_snapshot_instance(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006701 if (ret < 0)
6702 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006703 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006704 local_irq_disable();
6705 /* Now, we're going to swap */
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006706 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
Tom Zanussia35873a2019-02-13 17:42:45 -06006707 update_max_tr(tr, current, smp_processor_id(), NULL);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006708 else
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05006709 update_max_tr_single(tr, current, iter->cpu_file);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006710 local_irq_enable();
6711 break;
6712 default:
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05006713 if (tr->allocated_snapshot) {
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006714 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
6715 tracing_reset_online_cpus(&tr->max_buffer);
6716 else
6717 tracing_reset(&tr->max_buffer, iter->cpu_file);
6718 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006719 break;
6720 }
6721
6722 if (ret >= 0) {
6723 *ppos += cnt;
6724 ret = cnt;
6725 }
6726out:
6727 mutex_unlock(&trace_types_lock);
6728 return ret;
6729}
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006730
6731static int tracing_snapshot_release(struct inode *inode, struct file *file)
6732{
6733 struct seq_file *m = file->private_data;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006734 int ret;
6735
6736 ret = tracing_release(inode, file);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006737
6738 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006739 return ret;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006740
6741 /* If write only, the seq_file is just a stub */
6742 if (m)
6743 kfree(m->private);
6744 kfree(m);
6745
6746 return 0;
6747}
6748
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006749static int tracing_buffers_open(struct inode *inode, struct file *filp);
6750static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
6751 size_t count, loff_t *ppos);
6752static int tracing_buffers_release(struct inode *inode, struct file *file);
6753static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
6754 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
6755
6756static int snapshot_raw_open(struct inode *inode, struct file *filp)
6757{
6758 struct ftrace_buffer_info *info;
6759 int ret;
6760
6761 ret = tracing_buffers_open(inode, filp);
6762 if (ret < 0)
6763 return ret;
6764
6765 info = filp->private_data;
6766
6767 if (info->iter.trace->use_max_tr) {
6768 tracing_buffers_release(inode, filp);
6769 return -EBUSY;
6770 }
6771
6772 info->iter.snapshot = true;
6773 info->iter.trace_buffer = &info->iter.tr->max_buffer;
6774
6775 return ret;
6776}
6777
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006778#endif /* CONFIG_TRACER_SNAPSHOT */
6779
6780
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04006781static const struct file_operations tracing_thresh_fops = {
6782 .open = tracing_open_generic,
6783 .read = tracing_thresh_read,
6784 .write = tracing_thresh_write,
6785 .llseek = generic_file_llseek,
6786};
6787
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04006788#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006789static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006790 .open = tracing_open_generic,
6791 .read = tracing_max_lat_read,
6792 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006793 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006794};
Chen Gange428abb2015-11-10 05:15:15 +08006795#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006796
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006797static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006798 .open = tracing_open_generic,
6799 .read = tracing_set_trace_read,
6800 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006801 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006802};
6803
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006804static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006805 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006806 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006807 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006808 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006809 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006810 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02006811};
6812
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006813static const struct file_operations tracing_entries_fops = {
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006814 .open = tracing_open_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02006815 .read = tracing_entries_read,
6816 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006817 .llseek = generic_file_llseek,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006818 .release = tracing_release_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02006819};
6820
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006821static const struct file_operations tracing_total_entries_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006822 .open = tracing_open_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006823 .read = tracing_total_entries_read,
6824 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006825 .release = tracing_release_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006826};
6827
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006828static const struct file_operations tracing_free_buffer_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006829 .open = tracing_open_generic_tr,
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006830 .write = tracing_free_buffer_write,
6831 .release = tracing_free_buffer_release,
6832};
6833
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006834static const struct file_operations tracing_mark_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006835 .open = tracing_open_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006836 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006837 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006838 .release = tracing_release_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006839};
6840
Steven Rostedtfa32e852016-07-06 15:25:08 -04006841static const struct file_operations tracing_mark_raw_fops = {
6842 .open = tracing_open_generic_tr,
6843 .write = tracing_mark_raw_write,
6844 .llseek = generic_file_llseek,
6845 .release = tracing_release_generic_tr,
6846};
6847
Zhaolei5079f322009-08-25 16:12:56 +08006848static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08006849 .open = tracing_clock_open,
6850 .read = seq_read,
6851 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006852 .release = tracing_single_release_tr,
Zhaolei5079f322009-08-25 16:12:56 +08006853 .write = tracing_clock_write,
6854};
6855
Tom Zanussi2c1ea602018-01-15 20:51:41 -06006856static const struct file_operations trace_time_stamp_mode_fops = {
6857 .open = tracing_time_stamp_mode_open,
6858 .read = seq_read,
6859 .llseek = seq_lseek,
6860 .release = tracing_single_release_tr,
6861};
6862
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006863#ifdef CONFIG_TRACER_SNAPSHOT
6864static const struct file_operations snapshot_fops = {
6865 .open = tracing_snapshot_open,
6866 .read = seq_read,
6867 .write = tracing_snapshot_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006868 .llseek = tracing_lseek,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006869 .release = tracing_snapshot_release,
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006870};
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006871
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006872static const struct file_operations snapshot_raw_fops = {
6873 .open = snapshot_raw_open,
6874 .read = tracing_buffers_read,
6875 .release = tracing_buffers_release,
6876 .splice_read = tracing_buffers_splice_read,
6877 .llseek = no_llseek,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006878};
6879
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006880#endif /* CONFIG_TRACER_SNAPSHOT */
6881
Steven Rostedt2cadf912008-12-01 22:20:19 -05006882static int tracing_buffers_open(struct inode *inode, struct file *filp)
6883{
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006884 struct trace_array *tr = inode->i_private;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006885 struct ftrace_buffer_info *info;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006886 int ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006887
6888 if (tracing_disabled)
6889 return -ENODEV;
6890
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006891 if (trace_array_get(tr) < 0)
6892 return -ENODEV;
6893
Steven Rostedt2cadf912008-12-01 22:20:19 -05006894 info = kzalloc(sizeof(*info), GFP_KERNEL);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006895 if (!info) {
6896 trace_array_put(tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006897 return -ENOMEM;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006898 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05006899
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006900 mutex_lock(&trace_types_lock);
6901
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006902 info->iter.tr = tr;
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006903 info->iter.cpu_file = tracing_get_cpu(inode);
Steven Rostedtb6273442013-02-28 13:44:11 -05006904 info->iter.trace = tr->current_trace;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006905 info->iter.trace_buffer = &tr->trace_buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006906 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006907 /* Force reading ring buffer for first read */
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006908 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006909
6910 filp->private_data = info;
6911
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05006912 tr->current_trace->ref++;
6913
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006914 mutex_unlock(&trace_types_lock);
6915
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006916 ret = nonseekable_open(inode, filp);
6917 if (ret < 0)
6918 trace_array_put(tr);
6919
6920 return ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006921}
6922
Al Viro9dd95742017-07-03 00:42:43 -04006923static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006924tracing_buffers_poll(struct file *filp, poll_table *poll_table)
6925{
6926 struct ftrace_buffer_info *info = filp->private_data;
6927 struct trace_iterator *iter = &info->iter;
6928
6929 return trace_poll(iter, filp, poll_table);
6930}
6931
Steven Rostedt2cadf912008-12-01 22:20:19 -05006932static ssize_t
6933tracing_buffers_read(struct file *filp, char __user *ubuf,
6934 size_t count, loff_t *ppos)
6935{
6936 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006937 struct trace_iterator *iter = &info->iter;
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04006938 ssize_t ret = 0;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006939 ssize_t size;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006940
Steven Rostedt2dc5d122009-03-04 19:10:05 -05006941 if (!count)
6942 return 0;
6943
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006944#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006945 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
6946 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006947#endif
6948
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006949 if (!info->spare) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006950 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
6951 iter->cpu_file);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04006952 if (IS_ERR(info->spare)) {
6953 ret = PTR_ERR(info->spare);
6954 info->spare = NULL;
6955 } else {
6956 info->spare_cpu = iter->cpu_file;
6957 }
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006958 }
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006959 if (!info->spare)
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04006960 return ret;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006961
Steven Rostedt2cadf912008-12-01 22:20:19 -05006962 /* Do we have previous read data to read? */
6963 if (info->read < PAGE_SIZE)
6964 goto read;
6965
Steven Rostedtb6273442013-02-28 13:44:11 -05006966 again:
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006967 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006968 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006969 &info->spare,
6970 count,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006971 iter->cpu_file, 0);
6972 trace_access_unlock(iter->cpu_file);
Steven Rostedtb6273442013-02-28 13:44:11 -05006973
6974 if (ret < 0) {
6975 if (trace_empty(iter)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006976 if ((filp->f_flags & O_NONBLOCK))
6977 return -EAGAIN;
6978
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05006979 ret = wait_on_pipe(iter, 0);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006980 if (ret)
6981 return ret;
6982
Steven Rostedtb6273442013-02-28 13:44:11 -05006983 goto again;
6984 }
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006985 return 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05006986 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05006987
Steven Rostedt436fc282011-10-14 10:44:25 -04006988 info->read = 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05006989 read:
Steven Rostedt2cadf912008-12-01 22:20:19 -05006990 size = PAGE_SIZE - info->read;
6991 if (size > count)
6992 size = count;
6993
6994 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006995 if (ret == size)
6996 return -EFAULT;
6997
Steven Rostedt2dc5d122009-03-04 19:10:05 -05006998 size -= ret;
6999
Steven Rostedt2cadf912008-12-01 22:20:19 -05007000 *ppos += size;
7001 info->read += size;
7002
7003 return size;
7004}
7005
7006static int tracing_buffers_release(struct inode *inode, struct file *file)
7007{
7008 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007009 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007010
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007011 mutex_lock(&trace_types_lock);
7012
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05007013 iter->tr->current_trace->ref--;
7014
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04007015 __trace_array_put(iter->tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007016
Lai Jiangshanddd538f2009-04-02 15:16:59 +08007017 if (info->spare)
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007018 ring_buffer_free_read_page(iter->trace_buffer->buffer,
7019 info->spare_cpu, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007020 kfree(info);
7021
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007022 mutex_unlock(&trace_types_lock);
7023
Steven Rostedt2cadf912008-12-01 22:20:19 -05007024 return 0;
7025}
7026
7027struct buffer_ref {
7028 struct ring_buffer *buffer;
7029 void *page;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007030 int cpu;
Jann Hornb9872222019-04-04 23:59:25 +02007031 refcount_t refcount;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007032};
7033
Jann Hornb9872222019-04-04 23:59:25 +02007034static void buffer_ref_release(struct buffer_ref *ref)
7035{
7036 if (!refcount_dec_and_test(&ref->refcount))
7037 return;
7038 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
7039 kfree(ref);
7040}
7041
Steven Rostedt2cadf912008-12-01 22:20:19 -05007042static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
7043 struct pipe_buffer *buf)
7044{
7045 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7046
Jann Hornb9872222019-04-04 23:59:25 +02007047 buffer_ref_release(ref);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007048 buf->private = 0;
7049}
7050
Steven Rostedt2cadf912008-12-01 22:20:19 -05007051static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
7052 struct pipe_buffer *buf)
7053{
7054 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7055
Jann Hornb9872222019-04-04 23:59:25 +02007056 refcount_inc(&ref->refcount);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007057}
7058
7059/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08007060static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05007061 .confirm = generic_pipe_buf_confirm,
7062 .release = buffer_pipe_buf_release,
Jann Hornb9872222019-04-04 23:59:25 +02007063 .steal = generic_pipe_buf_nosteal,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007064 .get = buffer_pipe_buf_get,
7065};
7066
7067/*
7068 * Callback from splice_to_pipe(), if we need to release some pages
7069 * at the end of the spd in case we error'ed out in filling the pipe.
7070 */
7071static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
7072{
7073 struct buffer_ref *ref =
7074 (struct buffer_ref *)spd->partial[i].private;
7075
Jann Hornb9872222019-04-04 23:59:25 +02007076 buffer_ref_release(ref);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007077 spd->partial[i].private = 0;
7078}
7079
7080static ssize_t
7081tracing_buffers_splice_read(struct file *file, loff_t *ppos,
7082 struct pipe_inode_info *pipe, size_t len,
7083 unsigned int flags)
7084{
7085 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007086 struct trace_iterator *iter = &info->iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02007087 struct partial_page partial_def[PIPE_DEF_BUFFERS];
7088 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05007089 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02007090 .pages = pages_def,
7091 .partial = partial_def,
Eric Dumazet047fe362012-06-12 15:24:40 +02007092 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007093 .ops = &buffer_pipe_buf_ops,
7094 .spd_release = buffer_spd_release,
7095 };
7096 struct buffer_ref *ref;
Steven Rostedt (VMware)6b7e6332017-12-22 20:38:57 -05007097 int entries, i;
Rabin Vincent07906da2014-11-06 22:26:07 +01007098 ssize_t ret = 0;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007099
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007100#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007101 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
7102 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007103#endif
7104
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007105 if (*ppos & (PAGE_SIZE - 1))
7106 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08007107
7108 if (len & (PAGE_SIZE - 1)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007109 if (len < PAGE_SIZE)
7110 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08007111 len &= PAGE_MASK;
7112 }
7113
Al Viro1ae22932016-09-17 18:31:46 -04007114 if (splice_grow_spd(pipe, &spd))
7115 return -ENOMEM;
7116
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007117 again:
7118 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007119 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt93459c62009-04-29 00:23:13 -04007120
Al Viroa786c062014-04-11 12:01:03 -04007121 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05007122 struct page *page;
7123 int r;
7124
7125 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
Rabin Vincent07906da2014-11-06 22:26:07 +01007126 if (!ref) {
7127 ret = -ENOMEM;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007128 break;
Rabin Vincent07906da2014-11-06 22:26:07 +01007129 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05007130
Jann Hornb9872222019-04-04 23:59:25 +02007131 refcount_set(&ref->refcount, 1);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007132 ref->buffer = iter->trace_buffer->buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007133 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04007134 if (IS_ERR(ref->page)) {
7135 ret = PTR_ERR(ref->page);
7136 ref->page = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007137 kfree(ref);
7138 break;
7139 }
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007140 ref->cpu = iter->cpu_file;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007141
7142 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007143 len, iter->cpu_file, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007144 if (r < 0) {
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007145 ring_buffer_free_read_page(ref->buffer, ref->cpu,
7146 ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007147 kfree(ref);
7148 break;
7149 }
7150
Steven Rostedt2cadf912008-12-01 22:20:19 -05007151 page = virt_to_page(ref->page);
7152
7153 spd.pages[i] = page;
7154 spd.partial[i].len = PAGE_SIZE;
7155 spd.partial[i].offset = 0;
7156 spd.partial[i].private = (unsigned long)ref;
7157 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08007158 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04007159
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007160 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007161 }
7162
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007163 trace_access_unlock(iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007164 spd.nr_pages = i;
7165
7166 /* did we read anything? */
7167 if (!spd.nr_pages) {
Rabin Vincent07906da2014-11-06 22:26:07 +01007168 if (ret)
Al Viro1ae22932016-09-17 18:31:46 -04007169 goto out;
Rabin Vincent07906da2014-11-06 22:26:07 +01007170
Al Viro1ae22932016-09-17 18:31:46 -04007171 ret = -EAGAIN;
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007172 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
Al Viro1ae22932016-09-17 18:31:46 -04007173 goto out;
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007174
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05007175 ret = wait_on_pipe(iter, iter->tr->buffer_percent);
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04007176 if (ret)
Al Viro1ae22932016-09-17 18:31:46 -04007177 goto out;
Rabin Vincente30f53a2014-11-10 19:46:34 +01007178
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007179 goto again;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007180 }
7181
7182 ret = splice_to_pipe(pipe, &spd);
Al Viro1ae22932016-09-17 18:31:46 -04007183out:
Eric Dumazet047fe362012-06-12 15:24:40 +02007184 splice_shrink_spd(&spd);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007185
Steven Rostedt2cadf912008-12-01 22:20:19 -05007186 return ret;
7187}
7188
7189static const struct file_operations tracing_buffers_fops = {
7190 .open = tracing_buffers_open,
7191 .read = tracing_buffers_read,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007192 .poll = tracing_buffers_poll,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007193 .release = tracing_buffers_release,
7194 .splice_read = tracing_buffers_splice_read,
7195 .llseek = no_llseek,
7196};
7197
Steven Rostedtc8d77182009-04-29 18:03:45 -04007198static ssize_t
7199tracing_stats_read(struct file *filp, char __user *ubuf,
7200 size_t count, loff_t *ppos)
7201{
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007202 struct inode *inode = file_inode(filp);
7203 struct trace_array *tr = inode->i_private;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007204 struct trace_buffer *trace_buf = &tr->trace_buffer;
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007205 int cpu = tracing_get_cpu(inode);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007206 struct trace_seq *s;
7207 unsigned long cnt;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007208 unsigned long long t;
7209 unsigned long usec_rem;
Steven Rostedtc8d77182009-04-29 18:03:45 -04007210
Li Zefane4f2d102009-06-15 10:57:28 +08007211 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007212 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01007213 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04007214
7215 trace_seq_init(s);
7216
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007217 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007218 trace_seq_printf(s, "entries: %ld\n", cnt);
7219
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007220 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007221 trace_seq_printf(s, "overrun: %ld\n", cnt);
7222
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007223 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007224 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
7225
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007226 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007227 trace_seq_printf(s, "bytes: %ld\n", cnt);
7228
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09007229 if (trace_clocks[tr->clock_id].in_ns) {
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007230 /* local or global for trace_clock */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007231 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007232 usec_rem = do_div(t, USEC_PER_SEC);
7233 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
7234 t, usec_rem);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007235
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007236 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007237 usec_rem = do_div(t, USEC_PER_SEC);
7238 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
7239 } else {
7240 /* counter or tsc mode for trace_clock */
7241 trace_seq_printf(s, "oldest event ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007242 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007243
7244 trace_seq_printf(s, "now ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007245 ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007246 }
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007247
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007248 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
Slava Pestov884bfe82011-07-15 14:23:58 -07007249 trace_seq_printf(s, "dropped events: %ld\n", cnt);
7250
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007251 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05007252 trace_seq_printf(s, "read events: %ld\n", cnt);
7253
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05007254 count = simple_read_from_buffer(ubuf, count, ppos,
7255 s->buffer, trace_seq_used(s));
Steven Rostedtc8d77182009-04-29 18:03:45 -04007256
7257 kfree(s);
7258
7259 return count;
7260}
7261
7262static const struct file_operations tracing_stats_fops = {
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007263 .open = tracing_open_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04007264 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007265 .llseek = generic_file_llseek,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007266 .release = tracing_release_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04007267};
7268
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007269#ifdef CONFIG_DYNAMIC_FTRACE
7270
7271static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007272tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007273 size_t cnt, loff_t *ppos)
7274{
7275 unsigned long *p = filp->private_data;
Steven Rostedt (VMware)6a9c9812017-06-27 11:02:49 -04007276 char buf[64]; /* Not too big for a shallow stack */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007277 int r;
7278
Steven Rostedt (VMware)6a9c9812017-06-27 11:02:49 -04007279 r = scnprintf(buf, 63, "%ld", *p);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007280 buf[r++] = '\n';
7281
Steven Rostedt (VMware)6a9c9812017-06-27 11:02:49 -04007282 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007283}
7284
Steven Rostedt5e2336a2009-03-05 21:44:55 -05007285static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02007286 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007287 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007288 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007289};
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007290#endif /* CONFIG_DYNAMIC_FTRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007291
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007292#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
7293static void
Steven Rostedt (VMware)bca6c8d2017-04-03 18:18:47 -04007294ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007295 struct trace_array *tr, struct ftrace_probe_ops *ops,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007296 void *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007297{
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -04007298 tracing_snapshot_instance(tr);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007299}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007300
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007301static void
Steven Rostedt (VMware)bca6c8d2017-04-03 18:18:47 -04007302ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007303 struct trace_array *tr, struct ftrace_probe_ops *ops,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007304 void *data)
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007305{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007306 struct ftrace_func_mapper *mapper = data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007307 long *count = NULL;
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007308
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007309 if (mapper)
7310 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007311
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007312 if (count) {
7313
7314 if (*count <= 0)
7315 return;
7316
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007317 (*count)--;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007318 }
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007319
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -04007320 tracing_snapshot_instance(tr);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007321}
7322
7323static int
7324ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
7325 struct ftrace_probe_ops *ops, void *data)
7326{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007327 struct ftrace_func_mapper *mapper = data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007328 long *count = NULL;
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007329
7330 seq_printf(m, "%ps:", (void *)ip);
7331
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01007332 seq_puts(m, "snapshot");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007333
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007334 if (mapper)
7335 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
7336
7337 if (count)
7338 seq_printf(m, ":count=%ld\n", *count);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007339 else
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007340 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007341
7342 return 0;
7343}
7344
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007345static int
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007346ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007347 unsigned long ip, void *init_data, void **data)
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007348{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007349 struct ftrace_func_mapper *mapper = *data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007350
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007351 if (!mapper) {
7352 mapper = allocate_ftrace_func_mapper();
7353 if (!mapper)
7354 return -ENOMEM;
7355 *data = mapper;
7356 }
7357
7358 return ftrace_func_mapper_add_ip(mapper, ip, init_data);
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007359}
7360
7361static void
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007362ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007363 unsigned long ip, void *data)
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007364{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007365 struct ftrace_func_mapper *mapper = data;
7366
7367 if (!ip) {
7368 if (!mapper)
7369 return;
7370 free_ftrace_func_mapper(mapper, NULL);
7371 return;
7372 }
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007373
7374 ftrace_func_mapper_remove_ip(mapper, ip);
7375}
7376
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007377static struct ftrace_probe_ops snapshot_probe_ops = {
7378 .func = ftrace_snapshot,
7379 .print = ftrace_snapshot_print,
7380};
7381
7382static struct ftrace_probe_ops snapshot_count_probe_ops = {
7383 .func = ftrace_count_snapshot,
7384 .print = ftrace_snapshot_print,
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007385 .init = ftrace_snapshot_init,
7386 .free = ftrace_snapshot_free,
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007387};
7388
7389static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007390ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007391 char *glob, char *cmd, char *param, int enable)
7392{
7393 struct ftrace_probe_ops *ops;
7394 void *count = (void *)-1;
7395 char *number;
7396 int ret;
7397
Steven Rostedt (VMware)0f179762017-06-29 10:05:45 -04007398 if (!tr)
7399 return -ENODEV;
7400
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007401 /* hash funcs only work with set_ftrace_filter */
7402 if (!enable)
7403 return -EINVAL;
7404
7405 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
7406
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04007407 if (glob[0] == '!')
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04007408 return unregister_ftrace_function_probe_func(glob+1, tr, ops);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007409
7410 if (!param)
7411 goto out_reg;
7412
7413 number = strsep(&param, ":");
7414
7415 if (!strlen(number))
7416 goto out_reg;
7417
7418 /*
7419 * We use the callback data field (which is a pointer)
7420 * as our counter.
7421 */
7422 ret = kstrtoul(number, 0, (unsigned long *)&count);
7423 if (ret)
7424 return ret;
7425
7426 out_reg:
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04007427 ret = tracing_alloc_snapshot_instance(tr);
Steven Rostedt (VMware)df62db52017-04-19 12:07:08 -04007428 if (ret < 0)
7429 goto out;
7430
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007431 ret = register_ftrace_function_probe(glob, tr, ops, count);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007432
Steven Rostedt (VMware)df62db52017-04-19 12:07:08 -04007433 out:
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007434 return ret < 0 ? ret : 0;
7435}
7436
7437static struct ftrace_func_command ftrace_snapshot_cmd = {
7438 .name = "snapshot",
7439 .func = ftrace_trace_snapshot_callback,
7440};
7441
Tom Zanussi38de93a2013-10-24 08:34:18 -05007442static __init int register_snapshot_cmd(void)
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007443{
7444 return register_ftrace_command(&ftrace_snapshot_cmd);
7445}
7446#else
Tom Zanussi38de93a2013-10-24 08:34:18 -05007447static inline __init int register_snapshot_cmd(void) { return 0; }
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007448#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007449
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007450static struct dentry *tracing_get_dentry(struct trace_array *tr)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007451{
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007452 if (WARN_ON(!tr->dir))
7453 return ERR_PTR(-ENODEV);
7454
7455 /* Top directory uses NULL as the parent */
7456 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
7457 return NULL;
7458
7459 /* All sub buffers have a descriptor */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007460 return tr->dir;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007461}
7462
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007463static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
7464{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007465 struct dentry *d_tracer;
7466
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007467 if (tr->percpu_dir)
7468 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007469
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007470 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05007471 if (IS_ERR(d_tracer))
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007472 return NULL;
7473
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007474 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007475
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007476 WARN_ONCE(!tr->percpu_dir,
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007477 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007478
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007479 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007480}
7481
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007482static struct dentry *
7483trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
7484 void *data, long cpu, const struct file_operations *fops)
7485{
7486 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
7487
7488 if (ret) /* See tracing_get_cpu() */
David Howells7682c912015-03-17 22:26:16 +00007489 d_inode(ret)->i_cdev = (void *)(cpu + 1);
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007490 return ret;
7491}
7492
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007493static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007494tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007495{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007496 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007497 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04007498 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007499
Namhyung Kim0a3d7ce2012-04-23 10:11:57 +09007500 if (!d_percpu)
7501 return;
7502
Steven Rostedtdd49a382010-10-20 21:51:26 -04007503 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007504 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007505 if (!d_cpu) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07007506 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007507 return;
7508 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007509
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007510 /* per cpu trace_pipe */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007511 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
Oleg Nesterov15544202013-07-23 17:25:57 +02007512 tr, cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007513
7514 /* per cpu trace */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007515 trace_create_cpu_file("trace", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007516 tr, cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04007517
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007518 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007519 tr, cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007520
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007521 trace_create_cpu_file("stats", 0444, d_cpu,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007522 tr, cpu, &tracing_stats_fops);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08007523
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007524 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02007525 tr, cpu, &tracing_entries_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05007526
7527#ifdef CONFIG_TRACER_SNAPSHOT
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007528 trace_create_cpu_file("snapshot", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007529 tr, cpu, &snapshot_fops);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007530
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007531 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007532 tr, cpu, &snapshot_raw_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05007533#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007534}
7535
Steven Rostedt60a11772008-05-12 21:20:44 +02007536#ifdef CONFIG_FTRACE_SELFTEST
7537/* Let selftest have access to static functions in this file */
7538#include "trace_selftest.c"
7539#endif
7540
Steven Rostedt577b7852009-02-26 23:43:05 -05007541static ssize_t
7542trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
7543 loff_t *ppos)
7544{
7545 struct trace_option_dentry *topt = filp->private_data;
7546 char *buf;
7547
7548 if (topt->flags->val & topt->opt->bit)
7549 buf = "1\n";
7550 else
7551 buf = "0\n";
7552
7553 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
7554}
7555
7556static ssize_t
7557trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
7558 loff_t *ppos)
7559{
7560 struct trace_option_dentry *topt = filp->private_data;
7561 unsigned long val;
Steven Rostedt577b7852009-02-26 23:43:05 -05007562 int ret;
7563
Peter Huewe22fe9b52011-06-07 21:58:27 +02007564 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7565 if (ret)
Steven Rostedt577b7852009-02-26 23:43:05 -05007566 return ret;
7567
Li Zefan8d18eaa2009-12-08 11:17:06 +08007568 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05007569 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08007570
7571 if (!!(topt->flags->val & topt->opt->bit) != val) {
7572 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05007573 ret = __set_tracer_option(topt->tr, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05007574 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08007575 mutex_unlock(&trace_types_lock);
7576 if (ret)
7577 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05007578 }
7579
7580 *ppos += cnt;
7581
7582 return cnt;
7583}
7584
7585
7586static const struct file_operations trace_options_fops = {
7587 .open = tracing_open_generic,
7588 .read = trace_options_read,
7589 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007590 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05007591};
7592
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007593/*
7594 * In order to pass in both the trace_array descriptor as well as the index
7595 * to the flag that the trace option file represents, the trace_array
7596 * has a character array of trace_flags_index[], which holds the index
7597 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
7598 * The address of this character array is passed to the flag option file
7599 * read/write callbacks.
7600 *
7601 * In order to extract both the index and the trace_array descriptor,
7602 * get_tr_index() uses the following algorithm.
7603 *
7604 * idx = *ptr;
7605 *
7606 * As the pointer itself contains the address of the index (remember
7607 * index[1] == 1).
7608 *
7609 * Then to get the trace_array descriptor, by subtracting that index
7610 * from the ptr, we get to the start of the index itself.
7611 *
7612 * ptr - idx == &index[0]
7613 *
7614 * Then a simple container_of() from that pointer gets us to the
7615 * trace_array descriptor.
7616 */
7617static void get_tr_index(void *data, struct trace_array **ptr,
7618 unsigned int *pindex)
7619{
7620 *pindex = *(unsigned char *)data;
7621
7622 *ptr = container_of(data - *pindex, struct trace_array,
7623 trace_flags_index);
7624}
7625
Steven Rostedta8259072009-02-26 22:19:12 -05007626static ssize_t
7627trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
7628 loff_t *ppos)
7629{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007630 void *tr_index = filp->private_data;
7631 struct trace_array *tr;
7632 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05007633 char *buf;
7634
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007635 get_tr_index(tr_index, &tr, &index);
7636
7637 if (tr->trace_flags & (1 << index))
Steven Rostedta8259072009-02-26 22:19:12 -05007638 buf = "1\n";
7639 else
7640 buf = "0\n";
7641
7642 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
7643}
7644
7645static ssize_t
7646trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
7647 loff_t *ppos)
7648{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007649 void *tr_index = filp->private_data;
7650 struct trace_array *tr;
7651 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05007652 unsigned long val;
7653 int ret;
7654
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007655 get_tr_index(tr_index, &tr, &index);
7656
Peter Huewe22fe9b52011-06-07 21:58:27 +02007657 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7658 if (ret)
Steven Rostedta8259072009-02-26 22:19:12 -05007659 return ret;
7660
Zhaoleif2d84b62009-08-07 18:55:48 +08007661 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05007662 return -EINVAL;
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04007663
7664 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007665 ret = set_tracer_flag(tr, 1 << index, val);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04007666 mutex_unlock(&trace_types_lock);
Steven Rostedta8259072009-02-26 22:19:12 -05007667
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04007668 if (ret < 0)
7669 return ret;
7670
Steven Rostedta8259072009-02-26 22:19:12 -05007671 *ppos += cnt;
7672
7673 return cnt;
7674}
7675
Steven Rostedta8259072009-02-26 22:19:12 -05007676static const struct file_operations trace_options_core_fops = {
7677 .open = tracing_open_generic,
7678 .read = trace_options_core_read,
7679 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007680 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05007681};
7682
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007683struct dentry *trace_create_file(const char *name,
Al Virof4ae40a62011-07-24 04:33:43 -04007684 umode_t mode,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007685 struct dentry *parent,
7686 void *data,
7687 const struct file_operations *fops)
7688{
7689 struct dentry *ret;
7690
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007691 ret = tracefs_create_file(name, mode, parent, data, fops);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007692 if (!ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -07007693 pr_warn("Could not create tracefs '%s' entry\n", name);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007694
7695 return ret;
7696}
7697
7698
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007699static struct dentry *trace_options_init_dentry(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05007700{
7701 struct dentry *d_tracer;
Steven Rostedta8259072009-02-26 22:19:12 -05007702
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007703 if (tr->options)
7704 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05007705
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007706 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05007707 if (IS_ERR(d_tracer))
Steven Rostedta8259072009-02-26 22:19:12 -05007708 return NULL;
7709
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007710 tr->options = tracefs_create_dir("options", d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007711 if (!tr->options) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07007712 pr_warn("Could not create tracefs directory 'options'\n");
Steven Rostedta8259072009-02-26 22:19:12 -05007713 return NULL;
7714 }
7715
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007716 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05007717}
7718
Steven Rostedt577b7852009-02-26 23:43:05 -05007719static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007720create_trace_option_file(struct trace_array *tr,
7721 struct trace_option_dentry *topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05007722 struct tracer_flags *flags,
7723 struct tracer_opt *opt)
7724{
7725 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05007726
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007727 t_options = trace_options_init_dentry(tr);
Steven Rostedt577b7852009-02-26 23:43:05 -05007728 if (!t_options)
7729 return;
7730
7731 topt->flags = flags;
7732 topt->opt = opt;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007733 topt->tr = tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05007734
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007735 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05007736 &trace_options_fops);
7737
Steven Rostedt577b7852009-02-26 23:43:05 -05007738}
7739
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007740static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007741create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
Steven Rostedt577b7852009-02-26 23:43:05 -05007742{
7743 struct trace_option_dentry *topts;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007744 struct trace_options *tr_topts;
Steven Rostedt577b7852009-02-26 23:43:05 -05007745 struct tracer_flags *flags;
7746 struct tracer_opt *opts;
7747 int cnt;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007748 int i;
Steven Rostedt577b7852009-02-26 23:43:05 -05007749
7750 if (!tracer)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007751 return;
Steven Rostedt577b7852009-02-26 23:43:05 -05007752
7753 flags = tracer->flags;
7754
7755 if (!flags || !flags->opts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007756 return;
7757
7758 /*
7759 * If this is an instance, only create flags for tracers
7760 * the instance may have.
7761 */
7762 if (!trace_ok_for_array(tracer, tr))
7763 return;
7764
7765 for (i = 0; i < tr->nr_topts; i++) {
Chunyu Hud39cdd22016-03-08 21:37:01 +08007766 /* Make sure there's no duplicate flags. */
7767 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007768 return;
7769 }
Steven Rostedt577b7852009-02-26 23:43:05 -05007770
7771 opts = flags->opts;
7772
7773 for (cnt = 0; opts[cnt].name; cnt++)
7774 ;
7775
Steven Rostedt0cfe8242009-02-27 10:51:10 -05007776 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05007777 if (!topts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007778 return;
7779
7780 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
7781 GFP_KERNEL);
7782 if (!tr_topts) {
7783 kfree(topts);
7784 return;
7785 }
7786
7787 tr->topts = tr_topts;
7788 tr->topts[tr->nr_topts].tracer = tracer;
7789 tr->topts[tr->nr_topts].topts = topts;
7790 tr->nr_topts++;
Steven Rostedt577b7852009-02-26 23:43:05 -05007791
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04007792 for (cnt = 0; opts[cnt].name; cnt++) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007793 create_trace_option_file(tr, &topts[cnt], flags,
Steven Rostedt577b7852009-02-26 23:43:05 -05007794 &opts[cnt]);
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04007795 WARN_ONCE(topts[cnt].entry == NULL,
7796 "Failed to create trace option: %s",
7797 opts[cnt].name);
7798 }
Steven Rostedt577b7852009-02-26 23:43:05 -05007799}
7800
Steven Rostedta8259072009-02-26 22:19:12 -05007801static struct dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007802create_trace_option_core_file(struct trace_array *tr,
7803 const char *option, long index)
Steven Rostedta8259072009-02-26 22:19:12 -05007804{
7805 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05007806
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007807 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05007808 if (!t_options)
7809 return NULL;
7810
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007811 return trace_create_file(option, 0644, t_options,
7812 (void *)&tr->trace_flags_index[index],
7813 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05007814}
7815
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04007816static void create_trace_options_dir(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05007817{
7818 struct dentry *t_options;
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04007819 bool top_level = tr == &global_trace;
Steven Rostedta8259072009-02-26 22:19:12 -05007820 int i;
7821
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007822 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05007823 if (!t_options)
7824 return;
7825
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04007826 for (i = 0; trace_options[i]; i++) {
7827 if (top_level ||
7828 !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
7829 create_trace_option_core_file(tr, trace_options[i], i);
7830 }
Steven Rostedta8259072009-02-26 22:19:12 -05007831}
7832
Steven Rostedt499e5472012-02-22 15:50:28 -05007833static ssize_t
7834rb_simple_read(struct file *filp, char __user *ubuf,
7835 size_t cnt, loff_t *ppos)
7836{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04007837 struct trace_array *tr = filp->private_data;
Steven Rostedt499e5472012-02-22 15:50:28 -05007838 char buf[64];
7839 int r;
7840
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04007841 r = tracer_tracing_is_on(tr);
Steven Rostedt499e5472012-02-22 15:50:28 -05007842 r = sprintf(buf, "%d\n", r);
7843
7844 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7845}
7846
7847static ssize_t
7848rb_simple_write(struct file *filp, const char __user *ubuf,
7849 size_t cnt, loff_t *ppos)
7850{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04007851 struct trace_array *tr = filp->private_data;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007852 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt499e5472012-02-22 15:50:28 -05007853 unsigned long val;
7854 int ret;
7855
7856 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7857 if (ret)
7858 return ret;
7859
7860 if (buffer) {
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05007861 mutex_lock(&trace_types_lock);
Steven Rostedt (VMware)f1436412018-08-01 15:40:57 -04007862 if (!!val == tracer_tracing_is_on(tr)) {
7863 val = 0; /* do nothing */
7864 } else if (val) {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04007865 tracer_tracing_on(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007866 if (tr->current_trace->start)
7867 tr->current_trace->start(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05007868 } else {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04007869 tracer_tracing_off(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007870 if (tr->current_trace->stop)
7871 tr->current_trace->stop(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05007872 }
7873 mutex_unlock(&trace_types_lock);
Steven Rostedt499e5472012-02-22 15:50:28 -05007874 }
7875
7876 (*ppos)++;
7877
7878 return cnt;
7879}
7880
7881static const struct file_operations rb_simple_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007882 .open = tracing_open_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05007883 .read = rb_simple_read,
7884 .write = rb_simple_write,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007885 .release = tracing_release_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05007886 .llseek = default_llseek,
7887};
7888
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05007889static ssize_t
7890buffer_percent_read(struct file *filp, char __user *ubuf,
7891 size_t cnt, loff_t *ppos)
7892{
7893 struct trace_array *tr = filp->private_data;
7894 char buf[64];
7895 int r;
7896
7897 r = tr->buffer_percent;
7898 r = sprintf(buf, "%d\n", r);
7899
7900 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7901}
7902
7903static ssize_t
7904buffer_percent_write(struct file *filp, const char __user *ubuf,
7905 size_t cnt, loff_t *ppos)
7906{
7907 struct trace_array *tr = filp->private_data;
7908 unsigned long val;
7909 int ret;
7910
7911 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7912 if (ret)
7913 return ret;
7914
7915 if (val > 100)
7916 return -EINVAL;
7917
7918 if (!val)
7919 val = 1;
7920
7921 tr->buffer_percent = val;
7922
7923 (*ppos)++;
7924
7925 return cnt;
7926}
7927
7928static const struct file_operations buffer_percent_fops = {
7929 .open = tracing_open_generic_tr,
7930 .read = buffer_percent_read,
7931 .write = buffer_percent_write,
7932 .release = tracing_release_generic_tr,
7933 .llseek = default_llseek,
7934};
7935
Steven Rostedt277ba042012-08-03 16:10:49 -04007936struct dentry *trace_instance_dir;
7937
7938static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007939init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
Steven Rostedt277ba042012-08-03 16:10:49 -04007940
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007941static int
7942allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
Steven Rostedt277ba042012-08-03 16:10:49 -04007943{
7944 enum ring_buffer_flags rb_flags;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007945
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007946 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007947
Steven Rostedt (Red Hat)dced3412014-01-14 10:19:46 -05007948 buf->tr = tr;
7949
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007950 buf->buffer = ring_buffer_alloc(size, rb_flags);
7951 if (!buf->buffer)
7952 return -ENOMEM;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007953
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007954 buf->data = alloc_percpu(struct trace_array_cpu);
7955 if (!buf->data) {
7956 ring_buffer_free(buf->buffer);
Steven Rostedt (VMware)4397f042017-12-26 20:07:34 -05007957 buf->buffer = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007958 return -ENOMEM;
7959 }
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007960
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007961 /* Allocate the first page for all buffers */
7962 set_buffer_entries(&tr->trace_buffer,
7963 ring_buffer_size(tr->trace_buffer.buffer, 0));
7964
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007965 return 0;
7966}
7967
7968static int allocate_trace_buffers(struct trace_array *tr, int size)
7969{
7970 int ret;
7971
7972 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
7973 if (ret)
7974 return ret;
7975
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007976#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007977 ret = allocate_trace_buffer(tr, &tr->max_buffer,
7978 allocate_snapshot ? size : 1);
7979 if (WARN_ON(ret)) {
7980 ring_buffer_free(tr->trace_buffer.buffer);
Jing Xia24f2aaf2017-12-26 15:12:53 +08007981 tr->trace_buffer.buffer = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007982 free_percpu(tr->trace_buffer.data);
Jing Xia24f2aaf2017-12-26 15:12:53 +08007983 tr->trace_buffer.data = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007984 return -ENOMEM;
7985 }
7986 tr->allocated_snapshot = allocate_snapshot;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007987
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007988 /*
7989 * Only the top level trace array gets its snapshot allocated
7990 * from the kernel command line.
7991 */
7992 allocate_snapshot = false;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007993#endif
7994 return 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007995}
7996
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04007997static void free_trace_buffer(struct trace_buffer *buf)
7998{
7999 if (buf->buffer) {
8000 ring_buffer_free(buf->buffer);
8001 buf->buffer = NULL;
8002 free_percpu(buf->data);
8003 buf->data = NULL;
8004 }
8005}
8006
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008007static void free_trace_buffers(struct trace_array *tr)
8008{
8009 if (!tr)
8010 return;
8011
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04008012 free_trace_buffer(&tr->trace_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008013
8014#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04008015 free_trace_buffer(&tr->max_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008016#endif
8017}
8018
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008019static void init_trace_flags_index(struct trace_array *tr)
8020{
8021 int i;
8022
8023 /* Used by the trace options files */
8024 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
8025 tr->trace_flags_index[i] = i;
8026}
8027
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008028static void __update_tracer_options(struct trace_array *tr)
8029{
8030 struct tracer *t;
8031
8032 for (t = trace_types; t; t = t->next)
8033 add_tracer_options(tr, t);
8034}
8035
8036static void update_tracer_options(struct trace_array *tr)
8037{
8038 mutex_lock(&trace_types_lock);
8039 __update_tracer_options(tr);
8040 mutex_unlock(&trace_types_lock);
8041}
8042
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05008043static int instance_mkdir(const char *name)
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008044{
Steven Rostedt277ba042012-08-03 16:10:49 -04008045 struct trace_array *tr;
8046 int ret;
Steven Rostedt277ba042012-08-03 16:10:49 -04008047
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04008048 mutex_lock(&event_mutex);
Steven Rostedt277ba042012-08-03 16:10:49 -04008049 mutex_lock(&trace_types_lock);
8050
8051 ret = -EEXIST;
8052 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8053 if (tr->name && strcmp(tr->name, name) == 0)
8054 goto out_unlock;
8055 }
8056
8057 ret = -ENOMEM;
8058 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
8059 if (!tr)
8060 goto out_unlock;
8061
8062 tr->name = kstrdup(name, GFP_KERNEL);
8063 if (!tr->name)
8064 goto out_free_tr;
8065
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008066 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
8067 goto out_free_tr;
8068
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04008069 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008070
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008071 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
8072
Steven Rostedt277ba042012-08-03 16:10:49 -04008073 raw_spin_lock_init(&tr->start_lock);
8074
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05008075 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
8076
Steven Rostedt277ba042012-08-03 16:10:49 -04008077 tr->current_trace = &nop_trace;
8078
8079 INIT_LIST_HEAD(&tr->systems);
8080 INIT_LIST_HEAD(&tr->events);
Tom Zanussi067fe032018-01-15 20:51:56 -06008081 INIT_LIST_HEAD(&tr->hist_vars);
Steven Rostedt277ba042012-08-03 16:10:49 -04008082
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008083 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
Steven Rostedt277ba042012-08-03 16:10:49 -04008084 goto out_free_tr;
8085
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008086 tr->dir = tracefs_create_dir(name, trace_instance_dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04008087 if (!tr->dir)
8088 goto out_free_tr;
8089
8090 ret = event_trace_add_tracer(tr->dir, tr);
Alexander Z Lam609e85a2013-07-10 17:34:34 -07008091 if (ret) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008092 tracefs_remove_recursive(tr->dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04008093 goto out_free_tr;
Alexander Z Lam609e85a2013-07-10 17:34:34 -07008094 }
Steven Rostedt277ba042012-08-03 16:10:49 -04008095
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04008096 ftrace_init_trace_array(tr);
8097
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008098 init_tracer_tracefs(tr, tr->dir);
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008099 init_trace_flags_index(tr);
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008100 __update_tracer_options(tr);
Steven Rostedt277ba042012-08-03 16:10:49 -04008101
8102 list_add(&tr->list, &ftrace_trace_arrays);
8103
8104 mutex_unlock(&trace_types_lock);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04008105 mutex_unlock(&event_mutex);
Steven Rostedt277ba042012-08-03 16:10:49 -04008106
8107 return 0;
8108
8109 out_free_tr:
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008110 free_trace_buffers(tr);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008111 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt277ba042012-08-03 16:10:49 -04008112 kfree(tr->name);
8113 kfree(tr);
8114
8115 out_unlock:
8116 mutex_unlock(&trace_types_lock);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04008117 mutex_unlock(&event_mutex);
Steven Rostedt277ba042012-08-03 16:10:49 -04008118
8119 return ret;
8120
8121}
8122
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05008123static int instance_rmdir(const char *name)
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008124{
8125 struct trace_array *tr;
8126 int found = 0;
8127 int ret;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008128 int i;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008129
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04008130 mutex_lock(&event_mutex);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008131 mutex_lock(&trace_types_lock);
8132
8133 ret = -ENODEV;
8134 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8135 if (tr->name && strcmp(tr->name, name) == 0) {
8136 found = 1;
8137 break;
8138 }
8139 }
8140 if (!found)
8141 goto out_unlock;
8142
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05008143 ret = -EBUSY;
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05008144 if (tr->ref || (tr->current_trace && tr->current_trace->ref))
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05008145 goto out_unlock;
8146
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008147 list_del(&tr->list);
8148
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04008149 /* Disable all the flags that were enabled coming in */
8150 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
8151 if ((1 << i) & ZEROED_TRACE_FLAGS)
8152 set_tracer_flag(tr, 1 << i, 0);
8153 }
8154
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05008155 tracing_set_nop(tr);
Naveen N. Raoa0e63692017-05-16 23:21:26 +05308156 clear_ftrace_function_probes(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008157 event_trace_del_tracer(tr);
Namhyung Kimd879d0b2017-04-17 11:44:27 +09008158 ftrace_clear_pids(tr);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05008159 ftrace_destroy_function_files(tr);
Jiaxing Wang681a4a22015-10-18 19:58:08 +08008160 tracefs_remove_recursive(tr->dir);
Steven Rostedt (Red Hat)a9fcaaa2014-06-06 23:17:28 -04008161 free_trace_buffers(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008162
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008163 for (i = 0; i < tr->nr_topts; i++) {
8164 kfree(tr->topts[i].topts);
8165 }
8166 kfree(tr->topts);
8167
Chunyu Hudb9108e02017-07-20 18:36:09 +08008168 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008169 kfree(tr->name);
8170 kfree(tr);
8171
8172 ret = 0;
8173
8174 out_unlock:
8175 mutex_unlock(&trace_types_lock);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04008176 mutex_unlock(&event_mutex);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008177
8178 return ret;
8179}
8180
Steven Rostedt277ba042012-08-03 16:10:49 -04008181static __init void create_trace_instances(struct dentry *d_tracer)
8182{
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05008183 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
8184 instance_mkdir,
8185 instance_rmdir);
Steven Rostedt277ba042012-08-03 16:10:49 -04008186 if (WARN_ON(!trace_instance_dir))
8187 return;
Steven Rostedt277ba042012-08-03 16:10:49 -04008188}
8189
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008190static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008191init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008192{
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04008193 struct trace_event_file *file;
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008194 int cpu;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008195
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05008196 trace_create_file("available_tracers", 0444, d_tracer,
8197 tr, &show_traces_fops);
8198
8199 trace_create_file("current_tracer", 0644, d_tracer,
8200 tr, &set_tracer_fops);
8201
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008202 trace_create_file("tracing_cpumask", 0644, d_tracer,
8203 tr, &tracing_cpumask_fops);
8204
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008205 trace_create_file("trace_options", 0644, d_tracer,
8206 tr, &tracing_iter_fops);
8207
8208 trace_create_file("trace", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02008209 tr, &tracing_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008210
8211 trace_create_file("trace_pipe", 0444, d_tracer,
Oleg Nesterov15544202013-07-23 17:25:57 +02008212 tr, &tracing_pipe_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008213
8214 trace_create_file("buffer_size_kb", 0644, d_tracer,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02008215 tr, &tracing_entries_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008216
8217 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
8218 tr, &tracing_total_entries_fops);
8219
Wang YanQing238ae932013-05-26 16:52:01 +08008220 trace_create_file("free_buffer", 0200, d_tracer,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008221 tr, &tracing_free_buffer_fops);
8222
8223 trace_create_file("trace_marker", 0220, d_tracer,
8224 tr, &tracing_mark_fops);
8225
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04008226 file = __find_event_file(tr, "ftrace", "print");
8227 if (file && file->dir)
8228 trace_create_file("trigger", 0644, file->dir, file,
8229 &event_trigger_fops);
8230 tr->trace_marker_file = file;
8231
Steven Rostedtfa32e852016-07-06 15:25:08 -04008232 trace_create_file("trace_marker_raw", 0220, d_tracer,
8233 tr, &tracing_mark_raw_fops);
8234
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008235 trace_create_file("trace_clock", 0644, d_tracer, tr,
8236 &trace_clock_fops);
8237
8238 trace_create_file("tracing_on", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02008239 tr, &rb_simple_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008240
Tom Zanussi2c1ea602018-01-15 20:51:41 -06008241 trace_create_file("timestamp_mode", 0444, d_tracer, tr,
8242 &trace_time_stamp_mode_fops);
8243
Steven Rostedt (VMware)a7b1d742018-11-29 22:36:47 -05008244 tr->buffer_percent = 50;
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05008245
8246 trace_create_file("buffer_percent", 0444, d_tracer,
8247 tr, &buffer_percent_fops);
8248
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04008249 create_trace_options_dir(tr);
8250
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04008251#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05008252 trace_create_file("tracing_max_latency", 0644, d_tracer,
8253 &tr->max_latency, &tracing_max_lat_fops);
8254#endif
8255
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05008256 if (ftrace_create_function_files(tr, d_tracer))
8257 WARN(1, "Could not allocate function filter files");
8258
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008259#ifdef CONFIG_TRACER_SNAPSHOT
8260 trace_create_file("snapshot", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02008261 tr, &snapshot_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008262#endif
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008263
8264 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008265 tracing_init_tracefs_percpu(tr, cpu);
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008266
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04008267 ftrace_init_tracefs(tr, d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008268}
8269
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13008270static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008271{
8272 struct vfsmount *mnt;
8273 struct file_system_type *type;
8274
8275 /*
8276 * To maintain backward compatibility for tools that mount
8277 * debugfs to get to the tracing facility, tracefs is automatically
8278 * mounted to the debugfs/tracing directory.
8279 */
8280 type = get_fs_type("tracefs");
8281 if (!type)
8282 return NULL;
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13008283 mnt = vfs_submount(mntpt, type, "tracefs", NULL);
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008284 put_filesystem(type);
8285 if (IS_ERR(mnt))
8286 return NULL;
8287 mntget(mnt);
8288
8289 return mnt;
8290}
8291
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008292/**
8293 * tracing_init_dentry - initialize top level trace array
8294 *
8295 * This is called when creating files or directories in the tracing
8296 * directory. It is called via fs_initcall() by any of the boot up code
8297 * and expects to return the dentry of the top level tracing directory.
8298 */
8299struct dentry *tracing_init_dentry(void)
8300{
8301 struct trace_array *tr = &global_trace;
8302
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008303 /* The top level trace array uses NULL as parent */
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008304 if (tr->dir)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008305 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008306
Jiaxing Wang8b129192015-11-06 16:04:16 +08008307 if (WARN_ON(!tracefs_initialized()) ||
8308 (IS_ENABLED(CONFIG_DEBUG_FS) &&
8309 WARN_ON(!debugfs_initialized())))
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008310 return ERR_PTR(-ENODEV);
8311
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008312 /*
8313 * As there may still be users that expect the tracing
8314 * files to exist in debugfs/tracing, we must automount
8315 * the tracefs file system there, so older tools still
8316 * work with the newer kerenl.
8317 */
8318 tr->dir = debugfs_create_automount("tracing", NULL,
8319 trace_automount, NULL);
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008320 if (!tr->dir) {
8321 pr_warn_once("Could not create debugfs directory 'tracing'\n");
8322 return ERR_PTR(-ENOMEM);
8323 }
8324
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008325 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008326}
8327
Jeremy Linton00f4b652017-05-31 16:56:43 -05008328extern struct trace_eval_map *__start_ftrace_eval_maps[];
8329extern struct trace_eval_map *__stop_ftrace_eval_maps[];
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008330
Jeremy Linton5f60b352017-05-31 16:56:47 -05008331static void __init trace_eval_init(void)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008332{
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008333 int len;
8334
Jeremy Linton02fd7f62017-05-31 16:56:42 -05008335 len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008336 trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008337}
8338
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008339#ifdef CONFIG_MODULES
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008340static void trace_module_add_evals(struct module *mod)
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008341{
Jeremy Linton99be6472017-05-31 16:56:44 -05008342 if (!mod->num_trace_evals)
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008343 return;
8344
8345 /*
8346 * Modules with bad taint do not have events created, do
8347 * not bother with enums either.
8348 */
8349 if (trace_module_has_bad_taint(mod))
8350 return;
8351
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008352 trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008353}
8354
Jeremy Linton681bec02017-05-31 16:56:53 -05008355#ifdef CONFIG_TRACE_EVAL_MAP_FILE
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008356static void trace_module_remove_evals(struct module *mod)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008357{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05008358 union trace_eval_map_item *map;
8359 union trace_eval_map_item **last = &trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008360
Jeremy Linton99be6472017-05-31 16:56:44 -05008361 if (!mod->num_trace_evals)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008362 return;
8363
Jeremy Linton1793ed92017-05-31 16:56:46 -05008364 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008365
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05008366 map = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008367
8368 while (map) {
8369 if (map->head.mod == mod)
8370 break;
Jeremy Linton5f60b352017-05-31 16:56:47 -05008371 map = trace_eval_jmp_to_tail(map);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008372 last = &map->tail.next;
8373 map = map->tail.next;
8374 }
8375 if (!map)
8376 goto out;
8377
Jeremy Linton5f60b352017-05-31 16:56:47 -05008378 *last = trace_eval_jmp_to_tail(map)->tail.next;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008379 kfree(map);
8380 out:
Jeremy Linton1793ed92017-05-31 16:56:46 -05008381 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008382}
8383#else
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008384static inline void trace_module_remove_evals(struct module *mod) { }
Jeremy Linton681bec02017-05-31 16:56:53 -05008385#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008386
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008387static int trace_module_notify(struct notifier_block *self,
8388 unsigned long val, void *data)
8389{
8390 struct module *mod = data;
8391
8392 switch (val) {
8393 case MODULE_STATE_COMING:
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008394 trace_module_add_evals(mod);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008395 break;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008396 case MODULE_STATE_GOING:
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008397 trace_module_remove_evals(mod);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008398 break;
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008399 }
8400
8401 return 0;
8402}
8403
8404static struct notifier_block trace_module_nb = {
8405 .notifier_call = trace_module_notify,
8406 .priority = 0,
8407};
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008408#endif /* CONFIG_MODULES */
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008409
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008410static __init int tracer_init_tracefs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008411{
8412 struct dentry *d_tracer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008413
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08008414 trace_access_lock_init();
8415
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008416 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05008417 if (IS_ERR(d_tracer))
Namhyung Kimed6f1c92013-04-10 09:18:12 +09008418 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008419
Steven Rostedt (VMware)58b92542018-05-08 15:09:27 -04008420 event_trace_init();
8421
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008422 init_tracer_tracefs(&global_trace, d_tracer);
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04008423 ftrace_init_tracefs_toplevel(&global_trace, d_tracer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008424
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008425 trace_create_file("tracing_thresh", 0644, d_tracer,
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04008426 &global_trace, &tracing_thresh_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008427
Li Zefan339ae5d2009-04-17 10:34:30 +08008428 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008429 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02008430
Avadh Patel69abe6a2009-04-10 16:04:48 -04008431 trace_create_file("saved_cmdlines", 0444, d_tracer,
8432 NULL, &tracing_saved_cmdlines_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03008433
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09008434 trace_create_file("saved_cmdlines_size", 0644, d_tracer,
8435 NULL, &tracing_saved_cmdlines_size_fops);
8436
Michael Sartain99c621d2017-07-05 22:07:15 -06008437 trace_create_file("saved_tgids", 0444, d_tracer,
8438 NULL, &tracing_saved_tgids_fops);
8439
Jeremy Linton5f60b352017-05-31 16:56:47 -05008440 trace_eval_init();
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008441
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008442 trace_create_eval_file(d_tracer);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008443
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008444#ifdef CONFIG_MODULES
8445 register_module_notifier(&trace_module_nb);
8446#endif
8447
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008448#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008449 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
8450 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008451#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01008452
Steven Rostedt277ba042012-08-03 16:10:49 -04008453 create_trace_instances(d_tracer);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09008454
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008455 update_tracer_options(&global_trace);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05008456
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01008457 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008458}
8459
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008460static int trace_panic_handler(struct notifier_block *this,
8461 unsigned long event, void *unused)
8462{
Steven Rostedt944ac422008-10-23 19:26:08 -04008463 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008464 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008465 return NOTIFY_OK;
8466}
8467
8468static struct notifier_block trace_panic_notifier = {
8469 .notifier_call = trace_panic_handler,
8470 .next = NULL,
8471 .priority = 150 /* priority: INT_MAX >= x >= 0 */
8472};
8473
8474static int trace_die_handler(struct notifier_block *self,
8475 unsigned long val,
8476 void *data)
8477{
8478 switch (val) {
8479 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04008480 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008481 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008482 break;
8483 default:
8484 break;
8485 }
8486 return NOTIFY_OK;
8487}
8488
8489static struct notifier_block trace_die_notifier = {
8490 .notifier_call = trace_die_handler,
8491 .priority = 200
8492};
8493
8494/*
8495 * printk is set to max of 1024, we really don't need it that big.
8496 * Nothing should be printing 1000 characters anyway.
8497 */
8498#define TRACE_MAX_PRINT 1000
8499
8500/*
8501 * Define here KERN_TRACE so that we have one place to modify
8502 * it if we decide to change what log level the ftrace dump
8503 * should be at.
8504 */
Steven Rostedt428aee12009-01-14 12:24:42 -05008505#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008506
Jason Wessel955b61e2010-08-05 09:22:23 -05008507void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008508trace_printk_seq(struct trace_seq *s)
8509{
8510 /* Probably should print a warning here. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04008511 if (s->seq.len >= TRACE_MAX_PRINT)
8512 s->seq.len = TRACE_MAX_PRINT;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008513
Steven Rostedt (Red Hat)820b75f2014-11-19 10:56:41 -05008514 /*
8515 * More paranoid code. Although the buffer size is set to
8516 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
8517 * an extra layer of protection.
8518 */
8519 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
8520 s->seq.len = s->seq.size - 1;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008521
8522 /* should be zero ended, but we are paranoid. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04008523 s->buffer[s->seq.len] = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008524
8525 printk(KERN_TRACE "%s", s->buffer);
8526
Steven Rostedtf9520752009-03-02 14:04:40 -05008527 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008528}
8529
Jason Wessel955b61e2010-08-05 09:22:23 -05008530void trace_init_global_iter(struct trace_iterator *iter)
8531{
8532 iter->tr = &global_trace;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008533 iter->trace = iter->tr->current_trace;
Steven Rostedtae3b5092013-01-23 15:22:59 -05008534 iter->cpu_file = RING_BUFFER_ALL_CPUS;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05008535 iter->trace_buffer = &global_trace.trace_buffer;
Cody P Schaferb2f974d2013-10-23 11:49:57 -07008536
8537 if (iter->trace && iter->trace->open)
8538 iter->trace->open(iter);
8539
8540 /* Annotate start of buffers if we had overruns */
8541 if (ring_buffer_overruns(iter->trace_buffer->buffer))
8542 iter->iter_flags |= TRACE_FILE_ANNOTATE;
8543
8544 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
8545 if (trace_clocks[iter->tr->clock_id].in_ns)
8546 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
Jason Wessel955b61e2010-08-05 09:22:23 -05008547}
8548
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008549void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008550{
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008551 /* use static because iter can be a bit big for the stack */
8552 static struct trace_iterator iter;
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008553 static atomic_t dump_running;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008554 struct trace_array *tr = &global_trace;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01008555 unsigned int old_userobj;
Steven Rostedtd7690412008-10-01 00:29:53 -04008556 unsigned long flags;
8557 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008558
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008559 /* Only allow one dump user at a time. */
8560 if (atomic_inc_return(&dump_running) != 1) {
8561 atomic_dec(&dump_running);
8562 return;
Steven Rostedte0a413f2011-09-29 21:26:16 -04008563 }
8564
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008565 /*
8566 * Always turn off tracing when we dump.
8567 * We don't need to show trace output of what happens
8568 * between multiple crashes.
8569 *
8570 * If the user does a sysrq-z, then they can re-enable
8571 * tracing with echo 1 > tracing_on.
8572 */
8573 tracing_off();
8574
8575 local_irq_save(flags);
Petr Mladek03fc7f92018-06-27 16:20:28 +02008576 printk_nmi_direct_enter();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008577
Jovi Zhang38dbe0b2013-01-25 18:03:07 +08008578 /* Simulate the iterator */
Jason Wessel955b61e2010-08-05 09:22:23 -05008579 trace_init_global_iter(&iter);
8580
Steven Rostedtd7690412008-10-01 00:29:53 -04008581 for_each_tracing_cpu(cpu) {
Umesh Tiwari5e2d5ef2015-06-22 16:55:06 +05308582 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04008583 }
8584
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008585 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01008586
Török Edwinb54d3de2008-11-22 13:28:48 +02008587 /* don't look at user memory in panic mode */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008588 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
Török Edwinb54d3de2008-11-22 13:28:48 +02008589
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008590 switch (oops_dump_mode) {
8591 case DUMP_ALL:
Steven Rostedtae3b5092013-01-23 15:22:59 -05008592 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008593 break;
8594 case DUMP_ORIG:
8595 iter.cpu_file = raw_smp_processor_id();
8596 break;
8597 case DUMP_NONE:
8598 goto out_enable;
8599 default:
8600 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
Steven Rostedtae3b5092013-01-23 15:22:59 -05008601 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008602 }
8603
8604 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008605
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008606 /* Did function tracer already get disabled? */
8607 if (ftrace_is_dead()) {
8608 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
8609 printk("# MAY BE MISSING FUNCTION EVENTS\n");
8610 }
8611
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008612 /*
8613 * We need to stop all tracing on all CPUS to read the
8614 * the next buffer. This is a bit expensive, but is
8615 * not done often. We fill all what we can read,
8616 * and then release the locks again.
8617 */
8618
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008619 while (!trace_empty(&iter)) {
8620
8621 if (!cnt)
8622 printk(KERN_TRACE "---------------------------------\n");
8623
8624 cnt++;
8625
8626 /* reset all but tr, trace, and overruns */
8627 memset(&iter.seq, 0,
8628 sizeof(struct trace_iterator) -
8629 offsetof(struct trace_iterator, seq));
8630 iter.iter_flags |= TRACE_FILE_LAT_FMT;
8631 iter.pos = -1;
8632
Jason Wessel955b61e2010-08-05 09:22:23 -05008633 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08008634 int ret;
8635
8636 ret = print_trace_line(&iter);
8637 if (ret != TRACE_TYPE_NO_CONSUME)
8638 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008639 }
Steven Rostedtb892e5c2012-03-01 22:06:48 -05008640 touch_nmi_watchdog();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008641
8642 trace_printk_seq(&iter.seq);
8643 }
8644
8645 if (!cnt)
8646 printk(KERN_TRACE " (ftrace buffer empty)\n");
8647 else
8648 printk(KERN_TRACE "---------------------------------\n");
8649
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008650 out_enable:
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008651 tr->trace_flags |= old_userobj;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01008652
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04008653 for_each_tracing_cpu(cpu) {
8654 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01008655 }
Petr Mladek03fc7f92018-06-27 16:20:28 +02008656 atomic_dec(&dump_running);
8657 printk_nmi_direct_exit();
Steven Rostedtcd891ae2009-04-28 11:39:34 -04008658 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008659}
Paul E. McKenneya8eecf22011-10-02 11:01:15 -07008660EXPORT_SYMBOL_GPL(ftrace_dump);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01008661
Tom Zanussi7e465ba2017-09-22 14:58:20 -05008662int trace_run_command(const char *buf, int (*createfn)(int, char **))
8663{
8664 char **argv;
8665 int argc, ret;
8666
8667 argc = 0;
8668 ret = 0;
8669 argv = argv_split(GFP_KERNEL, buf, &argc);
8670 if (!argv)
8671 return -ENOMEM;
8672
8673 if (argc)
8674 ret = createfn(argc, argv);
8675
8676 argv_free(argv);
8677
8678 return ret;
8679}
8680
8681#define WRITE_BUFSIZE 4096
8682
8683ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
8684 size_t count, loff_t *ppos,
8685 int (*createfn)(int, char **))
8686{
8687 char *kbuf, *buf, *tmp;
8688 int ret = 0;
8689 size_t done = 0;
8690 size_t size;
8691
8692 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
8693 if (!kbuf)
8694 return -ENOMEM;
8695
8696 while (done < count) {
8697 size = count - done;
8698
8699 if (size >= WRITE_BUFSIZE)
8700 size = WRITE_BUFSIZE - 1;
8701
8702 if (copy_from_user(kbuf, buffer + done, size)) {
8703 ret = -EFAULT;
8704 goto out;
8705 }
8706 kbuf[size] = '\0';
8707 buf = kbuf;
8708 do {
8709 tmp = strchr(buf, '\n');
8710 if (tmp) {
8711 *tmp = '\0';
8712 size = tmp - buf + 1;
8713 } else {
8714 size = strlen(buf);
8715 if (done + size < count) {
8716 if (buf != kbuf)
8717 break;
8718 /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
8719 pr_warn("Line length is too long: Should be less than %d\n",
8720 WRITE_BUFSIZE - 2);
8721 ret = -EINVAL;
8722 goto out;
8723 }
8724 }
8725 done += size;
8726
8727 /* Remove comments */
8728 tmp = strchr(buf, '#');
8729
8730 if (tmp)
8731 *tmp = '\0';
8732
8733 ret = trace_run_command(buf, createfn);
8734 if (ret)
8735 goto out;
8736 buf += size;
8737
8738 } while (done < count);
8739 }
8740 ret = done;
8741
8742out:
8743 kfree(kbuf);
8744
8745 return ret;
8746}
8747
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008748__init static int tracer_alloc_buffers(void)
8749{
Steven Rostedt73c51622009-03-11 13:42:01 -04008750 int ring_buf_size;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308751 int ret = -ENOMEM;
8752
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04008753 /*
8754 * Make sure we don't accidently add more trace options
8755 * than we have bits for.
8756 */
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008757 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04008758
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308759 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
8760 goto out;
8761
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008762 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308763 goto out_free_buffer_mask;
8764
Steven Rostedt07d777f2011-09-22 14:01:55 -04008765 /* Only allocate trace_printk buffers if a trace_printk exists */
8766 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
Steven Rostedt81698832012-10-11 10:15:05 -04008767 /* Must be called before global_trace.buffer is allocated */
Steven Rostedt07d777f2011-09-22 14:01:55 -04008768 trace_printk_init_buffers();
8769
Steven Rostedt73c51622009-03-11 13:42:01 -04008770 /* To save memory, keep the ring buffer size to its minimum */
8771 if (ring_buffer_expanded)
8772 ring_buf_size = trace_buf_size;
8773 else
8774 ring_buf_size = 1;
8775
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308776 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008777 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008778
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008779 raw_spin_lock_init(&global_trace.start_lock);
8780
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01008781 /*
8782 * The prepare callbacks allocates some memory for the ring buffer. We
8783 * don't free the buffer if the if the CPU goes down. If we were to free
8784 * the buffer, then the user would lose any trace that was in the
8785 * buffer. The memory will be removed once the "instance" is removed.
8786 */
8787 ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
8788 "trace/RB:preapre", trace_rb_cpu_prepare,
8789 NULL);
8790 if (ret < 0)
8791 goto out_free_cpumask;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04008792 /* Used for event triggers */
Dan Carpenter147d88e02017-08-01 14:02:01 +03008793 ret = -ENOMEM;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04008794 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
8795 if (!temp_buffer)
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01008796 goto out_rm_hp_state;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04008797
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09008798 if (trace_create_savedcmd() < 0)
8799 goto out_free_temp_buffer;
8800
Steven Rostedtab464282008-05-12 21:21:00 +02008801 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008802 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04008803 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
8804 WARN_ON(1);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09008805 goto out_free_savedcmd;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04008806 }
Steven Rostedta7603ff2012-08-06 16:24:11 -04008807
Steven Rostedt499e5472012-02-22 15:50:28 -05008808 if (global_trace.buffer_disabled)
8809 tracing_off();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04008810
Steven Rostedte1e232c2014-02-10 23:38:46 -05008811 if (trace_boot_clock) {
8812 ret = tracing_set_clock(&global_trace, trace_boot_clock);
8813 if (ret < 0)
Joe Perchesa395d6a2016-03-22 14:28:09 -07008814 pr_warn("Trace clock %s not defined, going back to default\n",
8815 trace_boot_clock);
Steven Rostedte1e232c2014-02-10 23:38:46 -05008816 }
8817
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04008818 /*
8819 * register_tracer() might reference current_trace, so it
8820 * needs to be set before we register anything. This is
8821 * just a bootstrap of current_trace anyway.
8822 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008823 global_trace.current_trace = &nop_trace;
8824
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05008825 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
8826
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05008827 ftrace_init_global_array_ops(&global_trace);
8828
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008829 init_trace_flags_index(&global_trace);
8830
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04008831 register_tracer(&nop_trace);
8832
Steven Rostedt (VMware)dbeafd02017-03-03 13:48:42 -05008833 /* Function tracing may start here (via kernel command line) */
8834 init_function_trace();
8835
Steven Rostedt60a11772008-05-12 21:20:44 +02008836 /* All seems OK, enable tracing */
8837 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04008838
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008839 atomic_notifier_chain_register(&panic_notifier_list,
8840 &trace_panic_notifier);
8841
8842 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01008843
Steven Rostedtae63b31e2012-05-03 23:09:03 -04008844 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
8845
8846 INIT_LIST_HEAD(&global_trace.systems);
8847 INIT_LIST_HEAD(&global_trace.events);
Tom Zanussi067fe032018-01-15 20:51:56 -06008848 INIT_LIST_HEAD(&global_trace.hist_vars);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04008849 list_add(&global_trace.list, &ftrace_trace_arrays);
8850
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08008851 apply_trace_boot_options();
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04008852
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04008853 register_snapshot_cmd();
8854
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01008855 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008856
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09008857out_free_savedcmd:
8858 free_saved_cmdlines_buffer(savedcmd);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04008859out_free_temp_buffer:
8860 ring_buffer_free(temp_buffer);
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01008861out_rm_hp_state:
8862 cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308863out_free_cpumask:
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008864 free_cpumask_var(global_trace.tracing_cpumask);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10308865out_free_buffer_mask:
8866 free_cpumask_var(tracing_buffer_mask);
8867out:
8868 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008869}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05008870
Steven Rostedt (VMware)e725c732017-03-03 13:37:33 -05008871void __init early_trace_init(void)
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05008872{
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05008873 if (tracepoint_printk) {
8874 tracepoint_print_iter =
8875 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
8876 if (WARN_ON(!tracepoint_print_iter))
8877 tracepoint_printk = 0;
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05008878 else
8879 static_key_enable(&tracepoint_printk_key.key);
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05008880 }
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05008881 tracer_alloc_buffers();
Steven Rostedt (VMware)e725c732017-03-03 13:37:33 -05008882}
8883
8884void __init trace_init(void)
8885{
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008886 trace_event_init();
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05008887}
8888
Steven Rostedtb2821ae2009-02-02 21:38:32 -05008889__init static int clear_boot_tracer(void)
8890{
8891 /*
8892 * The default tracer at boot buffer is an init section.
8893 * This function is called in lateinit. If we did not
8894 * find the boot tracer, then clear it out, to prevent
8895 * later registration from accessing the buffer that is
8896 * about to be freed.
8897 */
8898 if (!default_bootup_tracer)
8899 return 0;
8900
8901 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
8902 default_bootup_tracer);
8903 default_bootup_tracer = NULL;
8904
8905 return 0;
8906}
8907
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008908fs_initcall(tracer_init_tracefs);
Steven Rostedt (VMware)4bb0f0e2017-08-01 12:01:52 -04008909late_initcall_sync(clear_boot_tracer);
Chris Wilson3fd49c92018-03-30 16:01:31 +01008910
8911#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
8912__init static int tracing_set_default_clock(void)
8913{
8914 /* sched_clock_stable() is determined in late_initcall */
Chris Wilson5125eee2018-04-04 22:24:50 +01008915 if (!trace_boot_clock && !sched_clock_stable()) {
Chris Wilson3fd49c92018-03-30 16:01:31 +01008916 printk(KERN_WARNING
8917 "Unstable clock detected, switching default tracing clock to \"global\"\n"
8918 "If you want to keep using the local clock, then add:\n"
8919 " \"trace_clock=local\"\n"
8920 "on the kernel command line\n");
8921 tracing_set_clock(&global_trace, "global");
8922 }
8923
8924 return 0;
8925}
8926late_initcall_sync(tracing_set_default_clock);
8927#endif