blob: dade4c9559cc036c1b6aa8567abf0b0887847923 [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>
Ingo Molnar86387f72008-05-12 21:20:51 +020044
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020045#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050046#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020047
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010048/*
Steven Rostedt73c51622009-03-11 13:42:01 -040049 * On boot up, the ring buffer is set to the minimum size, so that
50 * we do not waste memory on systems that are not using tracing.
51 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -050052bool ring_buffer_expanded;
Steven Rostedt73c51622009-03-11 13:42:01 -040053
54/*
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010055 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010056 * A selftest will lurk into the ring-buffer to count the
57 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010058 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010059 * at the same time, giving false positive or negative results.
60 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010061static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010062
Steven Rostedtb2821ae2009-02-02 21:38:32 -050063/*
64 * If a tracer is running, we do not want to run SELFTEST.
65 */
Li Zefan020e5f82009-07-01 10:47:05 +080066bool __read_mostly tracing_selftest_disabled;
Steven Rostedtb2821ae2009-02-02 21:38:32 -050067
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -050068/* Pipe tracepoints to printk */
69struct trace_iterator *tracepoint_print_iter;
70int tracepoint_printk;
71
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010072/* For tracers that don't implement custom flags */
73static struct tracer_opt dummy_tracer_opt[] = {
74 { }
75};
76
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -050077static int
78dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010079{
80 return 0;
81}
Steven Rostedt0f048702008-11-05 16:05:44 -050082
83/*
Steven Rostedt7ffbd482012-10-11 12:14:25 -040084 * To prevent the comm cache from being overwritten when no
85 * tracing is active, only save the comm when a trace event
86 * occurred.
87 */
88static DEFINE_PER_CPU(bool, trace_cmdline_save);
89
90/*
Steven Rostedt0f048702008-11-05 16:05:44 -050091 * Kill all tracing for good (never come back).
92 * It is initialized to 1 but will turn to zero if the initialization
93 * of the tracer is successful. But that is the only place that sets
94 * this back to zero.
95 */
Hannes Eder4fd27352009-02-10 19:44:12 +010096static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -050097
Jason Wessel955b61e2010-08-05 09:22:23 -050098cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +020099
Steven Rostedt944ac422008-10-23 19:26:08 -0400100/*
101 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
102 *
103 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
104 * is set, then ftrace_dump is called. This will output the contents
105 * of the ftrace buffers to the console. This is very useful for
106 * capturing traces that lead to crashes and outputing it to a
107 * serial console.
108 *
109 * It is default off, but you can enable it with either specifying
110 * "ftrace_dump_on_oops" in the kernel command line, or setting
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200111 * /proc/sys/kernel/ftrace_dump_on_oops
112 * Set 1 if you want to dump buffers of all CPUs
113 * Set 2 if you want to dump the buffer of the CPU that triggered oops
Steven Rostedt944ac422008-10-23 19:26:08 -0400114 */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200115
116enum ftrace_dump_mode ftrace_dump_on_oops;
Steven Rostedt944ac422008-10-23 19:26:08 -0400117
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400118/* When set, tracing will stop when a WARN*() is hit */
119int __disable_trace_on_warning;
120
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400121#ifdef CONFIG_TRACE_ENUM_MAP_FILE
122/* Map of enums to their values, for "enum_map" file */
123struct trace_enum_map_head {
124 struct module *mod;
125 unsigned long length;
126};
127
128union trace_enum_map_item;
129
130struct trace_enum_map_tail {
131 /*
132 * "end" is first and points to NULL as it must be different
133 * than "mod" or "enum_string"
134 */
135 union trace_enum_map_item *next;
136 const char *end; /* points to NULL */
137};
138
139static DEFINE_MUTEX(trace_enum_mutex);
140
141/*
142 * The trace_enum_maps are saved in an array with two extra elements,
143 * one at the beginning, and one at the end. The beginning item contains
144 * the count of the saved maps (head.length), and the module they
145 * belong to if not built in (head.mod). The ending item contains a
146 * pointer to the next array of saved enum_map items.
147 */
148union trace_enum_map_item {
149 struct trace_enum_map map;
150 struct trace_enum_map_head head;
151 struct trace_enum_map_tail tail;
152};
153
154static union trace_enum_map_item *trace_enum_maps;
155#endif /* CONFIG_TRACE_ENUM_MAP_FILE */
156
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -0500157static int tracing_set_tracer(struct trace_array *tr, const char *buf);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500158
Li Zefanee6c2c12009-09-18 14:06:47 +0800159#define MAX_TRACER_SIZE 100
160static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500161static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100162
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500163static bool allocate_snapshot;
164
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200165static int __init set_cmdline_ftrace(char *str)
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100166{
Chen Gang67012ab2013-04-08 12:06:44 +0800167 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500168 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400169 /* We are using ftrace early, expand it */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500170 ring_buffer_expanded = true;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100171 return 1;
172}
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200173__setup("ftrace=", set_cmdline_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100174
Steven Rostedt944ac422008-10-23 19:26:08 -0400175static int __init set_ftrace_dump_on_oops(char *str)
176{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200177 if (*str++ != '=' || !*str) {
178 ftrace_dump_on_oops = DUMP_ALL;
179 return 1;
180 }
181
182 if (!strcmp("orig_cpu", str)) {
183 ftrace_dump_on_oops = DUMP_ORIG;
184 return 1;
185 }
186
187 return 0;
Steven Rostedt944ac422008-10-23 19:26:08 -0400188}
189__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200190
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400191static int __init stop_trace_on_warning(char *str)
192{
Luis Claudio R. Goncalves933ff9f2014-11-12 21:14:00 -0200193 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
194 __disable_trace_on_warning = 1;
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400195 return 1;
196}
Luis Claudio R. Goncalves933ff9f2014-11-12 21:14:00 -0200197__setup("traceoff_on_warning", stop_trace_on_warning);
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400198
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400199static int __init boot_alloc_snapshot(char *str)
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500200{
201 allocate_snapshot = true;
202 /* We also need the main ring buffer expanded */
203 ring_buffer_expanded = true;
204 return 1;
205}
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400206__setup("alloc_snapshot", boot_alloc_snapshot);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500207
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400208
209static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400210
211static int __init set_trace_boot_options(char *str)
212{
Chen Gang67012ab2013-04-08 12:06:44 +0800213 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400214 return 0;
215}
216__setup("trace_options=", set_trace_boot_options);
217
Steven Rostedte1e232c2014-02-10 23:38:46 -0500218static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
219static char *trace_boot_clock __initdata;
220
221static int __init set_trace_boot_clock(char *str)
222{
223 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
224 trace_boot_clock = trace_boot_clock_buf;
225 return 0;
226}
227__setup("trace_clock=", set_trace_boot_clock);
228
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500229static int __init set_tracepoint_printk(char *str)
230{
231 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
232 tracepoint_printk = 1;
233 return 1;
234}
235__setup("tp_printk", set_tracepoint_printk);
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400236
Lai Jiangshancf8e3472009-03-30 13:48:00 +0800237unsigned long long ns2usecs(cycle_t nsec)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200238{
239 nsec += 500;
240 do_div(nsec, 1000);
241 return nsec;
242}
243
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400244/* trace_flags holds trace_options default values */
245#define TRACE_DEFAULT_FLAGS \
246 (FUNCTION_DEFAULT_FLAGS | \
247 TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \
248 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \
249 TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \
250 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS)
251
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -0400252/* trace_options that are only supported by global_trace */
253#define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \
254 TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
255
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -0400256/* trace_flags that are default zero for instances */
257#define ZEROED_TRACE_FLAGS \
258 TRACE_ITER_EVENT_FORK
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -0400259
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200260/*
261 * The global_trace is the descriptor that holds the tracing
262 * buffers for the live tracing. For each CPU, it contains
263 * a link list of pages that will store trace entries. The
264 * page descriptor of the pages in the memory is used to hold
265 * the link list by linking the lru item in the page descriptor
266 * to each of the pages in the buffer per CPU.
267 *
268 * For each active CPU there is a data field that holds the
269 * pages for the buffer for that CPU. Each CPU has the same number
270 * of pages allocated for its buffer.
271 */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400272static struct trace_array global_trace = {
273 .trace_flags = TRACE_DEFAULT_FLAGS,
274};
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200275
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400276LIST_HEAD(ftrace_trace_arrays);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200277
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -0400278int trace_array_get(struct trace_array *this_tr)
279{
280 struct trace_array *tr;
281 int ret = -ENODEV;
282
283 mutex_lock(&trace_types_lock);
284 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
285 if (tr == this_tr) {
286 tr->ref++;
287 ret = 0;
288 break;
289 }
290 }
291 mutex_unlock(&trace_types_lock);
292
293 return ret;
294}
295
296static void __trace_array_put(struct trace_array *this_tr)
297{
298 WARN_ON(!this_tr->ref);
299 this_tr->ref--;
300}
301
302void trace_array_put(struct trace_array *this_tr)
303{
304 mutex_lock(&trace_types_lock);
305 __trace_array_put(this_tr);
306 mutex_unlock(&trace_types_lock);
307}
308
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400309int call_filter_check_discard(struct trace_event_call *call, void *rec,
Tom Zanussif306cc82013-10-24 08:34:17 -0500310 struct ring_buffer *buffer,
311 struct ring_buffer_event *event)
312{
313 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
314 !filter_match_preds(call->filter, rec)) {
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -0400315 __trace_event_discard_commit(buffer, event);
Tom Zanussif306cc82013-10-24 08:34:17 -0500316 return 1;
317 }
318
319 return 0;
320}
Tom Zanussieb02ce02009-04-08 03:15:54 -0500321
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400322void trace_free_pid_list(struct trace_pid_list *pid_list)
323{
324 vfree(pid_list->pids);
325 kfree(pid_list);
326}
327
Steven Rostedtd8275c42016-04-14 12:15:22 -0400328/**
329 * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
330 * @filtered_pids: The list of pids to check
331 * @search_pid: The PID to find in @filtered_pids
332 *
333 * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis.
334 */
335bool
336trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
337{
338 /*
339 * If pid_max changed after filtered_pids was created, we
340 * by default ignore all pids greater than the previous pid_max.
341 */
342 if (search_pid >= filtered_pids->pid_max)
343 return false;
344
345 return test_bit(search_pid, filtered_pids->pids);
346}
347
348/**
349 * trace_ignore_this_task - should a task be ignored for tracing
350 * @filtered_pids: The list of pids to check
351 * @task: The task that should be ignored if not filtered
352 *
353 * Checks if @task should be traced or not from @filtered_pids.
354 * Returns true if @task should *NOT* be traced.
355 * Returns false if @task should be traced.
356 */
357bool
358trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task)
359{
360 /*
361 * Return false, because if filtered_pids does not exist,
362 * all pids are good to trace.
363 */
364 if (!filtered_pids)
365 return false;
366
367 return !trace_find_filtered_pid(filtered_pids, task->pid);
368}
369
370/**
371 * trace_pid_filter_add_remove - Add or remove a task from a pid_list
372 * @pid_list: The list to modify
373 * @self: The current task for fork or NULL for exit
374 * @task: The task to add or remove
375 *
376 * If adding a task, if @self is defined, the task is only added if @self
377 * is also included in @pid_list. This happens on fork and tasks should
378 * only be added when the parent is listed. If @self is NULL, then the
379 * @task pid will be removed from the list, which would happen on exit
380 * of a task.
381 */
382void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
383 struct task_struct *self,
384 struct task_struct *task)
385{
386 if (!pid_list)
387 return;
388
389 /* For forks, we only add if the forking task is listed */
390 if (self) {
391 if (!trace_find_filtered_pid(pid_list, self->pid))
392 return;
393 }
394
395 /* Sorry, but we don't support pid_max changing after setting */
396 if (task->pid >= pid_list->pid_max)
397 return;
398
399 /* "self" is set for forks, and NULL for exits */
400 if (self)
401 set_bit(task->pid, pid_list->pids);
402 else
403 clear_bit(task->pid, pid_list->pids);
404}
405
Steven Rostedt (Red Hat)5cc89762016-04-20 15:19:54 -0400406/**
407 * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
408 * @pid_list: The pid list to show
409 * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
410 * @pos: The position of the file
411 *
412 * This is used by the seq_file "next" operation to iterate the pids
413 * listed in a trace_pid_list structure.
414 *
415 * Returns the pid+1 as we want to display pid of zero, but NULL would
416 * stop the iteration.
417 */
418void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
419{
420 unsigned long pid = (unsigned long)v;
421
422 (*pos)++;
423
424 /* pid already is +1 of the actual prevous bit */
425 pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
426
427 /* Return pid + 1 to allow zero to be represented */
428 if (pid < pid_list->pid_max)
429 return (void *)(pid + 1);
430
431 return NULL;
432}
433
434/**
435 * trace_pid_start - Used for seq_file to start reading pid lists
436 * @pid_list: The pid list to show
437 * @pos: The position of the file
438 *
439 * This is used by seq_file "start" operation to start the iteration
440 * of listing pids.
441 *
442 * Returns the pid+1 as we want to display pid of zero, but NULL would
443 * stop the iteration.
444 */
445void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
446{
447 unsigned long pid;
448 loff_t l = 0;
449
450 pid = find_first_bit(pid_list->pids, pid_list->pid_max);
451 if (pid >= pid_list->pid_max)
452 return NULL;
453
454 /* Return pid + 1 so that zero can be the exit value */
455 for (pid++; pid && l < *pos;
456 pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
457 ;
458 return (void *)pid;
459}
460
461/**
462 * trace_pid_show - show the current pid in seq_file processing
463 * @m: The seq_file structure to write into
464 * @v: A void pointer of the pid (+1) value to display
465 *
466 * Can be directly used by seq_file operations to display the current
467 * pid value.
468 */
469int trace_pid_show(struct seq_file *m, void *v)
470{
471 unsigned long pid = (unsigned long)v - 1;
472
473 seq_printf(m, "%lu\n", pid);
474 return 0;
475}
476
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400477/* 128 should be much more than enough */
478#define PID_BUF_SIZE 127
479
480int trace_pid_write(struct trace_pid_list *filtered_pids,
481 struct trace_pid_list **new_pid_list,
482 const char __user *ubuf, size_t cnt)
483{
484 struct trace_pid_list *pid_list;
485 struct trace_parser parser;
486 unsigned long val;
487 int nr_pids = 0;
488 ssize_t read = 0;
489 ssize_t ret = 0;
490 loff_t pos;
491 pid_t pid;
492
493 if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
494 return -ENOMEM;
495
496 /*
497 * Always recreate a new array. The write is an all or nothing
498 * operation. Always create a new array when adding new pids by
499 * the user. If the operation fails, then the current list is
500 * not modified.
501 */
502 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
503 if (!pid_list)
504 return -ENOMEM;
505
506 pid_list->pid_max = READ_ONCE(pid_max);
507
508 /* Only truncating will shrink pid_max */
509 if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
510 pid_list->pid_max = filtered_pids->pid_max;
511
512 pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
513 if (!pid_list->pids) {
514 kfree(pid_list);
515 return -ENOMEM;
516 }
517
518 if (filtered_pids) {
519 /* copy the current bits to the new max */
Wei Yongjun67f20b02016-07-04 15:10:04 +0000520 for_each_set_bit(pid, filtered_pids->pids,
521 filtered_pids->pid_max) {
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400522 set_bit(pid, pid_list->pids);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400523 nr_pids++;
524 }
525 }
526
527 while (cnt > 0) {
528
529 pos = 0;
530
531 ret = trace_get_user(&parser, ubuf, cnt, &pos);
532 if (ret < 0 || !trace_parser_loaded(&parser))
533 break;
534
535 read += ret;
536 ubuf += ret;
537 cnt -= ret;
538
539 parser.buffer[parser.idx] = 0;
540
541 ret = -EINVAL;
542 if (kstrtoul(parser.buffer, 0, &val))
543 break;
544 if (val >= pid_list->pid_max)
545 break;
546
547 pid = (pid_t)val;
548
549 set_bit(pid, pid_list->pids);
550 nr_pids++;
551
552 trace_parser_clear(&parser);
553 ret = 0;
554 }
555 trace_parser_put(&parser);
556
557 if (ret < 0) {
558 trace_free_pid_list(pid_list);
559 return ret;
560 }
561
562 if (!nr_pids) {
563 /* Cleared the list of pids */
564 trace_free_pid_list(pid_list);
565 read = ret;
566 pid_list = NULL;
567 }
568
569 *new_pid_list = pid_list;
570
571 return read;
572}
573
Fabian Frederickad1438a2014-04-17 21:44:42 +0200574static cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
Steven Rostedt37886f62009-03-17 17:22:06 -0400575{
576 u64 ts;
577
578 /* Early boot up does not have a buffer yet */
Alexander Z Lam94571582013-08-02 18:36:16 -0700579 if (!buf->buffer)
Steven Rostedt37886f62009-03-17 17:22:06 -0400580 return trace_clock_local();
581
Alexander Z Lam94571582013-08-02 18:36:16 -0700582 ts = ring_buffer_time_stamp(buf->buffer, cpu);
583 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
Steven Rostedt37886f62009-03-17 17:22:06 -0400584
585 return ts;
586}
587
Alexander Z Lam94571582013-08-02 18:36:16 -0700588cycle_t ftrace_now(int cpu)
589{
590 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
591}
592
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400593/**
594 * tracing_is_enabled - Show if global_trace has been disabled
595 *
596 * Shows if the global trace has been enabled or not. It uses the
597 * mirror flag "buffer_disabled" to be used in fast paths such as for
598 * the irqsoff tracer. But it may be inaccurate due to races. If you
599 * need to know the accurate state, use tracing_is_on() which is a little
600 * slower, but accurate.
601 */
Steven Rostedt90369902008-11-05 16:05:44 -0500602int tracing_is_enabled(void)
603{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400604 /*
605 * For quick access (irqsoff uses this in fast path), just
606 * return the mirror variable of the state of the ring buffer.
607 * It's a little racy, but we don't really care.
608 */
609 smp_rmb();
610 return !global_trace.buffer_disabled;
Steven Rostedt90369902008-11-05 16:05:44 -0500611}
612
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200613/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400614 * trace_buf_size is the size in bytes that is allocated
615 * for a buffer. Note, the number of bytes is always rounded
616 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400617 *
618 * This number is purposely set to a low number of 16384.
619 * If the dump on oops happens, it will be much appreciated
620 * to not have to wait for all that output. Anyway this can be
621 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200622 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400623#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400624
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400625static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200626
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200627/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200628static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200629
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200630/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200631 * trace_types_lock is used to protect the trace_types list.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200632 */
Alexander Z Lama8227412013-07-01 19:37:54 -0700633DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200634
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800635/*
636 * serialize the access of the ring buffer
637 *
638 * ring buffer serializes readers, but it is low level protection.
639 * The validity of the events (which returns by ring_buffer_peek() ..etc)
640 * are not protected by ring buffer.
641 *
642 * The content of events may become garbage if we allow other process consumes
643 * these events concurrently:
644 * A) the page of the consumed events may become a normal page
645 * (not reader page) in ring buffer, and this page will be rewrited
646 * by events producer.
647 * B) The page of the consumed events may become a page for splice_read,
648 * and this page will be returned to system.
649 *
650 * These primitives allow multi process access to different cpu ring buffer
651 * concurrently.
652 *
653 * These primitives don't distinguish read-only and read-consume access.
654 * Multi read-only access are also serialized.
655 */
656
657#ifdef CONFIG_SMP
658static DECLARE_RWSEM(all_cpu_access_lock);
659static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
660
661static inline void trace_access_lock(int cpu)
662{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500663 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800664 /* gain it for accessing the whole ring buffer. */
665 down_write(&all_cpu_access_lock);
666 } else {
667 /* gain it for accessing a cpu ring buffer. */
668
Steven Rostedtae3b5092013-01-23 15:22:59 -0500669 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800670 down_read(&all_cpu_access_lock);
671
672 /* Secondly block other access to this @cpu ring buffer. */
673 mutex_lock(&per_cpu(cpu_access_lock, cpu));
674 }
675}
676
677static inline void trace_access_unlock(int cpu)
678{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500679 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800680 up_write(&all_cpu_access_lock);
681 } else {
682 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
683 up_read(&all_cpu_access_lock);
684 }
685}
686
687static inline void trace_access_lock_init(void)
688{
689 int cpu;
690
691 for_each_possible_cpu(cpu)
692 mutex_init(&per_cpu(cpu_access_lock, cpu));
693}
694
695#else
696
697static DEFINE_MUTEX(access_lock);
698
699static inline void trace_access_lock(int cpu)
700{
701 (void)cpu;
702 mutex_lock(&access_lock);
703}
704
705static inline void trace_access_unlock(int cpu)
706{
707 (void)cpu;
708 mutex_unlock(&access_lock);
709}
710
711static inline void trace_access_lock_init(void)
712{
713}
714
715#endif
716
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400717#ifdef CONFIG_STACKTRACE
718static void __ftrace_trace_stack(struct ring_buffer *buffer,
719 unsigned long flags,
720 int skip, int pc, struct pt_regs *regs);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400721static inline void ftrace_trace_stack(struct trace_array *tr,
722 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -0400723 unsigned long flags,
724 int skip, int pc, struct pt_regs *regs);
Steven Rostedt (Red Hat)ca475e82015-09-28 09:41:11 -0400725
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400726#else
727static inline void __ftrace_trace_stack(struct ring_buffer *buffer,
728 unsigned long flags,
729 int skip, int pc, struct pt_regs *regs)
730{
731}
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400732static inline void ftrace_trace_stack(struct trace_array *tr,
733 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -0400734 unsigned long flags,
735 int skip, int pc, struct pt_regs *regs)
Steven Rostedt (Red Hat)ca475e82015-09-28 09:41:11 -0400736{
737}
738
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400739#endif
740
Steven Rostedt (Red Hat)5280bce2013-07-02 19:59:57 -0400741static void tracer_tracing_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400742{
743 if (tr->trace_buffer.buffer)
744 ring_buffer_record_on(tr->trace_buffer.buffer);
745 /*
746 * This flag is looked at when buffers haven't been allocated
747 * yet, or by some tracers (like irqsoff), that just want to
748 * know if the ring buffer has been disabled, but it can handle
749 * races of where it gets disabled but we still do a record.
750 * As the check is in the fast path of the tracers, it is more
751 * important to be fast than accurate.
752 */
753 tr->buffer_disabled = 0;
754 /* Make the flag seen by readers */
755 smp_wmb();
756}
757
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200758/**
Steven Rostedt499e5472012-02-22 15:50:28 -0500759 * tracing_on - enable tracing buffers
760 *
761 * This function enables tracing buffers that may have been
762 * disabled with tracing_off.
763 */
764void tracing_on(void)
765{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400766 tracer_tracing_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -0500767}
768EXPORT_SYMBOL_GPL(tracing_on);
769
770/**
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500771 * __trace_puts - write a constant string into the trace buffer.
772 * @ip: The address of the caller
773 * @str: The constant string to write
774 * @size: The size of the string.
775 */
776int __trace_puts(unsigned long ip, const char *str, int size)
777{
778 struct ring_buffer_event *event;
779 struct ring_buffer *buffer;
780 struct print_entry *entry;
781 unsigned long irq_flags;
782 int alloc;
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800783 int pc;
784
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400785 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800786 return 0;
787
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800788 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500789
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500790 if (unlikely(tracing_selftest_running || tracing_disabled))
791 return 0;
792
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500793 alloc = sizeof(*entry) + size + 2; /* possible \n added */
794
795 local_save_flags(irq_flags);
796 buffer = global_trace.trace_buffer.buffer;
797 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800798 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500799 if (!event)
800 return 0;
801
802 entry = ring_buffer_event_data(event);
803 entry->ip = ip;
804
805 memcpy(&entry->buf, str, size);
806
807 /* Add a newline if necessary */
808 if (entry->buf[size - 1] != '\n') {
809 entry->buf[size] = '\n';
810 entry->buf[size + 1] = '\0';
811 } else
812 entry->buf[size] = '\0';
813
814 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400815 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500816
817 return size;
818}
819EXPORT_SYMBOL_GPL(__trace_puts);
820
821/**
822 * __trace_bputs - write the pointer to a constant string into trace buffer
823 * @ip: The address of the caller
824 * @str: The constant string to write to the buffer to
825 */
826int __trace_bputs(unsigned long ip, const char *str)
827{
828 struct ring_buffer_event *event;
829 struct ring_buffer *buffer;
830 struct bputs_entry *entry;
831 unsigned long irq_flags;
832 int size = sizeof(struct bputs_entry);
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800833 int pc;
834
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400835 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800836 return 0;
837
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800838 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500839
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500840 if (unlikely(tracing_selftest_running || tracing_disabled))
841 return 0;
842
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500843 local_save_flags(irq_flags);
844 buffer = global_trace.trace_buffer.buffer;
845 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800846 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500847 if (!event)
848 return 0;
849
850 entry = ring_buffer_event_data(event);
851 entry->ip = ip;
852 entry->str = str;
853
854 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400855 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500856
857 return 1;
858}
859EXPORT_SYMBOL_GPL(__trace_bputs);
860
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500861#ifdef CONFIG_TRACER_SNAPSHOT
862/**
863 * trace_snapshot - take a snapshot of the current buffer.
864 *
865 * This causes a swap between the snapshot buffer and the current live
866 * tracing buffer. You can use this to take snapshots of the live
867 * trace when some condition is triggered, but continue to trace.
868 *
869 * Note, make sure to allocate the snapshot with either
870 * a tracing_snapshot_alloc(), or by doing it manually
871 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
872 *
873 * If the snapshot buffer is not allocated, it will stop tracing.
874 * Basically making a permanent snapshot.
875 */
876void tracing_snapshot(void)
877{
878 struct trace_array *tr = &global_trace;
879 struct tracer *tracer = tr->current_trace;
880 unsigned long flags;
881
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500882 if (in_nmi()) {
883 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
884 internal_trace_puts("*** snapshot is being ignored ***\n");
885 return;
886 }
887
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500888 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500889 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
890 internal_trace_puts("*** stopping trace here! ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500891 tracing_off();
892 return;
893 }
894
895 /* Note, snapshot can not be used when the tracer uses it */
896 if (tracer->use_max_tr) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500897 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
898 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500899 return;
900 }
901
902 local_irq_save(flags);
903 update_max_tr(tr, current, smp_processor_id());
904 local_irq_restore(flags);
905}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500906EXPORT_SYMBOL_GPL(tracing_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500907
908static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
909 struct trace_buffer *size_buf, int cpu_id);
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400910static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
911
912static int alloc_snapshot(struct trace_array *tr)
913{
914 int ret;
915
916 if (!tr->allocated_snapshot) {
917
918 /* allocate spare buffer */
919 ret = resize_buffer_duplicate_size(&tr->max_buffer,
920 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
921 if (ret < 0)
922 return ret;
923
924 tr->allocated_snapshot = true;
925 }
926
927 return 0;
928}
929
Fabian Frederickad1438a2014-04-17 21:44:42 +0200930static void free_snapshot(struct trace_array *tr)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400931{
932 /*
933 * We don't free the ring buffer. instead, resize it because
934 * The max_tr ring buffer has some state (e.g. ring->clock) and
935 * we want preserve it.
936 */
937 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
938 set_buffer_entries(&tr->max_buffer, 1);
939 tracing_reset_online_cpus(&tr->max_buffer);
940 tr->allocated_snapshot = false;
941}
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500942
943/**
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500944 * tracing_alloc_snapshot - allocate snapshot buffer.
945 *
946 * This only allocates the snapshot buffer if it isn't already
947 * allocated - it doesn't also take a snapshot.
948 *
949 * This is meant to be used in cases where the snapshot buffer needs
950 * to be set up for events that can't sleep but need to be able to
951 * trigger a snapshot.
952 */
953int tracing_alloc_snapshot(void)
954{
955 struct trace_array *tr = &global_trace;
956 int ret;
957
958 ret = alloc_snapshot(tr);
959 WARN_ON(ret < 0);
960
961 return ret;
962}
963EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
964
965/**
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500966 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
967 *
968 * This is similar to trace_snapshot(), but it will allocate the
969 * snapshot buffer if it isn't already allocated. Use this only
970 * where it is safe to sleep, as the allocation may sleep.
971 *
972 * This causes a swap between the snapshot buffer and the current live
973 * tracing buffer. You can use this to take snapshots of the live
974 * trace when some condition is triggered, but continue to trace.
975 */
976void tracing_snapshot_alloc(void)
977{
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500978 int ret;
979
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500980 ret = tracing_alloc_snapshot();
981 if (ret < 0)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400982 return;
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500983
984 tracing_snapshot();
985}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500986EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500987#else
988void tracing_snapshot(void)
989{
990 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
991}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500992EXPORT_SYMBOL_GPL(tracing_snapshot);
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500993int tracing_alloc_snapshot(void)
994{
995 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
996 return -ENODEV;
997}
998EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500999void tracing_snapshot_alloc(void)
1000{
1001 /* Give warning */
1002 tracing_snapshot();
1003}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001004EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001005#endif /* CONFIG_TRACER_SNAPSHOT */
1006
Steven Rostedt (Red Hat)5280bce2013-07-02 19:59:57 -04001007static void tracer_tracing_off(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001008{
1009 if (tr->trace_buffer.buffer)
1010 ring_buffer_record_off(tr->trace_buffer.buffer);
1011 /*
1012 * This flag is looked at when buffers haven't been allocated
1013 * yet, or by some tracers (like irqsoff), that just want to
1014 * know if the ring buffer has been disabled, but it can handle
1015 * races of where it gets disabled but we still do a record.
1016 * As the check is in the fast path of the tracers, it is more
1017 * important to be fast than accurate.
1018 */
1019 tr->buffer_disabled = 1;
1020 /* Make the flag seen by readers */
1021 smp_wmb();
1022}
1023
Steven Rostedt499e5472012-02-22 15:50:28 -05001024/**
1025 * tracing_off - turn off tracing buffers
1026 *
1027 * This function stops the tracing buffers from recording data.
1028 * It does not disable any overhead the tracers themselves may
1029 * be causing. This function simply causes all recording to
1030 * the ring buffers to fail.
1031 */
1032void tracing_off(void)
1033{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001034 tracer_tracing_off(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001035}
1036EXPORT_SYMBOL_GPL(tracing_off);
1037
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -04001038void disable_trace_on_warning(void)
1039{
1040 if (__disable_trace_on_warning)
1041 tracing_off();
1042}
1043
Steven Rostedt499e5472012-02-22 15:50:28 -05001044/**
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001045 * tracer_tracing_is_on - show real state of ring buffer enabled
1046 * @tr : the trace array to know if ring buffer is enabled
1047 *
1048 * Shows real state of the ring buffer if it is enabled or not.
1049 */
Steven Rostedt (Red Hat)5280bce2013-07-02 19:59:57 -04001050static int tracer_tracing_is_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001051{
1052 if (tr->trace_buffer.buffer)
1053 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
1054 return !tr->buffer_disabled;
1055}
1056
Steven Rostedt499e5472012-02-22 15:50:28 -05001057/**
1058 * tracing_is_on - show state of ring buffers enabled
1059 */
1060int tracing_is_on(void)
1061{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001062 return tracer_tracing_is_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001063}
1064EXPORT_SYMBOL_GPL(tracing_is_on);
1065
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001066static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001067{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001068 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001069
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001070 if (!str)
1071 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +08001072 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001073 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +08001074 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001075 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001076 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001077 return 1;
1078}
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001079__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001080
Tim Bird0e950172010-02-25 15:36:43 -08001081static int __init set_tracing_thresh(char *str)
1082{
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001083 unsigned long threshold;
Tim Bird0e950172010-02-25 15:36:43 -08001084 int ret;
1085
1086 if (!str)
1087 return 0;
Daniel Walterbcd83ea2012-09-26 22:08:38 +02001088 ret = kstrtoul(str, 0, &threshold);
Tim Bird0e950172010-02-25 15:36:43 -08001089 if (ret < 0)
1090 return 0;
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001091 tracing_thresh = threshold * 1000;
Tim Bird0e950172010-02-25 15:36:43 -08001092 return 1;
1093}
1094__setup("tracing_thresh=", set_tracing_thresh);
1095
Steven Rostedt57f50be2008-05-12 21:20:44 +02001096unsigned long nsecs_to_usecs(unsigned long nsecs)
1097{
1098 return nsecs / 1000;
1099}
1100
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001101/*
1102 * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
1103 * It uses C(a, b) where 'a' is the enum name and 'b' is the string that
1104 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
1105 * of strings in the order that the enums were defined.
1106 */
1107#undef C
1108#define C(a, b) b
1109
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001110/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001111static const char *trace_options[] = {
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001112 TRACE_FLAGS
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001113 NULL
1114};
1115
Zhaolei5079f322009-08-25 16:12:56 +08001116static struct {
1117 u64 (*func)(void);
1118 const char *name;
David Sharp8be07092012-11-13 12:18:22 -08001119 int in_ns; /* is this clock in nanoseconds? */
Zhaolei5079f322009-08-25 16:12:56 +08001120} trace_clocks[] = {
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001121 { trace_clock_local, "local", 1 },
1122 { trace_clock_global, "global", 1 },
1123 { trace_clock_counter, "counter", 0 },
Linus Torvaldse7fda6c2014-08-05 17:46:42 -07001124 { trace_clock_jiffies, "uptime", 0 },
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001125 { trace_clock, "perf", 1 },
1126 { ktime_get_mono_fast_ns, "mono", 1 },
Drew Richardsonaabfa5f2015-05-08 07:30:39 -07001127 { ktime_get_raw_fast_ns, "mono_raw", 1 },
David Sharp8cbd9cc2012-11-13 12:18:21 -08001128 ARCH_TRACE_CLOCKS
Zhaolei5079f322009-08-25 16:12:56 +08001129};
1130
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001131/*
1132 * trace_parser_get_init - gets the buffer for trace parser
1133 */
1134int trace_parser_get_init(struct trace_parser *parser, int size)
1135{
1136 memset(parser, 0, sizeof(*parser));
1137
1138 parser->buffer = kmalloc(size, GFP_KERNEL);
1139 if (!parser->buffer)
1140 return 1;
1141
1142 parser->size = size;
1143 return 0;
1144}
1145
1146/*
1147 * trace_parser_put - frees the buffer for trace parser
1148 */
1149void trace_parser_put(struct trace_parser *parser)
1150{
1151 kfree(parser->buffer);
1152}
1153
1154/*
1155 * trace_get_user - reads the user input string separated by space
1156 * (matched by isspace(ch))
1157 *
1158 * For each string found the 'struct trace_parser' is updated,
1159 * and the function returns.
1160 *
1161 * Returns number of bytes read.
1162 *
1163 * See kernel/trace/trace.h for 'struct trace_parser' details.
1164 */
1165int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1166 size_t cnt, loff_t *ppos)
1167{
1168 char ch;
1169 size_t read = 0;
1170 ssize_t ret;
1171
1172 if (!*ppos)
1173 trace_parser_clear(parser);
1174
1175 ret = get_user(ch, ubuf++);
1176 if (ret)
1177 goto out;
1178
1179 read++;
1180 cnt--;
1181
1182 /*
1183 * The parser is not finished with the last write,
1184 * continue reading the user input without skipping spaces.
1185 */
1186 if (!parser->cont) {
1187 /* skip white space */
1188 while (cnt && isspace(ch)) {
1189 ret = get_user(ch, ubuf++);
1190 if (ret)
1191 goto out;
1192 read++;
1193 cnt--;
1194 }
1195
1196 /* only spaces were written */
1197 if (isspace(ch)) {
1198 *ppos += read;
1199 ret = read;
1200 goto out;
1201 }
1202
1203 parser->idx = 0;
1204 }
1205
1206 /* read the non-space input */
1207 while (cnt && !isspace(ch)) {
Li Zefan3c235a32009-09-22 13:51:54 +08001208 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001209 parser->buffer[parser->idx++] = ch;
1210 else {
1211 ret = -EINVAL;
1212 goto out;
1213 }
1214 ret = get_user(ch, ubuf++);
1215 if (ret)
1216 goto out;
1217 read++;
1218 cnt--;
1219 }
1220
1221 /* We either got finished input or we have to wait for another call. */
1222 if (isspace(ch)) {
1223 parser->buffer[parser->idx] = 0;
1224 parser->cont = false;
Steven Rostedt057db842013-10-09 22:23:23 -04001225 } else if (parser->idx < parser->size - 1) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001226 parser->cont = true;
1227 parser->buffer[parser->idx++] = ch;
Steven Rostedt057db842013-10-09 22:23:23 -04001228 } else {
1229 ret = -EINVAL;
1230 goto out;
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001231 }
1232
1233 *ppos += read;
1234 ret = read;
1235
1236out:
1237 return ret;
1238}
1239
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001240/* TODO add a seq_buf_to_buffer() */
Dmitri Vorobievb8b94262009-03-22 19:11:11 +02001241static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001242{
1243 int len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001244
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001245 if (trace_seq_used(s) <= s->seq.readpos)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001246 return -EBUSY;
1247
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001248 len = trace_seq_used(s) - s->seq.readpos;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001249 if (cnt > len)
1250 cnt = len;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001251 memcpy(buf, s->buffer + s->seq.readpos, cnt);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001252
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001253 s->seq.readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001254 return cnt;
1255}
1256
Tim Bird0e950172010-02-25 15:36:43 -08001257unsigned long __read_mostly tracing_thresh;
1258
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001259#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001260/*
1261 * Copy the new maximum trace into the separate maximum-trace
1262 * structure. (this way the maximum trace is permanently saved,
1263 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
1264 */
1265static void
1266__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1267{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001268 struct trace_buffer *trace_buf = &tr->trace_buffer;
1269 struct trace_buffer *max_buf = &tr->max_buffer;
1270 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1271 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001272
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001273 max_buf->cpu = cpu;
1274 max_buf->time_start = data->preempt_timestamp;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001275
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001276 max_data->saved_latency = tr->max_latency;
Steven Rostedt8248ac02009-09-02 12:27:41 -04001277 max_data->critical_start = data->critical_start;
1278 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001279
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -03001280 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -04001281 max_data->pid = tsk->pid;
Steven Rostedt (Red Hat)f17a5192013-05-30 21:10:37 -04001282 /*
1283 * If tsk == current, then use current_uid(), as that does not use
1284 * RCU. The irq tracer can be called out of RCU scope.
1285 */
1286 if (tsk == current)
1287 max_data->uid = current_uid();
1288 else
1289 max_data->uid = task_uid(tsk);
1290
Steven Rostedt8248ac02009-09-02 12:27:41 -04001291 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1292 max_data->policy = tsk->policy;
1293 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001294
1295 /* record this tasks comm */
1296 tracing_record_cmdline(tsk);
1297}
1298
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001299/**
1300 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1301 * @tr: tracer
1302 * @tsk: the task with the latency
1303 * @cpu: The cpu that initiated the trace.
1304 *
1305 * Flip the buffers between the @tr and the max_tr and record information
1306 * about which task was the cause of this latency.
1307 */
Ingo Molnare309b412008-05-12 21:20:51 +02001308void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001309update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1310{
Steven Rostedt (Red Hat)2721e722013-03-12 11:32:32 -04001311 struct ring_buffer *buf;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001312
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001313 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001314 return;
1315
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001316 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt34600f02013-01-22 13:35:11 -05001317
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05001318 if (!tr->allocated_snapshot) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001319 /* Only the nop tracer should hit this when disabling */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001320 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001321 return;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001322 }
Steven Rostedt34600f02013-01-22 13:35:11 -05001323
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001324 arch_spin_lock(&tr->max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001325
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001326 buf = tr->trace_buffer.buffer;
1327 tr->trace_buffer.buffer = tr->max_buffer.buffer;
1328 tr->max_buffer.buffer = buf;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001329
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001330 __update_max_tr(tr, tsk, cpu);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001331 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001332}
1333
1334/**
1335 * update_max_tr_single - only copy one trace over, and reset the rest
1336 * @tr - tracer
1337 * @tsk - task with the latency
1338 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001339 *
1340 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001341 */
Ingo Molnare309b412008-05-12 21:20:51 +02001342void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001343update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1344{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001345 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001346
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001347 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001348 return;
1349
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001350 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt6c244992013-04-29 20:08:14 -04001351 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001352 /* Only the nop tracer should hit this when disabling */
Linus Torvalds9e8529a2013-04-29 13:55:38 -07001353 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001354 return;
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001355 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001356
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001357 arch_spin_lock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001358
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001359 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001360
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001361 if (ret == -EBUSY) {
1362 /*
1363 * We failed to swap the buffer due to a commit taking
1364 * place on this CPU. We fail to record, but we reset
1365 * the max trace buffer (no one writes directly to it)
1366 * and flag that it failed.
1367 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001368 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001369 "Failed to swap buffers due to commit in progress\n");
1370 }
1371
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001372 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001373
1374 __update_max_tr(tr, tsk, cpu);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001375 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001376}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001377#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001378
Rabin Vincente30f53a2014-11-10 19:46:34 +01001379static int wait_on_pipe(struct trace_iterator *iter, bool full)
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001380{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001381 /* Iterators are static, they should be filled or empty */
1382 if (trace_buffer_iter(iter, iter->cpu_file))
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04001383 return 0;
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001384
Rabin Vincente30f53a2014-11-10 19:46:34 +01001385 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1386 full);
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001387}
1388
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001389#ifdef CONFIG_FTRACE_STARTUP_TEST
1390static int run_tracer_selftest(struct tracer *type)
1391{
1392 struct trace_array *tr = &global_trace;
1393 struct tracer *saved_tracer = tr->current_trace;
1394 int ret;
1395
1396 if (!type->selftest || tracing_selftest_disabled)
1397 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001398
1399 /*
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001400 * Run a selftest on this tracer.
1401 * Here we reset the trace buffer, and set the current
1402 * tracer to be this tracer. The tracer can then run some
1403 * internal tracing to verify that everything is in order.
1404 * If we fail, we do not register this tracer.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001405 */
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001406 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001407
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001408 tr->current_trace = type;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001409
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001410#ifdef CONFIG_TRACER_MAX_TRACE
1411 if (type->use_max_tr) {
1412 /* If we expanded the buffers, make sure the max is expanded too */
1413 if (ring_buffer_expanded)
1414 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1415 RING_BUFFER_ALL_CPUS);
1416 tr->allocated_snapshot = true;
1417 }
1418#endif
1419
1420 /* the test is responsible for initializing and enabling */
1421 pr_info("Testing tracer %s: ", type->name);
1422 ret = type->selftest(type, tr);
1423 /* the test is responsible for resetting too */
1424 tr->current_trace = saved_tracer;
1425 if (ret) {
1426 printk(KERN_CONT "FAILED!\n");
1427 /* Add the warning after printing 'FAILED' */
1428 WARN_ON(1);
1429 return -1;
1430 }
1431 /* Only reset on passing, to avoid touching corrupted buffers */
1432 tracing_reset_online_cpus(&tr->trace_buffer);
1433
1434#ifdef CONFIG_TRACER_MAX_TRACE
1435 if (type->use_max_tr) {
1436 tr->allocated_snapshot = false;
1437
1438 /* Shrink the max buffer again */
1439 if (ring_buffer_expanded)
1440 ring_buffer_resize(tr->max_buffer.buffer, 1,
1441 RING_BUFFER_ALL_CPUS);
1442 }
1443#endif
1444
1445 printk(KERN_CONT "PASSED\n");
1446 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001447}
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001448#else
1449static inline int run_tracer_selftest(struct tracer *type)
1450{
1451 return 0;
1452}
1453#endif /* CONFIG_FTRACE_STARTUP_TEST */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001454
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001455static void add_tracer_options(struct trace_array *tr, struct tracer *t);
1456
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001457static void __init apply_trace_boot_options(void);
1458
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001459/**
1460 * register_tracer - register a tracer with the ftrace system.
1461 * @type - the plugin for the tracer
1462 *
1463 * Register a new plugin tracer.
1464 */
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001465int __init register_tracer(struct tracer *type)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001466{
1467 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001468 int ret = 0;
1469
1470 if (!type->name) {
1471 pr_info("Tracer must have a name\n");
1472 return -1;
1473 }
1474
Dan Carpenter24a461d2010-07-10 12:06:44 +02001475 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +08001476 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1477 return -1;
1478 }
1479
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001480 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +01001481
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001482 tracing_selftest_running = true;
1483
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001484 for (t = trace_types; t; t = t->next) {
1485 if (strcmp(type->name, t->name) == 0) {
1486 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +08001487 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001488 type->name);
1489 ret = -1;
1490 goto out;
1491 }
1492 }
1493
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001494 if (!type->set_flag)
1495 type->set_flag = &dummy_set_flag;
Chunyu Hud39cdd22016-03-08 21:37:01 +08001496 if (!type->flags) {
1497 /*allocate a dummy tracer_flags*/
1498 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
Chunyu Huc8ca0032016-03-14 20:35:41 +08001499 if (!type->flags) {
1500 ret = -ENOMEM;
1501 goto out;
1502 }
Chunyu Hud39cdd22016-03-08 21:37:01 +08001503 type->flags->val = 0;
1504 type->flags->opts = dummy_tracer_opt;
1505 } else
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001506 if (!type->flags->opts)
1507 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001508
Chunyu Hud39cdd22016-03-08 21:37:01 +08001509 /* store the tracer for __set_tracer_option */
1510 type->flags->trace = type;
1511
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001512 ret = run_tracer_selftest(type);
1513 if (ret < 0)
1514 goto out;
Steven Rostedt60a11772008-05-12 21:20:44 +02001515
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001516 type->next = trace_types;
1517 trace_types = type;
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001518 add_tracer_options(&global_trace, type);
Steven Rostedt60a11772008-05-12 21:20:44 +02001519
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001520 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001521 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001522 mutex_unlock(&trace_types_lock);
1523
Steven Rostedtdac74942009-02-05 01:13:38 -05001524 if (ret || !default_bootup_tracer)
1525 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -05001526
Li Zefanee6c2c12009-09-18 14:06:47 +08001527 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -05001528 goto out_unlock;
1529
1530 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1531 /* Do we want this tracer to start on bootup? */
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05001532 tracing_set_tracer(&global_trace, type->name);
Steven Rostedtdac74942009-02-05 01:13:38 -05001533 default_bootup_tracer = NULL;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001534
1535 apply_trace_boot_options();
1536
Steven Rostedtdac74942009-02-05 01:13:38 -05001537 /* disable other selftests, since this will break it. */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05001538 tracing_selftest_disabled = true;
Steven Rostedtdac74942009-02-05 01:13:38 -05001539#ifdef CONFIG_FTRACE_STARTUP_TEST
1540 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1541 type->name);
1542#endif
1543
1544 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001545 return ret;
1546}
1547
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001548void tracing_reset(struct trace_buffer *buf, int cpu)
Steven Rostedtf6339032009-09-04 12:35:16 -04001549{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001550 struct ring_buffer *buffer = buf->buffer;
Steven Rostedtf6339032009-09-04 12:35:16 -04001551
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001552 if (!buffer)
1553 return;
1554
Steven Rostedtf6339032009-09-04 12:35:16 -04001555 ring_buffer_record_disable(buffer);
1556
1557 /* Make sure all commits have finished */
1558 synchronize_sched();
Steven Rostedt68179682012-05-08 20:57:53 -04001559 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -04001560
1561 ring_buffer_record_enable(buffer);
1562}
1563
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001564void tracing_reset_online_cpus(struct trace_buffer *buf)
Pekka J Enberg213cc062008-12-19 12:08:39 +02001565{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001566 struct ring_buffer *buffer = buf->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +02001567 int cpu;
1568
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001569 if (!buffer)
1570 return;
1571
Steven Rostedt621968c2009-09-04 12:02:35 -04001572 ring_buffer_record_disable(buffer);
1573
1574 /* Make sure all commits have finished */
1575 synchronize_sched();
1576
Alexander Z Lam94571582013-08-02 18:36:16 -07001577 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001578
1579 for_each_online_cpu(cpu)
Steven Rostedt68179682012-05-08 20:57:53 -04001580 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -04001581
1582 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001583}
1584
Steven Rostedt (Red Hat)09d80912013-07-23 22:21:59 -04001585/* Must have trace_types_lock held */
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001586void tracing_reset_all_online_cpus(void)
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001587{
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001588 struct trace_array *tr;
1589
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001590 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001591 tracing_reset_online_cpus(&tr->trace_buffer);
1592#ifdef CONFIG_TRACER_MAX_TRACE
1593 tracing_reset_online_cpus(&tr->max_buffer);
1594#endif
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05001595 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001596}
1597
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001598#define SAVED_CMDLINES_DEFAULT 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001599#define NO_CMDLINE_MAP UINT_MAX
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001600static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001601struct saved_cmdlines_buffer {
1602 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1603 unsigned *map_cmdline_to_pid;
1604 unsigned cmdline_num;
1605 int cmdline_idx;
1606 char *saved_cmdlines;
1607};
1608static struct saved_cmdlines_buffer *savedcmd;
Steven Rostedt25b0b442008-05-12 21:21:00 +02001609
Steven Rostedt25b0b442008-05-12 21:21:00 +02001610/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +01001611static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001612
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001613static inline char *get_saved_cmdlines(int idx)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001614{
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001615 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
1616}
1617
1618static inline void set_cmdline(int idx, const char *cmdline)
1619{
1620 memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
1621}
1622
1623static int allocate_cmdlines_buffer(unsigned int val,
1624 struct saved_cmdlines_buffer *s)
1625{
1626 s->map_cmdline_to_pid = kmalloc(val * sizeof(*s->map_cmdline_to_pid),
1627 GFP_KERNEL);
1628 if (!s->map_cmdline_to_pid)
1629 return -ENOMEM;
1630
1631 s->saved_cmdlines = kmalloc(val * TASK_COMM_LEN, GFP_KERNEL);
1632 if (!s->saved_cmdlines) {
1633 kfree(s->map_cmdline_to_pid);
1634 return -ENOMEM;
1635 }
1636
1637 s->cmdline_idx = 0;
1638 s->cmdline_num = val;
1639 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
1640 sizeof(s->map_pid_to_cmdline));
1641 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
1642 val * sizeof(*s->map_cmdline_to_pid));
1643
1644 return 0;
1645}
1646
1647static int trace_create_savedcmd(void)
1648{
1649 int ret;
1650
Namhyung Kima6af8fb2014-06-10 16:11:35 +09001651 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001652 if (!savedcmd)
1653 return -ENOMEM;
1654
1655 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
1656 if (ret < 0) {
1657 kfree(savedcmd);
1658 savedcmd = NULL;
1659 return -ENOMEM;
1660 }
1661
1662 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001663}
1664
Carsten Emdeb5130b12009-09-13 01:43:07 +02001665int is_tracing_stopped(void)
1666{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001667 return global_trace.stop_count;
Carsten Emdeb5130b12009-09-13 01:43:07 +02001668}
1669
Steven Rostedt0f048702008-11-05 16:05:44 -05001670/**
1671 * tracing_start - quick start of the tracer
1672 *
1673 * If tracing is enabled but was stopped by tracing_stop,
1674 * this will start the tracer back up.
1675 */
1676void tracing_start(void)
1677{
1678 struct ring_buffer *buffer;
1679 unsigned long flags;
1680
1681 if (tracing_disabled)
1682 return;
1683
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001684 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1685 if (--global_trace.stop_count) {
1686 if (global_trace.stop_count < 0) {
Steven Rostedtb06a8302009-01-22 14:26:15 -05001687 /* Someone screwed up their debugging */
1688 WARN_ON_ONCE(1);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001689 global_trace.stop_count = 0;
Steven Rostedtb06a8302009-01-22 14:26:15 -05001690 }
Steven Rostedt0f048702008-11-05 16:05:44 -05001691 goto out;
1692 }
1693
Steven Rostedta2f80712010-03-12 19:56:00 -05001694 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001695 arch_spin_lock(&global_trace.max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -05001696
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001697 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001698 if (buffer)
1699 ring_buffer_record_enable(buffer);
1700
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001701#ifdef CONFIG_TRACER_MAX_TRACE
1702 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001703 if (buffer)
1704 ring_buffer_record_enable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001705#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001706
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001707 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001708
Steven Rostedt0f048702008-11-05 16:05:44 -05001709 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001710 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1711}
1712
1713static void tracing_start_tr(struct trace_array *tr)
1714{
1715 struct ring_buffer *buffer;
1716 unsigned long flags;
1717
1718 if (tracing_disabled)
1719 return;
1720
1721 /* If global, we need to also start the max tracer */
1722 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1723 return tracing_start();
1724
1725 raw_spin_lock_irqsave(&tr->start_lock, flags);
1726
1727 if (--tr->stop_count) {
1728 if (tr->stop_count < 0) {
1729 /* Someone screwed up their debugging */
1730 WARN_ON_ONCE(1);
1731 tr->stop_count = 0;
1732 }
1733 goto out;
1734 }
1735
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001736 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001737 if (buffer)
1738 ring_buffer_record_enable(buffer);
1739
1740 out:
1741 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001742}
1743
1744/**
1745 * tracing_stop - quick stop of the tracer
1746 *
1747 * Light weight way to stop tracing. Use in conjunction with
1748 * tracing_start.
1749 */
1750void tracing_stop(void)
1751{
1752 struct ring_buffer *buffer;
1753 unsigned long flags;
1754
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001755 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1756 if (global_trace.stop_count++)
Steven Rostedt0f048702008-11-05 16:05:44 -05001757 goto out;
1758
Steven Rostedta2f80712010-03-12 19:56:00 -05001759 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001760 arch_spin_lock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001761
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001762 buffer = global_trace.trace_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001763 if (buffer)
1764 ring_buffer_record_disable(buffer);
1765
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001766#ifdef CONFIG_TRACER_MAX_TRACE
1767 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05001768 if (buffer)
1769 ring_buffer_record_disable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001770#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05001771
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001772 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05001773
Steven Rostedt0f048702008-11-05 16:05:44 -05001774 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001775 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1776}
1777
1778static void tracing_stop_tr(struct trace_array *tr)
1779{
1780 struct ring_buffer *buffer;
1781 unsigned long flags;
1782
1783 /* If global, we need to also stop the max tracer */
1784 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1785 return tracing_stop();
1786
1787 raw_spin_lock_irqsave(&tr->start_lock, flags);
1788 if (tr->stop_count++)
1789 goto out;
1790
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001791 buffer = tr->trace_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001792 if (buffer)
1793 ring_buffer_record_disable(buffer);
1794
1795 out:
1796 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001797}
1798
Ingo Molnare309b412008-05-12 21:20:51 +02001799void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001800
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001801static int trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001802{
Carsten Emdea635cf02009-03-18 09:00:41 +01001803 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001804
1805 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001806 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001807
1808 /*
1809 * It's not the end of the world if we don't get
1810 * the lock, but we also don't want to spin
1811 * nor do we want to disable interrupts,
1812 * so if we miss here, then better luck next time.
1813 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001814 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001815 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001816
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001817 idx = savedcmd->map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001818 if (idx == NO_CMDLINE_MAP) {
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001819 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001820
Carsten Emdea635cf02009-03-18 09:00:41 +01001821 /*
1822 * Check whether the cmdline buffer at idx has a pid
1823 * mapped. We are going to overwrite that entry so we
1824 * need to clear the map_pid_to_cmdline. Otherwise we
1825 * would read the new comm for the old pid.
1826 */
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001827 pid = savedcmd->map_cmdline_to_pid[idx];
Carsten Emdea635cf02009-03-18 09:00:41 +01001828 if (pid != NO_CMDLINE_MAP)
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001829 savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001830
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001831 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
1832 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001833
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001834 savedcmd->cmdline_idx = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001835 }
1836
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001837 set_cmdline(idx, tsk->comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001838
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001839 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001840
1841 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001842}
1843
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04001844static void __trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001845{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001846 unsigned map;
1847
Steven Rostedt4ca530852009-03-16 19:20:15 -04001848 if (!pid) {
1849 strcpy(comm, "<idle>");
1850 return;
1851 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001852
Steven Rostedt74bf4072010-01-25 15:11:53 -05001853 if (WARN_ON_ONCE(pid < 0)) {
1854 strcpy(comm, "<XXX>");
1855 return;
1856 }
1857
Steven Rostedt4ca530852009-03-16 19:20:15 -04001858 if (pid > PID_MAX_DEFAULT) {
1859 strcpy(comm, "<...>");
1860 return;
1861 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001862
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001863 map = savedcmd->map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01001864 if (map != NO_CMDLINE_MAP)
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09001865 strcpy(comm, get_saved_cmdlines(map));
Thomas Gleixner50d88752009-03-18 08:58:44 +01001866 else
1867 strcpy(comm, "<...>");
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04001868}
1869
1870void trace_find_cmdline(int pid, char comm[])
1871{
1872 preempt_disable();
1873 arch_spin_lock(&trace_cmdline_lock);
1874
1875 __trace_find_cmdline(pid, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001876
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001877 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001878 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001879}
1880
Ingo Molnare309b412008-05-12 21:20:51 +02001881void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001882{
Steven Rostedt0fb96562012-05-11 14:25:30 -04001883 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001884 return;
1885
Steven Rostedt7ffbd482012-10-11 12:14:25 -04001886 if (!__this_cpu_read(trace_cmdline_save))
1887 return;
1888
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04001889 if (trace_save_cmdline(tsk))
1890 __this_cpu_write(trace_cmdline_save, false);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001891}
1892
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03001893void
Steven Rostedt38697052008-10-01 13:14:09 -04001894tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1895 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001896{
1897 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001898
Steven Rostedt777e2082008-09-29 23:02:42 -04001899 entry->preempt_count = pc & 0xff;
1900 entry->pid = (tsk) ? tsk->pid : 0;
1901 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04001902#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001903 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04001904#else
1905 TRACE_FLAG_IRQS_NOSUPPORT |
1906#endif
Peter Zijlstra7e6867b2016-03-18 16:28:04 +01001907 ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001908 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1909 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
Peter Zijlstrae5137b52013-10-04 17:28:26 +02001910 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
1911 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001912}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02001913EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001914
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04001915static __always_inline void
1916trace_event_setup(struct ring_buffer_event *event,
1917 int type, unsigned long flags, int pc)
1918{
1919 struct trace_entry *ent = ring_buffer_event_data(event);
1920
1921 tracing_generic_entry_update(ent, flags, pc);
1922 ent->type = type;
1923}
1924
Steven Rostedte77405a2009-09-02 14:17:06 -04001925struct ring_buffer_event *
1926trace_buffer_lock_reserve(struct ring_buffer *buffer,
1927 int type,
1928 unsigned long len,
1929 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001930{
1931 struct ring_buffer_event *event;
1932
Steven Rostedte77405a2009-09-02 14:17:06 -04001933 event = ring_buffer_lock_reserve(buffer, len);
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04001934 if (event != NULL)
1935 trace_event_setup(event, type, flags, pc);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001936
1937 return event;
1938}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001939
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04001940DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
1941DEFINE_PER_CPU(int, trace_buffered_event_cnt);
1942static int trace_buffered_event_ref;
1943
1944/**
1945 * trace_buffered_event_enable - enable buffering events
1946 *
1947 * When events are being filtered, it is quicker to use a temporary
1948 * buffer to write the event data into if there's a likely chance
1949 * that it will not be committed. The discard of the ring buffer
1950 * is not as fast as committing, and is much slower than copying
1951 * a commit.
1952 *
1953 * When an event is to be filtered, allocate per cpu buffers to
1954 * write the event data into, and if the event is filtered and discarded
1955 * it is simply dropped, otherwise, the entire data is to be committed
1956 * in one shot.
1957 */
1958void trace_buffered_event_enable(void)
1959{
1960 struct ring_buffer_event *event;
1961 struct page *page;
1962 int cpu;
1963
1964 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
1965
1966 if (trace_buffered_event_ref++)
1967 return;
1968
1969 for_each_tracing_cpu(cpu) {
1970 page = alloc_pages_node(cpu_to_node(cpu),
1971 GFP_KERNEL | __GFP_NORETRY, 0);
1972 if (!page)
1973 goto failed;
1974
1975 event = page_address(page);
1976 memset(event, 0, sizeof(*event));
1977
1978 per_cpu(trace_buffered_event, cpu) = event;
1979
1980 preempt_disable();
1981 if (cpu == smp_processor_id() &&
1982 this_cpu_read(trace_buffered_event) !=
1983 per_cpu(trace_buffered_event, cpu))
1984 WARN_ON_ONCE(1);
1985 preempt_enable();
1986 }
1987
1988 return;
1989 failed:
1990 trace_buffered_event_disable();
1991}
1992
1993static void enable_trace_buffered_event(void *data)
1994{
1995 /* Probably not needed, but do it anyway */
1996 smp_rmb();
1997 this_cpu_dec(trace_buffered_event_cnt);
1998}
1999
2000static void disable_trace_buffered_event(void *data)
2001{
2002 this_cpu_inc(trace_buffered_event_cnt);
2003}
2004
2005/**
2006 * trace_buffered_event_disable - disable buffering events
2007 *
2008 * When a filter is removed, it is faster to not use the buffered
2009 * events, and to commit directly into the ring buffer. Free up
2010 * the temp buffers when there are no more users. This requires
2011 * special synchronization with current events.
2012 */
2013void trace_buffered_event_disable(void)
2014{
2015 int cpu;
2016
2017 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2018
2019 if (WARN_ON_ONCE(!trace_buffered_event_ref))
2020 return;
2021
2022 if (--trace_buffered_event_ref)
2023 return;
2024
2025 preempt_disable();
2026 /* For each CPU, set the buffer as used. */
2027 smp_call_function_many(tracing_buffer_mask,
2028 disable_trace_buffered_event, NULL, 1);
2029 preempt_enable();
2030
2031 /* Wait for all current users to finish */
2032 synchronize_sched();
2033
2034 for_each_tracing_cpu(cpu) {
2035 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2036 per_cpu(trace_buffered_event, cpu) = NULL;
2037 }
2038 /*
2039 * Make sure trace_buffered_event is NULL before clearing
2040 * trace_buffered_event_cnt.
2041 */
2042 smp_wmb();
2043
2044 preempt_disable();
2045 /* Do the work on each cpu */
2046 smp_call_function_many(tracing_buffer_mask,
2047 enable_trace_buffered_event, NULL, 1);
2048 preempt_enable();
2049}
2050
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002051void
2052__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
2053{
2054 __this_cpu_write(trace_cmdline_save, true);
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002055
2056 /* If this is the temp buffer, we need to commit fully */
2057 if (this_cpu_read(trace_buffered_event) == event) {
2058 /* Length is in event->array[0] */
2059 ring_buffer_write(buffer, event->array[0], &event->array[1]);
2060 /* Release the temp buffer */
2061 this_cpu_dec(trace_buffered_event_cnt);
2062 } else
2063 ring_buffer_unlock_commit(buffer, event);
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002064}
2065
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002066static struct ring_buffer *temp_buffer;
2067
Steven Rostedtef5580d2009-02-27 19:38:04 -05002068struct ring_buffer_event *
Steven Rostedtccb469a2012-08-02 10:32:10 -04002069trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002070 struct trace_event_file *trace_file,
Steven Rostedtccb469a2012-08-02 10:32:10 -04002071 int type, unsigned long len,
2072 unsigned long flags, int pc)
2073{
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002074 struct ring_buffer_event *entry;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002075 int val;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002076
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002077 *current_rb = trace_file->tr->trace_buffer.buffer;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002078
2079 if ((trace_file->flags &
2080 (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
2081 (entry = this_cpu_read(trace_buffered_event))) {
2082 /* Try to use the per cpu buffer first */
2083 val = this_cpu_inc_return(trace_buffered_event_cnt);
2084 if (val == 1) {
2085 trace_event_setup(entry, type, flags, pc);
2086 entry->array[0] = len;
2087 return entry;
2088 }
2089 this_cpu_dec(trace_buffered_event_cnt);
2090 }
2091
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002092 entry = trace_buffer_lock_reserve(*current_rb,
Steven Rostedtccb469a2012-08-02 10:32:10 -04002093 type, len, flags, pc);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002094 /*
2095 * If tracing is off, but we have triggers enabled
2096 * we still need to look at the event data. Use the temp_buffer
2097 * to store the trace event for the tigger to use. It's recusive
2098 * safe and will not be recorded anywhere.
2099 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002100 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002101 *current_rb = temp_buffer;
2102 entry = trace_buffer_lock_reserve(*current_rb,
2103 type, len, flags, pc);
2104 }
2105 return entry;
Steven Rostedtccb469a2012-08-02 10:32:10 -04002106}
2107EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2108
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04002109void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2110 struct ring_buffer *buffer,
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04002111 struct ring_buffer_event *event,
2112 unsigned long flags, int pc,
2113 struct pt_regs *regs)
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002114{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002115 __buffer_unlock_commit(buffer, event);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002116
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002117 /*
2118 * If regs is not set, then skip the following callers:
2119 * trace_buffer_unlock_commit_regs
2120 * event_trigger_unlock_commit
2121 * trace_event_buffer_commit
2122 * trace_event_raw_event_sched_switch
2123 * Note, we can still get here via blktrace, wakeup tracer
2124 * and mmiotrace, but that's ok if they lose a function or
2125 * two. They are that meaningful.
2126 */
2127 ftrace_trace_stack(tr, buffer, flags, regs ? 0 : 4, pc, regs);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002128 ftrace_trace_userstack(buffer, flags, pc);
2129}
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002130
Ingo Molnare309b412008-05-12 21:20:51 +02002131void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05002132trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04002133 unsigned long ip, unsigned long parent_ip, unsigned long flags,
2134 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002135{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002136 struct trace_event_call *call = &event_function;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002137 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002138 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04002139 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002140
Steven Rostedte77405a2009-09-02 14:17:06 -04002141 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002142 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002143 if (!event)
2144 return;
2145 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04002146 entry->ip = ip;
2147 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05002148
Tom Zanussif306cc82013-10-24 08:34:17 -05002149 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002150 __buffer_unlock_commit(buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002151}
2152
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002153#ifdef CONFIG_STACKTRACE
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002154
2155#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
2156struct ftrace_stack {
2157 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
2158};
2159
2160static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
2161static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2162
Steven Rostedte77405a2009-09-02 14:17:06 -04002163static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05002164 unsigned long flags,
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002165 int skip, int pc, struct pt_regs *regs)
Ingo Molnar86387f72008-05-12 21:20:51 +02002166{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002167 struct trace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002168 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04002169 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02002170 struct stack_trace trace;
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002171 int use_stack;
2172 int size = FTRACE_STACK_ENTRIES;
Ingo Molnar86387f72008-05-12 21:20:51 +02002173
2174 trace.nr_entries = 0;
Ingo Molnar86387f72008-05-12 21:20:51 +02002175 trace.skip = skip;
Ingo Molnar86387f72008-05-12 21:20:51 +02002176
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002177 /*
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002178 * Add two, for this function and the call to save_stack_trace()
2179 * If regs is set, then these functions will not be in the way.
2180 */
2181 if (!regs)
2182 trace.skip += 2;
2183
2184 /*
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002185 * Since events can happen in NMIs there's no safe way to
2186 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2187 * or NMI comes in, it will just have to use the default
2188 * FTRACE_STACK_SIZE.
2189 */
2190 preempt_disable_notrace();
2191
Shan Wei82146522012-11-19 13:21:01 +08002192 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002193 /*
2194 * We don't need any atomic variables, just a barrier.
2195 * If an interrupt comes in, we don't care, because it would
2196 * have exited and put the counter back to what we want.
2197 * We just need a barrier to keep gcc from moving things
2198 * around.
2199 */
2200 barrier();
2201 if (use_stack == 1) {
Christoph Lameterbdffd892014-04-29 14:17:40 -05002202 trace.entries = this_cpu_ptr(ftrace_stack.calls);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002203 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
2204
2205 if (regs)
2206 save_stack_trace_regs(regs, &trace);
2207 else
2208 save_stack_trace(&trace);
2209
2210 if (trace.nr_entries > size)
2211 size = trace.nr_entries;
2212 } else
2213 /* From now on, use_stack is a boolean */
2214 use_stack = 0;
2215
2216 size *= sizeof(unsigned long);
2217
2218 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
2219 sizeof(*entry) + size, flags, pc);
2220 if (!event)
2221 goto out;
2222 entry = ring_buffer_event_data(event);
2223
2224 memset(&entry->caller, 0, size);
2225
2226 if (use_stack)
2227 memcpy(&entry->caller, trace.entries,
2228 trace.nr_entries * sizeof(unsigned long));
2229 else {
2230 trace.max_entries = FTRACE_STACK_ENTRIES;
2231 trace.entries = entry->caller;
2232 if (regs)
2233 save_stack_trace_regs(regs, &trace);
2234 else
2235 save_stack_trace(&trace);
2236 }
2237
2238 entry->size = trace.nr_entries;
2239
Tom Zanussif306cc82013-10-24 08:34:17 -05002240 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002241 __buffer_unlock_commit(buffer, event);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002242
2243 out:
2244 /* Again, don't let gcc optimize things here */
2245 barrier();
Shan Wei82146522012-11-19 13:21:01 +08002246 __this_cpu_dec(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002247 preempt_enable_notrace();
2248
Ingo Molnarf0a920d2008-05-12 21:20:47 +02002249}
2250
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002251static inline void ftrace_trace_stack(struct trace_array *tr,
2252 struct ring_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002253 unsigned long flags,
2254 int skip, int pc, struct pt_regs *regs)
Steven Rostedt53614992009-01-15 19:12:40 -05002255{
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002256 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
Steven Rostedt53614992009-01-15 19:12:40 -05002257 return;
2258
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002259 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
Steven Rostedt53614992009-01-15 19:12:40 -05002260}
2261
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002262void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
2263 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04002264{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002265 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
Steven Rostedt38697052008-10-01 13:14:09 -04002266}
2267
Steven Rostedt03889382009-12-11 09:48:22 -05002268/**
2269 * trace_dump_stack - record a stack back trace in the trace buffer
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002270 * @skip: Number of functions to skip (helper handlers)
Steven Rostedt03889382009-12-11 09:48:22 -05002271 */
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002272void trace_dump_stack(int skip)
Steven Rostedt03889382009-12-11 09:48:22 -05002273{
2274 unsigned long flags;
2275
2276 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05002277 return;
Steven Rostedt03889382009-12-11 09:48:22 -05002278
2279 local_save_flags(flags);
2280
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04002281 /*
2282 * Skip 3 more, seems to get us at the caller of
2283 * this function.
2284 */
2285 skip += 3;
2286 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
2287 flags, skip, preempt_count(), NULL);
Steven Rostedt03889382009-12-11 09:48:22 -05002288}
2289
Steven Rostedt91e86e52010-11-10 12:56:12 +01002290static DEFINE_PER_CPU(int, user_stack_count);
2291
Steven Rostedte77405a2009-09-02 14:17:06 -04002292void
2293ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02002294{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002295 struct trace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02002296 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02002297 struct userstack_entry *entry;
2298 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02002299
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04002300 if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE))
Török Edwin02b67512008-11-22 13:28:47 +02002301 return;
2302
Steven Rostedtb6345872010-03-12 20:03:30 -05002303 /*
2304 * NMIs can not handle page faults, even with fix ups.
2305 * The save user stack can (and often does) fault.
2306 */
2307 if (unlikely(in_nmi()))
2308 return;
2309
Steven Rostedt91e86e52010-11-10 12:56:12 +01002310 /*
2311 * prevent recursion, since the user stack tracing may
2312 * trigger other kernel events.
2313 */
2314 preempt_disable();
2315 if (__this_cpu_read(user_stack_count))
2316 goto out;
2317
2318 __this_cpu_inc(user_stack_count);
2319
Steven Rostedte77405a2009-09-02 14:17:06 -04002320 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002321 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02002322 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08002323 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02002324 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02002325
Steven Rostedt48659d32009-09-11 11:36:23 -04002326 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02002327 memset(&entry->caller, 0, sizeof(entry->caller));
2328
2329 trace.nr_entries = 0;
2330 trace.max_entries = FTRACE_STACK_ENTRIES;
2331 trace.skip = 0;
2332 trace.entries = entry->caller;
2333
2334 save_stack_trace_user(&trace);
Tom Zanussif306cc82013-10-24 08:34:17 -05002335 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002336 __buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01002337
Li Zefan1dbd1952010-12-09 15:47:56 +08002338 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01002339 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01002340 out:
2341 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02002342}
2343
Hannes Eder4fd27352009-02-10 19:44:12 +01002344#ifdef UNUSED
2345static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02002346{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05002347 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02002348}
Hannes Eder4fd27352009-02-10 19:44:12 +01002349#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02002350
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002351#endif /* CONFIG_STACKTRACE */
2352
Steven Rostedt07d777f2011-09-22 14:01:55 -04002353/* created for use with alloc_percpu */
2354struct trace_buffer_struct {
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002355 int nesting;
2356 char buffer[4][TRACE_BUF_SIZE];
Steven Rostedt07d777f2011-09-22 14:01:55 -04002357};
2358
2359static struct trace_buffer_struct *trace_percpu_buffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002360
2361/*
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002362 * Thise allows for lockless recording. If we're nested too deeply, then
2363 * this returns NULL.
Steven Rostedt07d777f2011-09-22 14:01:55 -04002364 */
2365static char *get_trace_buf(void)
2366{
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002367 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
Steven Rostedt07d777f2011-09-22 14:01:55 -04002368
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002369 if (!buffer || buffer->nesting >= 4)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002370 return NULL;
2371
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002372 return &buffer->buffer[buffer->nesting++][0];
2373}
2374
2375static void put_trace_buf(void)
2376{
2377 this_cpu_dec(trace_percpu_buffer->nesting);
Steven Rostedt07d777f2011-09-22 14:01:55 -04002378}
2379
2380static int alloc_percpu_trace_buffer(void)
2381{
2382 struct trace_buffer_struct *buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002383
2384 buffers = alloc_percpu(struct trace_buffer_struct);
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002385 if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
2386 return -ENOMEM;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002387
2388 trace_percpu_buffer = buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002389 return 0;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002390}
2391
Steven Rostedt81698832012-10-11 10:15:05 -04002392static int buffers_allocated;
2393
Steven Rostedt07d777f2011-09-22 14:01:55 -04002394void trace_printk_init_buffers(void)
2395{
Steven Rostedt07d777f2011-09-22 14:01:55 -04002396 if (buffers_allocated)
2397 return;
2398
2399 if (alloc_percpu_trace_buffer())
2400 return;
2401
Steven Rostedt2184db42014-05-28 13:14:40 -04002402 /* trace_printk() is for debug use only. Don't use it in production. */
2403
Joe Perchesa395d6a2016-03-22 14:28:09 -07002404 pr_warn("\n");
2405 pr_warn("**********************************************************\n");
2406 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2407 pr_warn("** **\n");
2408 pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
2409 pr_warn("** **\n");
2410 pr_warn("** This means that this is a DEBUG kernel and it is **\n");
2411 pr_warn("** unsafe for production use. **\n");
2412 pr_warn("** **\n");
2413 pr_warn("** If you see this message and you are not debugging **\n");
2414 pr_warn("** the kernel, report this immediately to your vendor! **\n");
2415 pr_warn("** **\n");
2416 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2417 pr_warn("**********************************************************\n");
Steven Rostedt07d777f2011-09-22 14:01:55 -04002418
Steven Rostedtb382ede62012-10-10 21:44:34 -04002419 /* Expand the buffers to set size */
2420 tracing_update_buffers();
2421
Steven Rostedt07d777f2011-09-22 14:01:55 -04002422 buffers_allocated = 1;
Steven Rostedt81698832012-10-11 10:15:05 -04002423
2424 /*
2425 * trace_printk_init_buffers() can be called by modules.
2426 * If that happens, then we need to start cmdline recording
2427 * directly here. If the global_trace.buffer is already
2428 * allocated here, then this was called by module code.
2429 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002430 if (global_trace.trace_buffer.buffer)
Steven Rostedt81698832012-10-11 10:15:05 -04002431 tracing_start_cmdline_record();
2432}
2433
2434void trace_printk_start_comm(void)
2435{
2436 /* Start tracing comms if trace printk is set */
2437 if (!buffers_allocated)
2438 return;
2439 tracing_start_cmdline_record();
2440}
2441
2442static void trace_printk_start_stop_comm(int enabled)
2443{
2444 if (!buffers_allocated)
2445 return;
2446
2447 if (enabled)
2448 tracing_start_cmdline_record();
2449 else
2450 tracing_stop_cmdline_record();
Steven Rostedt07d777f2011-09-22 14:01:55 -04002451}
2452
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002453/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002454 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002455 *
2456 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04002457int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002458{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002459 struct trace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002460 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04002461 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002462 struct trace_array *tr = &global_trace;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002463 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002464 unsigned long flags;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002465 char *tbuffer;
2466 int len = 0, size, pc;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002467
2468 if (unlikely(tracing_selftest_running || tracing_disabled))
2469 return 0;
2470
2471 /* Don't pollute graph traces with trace_vprintk internals */
2472 pause_graph_tracing();
2473
2474 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04002475 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002476
Steven Rostedt07d777f2011-09-22 14:01:55 -04002477 tbuffer = get_trace_buf();
2478 if (!tbuffer) {
2479 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002480 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002481 }
2482
2483 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
2484
2485 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002486 goto out;
2487
Steven Rostedt07d777f2011-09-22 14:01:55 -04002488 local_save_flags(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002489 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002490 buffer = tr->trace_buffer.buffer;
Steven Rostedte77405a2009-09-02 14:17:06 -04002491 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2492 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002493 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002494 goto out;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002495 entry = ring_buffer_event_data(event);
2496 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002497 entry->fmt = fmt;
2498
Steven Rostedt07d777f2011-09-22 14:01:55 -04002499 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
Tom Zanussif306cc82013-10-24 08:34:17 -05002500 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002501 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002502 ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05002503 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002504
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002505out:
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002506 put_trace_buf();
2507
2508out_nobuffer:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002509 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002510 unpause_graph_tracing();
2511
2512 return len;
2513}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002514EXPORT_SYMBOL_GPL(trace_vbprintk);
2515
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002516static int
2517__trace_array_vprintk(struct ring_buffer *buffer,
2518 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002519{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002520 struct trace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002521 struct ring_buffer_event *event;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002522 int len = 0, size, pc;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002523 struct print_entry *entry;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002524 unsigned long flags;
2525 char *tbuffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002526
2527 if (tracing_disabled || tracing_selftest_running)
2528 return 0;
2529
Steven Rostedt07d777f2011-09-22 14:01:55 -04002530 /* Don't pollute graph traces with trace_vprintk internals */
2531 pause_graph_tracing();
2532
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002533 pc = preempt_count();
2534 preempt_disable_notrace();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002535
Steven Rostedt07d777f2011-09-22 14:01:55 -04002536
2537 tbuffer = get_trace_buf();
2538 if (!tbuffer) {
2539 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002540 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04002541 }
2542
Dan Carpenter3558a5a2014-11-27 18:57:52 +03002543 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002544
Steven Rostedt07d777f2011-09-22 14:01:55 -04002545 local_save_flags(flags);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002546 size = sizeof(*entry) + len + 1;
Steven Rostedte77405a2009-09-02 14:17:06 -04002547 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
Steven Rostedt07d777f2011-09-22 14:01:55 -04002548 flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002549 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04002550 goto out;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002551 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01002552 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002553
Dan Carpenter3558a5a2014-11-27 18:57:52 +03002554 memcpy(&entry->buf, tbuffer, len + 1);
Tom Zanussif306cc82013-10-24 08:34:17 -05002555 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002556 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002557 ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05002558 }
Andy Lutomirskie2ace002016-05-26 12:00:33 -07002559
2560out:
2561 put_trace_buf();
2562
2563out_nobuffer:
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002564 preempt_enable_notrace();
Steven Rostedt07d777f2011-09-22 14:01:55 -04002565 unpause_graph_tracing();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002566
2567 return len;
2568}
Steven Rostedt659372d2009-09-03 19:11:07 -04002569
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002570int trace_array_vprintk(struct trace_array *tr,
2571 unsigned long ip, const char *fmt, va_list args)
2572{
2573 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2574}
2575
2576int trace_array_printk(struct trace_array *tr,
2577 unsigned long ip, const char *fmt, ...)
2578{
2579 int ret;
2580 va_list ap;
2581
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04002582 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002583 return 0;
2584
2585 va_start(ap, fmt);
2586 ret = trace_array_vprintk(tr, ip, fmt, ap);
2587 va_end(ap);
2588 return ret;
2589}
2590
2591int trace_array_printk_buf(struct ring_buffer *buffer,
2592 unsigned long ip, const char *fmt, ...)
2593{
2594 int ret;
2595 va_list ap;
2596
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04002597 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002598 return 0;
2599
2600 va_start(ap, fmt);
2601 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2602 va_end(ap);
2603 return ret;
2604}
2605
Steven Rostedt659372d2009-09-03 19:11:07 -04002606int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2607{
Steven Rostedta813a152009-10-09 01:41:35 -04002608 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04002609}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01002610EXPORT_SYMBOL_GPL(trace_vprintk);
2611
Robert Richtere2ac8ef2008-11-12 12:59:32 +01002612static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04002613{
Steven Rostedt6d158a82012-06-27 20:46:14 -04002614 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2615
Steven Rostedt5a90f572008-09-03 17:42:51 -04002616 iter->idx++;
Steven Rostedt6d158a82012-06-27 20:46:14 -04002617 if (buf_iter)
2618 ring_buffer_read(buf_iter, NULL);
Steven Rostedt5a90f572008-09-03 17:42:51 -04002619}
2620
Ingo Molnare309b412008-05-12 21:20:51 +02002621static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04002622peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2623 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002624{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002625 struct ring_buffer_event *event;
Steven Rostedt6d158a82012-06-27 20:46:14 -04002626 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002627
Steven Rostedtd7690412008-10-01 00:29:53 -04002628 if (buf_iter)
2629 event = ring_buffer_iter_peek(buf_iter, ts);
2630 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002631 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04002632 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04002633
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002634 if (event) {
2635 iter->ent_size = ring_buffer_event_length(event);
2636 return ring_buffer_event_data(event);
2637 }
2638 iter->ent_size = 0;
2639 return NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002640}
Steven Rostedtd7690412008-10-01 00:29:53 -04002641
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002642static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04002643__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2644 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002645{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002646 struct ring_buffer *buffer = iter->trace_buffer->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002647 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08002648 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002649 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002650 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002651 int next_cpu = -1;
Steven Rostedt12b5da32012-03-27 10:43:28 -04002652 int next_size = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002653 int cpu;
2654
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002655 /*
2656 * If we are in a per_cpu trace file, don't bother by iterating over
2657 * all cpu and peek directly.
2658 */
Steven Rostedtae3b5092013-01-23 15:22:59 -05002659 if (cpu_file > RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002660 if (ring_buffer_empty_cpu(buffer, cpu_file))
2661 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002662 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002663 if (ent_cpu)
2664 *ent_cpu = cpu_file;
2665
2666 return ent;
2667 }
2668
Steven Rostedtab464282008-05-12 21:21:00 +02002669 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002670
2671 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002672 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002673
Steven Rostedtbc21b472010-03-31 19:49:26 -04002674 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002675
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02002676 /*
2677 * Pick the entry with the smallest timestamp:
2678 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002679 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002680 next = ent;
2681 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002682 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002683 next_lost = lost_events;
Steven Rostedt12b5da32012-03-27 10:43:28 -04002684 next_size = iter->ent_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002685 }
2686 }
2687
Steven Rostedt12b5da32012-03-27 10:43:28 -04002688 iter->ent_size = next_size;
2689
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002690 if (ent_cpu)
2691 *ent_cpu = next_cpu;
2692
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002693 if (ent_ts)
2694 *ent_ts = next_ts;
2695
Steven Rostedtbc21b472010-03-31 19:49:26 -04002696 if (missing_events)
2697 *missing_events = next_lost;
2698
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002699 return next;
2700}
2701
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002702/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002703struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2704 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002705{
Steven Rostedtbc21b472010-03-31 19:49:26 -04002706 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002707}
Ingo Molnar8c523a92008-05-12 21:20:46 +02002708
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002709/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05002710void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002711{
Steven Rostedtbc21b472010-03-31 19:49:26 -04002712 iter->ent = __find_next_entry(iter, &iter->cpu,
2713 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002714
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002715 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01002716 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002717
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002718 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002719}
2720
Ingo Molnare309b412008-05-12 21:20:51 +02002721static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002722{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002723 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04002724 &iter->lost_events);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002725}
2726
Ingo Molnare309b412008-05-12 21:20:51 +02002727static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002728{
2729 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002730 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002731 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002732
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002733 WARN_ON_ONCE(iter->leftover);
2734
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002735 (*pos)++;
2736
2737 /* can't go backwards */
2738 if (iter->idx > i)
2739 return NULL;
2740
2741 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05002742 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002743 else
2744 ent = iter;
2745
2746 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05002747 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002748
2749 iter->pos = *pos;
2750
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002751 return ent;
2752}
2753
Jason Wessel955b61e2010-08-05 09:22:23 -05002754void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002755{
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002756 struct ring_buffer_event *event;
2757 struct ring_buffer_iter *buf_iter;
2758 unsigned long entries = 0;
2759 u64 ts;
2760
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002761 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002762
Steven Rostedt6d158a82012-06-27 20:46:14 -04002763 buf_iter = trace_buffer_iter(iter, cpu);
2764 if (!buf_iter)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002765 return;
2766
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002767 ring_buffer_iter_reset(buf_iter);
2768
2769 /*
2770 * We could have the case with the max latency tracers
2771 * that a reset never took place on a cpu. This is evident
2772 * by the timestamp being before the start of the buffer.
2773 */
2774 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002775 if (ts >= iter->trace_buffer->time_start)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002776 break;
2777 entries++;
2778 ring_buffer_read(buf_iter, NULL);
2779 }
2780
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002781 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002782}
2783
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002784/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002785 * The current tracer is copied to avoid a global locking
2786 * all around.
2787 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002788static void *s_start(struct seq_file *m, loff_t *pos)
2789{
2790 struct trace_iterator *iter = m->private;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002791 struct trace_array *tr = iter->tr;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002792 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002793 void *p = NULL;
2794 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002795 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002796
Hiraku Toyooka2fd196e2012-12-26 11:52:52 +09002797 /*
2798 * copy the tracer to avoid using a global lock all around.
2799 * iter->trace is a copy of current_trace, the pointer to the
2800 * name may be used instead of a strcmp(), as iter->trace->name
2801 * will point to the same string as current_trace->name.
2802 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002803 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002804 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2805 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002806 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002807
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002808#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002809 if (iter->snapshot && iter->trace->use_max_tr)
2810 return ERR_PTR(-EBUSY);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002811#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002812
2813 if (!iter->snapshot)
2814 atomic_inc(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002815
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002816 if (*pos != iter->pos) {
2817 iter->ent = NULL;
2818 iter->cpu = 0;
2819 iter->idx = -1;
2820
Steven Rostedtae3b5092013-01-23 15:22:59 -05002821 if (cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002822 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002823 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002824 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002825 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002826
Lai Jiangshanac91d852010-03-02 17:54:50 +08002827 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002828 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2829 ;
2830
2831 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002832 /*
2833 * If we overflowed the seq_file before, then we want
2834 * to just reuse the trace_seq buffer again.
2835 */
2836 if (iter->leftover)
2837 p = iter;
2838 else {
2839 l = *pos - 1;
2840 p = s_next(m, p, &l);
2841 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002842 }
2843
Lai Jiangshan4f535962009-05-18 19:35:34 +08002844 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002845 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002846 return p;
2847}
2848
2849static void s_stop(struct seq_file *m, void *p)
2850{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002851 struct trace_iterator *iter = m->private;
2852
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002853#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002854 if (iter->snapshot && iter->trace->use_max_tr)
2855 return;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002856#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09002857
2858 if (!iter->snapshot)
2859 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002860
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08002861 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08002862 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002863}
2864
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002865static void
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002866get_total_entries(struct trace_buffer *buf,
2867 unsigned long *total, unsigned long *entries)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002868{
2869 unsigned long count;
2870 int cpu;
2871
2872 *total = 0;
2873 *entries = 0;
2874
2875 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002876 count = ring_buffer_entries_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002877 /*
2878 * If this buffer has skipped entries, then we hold all
2879 * entries for the trace and we need to ignore the
2880 * ones before the time stamp.
2881 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002882 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2883 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002884 /* total is the same as the entries */
2885 *total += count;
2886 } else
2887 *total += count +
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002888 ring_buffer_overrun_cpu(buf->buffer, cpu);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002889 *entries += count;
2890 }
2891}
2892
Ingo Molnare309b412008-05-12 21:20:51 +02002893static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002894{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002895 seq_puts(m, "# _------=> CPU# \n"
2896 "# / _-----=> irqs-off \n"
2897 "# | / _----=> need-resched \n"
2898 "# || / _---=> hardirq/softirq \n"
2899 "# ||| / _--=> preempt-depth \n"
2900 "# |||| / delay \n"
2901 "# cmd pid ||||| time | caller \n"
2902 "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002903}
2904
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002905static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002906{
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002907 unsigned long total;
2908 unsigned long entries;
2909
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002910 get_total_entries(buf, &total, &entries);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002911 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2912 entries, total, num_online_cpus());
2913 seq_puts(m, "#\n");
2914}
2915
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002916static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002917{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002918 print_event_info(buf, m);
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002919 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"
2920 "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002921}
2922
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002923static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
Steven Rostedt77271ce2011-11-17 09:34:33 -05002924{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002925 print_event_info(buf, m);
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01002926 seq_puts(m, "# _-----=> irqs-off\n"
2927 "# / _----=> need-resched\n"
2928 "# | / _---=> hardirq/softirq\n"
2929 "# || / _--=> preempt-depth\n"
2930 "# ||| / delay\n"
2931 "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
2932 "# | | | |||| | |\n");
Steven Rostedt77271ce2011-11-17 09:34:33 -05002933}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002934
Jiri Olsa62b915f2010-04-02 19:01:22 +02002935void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002936print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2937{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04002938 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002939 struct trace_buffer *buf = iter->trace_buffer;
2940 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002941 struct tracer *type = iter->trace;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002942 unsigned long entries;
2943 unsigned long total;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002944 const char *name = "preemption";
2945
Steven Rostedt (Red Hat)d840f712013-02-01 18:38:47 -05002946 name = type->name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002947
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002948 get_total_entries(buf, &total, &entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002949
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002950 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002951 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002952 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002953 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002954 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002955 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02002956 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002957 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002958 total,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002959 buf->cpu,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002960#if defined(CONFIG_PREEMPT_NONE)
2961 "server",
2962#elif defined(CONFIG_PREEMPT_VOLUNTARY)
2963 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04002964#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002965 "preempt",
2966#else
2967 "unknown",
2968#endif
2969 /* These are reserved for later use */
2970 0, 0, 0, 0);
2971#ifdef CONFIG_SMP
2972 seq_printf(m, " #P:%d)\n", num_online_cpus());
2973#else
2974 seq_puts(m, ")\n");
2975#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002976 seq_puts(m, "# -----------------\n");
2977 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002978 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
Eric W. Biedermand20b92a2012-03-13 16:02:19 -07002979 data->comm, data->pid,
2980 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002981 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002982 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002983
2984 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002985 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02002986 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2987 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002988 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02002989 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2990 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04002991 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002992 }
2993
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002994 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002995}
2996
Steven Rostedta3097202008-11-07 22:36:02 -05002997static void test_cpu_buff_start(struct trace_iterator *iter)
2998{
2999 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003000 struct trace_array *tr = iter->tr;
Steven Rostedta3097202008-11-07 22:36:02 -05003001
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003002 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003003 return;
3004
3005 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
3006 return;
3007
Sasha Levin919cd972015-09-04 12:45:56 -04003008 if (iter->started && cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05003009 return;
3010
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003011 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003012 return;
3013
Sasha Levin919cd972015-09-04 12:45:56 -04003014 if (iter->started)
3015 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003016
3017 /* Don't print started cpu buffer for the first entry of the trace */
3018 if (iter->idx > 1)
3019 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
3020 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05003021}
3022
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003023static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003024{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003025 struct trace_array *tr = iter->tr;
Steven Rostedt214023c2008-05-12 21:20:46 +02003026 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003027 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003028 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003029 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003030
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003031 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003032
Steven Rostedta3097202008-11-07 22:36:02 -05003033 test_cpu_buff_start(iter);
3034
Steven Rostedtf633cef2008-12-23 23:24:13 -05003035 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003036
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003037 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003038 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3039 trace_print_lat_context(iter);
3040 else
3041 trace_print_context(iter);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003042 }
3043
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003044 if (trace_seq_has_overflowed(s))
3045 return TRACE_TYPE_PARTIAL_LINE;
3046
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003047 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003048 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003049
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003050 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003051
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003052 return trace_handle_return(s);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003053}
3054
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003055static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003056{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003057 struct trace_array *tr = iter->tr;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003058 struct trace_seq *s = &iter->seq;
3059 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003060 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003061
3062 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003063
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003064 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003065 trace_seq_printf(s, "%d %d %llu ",
3066 entry->pid, iter->cpu, iter->ts);
3067
3068 if (trace_seq_has_overflowed(s))
3069 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003070
Steven Rostedtf633cef2008-12-23 23:24:13 -05003071 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003072 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003073 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003074
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003075 trace_seq_printf(s, "%d ?\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003076
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003077 return trace_handle_return(s);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003078}
3079
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003080static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003081{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003082 struct trace_array *tr = iter->tr;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003083 struct trace_seq *s = &iter->seq;
3084 unsigned char newline = '\n';
3085 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003086 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003087
3088 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003089
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003090 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003091 SEQ_PUT_HEX_FIELD(s, entry->pid);
3092 SEQ_PUT_HEX_FIELD(s, iter->cpu);
3093 SEQ_PUT_HEX_FIELD(s, iter->ts);
3094 if (trace_seq_has_overflowed(s))
3095 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003096 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003097
Steven Rostedtf633cef2008-12-23 23:24:13 -05003098 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003099 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04003100 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003101 if (ret != TRACE_TYPE_HANDLED)
3102 return ret;
3103 }
Steven Rostedt7104f302008-10-01 10:52:51 -04003104
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003105 SEQ_PUT_FIELD(s, newline);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003106
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003107 return trace_handle_return(s);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003108}
3109
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003110static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003111{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003112 struct trace_array *tr = iter->tr;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003113 struct trace_seq *s = &iter->seq;
3114 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003115 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003116
3117 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003118
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003119 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003120 SEQ_PUT_FIELD(s, entry->pid);
3121 SEQ_PUT_FIELD(s, iter->cpu);
3122 SEQ_PUT_FIELD(s, iter->ts);
3123 if (trace_seq_has_overflowed(s))
3124 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003125 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003126
Steven Rostedtf633cef2008-12-23 23:24:13 -05003127 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04003128 return event ? event->funcs->binary(iter, 0, event) :
3129 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003130}
3131
Jiri Olsa62b915f2010-04-02 19:01:22 +02003132int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003133{
Steven Rostedt6d158a82012-06-27 20:46:14 -04003134 struct ring_buffer_iter *buf_iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003135 int cpu;
3136
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003137 /* If we are looking at one CPU buffer, only check that one */
Steven Rostedtae3b5092013-01-23 15:22:59 -05003138 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003139 cpu = iter->cpu_file;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003140 buf_iter = trace_buffer_iter(iter, cpu);
3141 if (buf_iter) {
3142 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003143 return 0;
3144 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003145 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003146 return 0;
3147 }
3148 return 1;
3149 }
3150
Steven Rostedtab464282008-05-12 21:21:00 +02003151 for_each_tracing_cpu(cpu) {
Steven Rostedt6d158a82012-06-27 20:46:14 -04003152 buf_iter = trace_buffer_iter(iter, cpu);
3153 if (buf_iter) {
3154 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedtd7690412008-10-01 00:29:53 -04003155 return 0;
3156 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003157 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
Steven Rostedtd7690412008-10-01 00:29:53 -04003158 return 0;
3159 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003160 }
Steven Rostedtd7690412008-10-01 00:29:53 -04003161
Frederic Weisbecker797d3712008-09-30 18:13:45 +02003162 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003163}
3164
Lai Jiangshan4f535962009-05-18 19:35:34 +08003165/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05003166enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003167{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003168 struct trace_array *tr = iter->tr;
3169 unsigned long trace_flags = tr->trace_flags;
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003170 enum print_line_t ret;
3171
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003172 if (iter->lost_events) {
3173 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3174 iter->cpu, iter->lost_events);
3175 if (trace_seq_has_overflowed(&iter->seq))
3176 return TRACE_TYPE_PARTIAL_LINE;
3177 }
Steven Rostedtbc21b472010-03-31 19:49:26 -04003178
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003179 if (iter->trace && iter->trace->print_line) {
3180 ret = iter->trace->print_line(iter);
3181 if (ret != TRACE_TYPE_UNHANDLED)
3182 return ret;
3183 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02003184
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -05003185 if (iter->ent->type == TRACE_BPUTS &&
3186 trace_flags & TRACE_ITER_PRINTK &&
3187 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3188 return trace_print_bputs_msg_only(iter);
3189
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003190 if (iter->ent->type == TRACE_BPRINT &&
3191 trace_flags & TRACE_ITER_PRINTK &&
3192 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04003193 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003194
Frederic Weisbecker66896a82008-12-13 20:18:13 +01003195 if (iter->ent->type == TRACE_PRINT &&
3196 trace_flags & TRACE_ITER_PRINTK &&
3197 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04003198 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01003199
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003200 if (trace_flags & TRACE_ITER_BIN)
3201 return print_bin_fmt(iter);
3202
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003203 if (trace_flags & TRACE_ITER_HEX)
3204 return print_hex_fmt(iter);
3205
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003206 if (trace_flags & TRACE_ITER_RAW)
3207 return print_raw_fmt(iter);
3208
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003209 return print_trace_fmt(iter);
3210}
3211
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003212void trace_latency_header(struct seq_file *m)
3213{
3214 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003215 struct trace_array *tr = iter->tr;
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003216
3217 /* print nothing if the buffers are empty */
3218 if (trace_empty(iter))
3219 return;
3220
3221 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3222 print_trace_header(m, iter);
3223
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003224 if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01003225 print_lat_help_header(m);
3226}
3227
Jiri Olsa62b915f2010-04-02 19:01:22 +02003228void trace_default_header(struct seq_file *m)
3229{
3230 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003231 struct trace_array *tr = iter->tr;
3232 unsigned long trace_flags = tr->trace_flags;
Jiri Olsa62b915f2010-04-02 19:01:22 +02003233
Jiri Olsaf56e7f82011-06-03 16:58:49 +02003234 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
3235 return;
3236
Jiri Olsa62b915f2010-04-02 19:01:22 +02003237 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
3238 /* print nothing if the buffers are empty */
3239 if (trace_empty(iter))
3240 return;
3241 print_trace_header(m, iter);
3242 if (!(trace_flags & TRACE_ITER_VERBOSE))
3243 print_lat_help_header(m);
3244 } else {
Steven Rostedt77271ce2011-11-17 09:34:33 -05003245 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
3246 if (trace_flags & TRACE_ITER_IRQ_INFO)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003247 print_func_help_header_irq(iter->trace_buffer, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05003248 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003249 print_func_help_header(iter->trace_buffer, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05003250 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02003251 }
3252}
3253
Steven Rostedte0a413f2011-09-29 21:26:16 -04003254static void test_ftrace_alive(struct seq_file *m)
3255{
3256 if (!ftrace_is_dead())
3257 return;
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003258 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
3259 "# MAY BE MISSING FUNCTION EVENTS\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04003260}
3261
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003262#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003263static void show_snapshot_main_help(struct seq_file *m)
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003264{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003265 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
3266 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3267 "# Takes a snapshot of the main buffer.\n"
3268 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
3269 "# (Doesn't have to be '2' works with any number that\n"
3270 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003271}
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003272
3273static void show_snapshot_percpu_help(struct seq_file *m)
3274{
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003275 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003276#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003277 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3278 "# Takes a snapshot of the main buffer for this cpu.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003279#else
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003280 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
3281 "# Must use main snapshot file to allocate.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003282#endif
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003283 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
3284 "# (Doesn't have to be '2' works with any number that\n"
3285 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003286}
3287
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003288static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
3289{
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05003290 if (iter->tr->allocated_snapshot)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003291 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003292 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003293 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003294
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003295 seq_puts(m, "# Snapshot commands:\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05003296 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
3297 show_snapshot_main_help(m);
3298 else
3299 show_snapshot_percpu_help(m);
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003300}
3301#else
3302/* Should never be called */
3303static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
3304#endif
3305
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003306static int s_show(struct seq_file *m, void *v)
3307{
3308 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003309 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003310
3311 if (iter->ent == NULL) {
3312 if (iter->tr) {
3313 seq_printf(m, "# tracer: %s\n", iter->trace->name);
3314 seq_puts(m, "#\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04003315 test_ftrace_alive(m);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003316 }
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05003317 if (iter->snapshot && trace_empty(iter))
3318 print_snapshot_help(m, iter);
3319 else if (iter->trace && iter->trace->print_header)
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003320 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02003321 else
3322 trace_default_header(m);
3323
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003324 } else if (iter->leftover) {
3325 /*
3326 * If we filled the seq_file buffer earlier, we
3327 * want to just show it now.
3328 */
3329 ret = trace_print_seq(m, &iter->seq);
3330
3331 /* ret should this time be zero, but you never know */
3332 iter->leftover = ret;
3333
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003334 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003335 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003336 ret = trace_print_seq(m, &iter->seq);
3337 /*
3338 * If we overflow the seq_file buffer, then it will
3339 * ask us for this data again at start up.
3340 * Use that instead.
3341 * ret is 0 if seq_file write succeeded.
3342 * -1 otherwise.
3343 */
3344 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003345 }
3346
3347 return 0;
3348}
3349
Oleg Nesterov649e9c702013-07-23 17:25:54 +02003350/*
3351 * Should be used after trace_array_get(), trace_types_lock
3352 * ensures that i_cdev was already initialized.
3353 */
3354static inline int tracing_get_cpu(struct inode *inode)
3355{
3356 if (inode->i_cdev) /* See trace_create_cpu_file() */
3357 return (long)inode->i_cdev - 1;
3358 return RING_BUFFER_ALL_CPUS;
3359}
3360
James Morris88e9d342009-09-22 16:43:43 -07003361static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003362 .start = s_start,
3363 .next = s_next,
3364 .stop = s_stop,
3365 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003366};
3367
Ingo Molnare309b412008-05-12 21:20:51 +02003368static struct trace_iterator *
Oleg Nesterov6484c712013-07-23 17:26:10 +02003369__tracing_open(struct inode *inode, struct file *file, bool snapshot)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003370{
Oleg Nesterov6484c712013-07-23 17:26:10 +02003371 struct trace_array *tr = inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003372 struct trace_iterator *iter;
Jiri Olsa50e18b92012-04-25 10:23:39 +02003373 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003374
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003375 if (tracing_disabled)
3376 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02003377
Jiri Olsa50e18b92012-04-25 10:23:39 +02003378 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003379 if (!iter)
3380 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003381
Gil Fruchter72917232015-06-09 10:32:35 +03003382 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
Steven Rostedt6d158a82012-06-27 20:46:14 -04003383 GFP_KERNEL);
Dan Carpenter93574fc2012-07-11 09:35:08 +03003384 if (!iter->buffer_iter)
3385 goto release;
3386
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003387 /*
3388 * We make a copy of the current tracer to avoid concurrent
3389 * changes on it while we are reading.
3390 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003391 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003392 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003393 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003394 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003395
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003396 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003397
Li Zefan79f55992009-06-15 14:58:26 +08003398 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003399 goto fail;
3400
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003401 iter->tr = tr;
3402
3403#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003404 /* Currently only the top directory has a snapshot */
3405 if (tr->current_trace->print_max || snapshot)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003406 iter->trace_buffer = &tr->max_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003407 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003408#endif
3409 iter->trace_buffer = &tr->trace_buffer;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003410 iter->snapshot = snapshot;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003411 iter->pos = -1;
Oleg Nesterov6484c712013-07-23 17:26:10 +02003412 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003413 mutex_init(&iter->mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003414
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003415 /* Notify the tracer early; before we stop tracing. */
3416 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01003417 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01003418
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003419 /* Annotate start of buffers if we had overruns */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003420 if (ring_buffer_overruns(iter->trace_buffer->buffer))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003421 iter->iter_flags |= TRACE_FILE_ANNOTATE;
3422
David Sharp8be07092012-11-13 12:18:22 -08003423 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09003424 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08003425 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3426
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003427 /* stop the trace while dumping if we are not opening "snapshot" */
3428 if (!iter->snapshot)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003429 tracing_stop_tr(tr);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003430
Steven Rostedtae3b5092013-01-23 15:22:59 -05003431 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003432 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003433 iter->buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003434 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
David Miller72c9ddf2010-04-20 15:47:11 -07003435 }
3436 ring_buffer_read_prepare_sync();
3437 for_each_tracing_cpu(cpu) {
3438 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003439 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003440 }
3441 } else {
3442 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003443 iter->buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003444 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
David Miller72c9ddf2010-04-20 15:47:11 -07003445 ring_buffer_read_prepare_sync();
3446 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003447 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003448 }
3449
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003450 mutex_unlock(&trace_types_lock);
3451
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003452 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003453
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003454 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003455 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003456 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04003457 kfree(iter->buffer_iter);
Dan Carpenter93574fc2012-07-11 09:35:08 +03003458release:
Jiri Olsa50e18b92012-04-25 10:23:39 +02003459 seq_release_private(inode, file);
3460 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003461}
3462
3463int tracing_open_generic(struct inode *inode, struct file *filp)
3464{
Steven Rostedt60a11772008-05-12 21:20:44 +02003465 if (tracing_disabled)
3466 return -ENODEV;
3467
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003468 filp->private_data = inode->i_private;
3469 return 0;
3470}
3471
Geyslan G. Bem2e864212013-10-18 21:15:54 -03003472bool tracing_is_disabled(void)
3473{
3474 return (tracing_disabled) ? true: false;
3475}
3476
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003477/*
3478 * Open and update trace_array ref count.
3479 * Must have the current trace_array passed to it.
3480 */
Steven Rostedt (Red Hat)dcc30222013-07-02 20:30:52 -04003481static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003482{
3483 struct trace_array *tr = inode->i_private;
3484
3485 if (tracing_disabled)
3486 return -ENODEV;
3487
3488 if (trace_array_get(tr) < 0)
3489 return -ENODEV;
3490
3491 filp->private_data = inode->i_private;
3492
3493 return 0;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003494}
3495
Hannes Eder4fd27352009-02-10 19:44:12 +01003496static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003497{
Oleg Nesterov6484c712013-07-23 17:26:10 +02003498 struct trace_array *tr = inode->i_private;
matt mooney907f2782010-09-27 19:04:53 -07003499 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003500 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003501 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003502
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003503 if (!(file->f_mode & FMODE_READ)) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02003504 trace_array_put(tr);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003505 return 0;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003506 }
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003507
Oleg Nesterov6484c712013-07-23 17:26:10 +02003508 /* Writes do not use seq_file */
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003509 iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003510 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05003511
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003512 for_each_tracing_cpu(cpu) {
3513 if (iter->buffer_iter[cpu])
3514 ring_buffer_read_finish(iter->buffer_iter[cpu]);
3515 }
3516
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003517 if (iter->trace && iter->trace->close)
3518 iter->trace->close(iter);
3519
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003520 if (!iter->snapshot)
3521 /* reenable tracing if it was previously enabled */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003522 tracing_start_tr(tr);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07003523
3524 __trace_array_put(tr);
3525
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003526 mutex_unlock(&trace_types_lock);
3527
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003528 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003529 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003530 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04003531 kfree(iter->buffer_iter);
Jiri Olsa50e18b92012-04-25 10:23:39 +02003532 seq_release_private(inode, file);
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003533
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003534 return 0;
3535}
3536
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003537static int tracing_release_generic_tr(struct inode *inode, struct file *file)
3538{
3539 struct trace_array *tr = inode->i_private;
3540
3541 trace_array_put(tr);
3542 return 0;
3543}
3544
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04003545static int tracing_single_release_tr(struct inode *inode, struct file *file)
3546{
3547 struct trace_array *tr = inode->i_private;
3548
3549 trace_array_put(tr);
3550
3551 return single_release(inode, file);
3552}
3553
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003554static int tracing_open(struct inode *inode, struct file *file)
3555{
Oleg Nesterov6484c712013-07-23 17:26:10 +02003556 struct trace_array *tr = inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05003557 struct trace_iterator *iter;
3558 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003559
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003560 if (trace_array_get(tr) < 0)
3561 return -ENODEV;
3562
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003563 /* If this file was open for write, then erase contents */
Oleg Nesterov6484c712013-07-23 17:26:10 +02003564 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
3565 int cpu = tracing_get_cpu(inode);
3566
3567 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003568 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003569 else
Oleg Nesterov6484c712013-07-23 17:26:10 +02003570 tracing_reset(&tr->trace_buffer, cpu);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003571 }
3572
3573 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02003574 iter = __tracing_open(inode, file, false);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003575 if (IS_ERR(iter))
3576 ret = PTR_ERR(iter);
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003577 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003578 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3579 }
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04003580
3581 if (ret < 0)
3582 trace_array_put(tr);
3583
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003584 return ret;
3585}
3586
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003587/*
3588 * Some tracers are not suitable for instance buffers.
3589 * A tracer is always available for the global array (toplevel)
3590 * or if it explicitly states that it is.
3591 */
3592static bool
3593trace_ok_for_array(struct tracer *t, struct trace_array *tr)
3594{
3595 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
3596}
3597
3598/* Find the next tracer that this trace array may use */
3599static struct tracer *
3600get_tracer_for_array(struct trace_array *tr, struct tracer *t)
3601{
3602 while (t && !trace_ok_for_array(t, tr))
3603 t = t->next;
3604
3605 return t;
3606}
3607
Ingo Molnare309b412008-05-12 21:20:51 +02003608static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003609t_next(struct seq_file *m, void *v, loff_t *pos)
3610{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003611 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08003612 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003613
3614 (*pos)++;
3615
3616 if (t)
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003617 t = get_tracer_for_array(tr, t->next);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003618
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003619 return t;
3620}
3621
3622static void *t_start(struct seq_file *m, loff_t *pos)
3623{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003624 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08003625 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003626 loff_t l = 0;
3627
3628 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003629
3630 t = get_tracer_for_array(tr, trace_types);
3631 for (; t && l < *pos; t = t_next(m, t, &l))
3632 ;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003633
3634 return t;
3635}
3636
3637static void t_stop(struct seq_file *m, void *p)
3638{
3639 mutex_unlock(&trace_types_lock);
3640}
3641
3642static int t_show(struct seq_file *m, void *v)
3643{
3644 struct tracer *t = v;
3645
3646 if (!t)
3647 return 0;
3648
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003649 seq_puts(m, t->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003650 if (t->next)
3651 seq_putc(m, ' ');
3652 else
3653 seq_putc(m, '\n');
3654
3655 return 0;
3656}
3657
James Morris88e9d342009-09-22 16:43:43 -07003658static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003659 .start = t_start,
3660 .next = t_next,
3661 .stop = t_stop,
3662 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003663};
3664
3665static int show_traces_open(struct inode *inode, struct file *file)
3666{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003667 struct trace_array *tr = inode->i_private;
3668 struct seq_file *m;
3669 int ret;
3670
Steven Rostedt60a11772008-05-12 21:20:44 +02003671 if (tracing_disabled)
3672 return -ENODEV;
3673
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05003674 ret = seq_open(file, &show_traces_seq_ops);
3675 if (ret)
3676 return ret;
3677
3678 m = file->private_data;
3679 m->private = tr;
3680
3681 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003682}
3683
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003684static ssize_t
3685tracing_write_stub(struct file *filp, const char __user *ubuf,
3686 size_t count, loff_t *ppos)
3687{
3688 return count;
3689}
3690
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003691loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
Slava Pestov364829b2010-11-24 15:13:16 -08003692{
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003693 int ret;
3694
Slava Pestov364829b2010-11-24 15:13:16 -08003695 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003696 ret = seq_lseek(file, offset, whence);
Slava Pestov364829b2010-11-24 15:13:16 -08003697 else
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003698 file->f_pos = ret = 0;
3699
3700 return ret;
Slava Pestov364829b2010-11-24 15:13:16 -08003701}
3702
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003703static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003704 .open = tracing_open,
3705 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003706 .write = tracing_write_stub,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003707 .llseek = tracing_lseek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003708 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003709};
3710
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003711static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02003712 .open = show_traces_open,
3713 .read = seq_read,
3714 .release = seq_release,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003715 .llseek = seq_lseek,
Ingo Molnarc7078de2008-05-12 21:20:52 +02003716};
3717
Ingo Molnar36dfe922008-05-12 21:20:52 +02003718/*
Ingo Molnar36dfe922008-05-12 21:20:52 +02003719 * The tracer itself will not take this lock, but still we want
3720 * to provide a consistent cpumask to user-space:
3721 */
3722static DEFINE_MUTEX(tracing_cpumask_update_lock);
3723
3724/*
3725 * Temporary storage for the character representation of the
3726 * CPU bitmask (and one more byte for the newline):
3727 */
3728static char mask_str[NR_CPUS + 1];
3729
Ingo Molnarc7078de2008-05-12 21:20:52 +02003730static ssize_t
3731tracing_cpumask_read(struct file *filp, char __user *ubuf,
3732 size_t count, loff_t *ppos)
3733{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003734 struct trace_array *tr = file_inode(filp)->i_private;
Ingo Molnar36dfe922008-05-12 21:20:52 +02003735 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003736
3737 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003738
Tejun Heo1a402432015-02-13 14:37:39 -08003739 len = snprintf(mask_str, count, "%*pb\n",
3740 cpumask_pr_args(tr->tracing_cpumask));
3741 if (len >= count) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02003742 count = -EINVAL;
3743 goto out_err;
3744 }
Ingo Molnar36dfe922008-05-12 21:20:52 +02003745 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3746
3747out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02003748 mutex_unlock(&tracing_cpumask_update_lock);
3749
3750 return count;
3751}
3752
3753static ssize_t
3754tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3755 size_t count, loff_t *ppos)
3756{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003757 struct trace_array *tr = file_inode(filp)->i_private;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303758 cpumask_var_t tracing_cpumask_new;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003759 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303760
3761 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3762 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003763
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303764 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003765 if (err)
3766 goto err_unlock;
3767
Li Zefan215368e2009-06-15 10:56:42 +08003768 mutex_lock(&tracing_cpumask_update_lock);
3769
Steven Rostedta5e25882008-12-02 15:34:05 -05003770 local_irq_disable();
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05003771 arch_spin_lock(&tr->max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02003772 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02003773 /*
3774 * Increase/decrease the disabled counter if we are
3775 * about to flip a bit in the cpumask:
3776 */
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003777 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303778 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003779 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3780 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003781 }
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003782 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303783 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003784 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3785 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003786 }
3787 }
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05003788 arch_spin_unlock(&tr->max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05003789 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02003790
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003791 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003792
Ingo Molnarc7078de2008-05-12 21:20:52 +02003793 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303794 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02003795
Ingo Molnarc7078de2008-05-12 21:20:52 +02003796 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02003797
3798err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08003799 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02003800
3801 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02003802}
3803
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003804static const struct file_operations tracing_cpumask_fops = {
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003805 .open = tracing_open_generic_tr,
Ingo Molnarc7078de2008-05-12 21:20:52 +02003806 .read = tracing_cpumask_read,
3807 .write = tracing_cpumask_write,
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07003808 .release = tracing_release_generic_tr,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003809 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003810};
3811
Li Zefanfdb372e2009-12-08 11:15:59 +08003812static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003813{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003814 struct tracer_opt *trace_opts;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003815 struct trace_array *tr = m->private;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003816 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003817 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003818
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003819 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003820 tracer_flags = tr->current_trace->flags->val;
3821 trace_opts = tr->current_trace->flags->opts;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003822
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003823 for (i = 0; trace_options[i]; i++) {
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003824 if (tr->trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08003825 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003826 else
Li Zefanfdb372e2009-12-08 11:15:59 +08003827 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003828 }
3829
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003830 for (i = 0; trace_opts[i].name; i++) {
3831 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08003832 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003833 else
Li Zefanfdb372e2009-12-08 11:15:59 +08003834 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003835 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05003836 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003837
Li Zefanfdb372e2009-12-08 11:15:59 +08003838 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003839}
3840
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003841static int __set_tracer_option(struct trace_array *tr,
Li Zefan8d18eaa2009-12-08 11:17:06 +08003842 struct tracer_flags *tracer_flags,
3843 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003844{
Chunyu Hud39cdd22016-03-08 21:37:01 +08003845 struct tracer *trace = tracer_flags->trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08003846 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003847
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003848 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003849 if (ret)
3850 return ret;
3851
3852 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08003853 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003854 else
Zhaolei77708412009-08-07 18:53:21 +08003855 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003856 return 0;
3857}
3858
Li Zefan8d18eaa2009-12-08 11:17:06 +08003859/* Try to assign a tracer specific option */
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003860static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
Li Zefan8d18eaa2009-12-08 11:17:06 +08003861{
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003862 struct tracer *trace = tr->current_trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08003863 struct tracer_flags *tracer_flags = trace->flags;
3864 struct tracer_opt *opts = NULL;
3865 int i;
3866
3867 for (i = 0; tracer_flags->opts[i].name; i++) {
3868 opts = &tracer_flags->opts[i];
3869
3870 if (strcmp(cmp, opts->name) == 0)
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003871 return __set_tracer_option(tr, trace->flags, opts, neg);
Li Zefan8d18eaa2009-12-08 11:17:06 +08003872 }
3873
3874 return -EINVAL;
3875}
3876
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003877/* Some tracers require overwrite to stay enabled */
3878int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3879{
3880 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3881 return -1;
3882
3883 return 0;
3884}
3885
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003886int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003887{
3888 /* do nothing if flag is already set */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003889 if (!!(tr->trace_flags & mask) == !!enabled)
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003890 return 0;
3891
3892 /* Give the tracer a chance to approve the change */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003893 if (tr->current_trace->flag_changed)
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -05003894 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003895 return -EINVAL;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003896
3897 if (enabled)
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003898 tr->trace_flags |= mask;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003899 else
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003900 tr->trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08003901
3902 if (mask == TRACE_ITER_RECORD_CMD)
3903 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08003904
Steven Rostedtc37775d2016-04-13 16:59:18 -04003905 if (mask == TRACE_ITER_EVENT_FORK)
3906 trace_event_follow_fork(tr, enabled);
3907
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003908 if (mask == TRACE_ITER_OVERWRITE) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003909 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003910#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003911 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04003912#endif
3913 }
Steven Rostedt81698832012-10-11 10:15:05 -04003914
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04003915 if (mask == TRACE_ITER_PRINTK) {
Steven Rostedt81698832012-10-11 10:15:05 -04003916 trace_printk_start_stop_comm(enabled);
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04003917 trace_printk_control(enabled);
3918 }
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003919
3920 return 0;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04003921}
3922
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003923static int trace_set_options(struct trace_array *tr, char *option)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003924{
Li Zefan8d18eaa2009-12-08 11:17:06 +08003925 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003926 int neg = 0;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003927 int ret = -ENODEV;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003928 int i;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08003929 size_t orig_len = strlen(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003930
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003931 cmp = strstrip(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003932
Li Zefan8d18eaa2009-12-08 11:17:06 +08003933 if (strncmp(cmp, "no", 2) == 0) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003934 neg = 1;
3935 cmp += 2;
3936 }
3937
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003938 mutex_lock(&trace_types_lock);
3939
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003940 for (i = 0; trace_options[i]; i++) {
Li Zefan8d18eaa2009-12-08 11:17:06 +08003941 if (strcmp(cmp, trace_options[i]) == 0) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003942 ret = set_tracer_flag(tr, 1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003943 break;
3944 }
3945 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01003946
3947 /* If no option could be set, test the specific tracer options */
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003948 if (!trace_options[i])
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05003949 ret = set_tracer_option(tr, cmp, neg);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04003950
3951 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003952
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08003953 /*
3954 * If the first trailing whitespace is replaced with '\0' by strstrip,
3955 * turn it back into a space.
3956 */
3957 if (orig_len > strlen(option))
3958 option[strlen(option)] = ' ';
3959
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003960 return ret;
3961}
3962
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08003963static void __init apply_trace_boot_options(void)
3964{
3965 char *buf = trace_boot_options_buf;
3966 char *option;
3967
3968 while (true) {
3969 option = strsep(&buf, ",");
3970
3971 if (!option)
3972 break;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08003973
Steven Rostedt (Red Hat)43ed3842015-11-03 22:15:14 -05003974 if (*option)
3975 trace_set_options(&global_trace, option);
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08003976
3977 /* Put back the comma to allow this to be called again */
3978 if (buf)
3979 *(buf - 1) = ',';
3980 }
3981}
3982
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003983static ssize_t
3984tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3985 size_t cnt, loff_t *ppos)
3986{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003987 struct seq_file *m = filp->private_data;
3988 struct trace_array *tr = m->private;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003989 char buf[64];
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04003990 int ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003991
3992 if (cnt >= sizeof(buf))
3993 return -EINVAL;
3994
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08003995 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04003996 return -EFAULT;
3997
Steven Rostedta8dd2172013-01-09 20:54:17 -05003998 buf[cnt] = 0;
3999
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004000 ret = trace_set_options(tr, buf);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004001 if (ret < 0)
4002 return ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004003
Jiri Olsacf8517c2009-10-23 19:36:16 -04004004 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004005
4006 return cnt;
4007}
4008
Li Zefanfdb372e2009-12-08 11:15:59 +08004009static int tracing_trace_options_open(struct inode *inode, struct file *file)
4010{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004011 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004012 int ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004013
Li Zefanfdb372e2009-12-08 11:15:59 +08004014 if (tracing_disabled)
4015 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004016
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004017 if (trace_array_get(tr) < 0)
4018 return -ENODEV;
4019
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004020 ret = single_open(file, tracing_trace_options_show, inode->i_private);
4021 if (ret < 0)
4022 trace_array_put(tr);
4023
4024 return ret;
Li Zefanfdb372e2009-12-08 11:15:59 +08004025}
4026
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004027static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08004028 .open = tracing_trace_options_open,
4029 .read = seq_read,
4030 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004031 .release = tracing_single_release_tr,
Steven Rostedtee6bce52008-11-12 17:52:37 -05004032 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004033};
4034
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004035static const char readme_msg[] =
4036 "tracing mini-HOWTO:\n\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004037 "# echo 0 > tracing_on : quick way to disable tracing\n"
4038 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
4039 " Important files:\n"
4040 " trace\t\t\t- The static contents of the buffer\n"
4041 "\t\t\t To clear the buffer write into this file: echo > trace\n"
4042 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
4043 " current_tracer\t- function and latency tracers\n"
4044 " available_tracers\t- list of configured tracers for current_tracer\n"
4045 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
4046 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
4047 " trace_clock\t\t-change the clock used to order events\n"
4048 " local: Per cpu clock but may not be synced across CPUs\n"
4049 " global: Synced across CPUs but slows tracing down.\n"
4050 " counter: Not a clock, but just an increment\n"
4051 " uptime: Jiffy counter from time of boot\n"
4052 " perf: Same clock that perf events use\n"
4053#ifdef CONFIG_X86_64
4054 " x86-tsc: TSC cycle counter\n"
4055#endif
4056 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
4057 " tracing_cpumask\t- Limit which CPUs to trace\n"
4058 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
4059 "\t\t\t Remove sub-buffer with rmdir\n"
4060 " trace_options\t\t- Set format or modify how tracing happens\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004061 "\t\t\t Disable an option by adding a suffix 'no' to the\n"
4062 "\t\t\t option name\n"
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004063 " 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 -04004064#ifdef CONFIG_DYNAMIC_FTRACE
4065 "\n available_filter_functions - list of functions that can be filtered on\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004066 " set_ftrace_filter\t- echo function name in here to only trace these\n"
4067 "\t\t\t functions\n"
4068 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
4069 "\t modules: Can select a group via module\n"
4070 "\t Format: :mod:<module-name>\n"
4071 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
4072 "\t triggers: a command to perform when function is hit\n"
4073 "\t Format: <function>:<trigger>[:count]\n"
4074 "\t trigger: traceon, traceoff\n"
4075 "\t\t enable_event:<system>:<event>\n"
4076 "\t\t disable_event:<system>:<event>\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004077#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004078 "\t\t stacktrace\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004079#endif
4080#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004081 "\t\t snapshot\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004082#endif
Steven Rostedt (Red Hat)17a280e2014-04-10 22:43:37 -04004083 "\t\t dump\n"
4084 "\t\t cpudump\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004085 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
4086 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
4087 "\t The first one will disable tracing every time do_fault is hit\n"
4088 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
4089 "\t The first time do trap is hit and it disables tracing, the\n"
4090 "\t counter will decrement to 2. If tracing is already disabled,\n"
4091 "\t the counter will not decrement. It only decrements when the\n"
4092 "\t trigger did work\n"
4093 "\t To remove trigger without count:\n"
4094 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
4095 "\t To remove trigger with a count:\n"
4096 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004097 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004098 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
4099 "\t modules: Can select a group via module command :mod:\n"
4100 "\t Does not accept triggers\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004101#endif /* CONFIG_DYNAMIC_FTRACE */
4102#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004103 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
4104 "\t\t (function)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004105#endif
4106#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4107 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
Namhyung Kimd048a8c72014-06-13 01:23:53 +09004108 " 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 -04004109 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
4110#endif
4111#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004112 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
4113 "\t\t\t snapshot buffer. Read the contents for more\n"
4114 "\t\t\t information\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004115#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004116#ifdef CONFIG_STACK_TRACER
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004117 " stack_trace\t\t- Shows the max stack trace when active\n"
4118 " stack_max_size\t- Shows current max stack size that was traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004119 "\t\t\t Write into this file to reset the max size (trigger a\n"
4120 "\t\t\t new trace)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004121#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004122 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
4123 "\t\t\t traces\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004124#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004125#endif /* CONFIG_STACK_TRACER */
Tom Zanussi26f25562014-01-17 15:11:44 -06004126 " events/\t\t- Directory containing all trace event subsystems:\n"
4127 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
4128 " events/<system>/\t- Directory containing all trace events for <system>:\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004129 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
4130 "\t\t\t events\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004131 " filter\t\t- If set, only events passing filter are traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004132 " events/<system>/<event>/\t- Directory containing control files for\n"
4133 "\t\t\t <event>:\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004134 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
4135 " filter\t\t- If set, only events passing filter are traced\n"
4136 " trigger\t\t- If set, a command to perform when event is hit\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004137 "\t Format: <trigger>[:count][if <filter>]\n"
4138 "\t trigger: traceon, traceoff\n"
4139 "\t enable_event:<system>:<event>\n"
4140 "\t disable_event:<system>:<event>\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06004141#ifdef CONFIG_HIST_TRIGGERS
4142 "\t enable_hist:<system>:<event>\n"
4143 "\t disable_hist:<system>:<event>\n"
4144#endif
Tom Zanussi26f25562014-01-17 15:11:44 -06004145#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004146 "\t\t stacktrace\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004147#endif
4148#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004149 "\t\t snapshot\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06004150#endif
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004151#ifdef CONFIG_HIST_TRIGGERS
4152 "\t\t hist (see below)\n"
4153#endif
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004154 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
4155 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
4156 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
4157 "\t events/block/block_unplug/trigger\n"
4158 "\t The first disables tracing every time block_unplug is hit.\n"
4159 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
4160 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
4161 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
4162 "\t Like function triggers, the counter is only decremented if it\n"
4163 "\t enabled or disabled tracing.\n"
4164 "\t To remove a trigger without a count:\n"
4165 "\t echo '!<trigger> > <system>/<event>/trigger\n"
4166 "\t To remove a trigger with a count:\n"
4167 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
4168 "\t Filters can be ignored when removing a trigger.\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004169#ifdef CONFIG_HIST_TRIGGERS
4170 " hist trigger\t- If set, event hits are aggregated into a hash table\n"
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004171 "\t Format: hist:keys=<field1[,field2,...]>\n"
Tom Zanussif2606832016-03-03 12:54:43 -06004172 "\t [:values=<field1[,field2,...]>]\n"
Tom Zanussie62347d2016-03-03 12:54:45 -06004173 "\t [:sort=<field1[,field2,...]>]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004174 "\t [:size=#entries]\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06004175 "\t [:pause][:continue][:clear]\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004176 "\t [:name=histname1]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004177 "\t [if <filter>]\n\n"
4178 "\t When a matching event is hit, an entry is added to a hash\n"
Tom Zanussif2606832016-03-03 12:54:43 -06004179 "\t table using the key(s) and value(s) named, and the value of a\n"
4180 "\t sum called 'hitcount' is incremented. Keys and values\n"
4181 "\t correspond to fields in the event's format description. Keys\n"
Tom Zanussi69a02002016-03-03 12:54:52 -06004182 "\t can be any field, or the special string 'stacktrace'.\n"
4183 "\t Compound keys consisting of up to two fields can be specified\n"
4184 "\t by the 'keys' keyword. Values must correspond to numeric\n"
4185 "\t fields. Sort keys consisting of up to two fields can be\n"
4186 "\t specified using the 'sort' keyword. The sort direction can\n"
4187 "\t be modified by appending '.descending' or '.ascending' to a\n"
4188 "\t sort field. The 'size' parameter can be used to specify more\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004189 "\t or fewer than the default 2048 entries for the hashtable size.\n"
4190 "\t If a hist trigger is given a name using the 'name' parameter,\n"
4191 "\t its histogram data will be shared with other triggers of the\n"
4192 "\t same name, and trigger hits will update this common data.\n\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004193 "\t Reading the 'hist' file for the event will dump the hash\n"
Tom Zanussi52a7f162016-03-03 12:54:57 -06004194 "\t table in its entirety to stdout. If there are multiple hist\n"
4195 "\t triggers attached to an event, there will be a table for each\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004196 "\t trigger in the output. The table displayed for a named\n"
4197 "\t trigger will be the same as any other instance having the\n"
4198 "\t same name. The default format used to display a given field\n"
4199 "\t can be modified by appending any of the following modifiers\n"
4200 "\t to the field name, as applicable:\n\n"
Tom Zanussic6afad42016-03-03 12:54:49 -06004201 "\t .hex display a number as a hex value\n"
4202 "\t .sym display an address as a symbol\n"
Tom Zanussi6b4827a2016-03-03 12:54:50 -06004203 "\t .sym-offset display an address as a symbol and offset\n"
Tom Zanussi31696192016-03-03 12:54:51 -06004204 "\t .execname display a common_pid as a program name\n"
4205 "\t .syscall display a syscall id as a syscall name\n\n"
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06004206 "\t .log2 display log2 value rather than raw number\n\n"
Tom Zanussi83e99912016-03-03 12:54:46 -06004207 "\t The 'pause' parameter can be used to pause an existing hist\n"
4208 "\t trigger or to start a hist trigger but not log any events\n"
4209 "\t until told to do so. 'continue' can be used to start or\n"
4210 "\t restart a paused hist trigger.\n\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06004211 "\t The 'clear' parameter will clear the contents of a running\n"
4212 "\t hist trigger and leave its current paused/active state\n"
4213 "\t unchanged.\n\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06004214 "\t The enable_hist and disable_hist triggers can be used to\n"
4215 "\t have one event conditionally start and stop another event's\n"
4216 "\t already-attached hist trigger. The syntax is analagous to\n"
4217 "\t the enable_event and disable_event triggers.\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004218#endif
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004219;
4220
4221static ssize_t
4222tracing_readme_read(struct file *filp, char __user *ubuf,
4223 size_t cnt, loff_t *ppos)
4224{
4225 return simple_read_from_buffer(ubuf, cnt, ppos,
4226 readme_msg, strlen(readme_msg));
4227}
4228
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004229static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02004230 .open = tracing_open_generic,
4231 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004232 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004233};
4234
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004235static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
Avadh Patel69abe6a2009-04-10 16:04:48 -04004236{
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004237 unsigned int *ptr = v;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004238
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004239 if (*pos || m->count)
4240 ptr++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004241
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004242 (*pos)++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004243
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004244 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
4245 ptr++) {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004246 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
Avadh Patel69abe6a2009-04-10 16:04:48 -04004247 continue;
4248
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004249 return ptr;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004250 }
4251
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004252 return NULL;
4253}
Avadh Patel69abe6a2009-04-10 16:04:48 -04004254
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004255static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
4256{
4257 void *v;
4258 loff_t l = 0;
Avadh Patel69abe6a2009-04-10 16:04:48 -04004259
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04004260 preempt_disable();
4261 arch_spin_lock(&trace_cmdline_lock);
4262
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004263 v = &savedcmd->map_cmdline_to_pid[0];
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004264 while (l <= *pos) {
4265 v = saved_cmdlines_next(m, v, &l);
4266 if (!v)
4267 return NULL;
4268 }
4269
4270 return v;
4271}
4272
4273static void saved_cmdlines_stop(struct seq_file *m, void *v)
4274{
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04004275 arch_spin_unlock(&trace_cmdline_lock);
4276 preempt_enable();
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004277}
4278
4279static int saved_cmdlines_show(struct seq_file *m, void *v)
4280{
4281 char buf[TASK_COMM_LEN];
4282 unsigned int *pid = v;
4283
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04004284 __trace_find_cmdline(*pid, buf);
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004285 seq_printf(m, "%d %s\n", *pid, buf);
4286 return 0;
4287}
4288
4289static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
4290 .start = saved_cmdlines_start,
4291 .next = saved_cmdlines_next,
4292 .stop = saved_cmdlines_stop,
4293 .show = saved_cmdlines_show,
4294};
4295
4296static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
4297{
4298 if (tracing_disabled)
4299 return -ENODEV;
4300
4301 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
Avadh Patel69abe6a2009-04-10 16:04:48 -04004302}
4303
4304static const struct file_operations tracing_saved_cmdlines_fops = {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09004305 .open = tracing_saved_cmdlines_open,
4306 .read = seq_read,
4307 .llseek = seq_lseek,
4308 .release = seq_release,
Avadh Patel69abe6a2009-04-10 16:04:48 -04004309};
4310
4311static ssize_t
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004312tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
4313 size_t cnt, loff_t *ppos)
4314{
4315 char buf[64];
4316 int r;
4317
4318 arch_spin_lock(&trace_cmdline_lock);
Namhyung Kima6af8fb2014-06-10 16:11:35 +09004319 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004320 arch_spin_unlock(&trace_cmdline_lock);
4321
4322 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4323}
4324
4325static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
4326{
4327 kfree(s->saved_cmdlines);
4328 kfree(s->map_cmdline_to_pid);
4329 kfree(s);
4330}
4331
4332static int tracing_resize_saved_cmdlines(unsigned int val)
4333{
4334 struct saved_cmdlines_buffer *s, *savedcmd_temp;
4335
Namhyung Kima6af8fb2014-06-10 16:11:35 +09004336 s = kmalloc(sizeof(*s), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004337 if (!s)
4338 return -ENOMEM;
4339
4340 if (allocate_cmdlines_buffer(val, s) < 0) {
4341 kfree(s);
4342 return -ENOMEM;
4343 }
4344
4345 arch_spin_lock(&trace_cmdline_lock);
4346 savedcmd_temp = savedcmd;
4347 savedcmd = s;
4348 arch_spin_unlock(&trace_cmdline_lock);
4349 free_saved_cmdlines_buffer(savedcmd_temp);
4350
4351 return 0;
4352}
4353
4354static ssize_t
4355tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
4356 size_t cnt, loff_t *ppos)
4357{
4358 unsigned long val;
4359 int ret;
4360
4361 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4362 if (ret)
4363 return ret;
4364
4365 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
4366 if (!val || val > PID_MAX_DEFAULT)
4367 return -EINVAL;
4368
4369 ret = tracing_resize_saved_cmdlines((unsigned int)val);
4370 if (ret < 0)
4371 return ret;
4372
4373 *ppos += cnt;
4374
4375 return cnt;
4376}
4377
4378static const struct file_operations tracing_saved_cmdlines_size_fops = {
4379 .open = tracing_open_generic,
4380 .read = tracing_saved_cmdlines_size_read,
4381 .write = tracing_saved_cmdlines_size_write,
4382};
4383
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004384#ifdef CONFIG_TRACE_ENUM_MAP_FILE
4385static union trace_enum_map_item *
4386update_enum_map(union trace_enum_map_item *ptr)
4387{
4388 if (!ptr->map.enum_string) {
4389 if (ptr->tail.next) {
4390 ptr = ptr->tail.next;
4391 /* Set ptr to the next real item (skip head) */
4392 ptr++;
4393 } else
4394 return NULL;
4395 }
4396 return ptr;
4397}
4398
4399static void *enum_map_next(struct seq_file *m, void *v, loff_t *pos)
4400{
4401 union trace_enum_map_item *ptr = v;
4402
4403 /*
4404 * Paranoid! If ptr points to end, we don't want to increment past it.
4405 * This really should never happen.
4406 */
4407 ptr = update_enum_map(ptr);
4408 if (WARN_ON_ONCE(!ptr))
4409 return NULL;
4410
4411 ptr++;
4412
4413 (*pos)++;
4414
4415 ptr = update_enum_map(ptr);
4416
4417 return ptr;
4418}
4419
4420static void *enum_map_start(struct seq_file *m, loff_t *pos)
4421{
4422 union trace_enum_map_item *v;
4423 loff_t l = 0;
4424
4425 mutex_lock(&trace_enum_mutex);
4426
4427 v = trace_enum_maps;
4428 if (v)
4429 v++;
4430
4431 while (v && l < *pos) {
4432 v = enum_map_next(m, v, &l);
4433 }
4434
4435 return v;
4436}
4437
4438static void enum_map_stop(struct seq_file *m, void *v)
4439{
4440 mutex_unlock(&trace_enum_mutex);
4441}
4442
4443static int enum_map_show(struct seq_file *m, void *v)
4444{
4445 union trace_enum_map_item *ptr = v;
4446
4447 seq_printf(m, "%s %ld (%s)\n",
4448 ptr->map.enum_string, ptr->map.enum_value,
4449 ptr->map.system);
4450
4451 return 0;
4452}
4453
4454static const struct seq_operations tracing_enum_map_seq_ops = {
4455 .start = enum_map_start,
4456 .next = enum_map_next,
4457 .stop = enum_map_stop,
4458 .show = enum_map_show,
4459};
4460
4461static int tracing_enum_map_open(struct inode *inode, struct file *filp)
4462{
4463 if (tracing_disabled)
4464 return -ENODEV;
4465
4466 return seq_open(filp, &tracing_enum_map_seq_ops);
4467}
4468
4469static const struct file_operations tracing_enum_map_fops = {
4470 .open = tracing_enum_map_open,
4471 .read = seq_read,
4472 .llseek = seq_lseek,
4473 .release = seq_release,
4474};
4475
4476static inline union trace_enum_map_item *
4477trace_enum_jmp_to_tail(union trace_enum_map_item *ptr)
4478{
4479 /* Return tail of array given the head */
4480 return ptr + ptr->head.length + 1;
4481}
4482
4483static void
4484trace_insert_enum_map_file(struct module *mod, struct trace_enum_map **start,
4485 int len)
4486{
4487 struct trace_enum_map **stop;
4488 struct trace_enum_map **map;
4489 union trace_enum_map_item *map_array;
4490 union trace_enum_map_item *ptr;
4491
4492 stop = start + len;
4493
4494 /*
4495 * The trace_enum_maps contains the map plus a head and tail item,
4496 * where the head holds the module and length of array, and the
4497 * tail holds a pointer to the next list.
4498 */
4499 map_array = kmalloc(sizeof(*map_array) * (len + 2), GFP_KERNEL);
4500 if (!map_array) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07004501 pr_warn("Unable to allocate trace enum mapping\n");
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004502 return;
4503 }
4504
4505 mutex_lock(&trace_enum_mutex);
4506
4507 if (!trace_enum_maps)
4508 trace_enum_maps = map_array;
4509 else {
4510 ptr = trace_enum_maps;
4511 for (;;) {
4512 ptr = trace_enum_jmp_to_tail(ptr);
4513 if (!ptr->tail.next)
4514 break;
4515 ptr = ptr->tail.next;
4516
4517 }
4518 ptr->tail.next = map_array;
4519 }
4520 map_array->head.mod = mod;
4521 map_array->head.length = len;
4522 map_array++;
4523
4524 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
4525 map_array->map = **map;
4526 map_array++;
4527 }
4528 memset(map_array, 0, sizeof(*map_array));
4529
4530 mutex_unlock(&trace_enum_mutex);
4531}
4532
4533static void trace_create_enum_file(struct dentry *d_tracer)
4534{
4535 trace_create_file("enum_map", 0444, d_tracer,
4536 NULL, &tracing_enum_map_fops);
4537}
4538
4539#else /* CONFIG_TRACE_ENUM_MAP_FILE */
4540static inline void trace_create_enum_file(struct dentry *d_tracer) { }
4541static inline void trace_insert_enum_map_file(struct module *mod,
4542 struct trace_enum_map **start, int len) { }
4543#endif /* !CONFIG_TRACE_ENUM_MAP_FILE */
4544
4545static void trace_insert_enum_map(struct module *mod,
4546 struct trace_enum_map **start, int len)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04004547{
4548 struct trace_enum_map **map;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04004549
4550 if (len <= 0)
4551 return;
4552
4553 map = start;
4554
4555 trace_event_enum_update(map, len);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04004556
4557 trace_insert_enum_map_file(mod, start, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04004558}
4559
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004560static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004561tracing_set_trace_read(struct file *filp, char __user *ubuf,
4562 size_t cnt, loff_t *ppos)
4563{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004564 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08004565 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004566 int r;
4567
4568 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004569 r = sprintf(buf, "%s\n", tr->current_trace->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004570 mutex_unlock(&trace_types_lock);
4571
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004572 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004573}
4574
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02004575int tracer_init(struct tracer *t, struct trace_array *tr)
4576{
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004577 tracing_reset_online_cpus(&tr->trace_buffer);
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02004578 return t->init(tr);
4579}
4580
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004581static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004582{
4583 int cpu;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05004584
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004585 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004586 per_cpu_ptr(buf->data, cpu)->entries = val;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004587}
4588
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004589#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookad60da502012-10-17 11:56:16 +09004590/* resize @tr's buffer to the size of @size_tr's entries */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004591static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
4592 struct trace_buffer *size_buf, int cpu_id)
Hiraku Toyookad60da502012-10-17 11:56:16 +09004593{
4594 int cpu, ret = 0;
4595
4596 if (cpu_id == RING_BUFFER_ALL_CPUS) {
4597 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004598 ret = ring_buffer_resize(trace_buf->buffer,
4599 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
Hiraku Toyookad60da502012-10-17 11:56:16 +09004600 if (ret < 0)
4601 break;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004602 per_cpu_ptr(trace_buf->data, cpu)->entries =
4603 per_cpu_ptr(size_buf->data, cpu)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09004604 }
4605 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004606 ret = ring_buffer_resize(trace_buf->buffer,
4607 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
Hiraku Toyookad60da502012-10-17 11:56:16 +09004608 if (ret == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004609 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
4610 per_cpu_ptr(size_buf->data, cpu_id)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09004611 }
4612
4613 return ret;
4614}
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004615#endif /* CONFIG_TRACER_MAX_TRACE */
Hiraku Toyookad60da502012-10-17 11:56:16 +09004616
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004617static int __tracing_resize_ring_buffer(struct trace_array *tr,
4618 unsigned long size, int cpu)
Steven Rostedt73c51622009-03-11 13:42:01 -04004619{
4620 int ret;
4621
4622 /*
4623 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04004624 * we use the size that was given, and we can forget about
4625 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04004626 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05004627 ring_buffer_expanded = true;
Steven Rostedt73c51622009-03-11 13:42:01 -04004628
Steven Rostedtb382ede62012-10-10 21:44:34 -04004629 /* May be called before buffers are initialized */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004630 if (!tr->trace_buffer.buffer)
Steven Rostedtb382ede62012-10-10 21:44:34 -04004631 return 0;
4632
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004633 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04004634 if (ret < 0)
4635 return ret;
4636
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004637#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004638 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
4639 !tr->current_trace->use_max_tr)
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004640 goto out;
4641
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004642 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04004643 if (ret < 0) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004644 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
4645 &tr->trace_buffer, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04004646 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04004647 /*
4648 * AARGH! We are left with different
4649 * size max buffer!!!!
4650 * The max buffer is our "snapshot" buffer.
4651 * When a tracer needs a snapshot (one of the
4652 * latency tracers), it swaps the max buffer
4653 * with the saved snap shot. We succeeded to
4654 * update the size of the main buffer, but failed to
4655 * update the size of the max buffer. But when we tried
4656 * to reset the main buffer to the original size, we
4657 * failed there too. This is very unlikely to
4658 * happen, but if it does, warn and kill all
4659 * tracing.
4660 */
Steven Rostedt73c51622009-03-11 13:42:01 -04004661 WARN_ON(1);
4662 tracing_disabled = 1;
4663 }
4664 return ret;
4665 }
4666
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004667 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004668 set_buffer_entries(&tr->max_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004669 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004670 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004671
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004672 out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004673#endif /* CONFIG_TRACER_MAX_TRACE */
4674
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004675 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004676 set_buffer_entries(&tr->trace_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004677 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004678 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
Steven Rostedt73c51622009-03-11 13:42:01 -04004679
4680 return ret;
4681}
4682
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004683static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
4684 unsigned long size, int cpu_id)
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004685{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004686 int ret = size;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004687
4688 mutex_lock(&trace_types_lock);
4689
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004690 if (cpu_id != RING_BUFFER_ALL_CPUS) {
4691 /* make sure, this cpu is enabled in the mask */
4692 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
4693 ret = -EINVAL;
4694 goto out;
4695 }
4696 }
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004697
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004698 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004699 if (ret < 0)
4700 ret = -ENOMEM;
4701
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004702out:
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004703 mutex_unlock(&trace_types_lock);
4704
4705 return ret;
4706}
4707
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004708
Steven Rostedt1852fcc2009-03-11 14:33:00 -04004709/**
4710 * tracing_update_buffers - used by tracing facility to expand ring buffers
4711 *
4712 * To save on memory when the tracing is never used on a system with it
4713 * configured in. The ring buffers are set to a minimum size. But once
4714 * a user starts to use the tracing facility, then they need to grow
4715 * to their default size.
4716 *
4717 * This function is to be called when a tracer is about to be used.
4718 */
4719int tracing_update_buffers(void)
4720{
4721 int ret = 0;
4722
Steven Rostedt1027fcb2009-03-12 11:33:20 -04004723 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04004724 if (!ring_buffer_expanded)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004725 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004726 RING_BUFFER_ALL_CPUS);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04004727 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04004728
4729 return ret;
4730}
4731
Steven Rostedt577b7852009-02-26 23:43:05 -05004732struct trace_option_dentry;
4733
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04004734static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004735create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
Steven Rostedt577b7852009-02-26 23:43:05 -05004736
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05004737/*
4738 * Used to clear out the tracer before deletion of an instance.
4739 * Must have trace_types_lock held.
4740 */
4741static void tracing_set_nop(struct trace_array *tr)
4742{
4743 if (tr->current_trace == &nop_trace)
4744 return;
4745
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05004746 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05004747
4748 if (tr->current_trace->reset)
4749 tr->current_trace->reset(tr);
4750
4751 tr->current_trace = &nop_trace;
4752}
4753
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04004754static void add_tracer_options(struct trace_array *tr, struct tracer *t)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004755{
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05004756 /* Only enable if the directory has been created already. */
4757 if (!tr->dir)
4758 return;
4759
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04004760 create_trace_option_files(tr, t);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05004761}
4762
4763static int tracing_set_tracer(struct trace_array *tr, const char *buf)
4764{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004765 struct tracer *t;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004766#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05004767 bool had_max_tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004768#endif
Peter Zijlstrad9e54072008-11-01 19:57:37 +01004769 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004770
Steven Rostedt1027fcb2009-03-12 11:33:20 -04004771 mutex_lock(&trace_types_lock);
4772
Steven Rostedt73c51622009-03-11 13:42:01 -04004773 if (!ring_buffer_expanded) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004774 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004775 RING_BUFFER_ALL_CPUS);
Steven Rostedt73c51622009-03-11 13:42:01 -04004776 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01004777 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04004778 ret = 0;
4779 }
4780
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004781 for (t = trace_types; t; t = t->next) {
4782 if (strcmp(t->name, buf) == 0)
4783 break;
4784 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02004785 if (!t) {
4786 ret = -EINVAL;
4787 goto out;
4788 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004789 if (t == tr->current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004790 goto out;
4791
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004792 /* Some tracers are only allowed for the top level buffer */
4793 if (!trace_ok_for_array(t, tr)) {
4794 ret = -EINVAL;
4795 goto out;
4796 }
4797
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05004798 /* If trace pipe files are being read, we can't change the tracer */
4799 if (tr->current_trace->ref) {
4800 ret = -EBUSY;
4801 goto out;
4802 }
4803
Steven Rostedt9f029e82008-11-12 15:24:24 -05004804 trace_branch_disable();
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004805
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05004806 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004807
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004808 if (tr->current_trace->reset)
4809 tr->current_trace->reset(tr);
Steven Rostedt34600f02013-01-22 13:35:11 -05004810
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004811 /* Current trace needs to be nop_trace before synchronize_sched */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004812 tr->current_trace = &nop_trace;
Steven Rostedt34600f02013-01-22 13:35:11 -05004813
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05004814#ifdef CONFIG_TRACER_MAX_TRACE
4815 had_max_tr = tr->allocated_snapshot;
Steven Rostedt34600f02013-01-22 13:35:11 -05004816
4817 if (had_max_tr && !t->use_max_tr) {
4818 /*
4819 * We need to make sure that the update_max_tr sees that
4820 * current_trace changed to nop_trace to keep it from
4821 * swapping the buffers after we resize it.
4822 * The update_max_tr is called from interrupts disabled
4823 * so a synchronized_sched() is sufficient.
4824 */
4825 synchronize_sched();
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04004826 free_snapshot(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004827 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004828#endif
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004829
4830#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05004831 if (t->use_max_tr && !had_max_tr) {
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04004832 ret = alloc_snapshot(tr);
Hiraku Toyookad60da502012-10-17 11:56:16 +09004833 if (ret < 0)
4834 goto out;
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004835 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004836#endif
Steven Rostedt577b7852009-02-26 23:43:05 -05004837
Frederic Weisbecker1c800252008-11-16 05:57:26 +01004838 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02004839 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01004840 if (ret)
4841 goto out;
4842 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004843
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004844 tr->current_trace = t;
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05004845 tr->current_trace->enabled++;
Steven Rostedt9f029e82008-11-12 15:24:24 -05004846 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004847 out:
4848 mutex_unlock(&trace_types_lock);
4849
Peter Zijlstrad9e54072008-11-01 19:57:37 +01004850 return ret;
4851}
4852
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004853static ssize_t
4854tracing_set_trace_write(struct file *filp, const char __user *ubuf,
4855 size_t cnt, loff_t *ppos)
4856{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004857 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08004858 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004859 int i;
4860 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01004861 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004862
Steven Rostedt60063a62008-10-28 10:44:24 -04004863 ret = cnt;
4864
Li Zefanee6c2c12009-09-18 14:06:47 +08004865 if (cnt > MAX_TRACER_SIZE)
4866 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004867
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08004868 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004869 return -EFAULT;
4870
4871 buf[cnt] = 0;
4872
4873 /* strip ending whitespace. */
4874 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
4875 buf[i] = 0;
4876
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004877 err = tracing_set_tracer(tr, buf);
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01004878 if (err)
4879 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004880
Jiri Olsacf8517c2009-10-23 19:36:16 -04004881 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004882
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02004883 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004884}
4885
4886static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04004887tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
4888 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004889{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004890 char buf[64];
4891 int r;
4892
Steven Rostedtcffae432008-05-12 21:21:00 +02004893 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004894 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02004895 if (r > sizeof(buf))
4896 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004897 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004898}
4899
4900static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04004901tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
4902 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004903{
Hannes Eder5e398412009-02-10 19:44:34 +01004904 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02004905 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004906
Peter Huewe22fe9b52011-06-07 21:58:27 +02004907 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4908 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02004909 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004910
4911 *ptr = val * 1000;
4912
4913 return cnt;
4914}
4915
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04004916static ssize_t
4917tracing_thresh_read(struct file *filp, char __user *ubuf,
4918 size_t cnt, loff_t *ppos)
4919{
4920 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
4921}
4922
4923static ssize_t
4924tracing_thresh_write(struct file *filp, const char __user *ubuf,
4925 size_t cnt, loff_t *ppos)
4926{
4927 struct trace_array *tr = filp->private_data;
4928 int ret;
4929
4930 mutex_lock(&trace_types_lock);
4931 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
4932 if (ret < 0)
4933 goto out;
4934
4935 if (tr->current_trace->update_thresh) {
4936 ret = tr->current_trace->update_thresh(tr);
4937 if (ret < 0)
4938 goto out;
4939 }
4940
4941 ret = cnt;
4942out:
4943 mutex_unlock(&trace_types_lock);
4944
4945 return ret;
4946}
4947
Chen Gange428abb2015-11-10 05:15:15 +08004948#ifdef CONFIG_TRACER_MAX_TRACE
4949
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04004950static ssize_t
4951tracing_max_lat_read(struct file *filp, char __user *ubuf,
4952 size_t cnt, loff_t *ppos)
4953{
4954 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos);
4955}
4956
4957static ssize_t
4958tracing_max_lat_write(struct file *filp, const char __user *ubuf,
4959 size_t cnt, loff_t *ppos)
4960{
4961 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos);
4962}
4963
Chen Gange428abb2015-11-10 05:15:15 +08004964#endif
4965
Steven Rostedtb3806b42008-05-12 21:20:46 +02004966static int tracing_open_pipe(struct inode *inode, struct file *filp)
4967{
Oleg Nesterov15544202013-07-23 17:25:57 +02004968 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004969 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004970 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02004971
4972 if (tracing_disabled)
4973 return -ENODEV;
4974
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004975 if (trace_array_get(tr) < 0)
4976 return -ENODEV;
4977
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004978 mutex_lock(&trace_types_lock);
4979
Steven Rostedtb3806b42008-05-12 21:20:46 +02004980 /* create a buffer to store the information to pass to userspace */
4981 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004982 if (!iter) {
4983 ret = -ENOMEM;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004984 __trace_array_put(tr);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004985 goto out;
4986 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02004987
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04004988 trace_seq_init(&iter->seq);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05004989 iter->trace = tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004990
4991 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
4992 ret = -ENOMEM;
4993 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10304994 }
4995
Steven Rostedta3097202008-11-07 22:36:02 -05004996 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10304997 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05004998
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004999 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt112f38a72009-06-01 15:16:05 -04005000 iter->iter_flags |= TRACE_FILE_LAT_FMT;
5001
David Sharp8be07092012-11-13 12:18:22 -08005002 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09005003 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08005004 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
5005
Oleg Nesterov15544202013-07-23 17:25:57 +02005006 iter->tr = tr;
5007 iter->trace_buffer = &tr->trace_buffer;
5008 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005009 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005010 filp->private_data = iter;
5011
Steven Rostedt107bad82008-05-12 21:21:01 +02005012 if (iter->trace->pipe_open)
5013 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02005014
Arnd Bergmannb4447862010-07-07 23:40:11 +02005015 nonseekable_open(inode, filp);
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005016
5017 tr->current_trace->ref++;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005018out:
5019 mutex_unlock(&trace_types_lock);
5020 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005021
5022fail:
5023 kfree(iter->trace);
5024 kfree(iter);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005025 __trace_array_put(tr);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005026 mutex_unlock(&trace_types_lock);
5027 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005028}
5029
5030static int tracing_release_pipe(struct inode *inode, struct file *file)
5031{
5032 struct trace_iterator *iter = file->private_data;
Oleg Nesterov15544202013-07-23 17:25:57 +02005033 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005034
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005035 mutex_lock(&trace_types_lock);
5036
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005037 tr->current_trace->ref--;
5038
Steven Rostedt29bf4a52009-12-09 12:37:43 -05005039 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05005040 iter->trace->pipe_close(iter);
5041
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005042 mutex_unlock(&trace_types_lock);
5043
Rusty Russell44623442009-01-01 10:12:23 +10305044 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005045 mutex_destroy(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005046 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005047
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005048 trace_array_put(tr);
5049
Steven Rostedtb3806b42008-05-12 21:20:46 +02005050 return 0;
5051}
5052
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005053static unsigned int
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005054trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005055{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005056 struct trace_array *tr = iter->tr;
5057
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005058 /* Iterators are static, they should be filled or empty */
5059 if (trace_buffer_iter(iter, iter->cpu_file))
5060 return POLLIN | POLLRDNORM;
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005061
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005062 if (tr->trace_flags & TRACE_ITER_BLOCK)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005063 /*
5064 * Always select as readable when in blocking mode
5065 */
5066 return POLLIN | POLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005067 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005068 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05005069 filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005070}
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005071
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005072static unsigned int
5073tracing_poll_pipe(struct file *filp, poll_table *poll_table)
5074{
5075 struct trace_iterator *iter = filp->private_data;
5076
5077 return trace_poll(iter, filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005078}
5079
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005080/* Must be called with iter->mutex held. */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005081static int tracing_wait_pipe(struct file *filp)
5082{
5083 struct trace_iterator *iter = filp->private_data;
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04005084 int ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005085
5086 while (trace_empty(iter)) {
5087
5088 if ((filp->f_flags & O_NONBLOCK)) {
5089 return -EAGAIN;
5090 }
5091
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005092 /*
Liu Bo250bfd32013-01-14 10:54:11 +08005093 * We block until we read something and tracing is disabled.
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005094 * We still block if tracing is disabled, but we have never
5095 * read anything. This allows a user to cat this file, and
5096 * then enable tracing. But after we have read something,
5097 * we give an EOF when tracing is again disabled.
5098 *
5099 * iter->pos will be 0 if we haven't read anything.
5100 */
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04005101 if (!tracing_is_on() && iter->pos)
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005102 break;
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04005103
5104 mutex_unlock(&iter->mutex);
5105
Rabin Vincente30f53a2014-11-10 19:46:34 +01005106 ret = wait_on_pipe(iter, false);
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04005107
5108 mutex_lock(&iter->mutex);
5109
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04005110 if (ret)
5111 return ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005112 }
5113
5114 return 1;
5115}
5116
Steven Rostedtb3806b42008-05-12 21:20:46 +02005117/*
5118 * Consumer reader.
5119 */
5120static ssize_t
5121tracing_read_pipe(struct file *filp, char __user *ubuf,
5122 size_t cnt, loff_t *ppos)
5123{
5124 struct trace_iterator *iter = filp->private_data;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005125 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005126
5127 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005128 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
5129 if (sret != -EBUSY)
5130 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005131
Steven Rostedtf9520752009-03-02 14:04:40 -05005132 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005133
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005134 /*
5135 * Avoid more than one consumer on a single file descriptor
5136 * This is just a matter of traces coherency, the ring buffer itself
5137 * is protected.
5138 */
5139 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02005140 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005141 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
5142 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02005143 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02005144 }
5145
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005146waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005147 sret = tracing_wait_pipe(filp);
5148 if (sret <= 0)
5149 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005150
5151 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005152 if (trace_empty(iter)) {
5153 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02005154 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02005155 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02005156
5157 if (cnt >= PAGE_SIZE)
5158 cnt = PAGE_SIZE - 1;
5159
Steven Rostedt53d0aa72008-05-12 21:21:01 +02005160 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02005161 memset(&iter->seq, 0,
5162 sizeof(struct trace_iterator) -
5163 offsetof(struct trace_iterator, seq));
Andrew Vagined5467d2013-08-02 21:16:43 +04005164 cpumask_clear(iter->started);
Steven Rostedt4823ed72008-05-12 21:21:01 +02005165 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005166
Lai Jiangshan4f535962009-05-18 19:35:34 +08005167 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005168 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05005169 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02005170 enum print_line_t ret;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005171 int save_len = iter->seq.seq.len;
Steven Rostedt088b1e422008-05-12 21:20:48 +02005172
Ingo Molnarf9896bf2008-05-12 21:20:47 +02005173 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02005174 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02005175 /* don't print partial lines */
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005176 iter->seq.seq.len = save_len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005177 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02005178 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01005179 if (ret != TRACE_TYPE_NO_CONSUME)
5180 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005181
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005182 if (trace_seq_used(&iter->seq) >= cnt)
Steven Rostedtb3806b42008-05-12 21:20:46 +02005183 break;
Jiri Olsaee5e51f2011-03-25 12:05:18 +01005184
5185 /*
5186 * Setting the full flag means we reached the trace_seq buffer
5187 * size and we should leave by partial output condition above.
5188 * One of the trace_seq_* functions is not used properly.
5189 */
5190 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
5191 iter->ent->type);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005192 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005193 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08005194 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02005195
Steven Rostedtb3806b42008-05-12 21:20:46 +02005196 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005197 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005198 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
Steven Rostedtf9520752009-03-02 14:04:40 -05005199 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005200
5201 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005202 * If there was nothing to send to user, in spite of consuming trace
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005203 * entries, go back to wait for more entries.
5204 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005205 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02005206 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005207
Steven Rostedt107bad82008-05-12 21:21:01 +02005208out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005209 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02005210
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02005211 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005212}
5213
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005214static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
5215 unsigned int idx)
5216{
5217 __free_page(spd->pages[idx]);
5218}
5219
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08005220static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05005221 .can_merge = 0,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005222 .confirm = generic_pipe_buf_confirm,
Al Viro92fdd982014-01-17 07:53:39 -05005223 .release = generic_pipe_buf_release,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005224 .steal = generic_pipe_buf_steal,
5225 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005226};
5227
Steven Rostedt34cd4992009-02-09 12:06:29 -05005228static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01005229tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05005230{
5231 size_t count;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005232 int save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005233 int ret;
5234
5235 /* Seq buffer is page-sized, exactly what we need. */
5236 for (;;) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005237 save_len = iter->seq.seq.len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005238 ret = print_trace_line(iter);
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005239
5240 if (trace_seq_has_overflowed(&iter->seq)) {
5241 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005242 break;
5243 }
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005244
5245 /*
5246 * This should not be hit, because it should only
5247 * be set if the iter->seq overflowed. But check it
5248 * anyway to be safe.
5249 */
Steven Rostedt34cd4992009-02-09 12:06:29 -05005250 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005251 iter->seq.seq.len = save_len;
5252 break;
5253 }
5254
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005255 count = trace_seq_used(&iter->seq) - save_len;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05005256 if (rem < count) {
5257 rem = 0;
5258 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005259 break;
5260 }
5261
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08005262 if (ret != TRACE_TYPE_NO_CONSUME)
5263 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05005264 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05005265 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05005266 rem = 0;
5267 iter->ent = NULL;
5268 break;
5269 }
5270 }
5271
5272 return rem;
5273}
5274
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005275static ssize_t tracing_splice_read_pipe(struct file *filp,
5276 loff_t *ppos,
5277 struct pipe_inode_info *pipe,
5278 size_t len,
5279 unsigned int flags)
5280{
Jens Axboe35f3d142010-05-20 10:43:18 +02005281 struct page *pages_def[PIPE_DEF_BUFFERS];
5282 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005283 struct trace_iterator *iter = filp->private_data;
5284 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02005285 .pages = pages_def,
5286 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005287 .nr_pages = 0, /* This gets updated below. */
Eric Dumazet047fe362012-06-12 15:24:40 +02005288 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt34cd4992009-02-09 12:06:29 -05005289 .flags = flags,
5290 .ops = &tracing_pipe_buf_ops,
5291 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005292 };
5293 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005294 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005295 unsigned int i;
5296
Jens Axboe35f3d142010-05-20 10:43:18 +02005297 if (splice_grow_spd(pipe, &spd))
5298 return -ENOMEM;
5299
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005300 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005301
5302 if (iter->trace->splice_read) {
5303 ret = iter->trace->splice_read(iter, filp,
5304 ppos, pipe, len, flags);
5305 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05005306 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005307 }
5308
5309 ret = tracing_wait_pipe(filp);
5310 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05005311 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005312
Jason Wessel955b61e2010-08-05 09:22:23 -05005313 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005314 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05005315 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005316 }
5317
Lai Jiangshan4f535962009-05-18 19:35:34 +08005318 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005319 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08005320
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005321 /* Fill as many pages as possible. */
Al Viroa786c062014-04-11 12:01:03 -04005322 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
Jens Axboe35f3d142010-05-20 10:43:18 +02005323 spd.pages[i] = alloc_page(GFP_KERNEL);
5324 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05005325 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005326
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01005327 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005328
5329 /* Copy the data into the page, so we can start over. */
5330 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02005331 page_address(spd.pages[i]),
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005332 trace_seq_used(&iter->seq));
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005333 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02005334 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005335 break;
5336 }
Jens Axboe35f3d142010-05-20 10:43:18 +02005337 spd.partial[i].offset = 0;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05005338 spd.partial[i].len = trace_seq_used(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005339
Steven Rostedtf9520752009-03-02 14:04:40 -05005340 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005341 }
5342
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08005343 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08005344 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005345 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005346
5347 spd.nr_pages = i;
5348
Steven Rostedt (Red Hat)a29054d92016-03-18 15:46:48 -04005349 if (i)
5350 ret = splice_to_pipe(pipe, &spd);
5351 else
5352 ret = 0;
Jens Axboe35f3d142010-05-20 10:43:18 +02005353out:
Eric Dumazet047fe362012-06-12 15:24:40 +02005354 splice_shrink_spd(&spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02005355 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005356
Steven Rostedt34cd4992009-02-09 12:06:29 -05005357out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005358 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02005359 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005360}
5361
Steven Rostedta98a3c32008-05-12 21:20:59 +02005362static ssize_t
5363tracing_entries_read(struct file *filp, char __user *ubuf,
5364 size_t cnt, loff_t *ppos)
5365{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005366 struct inode *inode = file_inode(filp);
5367 struct trace_array *tr = inode->i_private;
5368 int cpu = tracing_get_cpu(inode);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005369 char buf[64];
5370 int r = 0;
5371 ssize_t ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005372
Steven Rostedtdb526ca2009-03-12 13:53:25 -04005373 mutex_lock(&trace_types_lock);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005374
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005375 if (cpu == RING_BUFFER_ALL_CPUS) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005376 int cpu, buf_size_same;
5377 unsigned long size;
5378
5379 size = 0;
5380 buf_size_same = 1;
5381 /* check if all cpu sizes are same */
5382 for_each_tracing_cpu(cpu) {
5383 /* fill in the size from first enabled cpu */
5384 if (size == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005385 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
5386 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005387 buf_size_same = 0;
5388 break;
5389 }
5390 }
5391
5392 if (buf_size_same) {
5393 if (!ring_buffer_expanded)
5394 r = sprintf(buf, "%lu (expanded: %lu)\n",
5395 size >> 10,
5396 trace_buf_size >> 10);
5397 else
5398 r = sprintf(buf, "%lu\n", size >> 10);
5399 } else
5400 r = sprintf(buf, "X\n");
5401 } else
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005402 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005403
Steven Rostedtdb526ca2009-03-12 13:53:25 -04005404 mutex_unlock(&trace_types_lock);
5405
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005406 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5407 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005408}
5409
5410static ssize_t
5411tracing_entries_write(struct file *filp, const char __user *ubuf,
5412 size_t cnt, loff_t *ppos)
5413{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005414 struct inode *inode = file_inode(filp);
5415 struct trace_array *tr = inode->i_private;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005416 unsigned long val;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005417 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005418
Peter Huewe22fe9b52011-06-07 21:58:27 +02005419 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5420 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005421 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005422
5423 /* must have at least 1 entry */
5424 if (!val)
5425 return -EINVAL;
5426
Steven Rostedt1696b2b2008-11-13 00:09:35 -05005427 /* value is in KB */
5428 val <<= 10;
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005429 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005430 if (ret < 0)
5431 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005432
Jiri Olsacf8517c2009-10-23 19:36:16 -04005433 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005434
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005435 return cnt;
5436}
Steven Rostedtbf5e6512008-11-10 21:46:00 -05005437
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005438static ssize_t
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005439tracing_total_entries_read(struct file *filp, char __user *ubuf,
5440 size_t cnt, loff_t *ppos)
5441{
5442 struct trace_array *tr = filp->private_data;
5443 char buf[64];
5444 int r, cpu;
5445 unsigned long size = 0, expanded_size = 0;
5446
5447 mutex_lock(&trace_types_lock);
5448 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005449 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005450 if (!ring_buffer_expanded)
5451 expanded_size += trace_buf_size >> 10;
5452 }
5453 if (ring_buffer_expanded)
5454 r = sprintf(buf, "%lu\n", size);
5455 else
5456 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
5457 mutex_unlock(&trace_types_lock);
5458
5459 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5460}
5461
5462static ssize_t
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005463tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
5464 size_t cnt, loff_t *ppos)
5465{
5466 /*
5467 * There is no need to read what the user has written, this function
5468 * is just to make sure that there is no error when "echo" is used
5469 */
5470
5471 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02005472
5473 return cnt;
5474}
5475
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005476static int
5477tracing_free_buffer_release(struct inode *inode, struct file *filp)
5478{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005479 struct trace_array *tr = inode->i_private;
5480
Steven Rostedtcf30cf62011-06-14 22:44:07 -04005481 /* disable tracing ? */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005482 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
Alexander Z Lam711e1242013-08-02 18:36:15 -07005483 tracer_tracing_off(tr);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005484 /* resize the ring buffer to 0 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005485 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005486
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005487 trace_array_put(tr);
5488
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005489 return 0;
5490}
5491
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005492static ssize_t
5493tracing_mark_write(struct file *filp, const char __user *ubuf,
5494 size_t cnt, loff_t *fpos)
5495{
Steven Rostedtd696b582011-09-22 11:50:27 -04005496 unsigned long addr = (unsigned long)ubuf;
Alexander Z Lam2d716192013-07-01 15:31:24 -07005497 struct trace_array *tr = filp->private_data;
Steven Rostedtd696b582011-09-22 11:50:27 -04005498 struct ring_buffer_event *event;
5499 struct ring_buffer *buffer;
5500 struct print_entry *entry;
5501 unsigned long irq_flags;
5502 struct page *pages[2];
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005503 void *map_page[2];
Steven Rostedtd696b582011-09-22 11:50:27 -04005504 int nr_pages = 1;
5505 ssize_t written;
Steven Rostedtd696b582011-09-22 11:50:27 -04005506 int offset;
5507 int size;
5508 int len;
5509 int ret;
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005510 int i;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005511
Steven Rostedtc76f0692008-11-07 22:36:02 -05005512 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005513 return -EINVAL;
5514
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005515 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -07005516 return -EINVAL;
5517
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005518 if (cnt > TRACE_BUF_SIZE)
5519 cnt = TRACE_BUF_SIZE;
5520
Steven Rostedtd696b582011-09-22 11:50:27 -04005521 /*
5522 * Userspace is injecting traces into the kernel trace buffer.
5523 * We want to be as non intrusive as possible.
5524 * To do so, we do not want to allocate any special buffers
5525 * or take any locks, but instead write the userspace data
5526 * straight into the ring buffer.
5527 *
5528 * First we need to pin the userspace buffer into memory,
5529 * which, most likely it is, because it just referenced it.
5530 * But there's no guarantee that it is. By using get_user_pages_fast()
5531 * and kmap_atomic/kunmap_atomic() we can get access to the
5532 * pages directly. We then write the data directly into the
5533 * ring buffer.
5534 */
5535 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005536
Steven Rostedtd696b582011-09-22 11:50:27 -04005537 /* check if we cross pages */
5538 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
5539 nr_pages = 2;
5540
5541 offset = addr & (PAGE_SIZE - 1);
5542 addr &= PAGE_MASK;
5543
5544 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
5545 if (ret < nr_pages) {
5546 while (--ret >= 0)
5547 put_page(pages[ret]);
5548 written = -EFAULT;
5549 goto out;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005550 }
5551
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005552 for (i = 0; i < nr_pages; i++)
5553 map_page[i] = kmap_atomic(pages[i]);
Steven Rostedtd696b582011-09-22 11:50:27 -04005554
5555 local_save_flags(irq_flags);
5556 size = sizeof(*entry) + cnt + 2; /* possible \n added */
Alexander Z Lam2d716192013-07-01 15:31:24 -07005557 buffer = tr->trace_buffer.buffer;
Steven Rostedtd696b582011-09-22 11:50:27 -04005558 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
5559 irq_flags, preempt_count());
5560 if (!event) {
5561 /* Ring buffer disabled, return as if not open for write */
5562 written = -EBADF;
5563 goto out_unlock;
5564 }
5565
5566 entry = ring_buffer_event_data(event);
5567 entry->ip = _THIS_IP_;
5568
5569 if (nr_pages == 2) {
5570 len = PAGE_SIZE - offset;
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005571 memcpy(&entry->buf, map_page[0] + offset, len);
5572 memcpy(&entry->buf[len], map_page[1], cnt - len);
Steven Rostedtd696b582011-09-22 11:50:27 -04005573 } else
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005574 memcpy(&entry->buf, map_page[0] + offset, cnt);
Steven Rostedtd696b582011-09-22 11:50:27 -04005575
5576 if (entry->buf[cnt - 1] != '\n') {
5577 entry->buf[cnt] = '\n';
5578 entry->buf[cnt + 1] = '\0';
5579 } else
5580 entry->buf[cnt] = '\0';
5581
Steven Rostedt7ffbd482012-10-11 12:14:25 -04005582 __buffer_unlock_commit(buffer, event);
Steven Rostedtd696b582011-09-22 11:50:27 -04005583
5584 written = cnt;
5585
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02005586 *fpos += written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005587
Steven Rostedtd696b582011-09-22 11:50:27 -04005588 out_unlock:
Vikram Mulukutla72158532014-12-17 18:50:56 -08005589 for (i = nr_pages - 1; i >= 0; i--) {
Steven Rostedt6edb2a82012-05-11 23:28:49 -04005590 kunmap_atomic(map_page[i]);
5591 put_page(pages[i]);
5592 }
Steven Rostedtd696b582011-09-22 11:50:27 -04005593 out:
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02005594 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005595}
5596
Li Zefan13f16d22009-12-08 11:16:11 +08005597static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08005598{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005599 struct trace_array *tr = m->private;
Zhaolei5079f322009-08-25 16:12:56 +08005600 int i;
5601
5602 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08005603 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08005604 "%s%s%s%s", i ? " " : "",
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005605 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
5606 i == tr->clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08005607 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08005608
Li Zefan13f16d22009-12-08 11:16:11 +08005609 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08005610}
5611
Steven Rostedte1e232c2014-02-10 23:38:46 -05005612static int tracing_set_clock(struct trace_array *tr, const char *clockstr)
Zhaolei5079f322009-08-25 16:12:56 +08005613{
Zhaolei5079f322009-08-25 16:12:56 +08005614 int i;
5615
Zhaolei5079f322009-08-25 16:12:56 +08005616 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
5617 if (strcmp(trace_clocks[i].name, clockstr) == 0)
5618 break;
5619 }
5620 if (i == ARRAY_SIZE(trace_clocks))
5621 return -EINVAL;
5622
Zhaolei5079f322009-08-25 16:12:56 +08005623 mutex_lock(&trace_types_lock);
5624
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005625 tr->clock_id = i;
5626
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005627 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
Zhaolei5079f322009-08-25 16:12:56 +08005628
David Sharp60303ed2012-10-11 16:27:52 -07005629 /*
5630 * New clock may not be consistent with the previous clock.
5631 * Reset the buffer so that it doesn't have incomparable timestamps.
5632 */
Alexander Z Lam94571582013-08-02 18:36:16 -07005633 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005634
5635#ifdef CONFIG_TRACER_MAX_TRACE
5636 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
5637 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
Alexander Z Lam94571582013-08-02 18:36:16 -07005638 tracing_reset_online_cpus(&tr->max_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005639#endif
David Sharp60303ed2012-10-11 16:27:52 -07005640
Zhaolei5079f322009-08-25 16:12:56 +08005641 mutex_unlock(&trace_types_lock);
5642
Steven Rostedte1e232c2014-02-10 23:38:46 -05005643 return 0;
5644}
5645
5646static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
5647 size_t cnt, loff_t *fpos)
5648{
5649 struct seq_file *m = filp->private_data;
5650 struct trace_array *tr = m->private;
5651 char buf[64];
5652 const char *clockstr;
5653 int ret;
5654
5655 if (cnt >= sizeof(buf))
5656 return -EINVAL;
5657
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08005658 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedte1e232c2014-02-10 23:38:46 -05005659 return -EFAULT;
5660
5661 buf[cnt] = 0;
5662
5663 clockstr = strstrip(buf);
5664
5665 ret = tracing_set_clock(tr, clockstr);
5666 if (ret)
5667 return ret;
5668
Zhaolei5079f322009-08-25 16:12:56 +08005669 *fpos += cnt;
5670
5671 return cnt;
5672}
5673
Li Zefan13f16d22009-12-08 11:16:11 +08005674static int tracing_clock_open(struct inode *inode, struct file *file)
5675{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005676 struct trace_array *tr = inode->i_private;
5677 int ret;
5678
Li Zefan13f16d22009-12-08 11:16:11 +08005679 if (tracing_disabled)
5680 return -ENODEV;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005681
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005682 if (trace_array_get(tr))
5683 return -ENODEV;
5684
5685 ret = single_open(file, tracing_clock_show, inode->i_private);
5686 if (ret < 0)
5687 trace_array_put(tr);
5688
5689 return ret;
Li Zefan13f16d22009-12-08 11:16:11 +08005690}
5691
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005692struct ftrace_buffer_info {
5693 struct trace_iterator iter;
5694 void *spare;
5695 unsigned int read;
5696};
5697
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005698#ifdef CONFIG_TRACER_SNAPSHOT
5699static int tracing_snapshot_open(struct inode *inode, struct file *file)
5700{
Oleg Nesterov6484c712013-07-23 17:26:10 +02005701 struct trace_array *tr = inode->i_private;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005702 struct trace_iterator *iter;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005703 struct seq_file *m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005704 int ret = 0;
5705
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005706 if (trace_array_get(tr) < 0)
5707 return -ENODEV;
5708
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005709 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02005710 iter = __tracing_open(inode, file, true);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005711 if (IS_ERR(iter))
5712 ret = PTR_ERR(iter);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005713 } else {
5714 /* Writes still need the seq_file to hold the private data */
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005715 ret = -ENOMEM;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005716 m = kzalloc(sizeof(*m), GFP_KERNEL);
5717 if (!m)
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005718 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005719 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
5720 if (!iter) {
5721 kfree(m);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005722 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005723 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005724 ret = 0;
5725
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005726 iter->tr = tr;
Oleg Nesterov6484c712013-07-23 17:26:10 +02005727 iter->trace_buffer = &tr->max_buffer;
5728 iter->cpu_file = tracing_get_cpu(inode);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005729 m->private = iter;
5730 file->private_data = m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005731 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005732out:
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005733 if (ret < 0)
5734 trace_array_put(tr);
5735
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005736 return ret;
5737}
5738
5739static ssize_t
5740tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
5741 loff_t *ppos)
5742{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005743 struct seq_file *m = filp->private_data;
5744 struct trace_iterator *iter = m->private;
5745 struct trace_array *tr = iter->tr;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005746 unsigned long val;
5747 int ret;
5748
5749 ret = tracing_update_buffers();
5750 if (ret < 0)
5751 return ret;
5752
5753 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5754 if (ret)
5755 return ret;
5756
5757 mutex_lock(&trace_types_lock);
5758
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005759 if (tr->current_trace->use_max_tr) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005760 ret = -EBUSY;
5761 goto out;
5762 }
5763
5764 switch (val) {
5765 case 0:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005766 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
5767 ret = -EINVAL;
5768 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005769 }
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04005770 if (tr->allocated_snapshot)
5771 free_snapshot(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005772 break;
5773 case 1:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005774/* Only allow per-cpu swap if the ring buffer supports it */
5775#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
5776 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
5777 ret = -EINVAL;
5778 break;
5779 }
5780#endif
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05005781 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04005782 ret = alloc_snapshot(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005783 if (ret < 0)
5784 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005785 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005786 local_irq_disable();
5787 /* Now, we're going to swap */
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005788 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05005789 update_max_tr(tr, current, smp_processor_id());
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005790 else
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05005791 update_max_tr_single(tr, current, iter->cpu_file);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005792 local_irq_enable();
5793 break;
5794 default:
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05005795 if (tr->allocated_snapshot) {
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05005796 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
5797 tracing_reset_online_cpus(&tr->max_buffer);
5798 else
5799 tracing_reset(&tr->max_buffer, iter->cpu_file);
5800 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005801 break;
5802 }
5803
5804 if (ret >= 0) {
5805 *ppos += cnt;
5806 ret = cnt;
5807 }
5808out:
5809 mutex_unlock(&trace_types_lock);
5810 return ret;
5811}
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005812
5813static int tracing_snapshot_release(struct inode *inode, struct file *file)
5814{
5815 struct seq_file *m = file->private_data;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005816 int ret;
5817
5818 ret = tracing_release(inode, file);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005819
5820 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04005821 return ret;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005822
5823 /* If write only, the seq_file is just a stub */
5824 if (m)
5825 kfree(m->private);
5826 kfree(m);
5827
5828 return 0;
5829}
5830
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005831static int tracing_buffers_open(struct inode *inode, struct file *filp);
5832static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
5833 size_t count, loff_t *ppos);
5834static int tracing_buffers_release(struct inode *inode, struct file *file);
5835static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5836 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
5837
5838static int snapshot_raw_open(struct inode *inode, struct file *filp)
5839{
5840 struct ftrace_buffer_info *info;
5841 int ret;
5842
5843 ret = tracing_buffers_open(inode, filp);
5844 if (ret < 0)
5845 return ret;
5846
5847 info = filp->private_data;
5848
5849 if (info->iter.trace->use_max_tr) {
5850 tracing_buffers_release(inode, filp);
5851 return -EBUSY;
5852 }
5853
5854 info->iter.snapshot = true;
5855 info->iter.trace_buffer = &info->iter.tr->max_buffer;
5856
5857 return ret;
5858}
5859
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005860#endif /* CONFIG_TRACER_SNAPSHOT */
5861
5862
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005863static const struct file_operations tracing_thresh_fops = {
5864 .open = tracing_open_generic,
5865 .read = tracing_thresh_read,
5866 .write = tracing_thresh_write,
5867 .llseek = generic_file_llseek,
5868};
5869
Chen Gange428abb2015-11-10 05:15:15 +08005870#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005871static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005872 .open = tracing_open_generic,
5873 .read = tracing_max_lat_read,
5874 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005875 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005876};
Chen Gange428abb2015-11-10 05:15:15 +08005877#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005878
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005879static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005880 .open = tracing_open_generic,
5881 .read = tracing_set_trace_read,
5882 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005883 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005884};
5885
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005886static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005887 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02005888 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005889 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02005890 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005891 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005892 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02005893};
5894
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005895static const struct file_operations tracing_entries_fops = {
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005896 .open = tracing_open_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02005897 .read = tracing_entries_read,
5898 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005899 .llseek = generic_file_llseek,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02005900 .release = tracing_release_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02005901};
5902
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005903static const struct file_operations tracing_total_entries_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005904 .open = tracing_open_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005905 .read = tracing_total_entries_read,
5906 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005907 .release = tracing_release_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07005908};
5909
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005910static const struct file_operations tracing_free_buffer_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005911 .open = tracing_open_generic_tr,
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005912 .write = tracing_free_buffer_write,
5913 .release = tracing_free_buffer_release,
5914};
5915
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005916static const struct file_operations tracing_mark_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005917 .open = tracing_open_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005918 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005919 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005920 .release = tracing_release_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03005921};
5922
Zhaolei5079f322009-08-25 16:12:56 +08005923static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08005924 .open = tracing_clock_open,
5925 .read = seq_read,
5926 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005927 .release = tracing_single_release_tr,
Zhaolei5079f322009-08-25 16:12:56 +08005928 .write = tracing_clock_write,
5929};
5930
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005931#ifdef CONFIG_TRACER_SNAPSHOT
5932static const struct file_operations snapshot_fops = {
5933 .open = tracing_snapshot_open,
5934 .read = seq_read,
5935 .write = tracing_snapshot_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005936 .llseek = tracing_lseek,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005937 .release = tracing_snapshot_release,
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005938};
Hiraku Toyookadebdd572012-12-26 11:53:00 +09005939
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005940static const struct file_operations snapshot_raw_fops = {
5941 .open = snapshot_raw_open,
5942 .read = tracing_buffers_read,
5943 .release = tracing_buffers_release,
5944 .splice_read = tracing_buffers_splice_read,
5945 .llseek = no_llseek,
Steven Rostedt2cadf912008-12-01 22:20:19 -05005946};
5947
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05005948#endif /* CONFIG_TRACER_SNAPSHOT */
5949
Steven Rostedt2cadf912008-12-01 22:20:19 -05005950static int tracing_buffers_open(struct inode *inode, struct file *filp)
5951{
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02005952 struct trace_array *tr = inode->i_private;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005953 struct ftrace_buffer_info *info;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005954 int ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005955
5956 if (tracing_disabled)
5957 return -ENODEV;
5958
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005959 if (trace_array_get(tr) < 0)
5960 return -ENODEV;
5961
Steven Rostedt2cadf912008-12-01 22:20:19 -05005962 info = kzalloc(sizeof(*info), GFP_KERNEL);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005963 if (!info) {
5964 trace_array_put(tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05005965 return -ENOMEM;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005966 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05005967
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05005968 mutex_lock(&trace_types_lock);
5969
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005970 info->iter.tr = tr;
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02005971 info->iter.cpu_file = tracing_get_cpu(inode);
Steven Rostedtb6273442013-02-28 13:44:11 -05005972 info->iter.trace = tr->current_trace;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005973 info->iter.trace_buffer = &tr->trace_buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005974 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005975 /* Force reading ring buffer for first read */
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005976 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005977
5978 filp->private_data = info;
5979
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005980 tr->current_trace->ref++;
5981
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05005982 mutex_unlock(&trace_types_lock);
5983
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005984 ret = nonseekable_open(inode, filp);
5985 if (ret < 0)
5986 trace_array_put(tr);
5987
5988 return ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05005989}
5990
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05005991static unsigned int
5992tracing_buffers_poll(struct file *filp, poll_table *poll_table)
5993{
5994 struct ftrace_buffer_info *info = filp->private_data;
5995 struct trace_iterator *iter = &info->iter;
5996
5997 return trace_poll(iter, filp, poll_table);
5998}
5999
Steven Rostedt2cadf912008-12-01 22:20:19 -05006000static ssize_t
6001tracing_buffers_read(struct file *filp, char __user *ubuf,
6002 size_t count, loff_t *ppos)
6003{
6004 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006005 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006006 ssize_t ret;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006007 ssize_t size;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006008
Steven Rostedt2dc5d122009-03-04 19:10:05 -05006009 if (!count)
6010 return 0;
6011
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006012#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006013 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
6014 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006015#endif
6016
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006017 if (!info->spare)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006018 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
6019 iter->cpu_file);
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006020 if (!info->spare)
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006021 return -ENOMEM;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006022
Steven Rostedt2cadf912008-12-01 22:20:19 -05006023 /* Do we have previous read data to read? */
6024 if (info->read < PAGE_SIZE)
6025 goto read;
6026
Steven Rostedtb6273442013-02-28 13:44:11 -05006027 again:
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006028 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006029 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006030 &info->spare,
6031 count,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006032 iter->cpu_file, 0);
6033 trace_access_unlock(iter->cpu_file);
Steven Rostedtb6273442013-02-28 13:44:11 -05006034
6035 if (ret < 0) {
6036 if (trace_empty(iter)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006037 if ((filp->f_flags & O_NONBLOCK))
6038 return -EAGAIN;
6039
Rabin Vincente30f53a2014-11-10 19:46:34 +01006040 ret = wait_on_pipe(iter, false);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006041 if (ret)
6042 return ret;
6043
Steven Rostedtb6273442013-02-28 13:44:11 -05006044 goto again;
6045 }
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006046 return 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05006047 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05006048
Steven Rostedt436fc282011-10-14 10:44:25 -04006049 info->read = 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05006050 read:
Steven Rostedt2cadf912008-12-01 22:20:19 -05006051 size = PAGE_SIZE - info->read;
6052 if (size > count)
6053 size = count;
6054
6055 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006056 if (ret == size)
6057 return -EFAULT;
6058
Steven Rostedt2dc5d122009-03-04 19:10:05 -05006059 size -= ret;
6060
Steven Rostedt2cadf912008-12-01 22:20:19 -05006061 *ppos += size;
6062 info->read += size;
6063
6064 return size;
6065}
6066
6067static int tracing_buffers_release(struct inode *inode, struct file *file)
6068{
6069 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006070 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006071
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006072 mutex_lock(&trace_types_lock);
6073
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05006074 iter->tr->current_trace->ref--;
6075
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006076 __trace_array_put(iter->tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006077
Lai Jiangshanddd538f2009-04-02 15:16:59 +08006078 if (info->spare)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006079 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006080 kfree(info);
6081
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05006082 mutex_unlock(&trace_types_lock);
6083
Steven Rostedt2cadf912008-12-01 22:20:19 -05006084 return 0;
6085}
6086
6087struct buffer_ref {
6088 struct ring_buffer *buffer;
6089 void *page;
6090 int ref;
6091};
6092
6093static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
6094 struct pipe_buffer *buf)
6095{
6096 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
6097
6098 if (--ref->ref)
6099 return;
6100
6101 ring_buffer_free_read_page(ref->buffer, ref->page);
6102 kfree(ref);
6103 buf->private = 0;
6104}
6105
Steven Rostedt2cadf912008-12-01 22:20:19 -05006106static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
6107 struct pipe_buffer *buf)
6108{
6109 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
6110
6111 ref->ref++;
6112}
6113
6114/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08006115static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05006116 .can_merge = 0,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006117 .confirm = generic_pipe_buf_confirm,
6118 .release = buffer_pipe_buf_release,
Masami Hiramatsud55cb6c2012-08-09 21:31:10 +09006119 .steal = generic_pipe_buf_steal,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006120 .get = buffer_pipe_buf_get,
6121};
6122
6123/*
6124 * Callback from splice_to_pipe(), if we need to release some pages
6125 * at the end of the spd in case we error'ed out in filling the pipe.
6126 */
6127static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
6128{
6129 struct buffer_ref *ref =
6130 (struct buffer_ref *)spd->partial[i].private;
6131
6132 if (--ref->ref)
6133 return;
6134
6135 ring_buffer_free_read_page(ref->buffer, ref->page);
6136 kfree(ref);
6137 spd->partial[i].private = 0;
6138}
6139
6140static ssize_t
6141tracing_buffers_splice_read(struct file *file, loff_t *ppos,
6142 struct pipe_inode_info *pipe, size_t len,
6143 unsigned int flags)
6144{
6145 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006146 struct trace_iterator *iter = &info->iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02006147 struct partial_page partial_def[PIPE_DEF_BUFFERS];
6148 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05006149 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02006150 .pages = pages_def,
6151 .partial = partial_def,
Eric Dumazet047fe362012-06-12 15:24:40 +02006152 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006153 .flags = flags,
6154 .ops = &buffer_pipe_buf_ops,
6155 .spd_release = buffer_spd_release,
6156 };
6157 struct buffer_ref *ref;
Steven Rostedt93459c62009-04-29 00:23:13 -04006158 int entries, size, i;
Rabin Vincent07906da2014-11-06 22:26:07 +01006159 ssize_t ret = 0;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006160
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006161#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006162 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
6163 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006164#endif
6165
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006166 if (splice_grow_spd(pipe, &spd))
6167 return -ENOMEM;
Jens Axboe35f3d142010-05-20 10:43:18 +02006168
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006169 if (*ppos & (PAGE_SIZE - 1))
6170 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08006171
6172 if (len & (PAGE_SIZE - 1)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006173 if (len < PAGE_SIZE)
6174 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08006175 len &= PAGE_MASK;
6176 }
6177
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006178 again:
6179 trace_access_lock(iter->cpu_file);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006180 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt93459c62009-04-29 00:23:13 -04006181
Al Viroa786c062014-04-11 12:01:03 -04006182 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05006183 struct page *page;
6184 int r;
6185
6186 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
Rabin Vincent07906da2014-11-06 22:26:07 +01006187 if (!ref) {
6188 ret = -ENOMEM;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006189 break;
Rabin Vincent07906da2014-11-06 22:26:07 +01006190 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05006191
Steven Rostedt7267fa62009-04-29 00:16:21 -04006192 ref->ref = 1;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006193 ref->buffer = iter->trace_buffer->buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006194 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006195 if (!ref->page) {
Rabin Vincent07906da2014-11-06 22:26:07 +01006196 ret = -ENOMEM;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006197 kfree(ref);
6198 break;
6199 }
6200
6201 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006202 len, iter->cpu_file, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006203 if (r < 0) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07006204 ring_buffer_free_read_page(ref->buffer, ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006205 kfree(ref);
6206 break;
6207 }
6208
6209 /*
6210 * zero out any left over data, this is going to
6211 * user land.
6212 */
6213 size = ring_buffer_page_len(ref->page);
6214 if (size < PAGE_SIZE)
6215 memset(ref->page + size, 0, PAGE_SIZE - size);
6216
6217 page = virt_to_page(ref->page);
6218
6219 spd.pages[i] = page;
6220 spd.partial[i].len = PAGE_SIZE;
6221 spd.partial[i].offset = 0;
6222 spd.partial[i].private = (unsigned long)ref;
6223 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08006224 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04006225
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006226 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006227 }
6228
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006229 trace_access_unlock(iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05006230 spd.nr_pages = i;
6231
6232 /* did we read anything? */
6233 if (!spd.nr_pages) {
Rabin Vincent07906da2014-11-06 22:26:07 +01006234 if (ret)
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006235 return ret;
Rabin Vincent07906da2014-11-06 22:26:07 +01006236
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006237 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
6238 return -EAGAIN;
6239
Rabin Vincente30f53a2014-11-10 19:46:34 +01006240 ret = wait_on_pipe(iter, true);
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04006241 if (ret)
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006242 return ret;
Rabin Vincente30f53a2014-11-10 19:46:34 +01006243
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006244 goto again;
Steven Rostedt2cadf912008-12-01 22:20:19 -05006245 }
6246
6247 ret = splice_to_pipe(pipe, &spd);
Eric Dumazet047fe362012-06-12 15:24:40 +02006248 splice_shrink_spd(&spd);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006249
Steven Rostedt2cadf912008-12-01 22:20:19 -05006250 return ret;
6251}
6252
6253static const struct file_operations tracing_buffers_fops = {
6254 .open = tracing_buffers_open,
6255 .read = tracing_buffers_read,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006256 .poll = tracing_buffers_poll,
Steven Rostedt2cadf912008-12-01 22:20:19 -05006257 .release = tracing_buffers_release,
6258 .splice_read = tracing_buffers_splice_read,
6259 .llseek = no_llseek,
6260};
6261
Steven Rostedtc8d77182009-04-29 18:03:45 -04006262static ssize_t
6263tracing_stats_read(struct file *filp, char __user *ubuf,
6264 size_t count, loff_t *ppos)
6265{
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006266 struct inode *inode = file_inode(filp);
6267 struct trace_array *tr = inode->i_private;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006268 struct trace_buffer *trace_buf = &tr->trace_buffer;
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006269 int cpu = tracing_get_cpu(inode);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006270 struct trace_seq *s;
6271 unsigned long cnt;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07006272 unsigned long long t;
6273 unsigned long usec_rem;
Steven Rostedtc8d77182009-04-29 18:03:45 -04006274
Li Zefane4f2d102009-06-15 10:57:28 +08006275 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006276 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01006277 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04006278
6279 trace_seq_init(s);
6280
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006281 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006282 trace_seq_printf(s, "entries: %ld\n", cnt);
6283
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006284 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006285 trace_seq_printf(s, "overrun: %ld\n", cnt);
6286
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006287 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006288 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
6289
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006290 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07006291 trace_seq_printf(s, "bytes: %ld\n", cnt);
6292
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09006293 if (trace_clocks[tr->clock_id].in_ns) {
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08006294 /* local or global for trace_clock */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006295 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08006296 usec_rem = do_div(t, USEC_PER_SEC);
6297 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
6298 t, usec_rem);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07006299
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006300 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08006301 usec_rem = do_div(t, USEC_PER_SEC);
6302 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
6303 } else {
6304 /* counter or tsc mode for trace_clock */
6305 trace_seq_printf(s, "oldest event ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006306 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08006307
6308 trace_seq_printf(s, "now ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006309 ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08006310 }
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07006311
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006312 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
Slava Pestov884bfe82011-07-15 14:23:58 -07006313 trace_seq_printf(s, "dropped events: %ld\n", cnt);
6314
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006315 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05006316 trace_seq_printf(s, "read events: %ld\n", cnt);
6317
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006318 count = simple_read_from_buffer(ubuf, count, ppos,
6319 s->buffer, trace_seq_used(s));
Steven Rostedtc8d77182009-04-29 18:03:45 -04006320
6321 kfree(s);
6322
6323 return count;
6324}
6325
6326static const struct file_operations tracing_stats_fops = {
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006327 .open = tracing_open_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04006328 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006329 .llseek = generic_file_llseek,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006330 .release = tracing_release_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04006331};
6332
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006333#ifdef CONFIG_DYNAMIC_FTRACE
6334
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006335int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006336{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006337 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006338}
6339
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006340static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006341tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006342 size_t cnt, loff_t *ppos)
6343{
Steven Rostedta26a2a22008-10-31 00:03:22 -04006344 static char ftrace_dyn_info_buffer[1024];
6345 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006346 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006347 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04006348 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006349 int r;
6350
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006351 mutex_lock(&dyn_info_mutex);
6352 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006353
Steven Rostedta26a2a22008-10-31 00:03:22 -04006354 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006355 buf[r++] = '\n';
6356
6357 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6358
6359 mutex_unlock(&dyn_info_mutex);
6360
6361 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006362}
6363
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006364static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006365 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04006366 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006367 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006368};
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006369#endif /* CONFIG_DYNAMIC_FTRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006370
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006371#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
6372static void
6373ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006374{
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006375 tracing_snapshot();
6376}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006377
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006378static void
6379ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
6380{
6381 unsigned long *count = (long *)data;
6382
6383 if (!*count)
6384 return;
6385
6386 if (*count != -1)
6387 (*count)--;
6388
6389 tracing_snapshot();
6390}
6391
6392static int
6393ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
6394 struct ftrace_probe_ops *ops, void *data)
6395{
6396 long count = (long)data;
6397
6398 seq_printf(m, "%ps:", (void *)ip);
6399
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006400 seq_puts(m, "snapshot");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006401
6402 if (count == -1)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006403 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006404 else
6405 seq_printf(m, ":count=%ld\n", count);
6406
6407 return 0;
6408}
6409
6410static struct ftrace_probe_ops snapshot_probe_ops = {
6411 .func = ftrace_snapshot,
6412 .print = ftrace_snapshot_print,
6413};
6414
6415static struct ftrace_probe_ops snapshot_count_probe_ops = {
6416 .func = ftrace_count_snapshot,
6417 .print = ftrace_snapshot_print,
6418};
6419
6420static int
6421ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
6422 char *glob, char *cmd, char *param, int enable)
6423{
6424 struct ftrace_probe_ops *ops;
6425 void *count = (void *)-1;
6426 char *number;
6427 int ret;
6428
6429 /* hash funcs only work with set_ftrace_filter */
6430 if (!enable)
6431 return -EINVAL;
6432
6433 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
6434
6435 if (glob[0] == '!') {
6436 unregister_ftrace_function_probe_func(glob+1, ops);
6437 return 0;
6438 }
6439
6440 if (!param)
6441 goto out_reg;
6442
6443 number = strsep(&param, ":");
6444
6445 if (!strlen(number))
6446 goto out_reg;
6447
6448 /*
6449 * We use the callback data field (which is a pointer)
6450 * as our counter.
6451 */
6452 ret = kstrtoul(number, 0, (unsigned long *)&count);
6453 if (ret)
6454 return ret;
6455
6456 out_reg:
6457 ret = register_ftrace_function_probe(glob, ops, count);
6458
6459 if (ret >= 0)
6460 alloc_snapshot(&global_trace);
6461
6462 return ret < 0 ? ret : 0;
6463}
6464
6465static struct ftrace_func_command ftrace_snapshot_cmd = {
6466 .name = "snapshot",
6467 .func = ftrace_trace_snapshot_callback,
6468};
6469
Tom Zanussi38de93a2013-10-24 08:34:18 -05006470static __init int register_snapshot_cmd(void)
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006471{
6472 return register_ftrace_command(&ftrace_snapshot_cmd);
6473}
6474#else
Tom Zanussi38de93a2013-10-24 08:34:18 -05006475static inline __init int register_snapshot_cmd(void) { return 0; }
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04006476#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006477
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006478static struct dentry *tracing_get_dentry(struct trace_array *tr)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006479{
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006480 if (WARN_ON(!tr->dir))
6481 return ERR_PTR(-ENODEV);
6482
6483 /* Top directory uses NULL as the parent */
6484 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
6485 return NULL;
6486
6487 /* All sub buffers have a descriptor */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006488 return tr->dir;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006489}
6490
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006491static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
6492{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006493 struct dentry *d_tracer;
6494
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006495 if (tr->percpu_dir)
6496 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006497
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006498 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05006499 if (IS_ERR(d_tracer))
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006500 return NULL;
6501
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006502 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006503
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006504 WARN_ONCE(!tr->percpu_dir,
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006505 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006506
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006507 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006508}
6509
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006510static struct dentry *
6511trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
6512 void *data, long cpu, const struct file_operations *fops)
6513{
6514 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
6515
6516 if (ret) /* See tracing_get_cpu() */
David Howells7682c912015-03-17 22:26:16 +00006517 d_inode(ret)->i_cdev = (void *)(cpu + 1);
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006518 return ret;
6519}
6520
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006521static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006522tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006523{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006524 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006525 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04006526 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006527
Namhyung Kim0a3d7ce2012-04-23 10:11:57 +09006528 if (!d_percpu)
6529 return;
6530
Steven Rostedtdd49a382010-10-20 21:51:26 -04006531 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006532 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01006533 if (!d_cpu) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07006534 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01006535 return;
6536 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006537
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01006538 /* per cpu trace_pipe */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006539 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
Oleg Nesterov15544202013-07-23 17:25:57 +02006540 tr, cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006541
6542 /* per cpu trace */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006543 trace_create_cpu_file("trace", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02006544 tr, cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04006545
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006546 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006547 tr, cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04006548
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006549 trace_create_cpu_file("stats", 0444, d_cpu,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02006550 tr, cpu, &tracing_stats_fops);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006551
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006552 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006553 tr, cpu, &tracing_entries_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006554
6555#ifdef CONFIG_TRACER_SNAPSHOT
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006556 trace_create_cpu_file("snapshot", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02006557 tr, cpu, &snapshot_fops);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006558
Oleg Nesterov649e9c702013-07-23 17:25:54 +02006559 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02006560 tr, cpu, &snapshot_raw_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006561#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006562}
6563
Steven Rostedt60a11772008-05-12 21:20:44 +02006564#ifdef CONFIG_FTRACE_SELFTEST
6565/* Let selftest have access to static functions in this file */
6566#include "trace_selftest.c"
6567#endif
6568
Steven Rostedt577b7852009-02-26 23:43:05 -05006569static ssize_t
6570trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
6571 loff_t *ppos)
6572{
6573 struct trace_option_dentry *topt = filp->private_data;
6574 char *buf;
6575
6576 if (topt->flags->val & topt->opt->bit)
6577 buf = "1\n";
6578 else
6579 buf = "0\n";
6580
6581 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
6582}
6583
6584static ssize_t
6585trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
6586 loff_t *ppos)
6587{
6588 struct trace_option_dentry *topt = filp->private_data;
6589 unsigned long val;
Steven Rostedt577b7852009-02-26 23:43:05 -05006590 int ret;
6591
Peter Huewe22fe9b52011-06-07 21:58:27 +02006592 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6593 if (ret)
Steven Rostedt577b7852009-02-26 23:43:05 -05006594 return ret;
6595
Li Zefan8d18eaa2009-12-08 11:17:06 +08006596 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05006597 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08006598
6599 if (!!(topt->flags->val & topt->opt->bit) != val) {
6600 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05006601 ret = __set_tracer_option(topt->tr, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05006602 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08006603 mutex_unlock(&trace_types_lock);
6604 if (ret)
6605 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05006606 }
6607
6608 *ppos += cnt;
6609
6610 return cnt;
6611}
6612
6613
6614static const struct file_operations trace_options_fops = {
6615 .open = tracing_open_generic,
6616 .read = trace_options_read,
6617 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006618 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05006619};
6620
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006621/*
6622 * In order to pass in both the trace_array descriptor as well as the index
6623 * to the flag that the trace option file represents, the trace_array
6624 * has a character array of trace_flags_index[], which holds the index
6625 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
6626 * The address of this character array is passed to the flag option file
6627 * read/write callbacks.
6628 *
6629 * In order to extract both the index and the trace_array descriptor,
6630 * get_tr_index() uses the following algorithm.
6631 *
6632 * idx = *ptr;
6633 *
6634 * As the pointer itself contains the address of the index (remember
6635 * index[1] == 1).
6636 *
6637 * Then to get the trace_array descriptor, by subtracting that index
6638 * from the ptr, we get to the start of the index itself.
6639 *
6640 * ptr - idx == &index[0]
6641 *
6642 * Then a simple container_of() from that pointer gets us to the
6643 * trace_array descriptor.
6644 */
6645static void get_tr_index(void *data, struct trace_array **ptr,
6646 unsigned int *pindex)
6647{
6648 *pindex = *(unsigned char *)data;
6649
6650 *ptr = container_of(data - *pindex, struct trace_array,
6651 trace_flags_index);
6652}
6653
Steven Rostedta8259072009-02-26 22:19:12 -05006654static ssize_t
6655trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
6656 loff_t *ppos)
6657{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006658 void *tr_index = filp->private_data;
6659 struct trace_array *tr;
6660 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05006661 char *buf;
6662
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006663 get_tr_index(tr_index, &tr, &index);
6664
6665 if (tr->trace_flags & (1 << index))
Steven Rostedta8259072009-02-26 22:19:12 -05006666 buf = "1\n";
6667 else
6668 buf = "0\n";
6669
6670 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
6671}
6672
6673static ssize_t
6674trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
6675 loff_t *ppos)
6676{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006677 void *tr_index = filp->private_data;
6678 struct trace_array *tr;
6679 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05006680 unsigned long val;
6681 int ret;
6682
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006683 get_tr_index(tr_index, &tr, &index);
6684
Peter Huewe22fe9b52011-06-07 21:58:27 +02006685 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6686 if (ret)
Steven Rostedta8259072009-02-26 22:19:12 -05006687 return ret;
6688
Zhaoleif2d84b62009-08-07 18:55:48 +08006689 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05006690 return -EINVAL;
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04006691
6692 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006693 ret = set_tracer_flag(tr, 1 << index, val);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04006694 mutex_unlock(&trace_types_lock);
Steven Rostedta8259072009-02-26 22:19:12 -05006695
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04006696 if (ret < 0)
6697 return ret;
6698
Steven Rostedta8259072009-02-26 22:19:12 -05006699 *ppos += cnt;
6700
6701 return cnt;
6702}
6703
Steven Rostedta8259072009-02-26 22:19:12 -05006704static const struct file_operations trace_options_core_fops = {
6705 .open = tracing_open_generic,
6706 .read = trace_options_core_read,
6707 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006708 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05006709};
6710
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006711struct dentry *trace_create_file(const char *name,
Al Virof4ae40a62011-07-24 04:33:43 -04006712 umode_t mode,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006713 struct dentry *parent,
6714 void *data,
6715 const struct file_operations *fops)
6716{
6717 struct dentry *ret;
6718
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006719 ret = tracefs_create_file(name, mode, parent, data, fops);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006720 if (!ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -07006721 pr_warn("Could not create tracefs '%s' entry\n", name);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006722
6723 return ret;
6724}
6725
6726
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006727static struct dentry *trace_options_init_dentry(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05006728{
6729 struct dentry *d_tracer;
Steven Rostedta8259072009-02-26 22:19:12 -05006730
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006731 if (tr->options)
6732 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05006733
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05006734 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05006735 if (IS_ERR(d_tracer))
Steven Rostedta8259072009-02-26 22:19:12 -05006736 return NULL;
6737
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006738 tr->options = tracefs_create_dir("options", d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006739 if (!tr->options) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07006740 pr_warn("Could not create tracefs directory 'options'\n");
Steven Rostedta8259072009-02-26 22:19:12 -05006741 return NULL;
6742 }
6743
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006744 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05006745}
6746
Steven Rostedt577b7852009-02-26 23:43:05 -05006747static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006748create_trace_option_file(struct trace_array *tr,
6749 struct trace_option_dentry *topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05006750 struct tracer_flags *flags,
6751 struct tracer_opt *opt)
6752{
6753 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05006754
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006755 t_options = trace_options_init_dentry(tr);
Steven Rostedt577b7852009-02-26 23:43:05 -05006756 if (!t_options)
6757 return;
6758
6759 topt->flags = flags;
6760 topt->opt = opt;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006761 topt->tr = tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05006762
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006763 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05006764 &trace_options_fops);
6765
Steven Rostedt577b7852009-02-26 23:43:05 -05006766}
6767
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04006768static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006769create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
Steven Rostedt577b7852009-02-26 23:43:05 -05006770{
6771 struct trace_option_dentry *topts;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04006772 struct trace_options *tr_topts;
Steven Rostedt577b7852009-02-26 23:43:05 -05006773 struct tracer_flags *flags;
6774 struct tracer_opt *opts;
6775 int cnt;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04006776 int i;
Steven Rostedt577b7852009-02-26 23:43:05 -05006777
6778 if (!tracer)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04006779 return;
Steven Rostedt577b7852009-02-26 23:43:05 -05006780
6781 flags = tracer->flags;
6782
6783 if (!flags || !flags->opts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04006784 return;
6785
6786 /*
6787 * If this is an instance, only create flags for tracers
6788 * the instance may have.
6789 */
6790 if (!trace_ok_for_array(tracer, tr))
6791 return;
6792
6793 for (i = 0; i < tr->nr_topts; i++) {
Chunyu Hud39cdd22016-03-08 21:37:01 +08006794 /* Make sure there's no duplicate flags. */
6795 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04006796 return;
6797 }
Steven Rostedt577b7852009-02-26 23:43:05 -05006798
6799 opts = flags->opts;
6800
6801 for (cnt = 0; opts[cnt].name; cnt++)
6802 ;
6803
Steven Rostedt0cfe8242009-02-27 10:51:10 -05006804 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05006805 if (!topts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04006806 return;
6807
6808 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
6809 GFP_KERNEL);
6810 if (!tr_topts) {
6811 kfree(topts);
6812 return;
6813 }
6814
6815 tr->topts = tr_topts;
6816 tr->topts[tr->nr_topts].tracer = tracer;
6817 tr->topts[tr->nr_topts].topts = topts;
6818 tr->nr_topts++;
Steven Rostedt577b7852009-02-26 23:43:05 -05006819
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04006820 for (cnt = 0; opts[cnt].name; cnt++) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006821 create_trace_option_file(tr, &topts[cnt], flags,
Steven Rostedt577b7852009-02-26 23:43:05 -05006822 &opts[cnt]);
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04006823 WARN_ONCE(topts[cnt].entry == NULL,
6824 "Failed to create trace option: %s",
6825 opts[cnt].name);
6826 }
Steven Rostedt577b7852009-02-26 23:43:05 -05006827}
6828
Steven Rostedta8259072009-02-26 22:19:12 -05006829static struct dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006830create_trace_option_core_file(struct trace_array *tr,
6831 const char *option, long index)
Steven Rostedta8259072009-02-26 22:19:12 -05006832{
6833 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05006834
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006835 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05006836 if (!t_options)
6837 return NULL;
6838
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006839 return trace_create_file(option, 0644, t_options,
6840 (void *)&tr->trace_flags_index[index],
6841 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05006842}
6843
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04006844static void create_trace_options_dir(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05006845{
6846 struct dentry *t_options;
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04006847 bool top_level = tr == &global_trace;
Steven Rostedta8259072009-02-26 22:19:12 -05006848 int i;
6849
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006850 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05006851 if (!t_options)
6852 return;
6853
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04006854 for (i = 0; trace_options[i]; i++) {
6855 if (top_level ||
6856 !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
6857 create_trace_option_core_file(tr, trace_options[i], i);
6858 }
Steven Rostedta8259072009-02-26 22:19:12 -05006859}
6860
Steven Rostedt499e5472012-02-22 15:50:28 -05006861static ssize_t
6862rb_simple_read(struct file *filp, char __user *ubuf,
6863 size_t cnt, loff_t *ppos)
6864{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04006865 struct trace_array *tr = filp->private_data;
Steven Rostedt499e5472012-02-22 15:50:28 -05006866 char buf[64];
6867 int r;
6868
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04006869 r = tracer_tracing_is_on(tr);
Steven Rostedt499e5472012-02-22 15:50:28 -05006870 r = sprintf(buf, "%d\n", r);
6871
6872 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6873}
6874
6875static ssize_t
6876rb_simple_write(struct file *filp, const char __user *ubuf,
6877 size_t cnt, loff_t *ppos)
6878{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04006879 struct trace_array *tr = filp->private_data;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006880 struct ring_buffer *buffer = tr->trace_buffer.buffer;
Steven Rostedt499e5472012-02-22 15:50:28 -05006881 unsigned long val;
6882 int ret;
6883
6884 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6885 if (ret)
6886 return ret;
6887
6888 if (buffer) {
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05006889 mutex_lock(&trace_types_lock);
6890 if (val) {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04006891 tracer_tracing_on(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006892 if (tr->current_trace->start)
6893 tr->current_trace->start(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05006894 } else {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04006895 tracer_tracing_off(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006896 if (tr->current_trace->stop)
6897 tr->current_trace->stop(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05006898 }
6899 mutex_unlock(&trace_types_lock);
Steven Rostedt499e5472012-02-22 15:50:28 -05006900 }
6901
6902 (*ppos)++;
6903
6904 return cnt;
6905}
6906
6907static const struct file_operations rb_simple_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006908 .open = tracing_open_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05006909 .read = rb_simple_read,
6910 .write = rb_simple_write,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006911 .release = tracing_release_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05006912 .llseek = default_llseek,
6913};
6914
Steven Rostedt277ba042012-08-03 16:10:49 -04006915struct dentry *trace_instance_dir;
6916
6917static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006918init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
Steven Rostedt277ba042012-08-03 16:10:49 -04006919
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006920static int
6921allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
Steven Rostedt277ba042012-08-03 16:10:49 -04006922{
6923 enum ring_buffer_flags rb_flags;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006924
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006925 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006926
Steven Rostedt (Red Hat)dced3412014-01-14 10:19:46 -05006927 buf->tr = tr;
6928
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006929 buf->buffer = ring_buffer_alloc(size, rb_flags);
6930 if (!buf->buffer)
6931 return -ENOMEM;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006932
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006933 buf->data = alloc_percpu(struct trace_array_cpu);
6934 if (!buf->data) {
6935 ring_buffer_free(buf->buffer);
6936 return -ENOMEM;
6937 }
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006938
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006939 /* Allocate the first page for all buffers */
6940 set_buffer_entries(&tr->trace_buffer,
6941 ring_buffer_size(tr->trace_buffer.buffer, 0));
6942
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006943 return 0;
6944}
6945
6946static int allocate_trace_buffers(struct trace_array *tr, int size)
6947{
6948 int ret;
6949
6950 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
6951 if (ret)
6952 return ret;
6953
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006954#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006955 ret = allocate_trace_buffer(tr, &tr->max_buffer,
6956 allocate_snapshot ? size : 1);
6957 if (WARN_ON(ret)) {
6958 ring_buffer_free(tr->trace_buffer.buffer);
6959 free_percpu(tr->trace_buffer.data);
6960 return -ENOMEM;
6961 }
6962 tr->allocated_snapshot = allocate_snapshot;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006963
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05006964 /*
6965 * Only the top level trace array gets its snapshot allocated
6966 * from the kernel command line.
6967 */
6968 allocate_snapshot = false;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006969#endif
6970 return 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05006971}
6972
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04006973static void free_trace_buffer(struct trace_buffer *buf)
6974{
6975 if (buf->buffer) {
6976 ring_buffer_free(buf->buffer);
6977 buf->buffer = NULL;
6978 free_percpu(buf->data);
6979 buf->data = NULL;
6980 }
6981}
6982
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04006983static void free_trace_buffers(struct trace_array *tr)
6984{
6985 if (!tr)
6986 return;
6987
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04006988 free_trace_buffer(&tr->trace_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04006989
6990#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04006991 free_trace_buffer(&tr->max_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04006992#endif
6993}
6994
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04006995static void init_trace_flags_index(struct trace_array *tr)
6996{
6997 int i;
6998
6999 /* Used by the trace options files */
7000 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
7001 tr->trace_flags_index[i] = i;
7002}
7003
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007004static void __update_tracer_options(struct trace_array *tr)
7005{
7006 struct tracer *t;
7007
7008 for (t = trace_types; t; t = t->next)
7009 add_tracer_options(tr, t);
7010}
7011
7012static void update_tracer_options(struct trace_array *tr)
7013{
7014 mutex_lock(&trace_types_lock);
7015 __update_tracer_options(tr);
7016 mutex_unlock(&trace_types_lock);
7017}
7018
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05007019static int instance_mkdir(const char *name)
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007020{
Steven Rostedt277ba042012-08-03 16:10:49 -04007021 struct trace_array *tr;
7022 int ret;
Steven Rostedt277ba042012-08-03 16:10:49 -04007023
7024 mutex_lock(&trace_types_lock);
7025
7026 ret = -EEXIST;
7027 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7028 if (tr->name && strcmp(tr->name, name) == 0)
7029 goto out_unlock;
7030 }
7031
7032 ret = -ENOMEM;
7033 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
7034 if (!tr)
7035 goto out_unlock;
7036
7037 tr->name = kstrdup(name, GFP_KERNEL);
7038 if (!tr->name)
7039 goto out_free_tr;
7040
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007041 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
7042 goto out_free_tr;
7043
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04007044 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007045
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007046 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
7047
Steven Rostedt277ba042012-08-03 16:10:49 -04007048 raw_spin_lock_init(&tr->start_lock);
7049
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05007050 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
7051
Steven Rostedt277ba042012-08-03 16:10:49 -04007052 tr->current_trace = &nop_trace;
7053
7054 INIT_LIST_HEAD(&tr->systems);
7055 INIT_LIST_HEAD(&tr->events);
7056
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007057 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
Steven Rostedt277ba042012-08-03 16:10:49 -04007058 goto out_free_tr;
7059
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007060 tr->dir = tracefs_create_dir(name, trace_instance_dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04007061 if (!tr->dir)
7062 goto out_free_tr;
7063
7064 ret = event_trace_add_tracer(tr->dir, tr);
Alexander Z Lam609e85a2013-07-10 17:34:34 -07007065 if (ret) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007066 tracefs_remove_recursive(tr->dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04007067 goto out_free_tr;
Alexander Z Lam609e85a2013-07-10 17:34:34 -07007068 }
Steven Rostedt277ba042012-08-03 16:10:49 -04007069
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007070 init_tracer_tracefs(tr, tr->dir);
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007071 init_trace_flags_index(tr);
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007072 __update_tracer_options(tr);
Steven Rostedt277ba042012-08-03 16:10:49 -04007073
7074 list_add(&tr->list, &ftrace_trace_arrays);
7075
7076 mutex_unlock(&trace_types_lock);
7077
7078 return 0;
7079
7080 out_free_tr:
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04007081 free_trace_buffers(tr);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007082 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt277ba042012-08-03 16:10:49 -04007083 kfree(tr->name);
7084 kfree(tr);
7085
7086 out_unlock:
7087 mutex_unlock(&trace_types_lock);
7088
7089 return ret;
7090
7091}
7092
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05007093static int instance_rmdir(const char *name)
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007094{
7095 struct trace_array *tr;
7096 int found = 0;
7097 int ret;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007098 int i;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007099
7100 mutex_lock(&trace_types_lock);
7101
7102 ret = -ENODEV;
7103 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7104 if (tr->name && strcmp(tr->name, name) == 0) {
7105 found = 1;
7106 break;
7107 }
7108 }
7109 if (!found)
7110 goto out_unlock;
7111
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007112 ret = -EBUSY;
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05007113 if (tr->ref || (tr->current_trace && tr->current_trace->ref))
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007114 goto out_unlock;
7115
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007116 list_del(&tr->list);
7117
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04007118 /* Disable all the flags that were enabled coming in */
7119 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
7120 if ((1 << i) & ZEROED_TRACE_FLAGS)
7121 set_tracer_flag(tr, 1 << i, 0);
7122 }
7123
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05007124 tracing_set_nop(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007125 event_trace_del_tracer(tr);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05007126 ftrace_destroy_function_files(tr);
Jiaxing Wang681a4a22015-10-18 19:58:08 +08007127 tracefs_remove_recursive(tr->dir);
Steven Rostedt (Red Hat)a9fcaaa2014-06-06 23:17:28 -04007128 free_trace_buffers(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007129
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007130 for (i = 0; i < tr->nr_topts; i++) {
7131 kfree(tr->topts[i].topts);
7132 }
7133 kfree(tr->topts);
7134
Steven Rostedt0c8916c2012-08-07 16:14:16 -04007135 kfree(tr->name);
7136 kfree(tr);
7137
7138 ret = 0;
7139
7140 out_unlock:
7141 mutex_unlock(&trace_types_lock);
7142
7143 return ret;
7144}
7145
Steven Rostedt277ba042012-08-03 16:10:49 -04007146static __init void create_trace_instances(struct dentry *d_tracer)
7147{
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05007148 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
7149 instance_mkdir,
7150 instance_rmdir);
Steven Rostedt277ba042012-08-03 16:10:49 -04007151 if (WARN_ON(!trace_instance_dir))
7152 return;
Steven Rostedt277ba042012-08-03 16:10:49 -04007153}
7154
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007155static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007156init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007157{
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05007158 int cpu;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007159
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05007160 trace_create_file("available_tracers", 0444, d_tracer,
7161 tr, &show_traces_fops);
7162
7163 trace_create_file("current_tracer", 0644, d_tracer,
7164 tr, &set_tracer_fops);
7165
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007166 trace_create_file("tracing_cpumask", 0644, d_tracer,
7167 tr, &tracing_cpumask_fops);
7168
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007169 trace_create_file("trace_options", 0644, d_tracer,
7170 tr, &tracing_iter_fops);
7171
7172 trace_create_file("trace", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007173 tr, &tracing_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007174
7175 trace_create_file("trace_pipe", 0444, d_tracer,
Oleg Nesterov15544202013-07-23 17:25:57 +02007176 tr, &tracing_pipe_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007177
7178 trace_create_file("buffer_size_kb", 0644, d_tracer,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02007179 tr, &tracing_entries_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007180
7181 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
7182 tr, &tracing_total_entries_fops);
7183
Wang YanQing238ae932013-05-26 16:52:01 +08007184 trace_create_file("free_buffer", 0200, d_tracer,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007185 tr, &tracing_free_buffer_fops);
7186
7187 trace_create_file("trace_marker", 0220, d_tracer,
7188 tr, &tracing_mark_fops);
7189
7190 trace_create_file("trace_clock", 0644, d_tracer, tr,
7191 &trace_clock_fops);
7192
7193 trace_create_file("tracing_on", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007194 tr, &rb_simple_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05007195
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04007196 create_trace_options_dir(tr);
7197
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05007198#ifdef CONFIG_TRACER_MAX_TRACE
7199 trace_create_file("tracing_max_latency", 0644, d_tracer,
7200 &tr->max_latency, &tracing_max_lat_fops);
7201#endif
7202
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05007203 if (ftrace_create_function_files(tr, d_tracer))
7204 WARN(1, "Could not allocate function filter files");
7205
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05007206#ifdef CONFIG_TRACER_SNAPSHOT
7207 trace_create_file("snapshot", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007208 tr, &snapshot_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05007209#endif
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05007210
7211 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007212 tracing_init_tracefs_percpu(tr, cpu);
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05007213
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007214 ftrace_init_tracefs(tr, d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007215}
7216
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05007217static struct vfsmount *trace_automount(void *ingore)
7218{
7219 struct vfsmount *mnt;
7220 struct file_system_type *type;
7221
7222 /*
7223 * To maintain backward compatibility for tools that mount
7224 * debugfs to get to the tracing facility, tracefs is automatically
7225 * mounted to the debugfs/tracing directory.
7226 */
7227 type = get_fs_type("tracefs");
7228 if (!type)
7229 return NULL;
7230 mnt = vfs_kern_mount(type, 0, "tracefs", NULL);
7231 put_filesystem(type);
7232 if (IS_ERR(mnt))
7233 return NULL;
7234 mntget(mnt);
7235
7236 return mnt;
7237}
7238
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007239/**
7240 * tracing_init_dentry - initialize top level trace array
7241 *
7242 * This is called when creating files or directories in the tracing
7243 * directory. It is called via fs_initcall() by any of the boot up code
7244 * and expects to return the dentry of the top level tracing directory.
7245 */
7246struct dentry *tracing_init_dentry(void)
7247{
7248 struct trace_array *tr = &global_trace;
7249
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05007250 /* The top level trace array uses NULL as parent */
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007251 if (tr->dir)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05007252 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007253
Jiaxing Wang8b129192015-11-06 16:04:16 +08007254 if (WARN_ON(!tracefs_initialized()) ||
7255 (IS_ENABLED(CONFIG_DEBUG_FS) &&
7256 WARN_ON(!debugfs_initialized())))
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007257 return ERR_PTR(-ENODEV);
7258
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05007259 /*
7260 * As there may still be users that expect the tracing
7261 * files to exist in debugfs/tracing, we must automount
7262 * the tracefs file system there, so older tools still
7263 * work with the newer kerenl.
7264 */
7265 tr->dir = debugfs_create_automount("tracing", NULL,
7266 trace_automount, NULL);
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007267 if (!tr->dir) {
7268 pr_warn_once("Could not create debugfs directory 'tracing'\n");
7269 return ERR_PTR(-ENOMEM);
7270 }
7271
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007272 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007273}
7274
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04007275extern struct trace_enum_map *__start_ftrace_enum_maps[];
7276extern struct trace_enum_map *__stop_ftrace_enum_maps[];
7277
7278static void __init trace_enum_init(void)
7279{
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007280 int len;
7281
7282 len = __stop_ftrace_enum_maps - __start_ftrace_enum_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007283 trace_insert_enum_map(NULL, __start_ftrace_enum_maps, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04007284}
7285
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007286#ifdef CONFIG_MODULES
7287static void trace_module_add_enums(struct module *mod)
7288{
7289 if (!mod->num_trace_enums)
7290 return;
7291
7292 /*
7293 * Modules with bad taint do not have events created, do
7294 * not bother with enums either.
7295 */
7296 if (trace_module_has_bad_taint(mod))
7297 return;
7298
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007299 trace_insert_enum_map(mod, mod->trace_enums, mod->num_trace_enums);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007300}
7301
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007302#ifdef CONFIG_TRACE_ENUM_MAP_FILE
7303static void trace_module_remove_enums(struct module *mod)
7304{
7305 union trace_enum_map_item *map;
7306 union trace_enum_map_item **last = &trace_enum_maps;
7307
7308 if (!mod->num_trace_enums)
7309 return;
7310
7311 mutex_lock(&trace_enum_mutex);
7312
7313 map = trace_enum_maps;
7314
7315 while (map) {
7316 if (map->head.mod == mod)
7317 break;
7318 map = trace_enum_jmp_to_tail(map);
7319 last = &map->tail.next;
7320 map = map->tail.next;
7321 }
7322 if (!map)
7323 goto out;
7324
7325 *last = trace_enum_jmp_to_tail(map)->tail.next;
7326 kfree(map);
7327 out:
7328 mutex_unlock(&trace_enum_mutex);
7329}
7330#else
7331static inline void trace_module_remove_enums(struct module *mod) { }
7332#endif /* CONFIG_TRACE_ENUM_MAP_FILE */
7333
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007334static int trace_module_notify(struct notifier_block *self,
7335 unsigned long val, void *data)
7336{
7337 struct module *mod = data;
7338
7339 switch (val) {
7340 case MODULE_STATE_COMING:
7341 trace_module_add_enums(mod);
7342 break;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007343 case MODULE_STATE_GOING:
7344 trace_module_remove_enums(mod);
7345 break;
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007346 }
7347
7348 return 0;
7349}
7350
7351static struct notifier_block trace_module_nb = {
7352 .notifier_call = trace_module_notify,
7353 .priority = 0,
7354};
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007355#endif /* CONFIG_MODULES */
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007356
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007357static __init int tracer_init_tracefs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007358{
7359 struct dentry *d_tracer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007360
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08007361 trace_access_lock_init();
7362
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007363 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05007364 if (IS_ERR(d_tracer))
Namhyung Kimed6f1c92013-04-10 09:18:12 +09007365 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007366
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007367 init_tracer_tracefs(&global_trace, d_tracer);
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04007368 ftrace_init_tracefs_toplevel(&global_trace, d_tracer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007369
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007370 trace_create_file("tracing_thresh", 0644, d_tracer,
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04007371 &global_trace, &tracing_thresh_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007372
Li Zefan339ae5d2009-04-17 10:34:30 +08007373 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007374 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02007375
Avadh Patel69abe6a2009-04-10 16:04:48 -04007376 trace_create_file("saved_cmdlines", 0444, d_tracer,
7377 NULL, &tracing_saved_cmdlines_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03007378
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09007379 trace_create_file("saved_cmdlines_size", 0644, d_tracer,
7380 NULL, &tracing_saved_cmdlines_size_fops);
7381
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04007382 trace_enum_init();
7383
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04007384 trace_create_enum_file(d_tracer);
7385
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04007386#ifdef CONFIG_MODULES
7387 register_module_notifier(&trace_module_nb);
7388#endif
7389
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007390#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007391 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
7392 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007393#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007394
Steven Rostedt277ba042012-08-03 16:10:49 -04007395 create_trace_instances(d_tracer);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09007396
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04007397 update_tracer_options(&global_trace);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05007398
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01007399 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007400}
7401
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007402static int trace_panic_handler(struct notifier_block *this,
7403 unsigned long event, void *unused)
7404{
Steven Rostedt944ac422008-10-23 19:26:08 -04007405 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007406 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007407 return NOTIFY_OK;
7408}
7409
7410static struct notifier_block trace_panic_notifier = {
7411 .notifier_call = trace_panic_handler,
7412 .next = NULL,
7413 .priority = 150 /* priority: INT_MAX >= x >= 0 */
7414};
7415
7416static int trace_die_handler(struct notifier_block *self,
7417 unsigned long val,
7418 void *data)
7419{
7420 switch (val) {
7421 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04007422 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007423 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007424 break;
7425 default:
7426 break;
7427 }
7428 return NOTIFY_OK;
7429}
7430
7431static struct notifier_block trace_die_notifier = {
7432 .notifier_call = trace_die_handler,
7433 .priority = 200
7434};
7435
7436/*
7437 * printk is set to max of 1024, we really don't need it that big.
7438 * Nothing should be printing 1000 characters anyway.
7439 */
7440#define TRACE_MAX_PRINT 1000
7441
7442/*
7443 * Define here KERN_TRACE so that we have one place to modify
7444 * it if we decide to change what log level the ftrace dump
7445 * should be at.
7446 */
Steven Rostedt428aee12009-01-14 12:24:42 -05007447#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007448
Jason Wessel955b61e2010-08-05 09:22:23 -05007449void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007450trace_printk_seq(struct trace_seq *s)
7451{
7452 /* Probably should print a warning here. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04007453 if (s->seq.len >= TRACE_MAX_PRINT)
7454 s->seq.len = TRACE_MAX_PRINT;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007455
Steven Rostedt (Red Hat)820b75f2014-11-19 10:56:41 -05007456 /*
7457 * More paranoid code. Although the buffer size is set to
7458 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
7459 * an extra layer of protection.
7460 */
7461 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
7462 s->seq.len = s->seq.size - 1;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007463
7464 /* should be zero ended, but we are paranoid. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04007465 s->buffer[s->seq.len] = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007466
7467 printk(KERN_TRACE "%s", s->buffer);
7468
Steven Rostedtf9520752009-03-02 14:04:40 -05007469 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007470}
7471
Jason Wessel955b61e2010-08-05 09:22:23 -05007472void trace_init_global_iter(struct trace_iterator *iter)
7473{
7474 iter->tr = &global_trace;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007475 iter->trace = iter->tr->current_trace;
Steven Rostedtae3b5092013-01-23 15:22:59 -05007476 iter->cpu_file = RING_BUFFER_ALL_CPUS;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007477 iter->trace_buffer = &global_trace.trace_buffer;
Cody P Schaferb2f974d2013-10-23 11:49:57 -07007478
7479 if (iter->trace && iter->trace->open)
7480 iter->trace->open(iter);
7481
7482 /* Annotate start of buffers if we had overruns */
7483 if (ring_buffer_overruns(iter->trace_buffer->buffer))
7484 iter->iter_flags |= TRACE_FILE_ANNOTATE;
7485
7486 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
7487 if (trace_clocks[iter->tr->clock_id].in_ns)
7488 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
Jason Wessel955b61e2010-08-05 09:22:23 -05007489}
7490
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007491void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007492{
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007493 /* use static because iter can be a bit big for the stack */
7494 static struct trace_iterator iter;
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007495 static atomic_t dump_running;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007496 struct trace_array *tr = &global_trace;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007497 unsigned int old_userobj;
Steven Rostedtd7690412008-10-01 00:29:53 -04007498 unsigned long flags;
7499 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007500
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007501 /* Only allow one dump user at a time. */
7502 if (atomic_inc_return(&dump_running) != 1) {
7503 atomic_dec(&dump_running);
7504 return;
Steven Rostedte0a413f2011-09-29 21:26:16 -04007505 }
7506
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007507 /*
7508 * Always turn off tracing when we dump.
7509 * We don't need to show trace output of what happens
7510 * between multiple crashes.
7511 *
7512 * If the user does a sysrq-z, then they can re-enable
7513 * tracing with echo 1 > tracing_on.
7514 */
7515 tracing_off();
7516
7517 local_irq_save(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007518
Jovi Zhang38dbe0b2013-01-25 18:03:07 +08007519 /* Simulate the iterator */
Jason Wessel955b61e2010-08-05 09:22:23 -05007520 trace_init_global_iter(&iter);
7521
Steven Rostedtd7690412008-10-01 00:29:53 -04007522 for_each_tracing_cpu(cpu) {
Umesh Tiwari5e2d5ef2015-06-22 16:55:06 +05307523 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04007524 }
7525
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007526 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007527
Török Edwinb54d3de2008-11-22 13:28:48 +02007528 /* don't look at user memory in panic mode */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007529 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
Török Edwinb54d3de2008-11-22 13:28:48 +02007530
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007531 switch (oops_dump_mode) {
7532 case DUMP_ALL:
Steven Rostedtae3b5092013-01-23 15:22:59 -05007533 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007534 break;
7535 case DUMP_ORIG:
7536 iter.cpu_file = raw_smp_processor_id();
7537 break;
7538 case DUMP_NONE:
7539 goto out_enable;
7540 default:
7541 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
Steven Rostedtae3b5092013-01-23 15:22:59 -05007542 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007543 }
7544
7545 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007546
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007547 /* Did function tracer already get disabled? */
7548 if (ftrace_is_dead()) {
7549 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
7550 printk("# MAY BE MISSING FUNCTION EVENTS\n");
7551 }
7552
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007553 /*
7554 * We need to stop all tracing on all CPUS to read the
7555 * the next buffer. This is a bit expensive, but is
7556 * not done often. We fill all what we can read,
7557 * and then release the locks again.
7558 */
7559
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007560 while (!trace_empty(&iter)) {
7561
7562 if (!cnt)
7563 printk(KERN_TRACE "---------------------------------\n");
7564
7565 cnt++;
7566
7567 /* reset all but tr, trace, and overruns */
7568 memset(&iter.seq, 0,
7569 sizeof(struct trace_iterator) -
7570 offsetof(struct trace_iterator, seq));
7571 iter.iter_flags |= TRACE_FILE_LAT_FMT;
7572 iter.pos = -1;
7573
Jason Wessel955b61e2010-08-05 09:22:23 -05007574 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08007575 int ret;
7576
7577 ret = print_trace_line(&iter);
7578 if (ret != TRACE_TYPE_NO_CONSUME)
7579 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007580 }
Steven Rostedtb892e5c2012-03-01 22:06:48 -05007581 touch_nmi_watchdog();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007582
7583 trace_printk_seq(&iter.seq);
7584 }
7585
7586 if (!cnt)
7587 printk(KERN_TRACE " (ftrace buffer empty)\n");
7588 else
7589 printk(KERN_TRACE "---------------------------------\n");
7590
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02007591 out_enable:
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04007592 tr->trace_flags |= old_userobj;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007593
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007594 for_each_tracing_cpu(cpu) {
7595 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007596 }
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04007597 atomic_dec(&dump_running);
Steven Rostedtcd891ae2009-04-28 11:39:34 -04007598 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007599}
Paul E. McKenneya8eecf22011-10-02 11:01:15 -07007600EXPORT_SYMBOL_GPL(ftrace_dump);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01007601
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007602__init static int tracer_alloc_buffers(void)
7603{
Steven Rostedt73c51622009-03-11 13:42:01 -04007604 int ring_buf_size;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307605 int ret = -ENOMEM;
7606
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04007607 /*
7608 * Make sure we don't accidently add more trace options
7609 * than we have bits for.
7610 */
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007611 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04007612
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307613 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
7614 goto out;
7615
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007616 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307617 goto out_free_buffer_mask;
7618
Steven Rostedt07d777f2011-09-22 14:01:55 -04007619 /* Only allocate trace_printk buffers if a trace_printk exists */
7620 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
Steven Rostedt81698832012-10-11 10:15:05 -04007621 /* Must be called before global_trace.buffer is allocated */
Steven Rostedt07d777f2011-09-22 14:01:55 -04007622 trace_printk_init_buffers();
7623
Steven Rostedt73c51622009-03-11 13:42:01 -04007624 /* To save memory, keep the ring buffer size to its minimum */
7625 if (ring_buffer_expanded)
7626 ring_buf_size = trace_buf_size;
7627 else
7628 ring_buf_size = 1;
7629
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307630 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007631 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007632
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007633 raw_spin_lock_init(&global_trace.start_lock);
7634
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04007635 /* Used for event triggers */
7636 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
7637 if (!temp_buffer)
7638 goto out_free_cpumask;
7639
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09007640 if (trace_create_savedcmd() < 0)
7641 goto out_free_temp_buffer;
7642
Steven Rostedtab464282008-05-12 21:21:00 +02007643 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05007644 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007645 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
7646 WARN_ON(1);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09007647 goto out_free_savedcmd;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007648 }
Steven Rostedta7603ff2012-08-06 16:24:11 -04007649
Steven Rostedt499e5472012-02-22 15:50:28 -05007650 if (global_trace.buffer_disabled)
7651 tracing_off();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007652
Steven Rostedte1e232c2014-02-10 23:38:46 -05007653 if (trace_boot_clock) {
7654 ret = tracing_set_clock(&global_trace, trace_boot_clock);
7655 if (ret < 0)
Joe Perchesa395d6a2016-03-22 14:28:09 -07007656 pr_warn("Trace clock %s not defined, going back to default\n",
7657 trace_boot_clock);
Steven Rostedte1e232c2014-02-10 23:38:46 -05007658 }
7659
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04007660 /*
7661 * register_tracer() might reference current_trace, so it
7662 * needs to be set before we register anything. This is
7663 * just a bootstrap of current_trace anyway.
7664 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007665 global_trace.current_trace = &nop_trace;
7666
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05007667 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
7668
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007669 ftrace_init_global_array_ops(&global_trace);
7670
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04007671 init_trace_flags_index(&global_trace);
7672
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04007673 register_tracer(&nop_trace);
7674
Steven Rostedt60a11772008-05-12 21:20:44 +02007675 /* All seems OK, enable tracing */
7676 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04007677
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007678 atomic_notifier_chain_register(&panic_notifier_list,
7679 &trace_panic_notifier);
7680
7681 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01007682
Steven Rostedtae63b31e2012-05-03 23:09:03 -04007683 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
7684
7685 INIT_LIST_HEAD(&global_trace.systems);
7686 INIT_LIST_HEAD(&global_trace.events);
7687 list_add(&global_trace.list, &ftrace_trace_arrays);
7688
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08007689 apply_trace_boot_options();
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04007690
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007691 register_snapshot_cmd();
7692
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01007693 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04007694
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09007695out_free_savedcmd:
7696 free_saved_cmdlines_buffer(savedcmd);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04007697out_free_temp_buffer:
7698 ring_buffer_free(temp_buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307699out_free_cpumask:
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07007700 free_cpumask_var(global_trace.tracing_cpumask);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10307701out_free_buffer_mask:
7702 free_cpumask_var(tracing_buffer_mask);
7703out:
7704 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007705}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05007706
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05007707void __init trace_init(void)
7708{
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05007709 if (tracepoint_printk) {
7710 tracepoint_print_iter =
7711 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
7712 if (WARN_ON(!tracepoint_print_iter))
7713 tracepoint_printk = 0;
7714 }
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05007715 tracer_alloc_buffers();
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04007716 trace_event_init();
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05007717}
7718
Steven Rostedtb2821ae2009-02-02 21:38:32 -05007719__init static int clear_boot_tracer(void)
7720{
7721 /*
7722 * The default tracer at boot buffer is an init section.
7723 * This function is called in lateinit. If we did not
7724 * find the boot tracer, then clear it out, to prevent
7725 * later registration from accessing the buffer that is
7726 * about to be freed.
7727 */
7728 if (!default_bootup_tracer)
7729 return 0;
7730
7731 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
7732 default_bootup_tracer);
7733 default_bootup_tracer = NULL;
7734
7735 return 0;
7736}
7737
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007738fs_initcall(tracer_init_tracefs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05007739late_initcall(clear_boot_tracer);