Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ring buffer based function tracer |
| 3 | * |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4 | * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5 | * 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 Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 12 | * Copyright (C) 2004 Nadia Yvette Chambers |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 13 | */ |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 14 | #include <linux/ring_buffer.h> |
Sam Ravnborg | 273b281 | 2009-10-18 00:52:28 +0200 | [diff] [blame] | 15 | #include <generated/utsrelease.h> |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 16 | #include <linux/stacktrace.h> |
| 17 | #include <linux/writeback.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 18 | #include <linux/kallsyms.h> |
| 19 | #include <linux/seq_file.h> |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 20 | #include <linux/notifier.h> |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 21 | #include <linux/irqflags.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 22 | #include <linux/debugfs.h> |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 23 | #include <linux/tracefs.h> |
Steven Rostedt | 4c11d7a | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 24 | #include <linux/pagemap.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 25 | #include <linux/hardirq.h> |
| 26 | #include <linux/linkage.h> |
| 27 | #include <linux/uaccess.h> |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 28 | #include <linux/vmalloc.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 29 | #include <linux/ftrace.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/percpu.h> |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 32 | #include <linux/splice.h> |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 33 | #include <linux/kdebug.h> |
Frederic Weisbecker | 5f0c6c0 | 2009-03-27 14:22:10 +0100 | [diff] [blame] | 34 | #include <linux/string.h> |
Steven Rostedt (Red Hat) | f76180b | 2015-01-20 15:48:46 -0500 | [diff] [blame] | 35 | #include <linux/mount.h> |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 36 | #include <linux/rwsem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 38 | #include <linux/ctype.h> |
| 39 | #include <linux/init.h> |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 40 | #include <linux/poll.h> |
Steven Rostedt | b892e5c | 2012-03-01 22:06:48 -0500 | [diff] [blame] | 41 | #include <linux/nmi.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 42 | #include <linux/fs.h> |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 43 | #include <linux/trace.h> |
Clark Williams | 8bd75c7 | 2013-02-07 09:47:07 -0600 | [diff] [blame] | 44 | #include <linux/sched/rt.h> |
Ingo Molnar | 86387f7 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 45 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 46 | #include "trace.h" |
Steven Rostedt | f0868d1 | 2008-12-23 23:24:12 -0500 | [diff] [blame] | 47 | #include "trace_output.h" |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 48 | |
Frederic Weisbecker | 8e1b82e | 2008-12-06 03:41:33 +0100 | [diff] [blame] | 49 | /* |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 50 | * On boot up, the ring buffer is set to the minimum size, so that |
| 51 | * we do not waste memory on systems that are not using tracing. |
| 52 | */ |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 53 | bool ring_buffer_expanded; |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 54 | |
| 55 | /* |
Frederic Weisbecker | 8e1b82e | 2008-12-06 03:41:33 +0100 | [diff] [blame] | 56 | * We need to change this state when a selftest is running. |
Frederic Weisbecker | ff32504 | 2008-12-04 23:47:35 +0100 | [diff] [blame] | 57 | * A selftest will lurk into the ring-buffer to count the |
| 58 | * entries inserted during the selftest although some concurrent |
Ingo Molnar | 5e1607a | 2009-03-05 10:24:48 +0100 | [diff] [blame] | 59 | * insertions into the ring-buffer such as trace_printk could occurred |
Frederic Weisbecker | ff32504 | 2008-12-04 23:47:35 +0100 | [diff] [blame] | 60 | * at the same time, giving false positive or negative results. |
| 61 | */ |
Frederic Weisbecker | 8e1b82e | 2008-12-06 03:41:33 +0100 | [diff] [blame] | 62 | static bool __read_mostly tracing_selftest_running; |
Frederic Weisbecker | ff32504 | 2008-12-04 23:47:35 +0100 | [diff] [blame] | 63 | |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 64 | /* |
| 65 | * If a tracer is running, we do not want to run SELFTEST. |
| 66 | */ |
Li Zefan | 020e5f8 | 2009-07-01 10:47:05 +0800 | [diff] [blame] | 67 | bool __read_mostly tracing_selftest_disabled; |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 68 | |
Steven Rostedt (Red Hat) | 0daa2302 | 2014-12-12 22:27:10 -0500 | [diff] [blame] | 69 | /* Pipe tracepoints to printk */ |
| 70 | struct trace_iterator *tracepoint_print_iter; |
| 71 | int tracepoint_printk; |
Steven Rostedt (Red Hat) | 42391745 | 2016-11-23 15:52:45 -0500 | [diff] [blame] | 72 | static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key); |
Steven Rostedt (Red Hat) | 0daa2302 | 2014-12-12 22:27:10 -0500 | [diff] [blame] | 73 | |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 74 | /* For tracers that don't implement custom flags */ |
| 75 | static struct tracer_opt dummy_tracer_opt[] = { |
| 76 | { } |
| 77 | }; |
| 78 | |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 79 | static int |
| 80 | dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set) |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 81 | { |
| 82 | return 0; |
| 83 | } |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 84 | |
| 85 | /* |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 86 | * To prevent the comm cache from being overwritten when no |
| 87 | * tracing is active, only save the comm when a trace event |
| 88 | * occurred. |
| 89 | */ |
| 90 | static DEFINE_PER_CPU(bool, trace_cmdline_save); |
| 91 | |
| 92 | /* |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 93 | * Kill all tracing for good (never come back). |
| 94 | * It is initialized to 1 but will turn to zero if the initialization |
| 95 | * of the tracer is successful. But that is the only place that sets |
| 96 | * this back to zero. |
| 97 | */ |
Hannes Eder | 4fd2735 | 2009-02-10 19:44:12 +0100 | [diff] [blame] | 98 | static int tracing_disabled = 1; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 99 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 100 | cpumask_var_t __read_mostly tracing_buffer_mask; |
Steven Rostedt | ab46428 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 101 | |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 102 | /* |
| 103 | * ftrace_dump_on_oops - variable to dump ftrace buffer on oops |
| 104 | * |
| 105 | * If there is an oops (or kernel panic) and the ftrace_dump_on_oops |
| 106 | * is set, then ftrace_dump is called. This will output the contents |
| 107 | * of the ftrace buffers to the console. This is very useful for |
| 108 | * capturing traces that lead to crashes and outputing it to a |
| 109 | * serial console. |
| 110 | * |
| 111 | * It is default off, but you can enable it with either specifying |
| 112 | * "ftrace_dump_on_oops" in the kernel command line, or setting |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 113 | * /proc/sys/kernel/ftrace_dump_on_oops |
| 114 | * Set 1 if you want to dump buffers of all CPUs |
| 115 | * Set 2 if you want to dump the buffer of the CPU that triggered oops |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 116 | */ |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 117 | |
| 118 | enum ftrace_dump_mode ftrace_dump_on_oops; |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 119 | |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 120 | /* When set, tracing will stop when a WARN*() is hit */ |
| 121 | int __disable_trace_on_warning; |
| 122 | |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 123 | #ifdef CONFIG_TRACE_ENUM_MAP_FILE |
| 124 | /* Map of enums to their values, for "enum_map" file */ |
| 125 | struct trace_enum_map_head { |
| 126 | struct module *mod; |
| 127 | unsigned long length; |
| 128 | }; |
| 129 | |
| 130 | union trace_enum_map_item; |
| 131 | |
| 132 | struct trace_enum_map_tail { |
| 133 | /* |
| 134 | * "end" is first and points to NULL as it must be different |
| 135 | * than "mod" or "enum_string" |
| 136 | */ |
| 137 | union trace_enum_map_item *next; |
| 138 | const char *end; /* points to NULL */ |
| 139 | }; |
| 140 | |
| 141 | static DEFINE_MUTEX(trace_enum_mutex); |
| 142 | |
| 143 | /* |
| 144 | * The trace_enum_maps are saved in an array with two extra elements, |
| 145 | * one at the beginning, and one at the end. The beginning item contains |
| 146 | * the count of the saved maps (head.length), and the module they |
| 147 | * belong to if not built in (head.mod). The ending item contains a |
| 148 | * pointer to the next array of saved enum_map items. |
| 149 | */ |
| 150 | union trace_enum_map_item { |
| 151 | struct trace_enum_map map; |
| 152 | struct trace_enum_map_head head; |
| 153 | struct trace_enum_map_tail tail; |
| 154 | }; |
| 155 | |
| 156 | static union trace_enum_map_item *trace_enum_maps; |
| 157 | #endif /* CONFIG_TRACE_ENUM_MAP_FILE */ |
| 158 | |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 159 | static int tracing_set_tracer(struct trace_array *tr, const char *buf); |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 160 | |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 161 | #define MAX_TRACER_SIZE 100 |
| 162 | static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata; |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 163 | static char *default_bootup_tracer; |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 164 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 165 | static bool allocate_snapshot; |
| 166 | |
Frederic Weisbecker | 1beee96 | 2009-10-14 20:50:32 +0200 | [diff] [blame] | 167 | static int __init set_cmdline_ftrace(char *str) |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 168 | { |
Chen Gang | 67012ab | 2013-04-08 12:06:44 +0800 | [diff] [blame] | 169 | strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE); |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 170 | default_bootup_tracer = bootup_tracer_buf; |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 171 | /* We are using ftrace early, expand it */ |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 172 | ring_buffer_expanded = true; |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 173 | return 1; |
| 174 | } |
Frederic Weisbecker | 1beee96 | 2009-10-14 20:50:32 +0200 | [diff] [blame] | 175 | __setup("ftrace=", set_cmdline_ftrace); |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 176 | |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 177 | static int __init set_ftrace_dump_on_oops(char *str) |
| 178 | { |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 179 | if (*str++ != '=' || !*str) { |
| 180 | ftrace_dump_on_oops = DUMP_ALL; |
| 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | if (!strcmp("orig_cpu", str)) { |
| 185 | ftrace_dump_on_oops = DUMP_ORIG; |
| 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | return 0; |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 190 | } |
| 191 | __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops); |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 192 | |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 193 | static int __init stop_trace_on_warning(char *str) |
| 194 | { |
Luis Claudio R. Goncalves | 933ff9f | 2014-11-12 21:14:00 -0200 | [diff] [blame] | 195 | if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0)) |
| 196 | __disable_trace_on_warning = 1; |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 197 | return 1; |
| 198 | } |
Luis Claudio R. Goncalves | 933ff9f | 2014-11-12 21:14:00 -0200 | [diff] [blame] | 199 | __setup("traceoff_on_warning", stop_trace_on_warning); |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 200 | |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 201 | static int __init boot_alloc_snapshot(char *str) |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 202 | { |
| 203 | allocate_snapshot = true; |
| 204 | /* We also need the main ring buffer expanded */ |
| 205 | ring_buffer_expanded = true; |
| 206 | return 1; |
| 207 | } |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 208 | __setup("alloc_snapshot", boot_alloc_snapshot); |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 209 | |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 210 | |
| 211 | static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata; |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 212 | |
| 213 | static int __init set_trace_boot_options(char *str) |
| 214 | { |
Chen Gang | 67012ab | 2013-04-08 12:06:44 +0800 | [diff] [blame] | 215 | strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE); |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | __setup("trace_options=", set_trace_boot_options); |
| 219 | |
Steven Rostedt | e1e232c | 2014-02-10 23:38:46 -0500 | [diff] [blame] | 220 | static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata; |
| 221 | static char *trace_boot_clock __initdata; |
| 222 | |
| 223 | static int __init set_trace_boot_clock(char *str) |
| 224 | { |
| 225 | strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE); |
| 226 | trace_boot_clock = trace_boot_clock_buf; |
| 227 | return 0; |
| 228 | } |
| 229 | __setup("trace_clock=", set_trace_boot_clock); |
| 230 | |
Steven Rostedt (Red Hat) | 0daa2302 | 2014-12-12 22:27:10 -0500 | [diff] [blame] | 231 | static int __init set_tracepoint_printk(char *str) |
| 232 | { |
| 233 | if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0)) |
| 234 | tracepoint_printk = 1; |
| 235 | return 1; |
| 236 | } |
| 237 | __setup("tp_printk", set_tracepoint_printk); |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 238 | |
Lai Jiangshan | cf8e347 | 2009-03-30 13:48:00 +0800 | [diff] [blame] | 239 | unsigned long long ns2usecs(cycle_t nsec) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 240 | { |
| 241 | nsec += 500; |
| 242 | do_div(nsec, 1000); |
| 243 | return nsec; |
| 244 | } |
| 245 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 246 | /* trace_flags holds trace_options default values */ |
| 247 | #define TRACE_DEFAULT_FLAGS \ |
| 248 | (FUNCTION_DEFAULT_FLAGS | \ |
| 249 | TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \ |
| 250 | TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \ |
| 251 | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \ |
| 252 | TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS) |
| 253 | |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 254 | /* trace_options that are only supported by global_trace */ |
| 255 | #define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \ |
| 256 | TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD) |
| 257 | |
Steven Rostedt (Red Hat) | 2055062 | 2016-04-25 22:40:12 -0400 | [diff] [blame] | 258 | /* trace_flags that are default zero for instances */ |
| 259 | #define ZEROED_TRACE_FLAGS \ |
| 260 | TRACE_ITER_EVENT_FORK |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 261 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 262 | /* |
| 263 | * The global_trace is the descriptor that holds the tracing |
| 264 | * buffers for the live tracing. For each CPU, it contains |
| 265 | * a link list of pages that will store trace entries. The |
| 266 | * page descriptor of the pages in the memory is used to hold |
| 267 | * the link list by linking the lru item in the page descriptor |
| 268 | * to each of the pages in the buffer per CPU. |
| 269 | * |
| 270 | * For each active CPU there is a data field that holds the |
| 271 | * pages for the buffer for that CPU. Each CPU has the same number |
| 272 | * of pages allocated for its buffer. |
| 273 | */ |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 274 | static struct trace_array global_trace = { |
| 275 | .trace_flags = TRACE_DEFAULT_FLAGS, |
| 276 | }; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 277 | |
Steven Rostedt | ae63b31e | 2012-05-03 23:09:03 -0400 | [diff] [blame] | 278 | LIST_HEAD(ftrace_trace_arrays); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 279 | |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 280 | int trace_array_get(struct trace_array *this_tr) |
| 281 | { |
| 282 | struct trace_array *tr; |
| 283 | int ret = -ENODEV; |
| 284 | |
| 285 | mutex_lock(&trace_types_lock); |
| 286 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 287 | if (tr == this_tr) { |
| 288 | tr->ref++; |
| 289 | ret = 0; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | mutex_unlock(&trace_types_lock); |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | static void __trace_array_put(struct trace_array *this_tr) |
| 299 | { |
| 300 | WARN_ON(!this_tr->ref); |
| 301 | this_tr->ref--; |
| 302 | } |
| 303 | |
| 304 | void trace_array_put(struct trace_array *this_tr) |
| 305 | { |
| 306 | mutex_lock(&trace_types_lock); |
| 307 | __trace_array_put(this_tr); |
| 308 | mutex_unlock(&trace_types_lock); |
| 309 | } |
| 310 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 311 | int call_filter_check_discard(struct trace_event_call *call, void *rec, |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 312 | struct ring_buffer *buffer, |
| 313 | struct ring_buffer_event *event) |
| 314 | { |
| 315 | if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) && |
| 316 | !filter_match_preds(call->filter, rec)) { |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 317 | __trace_event_discard_commit(buffer, event); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
Tom Zanussi | eb02ce0 | 2009-04-08 03:15:54 -0500 | [diff] [blame] | 323 | |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 324 | void trace_free_pid_list(struct trace_pid_list *pid_list) |
| 325 | { |
| 326 | vfree(pid_list->pids); |
| 327 | kfree(pid_list); |
| 328 | } |
| 329 | |
Steven Rostedt | d8275c4 | 2016-04-14 12:15:22 -0400 | [diff] [blame] | 330 | /** |
| 331 | * trace_find_filtered_pid - check if a pid exists in a filtered_pid list |
| 332 | * @filtered_pids: The list of pids to check |
| 333 | * @search_pid: The PID to find in @filtered_pids |
| 334 | * |
| 335 | * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis. |
| 336 | */ |
| 337 | bool |
| 338 | trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid) |
| 339 | { |
| 340 | /* |
| 341 | * If pid_max changed after filtered_pids was created, we |
| 342 | * by default ignore all pids greater than the previous pid_max. |
| 343 | */ |
| 344 | if (search_pid >= filtered_pids->pid_max) |
| 345 | return false; |
| 346 | |
| 347 | return test_bit(search_pid, filtered_pids->pids); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * trace_ignore_this_task - should a task be ignored for tracing |
| 352 | * @filtered_pids: The list of pids to check |
| 353 | * @task: The task that should be ignored if not filtered |
| 354 | * |
| 355 | * Checks if @task should be traced or not from @filtered_pids. |
| 356 | * Returns true if @task should *NOT* be traced. |
| 357 | * Returns false if @task should be traced. |
| 358 | */ |
| 359 | bool |
| 360 | trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task) |
| 361 | { |
| 362 | /* |
| 363 | * Return false, because if filtered_pids does not exist, |
| 364 | * all pids are good to trace. |
| 365 | */ |
| 366 | if (!filtered_pids) |
| 367 | return false; |
| 368 | |
| 369 | return !trace_find_filtered_pid(filtered_pids, task->pid); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * trace_pid_filter_add_remove - Add or remove a task from a pid_list |
| 374 | * @pid_list: The list to modify |
| 375 | * @self: The current task for fork or NULL for exit |
| 376 | * @task: The task to add or remove |
| 377 | * |
| 378 | * If adding a task, if @self is defined, the task is only added if @self |
| 379 | * is also included in @pid_list. This happens on fork and tasks should |
| 380 | * only be added when the parent is listed. If @self is NULL, then the |
| 381 | * @task pid will be removed from the list, which would happen on exit |
| 382 | * of a task. |
| 383 | */ |
| 384 | void trace_filter_add_remove_task(struct trace_pid_list *pid_list, |
| 385 | struct task_struct *self, |
| 386 | struct task_struct *task) |
| 387 | { |
| 388 | if (!pid_list) |
| 389 | return; |
| 390 | |
| 391 | /* For forks, we only add if the forking task is listed */ |
| 392 | if (self) { |
| 393 | if (!trace_find_filtered_pid(pid_list, self->pid)) |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | /* Sorry, but we don't support pid_max changing after setting */ |
| 398 | if (task->pid >= pid_list->pid_max) |
| 399 | return; |
| 400 | |
| 401 | /* "self" is set for forks, and NULL for exits */ |
| 402 | if (self) |
| 403 | set_bit(task->pid, pid_list->pids); |
| 404 | else |
| 405 | clear_bit(task->pid, pid_list->pids); |
| 406 | } |
| 407 | |
Steven Rostedt (Red Hat) | 5cc8976 | 2016-04-20 15:19:54 -0400 | [diff] [blame] | 408 | /** |
| 409 | * trace_pid_next - Used for seq_file to get to the next pid of a pid_list |
| 410 | * @pid_list: The pid list to show |
| 411 | * @v: The last pid that was shown (+1 the actual pid to let zero be displayed) |
| 412 | * @pos: The position of the file |
| 413 | * |
| 414 | * This is used by the seq_file "next" operation to iterate the pids |
| 415 | * listed in a trace_pid_list structure. |
| 416 | * |
| 417 | * Returns the pid+1 as we want to display pid of zero, but NULL would |
| 418 | * stop the iteration. |
| 419 | */ |
| 420 | void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos) |
| 421 | { |
| 422 | unsigned long pid = (unsigned long)v; |
| 423 | |
| 424 | (*pos)++; |
| 425 | |
| 426 | /* pid already is +1 of the actual prevous bit */ |
| 427 | pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid); |
| 428 | |
| 429 | /* Return pid + 1 to allow zero to be represented */ |
| 430 | if (pid < pid_list->pid_max) |
| 431 | return (void *)(pid + 1); |
| 432 | |
| 433 | return NULL; |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * trace_pid_start - Used for seq_file to start reading pid lists |
| 438 | * @pid_list: The pid list to show |
| 439 | * @pos: The position of the file |
| 440 | * |
| 441 | * This is used by seq_file "start" operation to start the iteration |
| 442 | * of listing pids. |
| 443 | * |
| 444 | * Returns the pid+1 as we want to display pid of zero, but NULL would |
| 445 | * stop the iteration. |
| 446 | */ |
| 447 | void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos) |
| 448 | { |
| 449 | unsigned long pid; |
| 450 | loff_t l = 0; |
| 451 | |
| 452 | pid = find_first_bit(pid_list->pids, pid_list->pid_max); |
| 453 | if (pid >= pid_list->pid_max) |
| 454 | return NULL; |
| 455 | |
| 456 | /* Return pid + 1 so that zero can be the exit value */ |
| 457 | for (pid++; pid && l < *pos; |
| 458 | pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l)) |
| 459 | ; |
| 460 | return (void *)pid; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * trace_pid_show - show the current pid in seq_file processing |
| 465 | * @m: The seq_file structure to write into |
| 466 | * @v: A void pointer of the pid (+1) value to display |
| 467 | * |
| 468 | * Can be directly used by seq_file operations to display the current |
| 469 | * pid value. |
| 470 | */ |
| 471 | int trace_pid_show(struct seq_file *m, void *v) |
| 472 | { |
| 473 | unsigned long pid = (unsigned long)v - 1; |
| 474 | |
| 475 | seq_printf(m, "%lu\n", pid); |
| 476 | return 0; |
| 477 | } |
| 478 | |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 479 | /* 128 should be much more than enough */ |
| 480 | #define PID_BUF_SIZE 127 |
| 481 | |
| 482 | int trace_pid_write(struct trace_pid_list *filtered_pids, |
| 483 | struct trace_pid_list **new_pid_list, |
| 484 | const char __user *ubuf, size_t cnt) |
| 485 | { |
| 486 | struct trace_pid_list *pid_list; |
| 487 | struct trace_parser parser; |
| 488 | unsigned long val; |
| 489 | int nr_pids = 0; |
| 490 | ssize_t read = 0; |
| 491 | ssize_t ret = 0; |
| 492 | loff_t pos; |
| 493 | pid_t pid; |
| 494 | |
| 495 | if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1)) |
| 496 | return -ENOMEM; |
| 497 | |
| 498 | /* |
| 499 | * Always recreate a new array. The write is an all or nothing |
| 500 | * operation. Always create a new array when adding new pids by |
| 501 | * the user. If the operation fails, then the current list is |
| 502 | * not modified. |
| 503 | */ |
| 504 | pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL); |
| 505 | if (!pid_list) |
| 506 | return -ENOMEM; |
| 507 | |
| 508 | pid_list->pid_max = READ_ONCE(pid_max); |
| 509 | |
| 510 | /* Only truncating will shrink pid_max */ |
| 511 | if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max) |
| 512 | pid_list->pid_max = filtered_pids->pid_max; |
| 513 | |
| 514 | pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3); |
| 515 | if (!pid_list->pids) { |
| 516 | kfree(pid_list); |
| 517 | return -ENOMEM; |
| 518 | } |
| 519 | |
| 520 | if (filtered_pids) { |
| 521 | /* copy the current bits to the new max */ |
Wei Yongjun | 67f20b0 | 2016-07-04 15:10:04 +0000 | [diff] [blame] | 522 | for_each_set_bit(pid, filtered_pids->pids, |
| 523 | filtered_pids->pid_max) { |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 524 | set_bit(pid, pid_list->pids); |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 525 | nr_pids++; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | while (cnt > 0) { |
| 530 | |
| 531 | pos = 0; |
| 532 | |
| 533 | ret = trace_get_user(&parser, ubuf, cnt, &pos); |
| 534 | if (ret < 0 || !trace_parser_loaded(&parser)) |
| 535 | break; |
| 536 | |
| 537 | read += ret; |
| 538 | ubuf += ret; |
| 539 | cnt -= ret; |
| 540 | |
| 541 | parser.buffer[parser.idx] = 0; |
| 542 | |
| 543 | ret = -EINVAL; |
| 544 | if (kstrtoul(parser.buffer, 0, &val)) |
| 545 | break; |
| 546 | if (val >= pid_list->pid_max) |
| 547 | break; |
| 548 | |
| 549 | pid = (pid_t)val; |
| 550 | |
| 551 | set_bit(pid, pid_list->pids); |
| 552 | nr_pids++; |
| 553 | |
| 554 | trace_parser_clear(&parser); |
| 555 | ret = 0; |
| 556 | } |
| 557 | trace_parser_put(&parser); |
| 558 | |
| 559 | if (ret < 0) { |
| 560 | trace_free_pid_list(pid_list); |
| 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | if (!nr_pids) { |
| 565 | /* Cleared the list of pids */ |
| 566 | trace_free_pid_list(pid_list); |
| 567 | read = ret; |
| 568 | pid_list = NULL; |
| 569 | } |
| 570 | |
| 571 | *new_pid_list = pid_list; |
| 572 | |
| 573 | return read; |
| 574 | } |
| 575 | |
Fabian Frederick | ad1438a | 2014-04-17 21:44:42 +0200 | [diff] [blame] | 576 | static cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu) |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 577 | { |
| 578 | u64 ts; |
| 579 | |
| 580 | /* Early boot up does not have a buffer yet */ |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 581 | if (!buf->buffer) |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 582 | return trace_clock_local(); |
| 583 | |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 584 | ts = ring_buffer_time_stamp(buf->buffer, cpu); |
| 585 | ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts); |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 586 | |
| 587 | return ts; |
| 588 | } |
| 589 | |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 590 | cycle_t ftrace_now(int cpu) |
| 591 | { |
| 592 | return buffer_ftrace_now(&global_trace.trace_buffer, cpu); |
| 593 | } |
| 594 | |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 595 | /** |
| 596 | * tracing_is_enabled - Show if global_trace has been disabled |
| 597 | * |
| 598 | * Shows if the global trace has been enabled or not. It uses the |
| 599 | * mirror flag "buffer_disabled" to be used in fast paths such as for |
| 600 | * the irqsoff tracer. But it may be inaccurate due to races. If you |
| 601 | * need to know the accurate state, use tracing_is_on() which is a little |
| 602 | * slower, but accurate. |
| 603 | */ |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 604 | int tracing_is_enabled(void) |
| 605 | { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 606 | /* |
| 607 | * For quick access (irqsoff uses this in fast path), just |
| 608 | * return the mirror variable of the state of the ring buffer. |
| 609 | * It's a little racy, but we don't really care. |
| 610 | */ |
| 611 | smp_rmb(); |
| 612 | return !global_trace.buffer_disabled; |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 613 | } |
| 614 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 615 | /* |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 616 | * trace_buf_size is the size in bytes that is allocated |
| 617 | * for a buffer. Note, the number of bytes is always rounded |
| 618 | * to page size. |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 619 | * |
| 620 | * This number is purposely set to a low number of 16384. |
| 621 | * If the dump on oops happens, it will be much appreciated |
| 622 | * to not have to wait for all that output. Anyway this can be |
| 623 | * boot time and run time configurable. |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 624 | */ |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 625 | #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */ |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 626 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 627 | static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 628 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 629 | /* trace_types holds a link list of available tracers. */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 630 | static struct tracer *trace_types __read_mostly; |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 631 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 632 | /* |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 633 | * trace_types_lock is used to protect the trace_types list. |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 634 | */ |
Alexander Z Lam | a822741 | 2013-07-01 19:37:54 -0700 | [diff] [blame] | 635 | DEFINE_MUTEX(trace_types_lock); |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 636 | |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 637 | /* |
| 638 | * serialize the access of the ring buffer |
| 639 | * |
| 640 | * ring buffer serializes readers, but it is low level protection. |
| 641 | * The validity of the events (which returns by ring_buffer_peek() ..etc) |
| 642 | * are not protected by ring buffer. |
| 643 | * |
| 644 | * The content of events may become garbage if we allow other process consumes |
| 645 | * these events concurrently: |
| 646 | * A) the page of the consumed events may become a normal page |
| 647 | * (not reader page) in ring buffer, and this page will be rewrited |
| 648 | * by events producer. |
| 649 | * B) The page of the consumed events may become a page for splice_read, |
| 650 | * and this page will be returned to system. |
| 651 | * |
| 652 | * These primitives allow multi process access to different cpu ring buffer |
| 653 | * concurrently. |
| 654 | * |
| 655 | * These primitives don't distinguish read-only and read-consume access. |
| 656 | * Multi read-only access are also serialized. |
| 657 | */ |
| 658 | |
| 659 | #ifdef CONFIG_SMP |
| 660 | static DECLARE_RWSEM(all_cpu_access_lock); |
| 661 | static DEFINE_PER_CPU(struct mutex, cpu_access_lock); |
| 662 | |
| 663 | static inline void trace_access_lock(int cpu) |
| 664 | { |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 665 | if (cpu == RING_BUFFER_ALL_CPUS) { |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 666 | /* gain it for accessing the whole ring buffer. */ |
| 667 | down_write(&all_cpu_access_lock); |
| 668 | } else { |
| 669 | /* gain it for accessing a cpu ring buffer. */ |
| 670 | |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 671 | /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */ |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 672 | down_read(&all_cpu_access_lock); |
| 673 | |
| 674 | /* Secondly block other access to this @cpu ring buffer. */ |
| 675 | mutex_lock(&per_cpu(cpu_access_lock, cpu)); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | static inline void trace_access_unlock(int cpu) |
| 680 | { |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 681 | if (cpu == RING_BUFFER_ALL_CPUS) { |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 682 | up_write(&all_cpu_access_lock); |
| 683 | } else { |
| 684 | mutex_unlock(&per_cpu(cpu_access_lock, cpu)); |
| 685 | up_read(&all_cpu_access_lock); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | static inline void trace_access_lock_init(void) |
| 690 | { |
| 691 | int cpu; |
| 692 | |
| 693 | for_each_possible_cpu(cpu) |
| 694 | mutex_init(&per_cpu(cpu_access_lock, cpu)); |
| 695 | } |
| 696 | |
| 697 | #else |
| 698 | |
| 699 | static DEFINE_MUTEX(access_lock); |
| 700 | |
| 701 | static inline void trace_access_lock(int cpu) |
| 702 | { |
| 703 | (void)cpu; |
| 704 | mutex_lock(&access_lock); |
| 705 | } |
| 706 | |
| 707 | static inline void trace_access_unlock(int cpu) |
| 708 | { |
| 709 | (void)cpu; |
| 710 | mutex_unlock(&access_lock); |
| 711 | } |
| 712 | |
| 713 | static inline void trace_access_lock_init(void) |
| 714 | { |
| 715 | } |
| 716 | |
| 717 | #endif |
| 718 | |
Steven Rostedt (Red Hat) | d78a461 | 2015-09-25 13:30:47 -0400 | [diff] [blame] | 719 | #ifdef CONFIG_STACKTRACE |
| 720 | static void __ftrace_trace_stack(struct ring_buffer *buffer, |
| 721 | unsigned long flags, |
| 722 | int skip, int pc, struct pt_regs *regs); |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 723 | static inline void ftrace_trace_stack(struct trace_array *tr, |
| 724 | struct ring_buffer *buffer, |
Steven Rostedt (Red Hat) | 73dddbb | 2015-09-29 15:38:55 -0400 | [diff] [blame] | 725 | unsigned long flags, |
| 726 | int skip, int pc, struct pt_regs *regs); |
Steven Rostedt (Red Hat) | ca475e8 | 2015-09-28 09:41:11 -0400 | [diff] [blame] | 727 | |
Steven Rostedt (Red Hat) | d78a461 | 2015-09-25 13:30:47 -0400 | [diff] [blame] | 728 | #else |
| 729 | static inline void __ftrace_trace_stack(struct ring_buffer *buffer, |
| 730 | unsigned long flags, |
| 731 | int skip, int pc, struct pt_regs *regs) |
| 732 | { |
| 733 | } |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 734 | static inline void ftrace_trace_stack(struct trace_array *tr, |
| 735 | struct ring_buffer *buffer, |
Steven Rostedt (Red Hat) | 73dddbb | 2015-09-29 15:38:55 -0400 | [diff] [blame] | 736 | unsigned long flags, |
| 737 | int skip, int pc, struct pt_regs *regs) |
Steven Rostedt (Red Hat) | ca475e8 | 2015-09-28 09:41:11 -0400 | [diff] [blame] | 738 | { |
| 739 | } |
| 740 | |
Steven Rostedt (Red Hat) | d78a461 | 2015-09-25 13:30:47 -0400 | [diff] [blame] | 741 | #endif |
| 742 | |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 743 | static __always_inline void |
| 744 | trace_event_setup(struct ring_buffer_event *event, |
| 745 | int type, unsigned long flags, int pc) |
| 746 | { |
| 747 | struct trace_entry *ent = ring_buffer_event_data(event); |
| 748 | |
| 749 | tracing_generic_entry_update(ent, flags, pc); |
| 750 | ent->type = type; |
| 751 | } |
| 752 | |
| 753 | static __always_inline struct ring_buffer_event * |
| 754 | __trace_buffer_lock_reserve(struct ring_buffer *buffer, |
| 755 | int type, |
| 756 | unsigned long len, |
| 757 | unsigned long flags, int pc) |
| 758 | { |
| 759 | struct ring_buffer_event *event; |
| 760 | |
| 761 | event = ring_buffer_lock_reserve(buffer, len); |
| 762 | if (event != NULL) |
| 763 | trace_event_setup(event, type, flags, pc); |
| 764 | |
| 765 | return event; |
| 766 | } |
| 767 | |
Steven Rostedt (Red Hat) | 5280bce | 2013-07-02 19:59:57 -0400 | [diff] [blame] | 768 | static void tracer_tracing_on(struct trace_array *tr) |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 769 | { |
| 770 | if (tr->trace_buffer.buffer) |
| 771 | ring_buffer_record_on(tr->trace_buffer.buffer); |
| 772 | /* |
| 773 | * This flag is looked at when buffers haven't been allocated |
| 774 | * yet, or by some tracers (like irqsoff), that just want to |
| 775 | * know if the ring buffer has been disabled, but it can handle |
| 776 | * races of where it gets disabled but we still do a record. |
| 777 | * As the check is in the fast path of the tracers, it is more |
| 778 | * important to be fast than accurate. |
| 779 | */ |
| 780 | tr->buffer_disabled = 0; |
| 781 | /* Make the flag seen by readers */ |
| 782 | smp_wmb(); |
| 783 | } |
| 784 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 785 | /** |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 786 | * tracing_on - enable tracing buffers |
| 787 | * |
| 788 | * This function enables tracing buffers that may have been |
| 789 | * disabled with tracing_off. |
| 790 | */ |
| 791 | void tracing_on(void) |
| 792 | { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 793 | tracer_tracing_on(&global_trace); |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 794 | } |
| 795 | EXPORT_SYMBOL_GPL(tracing_on); |
| 796 | |
Steven Rostedt (Red Hat) | 52ffabe3 | 2016-11-23 20:28:38 -0500 | [diff] [blame^] | 797 | |
| 798 | static __always_inline void |
| 799 | __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event) |
| 800 | { |
| 801 | __this_cpu_write(trace_cmdline_save, true); |
| 802 | |
| 803 | /* If this is the temp buffer, we need to commit fully */ |
| 804 | if (this_cpu_read(trace_buffered_event) == event) { |
| 805 | /* Length is in event->array[0] */ |
| 806 | ring_buffer_write(buffer, event->array[0], &event->array[1]); |
| 807 | /* Release the temp buffer */ |
| 808 | this_cpu_dec(trace_buffered_event_cnt); |
| 809 | } else |
| 810 | ring_buffer_unlock_commit(buffer, event); |
| 811 | } |
| 812 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 813 | /** |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 814 | * __trace_puts - write a constant string into the trace buffer. |
| 815 | * @ip: The address of the caller |
| 816 | * @str: The constant string to write |
| 817 | * @size: The size of the string. |
| 818 | */ |
| 819 | int __trace_puts(unsigned long ip, const char *str, int size) |
| 820 | { |
| 821 | struct ring_buffer_event *event; |
| 822 | struct ring_buffer *buffer; |
| 823 | struct print_entry *entry; |
| 824 | unsigned long irq_flags; |
| 825 | int alloc; |
zhangwei(Jovi) | 8abfb87 | 2013-07-18 16:31:05 +0800 | [diff] [blame] | 826 | int pc; |
| 827 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 828 | if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
zhangwei(Jovi) | f0160a5 | 2013-07-18 16:31:18 +0800 | [diff] [blame] | 829 | return 0; |
| 830 | |
zhangwei(Jovi) | 8abfb87 | 2013-07-18 16:31:05 +0800 | [diff] [blame] | 831 | pc = preempt_count(); |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 832 | |
Steven Rostedt (Red Hat) | 3132e10 | 2014-01-23 12:27:59 -0500 | [diff] [blame] | 833 | if (unlikely(tracing_selftest_running || tracing_disabled)) |
| 834 | return 0; |
| 835 | |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 836 | alloc = sizeof(*entry) + size + 2; /* possible \n added */ |
| 837 | |
| 838 | local_save_flags(irq_flags); |
| 839 | buffer = global_trace.trace_buffer.buffer; |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 840 | event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, |
| 841 | irq_flags, pc); |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 842 | if (!event) |
| 843 | return 0; |
| 844 | |
| 845 | entry = ring_buffer_event_data(event); |
| 846 | entry->ip = ip; |
| 847 | |
| 848 | memcpy(&entry->buf, str, size); |
| 849 | |
| 850 | /* Add a newline if necessary */ |
| 851 | if (entry->buf[size - 1] != '\n') { |
| 852 | entry->buf[size] = '\n'; |
| 853 | entry->buf[size + 1] = '\0'; |
| 854 | } else |
| 855 | entry->buf[size] = '\0'; |
| 856 | |
| 857 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 858 | ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL); |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 859 | |
| 860 | return size; |
| 861 | } |
| 862 | EXPORT_SYMBOL_GPL(__trace_puts); |
| 863 | |
| 864 | /** |
| 865 | * __trace_bputs - write the pointer to a constant string into trace buffer |
| 866 | * @ip: The address of the caller |
| 867 | * @str: The constant string to write to the buffer to |
| 868 | */ |
| 869 | int __trace_bputs(unsigned long ip, const char *str) |
| 870 | { |
| 871 | struct ring_buffer_event *event; |
| 872 | struct ring_buffer *buffer; |
| 873 | struct bputs_entry *entry; |
| 874 | unsigned long irq_flags; |
| 875 | int size = sizeof(struct bputs_entry); |
zhangwei(Jovi) | 8abfb87 | 2013-07-18 16:31:05 +0800 | [diff] [blame] | 876 | int pc; |
| 877 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 878 | if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
zhangwei(Jovi) | f0160a5 | 2013-07-18 16:31:18 +0800 | [diff] [blame] | 879 | return 0; |
| 880 | |
zhangwei(Jovi) | 8abfb87 | 2013-07-18 16:31:05 +0800 | [diff] [blame] | 881 | pc = preempt_count(); |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 882 | |
Steven Rostedt (Red Hat) | 3132e10 | 2014-01-23 12:27:59 -0500 | [diff] [blame] | 883 | if (unlikely(tracing_selftest_running || tracing_disabled)) |
| 884 | return 0; |
| 885 | |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 886 | local_save_flags(irq_flags); |
| 887 | buffer = global_trace.trace_buffer.buffer; |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 888 | event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size, |
| 889 | irq_flags, pc); |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 890 | if (!event) |
| 891 | return 0; |
| 892 | |
| 893 | entry = ring_buffer_event_data(event); |
| 894 | entry->ip = ip; |
| 895 | entry->str = str; |
| 896 | |
| 897 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 898 | ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL); |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 899 | |
| 900 | return 1; |
| 901 | } |
| 902 | EXPORT_SYMBOL_GPL(__trace_bputs); |
| 903 | |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 904 | #ifdef CONFIG_TRACER_SNAPSHOT |
| 905 | /** |
| 906 | * trace_snapshot - take a snapshot of the current buffer. |
| 907 | * |
| 908 | * This causes a swap between the snapshot buffer and the current live |
| 909 | * tracing buffer. You can use this to take snapshots of the live |
| 910 | * trace when some condition is triggered, but continue to trace. |
| 911 | * |
| 912 | * Note, make sure to allocate the snapshot with either |
| 913 | * a tracing_snapshot_alloc(), or by doing it manually |
| 914 | * with: echo 1 > /sys/kernel/debug/tracing/snapshot |
| 915 | * |
| 916 | * If the snapshot buffer is not allocated, it will stop tracing. |
| 917 | * Basically making a permanent snapshot. |
| 918 | */ |
| 919 | void tracing_snapshot(void) |
| 920 | { |
| 921 | struct trace_array *tr = &global_trace; |
| 922 | struct tracer *tracer = tr->current_trace; |
| 923 | unsigned long flags; |
| 924 | |
Steven Rostedt (Red Hat) | 1b22e38 | 2013-03-09 00:56:08 -0500 | [diff] [blame] | 925 | if (in_nmi()) { |
| 926 | internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n"); |
| 927 | internal_trace_puts("*** snapshot is being ignored ***\n"); |
| 928 | return; |
| 929 | } |
| 930 | |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 931 | if (!tr->allocated_snapshot) { |
Steven Rostedt (Red Hat) | ca268da | 2013-03-09 00:40:58 -0500 | [diff] [blame] | 932 | internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n"); |
| 933 | internal_trace_puts("*** stopping trace here! ***\n"); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 934 | tracing_off(); |
| 935 | return; |
| 936 | } |
| 937 | |
| 938 | /* Note, snapshot can not be used when the tracer uses it */ |
| 939 | if (tracer->use_max_tr) { |
Steven Rostedt (Red Hat) | ca268da | 2013-03-09 00:40:58 -0500 | [diff] [blame] | 940 | internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n"); |
| 941 | internal_trace_puts("*** Can not use snapshot (sorry) ***\n"); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 942 | return; |
| 943 | } |
| 944 | |
| 945 | local_irq_save(flags); |
| 946 | update_max_tr(tr, current, smp_processor_id()); |
| 947 | local_irq_restore(flags); |
| 948 | } |
Steven Rostedt (Red Hat) | 1b22e38 | 2013-03-09 00:56:08 -0500 | [diff] [blame] | 949 | EXPORT_SYMBOL_GPL(tracing_snapshot); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 950 | |
| 951 | static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf, |
| 952 | struct trace_buffer *size_buf, int cpu_id); |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 953 | static void set_buffer_entries(struct trace_buffer *buf, unsigned long val); |
| 954 | |
| 955 | static int alloc_snapshot(struct trace_array *tr) |
| 956 | { |
| 957 | int ret; |
| 958 | |
| 959 | if (!tr->allocated_snapshot) { |
| 960 | |
| 961 | /* allocate spare buffer */ |
| 962 | ret = resize_buffer_duplicate_size(&tr->max_buffer, |
| 963 | &tr->trace_buffer, RING_BUFFER_ALL_CPUS); |
| 964 | if (ret < 0) |
| 965 | return ret; |
| 966 | |
| 967 | tr->allocated_snapshot = true; |
| 968 | } |
| 969 | |
| 970 | return 0; |
| 971 | } |
| 972 | |
Fabian Frederick | ad1438a | 2014-04-17 21:44:42 +0200 | [diff] [blame] | 973 | static void free_snapshot(struct trace_array *tr) |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 974 | { |
| 975 | /* |
| 976 | * We don't free the ring buffer. instead, resize it because |
| 977 | * The max_tr ring buffer has some state (e.g. ring->clock) and |
| 978 | * we want preserve it. |
| 979 | */ |
| 980 | ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS); |
| 981 | set_buffer_entries(&tr->max_buffer, 1); |
| 982 | tracing_reset_online_cpus(&tr->max_buffer); |
| 983 | tr->allocated_snapshot = false; |
| 984 | } |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 985 | |
| 986 | /** |
Tom Zanussi | 93e31ff | 2013-10-24 08:59:26 -0500 | [diff] [blame] | 987 | * tracing_alloc_snapshot - allocate snapshot buffer. |
| 988 | * |
| 989 | * This only allocates the snapshot buffer if it isn't already |
| 990 | * allocated - it doesn't also take a snapshot. |
| 991 | * |
| 992 | * This is meant to be used in cases where the snapshot buffer needs |
| 993 | * to be set up for events that can't sleep but need to be able to |
| 994 | * trigger a snapshot. |
| 995 | */ |
| 996 | int tracing_alloc_snapshot(void) |
| 997 | { |
| 998 | struct trace_array *tr = &global_trace; |
| 999 | int ret; |
| 1000 | |
| 1001 | ret = alloc_snapshot(tr); |
| 1002 | WARN_ON(ret < 0); |
| 1003 | |
| 1004 | return ret; |
| 1005 | } |
| 1006 | EXPORT_SYMBOL_GPL(tracing_alloc_snapshot); |
| 1007 | |
| 1008 | /** |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1009 | * trace_snapshot_alloc - allocate and take a snapshot of the current buffer. |
| 1010 | * |
| 1011 | * This is similar to trace_snapshot(), but it will allocate the |
| 1012 | * snapshot buffer if it isn't already allocated. Use this only |
| 1013 | * where it is safe to sleep, as the allocation may sleep. |
| 1014 | * |
| 1015 | * This causes a swap between the snapshot buffer and the current live |
| 1016 | * tracing buffer. You can use this to take snapshots of the live |
| 1017 | * trace when some condition is triggered, but continue to trace. |
| 1018 | */ |
| 1019 | void tracing_snapshot_alloc(void) |
| 1020 | { |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1021 | int ret; |
| 1022 | |
Tom Zanussi | 93e31ff | 2013-10-24 08:59:26 -0500 | [diff] [blame] | 1023 | ret = tracing_alloc_snapshot(); |
| 1024 | if (ret < 0) |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 1025 | return; |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1026 | |
| 1027 | tracing_snapshot(); |
| 1028 | } |
Steven Rostedt (Red Hat) | 1b22e38 | 2013-03-09 00:56:08 -0500 | [diff] [blame] | 1029 | EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1030 | #else |
| 1031 | void tracing_snapshot(void) |
| 1032 | { |
| 1033 | WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used"); |
| 1034 | } |
Steven Rostedt (Red Hat) | 1b22e38 | 2013-03-09 00:56:08 -0500 | [diff] [blame] | 1035 | EXPORT_SYMBOL_GPL(tracing_snapshot); |
Tom Zanussi | 93e31ff | 2013-10-24 08:59:26 -0500 | [diff] [blame] | 1036 | int tracing_alloc_snapshot(void) |
| 1037 | { |
| 1038 | WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used"); |
| 1039 | return -ENODEV; |
| 1040 | } |
| 1041 | EXPORT_SYMBOL_GPL(tracing_alloc_snapshot); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1042 | void tracing_snapshot_alloc(void) |
| 1043 | { |
| 1044 | /* Give warning */ |
| 1045 | tracing_snapshot(); |
| 1046 | } |
Steven Rostedt (Red Hat) | 1b22e38 | 2013-03-09 00:56:08 -0500 | [diff] [blame] | 1047 | EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1048 | #endif /* CONFIG_TRACER_SNAPSHOT */ |
| 1049 | |
Steven Rostedt (Red Hat) | 5280bce | 2013-07-02 19:59:57 -0400 | [diff] [blame] | 1050 | static void tracer_tracing_off(struct trace_array *tr) |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1051 | { |
| 1052 | if (tr->trace_buffer.buffer) |
| 1053 | ring_buffer_record_off(tr->trace_buffer.buffer); |
| 1054 | /* |
| 1055 | * This flag is looked at when buffers haven't been allocated |
| 1056 | * yet, or by some tracers (like irqsoff), that just want to |
| 1057 | * know if the ring buffer has been disabled, but it can handle |
| 1058 | * races of where it gets disabled but we still do a record. |
| 1059 | * As the check is in the fast path of the tracers, it is more |
| 1060 | * important to be fast than accurate. |
| 1061 | */ |
| 1062 | tr->buffer_disabled = 1; |
| 1063 | /* Make the flag seen by readers */ |
| 1064 | smp_wmb(); |
| 1065 | } |
| 1066 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 1067 | /** |
| 1068 | * tracing_off - turn off tracing buffers |
| 1069 | * |
| 1070 | * This function stops the tracing buffers from recording data. |
| 1071 | * It does not disable any overhead the tracers themselves may |
| 1072 | * be causing. This function simply causes all recording to |
| 1073 | * the ring buffers to fail. |
| 1074 | */ |
| 1075 | void tracing_off(void) |
| 1076 | { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1077 | tracer_tracing_off(&global_trace); |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 1078 | } |
| 1079 | EXPORT_SYMBOL_GPL(tracing_off); |
| 1080 | |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 1081 | void disable_trace_on_warning(void) |
| 1082 | { |
| 1083 | if (__disable_trace_on_warning) |
| 1084 | tracing_off(); |
| 1085 | } |
| 1086 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 1087 | /** |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1088 | * tracer_tracing_is_on - show real state of ring buffer enabled |
| 1089 | * @tr : the trace array to know if ring buffer is enabled |
| 1090 | * |
| 1091 | * Shows real state of the ring buffer if it is enabled or not. |
| 1092 | */ |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 1093 | int tracer_tracing_is_on(struct trace_array *tr) |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1094 | { |
| 1095 | if (tr->trace_buffer.buffer) |
| 1096 | return ring_buffer_record_is_on(tr->trace_buffer.buffer); |
| 1097 | return !tr->buffer_disabled; |
| 1098 | } |
| 1099 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 1100 | /** |
| 1101 | * tracing_is_on - show state of ring buffers enabled |
| 1102 | */ |
| 1103 | int tracing_is_on(void) |
| 1104 | { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1105 | return tracer_tracing_is_on(&global_trace); |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 1106 | } |
| 1107 | EXPORT_SYMBOL_GPL(tracing_is_on); |
| 1108 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1109 | static int __init set_buf_size(char *str) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1110 | { |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1111 | unsigned long buf_size; |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1112 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1113 | if (!str) |
| 1114 | return 0; |
Li Zefan | 9d612be | 2009-06-24 17:33:15 +0800 | [diff] [blame] | 1115 | buf_size = memparse(str, &str); |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1116 | /* nr_entries can not be zero */ |
Li Zefan | 9d612be | 2009-06-24 17:33:15 +0800 | [diff] [blame] | 1117 | if (buf_size == 0) |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1118 | return 0; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1119 | trace_buf_size = buf_size; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1120 | return 1; |
| 1121 | } |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1122 | __setup("trace_buf_size=", set_buf_size); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1123 | |
Tim Bird | 0e95017 | 2010-02-25 15:36:43 -0800 | [diff] [blame] | 1124 | static int __init set_tracing_thresh(char *str) |
| 1125 | { |
Wang Tianhong | 87abb3b | 2012-08-02 14:02:00 +0800 | [diff] [blame] | 1126 | unsigned long threshold; |
Tim Bird | 0e95017 | 2010-02-25 15:36:43 -0800 | [diff] [blame] | 1127 | int ret; |
| 1128 | |
| 1129 | if (!str) |
| 1130 | return 0; |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 1131 | ret = kstrtoul(str, 0, &threshold); |
Tim Bird | 0e95017 | 2010-02-25 15:36:43 -0800 | [diff] [blame] | 1132 | if (ret < 0) |
| 1133 | return 0; |
Wang Tianhong | 87abb3b | 2012-08-02 14:02:00 +0800 | [diff] [blame] | 1134 | tracing_thresh = threshold * 1000; |
Tim Bird | 0e95017 | 2010-02-25 15:36:43 -0800 | [diff] [blame] | 1135 | return 1; |
| 1136 | } |
| 1137 | __setup("tracing_thresh=", set_tracing_thresh); |
| 1138 | |
Steven Rostedt | 57f50be | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 1139 | unsigned long nsecs_to_usecs(unsigned long nsecs) |
| 1140 | { |
| 1141 | return nsecs / 1000; |
| 1142 | } |
| 1143 | |
Steven Rostedt (Red Hat) | a3418a3 | 2015-09-29 09:43:30 -0400 | [diff] [blame] | 1144 | /* |
| 1145 | * TRACE_FLAGS is defined as a tuple matching bit masks with strings. |
| 1146 | * It uses C(a, b) where 'a' is the enum name and 'b' is the string that |
| 1147 | * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list |
| 1148 | * of strings in the order that the enums were defined. |
| 1149 | */ |
| 1150 | #undef C |
| 1151 | #define C(a, b) b |
| 1152 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1153 | /* These must match the bit postions in trace_iterator_flags */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1154 | static const char *trace_options[] = { |
Steven Rostedt (Red Hat) | a3418a3 | 2015-09-29 09:43:30 -0400 | [diff] [blame] | 1155 | TRACE_FLAGS |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1156 | NULL |
| 1157 | }; |
| 1158 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 1159 | static struct { |
| 1160 | u64 (*func)(void); |
| 1161 | const char *name; |
David Sharp | 8be0709 | 2012-11-13 12:18:22 -0800 | [diff] [blame] | 1162 | int in_ns; /* is this clock in nanoseconds? */ |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 1163 | } trace_clocks[] = { |
Thomas Gleixner | 1b3e5c0 | 2014-07-16 21:05:25 +0000 | [diff] [blame] | 1164 | { trace_clock_local, "local", 1 }, |
| 1165 | { trace_clock_global, "global", 1 }, |
| 1166 | { trace_clock_counter, "counter", 0 }, |
Linus Torvalds | e7fda6c | 2014-08-05 17:46:42 -0700 | [diff] [blame] | 1167 | { trace_clock_jiffies, "uptime", 0 }, |
Thomas Gleixner | 1b3e5c0 | 2014-07-16 21:05:25 +0000 | [diff] [blame] | 1168 | { trace_clock, "perf", 1 }, |
| 1169 | { ktime_get_mono_fast_ns, "mono", 1 }, |
Drew Richardson | aabfa5f | 2015-05-08 07:30:39 -0700 | [diff] [blame] | 1170 | { ktime_get_raw_fast_ns, "mono_raw", 1 }, |
David Sharp | 8cbd9cc | 2012-11-13 12:18:21 -0800 | [diff] [blame] | 1171 | ARCH_TRACE_CLOCKS |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 1172 | }; |
| 1173 | |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1174 | /* |
| 1175 | * trace_parser_get_init - gets the buffer for trace parser |
| 1176 | */ |
| 1177 | int trace_parser_get_init(struct trace_parser *parser, int size) |
| 1178 | { |
| 1179 | memset(parser, 0, sizeof(*parser)); |
| 1180 | |
| 1181 | parser->buffer = kmalloc(size, GFP_KERNEL); |
| 1182 | if (!parser->buffer) |
| 1183 | return 1; |
| 1184 | |
| 1185 | parser->size = size; |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | * trace_parser_put - frees the buffer for trace parser |
| 1191 | */ |
| 1192 | void trace_parser_put(struct trace_parser *parser) |
| 1193 | { |
| 1194 | kfree(parser->buffer); |
| 1195 | } |
| 1196 | |
| 1197 | /* |
| 1198 | * trace_get_user - reads the user input string separated by space |
| 1199 | * (matched by isspace(ch)) |
| 1200 | * |
| 1201 | * For each string found the 'struct trace_parser' is updated, |
| 1202 | * and the function returns. |
| 1203 | * |
| 1204 | * Returns number of bytes read. |
| 1205 | * |
| 1206 | * See kernel/trace/trace.h for 'struct trace_parser' details. |
| 1207 | */ |
| 1208 | int trace_get_user(struct trace_parser *parser, const char __user *ubuf, |
| 1209 | size_t cnt, loff_t *ppos) |
| 1210 | { |
| 1211 | char ch; |
| 1212 | size_t read = 0; |
| 1213 | ssize_t ret; |
| 1214 | |
| 1215 | if (!*ppos) |
| 1216 | trace_parser_clear(parser); |
| 1217 | |
| 1218 | ret = get_user(ch, ubuf++); |
| 1219 | if (ret) |
| 1220 | goto out; |
| 1221 | |
| 1222 | read++; |
| 1223 | cnt--; |
| 1224 | |
| 1225 | /* |
| 1226 | * The parser is not finished with the last write, |
| 1227 | * continue reading the user input without skipping spaces. |
| 1228 | */ |
| 1229 | if (!parser->cont) { |
| 1230 | /* skip white space */ |
| 1231 | while (cnt && isspace(ch)) { |
| 1232 | ret = get_user(ch, ubuf++); |
| 1233 | if (ret) |
| 1234 | goto out; |
| 1235 | read++; |
| 1236 | cnt--; |
| 1237 | } |
| 1238 | |
| 1239 | /* only spaces were written */ |
| 1240 | if (isspace(ch)) { |
| 1241 | *ppos += read; |
| 1242 | ret = read; |
| 1243 | goto out; |
| 1244 | } |
| 1245 | |
| 1246 | parser->idx = 0; |
| 1247 | } |
| 1248 | |
| 1249 | /* read the non-space input */ |
| 1250 | while (cnt && !isspace(ch)) { |
Li Zefan | 3c235a3 | 2009-09-22 13:51:54 +0800 | [diff] [blame] | 1251 | if (parser->idx < parser->size - 1) |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1252 | parser->buffer[parser->idx++] = ch; |
| 1253 | else { |
| 1254 | ret = -EINVAL; |
| 1255 | goto out; |
| 1256 | } |
| 1257 | ret = get_user(ch, ubuf++); |
| 1258 | if (ret) |
| 1259 | goto out; |
| 1260 | read++; |
| 1261 | cnt--; |
| 1262 | } |
| 1263 | |
| 1264 | /* We either got finished input or we have to wait for another call. */ |
| 1265 | if (isspace(ch)) { |
| 1266 | parser->buffer[parser->idx] = 0; |
| 1267 | parser->cont = false; |
Steven Rostedt | 057db84 | 2013-10-09 22:23:23 -0400 | [diff] [blame] | 1268 | } else if (parser->idx < parser->size - 1) { |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1269 | parser->cont = true; |
| 1270 | parser->buffer[parser->idx++] = ch; |
Steven Rostedt | 057db84 | 2013-10-09 22:23:23 -0400 | [diff] [blame] | 1271 | } else { |
| 1272 | ret = -EINVAL; |
| 1273 | goto out; |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | *ppos += read; |
| 1277 | ret = read; |
| 1278 | |
| 1279 | out: |
| 1280 | return ret; |
| 1281 | } |
| 1282 | |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 1283 | /* TODO add a seq_buf_to_buffer() */ |
Dmitri Vorobiev | b8b9426 | 2009-03-22 19:11:11 +0200 | [diff] [blame] | 1284 | static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt) |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1285 | { |
| 1286 | int len; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1287 | |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 1288 | if (trace_seq_used(s) <= s->seq.readpos) |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1289 | return -EBUSY; |
| 1290 | |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 1291 | len = trace_seq_used(s) - s->seq.readpos; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1292 | if (cnt > len) |
| 1293 | cnt = len; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 1294 | memcpy(buf, s->buffer + s->seq.readpos, cnt); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1295 | |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 1296 | s->seq.readpos += cnt; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1297 | return cnt; |
| 1298 | } |
| 1299 | |
Tim Bird | 0e95017 | 2010-02-25 15:36:43 -0800 | [diff] [blame] | 1300 | unsigned long __read_mostly tracing_thresh; |
| 1301 | |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1302 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1303 | /* |
| 1304 | * Copy the new maximum trace into the separate maximum-trace |
| 1305 | * structure. (this way the maximum trace is permanently saved, |
| 1306 | * for later retrieval via /sys/kernel/debug/tracing/latency_trace) |
| 1307 | */ |
| 1308 | static void |
| 1309 | __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
| 1310 | { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1311 | struct trace_buffer *trace_buf = &tr->trace_buffer; |
| 1312 | struct trace_buffer *max_buf = &tr->max_buffer; |
| 1313 | struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu); |
| 1314 | struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu); |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1315 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1316 | max_buf->cpu = cpu; |
| 1317 | max_buf->time_start = data->preempt_timestamp; |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1318 | |
Steven Rostedt (Red Hat) | 6d9b3fa | 2014-01-14 11:28:38 -0500 | [diff] [blame] | 1319 | max_data->saved_latency = tr->max_latency; |
Steven Rostedt | 8248ac0 | 2009-09-02 12:27:41 -0400 | [diff] [blame] | 1320 | max_data->critical_start = data->critical_start; |
| 1321 | max_data->critical_end = data->critical_end; |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1322 | |
Arnaldo Carvalho de Melo | 1acaa1b | 2010-03-05 18:23:50 -0300 | [diff] [blame] | 1323 | memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN); |
Steven Rostedt | 8248ac0 | 2009-09-02 12:27:41 -0400 | [diff] [blame] | 1324 | max_data->pid = tsk->pid; |
Steven Rostedt (Red Hat) | f17a519 | 2013-05-30 21:10:37 -0400 | [diff] [blame] | 1325 | /* |
| 1326 | * If tsk == current, then use current_uid(), as that does not use |
| 1327 | * RCU. The irq tracer can be called out of RCU scope. |
| 1328 | */ |
| 1329 | if (tsk == current) |
| 1330 | max_data->uid = current_uid(); |
| 1331 | else |
| 1332 | max_data->uid = task_uid(tsk); |
| 1333 | |
Steven Rostedt | 8248ac0 | 2009-09-02 12:27:41 -0400 | [diff] [blame] | 1334 | max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO; |
| 1335 | max_data->policy = tsk->policy; |
| 1336 | max_data->rt_priority = tsk->rt_priority; |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1337 | |
| 1338 | /* record this tasks comm */ |
| 1339 | tracing_record_cmdline(tsk); |
| 1340 | } |
| 1341 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1342 | /** |
| 1343 | * update_max_tr - snapshot all trace buffers from global_trace to max_tr |
| 1344 | * @tr: tracer |
| 1345 | * @tsk: the task with the latency |
| 1346 | * @cpu: The cpu that initiated the trace. |
| 1347 | * |
| 1348 | * Flip the buffers between the @tr and the max_tr and record information |
| 1349 | * about which task was the cause of this latency. |
| 1350 | */ |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1351 | void |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1352 | update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
| 1353 | { |
Steven Rostedt (Red Hat) | 2721e72 | 2013-03-12 11:32:32 -0400 | [diff] [blame] | 1354 | struct ring_buffer *buf; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1355 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1356 | if (tr->stop_count) |
Steven Rostedt | b8de7bd1 | 2009-08-31 22:32:27 -0400 | [diff] [blame] | 1357 | return; |
| 1358 | |
Steven Rostedt | 4c11d7a | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1359 | WARN_ON_ONCE(!irqs_disabled()); |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 1360 | |
Steven Rostedt (Red Hat) | 45ad21c | 2013-03-05 18:25:02 -0500 | [diff] [blame] | 1361 | if (!tr->allocated_snapshot) { |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 1362 | /* Only the nop tracer should hit this when disabling */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1363 | WARN_ON_ONCE(tr->current_trace != &nop_trace); |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 1364 | return; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 1365 | } |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 1366 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1367 | arch_spin_lock(&tr->max_lock); |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1368 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1369 | buf = tr->trace_buffer.buffer; |
| 1370 | tr->trace_buffer.buffer = tr->max_buffer.buffer; |
| 1371 | tr->max_buffer.buffer = buf; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1372 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1373 | __update_max_tr(tr, tsk, cpu); |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1374 | arch_spin_unlock(&tr->max_lock); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | /** |
| 1378 | * update_max_tr_single - only copy one trace over, and reset the rest |
| 1379 | * @tr - tracer |
| 1380 | * @tsk - task with the latency |
| 1381 | * @cpu - the cpu of the buffer to copy. |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1382 | * |
| 1383 | * Flip the trace of a single CPU buffer between the @tr and the max_tr. |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1384 | */ |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1385 | void |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1386 | update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) |
| 1387 | { |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1388 | int ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1389 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1390 | if (tr->stop_count) |
Steven Rostedt | b8de7bd1 | 2009-08-31 22:32:27 -0400 | [diff] [blame] | 1391 | return; |
| 1392 | |
Steven Rostedt | 4c11d7a | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1393 | WARN_ON_ONCE(!irqs_disabled()); |
Steven Rostedt | 6c24499 | 2013-04-29 20:08:14 -0400 | [diff] [blame] | 1394 | if (!tr->allocated_snapshot) { |
Steven Rostedt (Red Hat) | 2930e04 | 2013-03-26 17:33:00 -0400 | [diff] [blame] | 1395 | /* Only the nop tracer should hit this when disabling */ |
Linus Torvalds | 9e8529a | 2013-04-29 13:55:38 -0700 | [diff] [blame] | 1396 | WARN_ON_ONCE(tr->current_trace != &nop_trace); |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 1397 | return; |
Steven Rostedt (Red Hat) | 2930e04 | 2013-03-26 17:33:00 -0400 | [diff] [blame] | 1398 | } |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 1399 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1400 | arch_spin_lock(&tr->max_lock); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1401 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1402 | ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu); |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1403 | |
Steven Rostedt | e8165dbb | 2009-09-03 19:13:05 -0400 | [diff] [blame] | 1404 | if (ret == -EBUSY) { |
| 1405 | /* |
| 1406 | * We failed to swap the buffer due to a commit taking |
| 1407 | * place on this CPU. We fail to record, but we reset |
| 1408 | * the max trace buffer (no one writes directly to it) |
| 1409 | * and flag that it failed. |
| 1410 | */ |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1411 | trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_, |
Steven Rostedt | e8165dbb | 2009-09-03 19:13:05 -0400 | [diff] [blame] | 1412 | "Failed to swap buffers due to commit in progress\n"); |
| 1413 | } |
| 1414 | |
Steven Rostedt | e8165dbb | 2009-09-03 19:13:05 -0400 | [diff] [blame] | 1415 | WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1416 | |
| 1417 | __update_max_tr(tr, tsk, cpu); |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1418 | arch_spin_unlock(&tr->max_lock); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1419 | } |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1420 | #endif /* CONFIG_TRACER_MAX_TRACE */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1421 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 1422 | static int wait_on_pipe(struct trace_iterator *iter, bool full) |
Steven Rostedt | 0d5c6e1 | 2012-11-01 20:54:21 -0400 | [diff] [blame] | 1423 | { |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 1424 | /* Iterators are static, they should be filled or empty */ |
| 1425 | if (trace_buffer_iter(iter, iter->cpu_file)) |
Steven Rostedt (Red Hat) | 8b8b368 | 2014-06-10 09:46:00 -0400 | [diff] [blame] | 1426 | return 0; |
Steven Rostedt | 0d5c6e1 | 2012-11-01 20:54:21 -0400 | [diff] [blame] | 1427 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 1428 | return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file, |
| 1429 | full); |
Steven Rostedt | 0d5c6e1 | 2012-11-01 20:54:21 -0400 | [diff] [blame] | 1430 | } |
| 1431 | |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1432 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
| 1433 | static int run_tracer_selftest(struct tracer *type) |
| 1434 | { |
| 1435 | struct trace_array *tr = &global_trace; |
| 1436 | struct tracer *saved_tracer = tr->current_trace; |
| 1437 | int ret; |
| 1438 | |
| 1439 | if (!type->selftest || tracing_selftest_disabled) |
| 1440 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1441 | |
| 1442 | /* |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1443 | * Run a selftest on this tracer. |
| 1444 | * Here we reset the trace buffer, and set the current |
| 1445 | * tracer to be this tracer. The tracer can then run some |
| 1446 | * internal tracing to verify that everything is in order. |
| 1447 | * If we fail, we do not register this tracer. |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1448 | */ |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1449 | tracing_reset_online_cpus(&tr->trace_buffer); |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1450 | |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1451 | tr->current_trace = type; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1452 | |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1453 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 1454 | if (type->use_max_tr) { |
| 1455 | /* If we expanded the buffers, make sure the max is expanded too */ |
| 1456 | if (ring_buffer_expanded) |
| 1457 | ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size, |
| 1458 | RING_BUFFER_ALL_CPUS); |
| 1459 | tr->allocated_snapshot = true; |
| 1460 | } |
| 1461 | #endif |
| 1462 | |
| 1463 | /* the test is responsible for initializing and enabling */ |
| 1464 | pr_info("Testing tracer %s: ", type->name); |
| 1465 | ret = type->selftest(type, tr); |
| 1466 | /* the test is responsible for resetting too */ |
| 1467 | tr->current_trace = saved_tracer; |
| 1468 | if (ret) { |
| 1469 | printk(KERN_CONT "FAILED!\n"); |
| 1470 | /* Add the warning after printing 'FAILED' */ |
| 1471 | WARN_ON(1); |
| 1472 | return -1; |
| 1473 | } |
| 1474 | /* Only reset on passing, to avoid touching corrupted buffers */ |
| 1475 | tracing_reset_online_cpus(&tr->trace_buffer); |
| 1476 | |
| 1477 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 1478 | if (type->use_max_tr) { |
| 1479 | tr->allocated_snapshot = false; |
| 1480 | |
| 1481 | /* Shrink the max buffer again */ |
| 1482 | if (ring_buffer_expanded) |
| 1483 | ring_buffer_resize(tr->max_buffer.buffer, 1, |
| 1484 | RING_BUFFER_ALL_CPUS); |
| 1485 | } |
| 1486 | #endif |
| 1487 | |
| 1488 | printk(KERN_CONT "PASSED\n"); |
| 1489 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1490 | } |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1491 | #else |
| 1492 | static inline int run_tracer_selftest(struct tracer *type) |
| 1493 | { |
| 1494 | return 0; |
| 1495 | } |
| 1496 | #endif /* CONFIG_FTRACE_STARTUP_TEST */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1497 | |
Steven Rostedt (Red Hat) | 41d9c0b | 2015-09-29 17:31:55 -0400 | [diff] [blame] | 1498 | static void add_tracer_options(struct trace_array *tr, struct tracer *t); |
| 1499 | |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 1500 | static void __init apply_trace_boot_options(void); |
| 1501 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1502 | /** |
| 1503 | * register_tracer - register a tracer with the ftrace system. |
| 1504 | * @type - the plugin for the tracer |
| 1505 | * |
| 1506 | * Register a new plugin tracer. |
| 1507 | */ |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 1508 | int __init register_tracer(struct tracer *type) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1509 | { |
| 1510 | struct tracer *t; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1511 | int ret = 0; |
| 1512 | |
| 1513 | if (!type->name) { |
| 1514 | pr_info("Tracer must have a name\n"); |
| 1515 | return -1; |
| 1516 | } |
| 1517 | |
Dan Carpenter | 24a461d | 2010-07-10 12:06:44 +0200 | [diff] [blame] | 1518 | if (strlen(type->name) >= MAX_TRACER_SIZE) { |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 1519 | pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE); |
| 1520 | return -1; |
| 1521 | } |
| 1522 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1523 | mutex_lock(&trace_types_lock); |
Ingo Molnar | 86fa2f6 | 2008-11-19 10:00:15 +0100 | [diff] [blame] | 1524 | |
Frederic Weisbecker | 8e1b82e | 2008-12-06 03:41:33 +0100 | [diff] [blame] | 1525 | tracing_selftest_running = true; |
| 1526 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1527 | for (t = trace_types; t; t = t->next) { |
| 1528 | if (strcmp(type->name, t->name) == 0) { |
| 1529 | /* already found */ |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 1530 | pr_info("Tracer %s already registered\n", |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1531 | type->name); |
| 1532 | ret = -1; |
| 1533 | goto out; |
| 1534 | } |
| 1535 | } |
| 1536 | |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 1537 | if (!type->set_flag) |
| 1538 | type->set_flag = &dummy_set_flag; |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 1539 | if (!type->flags) { |
| 1540 | /*allocate a dummy tracer_flags*/ |
| 1541 | type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL); |
Chunyu Hu | c8ca003 | 2016-03-14 20:35:41 +0800 | [diff] [blame] | 1542 | if (!type->flags) { |
| 1543 | ret = -ENOMEM; |
| 1544 | goto out; |
| 1545 | } |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 1546 | type->flags->val = 0; |
| 1547 | type->flags->opts = dummy_tracer_opt; |
| 1548 | } else |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 1549 | if (!type->flags->opts) |
| 1550 | type->flags->opts = dummy_tracer_opt; |
Frederic Weisbecker | 6eaaa5d | 2009-02-11 02:25:00 +0100 | [diff] [blame] | 1551 | |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 1552 | /* store the tracer for __set_tracer_option */ |
| 1553 | type->flags->trace = type; |
| 1554 | |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1555 | ret = run_tracer_selftest(type); |
| 1556 | if (ret < 0) |
| 1557 | goto out; |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 1558 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1559 | type->next = trace_types; |
| 1560 | trace_types = type; |
Steven Rostedt (Red Hat) | 41d9c0b | 2015-09-29 17:31:55 -0400 | [diff] [blame] | 1561 | add_tracer_options(&global_trace, type); |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 1562 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1563 | out: |
Frederic Weisbecker | 8e1b82e | 2008-12-06 03:41:33 +0100 | [diff] [blame] | 1564 | tracing_selftest_running = false; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1565 | mutex_unlock(&trace_types_lock); |
| 1566 | |
Steven Rostedt | dac7494 | 2009-02-05 01:13:38 -0500 | [diff] [blame] | 1567 | if (ret || !default_bootup_tracer) |
| 1568 | goto out_unlock; |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 1569 | |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 1570 | if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE)) |
Steven Rostedt | dac7494 | 2009-02-05 01:13:38 -0500 | [diff] [blame] | 1571 | goto out_unlock; |
| 1572 | |
| 1573 | printk(KERN_INFO "Starting tracer '%s'\n", type->name); |
| 1574 | /* Do we want this tracer to start on bootup? */ |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 1575 | tracing_set_tracer(&global_trace, type->name); |
Steven Rostedt | dac7494 | 2009-02-05 01:13:38 -0500 | [diff] [blame] | 1576 | default_bootup_tracer = NULL; |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 1577 | |
| 1578 | apply_trace_boot_options(); |
| 1579 | |
Steven Rostedt | dac7494 | 2009-02-05 01:13:38 -0500 | [diff] [blame] | 1580 | /* disable other selftests, since this will break it. */ |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 1581 | tracing_selftest_disabled = true; |
Steven Rostedt | dac7494 | 2009-02-05 01:13:38 -0500 | [diff] [blame] | 1582 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
| 1583 | printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n", |
| 1584 | type->name); |
| 1585 | #endif |
| 1586 | |
| 1587 | out_unlock: |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1588 | return ret; |
| 1589 | } |
| 1590 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1591 | void tracing_reset(struct trace_buffer *buf, int cpu) |
Steven Rostedt | f633903 | 2009-09-04 12:35:16 -0400 | [diff] [blame] | 1592 | { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1593 | struct ring_buffer *buffer = buf->buffer; |
Steven Rostedt | f633903 | 2009-09-04 12:35:16 -0400 | [diff] [blame] | 1594 | |
Hiraku Toyooka | a541641 | 2012-12-19 16:02:34 +0900 | [diff] [blame] | 1595 | if (!buffer) |
| 1596 | return; |
| 1597 | |
Steven Rostedt | f633903 | 2009-09-04 12:35:16 -0400 | [diff] [blame] | 1598 | ring_buffer_record_disable(buffer); |
| 1599 | |
| 1600 | /* Make sure all commits have finished */ |
| 1601 | synchronize_sched(); |
Steven Rostedt | 6817968 | 2012-05-08 20:57:53 -0400 | [diff] [blame] | 1602 | ring_buffer_reset_cpu(buffer, cpu); |
Steven Rostedt | f633903 | 2009-09-04 12:35:16 -0400 | [diff] [blame] | 1603 | |
| 1604 | ring_buffer_record_enable(buffer); |
| 1605 | } |
| 1606 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1607 | void tracing_reset_online_cpus(struct trace_buffer *buf) |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 1608 | { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1609 | struct ring_buffer *buffer = buf->buffer; |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 1610 | int cpu; |
| 1611 | |
Hiraku Toyooka | a541641 | 2012-12-19 16:02:34 +0900 | [diff] [blame] | 1612 | if (!buffer) |
| 1613 | return; |
| 1614 | |
Steven Rostedt | 621968c | 2009-09-04 12:02:35 -0400 | [diff] [blame] | 1615 | ring_buffer_record_disable(buffer); |
| 1616 | |
| 1617 | /* Make sure all commits have finished */ |
| 1618 | synchronize_sched(); |
| 1619 | |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 1620 | buf->time_start = buffer_ftrace_now(buf, buf->cpu); |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 1621 | |
| 1622 | for_each_online_cpu(cpu) |
Steven Rostedt | 6817968 | 2012-05-08 20:57:53 -0400 | [diff] [blame] | 1623 | ring_buffer_reset_cpu(buffer, cpu); |
Steven Rostedt | 621968c | 2009-09-04 12:02:35 -0400 | [diff] [blame] | 1624 | |
| 1625 | ring_buffer_record_enable(buffer); |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 1626 | } |
| 1627 | |
Steven Rostedt (Red Hat) | 09d8091 | 2013-07-23 22:21:59 -0400 | [diff] [blame] | 1628 | /* Must have trace_types_lock held */ |
Steven Rostedt (Red Hat) | 873c642 | 2013-03-04 23:26:06 -0500 | [diff] [blame] | 1629 | void tracing_reset_all_online_cpus(void) |
Steven Rostedt | 9456f0f | 2009-05-06 21:54:09 -0400 | [diff] [blame] | 1630 | { |
Steven Rostedt (Red Hat) | 873c642 | 2013-03-04 23:26:06 -0500 | [diff] [blame] | 1631 | struct trace_array *tr; |
| 1632 | |
Steven Rostedt (Red Hat) | 873c642 | 2013-03-04 23:26:06 -0500 | [diff] [blame] | 1633 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1634 | tracing_reset_online_cpus(&tr->trace_buffer); |
| 1635 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 1636 | tracing_reset_online_cpus(&tr->max_buffer); |
| 1637 | #endif |
Steven Rostedt (Red Hat) | 873c642 | 2013-03-04 23:26:06 -0500 | [diff] [blame] | 1638 | } |
Steven Rostedt | 9456f0f | 2009-05-06 21:54:09 -0400 | [diff] [blame] | 1639 | } |
| 1640 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1641 | #define SAVED_CMDLINES_DEFAULT 128 |
Thomas Gleixner | 2c7eea4 | 2009-03-18 09:03:19 +0100 | [diff] [blame] | 1642 | #define NO_CMDLINE_MAP UINT_MAX |
Thomas Gleixner | edc35bd | 2009-12-03 12:38:57 +0100 | [diff] [blame] | 1643 | static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED; |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1644 | struct saved_cmdlines_buffer { |
| 1645 | unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1]; |
| 1646 | unsigned *map_cmdline_to_pid; |
| 1647 | unsigned cmdline_num; |
| 1648 | int cmdline_idx; |
| 1649 | char *saved_cmdlines; |
| 1650 | }; |
| 1651 | static struct saved_cmdlines_buffer *savedcmd; |
Steven Rostedt | 25b0b44 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1652 | |
Steven Rostedt | 25b0b44 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1653 | /* temporary disable recording */ |
Hannes Eder | 4fd2735 | 2009-02-10 19:44:12 +0100 | [diff] [blame] | 1654 | static atomic_t trace_record_cmdline_disabled __read_mostly; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1655 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1656 | static inline char *get_saved_cmdlines(int idx) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1657 | { |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1658 | return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN]; |
| 1659 | } |
| 1660 | |
| 1661 | static inline void set_cmdline(int idx, const char *cmdline) |
| 1662 | { |
| 1663 | memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN); |
| 1664 | } |
| 1665 | |
| 1666 | static int allocate_cmdlines_buffer(unsigned int val, |
| 1667 | struct saved_cmdlines_buffer *s) |
| 1668 | { |
| 1669 | s->map_cmdline_to_pid = kmalloc(val * sizeof(*s->map_cmdline_to_pid), |
| 1670 | GFP_KERNEL); |
| 1671 | if (!s->map_cmdline_to_pid) |
| 1672 | return -ENOMEM; |
| 1673 | |
| 1674 | s->saved_cmdlines = kmalloc(val * TASK_COMM_LEN, GFP_KERNEL); |
| 1675 | if (!s->saved_cmdlines) { |
| 1676 | kfree(s->map_cmdline_to_pid); |
| 1677 | return -ENOMEM; |
| 1678 | } |
| 1679 | |
| 1680 | s->cmdline_idx = 0; |
| 1681 | s->cmdline_num = val; |
| 1682 | memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP, |
| 1683 | sizeof(s->map_pid_to_cmdline)); |
| 1684 | memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP, |
| 1685 | val * sizeof(*s->map_cmdline_to_pid)); |
| 1686 | |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | static int trace_create_savedcmd(void) |
| 1691 | { |
| 1692 | int ret; |
| 1693 | |
Namhyung Kim | a6af8fb | 2014-06-10 16:11:35 +0900 | [diff] [blame] | 1694 | savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL); |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1695 | if (!savedcmd) |
| 1696 | return -ENOMEM; |
| 1697 | |
| 1698 | ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd); |
| 1699 | if (ret < 0) { |
| 1700 | kfree(savedcmd); |
| 1701 | savedcmd = NULL; |
| 1702 | return -ENOMEM; |
| 1703 | } |
| 1704 | |
| 1705 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
Carsten Emde | b5130b1 | 2009-09-13 01:43:07 +0200 | [diff] [blame] | 1708 | int is_tracing_stopped(void) |
| 1709 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1710 | return global_trace.stop_count; |
Carsten Emde | b5130b1 | 2009-09-13 01:43:07 +0200 | [diff] [blame] | 1711 | } |
| 1712 | |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1713 | /** |
| 1714 | * tracing_start - quick start of the tracer |
| 1715 | * |
| 1716 | * If tracing is enabled but was stopped by tracing_stop, |
| 1717 | * this will start the tracer back up. |
| 1718 | */ |
| 1719 | void tracing_start(void) |
| 1720 | { |
| 1721 | struct ring_buffer *buffer; |
| 1722 | unsigned long flags; |
| 1723 | |
| 1724 | if (tracing_disabled) |
| 1725 | return; |
| 1726 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1727 | raw_spin_lock_irqsave(&global_trace.start_lock, flags); |
| 1728 | if (--global_trace.stop_count) { |
| 1729 | if (global_trace.stop_count < 0) { |
Steven Rostedt | b06a830 | 2009-01-22 14:26:15 -0500 | [diff] [blame] | 1730 | /* Someone screwed up their debugging */ |
| 1731 | WARN_ON_ONCE(1); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1732 | global_trace.stop_count = 0; |
Steven Rostedt | b06a830 | 2009-01-22 14:26:15 -0500 | [diff] [blame] | 1733 | } |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1734 | goto out; |
| 1735 | } |
| 1736 | |
Steven Rostedt | a2f8071 | 2010-03-12 19:56:00 -0500 | [diff] [blame] | 1737 | /* Prevent the buffers from switching */ |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1738 | arch_spin_lock(&global_trace.max_lock); |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1739 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1740 | buffer = global_trace.trace_buffer.buffer; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1741 | if (buffer) |
| 1742 | ring_buffer_record_enable(buffer); |
| 1743 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1744 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 1745 | buffer = global_trace.max_buffer.buffer; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1746 | if (buffer) |
| 1747 | ring_buffer_record_enable(buffer); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1748 | #endif |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1749 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1750 | arch_spin_unlock(&global_trace.max_lock); |
Steven Rostedt | a2f8071 | 2010-03-12 19:56:00 -0500 | [diff] [blame] | 1751 | |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1752 | out: |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1753 | raw_spin_unlock_irqrestore(&global_trace.start_lock, flags); |
| 1754 | } |
| 1755 | |
| 1756 | static void tracing_start_tr(struct trace_array *tr) |
| 1757 | { |
| 1758 | struct ring_buffer *buffer; |
| 1759 | unsigned long flags; |
| 1760 | |
| 1761 | if (tracing_disabled) |
| 1762 | return; |
| 1763 | |
| 1764 | /* If global, we need to also start the max tracer */ |
| 1765 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) |
| 1766 | return tracing_start(); |
| 1767 | |
| 1768 | raw_spin_lock_irqsave(&tr->start_lock, flags); |
| 1769 | |
| 1770 | if (--tr->stop_count) { |
| 1771 | if (tr->stop_count < 0) { |
| 1772 | /* Someone screwed up their debugging */ |
| 1773 | WARN_ON_ONCE(1); |
| 1774 | tr->stop_count = 0; |
| 1775 | } |
| 1776 | goto out; |
| 1777 | } |
| 1778 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1779 | buffer = tr->trace_buffer.buffer; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1780 | if (buffer) |
| 1781 | ring_buffer_record_enable(buffer); |
| 1782 | |
| 1783 | out: |
| 1784 | raw_spin_unlock_irqrestore(&tr->start_lock, flags); |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | /** |
| 1788 | * tracing_stop - quick stop of the tracer |
| 1789 | * |
| 1790 | * Light weight way to stop tracing. Use in conjunction with |
| 1791 | * tracing_start. |
| 1792 | */ |
| 1793 | void tracing_stop(void) |
| 1794 | { |
| 1795 | struct ring_buffer *buffer; |
| 1796 | unsigned long flags; |
| 1797 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1798 | raw_spin_lock_irqsave(&global_trace.start_lock, flags); |
| 1799 | if (global_trace.stop_count++) |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1800 | goto out; |
| 1801 | |
Steven Rostedt | a2f8071 | 2010-03-12 19:56:00 -0500 | [diff] [blame] | 1802 | /* Prevent the buffers from switching */ |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1803 | arch_spin_lock(&global_trace.max_lock); |
Steven Rostedt | a2f8071 | 2010-03-12 19:56:00 -0500 | [diff] [blame] | 1804 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1805 | buffer = global_trace.trace_buffer.buffer; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1806 | if (buffer) |
| 1807 | ring_buffer_record_disable(buffer); |
| 1808 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1809 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 1810 | buffer = global_trace.max_buffer.buffer; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1811 | if (buffer) |
| 1812 | ring_buffer_record_disable(buffer); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1813 | #endif |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1814 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1815 | arch_spin_unlock(&global_trace.max_lock); |
Steven Rostedt | a2f8071 | 2010-03-12 19:56:00 -0500 | [diff] [blame] | 1816 | |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1817 | out: |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1818 | raw_spin_unlock_irqrestore(&global_trace.start_lock, flags); |
| 1819 | } |
| 1820 | |
| 1821 | static void tracing_stop_tr(struct trace_array *tr) |
| 1822 | { |
| 1823 | struct ring_buffer *buffer; |
| 1824 | unsigned long flags; |
| 1825 | |
| 1826 | /* If global, we need to also stop the max tracer */ |
| 1827 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) |
| 1828 | return tracing_stop(); |
| 1829 | |
| 1830 | raw_spin_lock_irqsave(&tr->start_lock, flags); |
| 1831 | if (tr->stop_count++) |
| 1832 | goto out; |
| 1833 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1834 | buffer = tr->trace_buffer.buffer; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1835 | if (buffer) |
| 1836 | ring_buffer_record_disable(buffer); |
| 1837 | |
| 1838 | out: |
| 1839 | raw_spin_unlock_irqrestore(&tr->start_lock, flags); |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1840 | } |
| 1841 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1842 | void trace_stop_cmdline_recording(void); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1843 | |
Steven Rostedt (Red Hat) | 379cfda | 2014-05-30 09:42:39 -0400 | [diff] [blame] | 1844 | static int trace_save_cmdline(struct task_struct *tsk) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1845 | { |
Carsten Emde | a635cf0 | 2009-03-18 09:00:41 +0100 | [diff] [blame] | 1846 | unsigned pid, idx; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1847 | |
| 1848 | if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT)) |
Steven Rostedt (Red Hat) | 379cfda | 2014-05-30 09:42:39 -0400 | [diff] [blame] | 1849 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1850 | |
| 1851 | /* |
| 1852 | * It's not the end of the world if we don't get |
| 1853 | * the lock, but we also don't want to spin |
| 1854 | * nor do we want to disable interrupts, |
| 1855 | * so if we miss here, then better luck next time. |
| 1856 | */ |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 1857 | if (!arch_spin_trylock(&trace_cmdline_lock)) |
Steven Rostedt (Red Hat) | 379cfda | 2014-05-30 09:42:39 -0400 | [diff] [blame] | 1858 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1859 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1860 | idx = savedcmd->map_pid_to_cmdline[tsk->pid]; |
Thomas Gleixner | 2c7eea4 | 2009-03-18 09:03:19 +0100 | [diff] [blame] | 1861 | if (idx == NO_CMDLINE_MAP) { |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1862 | idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1863 | |
Carsten Emde | a635cf0 | 2009-03-18 09:00:41 +0100 | [diff] [blame] | 1864 | /* |
| 1865 | * Check whether the cmdline buffer at idx has a pid |
| 1866 | * mapped. We are going to overwrite that entry so we |
| 1867 | * need to clear the map_pid_to_cmdline. Otherwise we |
| 1868 | * would read the new comm for the old pid. |
| 1869 | */ |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1870 | pid = savedcmd->map_cmdline_to_pid[idx]; |
Carsten Emde | a635cf0 | 2009-03-18 09:00:41 +0100 | [diff] [blame] | 1871 | if (pid != NO_CMDLINE_MAP) |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1872 | savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1873 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1874 | savedcmd->map_cmdline_to_pid[idx] = tsk->pid; |
| 1875 | savedcmd->map_pid_to_cmdline[tsk->pid] = idx; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1876 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1877 | savedcmd->cmdline_idx = idx; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1878 | } |
| 1879 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1880 | set_cmdline(idx, tsk->comm); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1881 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 1882 | arch_spin_unlock(&trace_cmdline_lock); |
Steven Rostedt (Red Hat) | 379cfda | 2014-05-30 09:42:39 -0400 | [diff] [blame] | 1883 | |
| 1884 | return 1; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1885 | } |
| 1886 | |
Steven Rostedt (Red Hat) | 4c27e75 | 2014-05-30 10:49:46 -0400 | [diff] [blame] | 1887 | static void __trace_find_cmdline(int pid, char comm[]) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1888 | { |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1889 | unsigned map; |
| 1890 | |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1891 | if (!pid) { |
| 1892 | strcpy(comm, "<idle>"); |
| 1893 | return; |
| 1894 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1895 | |
Steven Rostedt | 74bf407 | 2010-01-25 15:11:53 -0500 | [diff] [blame] | 1896 | if (WARN_ON_ONCE(pid < 0)) { |
| 1897 | strcpy(comm, "<XXX>"); |
| 1898 | return; |
| 1899 | } |
| 1900 | |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1901 | if (pid > PID_MAX_DEFAULT) { |
| 1902 | strcpy(comm, "<...>"); |
| 1903 | return; |
| 1904 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1905 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1906 | map = savedcmd->map_pid_to_cmdline[pid]; |
Thomas Gleixner | 50d8875 | 2009-03-18 08:58:44 +0100 | [diff] [blame] | 1907 | if (map != NO_CMDLINE_MAP) |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 1908 | strcpy(comm, get_saved_cmdlines(map)); |
Thomas Gleixner | 50d8875 | 2009-03-18 08:58:44 +0100 | [diff] [blame] | 1909 | else |
| 1910 | strcpy(comm, "<...>"); |
Steven Rostedt (Red Hat) | 4c27e75 | 2014-05-30 10:49:46 -0400 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | void trace_find_cmdline(int pid, char comm[]) |
| 1914 | { |
| 1915 | preempt_disable(); |
| 1916 | arch_spin_lock(&trace_cmdline_lock); |
| 1917 | |
| 1918 | __trace_find_cmdline(pid, comm); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1919 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 1920 | arch_spin_unlock(&trace_cmdline_lock); |
Heiko Carstens | 5b6045a | 2009-05-26 17:28:02 +0200 | [diff] [blame] | 1921 | preempt_enable(); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1922 | } |
| 1923 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1924 | void tracing_record_cmdline(struct task_struct *tsk) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1925 | { |
Steven Rostedt | 0fb9656 | 2012-05-11 14:25:30 -0400 | [diff] [blame] | 1926 | if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on()) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1927 | return; |
| 1928 | |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 1929 | if (!__this_cpu_read(trace_cmdline_save)) |
| 1930 | return; |
| 1931 | |
Steven Rostedt (Red Hat) | 379cfda | 2014-05-30 09:42:39 -0400 | [diff] [blame] | 1932 | if (trace_save_cmdline(tsk)) |
| 1933 | __this_cpu_write(trace_cmdline_save, false); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1934 | } |
| 1935 | |
Pekka Paalanen | 45dcd8b | 2008-09-16 21:56:41 +0300 | [diff] [blame] | 1936 | void |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 1937 | tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags, |
| 1938 | int pc) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1939 | { |
| 1940 | struct task_struct *tsk = current; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1941 | |
Steven Rostedt | 777e208 | 2008-09-29 23:02:42 -0400 | [diff] [blame] | 1942 | entry->preempt_count = pc & 0xff; |
| 1943 | entry->pid = (tsk) ? tsk->pid : 0; |
| 1944 | entry->flags = |
Steven Rostedt | 9244489 | 2008-10-24 09:42:59 -0400 | [diff] [blame] | 1945 | #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT |
Steven Rostedt | 2e2ca15 | 2008-08-01 12:26:40 -0400 | [diff] [blame] | 1946 | (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) | |
Steven Rostedt | 9244489 | 2008-10-24 09:42:59 -0400 | [diff] [blame] | 1947 | #else |
| 1948 | TRACE_FLAG_IRQS_NOSUPPORT | |
| 1949 | #endif |
Peter Zijlstra | 7e6867b | 2016-03-18 16:28:04 +0100 | [diff] [blame] | 1950 | ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1951 | ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) | |
| 1952 | ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) | |
Peter Zijlstra | e5137b5 | 2013-10-04 17:28:26 +0200 | [diff] [blame] | 1953 | (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) | |
| 1954 | (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1955 | } |
Frederic Weisbecker | f413cdb | 2009-08-07 01:25:54 +0200 | [diff] [blame] | 1956 | EXPORT_SYMBOL_GPL(tracing_generic_entry_update); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1957 | |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 1958 | struct ring_buffer_event * |
| 1959 | trace_buffer_lock_reserve(struct ring_buffer *buffer, |
| 1960 | int type, |
| 1961 | unsigned long len, |
| 1962 | unsigned long flags, int pc) |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 1963 | { |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 1964 | return __trace_buffer_lock_reserve(buffer, type, len, flags, pc); |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 1965 | } |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 1966 | |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 1967 | DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event); |
| 1968 | DEFINE_PER_CPU(int, trace_buffered_event_cnt); |
| 1969 | static int trace_buffered_event_ref; |
| 1970 | |
| 1971 | /** |
| 1972 | * trace_buffered_event_enable - enable buffering events |
| 1973 | * |
| 1974 | * When events are being filtered, it is quicker to use a temporary |
| 1975 | * buffer to write the event data into if there's a likely chance |
| 1976 | * that it will not be committed. The discard of the ring buffer |
| 1977 | * is not as fast as committing, and is much slower than copying |
| 1978 | * a commit. |
| 1979 | * |
| 1980 | * When an event is to be filtered, allocate per cpu buffers to |
| 1981 | * write the event data into, and if the event is filtered and discarded |
| 1982 | * it is simply dropped, otherwise, the entire data is to be committed |
| 1983 | * in one shot. |
| 1984 | */ |
| 1985 | void trace_buffered_event_enable(void) |
| 1986 | { |
| 1987 | struct ring_buffer_event *event; |
| 1988 | struct page *page; |
| 1989 | int cpu; |
| 1990 | |
| 1991 | WARN_ON_ONCE(!mutex_is_locked(&event_mutex)); |
| 1992 | |
| 1993 | if (trace_buffered_event_ref++) |
| 1994 | return; |
| 1995 | |
| 1996 | for_each_tracing_cpu(cpu) { |
| 1997 | page = alloc_pages_node(cpu_to_node(cpu), |
| 1998 | GFP_KERNEL | __GFP_NORETRY, 0); |
| 1999 | if (!page) |
| 2000 | goto failed; |
| 2001 | |
| 2002 | event = page_address(page); |
| 2003 | memset(event, 0, sizeof(*event)); |
| 2004 | |
| 2005 | per_cpu(trace_buffered_event, cpu) = event; |
| 2006 | |
| 2007 | preempt_disable(); |
| 2008 | if (cpu == smp_processor_id() && |
| 2009 | this_cpu_read(trace_buffered_event) != |
| 2010 | per_cpu(trace_buffered_event, cpu)) |
| 2011 | WARN_ON_ONCE(1); |
| 2012 | preempt_enable(); |
| 2013 | } |
| 2014 | |
| 2015 | return; |
| 2016 | failed: |
| 2017 | trace_buffered_event_disable(); |
| 2018 | } |
| 2019 | |
| 2020 | static void enable_trace_buffered_event(void *data) |
| 2021 | { |
| 2022 | /* Probably not needed, but do it anyway */ |
| 2023 | smp_rmb(); |
| 2024 | this_cpu_dec(trace_buffered_event_cnt); |
| 2025 | } |
| 2026 | |
| 2027 | static void disable_trace_buffered_event(void *data) |
| 2028 | { |
| 2029 | this_cpu_inc(trace_buffered_event_cnt); |
| 2030 | } |
| 2031 | |
| 2032 | /** |
| 2033 | * trace_buffered_event_disable - disable buffering events |
| 2034 | * |
| 2035 | * When a filter is removed, it is faster to not use the buffered |
| 2036 | * events, and to commit directly into the ring buffer. Free up |
| 2037 | * the temp buffers when there are no more users. This requires |
| 2038 | * special synchronization with current events. |
| 2039 | */ |
| 2040 | void trace_buffered_event_disable(void) |
| 2041 | { |
| 2042 | int cpu; |
| 2043 | |
| 2044 | WARN_ON_ONCE(!mutex_is_locked(&event_mutex)); |
| 2045 | |
| 2046 | if (WARN_ON_ONCE(!trace_buffered_event_ref)) |
| 2047 | return; |
| 2048 | |
| 2049 | if (--trace_buffered_event_ref) |
| 2050 | return; |
| 2051 | |
| 2052 | preempt_disable(); |
| 2053 | /* For each CPU, set the buffer as used. */ |
| 2054 | smp_call_function_many(tracing_buffer_mask, |
| 2055 | disable_trace_buffered_event, NULL, 1); |
| 2056 | preempt_enable(); |
| 2057 | |
| 2058 | /* Wait for all current users to finish */ |
| 2059 | synchronize_sched(); |
| 2060 | |
| 2061 | for_each_tracing_cpu(cpu) { |
| 2062 | free_page((unsigned long)per_cpu(trace_buffered_event, cpu)); |
| 2063 | per_cpu(trace_buffered_event, cpu) = NULL; |
| 2064 | } |
| 2065 | /* |
| 2066 | * Make sure trace_buffered_event is NULL before clearing |
| 2067 | * trace_buffered_event_cnt. |
| 2068 | */ |
| 2069 | smp_wmb(); |
| 2070 | |
| 2071 | preempt_disable(); |
| 2072 | /* Do the work on each cpu */ |
| 2073 | smp_call_function_many(tracing_buffer_mask, |
| 2074 | enable_trace_buffered_event, NULL, 1); |
| 2075 | preempt_enable(); |
| 2076 | } |
| 2077 | |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2078 | static struct ring_buffer *temp_buffer; |
| 2079 | |
Steven Rostedt | ef5580d | 2009-02-27 19:38:04 -0500 | [diff] [blame] | 2080 | struct ring_buffer_event * |
Steven Rostedt | ccb469a | 2012-08-02 10:32:10 -0400 | [diff] [blame] | 2081 | trace_event_buffer_lock_reserve(struct ring_buffer **current_rb, |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 2082 | struct trace_event_file *trace_file, |
Steven Rostedt | ccb469a | 2012-08-02 10:32:10 -0400 | [diff] [blame] | 2083 | int type, unsigned long len, |
| 2084 | unsigned long flags, int pc) |
| 2085 | { |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2086 | struct ring_buffer_event *entry; |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 2087 | int val; |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2088 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 2089 | *current_rb = trace_file->tr->trace_buffer.buffer; |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 2090 | |
| 2091 | if ((trace_file->flags & |
| 2092 | (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) && |
| 2093 | (entry = this_cpu_read(trace_buffered_event))) { |
| 2094 | /* Try to use the per cpu buffer first */ |
| 2095 | val = this_cpu_inc_return(trace_buffered_event_cnt); |
| 2096 | if (val == 1) { |
| 2097 | trace_event_setup(entry, type, flags, pc); |
| 2098 | entry->array[0] = len; |
| 2099 | return entry; |
| 2100 | } |
| 2101 | this_cpu_dec(trace_buffered_event_cnt); |
| 2102 | } |
| 2103 | |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2104 | entry = __trace_buffer_lock_reserve(*current_rb, |
| 2105 | type, len, flags, pc); |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2106 | /* |
| 2107 | * If tracing is off, but we have triggers enabled |
| 2108 | * we still need to look at the event data. Use the temp_buffer |
| 2109 | * to store the trace event for the tigger to use. It's recusive |
| 2110 | * safe and will not be recorded anywhere. |
| 2111 | */ |
Steven Rostedt (Red Hat) | 5d6ad96 | 2015-05-13 15:12:33 -0400 | [diff] [blame] | 2112 | if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) { |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2113 | *current_rb = temp_buffer; |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2114 | entry = __trace_buffer_lock_reserve(*current_rb, |
| 2115 | type, len, flags, pc); |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2116 | } |
| 2117 | return entry; |
Steven Rostedt | ccb469a | 2012-08-02 10:32:10 -0400 | [diff] [blame] | 2118 | } |
| 2119 | EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve); |
| 2120 | |
Steven Rostedt (Red Hat) | 42391745 | 2016-11-23 15:52:45 -0500 | [diff] [blame] | 2121 | static DEFINE_SPINLOCK(tracepoint_iter_lock); |
| 2122 | static DEFINE_MUTEX(tracepoint_printk_mutex); |
| 2123 | |
| 2124 | static void output_printk(struct trace_event_buffer *fbuffer) |
| 2125 | { |
| 2126 | struct trace_event_call *event_call; |
| 2127 | struct trace_event *event; |
| 2128 | unsigned long flags; |
| 2129 | struct trace_iterator *iter = tracepoint_print_iter; |
| 2130 | |
| 2131 | /* We should never get here if iter is NULL */ |
| 2132 | if (WARN_ON_ONCE(!iter)) |
| 2133 | return; |
| 2134 | |
| 2135 | event_call = fbuffer->trace_file->event_call; |
| 2136 | if (!event_call || !event_call->event.funcs || |
| 2137 | !event_call->event.funcs->trace) |
| 2138 | return; |
| 2139 | |
| 2140 | event = &fbuffer->trace_file->event_call->event; |
| 2141 | |
| 2142 | spin_lock_irqsave(&tracepoint_iter_lock, flags); |
| 2143 | trace_seq_init(&iter->seq); |
| 2144 | iter->ent = fbuffer->entry; |
| 2145 | event_call->event.funcs->trace(iter, 0, event); |
| 2146 | trace_seq_putc(&iter->seq, 0); |
| 2147 | printk("%s", iter->seq.buffer); |
| 2148 | |
| 2149 | spin_unlock_irqrestore(&tracepoint_iter_lock, flags); |
| 2150 | } |
| 2151 | |
| 2152 | int tracepoint_printk_sysctl(struct ctl_table *table, int write, |
| 2153 | void __user *buffer, size_t *lenp, |
| 2154 | loff_t *ppos) |
| 2155 | { |
| 2156 | int save_tracepoint_printk; |
| 2157 | int ret; |
| 2158 | |
| 2159 | mutex_lock(&tracepoint_printk_mutex); |
| 2160 | save_tracepoint_printk = tracepoint_printk; |
| 2161 | |
| 2162 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 2163 | |
| 2164 | /* |
| 2165 | * This will force exiting early, as tracepoint_printk |
| 2166 | * is always zero when tracepoint_printk_iter is not allocated |
| 2167 | */ |
| 2168 | if (!tracepoint_print_iter) |
| 2169 | tracepoint_printk = 0; |
| 2170 | |
| 2171 | if (save_tracepoint_printk == tracepoint_printk) |
| 2172 | goto out; |
| 2173 | |
| 2174 | if (tracepoint_printk) |
| 2175 | static_key_enable(&tracepoint_printk_key.key); |
| 2176 | else |
| 2177 | static_key_disable(&tracepoint_printk_key.key); |
| 2178 | |
| 2179 | out: |
| 2180 | mutex_unlock(&tracepoint_printk_mutex); |
| 2181 | |
| 2182 | return ret; |
| 2183 | } |
| 2184 | |
| 2185 | void trace_event_buffer_commit(struct trace_event_buffer *fbuffer) |
| 2186 | { |
| 2187 | if (static_key_false(&tracepoint_printk_key.key)) |
| 2188 | output_printk(fbuffer); |
| 2189 | |
| 2190 | event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer, |
| 2191 | fbuffer->event, fbuffer->entry, |
| 2192 | fbuffer->flags, fbuffer->pc); |
| 2193 | } |
| 2194 | EXPORT_SYMBOL_GPL(trace_event_buffer_commit); |
| 2195 | |
Steven Rostedt (Red Hat) | b7f0c95 | 2015-09-25 17:38:44 -0400 | [diff] [blame] | 2196 | void trace_buffer_unlock_commit_regs(struct trace_array *tr, |
| 2197 | struct ring_buffer *buffer, |
Steven Rostedt | 0d5c6e1 | 2012-11-01 20:54:21 -0400 | [diff] [blame] | 2198 | struct ring_buffer_event *event, |
| 2199 | unsigned long flags, int pc, |
| 2200 | struct pt_regs *regs) |
Masami Hiramatsu | 1fd8df2 | 2011-06-08 16:09:34 +0900 | [diff] [blame] | 2201 | { |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 2202 | __buffer_unlock_commit(buffer, event); |
Masami Hiramatsu | 1fd8df2 | 2011-06-08 16:09:34 +0900 | [diff] [blame] | 2203 | |
Steven Rostedt (Red Hat) | be54f69 | 2016-06-23 14:03:47 -0400 | [diff] [blame] | 2204 | /* |
| 2205 | * If regs is not set, then skip the following callers: |
| 2206 | * trace_buffer_unlock_commit_regs |
| 2207 | * event_trigger_unlock_commit |
| 2208 | * trace_event_buffer_commit |
| 2209 | * trace_event_raw_event_sched_switch |
| 2210 | * Note, we can still get here via blktrace, wakeup tracer |
| 2211 | * and mmiotrace, but that's ok if they lose a function or |
| 2212 | * two. They are that meaningful. |
| 2213 | */ |
| 2214 | ftrace_trace_stack(tr, buffer, flags, regs ? 0 : 4, pc, regs); |
Masami Hiramatsu | 1fd8df2 | 2011-06-08 16:09:34 +0900 | [diff] [blame] | 2215 | ftrace_trace_userstack(buffer, flags, pc); |
| 2216 | } |
Masami Hiramatsu | 1fd8df2 | 2011-06-08 16:09:34 +0900 | [diff] [blame] | 2217 | |
Steven Rostedt (Red Hat) | 52ffabe3 | 2016-11-23 20:28:38 -0500 | [diff] [blame^] | 2218 | /* |
| 2219 | * Similar to trace_buffer_unlock_commit_regs() but do not dump stack. |
| 2220 | */ |
| 2221 | void |
| 2222 | trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer, |
| 2223 | struct ring_buffer_event *event) |
| 2224 | { |
| 2225 | __buffer_unlock_commit(buffer, event); |
| 2226 | } |
| 2227 | |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 2228 | static void |
| 2229 | trace_process_export(struct trace_export *export, |
| 2230 | struct ring_buffer_event *event) |
| 2231 | { |
| 2232 | struct trace_entry *entry; |
| 2233 | unsigned int size = 0; |
| 2234 | |
| 2235 | entry = ring_buffer_event_data(event); |
| 2236 | size = ring_buffer_event_length(event); |
| 2237 | export->write(entry, size); |
| 2238 | } |
| 2239 | |
| 2240 | static DEFINE_MUTEX(ftrace_export_lock); |
| 2241 | |
| 2242 | static struct trace_export __rcu *ftrace_exports_list __read_mostly; |
| 2243 | |
| 2244 | static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled); |
| 2245 | |
| 2246 | static inline void ftrace_exports_enable(void) |
| 2247 | { |
| 2248 | static_branch_enable(&ftrace_exports_enabled); |
| 2249 | } |
| 2250 | |
| 2251 | static inline void ftrace_exports_disable(void) |
| 2252 | { |
| 2253 | static_branch_disable(&ftrace_exports_enabled); |
| 2254 | } |
| 2255 | |
| 2256 | void ftrace_exports(struct ring_buffer_event *event) |
| 2257 | { |
| 2258 | struct trace_export *export; |
| 2259 | |
| 2260 | preempt_disable_notrace(); |
| 2261 | |
| 2262 | export = rcu_dereference_raw_notrace(ftrace_exports_list); |
| 2263 | while (export) { |
| 2264 | trace_process_export(export, event); |
| 2265 | export = rcu_dereference_raw_notrace(export->next); |
| 2266 | } |
| 2267 | |
| 2268 | preempt_enable_notrace(); |
| 2269 | } |
| 2270 | |
| 2271 | static inline void |
| 2272 | add_trace_export(struct trace_export **list, struct trace_export *export) |
| 2273 | { |
| 2274 | rcu_assign_pointer(export->next, *list); |
| 2275 | /* |
| 2276 | * We are entering export into the list but another |
| 2277 | * CPU might be walking that list. We need to make sure |
| 2278 | * the export->next pointer is valid before another CPU sees |
| 2279 | * the export pointer included into the list. |
| 2280 | */ |
| 2281 | rcu_assign_pointer(*list, export); |
| 2282 | } |
| 2283 | |
| 2284 | static inline int |
| 2285 | rm_trace_export(struct trace_export **list, struct trace_export *export) |
| 2286 | { |
| 2287 | struct trace_export **p; |
| 2288 | |
| 2289 | for (p = list; *p != NULL; p = &(*p)->next) |
| 2290 | if (*p == export) |
| 2291 | break; |
| 2292 | |
| 2293 | if (*p != export) |
| 2294 | return -1; |
| 2295 | |
| 2296 | rcu_assign_pointer(*p, (*p)->next); |
| 2297 | |
| 2298 | return 0; |
| 2299 | } |
| 2300 | |
| 2301 | static inline void |
| 2302 | add_ftrace_export(struct trace_export **list, struct trace_export *export) |
| 2303 | { |
| 2304 | if (*list == NULL) |
| 2305 | ftrace_exports_enable(); |
| 2306 | |
| 2307 | add_trace_export(list, export); |
| 2308 | } |
| 2309 | |
| 2310 | static inline int |
| 2311 | rm_ftrace_export(struct trace_export **list, struct trace_export *export) |
| 2312 | { |
| 2313 | int ret; |
| 2314 | |
| 2315 | ret = rm_trace_export(list, export); |
| 2316 | if (*list == NULL) |
| 2317 | ftrace_exports_disable(); |
| 2318 | |
| 2319 | return ret; |
| 2320 | } |
| 2321 | |
| 2322 | int register_ftrace_export(struct trace_export *export) |
| 2323 | { |
| 2324 | if (WARN_ON_ONCE(!export->write)) |
| 2325 | return -1; |
| 2326 | |
| 2327 | mutex_lock(&ftrace_export_lock); |
| 2328 | |
| 2329 | add_ftrace_export(&ftrace_exports_list, export); |
| 2330 | |
| 2331 | mutex_unlock(&ftrace_export_lock); |
| 2332 | |
| 2333 | return 0; |
| 2334 | } |
| 2335 | EXPORT_SYMBOL_GPL(register_ftrace_export); |
| 2336 | |
| 2337 | int unregister_ftrace_export(struct trace_export *export) |
| 2338 | { |
| 2339 | int ret; |
| 2340 | |
| 2341 | mutex_lock(&ftrace_export_lock); |
| 2342 | |
| 2343 | ret = rm_ftrace_export(&ftrace_exports_list, export); |
| 2344 | |
| 2345 | mutex_unlock(&ftrace_export_lock); |
| 2346 | |
| 2347 | return ret; |
| 2348 | } |
| 2349 | EXPORT_SYMBOL_GPL(unregister_ftrace_export); |
| 2350 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2351 | void |
Arnaldo Carvalho de Melo | 7be4215 | 2009-02-05 01:13:37 -0500 | [diff] [blame] | 2352 | trace_function(struct trace_array *tr, |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 2353 | unsigned long ip, unsigned long parent_ip, unsigned long flags, |
| 2354 | int pc) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2355 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 2356 | struct trace_event_call *call = &event_function; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2357 | struct ring_buffer *buffer = tr->trace_buffer.buffer; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2358 | struct ring_buffer_event *event; |
Steven Rostedt | 777e208 | 2008-09-29 23:02:42 -0400 | [diff] [blame] | 2359 | struct ftrace_entry *entry; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2360 | |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2361 | event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry), |
| 2362 | flags, pc); |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2363 | if (!event) |
| 2364 | return; |
| 2365 | entry = ring_buffer_event_data(event); |
Steven Rostedt | 777e208 | 2008-09-29 23:02:42 -0400 | [diff] [blame] | 2366 | entry->ip = ip; |
| 2367 | entry->parent_ip = parent_ip; |
Tom Zanussi | e1112b4 | 2009-03-31 00:48:49 -0500 | [diff] [blame] | 2368 | |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 2369 | if (!call_filter_check_discard(call, entry, buffer, event)) { |
| 2370 | if (static_branch_unlikely(&ftrace_exports_enabled)) |
| 2371 | ftrace_exports(event); |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 2372 | __buffer_unlock_commit(buffer, event); |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 2373 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2374 | } |
| 2375 | |
Frederic Weisbecker | c0a0d0d | 2009-07-29 17:51:13 +0200 | [diff] [blame] | 2376 | #ifdef CONFIG_STACKTRACE |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2377 | |
| 2378 | #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long)) |
| 2379 | struct ftrace_stack { |
| 2380 | unsigned long calls[FTRACE_STACK_MAX_ENTRIES]; |
| 2381 | }; |
| 2382 | |
| 2383 | static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack); |
| 2384 | static DEFINE_PER_CPU(int, ftrace_stack_reserve); |
| 2385 | |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 2386 | static void __ftrace_trace_stack(struct ring_buffer *buffer, |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 2387 | unsigned long flags, |
Masami Hiramatsu | 1fd8df2 | 2011-06-08 16:09:34 +0900 | [diff] [blame] | 2388 | int skip, int pc, struct pt_regs *regs) |
Ingo Molnar | 86387f7 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2389 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 2390 | struct trace_event_call *call = &event_kernel_stack; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2391 | struct ring_buffer_event *event; |
Steven Rostedt | 777e208 | 2008-09-29 23:02:42 -0400 | [diff] [blame] | 2392 | struct stack_entry *entry; |
Ingo Molnar | 86387f7 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2393 | struct stack_trace trace; |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2394 | int use_stack; |
| 2395 | int size = FTRACE_STACK_ENTRIES; |
Ingo Molnar | 86387f7 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2396 | |
| 2397 | trace.nr_entries = 0; |
Ingo Molnar | 86387f7 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2398 | trace.skip = skip; |
Ingo Molnar | 86387f7 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2399 | |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2400 | /* |
Steven Rostedt (Red Hat) | be54f69 | 2016-06-23 14:03:47 -0400 | [diff] [blame] | 2401 | * Add two, for this function and the call to save_stack_trace() |
| 2402 | * If regs is set, then these functions will not be in the way. |
| 2403 | */ |
| 2404 | if (!regs) |
| 2405 | trace.skip += 2; |
| 2406 | |
| 2407 | /* |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2408 | * Since events can happen in NMIs there's no safe way to |
| 2409 | * use the per cpu ftrace_stacks. We reserve it and if an interrupt |
| 2410 | * or NMI comes in, it will just have to use the default |
| 2411 | * FTRACE_STACK_SIZE. |
| 2412 | */ |
| 2413 | preempt_disable_notrace(); |
| 2414 | |
Shan Wei | 8214652 | 2012-11-19 13:21:01 +0800 | [diff] [blame] | 2415 | use_stack = __this_cpu_inc_return(ftrace_stack_reserve); |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2416 | /* |
| 2417 | * We don't need any atomic variables, just a barrier. |
| 2418 | * If an interrupt comes in, we don't care, because it would |
| 2419 | * have exited and put the counter back to what we want. |
| 2420 | * We just need a barrier to keep gcc from moving things |
| 2421 | * around. |
| 2422 | */ |
| 2423 | barrier(); |
| 2424 | if (use_stack == 1) { |
Christoph Lameter | bdffd89 | 2014-04-29 14:17:40 -0500 | [diff] [blame] | 2425 | trace.entries = this_cpu_ptr(ftrace_stack.calls); |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2426 | trace.max_entries = FTRACE_STACK_MAX_ENTRIES; |
| 2427 | |
| 2428 | if (regs) |
| 2429 | save_stack_trace_regs(regs, &trace); |
| 2430 | else |
| 2431 | save_stack_trace(&trace); |
| 2432 | |
| 2433 | if (trace.nr_entries > size) |
| 2434 | size = trace.nr_entries; |
| 2435 | } else |
| 2436 | /* From now on, use_stack is a boolean */ |
| 2437 | use_stack = 0; |
| 2438 | |
| 2439 | size *= sizeof(unsigned long); |
| 2440 | |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2441 | event = __trace_buffer_lock_reserve(buffer, TRACE_STACK, |
| 2442 | sizeof(*entry) + size, flags, pc); |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2443 | if (!event) |
| 2444 | goto out; |
| 2445 | entry = ring_buffer_event_data(event); |
| 2446 | |
| 2447 | memset(&entry->caller, 0, size); |
| 2448 | |
| 2449 | if (use_stack) |
| 2450 | memcpy(&entry->caller, trace.entries, |
| 2451 | trace.nr_entries * sizeof(unsigned long)); |
| 2452 | else { |
| 2453 | trace.max_entries = FTRACE_STACK_ENTRIES; |
| 2454 | trace.entries = entry->caller; |
| 2455 | if (regs) |
| 2456 | save_stack_trace_regs(regs, &trace); |
| 2457 | else |
| 2458 | save_stack_trace(&trace); |
| 2459 | } |
| 2460 | |
| 2461 | entry->size = trace.nr_entries; |
| 2462 | |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2463 | if (!call_filter_check_discard(call, entry, buffer, event)) |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 2464 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2465 | |
| 2466 | out: |
| 2467 | /* Again, don't let gcc optimize things here */ |
| 2468 | barrier(); |
Shan Wei | 8214652 | 2012-11-19 13:21:01 +0800 | [diff] [blame] | 2469 | __this_cpu_dec(ftrace_stack_reserve); |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2470 | preempt_enable_notrace(); |
| 2471 | |
Ingo Molnar | f0a920d | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 2472 | } |
| 2473 | |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 2474 | static inline void ftrace_trace_stack(struct trace_array *tr, |
| 2475 | struct ring_buffer *buffer, |
Steven Rostedt (Red Hat) | 73dddbb | 2015-09-29 15:38:55 -0400 | [diff] [blame] | 2476 | unsigned long flags, |
| 2477 | int skip, int pc, struct pt_regs *regs) |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 2478 | { |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 2479 | if (!(tr->trace_flags & TRACE_ITER_STACKTRACE)) |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 2480 | return; |
| 2481 | |
Steven Rostedt (Red Hat) | 73dddbb | 2015-09-29 15:38:55 -0400 | [diff] [blame] | 2482 | __ftrace_trace_stack(buffer, flags, skip, pc, regs); |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 2483 | } |
| 2484 | |
Frederic Weisbecker | c0a0d0d | 2009-07-29 17:51:13 +0200 | [diff] [blame] | 2485 | void __trace_stack(struct trace_array *tr, unsigned long flags, int skip, |
| 2486 | int pc) |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 2487 | { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2488 | __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL); |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 2489 | } |
| 2490 | |
Steven Rostedt | 0388938 | 2009-12-11 09:48:22 -0500 | [diff] [blame] | 2491 | /** |
| 2492 | * trace_dump_stack - record a stack back trace in the trace buffer |
Steven Rostedt (Red Hat) | c142be8 | 2013-03-13 09:55:57 -0400 | [diff] [blame] | 2493 | * @skip: Number of functions to skip (helper handlers) |
Steven Rostedt | 0388938 | 2009-12-11 09:48:22 -0500 | [diff] [blame] | 2494 | */ |
Steven Rostedt (Red Hat) | c142be8 | 2013-03-13 09:55:57 -0400 | [diff] [blame] | 2495 | void trace_dump_stack(int skip) |
Steven Rostedt | 0388938 | 2009-12-11 09:48:22 -0500 | [diff] [blame] | 2496 | { |
| 2497 | unsigned long flags; |
| 2498 | |
| 2499 | if (tracing_disabled || tracing_selftest_running) |
Steven Rostedt | e36c545 | 2009-12-14 15:58:33 -0500 | [diff] [blame] | 2500 | return; |
Steven Rostedt | 0388938 | 2009-12-11 09:48:22 -0500 | [diff] [blame] | 2501 | |
| 2502 | local_save_flags(flags); |
| 2503 | |
Steven Rostedt (Red Hat) | c142be8 | 2013-03-13 09:55:57 -0400 | [diff] [blame] | 2504 | /* |
| 2505 | * Skip 3 more, seems to get us at the caller of |
| 2506 | * this function. |
| 2507 | */ |
| 2508 | skip += 3; |
| 2509 | __ftrace_trace_stack(global_trace.trace_buffer.buffer, |
| 2510 | flags, skip, preempt_count(), NULL); |
Steven Rostedt | 0388938 | 2009-12-11 09:48:22 -0500 | [diff] [blame] | 2511 | } |
| 2512 | |
Steven Rostedt | 91e86e5 | 2010-11-10 12:56:12 +0100 | [diff] [blame] | 2513 | static DEFINE_PER_CPU(int, user_stack_count); |
| 2514 | |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 2515 | void |
| 2516 | ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc) |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2517 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 2518 | struct trace_event_call *call = &event_user_stack; |
Török Edwin | 8d7c6a9 | 2008-11-23 12:39:06 +0200 | [diff] [blame] | 2519 | struct ring_buffer_event *event; |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2520 | struct userstack_entry *entry; |
| 2521 | struct stack_trace trace; |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2522 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 2523 | if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE)) |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2524 | return; |
| 2525 | |
Steven Rostedt | b634587 | 2010-03-12 20:03:30 -0500 | [diff] [blame] | 2526 | /* |
| 2527 | * NMIs can not handle page faults, even with fix ups. |
| 2528 | * The save user stack can (and often does) fault. |
| 2529 | */ |
| 2530 | if (unlikely(in_nmi())) |
| 2531 | return; |
| 2532 | |
Steven Rostedt | 91e86e5 | 2010-11-10 12:56:12 +0100 | [diff] [blame] | 2533 | /* |
| 2534 | * prevent recursion, since the user stack tracing may |
| 2535 | * trigger other kernel events. |
| 2536 | */ |
| 2537 | preempt_disable(); |
| 2538 | if (__this_cpu_read(user_stack_count)) |
| 2539 | goto out; |
| 2540 | |
| 2541 | __this_cpu_inc(user_stack_count); |
| 2542 | |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2543 | event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK, |
| 2544 | sizeof(*entry), flags, pc); |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2545 | if (!event) |
Li Zefan | 1dbd195 | 2010-12-09 15:47:56 +0800 | [diff] [blame] | 2546 | goto out_drop_count; |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2547 | entry = ring_buffer_event_data(event); |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2548 | |
Steven Rostedt | 48659d3 | 2009-09-11 11:36:23 -0400 | [diff] [blame] | 2549 | entry->tgid = current->tgid; |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2550 | memset(&entry->caller, 0, sizeof(entry->caller)); |
| 2551 | |
| 2552 | trace.nr_entries = 0; |
| 2553 | trace.max_entries = FTRACE_STACK_ENTRIES; |
| 2554 | trace.skip = 0; |
| 2555 | trace.entries = entry->caller; |
| 2556 | |
| 2557 | save_stack_trace_user(&trace); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2558 | if (!call_filter_check_discard(call, entry, buffer, event)) |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 2559 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt | 91e86e5 | 2010-11-10 12:56:12 +0100 | [diff] [blame] | 2560 | |
Li Zefan | 1dbd195 | 2010-12-09 15:47:56 +0800 | [diff] [blame] | 2561 | out_drop_count: |
Steven Rostedt | 91e86e5 | 2010-11-10 12:56:12 +0100 | [diff] [blame] | 2562 | __this_cpu_dec(user_stack_count); |
Steven Rostedt | 91e86e5 | 2010-11-10 12:56:12 +0100 | [diff] [blame] | 2563 | out: |
| 2564 | preempt_enable(); |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2565 | } |
| 2566 | |
Hannes Eder | 4fd2735 | 2009-02-10 19:44:12 +0100 | [diff] [blame] | 2567 | #ifdef UNUSED |
| 2568 | static void __trace_userstack(struct trace_array *tr, unsigned long flags) |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2569 | { |
Arnaldo Carvalho de Melo | 7be4215 | 2009-02-05 01:13:37 -0500 | [diff] [blame] | 2570 | ftrace_trace_userstack(tr, flags, preempt_count()); |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2571 | } |
Hannes Eder | 4fd2735 | 2009-02-10 19:44:12 +0100 | [diff] [blame] | 2572 | #endif /* UNUSED */ |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 2573 | |
Frederic Weisbecker | c0a0d0d | 2009-07-29 17:51:13 +0200 | [diff] [blame] | 2574 | #endif /* CONFIG_STACKTRACE */ |
| 2575 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2576 | /* created for use with alloc_percpu */ |
| 2577 | struct trace_buffer_struct { |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 2578 | int nesting; |
| 2579 | char buffer[4][TRACE_BUF_SIZE]; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2580 | }; |
| 2581 | |
| 2582 | static struct trace_buffer_struct *trace_percpu_buffer; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2583 | |
| 2584 | /* |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 2585 | * Thise allows for lockless recording. If we're nested too deeply, then |
| 2586 | * this returns NULL. |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2587 | */ |
| 2588 | static char *get_trace_buf(void) |
| 2589 | { |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 2590 | struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer); |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2591 | |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 2592 | if (!buffer || buffer->nesting >= 4) |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2593 | return NULL; |
| 2594 | |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 2595 | return &buffer->buffer[buffer->nesting++][0]; |
| 2596 | } |
| 2597 | |
| 2598 | static void put_trace_buf(void) |
| 2599 | { |
| 2600 | this_cpu_dec(trace_percpu_buffer->nesting); |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | static int alloc_percpu_trace_buffer(void) |
| 2604 | { |
| 2605 | struct trace_buffer_struct *buffers; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2606 | |
| 2607 | buffers = alloc_percpu(struct trace_buffer_struct); |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 2608 | if (WARN(!buffers, "Could not allocate percpu trace_printk buffer")) |
| 2609 | return -ENOMEM; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2610 | |
| 2611 | trace_percpu_buffer = buffers; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2612 | return 0; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2613 | } |
| 2614 | |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 2615 | static int buffers_allocated; |
| 2616 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2617 | void trace_printk_init_buffers(void) |
| 2618 | { |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2619 | if (buffers_allocated) |
| 2620 | return; |
| 2621 | |
| 2622 | if (alloc_percpu_trace_buffer()) |
| 2623 | return; |
| 2624 | |
Steven Rostedt | 2184db4 | 2014-05-28 13:14:40 -0400 | [diff] [blame] | 2625 | /* trace_printk() is for debug use only. Don't use it in production. */ |
| 2626 | |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 2627 | pr_warn("\n"); |
| 2628 | pr_warn("**********************************************************\n"); |
| 2629 | pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); |
| 2630 | pr_warn("** **\n"); |
| 2631 | pr_warn("** trace_printk() being used. Allocating extra memory. **\n"); |
| 2632 | pr_warn("** **\n"); |
| 2633 | pr_warn("** This means that this is a DEBUG kernel and it is **\n"); |
| 2634 | pr_warn("** unsafe for production use. **\n"); |
| 2635 | pr_warn("** **\n"); |
| 2636 | pr_warn("** If you see this message and you are not debugging **\n"); |
| 2637 | pr_warn("** the kernel, report this immediately to your vendor! **\n"); |
| 2638 | pr_warn("** **\n"); |
| 2639 | pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); |
| 2640 | pr_warn("**********************************************************\n"); |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2641 | |
Steven Rostedt | b382ede6 | 2012-10-10 21:44:34 -0400 | [diff] [blame] | 2642 | /* Expand the buffers to set size */ |
| 2643 | tracing_update_buffers(); |
| 2644 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2645 | buffers_allocated = 1; |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 2646 | |
| 2647 | /* |
| 2648 | * trace_printk_init_buffers() can be called by modules. |
| 2649 | * If that happens, then we need to start cmdline recording |
| 2650 | * directly here. If the global_trace.buffer is already |
| 2651 | * allocated here, then this was called by module code. |
| 2652 | */ |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2653 | if (global_trace.trace_buffer.buffer) |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 2654 | tracing_start_cmdline_record(); |
| 2655 | } |
| 2656 | |
| 2657 | void trace_printk_start_comm(void) |
| 2658 | { |
| 2659 | /* Start tracing comms if trace printk is set */ |
| 2660 | if (!buffers_allocated) |
| 2661 | return; |
| 2662 | tracing_start_cmdline_record(); |
| 2663 | } |
| 2664 | |
| 2665 | static void trace_printk_start_stop_comm(int enabled) |
| 2666 | { |
| 2667 | if (!buffers_allocated) |
| 2668 | return; |
| 2669 | |
| 2670 | if (enabled) |
| 2671 | tracing_start_cmdline_record(); |
| 2672 | else |
| 2673 | tracing_stop_cmdline_record(); |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2674 | } |
| 2675 | |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2676 | /** |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2677 | * trace_vbprintk - write binary msg to tracing buffer |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2678 | * |
| 2679 | */ |
Steven Rostedt | 40ce74f | 2009-03-19 14:03:53 -0400 | [diff] [blame] | 2680 | int trace_vbprintk(unsigned long ip, const char *fmt, va_list args) |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2681 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 2682 | struct trace_event_call *call = &event_bprint; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2683 | struct ring_buffer_event *event; |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 2684 | struct ring_buffer *buffer; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2685 | struct trace_array *tr = &global_trace; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2686 | struct bprint_entry *entry; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2687 | unsigned long flags; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2688 | char *tbuffer; |
| 2689 | int len = 0, size, pc; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2690 | |
| 2691 | if (unlikely(tracing_selftest_running || tracing_disabled)) |
| 2692 | return 0; |
| 2693 | |
| 2694 | /* Don't pollute graph traces with trace_vprintk internals */ |
| 2695 | pause_graph_tracing(); |
| 2696 | |
| 2697 | pc = preempt_count(); |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 2698 | preempt_disable_notrace(); |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2699 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2700 | tbuffer = get_trace_buf(); |
| 2701 | if (!tbuffer) { |
| 2702 | len = 0; |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 2703 | goto out_nobuffer; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args); |
| 2707 | |
| 2708 | if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0) |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2709 | goto out; |
| 2710 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2711 | local_save_flags(flags); |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2712 | size = sizeof(*entry) + sizeof(u32) * len; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2713 | buffer = tr->trace_buffer.buffer; |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2714 | event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size, |
| 2715 | flags, pc); |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2716 | if (!event) |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2717 | goto out; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2718 | entry = ring_buffer_event_data(event); |
| 2719 | entry->ip = ip; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2720 | entry->fmt = fmt; |
| 2721 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2722 | memcpy(entry->buf, tbuffer, sizeof(u32) * len); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2723 | if (!call_filter_check_discard(call, entry, buffer, event)) { |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 2724 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 2725 | ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL); |
Steven Rostedt | d931369 | 2010-01-06 17:27:11 -0500 | [diff] [blame] | 2726 | } |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2727 | |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2728 | out: |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 2729 | put_trace_buf(); |
| 2730 | |
| 2731 | out_nobuffer: |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 2732 | preempt_enable_notrace(); |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2733 | unpause_graph_tracing(); |
| 2734 | |
| 2735 | return len; |
| 2736 | } |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2737 | EXPORT_SYMBOL_GPL(trace_vbprintk); |
| 2738 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2739 | static int |
| 2740 | __trace_array_vprintk(struct ring_buffer *buffer, |
| 2741 | unsigned long ip, const char *fmt, va_list args) |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2742 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 2743 | struct trace_event_call *call = &event_print; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2744 | struct ring_buffer_event *event; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2745 | int len = 0, size, pc; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2746 | struct print_entry *entry; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2747 | unsigned long flags; |
| 2748 | char *tbuffer; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2749 | |
| 2750 | if (tracing_disabled || tracing_selftest_running) |
| 2751 | return 0; |
| 2752 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2753 | /* Don't pollute graph traces with trace_vprintk internals */ |
| 2754 | pause_graph_tracing(); |
| 2755 | |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2756 | pc = preempt_count(); |
| 2757 | preempt_disable_notrace(); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2758 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2759 | |
| 2760 | tbuffer = get_trace_buf(); |
| 2761 | if (!tbuffer) { |
| 2762 | len = 0; |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 2763 | goto out_nobuffer; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2764 | } |
| 2765 | |
Dan Carpenter | 3558a5a | 2014-11-27 18:57:52 +0300 | [diff] [blame] | 2766 | len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2767 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2768 | local_save_flags(flags); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2769 | size = sizeof(*entry) + len + 1; |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2770 | event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, |
| 2771 | flags, pc); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2772 | if (!event) |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2773 | goto out; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2774 | entry = ring_buffer_event_data(event); |
Carsten Emde | c13d2f7 | 2009-11-16 20:56:13 +0100 | [diff] [blame] | 2775 | entry->ip = ip; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2776 | |
Dan Carpenter | 3558a5a | 2014-11-27 18:57:52 +0300 | [diff] [blame] | 2777 | memcpy(&entry->buf, tbuffer, len + 1); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2778 | if (!call_filter_check_discard(call, entry, buffer, event)) { |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 2779 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 2780 | ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL); |
Steven Rostedt | d931369 | 2010-01-06 17:27:11 -0500 | [diff] [blame] | 2781 | } |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 2782 | |
| 2783 | out: |
| 2784 | put_trace_buf(); |
| 2785 | |
| 2786 | out_nobuffer: |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2787 | preempt_enable_notrace(); |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 2788 | unpause_graph_tracing(); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 2789 | |
| 2790 | return len; |
| 2791 | } |
Steven Rostedt | 659372d | 2009-09-03 19:11:07 -0400 | [diff] [blame] | 2792 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2793 | int trace_array_vprintk(struct trace_array *tr, |
| 2794 | unsigned long ip, const char *fmt, va_list args) |
| 2795 | { |
| 2796 | return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args); |
| 2797 | } |
| 2798 | |
| 2799 | int trace_array_printk(struct trace_array *tr, |
| 2800 | unsigned long ip, const char *fmt, ...) |
| 2801 | { |
| 2802 | int ret; |
| 2803 | va_list ap; |
| 2804 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 2805 | if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2806 | return 0; |
| 2807 | |
| 2808 | va_start(ap, fmt); |
| 2809 | ret = trace_array_vprintk(tr, ip, fmt, ap); |
| 2810 | va_end(ap); |
| 2811 | return ret; |
| 2812 | } |
| 2813 | |
| 2814 | int trace_array_printk_buf(struct ring_buffer *buffer, |
| 2815 | unsigned long ip, const char *fmt, ...) |
| 2816 | { |
| 2817 | int ret; |
| 2818 | va_list ap; |
| 2819 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 2820 | if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2821 | return 0; |
| 2822 | |
| 2823 | va_start(ap, fmt); |
| 2824 | ret = __trace_array_vprintk(buffer, ip, fmt, ap); |
| 2825 | va_end(ap); |
| 2826 | return ret; |
| 2827 | } |
| 2828 | |
Steven Rostedt | 659372d | 2009-09-03 19:11:07 -0400 | [diff] [blame] | 2829 | int trace_vprintk(unsigned long ip, const char *fmt, va_list args) |
| 2830 | { |
Steven Rostedt | a813a15 | 2009-10-09 01:41:35 -0400 | [diff] [blame] | 2831 | return trace_array_vprintk(&global_trace, ip, fmt, args); |
Steven Rostedt | 659372d | 2009-09-03 19:11:07 -0400 | [diff] [blame] | 2832 | } |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 2833 | EXPORT_SYMBOL_GPL(trace_vprintk); |
| 2834 | |
Robert Richter | e2ac8ef | 2008-11-12 12:59:32 +0100 | [diff] [blame] | 2835 | static void trace_iterator_increment(struct trace_iterator *iter) |
Steven Rostedt | 5a90f57 | 2008-09-03 17:42:51 -0400 | [diff] [blame] | 2836 | { |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 2837 | struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu); |
| 2838 | |
Steven Rostedt | 5a90f57 | 2008-09-03 17:42:51 -0400 | [diff] [blame] | 2839 | iter->idx++; |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 2840 | if (buf_iter) |
| 2841 | ring_buffer_read(buf_iter, NULL); |
Steven Rostedt | 5a90f57 | 2008-09-03 17:42:51 -0400 | [diff] [blame] | 2842 | } |
| 2843 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2844 | static struct trace_entry * |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 2845 | peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts, |
| 2846 | unsigned long *lost_events) |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 2847 | { |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2848 | struct ring_buffer_event *event; |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 2849 | struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu); |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 2850 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2851 | if (buf_iter) |
| 2852 | event = ring_buffer_iter_peek(buf_iter, ts); |
| 2853 | else |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2854 | event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts, |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 2855 | lost_events); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2856 | |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2857 | if (event) { |
| 2858 | iter->ent_size = ring_buffer_event_length(event); |
| 2859 | return ring_buffer_event_data(event); |
| 2860 | } |
| 2861 | iter->ent_size = 0; |
| 2862 | return NULL; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 2863 | } |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2864 | |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 2865 | static struct trace_entry * |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 2866 | __find_next_entry(struct trace_iterator *iter, int *ent_cpu, |
| 2867 | unsigned long *missing_events, u64 *ent_ts) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2868 | { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2869 | struct ring_buffer *buffer = iter->trace_buffer->buffer; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2870 | struct trace_entry *ent, *next = NULL; |
Lai Jiangshan | aa27497 | 2010-04-05 17:11:05 +0800 | [diff] [blame] | 2871 | unsigned long lost_events = 0, next_lost = 0; |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 2872 | int cpu_file = iter->cpu_file; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2873 | u64 next_ts = 0, ts; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2874 | int next_cpu = -1; |
Steven Rostedt | 12b5da3 | 2012-03-27 10:43:28 -0400 | [diff] [blame] | 2875 | int next_size = 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2876 | int cpu; |
| 2877 | |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 2878 | /* |
| 2879 | * If we are in a per_cpu trace file, don't bother by iterating over |
| 2880 | * all cpu and peek directly. |
| 2881 | */ |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 2882 | if (cpu_file > RING_BUFFER_ALL_CPUS) { |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 2883 | if (ring_buffer_empty_cpu(buffer, cpu_file)) |
| 2884 | return NULL; |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 2885 | ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 2886 | if (ent_cpu) |
| 2887 | *ent_cpu = cpu_file; |
| 2888 | |
| 2889 | return ent; |
| 2890 | } |
| 2891 | |
Steven Rostedt | ab46428 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 2892 | for_each_tracing_cpu(cpu) { |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2893 | |
| 2894 | if (ring_buffer_empty_cpu(buffer, cpu)) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2895 | continue; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 2896 | |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 2897 | ent = peek_next_entry(iter, cpu, &ts, &lost_events); |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 2898 | |
Ingo Molnar | cdd31cd2 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2899 | /* |
| 2900 | * Pick the entry with the smallest timestamp: |
| 2901 | */ |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2902 | if (ent && (!next || ts < next_ts)) { |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2903 | next = ent; |
| 2904 | next_cpu = cpu; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2905 | next_ts = ts; |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 2906 | next_lost = lost_events; |
Steven Rostedt | 12b5da3 | 2012-03-27 10:43:28 -0400 | [diff] [blame] | 2907 | next_size = iter->ent_size; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2908 | } |
| 2909 | } |
| 2910 | |
Steven Rostedt | 12b5da3 | 2012-03-27 10:43:28 -0400 | [diff] [blame] | 2911 | iter->ent_size = next_size; |
| 2912 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2913 | if (ent_cpu) |
| 2914 | *ent_cpu = next_cpu; |
| 2915 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2916 | if (ent_ts) |
| 2917 | *ent_ts = next_ts; |
| 2918 | |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 2919 | if (missing_events) |
| 2920 | *missing_events = next_lost; |
| 2921 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2922 | return next; |
| 2923 | } |
| 2924 | |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 2925 | /* Find the next real entry, without updating the iterator itself */ |
Frederic Weisbecker | c4a8e8b | 2009-02-02 20:29:21 -0200 | [diff] [blame] | 2926 | struct trace_entry *trace_find_next_entry(struct trace_iterator *iter, |
| 2927 | int *ent_cpu, u64 *ent_ts) |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2928 | { |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 2929 | return __find_next_entry(iter, ent_cpu, NULL, ent_ts); |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 2930 | } |
Ingo Molnar | 8c523a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2931 | |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 2932 | /* Find the next real entry, and increment the iterator to the next entry */ |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 2933 | void *trace_find_next_entry_inc(struct trace_iterator *iter) |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 2934 | { |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 2935 | iter->ent = __find_next_entry(iter, &iter->cpu, |
| 2936 | &iter->lost_events, &iter->ts); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2937 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2938 | if (iter->ent) |
Robert Richter | e2ac8ef | 2008-11-12 12:59:32 +0100 | [diff] [blame] | 2939 | trace_iterator_increment(iter); |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 2940 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2941 | return iter->ent ? iter : NULL; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2942 | } |
| 2943 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2944 | static void trace_consume(struct trace_iterator *iter) |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2945 | { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2946 | ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts, |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 2947 | &iter->lost_events); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2948 | } |
| 2949 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2950 | static void *s_next(struct seq_file *m, void *v, loff_t *pos) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2951 | { |
| 2952 | struct trace_iterator *iter = m->private; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2953 | int i = (int)*pos; |
Ingo Molnar | 4e3c333 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 2954 | void *ent; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2955 | |
Steven Rostedt | a63ce5b | 2009-12-07 09:11:39 -0500 | [diff] [blame] | 2956 | WARN_ON_ONCE(iter->leftover); |
| 2957 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2958 | (*pos)++; |
| 2959 | |
| 2960 | /* can't go backwards */ |
| 2961 | if (iter->idx > i) |
| 2962 | return NULL; |
| 2963 | |
| 2964 | if (iter->idx < 0) |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 2965 | ent = trace_find_next_entry_inc(iter); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2966 | else |
| 2967 | ent = iter; |
| 2968 | |
| 2969 | while (ent && iter->idx < i) |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 2970 | ent = trace_find_next_entry_inc(iter); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2971 | |
| 2972 | iter->pos = *pos; |
| 2973 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2974 | return ent; |
| 2975 | } |
| 2976 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 2977 | void tracing_iter_reset(struct trace_iterator *iter, int cpu) |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 2978 | { |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 2979 | struct ring_buffer_event *event; |
| 2980 | struct ring_buffer_iter *buf_iter; |
| 2981 | unsigned long entries = 0; |
| 2982 | u64 ts; |
| 2983 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2984 | per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0; |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 2985 | |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 2986 | buf_iter = trace_buffer_iter(iter, cpu); |
| 2987 | if (!buf_iter) |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 2988 | return; |
| 2989 | |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 2990 | ring_buffer_iter_reset(buf_iter); |
| 2991 | |
| 2992 | /* |
| 2993 | * We could have the case with the max latency tracers |
| 2994 | * that a reset never took place on a cpu. This is evident |
| 2995 | * by the timestamp being before the start of the buffer. |
| 2996 | */ |
| 2997 | while ((event = ring_buffer_iter_peek(buf_iter, &ts))) { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2998 | if (ts >= iter->trace_buffer->time_start) |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 2999 | break; |
| 3000 | entries++; |
| 3001 | ring_buffer_read(buf_iter, NULL); |
| 3002 | } |
| 3003 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3004 | per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries; |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3005 | } |
| 3006 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3007 | /* |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3008 | * The current tracer is copied to avoid a global locking |
| 3009 | * all around. |
| 3010 | */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3011 | static void *s_start(struct seq_file *m, loff_t *pos) |
| 3012 | { |
| 3013 | struct trace_iterator *iter = m->private; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 3014 | struct trace_array *tr = iter->tr; |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3015 | int cpu_file = iter->cpu_file; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3016 | void *p = NULL; |
| 3017 | loff_t l = 0; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3018 | int cpu; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3019 | |
Hiraku Toyooka | 2fd196e | 2012-12-26 11:52:52 +0900 | [diff] [blame] | 3020 | /* |
| 3021 | * copy the tracer to avoid using a global lock all around. |
| 3022 | * iter->trace is a copy of current_trace, the pointer to the |
| 3023 | * name may be used instead of a strcmp(), as iter->trace->name |
| 3024 | * will point to the same string as current_trace->name. |
| 3025 | */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3026 | mutex_lock(&trace_types_lock); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 3027 | if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name)) |
| 3028 | *iter->trace = *tr->current_trace; |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3029 | mutex_unlock(&trace_types_lock); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3030 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3031 | #ifdef CONFIG_TRACER_MAX_TRACE |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 3032 | if (iter->snapshot && iter->trace->use_max_tr) |
| 3033 | return ERR_PTR(-EBUSY); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3034 | #endif |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 3035 | |
| 3036 | if (!iter->snapshot) |
| 3037 | atomic_inc(&trace_record_cmdline_disabled); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3038 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3039 | if (*pos != iter->pos) { |
| 3040 | iter->ent = NULL; |
| 3041 | iter->cpu = 0; |
| 3042 | iter->idx = -1; |
| 3043 | |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 3044 | if (cpu_file == RING_BUFFER_ALL_CPUS) { |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3045 | for_each_tracing_cpu(cpu) |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3046 | tracing_iter_reset(iter, cpu); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3047 | } else |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3048 | tracing_iter_reset(iter, cpu_file); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3049 | |
Lai Jiangshan | ac91d85 | 2010-03-02 17:54:50 +0800 | [diff] [blame] | 3050 | iter->leftover = 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3051 | for (p = iter; p && l < *pos; p = s_next(m, p, &l)) |
| 3052 | ; |
| 3053 | |
| 3054 | } else { |
Steven Rostedt | a63ce5b | 2009-12-07 09:11:39 -0500 | [diff] [blame] | 3055 | /* |
| 3056 | * If we overflowed the seq_file before, then we want |
| 3057 | * to just reuse the trace_seq buffer again. |
| 3058 | */ |
| 3059 | if (iter->leftover) |
| 3060 | p = iter; |
| 3061 | else { |
| 3062 | l = *pos - 1; |
| 3063 | p = s_next(m, p, &l); |
| 3064 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3065 | } |
| 3066 | |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 3067 | trace_event_read_lock(); |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 3068 | trace_access_lock(cpu_file); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3069 | return p; |
| 3070 | } |
| 3071 | |
| 3072 | static void s_stop(struct seq_file *m, void *p) |
| 3073 | { |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 3074 | struct trace_iterator *iter = m->private; |
| 3075 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3076 | #ifdef CONFIG_TRACER_MAX_TRACE |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 3077 | if (iter->snapshot && iter->trace->use_max_tr) |
| 3078 | return; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3079 | #endif |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 3080 | |
| 3081 | if (!iter->snapshot) |
| 3082 | atomic_dec(&trace_record_cmdline_disabled); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3083 | |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 3084 | trace_access_unlock(iter->cpu_file); |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 3085 | trace_event_read_unlock(); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3086 | } |
| 3087 | |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3088 | static void |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3089 | get_total_entries(struct trace_buffer *buf, |
| 3090 | unsigned long *total, unsigned long *entries) |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3091 | { |
| 3092 | unsigned long count; |
| 3093 | int cpu; |
| 3094 | |
| 3095 | *total = 0; |
| 3096 | *entries = 0; |
| 3097 | |
| 3098 | for_each_tracing_cpu(cpu) { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3099 | count = ring_buffer_entries_cpu(buf->buffer, cpu); |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3100 | /* |
| 3101 | * If this buffer has skipped entries, then we hold all |
| 3102 | * entries for the trace and we need to ignore the |
| 3103 | * ones before the time stamp. |
| 3104 | */ |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3105 | if (per_cpu_ptr(buf->data, cpu)->skipped_entries) { |
| 3106 | count -= per_cpu_ptr(buf->data, cpu)->skipped_entries; |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3107 | /* total is the same as the entries */ |
| 3108 | *total += count; |
| 3109 | } else |
| 3110 | *total += count + |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3111 | ring_buffer_overrun_cpu(buf->buffer, cpu); |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3112 | *entries += count; |
| 3113 | } |
| 3114 | } |
| 3115 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3116 | static void print_lat_help_header(struct seq_file *m) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3117 | { |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 3118 | seq_puts(m, "# _------=> CPU# \n" |
| 3119 | "# / _-----=> irqs-off \n" |
| 3120 | "# | / _----=> need-resched \n" |
| 3121 | "# || / _---=> hardirq/softirq \n" |
| 3122 | "# ||| / _--=> preempt-depth \n" |
| 3123 | "# |||| / delay \n" |
| 3124 | "# cmd pid ||||| time | caller \n" |
| 3125 | "# \\ / ||||| \\ | / \n"); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3126 | } |
| 3127 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3128 | static void print_event_info(struct trace_buffer *buf, struct seq_file *m) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3129 | { |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3130 | unsigned long total; |
| 3131 | unsigned long entries; |
| 3132 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3133 | get_total_entries(buf, &total, &entries); |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3134 | seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n", |
| 3135 | entries, total, num_online_cpus()); |
| 3136 | seq_puts(m, "#\n"); |
| 3137 | } |
| 3138 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3139 | static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m) |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3140 | { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3141 | print_event_info(buf, m); |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 3142 | seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n" |
| 3143 | "# | | | | |\n"); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3144 | } |
| 3145 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3146 | static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m) |
Steven Rostedt | 77271ce | 2011-11-17 09:34:33 -0500 | [diff] [blame] | 3147 | { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3148 | print_event_info(buf, m); |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 3149 | seq_puts(m, "# _-----=> irqs-off\n" |
| 3150 | "# / _----=> need-resched\n" |
| 3151 | "# | / _---=> hardirq/softirq\n" |
| 3152 | "# || / _--=> preempt-depth\n" |
| 3153 | "# ||| / delay\n" |
| 3154 | "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n" |
| 3155 | "# | | | |||| | |\n"); |
Steven Rostedt | 77271ce | 2011-11-17 09:34:33 -0500 | [diff] [blame] | 3156 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3157 | |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 3158 | void |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3159 | print_trace_header(struct seq_file *m, struct trace_iterator *iter) |
| 3160 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3161 | unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3162 | struct trace_buffer *buf = iter->trace_buffer; |
| 3163 | struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 3164 | struct tracer *type = iter->trace; |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3165 | unsigned long entries; |
| 3166 | unsigned long total; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3167 | const char *name = "preemption"; |
| 3168 | |
Steven Rostedt (Red Hat) | d840f71 | 2013-02-01 18:38:47 -0500 | [diff] [blame] | 3169 | name = type->name; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3170 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3171 | get_total_entries(buf, &total, &entries); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3172 | |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3173 | seq_printf(m, "# %s latency trace v1.1.5 on %s\n", |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3174 | name, UTS_RELEASE); |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3175 | seq_puts(m, "# -----------------------------------" |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3176 | "---------------------------------\n"); |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3177 | seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |" |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3178 | " (M:%s VP:%d, KP:%d, SP:%d HP:%d", |
Steven Rostedt | 57f50be | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 3179 | nsecs_to_usecs(data->saved_latency), |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3180 | entries, |
Steven Rostedt | 4c11d7a | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3181 | total, |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3182 | buf->cpu, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3183 | #if defined(CONFIG_PREEMPT_NONE) |
| 3184 | "server", |
| 3185 | #elif defined(CONFIG_PREEMPT_VOLUNTARY) |
| 3186 | "desktop", |
Steven Rostedt | b5c21b4 | 2008-07-10 20:58:12 -0400 | [diff] [blame] | 3187 | #elif defined(CONFIG_PREEMPT) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3188 | "preempt", |
| 3189 | #else |
| 3190 | "unknown", |
| 3191 | #endif |
| 3192 | /* These are reserved for later use */ |
| 3193 | 0, 0, 0, 0); |
| 3194 | #ifdef CONFIG_SMP |
| 3195 | seq_printf(m, " #P:%d)\n", num_online_cpus()); |
| 3196 | #else |
| 3197 | seq_puts(m, ")\n"); |
| 3198 | #endif |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3199 | seq_puts(m, "# -----------------\n"); |
| 3200 | seq_printf(m, "# | task: %.16s-%d " |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3201 | "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n", |
Eric W. Biederman | d20b92a | 2012-03-13 16:02:19 -0700 | [diff] [blame] | 3202 | data->comm, data->pid, |
| 3203 | from_kuid_munged(seq_user_ns(m), data->uid), data->nice, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3204 | data->policy, data->rt_priority); |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3205 | seq_puts(m, "# -----------------\n"); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3206 | |
| 3207 | if (data->critical_start) { |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3208 | seq_puts(m, "# => started at: "); |
Steven Rostedt | 214023c | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3209 | seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags); |
| 3210 | trace_print_seq(m, &iter->seq); |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3211 | seq_puts(m, "\n# => ended at: "); |
Steven Rostedt | 214023c | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3212 | seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags); |
| 3213 | trace_print_seq(m, &iter->seq); |
Steven Rostedt | 8248ac0 | 2009-09-02 12:27:41 -0400 | [diff] [blame] | 3214 | seq_puts(m, "\n#\n"); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3215 | } |
| 3216 | |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3217 | seq_puts(m, "#\n"); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3218 | } |
| 3219 | |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3220 | static void test_cpu_buff_start(struct trace_iterator *iter) |
| 3221 | { |
| 3222 | struct trace_seq *s = &iter->seq; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3223 | struct trace_array *tr = iter->tr; |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3224 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3225 | if (!(tr->trace_flags & TRACE_ITER_ANNOTATE)) |
Steven Rostedt | 12ef7d4 | 2008-11-12 17:52:38 -0500 | [diff] [blame] | 3226 | return; |
| 3227 | |
| 3228 | if (!(iter->iter_flags & TRACE_FILE_ANNOTATE)) |
| 3229 | return; |
| 3230 | |
Sasha Levin | 919cd97 | 2015-09-04 12:45:56 -0400 | [diff] [blame] | 3231 | if (iter->started && cpumask_test_cpu(iter->cpu, iter->started)) |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3232 | return; |
| 3233 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3234 | if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries) |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3235 | return; |
| 3236 | |
Sasha Levin | 919cd97 | 2015-09-04 12:45:56 -0400 | [diff] [blame] | 3237 | if (iter->started) |
| 3238 | cpumask_set_cpu(iter->cpu, iter->started); |
Frederic Weisbecker | b0dfa97 | 2009-04-01 22:53:08 +0200 | [diff] [blame] | 3239 | |
| 3240 | /* Don't print started cpu buffer for the first entry of the trace */ |
| 3241 | if (iter->idx > 1) |
| 3242 | trace_seq_printf(s, "##### CPU %u buffer started ####\n", |
| 3243 | iter->cpu); |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3244 | } |
| 3245 | |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3246 | static enum print_line_t print_trace_fmt(struct trace_iterator *iter) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3247 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3248 | struct trace_array *tr = iter->tr; |
Steven Rostedt | 214023c | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3249 | struct trace_seq *s = &iter->seq; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3250 | unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK); |
Ingo Molnar | 4e3c333 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3251 | struct trace_entry *entry; |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3252 | struct trace_event *event; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3253 | |
Ingo Molnar | 4e3c333 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3254 | entry = iter->ent; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3255 | |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3256 | test_cpu_buff_start(iter); |
| 3257 | |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3258 | event = ftrace_find_event(entry->type); |
Frederic Weisbecker | c4a8e8b | 2009-02-02 20:29:21 -0200 | [diff] [blame] | 3259 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3260 | if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) { |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3261 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) |
| 3262 | trace_print_lat_context(iter); |
| 3263 | else |
| 3264 | trace_print_context(iter); |
Frederic Weisbecker | c4a8e8b | 2009-02-02 20:29:21 -0200 | [diff] [blame] | 3265 | } |
| 3266 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3267 | if (trace_seq_has_overflowed(s)) |
| 3268 | return TRACE_TYPE_PARTIAL_LINE; |
| 3269 | |
Arnaldo Carvalho de Melo | 268ccda | 2009-02-04 20:16:39 -0200 | [diff] [blame] | 3270 | if (event) |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 3271 | return event->funcs->trace(iter, sym_flags, event); |
Arnaldo Carvalho de Melo | d9793bd | 2009-02-03 20:20:41 -0200 | [diff] [blame] | 3272 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3273 | trace_seq_printf(s, "Unknown type %d\n", entry->type); |
Steven Rostedt | 7104f30 | 2008-10-01 10:52:51 -0400 | [diff] [blame] | 3274 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3275 | return trace_handle_return(s); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3276 | } |
| 3277 | |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3278 | static enum print_line_t print_raw_fmt(struct trace_iterator *iter) |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3279 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3280 | struct trace_array *tr = iter->tr; |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3281 | struct trace_seq *s = &iter->seq; |
| 3282 | struct trace_entry *entry; |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3283 | struct trace_event *event; |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3284 | |
| 3285 | entry = iter->ent; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3286 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3287 | if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3288 | trace_seq_printf(s, "%d %d %llu ", |
| 3289 | entry->pid, iter->cpu, iter->ts); |
| 3290 | |
| 3291 | if (trace_seq_has_overflowed(s)) |
| 3292 | return TRACE_TYPE_PARTIAL_LINE; |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3293 | |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3294 | event = ftrace_find_event(entry->type); |
Arnaldo Carvalho de Melo | 268ccda | 2009-02-04 20:16:39 -0200 | [diff] [blame] | 3295 | if (event) |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 3296 | return event->funcs->raw(iter, 0, event); |
Arnaldo Carvalho de Melo | d9793bd | 2009-02-03 20:20:41 -0200 | [diff] [blame] | 3297 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3298 | trace_seq_printf(s, "%d ?\n", entry->type); |
Steven Rostedt | 7104f30 | 2008-10-01 10:52:51 -0400 | [diff] [blame] | 3299 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3300 | return trace_handle_return(s); |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3301 | } |
| 3302 | |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3303 | static enum print_line_t print_hex_fmt(struct trace_iterator *iter) |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3304 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3305 | struct trace_array *tr = iter->tr; |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3306 | struct trace_seq *s = &iter->seq; |
| 3307 | unsigned char newline = '\n'; |
| 3308 | struct trace_entry *entry; |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3309 | struct trace_event *event; |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3310 | |
| 3311 | entry = iter->ent; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3312 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3313 | if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) { |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3314 | SEQ_PUT_HEX_FIELD(s, entry->pid); |
| 3315 | SEQ_PUT_HEX_FIELD(s, iter->cpu); |
| 3316 | SEQ_PUT_HEX_FIELD(s, iter->ts); |
| 3317 | if (trace_seq_has_overflowed(s)) |
| 3318 | return TRACE_TYPE_PARTIAL_LINE; |
Frederic Weisbecker | c4a8e8b | 2009-02-02 20:29:21 -0200 | [diff] [blame] | 3319 | } |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3320 | |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3321 | event = ftrace_find_event(entry->type); |
Arnaldo Carvalho de Melo | 268ccda | 2009-02-04 20:16:39 -0200 | [diff] [blame] | 3322 | if (event) { |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 3323 | enum print_line_t ret = event->funcs->hex(iter, 0, event); |
Arnaldo Carvalho de Melo | d9793bd | 2009-02-03 20:20:41 -0200 | [diff] [blame] | 3324 | if (ret != TRACE_TYPE_HANDLED) |
| 3325 | return ret; |
| 3326 | } |
Steven Rostedt | 7104f30 | 2008-10-01 10:52:51 -0400 | [diff] [blame] | 3327 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3328 | SEQ_PUT_FIELD(s, newline); |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3329 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3330 | return trace_handle_return(s); |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3331 | } |
| 3332 | |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3333 | static enum print_line_t print_bin_fmt(struct trace_iterator *iter) |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3334 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3335 | struct trace_array *tr = iter->tr; |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3336 | struct trace_seq *s = &iter->seq; |
| 3337 | struct trace_entry *entry; |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3338 | struct trace_event *event; |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3339 | |
| 3340 | entry = iter->ent; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3341 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3342 | if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) { |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3343 | SEQ_PUT_FIELD(s, entry->pid); |
| 3344 | SEQ_PUT_FIELD(s, iter->cpu); |
| 3345 | SEQ_PUT_FIELD(s, iter->ts); |
| 3346 | if (trace_seq_has_overflowed(s)) |
| 3347 | return TRACE_TYPE_PARTIAL_LINE; |
Frederic Weisbecker | c4a8e8b | 2009-02-02 20:29:21 -0200 | [diff] [blame] | 3348 | } |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3349 | |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3350 | event = ftrace_find_event(entry->type); |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 3351 | return event ? event->funcs->binary(iter, 0, event) : |
| 3352 | TRACE_TYPE_HANDLED; |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3353 | } |
| 3354 | |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 3355 | int trace_empty(struct trace_iterator *iter) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3356 | { |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3357 | struct ring_buffer_iter *buf_iter; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3358 | int cpu; |
| 3359 | |
Steven Rostedt | 9aba60f | 2009-03-11 19:52:30 -0400 | [diff] [blame] | 3360 | /* If we are looking at one CPU buffer, only check that one */ |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 3361 | if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { |
Steven Rostedt | 9aba60f | 2009-03-11 19:52:30 -0400 | [diff] [blame] | 3362 | cpu = iter->cpu_file; |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3363 | buf_iter = trace_buffer_iter(iter, cpu); |
| 3364 | if (buf_iter) { |
| 3365 | if (!ring_buffer_iter_empty(buf_iter)) |
Steven Rostedt | 9aba60f | 2009-03-11 19:52:30 -0400 | [diff] [blame] | 3366 | return 0; |
| 3367 | } else { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3368 | if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu)) |
Steven Rostedt | 9aba60f | 2009-03-11 19:52:30 -0400 | [diff] [blame] | 3369 | return 0; |
| 3370 | } |
| 3371 | return 1; |
| 3372 | } |
| 3373 | |
Steven Rostedt | ab46428 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 3374 | for_each_tracing_cpu(cpu) { |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3375 | buf_iter = trace_buffer_iter(iter, cpu); |
| 3376 | if (buf_iter) { |
| 3377 | if (!ring_buffer_iter_empty(buf_iter)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3378 | return 0; |
| 3379 | } else { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3380 | if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3381 | return 0; |
| 3382 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3383 | } |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3384 | |
Frederic Weisbecker | 797d371 | 2008-09-30 18:13:45 +0200 | [diff] [blame] | 3385 | return 1; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3386 | } |
| 3387 | |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 3388 | /* Called with trace_event_read_lock() held. */ |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 3389 | enum print_line_t print_trace_line(struct trace_iterator *iter) |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3390 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3391 | struct trace_array *tr = iter->tr; |
| 3392 | unsigned long trace_flags = tr->trace_flags; |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3393 | enum print_line_t ret; |
| 3394 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3395 | if (iter->lost_events) { |
| 3396 | trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n", |
| 3397 | iter->cpu, iter->lost_events); |
| 3398 | if (trace_seq_has_overflowed(&iter->seq)) |
| 3399 | return TRACE_TYPE_PARTIAL_LINE; |
| 3400 | } |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3401 | |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3402 | if (iter->trace && iter->trace->print_line) { |
| 3403 | ret = iter->trace->print_line(iter); |
| 3404 | if (ret != TRACE_TYPE_UNHANDLED) |
| 3405 | return ret; |
| 3406 | } |
Thomas Gleixner | 72829bc | 2008-05-23 21:37:28 +0200 | [diff] [blame] | 3407 | |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 3408 | if (iter->ent->type == TRACE_BPUTS && |
| 3409 | trace_flags & TRACE_ITER_PRINTK && |
| 3410 | trace_flags & TRACE_ITER_PRINTK_MSGONLY) |
| 3411 | return trace_print_bputs_msg_only(iter); |
| 3412 | |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3413 | if (iter->ent->type == TRACE_BPRINT && |
| 3414 | trace_flags & TRACE_ITER_PRINTK && |
| 3415 | trace_flags & TRACE_ITER_PRINTK_MSGONLY) |
Steven Rostedt | 5ef841f | 2009-03-19 12:20:38 -0400 | [diff] [blame] | 3416 | return trace_print_bprintk_msg_only(iter); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3417 | |
Frederic Weisbecker | 66896a8 | 2008-12-13 20:18:13 +0100 | [diff] [blame] | 3418 | if (iter->ent->type == TRACE_PRINT && |
| 3419 | trace_flags & TRACE_ITER_PRINTK && |
| 3420 | trace_flags & TRACE_ITER_PRINTK_MSGONLY) |
Steven Rostedt | 5ef841f | 2009-03-19 12:20:38 -0400 | [diff] [blame] | 3421 | return trace_print_printk_msg_only(iter); |
Frederic Weisbecker | 66896a8 | 2008-12-13 20:18:13 +0100 | [diff] [blame] | 3422 | |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3423 | if (trace_flags & TRACE_ITER_BIN) |
| 3424 | return print_bin_fmt(iter); |
| 3425 | |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3426 | if (trace_flags & TRACE_ITER_HEX) |
| 3427 | return print_hex_fmt(iter); |
| 3428 | |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3429 | if (trace_flags & TRACE_ITER_RAW) |
| 3430 | return print_raw_fmt(iter); |
| 3431 | |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3432 | return print_trace_fmt(iter); |
| 3433 | } |
| 3434 | |
Jiri Olsa | 7e9a49e | 2011-11-07 16:08:49 +0100 | [diff] [blame] | 3435 | void trace_latency_header(struct seq_file *m) |
| 3436 | { |
| 3437 | struct trace_iterator *iter = m->private; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3438 | struct trace_array *tr = iter->tr; |
Jiri Olsa | 7e9a49e | 2011-11-07 16:08:49 +0100 | [diff] [blame] | 3439 | |
| 3440 | /* print nothing if the buffers are empty */ |
| 3441 | if (trace_empty(iter)) |
| 3442 | return; |
| 3443 | |
| 3444 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) |
| 3445 | print_trace_header(m, iter); |
| 3446 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3447 | if (!(tr->trace_flags & TRACE_ITER_VERBOSE)) |
Jiri Olsa | 7e9a49e | 2011-11-07 16:08:49 +0100 | [diff] [blame] | 3448 | print_lat_help_header(m); |
| 3449 | } |
| 3450 | |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 3451 | void trace_default_header(struct seq_file *m) |
| 3452 | { |
| 3453 | struct trace_iterator *iter = m->private; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3454 | struct trace_array *tr = iter->tr; |
| 3455 | unsigned long trace_flags = tr->trace_flags; |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 3456 | |
Jiri Olsa | f56e7f8 | 2011-06-03 16:58:49 +0200 | [diff] [blame] | 3457 | if (!(trace_flags & TRACE_ITER_CONTEXT_INFO)) |
| 3458 | return; |
| 3459 | |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 3460 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) { |
| 3461 | /* print nothing if the buffers are empty */ |
| 3462 | if (trace_empty(iter)) |
| 3463 | return; |
| 3464 | print_trace_header(m, iter); |
| 3465 | if (!(trace_flags & TRACE_ITER_VERBOSE)) |
| 3466 | print_lat_help_header(m); |
| 3467 | } else { |
Steven Rostedt | 77271ce | 2011-11-17 09:34:33 -0500 | [diff] [blame] | 3468 | if (!(trace_flags & TRACE_ITER_VERBOSE)) { |
| 3469 | if (trace_flags & TRACE_ITER_IRQ_INFO) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3470 | print_func_help_header_irq(iter->trace_buffer, m); |
Steven Rostedt | 77271ce | 2011-11-17 09:34:33 -0500 | [diff] [blame] | 3471 | else |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3472 | print_func_help_header(iter->trace_buffer, m); |
Steven Rostedt | 77271ce | 2011-11-17 09:34:33 -0500 | [diff] [blame] | 3473 | } |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 3474 | } |
| 3475 | } |
| 3476 | |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 3477 | static void test_ftrace_alive(struct seq_file *m) |
| 3478 | { |
| 3479 | if (!ftrace_is_dead()) |
| 3480 | return; |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 3481 | seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n" |
| 3482 | "# MAY BE MISSING FUNCTION EVENTS\n"); |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 3483 | } |
| 3484 | |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 3485 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 3486 | static void show_snapshot_main_help(struct seq_file *m) |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 3487 | { |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 3488 | seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n" |
| 3489 | "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n" |
| 3490 | "# Takes a snapshot of the main buffer.\n" |
| 3491 | "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n" |
| 3492 | "# (Doesn't have to be '2' works with any number that\n" |
| 3493 | "# is not a '0' or '1')\n"); |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 3494 | } |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 3495 | |
| 3496 | static void show_snapshot_percpu_help(struct seq_file *m) |
| 3497 | { |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3498 | seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n"); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 3499 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 3500 | seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n" |
| 3501 | "# Takes a snapshot of the main buffer for this cpu.\n"); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 3502 | #else |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 3503 | seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n" |
| 3504 | "# Must use main snapshot file to allocate.\n"); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 3505 | #endif |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 3506 | seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n" |
| 3507 | "# (Doesn't have to be '2' works with any number that\n" |
| 3508 | "# is not a '0' or '1')\n"); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 3509 | } |
| 3510 | |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 3511 | static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) |
| 3512 | { |
Steven Rostedt (Red Hat) | 45ad21c | 2013-03-05 18:25:02 -0500 | [diff] [blame] | 3513 | if (iter->tr->allocated_snapshot) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3514 | seq_puts(m, "#\n# * Snapshot is allocated *\n#\n"); |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 3515 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3516 | seq_puts(m, "#\n# * Snapshot is freed *\n#\n"); |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 3517 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3518 | seq_puts(m, "# Snapshot commands:\n"); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 3519 | if (iter->cpu_file == RING_BUFFER_ALL_CPUS) |
| 3520 | show_snapshot_main_help(m); |
| 3521 | else |
| 3522 | show_snapshot_percpu_help(m); |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 3523 | } |
| 3524 | #else |
| 3525 | /* Should never be called */ |
| 3526 | static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { } |
| 3527 | #endif |
| 3528 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3529 | static int s_show(struct seq_file *m, void *v) |
| 3530 | { |
| 3531 | struct trace_iterator *iter = v; |
Steven Rostedt | a63ce5b | 2009-12-07 09:11:39 -0500 | [diff] [blame] | 3532 | int ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3533 | |
| 3534 | if (iter->ent == NULL) { |
| 3535 | if (iter->tr) { |
| 3536 | seq_printf(m, "# tracer: %s\n", iter->trace->name); |
| 3537 | seq_puts(m, "#\n"); |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 3538 | test_ftrace_alive(m); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3539 | } |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 3540 | if (iter->snapshot && trace_empty(iter)) |
| 3541 | print_snapshot_help(m, iter); |
| 3542 | else if (iter->trace && iter->trace->print_header) |
Markus Metzger | 8bba1bf | 2008-11-25 09:12:31 +0100 | [diff] [blame] | 3543 | iter->trace->print_header(m); |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 3544 | else |
| 3545 | trace_default_header(m); |
| 3546 | |
Steven Rostedt | a63ce5b | 2009-12-07 09:11:39 -0500 | [diff] [blame] | 3547 | } else if (iter->leftover) { |
| 3548 | /* |
| 3549 | * If we filled the seq_file buffer earlier, we |
| 3550 | * want to just show it now. |
| 3551 | */ |
| 3552 | ret = trace_print_seq(m, &iter->seq); |
| 3553 | |
| 3554 | /* ret should this time be zero, but you never know */ |
| 3555 | iter->leftover = ret; |
| 3556 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3557 | } else { |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3558 | print_trace_line(iter); |
Steven Rostedt | a63ce5b | 2009-12-07 09:11:39 -0500 | [diff] [blame] | 3559 | ret = trace_print_seq(m, &iter->seq); |
| 3560 | /* |
| 3561 | * If we overflow the seq_file buffer, then it will |
| 3562 | * ask us for this data again at start up. |
| 3563 | * Use that instead. |
| 3564 | * ret is 0 if seq_file write succeeded. |
| 3565 | * -1 otherwise. |
| 3566 | */ |
| 3567 | iter->leftover = ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | return 0; |
| 3571 | } |
| 3572 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 3573 | /* |
| 3574 | * Should be used after trace_array_get(), trace_types_lock |
| 3575 | * ensures that i_cdev was already initialized. |
| 3576 | */ |
| 3577 | static inline int tracing_get_cpu(struct inode *inode) |
| 3578 | { |
| 3579 | if (inode->i_cdev) /* See trace_create_cpu_file() */ |
| 3580 | return (long)inode->i_cdev - 1; |
| 3581 | return RING_BUFFER_ALL_CPUS; |
| 3582 | } |
| 3583 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 3584 | static const struct seq_operations tracer_seq_ops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3585 | .start = s_start, |
| 3586 | .next = s_next, |
| 3587 | .stop = s_stop, |
| 3588 | .show = s_show, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3589 | }; |
| 3590 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3591 | static struct trace_iterator * |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 3592 | __tracing_open(struct inode *inode, struct file *file, bool snapshot) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3593 | { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 3594 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3595 | struct trace_iterator *iter; |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3596 | int cpu; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3597 | |
Steven Rostedt | 85a2f9b | 2009-02-27 00:12:38 -0500 | [diff] [blame] | 3598 | if (tracing_disabled) |
| 3599 | return ERR_PTR(-ENODEV); |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 3600 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3601 | iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter)); |
Steven Rostedt | 85a2f9b | 2009-02-27 00:12:38 -0500 | [diff] [blame] | 3602 | if (!iter) |
| 3603 | return ERR_PTR(-ENOMEM); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3604 | |
Gil Fruchter | 7291723 | 2015-06-09 10:32:35 +0300 | [diff] [blame] | 3605 | iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter), |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3606 | GFP_KERNEL); |
Dan Carpenter | 93574fc | 2012-07-11 09:35:08 +0300 | [diff] [blame] | 3607 | if (!iter->buffer_iter) |
| 3608 | goto release; |
| 3609 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3610 | /* |
| 3611 | * We make a copy of the current tracer to avoid concurrent |
| 3612 | * changes on it while we are reading. |
| 3613 | */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3614 | mutex_lock(&trace_types_lock); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3615 | iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL); |
Steven Rostedt | 85a2f9b | 2009-02-27 00:12:38 -0500 | [diff] [blame] | 3616 | if (!iter->trace) |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3617 | goto fail; |
Steven Rostedt | 85a2f9b | 2009-02-27 00:12:38 -0500 | [diff] [blame] | 3618 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 3619 | *iter->trace = *tr->current_trace; |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3620 | |
Li Zefan | 79f5599 | 2009-06-15 14:58:26 +0800 | [diff] [blame] | 3621 | if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL)) |
Frederic Weisbecker | b0dfa97 | 2009-04-01 22:53:08 +0200 | [diff] [blame] | 3622 | goto fail; |
| 3623 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3624 | iter->tr = tr; |
| 3625 | |
| 3626 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 3627 | /* Currently only the top directory has a snapshot */ |
| 3628 | if (tr->current_trace->print_max || snapshot) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3629 | iter->trace_buffer = &tr->max_buffer; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3630 | else |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3631 | #endif |
| 3632 | iter->trace_buffer = &tr->trace_buffer; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 3633 | iter->snapshot = snapshot; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3634 | iter->pos = -1; |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 3635 | iter->cpu_file = tracing_get_cpu(inode); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3636 | mutex_init(&iter->mutex); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3637 | |
Markus Metzger | 8bba1bf | 2008-11-25 09:12:31 +0100 | [diff] [blame] | 3638 | /* Notify the tracer early; before we stop tracing. */ |
| 3639 | if (iter->trace && iter->trace->open) |
Markus Metzger | a93751c | 2008-12-11 13:53:26 +0100 | [diff] [blame] | 3640 | iter->trace->open(iter); |
Markus Metzger | 8bba1bf | 2008-11-25 09:12:31 +0100 | [diff] [blame] | 3641 | |
Steven Rostedt | 12ef7d4 | 2008-11-12 17:52:38 -0500 | [diff] [blame] | 3642 | /* Annotate start of buffers if we had overruns */ |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3643 | if (ring_buffer_overruns(iter->trace_buffer->buffer)) |
Steven Rostedt | 12ef7d4 | 2008-11-12 17:52:38 -0500 | [diff] [blame] | 3644 | iter->iter_flags |= TRACE_FILE_ANNOTATE; |
| 3645 | |
David Sharp | 8be0709 | 2012-11-13 12:18:22 -0800 | [diff] [blame] | 3646 | /* Output in nanoseconds only if we are using a clock in nanoseconds. */ |
Yoshihiro YUNOMAE | 58e8eed | 2013-04-23 10:32:39 +0900 | [diff] [blame] | 3647 | if (trace_clocks[tr->clock_id].in_ns) |
David Sharp | 8be0709 | 2012-11-13 12:18:22 -0800 | [diff] [blame] | 3648 | iter->iter_flags |= TRACE_FILE_TIME_IN_NS; |
| 3649 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 3650 | /* stop the trace while dumping if we are not opening "snapshot" */ |
| 3651 | if (!iter->snapshot) |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 3652 | tracing_stop_tr(tr); |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3653 | |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 3654 | if (iter->cpu_file == RING_BUFFER_ALL_CPUS) { |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3655 | for_each_tracing_cpu(cpu) { |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3656 | iter->buffer_iter[cpu] = |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3657 | ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu); |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 3658 | } |
| 3659 | ring_buffer_read_prepare_sync(); |
| 3660 | for_each_tracing_cpu(cpu) { |
| 3661 | ring_buffer_read_start(iter->buffer_iter[cpu]); |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3662 | tracing_iter_reset(iter, cpu); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3663 | } |
| 3664 | } else { |
| 3665 | cpu = iter->cpu_file; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3666 | iter->buffer_iter[cpu] = |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3667 | ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu); |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 3668 | ring_buffer_read_prepare_sync(); |
| 3669 | ring_buffer_read_start(iter->buffer_iter[cpu]); |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3670 | tracing_iter_reset(iter, cpu); |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3671 | } |
| 3672 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3673 | mutex_unlock(&trace_types_lock); |
| 3674 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3675 | return iter; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3676 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3677 | fail: |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3678 | mutex_unlock(&trace_types_lock); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3679 | kfree(iter->trace); |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3680 | kfree(iter->buffer_iter); |
Dan Carpenter | 93574fc | 2012-07-11 09:35:08 +0300 | [diff] [blame] | 3681 | release: |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3682 | seq_release_private(inode, file); |
| 3683 | return ERR_PTR(-ENOMEM); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | int tracing_open_generic(struct inode *inode, struct file *filp) |
| 3687 | { |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 3688 | if (tracing_disabled) |
| 3689 | return -ENODEV; |
| 3690 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3691 | filp->private_data = inode->i_private; |
| 3692 | return 0; |
| 3693 | } |
| 3694 | |
Geyslan G. Bem | 2e86421 | 2013-10-18 21:15:54 -0300 | [diff] [blame] | 3695 | bool tracing_is_disabled(void) |
| 3696 | { |
| 3697 | return (tracing_disabled) ? true: false; |
| 3698 | } |
| 3699 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 3700 | /* |
| 3701 | * Open and update trace_array ref count. |
| 3702 | * Must have the current trace_array passed to it. |
| 3703 | */ |
Steven Rostedt (Red Hat) | dcc3022 | 2013-07-02 20:30:52 -0400 | [diff] [blame] | 3704 | static int tracing_open_generic_tr(struct inode *inode, struct file *filp) |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 3705 | { |
| 3706 | struct trace_array *tr = inode->i_private; |
| 3707 | |
| 3708 | if (tracing_disabled) |
| 3709 | return -ENODEV; |
| 3710 | |
| 3711 | if (trace_array_get(tr) < 0) |
| 3712 | return -ENODEV; |
| 3713 | |
| 3714 | filp->private_data = inode->i_private; |
| 3715 | |
| 3716 | return 0; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 3717 | } |
| 3718 | |
Hannes Eder | 4fd2735 | 2009-02-10 19:44:12 +0100 | [diff] [blame] | 3719 | static int tracing_release(struct inode *inode, struct file *file) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3720 | { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 3721 | struct trace_array *tr = inode->i_private; |
matt mooney | 907f278 | 2010-09-27 19:04:53 -0700 | [diff] [blame] | 3722 | struct seq_file *m = file->private_data; |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 3723 | struct trace_iterator *iter; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3724 | int cpu; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3725 | |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 3726 | if (!(file->f_mode & FMODE_READ)) { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 3727 | trace_array_put(tr); |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 3728 | return 0; |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 3729 | } |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 3730 | |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 3731 | /* Writes do not use seq_file */ |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 3732 | iter = m->private; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3733 | mutex_lock(&trace_types_lock); |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 3734 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3735 | for_each_tracing_cpu(cpu) { |
| 3736 | if (iter->buffer_iter[cpu]) |
| 3737 | ring_buffer_read_finish(iter->buffer_iter[cpu]); |
| 3738 | } |
| 3739 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3740 | if (iter->trace && iter->trace->close) |
| 3741 | iter->trace->close(iter); |
| 3742 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 3743 | if (!iter->snapshot) |
| 3744 | /* reenable tracing if it was previously enabled */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 3745 | tracing_start_tr(tr); |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 3746 | |
| 3747 | __trace_array_put(tr); |
| 3748 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3749 | mutex_unlock(&trace_types_lock); |
| 3750 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3751 | mutex_destroy(&iter->mutex); |
Frederic Weisbecker | b0dfa97 | 2009-04-01 22:53:08 +0200 | [diff] [blame] | 3752 | free_cpumask_var(iter->started); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3753 | kfree(iter->trace); |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3754 | kfree(iter->buffer_iter); |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 3755 | seq_release_private(inode, file); |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 3756 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3757 | return 0; |
| 3758 | } |
| 3759 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 3760 | static int tracing_release_generic_tr(struct inode *inode, struct file *file) |
| 3761 | { |
| 3762 | struct trace_array *tr = inode->i_private; |
| 3763 | |
| 3764 | trace_array_put(tr); |
| 3765 | return 0; |
| 3766 | } |
| 3767 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 3768 | static int tracing_single_release_tr(struct inode *inode, struct file *file) |
| 3769 | { |
| 3770 | struct trace_array *tr = inode->i_private; |
| 3771 | |
| 3772 | trace_array_put(tr); |
| 3773 | |
| 3774 | return single_release(inode, file); |
| 3775 | } |
| 3776 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3777 | static int tracing_open(struct inode *inode, struct file *file) |
| 3778 | { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 3779 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | 85a2f9b | 2009-02-27 00:12:38 -0500 | [diff] [blame] | 3780 | struct trace_iterator *iter; |
| 3781 | int ret = 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3782 | |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 3783 | if (trace_array_get(tr) < 0) |
| 3784 | return -ENODEV; |
| 3785 | |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 3786 | /* If this file was open for write, then erase contents */ |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 3787 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { |
| 3788 | int cpu = tracing_get_cpu(inode); |
| 3789 | |
| 3790 | if (cpu == RING_BUFFER_ALL_CPUS) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3791 | tracing_reset_online_cpus(&tr->trace_buffer); |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 3792 | else |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 3793 | tracing_reset(&tr->trace_buffer, cpu); |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | if (file->f_mode & FMODE_READ) { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 3797 | iter = __tracing_open(inode, file, false); |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 3798 | if (IS_ERR(iter)) |
| 3799 | ret = PTR_ERR(iter); |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3800 | else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 3801 | iter->iter_flags |= TRACE_FILE_LAT_FMT; |
| 3802 | } |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 3803 | |
| 3804 | if (ret < 0) |
| 3805 | trace_array_put(tr); |
| 3806 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3807 | return ret; |
| 3808 | } |
| 3809 | |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 3810 | /* |
| 3811 | * Some tracers are not suitable for instance buffers. |
| 3812 | * A tracer is always available for the global array (toplevel) |
| 3813 | * or if it explicitly states that it is. |
| 3814 | */ |
| 3815 | static bool |
| 3816 | trace_ok_for_array(struct tracer *t, struct trace_array *tr) |
| 3817 | { |
| 3818 | return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances; |
| 3819 | } |
| 3820 | |
| 3821 | /* Find the next tracer that this trace array may use */ |
| 3822 | static struct tracer * |
| 3823 | get_tracer_for_array(struct trace_array *tr, struct tracer *t) |
| 3824 | { |
| 3825 | while (t && !trace_ok_for_array(t, tr)) |
| 3826 | t = t->next; |
| 3827 | |
| 3828 | return t; |
| 3829 | } |
| 3830 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3831 | static void * |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3832 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 3833 | { |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 3834 | struct trace_array *tr = m->private; |
Li Zefan | f129e96 | 2009-06-24 09:53:44 +0800 | [diff] [blame] | 3835 | struct tracer *t = v; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3836 | |
| 3837 | (*pos)++; |
| 3838 | |
| 3839 | if (t) |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 3840 | t = get_tracer_for_array(tr, t->next); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3841 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3842 | return t; |
| 3843 | } |
| 3844 | |
| 3845 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 3846 | { |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 3847 | struct trace_array *tr = m->private; |
Li Zefan | f129e96 | 2009-06-24 09:53:44 +0800 | [diff] [blame] | 3848 | struct tracer *t; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3849 | loff_t l = 0; |
| 3850 | |
| 3851 | mutex_lock(&trace_types_lock); |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 3852 | |
| 3853 | t = get_tracer_for_array(tr, trace_types); |
| 3854 | for (; t && l < *pos; t = t_next(m, t, &l)) |
| 3855 | ; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3856 | |
| 3857 | return t; |
| 3858 | } |
| 3859 | |
| 3860 | static void t_stop(struct seq_file *m, void *p) |
| 3861 | { |
| 3862 | mutex_unlock(&trace_types_lock); |
| 3863 | } |
| 3864 | |
| 3865 | static int t_show(struct seq_file *m, void *v) |
| 3866 | { |
| 3867 | struct tracer *t = v; |
| 3868 | |
| 3869 | if (!t) |
| 3870 | return 0; |
| 3871 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 3872 | seq_puts(m, t->name); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3873 | if (t->next) |
| 3874 | seq_putc(m, ' '); |
| 3875 | else |
| 3876 | seq_putc(m, '\n'); |
| 3877 | |
| 3878 | return 0; |
| 3879 | } |
| 3880 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 3881 | static const struct seq_operations show_traces_seq_ops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3882 | .start = t_start, |
| 3883 | .next = t_next, |
| 3884 | .stop = t_stop, |
| 3885 | .show = t_show, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3886 | }; |
| 3887 | |
| 3888 | static int show_traces_open(struct inode *inode, struct file *file) |
| 3889 | { |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 3890 | struct trace_array *tr = inode->i_private; |
| 3891 | struct seq_file *m; |
| 3892 | int ret; |
| 3893 | |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 3894 | if (tracing_disabled) |
| 3895 | return -ENODEV; |
| 3896 | |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 3897 | ret = seq_open(file, &show_traces_seq_ops); |
| 3898 | if (ret) |
| 3899 | return ret; |
| 3900 | |
| 3901 | m = file->private_data; |
| 3902 | m->private = tr; |
| 3903 | |
| 3904 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3905 | } |
| 3906 | |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 3907 | static ssize_t |
| 3908 | tracing_write_stub(struct file *filp, const char __user *ubuf, |
| 3909 | size_t count, loff_t *ppos) |
| 3910 | { |
| 3911 | return count; |
| 3912 | } |
| 3913 | |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3914 | loff_t tracing_lseek(struct file *file, loff_t offset, int whence) |
Slava Pestov | 364829b | 2010-11-24 15:13:16 -0800 | [diff] [blame] | 3915 | { |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3916 | int ret; |
| 3917 | |
Slava Pestov | 364829b | 2010-11-24 15:13:16 -0800 | [diff] [blame] | 3918 | if (file->f_mode & FMODE_READ) |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3919 | ret = seq_lseek(file, offset, whence); |
Slava Pestov | 364829b | 2010-11-24 15:13:16 -0800 | [diff] [blame] | 3920 | else |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3921 | file->f_pos = ret = 0; |
| 3922 | |
| 3923 | return ret; |
Slava Pestov | 364829b | 2010-11-24 15:13:16 -0800 | [diff] [blame] | 3924 | } |
| 3925 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 3926 | static const struct file_operations tracing_fops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3927 | .open = tracing_open, |
| 3928 | .read = seq_read, |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 3929 | .write = tracing_write_stub, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 3930 | .llseek = tracing_lseek, |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3931 | .release = tracing_release, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3932 | }; |
| 3933 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 3934 | static const struct file_operations show_traces_fops = { |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3935 | .open = show_traces_open, |
| 3936 | .read = seq_read, |
| 3937 | .release = seq_release, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 3938 | .llseek = seq_lseek, |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3939 | }; |
| 3940 | |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3941 | /* |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3942 | * The tracer itself will not take this lock, but still we want |
| 3943 | * to provide a consistent cpumask to user-space: |
| 3944 | */ |
| 3945 | static DEFINE_MUTEX(tracing_cpumask_update_lock); |
| 3946 | |
| 3947 | /* |
| 3948 | * Temporary storage for the character representation of the |
| 3949 | * CPU bitmask (and one more byte for the newline): |
| 3950 | */ |
| 3951 | static char mask_str[NR_CPUS + 1]; |
| 3952 | |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3953 | static ssize_t |
| 3954 | tracing_cpumask_read(struct file *filp, char __user *ubuf, |
| 3955 | size_t count, loff_t *ppos) |
| 3956 | { |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 3957 | struct trace_array *tr = file_inode(filp)->i_private; |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3958 | int len; |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3959 | |
| 3960 | mutex_lock(&tracing_cpumask_update_lock); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3961 | |
Tejun Heo | 1a40243 | 2015-02-13 14:37:39 -0800 | [diff] [blame] | 3962 | len = snprintf(mask_str, count, "%*pb\n", |
| 3963 | cpumask_pr_args(tr->tracing_cpumask)); |
| 3964 | if (len >= count) { |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3965 | count = -EINVAL; |
| 3966 | goto out_err; |
| 3967 | } |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3968 | count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1); |
| 3969 | |
| 3970 | out_err: |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3971 | mutex_unlock(&tracing_cpumask_update_lock); |
| 3972 | |
| 3973 | return count; |
| 3974 | } |
| 3975 | |
| 3976 | static ssize_t |
| 3977 | tracing_cpumask_write(struct file *filp, const char __user *ubuf, |
| 3978 | size_t count, loff_t *ppos) |
| 3979 | { |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 3980 | struct trace_array *tr = file_inode(filp)->i_private; |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3981 | cpumask_var_t tracing_cpumask_new; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 3982 | int err, cpu; |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3983 | |
| 3984 | if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL)) |
| 3985 | return -ENOMEM; |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3986 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3987 | err = cpumask_parse_user(ubuf, count, tracing_cpumask_new); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3988 | if (err) |
| 3989 | goto err_unlock; |
| 3990 | |
Li Zefan | 215368e | 2009-06-15 10:56:42 +0800 | [diff] [blame] | 3991 | mutex_lock(&tracing_cpumask_update_lock); |
| 3992 | |
Steven Rostedt | a5e2588 | 2008-12-02 15:34:05 -0500 | [diff] [blame] | 3993 | local_irq_disable(); |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 3994 | arch_spin_lock(&tr->max_lock); |
Steven Rostedt | ab46428 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 3995 | for_each_tracing_cpu(cpu) { |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 3996 | /* |
| 3997 | * Increase/decrease the disabled counter if we are |
| 3998 | * about to flip a bit in the cpumask: |
| 3999 | */ |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 4000 | if (cpumask_test_cpu(cpu, tr->tracing_cpumask) && |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4001 | !cpumask_test_cpu(cpu, tracing_cpumask_new)) { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4002 | atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled); |
| 4003 | ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4004 | } |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 4005 | if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) && |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4006 | cpumask_test_cpu(cpu, tracing_cpumask_new)) { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4007 | atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled); |
| 4008 | ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4009 | } |
| 4010 | } |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 4011 | arch_spin_unlock(&tr->max_lock); |
Steven Rostedt | a5e2588 | 2008-12-02 15:34:05 -0500 | [diff] [blame] | 4012 | local_irq_enable(); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4013 | |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 4014 | cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4015 | |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4016 | mutex_unlock(&tracing_cpumask_update_lock); |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4017 | free_cpumask_var(tracing_cpumask_new); |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4018 | |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4019 | return count; |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4020 | |
| 4021 | err_unlock: |
Li Zefan | 215368e | 2009-06-15 10:56:42 +0800 | [diff] [blame] | 4022 | free_cpumask_var(tracing_cpumask_new); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4023 | |
| 4024 | return err; |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4025 | } |
| 4026 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4027 | static const struct file_operations tracing_cpumask_fops = { |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 4028 | .open = tracing_open_generic_tr, |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4029 | .read = tracing_cpumask_read, |
| 4030 | .write = tracing_cpumask_write, |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 4031 | .release = tracing_release_generic_tr, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 4032 | .llseek = generic_file_llseek, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4033 | }; |
| 4034 | |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4035 | static int tracing_trace_options_show(struct seq_file *m, void *v) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4036 | { |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4037 | struct tracer_opt *trace_opts; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4038 | struct trace_array *tr = m->private; |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4039 | u32 tracer_flags; |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4040 | int i; |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4041 | |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4042 | mutex_lock(&trace_types_lock); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4043 | tracer_flags = tr->current_trace->flags->val; |
| 4044 | trace_opts = tr->current_trace->flags->opts; |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4045 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4046 | for (i = 0; trace_options[i]; i++) { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4047 | if (tr->trace_flags & (1 << i)) |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4048 | seq_printf(m, "%s\n", trace_options[i]); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4049 | else |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4050 | seq_printf(m, "no%s\n", trace_options[i]); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4051 | } |
| 4052 | |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4053 | for (i = 0; trace_opts[i].name; i++) { |
| 4054 | if (tracer_flags & trace_opts[i].bit) |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4055 | seq_printf(m, "%s\n", trace_opts[i].name); |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4056 | else |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4057 | seq_printf(m, "no%s\n", trace_opts[i].name); |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4058 | } |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4059 | mutex_unlock(&trace_types_lock); |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4060 | |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4061 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4062 | } |
| 4063 | |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4064 | static int __set_tracer_option(struct trace_array *tr, |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4065 | struct tracer_flags *tracer_flags, |
| 4066 | struct tracer_opt *opts, int neg) |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4067 | { |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 4068 | struct tracer *trace = tracer_flags->trace; |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4069 | int ret; |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4070 | |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4071 | ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg); |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4072 | if (ret) |
| 4073 | return ret; |
| 4074 | |
| 4075 | if (neg) |
Zhaolei | 7770841 | 2009-08-07 18:53:21 +0800 | [diff] [blame] | 4076 | tracer_flags->val &= ~opts->bit; |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4077 | else |
Zhaolei | 7770841 | 2009-08-07 18:53:21 +0800 | [diff] [blame] | 4078 | tracer_flags->val |= opts->bit; |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4079 | return 0; |
| 4080 | } |
| 4081 | |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4082 | /* Try to assign a tracer specific option */ |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4083 | static int set_tracer_option(struct trace_array *tr, char *cmp, int neg) |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4084 | { |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4085 | struct tracer *trace = tr->current_trace; |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4086 | struct tracer_flags *tracer_flags = trace->flags; |
| 4087 | struct tracer_opt *opts = NULL; |
| 4088 | int i; |
| 4089 | |
| 4090 | for (i = 0; tracer_flags->opts[i].name; i++) { |
| 4091 | opts = &tracer_flags->opts[i]; |
| 4092 | |
| 4093 | if (strcmp(cmp, opts->name) == 0) |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4094 | return __set_tracer_option(tr, trace->flags, opts, neg); |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | return -EINVAL; |
| 4098 | } |
| 4099 | |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4100 | /* Some tracers require overwrite to stay enabled */ |
| 4101 | int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set) |
| 4102 | { |
| 4103 | if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set) |
| 4104 | return -1; |
| 4105 | |
| 4106 | return 0; |
| 4107 | } |
| 4108 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4109 | int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled) |
Steven Rostedt | af4617b | 2009-03-17 18:09:55 -0400 | [diff] [blame] | 4110 | { |
| 4111 | /* do nothing if flag is already set */ |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4112 | if (!!(tr->trace_flags & mask) == !!enabled) |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4113 | return 0; |
| 4114 | |
| 4115 | /* Give the tracer a chance to approve the change */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4116 | if (tr->current_trace->flag_changed) |
Steven Rostedt (Red Hat) | bf6065b | 2014-01-10 17:51:01 -0500 | [diff] [blame] | 4117 | if (tr->current_trace->flag_changed(tr, mask, !!enabled)) |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4118 | return -EINVAL; |
Steven Rostedt | af4617b | 2009-03-17 18:09:55 -0400 | [diff] [blame] | 4119 | |
| 4120 | if (enabled) |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4121 | tr->trace_flags |= mask; |
Steven Rostedt | af4617b | 2009-03-17 18:09:55 -0400 | [diff] [blame] | 4122 | else |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4123 | tr->trace_flags &= ~mask; |
Li Zefan | e870e9a | 2010-07-02 11:07:32 +0800 | [diff] [blame] | 4124 | |
| 4125 | if (mask == TRACE_ITER_RECORD_CMD) |
| 4126 | trace_event_enable_cmd_record(enabled); |
David Sharp | 750912f | 2010-12-08 13:46:47 -0800 | [diff] [blame] | 4127 | |
Steven Rostedt | c37775d | 2016-04-13 16:59:18 -0400 | [diff] [blame] | 4128 | if (mask == TRACE_ITER_EVENT_FORK) |
| 4129 | trace_event_follow_fork(tr, enabled); |
| 4130 | |
Steven Rostedt (Red Hat) | 8090282 | 2013-03-14 14:20:54 -0400 | [diff] [blame] | 4131 | if (mask == TRACE_ITER_OVERWRITE) { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4132 | ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled); |
Steven Rostedt (Red Hat) | 8090282 | 2013-03-14 14:20:54 -0400 | [diff] [blame] | 4133 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4134 | ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled); |
Steven Rostedt (Red Hat) | 8090282 | 2013-03-14 14:20:54 -0400 | [diff] [blame] | 4135 | #endif |
| 4136 | } |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 4137 | |
Steven Rostedt (Red Hat) | b9f9108 | 2015-09-29 18:21:35 -0400 | [diff] [blame] | 4138 | if (mask == TRACE_ITER_PRINTK) { |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 4139 | trace_printk_start_stop_comm(enabled); |
Steven Rostedt (Red Hat) | b9f9108 | 2015-09-29 18:21:35 -0400 | [diff] [blame] | 4140 | trace_printk_control(enabled); |
| 4141 | } |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4142 | |
| 4143 | return 0; |
Steven Rostedt | af4617b | 2009-03-17 18:09:55 -0400 | [diff] [blame] | 4144 | } |
| 4145 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4146 | static int trace_set_options(struct trace_array *tr, char *option) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4147 | { |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4148 | char *cmp; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4149 | int neg = 0; |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4150 | int ret = -ENODEV; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4151 | int i; |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 4152 | size_t orig_len = strlen(option); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4153 | |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4154 | cmp = strstrip(option); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4155 | |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4156 | if (strncmp(cmp, "no", 2) == 0) { |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4157 | neg = 1; |
| 4158 | cmp += 2; |
| 4159 | } |
| 4160 | |
Steven Rostedt (Red Hat) | 69d34da | 2013-03-14 13:50:56 -0400 | [diff] [blame] | 4161 | mutex_lock(&trace_types_lock); |
| 4162 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4163 | for (i = 0; trace_options[i]; i++) { |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4164 | if (strcmp(cmp, trace_options[i]) == 0) { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4165 | ret = set_tracer_flag(tr, 1 << i, !neg); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4166 | break; |
| 4167 | } |
| 4168 | } |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4169 | |
| 4170 | /* If no option could be set, test the specific tracer options */ |
Steven Rostedt (Red Hat) | 69d34da | 2013-03-14 13:50:56 -0400 | [diff] [blame] | 4171 | if (!trace_options[i]) |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4172 | ret = set_tracer_option(tr, cmp, neg); |
Steven Rostedt (Red Hat) | 69d34da | 2013-03-14 13:50:56 -0400 | [diff] [blame] | 4173 | |
| 4174 | mutex_unlock(&trace_types_lock); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4175 | |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 4176 | /* |
| 4177 | * If the first trailing whitespace is replaced with '\0' by strstrip, |
| 4178 | * turn it back into a space. |
| 4179 | */ |
| 4180 | if (orig_len > strlen(option)) |
| 4181 | option[strlen(option)] = ' '; |
| 4182 | |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4183 | return ret; |
| 4184 | } |
| 4185 | |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 4186 | static void __init apply_trace_boot_options(void) |
| 4187 | { |
| 4188 | char *buf = trace_boot_options_buf; |
| 4189 | char *option; |
| 4190 | |
| 4191 | while (true) { |
| 4192 | option = strsep(&buf, ","); |
| 4193 | |
| 4194 | if (!option) |
| 4195 | break; |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 4196 | |
Steven Rostedt (Red Hat) | 43ed384 | 2015-11-03 22:15:14 -0500 | [diff] [blame] | 4197 | if (*option) |
| 4198 | trace_set_options(&global_trace, option); |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 4199 | |
| 4200 | /* Put back the comma to allow this to be called again */ |
| 4201 | if (buf) |
| 4202 | *(buf - 1) = ','; |
| 4203 | } |
| 4204 | } |
| 4205 | |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4206 | static ssize_t |
| 4207 | tracing_trace_options_write(struct file *filp, const char __user *ubuf, |
| 4208 | size_t cnt, loff_t *ppos) |
| 4209 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4210 | struct seq_file *m = filp->private_data; |
| 4211 | struct trace_array *tr = m->private; |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4212 | char buf[64]; |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4213 | int ret; |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4214 | |
| 4215 | if (cnt >= sizeof(buf)) |
| 4216 | return -EINVAL; |
| 4217 | |
Wang Xiaoqiang | 4afe649 | 2016-04-18 15:23:29 +0800 | [diff] [blame] | 4218 | if (copy_from_user(buf, ubuf, cnt)) |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4219 | return -EFAULT; |
| 4220 | |
Steven Rostedt | a8dd217 | 2013-01-09 20:54:17 -0500 | [diff] [blame] | 4221 | buf[cnt] = 0; |
| 4222 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4223 | ret = trace_set_options(tr, buf); |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4224 | if (ret < 0) |
| 4225 | return ret; |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4226 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 4227 | *ppos += cnt; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4228 | |
| 4229 | return cnt; |
| 4230 | } |
| 4231 | |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4232 | static int tracing_trace_options_open(struct inode *inode, struct file *file) |
| 4233 | { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4234 | struct trace_array *tr = inode->i_private; |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 4235 | int ret; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4236 | |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4237 | if (tracing_disabled) |
| 4238 | return -ENODEV; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4239 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4240 | if (trace_array_get(tr) < 0) |
| 4241 | return -ENODEV; |
| 4242 | |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 4243 | ret = single_open(file, tracing_trace_options_show, inode->i_private); |
| 4244 | if (ret < 0) |
| 4245 | trace_array_put(tr); |
| 4246 | |
| 4247 | return ret; |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4248 | } |
| 4249 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4250 | static const struct file_operations tracing_iter_fops = { |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4251 | .open = tracing_trace_options_open, |
| 4252 | .read = seq_read, |
| 4253 | .llseek = seq_lseek, |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4254 | .release = tracing_single_release_tr, |
Steven Rostedt | ee6bce5 | 2008-11-12 17:52:37 -0500 | [diff] [blame] | 4255 | .write = tracing_trace_options_write, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4256 | }; |
| 4257 | |
Ingo Molnar | 7bd2f24 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4258 | static const char readme_msg[] = |
| 4259 | "tracing mini-HOWTO:\n\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4260 | "# echo 0 > tracing_on : quick way to disable tracing\n" |
| 4261 | "# echo 1 > tracing_on : quick way to re-enable tracing\n\n" |
| 4262 | " Important files:\n" |
| 4263 | " trace\t\t\t- The static contents of the buffer\n" |
| 4264 | "\t\t\t To clear the buffer write into this file: echo > trace\n" |
| 4265 | " trace_pipe\t\t- A consuming read to see the contents of the buffer\n" |
| 4266 | " current_tracer\t- function and latency tracers\n" |
| 4267 | " available_tracers\t- list of configured tracers for current_tracer\n" |
| 4268 | " buffer_size_kb\t- view and modify size of per cpu buffer\n" |
| 4269 | " buffer_total_size_kb - view total size of all cpu buffers\n\n" |
| 4270 | " trace_clock\t\t-change the clock used to order events\n" |
| 4271 | " local: Per cpu clock but may not be synced across CPUs\n" |
| 4272 | " global: Synced across CPUs but slows tracing down.\n" |
| 4273 | " counter: Not a clock, but just an increment\n" |
| 4274 | " uptime: Jiffy counter from time of boot\n" |
| 4275 | " perf: Same clock that perf events use\n" |
| 4276 | #ifdef CONFIG_X86_64 |
| 4277 | " x86-tsc: TSC cycle counter\n" |
| 4278 | #endif |
| 4279 | "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n" |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 4280 | "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4281 | " tracing_cpumask\t- Limit which CPUs to trace\n" |
| 4282 | " instances\t\t- Make sub-buffers with: mkdir instances/foo\n" |
| 4283 | "\t\t\t Remove sub-buffer with rmdir\n" |
| 4284 | " trace_options\t\t- Set format or modify how tracing happens\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4285 | "\t\t\t Disable an option by adding a suffix 'no' to the\n" |
| 4286 | "\t\t\t option name\n" |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 4287 | " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4288 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 4289 | "\n available_filter_functions - list of functions that can be filtered on\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4290 | " set_ftrace_filter\t- echo function name in here to only trace these\n" |
| 4291 | "\t\t\t functions\n" |
Masami Hiramatsu | 60f1d5e | 2016-10-05 20:58:15 +0900 | [diff] [blame] | 4292 | "\t accepts: func_full_name or glob-matching-pattern\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4293 | "\t modules: Can select a group via module\n" |
| 4294 | "\t Format: :mod:<module-name>\n" |
| 4295 | "\t example: echo :mod:ext3 > set_ftrace_filter\n" |
| 4296 | "\t triggers: a command to perform when function is hit\n" |
| 4297 | "\t Format: <function>:<trigger>[:count]\n" |
| 4298 | "\t trigger: traceon, traceoff\n" |
| 4299 | "\t\t enable_event:<system>:<event>\n" |
| 4300 | "\t\t disable_event:<system>:<event>\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4301 | #ifdef CONFIG_STACKTRACE |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4302 | "\t\t stacktrace\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4303 | #endif |
| 4304 | #ifdef CONFIG_TRACER_SNAPSHOT |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4305 | "\t\t snapshot\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4306 | #endif |
Steven Rostedt (Red Hat) | 17a280e | 2014-04-10 22:43:37 -0400 | [diff] [blame] | 4307 | "\t\t dump\n" |
| 4308 | "\t\t cpudump\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4309 | "\t example: echo do_fault:traceoff > set_ftrace_filter\n" |
| 4310 | "\t echo do_trap:traceoff:3 > set_ftrace_filter\n" |
| 4311 | "\t The first one will disable tracing every time do_fault is hit\n" |
| 4312 | "\t The second will disable tracing at most 3 times when do_trap is hit\n" |
| 4313 | "\t The first time do trap is hit and it disables tracing, the\n" |
| 4314 | "\t counter will decrement to 2. If tracing is already disabled,\n" |
| 4315 | "\t the counter will not decrement. It only decrements when the\n" |
| 4316 | "\t trigger did work\n" |
| 4317 | "\t To remove trigger without count:\n" |
| 4318 | "\t echo '!<function>:<trigger> > set_ftrace_filter\n" |
| 4319 | "\t To remove trigger with a count:\n" |
| 4320 | "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4321 | " set_ftrace_notrace\t- echo function name in here to never trace.\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4322 | "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n" |
| 4323 | "\t modules: Can select a group via module command :mod:\n" |
| 4324 | "\t Does not accept triggers\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4325 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 4326 | #ifdef CONFIG_FUNCTION_TRACER |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4327 | " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n" |
| 4328 | "\t\t (function)\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4329 | #endif |
| 4330 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4331 | " set_graph_function\t- Trace the nested calls of a function (function_graph)\n" |
Namhyung Kim | d048a8c7 | 2014-06-13 01:23:53 +0900 | [diff] [blame] | 4332 | " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4333 | " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n" |
| 4334 | #endif |
| 4335 | #ifdef CONFIG_TRACER_SNAPSHOT |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4336 | "\n snapshot\t\t- Like 'trace' but shows the content of the static\n" |
| 4337 | "\t\t\t snapshot buffer. Read the contents for more\n" |
| 4338 | "\t\t\t information\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4339 | #endif |
zhangwei(Jovi) | 991821c | 2013-07-15 16:32:34 +0800 | [diff] [blame] | 4340 | #ifdef CONFIG_STACK_TRACER |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4341 | " stack_trace\t\t- Shows the max stack trace when active\n" |
| 4342 | " stack_max_size\t- Shows current max stack size that was traced\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4343 | "\t\t\t Write into this file to reset the max size (trigger a\n" |
| 4344 | "\t\t\t new trace)\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4345 | #ifdef CONFIG_DYNAMIC_FTRACE |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4346 | " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n" |
| 4347 | "\t\t\t traces\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4348 | #endif |
zhangwei(Jovi) | 991821c | 2013-07-15 16:32:34 +0800 | [diff] [blame] | 4349 | #endif /* CONFIG_STACK_TRACER */ |
Masami Hiramatsu | 8642562 | 2016-08-18 17:58:15 +0900 | [diff] [blame] | 4350 | #ifdef CONFIG_KPROBE_EVENT |
| 4351 | " kprobe_events\t\t- Add/remove/show the kernel dynamic events\n" |
| 4352 | "\t\t\t Write into this file to define/undefine new trace events.\n" |
| 4353 | #endif |
| 4354 | #ifdef CONFIG_UPROBE_EVENT |
| 4355 | " uprobe_events\t\t- Add/remove/show the userspace dynamic events\n" |
| 4356 | "\t\t\t Write into this file to define/undefine new trace events.\n" |
| 4357 | #endif |
| 4358 | #if defined(CONFIG_KPROBE_EVENT) || defined(CONFIG_UPROBE_EVENT) |
| 4359 | "\t accepts: event-definitions (one definition per line)\n" |
| 4360 | "\t Format: p|r[:[<group>/]<event>] <place> [<args>]\n" |
| 4361 | "\t -:[<group>/]<event>\n" |
| 4362 | #ifdef CONFIG_KPROBE_EVENT |
| 4363 | "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n" |
| 4364 | #endif |
| 4365 | #ifdef CONFIG_UPROBE_EVENT |
| 4366 | "\t place: <path>:<offset>\n" |
| 4367 | #endif |
| 4368 | "\t args: <name>=fetcharg[:type]\n" |
| 4369 | "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n" |
| 4370 | "\t $stack<index>, $stack, $retval, $comm\n" |
| 4371 | "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string,\n" |
| 4372 | "\t b<bit-width>@<bit-offset>/<container-size>\n" |
| 4373 | #endif |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 4374 | " events/\t\t- Directory containing all trace event subsystems:\n" |
| 4375 | " enable\t\t- Write 0/1 to enable/disable tracing of all events\n" |
| 4376 | " events/<system>/\t- Directory containing all trace events for <system>:\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4377 | " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n" |
| 4378 | "\t\t\t events\n" |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 4379 | " filter\t\t- If set, only events passing filter are traced\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4380 | " events/<system>/<event>/\t- Directory containing control files for\n" |
| 4381 | "\t\t\t <event>:\n" |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 4382 | " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n" |
| 4383 | " filter\t\t- If set, only events passing filter are traced\n" |
| 4384 | " trigger\t\t- If set, a command to perform when event is hit\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4385 | "\t Format: <trigger>[:count][if <filter>]\n" |
| 4386 | "\t trigger: traceon, traceoff\n" |
| 4387 | "\t enable_event:<system>:<event>\n" |
| 4388 | "\t disable_event:<system>:<event>\n" |
Tom Zanussi | d0bad49 | 2016-03-03 12:54:55 -0600 | [diff] [blame] | 4389 | #ifdef CONFIG_HIST_TRIGGERS |
| 4390 | "\t enable_hist:<system>:<event>\n" |
| 4391 | "\t disable_hist:<system>:<event>\n" |
| 4392 | #endif |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 4393 | #ifdef CONFIG_STACKTRACE |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4394 | "\t\t stacktrace\n" |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 4395 | #endif |
| 4396 | #ifdef CONFIG_TRACER_SNAPSHOT |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4397 | "\t\t snapshot\n" |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 4398 | #endif |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 4399 | #ifdef CONFIG_HIST_TRIGGERS |
| 4400 | "\t\t hist (see below)\n" |
| 4401 | #endif |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4402 | "\t example: echo traceoff > events/block/block_unplug/trigger\n" |
| 4403 | "\t echo traceoff:3 > events/block/block_unplug/trigger\n" |
| 4404 | "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n" |
| 4405 | "\t events/block/block_unplug/trigger\n" |
| 4406 | "\t The first disables tracing every time block_unplug is hit.\n" |
| 4407 | "\t The second disables tracing the first 3 times block_unplug is hit.\n" |
| 4408 | "\t The third enables the kmalloc event the first 3 times block_unplug\n" |
| 4409 | "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n" |
| 4410 | "\t Like function triggers, the counter is only decremented if it\n" |
| 4411 | "\t enabled or disabled tracing.\n" |
| 4412 | "\t To remove a trigger without a count:\n" |
| 4413 | "\t echo '!<trigger> > <system>/<event>/trigger\n" |
| 4414 | "\t To remove a trigger with a count:\n" |
| 4415 | "\t echo '!<trigger>:0 > <system>/<event>/trigger\n" |
| 4416 | "\t Filters can be ignored when removing a trigger.\n" |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 4417 | #ifdef CONFIG_HIST_TRIGGERS |
| 4418 | " hist trigger\t- If set, event hits are aggregated into a hash table\n" |
Tom Zanussi | 76a3b0c | 2016-03-03 12:54:44 -0600 | [diff] [blame] | 4419 | "\t Format: hist:keys=<field1[,field2,...]>\n" |
Tom Zanussi | f260683 | 2016-03-03 12:54:43 -0600 | [diff] [blame] | 4420 | "\t [:values=<field1[,field2,...]>]\n" |
Tom Zanussi | e62347d | 2016-03-03 12:54:45 -0600 | [diff] [blame] | 4421 | "\t [:sort=<field1[,field2,...]>]\n" |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 4422 | "\t [:size=#entries]\n" |
Tom Zanussi | e86ae9b | 2016-03-03 12:54:47 -0600 | [diff] [blame] | 4423 | "\t [:pause][:continue][:clear]\n" |
Tom Zanussi | 5463bfd | 2016-03-03 12:54:59 -0600 | [diff] [blame] | 4424 | "\t [:name=histname1]\n" |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 4425 | "\t [if <filter>]\n\n" |
| 4426 | "\t When a matching event is hit, an entry is added to a hash\n" |
Tom Zanussi | f260683 | 2016-03-03 12:54:43 -0600 | [diff] [blame] | 4427 | "\t table using the key(s) and value(s) named, and the value of a\n" |
| 4428 | "\t sum called 'hitcount' is incremented. Keys and values\n" |
| 4429 | "\t correspond to fields in the event's format description. Keys\n" |
Tom Zanussi | 69a0200 | 2016-03-03 12:54:52 -0600 | [diff] [blame] | 4430 | "\t can be any field, or the special string 'stacktrace'.\n" |
| 4431 | "\t Compound keys consisting of up to two fields can be specified\n" |
| 4432 | "\t by the 'keys' keyword. Values must correspond to numeric\n" |
| 4433 | "\t fields. Sort keys consisting of up to two fields can be\n" |
| 4434 | "\t specified using the 'sort' keyword. The sort direction can\n" |
| 4435 | "\t be modified by appending '.descending' or '.ascending' to a\n" |
| 4436 | "\t sort field. The 'size' parameter can be used to specify more\n" |
Tom Zanussi | 5463bfd | 2016-03-03 12:54:59 -0600 | [diff] [blame] | 4437 | "\t or fewer than the default 2048 entries for the hashtable size.\n" |
| 4438 | "\t If a hist trigger is given a name using the 'name' parameter,\n" |
| 4439 | "\t its histogram data will be shared with other triggers of the\n" |
| 4440 | "\t same name, and trigger hits will update this common data.\n\n" |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 4441 | "\t Reading the 'hist' file for the event will dump the hash\n" |
Tom Zanussi | 52a7f16 | 2016-03-03 12:54:57 -0600 | [diff] [blame] | 4442 | "\t table in its entirety to stdout. If there are multiple hist\n" |
| 4443 | "\t triggers attached to an event, there will be a table for each\n" |
Tom Zanussi | 5463bfd | 2016-03-03 12:54:59 -0600 | [diff] [blame] | 4444 | "\t trigger in the output. The table displayed for a named\n" |
| 4445 | "\t trigger will be the same as any other instance having the\n" |
| 4446 | "\t same name. The default format used to display a given field\n" |
| 4447 | "\t can be modified by appending any of the following modifiers\n" |
| 4448 | "\t to the field name, as applicable:\n\n" |
Tom Zanussi | c6afad4 | 2016-03-03 12:54:49 -0600 | [diff] [blame] | 4449 | "\t .hex display a number as a hex value\n" |
| 4450 | "\t .sym display an address as a symbol\n" |
Tom Zanussi | 6b4827a | 2016-03-03 12:54:50 -0600 | [diff] [blame] | 4451 | "\t .sym-offset display an address as a symbol and offset\n" |
Tom Zanussi | 3169619 | 2016-03-03 12:54:51 -0600 | [diff] [blame] | 4452 | "\t .execname display a common_pid as a program name\n" |
| 4453 | "\t .syscall display a syscall id as a syscall name\n\n" |
Namhyung Kim | 4b94f5b | 2016-03-03 12:55:02 -0600 | [diff] [blame] | 4454 | "\t .log2 display log2 value rather than raw number\n\n" |
Tom Zanussi | 83e9991 | 2016-03-03 12:54:46 -0600 | [diff] [blame] | 4455 | "\t The 'pause' parameter can be used to pause an existing hist\n" |
| 4456 | "\t trigger or to start a hist trigger but not log any events\n" |
| 4457 | "\t until told to do so. 'continue' can be used to start or\n" |
| 4458 | "\t restart a paused hist trigger.\n\n" |
Tom Zanussi | e86ae9b | 2016-03-03 12:54:47 -0600 | [diff] [blame] | 4459 | "\t The 'clear' parameter will clear the contents of a running\n" |
| 4460 | "\t hist trigger and leave its current paused/active state\n" |
| 4461 | "\t unchanged.\n\n" |
Tom Zanussi | d0bad49 | 2016-03-03 12:54:55 -0600 | [diff] [blame] | 4462 | "\t The enable_hist and disable_hist triggers can be used to\n" |
| 4463 | "\t have one event conditionally start and stop another event's\n" |
| 4464 | "\t already-attached hist trigger. The syntax is analagous to\n" |
| 4465 | "\t the enable_event and disable_event triggers.\n" |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 4466 | #endif |
Ingo Molnar | 7bd2f24 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4467 | ; |
| 4468 | |
| 4469 | static ssize_t |
| 4470 | tracing_readme_read(struct file *filp, char __user *ubuf, |
| 4471 | size_t cnt, loff_t *ppos) |
| 4472 | { |
| 4473 | return simple_read_from_buffer(ubuf, cnt, ppos, |
| 4474 | readme_msg, strlen(readme_msg)); |
| 4475 | } |
| 4476 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4477 | static const struct file_operations tracing_readme_fops = { |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4478 | .open = tracing_open_generic, |
| 4479 | .read = tracing_readme_read, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 4480 | .llseek = generic_file_llseek, |
Ingo Molnar | 7bd2f24 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4481 | }; |
| 4482 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4483 | static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos) |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 4484 | { |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4485 | unsigned int *ptr = v; |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 4486 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4487 | if (*pos || m->count) |
| 4488 | ptr++; |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 4489 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4490 | (*pos)++; |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 4491 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 4492 | for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num]; |
| 4493 | ptr++) { |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4494 | if (*ptr == -1 || *ptr == NO_CMDLINE_MAP) |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 4495 | continue; |
| 4496 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4497 | return ptr; |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 4498 | } |
| 4499 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4500 | return NULL; |
| 4501 | } |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 4502 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4503 | static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos) |
| 4504 | { |
| 4505 | void *v; |
| 4506 | loff_t l = 0; |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 4507 | |
Steven Rostedt (Red Hat) | 4c27e75 | 2014-05-30 10:49:46 -0400 | [diff] [blame] | 4508 | preempt_disable(); |
| 4509 | arch_spin_lock(&trace_cmdline_lock); |
| 4510 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 4511 | v = &savedcmd->map_cmdline_to_pid[0]; |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4512 | while (l <= *pos) { |
| 4513 | v = saved_cmdlines_next(m, v, &l); |
| 4514 | if (!v) |
| 4515 | return NULL; |
| 4516 | } |
| 4517 | |
| 4518 | return v; |
| 4519 | } |
| 4520 | |
| 4521 | static void saved_cmdlines_stop(struct seq_file *m, void *v) |
| 4522 | { |
Steven Rostedt (Red Hat) | 4c27e75 | 2014-05-30 10:49:46 -0400 | [diff] [blame] | 4523 | arch_spin_unlock(&trace_cmdline_lock); |
| 4524 | preempt_enable(); |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | static int saved_cmdlines_show(struct seq_file *m, void *v) |
| 4528 | { |
| 4529 | char buf[TASK_COMM_LEN]; |
| 4530 | unsigned int *pid = v; |
| 4531 | |
Steven Rostedt (Red Hat) | 4c27e75 | 2014-05-30 10:49:46 -0400 | [diff] [blame] | 4532 | __trace_find_cmdline(*pid, buf); |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4533 | seq_printf(m, "%d %s\n", *pid, buf); |
| 4534 | return 0; |
| 4535 | } |
| 4536 | |
| 4537 | static const struct seq_operations tracing_saved_cmdlines_seq_ops = { |
| 4538 | .start = saved_cmdlines_start, |
| 4539 | .next = saved_cmdlines_next, |
| 4540 | .stop = saved_cmdlines_stop, |
| 4541 | .show = saved_cmdlines_show, |
| 4542 | }; |
| 4543 | |
| 4544 | static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp) |
| 4545 | { |
| 4546 | if (tracing_disabled) |
| 4547 | return -ENODEV; |
| 4548 | |
| 4549 | return seq_open(filp, &tracing_saved_cmdlines_seq_ops); |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 4550 | } |
| 4551 | |
| 4552 | static const struct file_operations tracing_saved_cmdlines_fops = { |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 4553 | .open = tracing_saved_cmdlines_open, |
| 4554 | .read = seq_read, |
| 4555 | .llseek = seq_lseek, |
| 4556 | .release = seq_release, |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 4557 | }; |
| 4558 | |
| 4559 | static ssize_t |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 4560 | tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf, |
| 4561 | size_t cnt, loff_t *ppos) |
| 4562 | { |
| 4563 | char buf[64]; |
| 4564 | int r; |
| 4565 | |
| 4566 | arch_spin_lock(&trace_cmdline_lock); |
Namhyung Kim | a6af8fb | 2014-06-10 16:11:35 +0900 | [diff] [blame] | 4567 | r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num); |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 4568 | arch_spin_unlock(&trace_cmdline_lock); |
| 4569 | |
| 4570 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 4571 | } |
| 4572 | |
| 4573 | static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s) |
| 4574 | { |
| 4575 | kfree(s->saved_cmdlines); |
| 4576 | kfree(s->map_cmdline_to_pid); |
| 4577 | kfree(s); |
| 4578 | } |
| 4579 | |
| 4580 | static int tracing_resize_saved_cmdlines(unsigned int val) |
| 4581 | { |
| 4582 | struct saved_cmdlines_buffer *s, *savedcmd_temp; |
| 4583 | |
Namhyung Kim | a6af8fb | 2014-06-10 16:11:35 +0900 | [diff] [blame] | 4584 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 4585 | if (!s) |
| 4586 | return -ENOMEM; |
| 4587 | |
| 4588 | if (allocate_cmdlines_buffer(val, s) < 0) { |
| 4589 | kfree(s); |
| 4590 | return -ENOMEM; |
| 4591 | } |
| 4592 | |
| 4593 | arch_spin_lock(&trace_cmdline_lock); |
| 4594 | savedcmd_temp = savedcmd; |
| 4595 | savedcmd = s; |
| 4596 | arch_spin_unlock(&trace_cmdline_lock); |
| 4597 | free_saved_cmdlines_buffer(savedcmd_temp); |
| 4598 | |
| 4599 | return 0; |
| 4600 | } |
| 4601 | |
| 4602 | static ssize_t |
| 4603 | tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf, |
| 4604 | size_t cnt, loff_t *ppos) |
| 4605 | { |
| 4606 | unsigned long val; |
| 4607 | int ret; |
| 4608 | |
| 4609 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 4610 | if (ret) |
| 4611 | return ret; |
| 4612 | |
| 4613 | /* must have at least 1 entry or less than PID_MAX_DEFAULT */ |
| 4614 | if (!val || val > PID_MAX_DEFAULT) |
| 4615 | return -EINVAL; |
| 4616 | |
| 4617 | ret = tracing_resize_saved_cmdlines((unsigned int)val); |
| 4618 | if (ret < 0) |
| 4619 | return ret; |
| 4620 | |
| 4621 | *ppos += cnt; |
| 4622 | |
| 4623 | return cnt; |
| 4624 | } |
| 4625 | |
| 4626 | static const struct file_operations tracing_saved_cmdlines_size_fops = { |
| 4627 | .open = tracing_open_generic, |
| 4628 | .read = tracing_saved_cmdlines_size_read, |
| 4629 | .write = tracing_saved_cmdlines_size_write, |
| 4630 | }; |
| 4631 | |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 4632 | #ifdef CONFIG_TRACE_ENUM_MAP_FILE |
| 4633 | static union trace_enum_map_item * |
| 4634 | update_enum_map(union trace_enum_map_item *ptr) |
| 4635 | { |
| 4636 | if (!ptr->map.enum_string) { |
| 4637 | if (ptr->tail.next) { |
| 4638 | ptr = ptr->tail.next; |
| 4639 | /* Set ptr to the next real item (skip head) */ |
| 4640 | ptr++; |
| 4641 | } else |
| 4642 | return NULL; |
| 4643 | } |
| 4644 | return ptr; |
| 4645 | } |
| 4646 | |
| 4647 | static void *enum_map_next(struct seq_file *m, void *v, loff_t *pos) |
| 4648 | { |
| 4649 | union trace_enum_map_item *ptr = v; |
| 4650 | |
| 4651 | /* |
| 4652 | * Paranoid! If ptr points to end, we don't want to increment past it. |
| 4653 | * This really should never happen. |
| 4654 | */ |
| 4655 | ptr = update_enum_map(ptr); |
| 4656 | if (WARN_ON_ONCE(!ptr)) |
| 4657 | return NULL; |
| 4658 | |
| 4659 | ptr++; |
| 4660 | |
| 4661 | (*pos)++; |
| 4662 | |
| 4663 | ptr = update_enum_map(ptr); |
| 4664 | |
| 4665 | return ptr; |
| 4666 | } |
| 4667 | |
| 4668 | static void *enum_map_start(struct seq_file *m, loff_t *pos) |
| 4669 | { |
| 4670 | union trace_enum_map_item *v; |
| 4671 | loff_t l = 0; |
| 4672 | |
| 4673 | mutex_lock(&trace_enum_mutex); |
| 4674 | |
| 4675 | v = trace_enum_maps; |
| 4676 | if (v) |
| 4677 | v++; |
| 4678 | |
| 4679 | while (v && l < *pos) { |
| 4680 | v = enum_map_next(m, v, &l); |
| 4681 | } |
| 4682 | |
| 4683 | return v; |
| 4684 | } |
| 4685 | |
| 4686 | static void enum_map_stop(struct seq_file *m, void *v) |
| 4687 | { |
| 4688 | mutex_unlock(&trace_enum_mutex); |
| 4689 | } |
| 4690 | |
| 4691 | static int enum_map_show(struct seq_file *m, void *v) |
| 4692 | { |
| 4693 | union trace_enum_map_item *ptr = v; |
| 4694 | |
| 4695 | seq_printf(m, "%s %ld (%s)\n", |
| 4696 | ptr->map.enum_string, ptr->map.enum_value, |
| 4697 | ptr->map.system); |
| 4698 | |
| 4699 | return 0; |
| 4700 | } |
| 4701 | |
| 4702 | static const struct seq_operations tracing_enum_map_seq_ops = { |
| 4703 | .start = enum_map_start, |
| 4704 | .next = enum_map_next, |
| 4705 | .stop = enum_map_stop, |
| 4706 | .show = enum_map_show, |
| 4707 | }; |
| 4708 | |
| 4709 | static int tracing_enum_map_open(struct inode *inode, struct file *filp) |
| 4710 | { |
| 4711 | if (tracing_disabled) |
| 4712 | return -ENODEV; |
| 4713 | |
| 4714 | return seq_open(filp, &tracing_enum_map_seq_ops); |
| 4715 | } |
| 4716 | |
| 4717 | static const struct file_operations tracing_enum_map_fops = { |
| 4718 | .open = tracing_enum_map_open, |
| 4719 | .read = seq_read, |
| 4720 | .llseek = seq_lseek, |
| 4721 | .release = seq_release, |
| 4722 | }; |
| 4723 | |
| 4724 | static inline union trace_enum_map_item * |
| 4725 | trace_enum_jmp_to_tail(union trace_enum_map_item *ptr) |
| 4726 | { |
| 4727 | /* Return tail of array given the head */ |
| 4728 | return ptr + ptr->head.length + 1; |
| 4729 | } |
| 4730 | |
| 4731 | static void |
| 4732 | trace_insert_enum_map_file(struct module *mod, struct trace_enum_map **start, |
| 4733 | int len) |
| 4734 | { |
| 4735 | struct trace_enum_map **stop; |
| 4736 | struct trace_enum_map **map; |
| 4737 | union trace_enum_map_item *map_array; |
| 4738 | union trace_enum_map_item *ptr; |
| 4739 | |
| 4740 | stop = start + len; |
| 4741 | |
| 4742 | /* |
| 4743 | * The trace_enum_maps contains the map plus a head and tail item, |
| 4744 | * where the head holds the module and length of array, and the |
| 4745 | * tail holds a pointer to the next list. |
| 4746 | */ |
| 4747 | map_array = kmalloc(sizeof(*map_array) * (len + 2), GFP_KERNEL); |
| 4748 | if (!map_array) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 4749 | pr_warn("Unable to allocate trace enum mapping\n"); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 4750 | return; |
| 4751 | } |
| 4752 | |
| 4753 | mutex_lock(&trace_enum_mutex); |
| 4754 | |
| 4755 | if (!trace_enum_maps) |
| 4756 | trace_enum_maps = map_array; |
| 4757 | else { |
| 4758 | ptr = trace_enum_maps; |
| 4759 | for (;;) { |
| 4760 | ptr = trace_enum_jmp_to_tail(ptr); |
| 4761 | if (!ptr->tail.next) |
| 4762 | break; |
| 4763 | ptr = ptr->tail.next; |
| 4764 | |
| 4765 | } |
| 4766 | ptr->tail.next = map_array; |
| 4767 | } |
| 4768 | map_array->head.mod = mod; |
| 4769 | map_array->head.length = len; |
| 4770 | map_array++; |
| 4771 | |
| 4772 | for (map = start; (unsigned long)map < (unsigned long)stop; map++) { |
| 4773 | map_array->map = **map; |
| 4774 | map_array++; |
| 4775 | } |
| 4776 | memset(map_array, 0, sizeof(*map_array)); |
| 4777 | |
| 4778 | mutex_unlock(&trace_enum_mutex); |
| 4779 | } |
| 4780 | |
| 4781 | static void trace_create_enum_file(struct dentry *d_tracer) |
| 4782 | { |
| 4783 | trace_create_file("enum_map", 0444, d_tracer, |
| 4784 | NULL, &tracing_enum_map_fops); |
| 4785 | } |
| 4786 | |
| 4787 | #else /* CONFIG_TRACE_ENUM_MAP_FILE */ |
| 4788 | static inline void trace_create_enum_file(struct dentry *d_tracer) { } |
| 4789 | static inline void trace_insert_enum_map_file(struct module *mod, |
| 4790 | struct trace_enum_map **start, int len) { } |
| 4791 | #endif /* !CONFIG_TRACE_ENUM_MAP_FILE */ |
| 4792 | |
| 4793 | static void trace_insert_enum_map(struct module *mod, |
| 4794 | struct trace_enum_map **start, int len) |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 4795 | { |
| 4796 | struct trace_enum_map **map; |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 4797 | |
| 4798 | if (len <= 0) |
| 4799 | return; |
| 4800 | |
| 4801 | map = start; |
| 4802 | |
| 4803 | trace_event_enum_update(map, len); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 4804 | |
| 4805 | trace_insert_enum_map_file(mod, start, len); |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 4806 | } |
| 4807 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 4808 | static ssize_t |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4809 | tracing_set_trace_read(struct file *filp, char __user *ubuf, |
| 4810 | size_t cnt, loff_t *ppos) |
| 4811 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4812 | struct trace_array *tr = filp->private_data; |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 4813 | char buf[MAX_TRACER_SIZE+2]; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4814 | int r; |
| 4815 | |
| 4816 | mutex_lock(&trace_types_lock); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4817 | r = sprintf(buf, "%s\n", tr->current_trace->name); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4818 | mutex_unlock(&trace_types_lock); |
| 4819 | |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 4820 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4821 | } |
| 4822 | |
Arnaldo Carvalho de Melo | b6f11df | 2009-02-05 18:02:00 -0200 | [diff] [blame] | 4823 | int tracer_init(struct tracer *t, struct trace_array *tr) |
| 4824 | { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4825 | tracing_reset_online_cpus(&tr->trace_buffer); |
Arnaldo Carvalho de Melo | b6f11df | 2009-02-05 18:02:00 -0200 | [diff] [blame] | 4826 | return t->init(tr); |
| 4827 | } |
| 4828 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4829 | static void set_buffer_entries(struct trace_buffer *buf, unsigned long val) |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4830 | { |
| 4831 | int cpu; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 4832 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4833 | for_each_tracing_cpu(cpu) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4834 | per_cpu_ptr(buf->data, cpu)->entries = val; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4835 | } |
| 4836 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4837 | #ifdef CONFIG_TRACER_MAX_TRACE |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 4838 | /* resize @tr's buffer to the size of @size_tr's entries */ |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4839 | static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf, |
| 4840 | struct trace_buffer *size_buf, int cpu_id) |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 4841 | { |
| 4842 | int cpu, ret = 0; |
| 4843 | |
| 4844 | if (cpu_id == RING_BUFFER_ALL_CPUS) { |
| 4845 | for_each_tracing_cpu(cpu) { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4846 | ret = ring_buffer_resize(trace_buf->buffer, |
| 4847 | per_cpu_ptr(size_buf->data, cpu)->entries, cpu); |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 4848 | if (ret < 0) |
| 4849 | break; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4850 | per_cpu_ptr(trace_buf->data, cpu)->entries = |
| 4851 | per_cpu_ptr(size_buf->data, cpu)->entries; |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 4852 | } |
| 4853 | } else { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4854 | ret = ring_buffer_resize(trace_buf->buffer, |
| 4855 | per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id); |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 4856 | if (ret == 0) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4857 | per_cpu_ptr(trace_buf->data, cpu_id)->entries = |
| 4858 | per_cpu_ptr(size_buf->data, cpu_id)->entries; |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 4859 | } |
| 4860 | |
| 4861 | return ret; |
| 4862 | } |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4863 | #endif /* CONFIG_TRACER_MAX_TRACE */ |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 4864 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4865 | static int __tracing_resize_ring_buffer(struct trace_array *tr, |
| 4866 | unsigned long size, int cpu) |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 4867 | { |
| 4868 | int ret; |
| 4869 | |
| 4870 | /* |
| 4871 | * If kernel or user changes the size of the ring buffer |
Steven Rostedt | a123c52 | 2009-03-12 11:21:08 -0400 | [diff] [blame] | 4872 | * we use the size that was given, and we can forget about |
| 4873 | * expanding it later. |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 4874 | */ |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 4875 | ring_buffer_expanded = true; |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 4876 | |
Steven Rostedt | b382ede6 | 2012-10-10 21:44:34 -0400 | [diff] [blame] | 4877 | /* May be called before buffers are initialized */ |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4878 | if (!tr->trace_buffer.buffer) |
Steven Rostedt | b382ede6 | 2012-10-10 21:44:34 -0400 | [diff] [blame] | 4879 | return 0; |
| 4880 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4881 | ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu); |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 4882 | if (ret < 0) |
| 4883 | return ret; |
| 4884 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4885 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4886 | if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) || |
| 4887 | !tr->current_trace->use_max_tr) |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 4888 | goto out; |
| 4889 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4890 | ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu); |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 4891 | if (ret < 0) { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4892 | int r = resize_buffer_duplicate_size(&tr->trace_buffer, |
| 4893 | &tr->trace_buffer, cpu); |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 4894 | if (r < 0) { |
Steven Rostedt | a123c52 | 2009-03-12 11:21:08 -0400 | [diff] [blame] | 4895 | /* |
| 4896 | * AARGH! We are left with different |
| 4897 | * size max buffer!!!! |
| 4898 | * The max buffer is our "snapshot" buffer. |
| 4899 | * When a tracer needs a snapshot (one of the |
| 4900 | * latency tracers), it swaps the max buffer |
| 4901 | * with the saved snap shot. We succeeded to |
| 4902 | * update the size of the main buffer, but failed to |
| 4903 | * update the size of the max buffer. But when we tried |
| 4904 | * to reset the main buffer to the original size, we |
| 4905 | * failed there too. This is very unlikely to |
| 4906 | * happen, but if it does, warn and kill all |
| 4907 | * tracing. |
| 4908 | */ |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 4909 | WARN_ON(1); |
| 4910 | tracing_disabled = 1; |
| 4911 | } |
| 4912 | return ret; |
| 4913 | } |
| 4914 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4915 | if (cpu == RING_BUFFER_ALL_CPUS) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4916 | set_buffer_entries(&tr->max_buffer, size); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4917 | else |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4918 | per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4919 | |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 4920 | out: |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4921 | #endif /* CONFIG_TRACER_MAX_TRACE */ |
| 4922 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4923 | if (cpu == RING_BUFFER_ALL_CPUS) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4924 | set_buffer_entries(&tr->trace_buffer, size); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4925 | else |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4926 | per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size; |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 4927 | |
| 4928 | return ret; |
| 4929 | } |
| 4930 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4931 | static ssize_t tracing_resize_ring_buffer(struct trace_array *tr, |
| 4932 | unsigned long size, int cpu_id) |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 4933 | { |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 4934 | int ret = size; |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 4935 | |
| 4936 | mutex_lock(&trace_types_lock); |
| 4937 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4938 | if (cpu_id != RING_BUFFER_ALL_CPUS) { |
| 4939 | /* make sure, this cpu is enabled in the mask */ |
| 4940 | if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) { |
| 4941 | ret = -EINVAL; |
| 4942 | goto out; |
| 4943 | } |
| 4944 | } |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 4945 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4946 | ret = __tracing_resize_ring_buffer(tr, size, cpu_id); |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 4947 | if (ret < 0) |
| 4948 | ret = -ENOMEM; |
| 4949 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4950 | out: |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 4951 | mutex_unlock(&trace_types_lock); |
| 4952 | |
| 4953 | return ret; |
| 4954 | } |
| 4955 | |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 4956 | |
Steven Rostedt | 1852fcc | 2009-03-11 14:33:00 -0400 | [diff] [blame] | 4957 | /** |
| 4958 | * tracing_update_buffers - used by tracing facility to expand ring buffers |
| 4959 | * |
| 4960 | * To save on memory when the tracing is never used on a system with it |
| 4961 | * configured in. The ring buffers are set to a minimum size. But once |
| 4962 | * a user starts to use the tracing facility, then they need to grow |
| 4963 | * to their default size. |
| 4964 | * |
| 4965 | * This function is to be called when a tracer is about to be used. |
| 4966 | */ |
| 4967 | int tracing_update_buffers(void) |
| 4968 | { |
| 4969 | int ret = 0; |
| 4970 | |
Steven Rostedt | 1027fcb | 2009-03-12 11:33:20 -0400 | [diff] [blame] | 4971 | mutex_lock(&trace_types_lock); |
Steven Rostedt | 1852fcc | 2009-03-11 14:33:00 -0400 | [diff] [blame] | 4972 | if (!ring_buffer_expanded) |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4973 | ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size, |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4974 | RING_BUFFER_ALL_CPUS); |
Steven Rostedt | 1027fcb | 2009-03-12 11:33:20 -0400 | [diff] [blame] | 4975 | mutex_unlock(&trace_types_lock); |
Steven Rostedt | 1852fcc | 2009-03-11 14:33:00 -0400 | [diff] [blame] | 4976 | |
| 4977 | return ret; |
| 4978 | } |
| 4979 | |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 4980 | struct trace_option_dentry; |
| 4981 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 4982 | static void |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4983 | create_trace_option_files(struct trace_array *tr, struct tracer *tracer); |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 4984 | |
Steven Rostedt (Red Hat) | 6b450d2 | 2014-01-14 08:43:01 -0500 | [diff] [blame] | 4985 | /* |
| 4986 | * Used to clear out the tracer before deletion of an instance. |
| 4987 | * Must have trace_types_lock held. |
| 4988 | */ |
| 4989 | static void tracing_set_nop(struct trace_array *tr) |
| 4990 | { |
| 4991 | if (tr->current_trace == &nop_trace) |
| 4992 | return; |
| 4993 | |
Steven Rostedt (Red Hat) | 50512ab | 2014-01-14 08:52:35 -0500 | [diff] [blame] | 4994 | tr->current_trace->enabled--; |
Steven Rostedt (Red Hat) | 6b450d2 | 2014-01-14 08:43:01 -0500 | [diff] [blame] | 4995 | |
| 4996 | if (tr->current_trace->reset) |
| 4997 | tr->current_trace->reset(tr); |
| 4998 | |
| 4999 | tr->current_trace = &nop_trace; |
| 5000 | } |
| 5001 | |
Steven Rostedt (Red Hat) | 41d9c0b | 2015-09-29 17:31:55 -0400 | [diff] [blame] | 5002 | static void add_tracer_options(struct trace_array *tr, struct tracer *t) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5003 | { |
Steven Rostedt (Red Hat) | 09d23a1 | 2015-02-03 12:45:53 -0500 | [diff] [blame] | 5004 | /* Only enable if the directory has been created already. */ |
| 5005 | if (!tr->dir) |
| 5006 | return; |
| 5007 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 5008 | create_trace_option_files(tr, t); |
Steven Rostedt (Red Hat) | 09d23a1 | 2015-02-03 12:45:53 -0500 | [diff] [blame] | 5009 | } |
| 5010 | |
| 5011 | static int tracing_set_tracer(struct trace_array *tr, const char *buf) |
| 5012 | { |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5013 | struct tracer *t; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5014 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 5015 | bool had_max_tr; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5016 | #endif |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 5017 | int ret = 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5018 | |
Steven Rostedt | 1027fcb | 2009-03-12 11:33:20 -0400 | [diff] [blame] | 5019 | mutex_lock(&trace_types_lock); |
| 5020 | |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5021 | if (!ring_buffer_expanded) { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5022 | ret = __tracing_resize_ring_buffer(tr, trace_buf_size, |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5023 | RING_BUFFER_ALL_CPUS); |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5024 | if (ret < 0) |
Frederic Weisbecker | 59f586d | 2009-03-15 22:10:39 +0100 | [diff] [blame] | 5025 | goto out; |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5026 | ret = 0; |
| 5027 | } |
| 5028 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5029 | for (t = trace_types; t; t = t->next) { |
| 5030 | if (strcmp(t->name, buf) == 0) |
| 5031 | break; |
| 5032 | } |
Frederic Weisbecker | c2931e0 | 2008-10-04 22:04:44 +0200 | [diff] [blame] | 5033 | if (!t) { |
| 5034 | ret = -EINVAL; |
| 5035 | goto out; |
| 5036 | } |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5037 | if (t == tr->current_trace) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5038 | goto out; |
| 5039 | |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 5040 | /* Some tracers are only allowed for the top level buffer */ |
| 5041 | if (!trace_ok_for_array(t, tr)) { |
| 5042 | ret = -EINVAL; |
| 5043 | goto out; |
| 5044 | } |
| 5045 | |
Steven Rostedt (Red Hat) | cf6ab6d | 2014-12-15 20:13:31 -0500 | [diff] [blame] | 5046 | /* If trace pipe files are being read, we can't change the tracer */ |
| 5047 | if (tr->current_trace->ref) { |
| 5048 | ret = -EBUSY; |
| 5049 | goto out; |
| 5050 | } |
| 5051 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 5052 | trace_branch_disable(); |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 5053 | |
Steven Rostedt (Red Hat) | 50512ab | 2014-01-14 08:52:35 -0500 | [diff] [blame] | 5054 | tr->current_trace->enabled--; |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 5055 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5056 | if (tr->current_trace->reset) |
| 5057 | tr->current_trace->reset(tr); |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 5058 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5059 | /* Current trace needs to be nop_trace before synchronize_sched */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5060 | tr->current_trace = &nop_trace; |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 5061 | |
Steven Rostedt (Red Hat) | 45ad21c | 2013-03-05 18:25:02 -0500 | [diff] [blame] | 5062 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 5063 | had_max_tr = tr->allocated_snapshot; |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 5064 | |
| 5065 | if (had_max_tr && !t->use_max_tr) { |
| 5066 | /* |
| 5067 | * We need to make sure that the update_max_tr sees that |
| 5068 | * current_trace changed to nop_trace to keep it from |
| 5069 | * swapping the buffers after we resize it. |
| 5070 | * The update_max_tr is called from interrupts disabled |
| 5071 | * so a synchronized_sched() is sufficient. |
| 5072 | */ |
| 5073 | synchronize_sched(); |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 5074 | free_snapshot(tr); |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 5075 | } |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5076 | #endif |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5077 | |
| 5078 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 5079 | if (t->use_max_tr && !had_max_tr) { |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 5080 | ret = alloc_snapshot(tr); |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 5081 | if (ret < 0) |
| 5082 | goto out; |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 5083 | } |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5084 | #endif |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 5085 | |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 5086 | if (t->init) { |
Arnaldo Carvalho de Melo | b6f11df | 2009-02-05 18:02:00 -0200 | [diff] [blame] | 5087 | ret = tracer_init(t, tr); |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 5088 | if (ret) |
| 5089 | goto out; |
| 5090 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5091 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5092 | tr->current_trace = t; |
Steven Rostedt (Red Hat) | 50512ab | 2014-01-14 08:52:35 -0500 | [diff] [blame] | 5093 | tr->current_trace->enabled++; |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 5094 | trace_branch_enable(tr); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5095 | out: |
| 5096 | mutex_unlock(&trace_types_lock); |
| 5097 | |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 5098 | return ret; |
| 5099 | } |
| 5100 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5101 | static ssize_t |
| 5102 | tracing_set_trace_write(struct file *filp, const char __user *ubuf, |
| 5103 | size_t cnt, loff_t *ppos) |
| 5104 | { |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 5105 | struct trace_array *tr = filp->private_data; |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 5106 | char buf[MAX_TRACER_SIZE+1]; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5107 | int i; |
| 5108 | size_t ret; |
Frederic Weisbecker | e6e7a65 | 2008-11-16 05:53:19 +0100 | [diff] [blame] | 5109 | int err; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5110 | |
Steven Rostedt | 60063a6 | 2008-10-28 10:44:24 -0400 | [diff] [blame] | 5111 | ret = cnt; |
| 5112 | |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 5113 | if (cnt > MAX_TRACER_SIZE) |
| 5114 | cnt = MAX_TRACER_SIZE; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5115 | |
Wang Xiaoqiang | 4afe649 | 2016-04-18 15:23:29 +0800 | [diff] [blame] | 5116 | if (copy_from_user(buf, ubuf, cnt)) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5117 | return -EFAULT; |
| 5118 | |
| 5119 | buf[cnt] = 0; |
| 5120 | |
| 5121 | /* strip ending whitespace. */ |
| 5122 | for (i = cnt - 1; i > 0 && isspace(buf[i]); i--) |
| 5123 | buf[i] = 0; |
| 5124 | |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 5125 | err = tracing_set_tracer(tr, buf); |
Frederic Weisbecker | e6e7a65 | 2008-11-16 05:53:19 +0100 | [diff] [blame] | 5126 | if (err) |
| 5127 | return err; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5128 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 5129 | *ppos += ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5130 | |
Frederic Weisbecker | c2931e0 | 2008-10-04 22:04:44 +0200 | [diff] [blame] | 5131 | return ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5132 | } |
| 5133 | |
| 5134 | static ssize_t |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 5135 | tracing_nsecs_read(unsigned long *ptr, char __user *ubuf, |
| 5136 | size_t cnt, loff_t *ppos) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5137 | { |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5138 | char buf[64]; |
| 5139 | int r; |
| 5140 | |
Steven Rostedt | cffae43 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 5141 | r = snprintf(buf, sizeof(buf), "%ld\n", |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5142 | *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr)); |
Steven Rostedt | cffae43 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 5143 | if (r > sizeof(buf)) |
| 5144 | r = sizeof(buf); |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5145 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | static ssize_t |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 5149 | tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf, |
| 5150 | size_t cnt, loff_t *ppos) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5151 | { |
Hannes Eder | 5e39841 | 2009-02-10 19:44:34 +0100 | [diff] [blame] | 5152 | unsigned long val; |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 5153 | int ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5154 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 5155 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 5156 | if (ret) |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 5157 | return ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5158 | |
| 5159 | *ptr = val * 1000; |
| 5160 | |
| 5161 | return cnt; |
| 5162 | } |
| 5163 | |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 5164 | static ssize_t |
| 5165 | tracing_thresh_read(struct file *filp, char __user *ubuf, |
| 5166 | size_t cnt, loff_t *ppos) |
| 5167 | { |
| 5168 | return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos); |
| 5169 | } |
| 5170 | |
| 5171 | static ssize_t |
| 5172 | tracing_thresh_write(struct file *filp, const char __user *ubuf, |
| 5173 | size_t cnt, loff_t *ppos) |
| 5174 | { |
| 5175 | struct trace_array *tr = filp->private_data; |
| 5176 | int ret; |
| 5177 | |
| 5178 | mutex_lock(&trace_types_lock); |
| 5179 | ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos); |
| 5180 | if (ret < 0) |
| 5181 | goto out; |
| 5182 | |
| 5183 | if (tr->current_trace->update_thresh) { |
| 5184 | ret = tr->current_trace->update_thresh(tr); |
| 5185 | if (ret < 0) |
| 5186 | goto out; |
| 5187 | } |
| 5188 | |
| 5189 | ret = cnt; |
| 5190 | out: |
| 5191 | mutex_unlock(&trace_types_lock); |
| 5192 | |
| 5193 | return ret; |
| 5194 | } |
| 5195 | |
Steven Rostedt (Red Hat) | f971cc9 | 2016-09-07 12:45:09 -0400 | [diff] [blame] | 5196 | #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER) |
Chen Gang | e428abb | 2015-11-10 05:15:15 +0800 | [diff] [blame] | 5197 | |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 5198 | static ssize_t |
| 5199 | tracing_max_lat_read(struct file *filp, char __user *ubuf, |
| 5200 | size_t cnt, loff_t *ppos) |
| 5201 | { |
| 5202 | return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos); |
| 5203 | } |
| 5204 | |
| 5205 | static ssize_t |
| 5206 | tracing_max_lat_write(struct file *filp, const char __user *ubuf, |
| 5207 | size_t cnt, loff_t *ppos) |
| 5208 | { |
| 5209 | return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos); |
| 5210 | } |
| 5211 | |
Chen Gang | e428abb | 2015-11-10 05:15:15 +0800 | [diff] [blame] | 5212 | #endif |
| 5213 | |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5214 | static int tracing_open_pipe(struct inode *inode, struct file *filp) |
| 5215 | { |
Oleg Nesterov | 1554420 | 2013-07-23 17:25:57 +0200 | [diff] [blame] | 5216 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5217 | struct trace_iterator *iter; |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 5218 | int ret = 0; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5219 | |
| 5220 | if (tracing_disabled) |
| 5221 | return -ENODEV; |
| 5222 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 5223 | if (trace_array_get(tr) < 0) |
| 5224 | return -ENODEV; |
| 5225 | |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 5226 | mutex_lock(&trace_types_lock); |
| 5227 | |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5228 | /* create a buffer to store the information to pass to userspace */ |
| 5229 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 5230 | if (!iter) { |
| 5231 | ret = -ENOMEM; |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 5232 | __trace_array_put(tr); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 5233 | goto out; |
| 5234 | } |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5235 | |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 5236 | trace_seq_init(&iter->seq); |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 5237 | iter->trace = tr->current_trace; |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 5238 | |
| 5239 | if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) { |
| 5240 | ret = -ENOMEM; |
| 5241 | goto fail; |
Rusty Russell | 4462344 | 2009-01-01 10:12:23 +1030 | [diff] [blame] | 5242 | } |
| 5243 | |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 5244 | /* trace pipe does not show start of buffer */ |
Rusty Russell | 4462344 | 2009-01-01 10:12:23 +1030 | [diff] [blame] | 5245 | cpumask_setall(iter->started); |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 5246 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 5247 | if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) |
Steven Rostedt | 112f38a7 | 2009-06-01 15:16:05 -0400 | [diff] [blame] | 5248 | iter->iter_flags |= TRACE_FILE_LAT_FMT; |
| 5249 | |
David Sharp | 8be0709 | 2012-11-13 12:18:22 -0800 | [diff] [blame] | 5250 | /* Output in nanoseconds only if we are using a clock in nanoseconds. */ |
Yoshihiro YUNOMAE | 58e8eed | 2013-04-23 10:32:39 +0900 | [diff] [blame] | 5251 | if (trace_clocks[tr->clock_id].in_ns) |
David Sharp | 8be0709 | 2012-11-13 12:18:22 -0800 | [diff] [blame] | 5252 | iter->iter_flags |= TRACE_FILE_TIME_IN_NS; |
| 5253 | |
Oleg Nesterov | 1554420 | 2013-07-23 17:25:57 +0200 | [diff] [blame] | 5254 | iter->tr = tr; |
| 5255 | iter->trace_buffer = &tr->trace_buffer; |
| 5256 | iter->cpu_file = tracing_get_cpu(inode); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 5257 | mutex_init(&iter->mutex); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5258 | filp->private_data = iter; |
| 5259 | |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 5260 | if (iter->trace->pipe_open) |
| 5261 | iter->trace->pipe_open(iter); |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 5262 | |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 5263 | nonseekable_open(inode, filp); |
Steven Rostedt (Red Hat) | cf6ab6d | 2014-12-15 20:13:31 -0500 | [diff] [blame] | 5264 | |
| 5265 | tr->current_trace->ref++; |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 5266 | out: |
| 5267 | mutex_unlock(&trace_types_lock); |
| 5268 | return ret; |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 5269 | |
| 5270 | fail: |
| 5271 | kfree(iter->trace); |
| 5272 | kfree(iter); |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 5273 | __trace_array_put(tr); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 5274 | mutex_unlock(&trace_types_lock); |
| 5275 | return ret; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | static int tracing_release_pipe(struct inode *inode, struct file *file) |
| 5279 | { |
| 5280 | struct trace_iterator *iter = file->private_data; |
Oleg Nesterov | 1554420 | 2013-07-23 17:25:57 +0200 | [diff] [blame] | 5281 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5282 | |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 5283 | mutex_lock(&trace_types_lock); |
| 5284 | |
Steven Rostedt (Red Hat) | cf6ab6d | 2014-12-15 20:13:31 -0500 | [diff] [blame] | 5285 | tr->current_trace->ref--; |
| 5286 | |
Steven Rostedt | 29bf4a5 | 2009-12-09 12:37:43 -0500 | [diff] [blame] | 5287 | if (iter->trace->pipe_close) |
Steven Rostedt | c521efd | 2009-12-07 09:06:24 -0500 | [diff] [blame] | 5288 | iter->trace->pipe_close(iter); |
| 5289 | |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 5290 | mutex_unlock(&trace_types_lock); |
| 5291 | |
Rusty Russell | 4462344 | 2009-01-01 10:12:23 +1030 | [diff] [blame] | 5292 | free_cpumask_var(iter->started); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 5293 | mutex_destroy(&iter->mutex); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5294 | kfree(iter); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5295 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 5296 | trace_array_put(tr); |
| 5297 | |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5298 | return 0; |
| 5299 | } |
| 5300 | |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 5301 | static unsigned int |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 5302 | trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table) |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 5303 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 5304 | struct trace_array *tr = iter->tr; |
| 5305 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 5306 | /* Iterators are static, they should be filled or empty */ |
| 5307 | if (trace_buffer_iter(iter, iter->cpu_file)) |
| 5308 | return POLLIN | POLLRDNORM; |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 5309 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 5310 | if (tr->trace_flags & TRACE_ITER_BLOCK) |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 5311 | /* |
| 5312 | * Always select as readable when in blocking mode |
| 5313 | */ |
| 5314 | return POLLIN | POLLRDNORM; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 5315 | else |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5316 | return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file, |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 5317 | filp, poll_table); |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 5318 | } |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 5319 | |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 5320 | static unsigned int |
| 5321 | tracing_poll_pipe(struct file *filp, poll_table *poll_table) |
| 5322 | { |
| 5323 | struct trace_iterator *iter = filp->private_data; |
| 5324 | |
| 5325 | return trace_poll(iter, filp, poll_table); |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 5326 | } |
| 5327 | |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 5328 | /* Must be called with iter->mutex held. */ |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 5329 | static int tracing_wait_pipe(struct file *filp) |
| 5330 | { |
| 5331 | struct trace_iterator *iter = filp->private_data; |
Steven Rostedt (Red Hat) | 8b8b368 | 2014-06-10 09:46:00 -0400 | [diff] [blame] | 5332 | int ret; |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 5333 | |
| 5334 | while (trace_empty(iter)) { |
| 5335 | |
| 5336 | if ((filp->f_flags & O_NONBLOCK)) { |
| 5337 | return -EAGAIN; |
| 5338 | } |
| 5339 | |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 5340 | /* |
Liu Bo | 250bfd3 | 2013-01-14 10:54:11 +0800 | [diff] [blame] | 5341 | * We block until we read something and tracing is disabled. |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 5342 | * We still block if tracing is disabled, but we have never |
| 5343 | * read anything. This allows a user to cat this file, and |
| 5344 | * then enable tracing. But after we have read something, |
| 5345 | * we give an EOF when tracing is again disabled. |
| 5346 | * |
| 5347 | * iter->pos will be 0 if we haven't read anything. |
| 5348 | */ |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 5349 | if (!tracing_is_on() && iter->pos) |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 5350 | break; |
Steven Rostedt (Red Hat) | f487426 | 2014-04-29 16:07:28 -0400 | [diff] [blame] | 5351 | |
| 5352 | mutex_unlock(&iter->mutex); |
| 5353 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 5354 | ret = wait_on_pipe(iter, false); |
Steven Rostedt (Red Hat) | f487426 | 2014-04-29 16:07:28 -0400 | [diff] [blame] | 5355 | |
| 5356 | mutex_lock(&iter->mutex); |
| 5357 | |
Steven Rostedt (Red Hat) | 8b8b368 | 2014-06-10 09:46:00 -0400 | [diff] [blame] | 5358 | if (ret) |
| 5359 | return ret; |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 5360 | } |
| 5361 | |
| 5362 | return 1; |
| 5363 | } |
| 5364 | |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5365 | /* |
| 5366 | * Consumer reader. |
| 5367 | */ |
| 5368 | static ssize_t |
| 5369 | tracing_read_pipe(struct file *filp, char __user *ubuf, |
| 5370 | size_t cnt, loff_t *ppos) |
| 5371 | { |
| 5372 | struct trace_iterator *iter = filp->private_data; |
Pekka Paalanen | 6c6c279 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 5373 | ssize_t sret; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5374 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 5375 | /* |
| 5376 | * Avoid more than one consumer on a single file descriptor |
| 5377 | * This is just a matter of traces coherency, the ring buffer itself |
| 5378 | * is protected. |
| 5379 | */ |
| 5380 | mutex_lock(&iter->mutex); |
Steven Rostedt (Red Hat) | 1245800 | 2016-09-23 22:57:13 -0400 | [diff] [blame] | 5381 | |
| 5382 | /* return any leftover data */ |
| 5383 | sret = trace_seq_to_user(&iter->seq, ubuf, cnt); |
| 5384 | if (sret != -EBUSY) |
| 5385 | goto out; |
| 5386 | |
| 5387 | trace_seq_init(&iter->seq); |
| 5388 | |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 5389 | if (iter->trace->read) { |
Pekka Paalanen | 6c6c279 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 5390 | sret = iter->trace->read(iter, filp, ubuf, cnt, ppos); |
| 5391 | if (sret) |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 5392 | goto out; |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 5393 | } |
| 5394 | |
Pekka Paalanen | 9ff4b97 | 2008-09-29 20:23:48 +0200 | [diff] [blame] | 5395 | waitagain: |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 5396 | sret = tracing_wait_pipe(filp); |
| 5397 | if (sret <= 0) |
| 5398 | goto out; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5399 | |
| 5400 | /* stop when tracing is finished */ |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 5401 | if (trace_empty(iter)) { |
| 5402 | sret = 0; |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 5403 | goto out; |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 5404 | } |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5405 | |
| 5406 | if (cnt >= PAGE_SIZE) |
| 5407 | cnt = PAGE_SIZE - 1; |
| 5408 | |
Steven Rostedt | 53d0aa7 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 5409 | /* reset all but tr, trace, and overruns */ |
Steven Rostedt | 53d0aa7 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 5410 | memset(&iter->seq, 0, |
| 5411 | sizeof(struct trace_iterator) - |
| 5412 | offsetof(struct trace_iterator, seq)); |
Andrew Vagin | ed5467d | 2013-08-02 21:16:43 +0400 | [diff] [blame] | 5413 | cpumask_clear(iter->started); |
Steven Rostedt | 4823ed7 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 5414 | iter->pos = -1; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5415 | |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 5416 | trace_event_read_lock(); |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 5417 | trace_access_lock(iter->cpu_file); |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 5418 | while (trace_find_next_entry_inc(iter) != NULL) { |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 5419 | enum print_line_t ret; |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 5420 | int save_len = iter->seq.seq.len; |
Steven Rostedt | 088b1e42 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 5421 | |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 5422 | ret = print_trace_line(iter); |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 5423 | if (ret == TRACE_TYPE_PARTIAL_LINE) { |
Steven Rostedt | 088b1e42 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 5424 | /* don't print partial lines */ |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 5425 | iter->seq.seq.len = save_len; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5426 | break; |
Steven Rostedt | 088b1e42 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 5427 | } |
Frederic Weisbecker | b91facc | 2009-02-06 18:30:44 +0100 | [diff] [blame] | 5428 | if (ret != TRACE_TYPE_NO_CONSUME) |
| 5429 | trace_consume(iter); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5430 | |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 5431 | if (trace_seq_used(&iter->seq) >= cnt) |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5432 | break; |
Jiri Olsa | ee5e51f | 2011-03-25 12:05:18 +0100 | [diff] [blame] | 5433 | |
| 5434 | /* |
| 5435 | * Setting the full flag means we reached the trace_seq buffer |
| 5436 | * size and we should leave by partial output condition above. |
| 5437 | * One of the trace_seq_* functions is not used properly. |
| 5438 | */ |
| 5439 | WARN_ONCE(iter->seq.full, "full flag set for trace type %d", |
| 5440 | iter->ent->type); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5441 | } |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 5442 | trace_access_unlock(iter->cpu_file); |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 5443 | trace_event_read_unlock(); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5444 | |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5445 | /* Now copy what we have to the user */ |
Pekka Paalanen | 6c6c279 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 5446 | sret = trace_seq_to_user(&iter->seq, ubuf, cnt); |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 5447 | if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq)) |
Steven Rostedt | f952075 | 2009-03-02 14:04:40 -0500 | [diff] [blame] | 5448 | trace_seq_init(&iter->seq); |
Pekka Paalanen | 9ff4b97 | 2008-09-29 20:23:48 +0200 | [diff] [blame] | 5449 | |
| 5450 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5451 | * If there was nothing to send to user, in spite of consuming trace |
Pekka Paalanen | 9ff4b97 | 2008-09-29 20:23:48 +0200 | [diff] [blame] | 5452 | * entries, go back to wait for more entries. |
| 5453 | */ |
Pekka Paalanen | 6c6c279 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 5454 | if (sret == -EBUSY) |
Pekka Paalanen | 9ff4b97 | 2008-09-29 20:23:48 +0200 | [diff] [blame] | 5455 | goto waitagain; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5456 | |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 5457 | out: |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 5458 | mutex_unlock(&iter->mutex); |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 5459 | |
Pekka Paalanen | 6c6c279 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 5460 | return sret; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5461 | } |
| 5462 | |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5463 | static void tracing_spd_release_pipe(struct splice_pipe_desc *spd, |
| 5464 | unsigned int idx) |
| 5465 | { |
| 5466 | __free_page(spd->pages[idx]); |
| 5467 | } |
| 5468 | |
Alexey Dobriyan | 28dfef8 | 2009-12-15 16:46:48 -0800 | [diff] [blame] | 5469 | static const struct pipe_buf_operations tracing_pipe_buf_ops = { |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5470 | .can_merge = 0, |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5471 | .confirm = generic_pipe_buf_confirm, |
Al Viro | 92fdd98 | 2014-01-17 07:53:39 -0500 | [diff] [blame] | 5472 | .release = generic_pipe_buf_release, |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5473 | .steal = generic_pipe_buf_steal, |
| 5474 | .get = generic_pipe_buf_get, |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5475 | }; |
| 5476 | |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5477 | static size_t |
Frederic Weisbecker | fa7c7f6 | 2009-02-11 02:51:30 +0100 | [diff] [blame] | 5478 | tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter) |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5479 | { |
| 5480 | size_t count; |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 5481 | int save_len; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5482 | int ret; |
| 5483 | |
| 5484 | /* Seq buffer is page-sized, exactly what we need. */ |
| 5485 | for (;;) { |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 5486 | save_len = iter->seq.seq.len; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5487 | ret = print_trace_line(iter); |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 5488 | |
| 5489 | if (trace_seq_has_overflowed(&iter->seq)) { |
| 5490 | iter->seq.seq.len = save_len; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5491 | break; |
| 5492 | } |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 5493 | |
| 5494 | /* |
| 5495 | * This should not be hit, because it should only |
| 5496 | * be set if the iter->seq overflowed. But check it |
| 5497 | * anyway to be safe. |
| 5498 | */ |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5499 | if (ret == TRACE_TYPE_PARTIAL_LINE) { |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 5500 | iter->seq.seq.len = save_len; |
| 5501 | break; |
| 5502 | } |
| 5503 | |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 5504 | count = trace_seq_used(&iter->seq) - save_len; |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 5505 | if (rem < count) { |
| 5506 | rem = 0; |
| 5507 | iter->seq.seq.len = save_len; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5508 | break; |
| 5509 | } |
| 5510 | |
Lai Jiangshan | 74e7ff8 | 2009-07-28 20:17:22 +0800 | [diff] [blame] | 5511 | if (ret != TRACE_TYPE_NO_CONSUME) |
| 5512 | trace_consume(iter); |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5513 | rem -= count; |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 5514 | if (!trace_find_next_entry_inc(iter)) { |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5515 | rem = 0; |
| 5516 | iter->ent = NULL; |
| 5517 | break; |
| 5518 | } |
| 5519 | } |
| 5520 | |
| 5521 | return rem; |
| 5522 | } |
| 5523 | |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5524 | static ssize_t tracing_splice_read_pipe(struct file *filp, |
| 5525 | loff_t *ppos, |
| 5526 | struct pipe_inode_info *pipe, |
| 5527 | size_t len, |
| 5528 | unsigned int flags) |
| 5529 | { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 5530 | struct page *pages_def[PIPE_DEF_BUFFERS]; |
| 5531 | struct partial_page partial_def[PIPE_DEF_BUFFERS]; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5532 | struct trace_iterator *iter = filp->private_data; |
| 5533 | struct splice_pipe_desc spd = { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 5534 | .pages = pages_def, |
| 5535 | .partial = partial_def, |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5536 | .nr_pages = 0, /* This gets updated below. */ |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 5537 | .nr_pages_max = PIPE_DEF_BUFFERS, |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5538 | .flags = flags, |
| 5539 | .ops = &tracing_pipe_buf_ops, |
| 5540 | .spd_release = tracing_spd_release_pipe, |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5541 | }; |
| 5542 | ssize_t ret; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5543 | size_t rem; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5544 | unsigned int i; |
| 5545 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 5546 | if (splice_grow_spd(pipe, &spd)) |
| 5547 | return -ENOMEM; |
| 5548 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 5549 | mutex_lock(&iter->mutex); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5550 | |
| 5551 | if (iter->trace->splice_read) { |
| 5552 | ret = iter->trace->splice_read(iter, filp, |
| 5553 | ppos, pipe, len, flags); |
| 5554 | if (ret) |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5555 | goto out_err; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | ret = tracing_wait_pipe(filp); |
| 5559 | if (ret <= 0) |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5560 | goto out_err; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5561 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 5562 | if (!iter->ent && !trace_find_next_entry_inc(iter)) { |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5563 | ret = -EFAULT; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5564 | goto out_err; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5565 | } |
| 5566 | |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 5567 | trace_event_read_lock(); |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 5568 | trace_access_lock(iter->cpu_file); |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 5569 | |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5570 | /* Fill as many pages as possible. */ |
Al Viro | a786c06 | 2014-04-11 12:01:03 -0400 | [diff] [blame] | 5571 | for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 5572 | spd.pages[i] = alloc_page(GFP_KERNEL); |
| 5573 | if (!spd.pages[i]) |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5574 | break; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5575 | |
Frederic Weisbecker | fa7c7f6 | 2009-02-11 02:51:30 +0100 | [diff] [blame] | 5576 | rem = tracing_fill_pipe_page(rem, iter); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5577 | |
| 5578 | /* Copy the data into the page, so we can start over. */ |
| 5579 | ret = trace_seq_to_buffer(&iter->seq, |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 5580 | page_address(spd.pages[i]), |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 5581 | trace_seq_used(&iter->seq)); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5582 | if (ret < 0) { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 5583 | __free_page(spd.pages[i]); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5584 | break; |
| 5585 | } |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 5586 | spd.partial[i].offset = 0; |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 5587 | spd.partial[i].len = trace_seq_used(&iter->seq); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5588 | |
Steven Rostedt | f952075 | 2009-03-02 14:04:40 -0500 | [diff] [blame] | 5589 | trace_seq_init(&iter->seq); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5590 | } |
| 5591 | |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 5592 | trace_access_unlock(iter->cpu_file); |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 5593 | trace_event_read_unlock(); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 5594 | mutex_unlock(&iter->mutex); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5595 | |
| 5596 | spd.nr_pages = i; |
| 5597 | |
Steven Rostedt (Red Hat) | a29054d9 | 2016-03-18 15:46:48 -0400 | [diff] [blame] | 5598 | if (i) |
| 5599 | ret = splice_to_pipe(pipe, &spd); |
| 5600 | else |
| 5601 | ret = 0; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 5602 | out: |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 5603 | splice_shrink_spd(&spd); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 5604 | return ret; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5605 | |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 5606 | out_err: |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 5607 | mutex_unlock(&iter->mutex); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 5608 | goto out; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 5609 | } |
| 5610 | |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 5611 | static ssize_t |
| 5612 | tracing_entries_read(struct file *filp, char __user *ubuf, |
| 5613 | size_t cnt, loff_t *ppos) |
| 5614 | { |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 5615 | struct inode *inode = file_inode(filp); |
| 5616 | struct trace_array *tr = inode->i_private; |
| 5617 | int cpu = tracing_get_cpu(inode); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5618 | char buf[64]; |
| 5619 | int r = 0; |
| 5620 | ssize_t ret; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 5621 | |
Steven Rostedt | db526ca | 2009-03-12 13:53:25 -0400 | [diff] [blame] | 5622 | mutex_lock(&trace_types_lock); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5623 | |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 5624 | if (cpu == RING_BUFFER_ALL_CPUS) { |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5625 | int cpu, buf_size_same; |
| 5626 | unsigned long size; |
| 5627 | |
| 5628 | size = 0; |
| 5629 | buf_size_same = 1; |
| 5630 | /* check if all cpu sizes are same */ |
| 5631 | for_each_tracing_cpu(cpu) { |
| 5632 | /* fill in the size from first enabled cpu */ |
| 5633 | if (size == 0) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5634 | size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries; |
| 5635 | if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) { |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5636 | buf_size_same = 0; |
| 5637 | break; |
| 5638 | } |
| 5639 | } |
| 5640 | |
| 5641 | if (buf_size_same) { |
| 5642 | if (!ring_buffer_expanded) |
| 5643 | r = sprintf(buf, "%lu (expanded: %lu)\n", |
| 5644 | size >> 10, |
| 5645 | trace_buf_size >> 10); |
| 5646 | else |
| 5647 | r = sprintf(buf, "%lu\n", size >> 10); |
| 5648 | } else |
| 5649 | r = sprintf(buf, "X\n"); |
| 5650 | } else |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 5651 | r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5652 | |
Steven Rostedt | db526ca | 2009-03-12 13:53:25 -0400 | [diff] [blame] | 5653 | mutex_unlock(&trace_types_lock); |
| 5654 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5655 | ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 5656 | return ret; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 5657 | } |
| 5658 | |
| 5659 | static ssize_t |
| 5660 | tracing_entries_write(struct file *filp, const char __user *ubuf, |
| 5661 | size_t cnt, loff_t *ppos) |
| 5662 | { |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 5663 | struct inode *inode = file_inode(filp); |
| 5664 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 5665 | unsigned long val; |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5666 | int ret; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 5667 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 5668 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 5669 | if (ret) |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 5670 | return ret; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 5671 | |
| 5672 | /* must have at least 1 entry */ |
| 5673 | if (!val) |
| 5674 | return -EINVAL; |
| 5675 | |
Steven Rostedt | 1696b2b | 2008-11-13 00:09:35 -0500 | [diff] [blame] | 5676 | /* value is in KB */ |
| 5677 | val <<= 10; |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 5678 | ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode)); |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5679 | if (ret < 0) |
| 5680 | return ret; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 5681 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 5682 | *ppos += cnt; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 5683 | |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5684 | return cnt; |
| 5685 | } |
Steven Rostedt | bf5e651 | 2008-11-10 21:46:00 -0500 | [diff] [blame] | 5686 | |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5687 | static ssize_t |
Vaibhav Nagarnaik | f81ab07 | 2011-08-16 14:46:15 -0700 | [diff] [blame] | 5688 | tracing_total_entries_read(struct file *filp, char __user *ubuf, |
| 5689 | size_t cnt, loff_t *ppos) |
| 5690 | { |
| 5691 | struct trace_array *tr = filp->private_data; |
| 5692 | char buf[64]; |
| 5693 | int r, cpu; |
| 5694 | unsigned long size = 0, expanded_size = 0; |
| 5695 | |
| 5696 | mutex_lock(&trace_types_lock); |
| 5697 | for_each_tracing_cpu(cpu) { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5698 | size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10; |
Vaibhav Nagarnaik | f81ab07 | 2011-08-16 14:46:15 -0700 | [diff] [blame] | 5699 | if (!ring_buffer_expanded) |
| 5700 | expanded_size += trace_buf_size >> 10; |
| 5701 | } |
| 5702 | if (ring_buffer_expanded) |
| 5703 | r = sprintf(buf, "%lu\n", size); |
| 5704 | else |
| 5705 | r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size); |
| 5706 | mutex_unlock(&trace_types_lock); |
| 5707 | |
| 5708 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 5709 | } |
| 5710 | |
| 5711 | static ssize_t |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5712 | tracing_free_buffer_write(struct file *filp, const char __user *ubuf, |
| 5713 | size_t cnt, loff_t *ppos) |
| 5714 | { |
| 5715 | /* |
| 5716 | * There is no need to read what the user has written, this function |
| 5717 | * is just to make sure that there is no error when "echo" is used |
| 5718 | */ |
| 5719 | |
| 5720 | *ppos += cnt; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 5721 | |
| 5722 | return cnt; |
| 5723 | } |
| 5724 | |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5725 | static int |
| 5726 | tracing_free_buffer_release(struct inode *inode, struct file *filp) |
| 5727 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5728 | struct trace_array *tr = inode->i_private; |
| 5729 | |
Steven Rostedt | cf30cf6 | 2011-06-14 22:44:07 -0400 | [diff] [blame] | 5730 | /* disable tracing ? */ |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 5731 | if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE) |
Alexander Z Lam | 711e124 | 2013-08-02 18:36:15 -0700 | [diff] [blame] | 5732 | tracer_tracing_off(tr); |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5733 | /* resize the ring buffer to 0 */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5734 | tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS); |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5735 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 5736 | trace_array_put(tr); |
| 5737 | |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5738 | return 0; |
| 5739 | } |
| 5740 | |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 5741 | static inline int lock_user_pages(const char __user *ubuf, size_t cnt, |
| 5742 | struct page **pages, void **map_page, |
| 5743 | int *offset) |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 5744 | { |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5745 | unsigned long addr = (unsigned long)ubuf; |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5746 | int nr_pages = 1; |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5747 | int ret; |
Steven Rostedt | 6edb2a8 | 2012-05-11 23:28:49 -0400 | [diff] [blame] | 5748 | int i; |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 5749 | |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5750 | /* |
| 5751 | * Userspace is injecting traces into the kernel trace buffer. |
| 5752 | * We want to be as non intrusive as possible. |
| 5753 | * To do so, we do not want to allocate any special buffers |
| 5754 | * or take any locks, but instead write the userspace data |
| 5755 | * straight into the ring buffer. |
| 5756 | * |
| 5757 | * First we need to pin the userspace buffer into memory, |
| 5758 | * which, most likely it is, because it just referenced it. |
| 5759 | * But there's no guarantee that it is. By using get_user_pages_fast() |
| 5760 | * and kmap_atomic/kunmap_atomic() we can get access to the |
| 5761 | * pages directly. We then write the data directly into the |
| 5762 | * ring buffer. |
| 5763 | */ |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 5764 | |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5765 | /* check if we cross pages */ |
| 5766 | if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK)) |
| 5767 | nr_pages = 2; |
| 5768 | |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 5769 | *offset = addr & (PAGE_SIZE - 1); |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5770 | addr &= PAGE_MASK; |
| 5771 | |
| 5772 | ret = get_user_pages_fast(addr, nr_pages, 0, pages); |
| 5773 | if (ret < nr_pages) { |
| 5774 | while (--ret >= 0) |
| 5775 | put_page(pages[ret]); |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 5776 | return -EFAULT; |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 5777 | } |
| 5778 | |
Steven Rostedt | 6edb2a8 | 2012-05-11 23:28:49 -0400 | [diff] [blame] | 5779 | for (i = 0; i < nr_pages; i++) |
| 5780 | map_page[i] = kmap_atomic(pages[i]); |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5781 | |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 5782 | return nr_pages; |
| 5783 | } |
| 5784 | |
| 5785 | static inline void unlock_user_pages(struct page **pages, |
| 5786 | void **map_page, int nr_pages) |
| 5787 | { |
| 5788 | int i; |
| 5789 | |
| 5790 | for (i = nr_pages - 1; i >= 0; i--) { |
| 5791 | kunmap_atomic(map_page[i]); |
| 5792 | put_page(pages[i]); |
| 5793 | } |
| 5794 | } |
| 5795 | |
| 5796 | static ssize_t |
| 5797 | tracing_mark_write(struct file *filp, const char __user *ubuf, |
| 5798 | size_t cnt, loff_t *fpos) |
| 5799 | { |
| 5800 | struct trace_array *tr = filp->private_data; |
| 5801 | struct ring_buffer_event *event; |
| 5802 | struct ring_buffer *buffer; |
| 5803 | struct print_entry *entry; |
| 5804 | unsigned long irq_flags; |
| 5805 | struct page *pages[2]; |
| 5806 | void *map_page[2]; |
| 5807 | int nr_pages = 1; |
| 5808 | ssize_t written; |
| 5809 | int offset; |
| 5810 | int size; |
| 5811 | int len; |
| 5812 | |
| 5813 | if (tracing_disabled) |
| 5814 | return -EINVAL; |
| 5815 | |
| 5816 | if (!(tr->trace_flags & TRACE_ITER_MARKERS)) |
| 5817 | return -EINVAL; |
| 5818 | |
| 5819 | if (cnt > TRACE_BUF_SIZE) |
| 5820 | cnt = TRACE_BUF_SIZE; |
| 5821 | |
| 5822 | BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE); |
| 5823 | |
| 5824 | nr_pages = lock_user_pages(ubuf, cnt, pages, map_page, &offset); |
| 5825 | if (nr_pages < 0) |
| 5826 | return nr_pages; |
| 5827 | |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5828 | local_save_flags(irq_flags); |
| 5829 | size = sizeof(*entry) + cnt + 2; /* possible \n added */ |
Alexander Z Lam | 2d71619 | 2013-07-01 15:31:24 -0700 | [diff] [blame] | 5830 | buffer = tr->trace_buffer.buffer; |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 5831 | event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, |
| 5832 | irq_flags, preempt_count()); |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5833 | if (!event) { |
| 5834 | /* Ring buffer disabled, return as if not open for write */ |
| 5835 | written = -EBADF; |
| 5836 | goto out_unlock; |
| 5837 | } |
| 5838 | |
| 5839 | entry = ring_buffer_event_data(event); |
| 5840 | entry->ip = _THIS_IP_; |
| 5841 | |
| 5842 | if (nr_pages == 2) { |
| 5843 | len = PAGE_SIZE - offset; |
Steven Rostedt | 6edb2a8 | 2012-05-11 23:28:49 -0400 | [diff] [blame] | 5844 | memcpy(&entry->buf, map_page[0] + offset, len); |
| 5845 | memcpy(&entry->buf[len], map_page[1], cnt - len); |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5846 | } else |
Steven Rostedt | 6edb2a8 | 2012-05-11 23:28:49 -0400 | [diff] [blame] | 5847 | memcpy(&entry->buf, map_page[0] + offset, cnt); |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5848 | |
| 5849 | if (entry->buf[cnt - 1] != '\n') { |
| 5850 | entry->buf[cnt] = '\n'; |
| 5851 | entry->buf[cnt + 1] = '\0'; |
| 5852 | } else |
| 5853 | entry->buf[cnt] = '\0'; |
| 5854 | |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 5855 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5856 | |
| 5857 | written = cnt; |
| 5858 | |
Marcin Slusarz | 1aa54bc | 2010-07-28 01:18:01 +0200 | [diff] [blame] | 5859 | *fpos += written; |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 5860 | |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 5861 | out_unlock: |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 5862 | unlock_user_pages(pages, map_page, nr_pages); |
| 5863 | |
| 5864 | return written; |
| 5865 | } |
| 5866 | |
| 5867 | /* Limit it for now to 3K (including tag) */ |
| 5868 | #define RAW_DATA_MAX_SIZE (1024*3) |
| 5869 | |
| 5870 | static ssize_t |
| 5871 | tracing_mark_raw_write(struct file *filp, const char __user *ubuf, |
| 5872 | size_t cnt, loff_t *fpos) |
| 5873 | { |
| 5874 | struct trace_array *tr = filp->private_data; |
| 5875 | struct ring_buffer_event *event; |
| 5876 | struct ring_buffer *buffer; |
| 5877 | struct raw_data_entry *entry; |
| 5878 | unsigned long irq_flags; |
| 5879 | struct page *pages[2]; |
| 5880 | void *map_page[2]; |
| 5881 | int nr_pages = 1; |
| 5882 | ssize_t written; |
| 5883 | int offset; |
| 5884 | int size; |
| 5885 | int len; |
| 5886 | |
| 5887 | if (tracing_disabled) |
| 5888 | return -EINVAL; |
| 5889 | |
| 5890 | if (!(tr->trace_flags & TRACE_ITER_MARKERS)) |
| 5891 | return -EINVAL; |
| 5892 | |
| 5893 | /* The marker must at least have a tag id */ |
| 5894 | if (cnt < sizeof(unsigned int) || cnt > RAW_DATA_MAX_SIZE) |
| 5895 | return -EINVAL; |
| 5896 | |
| 5897 | if (cnt > TRACE_BUF_SIZE) |
| 5898 | cnt = TRACE_BUF_SIZE; |
| 5899 | |
| 5900 | BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE); |
| 5901 | |
| 5902 | nr_pages = lock_user_pages(ubuf, cnt, pages, map_page, &offset); |
| 5903 | if (nr_pages < 0) |
| 5904 | return nr_pages; |
| 5905 | |
| 5906 | local_save_flags(irq_flags); |
| 5907 | size = sizeof(*entry) + cnt; |
| 5908 | buffer = tr->trace_buffer.buffer; |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 5909 | event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size, |
| 5910 | irq_flags, preempt_count()); |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 5911 | if (!event) { |
| 5912 | /* Ring buffer disabled, return as if not open for write */ |
| 5913 | written = -EBADF; |
| 5914 | goto out_unlock; |
Steven Rostedt | 6edb2a8 | 2012-05-11 23:28:49 -0400 | [diff] [blame] | 5915 | } |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 5916 | |
| 5917 | entry = ring_buffer_event_data(event); |
| 5918 | |
| 5919 | if (nr_pages == 2) { |
| 5920 | len = PAGE_SIZE - offset; |
| 5921 | memcpy(&entry->id, map_page[0] + offset, len); |
| 5922 | memcpy(((char *)&entry->id) + len, map_page[1], cnt - len); |
| 5923 | } else |
| 5924 | memcpy(&entry->id, map_page[0] + offset, cnt); |
| 5925 | |
| 5926 | __buffer_unlock_commit(buffer, event); |
| 5927 | |
| 5928 | written = cnt; |
| 5929 | |
| 5930 | *fpos += written; |
| 5931 | |
| 5932 | out_unlock: |
| 5933 | unlock_user_pages(pages, map_page, nr_pages); |
| 5934 | |
Marcin Slusarz | 1aa54bc | 2010-07-28 01:18:01 +0200 | [diff] [blame] | 5935 | return written; |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 5936 | } |
| 5937 | |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 5938 | static int tracing_clock_show(struct seq_file *m, void *v) |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 5939 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5940 | struct trace_array *tr = m->private; |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 5941 | int i; |
| 5942 | |
| 5943 | for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 5944 | seq_printf(m, |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 5945 | "%s%s%s%s", i ? " " : "", |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5946 | i == tr->clock_id ? "[" : "", trace_clocks[i].name, |
| 5947 | i == tr->clock_id ? "]" : ""); |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 5948 | seq_putc(m, '\n'); |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 5949 | |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 5950 | return 0; |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 5951 | } |
| 5952 | |
Steven Rostedt | e1e232c | 2014-02-10 23:38:46 -0500 | [diff] [blame] | 5953 | static int tracing_set_clock(struct trace_array *tr, const char *clockstr) |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 5954 | { |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 5955 | int i; |
| 5956 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 5957 | for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) { |
| 5958 | if (strcmp(trace_clocks[i].name, clockstr) == 0) |
| 5959 | break; |
| 5960 | } |
| 5961 | if (i == ARRAY_SIZE(trace_clocks)) |
| 5962 | return -EINVAL; |
| 5963 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 5964 | mutex_lock(&trace_types_lock); |
| 5965 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5966 | tr->clock_id = i; |
| 5967 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5968 | ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func); |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 5969 | |
David Sharp | 60303ed | 2012-10-11 16:27:52 -0700 | [diff] [blame] | 5970 | /* |
| 5971 | * New clock may not be consistent with the previous clock. |
| 5972 | * Reset the buffer so that it doesn't have incomparable timestamps. |
| 5973 | */ |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 5974 | tracing_reset_online_cpus(&tr->trace_buffer); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5975 | |
| 5976 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 5977 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer) |
| 5978 | ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func); |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 5979 | tracing_reset_online_cpus(&tr->max_buffer); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5980 | #endif |
David Sharp | 60303ed | 2012-10-11 16:27:52 -0700 | [diff] [blame] | 5981 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 5982 | mutex_unlock(&trace_types_lock); |
| 5983 | |
Steven Rostedt | e1e232c | 2014-02-10 23:38:46 -0500 | [diff] [blame] | 5984 | return 0; |
| 5985 | } |
| 5986 | |
| 5987 | static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf, |
| 5988 | size_t cnt, loff_t *fpos) |
| 5989 | { |
| 5990 | struct seq_file *m = filp->private_data; |
| 5991 | struct trace_array *tr = m->private; |
| 5992 | char buf[64]; |
| 5993 | const char *clockstr; |
| 5994 | int ret; |
| 5995 | |
| 5996 | if (cnt >= sizeof(buf)) |
| 5997 | return -EINVAL; |
| 5998 | |
Wang Xiaoqiang | 4afe649 | 2016-04-18 15:23:29 +0800 | [diff] [blame] | 5999 | if (copy_from_user(buf, ubuf, cnt)) |
Steven Rostedt | e1e232c | 2014-02-10 23:38:46 -0500 | [diff] [blame] | 6000 | return -EFAULT; |
| 6001 | |
| 6002 | buf[cnt] = 0; |
| 6003 | |
| 6004 | clockstr = strstrip(buf); |
| 6005 | |
| 6006 | ret = tracing_set_clock(tr, clockstr); |
| 6007 | if (ret) |
| 6008 | return ret; |
| 6009 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6010 | *fpos += cnt; |
| 6011 | |
| 6012 | return cnt; |
| 6013 | } |
| 6014 | |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 6015 | static int tracing_clock_open(struct inode *inode, struct file *file) |
| 6016 | { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6017 | struct trace_array *tr = inode->i_private; |
| 6018 | int ret; |
| 6019 | |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 6020 | if (tracing_disabled) |
| 6021 | return -ENODEV; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6022 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6023 | if (trace_array_get(tr)) |
| 6024 | return -ENODEV; |
| 6025 | |
| 6026 | ret = single_open(file, tracing_clock_show, inode->i_private); |
| 6027 | if (ret < 0) |
| 6028 | trace_array_put(tr); |
| 6029 | |
| 6030 | return ret; |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 6031 | } |
| 6032 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6033 | struct ftrace_buffer_info { |
| 6034 | struct trace_iterator iter; |
| 6035 | void *spare; |
| 6036 | unsigned int read; |
| 6037 | }; |
| 6038 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6039 | #ifdef CONFIG_TRACER_SNAPSHOT |
| 6040 | static int tracing_snapshot_open(struct inode *inode, struct file *file) |
| 6041 | { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 6042 | struct trace_array *tr = inode->i_private; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6043 | struct trace_iterator *iter; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6044 | struct seq_file *m; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6045 | int ret = 0; |
| 6046 | |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 6047 | if (trace_array_get(tr) < 0) |
| 6048 | return -ENODEV; |
| 6049 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6050 | if (file->f_mode & FMODE_READ) { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 6051 | iter = __tracing_open(inode, file, true); |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6052 | if (IS_ERR(iter)) |
| 6053 | ret = PTR_ERR(iter); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6054 | } else { |
| 6055 | /* Writes still need the seq_file to hold the private data */ |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 6056 | ret = -ENOMEM; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6057 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
| 6058 | if (!m) |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 6059 | goto out; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6060 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 6061 | if (!iter) { |
| 6062 | kfree(m); |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 6063 | goto out; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6064 | } |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 6065 | ret = 0; |
| 6066 | |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 6067 | iter->tr = tr; |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 6068 | iter->trace_buffer = &tr->max_buffer; |
| 6069 | iter->cpu_file = tracing_get_cpu(inode); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6070 | m->private = iter; |
| 6071 | file->private_data = m; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6072 | } |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 6073 | out: |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 6074 | if (ret < 0) |
| 6075 | trace_array_put(tr); |
| 6076 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6077 | return ret; |
| 6078 | } |
| 6079 | |
| 6080 | static ssize_t |
| 6081 | tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 6082 | loff_t *ppos) |
| 6083 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6084 | struct seq_file *m = filp->private_data; |
| 6085 | struct trace_iterator *iter = m->private; |
| 6086 | struct trace_array *tr = iter->tr; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6087 | unsigned long val; |
| 6088 | int ret; |
| 6089 | |
| 6090 | ret = tracing_update_buffers(); |
| 6091 | if (ret < 0) |
| 6092 | return ret; |
| 6093 | |
| 6094 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 6095 | if (ret) |
| 6096 | return ret; |
| 6097 | |
| 6098 | mutex_lock(&trace_types_lock); |
| 6099 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6100 | if (tr->current_trace->use_max_tr) { |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6101 | ret = -EBUSY; |
| 6102 | goto out; |
| 6103 | } |
| 6104 | |
| 6105 | switch (val) { |
| 6106 | case 0: |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6107 | if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { |
| 6108 | ret = -EINVAL; |
| 6109 | break; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6110 | } |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 6111 | if (tr->allocated_snapshot) |
| 6112 | free_snapshot(tr); |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6113 | break; |
| 6114 | case 1: |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6115 | /* Only allow per-cpu swap if the ring buffer supports it */ |
| 6116 | #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP |
| 6117 | if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { |
| 6118 | ret = -EINVAL; |
| 6119 | break; |
| 6120 | } |
| 6121 | #endif |
Steven Rostedt (Red Hat) | 45ad21c | 2013-03-05 18:25:02 -0500 | [diff] [blame] | 6122 | if (!tr->allocated_snapshot) { |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 6123 | ret = alloc_snapshot(tr); |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6124 | if (ret < 0) |
| 6125 | break; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6126 | } |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6127 | local_irq_disable(); |
| 6128 | /* Now, we're going to swap */ |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6129 | if (iter->cpu_file == RING_BUFFER_ALL_CPUS) |
Steven Rostedt (Red Hat) | ce9bae5 | 2013-03-05 21:23:55 -0500 | [diff] [blame] | 6130 | update_max_tr(tr, current, smp_processor_id()); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6131 | else |
Steven Rostedt (Red Hat) | ce9bae5 | 2013-03-05 21:23:55 -0500 | [diff] [blame] | 6132 | update_max_tr_single(tr, current, iter->cpu_file); |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6133 | local_irq_enable(); |
| 6134 | break; |
| 6135 | default: |
Steven Rostedt (Red Hat) | 45ad21c | 2013-03-05 18:25:02 -0500 | [diff] [blame] | 6136 | if (tr->allocated_snapshot) { |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6137 | if (iter->cpu_file == RING_BUFFER_ALL_CPUS) |
| 6138 | tracing_reset_online_cpus(&tr->max_buffer); |
| 6139 | else |
| 6140 | tracing_reset(&tr->max_buffer, iter->cpu_file); |
| 6141 | } |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6142 | break; |
| 6143 | } |
| 6144 | |
| 6145 | if (ret >= 0) { |
| 6146 | *ppos += cnt; |
| 6147 | ret = cnt; |
| 6148 | } |
| 6149 | out: |
| 6150 | mutex_unlock(&trace_types_lock); |
| 6151 | return ret; |
| 6152 | } |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6153 | |
| 6154 | static int tracing_snapshot_release(struct inode *inode, struct file *file) |
| 6155 | { |
| 6156 | struct seq_file *m = file->private_data; |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 6157 | int ret; |
| 6158 | |
| 6159 | ret = tracing_release(inode, file); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6160 | |
| 6161 | if (file->f_mode & FMODE_READ) |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 6162 | return ret; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6163 | |
| 6164 | /* If write only, the seq_file is just a stub */ |
| 6165 | if (m) |
| 6166 | kfree(m->private); |
| 6167 | kfree(m); |
| 6168 | |
| 6169 | return 0; |
| 6170 | } |
| 6171 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6172 | static int tracing_buffers_open(struct inode *inode, struct file *filp); |
| 6173 | static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf, |
| 6174 | size_t count, loff_t *ppos); |
| 6175 | static int tracing_buffers_release(struct inode *inode, struct file *file); |
| 6176 | static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, |
| 6177 | struct pipe_inode_info *pipe, size_t len, unsigned int flags); |
| 6178 | |
| 6179 | static int snapshot_raw_open(struct inode *inode, struct file *filp) |
| 6180 | { |
| 6181 | struct ftrace_buffer_info *info; |
| 6182 | int ret; |
| 6183 | |
| 6184 | ret = tracing_buffers_open(inode, filp); |
| 6185 | if (ret < 0) |
| 6186 | return ret; |
| 6187 | |
| 6188 | info = filp->private_data; |
| 6189 | |
| 6190 | if (info->iter.trace->use_max_tr) { |
| 6191 | tracing_buffers_release(inode, filp); |
| 6192 | return -EBUSY; |
| 6193 | } |
| 6194 | |
| 6195 | info->iter.snapshot = true; |
| 6196 | info->iter.trace_buffer = &info->iter.tr->max_buffer; |
| 6197 | |
| 6198 | return ret; |
| 6199 | } |
| 6200 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6201 | #endif /* CONFIG_TRACER_SNAPSHOT */ |
| 6202 | |
| 6203 | |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 6204 | static const struct file_operations tracing_thresh_fops = { |
| 6205 | .open = tracing_open_generic, |
| 6206 | .read = tracing_thresh_read, |
| 6207 | .write = tracing_thresh_write, |
| 6208 | .llseek = generic_file_llseek, |
| 6209 | }; |
| 6210 | |
Steven Rostedt (Red Hat) | f971cc9 | 2016-09-07 12:45:09 -0400 | [diff] [blame] | 6211 | #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER) |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 6212 | static const struct file_operations tracing_max_lat_fops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6213 | .open = tracing_open_generic, |
| 6214 | .read = tracing_max_lat_read, |
| 6215 | .write = tracing_max_lat_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 6216 | .llseek = generic_file_llseek, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6217 | }; |
Chen Gang | e428abb | 2015-11-10 05:15:15 +0800 | [diff] [blame] | 6218 | #endif |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6219 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 6220 | static const struct file_operations set_tracer_fops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6221 | .open = tracing_open_generic, |
| 6222 | .read = tracing_set_trace_read, |
| 6223 | .write = tracing_set_trace_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 6224 | .llseek = generic_file_llseek, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6225 | }; |
| 6226 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 6227 | static const struct file_operations tracing_pipe_fops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6228 | .open = tracing_open_pipe, |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 6229 | .poll = tracing_poll_pipe, |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6230 | .read = tracing_read_pipe, |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6231 | .splice_read = tracing_splice_read_pipe, |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6232 | .release = tracing_release_pipe, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 6233 | .llseek = no_llseek, |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6234 | }; |
| 6235 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 6236 | static const struct file_operations tracing_entries_fops = { |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 6237 | .open = tracing_open_generic_tr, |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 6238 | .read = tracing_entries_read, |
| 6239 | .write = tracing_entries_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 6240 | .llseek = generic_file_llseek, |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 6241 | .release = tracing_release_generic_tr, |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 6242 | }; |
| 6243 | |
Vaibhav Nagarnaik | f81ab07 | 2011-08-16 14:46:15 -0700 | [diff] [blame] | 6244 | static const struct file_operations tracing_total_entries_fops = { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6245 | .open = tracing_open_generic_tr, |
Vaibhav Nagarnaik | f81ab07 | 2011-08-16 14:46:15 -0700 | [diff] [blame] | 6246 | .read = tracing_total_entries_read, |
| 6247 | .llseek = generic_file_llseek, |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6248 | .release = tracing_release_generic_tr, |
Vaibhav Nagarnaik | f81ab07 | 2011-08-16 14:46:15 -0700 | [diff] [blame] | 6249 | }; |
| 6250 | |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 6251 | static const struct file_operations tracing_free_buffer_fops = { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6252 | .open = tracing_open_generic_tr, |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 6253 | .write = tracing_free_buffer_write, |
| 6254 | .release = tracing_free_buffer_release, |
| 6255 | }; |
| 6256 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 6257 | static const struct file_operations tracing_mark_fops = { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6258 | .open = tracing_open_generic_tr, |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 6259 | .write = tracing_mark_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 6260 | .llseek = generic_file_llseek, |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6261 | .release = tracing_release_generic_tr, |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 6262 | }; |
| 6263 | |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6264 | static const struct file_operations tracing_mark_raw_fops = { |
| 6265 | .open = tracing_open_generic_tr, |
| 6266 | .write = tracing_mark_raw_write, |
| 6267 | .llseek = generic_file_llseek, |
| 6268 | .release = tracing_release_generic_tr, |
| 6269 | }; |
| 6270 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6271 | static const struct file_operations trace_clock_fops = { |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 6272 | .open = tracing_clock_open, |
| 6273 | .read = seq_read, |
| 6274 | .llseek = seq_lseek, |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6275 | .release = tracing_single_release_tr, |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6276 | .write = tracing_clock_write, |
| 6277 | }; |
| 6278 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6279 | #ifdef CONFIG_TRACER_SNAPSHOT |
| 6280 | static const struct file_operations snapshot_fops = { |
| 6281 | .open = tracing_snapshot_open, |
| 6282 | .read = seq_read, |
| 6283 | .write = tracing_snapshot_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 6284 | .llseek = tracing_lseek, |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6285 | .release = tracing_snapshot_release, |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6286 | }; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6287 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6288 | static const struct file_operations snapshot_raw_fops = { |
| 6289 | .open = snapshot_raw_open, |
| 6290 | .read = tracing_buffers_read, |
| 6291 | .release = tracing_buffers_release, |
| 6292 | .splice_read = tracing_buffers_splice_read, |
| 6293 | .llseek = no_llseek, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6294 | }; |
| 6295 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6296 | #endif /* CONFIG_TRACER_SNAPSHOT */ |
| 6297 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6298 | static int tracing_buffers_open(struct inode *inode, struct file *filp) |
| 6299 | { |
Oleg Nesterov | 46ef2be | 2013-07-23 17:26:00 +0200 | [diff] [blame] | 6300 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6301 | struct ftrace_buffer_info *info; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6302 | int ret; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6303 | |
| 6304 | if (tracing_disabled) |
| 6305 | return -ENODEV; |
| 6306 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6307 | if (trace_array_get(tr) < 0) |
| 6308 | return -ENODEV; |
| 6309 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6310 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6311 | if (!info) { |
| 6312 | trace_array_put(tr); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6313 | return -ENOMEM; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6314 | } |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6315 | |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 6316 | mutex_lock(&trace_types_lock); |
| 6317 | |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6318 | info->iter.tr = tr; |
Oleg Nesterov | 46ef2be | 2013-07-23 17:26:00 +0200 | [diff] [blame] | 6319 | info->iter.cpu_file = tracing_get_cpu(inode); |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 6320 | info->iter.trace = tr->current_trace; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6321 | info->iter.trace_buffer = &tr->trace_buffer; |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6322 | info->spare = NULL; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6323 | /* Force reading ring buffer for first read */ |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6324 | info->read = (unsigned int)-1; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6325 | |
| 6326 | filp->private_data = info; |
| 6327 | |
Steven Rostedt (Red Hat) | cf6ab6d | 2014-12-15 20:13:31 -0500 | [diff] [blame] | 6328 | tr->current_trace->ref++; |
| 6329 | |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 6330 | mutex_unlock(&trace_types_lock); |
| 6331 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6332 | ret = nonseekable_open(inode, filp); |
| 6333 | if (ret < 0) |
| 6334 | trace_array_put(tr); |
| 6335 | |
| 6336 | return ret; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6337 | } |
| 6338 | |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6339 | static unsigned int |
| 6340 | tracing_buffers_poll(struct file *filp, poll_table *poll_table) |
| 6341 | { |
| 6342 | struct ftrace_buffer_info *info = filp->private_data; |
| 6343 | struct trace_iterator *iter = &info->iter; |
| 6344 | |
| 6345 | return trace_poll(iter, filp, poll_table); |
| 6346 | } |
| 6347 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6348 | static ssize_t |
| 6349 | tracing_buffers_read(struct file *filp, char __user *ubuf, |
| 6350 | size_t count, loff_t *ppos) |
| 6351 | { |
| 6352 | struct ftrace_buffer_info *info = filp->private_data; |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6353 | struct trace_iterator *iter = &info->iter; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6354 | ssize_t ret; |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6355 | ssize_t size; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6356 | |
Steven Rostedt | 2dc5d12 | 2009-03-04 19:10:05 -0500 | [diff] [blame] | 6357 | if (!count) |
| 6358 | return 0; |
| 6359 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6360 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6361 | if (iter->snapshot && iter->tr->current_trace->use_max_tr) |
| 6362 | return -EBUSY; |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6363 | #endif |
| 6364 | |
Lai Jiangshan | ddd538f | 2009-04-02 15:16:59 +0800 | [diff] [blame] | 6365 | if (!info->spare) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6366 | info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer, |
| 6367 | iter->cpu_file); |
Lai Jiangshan | ddd538f | 2009-04-02 15:16:59 +0800 | [diff] [blame] | 6368 | if (!info->spare) |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6369 | return -ENOMEM; |
Lai Jiangshan | ddd538f | 2009-04-02 15:16:59 +0800 | [diff] [blame] | 6370 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6371 | /* Do we have previous read data to read? */ |
| 6372 | if (info->read < PAGE_SIZE) |
| 6373 | goto read; |
| 6374 | |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 6375 | again: |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6376 | trace_access_lock(iter->cpu_file); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6377 | ret = ring_buffer_read_page(iter->trace_buffer->buffer, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6378 | &info->spare, |
| 6379 | count, |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6380 | iter->cpu_file, 0); |
| 6381 | trace_access_unlock(iter->cpu_file); |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 6382 | |
| 6383 | if (ret < 0) { |
| 6384 | if (trace_empty(iter)) { |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6385 | if ((filp->f_flags & O_NONBLOCK)) |
| 6386 | return -EAGAIN; |
| 6387 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 6388 | ret = wait_on_pipe(iter, false); |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6389 | if (ret) |
| 6390 | return ret; |
| 6391 | |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 6392 | goto again; |
| 6393 | } |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6394 | return 0; |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 6395 | } |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6396 | |
Steven Rostedt | 436fc28 | 2011-10-14 10:44:25 -0400 | [diff] [blame] | 6397 | info->read = 0; |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 6398 | read: |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6399 | size = PAGE_SIZE - info->read; |
| 6400 | if (size > count) |
| 6401 | size = count; |
| 6402 | |
| 6403 | ret = copy_to_user(ubuf, info->spare + info->read, size); |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6404 | if (ret == size) |
| 6405 | return -EFAULT; |
| 6406 | |
Steven Rostedt | 2dc5d12 | 2009-03-04 19:10:05 -0500 | [diff] [blame] | 6407 | size -= ret; |
| 6408 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6409 | *ppos += size; |
| 6410 | info->read += size; |
| 6411 | |
| 6412 | return size; |
| 6413 | } |
| 6414 | |
| 6415 | static int tracing_buffers_release(struct inode *inode, struct file *file) |
| 6416 | { |
| 6417 | struct ftrace_buffer_info *info = file->private_data; |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6418 | struct trace_iterator *iter = &info->iter; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6419 | |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 6420 | mutex_lock(&trace_types_lock); |
| 6421 | |
Steven Rostedt (Red Hat) | cf6ab6d | 2014-12-15 20:13:31 -0500 | [diff] [blame] | 6422 | iter->tr->current_trace->ref--; |
| 6423 | |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 6424 | __trace_array_put(iter->tr); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6425 | |
Lai Jiangshan | ddd538f | 2009-04-02 15:16:59 +0800 | [diff] [blame] | 6426 | if (info->spare) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6427 | ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6428 | kfree(info); |
| 6429 | |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 6430 | mutex_unlock(&trace_types_lock); |
| 6431 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6432 | return 0; |
| 6433 | } |
| 6434 | |
| 6435 | struct buffer_ref { |
| 6436 | struct ring_buffer *buffer; |
| 6437 | void *page; |
| 6438 | int ref; |
| 6439 | }; |
| 6440 | |
| 6441 | static void buffer_pipe_buf_release(struct pipe_inode_info *pipe, |
| 6442 | struct pipe_buffer *buf) |
| 6443 | { |
| 6444 | struct buffer_ref *ref = (struct buffer_ref *)buf->private; |
| 6445 | |
| 6446 | if (--ref->ref) |
| 6447 | return; |
| 6448 | |
| 6449 | ring_buffer_free_read_page(ref->buffer, ref->page); |
| 6450 | kfree(ref); |
| 6451 | buf->private = 0; |
| 6452 | } |
| 6453 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6454 | static void buffer_pipe_buf_get(struct pipe_inode_info *pipe, |
| 6455 | struct pipe_buffer *buf) |
| 6456 | { |
| 6457 | struct buffer_ref *ref = (struct buffer_ref *)buf->private; |
| 6458 | |
| 6459 | ref->ref++; |
| 6460 | } |
| 6461 | |
| 6462 | /* Pipe buffer operations for a buffer. */ |
Alexey Dobriyan | 28dfef8 | 2009-12-15 16:46:48 -0800 | [diff] [blame] | 6463 | static const struct pipe_buf_operations buffer_pipe_buf_ops = { |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6464 | .can_merge = 0, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6465 | .confirm = generic_pipe_buf_confirm, |
| 6466 | .release = buffer_pipe_buf_release, |
Masami Hiramatsu | d55cb6c | 2012-08-09 21:31:10 +0900 | [diff] [blame] | 6467 | .steal = generic_pipe_buf_steal, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6468 | .get = buffer_pipe_buf_get, |
| 6469 | }; |
| 6470 | |
| 6471 | /* |
| 6472 | * Callback from splice_to_pipe(), if we need to release some pages |
| 6473 | * at the end of the spd in case we error'ed out in filling the pipe. |
| 6474 | */ |
| 6475 | static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i) |
| 6476 | { |
| 6477 | struct buffer_ref *ref = |
| 6478 | (struct buffer_ref *)spd->partial[i].private; |
| 6479 | |
| 6480 | if (--ref->ref) |
| 6481 | return; |
| 6482 | |
| 6483 | ring_buffer_free_read_page(ref->buffer, ref->page); |
| 6484 | kfree(ref); |
| 6485 | spd->partial[i].private = 0; |
| 6486 | } |
| 6487 | |
| 6488 | static ssize_t |
| 6489 | tracing_buffers_splice_read(struct file *file, loff_t *ppos, |
| 6490 | struct pipe_inode_info *pipe, size_t len, |
| 6491 | unsigned int flags) |
| 6492 | { |
| 6493 | struct ftrace_buffer_info *info = file->private_data; |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6494 | struct trace_iterator *iter = &info->iter; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6495 | struct partial_page partial_def[PIPE_DEF_BUFFERS]; |
| 6496 | struct page *pages_def[PIPE_DEF_BUFFERS]; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6497 | struct splice_pipe_desc spd = { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6498 | .pages = pages_def, |
| 6499 | .partial = partial_def, |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 6500 | .nr_pages_max = PIPE_DEF_BUFFERS, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6501 | .flags = flags, |
| 6502 | .ops = &buffer_pipe_buf_ops, |
| 6503 | .spd_release = buffer_spd_release, |
| 6504 | }; |
| 6505 | struct buffer_ref *ref; |
Steven Rostedt | 93459c6 | 2009-04-29 00:23:13 -0400 | [diff] [blame] | 6506 | int entries, size, i; |
Rabin Vincent | 07906da | 2014-11-06 22:26:07 +0100 | [diff] [blame] | 6507 | ssize_t ret = 0; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6508 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6509 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6510 | if (iter->snapshot && iter->tr->current_trace->use_max_tr) |
| 6511 | return -EBUSY; |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6512 | #endif |
| 6513 | |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6514 | if (*ppos & (PAGE_SIZE - 1)) |
| 6515 | return -EINVAL; |
Lai Jiangshan | 93cfb3c | 2009-04-02 15:17:08 +0800 | [diff] [blame] | 6516 | |
| 6517 | if (len & (PAGE_SIZE - 1)) { |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6518 | if (len < PAGE_SIZE) |
| 6519 | return -EINVAL; |
Lai Jiangshan | 93cfb3c | 2009-04-02 15:17:08 +0800 | [diff] [blame] | 6520 | len &= PAGE_MASK; |
| 6521 | } |
| 6522 | |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 6523 | if (splice_grow_spd(pipe, &spd)) |
| 6524 | return -ENOMEM; |
| 6525 | |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6526 | again: |
| 6527 | trace_access_lock(iter->cpu_file); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6528 | entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file); |
Steven Rostedt | 93459c6 | 2009-04-29 00:23:13 -0400 | [diff] [blame] | 6529 | |
Al Viro | a786c06 | 2014-04-11 12:01:03 -0400 | [diff] [blame] | 6530 | for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) { |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6531 | struct page *page; |
| 6532 | int r; |
| 6533 | |
| 6534 | ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
Rabin Vincent | 07906da | 2014-11-06 22:26:07 +0100 | [diff] [blame] | 6535 | if (!ref) { |
| 6536 | ret = -ENOMEM; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6537 | break; |
Rabin Vincent | 07906da | 2014-11-06 22:26:07 +0100 | [diff] [blame] | 6538 | } |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6539 | |
Steven Rostedt | 7267fa6 | 2009-04-29 00:16:21 -0400 | [diff] [blame] | 6540 | ref->ref = 1; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6541 | ref->buffer = iter->trace_buffer->buffer; |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6542 | ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6543 | if (!ref->page) { |
Rabin Vincent | 07906da | 2014-11-06 22:26:07 +0100 | [diff] [blame] | 6544 | ret = -ENOMEM; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6545 | kfree(ref); |
| 6546 | break; |
| 6547 | } |
| 6548 | |
| 6549 | r = ring_buffer_read_page(ref->buffer, &ref->page, |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6550 | len, iter->cpu_file, 1); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6551 | if (r < 0) { |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 6552 | ring_buffer_free_read_page(ref->buffer, ref->page); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6553 | kfree(ref); |
| 6554 | break; |
| 6555 | } |
| 6556 | |
| 6557 | /* |
| 6558 | * zero out any left over data, this is going to |
| 6559 | * user land. |
| 6560 | */ |
| 6561 | size = ring_buffer_page_len(ref->page); |
| 6562 | if (size < PAGE_SIZE) |
| 6563 | memset(ref->page + size, 0, PAGE_SIZE - size); |
| 6564 | |
| 6565 | page = virt_to_page(ref->page); |
| 6566 | |
| 6567 | spd.pages[i] = page; |
| 6568 | spd.partial[i].len = PAGE_SIZE; |
| 6569 | spd.partial[i].offset = 0; |
| 6570 | spd.partial[i].private = (unsigned long)ref; |
| 6571 | spd.nr_pages++; |
Lai Jiangshan | 93cfb3c | 2009-04-02 15:17:08 +0800 | [diff] [blame] | 6572 | *ppos += PAGE_SIZE; |
Steven Rostedt | 93459c6 | 2009-04-29 00:23:13 -0400 | [diff] [blame] | 6573 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6574 | entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6575 | } |
| 6576 | |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6577 | trace_access_unlock(iter->cpu_file); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6578 | spd.nr_pages = i; |
| 6579 | |
| 6580 | /* did we read anything? */ |
| 6581 | if (!spd.nr_pages) { |
Rabin Vincent | 07906da | 2014-11-06 22:26:07 +0100 | [diff] [blame] | 6582 | if (ret) |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 6583 | goto out; |
Rabin Vincent | 07906da | 2014-11-06 22:26:07 +0100 | [diff] [blame] | 6584 | |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 6585 | ret = -EAGAIN; |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6586 | if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 6587 | goto out; |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6588 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 6589 | ret = wait_on_pipe(iter, true); |
Steven Rostedt (Red Hat) | 8b8b368 | 2014-06-10 09:46:00 -0400 | [diff] [blame] | 6590 | if (ret) |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 6591 | goto out; |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 6592 | |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6593 | goto again; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | ret = splice_to_pipe(pipe, &spd); |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 6597 | out: |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 6598 | splice_shrink_spd(&spd); |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6599 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6600 | return ret; |
| 6601 | } |
| 6602 | |
| 6603 | static const struct file_operations tracing_buffers_fops = { |
| 6604 | .open = tracing_buffers_open, |
| 6605 | .read = tracing_buffers_read, |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6606 | .poll = tracing_buffers_poll, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 6607 | .release = tracing_buffers_release, |
| 6608 | .splice_read = tracing_buffers_splice_read, |
| 6609 | .llseek = no_llseek, |
| 6610 | }; |
| 6611 | |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6612 | static ssize_t |
| 6613 | tracing_stats_read(struct file *filp, char __user *ubuf, |
| 6614 | size_t count, loff_t *ppos) |
| 6615 | { |
Oleg Nesterov | 4d3435b | 2013-07-23 17:26:03 +0200 | [diff] [blame] | 6616 | struct inode *inode = file_inode(filp); |
| 6617 | struct trace_array *tr = inode->i_private; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6618 | struct trace_buffer *trace_buf = &tr->trace_buffer; |
Oleg Nesterov | 4d3435b | 2013-07-23 17:26:03 +0200 | [diff] [blame] | 6619 | int cpu = tracing_get_cpu(inode); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6620 | struct trace_seq *s; |
| 6621 | unsigned long cnt; |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 6622 | unsigned long long t; |
| 6623 | unsigned long usec_rem; |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6624 | |
Li Zefan | e4f2d10 | 2009-06-15 10:57:28 +0800 | [diff] [blame] | 6625 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6626 | if (!s) |
Roel Kluin | a646365 | 2009-11-11 22:26:35 +0100 | [diff] [blame] | 6627 | return -ENOMEM; |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6628 | |
| 6629 | trace_seq_init(s); |
| 6630 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6631 | cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6632 | trace_seq_printf(s, "entries: %ld\n", cnt); |
| 6633 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6634 | cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6635 | trace_seq_printf(s, "overrun: %ld\n", cnt); |
| 6636 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6637 | cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6638 | trace_seq_printf(s, "commit overrun: %ld\n", cnt); |
| 6639 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6640 | cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 6641 | trace_seq_printf(s, "bytes: %ld\n", cnt); |
| 6642 | |
Yoshihiro YUNOMAE | 58e8eed | 2013-04-23 10:32:39 +0900 | [diff] [blame] | 6643 | if (trace_clocks[tr->clock_id].in_ns) { |
Yoshihiro YUNOMAE | 11043d8b | 2012-11-13 12:18:23 -0800 | [diff] [blame] | 6644 | /* local or global for trace_clock */ |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6645 | t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu)); |
Yoshihiro YUNOMAE | 11043d8b | 2012-11-13 12:18:23 -0800 | [diff] [blame] | 6646 | usec_rem = do_div(t, USEC_PER_SEC); |
| 6647 | trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", |
| 6648 | t, usec_rem); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 6649 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6650 | t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu)); |
Yoshihiro YUNOMAE | 11043d8b | 2012-11-13 12:18:23 -0800 | [diff] [blame] | 6651 | usec_rem = do_div(t, USEC_PER_SEC); |
| 6652 | trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem); |
| 6653 | } else { |
| 6654 | /* counter or tsc mode for trace_clock */ |
| 6655 | trace_seq_printf(s, "oldest event ts: %llu\n", |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6656 | ring_buffer_oldest_event_ts(trace_buf->buffer, cpu)); |
Yoshihiro YUNOMAE | 11043d8b | 2012-11-13 12:18:23 -0800 | [diff] [blame] | 6657 | |
| 6658 | trace_seq_printf(s, "now ts: %llu\n", |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6659 | ring_buffer_time_stamp(trace_buf->buffer, cpu)); |
Yoshihiro YUNOMAE | 11043d8b | 2012-11-13 12:18:23 -0800 | [diff] [blame] | 6660 | } |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 6661 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6662 | cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu); |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 6663 | trace_seq_printf(s, "dropped events: %ld\n", cnt); |
| 6664 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6665 | cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu); |
Steven Rostedt (Red Hat) | ad96470 | 2013-01-29 17:45:49 -0500 | [diff] [blame] | 6666 | trace_seq_printf(s, "read events: %ld\n", cnt); |
| 6667 | |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 6668 | count = simple_read_from_buffer(ubuf, count, ppos, |
| 6669 | s->buffer, trace_seq_used(s)); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6670 | |
| 6671 | kfree(s); |
| 6672 | |
| 6673 | return count; |
| 6674 | } |
| 6675 | |
| 6676 | static const struct file_operations tracing_stats_fops = { |
Oleg Nesterov | 4d3435b | 2013-07-23 17:26:03 +0200 | [diff] [blame] | 6677 | .open = tracing_open_generic_tr, |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6678 | .read = tracing_stats_read, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 6679 | .llseek = generic_file_llseek, |
Oleg Nesterov | 4d3435b | 2013-07-23 17:26:03 +0200 | [diff] [blame] | 6680 | .release = tracing_release_generic_tr, |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6681 | }; |
| 6682 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6683 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 6684 | |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 6685 | int __weak ftrace_arch_read_dyn_info(char *buf, int size) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6686 | { |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 6687 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6688 | } |
| 6689 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6690 | static ssize_t |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 6691 | tracing_read_dyn_info(struct file *filp, char __user *ubuf, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6692 | size_t cnt, loff_t *ppos) |
| 6693 | { |
Steven Rostedt | a26a2a2 | 2008-10-31 00:03:22 -0400 | [diff] [blame] | 6694 | static char ftrace_dyn_info_buffer[1024]; |
| 6695 | static DEFINE_MUTEX(dyn_info_mutex); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6696 | unsigned long *p = filp->private_data; |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 6697 | char *buf = ftrace_dyn_info_buffer; |
Steven Rostedt | a26a2a2 | 2008-10-31 00:03:22 -0400 | [diff] [blame] | 6698 | int size = ARRAY_SIZE(ftrace_dyn_info_buffer); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6699 | int r; |
| 6700 | |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 6701 | mutex_lock(&dyn_info_mutex); |
| 6702 | r = sprintf(buf, "%ld ", *p); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6703 | |
Steven Rostedt | a26a2a2 | 2008-10-31 00:03:22 -0400 | [diff] [blame] | 6704 | r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r); |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 6705 | buf[r++] = '\n'; |
| 6706 | |
| 6707 | r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 6708 | |
| 6709 | mutex_unlock(&dyn_info_mutex); |
| 6710 | |
| 6711 | return r; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6712 | } |
| 6713 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 6714 | static const struct file_operations tracing_dyn_info_fops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6715 | .open = tracing_open_generic, |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 6716 | .read = tracing_read_dyn_info, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 6717 | .llseek = generic_file_llseek, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6718 | }; |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 6719 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6720 | |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 6721 | #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) |
| 6722 | static void |
| 6723 | ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6724 | { |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 6725 | tracing_snapshot(); |
| 6726 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6727 | |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 6728 | static void |
| 6729 | ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data) |
| 6730 | { |
| 6731 | unsigned long *count = (long *)data; |
| 6732 | |
| 6733 | if (!*count) |
| 6734 | return; |
| 6735 | |
| 6736 | if (*count != -1) |
| 6737 | (*count)--; |
| 6738 | |
| 6739 | tracing_snapshot(); |
| 6740 | } |
| 6741 | |
| 6742 | static int |
| 6743 | ftrace_snapshot_print(struct seq_file *m, unsigned long ip, |
| 6744 | struct ftrace_probe_ops *ops, void *data) |
| 6745 | { |
| 6746 | long count = (long)data; |
| 6747 | |
| 6748 | seq_printf(m, "%ps:", (void *)ip); |
| 6749 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 6750 | seq_puts(m, "snapshot"); |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 6751 | |
| 6752 | if (count == -1) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 6753 | seq_puts(m, ":unlimited\n"); |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 6754 | else |
| 6755 | seq_printf(m, ":count=%ld\n", count); |
| 6756 | |
| 6757 | return 0; |
| 6758 | } |
| 6759 | |
| 6760 | static struct ftrace_probe_ops snapshot_probe_ops = { |
| 6761 | .func = ftrace_snapshot, |
| 6762 | .print = ftrace_snapshot_print, |
| 6763 | }; |
| 6764 | |
| 6765 | static struct ftrace_probe_ops snapshot_count_probe_ops = { |
| 6766 | .func = ftrace_count_snapshot, |
| 6767 | .print = ftrace_snapshot_print, |
| 6768 | }; |
| 6769 | |
| 6770 | static int |
| 6771 | ftrace_trace_snapshot_callback(struct ftrace_hash *hash, |
| 6772 | char *glob, char *cmd, char *param, int enable) |
| 6773 | { |
| 6774 | struct ftrace_probe_ops *ops; |
| 6775 | void *count = (void *)-1; |
| 6776 | char *number; |
| 6777 | int ret; |
| 6778 | |
| 6779 | /* hash funcs only work with set_ftrace_filter */ |
| 6780 | if (!enable) |
| 6781 | return -EINVAL; |
| 6782 | |
| 6783 | ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops; |
| 6784 | |
| 6785 | if (glob[0] == '!') { |
| 6786 | unregister_ftrace_function_probe_func(glob+1, ops); |
| 6787 | return 0; |
| 6788 | } |
| 6789 | |
| 6790 | if (!param) |
| 6791 | goto out_reg; |
| 6792 | |
| 6793 | number = strsep(¶m, ":"); |
| 6794 | |
| 6795 | if (!strlen(number)) |
| 6796 | goto out_reg; |
| 6797 | |
| 6798 | /* |
| 6799 | * We use the callback data field (which is a pointer) |
| 6800 | * as our counter. |
| 6801 | */ |
| 6802 | ret = kstrtoul(number, 0, (unsigned long *)&count); |
| 6803 | if (ret) |
| 6804 | return ret; |
| 6805 | |
| 6806 | out_reg: |
| 6807 | ret = register_ftrace_function_probe(glob, ops, count); |
| 6808 | |
| 6809 | if (ret >= 0) |
| 6810 | alloc_snapshot(&global_trace); |
| 6811 | |
| 6812 | return ret < 0 ? ret : 0; |
| 6813 | } |
| 6814 | |
| 6815 | static struct ftrace_func_command ftrace_snapshot_cmd = { |
| 6816 | .name = "snapshot", |
| 6817 | .func = ftrace_trace_snapshot_callback, |
| 6818 | }; |
| 6819 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 6820 | static __init int register_snapshot_cmd(void) |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 6821 | { |
| 6822 | return register_ftrace_command(&ftrace_snapshot_cmd); |
| 6823 | } |
| 6824 | #else |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 6825 | static inline __init int register_snapshot_cmd(void) { return 0; } |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 6826 | #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6827 | |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 6828 | static struct dentry *tracing_get_dentry(struct trace_array *tr) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6829 | { |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 6830 | if (WARN_ON(!tr->dir)) |
| 6831 | return ERR_PTR(-ENODEV); |
| 6832 | |
| 6833 | /* Top directory uses NULL as the parent */ |
| 6834 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) |
| 6835 | return NULL; |
| 6836 | |
| 6837 | /* All sub buffers have a descriptor */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6838 | return tr->dir; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6839 | } |
| 6840 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6841 | static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu) |
| 6842 | { |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6843 | struct dentry *d_tracer; |
| 6844 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6845 | if (tr->percpu_dir) |
| 6846 | return tr->percpu_dir; |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6847 | |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 6848 | d_tracer = tracing_get_dentry(tr); |
Steven Rostedt (Red Hat) | 14a5ae4 | 2015-01-20 11:14:16 -0500 | [diff] [blame] | 6849 | if (IS_ERR(d_tracer)) |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6850 | return NULL; |
| 6851 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 6852 | tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6853 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6854 | WARN_ONCE(!tr->percpu_dir, |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 6855 | "Could not create tracefs directory 'per_cpu/%d'\n", cpu); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6856 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6857 | return tr->percpu_dir; |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6858 | } |
| 6859 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 6860 | static struct dentry * |
| 6861 | trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent, |
| 6862 | void *data, long cpu, const struct file_operations *fops) |
| 6863 | { |
| 6864 | struct dentry *ret = trace_create_file(name, mode, parent, data, fops); |
| 6865 | |
| 6866 | if (ret) /* See tracing_get_cpu() */ |
David Howells | 7682c91 | 2015-03-17 22:26:16 +0000 | [diff] [blame] | 6867 | d_inode(ret)->i_cdev = (void *)(cpu + 1); |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 6868 | return ret; |
| 6869 | } |
| 6870 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6871 | static void |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 6872 | tracing_init_tracefs_percpu(struct trace_array *tr, long cpu) |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6873 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6874 | struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu); |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 6875 | struct dentry *d_cpu; |
Steven Rostedt | dd49a38 | 2010-10-20 21:51:26 -0400 | [diff] [blame] | 6876 | char cpu_dir[30]; /* 30 characters should be more than enough */ |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6877 | |
Namhyung Kim | 0a3d7ce | 2012-04-23 10:11:57 +0900 | [diff] [blame] | 6878 | if (!d_percpu) |
| 6879 | return; |
| 6880 | |
Steven Rostedt | dd49a38 | 2010-10-20 21:51:26 -0400 | [diff] [blame] | 6881 | snprintf(cpu_dir, 30, "cpu%ld", cpu); |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 6882 | d_cpu = tracefs_create_dir(cpu_dir, d_percpu); |
Frederic Weisbecker | 8656e7a | 2009-02-26 00:41:38 +0100 | [diff] [blame] | 6883 | if (!d_cpu) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 6884 | pr_warn("Could not create tracefs '%s' entry\n", cpu_dir); |
Frederic Weisbecker | 8656e7a | 2009-02-26 00:41:38 +0100 | [diff] [blame] | 6885 | return; |
| 6886 | } |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6887 | |
Frederic Weisbecker | 8656e7a | 2009-02-26 00:41:38 +0100 | [diff] [blame] | 6888 | /* per cpu trace_pipe */ |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 6889 | trace_create_cpu_file("trace_pipe", 0444, d_cpu, |
Oleg Nesterov | 1554420 | 2013-07-23 17:25:57 +0200 | [diff] [blame] | 6890 | tr, cpu, &tracing_pipe_fops); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6891 | |
| 6892 | /* per cpu trace */ |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 6893 | trace_create_cpu_file("trace", 0644, d_cpu, |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 6894 | tr, cpu, &tracing_fops); |
Steven Rostedt | 7f96f93 | 2009-03-13 00:37:42 -0400 | [diff] [blame] | 6895 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 6896 | trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu, |
Oleg Nesterov | 46ef2be | 2013-07-23 17:26:00 +0200 | [diff] [blame] | 6897 | tr, cpu, &tracing_buffers_fops); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 6898 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 6899 | trace_create_cpu_file("stats", 0444, d_cpu, |
Oleg Nesterov | 4d3435b | 2013-07-23 17:26:03 +0200 | [diff] [blame] | 6900 | tr, cpu, &tracing_stats_fops); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 6901 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 6902 | trace_create_cpu_file("buffer_size_kb", 0444, d_cpu, |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 6903 | tr, cpu, &tracing_entries_fops); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6904 | |
| 6905 | #ifdef CONFIG_TRACER_SNAPSHOT |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 6906 | trace_create_cpu_file("snapshot", 0644, d_cpu, |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 6907 | tr, cpu, &snapshot_fops); |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6908 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 6909 | trace_create_cpu_file("snapshot_raw", 0444, d_cpu, |
Oleg Nesterov | 46ef2be | 2013-07-23 17:26:00 +0200 | [diff] [blame] | 6910 | tr, cpu, &snapshot_raw_fops); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6911 | #endif |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6912 | } |
| 6913 | |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 6914 | #ifdef CONFIG_FTRACE_SELFTEST |
| 6915 | /* Let selftest have access to static functions in this file */ |
| 6916 | #include "trace_selftest.c" |
| 6917 | #endif |
| 6918 | |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 6919 | static ssize_t |
| 6920 | trace_options_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 6921 | loff_t *ppos) |
| 6922 | { |
| 6923 | struct trace_option_dentry *topt = filp->private_data; |
| 6924 | char *buf; |
| 6925 | |
| 6926 | if (topt->flags->val & topt->opt->bit) |
| 6927 | buf = "1\n"; |
| 6928 | else |
| 6929 | buf = "0\n"; |
| 6930 | |
| 6931 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); |
| 6932 | } |
| 6933 | |
| 6934 | static ssize_t |
| 6935 | trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 6936 | loff_t *ppos) |
| 6937 | { |
| 6938 | struct trace_option_dentry *topt = filp->private_data; |
| 6939 | unsigned long val; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 6940 | int ret; |
| 6941 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 6942 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 6943 | if (ret) |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 6944 | return ret; |
| 6945 | |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 6946 | if (val != 0 && val != 1) |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 6947 | return -EINVAL; |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 6948 | |
| 6949 | if (!!(topt->flags->val & topt->opt->bit) != val) { |
| 6950 | mutex_lock(&trace_types_lock); |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 6951 | ret = __set_tracer_option(topt->tr, topt->flags, |
Steven Rostedt | c757bea | 2009-12-21 22:35:16 -0500 | [diff] [blame] | 6952 | topt->opt, !val); |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 6953 | mutex_unlock(&trace_types_lock); |
| 6954 | if (ret) |
| 6955 | return ret; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 6956 | } |
| 6957 | |
| 6958 | *ppos += cnt; |
| 6959 | |
| 6960 | return cnt; |
| 6961 | } |
| 6962 | |
| 6963 | |
| 6964 | static const struct file_operations trace_options_fops = { |
| 6965 | .open = tracing_open_generic, |
| 6966 | .read = trace_options_read, |
| 6967 | .write = trace_options_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 6968 | .llseek = generic_file_llseek, |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 6969 | }; |
| 6970 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 6971 | /* |
| 6972 | * In order to pass in both the trace_array descriptor as well as the index |
| 6973 | * to the flag that the trace option file represents, the trace_array |
| 6974 | * has a character array of trace_flags_index[], which holds the index |
| 6975 | * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc. |
| 6976 | * The address of this character array is passed to the flag option file |
| 6977 | * read/write callbacks. |
| 6978 | * |
| 6979 | * In order to extract both the index and the trace_array descriptor, |
| 6980 | * get_tr_index() uses the following algorithm. |
| 6981 | * |
| 6982 | * idx = *ptr; |
| 6983 | * |
| 6984 | * As the pointer itself contains the address of the index (remember |
| 6985 | * index[1] == 1). |
| 6986 | * |
| 6987 | * Then to get the trace_array descriptor, by subtracting that index |
| 6988 | * from the ptr, we get to the start of the index itself. |
| 6989 | * |
| 6990 | * ptr - idx == &index[0] |
| 6991 | * |
| 6992 | * Then a simple container_of() from that pointer gets us to the |
| 6993 | * trace_array descriptor. |
| 6994 | */ |
| 6995 | static void get_tr_index(void *data, struct trace_array **ptr, |
| 6996 | unsigned int *pindex) |
| 6997 | { |
| 6998 | *pindex = *(unsigned char *)data; |
| 6999 | |
| 7000 | *ptr = container_of(data - *pindex, struct trace_array, |
| 7001 | trace_flags_index); |
| 7002 | } |
| 7003 | |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7004 | static ssize_t |
| 7005 | trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 7006 | loff_t *ppos) |
| 7007 | { |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 7008 | void *tr_index = filp->private_data; |
| 7009 | struct trace_array *tr; |
| 7010 | unsigned int index; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7011 | char *buf; |
| 7012 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 7013 | get_tr_index(tr_index, &tr, &index); |
| 7014 | |
| 7015 | if (tr->trace_flags & (1 << index)) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7016 | buf = "1\n"; |
| 7017 | else |
| 7018 | buf = "0\n"; |
| 7019 | |
| 7020 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); |
| 7021 | } |
| 7022 | |
| 7023 | static ssize_t |
| 7024 | trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 7025 | loff_t *ppos) |
| 7026 | { |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 7027 | void *tr_index = filp->private_data; |
| 7028 | struct trace_array *tr; |
| 7029 | unsigned int index; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7030 | unsigned long val; |
| 7031 | int ret; |
| 7032 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 7033 | get_tr_index(tr_index, &tr, &index); |
| 7034 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 7035 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 7036 | if (ret) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7037 | return ret; |
| 7038 | |
Zhaolei | f2d84b6 | 2009-08-07 18:55:48 +0800 | [diff] [blame] | 7039 | if (val != 0 && val != 1) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7040 | return -EINVAL; |
Steven Rostedt (Red Hat) | 69d34da | 2013-03-14 13:50:56 -0400 | [diff] [blame] | 7041 | |
| 7042 | mutex_lock(&trace_types_lock); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7043 | ret = set_tracer_flag(tr, 1 << index, val); |
Steven Rostedt (Red Hat) | 69d34da | 2013-03-14 13:50:56 -0400 | [diff] [blame] | 7044 | mutex_unlock(&trace_types_lock); |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7045 | |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 7046 | if (ret < 0) |
| 7047 | return ret; |
| 7048 | |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7049 | *ppos += cnt; |
| 7050 | |
| 7051 | return cnt; |
| 7052 | } |
| 7053 | |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7054 | static const struct file_operations trace_options_core_fops = { |
| 7055 | .open = tracing_open_generic, |
| 7056 | .read = trace_options_core_read, |
| 7057 | .write = trace_options_core_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 7058 | .llseek = generic_file_llseek, |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7059 | }; |
| 7060 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 7061 | struct dentry *trace_create_file(const char *name, |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 7062 | umode_t mode, |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 7063 | struct dentry *parent, |
| 7064 | void *data, |
| 7065 | const struct file_operations *fops) |
| 7066 | { |
| 7067 | struct dentry *ret; |
| 7068 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7069 | ret = tracefs_create_file(name, mode, parent, data, fops); |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 7070 | if (!ret) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 7071 | pr_warn("Could not create tracefs '%s' entry\n", name); |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 7072 | |
| 7073 | return ret; |
| 7074 | } |
| 7075 | |
| 7076 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7077 | static struct dentry *trace_options_init_dentry(struct trace_array *tr) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7078 | { |
| 7079 | struct dentry *d_tracer; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7080 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7081 | if (tr->options) |
| 7082 | return tr->options; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7083 | |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 7084 | d_tracer = tracing_get_dentry(tr); |
Steven Rostedt (Red Hat) | 14a5ae4 | 2015-01-20 11:14:16 -0500 | [diff] [blame] | 7085 | if (IS_ERR(d_tracer)) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7086 | return NULL; |
| 7087 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7088 | tr->options = tracefs_create_dir("options", d_tracer); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7089 | if (!tr->options) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 7090 | pr_warn("Could not create tracefs directory 'options'\n"); |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7091 | return NULL; |
| 7092 | } |
| 7093 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7094 | return tr->options; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7095 | } |
| 7096 | |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7097 | static void |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7098 | create_trace_option_file(struct trace_array *tr, |
| 7099 | struct trace_option_dentry *topt, |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7100 | struct tracer_flags *flags, |
| 7101 | struct tracer_opt *opt) |
| 7102 | { |
| 7103 | struct dentry *t_options; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7104 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7105 | t_options = trace_options_init_dentry(tr); |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7106 | if (!t_options) |
| 7107 | return; |
| 7108 | |
| 7109 | topt->flags = flags; |
| 7110 | topt->opt = opt; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7111 | topt->tr = tr; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7112 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 7113 | topt->entry = trace_create_file(opt->name, 0644, t_options, topt, |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7114 | &trace_options_fops); |
| 7115 | |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7116 | } |
| 7117 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7118 | static void |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7119 | create_trace_option_files(struct trace_array *tr, struct tracer *tracer) |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7120 | { |
| 7121 | struct trace_option_dentry *topts; |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7122 | struct trace_options *tr_topts; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7123 | struct tracer_flags *flags; |
| 7124 | struct tracer_opt *opts; |
| 7125 | int cnt; |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7126 | int i; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7127 | |
| 7128 | if (!tracer) |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7129 | return; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7130 | |
| 7131 | flags = tracer->flags; |
| 7132 | |
| 7133 | if (!flags || !flags->opts) |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7134 | return; |
| 7135 | |
| 7136 | /* |
| 7137 | * If this is an instance, only create flags for tracers |
| 7138 | * the instance may have. |
| 7139 | */ |
| 7140 | if (!trace_ok_for_array(tracer, tr)) |
| 7141 | return; |
| 7142 | |
| 7143 | for (i = 0; i < tr->nr_topts; i++) { |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 7144 | /* Make sure there's no duplicate flags. */ |
| 7145 | if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags)) |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7146 | return; |
| 7147 | } |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7148 | |
| 7149 | opts = flags->opts; |
| 7150 | |
| 7151 | for (cnt = 0; opts[cnt].name; cnt++) |
| 7152 | ; |
| 7153 | |
Steven Rostedt | 0cfe824 | 2009-02-27 10:51:10 -0500 | [diff] [blame] | 7154 | topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL); |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7155 | if (!topts) |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7156 | return; |
| 7157 | |
| 7158 | tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1), |
| 7159 | GFP_KERNEL); |
| 7160 | if (!tr_topts) { |
| 7161 | kfree(topts); |
| 7162 | return; |
| 7163 | } |
| 7164 | |
| 7165 | tr->topts = tr_topts; |
| 7166 | tr->topts[tr->nr_topts].tracer = tracer; |
| 7167 | tr->topts[tr->nr_topts].topts = topts; |
| 7168 | tr->nr_topts++; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7169 | |
Steven Rostedt (Red Hat) | 41d9c0b | 2015-09-29 17:31:55 -0400 | [diff] [blame] | 7170 | for (cnt = 0; opts[cnt].name; cnt++) { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7171 | create_trace_option_file(tr, &topts[cnt], flags, |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7172 | &opts[cnt]); |
Steven Rostedt (Red Hat) | 41d9c0b | 2015-09-29 17:31:55 -0400 | [diff] [blame] | 7173 | WARN_ONCE(topts[cnt].entry == NULL, |
| 7174 | "Failed to create trace option: %s", |
| 7175 | opts[cnt].name); |
| 7176 | } |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 7177 | } |
| 7178 | |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7179 | static struct dentry * |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7180 | create_trace_option_core_file(struct trace_array *tr, |
| 7181 | const char *option, long index) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7182 | { |
| 7183 | struct dentry *t_options; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7184 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7185 | t_options = trace_options_init_dentry(tr); |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7186 | if (!t_options) |
| 7187 | return NULL; |
| 7188 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 7189 | return trace_create_file(option, 0644, t_options, |
| 7190 | (void *)&tr->trace_flags_index[index], |
| 7191 | &trace_options_core_fops); |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7192 | } |
| 7193 | |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 7194 | static void create_trace_options_dir(struct trace_array *tr) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7195 | { |
| 7196 | struct dentry *t_options; |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 7197 | bool top_level = tr == &global_trace; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7198 | int i; |
| 7199 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7200 | t_options = trace_options_init_dentry(tr); |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7201 | if (!t_options) |
| 7202 | return; |
| 7203 | |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 7204 | for (i = 0; trace_options[i]; i++) { |
| 7205 | if (top_level || |
| 7206 | !((1 << i) & TOP_LEVEL_TRACE_FLAGS)) |
| 7207 | create_trace_option_core_file(tr, trace_options[i], i); |
| 7208 | } |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 7209 | } |
| 7210 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 7211 | static ssize_t |
| 7212 | rb_simple_read(struct file *filp, char __user *ubuf, |
| 7213 | size_t cnt, loff_t *ppos) |
| 7214 | { |
Steven Rostedt | 348f0fc | 2012-04-16 15:41:28 -0400 | [diff] [blame] | 7215 | struct trace_array *tr = filp->private_data; |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 7216 | char buf[64]; |
| 7217 | int r; |
| 7218 | |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 7219 | r = tracer_tracing_is_on(tr); |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 7220 | r = sprintf(buf, "%d\n", r); |
| 7221 | |
| 7222 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 7223 | } |
| 7224 | |
| 7225 | static ssize_t |
| 7226 | rb_simple_write(struct file *filp, const char __user *ubuf, |
| 7227 | size_t cnt, loff_t *ppos) |
| 7228 | { |
Steven Rostedt | 348f0fc | 2012-04-16 15:41:28 -0400 | [diff] [blame] | 7229 | struct trace_array *tr = filp->private_data; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7230 | struct ring_buffer *buffer = tr->trace_buffer.buffer; |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 7231 | unsigned long val; |
| 7232 | int ret; |
| 7233 | |
| 7234 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 7235 | if (ret) |
| 7236 | return ret; |
| 7237 | |
| 7238 | if (buffer) { |
Steven Rostedt | 2df8f8a | 2013-01-11 16:14:10 -0500 | [diff] [blame] | 7239 | mutex_lock(&trace_types_lock); |
| 7240 | if (val) { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 7241 | tracer_tracing_on(tr); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7242 | if (tr->current_trace->start) |
| 7243 | tr->current_trace->start(tr); |
Steven Rostedt | 2df8f8a | 2013-01-11 16:14:10 -0500 | [diff] [blame] | 7244 | } else { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 7245 | tracer_tracing_off(tr); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7246 | if (tr->current_trace->stop) |
| 7247 | tr->current_trace->stop(tr); |
Steven Rostedt | 2df8f8a | 2013-01-11 16:14:10 -0500 | [diff] [blame] | 7248 | } |
| 7249 | mutex_unlock(&trace_types_lock); |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 7250 | } |
| 7251 | |
| 7252 | (*ppos)++; |
| 7253 | |
| 7254 | return cnt; |
| 7255 | } |
| 7256 | |
| 7257 | static const struct file_operations rb_simple_fops = { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7258 | .open = tracing_open_generic_tr, |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 7259 | .read = rb_simple_read, |
| 7260 | .write = rb_simple_write, |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7261 | .release = tracing_release_generic_tr, |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 7262 | .llseek = default_llseek, |
| 7263 | }; |
| 7264 | |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7265 | struct dentry *trace_instance_dir; |
| 7266 | |
| 7267 | static void |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7268 | init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7269 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 7270 | static int |
| 7271 | allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size) |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7272 | { |
| 7273 | enum ring_buffer_flags rb_flags; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7274 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 7275 | rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7276 | |
Steven Rostedt (Red Hat) | dced341 | 2014-01-14 10:19:46 -0500 | [diff] [blame] | 7277 | buf->tr = tr; |
| 7278 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 7279 | buf->buffer = ring_buffer_alloc(size, rb_flags); |
| 7280 | if (!buf->buffer) |
| 7281 | return -ENOMEM; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7282 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 7283 | buf->data = alloc_percpu(struct trace_array_cpu); |
| 7284 | if (!buf->data) { |
| 7285 | ring_buffer_free(buf->buffer); |
| 7286 | return -ENOMEM; |
| 7287 | } |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7288 | |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7289 | /* Allocate the first page for all buffers */ |
| 7290 | set_buffer_entries(&tr->trace_buffer, |
| 7291 | ring_buffer_size(tr->trace_buffer.buffer, 0)); |
| 7292 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 7293 | return 0; |
| 7294 | } |
| 7295 | |
| 7296 | static int allocate_trace_buffers(struct trace_array *tr, int size) |
| 7297 | { |
| 7298 | int ret; |
| 7299 | |
| 7300 | ret = allocate_trace_buffer(tr, &tr->trace_buffer, size); |
| 7301 | if (ret) |
| 7302 | return ret; |
| 7303 | |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7304 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 7305 | ret = allocate_trace_buffer(tr, &tr->max_buffer, |
| 7306 | allocate_snapshot ? size : 1); |
| 7307 | if (WARN_ON(ret)) { |
| 7308 | ring_buffer_free(tr->trace_buffer.buffer); |
| 7309 | free_percpu(tr->trace_buffer.data); |
| 7310 | return -ENOMEM; |
| 7311 | } |
| 7312 | tr->allocated_snapshot = allocate_snapshot; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7313 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 7314 | /* |
| 7315 | * Only the top level trace array gets its snapshot allocated |
| 7316 | * from the kernel command line. |
| 7317 | */ |
| 7318 | allocate_snapshot = false; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7319 | #endif |
| 7320 | return 0; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7321 | } |
| 7322 | |
Steven Rostedt (Red Hat) | f0b70cc | 2014-06-10 12:06:30 -0400 | [diff] [blame] | 7323 | static void free_trace_buffer(struct trace_buffer *buf) |
| 7324 | { |
| 7325 | if (buf->buffer) { |
| 7326 | ring_buffer_free(buf->buffer); |
| 7327 | buf->buffer = NULL; |
| 7328 | free_percpu(buf->data); |
| 7329 | buf->data = NULL; |
| 7330 | } |
| 7331 | } |
| 7332 | |
Steven Rostedt (Red Hat) | 23aaa3c | 2014-06-06 00:01:46 -0400 | [diff] [blame] | 7333 | static void free_trace_buffers(struct trace_array *tr) |
| 7334 | { |
| 7335 | if (!tr) |
| 7336 | return; |
| 7337 | |
Steven Rostedt (Red Hat) | f0b70cc | 2014-06-10 12:06:30 -0400 | [diff] [blame] | 7338 | free_trace_buffer(&tr->trace_buffer); |
Steven Rostedt (Red Hat) | 23aaa3c | 2014-06-06 00:01:46 -0400 | [diff] [blame] | 7339 | |
| 7340 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | f0b70cc | 2014-06-10 12:06:30 -0400 | [diff] [blame] | 7341 | free_trace_buffer(&tr->max_buffer); |
Steven Rostedt (Red Hat) | 23aaa3c | 2014-06-06 00:01:46 -0400 | [diff] [blame] | 7342 | #endif |
| 7343 | } |
| 7344 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 7345 | static void init_trace_flags_index(struct trace_array *tr) |
| 7346 | { |
| 7347 | int i; |
| 7348 | |
| 7349 | /* Used by the trace options files */ |
| 7350 | for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) |
| 7351 | tr->trace_flags_index[i] = i; |
| 7352 | } |
| 7353 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7354 | static void __update_tracer_options(struct trace_array *tr) |
| 7355 | { |
| 7356 | struct tracer *t; |
| 7357 | |
| 7358 | for (t = trace_types; t; t = t->next) |
| 7359 | add_tracer_options(tr, t); |
| 7360 | } |
| 7361 | |
| 7362 | static void update_tracer_options(struct trace_array *tr) |
| 7363 | { |
| 7364 | mutex_lock(&trace_types_lock); |
| 7365 | __update_tracer_options(tr); |
| 7366 | mutex_unlock(&trace_types_lock); |
| 7367 | } |
| 7368 | |
Steven Rostedt (Red Hat) | eae4735 | 2015-01-21 10:01:39 -0500 | [diff] [blame] | 7369 | static int instance_mkdir(const char *name) |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7370 | { |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7371 | struct trace_array *tr; |
| 7372 | int ret; |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7373 | |
| 7374 | mutex_lock(&trace_types_lock); |
| 7375 | |
| 7376 | ret = -EEXIST; |
| 7377 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 7378 | if (tr->name && strcmp(tr->name, name) == 0) |
| 7379 | goto out_unlock; |
| 7380 | } |
| 7381 | |
| 7382 | ret = -ENOMEM; |
| 7383 | tr = kzalloc(sizeof(*tr), GFP_KERNEL); |
| 7384 | if (!tr) |
| 7385 | goto out_unlock; |
| 7386 | |
| 7387 | tr->name = kstrdup(name, GFP_KERNEL); |
| 7388 | if (!tr->name) |
| 7389 | goto out_free_tr; |
| 7390 | |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 7391 | if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL)) |
| 7392 | goto out_free_tr; |
| 7393 | |
Steven Rostedt (Red Hat) | 2055062 | 2016-04-25 22:40:12 -0400 | [diff] [blame] | 7394 | tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 7395 | |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 7396 | cpumask_copy(tr->tracing_cpumask, cpu_all_mask); |
| 7397 | |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7398 | raw_spin_lock_init(&tr->start_lock); |
| 7399 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 7400 | tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
| 7401 | |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7402 | tr->current_trace = &nop_trace; |
| 7403 | |
| 7404 | INIT_LIST_HEAD(&tr->systems); |
| 7405 | INIT_LIST_HEAD(&tr->events); |
| 7406 | |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7407 | if (allocate_trace_buffers(tr, trace_buf_size) < 0) |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7408 | goto out_free_tr; |
| 7409 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7410 | tr->dir = tracefs_create_dir(name, trace_instance_dir); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7411 | if (!tr->dir) |
| 7412 | goto out_free_tr; |
| 7413 | |
| 7414 | ret = event_trace_add_tracer(tr->dir, tr); |
Alexander Z Lam | 609e85a | 2013-07-10 17:34:34 -0700 | [diff] [blame] | 7415 | if (ret) { |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7416 | tracefs_remove_recursive(tr->dir); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7417 | goto out_free_tr; |
Alexander Z Lam | 609e85a | 2013-07-10 17:34:34 -0700 | [diff] [blame] | 7418 | } |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7419 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7420 | init_tracer_tracefs(tr, tr->dir); |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 7421 | init_trace_flags_index(tr); |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7422 | __update_tracer_options(tr); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7423 | |
| 7424 | list_add(&tr->list, &ftrace_trace_arrays); |
| 7425 | |
| 7426 | mutex_unlock(&trace_types_lock); |
| 7427 | |
| 7428 | return 0; |
| 7429 | |
| 7430 | out_free_tr: |
Steven Rostedt (Red Hat) | 23aaa3c | 2014-06-06 00:01:46 -0400 | [diff] [blame] | 7431 | free_trace_buffers(tr); |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 7432 | free_cpumask_var(tr->tracing_cpumask); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7433 | kfree(tr->name); |
| 7434 | kfree(tr); |
| 7435 | |
| 7436 | out_unlock: |
| 7437 | mutex_unlock(&trace_types_lock); |
| 7438 | |
| 7439 | return ret; |
| 7440 | |
| 7441 | } |
| 7442 | |
Steven Rostedt (Red Hat) | eae4735 | 2015-01-21 10:01:39 -0500 | [diff] [blame] | 7443 | static int instance_rmdir(const char *name) |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 7444 | { |
| 7445 | struct trace_array *tr; |
| 7446 | int found = 0; |
| 7447 | int ret; |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7448 | int i; |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 7449 | |
| 7450 | mutex_lock(&trace_types_lock); |
| 7451 | |
| 7452 | ret = -ENODEV; |
| 7453 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 7454 | if (tr->name && strcmp(tr->name, name) == 0) { |
| 7455 | found = 1; |
| 7456 | break; |
| 7457 | } |
| 7458 | } |
| 7459 | if (!found) |
| 7460 | goto out_unlock; |
| 7461 | |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 7462 | ret = -EBUSY; |
Steven Rostedt (Red Hat) | cf6ab6d | 2014-12-15 20:13:31 -0500 | [diff] [blame] | 7463 | if (tr->ref || (tr->current_trace && tr->current_trace->ref)) |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 7464 | goto out_unlock; |
| 7465 | |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 7466 | list_del(&tr->list); |
| 7467 | |
Steven Rostedt (Red Hat) | 2055062 | 2016-04-25 22:40:12 -0400 | [diff] [blame] | 7468 | /* Disable all the flags that were enabled coming in */ |
| 7469 | for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) { |
| 7470 | if ((1 << i) & ZEROED_TRACE_FLAGS) |
| 7471 | set_tracer_flag(tr, 1 << i, 0); |
| 7472 | } |
| 7473 | |
Steven Rostedt (Red Hat) | 6b450d2 | 2014-01-14 08:43:01 -0500 | [diff] [blame] | 7474 | tracing_set_nop(tr); |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 7475 | event_trace_del_tracer(tr); |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 7476 | ftrace_destroy_function_files(tr); |
Jiaxing Wang | 681a4a2 | 2015-10-18 19:58:08 +0800 | [diff] [blame] | 7477 | tracefs_remove_recursive(tr->dir); |
Steven Rostedt (Red Hat) | a9fcaaa | 2014-06-06 23:17:28 -0400 | [diff] [blame] | 7478 | free_trace_buffers(tr); |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 7479 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7480 | for (i = 0; i < tr->nr_topts; i++) { |
| 7481 | kfree(tr->topts[i].topts); |
| 7482 | } |
| 7483 | kfree(tr->topts); |
| 7484 | |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 7485 | kfree(tr->name); |
| 7486 | kfree(tr); |
| 7487 | |
| 7488 | ret = 0; |
| 7489 | |
| 7490 | out_unlock: |
| 7491 | mutex_unlock(&trace_types_lock); |
| 7492 | |
| 7493 | return ret; |
| 7494 | } |
| 7495 | |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7496 | static __init void create_trace_instances(struct dentry *d_tracer) |
| 7497 | { |
Steven Rostedt (Red Hat) | eae4735 | 2015-01-21 10:01:39 -0500 | [diff] [blame] | 7498 | trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer, |
| 7499 | instance_mkdir, |
| 7500 | instance_rmdir); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7501 | if (WARN_ON(!trace_instance_dir)) |
| 7502 | return; |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7503 | } |
| 7504 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7505 | static void |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7506 | init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer) |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7507 | { |
Steven Rostedt (Red Hat) | 121aaee | 2013-03-05 21:52:25 -0500 | [diff] [blame] | 7508 | int cpu; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7509 | |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 7510 | trace_create_file("available_tracers", 0444, d_tracer, |
| 7511 | tr, &show_traces_fops); |
| 7512 | |
| 7513 | trace_create_file("current_tracer", 0644, d_tracer, |
| 7514 | tr, &set_tracer_fops); |
| 7515 | |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 7516 | trace_create_file("tracing_cpumask", 0644, d_tracer, |
| 7517 | tr, &tracing_cpumask_fops); |
| 7518 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7519 | trace_create_file("trace_options", 0644, d_tracer, |
| 7520 | tr, &tracing_iter_fops); |
| 7521 | |
| 7522 | trace_create_file("trace", 0644, d_tracer, |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 7523 | tr, &tracing_fops); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7524 | |
| 7525 | trace_create_file("trace_pipe", 0444, d_tracer, |
Oleg Nesterov | 1554420 | 2013-07-23 17:25:57 +0200 | [diff] [blame] | 7526 | tr, &tracing_pipe_fops); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7527 | |
| 7528 | trace_create_file("buffer_size_kb", 0644, d_tracer, |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 7529 | tr, &tracing_entries_fops); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7530 | |
| 7531 | trace_create_file("buffer_total_size_kb", 0444, d_tracer, |
| 7532 | tr, &tracing_total_entries_fops); |
| 7533 | |
Wang YanQing | 238ae93 | 2013-05-26 16:52:01 +0800 | [diff] [blame] | 7534 | trace_create_file("free_buffer", 0200, d_tracer, |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7535 | tr, &tracing_free_buffer_fops); |
| 7536 | |
| 7537 | trace_create_file("trace_marker", 0220, d_tracer, |
| 7538 | tr, &tracing_mark_fops); |
| 7539 | |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 7540 | trace_create_file("trace_marker_raw", 0220, d_tracer, |
| 7541 | tr, &tracing_mark_raw_fops); |
| 7542 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7543 | trace_create_file("trace_clock", 0644, d_tracer, tr, |
| 7544 | &trace_clock_fops); |
| 7545 | |
| 7546 | trace_create_file("tracing_on", 0644, d_tracer, |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 7547 | tr, &rb_simple_fops); |
Steven Rostedt (Red Hat) | ce9bae5 | 2013-03-05 21:23:55 -0500 | [diff] [blame] | 7548 | |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 7549 | create_trace_options_dir(tr); |
| 7550 | |
Steven Rostedt (Red Hat) | f971cc9 | 2016-09-07 12:45:09 -0400 | [diff] [blame] | 7551 | #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER) |
Steven Rostedt (Red Hat) | 6d9b3fa | 2014-01-14 11:28:38 -0500 | [diff] [blame] | 7552 | trace_create_file("tracing_max_latency", 0644, d_tracer, |
| 7553 | &tr->max_latency, &tracing_max_lat_fops); |
| 7554 | #endif |
| 7555 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 7556 | if (ftrace_create_function_files(tr, d_tracer)) |
| 7557 | WARN(1, "Could not allocate function filter files"); |
| 7558 | |
Steven Rostedt (Red Hat) | ce9bae5 | 2013-03-05 21:23:55 -0500 | [diff] [blame] | 7559 | #ifdef CONFIG_TRACER_SNAPSHOT |
| 7560 | trace_create_file("snapshot", 0644, d_tracer, |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 7561 | tr, &snapshot_fops); |
Steven Rostedt (Red Hat) | ce9bae5 | 2013-03-05 21:23:55 -0500 | [diff] [blame] | 7562 | #endif |
Steven Rostedt (Red Hat) | 121aaee | 2013-03-05 21:52:25 -0500 | [diff] [blame] | 7563 | |
| 7564 | for_each_tracing_cpu(cpu) |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7565 | tracing_init_tracefs_percpu(tr, cpu); |
Steven Rostedt (Red Hat) | 121aaee | 2013-03-05 21:52:25 -0500 | [diff] [blame] | 7566 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 7567 | ftrace_init_tracefs(tr, d_tracer); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7568 | } |
| 7569 | |
Steven Rostedt (Red Hat) | f76180b | 2015-01-20 15:48:46 -0500 | [diff] [blame] | 7570 | static struct vfsmount *trace_automount(void *ingore) |
| 7571 | { |
| 7572 | struct vfsmount *mnt; |
| 7573 | struct file_system_type *type; |
| 7574 | |
| 7575 | /* |
| 7576 | * To maintain backward compatibility for tools that mount |
| 7577 | * debugfs to get to the tracing facility, tracefs is automatically |
| 7578 | * mounted to the debugfs/tracing directory. |
| 7579 | */ |
| 7580 | type = get_fs_type("tracefs"); |
| 7581 | if (!type) |
| 7582 | return NULL; |
| 7583 | mnt = vfs_kern_mount(type, 0, "tracefs", NULL); |
| 7584 | put_filesystem(type); |
| 7585 | if (IS_ERR(mnt)) |
| 7586 | return NULL; |
| 7587 | mntget(mnt); |
| 7588 | |
| 7589 | return mnt; |
| 7590 | } |
| 7591 | |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 7592 | /** |
| 7593 | * tracing_init_dentry - initialize top level trace array |
| 7594 | * |
| 7595 | * This is called when creating files or directories in the tracing |
| 7596 | * directory. It is called via fs_initcall() by any of the boot up code |
| 7597 | * and expects to return the dentry of the top level tracing directory. |
| 7598 | */ |
| 7599 | struct dentry *tracing_init_dentry(void) |
| 7600 | { |
| 7601 | struct trace_array *tr = &global_trace; |
| 7602 | |
Steven Rostedt (Red Hat) | f76180b | 2015-01-20 15:48:46 -0500 | [diff] [blame] | 7603 | /* The top level trace array uses NULL as parent */ |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 7604 | if (tr->dir) |
Steven Rostedt (Red Hat) | f76180b | 2015-01-20 15:48:46 -0500 | [diff] [blame] | 7605 | return NULL; |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 7606 | |
Jiaxing Wang | 8b12919 | 2015-11-06 16:04:16 +0800 | [diff] [blame] | 7607 | if (WARN_ON(!tracefs_initialized()) || |
| 7608 | (IS_ENABLED(CONFIG_DEBUG_FS) && |
| 7609 | WARN_ON(!debugfs_initialized()))) |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 7610 | return ERR_PTR(-ENODEV); |
| 7611 | |
Steven Rostedt (Red Hat) | f76180b | 2015-01-20 15:48:46 -0500 | [diff] [blame] | 7612 | /* |
| 7613 | * As there may still be users that expect the tracing |
| 7614 | * files to exist in debugfs/tracing, we must automount |
| 7615 | * the tracefs file system there, so older tools still |
| 7616 | * work with the newer kerenl. |
| 7617 | */ |
| 7618 | tr->dir = debugfs_create_automount("tracing", NULL, |
| 7619 | trace_automount, NULL); |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 7620 | if (!tr->dir) { |
| 7621 | pr_warn_once("Could not create debugfs directory 'tracing'\n"); |
| 7622 | return ERR_PTR(-ENOMEM); |
| 7623 | } |
| 7624 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7625 | return NULL; |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 7626 | } |
| 7627 | |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 7628 | extern struct trace_enum_map *__start_ftrace_enum_maps[]; |
| 7629 | extern struct trace_enum_map *__stop_ftrace_enum_maps[]; |
| 7630 | |
| 7631 | static void __init trace_enum_init(void) |
| 7632 | { |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 7633 | int len; |
| 7634 | |
| 7635 | len = __stop_ftrace_enum_maps - __start_ftrace_enum_maps; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 7636 | trace_insert_enum_map(NULL, __start_ftrace_enum_maps, len); |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 7637 | } |
| 7638 | |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 7639 | #ifdef CONFIG_MODULES |
| 7640 | static void trace_module_add_enums(struct module *mod) |
| 7641 | { |
| 7642 | if (!mod->num_trace_enums) |
| 7643 | return; |
| 7644 | |
| 7645 | /* |
| 7646 | * Modules with bad taint do not have events created, do |
| 7647 | * not bother with enums either. |
| 7648 | */ |
| 7649 | if (trace_module_has_bad_taint(mod)) |
| 7650 | return; |
| 7651 | |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 7652 | trace_insert_enum_map(mod, mod->trace_enums, mod->num_trace_enums); |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 7653 | } |
| 7654 | |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 7655 | #ifdef CONFIG_TRACE_ENUM_MAP_FILE |
| 7656 | static void trace_module_remove_enums(struct module *mod) |
| 7657 | { |
| 7658 | union trace_enum_map_item *map; |
| 7659 | union trace_enum_map_item **last = &trace_enum_maps; |
| 7660 | |
| 7661 | if (!mod->num_trace_enums) |
| 7662 | return; |
| 7663 | |
| 7664 | mutex_lock(&trace_enum_mutex); |
| 7665 | |
| 7666 | map = trace_enum_maps; |
| 7667 | |
| 7668 | while (map) { |
| 7669 | if (map->head.mod == mod) |
| 7670 | break; |
| 7671 | map = trace_enum_jmp_to_tail(map); |
| 7672 | last = &map->tail.next; |
| 7673 | map = map->tail.next; |
| 7674 | } |
| 7675 | if (!map) |
| 7676 | goto out; |
| 7677 | |
| 7678 | *last = trace_enum_jmp_to_tail(map)->tail.next; |
| 7679 | kfree(map); |
| 7680 | out: |
| 7681 | mutex_unlock(&trace_enum_mutex); |
| 7682 | } |
| 7683 | #else |
| 7684 | static inline void trace_module_remove_enums(struct module *mod) { } |
| 7685 | #endif /* CONFIG_TRACE_ENUM_MAP_FILE */ |
| 7686 | |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 7687 | static int trace_module_notify(struct notifier_block *self, |
| 7688 | unsigned long val, void *data) |
| 7689 | { |
| 7690 | struct module *mod = data; |
| 7691 | |
| 7692 | switch (val) { |
| 7693 | case MODULE_STATE_COMING: |
| 7694 | trace_module_add_enums(mod); |
| 7695 | break; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 7696 | case MODULE_STATE_GOING: |
| 7697 | trace_module_remove_enums(mod); |
| 7698 | break; |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 7699 | } |
| 7700 | |
| 7701 | return 0; |
| 7702 | } |
| 7703 | |
| 7704 | static struct notifier_block trace_module_nb = { |
| 7705 | .notifier_call = trace_module_notify, |
| 7706 | .priority = 0, |
| 7707 | }; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 7708 | #endif /* CONFIG_MODULES */ |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 7709 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7710 | static __init int tracer_init_tracefs(void) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7711 | { |
| 7712 | struct dentry *d_tracer; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7713 | |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 7714 | trace_access_lock_init(); |
| 7715 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7716 | d_tracer = tracing_init_dentry(); |
Steven Rostedt (Red Hat) | 14a5ae4 | 2015-01-20 11:14:16 -0500 | [diff] [blame] | 7717 | if (IS_ERR(d_tracer)) |
Namhyung Kim | ed6f1c9 | 2013-04-10 09:18:12 +0900 | [diff] [blame] | 7718 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7719 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7720 | init_tracer_tracefs(&global_trace, d_tracer); |
Steven Rostedt (Red Hat) | 501c237 | 2016-07-05 10:04:34 -0400 | [diff] [blame] | 7721 | ftrace_init_tracefs_toplevel(&global_trace, d_tracer); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7722 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 7723 | trace_create_file("tracing_thresh", 0644, d_tracer, |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 7724 | &global_trace, &tracing_thresh_fops); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7725 | |
Li Zefan | 339ae5d | 2009-04-17 10:34:30 +0800 | [diff] [blame] | 7726 | trace_create_file("README", 0444, d_tracer, |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 7727 | NULL, &tracing_readme_fops); |
Ingo Molnar | 7bd2f24 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 7728 | |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 7729 | trace_create_file("saved_cmdlines", 0444, d_tracer, |
| 7730 | NULL, &tracing_saved_cmdlines_fops); |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 7731 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 7732 | trace_create_file("saved_cmdlines_size", 0644, d_tracer, |
| 7733 | NULL, &tracing_saved_cmdlines_size_fops); |
| 7734 | |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 7735 | trace_enum_init(); |
| 7736 | |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 7737 | trace_create_enum_file(d_tracer); |
| 7738 | |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 7739 | #ifdef CONFIG_MODULES |
| 7740 | register_module_notifier(&trace_module_nb); |
| 7741 | #endif |
| 7742 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7743 | #ifdef CONFIG_DYNAMIC_FTRACE |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 7744 | trace_create_file("dyn_ftrace_total_info", 0444, d_tracer, |
| 7745 | &ftrace_update_tot_cnt, &tracing_dyn_info_fops); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7746 | #endif |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7747 | |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 7748 | create_trace_instances(d_tracer); |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 7749 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 7750 | update_tracer_options(&global_trace); |
Steven Rostedt (Red Hat) | 09d23a1 | 2015-02-03 12:45:53 -0500 | [diff] [blame] | 7751 | |
Frédéric Weisbecker | b5ad384 | 2008-09-23 11:34:32 +0100 | [diff] [blame] | 7752 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7753 | } |
| 7754 | |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7755 | static int trace_panic_handler(struct notifier_block *this, |
| 7756 | unsigned long event, void *unused) |
| 7757 | { |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 7758 | if (ftrace_dump_on_oops) |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 7759 | ftrace_dump(ftrace_dump_on_oops); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7760 | return NOTIFY_OK; |
| 7761 | } |
| 7762 | |
| 7763 | static struct notifier_block trace_panic_notifier = { |
| 7764 | .notifier_call = trace_panic_handler, |
| 7765 | .next = NULL, |
| 7766 | .priority = 150 /* priority: INT_MAX >= x >= 0 */ |
| 7767 | }; |
| 7768 | |
| 7769 | static int trace_die_handler(struct notifier_block *self, |
| 7770 | unsigned long val, |
| 7771 | void *data) |
| 7772 | { |
| 7773 | switch (val) { |
| 7774 | case DIE_OOPS: |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 7775 | if (ftrace_dump_on_oops) |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 7776 | ftrace_dump(ftrace_dump_on_oops); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7777 | break; |
| 7778 | default: |
| 7779 | break; |
| 7780 | } |
| 7781 | return NOTIFY_OK; |
| 7782 | } |
| 7783 | |
| 7784 | static struct notifier_block trace_die_notifier = { |
| 7785 | .notifier_call = trace_die_handler, |
| 7786 | .priority = 200 |
| 7787 | }; |
| 7788 | |
| 7789 | /* |
| 7790 | * printk is set to max of 1024, we really don't need it that big. |
| 7791 | * Nothing should be printing 1000 characters anyway. |
| 7792 | */ |
| 7793 | #define TRACE_MAX_PRINT 1000 |
| 7794 | |
| 7795 | /* |
| 7796 | * Define here KERN_TRACE so that we have one place to modify |
| 7797 | * it if we decide to change what log level the ftrace dump |
| 7798 | * should be at. |
| 7799 | */ |
Steven Rostedt | 428aee1 | 2009-01-14 12:24:42 -0500 | [diff] [blame] | 7800 | #define KERN_TRACE KERN_EMERG |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7801 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 7802 | void |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7803 | trace_printk_seq(struct trace_seq *s) |
| 7804 | { |
| 7805 | /* Probably should print a warning here. */ |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 7806 | if (s->seq.len >= TRACE_MAX_PRINT) |
| 7807 | s->seq.len = TRACE_MAX_PRINT; |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7808 | |
Steven Rostedt (Red Hat) | 820b75f | 2014-11-19 10:56:41 -0500 | [diff] [blame] | 7809 | /* |
| 7810 | * More paranoid code. Although the buffer size is set to |
| 7811 | * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just |
| 7812 | * an extra layer of protection. |
| 7813 | */ |
| 7814 | if (WARN_ON_ONCE(s->seq.len >= s->seq.size)) |
| 7815 | s->seq.len = s->seq.size - 1; |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7816 | |
| 7817 | /* should be zero ended, but we are paranoid. */ |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 7818 | s->buffer[s->seq.len] = 0; |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7819 | |
| 7820 | printk(KERN_TRACE "%s", s->buffer); |
| 7821 | |
Steven Rostedt | f952075 | 2009-03-02 14:04:40 -0500 | [diff] [blame] | 7822 | trace_seq_init(s); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7823 | } |
| 7824 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 7825 | void trace_init_global_iter(struct trace_iterator *iter) |
| 7826 | { |
| 7827 | iter->tr = &global_trace; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7828 | iter->trace = iter->tr->current_trace; |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 7829 | iter->cpu_file = RING_BUFFER_ALL_CPUS; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7830 | iter->trace_buffer = &global_trace.trace_buffer; |
Cody P Schafer | b2f974d | 2013-10-23 11:49:57 -0700 | [diff] [blame] | 7831 | |
| 7832 | if (iter->trace && iter->trace->open) |
| 7833 | iter->trace->open(iter); |
| 7834 | |
| 7835 | /* Annotate start of buffers if we had overruns */ |
| 7836 | if (ring_buffer_overruns(iter->trace_buffer->buffer)) |
| 7837 | iter->iter_flags |= TRACE_FILE_ANNOTATE; |
| 7838 | |
| 7839 | /* Output in nanoseconds only if we are using a clock in nanoseconds. */ |
| 7840 | if (trace_clocks[iter->tr->clock_id].in_ns) |
| 7841 | iter->iter_flags |= TRACE_FILE_TIME_IN_NS; |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 7842 | } |
| 7843 | |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 7844 | void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7845 | { |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7846 | /* use static because iter can be a bit big for the stack */ |
| 7847 | static struct trace_iterator iter; |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 7848 | static atomic_t dump_running; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 7849 | struct trace_array *tr = &global_trace; |
Frederic Weisbecker | cf586b6 | 2009-03-22 05:04:35 +0100 | [diff] [blame] | 7850 | unsigned int old_userobj; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 7851 | unsigned long flags; |
| 7852 | int cnt = 0, cpu; |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7853 | |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 7854 | /* Only allow one dump user at a time. */ |
| 7855 | if (atomic_inc_return(&dump_running) != 1) { |
| 7856 | atomic_dec(&dump_running); |
| 7857 | return; |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 7858 | } |
| 7859 | |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 7860 | /* |
| 7861 | * Always turn off tracing when we dump. |
| 7862 | * We don't need to show trace output of what happens |
| 7863 | * between multiple crashes. |
| 7864 | * |
| 7865 | * If the user does a sysrq-z, then they can re-enable |
| 7866 | * tracing with echo 1 > tracing_on. |
| 7867 | */ |
| 7868 | tracing_off(); |
| 7869 | |
| 7870 | local_irq_save(flags); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7871 | |
Jovi Zhang | 38dbe0b | 2013-01-25 18:03:07 +0800 | [diff] [blame] | 7872 | /* Simulate the iterator */ |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 7873 | trace_init_global_iter(&iter); |
| 7874 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 7875 | for_each_tracing_cpu(cpu) { |
Umesh Tiwari | 5e2d5ef | 2015-06-22 16:55:06 +0530 | [diff] [blame] | 7876 | atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 7877 | } |
| 7878 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 7879 | old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ; |
Frederic Weisbecker | cf586b6 | 2009-03-22 05:04:35 +0100 | [diff] [blame] | 7880 | |
Török Edwin | b54d3de | 2008-11-22 13:28:48 +0200 | [diff] [blame] | 7881 | /* don't look at user memory in panic mode */ |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 7882 | tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ; |
Török Edwin | b54d3de | 2008-11-22 13:28:48 +0200 | [diff] [blame] | 7883 | |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 7884 | switch (oops_dump_mode) { |
| 7885 | case DUMP_ALL: |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 7886 | iter.cpu_file = RING_BUFFER_ALL_CPUS; |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 7887 | break; |
| 7888 | case DUMP_ORIG: |
| 7889 | iter.cpu_file = raw_smp_processor_id(); |
| 7890 | break; |
| 7891 | case DUMP_NONE: |
| 7892 | goto out_enable; |
| 7893 | default: |
| 7894 | printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n"); |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 7895 | iter.cpu_file = RING_BUFFER_ALL_CPUS; |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 7896 | } |
| 7897 | |
| 7898 | printk(KERN_TRACE "Dumping ftrace buffer:\n"); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7899 | |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 7900 | /* Did function tracer already get disabled? */ |
| 7901 | if (ftrace_is_dead()) { |
| 7902 | printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n"); |
| 7903 | printk("# MAY BE MISSING FUNCTION EVENTS\n"); |
| 7904 | } |
| 7905 | |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7906 | /* |
| 7907 | * We need to stop all tracing on all CPUS to read the |
| 7908 | * the next buffer. This is a bit expensive, but is |
| 7909 | * not done often. We fill all what we can read, |
| 7910 | * and then release the locks again. |
| 7911 | */ |
| 7912 | |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7913 | while (!trace_empty(&iter)) { |
| 7914 | |
| 7915 | if (!cnt) |
| 7916 | printk(KERN_TRACE "---------------------------------\n"); |
| 7917 | |
| 7918 | cnt++; |
| 7919 | |
| 7920 | /* reset all but tr, trace, and overruns */ |
| 7921 | memset(&iter.seq, 0, |
| 7922 | sizeof(struct trace_iterator) - |
| 7923 | offsetof(struct trace_iterator, seq)); |
| 7924 | iter.iter_flags |= TRACE_FILE_LAT_FMT; |
| 7925 | iter.pos = -1; |
| 7926 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 7927 | if (trace_find_next_entry_inc(&iter) != NULL) { |
Lai Jiangshan | 74e7ff8 | 2009-07-28 20:17:22 +0800 | [diff] [blame] | 7928 | int ret; |
| 7929 | |
| 7930 | ret = print_trace_line(&iter); |
| 7931 | if (ret != TRACE_TYPE_NO_CONSUME) |
| 7932 | trace_consume(&iter); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7933 | } |
Steven Rostedt | b892e5c | 2012-03-01 22:06:48 -0500 | [diff] [blame] | 7934 | touch_nmi_watchdog(); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7935 | |
| 7936 | trace_printk_seq(&iter.seq); |
| 7937 | } |
| 7938 | |
| 7939 | if (!cnt) |
| 7940 | printk(KERN_TRACE " (ftrace buffer empty)\n"); |
| 7941 | else |
| 7942 | printk(KERN_TRACE "---------------------------------\n"); |
| 7943 | |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 7944 | out_enable: |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 7945 | tr->trace_flags |= old_userobj; |
Frederic Weisbecker | cf586b6 | 2009-03-22 05:04:35 +0100 | [diff] [blame] | 7946 | |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 7947 | for_each_tracing_cpu(cpu) { |
| 7948 | atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled); |
Frederic Weisbecker | cf586b6 | 2009-03-22 05:04:35 +0100 | [diff] [blame] | 7949 | } |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 7950 | atomic_dec(&dump_running); |
Steven Rostedt | cd891ae | 2009-04-28 11:39:34 -0400 | [diff] [blame] | 7951 | local_irq_restore(flags); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 7952 | } |
Paul E. McKenney | a8eecf2 | 2011-10-02 11:01:15 -0700 | [diff] [blame] | 7953 | EXPORT_SYMBOL_GPL(ftrace_dump); |
Frederic Weisbecker | cf586b6 | 2009-03-22 05:04:35 +0100 | [diff] [blame] | 7954 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7955 | __init static int tracer_alloc_buffers(void) |
| 7956 | { |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 7957 | int ring_buf_size; |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 7958 | int ret = -ENOMEM; |
| 7959 | |
Steven Rostedt (Red Hat) | b5e87c0 | 2015-09-29 18:13:33 -0400 | [diff] [blame] | 7960 | /* |
| 7961 | * Make sure we don't accidently add more trace options |
| 7962 | * than we have bits for. |
| 7963 | */ |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 7964 | BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE); |
Steven Rostedt (Red Hat) | b5e87c0 | 2015-09-29 18:13:33 -0400 | [diff] [blame] | 7965 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 7966 | if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL)) |
| 7967 | goto out; |
| 7968 | |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 7969 | if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL)) |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 7970 | goto out_free_buffer_mask; |
| 7971 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 7972 | /* Only allocate trace_printk buffers if a trace_printk exists */ |
| 7973 | if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt) |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 7974 | /* Must be called before global_trace.buffer is allocated */ |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 7975 | trace_printk_init_buffers(); |
| 7976 | |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 7977 | /* To save memory, keep the ring buffer size to its minimum */ |
| 7978 | if (ring_buffer_expanded) |
| 7979 | ring_buf_size = trace_buf_size; |
| 7980 | else |
| 7981 | ring_buf_size = 1; |
| 7982 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 7983 | cpumask_copy(tracing_buffer_mask, cpu_possible_mask); |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 7984 | cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7985 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7986 | raw_spin_lock_init(&global_trace.start_lock); |
| 7987 | |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 7988 | /* Used for event triggers */ |
| 7989 | temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE); |
| 7990 | if (!temp_buffer) |
| 7991 | goto out_free_cpumask; |
| 7992 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 7993 | if (trace_create_savedcmd() < 0) |
| 7994 | goto out_free_temp_buffer; |
| 7995 | |
Steven Rostedt | ab46428 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 7996 | /* TODO: make the number of buffers hot pluggable with CPUS */ |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 7997 | if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) { |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 7998 | printk(KERN_ERR "tracer: failed to allocate ring buffer!\n"); |
| 7999 | WARN_ON(1); |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 8000 | goto out_free_savedcmd; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 8001 | } |
Steven Rostedt | a7603ff | 2012-08-06 16:24:11 -0400 | [diff] [blame] | 8002 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 8003 | if (global_trace.buffer_disabled) |
| 8004 | tracing_off(); |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 8005 | |
Steven Rostedt | e1e232c | 2014-02-10 23:38:46 -0500 | [diff] [blame] | 8006 | if (trace_boot_clock) { |
| 8007 | ret = tracing_set_clock(&global_trace, trace_boot_clock); |
| 8008 | if (ret < 0) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 8009 | pr_warn("Trace clock %s not defined, going back to default\n", |
| 8010 | trace_boot_clock); |
Steven Rostedt | e1e232c | 2014-02-10 23:38:46 -0500 | [diff] [blame] | 8011 | } |
| 8012 | |
Steven Rostedt (Red Hat) | ca16431 | 2013-05-23 11:51:10 -0400 | [diff] [blame] | 8013 | /* |
| 8014 | * register_tracer() might reference current_trace, so it |
| 8015 | * needs to be set before we register anything. This is |
| 8016 | * just a bootstrap of current_trace anyway. |
| 8017 | */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8018 | global_trace.current_trace = &nop_trace; |
| 8019 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 8020 | global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
| 8021 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 8022 | ftrace_init_global_array_ops(&global_trace); |
| 8023 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 8024 | init_trace_flags_index(&global_trace); |
| 8025 | |
Steven Rostedt (Red Hat) | ca16431 | 2013-05-23 11:51:10 -0400 | [diff] [blame] | 8026 | register_tracer(&nop_trace); |
| 8027 | |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 8028 | /* All seems OK, enable tracing */ |
| 8029 | tracing_disabled = 0; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 8030 | |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 8031 | atomic_notifier_chain_register(&panic_notifier_list, |
| 8032 | &trace_panic_notifier); |
| 8033 | |
| 8034 | register_die_notifier(&trace_die_notifier); |
Frederic Weisbecker | 2fc1dfb | 2009-03-16 01:45:03 +0100 | [diff] [blame] | 8035 | |
Steven Rostedt | ae63b31e | 2012-05-03 23:09:03 -0400 | [diff] [blame] | 8036 | global_trace.flags = TRACE_ARRAY_FL_GLOBAL; |
| 8037 | |
| 8038 | INIT_LIST_HEAD(&global_trace.systems); |
| 8039 | INIT_LIST_HEAD(&global_trace.events); |
| 8040 | list_add(&global_trace.list, &ftrace_trace_arrays); |
| 8041 | |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 8042 | apply_trace_boot_options(); |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 8043 | |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 8044 | register_snapshot_cmd(); |
| 8045 | |
Frederic Weisbecker | 2fc1dfb | 2009-03-16 01:45:03 +0100 | [diff] [blame] | 8046 | return 0; |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 8047 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 8048 | out_free_savedcmd: |
| 8049 | free_saved_cmdlines_buffer(savedcmd); |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 8050 | out_free_temp_buffer: |
| 8051 | ring_buffer_free(temp_buffer); |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 8052 | out_free_cpumask: |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 8053 | free_cpumask_var(global_trace.tracing_cpumask); |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 8054 | out_free_buffer_mask: |
| 8055 | free_cpumask_var(tracing_buffer_mask); |
| 8056 | out: |
| 8057 | return ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 8058 | } |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 8059 | |
Steven Rostedt (Red Hat) | 5f893b2 | 2014-12-12 20:05:10 -0500 | [diff] [blame] | 8060 | void __init trace_init(void) |
| 8061 | { |
Steven Rostedt (Red Hat) | 0daa2302 | 2014-12-12 22:27:10 -0500 | [diff] [blame] | 8062 | if (tracepoint_printk) { |
| 8063 | tracepoint_print_iter = |
| 8064 | kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL); |
| 8065 | if (WARN_ON(!tracepoint_print_iter)) |
| 8066 | tracepoint_printk = 0; |
Steven Rostedt (Red Hat) | 42391745 | 2016-11-23 15:52:45 -0500 | [diff] [blame] | 8067 | else |
| 8068 | static_key_enable(&tracepoint_printk_key.key); |
Steven Rostedt (Red Hat) | 0daa2302 | 2014-12-12 22:27:10 -0500 | [diff] [blame] | 8069 | } |
Steven Rostedt (Red Hat) | 5f893b2 | 2014-12-12 20:05:10 -0500 | [diff] [blame] | 8070 | tracer_alloc_buffers(); |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 8071 | trace_event_init(); |
Steven Rostedt (Red Hat) | 5f893b2 | 2014-12-12 20:05:10 -0500 | [diff] [blame] | 8072 | } |
| 8073 | |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 8074 | __init static int clear_boot_tracer(void) |
| 8075 | { |
| 8076 | /* |
| 8077 | * The default tracer at boot buffer is an init section. |
| 8078 | * This function is called in lateinit. If we did not |
| 8079 | * find the boot tracer, then clear it out, to prevent |
| 8080 | * later registration from accessing the buffer that is |
| 8081 | * about to be freed. |
| 8082 | */ |
| 8083 | if (!default_bootup_tracer) |
| 8084 | return 0; |
| 8085 | |
| 8086 | printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n", |
| 8087 | default_bootup_tracer); |
| 8088 | default_bootup_tracer = NULL; |
| 8089 | |
| 8090 | return 0; |
| 8091 | } |
| 8092 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8093 | fs_initcall(tracer_init_tracefs); |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 8094 | late_initcall(clear_boot_tracer); |