blob: 0ab0cf30559d9d2d9cd61b3411f831d5c31496b8 [file] [log] [blame]
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001/*
2 * ring buffer based function tracer
3 *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010012 * Copyright (C) 2004 Nadia Yvette Chambers
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020013 */
Steven Rostedt2cadf912008-12-01 22:20:19 -050014#include <linux/ring_buffer.h>
Sam Ravnborg273b2812009-10-18 00:52:28 +020015#include <generated/utsrelease.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050016#include <linux/stacktrace.h>
17#include <linux/writeback.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020018#include <linux/kallsyms.h>
19#include <linux/seq_file.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040020#include <linux/notifier.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050021#include <linux/irqflags.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020022#include <linux/debugfs.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050023#include <linux/tracefs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020024#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020025#include <linux/hardirq.h>
26#include <linux/linkage.h>
27#include <linux/uaccess.h>
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -040028#include <linux/vmalloc.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020029#include <linux/ftrace.h>
30#include <linux/module.h>
31#include <linux/percpu.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050032#include <linux/splice.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040033#include <linux/kdebug.h>
Frederic Weisbecker5f0c6c02009-03-27 14:22:10 +010034#include <linux/string.h>
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -050035#include <linux/mount.h>
Lai Jiangshan7e53bd42010-01-06 20:08:50 +080036#include <linux/rwsem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020038#include <linux/ctype.h>
39#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020040#include <linux/poll.h>
Steven Rostedtb892e5c2012-03-01 22:06:48 -050041#include <linux/nmi.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020042#include <linux/fs.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060043#include <linux/sched/rt.h>
Shashank Mittal43beb422016-05-20 13:06:09 -070044#include <linux/coresight-stm.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020045
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020046#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050047#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020048
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010049/*
Steven Rostedt73c51622009-03-11 13:42:01 -040050 * On boot up, the ring buffer is set to the minimum size, so that
51 * we do not waste memory on systems that are not using tracing.
52 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -050053bool ring_buffer_expanded;
Steven Rostedt73c51622009-03-11 13:42:01 -040054
55/*
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010056 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010057 * A selftest will lurk into the ring-buffer to count the
58 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010059 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010060 * at the same time, giving false positive or negative results.
61 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010062static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010063
Steven Rostedtb2821ae2009-02-02 21:38:32 -050064/*
65 * If a tracer is running, we do not want to run SELFTEST.
66 */
Li Zefan020e5f82009-07-01 10:47:05 +080067bool __read_mostly tracing_selftest_disabled;
Steven Rostedtb2821ae2009-02-02 21:38:32 -050068
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -050069/* Pipe tracepoints to printk */
70struct trace_iterator *tracepoint_print_iter;
71int tracepoint_printk;
72
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010073/* For tracers that don't implement custom flags */
74static struct tracer_opt dummy_tracer_opt[] = {
75 { }
76};
77
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -050078static int
79dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010080{
81 return 0;
82}
Steven Rostedt0f048702008-11-05 16:05:44 -050083
84/*
Steven Rostedt7ffbd482012-10-11 12:14:25 -040085 * To prevent the comm cache from being overwritten when no
86 * tracing is active, only save the comm when a trace event
87 * occurred.
88 */
89static DEFINE_PER_CPU(bool, trace_cmdline_save);
90
91/*
Steven Rostedt0f048702008-11-05 16:05:44 -050092 * Kill all tracing for good (never come back).
93 * It is initialized to 1 but will turn to zero if the initialization
94 * of the tracer is successful. But that is the only place that sets
95 * this back to zero.
96 */
Hannes Eder4fd27352009-02-10 19:44:12 +010097static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -050098
Jason Wessel955b61e2010-08-05 09:22:23 -050099cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +0200100
Steven Rostedt944ac422008-10-23 19:26:08 -0400101/*
102 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
103 *
104 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
105 * is set, then ftrace_dump is called. This will output the contents
106 * of the ftrace buffers to the console. This is very useful for
107 * capturing traces that lead to crashes and outputing it to a
108 * serial console.
109 *
110 * It is default off, but you can enable it with either specifying
111 * "ftrace_dump_on_oops" in the kernel command line, or setting
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200112 * /proc/sys/kernel/ftrace_dump_on_oops
113 * Set 1 if you want to dump buffers of all CPUs
114 * Set 2 if you want to dump the buffer of the CPU that triggered oops
Steven Rostedt944ac422008-10-23 19:26:08 -0400115 */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200116
117enum ftrace_dump_mode ftrace_dump_on_oops;
Steven Rostedt944ac422008-10-23 19:26:08 -0400118
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400119/* When set, tracing will stop when a WARN*() is hit */
120int __disable_trace_on_warning;
121
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400122#ifdef CONFIG_TRACE_ENUM_MAP_FILE
123/* Map of enums to their values, for "enum_map" file */
124struct trace_enum_map_head {
125 struct module *mod;
126 unsigned long length;
127};
128
129union trace_enum_map_item;
130
131struct trace_enum_map_tail {
132 /*
133 * "end" is first and points to NULL as it must be different
134 * than "mod" or "enum_string"
135 */
136 union trace_enum_map_item *next;
137 const char *end; /* points to NULL */
138};
139
140static DEFINE_MUTEX(trace_enum_mutex);
141
142/*
143 * The trace_enum_maps are saved in an array with two extra elements,
144 * one at the beginning, and one at the end. The beginning item contains
145 * the count of the saved maps (head.length), and the module they
146 * belong to if not built in (head.mod). The ending item contains a
147 * pointer to the next array of saved enum_map items.
148 */
149union trace_enum_map_item {
150 struct trace_enum_map map;
151 struct trace_enum_map_head head;
152 struct trace_enum_map_tail tail;
153};
154
155static union trace_enum_map_item *trace_enum_maps;
156#endif /* CONFIG_TRACE_ENUM_MAP_FILE */
157
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -0500158static int tracing_set_tracer(struct trace_array *tr, const char *buf);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500159
Li Zefanee6c2c12009-09-18 14:06:47 +0800160#define MAX_TRACER_SIZE 100
161static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500162static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100163
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500164static bool allocate_snapshot;
165
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200166static int __init set_cmdline_ftrace(char *str)
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100167{
Chen Gang67012ab2013-04-08 12:06:44 +0800168 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500169 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400170 /* We are using ftrace early, expand it */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500171 ring_buffer_expanded = true;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100172 return 1;
173}
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200174__setup("ftrace=", set_cmdline_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100175
Steven Rostedt944ac422008-10-23 19:26:08 -0400176static int __init set_ftrace_dump_on_oops(char *str)
177{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200178 if (*str++ != '=' || !*str) {
179 ftrace_dump_on_oops = DUMP_ALL;
180 return 1;
181 }
182
183 if (!strcmp("orig_cpu", str)) {
184 ftrace_dump_on_oops = DUMP_ORIG;
185 return 1;
186 }
187
188 return 0;
Steven Rostedt944ac422008-10-23 19:26:08 -0400189}
190__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200191
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400192static int __init stop_trace_on_warning(char *str)
193{
Luis Claudio R. Goncalves933ff9f2014-11-12 21:14:00 -0200194 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
195 __disable_trace_on_warning = 1;
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400196 return 1;
197}
Luis Claudio R. Goncalves933ff9f2014-11-12 21:14:00 -0200198__setup("traceoff_on_warning", stop_trace_on_warning);
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400199
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400200static int __init boot_alloc_snapshot(char *str)
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500201{
202 allocate_snapshot = true;
203 /* We also need the main ring buffer expanded */
204 ring_buffer_expanded = true;
205 return 1;
206}
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400207__setup("alloc_snapshot", boot_alloc_snapshot);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500208
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400209
210static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400211
212static int __init set_trace_boot_options(char *str)
213{
Chen Gang67012ab2013-04-08 12:06:44 +0800214 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400215 return 0;
216}
217__setup("trace_options=", set_trace_boot_options);
218
Steven Rostedte1e232c2014-02-10 23:38:46 -0500219static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
220static char *trace_boot_clock __initdata;
221
222static int __init set_trace_boot_clock(char *str)
223{
224 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
225 trace_boot_clock = trace_boot_clock_buf;
226 return 0;
227}
228__setup("trace_clock=", set_trace_boot_clock);
229
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500230static int __init set_tracepoint_printk(char *str)
231{
232 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
233 tracepoint_printk = 1;
234 return 1;
235}
236__setup("tp_printk", set_tracepoint_printk);
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400237
Lai Jiangshancf8e3472009-03-30 13:48:00 +0800238unsigned long long ns2usecs(cycle_t nsec)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200239{
240 nsec += 500;
241 do_div(nsec, 1000);
242 return nsec;
243}
244
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400245/* trace_flags holds trace_options default values */
246#define TRACE_DEFAULT_FLAGS \
247 (FUNCTION_DEFAULT_FLAGS | \
248 TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \
249 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \
250 TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \
251 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS)
252
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -0400253/* trace_options that are only supported by global_trace */
254#define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \
255 TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
256
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -0400257/* trace_flags that are default zero for instances */
258#define ZEROED_TRACE_FLAGS \
259 TRACE_ITER_EVENT_FORK
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -0400260
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200261/*
262 * The global_trace is the descriptor that holds the tracing
263 * buffers for the live tracing. For each CPU, it contains
264 * a link list of pages that will store trace entries. The
265 * page descriptor of the pages in the memory is used to hold
266 * the link list by linking the lru item in the page descriptor
267 * to each of the pages in the buffer per CPU.
268 *
269 * For each active CPU there is a data field that holds the
270 * pages for the buffer for that CPU. Each CPU has the same number
271 * of pages allocated for its buffer.
272 */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400273static struct trace_array global_trace = {
274 .trace_flags = TRACE_DEFAULT_FLAGS,
275};
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200276
Steven Rostedtae63b312012-05-03 23:09:03 -0400277LIST_HEAD(ftrace_trace_arrays);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200278
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -0400279int trace_array_get(struct trace_array *this_tr)
280{
281 struct trace_array *tr;
282 int ret = -ENODEV;
283
284 mutex_lock(&trace_types_lock);
285 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
286 if (tr == this_tr) {
287 tr->ref++;
288 ret = 0;
289 break;
290 }
291 }
292 mutex_unlock(&trace_types_lock);
293
294 return ret;
295}
296
297static void __trace_array_put(struct trace_array *this_tr)
298{
299 WARN_ON(!this_tr->ref);
300 this_tr->ref--;
301}
302
303void trace_array_put(struct trace_array *this_tr)
304{
305 mutex_lock(&trace_types_lock);
306 __trace_array_put(this_tr);
307 mutex_unlock(&trace_types_lock);
308}
309
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400310int call_filter_check_discard(struct trace_event_call *call, void *rec,
Tom Zanussif306cc82013-10-24 08:34:17 -0500311 struct ring_buffer *buffer,
312 struct ring_buffer_event *event)
313{
314 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
315 !filter_match_preds(call->filter, rec)) {
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -0400316 __trace_event_discard_commit(buffer, event);
Tom Zanussif306cc82013-10-24 08:34:17 -0500317 return 1;
318 }
319
320 return 0;
321}
Tom Zanussieb02ce02009-04-08 03:15:54 -0500322
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400323void trace_free_pid_list(struct trace_pid_list *pid_list)
324{
325 vfree(pid_list->pids);
326 kfree(pid_list);
327}
328
Steven Rostedtd8275c42016-04-14 12:15:22 -0400329/**
330 * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
331 * @filtered_pids: The list of pids to check
332 * @search_pid: The PID to find in @filtered_pids
333 *
334 * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis.
335 */
336bool
337trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
338{
339 /*
340 * If pid_max changed after filtered_pids was created, we
341 * by default ignore all pids greater than the previous pid_max.
342 */
343 if (search_pid >= filtered_pids->pid_max)
344 return false;
345
346 return test_bit(search_pid, filtered_pids->pids);
347}
348
349/**
350 * trace_ignore_this_task - should a task be ignored for tracing
351 * @filtered_pids: The list of pids to check
352 * @task: The task that should be ignored if not filtered
353 *
354 * Checks if @task should be traced or not from @filtered_pids.
355 * Returns true if @task should *NOT* be traced.
356 * Returns false if @task should be traced.
357 */
358bool
359trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task)
360{
361 /*
362 * Return false, because if filtered_pids does not exist,
363 * all pids are good to trace.
364 */
365 if (!filtered_pids)
366 return false;
367
368 return !trace_find_filtered_pid(filtered_pids, task->pid);
369}
370
371/**
372 * trace_pid_filter_add_remove - Add or remove a task from a pid_list
373 * @pid_list: The list to modify
374 * @self: The current task for fork or NULL for exit
375 * @task: The task to add or remove
376 *
377 * If adding a task, if @self is defined, the task is only added if @self
378 * is also included in @pid_list. This happens on fork and tasks should
379 * only be added when the parent is listed. If @self is NULL, then the
380 * @task pid will be removed from the list, which would happen on exit
381 * of a task.
382 */
383void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
384 struct task_struct *self,
385 struct task_struct *task)
386{
387 if (!pid_list)
388 return;
389
390 /* For forks, we only add if the forking task is listed */
391 if (self) {
392 if (!trace_find_filtered_pid(pid_list, self->pid))
393 return;
394 }
395
396 /* Sorry, but we don't support pid_max changing after setting */
397 if (task->pid >= pid_list->pid_max)
398 return;
399
400 /* "self" is set for forks, and NULL for exits */
401 if (self)
402 set_bit(task->pid, pid_list->pids);
403 else
404 clear_bit(task->pid, pid_list->pids);
405}
406
Steven Rostedt (Red Hat)5cc89762016-04-20 15:19:54 -0400407/**
408 * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
409 * @pid_list: The pid list to show
410 * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
411 * @pos: The position of the file
412 *
413 * This is used by the seq_file "next" operation to iterate the pids
414 * listed in a trace_pid_list structure.
415 *
416 * Returns the pid+1 as we want to display pid of zero, but NULL would
417 * stop the iteration.
418 */
419void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
420{
421 unsigned long pid = (unsigned long)v;
422
423 (*pos)++;
424
425 /* pid already is +1 of the actual prevous bit */
426 pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
427
428 /* Return pid + 1 to allow zero to be represented */
429 if (pid < pid_list->pid_max)
430 return (void *)(pid + 1);
431
432 return NULL;
433}
434
435/**
436 * trace_pid_start - Used for seq_file to start reading pid lists
437 * @pid_list: The pid list to show
438 * @pos: The position of the file
439 *
440 * This is used by seq_file "start" operation to start the iteration
441 * of listing pids.
442 *
443 * Returns the pid+1 as we want to display pid of zero, but NULL would
444 * stop the iteration.
445 */
446void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
447{
448 unsigned long pid;
449 loff_t l = 0;
450
451 pid = find_first_bit(pid_list->pids, pid_list->pid_max);
452 if (pid >= pid_list->pid_max)
453 return NULL;
454
455 /* Return pid + 1 so that zero can be the exit value */
456 for (pid++; pid && l < *pos;
457 pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
458 ;
459 return (void *)pid;
460}
461
462/**
463 * trace_pid_show - show the current pid in seq_file processing
464 * @m: The seq_file structure to write into
465 * @v: A void pointer of the pid (+1) value to display
466 *
467 * Can be directly used by seq_file operations to display the current
468 * pid value.
469 */
470int trace_pid_show(struct seq_file *m, void *v)
471{
472 unsigned long pid = (unsigned long)v - 1;
473
474 seq_printf(m, "%lu\n", pid);
475 return 0;
476}
477
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400478/* 128 should be much more than enough */
479#define PID_BUF_SIZE 127
480
481int trace_pid_write(struct trace_pid_list *filtered_pids,
482 struct trace_pid_list **new_pid_list,
483 const char __user *ubuf, size_t cnt)
484{
485 struct trace_pid_list *pid_list;
486 struct trace_parser parser;
487 unsigned long val;
488 int nr_pids = 0;
489 ssize_t read = 0;
490 ssize_t ret = 0;
491 loff_t pos;
492 pid_t pid;
493
494 if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
495 return -ENOMEM;
496
497 /*
498 * Always recreate a new array. The write is an all or nothing
499 * operation. Always create a new array when adding new pids by
500 * the user. If the operation fails, then the current list is
501 * not modified.
502 */
503 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
Wenwen Wang3ddc2992019-04-19 21:22:59 -0500504 if (!pid_list) {
505 trace_parser_put(&parser);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400506 return -ENOMEM;
Wenwen Wang3ddc2992019-04-19 21:22:59 -0500507 }
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400508
509 pid_list->pid_max = READ_ONCE(pid_max);
510
511 /* Only truncating will shrink pid_max */
512 if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
513 pid_list->pid_max = filtered_pids->pid_max;
514
515 pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
516 if (!pid_list->pids) {
Wenwen Wang3ddc2992019-04-19 21:22:59 -0500517 trace_parser_put(&parser);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400518 kfree(pid_list);
519 return -ENOMEM;
520 }
521
522 if (filtered_pids) {
523 /* copy the current bits to the new max */
Wei Yongjun67f20b02016-07-04 15:10:04 +0000524 for_each_set_bit(pid, filtered_pids->pids,
525 filtered_pids->pid_max) {
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400526 set_bit(pid, pid_list->pids);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400527 nr_pids++;
528 }
529 }
530
531 while (cnt > 0) {
532
533 pos = 0;
534
535 ret = trace_get_user(&parser, ubuf, cnt, &pos);
536 if (ret < 0 || !trace_parser_loaded(&parser))
537 break;
538
539 read += ret;
540 ubuf += ret;
541 cnt -= ret;
542
543 parser.buffer[parser.idx] = 0;
544
545 ret = -EINVAL;
546 if (kstrtoul(parser.buffer, 0, &val))
547 break;
548 if (val >= pid_list->pid_max)
549 break;
550
551 pid = (pid_t)val;
552
553 set_bit(pid, pid_list->pids);
554 nr_pids++;
555
556 trace_parser_clear(&parser);
557 ret = 0;
558 }
559 trace_parser_put(&parser);
560
561 if (ret < 0) {
562 trace_free_pid_list(pid_list);
563 return ret;
564 }
565
566 if (!nr_pids) {
567 /* Cleared the list of pids */
568 trace_free_pid_list(pid_list);
569 read = ret;
570 pid_list = NULL;
571 }
572
573 *new_pid_list = pid_list;
574
575 return read;
576}
577
Fabian Frederickad1438a2014-04-17 21:44:42 +0200578static cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
Steven Rostedt37886f62009-03-17 17:22:06 -0400579{
580 u64 ts;
581
582 /* Early boot up does not have a buffer yet */
Alexander Z Lam94571582013-08-02 18:36:16 -0700583 if (!buf->buffer)
Steven Rostedt37886f62009-03-17 17:22:06 -0400584 return trace_clock_local();
585
Alexander Z Lam94571582013-08-02 18:36:16 -0700586 ts = ring_buffer_time_stamp(buf->buffer, cpu);
587 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
Steven Rostedt37886f62009-03-17 17:22:06 -0400588
589 return ts;
590}
591
Alexander Z Lam94571582013-08-02 18:36:16 -0700592cycle_t ftrace_now(int cpu)
593{
594 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
595}
596
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400597/**
598 * tracing_is_enabled - Show if global_trace has been disabled
599 *
600 * Shows if the global trace has been enabled or not. It uses the
601 * mirror flag "buffer_disabled" to be used in fast paths such as for
602 * the irqsoff tracer. But it may be inaccurate due to races. If you
603 * need to know the accurate state, use tracing_is_on() which is a little
604 * slower, but accurate.
605 */
Steven Rostedt90369902008-11-05 16:05:44 -0500606int tracing_is_enabled(void)
607{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400608 /*
609 * For quick access (irqsoff uses this in fast path), just
610 * return the mirror variable of the state of the ring buffer.
611 * It's a little racy, but we don't really care.
612 */
613 smp_rmb();
614 return !global_trace.buffer_disabled;
Steven Rostedt90369902008-11-05 16:05:44 -0500615}
616
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200617/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400618 * trace_buf_size is the size in bytes that is allocated
619 * for a buffer. Note, the number of bytes is always rounded
620 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400621 *
622 * This number is purposely set to a low number of 16384.
623 * If the dump on oops happens, it will be much appreciated
624 * to not have to wait for all that output. Anyway this can be
625 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200626 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400627#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400628
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400629static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200630
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200631/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200632static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200633
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200634/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200635 * trace_types_lock is used to protect the trace_types list.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200636 */
Alexander Z Lama8227412013-07-01 19:37:54 -0700637DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200638
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800639/*
640 * serialize the access of the ring buffer
641 *
642 * ring buffer serializes readers, but it is low level protection.
643 * The validity of the events (which returns by ring_buffer_peek() ..etc)
644 * are not protected by ring buffer.
645 *
646 * The content of events may become garbage if we allow other process consumes
647 * these events concurrently:
648 * A) the page of the consumed events may become a normal page
649 * (not reader page) in ring buffer, and this page will be rewrited
650 * by events producer.
651 * B) The page of the consumed events may become a page for splice_read,
652 * and this page will be returned to system.
653 *
654 * These primitives allow multi process access to different cpu ring buffer
655 * concurrently.
656 *
657 * These primitives don't distinguish read-only and read-consume access.
658 * Multi read-only access are also serialized.
659 */
660
661#ifdef CONFIG_SMP
662static DECLARE_RWSEM(all_cpu_access_lock);
663static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
664
665static inline void trace_access_lock(int cpu)
666{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500667 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800668 /* gain it for accessing the whole ring buffer. */
669 down_write(&all_cpu_access_lock);
670 } else {
671 /* gain it for accessing a cpu ring buffer. */
672
Steven Rostedtae3b5092013-01-23 15:22:59 -0500673 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800674 down_read(&all_cpu_access_lock);
675
676 /* Secondly block other access to this @cpu ring buffer. */
677 mutex_lock(&per_cpu(cpu_access_lock, cpu));
678 }
679}
680
681static inline void trace_access_unlock(int cpu)
682{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500683 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800684 up_write(&all_cpu_access_lock);
685 } else {
686 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
687 up_read(&all_cpu_access_lock);
688 }
689}
690
691static inline void trace_access_lock_init(void)
692{
693 int cpu;
694
695 for_each_possible_cpu(cpu)
696 mutex_init(&per_cpu(cpu_access_lock, cpu));
697}
698
699#else
700
701static DEFINE_MUTEX(access_lock);
702
703static inline void trace_access_lock(int cpu)
704{
705 (void)cpu;
706 mutex_lock(&access_lock);
707}
708
709static inline void trace_access_unlock(int cpu)
710{
711 (void)cpu;
712 mutex_unlock(&access_lock);
713}
714
715static inline void trace_access_lock_init(void)
716{
717}
718
719#endif
720
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400721#ifdef CONFIG_STACKTRACE
722static void __ftrace_trace_stack(struct ring_buffer *buffer,
723 unsigned long flags,
724 int skip, int pc, struct pt_regs *regs);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400725static inline void ftrace_trace_stack(struct trace_array *tr,
726 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -0400727 unsigned long flags,
728 int skip, int pc, struct pt_regs *regs);
Steven Rostedt (Red Hat)ca475e82015-09-28 09:41:11 -0400729
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400730#else
731static inline void __ftrace_trace_stack(struct ring_buffer *buffer,
732 unsigned long flags,
733 int skip, int pc, struct pt_regs *regs)
734{
735}
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400736static inline void ftrace_trace_stack(struct trace_array *tr,
737 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -0400738 unsigned long flags,
739 int skip, int pc, struct pt_regs *regs)
Steven Rostedt (Red Hat)ca475e82015-09-28 09:41:11 -0400740{
741}
742
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400743#endif
744
Steven Rostedt (Red Hat)5280bce2013-07-02 19:59:57 -0400745static void tracer_tracing_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400746{
747 if (tr->trace_buffer.buffer)
748 ring_buffer_record_on(tr->trace_buffer.buffer);
749 /*
750 * This flag is looked at when buffers haven't been allocated
751 * yet, or by some tracers (like irqsoff), that just want to
752 * know if the ring buffer has been disabled, but it can handle
753 * races of where it gets disabled but we still do a record.
754 * As the check is in the fast path of the tracers, it is more
755 * important to be fast than accurate.
756 */
757 tr->buffer_disabled = 0;
758 /* Make the flag seen by readers */
759 smp_wmb();
760}
761
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200762/**
Steven Rostedt499e5472012-02-22 15:50:28 -0500763 * tracing_on - enable tracing buffers
764 *
765 * This function enables tracing buffers that may have been
766 * disabled with tracing_off.
767 */
768void tracing_on(void)
769{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400770 tracer_tracing_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -0500771}
772EXPORT_SYMBOL_GPL(tracing_on);
773
774/**
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500775 * __trace_puts - write a constant string into the trace buffer.
776 * @ip: The address of the caller
777 * @str: The constant string to write
778 * @size: The size of the string.
779 */
780int __trace_puts(unsigned long ip, const char *str, int size)
781{
782 struct ring_buffer_event *event;
783 struct ring_buffer *buffer;
784 struct print_entry *entry;
785 unsigned long irq_flags;
786 int alloc;
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800787 int pc;
788
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400789 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800790 return 0;
791
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800792 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500793
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500794 if (unlikely(tracing_selftest_running || tracing_disabled))
795 return 0;
796
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500797 alloc = sizeof(*entry) + size + 2; /* possible \n added */
798
799 local_save_flags(irq_flags);
800 buffer = global_trace.trace_buffer.buffer;
801 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800802 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500803 if (!event)
804 return 0;
805
806 entry = ring_buffer_event_data(event);
807 entry->ip = ip;
808
809 memcpy(&entry->buf, str, size);
810
811 /* Add a newline if necessary */
812 if (entry->buf[size - 1] != '\n') {
813 entry->buf[size] = '\n';
814 entry->buf[size + 1] = '\0';
815 } else
816 entry->buf[size] = '\0';
817
818 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400819 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500820
821 return size;
822}
823EXPORT_SYMBOL_GPL(__trace_puts);
824
825/**
826 * __trace_bputs - write the pointer to a constant string into trace buffer
827 * @ip: The address of the caller
828 * @str: The constant string to write to the buffer to
829 */
830int __trace_bputs(unsigned long ip, const char *str)
831{
832 struct ring_buffer_event *event;
833 struct ring_buffer *buffer;
834 struct bputs_entry *entry;
835 unsigned long irq_flags;
836 int size = sizeof(struct bputs_entry);
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800837 int pc;
838
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400839 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800840 return 0;
841
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800842 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500843
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500844 if (unlikely(tracing_selftest_running || tracing_disabled))
845 return 0;
846
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500847 local_save_flags(irq_flags);
848 buffer = global_trace.trace_buffer.buffer;
849 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800850 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500851 if (!event)
852 return 0;
853
854 entry = ring_buffer_event_data(event);
855 entry->ip = ip;
856 entry->str = str;
857
858 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400859 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500860
861 return 1;
862}
863EXPORT_SYMBOL_GPL(__trace_bputs);
864
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500865#ifdef CONFIG_TRACER_SNAPSHOT
866/**
867 * trace_snapshot - take a snapshot of the current buffer.
868 *
869 * This causes a swap between the snapshot buffer and the current live
870 * tracing buffer. You can use this to take snapshots of the live
871 * trace when some condition is triggered, but continue to trace.
872 *
873 * Note, make sure to allocate the snapshot with either
874 * a tracing_snapshot_alloc(), or by doing it manually
875 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
876 *
877 * If the snapshot buffer is not allocated, it will stop tracing.
878 * Basically making a permanent snapshot.
879 */
880void tracing_snapshot(void)
881{
882 struct trace_array *tr = &global_trace;
883 struct tracer *tracer = tr->current_trace;
884 unsigned long flags;
885
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500886 if (in_nmi()) {
887 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
888 internal_trace_puts("*** snapshot is being ignored ***\n");
889 return;
890 }
891
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500892 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500893 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
894 internal_trace_puts("*** stopping trace here! ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500895 tracing_off();
896 return;
897 }
898
899 /* Note, snapshot can not be used when the tracer uses it */
900 if (tracer->use_max_tr) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500901 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
902 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500903 return;
904 }
905
906 local_irq_save(flags);
907 update_max_tr(tr, current, smp_processor_id());
908 local_irq_restore(flags);
909}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500910EXPORT_SYMBOL_GPL(tracing_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500911
912static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
913 struct trace_buffer *size_buf, int cpu_id);
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400914static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
915
916static int alloc_snapshot(struct trace_array *tr)
917{
918 int ret;
919
920 if (!tr->allocated_snapshot) {
921
922 /* allocate spare buffer */
923 ret = resize_buffer_duplicate_size(&tr->max_buffer,
924 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
925 if (ret < 0)
926 return ret;
927
928 tr->allocated_snapshot = true;
929 }
930
931 return 0;
932}
933
Fabian Frederickad1438a2014-04-17 21:44:42 +0200934static void free_snapshot(struct trace_array *tr)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400935{
936 /*
937 * We don't free the ring buffer. instead, resize it because
938 * The max_tr ring buffer has some state (e.g. ring->clock) and
939 * we want preserve it.
940 */
941 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
942 set_buffer_entries(&tr->max_buffer, 1);
943 tracing_reset_online_cpus(&tr->max_buffer);
944 tr->allocated_snapshot = false;
945}
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500946
947/**
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500948 * tracing_alloc_snapshot - allocate snapshot buffer.
949 *
950 * This only allocates the snapshot buffer if it isn't already
951 * allocated - it doesn't also take a snapshot.
952 *
953 * This is meant to be used in cases where the snapshot buffer needs
954 * to be set up for events that can't sleep but need to be able to
955 * trigger a snapshot.
956 */
957int tracing_alloc_snapshot(void)
958{
959 struct trace_array *tr = &global_trace;
960 int ret;
961
962 ret = alloc_snapshot(tr);
963 WARN_ON(ret < 0);
964
965 return ret;
966}
967EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
968
969/**
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500970 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
971 *
972 * This is similar to trace_snapshot(), but it will allocate the
973 * snapshot buffer if it isn't already allocated. Use this only
974 * where it is safe to sleep, as the allocation may sleep.
975 *
976 * This causes a swap between the snapshot buffer and the current live
977 * tracing buffer. You can use this to take snapshots of the live
978 * trace when some condition is triggered, but continue to trace.
979 */
980void tracing_snapshot_alloc(void)
981{
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500982 int ret;
983
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500984 ret = tracing_alloc_snapshot();
985 if (ret < 0)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400986 return;
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500987
988 tracing_snapshot();
989}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500990EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500991#else
992void tracing_snapshot(void)
993{
994 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
995}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500996EXPORT_SYMBOL_GPL(tracing_snapshot);
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500997int tracing_alloc_snapshot(void)
998{
999 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1000 return -ENODEV;
1001}
1002EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001003void tracing_snapshot_alloc(void)
1004{
1005 /* Give warning */
1006 tracing_snapshot();
1007}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001008EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001009#endif /* CONFIG_TRACER_SNAPSHOT */
1010
Steven Rostedt (Red Hat)5280bce2013-07-02 19:59:57 -04001011static void tracer_tracing_off(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001012{
1013 if (tr->trace_buffer.buffer)
1014 ring_buffer_record_off(tr->trace_buffer.buffer);
1015 /*
1016 * This flag is looked at when buffers haven't been allocated
1017 * yet, or by some tracers (like irqsoff), that just want to
1018 * know if the ring buffer has been disabled, but it can handle
1019 * races of where it gets disabled but we still do a record.
1020 * As the check is in the fast path of the tracers, it is more
1021 * important to be fast than accurate.
1022 */
1023 tr->buffer_disabled = 1;
1024 /* Make the flag seen by readers */
1025 smp_wmb();
1026}
1027
Steven Rostedt499e5472012-02-22 15:50:28 -05001028/**
1029 * tracing_off - turn off tracing buffers
1030 *
1031 * This function stops the tracing buffers from recording data.
1032 * It does not disable any overhead the tracers themselves may
1033 * be causing. This function simply causes all recording to
1034 * the ring buffers to fail.
1035 */
1036void tracing_off(void)
1037{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001038 tracer_tracing_off(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001039}
1040EXPORT_SYMBOL_GPL(tracing_off);
1041
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -04001042void disable_trace_on_warning(void)
1043{
1044 if (__disable_trace_on_warning)
1045 tracing_off();
1046}
1047
Steven Rostedt499e5472012-02-22 15:50:28 -05001048/**
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001049 * tracer_tracing_is_on - show real state of ring buffer enabled
1050 * @tr : the trace array to know if ring buffer is enabled
1051 *
1052 * Shows real state of the ring buffer if it is enabled or not.
1053 */
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -04001054int tracer_tracing_is_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001055{
1056 if (tr->trace_buffer.buffer)
1057 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
1058 return !tr->buffer_disabled;
1059}
1060
Steven Rostedt499e5472012-02-22 15:50:28 -05001061/**
1062 * tracing_is_on - show state of ring buffers enabled
1063 */
1064int tracing_is_on(void)
1065{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001066 return tracer_tracing_is_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001067}
1068EXPORT_SYMBOL_GPL(tracing_is_on);
1069
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001070static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001071{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001072 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001073
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001074 if (!str)
1075 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +08001076 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001077 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +08001078 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001079 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001080 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001081 return 1;
1082}
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001083__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001084
Tim Bird0e950172010-02-25 15:36:43 -08001085static int __init set_tracing_thresh(char *str)
1086{
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001087 unsigned long threshold;
Tim Bird0e950172010-02-25 15:36:43 -08001088 int ret;
1089
1090 if (!str)
1091 return 0;
Daniel Walterbcd83ea2012-09-26 22:08:38 +02001092 ret = kstrtoul(str, 0, &threshold);
Tim Bird0e950172010-02-25 15:36:43 -08001093 if (ret < 0)
1094 return 0;
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001095 tracing_thresh = threshold * 1000;
Tim Bird0e950172010-02-25 15:36:43 -08001096 return 1;
1097}
1098__setup("tracing_thresh=", set_tracing_thresh);
1099
Steven Rostedt57f50be2008-05-12 21:20:44 +02001100unsigned long nsecs_to_usecs(unsigned long nsecs)
1101{
1102 return nsecs / 1000;
1103}
1104
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001105/*
1106 * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
1107 * It uses C(a, b) where 'a' is the enum name and 'b' is the string that
1108 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
1109 * of strings in the order that the enums were defined.
1110 */
1111#undef C
1112#define C(a, b) b
1113
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001114/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001115static const char *trace_options[] = {
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001116 TRACE_FLAGS
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001117 NULL
1118};
1119
Zhaolei5079f322009-08-25 16:12:56 +08001120static struct {
1121 u64 (*func)(void);
1122 const char *name;
David Sharp8be07092012-11-13 12:18:22 -08001123 int in_ns; /* is this clock in nanoseconds? */
Zhaolei5079f322009-08-25 16:12:56 +08001124} trace_clocks[] = {
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001125 { trace_clock_local, "local", 1 },
1126 { trace_clock_global, "global", 1 },
1127 { trace_clock_counter, "counter", 0 },
Linus Torvaldse7fda6c2014-08-05 17:46:42 -07001128 { trace_clock_jiffies, "uptime", 0 },
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001129 { trace_clock, "perf", 1 },
1130 { ktime_get_mono_fast_ns, "mono", 1 },
Drew Richardsonaabfa5f2015-05-08 07:30:39 -07001131 { ktime_get_raw_fast_ns, "mono_raw", 1 },
Joel Fernandes59cbbe32016-11-28 14:35:23 -08001132 { ktime_get_boot_fast_ns, "boot", 1 },
David Sharp8cbd9cc2012-11-13 12:18:21 -08001133 ARCH_TRACE_CLOCKS
Zhaolei5079f322009-08-25 16:12:56 +08001134};
1135
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001136/*
1137 * trace_parser_get_init - gets the buffer for trace parser
1138 */
1139int trace_parser_get_init(struct trace_parser *parser, int size)
1140{
1141 memset(parser, 0, sizeof(*parser));
1142
1143 parser->buffer = kmalloc(size, GFP_KERNEL);
1144 if (!parser->buffer)
1145 return 1;
1146
1147 parser->size = size;
1148 return 0;
1149}
1150
1151/*
1152 * trace_parser_put - frees the buffer for trace parser
1153 */
1154void trace_parser_put(struct trace_parser *parser)
1155{
1156 kfree(parser->buffer);
1157}
1158
1159/*
1160 * trace_get_user - reads the user input string separated by space
1161 * (matched by isspace(ch))
1162 *
1163 * For each string found the 'struct trace_parser' is updated,
1164 * and the function returns.
1165 *
1166 * Returns number of bytes read.
1167 *
1168 * See kernel/trace/trace.h for 'struct trace_parser' details.
1169 */
1170int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1171 size_t cnt, loff_t *ppos)
1172{
1173 char ch;
1174 size_t read = 0;
1175 ssize_t ret;
1176
1177 if (!*ppos)
1178 trace_parser_clear(parser);
1179
1180 ret = get_user(ch, ubuf++);
1181 if (ret)
1182 goto out;
1183
1184 read++;
1185 cnt--;
1186
1187 /*
1188 * The parser is not finished with the last write,
1189 * continue reading the user input without skipping spaces.
1190 */
1191 if (!parser->cont) {
1192 /* skip white space */
1193 while (cnt && isspace(ch)) {
1194 ret = get_user(ch, ubuf++);
1195 if (ret)
1196 goto out;
1197 read++;
1198 cnt--;
1199 }
1200
1201 /* only spaces were written */
1202 if (isspace(ch)) {
1203 *ppos += read;
1204 ret = read;
1205 goto out;
1206 }
1207
1208 parser->idx = 0;
1209 }
1210
1211 /* read the non-space input */
1212 while (cnt && !isspace(ch)) {
Li Zefan3c235a32009-09-22 13:51:54 +08001213 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001214 parser->buffer[parser->idx++] = ch;
1215 else {
1216 ret = -EINVAL;
1217 goto out;
1218 }
1219 ret = get_user(ch, ubuf++);
1220 if (ret)
1221 goto out;
1222 read++;
1223 cnt--;
1224 }
1225
1226 /* We either got finished input or we have to wait for another call. */
1227 if (isspace(ch)) {
1228 parser->buffer[parser->idx] = 0;
1229 parser->cont = false;
Steven Rostedt057db842013-10-09 22:23:23 -04001230 } else if (parser->idx < parser->size - 1) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001231 parser->cont = true;
1232 parser->buffer[parser->idx++] = ch;
Steven Rostedt057db842013-10-09 22:23:23 -04001233 } else {
1234 ret = -EINVAL;
1235 goto out;
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001236 }
1237
1238 *ppos += read;
1239 ret = read;
1240
1241out:
1242 return ret;
1243}
1244
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001245/* TODO add a seq_buf_to_buffer() */
Dmitri Vorobievb8b94262009-03-22 19:11:11 +02001246static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001247{
1248 int len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001249
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001250 if (trace_seq_used(s) <= s->seq.readpos)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001251 return -EBUSY;
1252
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001253 len = trace_seq_used(s) - s->seq.readpos;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001254 if (cnt > len)
1255 cnt = len;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001256 memcpy(buf, s->buffer + s->seq.readpos, cnt);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001257
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001258 s->seq.readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001259 return cnt;
1260}
1261
Tim Bird0e950172010-02-25 15:36:43 -08001262unsigned long __read_mostly tracing_thresh;
1263
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001264#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001265/*
1266 * Copy the new maximum trace into the separate maximum-trace
1267 * structure. (this way the maximum trace is permanently saved,
1268 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
1269 */
1270static void
1271__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1272{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001273 struct trace_buffer *trace_buf = &tr->trace_buffer;
1274 struct trace_buffer *max_buf = &tr->max_buffer;
1275 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1276 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001277
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001278 max_buf->cpu = cpu;
1279 max_buf->time_start = data->preempt_timestamp;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001280
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001281 max_data->saved_latency = tr->max_latency;
Steven Rostedt8248ac02009-09-02 12:27:41 -04001282 max_data->critical_start = data->critical_start;
1283 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001284
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -03001285 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -04001286 max_data->pid = tsk->pid;
Steven Rostedt (Red Hat)f17a5192013-05-30 21:10:37 -04001287 /*
1288 * If tsk == current, then use current_uid(), as that does not use
1289 * RCU. The irq tracer can be called out of RCU scope.
1290 */
1291 if (tsk == current)
1292 max_data->uid = current_uid();
1293 else
1294 max_data->uid = task_uid(tsk);
1295
Steven Rostedt8248ac02009-09-02 12:27:41 -04001296 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1297 max_data->policy = tsk->policy;
1298 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001299
1300 /* record this tasks comm */
1301 tracing_record_cmdline(tsk);
1302}
1303
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001304/**
1305 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1306 * @tr: tracer
1307 * @tsk: the task with the latency
1308 * @cpu: The cpu that initiated the trace.
1309 *
1310 * Flip the buffers between the @tr and the max_tr and record information
1311 * about which task was the cause of this latency.
1312 */
Ingo Molnare309b412008-05-12 21:20:51 +02001313void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001314update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1315{
Steven Rostedt (Red Hat)2721e722013-03-12 11:32:32 -04001316 struct ring_buffer *buf;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001317
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001318 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001319 return;
1320
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001321 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt34600f02013-01-22 13:35:11 -05001322
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05001323 if (!tr->allocated_snapshot) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001324 /* Only the nop tracer should hit this when disabling */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001325 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001326 return;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001327 }
Steven Rostedt34600f02013-01-22 13:35:11 -05001328
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001329 arch_spin_lock(&tr->max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001330
Masami Hiramatsua26030a2018-07-14 01:28:15 +09001331 /* Inherit the recordable setting from trace_buffer */
1332 if (ring_buffer_record_is_set_on(tr->trace_buffer.buffer))
1333 ring_buffer_record_on(tr->max_buffer.buffer);
1334 else
1335 ring_buffer_record_off(tr->max_buffer.buffer);
1336
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001337 buf = tr->trace_buffer.buffer;
1338 tr->trace_buffer.buffer = tr->max_buffer.buffer;
1339 tr->max_buffer.buffer = buf;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001340
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001341 __update_max_tr(tr, tsk, cpu);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001342 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001343}
1344
1345/**
1346 * update_max_tr_single - only copy one trace over, and reset the rest
1347 * @tr - tracer
1348 * @tsk - task with the latency
1349 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001350 *
1351 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001352 */
Ingo Molnare309b412008-05-12 21:20:51 +02001353void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001354update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1355{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001356 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001357
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001358 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001359 return;
1360
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001361 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt6c244992013-04-29 20:08:14 -04001362 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001363 /* Only the nop tracer should hit this when disabling */
Linus Torvalds9e8529a2013-04-29 13:55:38 -07001364 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001365 return;
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001366 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001367
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001368 arch_spin_lock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001369
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001370 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001371
Steven Rostedte8165db2009-09-03 19:13:05 -04001372 if (ret == -EBUSY) {
1373 /*
1374 * We failed to swap the buffer due to a commit taking
1375 * place on this CPU. We fail to record, but we reset
1376 * the max trace buffer (no one writes directly to it)
1377 * and flag that it failed.
1378 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001379 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
Steven Rostedte8165db2009-09-03 19:13:05 -04001380 "Failed to swap buffers due to commit in progress\n");
1381 }
1382
Steven Rostedte8165db2009-09-03 19:13:05 -04001383 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001384
1385 __update_max_tr(tr, tsk, cpu);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001386 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001387}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001388#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001389
Rabin Vincente30f53a2014-11-10 19:46:34 +01001390static int wait_on_pipe(struct trace_iterator *iter, bool full)
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001391{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001392 /* Iterators are static, they should be filled or empty */
1393 if (trace_buffer_iter(iter, iter->cpu_file))
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04001394 return 0;
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001395
Rabin Vincente30f53a2014-11-10 19:46:34 +01001396 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1397 full);
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001398}
1399
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001400#ifdef CONFIG_FTRACE_STARTUP_TEST
1401static int run_tracer_selftest(struct tracer *type)
1402{
1403 struct trace_array *tr = &global_trace;
1404 struct tracer *saved_tracer = tr->current_trace;
1405 int ret;
1406
1407 if (!type->selftest || tracing_selftest_disabled)
1408 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001409
1410 /*
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001411 * Run a selftest on this tracer.
1412 * Here we reset the trace buffer, and set the current
1413 * tracer to be this tracer. The tracer can then run some
1414 * internal tracing to verify that everything is in order.
1415 * If we fail, we do not register this tracer.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001416 */
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001417 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001418
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001419 tr->current_trace = type;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001420
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001421#ifdef CONFIG_TRACER_MAX_TRACE
1422 if (type->use_max_tr) {
1423 /* If we expanded the buffers, make sure the max is expanded too */
1424 if (ring_buffer_expanded)
1425 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1426 RING_BUFFER_ALL_CPUS);
1427 tr->allocated_snapshot = true;
1428 }
1429#endif
1430
1431 /* the test is responsible for initializing and enabling */
1432 pr_info("Testing tracer %s: ", type->name);
1433 ret = type->selftest(type, tr);
1434 /* the test is responsible for resetting too */
1435 tr->current_trace = saved_tracer;
1436 if (ret) {
1437 printk(KERN_CONT "FAILED!\n");
1438 /* Add the warning after printing 'FAILED' */
1439 WARN_ON(1);
1440 return -1;
1441 }
1442 /* Only reset on passing, to avoid touching corrupted buffers */
1443 tracing_reset_online_cpus(&tr->trace_buffer);
1444
1445#ifdef CONFIG_TRACER_MAX_TRACE
1446 if (type->use_max_tr) {
1447 tr->allocated_snapshot = false;
1448
1449 /* Shrink the max buffer again */
1450 if (ring_buffer_expanded)
1451 ring_buffer_resize(tr->max_buffer.buffer, 1,
1452 RING_BUFFER_ALL_CPUS);
1453 }
1454#endif
1455
1456 printk(KERN_CONT "PASSED\n");
1457 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001458}
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001459#else
1460static inline int run_tracer_selftest(struct tracer *type)
1461{
1462 return 0;
1463}
1464#endif /* CONFIG_FTRACE_STARTUP_TEST */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001465
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001466static void add_tracer_options(struct trace_array *tr, struct tracer *t);
1467
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001468static void __init apply_trace_boot_options(void);
1469
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001470/**
1471 * register_tracer - register a tracer with the ftrace system.
1472 * @type - the plugin for the tracer
1473 *
1474 * Register a new plugin tracer.
1475 */
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001476int __init register_tracer(struct tracer *type)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001477{
1478 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001479 int ret = 0;
1480
1481 if (!type->name) {
1482 pr_info("Tracer must have a name\n");
1483 return -1;
1484 }
1485
Dan Carpenter24a461d2010-07-10 12:06:44 +02001486 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +08001487 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1488 return -1;
1489 }
1490
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001491 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +01001492
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001493 tracing_selftest_running = true;
1494
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001495 for (t = trace_types; t; t = t->next) {
1496 if (strcmp(type->name, t->name) == 0) {
1497 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +08001498 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001499 type->name);
1500 ret = -1;
1501 goto out;
1502 }
1503 }
1504
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001505 if (!type->set_flag)
1506 type->set_flag = &dummy_set_flag;
Chunyu Hud39cdd22016-03-08 21:37:01 +08001507 if (!type->flags) {
1508 /*allocate a dummy tracer_flags*/
1509 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
Chunyu Huc8ca0032016-03-14 20:35:41 +08001510 if (!type->flags) {
1511 ret = -ENOMEM;
1512 goto out;
1513 }
Chunyu Hud39cdd22016-03-08 21:37:01 +08001514 type->flags->val = 0;
1515 type->flags->opts = dummy_tracer_opt;
1516 } else
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001517 if (!type->flags->opts)
1518 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001519
Chunyu Hud39cdd22016-03-08 21:37:01 +08001520 /* store the tracer for __set_tracer_option */
1521 type->flags->trace = type;
1522
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001523 ret = run_tracer_selftest(type);
1524 if (ret < 0)
1525 goto out;
Steven Rostedt60a11772008-05-12 21:20:44 +02001526
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001527 type->next = trace_types;
1528 trace_types = type;
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001529 add_tracer_options(&global_trace, type);
Steven Rostedt60a11772008-05-12 21:20:44 +02001530
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001531 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001532 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001533 mutex_unlock(&trace_types_lock);
1534
Steven Rostedtdac74942009-02-05 01:13:38 -05001535 if (ret || !default_bootup_tracer)
1536 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -05001537
Li Zefanee6c2c12009-09-18 14:06:47 +08001538 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -05001539 goto out_unlock;
1540
1541 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1542 /* Do we want this tracer to start on bootup? */
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05001543 tracing_set_tracer(&global_trace, type->name);
Steven Rostedtdac74942009-02-05 01:13:38 -05001544 default_bootup_tracer = NULL;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001545
1546 apply_trace_boot_options();
1547
Steven Rostedtdac74942009-02-05 01:13:38 -05001548 /* disable other selftests, since this will break it. */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05001549 tracing_selftest_disabled = true;
Steven Rostedtdac74942009-02-05 01:13:38 -05001550#ifdef CONFIG_FTRACE_STARTUP_TEST
1551 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1552 type->name);
1553#endif
1554
1555 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001556 return ret;
1557}
1558
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001559void tracing_reset(struct trace_buffer *buf, int cpu)
Steven Rostedtf6339032009-09-04 12:35:16 -04001560{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001561 struct ring_buffer *buffer = buf->buffer;
Steven Rostedtf6339032009-09-04 12:35:16 -04001562
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001563 if (!buffer)
1564 return;
1565
Steven Rostedtf6339032009-09-04 12:35:16 -04001566 ring_buffer_record_disable(buffer);
1567
1568 /* Make sure all commits have finished */
1569 synchronize_sched();
Steven Rostedt68179682012-05-08 20:57:53 -04001570 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -04001571
1572 ring_buffer_record_enable(buffer);
1573}
1574
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001575void tracing_reset_online_cpus(struct trace_buffer *buf)
Pekka J Enberg213cc062008-12-19 12:08:39 +02001576{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001577 struct ring_buffer *buffer = buf->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +02001578 int cpu;
1579
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001580 if (!buffer)
1581 return;
1582
Steven Rostedt621968c2009-09-04 12:02:35 -04001583 ring_buffer_record_disable(buffer);
1584
1585 /* Make sure all commits have finished */
1586 synchronize_sched();
1587
Alexander Z Lam94571582013-08-02 18:36:16 -07001588 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001589
1590 for_each_online_cpu(cpu)
Steven Rostedt68179682012-05-08 20:57:53 -04001591 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -04001592
1593 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001594}
1595
Steven Rostedt (Red Hat)09d80912013-07-23 22:21:59 -04001596/* Must have trace_types_lock held */
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001597void tracing_reset_all_online_cpus(void)
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001598{
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001599 struct trace_array *tr;
1600
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001601 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001602 tracing_reset_online_cpus(&tr->trace_buffer);
1603#ifdef CONFIG_TRACER_MAX_TRACE
1604 tracing_reset_online_cpus(&tr->max_buffer);
1605#endif
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001606 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001607}
1608
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001609#define SAVED_CMDLINES_DEFAULT 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001610#define NO_CMDLINE_MAP UINT_MAX
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001611static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001612struct saved_cmdlines_buffer {
1613 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1614 unsigned *map_cmdline_to_pid;
Adrian Salido50b2d862017-04-18 11:44:33 -07001615 unsigned *map_cmdline_to_tgid;
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001616 unsigned cmdline_num;
1617 int cmdline_idx;
1618 char *saved_cmdlines;
1619};
1620static struct saved_cmdlines_buffer *savedcmd;
Steven Rostedt25b0b442008-05-12 21:21:00 +02001621
Steven Rostedt25b0b442008-05-12 21:21:00 +02001622/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +01001623static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001624
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001625static inline char *get_saved_cmdlines(int idx)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001626{
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001627 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
1628}
1629
1630static inline void set_cmdline(int idx, const char *cmdline)
1631{
1632 memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
1633}
1634
1635static int allocate_cmdlines_buffer(unsigned int val,
1636 struct saved_cmdlines_buffer *s)
1637{
1638 s->map_cmdline_to_pid = kmalloc(val * sizeof(*s->map_cmdline_to_pid),
1639 GFP_KERNEL);
1640 if (!s->map_cmdline_to_pid)
1641 return -ENOMEM;
1642
1643 s->saved_cmdlines = kmalloc(val * TASK_COMM_LEN, GFP_KERNEL);
1644 if (!s->saved_cmdlines) {
1645 kfree(s->map_cmdline_to_pid);
1646 return -ENOMEM;
1647 }
1648
Adrian Salido50b2d862017-04-18 11:44:33 -07001649 s->map_cmdline_to_tgid = kmalloc_array(val,
1650 sizeof(*s->map_cmdline_to_tgid),
1651 GFP_KERNEL);
1652 if (!s->map_cmdline_to_tgid) {
1653 kfree(s->map_cmdline_to_pid);
1654 kfree(s->saved_cmdlines);
1655 return -ENOMEM;
1656 }
1657
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001658 s->cmdline_idx = 0;
1659 s->cmdline_num = val;
1660 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
1661 sizeof(s->map_pid_to_cmdline));
1662 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
1663 val * sizeof(*s->map_cmdline_to_pid));
Adrian Salido50b2d862017-04-18 11:44:33 -07001664 memset(s->map_cmdline_to_tgid, NO_CMDLINE_MAP,
1665 val * sizeof(*s->map_cmdline_to_tgid));
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001666
1667 return 0;
1668}
1669
1670static int trace_create_savedcmd(void)
1671{
1672 int ret;
1673
Namhyung Kima6af8fb2014-06-10 16:11:35 +09001674 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001675 if (!savedcmd)
1676 return -ENOMEM;
1677
1678 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
1679 if (ret < 0) {
1680 kfree(savedcmd);
1681 savedcmd = NULL;
1682 return -ENOMEM;
1683 }
1684
1685 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001686}
1687
Carsten Emdeb5130b12009-09-13 01:43:07 +02001688int is_tracing_stopped(void)
1689{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001690 return global_trace.stop_count;
Carsten Emdeb5130b12009-09-13 01:43:07 +02001691}
1692
Steven Rostedt0f048702008-11-05 16:05:44 -05001693/**
1694 * tracing_start - quick start of the tracer
1695 *
1696 * If tracing is enabled but was stopped by tracing_stop,
1697 * this will start the tracer back up.
1698 */
1699void tracing_start(void)
1700{
1701 struct ring_buffer *buffer;
1702 unsigned long flags;
1703
1704 if (tracing_disabled)
1705 return;
1706
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001707 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1708 if (--global_trace.stop_count) {
1709 if (global_trace.stop_count < 0) {
Steven Rostedtb06a8302009-01-22 14:26:15 -05001710 /* Someone screwed up their debugging */
1711 WARN_ON_ONCE(1);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001712 global_trace.stop_count = 0;
Steven Rostedtb06a8302009-01-22 14:26:15 -05001713 }
Steven Rostedt0f048702008-11-05 16:05:44 -05001714 goto out;
1715 }
1716
Steven Rostedta2f80712010-03-12 19:56:00 -05001717 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001718 arch_spin_lock(&global_trace.max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -05001719
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001720 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001721 if (buffer)
1722 ring_buffer_record_enable(buffer);
1723
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001724#ifdef CONFIG_TRACER_MAX_TRACE
1725 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001726 if (buffer)
1727 ring_buffer_record_enable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001728#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001729
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001730 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001731
Steven Rostedt0f048702008-11-05 16:05:44 -05001732 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001733 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1734}
1735
1736static void tracing_start_tr(struct trace_array *tr)
1737{
1738 struct ring_buffer *buffer;
1739 unsigned long flags;
1740
1741 if (tracing_disabled)
1742 return;
1743
1744 /* If global, we need to also start the max tracer */
1745 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1746 return tracing_start();
1747
1748 raw_spin_lock_irqsave(&tr->start_lock, flags);
1749
1750 if (--tr->stop_count) {
1751 if (tr->stop_count < 0) {
1752 /* Someone screwed up their debugging */
1753 WARN_ON_ONCE(1);
1754 tr->stop_count = 0;
1755 }
1756 goto out;
1757 }
1758
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001759 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001760 if (buffer)
1761 ring_buffer_record_enable(buffer);
1762
1763 out:
1764 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001765}
1766
1767/**
1768 * tracing_stop - quick stop of the tracer
1769 *
1770 * Light weight way to stop tracing. Use in conjunction with
1771 * tracing_start.
1772 */
1773void tracing_stop(void)
1774{
1775 struct ring_buffer *buffer;
1776 unsigned long flags;
1777
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001778 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1779 if (global_trace.stop_count++)
Steven Rostedt0f048702008-11-05 16:05:44 -05001780 goto out;
1781
Steven Rostedta2f80712010-03-12 19:56:00 -05001782 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001783 arch_spin_lock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001784
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001785 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001786 if (buffer)
1787 ring_buffer_record_disable(buffer);
1788
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001789#ifdef CONFIG_TRACER_MAX_TRACE
1790 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001791 if (buffer)
1792 ring_buffer_record_disable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001793#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001794
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001795 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001796
Steven Rostedt0f048702008-11-05 16:05:44 -05001797 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001798 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1799}
1800
1801static void tracing_stop_tr(struct trace_array *tr)
1802{
1803 struct ring_buffer *buffer;
1804 unsigned long flags;
1805
1806 /* If global, we need to also stop the max tracer */
1807 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1808 return tracing_stop();
1809
1810 raw_spin_lock_irqsave(&tr->start_lock, flags);
1811 if (tr->stop_count++)
1812 goto out;
1813
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001814 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001815 if (buffer)
1816 ring_buffer_record_disable(buffer);
1817
1818 out:
1819 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001820}
1821
Ingo Molnare309b412008-05-12 21:20:51 +02001822void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001823
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001824static int trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001825{
Carsten Emdea635cf02009-03-18 09:00:41 +01001826 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001827
1828 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001829 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001830
Adrian Salido50b2d862017-04-18 11:44:33 -07001831 preempt_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001832 /*
1833 * It's not the end of the world if we don't get
1834 * the lock, but we also don't want to spin
1835 * nor do we want to disable interrupts,
1836 * so if we miss here, then better luck next time.
1837 */
Adrian Salido50b2d862017-04-18 11:44:33 -07001838 if (!arch_spin_trylock(&trace_cmdline_lock)) {
1839 preempt_enable();
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001840 return 0;
Adrian Salido50b2d862017-04-18 11:44:33 -07001841 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001842
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001843 idx = savedcmd->map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001844 if (idx == NO_CMDLINE_MAP) {
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001845 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001846
Carsten Emdea635cf02009-03-18 09:00:41 +01001847 /*
1848 * Check whether the cmdline buffer at idx has a pid
1849 * mapped. We are going to overwrite that entry so we
1850 * need to clear the map_pid_to_cmdline. Otherwise we
1851 * would read the new comm for the old pid.
1852 */
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001853 pid = savedcmd->map_cmdline_to_pid[idx];
Carsten Emdea635cf02009-03-18 09:00:41 +01001854 if (pid != NO_CMDLINE_MAP)
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001855 savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001856
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001857 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
1858 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001859
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001860 savedcmd->cmdline_idx = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001861 }
1862
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001863 set_cmdline(idx, tsk->comm);
Adrian Salido50b2d862017-04-18 11:44:33 -07001864 savedcmd->map_cmdline_to_tgid[idx] = tsk->tgid;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001865 arch_spin_unlock(&trace_cmdline_lock);
Adrian Salido50b2d862017-04-18 11:44:33 -07001866 preempt_enable();
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001867
1868 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001869}
1870
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04001871static void __trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001872{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001873 unsigned map;
1874
Steven Rostedt4ca53082009-03-16 19:20:15 -04001875 if (!pid) {
1876 strcpy(comm, "<idle>");
1877 return;
1878 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001879
Steven Rostedt74bf4072010-01-25 15:11:53 -05001880 if (WARN_ON_ONCE(pid < 0)) {
1881 strcpy(comm, "<XXX>");
1882 return;
1883 }
1884
Steven Rostedt4ca53082009-03-16 19:20:15 -04001885 if (pid > PID_MAX_DEFAULT) {
1886 strcpy(comm, "<...>");
1887 return;
1888 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001889
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001890 map = savedcmd->map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01001891 if (map != NO_CMDLINE_MAP)
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001892 strcpy(comm, get_saved_cmdlines(map));
Thomas Gleixner50d88752009-03-18 08:58:44 +01001893 else
1894 strcpy(comm, "<...>");
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04001895}
1896
1897void trace_find_cmdline(int pid, char comm[])
1898{
1899 preempt_disable();
1900 arch_spin_lock(&trace_cmdline_lock);
1901
1902 __trace_find_cmdline(pid, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001903
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001904 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001905 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001906}
1907
Adrian Salido50b2d862017-04-18 11:44:33 -07001908static int __find_tgid_locked(int pid)
Jamie Gennis13b625d2012-11-21 15:04:25 -08001909{
1910 unsigned map;
1911 int tgid;
1912
Dmitry Shmidtcb575f72015-10-28 10:45:04 -07001913 map = savedcmd->map_pid_to_cmdline[pid];
Jamie Gennis13b625d2012-11-21 15:04:25 -08001914 if (map != NO_CMDLINE_MAP)
Adrian Salido50b2d862017-04-18 11:44:33 -07001915 tgid = savedcmd->map_cmdline_to_tgid[map];
Jamie Gennis13b625d2012-11-21 15:04:25 -08001916 else
1917 tgid = -1;
1918
Adrian Salido50b2d862017-04-18 11:44:33 -07001919 return tgid;
1920}
1921
1922int trace_find_tgid(int pid)
1923{
1924 int tgid;
1925
1926 preempt_disable();
1927 arch_spin_lock(&trace_cmdline_lock);
1928
1929 tgid = __find_tgid_locked(pid);
1930
Jamie Gennis13b625d2012-11-21 15:04:25 -08001931 arch_spin_unlock(&trace_cmdline_lock);
1932 preempt_enable();
1933
1934 return tgid;
1935}
1936
Ingo Molnare309b412008-05-12 21:20:51 +02001937void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001938{
Steven Rostedt0fb96562012-05-11 14:25:30 -04001939 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001940 return;
1941
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001942 if (!__this_cpu_read(trace_cmdline_save))
1943 return;
1944
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001945 if (trace_save_cmdline(tsk))
1946 __this_cpu_write(trace_cmdline_save, false);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001947}
1948
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03001949void
Steven Rostedt38697052008-10-01 13:14:09 -04001950tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1951 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001952{
1953 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001954
Steven Rostedt777e2082008-09-29 23:02:42 -04001955 entry->preempt_count = pc & 0xff;
1956 entry->pid = (tsk) ? tsk->pid : 0;
1957 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04001958#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001959 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04001960#else
1961 TRACE_FLAG_IRQS_NOSUPPORT |
1962#endif
Peter Zijlstra7e6867b2016-03-18 16:28:04 +01001963 ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001964 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
Pavankumar Kondeti04e002a2016-12-09 21:50:17 +05301965 ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
Peter Zijlstrae5137b52013-10-04 17:28:26 +02001966 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
1967 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001968}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02001969EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001970
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04001971static __always_inline void
1972trace_event_setup(struct ring_buffer_event *event,
1973 int type, unsigned long flags, int pc)
1974{
1975 struct trace_entry *ent = ring_buffer_event_data(event);
1976
1977 tracing_generic_entry_update(ent, flags, pc);
1978 ent->type = type;
1979}
1980
Steven Rostedte77405a2009-09-02 14:17:06 -04001981struct ring_buffer_event *
1982trace_buffer_lock_reserve(struct ring_buffer *buffer,
1983 int type,
1984 unsigned long len,
1985 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001986{
1987 struct ring_buffer_event *event;
1988
Steven Rostedte77405a2009-09-02 14:17:06 -04001989 event = ring_buffer_lock_reserve(buffer, len);
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04001990 if (event != NULL)
1991 trace_event_setup(event, type, flags, pc);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001992
1993 return event;
1994}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001995
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04001996DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
1997DEFINE_PER_CPU(int, trace_buffered_event_cnt);
1998static int trace_buffered_event_ref;
1999
2000/**
2001 * trace_buffered_event_enable - enable buffering events
2002 *
2003 * When events are being filtered, it is quicker to use a temporary
2004 * buffer to write the event data into if there's a likely chance
2005 * that it will not be committed. The discard of the ring buffer
2006 * is not as fast as committing, and is much slower than copying
2007 * a commit.
2008 *
2009 * When an event is to be filtered, allocate per cpu buffers to
2010 * write the event data into, and if the event is filtered and discarded
2011 * it is simply dropped, otherwise, the entire data is to be committed
2012 * in one shot.
2013 */
2014void trace_buffered_event_enable(void)
2015{
2016 struct ring_buffer_event *event;
2017 struct page *page;
2018 int cpu;
2019
2020 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2021
2022 if (trace_buffered_event_ref++)
2023 return;
2024
2025 for_each_tracing_cpu(cpu) {
2026 page = alloc_pages_node(cpu_to_node(cpu),
2027 GFP_KERNEL | __GFP_NORETRY, 0);
2028 if (!page)
2029 goto failed;
2030
2031 event = page_address(page);
2032 memset(event, 0, sizeof(*event));
2033
2034 per_cpu(trace_buffered_event, cpu) = event;
2035
2036 preempt_disable();
2037 if (cpu == smp_processor_id() &&
2038 this_cpu_read(trace_buffered_event) !=
2039 per_cpu(trace_buffered_event, cpu))
2040 WARN_ON_ONCE(1);
2041 preempt_enable();
2042 }
2043
2044 return;
2045 failed:
2046 trace_buffered_event_disable();
2047}
2048
2049static void enable_trace_buffered_event(void *data)
2050{
2051 /* Probably not needed, but do it anyway */
2052 smp_rmb();
2053 this_cpu_dec(trace_buffered_event_cnt);
2054}
2055
2056static void disable_trace_buffered_event(void *data)
2057{
2058 this_cpu_inc(trace_buffered_event_cnt);
2059}
2060
2061/**
2062 * trace_buffered_event_disable - disable buffering events
2063 *
2064 * When a filter is removed, it is faster to not use the buffered
2065 * events, and to commit directly into the ring buffer. Free up
2066 * the temp buffers when there are no more users. This requires
2067 * special synchronization with current events.
2068 */
2069void trace_buffered_event_disable(void)
2070{
2071 int cpu;
2072
2073 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2074
2075 if (WARN_ON_ONCE(!trace_buffered_event_ref))
2076 return;
2077
2078 if (--trace_buffered_event_ref)
2079 return;
2080
2081 preempt_disable();
2082 /* For each CPU, set the buffer as used. */
2083 smp_call_function_many(tracing_buffer_mask,
2084 disable_trace_buffered_event, NULL, 1);
2085 preempt_enable();
2086
2087 /* Wait for all current users to finish */
2088 synchronize_sched();
2089
2090 for_each_tracing_cpu(cpu) {
2091 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2092 per_cpu(trace_buffered_event, cpu) = NULL;
2093 }
2094 /*
2095 * Make sure trace_buffered_event is NULL before clearing
2096 * trace_buffered_event_cnt.
2097 */
2098 smp_wmb();
2099
2100 preempt_disable();
2101 /* Do the work on each cpu */
2102 smp_call_function_many(tracing_buffer_mask,
2103 enable_trace_buffered_event, NULL, 1);
2104 preempt_enable();
2105}
2106
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002107void
2108__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
2109{
2110 __this_cpu_write(trace_cmdline_save, true);
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002111
2112 /* If this is the temp buffer, we need to commit fully */
2113 if (this_cpu_read(trace_buffered_event) == event) {
2114 /* Length is in event->array[0] */
2115 ring_buffer_write(buffer, event->array[0], &event->array[1]);
2116 /* Release the temp buffer */
2117 this_cpu_dec(trace_buffered_event_cnt);
2118 } else
2119 ring_buffer_unlock_commit(buffer, event);
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002120}
2121
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002122static struct ring_buffer *temp_buffer;
2123
Steven Rostedtef5580d2009-02-27 19:38:04 -05002124struct ring_buffer_event *
Steven Rostedtccb469a2012-08-02 10:32:10 -04002125trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002126 struct trace_event_file *trace_file,
Steven Rostedtccb469a2012-08-02 10:32:10 -04002127 int type, unsigned long len,
2128 unsigned long flags, int pc)
2129{
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002130 struct ring_buffer_event *entry;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002131 int val;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002132
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002133 *current_rb = trace_file->tr->trace_buffer.buffer;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002134
2135 if ((trace_file->flags &
2136 (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
2137 (entry = this_cpu_read(trace_buffered_event))) {
2138 /* Try to use the per cpu buffer first */
2139 val = this_cpu_inc_return(trace_buffered_event_cnt);
Steven Rostedt (VMware)2e584b12021-02-10 11:53:22 -05002140 if ((len < (PAGE_SIZE - sizeof(*entry))) && val == 1) {
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002141 trace_event_setup(entry, type, flags, pc);
2142 entry->array[0] = len;
2143 return entry;
2144 }
2145 this_cpu_dec(trace_buffered_event_cnt);
2146 }
2147
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002148 entry = trace_buffer_lock_reserve(*current_rb,
Steven Rostedtccb469a2012-08-02 10:32:10 -04002149 type, len, flags, pc);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002150 /*
2151 * If tracing is off, but we have triggers enabled
2152 * we still need to look at the event data. Use the temp_buffer
2153 * to store the trace event for the tigger to use. It's recusive
2154 * safe and will not be recorded anywhere.
2155 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002156 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002157 *current_rb = temp_buffer;
2158 entry = trace_buffer_lock_reserve(*current_rb,
2159 type, len, flags, pc);
2160 }
2161 return entry;
Steven Rostedtccb469a2012-08-02 10:32:10 -04002162}
2163EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2164
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04002165void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2166 struct ring_buffer *buffer,
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04002167 struct ring_buffer_event *event,
2168 unsigned long flags, int pc,
2169 struct pt_regs *regs)
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002170{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002171 __buffer_unlock_commit(buffer, event);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002172
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002173 /*
2174 * If regs is not set, then skip the following callers:
2175 * trace_buffer_unlock_commit_regs
2176 * event_trigger_unlock_commit
2177 * trace_event_buffer_commit
2178 * trace_event_raw_event_sched_switch
2179 * Note, we can still get here via blktrace, wakeup tracer
2180 * and mmiotrace, but that's ok if they lose a function or
2181 * two. They are that meaningful.
2182 */
2183 ftrace_trace_stack(tr, buffer, flags, regs ? 0 : 4, pc, regs);
Steven Rostedt (VMware)0bde9d52020-12-04 16:36:16 -05002184 ftrace_trace_userstack(tr, buffer, flags, pc);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002185}
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002186
Ingo Molnare309b412008-05-12 21:20:51 +02002187void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05002188trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04002189 unsigned long ip, unsigned long parent_ip, unsigned long flags,
2190 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002191{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002192 struct trace_event_call *call = &event_function;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002193 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002194 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04002195 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002196
Steven Rostedte77405a2009-09-02 14:17:06 -04002197 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002198 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002199 if (!event)
2200 return;
2201 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04002202 entry->ip = ip;
2203 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05002204
Tom Zanussif306cc82013-10-24 08:34:17 -05002205 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002206 __buffer_unlock_commit(buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002207}
2208
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002209#ifdef CONFIG_STACKTRACE
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002210
2211#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
2212struct ftrace_stack {
2213 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
2214};
2215
2216static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
2217static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2218
Steven Rostedte77405a2009-09-02 14:17:06 -04002219static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05002220 unsigned long flags,
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002221 int skip, int pc, struct pt_regs *regs)
Ingo Molnar86387f72008-05-12 21:20:51 +02002222{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002223 struct trace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002224 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04002225 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02002226 struct stack_trace trace;
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002227 int use_stack;
2228 int size = FTRACE_STACK_ENTRIES;
Ingo Molnar86387f72008-05-12 21:20:51 +02002229
2230 trace.nr_entries = 0;
Ingo Molnar86387f72008-05-12 21:20:51 +02002231 trace.skip = skip;
Ingo Molnar86387f72008-05-12 21:20:51 +02002232
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002233 /*
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002234 * Add two, for this function and the call to save_stack_trace()
2235 * If regs is set, then these functions will not be in the way.
2236 */
2237 if (!regs)
2238 trace.skip += 2;
2239
2240 /*
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002241 * Since events can happen in NMIs there's no safe way to
2242 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2243 * or NMI comes in, it will just have to use the default
2244 * FTRACE_STACK_SIZE.
2245 */
2246 preempt_disable_notrace();
2247
Shan Wei82146522012-11-19 13:21:01 +08002248 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002249 /*
2250 * We don't need any atomic variables, just a barrier.
2251 * If an interrupt comes in, we don't care, because it would
2252 * have exited and put the counter back to what we want.
2253 * We just need a barrier to keep gcc from moving things
2254 * around.
2255 */
2256 barrier();
2257 if (use_stack == 1) {
Christoph Lameterbdffd892014-04-29 14:17:40 -05002258 trace.entries = this_cpu_ptr(ftrace_stack.calls);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002259 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
2260
2261 if (regs)
2262 save_stack_trace_regs(regs, &trace);
2263 else
2264 save_stack_trace(&trace);
2265
2266 if (trace.nr_entries > size)
2267 size = trace.nr_entries;
2268 } else
2269 /* From now on, use_stack is a boolean */
2270 use_stack = 0;
2271
2272 size *= sizeof(unsigned long);
2273
2274 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
Steven Rostedt (VMware)5a046202021-04-01 13:54:40 -04002275 (sizeof(*entry) - sizeof(entry->caller)) + size,
2276 flags, pc);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002277 if (!event)
2278 goto out;
2279 entry = ring_buffer_event_data(event);
2280
2281 memset(&entry->caller, 0, size);
2282
2283 if (use_stack)
2284 memcpy(&entry->caller, trace.entries,
2285 trace.nr_entries * sizeof(unsigned long));
2286 else {
2287 trace.max_entries = FTRACE_STACK_ENTRIES;
2288 trace.entries = entry->caller;
2289 if (regs)
2290 save_stack_trace_regs(regs, &trace);
2291 else
2292 save_stack_trace(&trace);
2293 }
2294
2295 entry->size = trace.nr_entries;
2296
Tom Zanussif306cc82013-10-24 08:34:17 -05002297 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002298 __buffer_unlock_commit(buffer, event);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002299
2300 out:
2301 /* Again, don't let gcc optimize things here */
2302 barrier();
Shan Wei82146522012-11-19 13:21:01 +08002303 __this_cpu_dec(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002304 preempt_enable_notrace();
2305
Ingo Molnarf0a920d2008-05-12 21:20:47 +02002306}
2307
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002308static inline void ftrace_trace_stack(struct trace_array *tr,
2309 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002310 unsigned long flags,
2311 int skip, int pc, struct pt_regs *regs)
Steven Rostedt53614992009-01-15 19:12:40 -05002312{
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002313 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
Steven Rostedt53614992009-01-15 19:12:40 -05002314 return;
2315
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002316 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
Steven Rostedt53614992009-01-15 19:12:40 -05002317}
2318
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002319void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
2320 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04002321{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002322 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
Steven Rostedt38697052008-10-01 13:14:09 -04002323}
2324
Steven Rostedt03889382009-12-11 09:48:22 -05002325/**
2326 * trace_dump_stack - record a stack back trace in the trace buffer
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002327 * @skip: Number of functions to skip (helper handlers)
Steven Rostedt03889382009-12-11 09:48:22 -05002328 */
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002329void trace_dump_stack(int skip)
Steven Rostedt03889382009-12-11 09:48:22 -05002330{
2331 unsigned long flags;
2332
2333 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05002334 return;
Steven Rostedt03889382009-12-11 09:48:22 -05002335
2336 local_save_flags(flags);
2337
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002338 /*
2339 * Skip 3 more, seems to get us at the caller of
2340 * this function.
2341 */
2342 skip += 3;
2343 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
2344 flags, skip, preempt_count(), NULL);
Steven Rostedt03889382009-12-11 09:48:22 -05002345}
2346
Steven Rostedt91e86e52010-11-10 12:56:12 +01002347static DEFINE_PER_CPU(int, user_stack_count);
2348
Steven Rostedte77405a2009-09-02 14:17:06 -04002349void
Steven Rostedt (VMware)0bde9d52020-12-04 16:36:16 -05002350ftrace_trace_userstack(struct trace_array *tr,
2351 struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02002352{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002353 struct trace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02002354 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02002355 struct userstack_entry *entry;
2356 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02002357
Steven Rostedt (VMware)0bde9d52020-12-04 16:36:16 -05002358 if (!(tr->trace_flags & TRACE_ITER_USERSTACKTRACE))
Török Edwin02b67512008-11-22 13:28:47 +02002359 return;
2360
Steven Rostedtb6345872010-03-12 20:03:30 -05002361 /*
2362 * NMIs can not handle page faults, even with fix ups.
2363 * The save user stack can (and often does) fault.
2364 */
2365 if (unlikely(in_nmi()))
2366 return;
2367
Steven Rostedt91e86e52010-11-10 12:56:12 +01002368 /*
2369 * prevent recursion, since the user stack tracing may
2370 * trigger other kernel events.
2371 */
2372 preempt_disable();
2373 if (__this_cpu_read(user_stack_count))
2374 goto out;
2375
2376 __this_cpu_inc(user_stack_count);
2377
Steven Rostedte77405a2009-09-02 14:17:06 -04002378 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002379 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02002380 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08002381 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02002382 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02002383
Steven Rostedt48659d32009-09-11 11:36:23 -04002384 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02002385 memset(&entry->caller, 0, sizeof(entry->caller));
2386
2387 trace.nr_entries = 0;
2388 trace.max_entries = FTRACE_STACK_ENTRIES;
2389 trace.skip = 0;
2390 trace.entries = entry->caller;
2391
2392 save_stack_trace_user(&trace);
Tom Zanussif306cc82013-10-24 08:34:17 -05002393 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002394 __buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01002395
Li Zefan1dbd1952010-12-09 15:47:56 +08002396 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01002397 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01002398 out:
2399 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02002400}
2401
Hannes Eder4fd27352009-02-10 19:44:12 +01002402#ifdef UNUSED
2403static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02002404{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05002405 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02002406}
Hannes Eder4fd27352009-02-10 19:44:12 +01002407#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02002408
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002409#endif /* CONFIG_STACKTRACE */
2410
Steven Rostedt07d777f2011-09-22 14:01:55 -04002411/* created for use with alloc_percpu */
2412struct trace_buffer_struct {
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002413 int nesting;
2414 char buffer[4][TRACE_BUF_SIZE];
Steven Rostedt07d777f2011-09-22 14:01:55 -04002415};
2416
2417static struct trace_buffer_struct *trace_percpu_buffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002418
2419/*
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002420 * Thise allows for lockless recording. If we're nested too deeply, then
2421 * this returns NULL.
Steven Rostedt07d777f2011-09-22 14:01:55 -04002422 */
2423static char *get_trace_buf(void)
2424{
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002425 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
Steven Rostedt07d777f2011-09-22 14:01:55 -04002426
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002427 if (!buffer || buffer->nesting >= 4)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002428 return NULL;
2429
Steven Rostedt (VMware)96cf9182017-09-05 11:32:01 -04002430 buffer->nesting++;
2431
2432 /* Interrupts must see nesting incremented before we use the buffer */
2433 barrier();
Qiujun Huangb1343202020-10-30 00:19:05 +08002434 return &buffer->buffer[buffer->nesting - 1][0];
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002435}
2436
2437static void put_trace_buf(void)
2438{
Steven Rostedt (VMware)96cf9182017-09-05 11:32:01 -04002439 /* Don't let the decrement of nesting leak before this */
2440 barrier();
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002441 this_cpu_dec(trace_percpu_buffer->nesting);
Steven Rostedt07d777f2011-09-22 14:01:55 -04002442}
2443
2444static int alloc_percpu_trace_buffer(void)
2445{
2446 struct trace_buffer_struct *buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002447
2448 buffers = alloc_percpu(struct trace_buffer_struct);
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002449 if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
2450 return -ENOMEM;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002451
2452 trace_percpu_buffer = buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002453 return 0;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002454}
2455
Steven Rostedt81698832012-10-11 10:15:05 -04002456static int buffers_allocated;
2457
Steven Rostedt07d777f2011-09-22 14:01:55 -04002458void trace_printk_init_buffers(void)
2459{
Steven Rostedt07d777f2011-09-22 14:01:55 -04002460 if (buffers_allocated)
2461 return;
2462
2463 if (alloc_percpu_trace_buffer())
2464 return;
2465
Steven Rostedt2184db42014-05-28 13:14:40 -04002466 /* trace_printk() is for debug use only. Don't use it in production. */
2467
Joe Perchesa395d6a2016-03-22 14:28:09 -07002468 pr_warn("\n");
2469 pr_warn("**********************************************************\n");
2470 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2471 pr_warn("** **\n");
2472 pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
2473 pr_warn("** **\n");
2474 pr_warn("** This means that this is a DEBUG kernel and it is **\n");
2475 pr_warn("** unsafe for production use. **\n");
2476 pr_warn("** **\n");
2477 pr_warn("** If you see this message and you are not debugging **\n");
2478 pr_warn("** the kernel, report this immediately to your vendor! **\n");
2479 pr_warn("** **\n");
2480 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2481 pr_warn("**********************************************************\n");
Steven Rostedt07d777f2011-09-22 14:01:55 -04002482
Steven Rostedtb382ede62012-10-10 21:44:34 -04002483 /* Expand the buffers to set size */
2484 tracing_update_buffers();
2485
Steven Rostedt07d777f2011-09-22 14:01:55 -04002486 buffers_allocated = 1;
Steven Rostedt81698832012-10-11 10:15:05 -04002487
2488 /*
2489 * trace_printk_init_buffers() can be called by modules.
2490 * If that happens, then we need to start cmdline recording
2491 * directly here. If the global_trace.buffer is already
2492 * allocated here, then this was called by module code.
2493 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002494 if (global_trace.trace_buffer.buffer)
Steven Rostedt81698832012-10-11 10:15:05 -04002495 tracing_start_cmdline_record();
2496}
2497
2498void trace_printk_start_comm(void)
2499{
2500 /* Start tracing comms if trace printk is set */
2501 if (!buffers_allocated)
2502 return;
2503 tracing_start_cmdline_record();
2504}
2505
2506static void trace_printk_start_stop_comm(int enabled)
2507{
2508 if (!buffers_allocated)
2509 return;
2510
2511 if (enabled)
2512 tracing_start_cmdline_record();
2513 else
2514 tracing_stop_cmdline_record();
Steven Rostedt07d777f2011-09-22 14:01:55 -04002515}
2516
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002517/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002518 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002519 *
2520 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04002521int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002522{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002523 struct trace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002524 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04002525 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002526 struct trace_array *tr = &global_trace;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002527 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002528 unsigned long flags;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002529 char *tbuffer;
2530 int len = 0, size, pc;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002531
2532 if (unlikely(tracing_selftest_running || tracing_disabled))
2533 return 0;
2534
2535 /* Don't pollute graph traces with trace_vprintk internals */
2536 pause_graph_tracing();
2537
2538 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04002539 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002540
Steven Rostedt07d777f2011-09-22 14:01:55 -04002541 tbuffer = get_trace_buf();
2542 if (!tbuffer) {
2543 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002544 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002545 }
2546
2547 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
2548
2549 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002550 goto out;
2551
Steven Rostedt07d777f2011-09-22 14:01:55 -04002552 local_save_flags(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002553 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002554 buffer = tr->trace_buffer.buffer;
Steven Rostedte77405a2009-09-02 14:17:06 -04002555 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2556 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002557 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002558 goto out;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002559 entry = ring_buffer_event_data(event);
2560 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002561 entry->fmt = fmt;
2562
Steven Rostedt07d777f2011-09-22 14:01:55 -04002563 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
Tom Zanussif306cc82013-10-24 08:34:17 -05002564 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002565 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002566 ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05002567 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002568
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002569out:
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002570 put_trace_buf();
2571
2572out_nobuffer:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002573 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002574 unpause_graph_tracing();
2575
2576 return len;
2577}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002578EXPORT_SYMBOL_GPL(trace_vbprintk);
2579
Mathieu Malaterrebca139f2018-03-08 21:58:43 +01002580__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002581static int
2582__trace_array_vprintk(struct ring_buffer *buffer,
2583 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002584{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002585 struct trace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002586 struct ring_buffer_event *event;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002587 int len = 0, size, pc;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002588 struct print_entry *entry;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002589 unsigned long flags;
2590 char *tbuffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002591
2592 if (tracing_disabled || tracing_selftest_running)
2593 return 0;
2594
Steven Rostedt07d777f2011-09-22 14:01:55 -04002595 /* Don't pollute graph traces with trace_vprintk internals */
2596 pause_graph_tracing();
2597
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002598 pc = preempt_count();
2599 preempt_disable_notrace();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002600
Steven Rostedt07d777f2011-09-22 14:01:55 -04002601
2602 tbuffer = get_trace_buf();
2603 if (!tbuffer) {
2604 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002605 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002606 }
2607
Dan Carpenter3558a5a2014-11-27 18:57:52 +03002608 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002609
Steven Rostedt07d777f2011-09-22 14:01:55 -04002610 local_save_flags(flags);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002611 size = sizeof(*entry) + len + 1;
Steven Rostedte77405a2009-09-02 14:17:06 -04002612 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
Steven Rostedt07d777f2011-09-22 14:01:55 -04002613 flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002614 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002615 goto out;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002616 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01002617 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002618
Dan Carpenter3558a5a2014-11-27 18:57:52 +03002619 memcpy(&entry->buf, tbuffer, len + 1);
Tom Zanussif306cc82013-10-24 08:34:17 -05002620 if (!call_filter_check_discard(call, entry, buffer, event)) {
Shashank Mittal43beb422016-05-20 13:06:09 -07002621 stm_log(OST_ENTITY_TRACE_PRINTK, entry->buf, len + 1);
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002622 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002623 ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05002624 }
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002625
2626out:
2627 put_trace_buf();
2628
2629out_nobuffer:
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002630 preempt_enable_notrace();
Steven Rostedt07d777f2011-09-22 14:01:55 -04002631 unpause_graph_tracing();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002632
2633 return len;
2634}
Steven Rostedt659372d2009-09-03 19:11:07 -04002635
Mathieu Malaterrebca139f2018-03-08 21:58:43 +01002636__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002637int trace_array_vprintk(struct trace_array *tr,
2638 unsigned long ip, const char *fmt, va_list args)
2639{
2640 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2641}
2642
Mathieu Malaterrebca139f2018-03-08 21:58:43 +01002643__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002644int trace_array_printk(struct trace_array *tr,
2645 unsigned long ip, const char *fmt, ...)
2646{
2647 int ret;
2648 va_list ap;
2649
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04002650 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002651 return 0;
2652
Divya Indi6f46c4f2019-08-14 10:55:25 -07002653 if (!tr)
2654 return -ENOENT;
2655
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002656 va_start(ap, fmt);
2657 ret = trace_array_vprintk(tr, ip, fmt, ap);
2658 va_end(ap);
2659 return ret;
2660}
2661
Mathieu Malaterrebca139f2018-03-08 21:58:43 +01002662__printf(3, 4)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002663int trace_array_printk_buf(struct ring_buffer *buffer,
2664 unsigned long ip, const char *fmt, ...)
2665{
2666 int ret;
2667 va_list ap;
2668
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04002669 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002670 return 0;
2671
2672 va_start(ap, fmt);
2673 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2674 va_end(ap);
2675 return ret;
2676}
2677
Mathieu Malaterrebca139f2018-03-08 21:58:43 +01002678__printf(2, 0)
Steven Rostedt659372d2009-09-03 19:11:07 -04002679int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2680{
Steven Rostedta813a152009-10-09 01:41:35 -04002681 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04002682}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002683EXPORT_SYMBOL_GPL(trace_vprintk);
2684
Robert Richtere2ac8ef2008-11-12 12:59:32 +01002685static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04002686{
Steven Rostedt6d158a82012-06-27 20:46:14 -04002687 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2688
Steven Rostedt5a90f572008-09-03 17:42:51 -04002689 iter->idx++;
Steven Rostedt6d158a82012-06-27 20:46:14 -04002690 if (buf_iter)
2691 ring_buffer_read(buf_iter, NULL);
Steven Rostedt5a90f572008-09-03 17:42:51 -04002692}
2693
Ingo Molnare309b412008-05-12 21:20:51 +02002694static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04002695peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2696 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002697{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002698 struct ring_buffer_event *event;
Steven Rostedt6d158a82012-06-27 20:46:14 -04002699 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002700
Steven Rostedtd7690412008-10-01 00:29:53 -04002701 if (buf_iter)
2702 event = ring_buffer_iter_peek(buf_iter, ts);
2703 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002704 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04002705 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04002706
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002707 if (event) {
2708 iter->ent_size = ring_buffer_event_length(event);
2709 return ring_buffer_event_data(event);
2710 }
2711 iter->ent_size = 0;
2712 return NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002713}
Steven Rostedtd7690412008-10-01 00:29:53 -04002714
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002715static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04002716__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2717 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002718{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002719 struct ring_buffer *buffer = iter->trace_buffer->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002720 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08002721 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002722 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002723 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002724 int next_cpu = -1;
Steven Rostedt12b5da32012-03-27 10:43:28 -04002725 int next_size = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002726 int cpu;
2727
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002728 /*
2729 * If we are in a per_cpu trace file, don't bother by iterating over
2730 * all cpu and peek directly.
2731 */
Steven Rostedtae3b5092013-01-23 15:22:59 -05002732 if (cpu_file > RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002733 if (ring_buffer_empty_cpu(buffer, cpu_file))
2734 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002735 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002736 if (ent_cpu)
2737 *ent_cpu = cpu_file;
2738
2739 return ent;
2740 }
2741
Steven Rostedtab464282008-05-12 21:21:00 +02002742 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002743
2744 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002745 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002746
Steven Rostedtbc21b472010-03-31 19:49:26 -04002747 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002748
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02002749 /*
2750 * Pick the entry with the smallest timestamp:
2751 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002752 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002753 next = ent;
2754 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002755 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002756 next_lost = lost_events;
Steven Rostedt12b5da32012-03-27 10:43:28 -04002757 next_size = iter->ent_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002758 }
2759 }
2760
Steven Rostedt12b5da32012-03-27 10:43:28 -04002761 iter->ent_size = next_size;
2762
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002763 if (ent_cpu)
2764 *ent_cpu = next_cpu;
2765
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002766 if (ent_ts)
2767 *ent_ts = next_ts;
2768
Steven Rostedtbc21b472010-03-31 19:49:26 -04002769 if (missing_events)
2770 *missing_events = next_lost;
2771
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002772 return next;
2773}
2774
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002775/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002776struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2777 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002778{
Steven Rostedtbc21b472010-03-31 19:49:26 -04002779 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002780}
Ingo Molnar8c523a92008-05-12 21:20:46 +02002781
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002782/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05002783void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002784{
Steven Rostedtbc21b472010-03-31 19:49:26 -04002785 iter->ent = __find_next_entry(iter, &iter->cpu,
2786 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002787
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002788 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01002789 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002790
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002791 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002792}
2793
Ingo Molnare309b412008-05-12 21:20:51 +02002794static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002795{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002796 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04002797 &iter->lost_events);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002798}
2799
Ingo Molnare309b412008-05-12 21:20:51 +02002800static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002801{
2802 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002803 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002804 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002805
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002806 WARN_ON_ONCE(iter->leftover);
2807
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002808 (*pos)++;
2809
2810 /* can't go backwards */
2811 if (iter->idx > i)
2812 return NULL;
2813
2814 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05002815 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002816 else
2817 ent = iter;
2818
2819 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05002820 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002821
2822 iter->pos = *pos;
2823
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002824 return ent;
2825}
2826
Jason Wessel955b61e2010-08-05 09:22:23 -05002827void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002828{
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002829 struct ring_buffer_event *event;
2830 struct ring_buffer_iter *buf_iter;
2831 unsigned long entries = 0;
2832 u64 ts;
2833
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002834 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002835
Steven Rostedt6d158a82012-06-27 20:46:14 -04002836 buf_iter = trace_buffer_iter(iter, cpu);
2837 if (!buf_iter)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002838 return;
2839
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002840 ring_buffer_iter_reset(buf_iter);
2841
2842 /*
2843 * We could have the case with the max latency tracers
2844 * that a reset never took place on a cpu. This is evident
2845 * by the timestamp being before the start of the buffer.
2846 */
2847 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002848 if (ts >= iter->trace_buffer->time_start)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002849 break;
2850 entries++;
2851 ring_buffer_read(buf_iter, NULL);
2852 }
2853
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002854 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002855}
2856
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002857/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002858 * The current tracer is copied to avoid a global locking
2859 * all around.
2860 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002861static void *s_start(struct seq_file *m, loff_t *pos)
2862{
2863 struct trace_iterator *iter = m->private;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002864 struct trace_array *tr = iter->tr;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002865 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002866 void *p = NULL;
2867 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002868 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002869
Hiraku Toyooka2fd196e2012-12-26 11:52:52 +09002870 /*
2871 * copy the tracer to avoid using a global lock all around.
2872 * iter->trace is a copy of current_trace, the pointer to the
2873 * name may be used instead of a strcmp(), as iter->trace->name
2874 * will point to the same string as current_trace->name.
2875 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002876 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002877 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2878 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002879 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002880
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002881#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002882 if (iter->snapshot && iter->trace->use_max_tr)
2883 return ERR_PTR(-EBUSY);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002884#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002885
2886 if (!iter->snapshot)
2887 atomic_inc(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002888
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002889 if (*pos != iter->pos) {
2890 iter->ent = NULL;
2891 iter->cpu = 0;
2892 iter->idx = -1;
2893
Steven Rostedtae3b5092013-01-23 15:22:59 -05002894 if (cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002895 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002896 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002897 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002898 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002899
Lai Jiangshanac91d852010-03-02 17:54:50 +08002900 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002901 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2902 ;
2903
2904 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002905 /*
2906 * If we overflowed the seq_file before, then we want
2907 * to just reuse the trace_seq buffer again.
2908 */
2909 if (iter->leftover)
2910 p = iter;
2911 else {
2912 l = *pos - 1;
2913 p = s_next(m, p, &l);
2914 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002915 }
2916
Lai Jiangshan4f535962009-05-18 19:35:34 +08002917 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002918 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002919 return p;
2920}
2921
2922static void s_stop(struct seq_file *m, void *p)
2923{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002924 struct trace_iterator *iter = m->private;
2925
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002926#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002927 if (iter->snapshot && iter->trace->use_max_tr)
2928 return;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002929#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002930
2931 if (!iter->snapshot)
2932 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002933
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002934 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08002935 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002936}
2937
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002938static void
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002939get_total_entries(struct trace_buffer *buf,
2940 unsigned long *total, unsigned long *entries)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002941{
2942 unsigned long count;
2943 int cpu;
2944
2945 *total = 0;
2946 *entries = 0;
2947
2948 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002949 count = ring_buffer_entries_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002950 /*
2951 * If this buffer has skipped entries, then we hold all
2952 * entries for the trace and we need to ignore the
2953 * ones before the time stamp.
2954 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002955 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2956 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002957 /* total is the same as the entries */
2958 *total += count;
2959 } else
2960 *total += count +
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002961 ring_buffer_overrun_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002962 *entries += count;
2963 }
2964}
2965
Ingo Molnare309b412008-05-12 21:20:51 +02002966static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002967{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002968 seq_puts(m, "# _------=> CPU# \n"
2969 "# / _-----=> irqs-off \n"
2970 "# | / _----=> need-resched \n"
2971 "# || / _---=> hardirq/softirq \n"
2972 "# ||| / _--=> preempt-depth \n"
2973 "# |||| / delay \n"
2974 "# cmd pid ||||| time | caller \n"
2975 "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002976}
2977
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002978static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002979{
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002980 unsigned long total;
2981 unsigned long entries;
2982
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002983 get_total_entries(buf, &total, &entries);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002984 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2985 entries, total, num_online_cpus());
2986 seq_puts(m, "#\n");
2987}
2988
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002989static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002990{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002991 print_event_info(buf, m);
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002992 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"
2993 "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002994}
2995
Jamie Gennis13b625d2012-11-21 15:04:25 -08002996static void print_func_help_header_tgid(struct trace_buffer *buf, struct seq_file *m)
2997{
2998 print_event_info(buf, m);
2999 seq_puts(m, "# TASK-PID TGID CPU# TIMESTAMP FUNCTION\n");
3000 seq_puts(m, "# | | | | | |\n");
3001}
3002
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003003static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedt77271ce2011-11-17 09:34:33 -05003004{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003005 print_event_info(buf, m);
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003006 seq_puts(m, "# _-----=> irqs-off\n"
3007 "# / _----=> need-resched\n"
3008 "# | / _---=> hardirq/softirq\n"
3009 "# || / _--=> preempt-depth\n"
3010 "# ||| / delay\n"
3011 "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
3012 "# | | | |||| | |\n");
Steven Rostedt77271ce2011-11-17 09:34:33 -05003013}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003014
Jamie Gennis13b625d2012-11-21 15:04:25 -08003015static void print_func_help_header_irq_tgid(struct trace_buffer *buf, struct seq_file *m)
3016{
3017 print_event_info(buf, m);
3018 seq_puts(m, "# _-----=> irqs-off\n");
3019 seq_puts(m, "# / _----=> need-resched\n");
3020 seq_puts(m, "# | / _---=> hardirq/softirq\n");
3021 seq_puts(m, "# || / _--=> preempt-depth\n");
3022 seq_puts(m, "# ||| / delay\n");
3023 seq_puts(m, "# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION\n");
3024 seq_puts(m, "# | | | | |||| | |\n");
3025}
3026
Jiri Olsa62b915f2010-04-02 19:01:22 +02003027void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003028print_trace_header(struct seq_file *m, struct trace_iterator *iter)
3029{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003030 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003031 struct trace_buffer *buf = iter->trace_buffer;
3032 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003033 struct tracer *type = iter->trace;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003034 unsigned long entries;
3035 unsigned long total;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003036 const char *name = "preemption";
3037
Steven Rostedt (Red Hat)d840f712013-02-01 18:38:47 -05003038 name = type->name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003039
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003040 get_total_entries(buf, &total, &entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003041
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003042 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003043 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003044 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003045 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003046 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003047 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02003048 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003049 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003050 total,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003051 buf->cpu,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003052#if defined(CONFIG_PREEMPT_NONE)
3053 "server",
3054#elif defined(CONFIG_PREEMPT_VOLUNTARY)
3055 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04003056#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003057 "preempt",
3058#else
3059 "unknown",
3060#endif
3061 /* These are reserved for later use */
3062 0, 0, 0, 0);
3063#ifdef CONFIG_SMP
3064 seq_printf(m, " #P:%d)\n", num_online_cpus());
3065#else
3066 seq_puts(m, ")\n");
3067#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003068 seq_puts(m, "# -----------------\n");
3069 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003070 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
Eric W. Biedermand20b92a2012-03-13 16:02:19 -07003071 data->comm, data->pid,
3072 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003073 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003074 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003075
3076 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003077 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02003078 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
3079 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003080 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02003081 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
3082 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04003083 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003084 }
3085
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003086 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003087}
3088
Steven Rostedta3097202008-11-07 22:36:02 -05003089static void test_cpu_buff_start(struct trace_iterator *iter)
3090{
3091 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003092 struct trace_array *tr = iter->tr;
Steven Rostedta3097202008-11-07 22:36:02 -05003093
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003094 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003095 return;
3096
3097 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
3098 return;
3099
Matthias Kaehlcke3341c562017-04-21 16:41:10 -07003100 if (cpumask_available(iter->started) &&
3101 cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05003102 return;
3103
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003104 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003105 return;
3106
Matthias Kaehlcke3341c562017-04-21 16:41:10 -07003107 if (cpumask_available(iter->started))
Sasha Levin919cd972015-09-04 12:45:56 -04003108 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003109
3110 /* Don't print started cpu buffer for the first entry of the trace */
3111 if (iter->idx > 1)
3112 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
3113 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05003114}
3115
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003116static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003117{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003118 struct trace_array *tr = iter->tr;
Steven Rostedt214023c2008-05-12 21:20:46 +02003119 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003120 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003121 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003122 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003123
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003124 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003125
Steven Rostedta3097202008-11-07 22:36:02 -05003126 test_cpu_buff_start(iter);
3127
Steven Rostedtf633cef2008-12-23 23:24:13 -05003128 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003129
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003130 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003131 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3132 trace_print_lat_context(iter);
3133 else
3134 trace_print_context(iter);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003135 }
3136
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003137 if (trace_seq_has_overflowed(s))
3138 return TRACE_TYPE_PARTIAL_LINE;
3139
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003140 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003141 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003142
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003143 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003144
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003145 return trace_handle_return(s);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003146}
3147
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003148static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003149{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003150 struct trace_array *tr = iter->tr;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003151 struct trace_seq *s = &iter->seq;
3152 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003153 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003154
3155 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003156
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003157 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003158 trace_seq_printf(s, "%d %d %llu ",
3159 entry->pid, iter->cpu, iter->ts);
3160
3161 if (trace_seq_has_overflowed(s))
3162 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003163
Steven Rostedtf633cef2008-12-23 23:24:13 -05003164 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003165 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003166 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003167
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003168 trace_seq_printf(s, "%d ?\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003169
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003170 return trace_handle_return(s);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003171}
3172
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003173static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003174{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003175 struct trace_array *tr = iter->tr;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003176 struct trace_seq *s = &iter->seq;
3177 unsigned char newline = '\n';
3178 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003179 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003180
3181 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003182
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003183 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003184 SEQ_PUT_HEX_FIELD(s, entry->pid);
3185 SEQ_PUT_HEX_FIELD(s, iter->cpu);
3186 SEQ_PUT_HEX_FIELD(s, iter->ts);
3187 if (trace_seq_has_overflowed(s))
3188 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003189 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003190
Steven Rostedtf633cef2008-12-23 23:24:13 -05003191 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003192 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04003193 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003194 if (ret != TRACE_TYPE_HANDLED)
3195 return ret;
3196 }
Steven Rostedt7104f302008-10-01 10:52:51 -04003197
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003198 SEQ_PUT_FIELD(s, newline);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003199
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003200 return trace_handle_return(s);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003201}
3202
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003203static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003204{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003205 struct trace_array *tr = iter->tr;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003206 struct trace_seq *s = &iter->seq;
3207 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003208 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003209
3210 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003211
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003212 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003213 SEQ_PUT_FIELD(s, entry->pid);
3214 SEQ_PUT_FIELD(s, iter->cpu);
3215 SEQ_PUT_FIELD(s, iter->ts);
3216 if (trace_seq_has_overflowed(s))
3217 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003218 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003219
Steven Rostedtf633cef2008-12-23 23:24:13 -05003220 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04003221 return event ? event->funcs->binary(iter, 0, event) :
3222 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003223}
3224
Jiri Olsa62b915f2010-04-02 19:01:22 +02003225int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003226{
Steven Rostedt6d158a82012-06-27 20:46:14 -04003227 struct ring_buffer_iter *buf_iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003228 int cpu;
3229
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003230 /* If we are looking at one CPU buffer, only check that one */
Steven Rostedtae3b5092013-01-23 15:22:59 -05003231 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003232 cpu = iter->cpu_file;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003233 buf_iter = trace_buffer_iter(iter, cpu);
3234 if (buf_iter) {
3235 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003236 return 0;
3237 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003238 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003239 return 0;
3240 }
3241 return 1;
3242 }
3243
Steven Rostedtab464282008-05-12 21:21:00 +02003244 for_each_tracing_cpu(cpu) {
Steven Rostedt6d158a82012-06-27 20:46:14 -04003245 buf_iter = trace_buffer_iter(iter, cpu);
3246 if (buf_iter) {
3247 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedtd7690412008-10-01 00:29:53 -04003248 return 0;
3249 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003250 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedtd7690412008-10-01 00:29:53 -04003251 return 0;
3252 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003253 }
Steven Rostedtd7690412008-10-01 00:29:53 -04003254
Frederic Weisbecker797d3712008-09-30 18:13:45 +02003255 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003256}
3257
Lai Jiangshan4f535962009-05-18 19:35:34 +08003258/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05003259enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003260{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003261 struct trace_array *tr = iter->tr;
3262 unsigned long trace_flags = tr->trace_flags;
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003263 enum print_line_t ret;
3264
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003265 if (iter->lost_events) {
3266 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3267 iter->cpu, iter->lost_events);
3268 if (trace_seq_has_overflowed(&iter->seq))
3269 return TRACE_TYPE_PARTIAL_LINE;
3270 }
Steven Rostedtbc21b472010-03-31 19:49:26 -04003271
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003272 if (iter->trace && iter->trace->print_line) {
3273 ret = iter->trace->print_line(iter);
3274 if (ret != TRACE_TYPE_UNHANDLED)
3275 return ret;
3276 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02003277
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -05003278 if (iter->ent->type == TRACE_BPUTS &&
3279 trace_flags & TRACE_ITER_PRINTK &&
3280 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3281 return trace_print_bputs_msg_only(iter);
3282
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003283 if (iter->ent->type == TRACE_BPRINT &&
3284 trace_flags & TRACE_ITER_PRINTK &&
3285 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04003286 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003287
Frederic Weisbecker66896a82008-12-13 20:18:13 +01003288 if (iter->ent->type == TRACE_PRINT &&
3289 trace_flags & TRACE_ITER_PRINTK &&
3290 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04003291 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01003292
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003293 if (trace_flags & TRACE_ITER_BIN)
3294 return print_bin_fmt(iter);
3295
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003296 if (trace_flags & TRACE_ITER_HEX)
3297 return print_hex_fmt(iter);
3298
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003299 if (trace_flags & TRACE_ITER_RAW)
3300 return print_raw_fmt(iter);
3301
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003302 return print_trace_fmt(iter);
3303}
3304
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003305void trace_latency_header(struct seq_file *m)
3306{
3307 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003308 struct trace_array *tr = iter->tr;
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003309
3310 /* print nothing if the buffers are empty */
3311 if (trace_empty(iter))
3312 return;
3313
3314 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3315 print_trace_header(m, iter);
3316
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003317 if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003318 print_lat_help_header(m);
3319}
3320
Jiri Olsa62b915f2010-04-02 19:01:22 +02003321void trace_default_header(struct seq_file *m)
3322{
3323 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003324 struct trace_array *tr = iter->tr;
3325 unsigned long trace_flags = tr->trace_flags;
Jiri Olsa62b915f2010-04-02 19:01:22 +02003326
Jiri Olsaf56e7f82011-06-03 16:58:49 +02003327 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
3328 return;
3329
Jiri Olsa62b915f2010-04-02 19:01:22 +02003330 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
3331 /* print nothing if the buffers are empty */
3332 if (trace_empty(iter))
3333 return;
3334 print_trace_header(m, iter);
3335 if (!(trace_flags & TRACE_ITER_VERBOSE))
3336 print_lat_help_header(m);
3337 } else {
Steven Rostedt77271ce2011-11-17 09:34:33 -05003338 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
3339 if (trace_flags & TRACE_ITER_IRQ_INFO)
Jamie Gennis13b625d2012-11-21 15:04:25 -08003340 if (trace_flags & TRACE_ITER_TGID)
3341 print_func_help_header_irq_tgid(iter->trace_buffer, m);
3342 else
3343 print_func_help_header_irq(iter->trace_buffer, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05003344 else
Jamie Gennis13b625d2012-11-21 15:04:25 -08003345 if (trace_flags & TRACE_ITER_TGID)
3346 print_func_help_header_tgid(iter->trace_buffer, m);
3347 else
3348 print_func_help_header(iter->trace_buffer, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05003349 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02003350 }
3351}
3352
Steven Rostedte0a413f2011-09-29 21:26:16 -04003353static void test_ftrace_alive(struct seq_file *m)
3354{
3355 if (!ftrace_is_dead())
3356 return;
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003357 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
3358 "# MAY BE MISSING FUNCTION EVENTS\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04003359}
3360
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003361#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003362static void show_snapshot_main_help(struct seq_file *m)
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003363{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003364 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
3365 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3366 "# Takes a snapshot of the main buffer.\n"
3367 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
3368 "# (Doesn't have to be '2' works with any number that\n"
3369 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003370}
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003371
3372static void show_snapshot_percpu_help(struct seq_file *m)
3373{
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003374 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003375#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003376 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3377 "# Takes a snapshot of the main buffer for this cpu.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003378#else
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003379 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
3380 "# Must use main snapshot file to allocate.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003381#endif
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003382 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
3383 "# (Doesn't have to be '2' works with any number that\n"
3384 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003385}
3386
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003387static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
3388{
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05003389 if (iter->tr->allocated_snapshot)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003390 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003391 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003392 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003393
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003394 seq_puts(m, "# Snapshot commands:\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003395 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
3396 show_snapshot_main_help(m);
3397 else
3398 show_snapshot_percpu_help(m);
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003399}
3400#else
3401/* Should never be called */
3402static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
3403#endif
3404
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003405static int s_show(struct seq_file *m, void *v)
3406{
3407 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003408 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003409
3410 if (iter->ent == NULL) {
3411 if (iter->tr) {
3412 seq_printf(m, "# tracer: %s\n", iter->trace->name);
3413 seq_puts(m, "#\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04003414 test_ftrace_alive(m);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003415 }
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003416 if (iter->snapshot && trace_empty(iter))
3417 print_snapshot_help(m, iter);
3418 else if (iter->trace && iter->trace->print_header)
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003419 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02003420 else
3421 trace_default_header(m);
3422
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003423 } else if (iter->leftover) {
3424 /*
3425 * If we filled the seq_file buffer earlier, we
3426 * want to just show it now.
3427 */
3428 ret = trace_print_seq(m, &iter->seq);
3429
3430 /* ret should this time be zero, but you never know */
3431 iter->leftover = ret;
3432
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003433 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003434 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003435 ret = trace_print_seq(m, &iter->seq);
3436 /*
3437 * If we overflow the seq_file buffer, then it will
3438 * ask us for this data again at start up.
3439 * Use that instead.
3440 * ret is 0 if seq_file write succeeded.
3441 * -1 otherwise.
3442 */
3443 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003444 }
3445
3446 return 0;
3447}
3448
Oleg Nesterov649e9c72013-07-23 17:25:54 +02003449/*
3450 * Should be used after trace_array_get(), trace_types_lock
3451 * ensures that i_cdev was already initialized.
3452 */
3453static inline int tracing_get_cpu(struct inode *inode)
3454{
3455 if (inode->i_cdev) /* See trace_create_cpu_file() */
3456 return (long)inode->i_cdev - 1;
3457 return RING_BUFFER_ALL_CPUS;
3458}
3459
James Morris88e9d342009-09-22 16:43:43 -07003460static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003461 .start = s_start,
3462 .next = s_next,
3463 .stop = s_stop,
3464 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003465};
3466
Ingo Molnare309b412008-05-12 21:20:51 +02003467static struct trace_iterator *
Oleg Nesterov6484c712013-07-23 17:26:10 +02003468__tracing_open(struct inode *inode, struct file *file, bool snapshot)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003469{
Oleg Nesterov6484c712013-07-23 17:26:10 +02003470 struct trace_array *tr = inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003471 struct trace_iterator *iter;
Jiri Olsa50e18b92012-04-25 10:23:39 +02003472 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003473
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003474 if (tracing_disabled)
3475 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02003476
Jiri Olsa50e18b92012-04-25 10:23:39 +02003477 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003478 if (!iter)
3479 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003480
Gil Fruchter72917232015-06-09 10:32:35 +03003481 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
Steven Rostedt6d158a82012-06-27 20:46:14 -04003482 GFP_KERNEL);
Dan Carpenter93574fc2012-07-11 09:35:08 +03003483 if (!iter->buffer_iter)
3484 goto release;
3485
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003486 /*
3487 * We make a copy of the current tracer to avoid concurrent
3488 * changes on it while we are reading.
3489 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003490 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003491 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003492 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003493 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003494
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003495 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003496
Li Zefan79f55992009-06-15 14:58:26 +08003497 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003498 goto fail;
3499
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003500 iter->tr = tr;
3501
3502#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003503 /* Currently only the top directory has a snapshot */
3504 if (tr->current_trace->print_max || snapshot)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003505 iter->trace_buffer = &tr->max_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003506 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003507#endif
3508 iter->trace_buffer = &tr->trace_buffer;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003509 iter->snapshot = snapshot;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003510 iter->pos = -1;
Oleg Nesterov6484c712013-07-23 17:26:10 +02003511 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003512 mutex_init(&iter->mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003513
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003514 /* Notify the tracer early; before we stop tracing. */
3515 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01003516 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003517
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003518 /* Annotate start of buffers if we had overruns */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003519 if (ring_buffer_overruns(iter->trace_buffer->buffer))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003520 iter->iter_flags |= TRACE_FILE_ANNOTATE;
3521
David Sharp8be07092012-11-13 12:18:22 -08003522 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09003523 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08003524 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3525
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003526 /* stop the trace while dumping if we are not opening "snapshot" */
3527 if (!iter->snapshot)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003528 tracing_stop_tr(tr);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003529
Steven Rostedtae3b5092013-01-23 15:22:59 -05003530 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003531 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003532 iter->buffer_iter[cpu] =
Douglas Anderson3085d412019-03-08 11:32:04 -08003533 ring_buffer_read_prepare(iter->trace_buffer->buffer,
3534 cpu, GFP_KERNEL);
David Miller72c9ddf2010-04-20 15:47:11 -07003535 }
3536 ring_buffer_read_prepare_sync();
3537 for_each_tracing_cpu(cpu) {
3538 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003539 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003540 }
3541 } else {
3542 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003543 iter->buffer_iter[cpu] =
Douglas Anderson3085d412019-03-08 11:32:04 -08003544 ring_buffer_read_prepare(iter->trace_buffer->buffer,
3545 cpu, GFP_KERNEL);
David Miller72c9ddf2010-04-20 15:47:11 -07003546 ring_buffer_read_prepare_sync();
3547 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003548 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003549 }
3550
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003551 mutex_unlock(&trace_types_lock);
3552
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003553 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003554
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003555 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003556 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003557 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04003558 kfree(iter->buffer_iter);
Dan Carpenter93574fc2012-07-11 09:35:08 +03003559release:
Jiri Olsa50e18b92012-04-25 10:23:39 +02003560 seq_release_private(inode, file);
3561 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003562}
3563
3564int tracing_open_generic(struct inode *inode, struct file *filp)
3565{
Steven Rostedt60a11772008-05-12 21:20:44 +02003566 if (tracing_disabled)
3567 return -ENODEV;
3568
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003569 filp->private_data = inode->i_private;
3570 return 0;
3571}
3572
Geyslan G. Bem2e864212013-10-18 21:15:54 -03003573bool tracing_is_disabled(void)
3574{
3575 return (tracing_disabled) ? true: false;
3576}
3577
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003578/*
3579 * Open and update trace_array ref count.
3580 * Must have the current trace_array passed to it.
3581 */
Steven Rostedt (Red Hat)dcc30222013-07-02 20:30:52 -04003582static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003583{
3584 struct trace_array *tr = inode->i_private;
3585
3586 if (tracing_disabled)
3587 return -ENODEV;
3588
3589 if (trace_array_get(tr) < 0)
3590 return -ENODEV;
3591
3592 filp->private_data = inode->i_private;
3593
3594 return 0;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003595}
3596
Hannes Eder4fd27352009-02-10 19:44:12 +01003597static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003598{
Oleg Nesterov6484c712013-07-23 17:26:10 +02003599 struct trace_array *tr = inode->i_private;
matt mooney907f2782010-09-27 19:04:53 -07003600 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003601 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003602 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003603
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003604 if (!(file->f_mode & FMODE_READ)) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02003605 trace_array_put(tr);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003606 return 0;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003607 }
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003608
Oleg Nesterov6484c712013-07-23 17:26:10 +02003609 /* Writes do not use seq_file */
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003610 iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003611 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05003612
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003613 for_each_tracing_cpu(cpu) {
3614 if (iter->buffer_iter[cpu])
3615 ring_buffer_read_finish(iter->buffer_iter[cpu]);
3616 }
3617
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003618 if (iter->trace && iter->trace->close)
3619 iter->trace->close(iter);
3620
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003621 if (!iter->snapshot)
3622 /* reenable tracing if it was previously enabled */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003623 tracing_start_tr(tr);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07003624
3625 __trace_array_put(tr);
3626
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003627 mutex_unlock(&trace_types_lock);
3628
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003629 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003630 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003631 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04003632 kfree(iter->buffer_iter);
Jiri Olsa50e18b92012-04-25 10:23:39 +02003633 seq_release_private(inode, file);
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003634
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003635 return 0;
3636}
3637
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003638static int tracing_release_generic_tr(struct inode *inode, struct file *file)
3639{
3640 struct trace_array *tr = inode->i_private;
3641
3642 trace_array_put(tr);
3643 return 0;
3644}
3645
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003646static int tracing_single_release_tr(struct inode *inode, struct file *file)
3647{
3648 struct trace_array *tr = inode->i_private;
3649
3650 trace_array_put(tr);
3651
3652 return single_release(inode, file);
3653}
3654
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003655static int tracing_open(struct inode *inode, struct file *file)
3656{
Oleg Nesterov6484c712013-07-23 17:26:10 +02003657 struct trace_array *tr = inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003658 struct trace_iterator *iter;
3659 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003660
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003661 if (trace_array_get(tr) < 0)
3662 return -ENODEV;
3663
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003664 /* If this file was open for write, then erase contents */
Oleg Nesterov6484c712013-07-23 17:26:10 +02003665 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
3666 int cpu = tracing_get_cpu(inode);
Bo Yan5fb4be22017-09-18 10:03:35 -07003667 struct trace_buffer *trace_buf = &tr->trace_buffer;
3668
3669#ifdef CONFIG_TRACER_MAX_TRACE
3670 if (tr->current_trace->print_max)
3671 trace_buf = &tr->max_buffer;
3672#endif
Oleg Nesterov6484c712013-07-23 17:26:10 +02003673
3674 if (cpu == RING_BUFFER_ALL_CPUS)
Bo Yan5fb4be22017-09-18 10:03:35 -07003675 tracing_reset_online_cpus(trace_buf);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003676 else
Bo Yan5fb4be22017-09-18 10:03:35 -07003677 tracing_reset(trace_buf, cpu);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003678 }
3679
3680 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02003681 iter = __tracing_open(inode, file, false);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003682 if (IS_ERR(iter))
3683 ret = PTR_ERR(iter);
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003684 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003685 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3686 }
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003687
3688 if (ret < 0)
3689 trace_array_put(tr);
3690
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003691 return ret;
3692}
3693
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003694/*
3695 * Some tracers are not suitable for instance buffers.
3696 * A tracer is always available for the global array (toplevel)
3697 * or if it explicitly states that it is.
3698 */
3699static bool
3700trace_ok_for_array(struct tracer *t, struct trace_array *tr)
3701{
3702 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
3703}
3704
3705/* Find the next tracer that this trace array may use */
3706static struct tracer *
3707get_tracer_for_array(struct trace_array *tr, struct tracer *t)
3708{
3709 while (t && !trace_ok_for_array(t, tr))
3710 t = t->next;
3711
3712 return t;
3713}
3714
Ingo Molnare309b412008-05-12 21:20:51 +02003715static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003716t_next(struct seq_file *m, void *v, loff_t *pos)
3717{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003718 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08003719 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003720
3721 (*pos)++;
3722
3723 if (t)
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003724 t = get_tracer_for_array(tr, t->next);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003725
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003726 return t;
3727}
3728
3729static void *t_start(struct seq_file *m, loff_t *pos)
3730{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003731 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08003732 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003733 loff_t l = 0;
3734
3735 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003736
3737 t = get_tracer_for_array(tr, trace_types);
3738 for (; t && l < *pos; t = t_next(m, t, &l))
3739 ;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003740
3741 return t;
3742}
3743
3744static void t_stop(struct seq_file *m, void *p)
3745{
3746 mutex_unlock(&trace_types_lock);
3747}
3748
3749static int t_show(struct seq_file *m, void *v)
3750{
3751 struct tracer *t = v;
3752
3753 if (!t)
3754 return 0;
3755
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003756 seq_puts(m, t->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003757 if (t->next)
3758 seq_putc(m, ' ');
3759 else
3760 seq_putc(m, '\n');
3761
3762 return 0;
3763}
3764
James Morris88e9d342009-09-22 16:43:43 -07003765static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003766 .start = t_start,
3767 .next = t_next,
3768 .stop = t_stop,
3769 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003770};
3771
3772static int show_traces_open(struct inode *inode, struct file *file)
3773{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003774 struct trace_array *tr = inode->i_private;
3775 struct seq_file *m;
3776 int ret;
3777
Steven Rostedt60a11772008-05-12 21:20:44 +02003778 if (tracing_disabled)
3779 return -ENODEV;
3780
Steven Rostedt (VMware)5e402ba2019-10-11 18:19:17 -04003781 if (trace_array_get(tr) < 0)
3782 return -ENODEV;
3783
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003784 ret = seq_open(file, &show_traces_seq_ops);
Steven Rostedt (VMware)5e402ba2019-10-11 18:19:17 -04003785 if (ret) {
3786 trace_array_put(tr);
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003787 return ret;
Steven Rostedt (VMware)5e402ba2019-10-11 18:19:17 -04003788 }
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003789
3790 m = file->private_data;
3791 m->private = tr;
3792
3793 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003794}
3795
Steven Rostedt (VMware)5e402ba2019-10-11 18:19:17 -04003796static int show_traces_release(struct inode *inode, struct file *file)
3797{
3798 struct trace_array *tr = inode->i_private;
3799
3800 trace_array_put(tr);
3801 return seq_release(inode, file);
3802}
3803
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003804static ssize_t
3805tracing_write_stub(struct file *filp, const char __user *ubuf,
3806 size_t count, loff_t *ppos)
3807{
3808 return count;
3809}
3810
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003811loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
Slava Pestov364829b2010-11-24 15:13:16 -08003812{
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003813 int ret;
3814
Slava Pestov364829b2010-11-24 15:13:16 -08003815 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003816 ret = seq_lseek(file, offset, whence);
Slava Pestov364829b2010-11-24 15:13:16 -08003817 else
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003818 file->f_pos = ret = 0;
3819
3820 return ret;
Slava Pestov364829b2010-11-24 15:13:16 -08003821}
3822
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003823static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003824 .open = tracing_open,
3825 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003826 .write = tracing_write_stub,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003827 .llseek = tracing_lseek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003828 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003829};
3830
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003831static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02003832 .open = show_traces_open,
3833 .read = seq_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003834 .llseek = seq_lseek,
Steven Rostedt (VMware)5e402ba2019-10-11 18:19:17 -04003835 .release = show_traces_release,
Ingo Molnarc7078de2008-05-12 21:20:52 +02003836};
3837
3838static ssize_t
3839tracing_cpumask_read(struct file *filp, char __user *ubuf,
3840 size_t count, loff_t *ppos)
3841{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003842 struct trace_array *tr = file_inode(filp)->i_private;
Changbin Dud760f902017-11-30 11:39:43 +08003843 char *mask_str;
Ingo Molnar36dfe922008-05-12 21:20:52 +02003844 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003845
Changbin Dud760f902017-11-30 11:39:43 +08003846 len = snprintf(NULL, 0, "%*pb\n",
3847 cpumask_pr_args(tr->tracing_cpumask)) + 1;
3848 mask_str = kmalloc(len, GFP_KERNEL);
3849 if (!mask_str)
3850 return -ENOMEM;
Ingo Molnar36dfe922008-05-12 21:20:52 +02003851
Changbin Dud760f902017-11-30 11:39:43 +08003852 len = snprintf(mask_str, len, "%*pb\n",
Tejun Heo1a402432015-02-13 14:37:39 -08003853 cpumask_pr_args(tr->tracing_cpumask));
3854 if (len >= count) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02003855 count = -EINVAL;
3856 goto out_err;
3857 }
Changbin Dud760f902017-11-30 11:39:43 +08003858 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003859
3860out_err:
Changbin Dud760f902017-11-30 11:39:43 +08003861 kfree(mask_str);
Ingo Molnarc7078de2008-05-12 21:20:52 +02003862
3863 return count;
3864}
3865
3866static ssize_t
3867tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3868 size_t count, loff_t *ppos)
3869{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003870 struct trace_array *tr = file_inode(filp)->i_private;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303871 cpumask_var_t tracing_cpumask_new;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003872 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303873
3874 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3875 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003876
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303877 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003878 if (err)
3879 goto err_unlock;
3880
Steven Rostedta5e25882008-12-02 15:34:05 -05003881 local_irq_disable();
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05003882 arch_spin_lock(&tr->max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02003883 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02003884 /*
3885 * Increase/decrease the disabled counter if we are
3886 * about to flip a bit in the cpumask:
3887 */
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003888 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303889 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003890 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3891 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003892 }
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003893 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303894 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003895 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3896 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003897 }
3898 }
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05003899 arch_spin_unlock(&tr->max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05003900 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02003901
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003902 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303903 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02003904
Ingo Molnarc7078de2008-05-12 21:20:52 +02003905 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02003906
3907err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08003908 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003909
3910 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003911}
3912
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003913static const struct file_operations tracing_cpumask_fops = {
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003914 .open = tracing_open_generic_tr,
Ingo Molnarc7078de2008-05-12 21:20:52 +02003915 .read = tracing_cpumask_read,
3916 .write = tracing_cpumask_write,
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003917 .release = tracing_release_generic_tr,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003918 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003919};
3920
Li Zefanfdb372e2009-12-08 11:15:59 +08003921static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003922{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003923 struct tracer_opt *trace_opts;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003924 struct trace_array *tr = m->private;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003925 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003926 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003927
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003928 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003929 tracer_flags = tr->current_trace->flags->val;
3930 trace_opts = tr->current_trace->flags->opts;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003931
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003932 for (i = 0; trace_options[i]; i++) {
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003933 if (tr->trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08003934 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003935 else
Li Zefanfdb372e2009-12-08 11:15:59 +08003936 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003937 }
3938
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003939 for (i = 0; trace_opts[i].name; i++) {
3940 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08003941 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003942 else
Li Zefanfdb372e2009-12-08 11:15:59 +08003943 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003944 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003945 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003946
Li Zefanfdb372e2009-12-08 11:15:59 +08003947 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003948}
3949
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003950static int __set_tracer_option(struct trace_array *tr,
Li Zefan8d18eaa2009-12-08 11:17:06 +08003951 struct tracer_flags *tracer_flags,
3952 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003953{
Chunyu Hud39cdd22016-03-08 21:37:01 +08003954 struct tracer *trace = tracer_flags->trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08003955 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003956
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003957 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003958 if (ret)
3959 return ret;
3960
3961 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08003962 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003963 else
Zhaolei77708412009-08-07 18:53:21 +08003964 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003965 return 0;
3966}
3967
Li Zefan8d18eaa2009-12-08 11:17:06 +08003968/* Try to assign a tracer specific option */
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003969static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
Li Zefan8d18eaa2009-12-08 11:17:06 +08003970{
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003971 struct tracer *trace = tr->current_trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08003972 struct tracer_flags *tracer_flags = trace->flags;
3973 struct tracer_opt *opts = NULL;
3974 int i;
3975
3976 for (i = 0; tracer_flags->opts[i].name; i++) {
3977 opts = &tracer_flags->opts[i];
3978
3979 if (strcmp(cmp, opts->name) == 0)
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003980 return __set_tracer_option(tr, trace->flags, opts, neg);
Li Zefan8d18eaa2009-12-08 11:17:06 +08003981 }
3982
3983 return -EINVAL;
3984}
3985
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003986/* Some tracers require overwrite to stay enabled */
3987int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3988{
3989 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3990 return -1;
3991
3992 return 0;
3993}
3994
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003995int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003996{
3997 /* do nothing if flag is already set */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003998 if (!!(tr->trace_flags & mask) == !!enabled)
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003999 return 0;
4000
4001 /* Give the tracer a chance to approve the change */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004002 if (tr->current_trace->flag_changed)
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -05004003 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004004 return -EINVAL;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004005
4006 if (enabled)
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004007 tr->trace_flags |= mask;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004008 else
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004009 tr->trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08004010
4011 if (mask == TRACE_ITER_RECORD_CMD)
4012 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08004013
Steven Rostedtc37775d2016-04-13 16:59:18 -04004014 if (mask == TRACE_ITER_EVENT_FORK)
4015 trace_event_follow_fork(tr, enabled);
4016
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004017 if (mask == TRACE_ITER_OVERWRITE) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004018 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004019#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004020 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004021#endif
4022 }
Steven Rostedt81698832012-10-11 10:15:05 -04004023
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04004024 if (mask == TRACE_ITER_PRINTK) {
Steven Rostedt81698832012-10-11 10:15:05 -04004025 trace_printk_start_stop_comm(enabled);
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04004026 trace_printk_control(enabled);
4027 }
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004028
4029 return 0;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004030}
4031
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004032static int trace_set_options(struct trace_array *tr, char *option)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004033{
Li Zefan8d18eaa2009-12-08 11:17:06 +08004034 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004035 int neg = 0;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004036 int ret = -ENODEV;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004037 int i;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004038 size_t orig_len = strlen(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004039
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004040 cmp = strstrip(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004041
Li Zefan8d18eaa2009-12-08 11:17:06 +08004042 if (strncmp(cmp, "no", 2) == 0) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004043 neg = 1;
4044 cmp += 2;
4045 }
4046
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04004047 mutex_lock(&trace_types_lock);
4048
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004049 for (i = 0; trace_options[i]; i++) {
Li Zefan8d18eaa2009-12-08 11:17:06 +08004050 if (strcmp(cmp, trace_options[i]) == 0) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004051 ret = set_tracer_flag(tr, 1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004052 break;
4053 }
4054 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004055
4056 /* If no option could be set, test the specific tracer options */
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04004057 if (!trace_options[i])
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004058 ret = set_tracer_option(tr, cmp, neg);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04004059
4060 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004061
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004062 /*
4063 * If the first trailing whitespace is replaced with '\0' by strstrip,
4064 * turn it back into a space.
4065 */
4066 if (orig_len > strlen(option))
4067 option[strlen(option)] = ' ';
4068
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004069 return ret;
4070}
4071
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004072static void __init apply_trace_boot_options(void)
4073{
4074 char *buf = trace_boot_options_buf;
4075 char *option;
4076
4077 while (true) {
4078 option = strsep(&buf, ",");
4079
4080 if (!option)
4081 break;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004082
Steven Rostedt (Red Hat)43ed3842015-11-03 22:15:14 -05004083 if (*option)
4084 trace_set_options(&global_trace, option);
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004085
4086 /* Put back the comma to allow this to be called again */
4087 if (buf)
4088 *(buf - 1) = ',';
4089 }
4090}
4091
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004092static ssize_t
4093tracing_trace_options_write(struct file *filp, const char __user *ubuf,
4094 size_t cnt, loff_t *ppos)
4095{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004096 struct seq_file *m = filp->private_data;
4097 struct trace_array *tr = m->private;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004098 char buf[64];
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004099 int ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004100
4101 if (cnt >= sizeof(buf))
4102 return -EINVAL;
4103
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08004104 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004105 return -EFAULT;
4106
Steven Rostedta8dd2172013-01-09 20:54:17 -05004107 buf[cnt] = 0;
4108
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004109 ret = trace_set_options(tr, buf);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004110 if (ret < 0)
4111 return ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004112
Jiri Olsacf8517c2009-10-23 19:36:16 -04004113 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004114
4115 return cnt;
4116}
4117
Li Zefanfdb372e2009-12-08 11:15:59 +08004118static int tracing_trace_options_open(struct inode *inode, struct file *file)
4119{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004120 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004121 int ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004122
Li Zefanfdb372e2009-12-08 11:15:59 +08004123 if (tracing_disabled)
4124 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004125
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004126 if (trace_array_get(tr) < 0)
4127 return -ENODEV;
4128
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004129 ret = single_open(file, tracing_trace_options_show, inode->i_private);
4130 if (ret < 0)
4131 trace_array_put(tr);
4132
4133 return ret;
Li Zefanfdb372e2009-12-08 11:15:59 +08004134}
4135
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004136static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08004137 .open = tracing_trace_options_open,
4138 .read = seq_read,
4139 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004140 .release = tracing_single_release_tr,
Steven Rostedtee6bce52008-11-12 17:52:37 -05004141 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004142};
4143
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004144static const char readme_msg[] =
4145 "tracing mini-HOWTO:\n\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004146 "# echo 0 > tracing_on : quick way to disable tracing\n"
4147 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
4148 " Important files:\n"
4149 " trace\t\t\t- The static contents of the buffer\n"
4150 "\t\t\t To clear the buffer write into this file: echo > trace\n"
4151 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
4152 " current_tracer\t- function and latency tracers\n"
4153 " available_tracers\t- list of configured tracers for current_tracer\n"
4154 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
4155 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
4156 " trace_clock\t\t-change the clock used to order events\n"
4157 " local: Per cpu clock but may not be synced across CPUs\n"
4158 " global: Synced across CPUs but slows tracing down.\n"
4159 " counter: Not a clock, but just an increment\n"
4160 " uptime: Jiffy counter from time of boot\n"
4161 " perf: Same clock that perf events use\n"
4162#ifdef CONFIG_X86_64
4163 " x86-tsc: TSC cycle counter\n"
4164#endif
4165 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
4166 " tracing_cpumask\t- Limit which CPUs to trace\n"
4167 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
4168 "\t\t\t Remove sub-buffer with rmdir\n"
4169 " trace_options\t\t- Set format or modify how tracing happens\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004170 "\t\t\t Disable an option by adding a suffix 'no' to the\n"
4171 "\t\t\t option name\n"
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004172 " 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 -04004173#ifdef CONFIG_DYNAMIC_FTRACE
4174 "\n available_filter_functions - list of functions that can be filtered on\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004175 " set_ftrace_filter\t- echo function name in here to only trace these\n"
4176 "\t\t\t functions\n"
4177 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
4178 "\t modules: Can select a group via module\n"
4179 "\t Format: :mod:<module-name>\n"
4180 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
4181 "\t triggers: a command to perform when function is hit\n"
4182 "\t Format: <function>:<trigger>[:count]\n"
4183 "\t trigger: traceon, traceoff\n"
4184 "\t\t enable_event:<system>:<event>\n"
4185 "\t\t disable_event:<system>:<event>\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004186#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004187 "\t\t stacktrace\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004188#endif
4189#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004190 "\t\t snapshot\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004191#endif
Steven Rostedt (Red Hat)17a280e2014-04-10 22:43:37 -04004192 "\t\t dump\n"
4193 "\t\t cpudump\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004194 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
4195 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
4196 "\t The first one will disable tracing every time do_fault is hit\n"
4197 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
4198 "\t The first time do trap is hit and it disables tracing, the\n"
4199 "\t counter will decrement to 2. If tracing is already disabled,\n"
4200 "\t the counter will not decrement. It only decrements when the\n"
4201 "\t trigger did work\n"
4202 "\t To remove trigger without count:\n"
4203 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
4204 "\t To remove trigger with a count:\n"
4205 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004206 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004207 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
4208 "\t modules: Can select a group via module command :mod:\n"
4209 "\t Does not accept triggers\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004210#endif /* CONFIG_DYNAMIC_FTRACE */
4211#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004212 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
4213 "\t\t (function)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004214#endif
4215#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4216 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
Namhyung Kimd048a8c72014-06-13 01:23:53 +09004217 " 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 -04004218 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
4219#endif
4220#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004221 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
4222 "\t\t\t snapshot buffer. Read the contents for more\n"
4223 "\t\t\t information\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004224#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004225#ifdef CONFIG_STACK_TRACER
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004226 " stack_trace\t\t- Shows the max stack trace when active\n"
4227 " stack_max_size\t- Shows current max stack size that was traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004228 "\t\t\t Write into this file to reset the max size (trigger a\n"
4229 "\t\t\t new trace)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004230#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004231 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
4232 "\t\t\t traces\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004233#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004234#endif /* CONFIG_STACK_TRACER */
Masami Hiramatsu86425622016-08-18 17:58:15 +09004235#ifdef CONFIG_KPROBE_EVENT
4236 " kprobe_events\t\t- Add/remove/show the kernel dynamic events\n"
4237 "\t\t\t Write into this file to define/undefine new trace events.\n"
4238#endif
4239#ifdef CONFIG_UPROBE_EVENT
4240 " uprobe_events\t\t- Add/remove/show the userspace dynamic events\n"
4241 "\t\t\t Write into this file to define/undefine new trace events.\n"
4242#endif
4243#if defined(CONFIG_KPROBE_EVENT) || defined(CONFIG_UPROBE_EVENT)
4244 "\t accepts: event-definitions (one definition per line)\n"
4245 "\t Format: p|r[:[<group>/]<event>] <place> [<args>]\n"
4246 "\t -:[<group>/]<event>\n"
4247#ifdef CONFIG_KPROBE_EVENT
4248 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
4249#endif
4250#ifdef CONFIG_UPROBE_EVENT
4251 "\t place: <path>:<offset>\n"
4252#endif
4253 "\t args: <name>=fetcharg[:type]\n"
4254 "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
4255 "\t $stack<index>, $stack, $retval, $comm\n"
4256 "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string,\n"
4257 "\t b<bit-width>@<bit-offset>/<container-size>\n"
4258#endif
Tom Zanussi26f25562014-01-17 15:11:44 -06004259 " events/\t\t- Directory containing all trace event subsystems:\n"
4260 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
4261 " events/<system>/\t- Directory containing all trace events for <system>:\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004262 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
4263 "\t\t\t events\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004264 " filter\t\t- If set, only events passing filter are traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004265 " events/<system>/<event>/\t- Directory containing control files for\n"
4266 "\t\t\t <event>:\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004267 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
4268 " filter\t\t- If set, only events passing filter are traced\n"
4269 " trigger\t\t- If set, a command to perform when event is hit\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004270 "\t Format: <trigger>[:count][if <filter>]\n"
4271 "\t trigger: traceon, traceoff\n"
4272 "\t enable_event:<system>:<event>\n"
4273 "\t disable_event:<system>:<event>\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06004274#ifdef CONFIG_HIST_TRIGGERS
4275 "\t enable_hist:<system>:<event>\n"
4276 "\t disable_hist:<system>:<event>\n"
4277#endif
Tom Zanussi26f25562014-01-17 15:11:44 -06004278#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004279 "\t\t stacktrace\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004280#endif
4281#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004282 "\t\t snapshot\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004283#endif
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004284#ifdef CONFIG_HIST_TRIGGERS
4285 "\t\t hist (see below)\n"
4286#endif
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004287 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
4288 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
4289 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
4290 "\t events/block/block_unplug/trigger\n"
4291 "\t The first disables tracing every time block_unplug is hit.\n"
4292 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
4293 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
4294 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
4295 "\t Like function triggers, the counter is only decremented if it\n"
4296 "\t enabled or disabled tracing.\n"
4297 "\t To remove a trigger without a count:\n"
4298 "\t echo '!<trigger> > <system>/<event>/trigger\n"
4299 "\t To remove a trigger with a count:\n"
4300 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
4301 "\t Filters can be ignored when removing a trigger.\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004302#ifdef CONFIG_HIST_TRIGGERS
4303 " hist trigger\t- If set, event hits are aggregated into a hash table\n"
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004304 "\t Format: hist:keys=<field1[,field2,...]>\n"
Tom Zanussif2606832016-03-03 12:54:43 -06004305 "\t [:values=<field1[,field2,...]>]\n"
Tom Zanussie62347d2016-03-03 12:54:45 -06004306 "\t [:sort=<field1[,field2,...]>]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004307 "\t [:size=#entries]\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06004308 "\t [:pause][:continue][:clear]\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004309 "\t [:name=histname1]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004310 "\t [if <filter>]\n\n"
4311 "\t When a matching event is hit, an entry is added to a hash\n"
Tom Zanussif2606832016-03-03 12:54:43 -06004312 "\t table using the key(s) and value(s) named, and the value of a\n"
4313 "\t sum called 'hitcount' is incremented. Keys and values\n"
4314 "\t correspond to fields in the event's format description. Keys\n"
Tom Zanussi69a02002016-03-03 12:54:52 -06004315 "\t can be any field, or the special string 'stacktrace'.\n"
4316 "\t Compound keys consisting of up to two fields can be specified\n"
4317 "\t by the 'keys' keyword. Values must correspond to numeric\n"
4318 "\t fields. Sort keys consisting of up to two fields can be\n"
4319 "\t specified using the 'sort' keyword. The sort direction can\n"
4320 "\t be modified by appending '.descending' or '.ascending' to a\n"
4321 "\t sort field. The 'size' parameter can be used to specify more\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004322 "\t or fewer than the default 2048 entries for the hashtable size.\n"
4323 "\t If a hist trigger is given a name using the 'name' parameter,\n"
4324 "\t its histogram data will be shared with other triggers of the\n"
4325 "\t same name, and trigger hits will update this common data.\n\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004326 "\t Reading the 'hist' file for the event will dump the hash\n"
Tom Zanussi52a7f162016-03-03 12:54:57 -06004327 "\t table in its entirety to stdout. If there are multiple hist\n"
4328 "\t triggers attached to an event, there will be a table for each\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004329 "\t trigger in the output. The table displayed for a named\n"
4330 "\t trigger will be the same as any other instance having the\n"
4331 "\t same name. The default format used to display a given field\n"
4332 "\t can be modified by appending any of the following modifiers\n"
4333 "\t to the field name, as applicable:\n\n"
Tom Zanussic6afad42016-03-03 12:54:49 -06004334 "\t .hex display a number as a hex value\n"
4335 "\t .sym display an address as a symbol\n"
Tom Zanussi6b4827a2016-03-03 12:54:50 -06004336 "\t .sym-offset display an address as a symbol and offset\n"
Tom Zanussi31696192016-03-03 12:54:51 -06004337 "\t .execname display a common_pid as a program name\n"
4338 "\t .syscall display a syscall id as a syscall name\n\n"
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06004339 "\t .log2 display log2 value rather than raw number\n\n"
Tom Zanussi83e99912016-03-03 12:54:46 -06004340 "\t The 'pause' parameter can be used to pause an existing hist\n"
4341 "\t trigger or to start a hist trigger but not log any events\n"
4342 "\t until told to do so. 'continue' can be used to start or\n"
4343 "\t restart a paused hist trigger.\n\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06004344 "\t The 'clear' parameter will clear the contents of a running\n"
4345 "\t hist trigger and leave its current paused/active state\n"
4346 "\t unchanged.\n\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06004347 "\t The enable_hist and disable_hist triggers can be used to\n"
4348 "\t have one event conditionally start and stop another event's\n"
4349 "\t already-attached hist trigger. The syntax is analagous to\n"
4350 "\t the enable_event and disable_event triggers.\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004351#endif
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004352;
4353
4354static ssize_t
4355tracing_readme_read(struct file *filp, char __user *ubuf,
4356 size_t cnt, loff_t *ppos)
4357{
4358 return simple_read_from_buffer(ubuf, cnt, ppos,
4359 readme_msg, strlen(readme_msg));
4360}
4361
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004362static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02004363 .open = tracing_open_generic,
4364 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004365 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004366};
4367
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004368static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
Avadh Patel69abe6a2009-04-10 16:04:48 -04004369{
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004370 unsigned int *ptr = v;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004371
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004372 if (*pos || m->count)
4373 ptr++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004374
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004375 (*pos)++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004376
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004377 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
4378 ptr++) {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004379 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
Avadh Patel69abe6a2009-04-10 16:04:48 -04004380 continue;
4381
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004382 return ptr;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004383 }
4384
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004385 return NULL;
4386}
Avadh Patel69abe6a2009-04-10 16:04:48 -04004387
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004388static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
4389{
4390 void *v;
4391 loff_t l = 0;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004392
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04004393 preempt_disable();
4394 arch_spin_lock(&trace_cmdline_lock);
4395
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004396 v = &savedcmd->map_cmdline_to_pid[0];
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004397 while (l <= *pos) {
4398 v = saved_cmdlines_next(m, v, &l);
4399 if (!v)
4400 return NULL;
4401 }
4402
4403 return v;
4404}
4405
4406static void saved_cmdlines_stop(struct seq_file *m, void *v)
4407{
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04004408 arch_spin_unlock(&trace_cmdline_lock);
4409 preempt_enable();
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004410}
4411
4412static int saved_cmdlines_show(struct seq_file *m, void *v)
4413{
4414 char buf[TASK_COMM_LEN];
4415 unsigned int *pid = v;
4416
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04004417 __trace_find_cmdline(*pid, buf);
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004418 seq_printf(m, "%d %s\n", *pid, buf);
4419 return 0;
4420}
4421
4422static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
4423 .start = saved_cmdlines_start,
4424 .next = saved_cmdlines_next,
4425 .stop = saved_cmdlines_stop,
4426 .show = saved_cmdlines_show,
4427};
4428
4429static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
4430{
4431 if (tracing_disabled)
4432 return -ENODEV;
4433
4434 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
Avadh Patel69abe6a2009-04-10 16:04:48 -04004435}
4436
4437static const struct file_operations tracing_saved_cmdlines_fops = {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004438 .open = tracing_saved_cmdlines_open,
4439 .read = seq_read,
4440 .llseek = seq_lseek,
4441 .release = seq_release,
Avadh Patel69abe6a2009-04-10 16:04:48 -04004442};
4443
4444static ssize_t
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004445tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
4446 size_t cnt, loff_t *ppos)
4447{
4448 char buf[64];
4449 int r;
Adrian Salido50b2d862017-04-18 11:44:33 -07004450 unsigned int n;
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004451
Adrian Salido50b2d862017-04-18 11:44:33 -07004452 preempt_disable();
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004453 arch_spin_lock(&trace_cmdline_lock);
Adrian Salido50b2d862017-04-18 11:44:33 -07004454 n = savedcmd->cmdline_num;
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004455 arch_spin_unlock(&trace_cmdline_lock);
Adrian Salido50b2d862017-04-18 11:44:33 -07004456 preempt_enable();
4457
4458 r = scnprintf(buf, sizeof(buf), "%u\n", n);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004459
4460 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4461}
4462
4463static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
4464{
4465 kfree(s->saved_cmdlines);
4466 kfree(s->map_cmdline_to_pid);
Adrian Salido50b2d862017-04-18 11:44:33 -07004467 kfree(s->map_cmdline_to_tgid);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004468 kfree(s);
4469}
4470
4471static int tracing_resize_saved_cmdlines(unsigned int val)
4472{
4473 struct saved_cmdlines_buffer *s, *savedcmd_temp;
4474
Namhyung Kima6af8fb2014-06-10 16:11:35 +09004475 s = kmalloc(sizeof(*s), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004476 if (!s)
4477 return -ENOMEM;
4478
4479 if (allocate_cmdlines_buffer(val, s) < 0) {
4480 kfree(s);
4481 return -ENOMEM;
4482 }
4483
Adrian Salido50b2d862017-04-18 11:44:33 -07004484 preempt_disable();
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004485 arch_spin_lock(&trace_cmdline_lock);
4486 savedcmd_temp = savedcmd;
4487 savedcmd = s;
4488 arch_spin_unlock(&trace_cmdline_lock);
Adrian Salido50b2d862017-04-18 11:44:33 -07004489 preempt_enable();
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004490 free_saved_cmdlines_buffer(savedcmd_temp);
4491
4492 return 0;
4493}
4494
4495static ssize_t
4496tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
4497 size_t cnt, loff_t *ppos)
4498{
4499 unsigned long val;
4500 int ret;
4501
4502 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4503 if (ret)
4504 return ret;
4505
4506 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
4507 if (!val || val > PID_MAX_DEFAULT)
4508 return -EINVAL;
4509
4510 ret = tracing_resize_saved_cmdlines((unsigned int)val);
4511 if (ret < 0)
4512 return ret;
4513
4514 *ppos += cnt;
4515
4516 return cnt;
4517}
4518
4519static const struct file_operations tracing_saved_cmdlines_size_fops = {
4520 .open = tracing_open_generic,
4521 .read = tracing_saved_cmdlines_size_read,
4522 .write = tracing_saved_cmdlines_size_write,
4523};
4524
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004525#ifdef CONFIG_TRACE_ENUM_MAP_FILE
4526static union trace_enum_map_item *
4527update_enum_map(union trace_enum_map_item *ptr)
4528{
4529 if (!ptr->map.enum_string) {
4530 if (ptr->tail.next) {
4531 ptr = ptr->tail.next;
4532 /* Set ptr to the next real item (skip head) */
4533 ptr++;
4534 } else
4535 return NULL;
4536 }
4537 return ptr;
4538}
4539
4540static void *enum_map_next(struct seq_file *m, void *v, loff_t *pos)
4541{
4542 union trace_enum_map_item *ptr = v;
4543
4544 /*
4545 * Paranoid! If ptr points to end, we don't want to increment past it.
4546 * This really should never happen.
4547 */
4548 ptr = update_enum_map(ptr);
4549 if (WARN_ON_ONCE(!ptr))
4550 return NULL;
4551
4552 ptr++;
4553
4554 (*pos)++;
4555
4556 ptr = update_enum_map(ptr);
4557
4558 return ptr;
4559}
4560
4561static void *enum_map_start(struct seq_file *m, loff_t *pos)
4562{
4563 union trace_enum_map_item *v;
4564 loff_t l = 0;
4565
4566 mutex_lock(&trace_enum_mutex);
4567
4568 v = trace_enum_maps;
4569 if (v)
4570 v++;
4571
4572 while (v && l < *pos) {
4573 v = enum_map_next(m, v, &l);
4574 }
4575
4576 return v;
4577}
4578
4579static void enum_map_stop(struct seq_file *m, void *v)
4580{
4581 mutex_unlock(&trace_enum_mutex);
4582}
4583
4584static int enum_map_show(struct seq_file *m, void *v)
4585{
4586 union trace_enum_map_item *ptr = v;
4587
4588 seq_printf(m, "%s %ld (%s)\n",
4589 ptr->map.enum_string, ptr->map.enum_value,
4590 ptr->map.system);
4591
4592 return 0;
4593}
4594
4595static const struct seq_operations tracing_enum_map_seq_ops = {
4596 .start = enum_map_start,
4597 .next = enum_map_next,
4598 .stop = enum_map_stop,
4599 .show = enum_map_show,
4600};
4601
4602static int tracing_enum_map_open(struct inode *inode, struct file *filp)
4603{
4604 if (tracing_disabled)
4605 return -ENODEV;
4606
4607 return seq_open(filp, &tracing_enum_map_seq_ops);
4608}
4609
4610static const struct file_operations tracing_enum_map_fops = {
4611 .open = tracing_enum_map_open,
4612 .read = seq_read,
4613 .llseek = seq_lseek,
4614 .release = seq_release,
4615};
4616
4617static inline union trace_enum_map_item *
4618trace_enum_jmp_to_tail(union trace_enum_map_item *ptr)
4619{
4620 /* Return tail of array given the head */
4621 return ptr + ptr->head.length + 1;
4622}
4623
4624static void
4625trace_insert_enum_map_file(struct module *mod, struct trace_enum_map **start,
4626 int len)
4627{
4628 struct trace_enum_map **stop;
4629 struct trace_enum_map **map;
4630 union trace_enum_map_item *map_array;
4631 union trace_enum_map_item *ptr;
4632
4633 stop = start + len;
4634
4635 /*
4636 * The trace_enum_maps contains the map plus a head and tail item,
4637 * where the head holds the module and length of array, and the
4638 * tail holds a pointer to the next list.
4639 */
4640 map_array = kmalloc(sizeof(*map_array) * (len + 2), GFP_KERNEL);
4641 if (!map_array) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07004642 pr_warn("Unable to allocate trace enum mapping\n");
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004643 return;
4644 }
4645
4646 mutex_lock(&trace_enum_mutex);
4647
4648 if (!trace_enum_maps)
4649 trace_enum_maps = map_array;
4650 else {
4651 ptr = trace_enum_maps;
4652 for (;;) {
4653 ptr = trace_enum_jmp_to_tail(ptr);
4654 if (!ptr->tail.next)
4655 break;
4656 ptr = ptr->tail.next;
4657
4658 }
4659 ptr->tail.next = map_array;
4660 }
4661 map_array->head.mod = mod;
4662 map_array->head.length = len;
4663 map_array++;
4664
4665 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
4666 map_array->map = **map;
4667 map_array++;
4668 }
4669 memset(map_array, 0, sizeof(*map_array));
4670
4671 mutex_unlock(&trace_enum_mutex);
4672}
4673
4674static void trace_create_enum_file(struct dentry *d_tracer)
4675{
4676 trace_create_file("enum_map", 0444, d_tracer,
4677 NULL, &tracing_enum_map_fops);
4678}
4679
4680#else /* CONFIG_TRACE_ENUM_MAP_FILE */
4681static inline void trace_create_enum_file(struct dentry *d_tracer) { }
4682static inline void trace_insert_enum_map_file(struct module *mod,
4683 struct trace_enum_map **start, int len) { }
4684#endif /* !CONFIG_TRACE_ENUM_MAP_FILE */
4685
4686static void trace_insert_enum_map(struct module *mod,
4687 struct trace_enum_map **start, int len)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04004688{
4689 struct trace_enum_map **map;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04004690
4691 if (len <= 0)
4692 return;
4693
4694 map = start;
4695
4696 trace_event_enum_update(map, len);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004697
4698 trace_insert_enum_map_file(mod, start, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04004699}
4700
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004701static ssize_t
Jamie Gennis13b625d2012-11-21 15:04:25 -08004702tracing_saved_tgids_read(struct file *file, char __user *ubuf,
4703 size_t cnt, loff_t *ppos)
4704{
4705 char *file_buf;
4706 char *buf;
4707 int len = 0;
Jamie Gennis13b625d2012-11-21 15:04:25 -08004708 int i;
Adrian Salido50b2d862017-04-18 11:44:33 -07004709 int *pids;
4710 int n = 0;
Jamie Gennis13b625d2012-11-21 15:04:25 -08004711
Adrian Salido50b2d862017-04-18 11:44:33 -07004712 preempt_disable();
4713 arch_spin_lock(&trace_cmdline_lock);
4714
Zhenhua Huang2ff2e572018-08-24 15:59:46 +08004715 pids = kmalloc_array(savedcmd->cmdline_num, 2*sizeof(int), GFP_ATOMIC);
Adrian Salido50b2d862017-04-18 11:44:33 -07004716 if (!pids) {
4717 arch_spin_unlock(&trace_cmdline_lock);
4718 preempt_enable();
Jamie Gennis13b625d2012-11-21 15:04:25 -08004719 return -ENOMEM;
Adrian Salido50b2d862017-04-18 11:44:33 -07004720 }
Jamie Gennis13b625d2012-11-21 15:04:25 -08004721
Adrian Salido50b2d862017-04-18 11:44:33 -07004722 for (i = 0; i < savedcmd->cmdline_num; i++) {
4723 int pid;
Jamie Gennis13b625d2012-11-21 15:04:25 -08004724
Dmitry Shmidtcb575f72015-10-28 10:45:04 -07004725 pid = savedcmd->map_cmdline_to_pid[i];
Jamie Gennis13b625d2012-11-21 15:04:25 -08004726 if (pid == -1 || pid == NO_CMDLINE_MAP)
4727 continue;
4728
Adrian Salido50b2d862017-04-18 11:44:33 -07004729 pids[n] = pid;
4730 pids[n+1] = __find_tgid_locked(pid);
4731 n += 2;
4732 }
4733 arch_spin_unlock(&trace_cmdline_lock);
4734 preempt_enable();
4735
4736 if (n == 0) {
4737 kfree(pids);
4738 return 0;
4739 }
4740
4741 /* enough to hold max pair of pids + space, lr and nul */
4742 len = n * 12;
4743 file_buf = kmalloc(len, GFP_KERNEL);
4744 if (!file_buf) {
4745 kfree(pids);
4746 return -ENOMEM;
4747 }
4748
4749 buf = file_buf;
4750 for (i = 0; i < n && len > 0; i += 2) {
4751 int r;
4752
4753 r = snprintf(buf, len, "%d %d\n", pids[i], pids[i+1]);
Jamie Gennis13b625d2012-11-21 15:04:25 -08004754 buf += r;
Adrian Salido50b2d862017-04-18 11:44:33 -07004755 len -= r;
Jamie Gennis13b625d2012-11-21 15:04:25 -08004756 }
4757
4758 len = simple_read_from_buffer(ubuf, cnt, ppos,
Adrian Salido50b2d862017-04-18 11:44:33 -07004759 file_buf, buf - file_buf);
Jamie Gennis13b625d2012-11-21 15:04:25 -08004760
4761 kfree(file_buf);
Adrian Salido50b2d862017-04-18 11:44:33 -07004762 kfree(pids);
Jamie Gennis13b625d2012-11-21 15:04:25 -08004763
4764 return len;
4765}
4766
4767static const struct file_operations tracing_saved_tgids_fops = {
4768 .open = tracing_open_generic,
4769 .read = tracing_saved_tgids_read,
4770 .llseek = generic_file_llseek,
4771};
4772
4773static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004774tracing_set_trace_read(struct file *filp, char __user *ubuf,
4775 size_t cnt, loff_t *ppos)
4776{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004777 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08004778 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004779 int r;
4780
4781 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004782 r = sprintf(buf, "%s\n", tr->current_trace->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004783 mutex_unlock(&trace_types_lock);
4784
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004785 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004786}
4787
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02004788int tracer_init(struct tracer *t, struct trace_array *tr)
4789{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004790 tracing_reset_online_cpus(&tr->trace_buffer);
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02004791 return t->init(tr);
4792}
4793
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004794static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004795{
4796 int cpu;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05004797
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004798 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004799 per_cpu_ptr(buf->data, cpu)->entries = val;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004800}
4801
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004802#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookad60da502012-10-17 11:56:16 +09004803/* resize @tr's buffer to the size of @size_tr's entries */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004804static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
4805 struct trace_buffer *size_buf, int cpu_id)
Hiraku Toyookad60da502012-10-17 11:56:16 +09004806{
4807 int cpu, ret = 0;
4808
4809 if (cpu_id == RING_BUFFER_ALL_CPUS) {
4810 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004811 ret = ring_buffer_resize(trace_buf->buffer,
4812 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
Hiraku Toyookad60da502012-10-17 11:56:16 +09004813 if (ret < 0)
4814 break;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004815 per_cpu_ptr(trace_buf->data, cpu)->entries =
4816 per_cpu_ptr(size_buf->data, cpu)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09004817 }
4818 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004819 ret = ring_buffer_resize(trace_buf->buffer,
4820 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
Hiraku Toyookad60da502012-10-17 11:56:16 +09004821 if (ret == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004822 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
4823 per_cpu_ptr(size_buf->data, cpu_id)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09004824 }
4825
4826 return ret;
4827}
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004828#endif /* CONFIG_TRACER_MAX_TRACE */
Hiraku Toyookad60da502012-10-17 11:56:16 +09004829
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004830static int __tracing_resize_ring_buffer(struct trace_array *tr,
4831 unsigned long size, int cpu)
Steven Rostedt73c51622009-03-11 13:42:01 -04004832{
4833 int ret;
4834
4835 /*
4836 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04004837 * we use the size that was given, and we can forget about
4838 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04004839 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05004840 ring_buffer_expanded = true;
Steven Rostedt73c51622009-03-11 13:42:01 -04004841
Steven Rostedtb382ede62012-10-10 21:44:34 -04004842 /* May be called before buffers are initialized */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004843 if (!tr->trace_buffer.buffer)
Steven Rostedtb382ede62012-10-10 21:44:34 -04004844 return 0;
4845
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004846 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04004847 if (ret < 0)
4848 return ret;
4849
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004850#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004851 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
4852 !tr->current_trace->use_max_tr)
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004853 goto out;
4854
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004855 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04004856 if (ret < 0) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004857 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
4858 &tr->trace_buffer, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04004859 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04004860 /*
4861 * AARGH! We are left with different
4862 * size max buffer!!!!
4863 * The max buffer is our "snapshot" buffer.
4864 * When a tracer needs a snapshot (one of the
4865 * latency tracers), it swaps the max buffer
4866 * with the saved snap shot. We succeeded to
4867 * update the size of the main buffer, but failed to
4868 * update the size of the max buffer. But when we tried
4869 * to reset the main buffer to the original size, we
4870 * failed there too. This is very unlikely to
4871 * happen, but if it does, warn and kill all
4872 * tracing.
4873 */
Steven Rostedt73c51622009-03-11 13:42:01 -04004874 WARN_ON(1);
4875 tracing_disabled = 1;
4876 }
4877 return ret;
4878 }
4879
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004880 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004881 set_buffer_entries(&tr->max_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004882 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004883 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004884
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004885 out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004886#endif /* CONFIG_TRACER_MAX_TRACE */
4887
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004888 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004889 set_buffer_entries(&tr->trace_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004890 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004891 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
Steven Rostedt73c51622009-03-11 13:42:01 -04004892
4893 return ret;
4894}
4895
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004896static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
4897 unsigned long size, int cpu_id)
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004898{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004899 int ret = size;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004900
4901 mutex_lock(&trace_types_lock);
4902
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004903 if (cpu_id != RING_BUFFER_ALL_CPUS) {
4904 /* make sure, this cpu is enabled in the mask */
4905 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
4906 ret = -EINVAL;
4907 goto out;
4908 }
4909 }
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004910
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004911 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004912 if (ret < 0)
4913 ret = -ENOMEM;
4914
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004915out:
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004916 mutex_unlock(&trace_types_lock);
4917
4918 return ret;
4919}
4920
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004921
Steven Rostedt1852fcc2009-03-11 14:33:00 -04004922/**
4923 * tracing_update_buffers - used by tracing facility to expand ring buffers
4924 *
4925 * To save on memory when the tracing is never used on a system with it
4926 * configured in. The ring buffers are set to a minimum size. But once
4927 * a user starts to use the tracing facility, then they need to grow
4928 * to their default size.
4929 *
4930 * This function is to be called when a tracer is about to be used.
4931 */
4932int tracing_update_buffers(void)
4933{
4934 int ret = 0;
4935
Steven Rostedt1027fcb2009-03-12 11:33:20 -04004936 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04004937 if (!ring_buffer_expanded)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004938 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004939 RING_BUFFER_ALL_CPUS);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04004940 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04004941
4942 return ret;
4943}
4944
Steven Rostedt577b7852009-02-26 23:43:05 -05004945struct trace_option_dentry;
4946
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04004947static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004948create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
Steven Rostedt577b7852009-02-26 23:43:05 -05004949
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05004950/*
4951 * Used to clear out the tracer before deletion of an instance.
4952 * Must have trace_types_lock held.
4953 */
4954static void tracing_set_nop(struct trace_array *tr)
4955{
4956 if (tr->current_trace == &nop_trace)
4957 return;
4958
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05004959 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05004960
4961 if (tr->current_trace->reset)
4962 tr->current_trace->reset(tr);
4963
4964 tr->current_trace = &nop_trace;
4965}
4966
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04004967static void add_tracer_options(struct trace_array *tr, struct tracer *t)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004968{
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05004969 /* Only enable if the directory has been created already. */
4970 if (!tr->dir)
4971 return;
4972
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04004973 create_trace_option_files(tr, t);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05004974}
4975
4976static int tracing_set_tracer(struct trace_array *tr, const char *buf)
4977{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004978 struct tracer *t;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004979#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05004980 bool had_max_tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004981#endif
Peter Zijlstrad9e54072008-11-01 19:57:37 +01004982 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004983
Steven Rostedt1027fcb2009-03-12 11:33:20 -04004984 mutex_lock(&trace_types_lock);
4985
Steven Rostedt73c51622009-03-11 13:42:01 -04004986 if (!ring_buffer_expanded) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004987 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004988 RING_BUFFER_ALL_CPUS);
Steven Rostedt73c51622009-03-11 13:42:01 -04004989 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01004990 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04004991 ret = 0;
4992 }
4993
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004994 for (t = trace_types; t; t = t->next) {
4995 if (strcmp(t->name, buf) == 0)
4996 break;
4997 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02004998 if (!t) {
4999 ret = -EINVAL;
5000 goto out;
5001 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005002 if (t == tr->current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005003 goto out;
5004
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005005 /* Some tracers are only allowed for the top level buffer */
5006 if (!trace_ok_for_array(t, tr)) {
5007 ret = -EINVAL;
5008 goto out;
5009 }
5010
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005011 /* If trace pipe files are being read, we can't change the tracer */
5012 if (tr->current_trace->ref) {
5013 ret = -EBUSY;
5014 goto out;
5015 }
5016
Steven Rostedt9f029e82008-11-12 15:24:24 -05005017 trace_branch_disable();
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005018
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005019 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005020
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005021 if (tr->current_trace->reset)
5022 tr->current_trace->reset(tr);
Steven Rostedt34600f02013-01-22 13:35:11 -05005023
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005024 /* Current trace needs to be nop_trace before synchronize_sched */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005025 tr->current_trace = &nop_trace;
Steven Rostedt34600f02013-01-22 13:35:11 -05005026
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05005027#ifdef CONFIG_TRACER_MAX_TRACE
5028 had_max_tr = tr->allocated_snapshot;
Steven Rostedt34600f02013-01-22 13:35:11 -05005029
5030 if (had_max_tr && !t->use_max_tr) {
5031 /*
5032 * We need to make sure that the update_max_tr sees that
5033 * current_trace changed to nop_trace to keep it from
5034 * swapping the buffers after we resize it.
5035 * The update_max_tr is called from interrupts disabled
5036 * so a synchronized_sched() is sufficient.
5037 */
5038 synchronize_sched();
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04005039 free_snapshot(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005040 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005041#endif
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005042
5043#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05005044 if (t->use_max_tr && !had_max_tr) {
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04005045 ret = alloc_snapshot(tr);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005046 if (ret < 0)
5047 goto out;
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005048 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005049#endif
Steven Rostedt577b7852009-02-26 23:43:05 -05005050
Frederic Weisbecker1c800252008-11-16 05:57:26 +01005051 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005052 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01005053 if (ret)
5054 goto out;
5055 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005056
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005057 tr->current_trace = t;
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005058 tr->current_trace->enabled++;
Steven Rostedt9f029e82008-11-12 15:24:24 -05005059 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005060 out:
5061 mutex_unlock(&trace_types_lock);
5062
Peter Zijlstrad9e54072008-11-01 19:57:37 +01005063 return ret;
5064}
5065
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005066static ssize_t
5067tracing_set_trace_write(struct file *filp, const char __user *ubuf,
5068 size_t cnt, loff_t *ppos)
5069{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005070 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08005071 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005072 int i;
5073 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01005074 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005075
Steven Rostedt60063a62008-10-28 10:44:24 -04005076 ret = cnt;
5077
Li Zefanee6c2c12009-09-18 14:06:47 +08005078 if (cnt > MAX_TRACER_SIZE)
5079 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005080
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08005081 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005082 return -EFAULT;
5083
5084 buf[cnt] = 0;
5085
5086 /* strip ending whitespace. */
5087 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
5088 buf[i] = 0;
5089
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005090 err = tracing_set_tracer(tr, buf);
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01005091 if (err)
5092 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005093
Jiri Olsacf8517c2009-10-23 19:36:16 -04005094 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005095
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02005096 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005097}
5098
5099static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005100tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
5101 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005102{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005103 char buf[64];
5104 int r;
5105
Steven Rostedtcffae432008-05-12 21:21:00 +02005106 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005107 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02005108 if (r > sizeof(buf))
5109 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005110 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005111}
5112
5113static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005114tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
5115 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005116{
Hannes Eder5e398412009-02-10 19:44:34 +01005117 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005118 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005119
Peter Huewe22fe9b52011-06-07 21:58:27 +02005120 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5121 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005122 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005123
5124 *ptr = val * 1000;
5125
5126 return cnt;
5127}
5128
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005129static ssize_t
5130tracing_thresh_read(struct file *filp, char __user *ubuf,
5131 size_t cnt, loff_t *ppos)
5132{
5133 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
5134}
5135
5136static ssize_t
5137tracing_thresh_write(struct file *filp, const char __user *ubuf,
5138 size_t cnt, loff_t *ppos)
5139{
5140 struct trace_array *tr = filp->private_data;
5141 int ret;
5142
5143 mutex_lock(&trace_types_lock);
5144 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
5145 if (ret < 0)
5146 goto out;
5147
5148 if (tr->current_trace->update_thresh) {
5149 ret = tr->current_trace->update_thresh(tr);
5150 if (ret < 0)
5151 goto out;
5152 }
5153
5154 ret = cnt;
5155out:
5156 mutex_unlock(&trace_types_lock);
5157
5158 return ret;
5159}
5160
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04005161#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Chen Gange428abb2015-11-10 05:15:15 +08005162
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005163static ssize_t
5164tracing_max_lat_read(struct file *filp, char __user *ubuf,
5165 size_t cnt, loff_t *ppos)
5166{
5167 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos);
5168}
5169
5170static ssize_t
5171tracing_max_lat_write(struct file *filp, const char __user *ubuf,
5172 size_t cnt, loff_t *ppos)
5173{
5174 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos);
5175}
5176
Chen Gange428abb2015-11-10 05:15:15 +08005177#endif
5178
Steven Rostedtb3806b42008-05-12 21:20:46 +02005179static int tracing_open_pipe(struct inode *inode, struct file *filp)
5180{
Oleg Nesterov15544202013-07-23 17:25:57 +02005181 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005182 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005183 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005184
5185 if (tracing_disabled)
5186 return -ENODEV;
5187
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005188 if (trace_array_get(tr) < 0)
5189 return -ENODEV;
5190
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005191 mutex_lock(&trace_types_lock);
5192
Steven Rostedtb3806b42008-05-12 21:20:46 +02005193 /* create a buffer to store the information to pass to userspace */
5194 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005195 if (!iter) {
5196 ret = -ENOMEM;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005197 __trace_array_put(tr);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005198 goto out;
5199 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02005200
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04005201 trace_seq_init(&iter->seq);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005202 iter->trace = tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005203
5204 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
5205 ret = -ENOMEM;
5206 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10305207 }
5208
Steven Rostedta3097202008-11-07 22:36:02 -05005209 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10305210 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05005211
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005212 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt112f38a72009-06-01 15:16:05 -04005213 iter->iter_flags |= TRACE_FILE_LAT_FMT;
5214
David Sharp8be07092012-11-13 12:18:22 -08005215 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09005216 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08005217 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
5218
Oleg Nesterov15544202013-07-23 17:25:57 +02005219 iter->tr = tr;
5220 iter->trace_buffer = &tr->trace_buffer;
5221 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005222 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005223 filp->private_data = iter;
5224
Steven Rostedt107bad82008-05-12 21:21:01 +02005225 if (iter->trace->pipe_open)
5226 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02005227
Arnd Bergmannb4447862010-07-07 23:40:11 +02005228 nonseekable_open(inode, filp);
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005229
5230 tr->current_trace->ref++;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005231out:
5232 mutex_unlock(&trace_types_lock);
5233 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005234
5235fail:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005236 kfree(iter);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005237 __trace_array_put(tr);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005238 mutex_unlock(&trace_types_lock);
5239 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005240}
5241
5242static int tracing_release_pipe(struct inode *inode, struct file *file)
5243{
5244 struct trace_iterator *iter = file->private_data;
Oleg Nesterov15544202013-07-23 17:25:57 +02005245 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005246
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005247 mutex_lock(&trace_types_lock);
5248
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005249 tr->current_trace->ref--;
5250
Steven Rostedt29bf4a52009-12-09 12:37:43 -05005251 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05005252 iter->trace->pipe_close(iter);
5253
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005254 mutex_unlock(&trace_types_lock);
5255
Rusty Russell44623442009-01-01 10:12:23 +10305256 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005257 mutex_destroy(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005258 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005259
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005260 trace_array_put(tr);
5261
Steven Rostedtb3806b42008-05-12 21:20:46 +02005262 return 0;
5263}
5264
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005265static unsigned int
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005266trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005267{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005268 struct trace_array *tr = iter->tr;
5269
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005270 /* Iterators are static, they should be filled or empty */
5271 if (trace_buffer_iter(iter, iter->cpu_file))
5272 return POLLIN | POLLRDNORM;
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005273
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005274 if (tr->trace_flags & TRACE_ITER_BLOCK)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005275 /*
5276 * Always select as readable when in blocking mode
5277 */
5278 return POLLIN | POLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005279 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005280 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005281 filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005282}
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005283
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005284static unsigned int
5285tracing_poll_pipe(struct file *filp, poll_table *poll_table)
5286{
5287 struct trace_iterator *iter = filp->private_data;
5288
5289 return trace_poll(iter, filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005290}
5291
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005292/* Must be called with iter->mutex held. */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005293static int tracing_wait_pipe(struct file *filp)
5294{
5295 struct trace_iterator *iter = filp->private_data;
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04005296 int ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005297
5298 while (trace_empty(iter)) {
5299
5300 if ((filp->f_flags & O_NONBLOCK)) {
5301 return -EAGAIN;
5302 }
5303
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005304 /*
Liu Bo250bfd32013-01-14 10:54:11 +08005305 * We block until we read something and tracing is disabled.
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005306 * We still block if tracing is disabled, but we have never
5307 * read anything. This allows a user to cat this file, and
5308 * then enable tracing. But after we have read something,
5309 * we give an EOF when tracing is again disabled.
5310 *
5311 * iter->pos will be 0 if we haven't read anything.
5312 */
Tahsin Erdogan97d402e2017-09-17 03:23:48 -07005313 if (!tracer_tracing_is_on(iter->tr) && iter->pos)
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005314 break;
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04005315
5316 mutex_unlock(&iter->mutex);
5317
Rabin Vincente30f53a2014-11-10 19:46:34 +01005318 ret = wait_on_pipe(iter, false);
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04005319
5320 mutex_lock(&iter->mutex);
5321
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04005322 if (ret)
5323 return ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005324 }
5325
5326 return 1;
5327}
5328
Steven Rostedtb3806b42008-05-12 21:20:46 +02005329/*
5330 * Consumer reader.
5331 */
5332static ssize_t
5333tracing_read_pipe(struct file *filp, char __user *ubuf,
5334 size_t cnt, loff_t *ppos)
5335{
5336 struct trace_iterator *iter = filp->private_data;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005337 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005338
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005339 /*
5340 * Avoid more than one consumer on a single file descriptor
5341 * This is just a matter of traces coherency, the ring buffer itself
5342 * is protected.
5343 */
5344 mutex_lock(&iter->mutex);
Steven Rostedt (Red Hat)12458002016-09-23 22:57:13 -04005345
5346 /* return any leftover data */
5347 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
5348 if (sret != -EBUSY)
5349 goto out;
5350
5351 trace_seq_init(&iter->seq);
5352
Steven Rostedt107bad82008-05-12 21:21:01 +02005353 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005354 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
5355 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02005356 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02005357 }
5358
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005359waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005360 sret = tracing_wait_pipe(filp);
5361 if (sret <= 0)
5362 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005363
5364 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005365 if (trace_empty(iter)) {
5366 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02005367 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005368 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02005369
5370 if (cnt >= PAGE_SIZE)
5371 cnt = PAGE_SIZE - 1;
5372
Steven Rostedt53d0aa72008-05-12 21:21:01 +02005373 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02005374 memset(&iter->seq, 0,
5375 sizeof(struct trace_iterator) -
5376 offsetof(struct trace_iterator, seq));
Andrew Vagined5467d2013-08-02 21:16:43 +04005377 cpumask_clear(iter->started);
Petr Mladekc24de622019-10-11 16:21:34 +02005378 trace_seq_init(&iter->seq);
Steven Rostedt4823ed72008-05-12 21:21:01 +02005379 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005380
Lai Jiangshan4f535962009-05-18 19:35:34 +08005381 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005382 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05005383 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02005384 enum print_line_t ret;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005385 int save_len = iter->seq.seq.len;
Steven Rostedt088b1e422008-05-12 21:20:48 +02005386
Ingo Molnarf9896bf2008-05-12 21:20:47 +02005387 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02005388 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02005389 /* don't print partial lines */
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005390 iter->seq.seq.len = save_len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005391 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02005392 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01005393 if (ret != TRACE_TYPE_NO_CONSUME)
5394 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005395
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005396 if (trace_seq_used(&iter->seq) >= cnt)
Steven Rostedtb3806b42008-05-12 21:20:46 +02005397 break;
Jiri Olsaee5e51f2011-03-25 12:05:18 +01005398
5399 /*
5400 * Setting the full flag means we reached the trace_seq buffer
5401 * size and we should leave by partial output condition above.
5402 * One of the trace_seq_* functions is not used properly.
5403 */
5404 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
5405 iter->ent->type);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005406 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005407 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08005408 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02005409
Steven Rostedtb3806b42008-05-12 21:20:46 +02005410 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005411 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005412 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
Steven Rostedtf9520752009-03-02 14:04:40 -05005413 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005414
5415 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005416 * If there was nothing to send to user, in spite of consuming trace
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005417 * entries, go back to wait for more entries.
5418 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005419 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005420 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005421
Steven Rostedt107bad82008-05-12 21:21:01 +02005422out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005423 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02005424
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005425 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005426}
5427
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005428static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
5429 unsigned int idx)
5430{
5431 __free_page(spd->pages[idx]);
5432}
5433
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08005434static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05005435 .can_merge = 0,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005436 .confirm = generic_pipe_buf_confirm,
Al Viro92fdd982014-01-17 07:53:39 -05005437 .release = generic_pipe_buf_release,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005438 .steal = generic_pipe_buf_steal,
5439 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005440};
5441
Steven Rostedt34cd4992009-02-09 12:06:29 -05005442static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01005443tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05005444{
5445 size_t count;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005446 int save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005447 int ret;
5448
5449 /* Seq buffer is page-sized, exactly what we need. */
5450 for (;;) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005451 save_len = iter->seq.seq.len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005452 ret = print_trace_line(iter);
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005453
5454 if (trace_seq_has_overflowed(&iter->seq)) {
5455 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005456 break;
5457 }
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005458
5459 /*
5460 * This should not be hit, because it should only
5461 * be set if the iter->seq overflowed. But check it
5462 * anyway to be safe.
5463 */
Steven Rostedt34cd4992009-02-09 12:06:29 -05005464 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005465 iter->seq.seq.len = save_len;
5466 break;
5467 }
5468
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005469 count = trace_seq_used(&iter->seq) - save_len;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005470 if (rem < count) {
5471 rem = 0;
5472 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005473 break;
5474 }
5475
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08005476 if (ret != TRACE_TYPE_NO_CONSUME)
5477 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05005478 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05005479 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05005480 rem = 0;
5481 iter->ent = NULL;
5482 break;
5483 }
5484 }
5485
5486 return rem;
5487}
5488
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005489static ssize_t tracing_splice_read_pipe(struct file *filp,
5490 loff_t *ppos,
5491 struct pipe_inode_info *pipe,
5492 size_t len,
5493 unsigned int flags)
5494{
Jens Axboe35f3d142010-05-20 10:43:18 +02005495 struct page *pages_def[PIPE_DEF_BUFFERS];
5496 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005497 struct trace_iterator *iter = filp->private_data;
5498 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02005499 .pages = pages_def,
5500 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005501 .nr_pages = 0, /* This gets updated below. */
Eric Dumazet047fe362012-06-12 15:24:40 +02005502 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005503 .flags = flags,
5504 .ops = &tracing_pipe_buf_ops,
5505 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005506 };
5507 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005508 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005509 unsigned int i;
5510
Jens Axboe35f3d142010-05-20 10:43:18 +02005511 if (splice_grow_spd(pipe, &spd))
5512 return -ENOMEM;
5513
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005514 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005515
5516 if (iter->trace->splice_read) {
5517 ret = iter->trace->splice_read(iter, filp,
5518 ppos, pipe, len, flags);
5519 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05005520 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005521 }
5522
5523 ret = tracing_wait_pipe(filp);
5524 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05005525 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005526
Jason Wessel955b61e2010-08-05 09:22:23 -05005527 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005528 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005529 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005530 }
5531
Lai Jiangshan4f535962009-05-18 19:35:34 +08005532 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005533 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08005534
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005535 /* Fill as many pages as possible. */
Al Viroa786c062014-04-11 12:01:03 -04005536 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
Jens Axboe35f3d142010-05-20 10:43:18 +02005537 spd.pages[i] = alloc_page(GFP_KERNEL);
5538 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05005539 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005540
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01005541 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005542
5543 /* Copy the data into the page, so we can start over. */
5544 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02005545 page_address(spd.pages[i]),
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005546 trace_seq_used(&iter->seq));
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005547 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02005548 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005549 break;
5550 }
Jens Axboe35f3d142010-05-20 10:43:18 +02005551 spd.partial[i].offset = 0;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005552 spd.partial[i].len = trace_seq_used(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005553
Steven Rostedtf9520752009-03-02 14:04:40 -05005554 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005555 }
5556
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005557 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08005558 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005559 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005560
5561 spd.nr_pages = i;
5562
Steven Rostedt (Red Hat)a29054d92016-03-18 15:46:48 -04005563 if (i)
5564 ret = splice_to_pipe(pipe, &spd);
5565 else
5566 ret = 0;
Jens Axboe35f3d142010-05-20 10:43:18 +02005567out:
Eric Dumazet047fe362012-06-12 15:24:40 +02005568 splice_shrink_spd(&spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02005569 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005570
Steven Rostedt34cd4992009-02-09 12:06:29 -05005571out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005572 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02005573 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005574}
5575
Steven Rostedta98a3c32008-05-12 21:20:59 +02005576static ssize_t
5577tracing_entries_read(struct file *filp, char __user *ubuf,
5578 size_t cnt, loff_t *ppos)
5579{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005580 struct inode *inode = file_inode(filp);
5581 struct trace_array *tr = inode->i_private;
5582 int cpu = tracing_get_cpu(inode);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005583 char buf[64];
5584 int r = 0;
5585 ssize_t ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005586
Steven Rostedtdb526ca2009-03-12 13:53:25 -04005587 mutex_lock(&trace_types_lock);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005588
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005589 if (cpu == RING_BUFFER_ALL_CPUS) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005590 int cpu, buf_size_same;
5591 unsigned long size;
5592
5593 size = 0;
5594 buf_size_same = 1;
5595 /* check if all cpu sizes are same */
5596 for_each_tracing_cpu(cpu) {
5597 /* fill in the size from first enabled cpu */
5598 if (size == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005599 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
5600 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005601 buf_size_same = 0;
5602 break;
5603 }
5604 }
5605
5606 if (buf_size_same) {
5607 if (!ring_buffer_expanded)
5608 r = sprintf(buf, "%lu (expanded: %lu)\n",
5609 size >> 10,
5610 trace_buf_size >> 10);
5611 else
5612 r = sprintf(buf, "%lu\n", size >> 10);
5613 } else
5614 r = sprintf(buf, "X\n");
5615 } else
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005616 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005617
Steven Rostedtdb526ca2009-03-12 13:53:25 -04005618 mutex_unlock(&trace_types_lock);
5619
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005620 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5621 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005622}
5623
5624static ssize_t
5625tracing_entries_write(struct file *filp, const char __user *ubuf,
5626 size_t cnt, loff_t *ppos)
5627{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005628 struct inode *inode = file_inode(filp);
5629 struct trace_array *tr = inode->i_private;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005630 unsigned long val;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005631 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005632
Peter Huewe22fe9b52011-06-07 21:58:27 +02005633 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5634 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005635 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005636
5637 /* must have at least 1 entry */
5638 if (!val)
5639 return -EINVAL;
5640
Steven Rostedt1696b2b2008-11-13 00:09:35 -05005641 /* value is in KB */
5642 val <<= 10;
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005643 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005644 if (ret < 0)
5645 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005646
Jiri Olsacf8517c2009-10-23 19:36:16 -04005647 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005648
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005649 return cnt;
5650}
Steven Rostedtbf5e6512008-11-10 21:46:00 -05005651
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005652static ssize_t
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005653tracing_total_entries_read(struct file *filp, char __user *ubuf,
5654 size_t cnt, loff_t *ppos)
5655{
5656 struct trace_array *tr = filp->private_data;
5657 char buf[64];
5658 int r, cpu;
5659 unsigned long size = 0, expanded_size = 0;
5660
5661 mutex_lock(&trace_types_lock);
5662 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005663 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005664 if (!ring_buffer_expanded)
5665 expanded_size += trace_buf_size >> 10;
5666 }
5667 if (ring_buffer_expanded)
5668 r = sprintf(buf, "%lu\n", size);
5669 else
5670 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
5671 mutex_unlock(&trace_types_lock);
5672
5673 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5674}
5675
5676static ssize_t
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005677tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
5678 size_t cnt, loff_t *ppos)
5679{
5680 /*
5681 * There is no need to read what the user has written, this function
5682 * is just to make sure that there is no error when "echo" is used
5683 */
5684
5685 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005686
5687 return cnt;
5688}
5689
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005690static int
5691tracing_free_buffer_release(struct inode *inode, struct file *filp)
5692{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005693 struct trace_array *tr = inode->i_private;
5694
Steven Rostedtcf30cf62011-06-14 22:44:07 -04005695 /* disable tracing ? */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005696 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
Alexander Z Lam711e1242013-08-02 18:36:15 -07005697 tracer_tracing_off(tr);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005698 /* resize the ring buffer to 0 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005699 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005700
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005701 trace_array_put(tr);
5702
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005703 return 0;
5704}
5705
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005706static ssize_t
5707tracing_mark_write(struct file *filp, const char __user *ubuf,
5708 size_t cnt, loff_t *fpos)
5709{
Steven Rostedtd696b582011-09-22 11:50:27 -04005710 unsigned long addr = (unsigned long)ubuf;
Alexander Z Lam2d716192013-07-01 15:31:24 -07005711 struct trace_array *tr = filp->private_data;
Steven Rostedtd696b582011-09-22 11:50:27 -04005712 struct ring_buffer_event *event;
5713 struct ring_buffer *buffer;
5714 struct print_entry *entry;
5715 unsigned long irq_flags;
5716 struct page *pages[2];
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005717 void *map_page[2];
Steven Rostedtd696b582011-09-22 11:50:27 -04005718 int nr_pages = 1;
5719 ssize_t written;
Steven Rostedtd696b582011-09-22 11:50:27 -04005720 int offset;
5721 int size;
5722 int len;
5723 int ret;
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005724 int i;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005725
Steven Rostedtc76f0692008-11-07 22:36:02 -05005726 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005727 return -EINVAL;
5728
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005729 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -07005730 return -EINVAL;
5731
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005732 if (cnt > TRACE_BUF_SIZE)
5733 cnt = TRACE_BUF_SIZE;
5734
Steven Rostedtd696b582011-09-22 11:50:27 -04005735 /*
5736 * Userspace is injecting traces into the kernel trace buffer.
5737 * We want to be as non intrusive as possible.
5738 * To do so, we do not want to allocate any special buffers
5739 * or take any locks, but instead write the userspace data
5740 * straight into the ring buffer.
5741 *
5742 * First we need to pin the userspace buffer into memory,
5743 * which, most likely it is, because it just referenced it.
5744 * But there's no guarantee that it is. By using get_user_pages_fast()
5745 * and kmap_atomic/kunmap_atomic() we can get access to the
5746 * pages directly. We then write the data directly into the
5747 * ring buffer.
5748 */
5749 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005750
Steven Rostedtd696b582011-09-22 11:50:27 -04005751 /* check if we cross pages */
5752 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
5753 nr_pages = 2;
5754
5755 offset = addr & (PAGE_SIZE - 1);
5756 addr &= PAGE_MASK;
5757
5758 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
5759 if (ret < nr_pages) {
5760 while (--ret >= 0)
5761 put_page(pages[ret]);
5762 written = -EFAULT;
5763 goto out;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005764 }
5765
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005766 for (i = 0; i < nr_pages; i++)
5767 map_page[i] = kmap_atomic(pages[i]);
Steven Rostedtd696b582011-09-22 11:50:27 -04005768
5769 local_save_flags(irq_flags);
5770 size = sizeof(*entry) + cnt + 2; /* possible \n added */
Alexander Z Lam2d716192013-07-01 15:31:24 -07005771 buffer = tr->trace_buffer.buffer;
Steven Rostedtd696b582011-09-22 11:50:27 -04005772 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
5773 irq_flags, preempt_count());
5774 if (!event) {
5775 /* Ring buffer disabled, return as if not open for write */
5776 written = -EBADF;
5777 goto out_unlock;
5778 }
5779
5780 entry = ring_buffer_event_data(event);
5781 entry->ip = _THIS_IP_;
5782
5783 if (nr_pages == 2) {
5784 len = PAGE_SIZE - offset;
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005785 memcpy(&entry->buf, map_page[0] + offset, len);
5786 memcpy(&entry->buf[len], map_page[1], cnt - len);
Steven Rostedtd696b582011-09-22 11:50:27 -04005787 } else
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005788 memcpy(&entry->buf, map_page[0] + offset, cnt);
Steven Rostedtd696b582011-09-22 11:50:27 -04005789
5790 if (entry->buf[cnt - 1] != '\n') {
5791 entry->buf[cnt] = '\n';
5792 entry->buf[cnt + 1] = '\0';
Shashank Mittal43beb422016-05-20 13:06:09 -07005793 stm_log(OST_ENTITY_TRACE_MARKER, entry->buf, cnt + 2);
5794 } else {
Steven Rostedtd696b582011-09-22 11:50:27 -04005795 entry->buf[cnt] = '\0';
Shashank Mittal43beb422016-05-20 13:06:09 -07005796 stm_log(OST_ENTITY_TRACE_MARKER, entry->buf, cnt + 1);
5797 }
Steven Rostedtd696b582011-09-22 11:50:27 -04005798
Steven Rostedt7ffbd482012-10-11 12:14:25 -04005799 __buffer_unlock_commit(buffer, event);
Steven Rostedtd696b582011-09-22 11:50:27 -04005800
5801 written = cnt;
5802
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02005803 *fpos += written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005804
Steven Rostedtd696b582011-09-22 11:50:27 -04005805 out_unlock:
Vikram Mulukutla72158532014-12-17 18:50:56 -08005806 for (i = nr_pages - 1; i >= 0; i--) {
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005807 kunmap_atomic(map_page[i]);
5808 put_page(pages[i]);
5809 }
Steven Rostedtd696b582011-09-22 11:50:27 -04005810 out:
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02005811 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005812}
5813
Li Zefan13f16d22009-12-08 11:16:11 +08005814static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08005815{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005816 struct trace_array *tr = m->private;
Zhaolei5079f322009-08-25 16:12:56 +08005817 int i;
5818
5819 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08005820 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08005821 "%s%s%s%s", i ? " " : "",
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005822 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
5823 i == tr->clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08005824 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08005825
Li Zefan13f16d22009-12-08 11:16:11 +08005826 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08005827}
5828
Steven Rostedte1e232c2014-02-10 23:38:46 -05005829static int tracing_set_clock(struct trace_array *tr, const char *clockstr)
Zhaolei5079f322009-08-25 16:12:56 +08005830{
Zhaolei5079f322009-08-25 16:12:56 +08005831 int i;
5832
Zhaolei5079f322009-08-25 16:12:56 +08005833 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
5834 if (strcmp(trace_clocks[i].name, clockstr) == 0)
5835 break;
5836 }
5837 if (i == ARRAY_SIZE(trace_clocks))
5838 return -EINVAL;
5839
Zhaolei5079f322009-08-25 16:12:56 +08005840 mutex_lock(&trace_types_lock);
5841
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005842 tr->clock_id = i;
5843
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005844 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
Zhaolei5079f322009-08-25 16:12:56 +08005845
David Sharp60303ed2012-10-11 16:27:52 -07005846 /*
5847 * New clock may not be consistent with the previous clock.
5848 * Reset the buffer so that it doesn't have incomparable timestamps.
5849 */
Alexander Z Lam94571582013-08-02 18:36:16 -07005850 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005851
5852#ifdef CONFIG_TRACER_MAX_TRACE
Baohong Liucf0523362017-09-05 16:57:19 -05005853 if (tr->max_buffer.buffer)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005854 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
Alexander Z Lam94571582013-08-02 18:36:16 -07005855 tracing_reset_online_cpus(&tr->max_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005856#endif
David Sharp60303ed2012-10-11 16:27:52 -07005857
Zhaolei5079f322009-08-25 16:12:56 +08005858 mutex_unlock(&trace_types_lock);
5859
Steven Rostedte1e232c2014-02-10 23:38:46 -05005860 return 0;
5861}
5862
5863static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
5864 size_t cnt, loff_t *fpos)
5865{
5866 struct seq_file *m = filp->private_data;
5867 struct trace_array *tr = m->private;
5868 char buf[64];
5869 const char *clockstr;
5870 int ret;
5871
5872 if (cnt >= sizeof(buf))
5873 return -EINVAL;
5874
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08005875 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedte1e232c2014-02-10 23:38:46 -05005876 return -EFAULT;
5877
5878 buf[cnt] = 0;
5879
5880 clockstr = strstrip(buf);
5881
5882 ret = tracing_set_clock(tr, clockstr);
5883 if (ret)
5884 return ret;
5885
Zhaolei5079f322009-08-25 16:12:56 +08005886 *fpos += cnt;
5887
5888 return cnt;
5889}
5890
Li Zefan13f16d22009-12-08 11:16:11 +08005891static int tracing_clock_open(struct inode *inode, struct file *file)
5892{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005893 struct trace_array *tr = inode->i_private;
5894 int ret;
5895
Li Zefan13f16d22009-12-08 11:16:11 +08005896 if (tracing_disabled)
5897 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005898
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005899 if (trace_array_get(tr))
5900 return -ENODEV;
5901
5902 ret = single_open(file, tracing_clock_show, inode->i_private);
5903 if (ret < 0)
5904 trace_array_put(tr);
5905
5906 return ret;
Li Zefan13f16d22009-12-08 11:16:11 +08005907}
5908
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005909struct ftrace_buffer_info {
5910 struct trace_iterator iter;
5911 void *spare;
5912 unsigned int read;
5913};
5914
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005915#ifdef CONFIG_TRACER_SNAPSHOT
5916static int tracing_snapshot_open(struct inode *inode, struct file *file)
5917{
Oleg Nesterov6484c712013-07-23 17:26:10 +02005918 struct trace_array *tr = inode->i_private;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005919 struct trace_iterator *iter;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005920 struct seq_file *m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005921 int ret = 0;
5922
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005923 if (trace_array_get(tr) < 0)
5924 return -ENODEV;
5925
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005926 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02005927 iter = __tracing_open(inode, file, true);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005928 if (IS_ERR(iter))
5929 ret = PTR_ERR(iter);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005930 } else {
5931 /* Writes still need the seq_file to hold the private data */
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005932 ret = -ENOMEM;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005933 m = kzalloc(sizeof(*m), GFP_KERNEL);
5934 if (!m)
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005935 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005936 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
5937 if (!iter) {
5938 kfree(m);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005939 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005940 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005941 ret = 0;
5942
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005943 iter->tr = tr;
Oleg Nesterov6484c712013-07-23 17:26:10 +02005944 iter->trace_buffer = &tr->max_buffer;
5945 iter->cpu_file = tracing_get_cpu(inode);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005946 m->private = iter;
5947 file->private_data = m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005948 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005949out:
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005950 if (ret < 0)
5951 trace_array_put(tr);
5952
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005953 return ret;
5954}
5955
5956static ssize_t
5957tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
5958 loff_t *ppos)
5959{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005960 struct seq_file *m = filp->private_data;
5961 struct trace_iterator *iter = m->private;
5962 struct trace_array *tr = iter->tr;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005963 unsigned long val;
5964 int ret;
5965
5966 ret = tracing_update_buffers();
5967 if (ret < 0)
5968 return ret;
5969
5970 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5971 if (ret)
5972 return ret;
5973
5974 mutex_lock(&trace_types_lock);
5975
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005976 if (tr->current_trace->use_max_tr) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005977 ret = -EBUSY;
5978 goto out;
5979 }
5980
5981 switch (val) {
5982 case 0:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005983 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
5984 ret = -EINVAL;
5985 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005986 }
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04005987 if (tr->allocated_snapshot)
5988 free_snapshot(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005989 break;
5990 case 1:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005991/* Only allow per-cpu swap if the ring buffer supports it */
5992#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
5993 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
5994 ret = -EINVAL;
5995 break;
5996 }
5997#endif
Eiichi Tsukatafc19ad32019-06-25 10:29:10 +09005998 if (!tr->allocated_snapshot)
5999 ret = resize_buffer_duplicate_size(&tr->max_buffer,
6000 &tr->trace_buffer, iter->cpu_file);
6001 else
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04006002 ret = alloc_snapshot(tr);
Eiichi Tsukatafc19ad32019-06-25 10:29:10 +09006003
6004 if (ret < 0)
6005 break;
6006
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006007 local_irq_disable();
6008 /* Now, we're going to swap */
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006009 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05006010 update_max_tr(tr, current, smp_processor_id());
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006011 else
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05006012 update_max_tr_single(tr, current, iter->cpu_file);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006013 local_irq_enable();
6014 break;
6015 default:
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05006016 if (tr->allocated_snapshot) {
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006017 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
6018 tracing_reset_online_cpus(&tr->max_buffer);
6019 else
6020 tracing_reset(&tr->max_buffer, iter->cpu_file);
6021 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006022 break;
6023 }
6024
6025 if (ret >= 0) {
6026 *ppos += cnt;
6027 ret = cnt;
6028 }
6029out:
6030 mutex_unlock(&trace_types_lock);
6031 return ret;
6032}
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006033
6034static int tracing_snapshot_release(struct inode *inode, struct file *file)
6035{
6036 struct seq_file *m = file->private_data;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006037 int ret;
6038
6039 ret = tracing_release(inode, file);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006040
6041 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006042 return ret;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006043
6044 /* If write only, the seq_file is just a stub */
6045 if (m)
6046 kfree(m->private);
6047 kfree(m);
6048
6049 return 0;
6050}
6051
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006052static int tracing_buffers_open(struct inode *inode, struct file *filp);
6053static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
6054 size_t count, loff_t *ppos);
6055static int tracing_buffers_release(struct inode *inode, struct file *file);
6056static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
6057 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
6058
6059static int snapshot_raw_open(struct inode *inode, struct file *filp)
6060{
6061 struct ftrace_buffer_info *info;
6062 int ret;
6063
6064 ret = tracing_buffers_open(inode, filp);
6065 if (ret < 0)
6066 return ret;
6067
6068 info = filp->private_data;
6069
6070 if (info->iter.trace->use_max_tr) {
6071 tracing_buffers_release(inode, filp);
6072 return -EBUSY;
6073 }
6074
6075 info->iter.snapshot = true;
6076 info->iter.trace_buffer = &info->iter.tr->max_buffer;
6077
6078 return ret;
6079}
6080
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006081#endif /* CONFIG_TRACER_SNAPSHOT */
6082
6083
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04006084static const struct file_operations tracing_thresh_fops = {
6085 .open = tracing_open_generic,
6086 .read = tracing_thresh_read,
6087 .write = tracing_thresh_write,
6088 .llseek = generic_file_llseek,
6089};
6090
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04006091#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006092static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006093 .open = tracing_open_generic,
6094 .read = tracing_max_lat_read,
6095 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006096 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006097};
Chen Gange428abb2015-11-10 05:15:15 +08006098#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006099
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006100static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006101 .open = tracing_open_generic,
6102 .read = tracing_set_trace_read,
6103 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006104 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006105};
6106
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006107static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006108 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006109 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006110 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006111 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006112 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006113 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02006114};
6115
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006116static const struct file_operations tracing_entries_fops = {
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006117 .open = tracing_open_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02006118 .read = tracing_entries_read,
6119 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006120 .llseek = generic_file_llseek,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006121 .release = tracing_release_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02006122};
6123
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006124static const struct file_operations tracing_total_entries_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006125 .open = tracing_open_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006126 .read = tracing_total_entries_read,
6127 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006128 .release = tracing_release_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006129};
6130
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006131static const struct file_operations tracing_free_buffer_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006132 .open = tracing_open_generic_tr,
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006133 .write = tracing_free_buffer_write,
6134 .release = tracing_free_buffer_release,
6135};
6136
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006137static const struct file_operations tracing_mark_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006138 .open = tracing_open_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006139 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006140 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006141 .release = tracing_release_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006142};
6143
Zhaolei5079f322009-08-25 16:12:56 +08006144static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08006145 .open = tracing_clock_open,
6146 .read = seq_read,
6147 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006148 .release = tracing_single_release_tr,
Zhaolei5079f322009-08-25 16:12:56 +08006149 .write = tracing_clock_write,
6150};
6151
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006152#ifdef CONFIG_TRACER_SNAPSHOT
6153static const struct file_operations snapshot_fops = {
6154 .open = tracing_snapshot_open,
6155 .read = seq_read,
6156 .write = tracing_snapshot_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006157 .llseek = tracing_lseek,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006158 .release = tracing_snapshot_release,
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006159};
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006160
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006161static const struct file_operations snapshot_raw_fops = {
6162 .open = snapshot_raw_open,
6163 .read = tracing_buffers_read,
6164 .release = tracing_buffers_release,
6165 .splice_read = tracing_buffers_splice_read,
6166 .llseek = no_llseek,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006167};
6168
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006169#endif /* CONFIG_TRACER_SNAPSHOT */
6170
Steven Rostedt2cadf912008-12-01 22:20:19 -05006171static int tracing_buffers_open(struct inode *inode, struct file *filp)
6172{
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006173 struct trace_array *tr = inode->i_private;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006174 struct ftrace_buffer_info *info;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006175 int ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006176
6177 if (tracing_disabled)
6178 return -ENODEV;
6179
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006180 if (trace_array_get(tr) < 0)
6181 return -ENODEV;
6182
Steven Rostedt2cadf912008-12-01 22:20:19 -05006183 info = kzalloc(sizeof(*info), GFP_KERNEL);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006184 if (!info) {
6185 trace_array_put(tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006186 return -ENOMEM;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006187 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05006188
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006189 mutex_lock(&trace_types_lock);
6190
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006191 info->iter.tr = tr;
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006192 info->iter.cpu_file = tracing_get_cpu(inode);
Steven Rostedtb6273442013-02-28 13:44:11 -05006193 info->iter.trace = tr->current_trace;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006194 info->iter.trace_buffer = &tr->trace_buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006195 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006196 /* Force reading ring buffer for first read */
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006197 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006198
6199 filp->private_data = info;
6200
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05006201 tr->current_trace->ref++;
6202
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006203 mutex_unlock(&trace_types_lock);
6204
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006205 ret = nonseekable_open(inode, filp);
6206 if (ret < 0)
6207 trace_array_put(tr);
6208
6209 return ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006210}
6211
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006212static unsigned int
6213tracing_buffers_poll(struct file *filp, poll_table *poll_table)
6214{
6215 struct ftrace_buffer_info *info = filp->private_data;
6216 struct trace_iterator *iter = &info->iter;
6217
6218 return trace_poll(iter, filp, poll_table);
6219}
6220
Steven Rostedt2cadf912008-12-01 22:20:19 -05006221static ssize_t
6222tracing_buffers_read(struct file *filp, char __user *ubuf,
6223 size_t count, loff_t *ppos)
6224{
6225 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006226 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006227 ssize_t ret;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006228 ssize_t size;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006229
Steven Rostedt2dc5d122009-03-04 19:10:05 -05006230 if (!count)
6231 return 0;
6232
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006233#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006234 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
6235 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006236#endif
6237
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006238 if (!info->spare)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006239 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
6240 iter->cpu_file);
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006241 if (!info->spare)
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006242 return -ENOMEM;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006243
Steven Rostedt2cadf912008-12-01 22:20:19 -05006244 /* Do we have previous read data to read? */
6245 if (info->read < PAGE_SIZE)
6246 goto read;
6247
Steven Rostedtb6273442013-02-28 13:44:11 -05006248 again:
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006249 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006250 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006251 &info->spare,
6252 count,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006253 iter->cpu_file, 0);
6254 trace_access_unlock(iter->cpu_file);
Steven Rostedtb6273442013-02-28 13:44:11 -05006255
6256 if (ret < 0) {
6257 if (trace_empty(iter)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006258 if ((filp->f_flags & O_NONBLOCK))
6259 return -EAGAIN;
6260
Rabin Vincente30f53a2014-11-10 19:46:34 +01006261 ret = wait_on_pipe(iter, false);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006262 if (ret)
6263 return ret;
6264
Steven Rostedtb6273442013-02-28 13:44:11 -05006265 goto again;
6266 }
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006267 return 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05006268 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05006269
Steven Rostedt436fc282011-10-14 10:44:25 -04006270 info->read = 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05006271 read:
Steven Rostedt2cadf912008-12-01 22:20:19 -05006272 size = PAGE_SIZE - info->read;
6273 if (size > count)
6274 size = count;
6275
6276 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006277 if (ret == size)
6278 return -EFAULT;
6279
Steven Rostedt2dc5d122009-03-04 19:10:05 -05006280 size -= ret;
6281
Steven Rostedt2cadf912008-12-01 22:20:19 -05006282 *ppos += size;
6283 info->read += size;
6284
6285 return size;
6286}
6287
6288static int tracing_buffers_release(struct inode *inode, struct file *file)
6289{
6290 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006291 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006292
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006293 mutex_lock(&trace_types_lock);
6294
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05006295 iter->tr->current_trace->ref--;
6296
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006297 __trace_array_put(iter->tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006298
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006299 if (info->spare)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006300 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006301 kfree(info);
6302
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006303 mutex_unlock(&trace_types_lock);
6304
Steven Rostedt2cadf912008-12-01 22:20:19 -05006305 return 0;
6306}
6307
6308struct buffer_ref {
6309 struct ring_buffer *buffer;
6310 void *page;
6311 int ref;
6312};
6313
6314static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
6315 struct pipe_buffer *buf)
6316{
6317 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
6318
6319 if (--ref->ref)
6320 return;
6321
6322 ring_buffer_free_read_page(ref->buffer, ref->page);
6323 kfree(ref);
6324 buf->private = 0;
6325}
6326
Matthew Wilcox95570902019-04-05 14:02:10 -07006327static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006328 struct pipe_buffer *buf)
6329{
6330 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
6331
Matthew Wilcox95570902019-04-05 14:02:10 -07006332 if (ref->ref > INT_MAX/2)
6333 return false;
6334
Steven Rostedt2cadf912008-12-01 22:20:19 -05006335 ref->ref++;
Matthew Wilcox95570902019-04-05 14:02:10 -07006336 return true;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006337}
6338
6339/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08006340static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05006341 .can_merge = 0,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006342 .confirm = generic_pipe_buf_confirm,
6343 .release = buffer_pipe_buf_release,
Masami Hiramatsud55cb6c2012-08-09 21:31:10 +09006344 .steal = generic_pipe_buf_steal,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006345 .get = buffer_pipe_buf_get,
6346};
6347
6348/*
6349 * Callback from splice_to_pipe(), if we need to release some pages
6350 * at the end of the spd in case we error'ed out in filling the pipe.
6351 */
6352static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
6353{
6354 struct buffer_ref *ref =
6355 (struct buffer_ref *)spd->partial[i].private;
6356
6357 if (--ref->ref)
6358 return;
6359
6360 ring_buffer_free_read_page(ref->buffer, ref->page);
6361 kfree(ref);
6362 spd->partial[i].private = 0;
6363}
6364
6365static ssize_t
6366tracing_buffers_splice_read(struct file *file, loff_t *ppos,
6367 struct pipe_inode_info *pipe, size_t len,
6368 unsigned int flags)
6369{
6370 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006371 struct trace_iterator *iter = &info->iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02006372 struct partial_page partial_def[PIPE_DEF_BUFFERS];
6373 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05006374 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02006375 .pages = pages_def,
6376 .partial = partial_def,
Eric Dumazet047fe362012-06-12 15:24:40 +02006377 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006378 .flags = flags,
6379 .ops = &buffer_pipe_buf_ops,
6380 .spd_release = buffer_spd_release,
6381 };
6382 struct buffer_ref *ref;
Steven Rostedt (VMware)6edea152017-12-22 20:38:57 -05006383 int entries, i;
Rabin Vincent07906da2014-11-06 22:26:07 +01006384 ssize_t ret = 0;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006385
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006386#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006387 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
6388 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006389#endif
6390
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006391 if (*ppos & (PAGE_SIZE - 1))
6392 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08006393
6394 if (len & (PAGE_SIZE - 1)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006395 if (len < PAGE_SIZE)
6396 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08006397 len &= PAGE_MASK;
6398 }
6399
Al Viro1ae22932016-09-17 18:31:46 -04006400 if (splice_grow_spd(pipe, &spd))
6401 return -ENOMEM;
6402
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006403 again:
6404 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006405 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt93459c62009-04-29 00:23:13 -04006406
Al Viroa786c062014-04-11 12:01:03 -04006407 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05006408 struct page *page;
6409 int r;
6410
6411 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
Rabin Vincent07906da2014-11-06 22:26:07 +01006412 if (!ref) {
6413 ret = -ENOMEM;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006414 break;
Rabin Vincent07906da2014-11-06 22:26:07 +01006415 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05006416
Steven Rostedt7267fa62009-04-29 00:16:21 -04006417 ref->ref = 1;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006418 ref->buffer = iter->trace_buffer->buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006419 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006420 if (!ref->page) {
Rabin Vincent07906da2014-11-06 22:26:07 +01006421 ret = -ENOMEM;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006422 kfree(ref);
6423 break;
6424 }
6425
6426 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006427 len, iter->cpu_file, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006428 if (r < 0) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07006429 ring_buffer_free_read_page(ref->buffer, ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006430 kfree(ref);
6431 break;
6432 }
6433
Steven Rostedt2cadf912008-12-01 22:20:19 -05006434 page = virt_to_page(ref->page);
6435
6436 spd.pages[i] = page;
6437 spd.partial[i].len = PAGE_SIZE;
6438 spd.partial[i].offset = 0;
6439 spd.partial[i].private = (unsigned long)ref;
6440 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08006441 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04006442
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006443 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006444 }
6445
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006446 trace_access_unlock(iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006447 spd.nr_pages = i;
6448
6449 /* did we read anything? */
6450 if (!spd.nr_pages) {
Rabin Vincent07906da2014-11-06 22:26:07 +01006451 if (ret)
Al Viro1ae22932016-09-17 18:31:46 -04006452 goto out;
Rabin Vincent07906da2014-11-06 22:26:07 +01006453
Al Viro1ae22932016-09-17 18:31:46 -04006454 ret = -EAGAIN;
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006455 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
Al Viro1ae22932016-09-17 18:31:46 -04006456 goto out;
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006457
Rabin Vincente30f53a2014-11-10 19:46:34 +01006458 ret = wait_on_pipe(iter, true);
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04006459 if (ret)
Al Viro1ae22932016-09-17 18:31:46 -04006460 goto out;
Rabin Vincente30f53a2014-11-10 19:46:34 +01006461
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006462 goto again;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006463 }
6464
6465 ret = splice_to_pipe(pipe, &spd);
Al Viro1ae22932016-09-17 18:31:46 -04006466out:
Eric Dumazet047fe362012-06-12 15:24:40 +02006467 splice_shrink_spd(&spd);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006468
Steven Rostedt2cadf912008-12-01 22:20:19 -05006469 return ret;
6470}
6471
6472static const struct file_operations tracing_buffers_fops = {
6473 .open = tracing_buffers_open,
6474 .read = tracing_buffers_read,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006475 .poll = tracing_buffers_poll,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006476 .release = tracing_buffers_release,
6477 .splice_read = tracing_buffers_splice_read,
6478 .llseek = no_llseek,
6479};
6480
Steven Rostedtc8d77182009-04-29 18:03:45 -04006481static ssize_t
6482tracing_stats_read(struct file *filp, char __user *ubuf,
6483 size_t count, loff_t *ppos)
6484{
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006485 struct inode *inode = file_inode(filp);
6486 struct trace_array *tr = inode->i_private;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006487 struct trace_buffer *trace_buf = &tr->trace_buffer;
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006488 int cpu = tracing_get_cpu(inode);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006489 struct trace_seq *s;
6490 unsigned long cnt;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07006491 unsigned long long t;
6492 unsigned long usec_rem;
Steven Rostedtc8d77182009-04-29 18:03:45 -04006493
Li Zefane4f2d102009-06-15 10:57:28 +08006494 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006495 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01006496 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04006497
6498 trace_seq_init(s);
6499
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006500 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006501 trace_seq_printf(s, "entries: %ld\n", cnt);
6502
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006503 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006504 trace_seq_printf(s, "overrun: %ld\n", cnt);
6505
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006506 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006507 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
6508
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006509 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07006510 trace_seq_printf(s, "bytes: %ld\n", cnt);
6511
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09006512 if (trace_clocks[tr->clock_id].in_ns) {
Yoshihiro YUNOMAE11043d82012-11-13 12:18:23 -08006513 /* local or global for trace_clock */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006514 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d82012-11-13 12:18:23 -08006515 usec_rem = do_div(t, USEC_PER_SEC);
6516 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
6517 t, usec_rem);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07006518
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006519 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d82012-11-13 12:18:23 -08006520 usec_rem = do_div(t, USEC_PER_SEC);
6521 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
6522 } else {
6523 /* counter or tsc mode for trace_clock */
6524 trace_seq_printf(s, "oldest event ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006525 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d82012-11-13 12:18:23 -08006526
6527 trace_seq_printf(s, "now ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006528 ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d82012-11-13 12:18:23 -08006529 }
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07006530
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006531 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
Slava Pestov884bfe82011-07-15 14:23:58 -07006532 trace_seq_printf(s, "dropped events: %ld\n", cnt);
6533
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006534 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05006535 trace_seq_printf(s, "read events: %ld\n", cnt);
6536
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006537 count = simple_read_from_buffer(ubuf, count, ppos,
6538 s->buffer, trace_seq_used(s));
Steven Rostedtc8d77182009-04-29 18:03:45 -04006539
6540 kfree(s);
6541
6542 return count;
6543}
6544
6545static const struct file_operations tracing_stats_fops = {
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006546 .open = tracing_open_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04006547 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006548 .llseek = generic_file_llseek,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006549 .release = tracing_release_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04006550};
6551
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006552#ifdef CONFIG_DYNAMIC_FTRACE
6553
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006554int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006555{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006556 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006557}
6558
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006559static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006560tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006561 size_t cnt, loff_t *ppos)
6562{
Steven Rostedta26a2a22008-10-31 00:03:22 -04006563 static char ftrace_dyn_info_buffer[1024];
6564 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006565 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006566 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04006567 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006568 int r;
6569
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006570 mutex_lock(&dyn_info_mutex);
6571 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006572
Steven Rostedta26a2a22008-10-31 00:03:22 -04006573 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006574 buf[r++] = '\n';
6575
6576 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6577
6578 mutex_unlock(&dyn_info_mutex);
6579
6580 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006581}
6582
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006583static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006584 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006585 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006586 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006587};
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006588#endif /* CONFIG_DYNAMIC_FTRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006589
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006590#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
6591static void
6592ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006593{
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006594 tracing_snapshot();
6595}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006596
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006597static void
6598ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
6599{
6600 unsigned long *count = (long *)data;
6601
6602 if (!*count)
6603 return;
6604
6605 if (*count != -1)
6606 (*count)--;
6607
6608 tracing_snapshot();
6609}
6610
6611static int
6612ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
6613 struct ftrace_probe_ops *ops, void *data)
6614{
6615 long count = (long)data;
6616
6617 seq_printf(m, "%ps:", (void *)ip);
6618
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006619 seq_puts(m, "snapshot");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006620
6621 if (count == -1)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006622 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006623 else
6624 seq_printf(m, ":count=%ld\n", count);
6625
6626 return 0;
6627}
6628
6629static struct ftrace_probe_ops snapshot_probe_ops = {
6630 .func = ftrace_snapshot,
6631 .print = ftrace_snapshot_print,
6632};
6633
6634static struct ftrace_probe_ops snapshot_count_probe_ops = {
6635 .func = ftrace_count_snapshot,
6636 .print = ftrace_snapshot_print,
6637};
6638
6639static int
6640ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
6641 char *glob, char *cmd, char *param, int enable)
6642{
6643 struct ftrace_probe_ops *ops;
6644 void *count = (void *)-1;
6645 char *number;
6646 int ret;
6647
6648 /* hash funcs only work with set_ftrace_filter */
6649 if (!enable)
6650 return -EINVAL;
6651
6652 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
6653
6654 if (glob[0] == '!') {
6655 unregister_ftrace_function_probe_func(glob+1, ops);
6656 return 0;
6657 }
6658
6659 if (!param)
6660 goto out_reg;
6661
6662 number = strsep(&param, ":");
6663
6664 if (!strlen(number))
6665 goto out_reg;
6666
6667 /*
6668 * We use the callback data field (which is a pointer)
6669 * as our counter.
6670 */
6671 ret = kstrtoul(number, 0, (unsigned long *)&count);
6672 if (ret)
6673 return ret;
6674
6675 out_reg:
Steven Rostedt (VMware)d4decac2017-04-19 12:07:08 -04006676 ret = alloc_snapshot(&global_trace);
6677 if (ret < 0)
6678 goto out;
6679
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006680 ret = register_ftrace_function_probe(glob, ops, count);
6681
Steven Rostedt (VMware)d4decac2017-04-19 12:07:08 -04006682 out:
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006683 return ret < 0 ? ret : 0;
6684}
6685
6686static struct ftrace_func_command ftrace_snapshot_cmd = {
6687 .name = "snapshot",
6688 .func = ftrace_trace_snapshot_callback,
6689};
6690
Tom Zanussi38de93a2013-10-24 08:34:18 -05006691static __init int register_snapshot_cmd(void)
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006692{
6693 return register_ftrace_command(&ftrace_snapshot_cmd);
6694}
6695#else
Tom Zanussi38de93a2013-10-24 08:34:18 -05006696static inline __init int register_snapshot_cmd(void) { return 0; }
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006697#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006698
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006699static struct dentry *tracing_get_dentry(struct trace_array *tr)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006700{
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006701 if (WARN_ON(!tr->dir))
6702 return ERR_PTR(-ENODEV);
6703
6704 /* Top directory uses NULL as the parent */
6705 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
6706 return NULL;
6707
6708 /* All sub buffers have a descriptor */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006709 return tr->dir;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006710}
6711
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006712static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
6713{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006714 struct dentry *d_tracer;
6715
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006716 if (tr->percpu_dir)
6717 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006718
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006719 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05006720 if (IS_ERR(d_tracer))
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006721 return NULL;
6722
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006723 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006724
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006725 WARN_ONCE(!tr->percpu_dir,
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006726 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006727
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006728 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006729}
6730
Oleg Nesterov649e9c72013-07-23 17:25:54 +02006731static struct dentry *
6732trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
6733 void *data, long cpu, const struct file_operations *fops)
6734{
6735 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
6736
6737 if (ret) /* See tracing_get_cpu() */
David Howells7682c912015-03-17 22:26:16 +00006738 d_inode(ret)->i_cdev = (void *)(cpu + 1);
Oleg Nesterov649e9c72013-07-23 17:25:54 +02006739 return ret;
6740}
6741
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006742static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006743tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006744{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006745 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006746 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04006747 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006748
Namhyung Kim0a3d7ce2012-04-23 10:11:57 +09006749 if (!d_percpu)
6750 return;
6751
Steven Rostedtdd49a382010-10-20 21:51:26 -04006752 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006753 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01006754 if (!d_cpu) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07006755 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01006756 return;
6757 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006758
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01006759 /* per cpu trace_pipe */
Oleg Nesterov649e9c72013-07-23 17:25:54 +02006760 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
Oleg Nesterov15544202013-07-23 17:25:57 +02006761 tr, cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006762
6763 /* per cpu trace */
Oleg Nesterov649e9c72013-07-23 17:25:54 +02006764 trace_create_cpu_file("trace", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02006765 tr, cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04006766
Oleg Nesterov649e9c72013-07-23 17:25:54 +02006767 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006768 tr, cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006769
Oleg Nesterov649e9c72013-07-23 17:25:54 +02006770 trace_create_cpu_file("stats", 0444, d_cpu,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006771 tr, cpu, &tracing_stats_fops);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006772
Oleg Nesterov649e9c72013-07-23 17:25:54 +02006773 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006774 tr, cpu, &tracing_entries_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006775
6776#ifdef CONFIG_TRACER_SNAPSHOT
Oleg Nesterov649e9c72013-07-23 17:25:54 +02006777 trace_create_cpu_file("snapshot", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02006778 tr, cpu, &snapshot_fops);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006779
Oleg Nesterov649e9c72013-07-23 17:25:54 +02006780 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006781 tr, cpu, &snapshot_raw_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006782#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006783}
6784
Steven Rostedt60a11772008-05-12 21:20:44 +02006785#ifdef CONFIG_FTRACE_SELFTEST
6786/* Let selftest have access to static functions in this file */
6787#include "trace_selftest.c"
6788#endif
6789
Steven Rostedt577b7852009-02-26 23:43:05 -05006790static ssize_t
6791trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
6792 loff_t *ppos)
6793{
6794 struct trace_option_dentry *topt = filp->private_data;
6795 char *buf;
6796
6797 if (topt->flags->val & topt->opt->bit)
6798 buf = "1\n";
6799 else
6800 buf = "0\n";
6801
6802 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
6803}
6804
6805static ssize_t
6806trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
6807 loff_t *ppos)
6808{
6809 struct trace_option_dentry *topt = filp->private_data;
6810 unsigned long val;
Steven Rostedt577b7852009-02-26 23:43:05 -05006811 int ret;
6812
Peter Huewe22fe9b52011-06-07 21:58:27 +02006813 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6814 if (ret)
Steven Rostedt577b7852009-02-26 23:43:05 -05006815 return ret;
6816
Li Zefan8d18eaa2009-12-08 11:17:06 +08006817 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05006818 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08006819
6820 if (!!(topt->flags->val & topt->opt->bit) != val) {
6821 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05006822 ret = __set_tracer_option(topt->tr, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05006823 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08006824 mutex_unlock(&trace_types_lock);
6825 if (ret)
6826 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05006827 }
6828
6829 *ppos += cnt;
6830
6831 return cnt;
6832}
6833
6834
6835static const struct file_operations trace_options_fops = {
6836 .open = tracing_open_generic,
6837 .read = trace_options_read,
6838 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006839 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05006840};
6841
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006842/*
6843 * In order to pass in both the trace_array descriptor as well as the index
6844 * to the flag that the trace option file represents, the trace_array
6845 * has a character array of trace_flags_index[], which holds the index
6846 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
6847 * The address of this character array is passed to the flag option file
6848 * read/write callbacks.
6849 *
6850 * In order to extract both the index and the trace_array descriptor,
6851 * get_tr_index() uses the following algorithm.
6852 *
6853 * idx = *ptr;
6854 *
6855 * As the pointer itself contains the address of the index (remember
6856 * index[1] == 1).
6857 *
6858 * Then to get the trace_array descriptor, by subtracting that index
6859 * from the ptr, we get to the start of the index itself.
6860 *
6861 * ptr - idx == &index[0]
6862 *
6863 * Then a simple container_of() from that pointer gets us to the
6864 * trace_array descriptor.
6865 */
6866static void get_tr_index(void *data, struct trace_array **ptr,
6867 unsigned int *pindex)
6868{
6869 *pindex = *(unsigned char *)data;
6870
6871 *ptr = container_of(data - *pindex, struct trace_array,
6872 trace_flags_index);
6873}
6874
Steven Rostedta8259072009-02-26 22:19:12 -05006875static ssize_t
6876trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
6877 loff_t *ppos)
6878{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006879 void *tr_index = filp->private_data;
6880 struct trace_array *tr;
6881 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05006882 char *buf;
6883
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006884 get_tr_index(tr_index, &tr, &index);
6885
6886 if (tr->trace_flags & (1 << index))
Steven Rostedta8259072009-02-26 22:19:12 -05006887 buf = "1\n";
6888 else
6889 buf = "0\n";
6890
6891 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
6892}
6893
6894static ssize_t
6895trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
6896 loff_t *ppos)
6897{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006898 void *tr_index = filp->private_data;
6899 struct trace_array *tr;
6900 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05006901 unsigned long val;
6902 int ret;
6903
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006904 get_tr_index(tr_index, &tr, &index);
6905
Peter Huewe22fe9b52011-06-07 21:58:27 +02006906 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6907 if (ret)
Steven Rostedta8259072009-02-26 22:19:12 -05006908 return ret;
6909
Zhaoleif2d84b62009-08-07 18:55:48 +08006910 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05006911 return -EINVAL;
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04006912
6913 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006914 ret = set_tracer_flag(tr, 1 << index, val);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04006915 mutex_unlock(&trace_types_lock);
Steven Rostedta8259072009-02-26 22:19:12 -05006916
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04006917 if (ret < 0)
6918 return ret;
6919
Steven Rostedta8259072009-02-26 22:19:12 -05006920 *ppos += cnt;
6921
6922 return cnt;
6923}
6924
Steven Rostedta8259072009-02-26 22:19:12 -05006925static const struct file_operations trace_options_core_fops = {
6926 .open = tracing_open_generic,
6927 .read = trace_options_core_read,
6928 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006929 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05006930};
6931
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006932struct dentry *trace_create_file(const char *name,
Al Virof4ae40a2011-07-24 04:33:43 -04006933 umode_t mode,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006934 struct dentry *parent,
6935 void *data,
6936 const struct file_operations *fops)
6937{
6938 struct dentry *ret;
6939
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006940 ret = tracefs_create_file(name, mode, parent, data, fops);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006941 if (!ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -07006942 pr_warn("Could not create tracefs '%s' entry\n", name);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006943
6944 return ret;
6945}
6946
6947
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006948static struct dentry *trace_options_init_dentry(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05006949{
6950 struct dentry *d_tracer;
Steven Rostedta8259072009-02-26 22:19:12 -05006951
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006952 if (tr->options)
6953 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05006954
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006955 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05006956 if (IS_ERR(d_tracer))
Steven Rostedta8259072009-02-26 22:19:12 -05006957 return NULL;
6958
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006959 tr->options = tracefs_create_dir("options", d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006960 if (!tr->options) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07006961 pr_warn("Could not create tracefs directory 'options'\n");
Steven Rostedta8259072009-02-26 22:19:12 -05006962 return NULL;
6963 }
6964
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006965 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05006966}
6967
Steven Rostedt577b7852009-02-26 23:43:05 -05006968static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006969create_trace_option_file(struct trace_array *tr,
6970 struct trace_option_dentry *topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05006971 struct tracer_flags *flags,
6972 struct tracer_opt *opt)
6973{
6974 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05006975
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006976 t_options = trace_options_init_dentry(tr);
Steven Rostedt577b7852009-02-26 23:43:05 -05006977 if (!t_options)
6978 return;
6979
6980 topt->flags = flags;
6981 topt->opt = opt;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006982 topt->tr = tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05006983
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006984 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05006985 &trace_options_fops);
6986
Steven Rostedt577b7852009-02-26 23:43:05 -05006987}
6988
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04006989static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006990create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
Steven Rostedt577b7852009-02-26 23:43:05 -05006991{
6992 struct trace_option_dentry *topts;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04006993 struct trace_options *tr_topts;
Steven Rostedt577b7852009-02-26 23:43:05 -05006994 struct tracer_flags *flags;
6995 struct tracer_opt *opts;
6996 int cnt;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04006997 int i;
Steven Rostedt577b7852009-02-26 23:43:05 -05006998
6999 if (!tracer)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007000 return;
Steven Rostedt577b7852009-02-26 23:43:05 -05007001
7002 flags = tracer->flags;
7003
7004 if (!flags || !flags->opts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007005 return;
7006
7007 /*
7008 * If this is an instance, only create flags for tracers
7009 * the instance may have.
7010 */
7011 if (!trace_ok_for_array(tracer, tr))
7012 return;
7013
7014 for (i = 0; i < tr->nr_topts; i++) {
Chunyu Hud39cdd22016-03-08 21:37:01 +08007015 /* Make sure there's no duplicate flags. */
7016 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007017 return;
7018 }
Steven Rostedt577b7852009-02-26 23:43:05 -05007019
7020 opts = flags->opts;
7021
7022 for (cnt = 0; opts[cnt].name; cnt++)
7023 ;
7024
Steven Rostedt0cfe8242009-02-27 10:51:10 -05007025 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05007026 if (!topts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007027 return;
7028
7029 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
7030 GFP_KERNEL);
7031 if (!tr_topts) {
7032 kfree(topts);
7033 return;
7034 }
7035
7036 tr->topts = tr_topts;
7037 tr->topts[tr->nr_topts].tracer = tracer;
7038 tr->topts[tr->nr_topts].topts = topts;
7039 tr->nr_topts++;
Steven Rostedt577b7852009-02-26 23:43:05 -05007040
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04007041 for (cnt = 0; opts[cnt].name; cnt++) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007042 create_trace_option_file(tr, &topts[cnt], flags,
Steven Rostedt577b7852009-02-26 23:43:05 -05007043 &opts[cnt]);
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04007044 WARN_ONCE(topts[cnt].entry == NULL,
7045 "Failed to create trace option: %s",
7046 opts[cnt].name);
7047 }
Steven Rostedt577b7852009-02-26 23:43:05 -05007048}
7049
Steven Rostedta8259072009-02-26 22:19:12 -05007050static struct dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007051create_trace_option_core_file(struct trace_array *tr,
7052 const char *option, long index)
Steven Rostedta8259072009-02-26 22:19:12 -05007053{
7054 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05007055
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007056 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05007057 if (!t_options)
7058 return NULL;
7059
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007060 return trace_create_file(option, 0644, t_options,
7061 (void *)&tr->trace_flags_index[index],
7062 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05007063}
7064
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04007065static void create_trace_options_dir(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05007066{
7067 struct dentry *t_options;
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04007068 bool top_level = tr == &global_trace;
Steven Rostedta8259072009-02-26 22:19:12 -05007069 int i;
7070
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007071 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05007072 if (!t_options)
7073 return;
7074
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04007075 for (i = 0; trace_options[i]; i++) {
7076 if (top_level ||
7077 !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
7078 create_trace_option_core_file(tr, trace_options[i], i);
7079 }
Steven Rostedta8259072009-02-26 22:19:12 -05007080}
7081
Steven Rostedt499e5472012-02-22 15:50:28 -05007082static ssize_t
7083rb_simple_read(struct file *filp, char __user *ubuf,
7084 size_t cnt, loff_t *ppos)
7085{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04007086 struct trace_array *tr = filp->private_data;
Steven Rostedt499e5472012-02-22 15:50:28 -05007087 char buf[64];
7088 int r;
7089
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04007090 r = tracer_tracing_is_on(tr);
Steven Rostedt499e5472012-02-22 15:50:28 -05007091 r = sprintf(buf, "%d\n", r);
7092
7093 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7094}
7095
7096static ssize_t
7097rb_simple_write(struct file *filp, const char __user *ubuf,
7098 size_t cnt, loff_t *ppos)
7099{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04007100 struct trace_array *tr = filp->private_data;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007101 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt499e5472012-02-22 15:50:28 -05007102 unsigned long val;
7103 int ret;
7104
7105 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7106 if (ret)
7107 return ret;
7108
7109 if (buffer) {
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05007110 mutex_lock(&trace_types_lock);
Steven Rostedt (VMware)dc697312018-08-01 15:40:57 -04007111 if (!!val == tracer_tracing_is_on(tr)) {
7112 val = 0; /* do nothing */
7113 } else if (val) {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04007114 tracer_tracing_on(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007115 if (tr->current_trace->start)
7116 tr->current_trace->start(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05007117 } else {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04007118 tracer_tracing_off(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007119 if (tr->current_trace->stop)
7120 tr->current_trace->stop(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05007121 }
7122 mutex_unlock(&trace_types_lock);
Steven Rostedt499e5472012-02-22 15:50:28 -05007123 }
7124
7125 (*ppos)++;
7126
7127 return cnt;
7128}
7129
7130static const struct file_operations rb_simple_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007131 .open = tracing_open_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05007132 .read = rb_simple_read,
7133 .write = rb_simple_write,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007134 .release = tracing_release_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05007135 .llseek = default_llseek,
7136};
7137
Steven Rostedt277ba042012-08-03 16:10:49 -04007138struct dentry *trace_instance_dir;
7139
7140static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007141init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
Steven Rostedt277ba042012-08-03 16:10:49 -04007142
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007143static int
7144allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
Steven Rostedt277ba042012-08-03 16:10:49 -04007145{
7146 enum ring_buffer_flags rb_flags;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007147
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007148 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007149
Steven Rostedt (Red Hat)dced3412014-01-14 10:19:46 -05007150 buf->tr = tr;
7151
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007152 buf->buffer = ring_buffer_alloc(size, rb_flags);
7153 if (!buf->buffer)
7154 return -ENOMEM;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007155
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007156 buf->data = alloc_percpu(struct trace_array_cpu);
7157 if (!buf->data) {
7158 ring_buffer_free(buf->buffer);
Steven Rostedt (VMware)5dc4cd22017-12-26 20:07:34 -05007159 buf->buffer = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007160 return -ENOMEM;
7161 }
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007162
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007163 /* Allocate the first page for all buffers */
7164 set_buffer_entries(&tr->trace_buffer,
7165 ring_buffer_size(tr->trace_buffer.buffer, 0));
7166
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007167 return 0;
7168}
7169
7170static int allocate_trace_buffers(struct trace_array *tr, int size)
7171{
7172 int ret;
7173
7174 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
7175 if (ret)
7176 return ret;
7177
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007178#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007179 ret = allocate_trace_buffer(tr, &tr->max_buffer,
7180 allocate_snapshot ? size : 1);
7181 if (WARN_ON(ret)) {
7182 ring_buffer_free(tr->trace_buffer.buffer);
Jing Xia81e155e2017-12-26 15:12:53 +08007183 tr->trace_buffer.buffer = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007184 free_percpu(tr->trace_buffer.data);
Jing Xia81e155e2017-12-26 15:12:53 +08007185 tr->trace_buffer.data = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007186 return -ENOMEM;
7187 }
7188 tr->allocated_snapshot = allocate_snapshot;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007189
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05007190 /*
7191 * Only the top level trace array gets its snapshot allocated
7192 * from the kernel command line.
7193 */
7194 allocate_snapshot = false;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007195#endif
Steven Rostedt (VMware)d6e55a32020-05-06 10:36:18 -04007196
7197 /*
7198 * Because of some magic with the way alloc_percpu() works on
7199 * x86_64, we need to synchronize the pgd of all the tables,
7200 * otherwise the trace events that happen in x86_64 page fault
7201 * handlers can't cope with accessing the chance that a
7202 * alloc_percpu()'d memory might be touched in the page fault trace
7203 * event. Oh, and we need to audit all other alloc_percpu() and vmalloc()
7204 * calls in tracing, because something might get triggered within a
7205 * page fault trace event!
7206 */
7207 vmalloc_sync_mappings();
7208
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007209 return 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007210}
7211
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04007212static void free_trace_buffer(struct trace_buffer *buf)
7213{
7214 if (buf->buffer) {
7215 ring_buffer_free(buf->buffer);
7216 buf->buffer = NULL;
7217 free_percpu(buf->data);
7218 buf->data = NULL;
7219 }
7220}
7221
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04007222static void free_trace_buffers(struct trace_array *tr)
7223{
7224 if (!tr)
7225 return;
7226
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04007227 free_trace_buffer(&tr->trace_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04007228
7229#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04007230 free_trace_buffer(&tr->max_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04007231#endif
7232}
7233
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007234static void init_trace_flags_index(struct trace_array *tr)
7235{
7236 int i;
7237
7238 /* Used by the trace options files */
7239 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
7240 tr->trace_flags_index[i] = i;
7241}
7242
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007243static void __update_tracer_options(struct trace_array *tr)
7244{
7245 struct tracer *t;
7246
7247 for (t = trace_types; t; t = t->next)
7248 add_tracer_options(tr, t);
7249}
7250
7251static void update_tracer_options(struct trace_array *tr)
7252{
7253 mutex_lock(&trace_types_lock);
7254 __update_tracer_options(tr);
7255 mutex_unlock(&trace_types_lock);
7256}
7257
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05007258static int instance_mkdir(const char *name)
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007259{
Steven Rostedt277ba042012-08-03 16:10:49 -04007260 struct trace_array *tr;
7261 int ret;
Steven Rostedt277ba042012-08-03 16:10:49 -04007262
7263 mutex_lock(&trace_types_lock);
7264
7265 ret = -EEXIST;
7266 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7267 if (tr->name && strcmp(tr->name, name) == 0)
7268 goto out_unlock;
7269 }
7270
7271 ret = -ENOMEM;
7272 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
7273 if (!tr)
7274 goto out_unlock;
7275
7276 tr->name = kstrdup(name, GFP_KERNEL);
7277 if (!tr->name)
7278 goto out_free_tr;
7279
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007280 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
7281 goto out_free_tr;
7282
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04007283 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007284
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007285 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
7286
Steven Rostedt277ba042012-08-03 16:10:49 -04007287 raw_spin_lock_init(&tr->start_lock);
7288
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05007289 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
7290
Steven Rostedt277ba042012-08-03 16:10:49 -04007291 tr->current_trace = &nop_trace;
7292
7293 INIT_LIST_HEAD(&tr->systems);
7294 INIT_LIST_HEAD(&tr->events);
7295
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007296 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
Steven Rostedt277ba042012-08-03 16:10:49 -04007297 goto out_free_tr;
7298
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007299 tr->dir = tracefs_create_dir(name, trace_instance_dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04007300 if (!tr->dir)
7301 goto out_free_tr;
7302
7303 ret = event_trace_add_tracer(tr->dir, tr);
Alexander Z Lam609e85a2013-07-10 17:34:34 -07007304 if (ret) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007305 tracefs_remove_recursive(tr->dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04007306 goto out_free_tr;
Alexander Z Lam609e85a2013-07-10 17:34:34 -07007307 }
Steven Rostedt277ba042012-08-03 16:10:49 -04007308
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007309 init_tracer_tracefs(tr, tr->dir);
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007310 init_trace_flags_index(tr);
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007311 __update_tracer_options(tr);
Steven Rostedt277ba042012-08-03 16:10:49 -04007312
7313 list_add(&tr->list, &ftrace_trace_arrays);
7314
7315 mutex_unlock(&trace_types_lock);
7316
7317 return 0;
7318
7319 out_free_tr:
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04007320 free_trace_buffers(tr);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007321 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt277ba042012-08-03 16:10:49 -04007322 kfree(tr->name);
7323 kfree(tr);
7324
7325 out_unlock:
7326 mutex_unlock(&trace_types_lock);
7327
7328 return ret;
7329
7330}
7331
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05007332static int instance_rmdir(const char *name)
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007333{
7334 struct trace_array *tr;
7335 int found = 0;
7336 int ret;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007337 int i;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007338
7339 mutex_lock(&trace_types_lock);
7340
7341 ret = -ENODEV;
7342 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7343 if (tr->name && strcmp(tr->name, name) == 0) {
7344 found = 1;
7345 break;
7346 }
7347 }
7348 if (!found)
7349 goto out_unlock;
7350
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007351 ret = -EBUSY;
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05007352 if (tr->ref || (tr->current_trace && tr->current_trace->ref))
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007353 goto out_unlock;
7354
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007355 list_del(&tr->list);
7356
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04007357 /* Disable all the flags that were enabled coming in */
7358 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
7359 if ((1 << i) & ZEROED_TRACE_FLAGS)
7360 set_tracer_flag(tr, 1 << i, 0);
7361 }
7362
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05007363 tracing_set_nop(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007364 event_trace_del_tracer(tr);
Namhyung Kim7da0f8e2017-04-17 11:44:27 +09007365 ftrace_clear_pids(tr);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05007366 ftrace_destroy_function_files(tr);
Jiaxing Wang681a4a22015-10-18 19:58:08 +08007367 tracefs_remove_recursive(tr->dir);
Steven Rostedt (Red Hat)a9fcaaa2014-06-06 23:17:28 -04007368 free_trace_buffers(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007369
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007370 for (i = 0; i < tr->nr_topts; i++) {
7371 kfree(tr->topts[i].topts);
7372 }
7373 kfree(tr->topts);
7374
Chunyu Hu919e4812017-07-20 18:36:09 +08007375 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007376 kfree(tr->name);
7377 kfree(tr);
7378
7379 ret = 0;
7380
7381 out_unlock:
7382 mutex_unlock(&trace_types_lock);
7383
7384 return ret;
7385}
7386
Steven Rostedt277ba042012-08-03 16:10:49 -04007387static __init void create_trace_instances(struct dentry *d_tracer)
7388{
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05007389 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
7390 instance_mkdir,
7391 instance_rmdir);
Steven Rostedt277ba042012-08-03 16:10:49 -04007392 if (WARN_ON(!trace_instance_dir))
7393 return;
Steven Rostedt277ba042012-08-03 16:10:49 -04007394}
7395
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007396static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007397init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007398{
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05007399 int cpu;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007400
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05007401 trace_create_file("available_tracers", 0444, d_tracer,
7402 tr, &show_traces_fops);
7403
7404 trace_create_file("current_tracer", 0644, d_tracer,
7405 tr, &set_tracer_fops);
7406
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007407 trace_create_file("tracing_cpumask", 0644, d_tracer,
7408 tr, &tracing_cpumask_fops);
7409
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007410 trace_create_file("trace_options", 0644, d_tracer,
7411 tr, &tracing_iter_fops);
7412
7413 trace_create_file("trace", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007414 tr, &tracing_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007415
7416 trace_create_file("trace_pipe", 0444, d_tracer,
Oleg Nesterov15544202013-07-23 17:25:57 +02007417 tr, &tracing_pipe_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007418
7419 trace_create_file("buffer_size_kb", 0644, d_tracer,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02007420 tr, &tracing_entries_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007421
7422 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
7423 tr, &tracing_total_entries_fops);
7424
Wang YanQing238ae932013-05-26 16:52:01 +08007425 trace_create_file("free_buffer", 0200, d_tracer,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007426 tr, &tracing_free_buffer_fops);
7427
7428 trace_create_file("trace_marker", 0220, d_tracer,
7429 tr, &tracing_mark_fops);
7430
Jamie Gennis13b625d2012-11-21 15:04:25 -08007431 trace_create_file("saved_tgids", 0444, d_tracer,
7432 tr, &tracing_saved_tgids_fops);
7433
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007434 trace_create_file("trace_clock", 0644, d_tracer, tr,
7435 &trace_clock_fops);
7436
7437 trace_create_file("tracing_on", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007438 tr, &rb_simple_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05007439
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04007440 create_trace_options_dir(tr);
7441
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04007442#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05007443 trace_create_file("tracing_max_latency", 0644, d_tracer,
7444 &tr->max_latency, &tracing_max_lat_fops);
7445#endif
7446
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05007447 if (ftrace_create_function_files(tr, d_tracer))
7448 WARN(1, "Could not allocate function filter files");
7449
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05007450#ifdef CONFIG_TRACER_SNAPSHOT
7451 trace_create_file("snapshot", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007452 tr, &snapshot_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05007453#endif
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05007454
7455 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007456 tracing_init_tracefs_percpu(tr, cpu);
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05007457
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007458 ftrace_init_tracefs(tr, d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007459}
7460
Eric W. Biedermand3381fa2017-02-01 06:06:16 +13007461static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05007462{
7463 struct vfsmount *mnt;
7464 struct file_system_type *type;
7465
7466 /*
7467 * To maintain backward compatibility for tools that mount
7468 * debugfs to get to the tracing facility, tracefs is automatically
7469 * mounted to the debugfs/tracing directory.
7470 */
7471 type = get_fs_type("tracefs");
7472 if (!type)
7473 return NULL;
Eric W. Biedermand3381fa2017-02-01 06:06:16 +13007474 mnt = vfs_submount(mntpt, type, "tracefs", NULL);
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05007475 put_filesystem(type);
7476 if (IS_ERR(mnt))
7477 return NULL;
7478 mntget(mnt);
7479
7480 return mnt;
7481}
7482
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007483/**
7484 * tracing_init_dentry - initialize top level trace array
7485 *
7486 * This is called when creating files or directories in the tracing
7487 * directory. It is called via fs_initcall() by any of the boot up code
7488 * and expects to return the dentry of the top level tracing directory.
7489 */
7490struct dentry *tracing_init_dentry(void)
7491{
7492 struct trace_array *tr = &global_trace;
7493
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05007494 /* The top level trace array uses NULL as parent */
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007495 if (tr->dir)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05007496 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007497
Jiaxing Wang8b129192015-11-06 16:04:16 +08007498 if (WARN_ON(!tracefs_initialized()) ||
7499 (IS_ENABLED(CONFIG_DEBUG_FS) &&
7500 WARN_ON(!debugfs_initialized())))
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007501 return ERR_PTR(-ENODEV);
7502
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05007503 /*
7504 * As there may still be users that expect the tracing
7505 * files to exist in debugfs/tracing, we must automount
7506 * the tracefs file system there, so older tools still
7507 * work with the newer kerenl.
7508 */
7509 tr->dir = debugfs_create_automount("tracing", NULL,
7510 trace_automount, NULL);
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007511 if (!tr->dir) {
7512 pr_warn_once("Could not create debugfs directory 'tracing'\n");
7513 return ERR_PTR(-ENOMEM);
7514 }
7515
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007516 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007517}
7518
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04007519extern struct trace_enum_map *__start_ftrace_enum_maps[];
7520extern struct trace_enum_map *__stop_ftrace_enum_maps[];
7521
7522static void __init trace_enum_init(void)
7523{
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007524 int len;
7525
7526 len = __stop_ftrace_enum_maps - __start_ftrace_enum_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007527 trace_insert_enum_map(NULL, __start_ftrace_enum_maps, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04007528}
7529
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007530#ifdef CONFIG_MODULES
7531static void trace_module_add_enums(struct module *mod)
7532{
7533 if (!mod->num_trace_enums)
7534 return;
7535
7536 /*
7537 * Modules with bad taint do not have events created, do
7538 * not bother with enums either.
7539 */
7540 if (trace_module_has_bad_taint(mod))
7541 return;
7542
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007543 trace_insert_enum_map(mod, mod->trace_enums, mod->num_trace_enums);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007544}
7545
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007546#ifdef CONFIG_TRACE_ENUM_MAP_FILE
7547static void trace_module_remove_enums(struct module *mod)
7548{
7549 union trace_enum_map_item *map;
7550 union trace_enum_map_item **last = &trace_enum_maps;
7551
7552 if (!mod->num_trace_enums)
7553 return;
7554
7555 mutex_lock(&trace_enum_mutex);
7556
7557 map = trace_enum_maps;
7558
7559 while (map) {
7560 if (map->head.mod == mod)
7561 break;
7562 map = trace_enum_jmp_to_tail(map);
7563 last = &map->tail.next;
7564 map = map->tail.next;
7565 }
7566 if (!map)
7567 goto out;
7568
7569 *last = trace_enum_jmp_to_tail(map)->tail.next;
7570 kfree(map);
7571 out:
7572 mutex_unlock(&trace_enum_mutex);
7573}
7574#else
7575static inline void trace_module_remove_enums(struct module *mod) { }
7576#endif /* CONFIG_TRACE_ENUM_MAP_FILE */
7577
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007578static int trace_module_notify(struct notifier_block *self,
7579 unsigned long val, void *data)
7580{
7581 struct module *mod = data;
7582
7583 switch (val) {
7584 case MODULE_STATE_COMING:
7585 trace_module_add_enums(mod);
7586 break;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007587 case MODULE_STATE_GOING:
7588 trace_module_remove_enums(mod);
7589 break;
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007590 }
7591
7592 return 0;
7593}
7594
7595static struct notifier_block trace_module_nb = {
7596 .notifier_call = trace_module_notify,
7597 .priority = 0,
7598};
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007599#endif /* CONFIG_MODULES */
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007600
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007601static __init int tracer_init_tracefs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007602{
7603 struct dentry *d_tracer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007604
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08007605 trace_access_lock_init();
7606
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007607 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05007608 if (IS_ERR(d_tracer))
Namhyung Kimed6f1c92013-04-10 09:18:12 +09007609 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007610
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007611 init_tracer_tracefs(&global_trace, d_tracer);
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04007612 ftrace_init_tracefs_toplevel(&global_trace, d_tracer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007613
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007614 trace_create_file("tracing_thresh", 0644, d_tracer,
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04007615 &global_trace, &tracing_thresh_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007616
Li Zefan339ae5d2009-04-17 10:34:30 +08007617 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007618 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02007619
Avadh Patel69abe6a2009-04-10 16:04:48 -04007620 trace_create_file("saved_cmdlines", 0444, d_tracer,
7621 NULL, &tracing_saved_cmdlines_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03007622
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09007623 trace_create_file("saved_cmdlines_size", 0644, d_tracer,
7624 NULL, &tracing_saved_cmdlines_size_fops);
7625
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04007626 trace_enum_init();
7627
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007628 trace_create_enum_file(d_tracer);
7629
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007630#ifdef CONFIG_MODULES
7631 register_module_notifier(&trace_module_nb);
7632#endif
7633
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007634#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007635 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
7636 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007637#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007638
Steven Rostedt277ba042012-08-03 16:10:49 -04007639 create_trace_instances(d_tracer);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09007640
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007641 update_tracer_options(&global_trace);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05007642
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01007643 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007644}
7645
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007646static int trace_panic_handler(struct notifier_block *this,
7647 unsigned long event, void *unused)
7648{
Steven Rostedt944ac422008-10-23 19:26:08 -04007649 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007650 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007651 return NOTIFY_OK;
7652}
7653
7654static struct notifier_block trace_panic_notifier = {
7655 .notifier_call = trace_panic_handler,
7656 .next = NULL,
7657 .priority = 150 /* priority: INT_MAX >= x >= 0 */
7658};
7659
7660static int trace_die_handler(struct notifier_block *self,
7661 unsigned long val,
7662 void *data)
7663{
7664 switch (val) {
7665 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04007666 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007667 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007668 break;
7669 default:
7670 break;
7671 }
7672 return NOTIFY_OK;
7673}
7674
7675static struct notifier_block trace_die_notifier = {
7676 .notifier_call = trace_die_handler,
7677 .priority = 200
7678};
7679
7680/*
7681 * printk is set to max of 1024, we really don't need it that big.
7682 * Nothing should be printing 1000 characters anyway.
7683 */
7684#define TRACE_MAX_PRINT 1000
7685
7686/*
7687 * Define here KERN_TRACE so that we have one place to modify
7688 * it if we decide to change what log level the ftrace dump
7689 * should be at.
7690 */
Steven Rostedt428aee12009-01-14 12:24:42 -05007691#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007692
Jason Wessel955b61e2010-08-05 09:22:23 -05007693void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007694trace_printk_seq(struct trace_seq *s)
7695{
7696 /* Probably should print a warning here. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04007697 if (s->seq.len >= TRACE_MAX_PRINT)
7698 s->seq.len = TRACE_MAX_PRINT;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007699
Steven Rostedt (Red Hat)820b75f2014-11-19 10:56:41 -05007700 /*
7701 * More paranoid code. Although the buffer size is set to
7702 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
7703 * an extra layer of protection.
7704 */
7705 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
7706 s->seq.len = s->seq.size - 1;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007707
7708 /* should be zero ended, but we are paranoid. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04007709 s->buffer[s->seq.len] = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007710
7711 printk(KERN_TRACE "%s", s->buffer);
7712
Steven Rostedtf9520752009-03-02 14:04:40 -05007713 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007714}
7715
Jason Wessel955b61e2010-08-05 09:22:23 -05007716void trace_init_global_iter(struct trace_iterator *iter)
7717{
7718 iter->tr = &global_trace;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007719 iter->trace = iter->tr->current_trace;
Steven Rostedtae3b5092013-01-23 15:22:59 -05007720 iter->cpu_file = RING_BUFFER_ALL_CPUS;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007721 iter->trace_buffer = &global_trace.trace_buffer;
Cody P Schaferb2f974d2013-10-23 11:49:57 -07007722
7723 if (iter->trace && iter->trace->open)
7724 iter->trace->open(iter);
7725
7726 /* Annotate start of buffers if we had overruns */
7727 if (ring_buffer_overruns(iter->trace_buffer->buffer))
7728 iter->iter_flags |= TRACE_FILE_ANNOTATE;
7729
7730 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
7731 if (trace_clocks[iter->tr->clock_id].in_ns)
7732 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
Jason Wessel955b61e2010-08-05 09:22:23 -05007733}
7734
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007735void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007736{
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007737 /* use static because iter can be a bit big for the stack */
7738 static struct trace_iterator iter;
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007739 static atomic_t dump_running;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007740 struct trace_array *tr = &global_trace;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007741 unsigned int old_userobj;
Steven Rostedtd7690412008-10-01 00:29:53 -04007742 unsigned long flags;
7743 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007744
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007745 /* Only allow one dump user at a time. */
7746 if (atomic_inc_return(&dump_running) != 1) {
7747 atomic_dec(&dump_running);
7748 return;
Steven Rostedte0a413f2011-09-29 21:26:16 -04007749 }
7750
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007751 /*
7752 * Always turn off tracing when we dump.
7753 * We don't need to show trace output of what happens
7754 * between multiple crashes.
7755 *
7756 * If the user does a sysrq-z, then they can re-enable
7757 * tracing with echo 1 > tracing_on.
7758 */
7759 tracing_off();
7760
7761 local_irq_save(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007762
Jovi Zhang38dbe0b2013-01-25 18:03:07 +08007763 /* Simulate the iterator */
Jason Wessel955b61e2010-08-05 09:22:23 -05007764 trace_init_global_iter(&iter);
7765
Steven Rostedtd7690412008-10-01 00:29:53 -04007766 for_each_tracing_cpu(cpu) {
Umesh Tiwari5e2d5ef2015-06-22 16:55:06 +05307767 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04007768 }
7769
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007770 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007771
Török Edwinb54d3de2008-11-22 13:28:48 +02007772 /* don't look at user memory in panic mode */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007773 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
Török Edwinb54d3de2008-11-22 13:28:48 +02007774
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007775 switch (oops_dump_mode) {
7776 case DUMP_ALL:
Steven Rostedtae3b5092013-01-23 15:22:59 -05007777 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007778 break;
7779 case DUMP_ORIG:
7780 iter.cpu_file = raw_smp_processor_id();
7781 break;
7782 case DUMP_NONE:
7783 goto out_enable;
7784 default:
7785 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
Steven Rostedtae3b5092013-01-23 15:22:59 -05007786 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007787 }
7788
7789 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007790
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007791 /* Did function tracer already get disabled? */
7792 if (ftrace_is_dead()) {
7793 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
7794 printk("# MAY BE MISSING FUNCTION EVENTS\n");
7795 }
7796
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007797 /*
7798 * We need to stop all tracing on all CPUS to read the
7799 * the next buffer. This is a bit expensive, but is
7800 * not done often. We fill all what we can read,
7801 * and then release the locks again.
7802 */
7803
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007804 while (!trace_empty(&iter)) {
7805
7806 if (!cnt)
7807 printk(KERN_TRACE "---------------------------------\n");
7808
7809 cnt++;
7810
Miguel Ojeda2e415392019-05-23 14:45:35 +02007811 trace_iterator_reset(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007812 iter.iter_flags |= TRACE_FILE_LAT_FMT;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007813
Jason Wessel955b61e2010-08-05 09:22:23 -05007814 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08007815 int ret;
7816
7817 ret = print_trace_line(&iter);
7818 if (ret != TRACE_TYPE_NO_CONSUME)
7819 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007820 }
Steven Rostedtb892e5c2012-03-01 22:06:48 -05007821 touch_nmi_watchdog();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007822
7823 trace_printk_seq(&iter.seq);
7824 }
7825
7826 if (!cnt)
7827 printk(KERN_TRACE " (ftrace buffer empty)\n");
7828 else
7829 printk(KERN_TRACE "---------------------------------\n");
7830
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007831 out_enable:
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007832 tr->trace_flags |= old_userobj;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007833
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007834 for_each_tracing_cpu(cpu) {
7835 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007836 }
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007837 atomic_dec(&dump_running);
Steven Rostedtcd891ae2009-04-28 11:39:34 -04007838 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007839}
Paul E. McKenneya8eecf22011-10-02 11:01:15 -07007840EXPORT_SYMBOL_GPL(ftrace_dump);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007841
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007842__init static int tracer_alloc_buffers(void)
7843{
Steven Rostedt73c51622009-03-11 13:42:01 -04007844 int ring_buf_size;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307845 int ret = -ENOMEM;
7846
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04007847 /*
7848 * Make sure we don't accidently add more trace options
7849 * than we have bits for.
7850 */
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007851 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04007852
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307853 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
7854 goto out;
7855
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007856 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307857 goto out_free_buffer_mask;
7858
Steven Rostedt07d777f2011-09-22 14:01:55 -04007859 /* Only allocate trace_printk buffers if a trace_printk exists */
Nathan Chancellorefff0332020-02-19 22:10:12 -07007860 if (&__stop___trace_bprintk_fmt != &__start___trace_bprintk_fmt)
Steven Rostedt81698832012-10-11 10:15:05 -04007861 /* Must be called before global_trace.buffer is allocated */
Steven Rostedt07d777f2011-09-22 14:01:55 -04007862 trace_printk_init_buffers();
7863
Steven Rostedt73c51622009-03-11 13:42:01 -04007864 /* To save memory, keep the ring buffer size to its minimum */
7865 if (ring_buffer_expanded)
7866 ring_buf_size = trace_buf_size;
7867 else
7868 ring_buf_size = 1;
7869
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307870 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007871 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007872
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007873 raw_spin_lock_init(&global_trace.start_lock);
7874
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04007875 /* Used for event triggers */
7876 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
7877 if (!temp_buffer)
7878 goto out_free_cpumask;
7879
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09007880 if (trace_create_savedcmd() < 0)
7881 goto out_free_temp_buffer;
7882
Steven Rostedtab464282008-05-12 21:21:00 +02007883 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007884 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007885 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
7886 WARN_ON(1);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09007887 goto out_free_savedcmd;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007888 }
Steven Rostedta7603ff2012-08-06 16:24:11 -04007889
Steven Rostedt499e5472012-02-22 15:50:28 -05007890 if (global_trace.buffer_disabled)
7891 tracing_off();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007892
Steven Rostedte1e232c2014-02-10 23:38:46 -05007893 if (trace_boot_clock) {
7894 ret = tracing_set_clock(&global_trace, trace_boot_clock);
7895 if (ret < 0)
Joe Perchesa395d6a2016-03-22 14:28:09 -07007896 pr_warn("Trace clock %s not defined, going back to default\n",
7897 trace_boot_clock);
Steven Rostedte1e232c2014-02-10 23:38:46 -05007898 }
7899
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04007900 /*
7901 * register_tracer() might reference current_trace, so it
7902 * needs to be set before we register anything. This is
7903 * just a bootstrap of current_trace anyway.
7904 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007905 global_trace.current_trace = &nop_trace;
7906
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05007907 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
7908
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007909 ftrace_init_global_array_ops(&global_trace);
7910
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007911 init_trace_flags_index(&global_trace);
7912
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04007913 register_tracer(&nop_trace);
7914
Steven Rostedt60a11772008-05-12 21:20:44 +02007915 /* All seems OK, enable tracing */
7916 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007917
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007918 atomic_notifier_chain_register(&panic_notifier_list,
7919 &trace_panic_notifier);
7920
7921 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01007922
Steven Rostedtae63b312012-05-03 23:09:03 -04007923 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
7924
7925 INIT_LIST_HEAD(&global_trace.systems);
7926 INIT_LIST_HEAD(&global_trace.events);
7927 list_add(&global_trace.list, &ftrace_trace_arrays);
7928
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08007929 apply_trace_boot_options();
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04007930
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007931 register_snapshot_cmd();
7932
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01007933 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007934
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09007935out_free_savedcmd:
7936 free_saved_cmdlines_buffer(savedcmd);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04007937out_free_temp_buffer:
7938 ring_buffer_free(temp_buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307939out_free_cpumask:
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007940 free_cpumask_var(global_trace.tracing_cpumask);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307941out_free_buffer_mask:
7942 free_cpumask_var(tracing_buffer_mask);
7943out:
7944 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007945}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05007946
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05007947void __init trace_init(void)
7948{
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05007949 if (tracepoint_printk) {
7950 tracepoint_print_iter =
7951 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
7952 if (WARN_ON(!tracepoint_print_iter))
7953 tracepoint_printk = 0;
7954 }
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05007955 tracer_alloc_buffers();
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04007956 trace_event_init();
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05007957}
7958
Steven Rostedtb2821ae2009-02-02 21:38:32 -05007959__init static int clear_boot_tracer(void)
7960{
7961 /*
7962 * The default tracer at boot buffer is an init section.
7963 * This function is called in lateinit. If we did not
7964 * find the boot tracer, then clear it out, to prevent
7965 * later registration from accessing the buffer that is
7966 * about to be freed.
7967 */
7968 if (!default_bootup_tracer)
7969 return 0;
7970
7971 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
7972 default_bootup_tracer);
7973 default_bootup_tracer = NULL;
7974
7975 return 0;
7976}
7977
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007978fs_initcall(tracer_init_tracefs);
Steven Rostedt (VMware)3170d9a2017-08-01 12:01:52 -04007979late_initcall_sync(clear_boot_tracer);