]>
Commit | Line | Data |
---|---|---|
bc0c38d1 SR |
1 | /* |
2 | * ring buffer based function tracer | |
3 | * | |
4 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> | |
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 | |
12 | * Copyright (C) 2004 William Lee Irwin III | |
13 | */ | |
2cadf913 | 14 | #include <linux/ring_buffer.h> |
273b281f | 15 | #include <generated/utsrelease.h> |
2cadf913 SR |
16 | #include <linux/stacktrace.h> |
17 | #include <linux/writeback.h> | |
bc0c38d1 SR |
18 | #include <linux/kallsyms.h> |
19 | #include <linux/seq_file.h> | |
3f5a54e3 | 20 | #include <linux/notifier.h> |
2cadf913 | 21 | #include <linux/irqflags.h> |
bc0c38d1 | 22 | #include <linux/debugfs.h> |
4c11d7ae | 23 | #include <linux/pagemap.h> |
bc0c38d1 SR |
24 | #include <linux/hardirq.h> |
25 | #include <linux/linkage.h> | |
26 | #include <linux/uaccess.h> | |
2cadf913 | 27 | #include <linux/kprobes.h> |
bc0c38d1 SR |
28 | #include <linux/ftrace.h> |
29 | #include <linux/module.h> | |
30 | #include <linux/percpu.h> | |
2cadf913 | 31 | #include <linux/splice.h> |
3f5a54e3 | 32 | #include <linux/kdebug.h> |
5f0c6c03 | 33 | #include <linux/string.h> |
7e53bd42 | 34 | #include <linux/rwsem.h> |
5a0e3ad6 | 35 | #include <linux/slab.h> |
bc0c38d1 SR |
36 | #include <linux/ctype.h> |
37 | #include <linux/init.h> | |
2a2cc8f7 | 38 | #include <linux/poll.h> |
bc0c38d1 | 39 | #include <linux/fs.h> |
86387f7e | 40 | |
bc0c38d1 | 41 | #include "trace.h" |
f0868d1e | 42 | #include "trace_output.h" |
bc0c38d1 | 43 | |
73c5162a SR |
44 | /* |
45 | * On boot up, the ring buffer is set to the minimum size, so that | |
46 | * we do not waste memory on systems that are not using tracing. | |
47 | */ | |
020e5f85 | 48 | int ring_buffer_expanded; |
73c5162a | 49 | |
8e1b82e0 FW |
50 | /* |
51 | * We need to change this state when a selftest is running. | |
ff32504f FW |
52 | * A selftest will lurk into the ring-buffer to count the |
53 | * entries inserted during the selftest although some concurrent | |
5e1607a0 | 54 | * insertions into the ring-buffer such as trace_printk could occurred |
ff32504f FW |
55 | * at the same time, giving false positive or negative results. |
56 | */ | |
8e1b82e0 | 57 | static bool __read_mostly tracing_selftest_running; |
ff32504f | 58 | |
b2821ae6 SR |
59 | /* |
60 | * If a tracer is running, we do not want to run SELFTEST. | |
61 | */ | |
020e5f85 | 62 | bool __read_mostly tracing_selftest_disabled; |
b2821ae6 | 63 | |
adf9f195 FW |
64 | /* For tracers that don't implement custom flags */ |
65 | static struct tracer_opt dummy_tracer_opt[] = { | |
66 | { } | |
67 | }; | |
68 | ||
69 | static struct tracer_flags dummy_tracer_flags = { | |
70 | .val = 0, | |
71 | .opts = dummy_tracer_opt | |
72 | }; | |
73 | ||
74 | static int dummy_set_flag(u32 old_flags, u32 bit, int set) | |
75 | { | |
76 | return 0; | |
77 | } | |
0f048701 SR |
78 | |
79 | /* | |
80 | * Kill all tracing for good (never come back). | |
81 | * It is initialized to 1 but will turn to zero if the initialization | |
82 | * of the tracer is successful. But that is the only place that sets | |
83 | * this back to zero. | |
84 | */ | |
4fd27358 | 85 | static int tracing_disabled = 1; |
0f048701 | 86 | |
9288f99a | 87 | DEFINE_PER_CPU(int, ftrace_cpu_disabled); |
d769041f SR |
88 | |
89 | static inline void ftrace_disable_cpu(void) | |
90 | { | |
91 | preempt_disable(); | |
dd17c8f7 | 92 | __this_cpu_inc(ftrace_cpu_disabled); |
d769041f SR |
93 | } |
94 | ||
95 | static inline void ftrace_enable_cpu(void) | |
96 | { | |
dd17c8f7 | 97 | __this_cpu_dec(ftrace_cpu_disabled); |
d769041f SR |
98 | preempt_enable(); |
99 | } | |
100 | ||
955b61e5 | 101 | cpumask_var_t __read_mostly tracing_buffer_mask; |
ab46428c | 102 | |
944ac425 SR |
103 | /* |
104 | * ftrace_dump_on_oops - variable to dump ftrace buffer on oops | |
105 | * | |
106 | * If there is an oops (or kernel panic) and the ftrace_dump_on_oops | |
107 | * is set, then ftrace_dump is called. This will output the contents | |
108 | * of the ftrace buffers to the console. This is very useful for | |
109 | * capturing traces that lead to crashes and outputing it to a | |
110 | * serial console. | |
111 | * | |
112 | * It is default off, but you can enable it with either specifying | |
113 | * "ftrace_dump_on_oops" in the kernel command line, or setting | |
cecbca96 FW |
114 | * /proc/sys/kernel/ftrace_dump_on_oops |
115 | * Set 1 if you want to dump buffers of all CPUs | |
116 | * Set 2 if you want to dump the buffer of the CPU that triggered oops | |
944ac425 | 117 | */ |
cecbca96 FW |
118 | |
119 | enum ftrace_dump_mode ftrace_dump_on_oops; | |
944ac425 | 120 | |
b2821ae6 SR |
121 | static int tracing_set_tracer(const char *buf); |
122 | ||
ee6c2c1b LZ |
123 | #define MAX_TRACER_SIZE 100 |
124 | static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata; | |
b2821ae6 | 125 | static char *default_bootup_tracer; |
d9e54076 | 126 | |
1beee96b | 127 | static int __init set_cmdline_ftrace(char *str) |
d9e54076 | 128 | { |
ee6c2c1b | 129 | strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE); |
b2821ae6 | 130 | default_bootup_tracer = bootup_tracer_buf; |
73c5162a SR |
131 | /* We are using ftrace early, expand it */ |
132 | ring_buffer_expanded = 1; | |
d9e54076 PZ |
133 | return 1; |
134 | } | |
1beee96b | 135 | __setup("ftrace=", set_cmdline_ftrace); |
d9e54076 | 136 | |
944ac425 SR |
137 | static int __init set_ftrace_dump_on_oops(char *str) |
138 | { | |
cecbca96 FW |
139 | if (*str++ != '=' || !*str) { |
140 | ftrace_dump_on_oops = DUMP_ALL; | |
141 | return 1; | |
142 | } | |
143 | ||
144 | if (!strcmp("orig_cpu", str)) { | |
145 | ftrace_dump_on_oops = DUMP_ORIG; | |
146 | return 1; | |
147 | } | |
148 | ||
149 | return 0; | |
944ac425 SR |
150 | } |
151 | __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops); | |
60a11774 | 152 | |
cf8e3474 | 153 | unsigned long long ns2usecs(cycle_t nsec) |
bc0c38d1 SR |
154 | { |
155 | nsec += 500; | |
156 | do_div(nsec, 1000); | |
157 | return nsec; | |
158 | } | |
159 | ||
4fcdae83 SR |
160 | /* |
161 | * The global_trace is the descriptor that holds the tracing | |
162 | * buffers for the live tracing. For each CPU, it contains | |
163 | * a link list of pages that will store trace entries. The | |
164 | * page descriptor of the pages in the memory is used to hold | |
165 | * the link list by linking the lru item in the page descriptor | |
166 | * to each of the pages in the buffer per CPU. | |
167 | * | |
168 | * For each active CPU there is a data field that holds the | |
169 | * pages for the buffer for that CPU. Each CPU has the same number | |
170 | * of pages allocated for its buffer. | |
171 | */ | |
bc0c38d1 SR |
172 | static struct trace_array global_trace; |
173 | ||
174 | static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu); | |
175 | ||
e77405ad SR |
176 | int filter_current_check_discard(struct ring_buffer *buffer, |
177 | struct ftrace_event_call *call, void *rec, | |
eb02ce01 TZ |
178 | struct ring_buffer_event *event) |
179 | { | |
e77405ad | 180 | return filter_check_discard(call, rec, buffer, event); |
eb02ce01 | 181 | } |
17c873ec | 182 | EXPORT_SYMBOL_GPL(filter_current_check_discard); |
eb02ce01 | 183 | |
37886f6a SR |
184 | cycle_t ftrace_now(int cpu) |
185 | { | |
186 | u64 ts; | |
187 | ||
188 | /* Early boot up does not have a buffer yet */ | |
189 | if (!global_trace.buffer) | |
190 | return trace_clock_local(); | |
191 | ||
192 | ts = ring_buffer_time_stamp(global_trace.buffer, cpu); | |
193 | ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts); | |
194 | ||
195 | return ts; | |
196 | } | |
bc0c38d1 | 197 | |
4fcdae83 SR |
198 | /* |
199 | * The max_tr is used to snapshot the global_trace when a maximum | |
200 | * latency is reached. Some tracers will use this to store a maximum | |
201 | * trace while it continues examining live traces. | |
202 | * | |
203 | * The buffers for the max_tr are set up the same as the global_trace. | |
204 | * When a snapshot is taken, the link list of the max_tr is swapped | |
205 | * with the link list of the global_trace and the buffers are reset for | |
206 | * the global_trace so the tracing can continue. | |
207 | */ | |
bc0c38d1 SR |
208 | static struct trace_array max_tr; |
209 | ||
9705f69e | 210 | static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data); |
bc0c38d1 | 211 | |
4fcdae83 | 212 | /* tracer_enabled is used to toggle activation of a tracer */ |
26994ead | 213 | static int tracer_enabled = 1; |
4fcdae83 | 214 | |
9036990d SR |
215 | /** |
216 | * tracing_is_enabled - return tracer_enabled status | |
217 | * | |
218 | * This function is used by other tracers to know the status | |
219 | * of the tracer_enabled flag. Tracers may use this function | |
220 | * to know if it should enable their features when starting | |
221 | * up. See irqsoff tracer for an example (start_irqsoff_tracer). | |
222 | */ | |
223 | int tracing_is_enabled(void) | |
224 | { | |
225 | return tracer_enabled; | |
226 | } | |
227 | ||
4fcdae83 | 228 | /* |
3928a8a2 SR |
229 | * trace_buf_size is the size in bytes that is allocated |
230 | * for a buffer. Note, the number of bytes is always rounded | |
231 | * to page size. | |
3f5a54e3 SR |
232 | * |
233 | * This number is purposely set to a low number of 16384. | |
234 | * If the dump on oops happens, it will be much appreciated | |
235 | * to not have to wait for all that output. Anyway this can be | |
236 | * boot time and run time configurable. | |
4fcdae83 | 237 | */ |
3928a8a2 | 238 | #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */ |
3f5a54e3 | 239 | |
3928a8a2 | 240 | static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT; |
bc0c38d1 | 241 | |
4fcdae83 | 242 | /* trace_types holds a link list of available tracers. */ |
bc0c38d1 | 243 | static struct tracer *trace_types __read_mostly; |
4fcdae83 SR |
244 | |
245 | /* current_trace points to the tracer that is currently active */ | |
bc0c38d1 | 246 | static struct tracer *current_trace __read_mostly; |
4fcdae83 | 247 | |
4fcdae83 SR |
248 | /* |
249 | * trace_types_lock is used to protect the trace_types list. | |
4fcdae83 | 250 | */ |
bc0c38d1 | 251 | static DEFINE_MUTEX(trace_types_lock); |
4fcdae83 | 252 | |
7e53bd42 LJ |
253 | /* |
254 | * serialize the access of the ring buffer | |
255 | * | |
256 | * ring buffer serializes readers, but it is low level protection. | |
257 | * The validity of the events (which returns by ring_buffer_peek() ..etc) | |
258 | * are not protected by ring buffer. | |
259 | * | |
260 | * The content of events may become garbage if we allow other process consumes | |
261 | * these events concurrently: | |
262 | * A) the page of the consumed events may become a normal page | |
263 | * (not reader page) in ring buffer, and this page will be rewrited | |
264 | * by events producer. | |
265 | * B) The page of the consumed events may become a page for splice_read, | |
266 | * and this page will be returned to system. | |
267 | * | |
268 | * These primitives allow multi process access to different cpu ring buffer | |
269 | * concurrently. | |
270 | * | |
271 | * These primitives don't distinguish read-only and read-consume access. | |
272 | * Multi read-only access are also serialized. | |
273 | */ | |
274 | ||
275 | #ifdef CONFIG_SMP | |
276 | static DECLARE_RWSEM(all_cpu_access_lock); | |
277 | static DEFINE_PER_CPU(struct mutex, cpu_access_lock); | |
278 | ||
279 | static inline void trace_access_lock(int cpu) | |
280 | { | |
281 | if (cpu == TRACE_PIPE_ALL_CPU) { | |
282 | /* gain it for accessing the whole ring buffer. */ | |
283 | down_write(&all_cpu_access_lock); | |
284 | } else { | |
285 | /* gain it for accessing a cpu ring buffer. */ | |
286 | ||
287 | /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */ | |
288 | down_read(&all_cpu_access_lock); | |
289 | ||
290 | /* Secondly block other access to this @cpu ring buffer. */ | |
291 | mutex_lock(&per_cpu(cpu_access_lock, cpu)); | |
292 | } | |
293 | } | |
294 | ||
295 | static inline void trace_access_unlock(int cpu) | |
296 | { | |
297 | if (cpu == TRACE_PIPE_ALL_CPU) { | |
298 | up_write(&all_cpu_access_lock); | |
299 | } else { | |
300 | mutex_unlock(&per_cpu(cpu_access_lock, cpu)); | |
301 | up_read(&all_cpu_access_lock); | |
302 | } | |
303 | } | |
304 | ||
305 | static inline void trace_access_lock_init(void) | |
306 | { | |
307 | int cpu; | |
308 | ||
309 | for_each_possible_cpu(cpu) | |
310 | mutex_init(&per_cpu(cpu_access_lock, cpu)); | |
311 | } | |
312 | ||
313 | #else | |
314 | ||
315 | static DEFINE_MUTEX(access_lock); | |
316 | ||
317 | static inline void trace_access_lock(int cpu) | |
318 | { | |
319 | (void)cpu; | |
320 | mutex_lock(&access_lock); | |
321 | } | |
322 | ||
323 | static inline void trace_access_unlock(int cpu) | |
324 | { | |
325 | (void)cpu; | |
326 | mutex_unlock(&access_lock); | |
327 | } | |
328 | ||
329 | static inline void trace_access_lock_init(void) | |
330 | { | |
331 | } | |
332 | ||
333 | #endif | |
334 | ||
4fcdae83 | 335 | /* trace_wait is a waitqueue for tasks blocked on trace_poll */ |
4e655519 IM |
336 | static DECLARE_WAIT_QUEUE_HEAD(trace_wait); |
337 | ||
ee6bce52 | 338 | /* trace_flags holds trace_options default values */ |
12ef7d44 | 339 | unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | |
a2a16d6a | 340 | TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME | |
77271ce4 SR |
341 | TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | |
342 | TRACE_ITER_IRQ_INFO; | |
4e655519 | 343 | |
b8de7bd1 | 344 | static int trace_stop_count; |
5389f6fa | 345 | static DEFINE_RAW_SPINLOCK(tracing_start_lock); |
b8de7bd1 | 346 | |
e7e2ee89 VN |
347 | static void wakeup_work_handler(struct work_struct *work) |
348 | { | |
349 | wake_up(&trace_wait); | |
350 | } | |
351 | ||
352 | static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler); | |
353 | ||
499e5470 SR |
354 | /** |
355 | * tracing_on - enable tracing buffers | |
356 | * | |
357 | * This function enables tracing buffers that may have been | |
358 | * disabled with tracing_off. | |
359 | */ | |
360 | void tracing_on(void) | |
361 | { | |
362 | if (global_trace.buffer) | |
363 | ring_buffer_record_on(global_trace.buffer); | |
364 | /* | |
365 | * This flag is only looked at when buffers haven't been | |
366 | * allocated yet. We don't really care about the race | |
367 | * between setting this flag and actually turning | |
368 | * on the buffer. | |
369 | */ | |
370 | global_trace.buffer_disabled = 0; | |
371 | } | |
372 | EXPORT_SYMBOL_GPL(tracing_on); | |
373 | ||
374 | /** | |
375 | * tracing_off - turn off tracing buffers | |
376 | * | |
377 | * This function stops the tracing buffers from recording data. | |
378 | * It does not disable any overhead the tracers themselves may | |
379 | * be causing. This function simply causes all recording to | |
380 | * the ring buffers to fail. | |
381 | */ | |
382 | void tracing_off(void) | |
383 | { | |
384 | if (global_trace.buffer) | |
385 | ring_buffer_record_on(global_trace.buffer); | |
386 | /* | |
387 | * This flag is only looked at when buffers haven't been | |
388 | * allocated yet. We don't really care about the race | |
389 | * between setting this flag and actually turning | |
390 | * on the buffer. | |
391 | */ | |
392 | global_trace.buffer_disabled = 1; | |
393 | } | |
394 | EXPORT_SYMBOL_GPL(tracing_off); | |
395 | ||
396 | /** | |
397 | * tracing_is_on - show state of ring buffers enabled | |
398 | */ | |
399 | int tracing_is_on(void) | |
400 | { | |
401 | if (global_trace.buffer) | |
402 | return ring_buffer_record_is_on(global_trace.buffer); | |
403 | return !global_trace.buffer_disabled; | |
404 | } | |
405 | EXPORT_SYMBOL_GPL(tracing_is_on); | |
406 | ||
4fcdae83 SR |
407 | /** |
408 | * trace_wake_up - wake up tasks waiting for trace input | |
409 | * | |
e7e2ee89 VN |
410 | * Schedules a delayed work to wake up any task that is blocked on the |
411 | * trace_wait queue. These is used with trace_poll for tasks polling the | |
412 | * trace. | |
4fcdae83 | 413 | */ |
4e655519 IM |
414 | void trace_wake_up(void) |
415 | { | |
e7e2ee89 | 416 | const unsigned long delay = msecs_to_jiffies(2); |
89f19f04 AM |
417 | |
418 | if (trace_flags & TRACE_ITER_BLOCK) | |
419 | return; | |
e7e2ee89 | 420 | schedule_delayed_work(&wakeup_work, delay); |
4e655519 | 421 | } |
bc0c38d1 | 422 | |
3928a8a2 | 423 | static int __init set_buf_size(char *str) |
bc0c38d1 | 424 | { |
3928a8a2 | 425 | unsigned long buf_size; |
c6caeeb1 | 426 | |
bc0c38d1 SR |
427 | if (!str) |
428 | return 0; | |
9d612bef | 429 | buf_size = memparse(str, &str); |
c6caeeb1 | 430 | /* nr_entries can not be zero */ |
9d612bef | 431 | if (buf_size == 0) |
c6caeeb1 | 432 | return 0; |
3928a8a2 | 433 | trace_buf_size = buf_size; |
bc0c38d1 SR |
434 | return 1; |
435 | } | |
3928a8a2 | 436 | __setup("trace_buf_size=", set_buf_size); |
bc0c38d1 | 437 | |
0e950173 TB |
438 | static int __init set_tracing_thresh(char *str) |
439 | { | |
440 | unsigned long threshhold; | |
441 | int ret; | |
442 | ||
443 | if (!str) | |
444 | return 0; | |
445 | ret = strict_strtoul(str, 0, &threshhold); | |
446 | if (ret < 0) | |
447 | return 0; | |
448 | tracing_thresh = threshhold * 1000; | |
449 | return 1; | |
450 | } | |
451 | __setup("tracing_thresh=", set_tracing_thresh); | |
452 | ||
57f50be1 SR |
453 | unsigned long nsecs_to_usecs(unsigned long nsecs) |
454 | { | |
455 | return nsecs / 1000; | |
456 | } | |
457 | ||
4fcdae83 | 458 | /* These must match the bit postions in trace_iterator_flags */ |
bc0c38d1 SR |
459 | static const char *trace_options[] = { |
460 | "print-parent", | |
461 | "sym-offset", | |
462 | "sym-addr", | |
463 | "verbose", | |
f9896bf3 | 464 | "raw", |
5e3ca0ec | 465 | "hex", |
cb0f12aa | 466 | "bin", |
2a2cc8f7 | 467 | "block", |
86387f7e | 468 | "stacktrace", |
5e1607a0 | 469 | "trace_printk", |
b2a866f9 | 470 | "ftrace_preempt", |
9f029e83 | 471 | "branch", |
12ef7d44 | 472 | "annotate", |
02b67518 | 473 | "userstacktrace", |
b54d3de9 | 474 | "sym-userobj", |
66896a85 | 475 | "printk-msg-only", |
c4a8e8be | 476 | "context-info", |
c032ef64 | 477 | "latency-format", |
be6f164a | 478 | "sleep-time", |
a2a16d6a | 479 | "graph-time", |
e870e9a1 | 480 | "record-cmd", |
750912fa | 481 | "overwrite", |
cf30cf67 | 482 | "disable_on_free", |
77271ce4 | 483 | "irq-info", |
bc0c38d1 SR |
484 | NULL |
485 | }; | |
486 | ||
5079f326 Z |
487 | static struct { |
488 | u64 (*func)(void); | |
489 | const char *name; | |
490 | } trace_clocks[] = { | |
491 | { trace_clock_local, "local" }, | |
492 | { trace_clock_global, "global" }, | |
6249687f | 493 | { trace_clock_counter, "counter" }, |
5079f326 Z |
494 | }; |
495 | ||
496 | int trace_clock_id; | |
497 | ||
b63f39ea | 498 | /* |
499 | * trace_parser_get_init - gets the buffer for trace parser | |
500 | */ | |
501 | int trace_parser_get_init(struct trace_parser *parser, int size) | |
502 | { | |
503 | memset(parser, 0, sizeof(*parser)); | |
504 | ||
505 | parser->buffer = kmalloc(size, GFP_KERNEL); | |
506 | if (!parser->buffer) | |
507 | return 1; | |
508 | ||
509 | parser->size = size; | |
510 | return 0; | |
511 | } | |
512 | ||
513 | /* | |
514 | * trace_parser_put - frees the buffer for trace parser | |
515 | */ | |
516 | void trace_parser_put(struct trace_parser *parser) | |
517 | { | |
518 | kfree(parser->buffer); | |
519 | } | |
520 | ||
521 | /* | |
522 | * trace_get_user - reads the user input string separated by space | |
523 | * (matched by isspace(ch)) | |
524 | * | |
525 | * For each string found the 'struct trace_parser' is updated, | |
526 | * and the function returns. | |
527 | * | |
528 | * Returns number of bytes read. | |
529 | * | |
530 | * See kernel/trace/trace.h for 'struct trace_parser' details. | |
531 | */ | |
532 | int trace_get_user(struct trace_parser *parser, const char __user *ubuf, | |
533 | size_t cnt, loff_t *ppos) | |
534 | { | |
535 | char ch; | |
536 | size_t read = 0; | |
537 | ssize_t ret; | |
538 | ||
539 | if (!*ppos) | |
540 | trace_parser_clear(parser); | |
541 | ||
542 | ret = get_user(ch, ubuf++); | |
543 | if (ret) | |
544 | goto out; | |
545 | ||
546 | read++; | |
547 | cnt--; | |
548 | ||
549 | /* | |
550 | * The parser is not finished with the last write, | |
551 | * continue reading the user input without skipping spaces. | |
552 | */ | |
553 | if (!parser->cont) { | |
554 | /* skip white space */ | |
555 | while (cnt && isspace(ch)) { | |
556 | ret = get_user(ch, ubuf++); | |
557 | if (ret) | |
558 | goto out; | |
559 | read++; | |
560 | cnt--; | |
561 | } | |
562 | ||
563 | /* only spaces were written */ | |
564 | if (isspace(ch)) { | |
565 | *ppos += read; | |
566 | ret = read; | |
567 | goto out; | |
568 | } | |
569 | ||
570 | parser->idx = 0; | |
571 | } | |
572 | ||
573 | /* read the non-space input */ | |
574 | while (cnt && !isspace(ch)) { | |
3c235a33 | 575 | if (parser->idx < parser->size - 1) |
b63f39ea | 576 | parser->buffer[parser->idx++] = ch; |
577 | else { | |
578 | ret = -EINVAL; | |
579 | goto out; | |
580 | } | |
581 | ret = get_user(ch, ubuf++); | |
582 | if (ret) | |
583 | goto out; | |
584 | read++; | |
585 | cnt--; | |
586 | } | |
587 | ||
588 | /* We either got finished input or we have to wait for another call. */ | |
589 | if (isspace(ch)) { | |
590 | parser->buffer[parser->idx] = 0; | |
591 | parser->cont = false; | |
592 | } else { | |
593 | parser->cont = true; | |
594 | parser->buffer[parser->idx++] = ch; | |
595 | } | |
596 | ||
597 | *ppos += read; | |
598 | ret = read; | |
599 | ||
600 | out: | |
601 | return ret; | |
602 | } | |
603 | ||
6c6c2796 PP |
604 | ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt) |
605 | { | |
606 | int len; | |
607 | int ret; | |
608 | ||
2dc5d12b SR |
609 | if (!cnt) |
610 | return 0; | |
611 | ||
6c6c2796 PP |
612 | if (s->len <= s->readpos) |
613 | return -EBUSY; | |
614 | ||
615 | len = s->len - s->readpos; | |
616 | if (cnt > len) | |
617 | cnt = len; | |
618 | ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt); | |
2dc5d12b | 619 | if (ret == cnt) |
6c6c2796 PP |
620 | return -EFAULT; |
621 | ||
2dc5d12b SR |
622 | cnt -= ret; |
623 | ||
e74da523 | 624 | s->readpos += cnt; |
6c6c2796 | 625 | return cnt; |
214023c3 SR |
626 | } |
627 | ||
b8b94265 | 628 | static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt) |
3c56819b EGM |
629 | { |
630 | int len; | |
631 | void *ret; | |
632 | ||
633 | if (s->len <= s->readpos) | |
634 | return -EBUSY; | |
635 | ||
636 | len = s->len - s->readpos; | |
637 | if (cnt > len) | |
638 | cnt = len; | |
639 | ret = memcpy(buf, s->buffer + s->readpos, cnt); | |
640 | if (!ret) | |
641 | return -EFAULT; | |
642 | ||
e74da523 | 643 | s->readpos += cnt; |
3c56819b EGM |
644 | return cnt; |
645 | } | |
646 | ||
5d4a9dba SR |
647 | /* |
648 | * ftrace_max_lock is used to protect the swapping of buffers | |
649 | * when taking a max snapshot. The buffers themselves are | |
650 | * protected by per_cpu spinlocks. But the action of the swap | |
651 | * needs its own lock. | |
652 | * | |
445c8951 | 653 | * This is defined as a arch_spinlock_t in order to help |
5d4a9dba SR |
654 | * with performance when lockdep debugging is enabled. |
655 | * | |
656 | * It is also used in other places outside the update_max_tr | |
657 | * so it needs to be defined outside of the | |
658 | * CONFIG_TRACER_MAX_TRACE. | |
659 | */ | |
445c8951 | 660 | static arch_spinlock_t ftrace_max_lock = |
edc35bd7 | 661 | (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
5d4a9dba | 662 | |
0e950173 TB |
663 | unsigned long __read_mostly tracing_thresh; |
664 | ||
5d4a9dba SR |
665 | #ifdef CONFIG_TRACER_MAX_TRACE |
666 | unsigned long __read_mostly tracing_max_latency; | |
5d4a9dba SR |
667 | |
668 | /* | |
669 | * Copy the new maximum trace into the separate maximum-trace | |
670 | * structure. (this way the maximum trace is permanently saved, | |
671 | * for later retrieval via /sys/kernel/debug/tracing/latency_trace) | |
672 | */ | |
673 | static void | |
674 | __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) | |
675 | { | |
676 | struct trace_array_cpu *data = tr->data[cpu]; | |
1acaa1b2 | 677 | struct trace_array_cpu *max_data; |
5d4a9dba SR |
678 | |
679 | max_tr.cpu = cpu; | |
680 | max_tr.time_start = data->preempt_timestamp; | |
681 | ||
8248ac05 SR |
682 | max_data = max_tr.data[cpu]; |
683 | max_data->saved_latency = tracing_max_latency; | |
684 | max_data->critical_start = data->critical_start; | |
685 | max_data->critical_end = data->critical_end; | |
5d4a9dba | 686 | |
1acaa1b2 | 687 | memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN); |
8248ac05 SR |
688 | max_data->pid = tsk->pid; |
689 | max_data->uid = task_uid(tsk); | |
690 | max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO; | |
691 | max_data->policy = tsk->policy; | |
692 | max_data->rt_priority = tsk->rt_priority; | |
5d4a9dba SR |
693 | |
694 | /* record this tasks comm */ | |
695 | tracing_record_cmdline(tsk); | |
696 | } | |
697 | ||
4fcdae83 SR |
698 | /** |
699 | * update_max_tr - snapshot all trace buffers from global_trace to max_tr | |
700 | * @tr: tracer | |
701 | * @tsk: the task with the latency | |
702 | * @cpu: The cpu that initiated the trace. | |
703 | * | |
704 | * Flip the buffers between the @tr and the max_tr and record information | |
705 | * about which task was the cause of this latency. | |
706 | */ | |
e309b41d | 707 | void |
bc0c38d1 SR |
708 | update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
709 | { | |
3928a8a2 | 710 | struct ring_buffer *buf = tr->buffer; |
bc0c38d1 | 711 | |
b8de7bd1 SR |
712 | if (trace_stop_count) |
713 | return; | |
714 | ||
4c11d7ae | 715 | WARN_ON_ONCE(!irqs_disabled()); |
ef710e10 KM |
716 | if (!current_trace->use_max_tr) { |
717 | WARN_ON_ONCE(1); | |
718 | return; | |
719 | } | |
0199c4e6 | 720 | arch_spin_lock(&ftrace_max_lock); |
3928a8a2 SR |
721 | |
722 | tr->buffer = max_tr.buffer; | |
723 | max_tr.buffer = buf; | |
724 | ||
bc0c38d1 | 725 | __update_max_tr(tr, tsk, cpu); |
0199c4e6 | 726 | arch_spin_unlock(&ftrace_max_lock); |
bc0c38d1 SR |
727 | } |
728 | ||
729 | /** | |
730 | * update_max_tr_single - only copy one trace over, and reset the rest | |
731 | * @tr - tracer | |
732 | * @tsk - task with the latency | |
733 | * @cpu - the cpu of the buffer to copy. | |
4fcdae83 SR |
734 | * |
735 | * Flip the trace of a single CPU buffer between the @tr and the max_tr. | |
bc0c38d1 | 736 | */ |
e309b41d | 737 | void |
bc0c38d1 SR |
738 | update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) |
739 | { | |
3928a8a2 | 740 | int ret; |
bc0c38d1 | 741 | |
b8de7bd1 SR |
742 | if (trace_stop_count) |
743 | return; | |
744 | ||
4c11d7ae | 745 | WARN_ON_ONCE(!irqs_disabled()); |
ef710e10 KM |
746 | if (!current_trace->use_max_tr) { |
747 | WARN_ON_ONCE(1); | |
748 | return; | |
749 | } | |
750 | ||
0199c4e6 | 751 | arch_spin_lock(&ftrace_max_lock); |
bc0c38d1 | 752 | |
d769041f SR |
753 | ftrace_disable_cpu(); |
754 | ||
3928a8a2 SR |
755 | ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu); |
756 | ||
e8165dbb SR |
757 | if (ret == -EBUSY) { |
758 | /* | |
759 | * We failed to swap the buffer due to a commit taking | |
760 | * place on this CPU. We fail to record, but we reset | |
761 | * the max trace buffer (no one writes directly to it) | |
762 | * and flag that it failed. | |
763 | */ | |
764 | trace_array_printk(&max_tr, _THIS_IP_, | |
765 | "Failed to swap buffers due to commit in progress\n"); | |
766 | } | |
767 | ||
d769041f SR |
768 | ftrace_enable_cpu(); |
769 | ||
e8165dbb | 770 | WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY); |
bc0c38d1 SR |
771 | |
772 | __update_max_tr(tr, tsk, cpu); | |
0199c4e6 | 773 | arch_spin_unlock(&ftrace_max_lock); |
bc0c38d1 | 774 | } |
5d4a9dba | 775 | #endif /* CONFIG_TRACER_MAX_TRACE */ |
bc0c38d1 | 776 | |
4fcdae83 SR |
777 | /** |
778 | * register_tracer - register a tracer with the ftrace system. | |
779 | * @type - the plugin for the tracer | |
780 | * | |
781 | * Register a new plugin tracer. | |
782 | */ | |
bc0c38d1 | 783 | int register_tracer(struct tracer *type) |
e7669b8e HE |
784 | __releases(kernel_lock) |
785 | __acquires(kernel_lock) | |
bc0c38d1 SR |
786 | { |
787 | struct tracer *t; | |
bc0c38d1 SR |
788 | int ret = 0; |
789 | ||
790 | if (!type->name) { | |
791 | pr_info("Tracer must have a name\n"); | |
792 | return -1; | |
793 | } | |
794 | ||
24a461d5 | 795 | if (strlen(type->name) >= MAX_TRACER_SIZE) { |
ee6c2c1b LZ |
796 | pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE); |
797 | return -1; | |
798 | } | |
799 | ||
bc0c38d1 | 800 | mutex_lock(&trace_types_lock); |
86fa2f60 | 801 | |
8e1b82e0 FW |
802 | tracing_selftest_running = true; |
803 | ||
bc0c38d1 SR |
804 | for (t = trace_types; t; t = t->next) { |
805 | if (strcmp(type->name, t->name) == 0) { | |
806 | /* already found */ | |
ee6c2c1b | 807 | pr_info("Tracer %s already registered\n", |
bc0c38d1 SR |
808 | type->name); |
809 | ret = -1; | |
810 | goto out; | |
811 | } | |
812 | } | |
813 | ||
adf9f195 FW |
814 | if (!type->set_flag) |
815 | type->set_flag = &dummy_set_flag; | |
816 | if (!type->flags) | |
817 | type->flags = &dummy_tracer_flags; | |
818 | else | |
819 | if (!type->flags->opts) | |
820 | type->flags->opts = dummy_tracer_opt; | |
6eaaa5d5 FW |
821 | if (!type->wait_pipe) |
822 | type->wait_pipe = default_wait_pipe; | |
823 | ||
adf9f195 | 824 | |
60a11774 | 825 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
b2821ae6 | 826 | if (type->selftest && !tracing_selftest_disabled) { |
60a11774 | 827 | struct tracer *saved_tracer = current_trace; |
60a11774 | 828 | struct trace_array *tr = &global_trace; |
ff32504f | 829 | |
60a11774 SR |
830 | /* |
831 | * Run a selftest on this tracer. | |
832 | * Here we reset the trace buffer, and set the current | |
833 | * tracer to be this tracer. The tracer can then run some | |
834 | * internal tracing to verify that everything is in order. | |
835 | * If we fail, we do not register this tracer. | |
836 | */ | |
76f0d073 | 837 | tracing_reset_online_cpus(tr); |
86fa2f60 | 838 | |
60a11774 | 839 | current_trace = type; |
4a0b1665 SR |
840 | |
841 | /* If we expanded the buffers, make sure the max is expanded too */ | |
842 | if (ring_buffer_expanded && type->use_max_tr) | |
843 | ring_buffer_resize(max_tr.buffer, trace_buf_size); | |
844 | ||
60a11774 SR |
845 | /* the test is responsible for initializing and enabling */ |
846 | pr_info("Testing tracer %s: ", type->name); | |
847 | ret = type->selftest(type, tr); | |
848 | /* the test is responsible for resetting too */ | |
849 | current_trace = saved_tracer; | |
60a11774 SR |
850 | if (ret) { |
851 | printk(KERN_CONT "FAILED!\n"); | |
852 | goto out; | |
853 | } | |
1d4db00a | 854 | /* Only reset on passing, to avoid touching corrupted buffers */ |
76f0d073 | 855 | tracing_reset_online_cpus(tr); |
86fa2f60 | 856 | |
4a0b1665 SR |
857 | /* Shrink the max buffer again */ |
858 | if (ring_buffer_expanded && type->use_max_tr) | |
859 | ring_buffer_resize(max_tr.buffer, 1); | |
860 | ||
60a11774 SR |
861 | printk(KERN_CONT "PASSED\n"); |
862 | } | |
863 | #endif | |
864 | ||
bc0c38d1 SR |
865 | type->next = trace_types; |
866 | trace_types = type; | |
60a11774 | 867 | |
bc0c38d1 | 868 | out: |
8e1b82e0 | 869 | tracing_selftest_running = false; |
bc0c38d1 SR |
870 | mutex_unlock(&trace_types_lock); |
871 | ||
dac74940 SR |
872 | if (ret || !default_bootup_tracer) |
873 | goto out_unlock; | |
874 | ||
ee6c2c1b | 875 | if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE)) |
dac74940 SR |
876 | goto out_unlock; |
877 | ||
878 | printk(KERN_INFO "Starting tracer '%s'\n", type->name); | |
879 | /* Do we want this tracer to start on bootup? */ | |
880 | tracing_set_tracer(type->name); | |
881 | default_bootup_tracer = NULL; | |
882 | /* disable other selftests, since this will break it. */ | |
883 | tracing_selftest_disabled = 1; | |
b2821ae6 | 884 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
dac74940 SR |
885 | printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n", |
886 | type->name); | |
b2821ae6 | 887 | #endif |
b2821ae6 | 888 | |
dac74940 | 889 | out_unlock: |
bc0c38d1 SR |
890 | return ret; |
891 | } | |
892 | ||
893 | void unregister_tracer(struct tracer *type) | |
894 | { | |
895 | struct tracer **t; | |
bc0c38d1 SR |
896 | |
897 | mutex_lock(&trace_types_lock); | |
898 | for (t = &trace_types; *t; t = &(*t)->next) { | |
899 | if (*t == type) | |
900 | goto found; | |
901 | } | |
ee6c2c1b | 902 | pr_info("Tracer %s not registered\n", type->name); |
bc0c38d1 SR |
903 | goto out; |
904 | ||
905 | found: | |
906 | *t = (*t)->next; | |
b5db03c4 ACM |
907 | |
908 | if (type == current_trace && tracer_enabled) { | |
909 | tracer_enabled = 0; | |
910 | tracing_stop(); | |
911 | if (current_trace->stop) | |
912 | current_trace->stop(&global_trace); | |
913 | current_trace = &nop_trace; | |
914 | } | |
ee6c2c1b | 915 | out: |
bc0c38d1 SR |
916 | mutex_unlock(&trace_types_lock); |
917 | } | |
918 | ||
283740c6 | 919 | static void __tracing_reset(struct ring_buffer *buffer, int cpu) |
bc0c38d1 | 920 | { |
d769041f | 921 | ftrace_disable_cpu(); |
283740c6 | 922 | ring_buffer_reset_cpu(buffer, cpu); |
d769041f | 923 | ftrace_enable_cpu(); |
bc0c38d1 SR |
924 | } |
925 | ||
f633903a SR |
926 | void tracing_reset(struct trace_array *tr, int cpu) |
927 | { | |
928 | struct ring_buffer *buffer = tr->buffer; | |
929 | ||
930 | ring_buffer_record_disable(buffer); | |
931 | ||
932 | /* Make sure all commits have finished */ | |
933 | synchronize_sched(); | |
283740c6 | 934 | __tracing_reset(buffer, cpu); |
f633903a SR |
935 | |
936 | ring_buffer_record_enable(buffer); | |
937 | } | |
938 | ||
213cc060 PE |
939 | void tracing_reset_online_cpus(struct trace_array *tr) |
940 | { | |
621968cd | 941 | struct ring_buffer *buffer = tr->buffer; |
213cc060 PE |
942 | int cpu; |
943 | ||
621968cd SR |
944 | ring_buffer_record_disable(buffer); |
945 | ||
946 | /* Make sure all commits have finished */ | |
947 | synchronize_sched(); | |
948 | ||
213cc060 PE |
949 | tr->time_start = ftrace_now(tr->cpu); |
950 | ||
951 | for_each_online_cpu(cpu) | |
283740c6 | 952 | __tracing_reset(buffer, cpu); |
621968cd SR |
953 | |
954 | ring_buffer_record_enable(buffer); | |
213cc060 PE |
955 | } |
956 | ||
9456f0fa SR |
957 | void tracing_reset_current(int cpu) |
958 | { | |
959 | tracing_reset(&global_trace, cpu); | |
960 | } | |
961 | ||
962 | void tracing_reset_current_online_cpus(void) | |
963 | { | |
964 | tracing_reset_online_cpus(&global_trace); | |
965 | } | |
966 | ||
bc0c38d1 | 967 | #define SAVED_CMDLINES 128 |
2c7eea4c | 968 | #define NO_CMDLINE_MAP UINT_MAX |
bc0c38d1 SR |
969 | static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1]; |
970 | static unsigned map_cmdline_to_pid[SAVED_CMDLINES]; | |
971 | static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN]; | |
972 | static int cmdline_idx; | |
edc35bd7 | 973 | static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED; |
25b0b44a | 974 | |
25b0b44a | 975 | /* temporary disable recording */ |
4fd27358 | 976 | static atomic_t trace_record_cmdline_disabled __read_mostly; |
bc0c38d1 SR |
977 | |
978 | static void trace_init_cmdlines(void) | |
979 | { | |
2c7eea4c TG |
980 | memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline)); |
981 | memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid)); | |
bc0c38d1 SR |
982 | cmdline_idx = 0; |
983 | } | |
984 | ||
b5130b1e CE |
985 | int is_tracing_stopped(void) |
986 | { | |
987 | return trace_stop_count; | |
988 | } | |
989 | ||
69bb54ec SR |
990 | /** |
991 | * ftrace_off_permanent - disable all ftrace code permanently | |
992 | * | |
993 | * This should only be called when a serious anomally has | |
994 | * been detected. This will turn off the function tracing, | |
995 | * ring buffers, and other tracing utilites. It takes no | |
996 | * locks and can be called from any context. | |
997 | */ | |
998 | void ftrace_off_permanent(void) | |
999 | { | |
1000 | tracing_disabled = 1; | |
1001 | ftrace_stop(); | |
1002 | tracing_off_permanent(); | |
1003 | } | |
1004 | ||
0f048701 SR |
1005 | /** |
1006 | * tracing_start - quick start of the tracer | |
1007 | * | |
1008 | * If tracing is enabled but was stopped by tracing_stop, | |
1009 | * this will start the tracer back up. | |
1010 | */ | |
1011 | void tracing_start(void) | |
1012 | { | |
1013 | struct ring_buffer *buffer; | |
1014 | unsigned long flags; | |
1015 | ||
1016 | if (tracing_disabled) | |
1017 | return; | |
1018 | ||
5389f6fa | 1019 | raw_spin_lock_irqsave(&tracing_start_lock, flags); |
b06a8301 SR |
1020 | if (--trace_stop_count) { |
1021 | if (trace_stop_count < 0) { | |
1022 | /* Someone screwed up their debugging */ | |
1023 | WARN_ON_ONCE(1); | |
1024 | trace_stop_count = 0; | |
1025 | } | |
0f048701 SR |
1026 | goto out; |
1027 | } | |
1028 | ||
a2f80714 SR |
1029 | /* Prevent the buffers from switching */ |
1030 | arch_spin_lock(&ftrace_max_lock); | |
0f048701 SR |
1031 | |
1032 | buffer = global_trace.buffer; | |
1033 | if (buffer) | |
1034 | ring_buffer_record_enable(buffer); | |
1035 | ||
1036 | buffer = max_tr.buffer; | |
1037 | if (buffer) | |
1038 | ring_buffer_record_enable(buffer); | |
1039 | ||
a2f80714 SR |
1040 | arch_spin_unlock(&ftrace_max_lock); |
1041 | ||
0f048701 SR |
1042 | ftrace_start(); |
1043 | out: | |
5389f6fa | 1044 | raw_spin_unlock_irqrestore(&tracing_start_lock, flags); |
0f048701 SR |
1045 | } |
1046 | ||
1047 | /** | |
1048 | * tracing_stop - quick stop of the tracer | |
1049 | * | |
1050 | * Light weight way to stop tracing. Use in conjunction with | |
1051 | * tracing_start. | |
1052 | */ | |
1053 | void tracing_stop(void) | |
1054 | { | |
1055 | struct ring_buffer *buffer; | |
1056 | unsigned long flags; | |
1057 | ||
1058 | ftrace_stop(); | |
5389f6fa | 1059 | raw_spin_lock_irqsave(&tracing_start_lock, flags); |
0f048701 SR |
1060 | if (trace_stop_count++) |
1061 | goto out; | |
1062 | ||
a2f80714 SR |
1063 | /* Prevent the buffers from switching */ |
1064 | arch_spin_lock(&ftrace_max_lock); | |
1065 | ||
0f048701 SR |
1066 | buffer = global_trace.buffer; |
1067 | if (buffer) | |
1068 | ring_buffer_record_disable(buffer); | |
1069 | ||
1070 | buffer = max_tr.buffer; | |
1071 | if (buffer) | |
1072 | ring_buffer_record_disable(buffer); | |
1073 | ||
a2f80714 SR |
1074 | arch_spin_unlock(&ftrace_max_lock); |
1075 | ||
0f048701 | 1076 | out: |
5389f6fa | 1077 | raw_spin_unlock_irqrestore(&tracing_start_lock, flags); |
0f048701 SR |
1078 | } |
1079 | ||
e309b41d | 1080 | void trace_stop_cmdline_recording(void); |
bc0c38d1 | 1081 | |
e309b41d | 1082 | static void trace_save_cmdline(struct task_struct *tsk) |
bc0c38d1 | 1083 | { |
a635cf04 | 1084 | unsigned pid, idx; |
bc0c38d1 SR |
1085 | |
1086 | if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT)) | |
1087 | return; | |
1088 | ||
1089 | /* | |
1090 | * It's not the end of the world if we don't get | |
1091 | * the lock, but we also don't want to spin | |
1092 | * nor do we want to disable interrupts, | |
1093 | * so if we miss here, then better luck next time. | |
1094 | */ | |
0199c4e6 | 1095 | if (!arch_spin_trylock(&trace_cmdline_lock)) |
bc0c38d1 SR |
1096 | return; |
1097 | ||
1098 | idx = map_pid_to_cmdline[tsk->pid]; | |
2c7eea4c | 1099 | if (idx == NO_CMDLINE_MAP) { |
bc0c38d1 SR |
1100 | idx = (cmdline_idx + 1) % SAVED_CMDLINES; |
1101 | ||
a635cf04 CE |
1102 | /* |
1103 | * Check whether the cmdline buffer at idx has a pid | |
1104 | * mapped. We are going to overwrite that entry so we | |
1105 | * need to clear the map_pid_to_cmdline. Otherwise we | |
1106 | * would read the new comm for the old pid. | |
1107 | */ | |
1108 | pid = map_cmdline_to_pid[idx]; | |
1109 | if (pid != NO_CMDLINE_MAP) | |
1110 | map_pid_to_cmdline[pid] = NO_CMDLINE_MAP; | |
bc0c38d1 | 1111 | |
a635cf04 | 1112 | map_cmdline_to_pid[idx] = tsk->pid; |
bc0c38d1 SR |
1113 | map_pid_to_cmdline[tsk->pid] = idx; |
1114 | ||
1115 | cmdline_idx = idx; | |
1116 | } | |
1117 | ||
1118 | memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN); | |
1119 | ||
0199c4e6 | 1120 | arch_spin_unlock(&trace_cmdline_lock); |
bc0c38d1 SR |
1121 | } |
1122 | ||
4ca53085 | 1123 | void trace_find_cmdline(int pid, char comm[]) |
bc0c38d1 | 1124 | { |
bc0c38d1 SR |
1125 | unsigned map; |
1126 | ||
4ca53085 SR |
1127 | if (!pid) { |
1128 | strcpy(comm, "<idle>"); | |
1129 | return; | |
1130 | } | |
bc0c38d1 | 1131 | |
74bf4076 SR |
1132 | if (WARN_ON_ONCE(pid < 0)) { |
1133 | strcpy(comm, "<XXX>"); | |
1134 | return; | |
1135 | } | |
1136 | ||
4ca53085 SR |
1137 | if (pid > PID_MAX_DEFAULT) { |
1138 | strcpy(comm, "<...>"); | |
1139 | return; | |
1140 | } | |
bc0c38d1 | 1141 | |
5b6045a9 | 1142 | preempt_disable(); |
0199c4e6 | 1143 | arch_spin_lock(&trace_cmdline_lock); |
bc0c38d1 | 1144 | map = map_pid_to_cmdline[pid]; |
50d88758 TG |
1145 | if (map != NO_CMDLINE_MAP) |
1146 | strcpy(comm, saved_cmdlines[map]); | |
1147 | else | |
1148 | strcpy(comm, "<...>"); | |
bc0c38d1 | 1149 | |
0199c4e6 | 1150 | arch_spin_unlock(&trace_cmdline_lock); |
5b6045a9 | 1151 | preempt_enable(); |
bc0c38d1 SR |
1152 | } |
1153 | ||
e309b41d | 1154 | void tracing_record_cmdline(struct task_struct *tsk) |
bc0c38d1 | 1155 | { |
18aecd36 TG |
1156 | if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled || |
1157 | !tracing_is_on()) | |
bc0c38d1 SR |
1158 | return; |
1159 | ||
1160 | trace_save_cmdline(tsk); | |
1161 | } | |
1162 | ||
45dcd8b8 | 1163 | void |
38697053 SR |
1164 | tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags, |
1165 | int pc) | |
bc0c38d1 SR |
1166 | { |
1167 | struct task_struct *tsk = current; | |
bc0c38d1 | 1168 | |
777e208d SR |
1169 | entry->preempt_count = pc & 0xff; |
1170 | entry->pid = (tsk) ? tsk->pid : 0; | |
a3a4a5ac | 1171 | entry->padding = 0; |
777e208d | 1172 | entry->flags = |
9244489a | 1173 | #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT |
2e2ca155 | 1174 | (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) | |
9244489a SR |
1175 | #else |
1176 | TRACE_FLAG_IRQS_NOSUPPORT | | |
1177 | #endif | |
bc0c38d1 SR |
1178 | ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) | |
1179 | ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) | | |
1180 | (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0); | |
1181 | } | |
f413cdb8 | 1182 | EXPORT_SYMBOL_GPL(tracing_generic_entry_update); |
bc0c38d1 | 1183 | |
e77405ad SR |
1184 | struct ring_buffer_event * |
1185 | trace_buffer_lock_reserve(struct ring_buffer *buffer, | |
1186 | int type, | |
1187 | unsigned long len, | |
1188 | unsigned long flags, int pc) | |
51a763dd ACM |
1189 | { |
1190 | struct ring_buffer_event *event; | |
1191 | ||
e77405ad | 1192 | event = ring_buffer_lock_reserve(buffer, len); |
51a763dd ACM |
1193 | if (event != NULL) { |
1194 | struct trace_entry *ent = ring_buffer_event_data(event); | |
1195 | ||
1196 | tracing_generic_entry_update(ent, flags, pc); | |
1197 | ent->type = type; | |
1198 | } | |
1199 | ||
1200 | return event; | |
1201 | } | |
51a763dd | 1202 | |
e77405ad SR |
1203 | static inline void |
1204 | __trace_buffer_unlock_commit(struct ring_buffer *buffer, | |
1205 | struct ring_buffer_event *event, | |
1206 | unsigned long flags, int pc, | |
1207 | int wake) | |
51a763dd | 1208 | { |
e77405ad | 1209 | ring_buffer_unlock_commit(buffer, event); |
51a763dd | 1210 | |
e77405ad SR |
1211 | ftrace_trace_stack(buffer, flags, 6, pc); |
1212 | ftrace_trace_userstack(buffer, flags, pc); | |
07edf712 FW |
1213 | |
1214 | if (wake) | |
1215 | trace_wake_up(); | |
1216 | } | |
1217 | ||
e77405ad SR |
1218 | void trace_buffer_unlock_commit(struct ring_buffer *buffer, |
1219 | struct ring_buffer_event *event, | |
1220 | unsigned long flags, int pc) | |
07edf712 | 1221 | { |
e77405ad | 1222 | __trace_buffer_unlock_commit(buffer, event, flags, pc, 1); |
51a763dd ACM |
1223 | } |
1224 | ||
ef5580d0 | 1225 | struct ring_buffer_event * |
e77405ad SR |
1226 | trace_current_buffer_lock_reserve(struct ring_buffer **current_rb, |
1227 | int type, unsigned long len, | |
ef5580d0 SR |
1228 | unsigned long flags, int pc) |
1229 | { | |
e77405ad SR |
1230 | *current_rb = global_trace.buffer; |
1231 | return trace_buffer_lock_reserve(*current_rb, | |
ef5580d0 SR |
1232 | type, len, flags, pc); |
1233 | } | |
94487d6d | 1234 | EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve); |
ef5580d0 | 1235 | |
e77405ad SR |
1236 | void trace_current_buffer_unlock_commit(struct ring_buffer *buffer, |
1237 | struct ring_buffer_event *event, | |
ef5580d0 SR |
1238 | unsigned long flags, int pc) |
1239 | { | |
e77405ad | 1240 | __trace_buffer_unlock_commit(buffer, event, flags, pc, 1); |
07edf712 | 1241 | } |
94487d6d | 1242 | EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit); |
07edf712 | 1243 | |
e77405ad SR |
1244 | void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer, |
1245 | struct ring_buffer_event *event, | |
1246 | unsigned long flags, int pc) | |
07edf712 | 1247 | { |
e77405ad | 1248 | __trace_buffer_unlock_commit(buffer, event, flags, pc, 0); |
77d9f465 | 1249 | } |
94487d6d | 1250 | EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit); |
77d9f465 | 1251 | |
1fd8df2c MH |
1252 | void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer, |
1253 | struct ring_buffer_event *event, | |
1254 | unsigned long flags, int pc, | |
1255 | struct pt_regs *regs) | |
1256 | { | |
1257 | ring_buffer_unlock_commit(buffer, event); | |
1258 | ||
1259 | ftrace_trace_stack_regs(buffer, flags, 0, pc, regs); | |
1260 | ftrace_trace_userstack(buffer, flags, pc); | |
1261 | } | |
1262 | EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs); | |
1263 | ||
e77405ad SR |
1264 | void trace_current_buffer_discard_commit(struct ring_buffer *buffer, |
1265 | struct ring_buffer_event *event) | |
77d9f465 | 1266 | { |
e77405ad | 1267 | ring_buffer_discard_commit(buffer, event); |
ef5580d0 | 1268 | } |
12acd473 | 1269 | EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit); |
ef5580d0 | 1270 | |
e309b41d | 1271 | void |
7be42151 | 1272 | trace_function(struct trace_array *tr, |
38697053 SR |
1273 | unsigned long ip, unsigned long parent_ip, unsigned long flags, |
1274 | int pc) | |
bc0c38d1 | 1275 | { |
e1112b4d | 1276 | struct ftrace_event_call *call = &event_function; |
e77405ad | 1277 | struct ring_buffer *buffer = tr->buffer; |
3928a8a2 | 1278 | struct ring_buffer_event *event; |
777e208d | 1279 | struct ftrace_entry *entry; |
bc0c38d1 | 1280 | |
d769041f | 1281 | /* If we are reading the ring buffer, don't trace */ |
dd17c8f7 | 1282 | if (unlikely(__this_cpu_read(ftrace_cpu_disabled))) |
d769041f SR |
1283 | return; |
1284 | ||
e77405ad | 1285 | event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry), |
51a763dd | 1286 | flags, pc); |
3928a8a2 SR |
1287 | if (!event) |
1288 | return; | |
1289 | entry = ring_buffer_event_data(event); | |
777e208d SR |
1290 | entry->ip = ip; |
1291 | entry->parent_ip = parent_ip; | |
e1112b4d | 1292 | |
e77405ad SR |
1293 | if (!filter_check_discard(call, entry, buffer, event)) |
1294 | ring_buffer_unlock_commit(buffer, event); | |
bc0c38d1 SR |
1295 | } |
1296 | ||
e309b41d | 1297 | void |
2e0f5761 | 1298 | ftrace(struct trace_array *tr, struct trace_array_cpu *data, |
38697053 SR |
1299 | unsigned long ip, unsigned long parent_ip, unsigned long flags, |
1300 | int pc) | |
2e0f5761 IM |
1301 | { |
1302 | if (likely(!atomic_read(&data->disabled))) | |
7be42151 | 1303 | trace_function(tr, ip, parent_ip, flags, pc); |
2e0f5761 IM |
1304 | } |
1305 | ||
c0a0d0d3 | 1306 | #ifdef CONFIG_STACKTRACE |
4a9bd3f1 SR |
1307 | |
1308 | #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long)) | |
1309 | struct ftrace_stack { | |
1310 | unsigned long calls[FTRACE_STACK_MAX_ENTRIES]; | |
1311 | }; | |
1312 | ||
1313 | static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack); | |
1314 | static DEFINE_PER_CPU(int, ftrace_stack_reserve); | |
1315 | ||
e77405ad | 1316 | static void __ftrace_trace_stack(struct ring_buffer *buffer, |
53614991 | 1317 | unsigned long flags, |
1fd8df2c | 1318 | int skip, int pc, struct pt_regs *regs) |
86387f7e | 1319 | { |
e1112b4d | 1320 | struct ftrace_event_call *call = &event_kernel_stack; |
3928a8a2 | 1321 | struct ring_buffer_event *event; |
777e208d | 1322 | struct stack_entry *entry; |
86387f7e | 1323 | struct stack_trace trace; |
4a9bd3f1 SR |
1324 | int use_stack; |
1325 | int size = FTRACE_STACK_ENTRIES; | |
1326 | ||
1327 | trace.nr_entries = 0; | |
1328 | trace.skip = skip; | |
1329 | ||
1330 | /* | |
1331 | * Since events can happen in NMIs there's no safe way to | |
1332 | * use the per cpu ftrace_stacks. We reserve it and if an interrupt | |
1333 | * or NMI comes in, it will just have to use the default | |
1334 | * FTRACE_STACK_SIZE. | |
1335 | */ | |
1336 | preempt_disable_notrace(); | |
1337 | ||
1338 | use_stack = ++__get_cpu_var(ftrace_stack_reserve); | |
1339 | /* | |
1340 | * We don't need any atomic variables, just a barrier. | |
1341 | * If an interrupt comes in, we don't care, because it would | |
1342 | * have exited and put the counter back to what we want. | |
1343 | * We just need a barrier to keep gcc from moving things | |
1344 | * around. | |
1345 | */ | |
1346 | barrier(); | |
1347 | if (use_stack == 1) { | |
1348 | trace.entries = &__get_cpu_var(ftrace_stack).calls[0]; | |
1349 | trace.max_entries = FTRACE_STACK_MAX_ENTRIES; | |
1350 | ||
1351 | if (regs) | |
1352 | save_stack_trace_regs(regs, &trace); | |
1353 | else | |
1354 | save_stack_trace(&trace); | |
1355 | ||
1356 | if (trace.nr_entries > size) | |
1357 | size = trace.nr_entries; | |
1358 | } else | |
1359 | /* From now on, use_stack is a boolean */ | |
1360 | use_stack = 0; | |
1361 | ||
1362 | size *= sizeof(unsigned long); | |
86387f7e | 1363 | |
e77405ad | 1364 | event = trace_buffer_lock_reserve(buffer, TRACE_STACK, |
4a9bd3f1 | 1365 | sizeof(*entry) + size, flags, pc); |
3928a8a2 | 1366 | if (!event) |
4a9bd3f1 SR |
1367 | goto out; |
1368 | entry = ring_buffer_event_data(event); | |
86387f7e | 1369 | |
4a9bd3f1 SR |
1370 | memset(&entry->caller, 0, size); |
1371 | ||
1372 | if (use_stack) | |
1373 | memcpy(&entry->caller, trace.entries, | |
1374 | trace.nr_entries * sizeof(unsigned long)); | |
1375 | else { | |
1376 | trace.max_entries = FTRACE_STACK_ENTRIES; | |
1377 | trace.entries = entry->caller; | |
1378 | if (regs) | |
1379 | save_stack_trace_regs(regs, &trace); | |
1380 | else | |
1381 | save_stack_trace(&trace); | |
1382 | } | |
1383 | ||
1384 | entry->size = trace.nr_entries; | |
86387f7e | 1385 | |
e77405ad SR |
1386 | if (!filter_check_discard(call, entry, buffer, event)) |
1387 | ring_buffer_unlock_commit(buffer, event); | |
4a9bd3f1 SR |
1388 | |
1389 | out: | |
1390 | /* Again, don't let gcc optimize things here */ | |
1391 | barrier(); | |
1392 | __get_cpu_var(ftrace_stack_reserve)--; | |
1393 | preempt_enable_notrace(); | |
1394 | ||
f0a920d5 IM |
1395 | } |
1396 | ||
1fd8df2c MH |
1397 | void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags, |
1398 | int skip, int pc, struct pt_regs *regs) | |
1399 | { | |
1400 | if (!(trace_flags & TRACE_ITER_STACKTRACE)) | |
1401 | return; | |
1402 | ||
1403 | __ftrace_trace_stack(buffer, flags, skip, pc, regs); | |
1404 | } | |
1405 | ||
e77405ad SR |
1406 | void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags, |
1407 | int skip, int pc) | |
53614991 SR |
1408 | { |
1409 | if (!(trace_flags & TRACE_ITER_STACKTRACE)) | |
1410 | return; | |
1411 | ||
1fd8df2c | 1412 | __ftrace_trace_stack(buffer, flags, skip, pc, NULL); |
53614991 SR |
1413 | } |
1414 | ||
c0a0d0d3 FW |
1415 | void __trace_stack(struct trace_array *tr, unsigned long flags, int skip, |
1416 | int pc) | |
38697053 | 1417 | { |
1fd8df2c | 1418 | __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL); |
38697053 SR |
1419 | } |
1420 | ||
03889384 SR |
1421 | /** |
1422 | * trace_dump_stack - record a stack back trace in the trace buffer | |
1423 | */ | |
1424 | void trace_dump_stack(void) | |
1425 | { | |
1426 | unsigned long flags; | |
1427 | ||
1428 | if (tracing_disabled || tracing_selftest_running) | |
e36c5458 | 1429 | return; |
03889384 SR |
1430 | |
1431 | local_save_flags(flags); | |
1432 | ||
1433 | /* skipping 3 traces, seems to get us at the caller of this function */ | |
1fd8df2c | 1434 | __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL); |
03889384 SR |
1435 | } |
1436 | ||
91e86e56 SR |
1437 | static DEFINE_PER_CPU(int, user_stack_count); |
1438 | ||
e77405ad SR |
1439 | void |
1440 | ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc) | |
02b67518 | 1441 | { |
e1112b4d | 1442 | struct ftrace_event_call *call = &event_user_stack; |
8d7c6a96 | 1443 | struct ring_buffer_event *event; |
02b67518 TE |
1444 | struct userstack_entry *entry; |
1445 | struct stack_trace trace; | |
02b67518 TE |
1446 | |
1447 | if (!(trace_flags & TRACE_ITER_USERSTACKTRACE)) | |
1448 | return; | |
1449 | ||
b6345879 SR |
1450 | /* |
1451 | * NMIs can not handle page faults, even with fix ups. | |
1452 | * The save user stack can (and often does) fault. | |
1453 | */ | |
1454 | if (unlikely(in_nmi())) | |
1455 | return; | |
02b67518 | 1456 | |
91e86e56 SR |
1457 | /* |
1458 | * prevent recursion, since the user stack tracing may | |
1459 | * trigger other kernel events. | |
1460 | */ | |
1461 | preempt_disable(); | |
1462 | if (__this_cpu_read(user_stack_count)) | |
1463 | goto out; | |
1464 | ||
1465 | __this_cpu_inc(user_stack_count); | |
1466 | ||
e77405ad | 1467 | event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK, |
51a763dd | 1468 | sizeof(*entry), flags, pc); |
02b67518 | 1469 | if (!event) |
1dbd1951 | 1470 | goto out_drop_count; |
02b67518 | 1471 | entry = ring_buffer_event_data(event); |
02b67518 | 1472 | |
48659d31 | 1473 | entry->tgid = current->tgid; |
02b67518 TE |
1474 | memset(&entry->caller, 0, sizeof(entry->caller)); |
1475 | ||
1476 | trace.nr_entries = 0; | |
1477 | trace.max_entries = FTRACE_STACK_ENTRIES; | |
1478 | trace.skip = 0; | |
1479 | trace.entries = entry->caller; | |
1480 | ||
1481 | save_stack_trace_user(&trace); | |
e77405ad SR |
1482 | if (!filter_check_discard(call, entry, buffer, event)) |
1483 | ring_buffer_unlock_commit(buffer, event); | |
91e86e56 | 1484 | |
1dbd1951 | 1485 | out_drop_count: |
91e86e56 | 1486 | __this_cpu_dec(user_stack_count); |
91e86e56 SR |
1487 | out: |
1488 | preempt_enable(); | |
02b67518 TE |
1489 | } |
1490 | ||
4fd27358 HE |
1491 | #ifdef UNUSED |
1492 | static void __trace_userstack(struct trace_array *tr, unsigned long flags) | |
02b67518 | 1493 | { |
7be42151 | 1494 | ftrace_trace_userstack(tr, flags, preempt_count()); |
02b67518 | 1495 | } |
4fd27358 | 1496 | #endif /* UNUSED */ |
02b67518 | 1497 | |
c0a0d0d3 FW |
1498 | #endif /* CONFIG_STACKTRACE */ |
1499 | ||
769b0441 | 1500 | /** |
48ead020 | 1501 | * trace_vbprintk - write binary msg to tracing buffer |
769b0441 FW |
1502 | * |
1503 | */ | |
40ce74f1 | 1504 | int trace_vbprintk(unsigned long ip, const char *fmt, va_list args) |
769b0441 | 1505 | { |
445c8951 | 1506 | static arch_spinlock_t trace_buf_lock = |
edc35bd7 | 1507 | (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
769b0441 FW |
1508 | static u32 trace_buf[TRACE_BUF_SIZE]; |
1509 | ||
e1112b4d | 1510 | struct ftrace_event_call *call = &event_bprint; |
769b0441 | 1511 | struct ring_buffer_event *event; |
e77405ad | 1512 | struct ring_buffer *buffer; |
769b0441 FW |
1513 | struct trace_array *tr = &global_trace; |
1514 | struct trace_array_cpu *data; | |
48ead020 | 1515 | struct bprint_entry *entry; |
769b0441 | 1516 | unsigned long flags; |
3189cdb3 | 1517 | int disable; |
769b0441 FW |
1518 | int cpu, len = 0, size, pc; |
1519 | ||
1520 | if (unlikely(tracing_selftest_running || tracing_disabled)) | |
1521 | return 0; | |
1522 | ||
1523 | /* Don't pollute graph traces with trace_vprintk internals */ | |
1524 | pause_graph_tracing(); | |
1525 | ||
1526 | pc = preempt_count(); | |
5168ae50 | 1527 | preempt_disable_notrace(); |
769b0441 FW |
1528 | cpu = raw_smp_processor_id(); |
1529 | data = tr->data[cpu]; | |
1530 | ||
3189cdb3 SR |
1531 | disable = atomic_inc_return(&data->disabled); |
1532 | if (unlikely(disable != 1)) | |
769b0441 FW |
1533 | goto out; |
1534 | ||
80370cb7 SR |
1535 | /* Lockdep uses trace_printk for lock tracing */ |
1536 | local_irq_save(flags); | |
0199c4e6 | 1537 | arch_spin_lock(&trace_buf_lock); |
769b0441 FW |
1538 | len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args); |
1539 | ||
1540 | if (len > TRACE_BUF_SIZE || len < 0) | |
1541 | goto out_unlock; | |
1542 | ||
1543 | size = sizeof(*entry) + sizeof(u32) * len; | |
e77405ad SR |
1544 | buffer = tr->buffer; |
1545 | event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size, | |
1546 | flags, pc); | |
769b0441 FW |
1547 | if (!event) |
1548 | goto out_unlock; | |
1549 | entry = ring_buffer_event_data(event); | |
1550 | entry->ip = ip; | |
769b0441 FW |
1551 | entry->fmt = fmt; |
1552 | ||
1553 | memcpy(entry->buf, trace_buf, sizeof(u32) * len); | |
d931369b | 1554 | if (!filter_check_discard(call, entry, buffer, event)) { |
e77405ad | 1555 | ring_buffer_unlock_commit(buffer, event); |
d931369b SR |
1556 | ftrace_trace_stack(buffer, flags, 6, pc); |
1557 | } | |
769b0441 FW |
1558 | |
1559 | out_unlock: | |
0199c4e6 | 1560 | arch_spin_unlock(&trace_buf_lock); |
80370cb7 | 1561 | local_irq_restore(flags); |
769b0441 FW |
1562 | |
1563 | out: | |
3189cdb3 | 1564 | atomic_dec_return(&data->disabled); |
5168ae50 | 1565 | preempt_enable_notrace(); |
769b0441 FW |
1566 | unpause_graph_tracing(); |
1567 | ||
1568 | return len; | |
1569 | } | |
48ead020 FW |
1570 | EXPORT_SYMBOL_GPL(trace_vbprintk); |
1571 | ||
659372d3 SR |
1572 | int trace_array_printk(struct trace_array *tr, |
1573 | unsigned long ip, const char *fmt, ...) | |
1574 | { | |
1575 | int ret; | |
1576 | va_list ap; | |
1577 | ||
1578 | if (!(trace_flags & TRACE_ITER_PRINTK)) | |
1579 | return 0; | |
1580 | ||
1581 | va_start(ap, fmt); | |
1582 | ret = trace_array_vprintk(tr, ip, fmt, ap); | |
1583 | va_end(ap); | |
1584 | return ret; | |
1585 | } | |
1586 | ||
1587 | int trace_array_vprintk(struct trace_array *tr, | |
1588 | unsigned long ip, const char *fmt, va_list args) | |
48ead020 | 1589 | { |
edc35bd7 | 1590 | static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED; |
48ead020 FW |
1591 | static char trace_buf[TRACE_BUF_SIZE]; |
1592 | ||
e1112b4d | 1593 | struct ftrace_event_call *call = &event_print; |
48ead020 | 1594 | struct ring_buffer_event *event; |
e77405ad | 1595 | struct ring_buffer *buffer; |
48ead020 FW |
1596 | struct trace_array_cpu *data; |
1597 | int cpu, len = 0, size, pc; | |
1598 | struct print_entry *entry; | |
1599 | unsigned long irq_flags; | |
3189cdb3 | 1600 | int disable; |
48ead020 FW |
1601 | |
1602 | if (tracing_disabled || tracing_selftest_running) | |
1603 | return 0; | |
1604 | ||
1605 | pc = preempt_count(); | |
1606 | preempt_disable_notrace(); | |
1607 | cpu = raw_smp_processor_id(); | |
1608 | data = tr->data[cpu]; | |
1609 | ||
3189cdb3 SR |
1610 | disable = atomic_inc_return(&data->disabled); |
1611 | if (unlikely(disable != 1)) | |
48ead020 FW |
1612 | goto out; |
1613 | ||
1614 | pause_graph_tracing(); | |
1615 | raw_local_irq_save(irq_flags); | |
0199c4e6 | 1616 | arch_spin_lock(&trace_buf_lock); |
f2942487 | 1617 | len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args); |
48ead020 FW |
1618 | |
1619 | size = sizeof(*entry) + len + 1; | |
e77405ad SR |
1620 | buffer = tr->buffer; |
1621 | event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, | |
1622 | irq_flags, pc); | |
48ead020 FW |
1623 | if (!event) |
1624 | goto out_unlock; | |
1625 | entry = ring_buffer_event_data(event); | |
c13d2f7c | 1626 | entry->ip = ip; |
48ead020 FW |
1627 | |
1628 | memcpy(&entry->buf, trace_buf, len); | |
c13d2f7c | 1629 | entry->buf[len] = '\0'; |
d931369b | 1630 | if (!filter_check_discard(call, entry, buffer, event)) { |
e77405ad | 1631 | ring_buffer_unlock_commit(buffer, event); |
d931369b SR |
1632 | ftrace_trace_stack(buffer, irq_flags, 6, pc); |
1633 | } | |
48ead020 FW |
1634 | |
1635 | out_unlock: | |
0199c4e6 | 1636 | arch_spin_unlock(&trace_buf_lock); |
48ead020 FW |
1637 | raw_local_irq_restore(irq_flags); |
1638 | unpause_graph_tracing(); | |
1639 | out: | |
3189cdb3 | 1640 | atomic_dec_return(&data->disabled); |
48ead020 FW |
1641 | preempt_enable_notrace(); |
1642 | ||
1643 | return len; | |
1644 | } | |
659372d3 SR |
1645 | |
1646 | int trace_vprintk(unsigned long ip, const char *fmt, va_list args) | |
1647 | { | |
a813a159 | 1648 | return trace_array_vprintk(&global_trace, ip, fmt, args); |
659372d3 | 1649 | } |
769b0441 FW |
1650 | EXPORT_SYMBOL_GPL(trace_vprintk); |
1651 | ||
e2ac8ef5 | 1652 | static void trace_iterator_increment(struct trace_iterator *iter) |
5a90f577 | 1653 | { |
d769041f SR |
1654 | /* Don't allow ftrace to trace into the ring buffers */ |
1655 | ftrace_disable_cpu(); | |
1656 | ||
5a90f577 | 1657 | iter->idx++; |
d769041f SR |
1658 | if (iter->buffer_iter[iter->cpu]) |
1659 | ring_buffer_read(iter->buffer_iter[iter->cpu], NULL); | |
1660 | ||
1661 | ftrace_enable_cpu(); | |
5a90f577 SR |
1662 | } |
1663 | ||
e309b41d | 1664 | static struct trace_entry * |
bc21b478 SR |
1665 | peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts, |
1666 | unsigned long *lost_events) | |
dd0e545f | 1667 | { |
3928a8a2 SR |
1668 | struct ring_buffer_event *event; |
1669 | struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu]; | |
dd0e545f | 1670 | |
d769041f SR |
1671 | /* Don't allow ftrace to trace into the ring buffers */ |
1672 | ftrace_disable_cpu(); | |
1673 | ||
1674 | if (buf_iter) | |
1675 | event = ring_buffer_iter_peek(buf_iter, ts); | |
1676 | else | |
bc21b478 SR |
1677 | event = ring_buffer_peek(iter->tr->buffer, cpu, ts, |
1678 | lost_events); | |
d769041f SR |
1679 | |
1680 | ftrace_enable_cpu(); | |
1681 | ||
4a9bd3f1 SR |
1682 | if (event) { |
1683 | iter->ent_size = ring_buffer_event_length(event); | |
1684 | return ring_buffer_event_data(event); | |
1685 | } | |
1686 | iter->ent_size = 0; | |
1687 | return NULL; | |
dd0e545f | 1688 | } |
d769041f | 1689 | |
dd0e545f | 1690 | static struct trace_entry * |
bc21b478 SR |
1691 | __find_next_entry(struct trace_iterator *iter, int *ent_cpu, |
1692 | unsigned long *missing_events, u64 *ent_ts) | |
bc0c38d1 | 1693 | { |
3928a8a2 | 1694 | struct ring_buffer *buffer = iter->tr->buffer; |
bc0c38d1 | 1695 | struct trace_entry *ent, *next = NULL; |
aa27497c | 1696 | unsigned long lost_events = 0, next_lost = 0; |
b04cc6b1 | 1697 | int cpu_file = iter->cpu_file; |
3928a8a2 | 1698 | u64 next_ts = 0, ts; |
bc0c38d1 SR |
1699 | int next_cpu = -1; |
1700 | int cpu; | |
1701 | ||
b04cc6b1 FW |
1702 | /* |
1703 | * If we are in a per_cpu trace file, don't bother by iterating over | |
1704 | * all cpu and peek directly. | |
1705 | */ | |
1706 | if (cpu_file > TRACE_PIPE_ALL_CPU) { | |
1707 | if (ring_buffer_empty_cpu(buffer, cpu_file)) | |
1708 | return NULL; | |
bc21b478 | 1709 | ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events); |
b04cc6b1 FW |
1710 | if (ent_cpu) |
1711 | *ent_cpu = cpu_file; | |
1712 | ||
1713 | return ent; | |
1714 | } | |
1715 | ||
ab46428c | 1716 | for_each_tracing_cpu(cpu) { |
dd0e545f | 1717 | |
3928a8a2 SR |
1718 | if (ring_buffer_empty_cpu(buffer, cpu)) |
1719 | continue; | |
dd0e545f | 1720 | |
bc21b478 | 1721 | ent = peek_next_entry(iter, cpu, &ts, &lost_events); |
dd0e545f | 1722 | |
cdd31cd2 IM |
1723 | /* |
1724 | * Pick the entry with the smallest timestamp: | |
1725 | */ | |
3928a8a2 | 1726 | if (ent && (!next || ts < next_ts)) { |
bc0c38d1 SR |
1727 | next = ent; |
1728 | next_cpu = cpu; | |
3928a8a2 | 1729 | next_ts = ts; |
bc21b478 | 1730 | next_lost = lost_events; |
bc0c38d1 SR |
1731 | } |
1732 | } | |
1733 | ||
1734 | if (ent_cpu) | |
1735 | *ent_cpu = next_cpu; | |
1736 | ||
3928a8a2 SR |
1737 | if (ent_ts) |
1738 | *ent_ts = next_ts; | |
1739 | ||
bc21b478 SR |
1740 | if (missing_events) |
1741 | *missing_events = next_lost; | |
1742 | ||
bc0c38d1 SR |
1743 | return next; |
1744 | } | |
1745 | ||
dd0e545f | 1746 | /* Find the next real entry, without updating the iterator itself */ |
c4a8e8be FW |
1747 | struct trace_entry *trace_find_next_entry(struct trace_iterator *iter, |
1748 | int *ent_cpu, u64 *ent_ts) | |
bc0c38d1 | 1749 | { |
bc21b478 | 1750 | return __find_next_entry(iter, ent_cpu, NULL, ent_ts); |
dd0e545f SR |
1751 | } |
1752 | ||
1753 | /* Find the next real entry, and increment the iterator to the next entry */ | |
955b61e5 | 1754 | void *trace_find_next_entry_inc(struct trace_iterator *iter) |
dd0e545f | 1755 | { |
bc21b478 SR |
1756 | iter->ent = __find_next_entry(iter, &iter->cpu, |
1757 | &iter->lost_events, &iter->ts); | |
dd0e545f | 1758 | |
3928a8a2 | 1759 | if (iter->ent) |
e2ac8ef5 | 1760 | trace_iterator_increment(iter); |
dd0e545f | 1761 | |
3928a8a2 | 1762 | return iter->ent ? iter : NULL; |
b3806b43 | 1763 | } |
bc0c38d1 | 1764 | |
e309b41d | 1765 | static void trace_consume(struct trace_iterator *iter) |
b3806b43 | 1766 | { |
d769041f SR |
1767 | /* Don't allow ftrace to trace into the ring buffers */ |
1768 | ftrace_disable_cpu(); | |
bc21b478 SR |
1769 | ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts, |
1770 | &iter->lost_events); | |
d769041f | 1771 | ftrace_enable_cpu(); |
bc0c38d1 SR |
1772 | } |
1773 | ||
e309b41d | 1774 | static void *s_next(struct seq_file *m, void *v, loff_t *pos) |
bc0c38d1 SR |
1775 | { |
1776 | struct trace_iterator *iter = m->private; | |
bc0c38d1 | 1777 | int i = (int)*pos; |
4e3c3333 | 1778 | void *ent; |
bc0c38d1 | 1779 | |
a63ce5b3 SR |
1780 | WARN_ON_ONCE(iter->leftover); |
1781 | ||
bc0c38d1 SR |
1782 | (*pos)++; |
1783 | ||
1784 | /* can't go backwards */ | |
1785 | if (iter->idx > i) | |
1786 | return NULL; | |
1787 | ||
1788 | if (iter->idx < 0) | |
955b61e5 | 1789 | ent = trace_find_next_entry_inc(iter); |
bc0c38d1 SR |
1790 | else |
1791 | ent = iter; | |
1792 | ||
1793 | while (ent && iter->idx < i) | |
955b61e5 | 1794 | ent = trace_find_next_entry_inc(iter); |
bc0c38d1 SR |
1795 | |
1796 | iter->pos = *pos; | |
1797 | ||
bc0c38d1 SR |
1798 | return ent; |
1799 | } | |
1800 | ||
955b61e5 | 1801 | void tracing_iter_reset(struct trace_iterator *iter, int cpu) |
2f26ebd5 SR |
1802 | { |
1803 | struct trace_array *tr = iter->tr; | |
1804 | struct ring_buffer_event *event; | |
1805 | struct ring_buffer_iter *buf_iter; | |
1806 | unsigned long entries = 0; | |
1807 | u64 ts; | |
1808 | ||
1809 | tr->data[cpu]->skipped_entries = 0; | |
1810 | ||
1811 | if (!iter->buffer_iter[cpu]) | |
1812 | return; | |
1813 | ||
1814 | buf_iter = iter->buffer_iter[cpu]; | |
1815 | ring_buffer_iter_reset(buf_iter); | |
1816 | ||
1817 | /* | |
1818 | * We could have the case with the max latency tracers | |
1819 | * that a reset never took place on a cpu. This is evident | |
1820 | * by the timestamp being before the start of the buffer. | |
1821 | */ | |
1822 | while ((event = ring_buffer_iter_peek(buf_iter, &ts))) { | |
1823 | if (ts >= iter->tr->time_start) | |
1824 | break; | |
1825 | entries++; | |
1826 | ring_buffer_read(buf_iter, NULL); | |
1827 | } | |
1828 | ||
1829 | tr->data[cpu]->skipped_entries = entries; | |
1830 | } | |
1831 | ||
d7350c3f | 1832 | /* |
d7350c3f FW |
1833 | * The current tracer is copied to avoid a global locking |
1834 | * all around. | |
1835 | */ | |
bc0c38d1 SR |
1836 | static void *s_start(struct seq_file *m, loff_t *pos) |
1837 | { | |
1838 | struct trace_iterator *iter = m->private; | |
d7350c3f | 1839 | static struct tracer *old_tracer; |
b04cc6b1 | 1840 | int cpu_file = iter->cpu_file; |
bc0c38d1 SR |
1841 | void *p = NULL; |
1842 | loff_t l = 0; | |
3928a8a2 | 1843 | int cpu; |
bc0c38d1 | 1844 | |
d7350c3f | 1845 | /* copy the tracer to avoid using a global lock all around */ |
bc0c38d1 | 1846 | mutex_lock(&trace_types_lock); |
d7350c3f FW |
1847 | if (unlikely(old_tracer != current_trace && current_trace)) { |
1848 | old_tracer = current_trace; | |
1849 | *iter->trace = *current_trace; | |
d15f57f2 | 1850 | } |
d7350c3f | 1851 | mutex_unlock(&trace_types_lock); |
bc0c38d1 SR |
1852 | |
1853 | atomic_inc(&trace_record_cmdline_disabled); | |
1854 | ||
bc0c38d1 SR |
1855 | if (*pos != iter->pos) { |
1856 | iter->ent = NULL; | |
1857 | iter->cpu = 0; | |
1858 | iter->idx = -1; | |
1859 | ||
d769041f SR |
1860 | ftrace_disable_cpu(); |
1861 | ||
b04cc6b1 FW |
1862 | if (cpu_file == TRACE_PIPE_ALL_CPU) { |
1863 | for_each_tracing_cpu(cpu) | |
2f26ebd5 | 1864 | tracing_iter_reset(iter, cpu); |
b04cc6b1 | 1865 | } else |
2f26ebd5 | 1866 | tracing_iter_reset(iter, cpu_file); |
bc0c38d1 | 1867 | |
d769041f SR |
1868 | ftrace_enable_cpu(); |
1869 | ||
ac91d854 | 1870 | iter->leftover = 0; |
bc0c38d1 SR |
1871 | for (p = iter; p && l < *pos; p = s_next(m, p, &l)) |
1872 | ; | |
1873 | ||
1874 | } else { | |
a63ce5b3 SR |
1875 | /* |
1876 | * If we overflowed the seq_file before, then we want | |
1877 | * to just reuse the trace_seq buffer again. | |
1878 | */ | |
1879 | if (iter->leftover) | |
1880 | p = iter; | |
1881 | else { | |
1882 | l = *pos - 1; | |
1883 | p = s_next(m, p, &l); | |
1884 | } | |
bc0c38d1 SR |
1885 | } |
1886 | ||
4f535968 | 1887 | trace_event_read_lock(); |
7e53bd42 | 1888 | trace_access_lock(cpu_file); |
bc0c38d1 SR |
1889 | return p; |
1890 | } | |
1891 | ||
1892 | static void s_stop(struct seq_file *m, void *p) | |
1893 | { | |
7e53bd42 LJ |
1894 | struct trace_iterator *iter = m->private; |
1895 | ||
bc0c38d1 | 1896 | atomic_dec(&trace_record_cmdline_disabled); |
7e53bd42 | 1897 | trace_access_unlock(iter->cpu_file); |
4f535968 | 1898 | trace_event_read_unlock(); |
bc0c38d1 SR |
1899 | } |
1900 | ||
39eaf7ef SR |
1901 | static void |
1902 | get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries) | |
1903 | { | |
1904 | unsigned long count; | |
1905 | int cpu; | |
1906 | ||
1907 | *total = 0; | |
1908 | *entries = 0; | |
1909 | ||
1910 | for_each_tracing_cpu(cpu) { | |
1911 | count = ring_buffer_entries_cpu(tr->buffer, cpu); | |
1912 | /* | |
1913 | * If this buffer has skipped entries, then we hold all | |
1914 | * entries for the trace and we need to ignore the | |
1915 | * ones before the time stamp. | |
1916 | */ | |
1917 | if (tr->data[cpu]->skipped_entries) { | |
1918 | count -= tr->data[cpu]->skipped_entries; | |
1919 | /* total is the same as the entries */ | |
1920 | *total += count; | |
1921 | } else | |
1922 | *total += count + | |
1923 | ring_buffer_overrun_cpu(tr->buffer, cpu); | |
1924 | *entries += count; | |
1925 | } | |
1926 | } | |
1927 | ||
e309b41d | 1928 | static void print_lat_help_header(struct seq_file *m) |
bc0c38d1 | 1929 | { |
a6168353 ME |
1930 | seq_puts(m, "# _------=> CPU# \n"); |
1931 | seq_puts(m, "# / _-----=> irqs-off \n"); | |
1932 | seq_puts(m, "# | / _----=> need-resched \n"); | |
1933 | seq_puts(m, "# || / _---=> hardirq/softirq \n"); | |
1934 | seq_puts(m, "# ||| / _--=> preempt-depth \n"); | |
e6e1e259 SR |
1935 | seq_puts(m, "# |||| / delay \n"); |
1936 | seq_puts(m, "# cmd pid ||||| time | caller \n"); | |
1937 | seq_puts(m, "# \\ / ||||| \\ | / \n"); | |
bc0c38d1 SR |
1938 | } |
1939 | ||
39eaf7ef | 1940 | static void print_event_info(struct trace_array *tr, struct seq_file *m) |
bc0c38d1 | 1941 | { |
39eaf7ef SR |
1942 | unsigned long total; |
1943 | unsigned long entries; | |
1944 | ||
1945 | get_total_entries(tr, &total, &entries); | |
1946 | seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n", | |
1947 | entries, total, num_online_cpus()); | |
1948 | seq_puts(m, "#\n"); | |
1949 | } | |
1950 | ||
1951 | static void print_func_help_header(struct trace_array *tr, struct seq_file *m) | |
1952 | { | |
1953 | print_event_info(tr, m); | |
77271ce4 | 1954 | seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"); |
a6168353 | 1955 | seq_puts(m, "# | | | | |\n"); |
bc0c38d1 SR |
1956 | } |
1957 | ||
39eaf7ef | 1958 | static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m) |
77271ce4 | 1959 | { |
39eaf7ef | 1960 | print_event_info(tr, m); |
77271ce4 SR |
1961 | seq_puts(m, "# _-----=> irqs-off\n"); |
1962 | seq_puts(m, "# / _----=> need-resched\n"); | |
1963 | seq_puts(m, "# | / _---=> hardirq/softirq\n"); | |
1964 | seq_puts(m, "# || / _--=> preempt-depth\n"); | |
1965 | seq_puts(m, "# ||| / delay\n"); | |
1966 | seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"); | |
1967 | seq_puts(m, "# | | | |||| | |\n"); | |
1968 | } | |
bc0c38d1 | 1969 | |
62b915f1 | 1970 | void |
bc0c38d1 SR |
1971 | print_trace_header(struct seq_file *m, struct trace_iterator *iter) |
1972 | { | |
1973 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); | |
1974 | struct trace_array *tr = iter->tr; | |
1975 | struct trace_array_cpu *data = tr->data[tr->cpu]; | |
1976 | struct tracer *type = current_trace; | |
39eaf7ef SR |
1977 | unsigned long entries; |
1978 | unsigned long total; | |
bc0c38d1 SR |
1979 | const char *name = "preemption"; |
1980 | ||
1981 | if (type) | |
1982 | name = type->name; | |
1983 | ||
39eaf7ef | 1984 | get_total_entries(tr, &total, &entries); |
bc0c38d1 | 1985 | |
888b55dc | 1986 | seq_printf(m, "# %s latency trace v1.1.5 on %s\n", |
bc0c38d1 | 1987 | name, UTS_RELEASE); |
888b55dc | 1988 | seq_puts(m, "# -----------------------------------" |
bc0c38d1 | 1989 | "---------------------------------\n"); |
888b55dc | 1990 | seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |" |
bc0c38d1 | 1991 | " (M:%s VP:%d, KP:%d, SP:%d HP:%d", |
57f50be1 | 1992 | nsecs_to_usecs(data->saved_latency), |
bc0c38d1 | 1993 | entries, |
4c11d7ae | 1994 | total, |
bc0c38d1 SR |
1995 | tr->cpu, |
1996 | #if defined(CONFIG_PREEMPT_NONE) | |
1997 | "server", | |
1998 | #elif defined(CONFIG_PREEMPT_VOLUNTARY) | |
1999 | "desktop", | |
b5c21b45 | 2000 | #elif defined(CONFIG_PREEMPT) |
bc0c38d1 SR |
2001 | "preempt", |
2002 | #else | |
2003 | "unknown", | |
2004 | #endif | |
2005 | /* These are reserved for later use */ | |
2006 | 0, 0, 0, 0); | |
2007 | #ifdef CONFIG_SMP | |
2008 | seq_printf(m, " #P:%d)\n", num_online_cpus()); | |
2009 | #else | |
2010 | seq_puts(m, ")\n"); | |
2011 | #endif | |
888b55dc KM |
2012 | seq_puts(m, "# -----------------\n"); |
2013 | seq_printf(m, "# | task: %.16s-%d " | |
bc0c38d1 SR |
2014 | "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n", |
2015 | data->comm, data->pid, data->uid, data->nice, | |
2016 | data->policy, data->rt_priority); | |
888b55dc | 2017 | seq_puts(m, "# -----------------\n"); |
bc0c38d1 SR |
2018 | |
2019 | if (data->critical_start) { | |
888b55dc | 2020 | seq_puts(m, "# => started at: "); |
214023c3 SR |
2021 | seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags); |
2022 | trace_print_seq(m, &iter->seq); | |
888b55dc | 2023 | seq_puts(m, "\n# => ended at: "); |
214023c3 SR |
2024 | seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags); |
2025 | trace_print_seq(m, &iter->seq); | |
8248ac05 | 2026 | seq_puts(m, "\n#\n"); |
bc0c38d1 SR |
2027 | } |
2028 | ||
888b55dc | 2029 | seq_puts(m, "#\n"); |
bc0c38d1 SR |
2030 | } |
2031 | ||
a309720c SR |
2032 | static void test_cpu_buff_start(struct trace_iterator *iter) |
2033 | { | |
2034 | struct trace_seq *s = &iter->seq; | |
2035 | ||
12ef7d44 SR |
2036 | if (!(trace_flags & TRACE_ITER_ANNOTATE)) |
2037 | return; | |
2038 | ||
2039 | if (!(iter->iter_flags & TRACE_FILE_ANNOTATE)) | |
2040 | return; | |
2041 | ||
4462344e | 2042 | if (cpumask_test_cpu(iter->cpu, iter->started)) |
a309720c SR |
2043 | return; |
2044 | ||
2f26ebd5 SR |
2045 | if (iter->tr->data[iter->cpu]->skipped_entries) |
2046 | return; | |
2047 | ||
4462344e | 2048 | cpumask_set_cpu(iter->cpu, iter->started); |
b0dfa978 FW |
2049 | |
2050 | /* Don't print started cpu buffer for the first entry of the trace */ | |
2051 | if (iter->idx > 1) | |
2052 | trace_seq_printf(s, "##### CPU %u buffer started ####\n", | |
2053 | iter->cpu); | |
a309720c SR |
2054 | } |
2055 | ||
2c4f035f | 2056 | static enum print_line_t print_trace_fmt(struct trace_iterator *iter) |
bc0c38d1 | 2057 | { |
214023c3 | 2058 | struct trace_seq *s = &iter->seq; |
bc0c38d1 | 2059 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); |
4e3c3333 | 2060 | struct trace_entry *entry; |
f633cef0 | 2061 | struct trace_event *event; |
bc0c38d1 | 2062 | |
4e3c3333 | 2063 | entry = iter->ent; |
dd0e545f | 2064 | |
a309720c SR |
2065 | test_cpu_buff_start(iter); |
2066 | ||
c4a8e8be | 2067 | event = ftrace_find_event(entry->type); |
bc0c38d1 | 2068 | |
c4a8e8be | 2069 | if (trace_flags & TRACE_ITER_CONTEXT_INFO) { |
27d48be8 SR |
2070 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) { |
2071 | if (!trace_print_lat_context(iter)) | |
2072 | goto partial; | |
2073 | } else { | |
2074 | if (!trace_print_context(iter)) | |
2075 | goto partial; | |
2076 | } | |
c4a8e8be | 2077 | } |
bc0c38d1 | 2078 | |
268ccda0 | 2079 | if (event) |
a9a57763 | 2080 | return event->funcs->trace(iter, sym_flags, event); |
d9793bd8 ACM |
2081 | |
2082 | if (!trace_seq_printf(s, "Unknown type %d\n", entry->type)) | |
2083 | goto partial; | |
02b67518 | 2084 | |
2c4f035f | 2085 | return TRACE_TYPE_HANDLED; |
d9793bd8 ACM |
2086 | partial: |
2087 | return TRACE_TYPE_PARTIAL_LINE; | |
bc0c38d1 SR |
2088 | } |
2089 | ||
2c4f035f | 2090 | static enum print_line_t print_raw_fmt(struct trace_iterator *iter) |
f9896bf3 IM |
2091 | { |
2092 | struct trace_seq *s = &iter->seq; | |
2093 | struct trace_entry *entry; | |
f633cef0 | 2094 | struct trace_event *event; |
f9896bf3 IM |
2095 | |
2096 | entry = iter->ent; | |
dd0e545f | 2097 | |
c4a8e8be | 2098 | if (trace_flags & TRACE_ITER_CONTEXT_INFO) { |
d9793bd8 ACM |
2099 | if (!trace_seq_printf(s, "%d %d %llu ", |
2100 | entry->pid, iter->cpu, iter->ts)) | |
2101 | goto partial; | |
c4a8e8be | 2102 | } |
f9896bf3 | 2103 | |
f633cef0 | 2104 | event = ftrace_find_event(entry->type); |
268ccda0 | 2105 | if (event) |
a9a57763 | 2106 | return event->funcs->raw(iter, 0, event); |
d9793bd8 ACM |
2107 | |
2108 | if (!trace_seq_printf(s, "%d ?\n", entry->type)) | |
2109 | goto partial; | |
777e208d | 2110 | |
2c4f035f | 2111 | return TRACE_TYPE_HANDLED; |
d9793bd8 ACM |
2112 | partial: |
2113 | return TRACE_TYPE_PARTIAL_LINE; | |
f9896bf3 IM |
2114 | } |
2115 | ||
2c4f035f | 2116 | static enum print_line_t print_hex_fmt(struct trace_iterator *iter) |
5e3ca0ec IM |
2117 | { |
2118 | struct trace_seq *s = &iter->seq; | |
2119 | unsigned char newline = '\n'; | |
2120 | struct trace_entry *entry; | |
f633cef0 | 2121 | struct trace_event *event; |
5e3ca0ec IM |
2122 | |
2123 | entry = iter->ent; | |
dd0e545f | 2124 | |
c4a8e8be FW |
2125 | if (trace_flags & TRACE_ITER_CONTEXT_INFO) { |
2126 | SEQ_PUT_HEX_FIELD_RET(s, entry->pid); | |
2127 | SEQ_PUT_HEX_FIELD_RET(s, iter->cpu); | |
2128 | SEQ_PUT_HEX_FIELD_RET(s, iter->ts); | |
2129 | } | |
5e3ca0ec | 2130 | |
f633cef0 | 2131 | event = ftrace_find_event(entry->type); |
268ccda0 | 2132 | if (event) { |
a9a57763 | 2133 | enum print_line_t ret = event->funcs->hex(iter, 0, event); |
d9793bd8 ACM |
2134 | if (ret != TRACE_TYPE_HANDLED) |
2135 | return ret; | |
2136 | } | |
7104f300 | 2137 | |
5e3ca0ec IM |
2138 | SEQ_PUT_FIELD_RET(s, newline); |
2139 | ||
2c4f035f | 2140 | return TRACE_TYPE_HANDLED; |
5e3ca0ec IM |
2141 | } |
2142 | ||
2c4f035f | 2143 | static enum print_line_t print_bin_fmt(struct trace_iterator *iter) |
cb0f12aa IM |
2144 | { |
2145 | struct trace_seq *s = &iter->seq; | |
2146 | struct trace_entry *entry; | |
f633cef0 | 2147 | struct trace_event *event; |
cb0f12aa IM |
2148 | |
2149 | entry = iter->ent; | |
dd0e545f | 2150 | |
c4a8e8be FW |
2151 | if (trace_flags & TRACE_ITER_CONTEXT_INFO) { |
2152 | SEQ_PUT_FIELD_RET(s, entry->pid); | |
1830b52d | 2153 | SEQ_PUT_FIELD_RET(s, iter->cpu); |
c4a8e8be FW |
2154 | SEQ_PUT_FIELD_RET(s, iter->ts); |
2155 | } | |
cb0f12aa | 2156 | |
f633cef0 | 2157 | event = ftrace_find_event(entry->type); |
a9a57763 SR |
2158 | return event ? event->funcs->binary(iter, 0, event) : |
2159 | TRACE_TYPE_HANDLED; | |
cb0f12aa IM |
2160 | } |
2161 | ||
62b915f1 | 2162 | int trace_empty(struct trace_iterator *iter) |
bc0c38d1 | 2163 | { |
bc0c38d1 SR |
2164 | int cpu; |
2165 | ||
9aba60fe SR |
2166 | /* If we are looking at one CPU buffer, only check that one */ |
2167 | if (iter->cpu_file != TRACE_PIPE_ALL_CPU) { | |
2168 | cpu = iter->cpu_file; | |
2169 | if (iter->buffer_iter[cpu]) { | |
2170 | if (!ring_buffer_iter_empty(iter->buffer_iter[cpu])) | |
2171 | return 0; | |
2172 | } else { | |
2173 | if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu)) | |
2174 | return 0; | |
2175 | } | |
2176 | return 1; | |
2177 | } | |
2178 | ||
ab46428c | 2179 | for_each_tracing_cpu(cpu) { |
d769041f SR |
2180 | if (iter->buffer_iter[cpu]) { |
2181 | if (!ring_buffer_iter_empty(iter->buffer_iter[cpu])) | |
2182 | return 0; | |
2183 | } else { | |
2184 | if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu)) | |
2185 | return 0; | |
2186 | } | |
bc0c38d1 | 2187 | } |
d769041f | 2188 | |
797d3712 | 2189 | return 1; |
bc0c38d1 SR |
2190 | } |
2191 | ||
4f535968 | 2192 | /* Called with trace_event_read_lock() held. */ |
955b61e5 | 2193 | enum print_line_t print_trace_line(struct trace_iterator *iter) |
f9896bf3 | 2194 | { |
2c4f035f FW |
2195 | enum print_line_t ret; |
2196 | ||
ee5e51f5 JO |
2197 | if (iter->lost_events && |
2198 | !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n", | |
2199 | iter->cpu, iter->lost_events)) | |
2200 | return TRACE_TYPE_PARTIAL_LINE; | |
bc21b478 | 2201 | |
2c4f035f FW |
2202 | if (iter->trace && iter->trace->print_line) { |
2203 | ret = iter->trace->print_line(iter); | |
2204 | if (ret != TRACE_TYPE_UNHANDLED) | |
2205 | return ret; | |
2206 | } | |
72829bc3 | 2207 | |
48ead020 FW |
2208 | if (iter->ent->type == TRACE_BPRINT && |
2209 | trace_flags & TRACE_ITER_PRINTK && | |
2210 | trace_flags & TRACE_ITER_PRINTK_MSGONLY) | |
5ef841f6 | 2211 | return trace_print_bprintk_msg_only(iter); |
48ead020 | 2212 | |
66896a85 FW |
2213 | if (iter->ent->type == TRACE_PRINT && |
2214 | trace_flags & TRACE_ITER_PRINTK && | |
2215 | trace_flags & TRACE_ITER_PRINTK_MSGONLY) | |
5ef841f6 | 2216 | return trace_print_printk_msg_only(iter); |
66896a85 | 2217 | |
cb0f12aa IM |
2218 | if (trace_flags & TRACE_ITER_BIN) |
2219 | return print_bin_fmt(iter); | |
2220 | ||
5e3ca0ec IM |
2221 | if (trace_flags & TRACE_ITER_HEX) |
2222 | return print_hex_fmt(iter); | |
2223 | ||
f9896bf3 IM |
2224 | if (trace_flags & TRACE_ITER_RAW) |
2225 | return print_raw_fmt(iter); | |
2226 | ||
f9896bf3 IM |
2227 | return print_trace_fmt(iter); |
2228 | } | |
2229 | ||
7e9a49ef JO |
2230 | void trace_latency_header(struct seq_file *m) |
2231 | { | |
2232 | struct trace_iterator *iter = m->private; | |
2233 | ||
2234 | /* print nothing if the buffers are empty */ | |
2235 | if (trace_empty(iter)) | |
2236 | return; | |
2237 | ||
2238 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) | |
2239 | print_trace_header(m, iter); | |
2240 | ||
2241 | if (!(trace_flags & TRACE_ITER_VERBOSE)) | |
2242 | print_lat_help_header(m); | |
2243 | } | |
2244 | ||
62b915f1 JO |
2245 | void trace_default_header(struct seq_file *m) |
2246 | { | |
2247 | struct trace_iterator *iter = m->private; | |
2248 | ||
f56e7f8e JO |
2249 | if (!(trace_flags & TRACE_ITER_CONTEXT_INFO)) |
2250 | return; | |
2251 | ||
62b915f1 JO |
2252 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) { |
2253 | /* print nothing if the buffers are empty */ | |
2254 | if (trace_empty(iter)) | |
2255 | return; | |
2256 | print_trace_header(m, iter); | |
2257 | if (!(trace_flags & TRACE_ITER_VERBOSE)) | |
2258 | print_lat_help_header(m); | |
2259 | } else { | |
77271ce4 SR |
2260 | if (!(trace_flags & TRACE_ITER_VERBOSE)) { |
2261 | if (trace_flags & TRACE_ITER_IRQ_INFO) | |
39eaf7ef | 2262 | print_func_help_header_irq(iter->tr, m); |
77271ce4 | 2263 | else |
39eaf7ef | 2264 | print_func_help_header(iter->tr, m); |
77271ce4 | 2265 | } |
62b915f1 JO |
2266 | } |
2267 | } | |
2268 | ||
e0a413f6 SR |
2269 | static void test_ftrace_alive(struct seq_file *m) |
2270 | { | |
2271 | if (!ftrace_is_dead()) | |
2272 | return; | |
2273 | seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"); | |
2274 | seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n"); | |
2275 | } | |
2276 | ||
bc0c38d1 SR |
2277 | static int s_show(struct seq_file *m, void *v) |
2278 | { | |
2279 | struct trace_iterator *iter = v; | |
a63ce5b3 | 2280 | int ret; |
bc0c38d1 SR |
2281 | |
2282 | if (iter->ent == NULL) { | |
2283 | if (iter->tr) { | |
2284 | seq_printf(m, "# tracer: %s\n", iter->trace->name); | |
2285 | seq_puts(m, "#\n"); | |
e0a413f6 | 2286 | test_ftrace_alive(m); |
bc0c38d1 | 2287 | } |
8bba1bf5 MM |
2288 | if (iter->trace && iter->trace->print_header) |
2289 | iter->trace->print_header(m); | |
62b915f1 JO |
2290 | else |
2291 | trace_default_header(m); | |
2292 | ||
a63ce5b3 SR |
2293 | } else if (iter->leftover) { |
2294 | /* | |
2295 | * If we filled the seq_file buffer earlier, we | |
2296 | * want to just show it now. | |
2297 | */ | |
2298 | ret = trace_print_seq(m, &iter->seq); | |
2299 | ||
2300 | /* ret should this time be zero, but you never know */ | |
2301 | iter->leftover = ret; | |
2302 | ||
bc0c38d1 | 2303 | } else { |
f9896bf3 | 2304 | print_trace_line(iter); |
a63ce5b3 SR |
2305 | ret = trace_print_seq(m, &iter->seq); |
2306 | /* | |
2307 | * If we overflow the seq_file buffer, then it will | |
2308 | * ask us for this data again at start up. | |
2309 | * Use that instead. | |
2310 | * ret is 0 if seq_file write succeeded. | |
2311 | * -1 otherwise. | |
2312 | */ | |
2313 | iter->leftover = ret; | |
bc0c38d1 SR |
2314 | } |
2315 | ||
2316 | return 0; | |
2317 | } | |
2318 | ||
88e9d34c | 2319 | static const struct seq_operations tracer_seq_ops = { |
4bf39a94 IM |
2320 | .start = s_start, |
2321 | .next = s_next, | |
2322 | .stop = s_stop, | |
2323 | .show = s_show, | |
bc0c38d1 SR |
2324 | }; |
2325 | ||
e309b41d | 2326 | static struct trace_iterator * |
85a2f9b4 | 2327 | __tracing_open(struct inode *inode, struct file *file) |
bc0c38d1 | 2328 | { |
b04cc6b1 | 2329 | long cpu_file = (long) inode->i_private; |
85a2f9b4 | 2330 | void *fail_ret = ERR_PTR(-ENOMEM); |
bc0c38d1 | 2331 | struct trace_iterator *iter; |
3928a8a2 | 2332 | struct seq_file *m; |
85a2f9b4 | 2333 | int cpu, ret; |
bc0c38d1 | 2334 | |
85a2f9b4 SR |
2335 | if (tracing_disabled) |
2336 | return ERR_PTR(-ENODEV); | |
60a11774 | 2337 | |
bc0c38d1 | 2338 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
85a2f9b4 SR |
2339 | if (!iter) |
2340 | return ERR_PTR(-ENOMEM); | |
bc0c38d1 | 2341 | |
d7350c3f FW |
2342 | /* |
2343 | * We make a copy of the current tracer to avoid concurrent | |
2344 | * changes on it while we are reading. | |
2345 | */ | |
bc0c38d1 | 2346 | mutex_lock(&trace_types_lock); |
d7350c3f | 2347 | iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL); |
85a2f9b4 | 2348 | if (!iter->trace) |
d7350c3f | 2349 | goto fail; |
85a2f9b4 | 2350 | |
d7350c3f FW |
2351 | if (current_trace) |
2352 | *iter->trace = *current_trace; | |
2353 | ||
79f55997 | 2354 | if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL)) |
b0dfa978 FW |
2355 | goto fail; |
2356 | ||
bc0c38d1 SR |
2357 | if (current_trace && current_trace->print_max) |
2358 | iter->tr = &max_tr; | |
2359 | else | |
b04cc6b1 | 2360 | iter->tr = &global_trace; |
bc0c38d1 | 2361 | iter->pos = -1; |
d7350c3f | 2362 | mutex_init(&iter->mutex); |
b04cc6b1 | 2363 | iter->cpu_file = cpu_file; |
bc0c38d1 | 2364 | |
8bba1bf5 MM |
2365 | /* Notify the tracer early; before we stop tracing. */ |
2366 | if (iter->trace && iter->trace->open) | |
a93751ca | 2367 | iter->trace->open(iter); |
8bba1bf5 | 2368 | |
12ef7d44 SR |
2369 | /* Annotate start of buffers if we had overruns */ |
2370 | if (ring_buffer_overruns(iter->tr->buffer)) | |
2371 | iter->iter_flags |= TRACE_FILE_ANNOTATE; | |
2372 | ||
2f26ebd5 SR |
2373 | /* stop the trace while dumping */ |
2374 | tracing_stop(); | |
2375 | ||
b04cc6b1 FW |
2376 | if (iter->cpu_file == TRACE_PIPE_ALL_CPU) { |
2377 | for_each_tracing_cpu(cpu) { | |
b04cc6b1 | 2378 | iter->buffer_iter[cpu] = |
72c9ddfd DM |
2379 | ring_buffer_read_prepare(iter->tr->buffer, cpu); |
2380 | } | |
2381 | ring_buffer_read_prepare_sync(); | |
2382 | for_each_tracing_cpu(cpu) { | |
2383 | ring_buffer_read_start(iter->buffer_iter[cpu]); | |
2f26ebd5 | 2384 | tracing_iter_reset(iter, cpu); |
b04cc6b1 FW |
2385 | } |
2386 | } else { | |
2387 | cpu = iter->cpu_file; | |
3928a8a2 | 2388 | iter->buffer_iter[cpu] = |
72c9ddfd DM |
2389 | ring_buffer_read_prepare(iter->tr->buffer, cpu); |
2390 | ring_buffer_read_prepare_sync(); | |
2391 | ring_buffer_read_start(iter->buffer_iter[cpu]); | |
2f26ebd5 | 2392 | tracing_iter_reset(iter, cpu); |
3928a8a2 SR |
2393 | } |
2394 | ||
85a2f9b4 SR |
2395 | ret = seq_open(file, &tracer_seq_ops); |
2396 | if (ret < 0) { | |
2397 | fail_ret = ERR_PTR(ret); | |
3928a8a2 | 2398 | goto fail_buffer; |
85a2f9b4 | 2399 | } |
bc0c38d1 | 2400 | |
3928a8a2 SR |
2401 | m = file->private_data; |
2402 | m->private = iter; | |
bc0c38d1 | 2403 | |
bc0c38d1 SR |
2404 | mutex_unlock(&trace_types_lock); |
2405 | ||
bc0c38d1 | 2406 | return iter; |
3928a8a2 SR |
2407 | |
2408 | fail_buffer: | |
2409 | for_each_tracing_cpu(cpu) { | |
2410 | if (iter->buffer_iter[cpu]) | |
2411 | ring_buffer_read_finish(iter->buffer_iter[cpu]); | |
2412 | } | |
b0dfa978 | 2413 | free_cpumask_var(iter->started); |
2f26ebd5 | 2414 | tracing_start(); |
d7350c3f | 2415 | fail: |
3928a8a2 | 2416 | mutex_unlock(&trace_types_lock); |
d7350c3f | 2417 | kfree(iter->trace); |
0bb943c7 | 2418 | kfree(iter); |
3928a8a2 | 2419 | |
85a2f9b4 | 2420 | return fail_ret; |
bc0c38d1 SR |
2421 | } |
2422 | ||
2423 | int tracing_open_generic(struct inode *inode, struct file *filp) | |
2424 | { | |
60a11774 SR |
2425 | if (tracing_disabled) |
2426 | return -ENODEV; | |
2427 | ||
bc0c38d1 SR |
2428 | filp->private_data = inode->i_private; |
2429 | return 0; | |
2430 | } | |
2431 | ||
4fd27358 | 2432 | static int tracing_release(struct inode *inode, struct file *file) |
bc0c38d1 | 2433 | { |
907f2784 | 2434 | struct seq_file *m = file->private_data; |
4acd4d00 | 2435 | struct trace_iterator *iter; |
3928a8a2 | 2436 | int cpu; |
bc0c38d1 | 2437 | |
4acd4d00 SR |
2438 | if (!(file->f_mode & FMODE_READ)) |
2439 | return 0; | |
2440 | ||
2441 | iter = m->private; | |
2442 | ||
bc0c38d1 | 2443 | mutex_lock(&trace_types_lock); |
3928a8a2 SR |
2444 | for_each_tracing_cpu(cpu) { |
2445 | if (iter->buffer_iter[cpu]) | |
2446 | ring_buffer_read_finish(iter->buffer_iter[cpu]); | |
2447 | } | |
2448 | ||
bc0c38d1 SR |
2449 | if (iter->trace && iter->trace->close) |
2450 | iter->trace->close(iter); | |
2451 | ||
2452 | /* reenable tracing if it was previously enabled */ | |
9036990d | 2453 | tracing_start(); |
bc0c38d1 SR |
2454 | mutex_unlock(&trace_types_lock); |
2455 | ||
2456 | seq_release(inode, file); | |
d7350c3f | 2457 | mutex_destroy(&iter->mutex); |
b0dfa978 | 2458 | free_cpumask_var(iter->started); |
d7350c3f | 2459 | kfree(iter->trace); |
bc0c38d1 SR |
2460 | kfree(iter); |
2461 | return 0; | |
2462 | } | |
2463 | ||
2464 | static int tracing_open(struct inode *inode, struct file *file) | |
2465 | { | |
85a2f9b4 SR |
2466 | struct trace_iterator *iter; |
2467 | int ret = 0; | |
bc0c38d1 | 2468 | |
4acd4d00 SR |
2469 | /* If this file was open for write, then erase contents */ |
2470 | if ((file->f_mode & FMODE_WRITE) && | |
8650ae32 | 2471 | (file->f_flags & O_TRUNC)) { |
4acd4d00 | 2472 | long cpu = (long) inode->i_private; |
bc0c38d1 | 2473 | |
4acd4d00 SR |
2474 | if (cpu == TRACE_PIPE_ALL_CPU) |
2475 | tracing_reset_online_cpus(&global_trace); | |
2476 | else | |
2477 | tracing_reset(&global_trace, cpu); | |
2478 | } | |
bc0c38d1 | 2479 | |
4acd4d00 SR |
2480 | if (file->f_mode & FMODE_READ) { |
2481 | iter = __tracing_open(inode, file); | |
2482 | if (IS_ERR(iter)) | |
2483 | ret = PTR_ERR(iter); | |
2484 | else if (trace_flags & TRACE_ITER_LATENCY_FMT) | |
2485 | iter->iter_flags |= TRACE_FILE_LAT_FMT; | |
2486 | } | |
bc0c38d1 SR |
2487 | return ret; |
2488 | } | |
2489 | ||
e309b41d | 2490 | static void * |
bc0c38d1 SR |
2491 | t_next(struct seq_file *m, void *v, loff_t *pos) |
2492 | { | |
f129e965 | 2493 | struct tracer *t = v; |
bc0c38d1 SR |
2494 | |
2495 | (*pos)++; | |
2496 | ||
2497 | if (t) | |
2498 | t = t->next; | |
2499 | ||
bc0c38d1 SR |
2500 | return t; |
2501 | } | |
2502 | ||
2503 | static void *t_start(struct seq_file *m, loff_t *pos) | |
2504 | { | |
f129e965 | 2505 | struct tracer *t; |
bc0c38d1 SR |
2506 | loff_t l = 0; |
2507 | ||
2508 | mutex_lock(&trace_types_lock); | |
f129e965 | 2509 | for (t = trace_types; t && l < *pos; t = t_next(m, t, &l)) |
bc0c38d1 SR |
2510 | ; |
2511 | ||
2512 | return t; | |
2513 | } | |
2514 | ||
2515 | static void t_stop(struct seq_file *m, void *p) | |
2516 | { | |
2517 | mutex_unlock(&trace_types_lock); | |
2518 | } | |
2519 | ||
2520 | static int t_show(struct seq_file *m, void *v) | |
2521 | { | |
2522 | struct tracer *t = v; | |
2523 | ||
2524 | if (!t) | |
2525 | return 0; | |
2526 | ||
2527 | seq_printf(m, "%s", t->name); | |
2528 | if (t->next) | |
2529 | seq_putc(m, ' '); | |
2530 | else | |
2531 | seq_putc(m, '\n'); | |
2532 | ||
2533 | return 0; | |
2534 | } | |
2535 | ||
88e9d34c | 2536 | static const struct seq_operations show_traces_seq_ops = { |
4bf39a94 IM |
2537 | .start = t_start, |
2538 | .next = t_next, | |
2539 | .stop = t_stop, | |
2540 | .show = t_show, | |
bc0c38d1 SR |
2541 | }; |
2542 | ||
2543 | static int show_traces_open(struct inode *inode, struct file *file) | |
2544 | { | |
60a11774 SR |
2545 | if (tracing_disabled) |
2546 | return -ENODEV; | |
2547 | ||
f129e965 | 2548 | return seq_open(file, &show_traces_seq_ops); |
bc0c38d1 SR |
2549 | } |
2550 | ||
4acd4d00 SR |
2551 | static ssize_t |
2552 | tracing_write_stub(struct file *filp, const char __user *ubuf, | |
2553 | size_t count, loff_t *ppos) | |
2554 | { | |
2555 | return count; | |
2556 | } | |
2557 | ||
364829b1 SP |
2558 | static loff_t tracing_seek(struct file *file, loff_t offset, int origin) |
2559 | { | |
2560 | if (file->f_mode & FMODE_READ) | |
2561 | return seq_lseek(file, offset, origin); | |
2562 | else | |
2563 | return 0; | |
2564 | } | |
2565 | ||
5e2336a0 | 2566 | static const struct file_operations tracing_fops = { |
4bf39a94 IM |
2567 | .open = tracing_open, |
2568 | .read = seq_read, | |
4acd4d00 | 2569 | .write = tracing_write_stub, |
364829b1 | 2570 | .llseek = tracing_seek, |
4bf39a94 | 2571 | .release = tracing_release, |
bc0c38d1 SR |
2572 | }; |
2573 | ||
5e2336a0 | 2574 | static const struct file_operations show_traces_fops = { |
c7078de1 IM |
2575 | .open = show_traces_open, |
2576 | .read = seq_read, | |
2577 | .release = seq_release, | |
b444786f | 2578 | .llseek = seq_lseek, |
c7078de1 IM |
2579 | }; |
2580 | ||
36dfe925 IM |
2581 | /* |
2582 | * Only trace on a CPU if the bitmask is set: | |
2583 | */ | |
9e01c1b7 | 2584 | static cpumask_var_t tracing_cpumask; |
36dfe925 IM |
2585 | |
2586 | /* | |
2587 | * The tracer itself will not take this lock, but still we want | |
2588 | * to provide a consistent cpumask to user-space: | |
2589 | */ | |
2590 | static DEFINE_MUTEX(tracing_cpumask_update_lock); | |
2591 | ||
2592 | /* | |
2593 | * Temporary storage for the character representation of the | |
2594 | * CPU bitmask (and one more byte for the newline): | |
2595 | */ | |
2596 | static char mask_str[NR_CPUS + 1]; | |
2597 | ||
c7078de1 IM |
2598 | static ssize_t |
2599 | tracing_cpumask_read(struct file *filp, char __user *ubuf, | |
2600 | size_t count, loff_t *ppos) | |
2601 | { | |
36dfe925 | 2602 | int len; |
c7078de1 IM |
2603 | |
2604 | mutex_lock(&tracing_cpumask_update_lock); | |
36dfe925 | 2605 | |
9e01c1b7 | 2606 | len = cpumask_scnprintf(mask_str, count, tracing_cpumask); |
36dfe925 IM |
2607 | if (count - len < 2) { |
2608 | count = -EINVAL; | |
2609 | goto out_err; | |
2610 | } | |
2611 | len += sprintf(mask_str + len, "\n"); | |
2612 | count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1); | |
2613 | ||
2614 | out_err: | |
c7078de1 IM |
2615 | mutex_unlock(&tracing_cpumask_update_lock); |
2616 | ||
2617 | return count; | |
2618 | } | |
2619 | ||
2620 | static ssize_t | |
2621 | tracing_cpumask_write(struct file *filp, const char __user *ubuf, | |
2622 | size_t count, loff_t *ppos) | |
2623 | { | |
36dfe925 | 2624 | int err, cpu; |
9e01c1b7 RR |
2625 | cpumask_var_t tracing_cpumask_new; |
2626 | ||
2627 | if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL)) | |
2628 | return -ENOMEM; | |
c7078de1 | 2629 | |
9e01c1b7 | 2630 | err = cpumask_parse_user(ubuf, count, tracing_cpumask_new); |
c7078de1 | 2631 | if (err) |
36dfe925 IM |
2632 | goto err_unlock; |
2633 | ||
215368e8 LZ |
2634 | mutex_lock(&tracing_cpumask_update_lock); |
2635 | ||
a5e25883 | 2636 | local_irq_disable(); |
0199c4e6 | 2637 | arch_spin_lock(&ftrace_max_lock); |
ab46428c | 2638 | for_each_tracing_cpu(cpu) { |
36dfe925 IM |
2639 | /* |
2640 | * Increase/decrease the disabled counter if we are | |
2641 | * about to flip a bit in the cpumask: | |
2642 | */ | |
9e01c1b7 RR |
2643 | if (cpumask_test_cpu(cpu, tracing_cpumask) && |
2644 | !cpumask_test_cpu(cpu, tracing_cpumask_new)) { | |
36dfe925 IM |
2645 | atomic_inc(&global_trace.data[cpu]->disabled); |
2646 | } | |
9e01c1b7 RR |
2647 | if (!cpumask_test_cpu(cpu, tracing_cpumask) && |
2648 | cpumask_test_cpu(cpu, tracing_cpumask_new)) { | |
36dfe925 IM |
2649 | atomic_dec(&global_trace.data[cpu]->disabled); |
2650 | } | |
2651 | } | |
0199c4e6 | 2652 | arch_spin_unlock(&ftrace_max_lock); |
a5e25883 | 2653 | local_irq_enable(); |
36dfe925 | 2654 | |
9e01c1b7 | 2655 | cpumask_copy(tracing_cpumask, tracing_cpumask_new); |
36dfe925 IM |
2656 | |
2657 | mutex_unlock(&tracing_cpumask_update_lock); | |
9e01c1b7 | 2658 | free_cpumask_var(tracing_cpumask_new); |
c7078de1 IM |
2659 | |
2660 | return count; | |
36dfe925 IM |
2661 | |
2662 | err_unlock: | |
215368e8 | 2663 | free_cpumask_var(tracing_cpumask_new); |
36dfe925 IM |
2664 | |
2665 | return err; | |
c7078de1 IM |
2666 | } |
2667 | ||
5e2336a0 | 2668 | static const struct file_operations tracing_cpumask_fops = { |
c7078de1 IM |
2669 | .open = tracing_open_generic, |
2670 | .read = tracing_cpumask_read, | |
2671 | .write = tracing_cpumask_write, | |
b444786f | 2672 | .llseek = generic_file_llseek, |
bc0c38d1 SR |
2673 | }; |
2674 | ||
fdb372ed | 2675 | static int tracing_trace_options_show(struct seq_file *m, void *v) |
bc0c38d1 | 2676 | { |
d8e83d26 SR |
2677 | struct tracer_opt *trace_opts; |
2678 | u32 tracer_flags; | |
d8e83d26 | 2679 | int i; |
adf9f195 | 2680 | |
d8e83d26 SR |
2681 | mutex_lock(&trace_types_lock); |
2682 | tracer_flags = current_trace->flags->val; | |
2683 | trace_opts = current_trace->flags->opts; | |
2684 | ||
bc0c38d1 SR |
2685 | for (i = 0; trace_options[i]; i++) { |
2686 | if (trace_flags & (1 << i)) | |
fdb372ed | 2687 | seq_printf(m, "%s\n", trace_options[i]); |
bc0c38d1 | 2688 | else |
fdb372ed | 2689 | seq_printf(m, "no%s\n", trace_options[i]); |
bc0c38d1 SR |
2690 | } |
2691 | ||
adf9f195 FW |
2692 | for (i = 0; trace_opts[i].name; i++) { |
2693 | if (tracer_flags & trace_opts[i].bit) | |
fdb372ed | 2694 | seq_printf(m, "%s\n", trace_opts[i].name); |
adf9f195 | 2695 | else |
fdb372ed | 2696 | seq_printf(m, "no%s\n", trace_opts[i].name); |
adf9f195 | 2697 | } |
d8e83d26 | 2698 | mutex_unlock(&trace_types_lock); |
adf9f195 | 2699 | |
fdb372ed | 2700 | return 0; |
bc0c38d1 | 2701 | } |
bc0c38d1 | 2702 | |
8d18eaaf LZ |
2703 | static int __set_tracer_option(struct tracer *trace, |
2704 | struct tracer_flags *tracer_flags, | |
2705 | struct tracer_opt *opts, int neg) | |
2706 | { | |
2707 | int ret; | |
bc0c38d1 | 2708 | |
8d18eaaf LZ |
2709 | ret = trace->set_flag(tracer_flags->val, opts->bit, !neg); |
2710 | if (ret) | |
2711 | return ret; | |
2712 | ||
2713 | if (neg) | |
2714 | tracer_flags->val &= ~opts->bit; | |
2715 | else | |
2716 | tracer_flags->val |= opts->bit; | |
2717 | return 0; | |
bc0c38d1 SR |
2718 | } |
2719 | ||
adf9f195 FW |
2720 | /* Try to assign a tracer specific option */ |
2721 | static int set_tracer_option(struct tracer *trace, char *cmp, int neg) | |
2722 | { | |
7770841e | 2723 | struct tracer_flags *tracer_flags = trace->flags; |
adf9f195 | 2724 | struct tracer_opt *opts = NULL; |
8d18eaaf | 2725 | int i; |
adf9f195 | 2726 | |
7770841e Z |
2727 | for (i = 0; tracer_flags->opts[i].name; i++) { |
2728 | opts = &tracer_flags->opts[i]; | |
adf9f195 | 2729 | |
8d18eaaf LZ |
2730 | if (strcmp(cmp, opts->name) == 0) |
2731 | return __set_tracer_option(trace, trace->flags, | |
2732 | opts, neg); | |
adf9f195 | 2733 | } |
adf9f195 | 2734 | |
8d18eaaf | 2735 | return -EINVAL; |
adf9f195 FW |
2736 | } |
2737 | ||
af4617bd SR |
2738 | static void set_tracer_flags(unsigned int mask, int enabled) |
2739 | { | |
2740 | /* do nothing if flag is already set */ | |
2741 | if (!!(trace_flags & mask) == !!enabled) | |
2742 | return; | |
2743 | ||
2744 | if (enabled) | |
2745 | trace_flags |= mask; | |
2746 | else | |
2747 | trace_flags &= ~mask; | |
e870e9a1 LZ |
2748 | |
2749 | if (mask == TRACE_ITER_RECORD_CMD) | |
2750 | trace_event_enable_cmd_record(enabled); | |
750912fa DS |
2751 | |
2752 | if (mask == TRACE_ITER_OVERWRITE) | |
2753 | ring_buffer_change_overwrite(global_trace.buffer, enabled); | |
af4617bd SR |
2754 | } |
2755 | ||
bc0c38d1 | 2756 | static ssize_t |
ee6bce52 | 2757 | tracing_trace_options_write(struct file *filp, const char __user *ubuf, |
bc0c38d1 SR |
2758 | size_t cnt, loff_t *ppos) |
2759 | { | |
2760 | char buf[64]; | |
8d18eaaf | 2761 | char *cmp; |
bc0c38d1 | 2762 | int neg = 0; |
adf9f195 | 2763 | int ret; |
bc0c38d1 SR |
2764 | int i; |
2765 | ||
cffae437 SR |
2766 | if (cnt >= sizeof(buf)) |
2767 | return -EINVAL; | |
bc0c38d1 SR |
2768 | |
2769 | if (copy_from_user(&buf, ubuf, cnt)) | |
2770 | return -EFAULT; | |
2771 | ||
2772 | buf[cnt] = 0; | |
8d18eaaf | 2773 | cmp = strstrip(buf); |
bc0c38d1 | 2774 | |
8d18eaaf | 2775 | if (strncmp(cmp, "no", 2) == 0) { |
bc0c38d1 SR |
2776 | neg = 1; |
2777 | cmp += 2; | |
2778 | } | |
2779 | ||
2780 | for (i = 0; trace_options[i]; i++) { | |
8d18eaaf | 2781 | if (strcmp(cmp, trace_options[i]) == 0) { |
af4617bd | 2782 | set_tracer_flags(1 << i, !neg); |
bc0c38d1 SR |
2783 | break; |
2784 | } | |
2785 | } | |
adf9f195 FW |
2786 | |
2787 | /* If no option could be set, test the specific tracer options */ | |
2788 | if (!trace_options[i]) { | |
d8e83d26 | 2789 | mutex_lock(&trace_types_lock); |
adf9f195 | 2790 | ret = set_tracer_option(current_trace, cmp, neg); |
d8e83d26 | 2791 | mutex_unlock(&trace_types_lock); |
adf9f195 FW |
2792 | if (ret) |
2793 | return ret; | |
2794 | } | |
bc0c38d1 | 2795 | |
cf8517cf | 2796 | *ppos += cnt; |
bc0c38d1 SR |
2797 | |
2798 | return cnt; | |
2799 | } | |
2800 | ||
fdb372ed LZ |
2801 | static int tracing_trace_options_open(struct inode *inode, struct file *file) |
2802 | { | |
2803 | if (tracing_disabled) | |
2804 | return -ENODEV; | |
2805 | return single_open(file, tracing_trace_options_show, NULL); | |
2806 | } | |
2807 | ||
5e2336a0 | 2808 | static const struct file_operations tracing_iter_fops = { |
fdb372ed LZ |
2809 | .open = tracing_trace_options_open, |
2810 | .read = seq_read, | |
2811 | .llseek = seq_lseek, | |
2812 | .release = single_release, | |
ee6bce52 | 2813 | .write = tracing_trace_options_write, |
bc0c38d1 SR |
2814 | }; |
2815 | ||
7bd2f24c IM |
2816 | static const char readme_msg[] = |
2817 | "tracing mini-HOWTO:\n\n" | |
156f5a78 GL |
2818 | "# mount -t debugfs nodev /sys/kernel/debug\n\n" |
2819 | "# cat /sys/kernel/debug/tracing/available_tracers\n" | |
1e42e83f | 2820 | "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n" |
156f5a78 | 2821 | "# cat /sys/kernel/debug/tracing/current_tracer\n" |
bc2b6871 | 2822 | "nop\n" |
1e42e83f | 2823 | "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n" |
156f5a78 | 2824 | "# cat /sys/kernel/debug/tracing/current_tracer\n" |
1e42e83f | 2825 | "wakeup\n" |
156f5a78 | 2826 | "# cat /sys/kernel/debug/tracing/trace_options\n" |
7bd2f24c | 2827 | "noprint-parent nosym-offset nosym-addr noverbose\n" |
156f5a78 | 2828 | "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n" |
9b5f8b31 | 2829 | "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n" |
156f5a78 | 2830 | "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n" |
9b5f8b31 | 2831 | "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n" |
7bd2f24c IM |
2832 | ; |
2833 | ||
2834 | static ssize_t | |
2835 | tracing_readme_read(struct file *filp, char __user *ubuf, | |
2836 | size_t cnt, loff_t *ppos) | |
2837 | { | |
2838 | return simple_read_from_buffer(ubuf, cnt, ppos, | |
2839 | readme_msg, strlen(readme_msg)); | |
2840 | } | |
2841 | ||
5e2336a0 | 2842 | static const struct file_operations tracing_readme_fops = { |
c7078de1 IM |
2843 | .open = tracing_open_generic, |
2844 | .read = tracing_readme_read, | |
b444786f | 2845 | .llseek = generic_file_llseek, |
7bd2f24c IM |
2846 | }; |
2847 | ||
69abe6a5 AP |
2848 | static ssize_t |
2849 | tracing_saved_cmdlines_read(struct file *file, char __user *ubuf, | |
2850 | size_t cnt, loff_t *ppos) | |
2851 | { | |
2852 | char *buf_comm; | |
2853 | char *file_buf; | |
2854 | char *buf; | |
2855 | int len = 0; | |
2856 | int pid; | |
2857 | int i; | |
2858 | ||
2859 | file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL); | |
2860 | if (!file_buf) | |
2861 | return -ENOMEM; | |
2862 | ||
2863 | buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL); | |
2864 | if (!buf_comm) { | |
2865 | kfree(file_buf); | |
2866 | return -ENOMEM; | |
2867 | } | |
2868 | ||
2869 | buf = file_buf; | |
2870 | ||
2871 | for (i = 0; i < SAVED_CMDLINES; i++) { | |
2872 | int r; | |
2873 | ||
2874 | pid = map_cmdline_to_pid[i]; | |
2875 | if (pid == -1 || pid == NO_CMDLINE_MAP) | |
2876 | continue; | |
2877 | ||
2878 | trace_find_cmdline(pid, buf_comm); | |
2879 | r = sprintf(buf, "%d %s\n", pid, buf_comm); | |
2880 | buf += r; | |
2881 | len += r; | |
2882 | } | |
2883 | ||
2884 | len = simple_read_from_buffer(ubuf, cnt, ppos, | |
2885 | file_buf, len); | |
2886 | ||
2887 | kfree(file_buf); | |
2888 | kfree(buf_comm); | |
2889 | ||
2890 | return len; | |
2891 | } | |
2892 | ||
2893 | static const struct file_operations tracing_saved_cmdlines_fops = { | |
2894 | .open = tracing_open_generic, | |
2895 | .read = tracing_saved_cmdlines_read, | |
b444786f | 2896 | .llseek = generic_file_llseek, |
69abe6a5 AP |
2897 | }; |
2898 | ||
bc0c38d1 SR |
2899 | static ssize_t |
2900 | tracing_ctrl_read(struct file *filp, char __user *ubuf, | |
2901 | size_t cnt, loff_t *ppos) | |
2902 | { | |
bc0c38d1 SR |
2903 | char buf[64]; |
2904 | int r; | |
2905 | ||
9036990d | 2906 | r = sprintf(buf, "%u\n", tracer_enabled); |
4e3c3333 | 2907 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
bc0c38d1 SR |
2908 | } |
2909 | ||
2910 | static ssize_t | |
2911 | tracing_ctrl_write(struct file *filp, const char __user *ubuf, | |
2912 | size_t cnt, loff_t *ppos) | |
2913 | { | |
2914 | struct trace_array *tr = filp->private_data; | |
5e39841c | 2915 | unsigned long val; |
c6caeeb1 | 2916 | int ret; |
bc0c38d1 | 2917 | |
22fe9b54 PH |
2918 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
2919 | if (ret) | |
c6caeeb1 | 2920 | return ret; |
bc0c38d1 SR |
2921 | |
2922 | val = !!val; | |
2923 | ||
2924 | mutex_lock(&trace_types_lock); | |
9036990d | 2925 | if (tracer_enabled ^ val) { |
6752ab4a SR |
2926 | |
2927 | /* Only need to warn if this is used to change the state */ | |
2928 | WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on"); | |
2929 | ||
9036990d | 2930 | if (val) { |
bc0c38d1 | 2931 | tracer_enabled = 1; |
9036990d SR |
2932 | if (current_trace->start) |
2933 | current_trace->start(tr); | |
2934 | tracing_start(); | |
2935 | } else { | |
bc0c38d1 | 2936 | tracer_enabled = 0; |
9036990d SR |
2937 | tracing_stop(); |
2938 | if (current_trace->stop) | |
2939 | current_trace->stop(tr); | |
2940 | } | |
bc0c38d1 SR |
2941 | } |
2942 | mutex_unlock(&trace_types_lock); | |
2943 | ||
cf8517cf | 2944 | *ppos += cnt; |
bc0c38d1 SR |
2945 | |
2946 | return cnt; | |
2947 | } | |
2948 | ||
2949 | static ssize_t | |
2950 | tracing_set_trace_read(struct file *filp, char __user *ubuf, | |
2951 | size_t cnt, loff_t *ppos) | |
2952 | { | |
ee6c2c1b | 2953 | char buf[MAX_TRACER_SIZE+2]; |
bc0c38d1 SR |
2954 | int r; |
2955 | ||
2956 | mutex_lock(&trace_types_lock); | |
2957 | if (current_trace) | |
2958 | r = sprintf(buf, "%s\n", current_trace->name); | |
2959 | else | |
2960 | r = sprintf(buf, "\n"); | |
2961 | mutex_unlock(&trace_types_lock); | |
2962 | ||
4bf39a94 | 2963 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
bc0c38d1 SR |
2964 | } |
2965 | ||
b6f11df2 ACM |
2966 | int tracer_init(struct tracer *t, struct trace_array *tr) |
2967 | { | |
2968 | tracing_reset_online_cpus(tr); | |
2969 | return t->init(tr); | |
2970 | } | |
2971 | ||
4f271a2a | 2972 | static int __tracing_resize_ring_buffer(unsigned long size) |
73c5162a SR |
2973 | { |
2974 | int ret; | |
2975 | ||
2976 | /* | |
2977 | * If kernel or user changes the size of the ring buffer | |
a123c52b SR |
2978 | * we use the size that was given, and we can forget about |
2979 | * expanding it later. | |
73c5162a SR |
2980 | */ |
2981 | ring_buffer_expanded = 1; | |
2982 | ||
2983 | ret = ring_buffer_resize(global_trace.buffer, size); | |
2984 | if (ret < 0) | |
2985 | return ret; | |
2986 | ||
ef710e10 KM |
2987 | if (!current_trace->use_max_tr) |
2988 | goto out; | |
2989 | ||
73c5162a SR |
2990 | ret = ring_buffer_resize(max_tr.buffer, size); |
2991 | if (ret < 0) { | |
2992 | int r; | |
2993 | ||
2994 | r = ring_buffer_resize(global_trace.buffer, | |
2995 | global_trace.entries); | |
2996 | if (r < 0) { | |
a123c52b SR |
2997 | /* |
2998 | * AARGH! We are left with different | |
2999 | * size max buffer!!!! | |
3000 | * The max buffer is our "snapshot" buffer. | |
3001 | * When a tracer needs a snapshot (one of the | |
3002 | * latency tracers), it swaps the max buffer | |
3003 | * with the saved snap shot. We succeeded to | |
3004 | * update the size of the main buffer, but failed to | |
3005 | * update the size of the max buffer. But when we tried | |
3006 | * to reset the main buffer to the original size, we | |
3007 | * failed there too. This is very unlikely to | |
3008 | * happen, but if it does, warn and kill all | |
3009 | * tracing. | |
3010 | */ | |
73c5162a SR |
3011 | WARN_ON(1); |
3012 | tracing_disabled = 1; | |
3013 | } | |
3014 | return ret; | |
3015 | } | |
3016 | ||
ef710e10 KM |
3017 | max_tr.entries = size; |
3018 | out: | |
73c5162a SR |
3019 | global_trace.entries = size; |
3020 | ||
3021 | return ret; | |
3022 | } | |
3023 | ||
4f271a2a VN |
3024 | static ssize_t tracing_resize_ring_buffer(unsigned long size) |
3025 | { | |
3026 | int cpu, ret = size; | |
3027 | ||
3028 | mutex_lock(&trace_types_lock); | |
3029 | ||
3030 | tracing_stop(); | |
3031 | ||
3032 | /* disable all cpu buffers */ | |
3033 | for_each_tracing_cpu(cpu) { | |
3034 | if (global_trace.data[cpu]) | |
3035 | atomic_inc(&global_trace.data[cpu]->disabled); | |
3036 | if (max_tr.data[cpu]) | |
3037 | atomic_inc(&max_tr.data[cpu]->disabled); | |
3038 | } | |
3039 | ||
3040 | if (size != global_trace.entries) | |
3041 | ret = __tracing_resize_ring_buffer(size); | |
3042 | ||
3043 | if (ret < 0) | |
3044 | ret = -ENOMEM; | |
3045 | ||
3046 | for_each_tracing_cpu(cpu) { | |
3047 | if (global_trace.data[cpu]) | |
3048 | atomic_dec(&global_trace.data[cpu]->disabled); | |
3049 | if (max_tr.data[cpu]) | |
3050 | atomic_dec(&max_tr.data[cpu]->disabled); | |
3051 | } | |
3052 | ||
3053 | tracing_start(); | |
3054 | mutex_unlock(&trace_types_lock); | |
3055 | ||
3056 | return ret; | |
3057 | } | |
3058 | ||
ef710e10 | 3059 | |
1852fcce SR |
3060 | /** |
3061 | * tracing_update_buffers - used by tracing facility to expand ring buffers | |
3062 | * | |
3063 | * To save on memory when the tracing is never used on a system with it | |
3064 | * configured in. The ring buffers are set to a minimum size. But once | |
3065 | * a user starts to use the tracing facility, then they need to grow | |
3066 | * to their default size. | |
3067 | * | |
3068 | * This function is to be called when a tracer is about to be used. | |
3069 | */ | |
3070 | int tracing_update_buffers(void) | |
3071 | { | |
3072 | int ret = 0; | |
3073 | ||
1027fcb2 | 3074 | mutex_lock(&trace_types_lock); |
1852fcce | 3075 | if (!ring_buffer_expanded) |
4f271a2a | 3076 | ret = __tracing_resize_ring_buffer(trace_buf_size); |
1027fcb2 | 3077 | mutex_unlock(&trace_types_lock); |
1852fcce SR |
3078 | |
3079 | return ret; | |
3080 | } | |
3081 | ||
577b785f SR |
3082 | struct trace_option_dentry; |
3083 | ||
3084 | static struct trace_option_dentry * | |
3085 | create_trace_option_files(struct tracer *tracer); | |
3086 | ||
3087 | static void | |
3088 | destroy_trace_option_files(struct trace_option_dentry *topts); | |
3089 | ||
b2821ae6 | 3090 | static int tracing_set_tracer(const char *buf) |
bc0c38d1 | 3091 | { |
577b785f | 3092 | static struct trace_option_dentry *topts; |
bc0c38d1 SR |
3093 | struct trace_array *tr = &global_trace; |
3094 | struct tracer *t; | |
d9e54076 | 3095 | int ret = 0; |
bc0c38d1 | 3096 | |
1027fcb2 SR |
3097 | mutex_lock(&trace_types_lock); |
3098 | ||
73c5162a | 3099 | if (!ring_buffer_expanded) { |
4f271a2a | 3100 | ret = __tracing_resize_ring_buffer(trace_buf_size); |
73c5162a | 3101 | if (ret < 0) |
59f586db | 3102 | goto out; |
73c5162a SR |
3103 | ret = 0; |
3104 | } | |
3105 | ||
bc0c38d1 SR |
3106 | for (t = trace_types; t; t = t->next) { |
3107 | if (strcmp(t->name, buf) == 0) | |
3108 | break; | |
3109 | } | |
c2931e05 FW |
3110 | if (!t) { |
3111 | ret = -EINVAL; | |
3112 | goto out; | |
3113 | } | |
3114 | if (t == current_trace) | |
bc0c38d1 SR |
3115 | goto out; |
3116 | ||
9f029e83 | 3117 | trace_branch_disable(); |
bc0c38d1 SR |
3118 | if (current_trace && current_trace->reset) |
3119 | current_trace->reset(tr); | |
ef710e10 KM |
3120 | if (current_trace && current_trace->use_max_tr) { |
3121 | /* | |
3122 | * We don't free the ring buffer. instead, resize it because | |
3123 | * The max_tr ring buffer has some state (e.g. ring->clock) and | |
3124 | * we want preserve it. | |
3125 | */ | |
3126 | ring_buffer_resize(max_tr.buffer, 1); | |
3127 | max_tr.entries = 1; | |
3128 | } | |
577b785f SR |
3129 | destroy_trace_option_files(topts); |
3130 | ||
bc0c38d1 | 3131 | current_trace = t; |
577b785f SR |
3132 | |
3133 | topts = create_trace_option_files(current_trace); | |
ef710e10 KM |
3134 | if (current_trace->use_max_tr) { |
3135 | ret = ring_buffer_resize(max_tr.buffer, global_trace.entries); | |
3136 | if (ret < 0) | |
3137 | goto out; | |
3138 | max_tr.entries = global_trace.entries; | |
3139 | } | |
577b785f | 3140 | |
1c80025a | 3141 | if (t->init) { |
b6f11df2 | 3142 | ret = tracer_init(t, tr); |
1c80025a FW |
3143 | if (ret) |
3144 | goto out; | |
3145 | } | |
bc0c38d1 | 3146 | |
9f029e83 | 3147 | trace_branch_enable(tr); |
bc0c38d1 SR |
3148 | out: |
3149 | mutex_unlock(&trace_types_lock); | |
3150 | ||
d9e54076 PZ |
3151 | return ret; |
3152 | } | |
3153 | ||
3154 | static ssize_t | |
3155 | tracing_set_trace_write(struct file *filp, const char __user *ubuf, | |
3156 | size_t cnt, loff_t *ppos) | |
3157 | { | |
ee6c2c1b | 3158 | char buf[MAX_TRACER_SIZE+1]; |
d9e54076 PZ |
3159 | int i; |
3160 | size_t ret; | |
e6e7a65a FW |
3161 | int err; |
3162 | ||
3163 | ret = cnt; | |
d9e54076 | 3164 | |
ee6c2c1b LZ |
3165 | if (cnt > MAX_TRACER_SIZE) |
3166 | cnt = MAX_TRACER_SIZE; | |
d9e54076 PZ |
3167 | |
3168 | if (copy_from_user(&buf, ubuf, cnt)) | |
3169 | return -EFAULT; | |
3170 | ||
3171 | buf[cnt] = 0; | |
3172 | ||
3173 | /* strip ending whitespace. */ | |
3174 | for (i = cnt - 1; i > 0 && isspace(buf[i]); i--) | |
3175 | buf[i] = 0; | |
3176 | ||
e6e7a65a FW |
3177 | err = tracing_set_tracer(buf); |
3178 | if (err) | |
3179 | return err; | |
d9e54076 | 3180 | |
cf8517cf | 3181 | *ppos += ret; |
bc0c38d1 | 3182 | |
c2931e05 | 3183 | return ret; |
bc0c38d1 SR |
3184 | } |
3185 | ||
3186 | static ssize_t | |
3187 | tracing_max_lat_read(struct file *filp, char __user *ubuf, | |
3188 | size_t cnt, loff_t *ppos) | |
3189 | { | |
3190 | unsigned long *ptr = filp->private_data; | |
3191 | char buf[64]; | |
3192 | int r; | |
3193 | ||
cffae437 | 3194 | r = snprintf(buf, sizeof(buf), "%ld\n", |
bc0c38d1 | 3195 | *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr)); |
cffae437 SR |
3196 | if (r > sizeof(buf)) |
3197 | r = sizeof(buf); | |
4bf39a94 | 3198 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
bc0c38d1 SR |
3199 | } |
3200 | ||
3201 | static ssize_t | |
3202 | tracing_max_lat_write(struct file *filp, const char __user *ubuf, | |
3203 | size_t cnt, loff_t *ppos) | |
3204 | { | |
5e39841c | 3205 | unsigned long *ptr = filp->private_data; |
5e39841c | 3206 | unsigned long val; |
c6caeeb1 | 3207 | int ret; |
bc0c38d1 | 3208 | |
22fe9b54 PH |
3209 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
3210 | if (ret) | |
c6caeeb1 | 3211 | return ret; |
bc0c38d1 SR |
3212 | |
3213 | *ptr = val * 1000; | |
3214 | ||
3215 | return cnt; | |
3216 | } | |
3217 | ||
b3806b43 SR |
3218 | static int tracing_open_pipe(struct inode *inode, struct file *filp) |
3219 | { | |
b04cc6b1 | 3220 | long cpu_file = (long) inode->i_private; |
b3806b43 | 3221 | struct trace_iterator *iter; |
b04cc6b1 | 3222 | int ret = 0; |
b3806b43 SR |
3223 | |
3224 | if (tracing_disabled) | |
3225 | return -ENODEV; | |
3226 | ||
b04cc6b1 FW |
3227 | mutex_lock(&trace_types_lock); |
3228 | ||
b3806b43 SR |
3229 | /* create a buffer to store the information to pass to userspace */ |
3230 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); | |
b04cc6b1 FW |
3231 | if (!iter) { |
3232 | ret = -ENOMEM; | |
3233 | goto out; | |
3234 | } | |
b3806b43 | 3235 | |
d7350c3f FW |
3236 | /* |
3237 | * We make a copy of the current tracer to avoid concurrent | |
3238 | * changes on it while we are reading. | |
3239 | */ | |
3240 | iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL); | |
3241 | if (!iter->trace) { | |
3242 | ret = -ENOMEM; | |
3243 | goto fail; | |
3244 | } | |
3245 | if (current_trace) | |
3246 | *iter->trace = *current_trace; | |
3247 | ||
4462344e | 3248 | if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) { |
b04cc6b1 | 3249 | ret = -ENOMEM; |
d7350c3f | 3250 | goto fail; |
4462344e RR |
3251 | } |
3252 | ||
a309720c | 3253 | /* trace pipe does not show start of buffer */ |
4462344e | 3254 | cpumask_setall(iter->started); |
a309720c | 3255 | |
112f38a7 SR |
3256 | if (trace_flags & TRACE_ITER_LATENCY_FMT) |
3257 | iter->iter_flags |= TRACE_FILE_LAT_FMT; | |
3258 | ||
b04cc6b1 | 3259 | iter->cpu_file = cpu_file; |
b3806b43 | 3260 | iter->tr = &global_trace; |
d7350c3f | 3261 | mutex_init(&iter->mutex); |
b3806b43 SR |
3262 | filp->private_data = iter; |
3263 | ||
107bad8b SR |
3264 | if (iter->trace->pipe_open) |
3265 | iter->trace->pipe_open(iter); | |
107bad8b | 3266 | |
b444786f | 3267 | nonseekable_open(inode, filp); |
b04cc6b1 FW |
3268 | out: |
3269 | mutex_unlock(&trace_types_lock); | |
3270 | return ret; | |
d7350c3f FW |
3271 | |
3272 | fail: | |
3273 | kfree(iter->trace); | |
3274 | kfree(iter); | |
3275 | mutex_unlock(&trace_types_lock); | |
3276 | return ret; | |
b3806b43 SR |
3277 | } |
3278 | ||
3279 | static int tracing_release_pipe(struct inode *inode, struct file *file) | |
3280 | { | |
3281 | struct trace_iterator *iter = file->private_data; | |
3282 | ||
b04cc6b1 FW |
3283 | mutex_lock(&trace_types_lock); |
3284 | ||
29bf4a5e | 3285 | if (iter->trace->pipe_close) |
c521efd1 SR |
3286 | iter->trace->pipe_close(iter); |
3287 | ||
b04cc6b1 FW |
3288 | mutex_unlock(&trace_types_lock); |
3289 | ||
4462344e | 3290 | free_cpumask_var(iter->started); |
d7350c3f FW |
3291 | mutex_destroy(&iter->mutex); |
3292 | kfree(iter->trace); | |
b3806b43 | 3293 | kfree(iter); |
b3806b43 SR |
3294 | |
3295 | return 0; | |
3296 | } | |
3297 | ||
2a2cc8f7 SSP |
3298 | static unsigned int |
3299 | tracing_poll_pipe(struct file *filp, poll_table *poll_table) | |
3300 | { | |
3301 | struct trace_iterator *iter = filp->private_data; | |
3302 | ||
3303 | if (trace_flags & TRACE_ITER_BLOCK) { | |
3304 | /* | |
3305 | * Always select as readable when in blocking mode | |
3306 | */ | |
3307 | return POLLIN | POLLRDNORM; | |
afc2abc0 | 3308 | } else { |
2a2cc8f7 SSP |
3309 | if (!trace_empty(iter)) |
3310 | return POLLIN | POLLRDNORM; | |
3311 | poll_wait(filp, &trace_wait, poll_table); | |
3312 | if (!trace_empty(iter)) | |
3313 | return POLLIN | POLLRDNORM; | |
3314 | ||
3315 | return 0; | |
3316 | } | |
3317 | } | |
3318 | ||
6eaaa5d5 FW |
3319 | |
3320 | void default_wait_pipe(struct trace_iterator *iter) | |
3321 | { | |
3322 | DEFINE_WAIT(wait); | |
3323 | ||
3324 | prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE); | |
3325 | ||
3326 | if (trace_empty(iter)) | |
3327 | schedule(); | |
3328 | ||
3329 | finish_wait(&trace_wait, &wait); | |
3330 | } | |
3331 | ||
3332 | /* | |
3333 | * This is a make-shift waitqueue. | |
3334 | * A tracer might use this callback on some rare cases: | |
3335 | * | |
3336 | * 1) the current tracer might hold the runqueue lock when it wakes up | |
3337 | * a reader, hence a deadlock (sched, function, and function graph tracers) | |
3338 | * 2) the function tracers, trace all functions, we don't want | |
3339 | * the overhead of calling wake_up and friends | |
3340 | * (and tracing them too) | |
3341 | * | |
3342 | * Anyway, this is really very primitive wakeup. | |
3343 | */ | |
3344 | void poll_wait_pipe(struct trace_iterator *iter) | |
3345 | { | |
3346 | set_current_state(TASK_INTERRUPTIBLE); | |
3347 | /* sleep for 100 msecs, and try again. */ | |
3348 | schedule_timeout(HZ / 10); | |
3349 | } | |
3350 | ||
ff98781b EGM |
3351 | /* Must be called with trace_types_lock mutex held. */ |
3352 | static int tracing_wait_pipe(struct file *filp) | |
b3806b43 SR |
3353 | { |
3354 | struct trace_iterator *iter = filp->private_data; | |
b3806b43 | 3355 | |
b3806b43 | 3356 | while (trace_empty(iter)) { |
2dc8f095 | 3357 | |
107bad8b | 3358 | if ((filp->f_flags & O_NONBLOCK)) { |
ff98781b | 3359 | return -EAGAIN; |
107bad8b | 3360 | } |
2dc8f095 | 3361 | |
d7350c3f | 3362 | mutex_unlock(&iter->mutex); |
107bad8b | 3363 | |
6eaaa5d5 | 3364 | iter->trace->wait_pipe(iter); |
b3806b43 | 3365 | |
d7350c3f | 3366 | mutex_lock(&iter->mutex); |
107bad8b | 3367 | |
6eaaa5d5 | 3368 | if (signal_pending(current)) |
ff98781b | 3369 | return -EINTR; |
b3806b43 SR |
3370 | |
3371 | /* | |
3372 | * We block until we read something and tracing is disabled. | |
3373 | * We still block if tracing is disabled, but we have never | |
3374 | * read anything. This allows a user to cat this file, and | |
3375 | * then enable tracing. But after we have read something, | |
3376 | * we give an EOF when tracing is again disabled. | |
3377 | * | |
3378 | * iter->pos will be 0 if we haven't read anything. | |
3379 | */ | |
3380 | if (!tracer_enabled && iter->pos) | |
3381 | break; | |
b3806b43 SR |
3382 | } |
3383 | ||
ff98781b EGM |
3384 | return 1; |
3385 | } | |
3386 | ||
3387 | /* | |
3388 | * Consumer reader. | |
3389 | */ | |
3390 | static ssize_t | |
3391 | tracing_read_pipe(struct file *filp, char __user *ubuf, | |
3392 | size_t cnt, loff_t *ppos) | |
3393 | { | |
3394 | struct trace_iterator *iter = filp->private_data; | |
d7350c3f | 3395 | static struct tracer *old_tracer; |
ff98781b EGM |
3396 | ssize_t sret; |
3397 | ||
3398 | /* return any leftover data */ | |
3399 | sret = trace_seq_to_user(&iter->seq, ubuf, cnt); | |
3400 | if (sret != -EBUSY) | |
3401 | return sret; | |
3402 | ||
f9520750 | 3403 | trace_seq_init(&iter->seq); |
ff98781b | 3404 | |
d7350c3f | 3405 | /* copy the tracer to avoid using a global lock all around */ |
ff98781b | 3406 | mutex_lock(&trace_types_lock); |
d7350c3f FW |
3407 | if (unlikely(old_tracer != current_trace && current_trace)) { |
3408 | old_tracer = current_trace; | |
3409 | *iter->trace = *current_trace; | |
3410 | } | |
3411 | mutex_unlock(&trace_types_lock); | |
3412 | ||
3413 | /* | |
3414 | * Avoid more than one consumer on a single file descriptor | |
3415 | * This is just a matter of traces coherency, the ring buffer itself | |
3416 | * is protected. | |
3417 | */ | |
3418 | mutex_lock(&iter->mutex); | |
ff98781b EGM |
3419 | if (iter->trace->read) { |
3420 | sret = iter->trace->read(iter, filp, ubuf, cnt, ppos); | |
3421 | if (sret) | |
3422 | goto out; | |
3423 | } | |
3424 | ||
3425 | waitagain: | |
3426 | sret = tracing_wait_pipe(filp); | |
3427 | if (sret <= 0) | |
3428 | goto out; | |
3429 | ||
b3806b43 | 3430 | /* stop when tracing is finished */ |
ff98781b EGM |
3431 | if (trace_empty(iter)) { |
3432 | sret = 0; | |
107bad8b | 3433 | goto out; |
ff98781b | 3434 | } |
b3806b43 SR |
3435 | |
3436 | if (cnt >= PAGE_SIZE) | |
3437 | cnt = PAGE_SIZE - 1; | |
3438 | ||
53d0aa77 | 3439 | /* reset all but tr, trace, and overruns */ |
53d0aa77 SR |
3440 | memset(&iter->seq, 0, |
3441 | sizeof(struct trace_iterator) - | |
3442 | offsetof(struct trace_iterator, seq)); | |
4823ed7e | 3443 | iter->pos = -1; |
b3806b43 | 3444 | |
4f535968 | 3445 | trace_event_read_lock(); |
7e53bd42 | 3446 | trace_access_lock(iter->cpu_file); |
955b61e5 | 3447 | while (trace_find_next_entry_inc(iter) != NULL) { |
2c4f035f | 3448 | enum print_line_t ret; |
088b1e42 SR |
3449 | int len = iter->seq.len; |
3450 | ||
f9896bf3 | 3451 | ret = print_trace_line(iter); |
2c4f035f | 3452 | if (ret == TRACE_TYPE_PARTIAL_LINE) { |
088b1e42 SR |
3453 | /* don't print partial lines */ |
3454 | iter->seq.len = len; | |
b3806b43 | 3455 | break; |
088b1e42 | 3456 | } |
b91facc3 FW |
3457 | if (ret != TRACE_TYPE_NO_CONSUME) |
3458 | trace_consume(iter); | |
b3806b43 SR |
3459 | |
3460 | if (iter->seq.len >= cnt) | |
3461 | break; | |
ee5e51f5 JO |
3462 | |
3463 | /* | |
3464 | * Setting the full flag means we reached the trace_seq buffer | |
3465 | * size and we should leave by partial output condition above. | |
3466 | * One of the trace_seq_* functions is not used properly. | |
3467 | */ | |
3468 | WARN_ONCE(iter->seq.full, "full flag set for trace type %d", | |
3469 | iter->ent->type); | |
b3806b43 | 3470 | } |
7e53bd42 | 3471 | trace_access_unlock(iter->cpu_file); |
4f535968 | 3472 | trace_event_read_unlock(); |
b3806b43 | 3473 | |
b3806b43 | 3474 | /* Now copy what we have to the user */ |
6c6c2796 PP |
3475 | sret = trace_seq_to_user(&iter->seq, ubuf, cnt); |
3476 | if (iter->seq.readpos >= iter->seq.len) | |
f9520750 | 3477 | trace_seq_init(&iter->seq); |
9ff4b974 PP |
3478 | |
3479 | /* | |
25985edc | 3480 | * If there was nothing to send to user, in spite of consuming trace |
9ff4b974 PP |
3481 | * entries, go back to wait for more entries. |
3482 | */ | |
6c6c2796 | 3483 | if (sret == -EBUSY) |
9ff4b974 | 3484 | goto waitagain; |
b3806b43 | 3485 | |
107bad8b | 3486 | out: |
d7350c3f | 3487 | mutex_unlock(&iter->mutex); |
107bad8b | 3488 | |
6c6c2796 | 3489 | return sret; |
b3806b43 SR |
3490 | } |
3491 | ||
3c56819b EGM |
3492 | static void tracing_pipe_buf_release(struct pipe_inode_info *pipe, |
3493 | struct pipe_buffer *buf) | |
3494 | { | |
3495 | __free_page(buf->page); | |
3496 | } | |
3497 | ||
3498 | static void tracing_spd_release_pipe(struct splice_pipe_desc *spd, | |
3499 | unsigned int idx) | |
3500 | { | |
3501 | __free_page(spd->pages[idx]); | |
3502 | } | |
3503 | ||
28dfef8f | 3504 | static const struct pipe_buf_operations tracing_pipe_buf_ops = { |
34cd4998 SR |
3505 | .can_merge = 0, |
3506 | .map = generic_pipe_buf_map, | |
3507 | .unmap = generic_pipe_buf_unmap, | |
3508 | .confirm = generic_pipe_buf_confirm, | |
3509 | .release = tracing_pipe_buf_release, | |
3510 | .steal = generic_pipe_buf_steal, | |
3511 | .get = generic_pipe_buf_get, | |
3c56819b EGM |
3512 | }; |
3513 | ||
34cd4998 | 3514 | static size_t |
fa7c7f6e | 3515 | tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter) |
34cd4998 SR |
3516 | { |
3517 | size_t count; | |
3518 | int ret; | |
3519 | ||
3520 | /* Seq buffer is page-sized, exactly what we need. */ | |
3521 | for (;;) { | |
3522 | count = iter->seq.len; | |
3523 | ret = print_trace_line(iter); | |
3524 | count = iter->seq.len - count; | |
3525 | if (rem < count) { | |
3526 | rem = 0; | |
3527 | iter->seq.len -= count; | |
3528 | break; | |
3529 | } | |
3530 | if (ret == TRACE_TYPE_PARTIAL_LINE) { | |
3531 | iter->seq.len -= count; | |
3532 | break; | |
3533 | } | |
3534 | ||
74e7ff8c LJ |
3535 | if (ret != TRACE_TYPE_NO_CONSUME) |
3536 | trace_consume(iter); | |
34cd4998 | 3537 | rem -= count; |
955b61e5 | 3538 | if (!trace_find_next_entry_inc(iter)) { |
34cd4998 SR |
3539 | rem = 0; |
3540 | iter->ent = NULL; | |
3541 | break; | |
3542 | } | |
3543 | } | |
3544 | ||
3545 | return rem; | |
3546 | } | |
3547 | ||
3c56819b EGM |
3548 | static ssize_t tracing_splice_read_pipe(struct file *filp, |
3549 | loff_t *ppos, | |
3550 | struct pipe_inode_info *pipe, | |
3551 | size_t len, | |
3552 | unsigned int flags) | |
3553 | { | |
35f3d14d JA |
3554 | struct page *pages_def[PIPE_DEF_BUFFERS]; |
3555 | struct partial_page partial_def[PIPE_DEF_BUFFERS]; | |
3c56819b EGM |
3556 | struct trace_iterator *iter = filp->private_data; |
3557 | struct splice_pipe_desc spd = { | |
35f3d14d JA |
3558 | .pages = pages_def, |
3559 | .partial = partial_def, | |
34cd4998 SR |
3560 | .nr_pages = 0, /* This gets updated below. */ |
3561 | .flags = flags, | |
3562 | .ops = &tracing_pipe_buf_ops, | |
3563 | .spd_release = tracing_spd_release_pipe, | |
3c56819b | 3564 | }; |
d7350c3f | 3565 | static struct tracer *old_tracer; |
3c56819b | 3566 | ssize_t ret; |
34cd4998 | 3567 | size_t rem; |
3c56819b EGM |
3568 | unsigned int i; |
3569 | ||
35f3d14d JA |
3570 | if (splice_grow_spd(pipe, &spd)) |
3571 | return -ENOMEM; | |
3572 | ||
d7350c3f | 3573 | /* copy the tracer to avoid using a global lock all around */ |
3c56819b | 3574 | mutex_lock(&trace_types_lock); |
d7350c3f FW |
3575 | if (unlikely(old_tracer != current_trace && current_trace)) { |
3576 | old_tracer = current_trace; | |
3577 | *iter->trace = *current_trace; | |
3578 | } | |
3579 | mutex_unlock(&trace_types_lock); | |
3580 | ||
3581 | mutex_lock(&iter->mutex); | |
3c56819b EGM |
3582 | |
3583 | if (iter->trace->splice_read) { | |
3584 | ret = iter->trace->splice_read(iter, filp, | |
3585 | ppos, pipe, len, flags); | |
3586 | if (ret) | |
34cd4998 | 3587 | goto out_err; |
3c56819b EGM |
3588 | } |
3589 | ||
3590 | ret = tracing_wait_pipe(filp); | |
3591 | if (ret <= 0) | |
34cd4998 | 3592 | goto out_err; |
3c56819b | 3593 | |
955b61e5 | 3594 | if (!iter->ent && !trace_find_next_entry_inc(iter)) { |
3c56819b | 3595 | ret = -EFAULT; |
34cd4998 | 3596 | goto out_err; |
3c56819b EGM |
3597 | } |
3598 | ||
4f535968 | 3599 | trace_event_read_lock(); |
7e53bd42 | 3600 | trace_access_lock(iter->cpu_file); |
4f535968 | 3601 | |
3c56819b | 3602 | /* Fill as many pages as possible. */ |
35f3d14d JA |
3603 | for (i = 0, rem = len; i < pipe->buffers && rem; i++) { |
3604 | spd.pages[i] = alloc_page(GFP_KERNEL); | |
3605 | if (!spd.pages[i]) | |
34cd4998 | 3606 | break; |
3c56819b | 3607 | |
fa7c7f6e | 3608 | rem = tracing_fill_pipe_page(rem, iter); |
3c56819b EGM |
3609 | |
3610 | /* Copy the data into the page, so we can start over. */ | |
3611 | ret = trace_seq_to_buffer(&iter->seq, | |
35f3d14d | 3612 | page_address(spd.pages[i]), |
3c56819b EGM |
3613 | iter->seq.len); |
3614 | if (ret < 0) { | |
35f3d14d | 3615 | __free_page(spd.pages[i]); |
3c56819b EGM |
3616 | break; |
3617 | } | |
35f3d14d JA |
3618 | spd.partial[i].offset = 0; |
3619 | spd.partial[i].len = iter->seq.len; | |
3c56819b | 3620 | |
f9520750 | 3621 | trace_seq_init(&iter->seq); |
3c56819b EGM |
3622 | } |
3623 | ||
7e53bd42 | 3624 | trace_access_unlock(iter->cpu_file); |
4f535968 | 3625 | trace_event_read_unlock(); |
d7350c3f | 3626 | mutex_unlock(&iter->mutex); |
3c56819b EGM |
3627 | |
3628 | spd.nr_pages = i; | |
3629 | ||
35f3d14d JA |
3630 | ret = splice_to_pipe(pipe, &spd); |
3631 | out: | |
3632 | splice_shrink_spd(pipe, &spd); | |
3633 | return ret; | |
3c56819b | 3634 | |
34cd4998 | 3635 | out_err: |
d7350c3f | 3636 | mutex_unlock(&iter->mutex); |
35f3d14d | 3637 | goto out; |
3c56819b EGM |
3638 | } |
3639 | ||
a98a3c3f SR |
3640 | static ssize_t |
3641 | tracing_entries_read(struct file *filp, char __user *ubuf, | |
3642 | size_t cnt, loff_t *ppos) | |
3643 | { | |
3644 | struct trace_array *tr = filp->private_data; | |
db526ca3 | 3645 | char buf[96]; |
a98a3c3f SR |
3646 | int r; |
3647 | ||
db526ca3 SR |
3648 | mutex_lock(&trace_types_lock); |
3649 | if (!ring_buffer_expanded) | |
3650 | r = sprintf(buf, "%lu (expanded: %lu)\n", | |
3651 | tr->entries >> 10, | |
3652 | trace_buf_size >> 10); | |
3653 | else | |
3654 | r = sprintf(buf, "%lu\n", tr->entries >> 10); | |
3655 | mutex_unlock(&trace_types_lock); | |
3656 | ||
a98a3c3f SR |
3657 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
3658 | } | |
3659 | ||
3660 | static ssize_t | |
3661 | tracing_entries_write(struct file *filp, const char __user *ubuf, | |
3662 | size_t cnt, loff_t *ppos) | |
3663 | { | |
3664 | unsigned long val; | |
4f271a2a | 3665 | int ret; |
a98a3c3f | 3666 | |
22fe9b54 PH |
3667 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
3668 | if (ret) | |
c6caeeb1 | 3669 | return ret; |
a98a3c3f SR |
3670 | |
3671 | /* must have at least 1 entry */ | |
3672 | if (!val) | |
3673 | return -EINVAL; | |
3674 | ||
1696b2b0 SR |
3675 | /* value is in KB */ |
3676 | val <<= 10; | |
3677 | ||
4f271a2a VN |
3678 | ret = tracing_resize_ring_buffer(val); |
3679 | if (ret < 0) | |
3680 | return ret; | |
a98a3c3f | 3681 | |
cf8517cf | 3682 | *ppos += cnt; |
a98a3c3f | 3683 | |
4f271a2a VN |
3684 | return cnt; |
3685 | } | |
bf5e6519 | 3686 | |
f81ab074 VN |
3687 | static ssize_t |
3688 | tracing_total_entries_read(struct file *filp, char __user *ubuf, | |
3689 | size_t cnt, loff_t *ppos) | |
3690 | { | |
3691 | struct trace_array *tr = filp->private_data; | |
3692 | char buf[64]; | |
3693 | int r, cpu; | |
3694 | unsigned long size = 0, expanded_size = 0; | |
3695 | ||
3696 | mutex_lock(&trace_types_lock); | |
3697 | for_each_tracing_cpu(cpu) { | |
3698 | size += tr->entries >> 10; | |
3699 | if (!ring_buffer_expanded) | |
3700 | expanded_size += trace_buf_size >> 10; | |
3701 | } | |
3702 | if (ring_buffer_expanded) | |
3703 | r = sprintf(buf, "%lu\n", size); | |
3704 | else | |
3705 | r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size); | |
3706 | mutex_unlock(&trace_types_lock); | |
3707 | ||
3708 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); | |
3709 | } | |
3710 | ||
4f271a2a VN |
3711 | static ssize_t |
3712 | tracing_free_buffer_write(struct file *filp, const char __user *ubuf, | |
3713 | size_t cnt, loff_t *ppos) | |
3714 | { | |
3715 | /* | |
3716 | * There is no need to read what the user has written, this function | |
3717 | * is just to make sure that there is no error when "echo" is used | |
3718 | */ | |
3719 | ||
3720 | *ppos += cnt; | |
a98a3c3f SR |
3721 | |
3722 | return cnt; | |
3723 | } | |
3724 | ||
4f271a2a VN |
3725 | static int |
3726 | tracing_free_buffer_release(struct inode *inode, struct file *filp) | |
3727 | { | |
cf30cf67 SR |
3728 | /* disable tracing ? */ |
3729 | if (trace_flags & TRACE_ITER_STOP_ON_FREE) | |
3730 | tracing_off(); | |
4f271a2a VN |
3731 | /* resize the ring buffer to 0 */ |
3732 | tracing_resize_ring_buffer(0); | |
3733 | ||
3734 | return 0; | |
3735 | } | |
3736 | ||
5bf9a1ee PP |
3737 | static ssize_t |
3738 | tracing_mark_write(struct file *filp, const char __user *ubuf, | |
3739 | size_t cnt, loff_t *fpos) | |
3740 | { | |
d696b58c SR |
3741 | unsigned long addr = (unsigned long)ubuf; |
3742 | struct ring_buffer_event *event; | |
3743 | struct ring_buffer *buffer; | |
3744 | struct print_entry *entry; | |
3745 | unsigned long irq_flags; | |
3746 | struct page *pages[2]; | |
3747 | int nr_pages = 1; | |
3748 | ssize_t written; | |
3749 | void *page1; | |
3750 | void *page2; | |
3751 | int offset; | |
3752 | int size; | |
3753 | int len; | |
3754 | int ret; | |
5bf9a1ee | 3755 | |
c76f0694 | 3756 | if (tracing_disabled) |
5bf9a1ee PP |
3757 | return -EINVAL; |
3758 | ||
3759 | if (cnt > TRACE_BUF_SIZE) | |
3760 | cnt = TRACE_BUF_SIZE; | |
3761 | ||
d696b58c SR |
3762 | /* |
3763 | * Userspace is injecting traces into the kernel trace buffer. | |
3764 | * We want to be as non intrusive as possible. | |
3765 | * To do so, we do not want to allocate any special buffers | |
3766 | * or take any locks, but instead write the userspace data | |
3767 | * straight into the ring buffer. | |
3768 | * | |
3769 | * First we need to pin the userspace buffer into memory, | |
3770 | * which, most likely it is, because it just referenced it. | |
3771 | * But there's no guarantee that it is. By using get_user_pages_fast() | |
3772 | * and kmap_atomic/kunmap_atomic() we can get access to the | |
3773 | * pages directly. We then write the data directly into the | |
3774 | * ring buffer. | |
3775 | */ | |
3776 | BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE); | |
5bf9a1ee | 3777 | |
d696b58c SR |
3778 | /* check if we cross pages */ |
3779 | if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK)) | |
3780 | nr_pages = 2; | |
3781 | ||
3782 | offset = addr & (PAGE_SIZE - 1); | |
3783 | addr &= PAGE_MASK; | |
3784 | ||
3785 | ret = get_user_pages_fast(addr, nr_pages, 0, pages); | |
3786 | if (ret < nr_pages) { | |
3787 | while (--ret >= 0) | |
3788 | put_page(pages[ret]); | |
3789 | written = -EFAULT; | |
3790 | goto out; | |
5bf9a1ee | 3791 | } |
d696b58c SR |
3792 | |
3793 | page1 = kmap_atomic(pages[0]); | |
3794 | if (nr_pages == 2) | |
3795 | page2 = kmap_atomic(pages[1]); | |
3796 | ||
3797 | local_save_flags(irq_flags); | |
3798 | size = sizeof(*entry) + cnt + 2; /* possible \n added */ | |
3799 | buffer = global_trace.buffer; | |
3800 | event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, | |
3801 | irq_flags, preempt_count()); | |
3802 | if (!event) { | |
3803 | /* Ring buffer disabled, return as if not open for write */ | |
3804 | written = -EBADF; | |
3805 | goto out_unlock; | |
5bf9a1ee | 3806 | } |
d696b58c SR |
3807 | |
3808 | entry = ring_buffer_event_data(event); | |
3809 | entry->ip = _THIS_IP_; | |
3810 | ||
3811 | if (nr_pages == 2) { | |
3812 | len = PAGE_SIZE - offset; | |
3813 | memcpy(&entry->buf, page1 + offset, len); | |
3814 | memcpy(&entry->buf[len], page2, cnt - len); | |
c13d2f7c | 3815 | } else |
d696b58c | 3816 | memcpy(&entry->buf, page1 + offset, cnt); |
5bf9a1ee | 3817 | |
d696b58c SR |
3818 | if (entry->buf[cnt - 1] != '\n') { |
3819 | entry->buf[cnt] = '\n'; | |
3820 | entry->buf[cnt + 1] = '\0'; | |
3821 | } else | |
3822 | entry->buf[cnt] = '\0'; | |
3823 | ||
3824 | ring_buffer_unlock_commit(buffer, event); | |
5bf9a1ee | 3825 | |
d696b58c | 3826 | written = cnt; |
5bf9a1ee | 3827 | |
d696b58c | 3828 | *fpos += written; |
1aa54bca | 3829 | |
d696b58c SR |
3830 | out_unlock: |
3831 | if (nr_pages == 2) | |
3832 | kunmap_atomic(page2); | |
3833 | kunmap_atomic(page1); | |
3834 | while (nr_pages > 0) | |
3835 | put_page(pages[--nr_pages]); | |
3836 | out: | |
1aa54bca | 3837 | return written; |
5bf9a1ee PP |
3838 | } |
3839 | ||
13f16d20 | 3840 | static int tracing_clock_show(struct seq_file *m, void *v) |
5079f326 | 3841 | { |
5079f326 Z |
3842 | int i; |
3843 | ||
3844 | for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) | |
13f16d20 | 3845 | seq_printf(m, |
5079f326 Z |
3846 | "%s%s%s%s", i ? " " : "", |
3847 | i == trace_clock_id ? "[" : "", trace_clocks[i].name, | |
3848 | i == trace_clock_id ? "]" : ""); | |
13f16d20 | 3849 | seq_putc(m, '\n'); |
5079f326 | 3850 | |
13f16d20 | 3851 | return 0; |
5079f326 Z |
3852 | } |
3853 | ||
3854 | static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf, | |
3855 | size_t cnt, loff_t *fpos) | |
3856 | { | |
3857 | char buf[64]; | |
3858 | const char *clockstr; | |
3859 | int i; | |
3860 | ||
3861 | if (cnt >= sizeof(buf)) | |
3862 | return -EINVAL; | |
3863 | ||
3864 | if (copy_from_user(&buf, ubuf, cnt)) | |
3865 | return -EFAULT; | |
3866 | ||
3867 | buf[cnt] = 0; | |
3868 | ||
3869 | clockstr = strstrip(buf); | |
3870 | ||
3871 | for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) { | |
3872 | if (strcmp(trace_clocks[i].name, clockstr) == 0) | |
3873 | break; | |
3874 | } | |
3875 | if (i == ARRAY_SIZE(trace_clocks)) | |
3876 | return -EINVAL; | |
3877 | ||
3878 | trace_clock_id = i; | |
3879 | ||
3880 | mutex_lock(&trace_types_lock); | |
3881 | ||
3882 | ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func); | |
3883 | if (max_tr.buffer) | |
3884 | ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func); | |
3885 | ||
3886 | mutex_unlock(&trace_types_lock); | |
3887 | ||
3888 | *fpos += cnt; | |
3889 | ||
3890 | return cnt; | |
3891 | } | |
3892 | ||
13f16d20 LZ |
3893 | static int tracing_clock_open(struct inode *inode, struct file *file) |
3894 | { | |
3895 | if (tracing_disabled) | |
3896 | return -ENODEV; | |
3897 | return single_open(file, tracing_clock_show, NULL); | |
3898 | } | |
3899 | ||
5e2336a0 | 3900 | static const struct file_operations tracing_max_lat_fops = { |
4bf39a94 IM |
3901 | .open = tracing_open_generic, |
3902 | .read = tracing_max_lat_read, | |
3903 | .write = tracing_max_lat_write, | |
b444786f | 3904 | .llseek = generic_file_llseek, |
bc0c38d1 SR |
3905 | }; |
3906 | ||
5e2336a0 | 3907 | static const struct file_operations tracing_ctrl_fops = { |
4bf39a94 IM |
3908 | .open = tracing_open_generic, |
3909 | .read = tracing_ctrl_read, | |
3910 | .write = tracing_ctrl_write, | |
b444786f | 3911 | .llseek = generic_file_llseek, |
bc0c38d1 SR |
3912 | }; |
3913 | ||
5e2336a0 | 3914 | static const struct file_operations set_tracer_fops = { |
4bf39a94 IM |
3915 | .open = tracing_open_generic, |
3916 | .read = tracing_set_trace_read, | |
3917 | .write = tracing_set_trace_write, | |
b444786f | 3918 | .llseek = generic_file_llseek, |
bc0c38d1 SR |
3919 | }; |
3920 | ||
5e2336a0 | 3921 | static const struct file_operations tracing_pipe_fops = { |
4bf39a94 | 3922 | .open = tracing_open_pipe, |
2a2cc8f7 | 3923 | .poll = tracing_poll_pipe, |
4bf39a94 | 3924 | .read = tracing_read_pipe, |
3c56819b | 3925 | .splice_read = tracing_splice_read_pipe, |
4bf39a94 | 3926 | .release = tracing_release_pipe, |
b444786f | 3927 | .llseek = no_llseek, |
b3806b43 SR |
3928 | }; |
3929 | ||
5e2336a0 | 3930 | static const struct file_operations tracing_entries_fops = { |
a98a3c3f SR |
3931 | .open = tracing_open_generic, |
3932 | .read = tracing_entries_read, | |
3933 | .write = tracing_entries_write, | |
b444786f | 3934 | .llseek = generic_file_llseek, |
a98a3c3f SR |
3935 | }; |
3936 | ||
f81ab074 VN |
3937 | static const struct file_operations tracing_total_entries_fops = { |
3938 | .open = tracing_open_generic, | |
3939 | .read = tracing_total_entries_read, | |
3940 | .llseek = generic_file_llseek, | |
3941 | }; | |
3942 | ||
4f271a2a VN |
3943 | static const struct file_operations tracing_free_buffer_fops = { |
3944 | .write = tracing_free_buffer_write, | |
3945 | .release = tracing_free_buffer_release, | |
3946 | }; | |
3947 | ||
5e2336a0 | 3948 | static const struct file_operations tracing_mark_fops = { |
43a15386 | 3949 | .open = tracing_open_generic, |
5bf9a1ee | 3950 | .write = tracing_mark_write, |
b444786f | 3951 | .llseek = generic_file_llseek, |
5bf9a1ee PP |
3952 | }; |
3953 | ||
5079f326 | 3954 | static const struct file_operations trace_clock_fops = { |
13f16d20 LZ |
3955 | .open = tracing_clock_open, |
3956 | .read = seq_read, | |
3957 | .llseek = seq_lseek, | |
3958 | .release = single_release, | |
5079f326 Z |
3959 | .write = tracing_clock_write, |
3960 | }; | |
3961 | ||
2cadf913 SR |
3962 | struct ftrace_buffer_info { |
3963 | struct trace_array *tr; | |
3964 | void *spare; | |
3965 | int cpu; | |
3966 | unsigned int read; | |
3967 | }; | |
3968 | ||
3969 | static int tracing_buffers_open(struct inode *inode, struct file *filp) | |
3970 | { | |
3971 | int cpu = (int)(long)inode->i_private; | |
3972 | struct ftrace_buffer_info *info; | |
3973 | ||
3974 | if (tracing_disabled) | |
3975 | return -ENODEV; | |
3976 | ||
3977 | info = kzalloc(sizeof(*info), GFP_KERNEL); | |
3978 | if (!info) | |
3979 | return -ENOMEM; | |
3980 | ||
3981 | info->tr = &global_trace; | |
3982 | info->cpu = cpu; | |
ddd538f3 | 3983 | info->spare = NULL; |
2cadf913 SR |
3984 | /* Force reading ring buffer for first read */ |
3985 | info->read = (unsigned int)-1; | |
2cadf913 SR |
3986 | |
3987 | filp->private_data = info; | |
3988 | ||
d1e7e02f | 3989 | return nonseekable_open(inode, filp); |
2cadf913 SR |
3990 | } |
3991 | ||
3992 | static ssize_t | |
3993 | tracing_buffers_read(struct file *filp, char __user *ubuf, | |
3994 | size_t count, loff_t *ppos) | |
3995 | { | |
3996 | struct ftrace_buffer_info *info = filp->private_data; | |
2cadf913 SR |
3997 | ssize_t ret; |
3998 | size_t size; | |
3999 | ||
2dc5d12b SR |
4000 | if (!count) |
4001 | return 0; | |
4002 | ||
ddd538f3 | 4003 | if (!info->spare) |
7ea59064 | 4004 | info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu); |
ddd538f3 LJ |
4005 | if (!info->spare) |
4006 | return -ENOMEM; | |
4007 | ||
2cadf913 SR |
4008 | /* Do we have previous read data to read? */ |
4009 | if (info->read < PAGE_SIZE) | |
4010 | goto read; | |
4011 | ||
7e53bd42 | 4012 | trace_access_lock(info->cpu); |
2cadf913 SR |
4013 | ret = ring_buffer_read_page(info->tr->buffer, |
4014 | &info->spare, | |
4015 | count, | |
4016 | info->cpu, 0); | |
7e53bd42 | 4017 | trace_access_unlock(info->cpu); |
2cadf913 SR |
4018 | if (ret < 0) |
4019 | return 0; | |
4020 | ||
436fc280 SR |
4021 | info->read = 0; |
4022 | ||
2cadf913 SR |
4023 | read: |
4024 | size = PAGE_SIZE - info->read; | |
4025 | if (size > count) | |
4026 | size = count; | |
4027 | ||
4028 | ret = copy_to_user(ubuf, info->spare + info->read, size); | |
2dc5d12b | 4029 | if (ret == size) |
2cadf913 | 4030 | return -EFAULT; |
2dc5d12b SR |
4031 | size -= ret; |
4032 | ||
2cadf913 SR |
4033 | *ppos += size; |
4034 | info->read += size; | |
4035 | ||
4036 | return size; | |
4037 | } | |
4038 | ||
4039 | static int tracing_buffers_release(struct inode *inode, struct file *file) | |
4040 | { | |
4041 | struct ftrace_buffer_info *info = file->private_data; | |
4042 | ||
ddd538f3 LJ |
4043 | if (info->spare) |
4044 | ring_buffer_free_read_page(info->tr->buffer, info->spare); | |
2cadf913 SR |
4045 | kfree(info); |
4046 | ||
4047 | return 0; | |
4048 | } | |
4049 | ||
4050 | struct buffer_ref { | |
4051 | struct ring_buffer *buffer; | |
4052 | void *page; | |
4053 | int ref; | |
4054 | }; | |
4055 | ||
4056 | static void buffer_pipe_buf_release(struct pipe_inode_info *pipe, | |
4057 | struct pipe_buffer *buf) | |
4058 | { | |
4059 | struct buffer_ref *ref = (struct buffer_ref *)buf->private; | |
4060 | ||
4061 | if (--ref->ref) | |
4062 | return; | |
4063 | ||
4064 | ring_buffer_free_read_page(ref->buffer, ref->page); | |
4065 | kfree(ref); | |
4066 | buf->private = 0; | |
4067 | } | |
4068 | ||
4069 | static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe, | |
4070 | struct pipe_buffer *buf) | |
4071 | { | |
4072 | return 1; | |
4073 | } | |
4074 | ||
4075 | static void buffer_pipe_buf_get(struct pipe_inode_info *pipe, | |
4076 | struct pipe_buffer *buf) | |
4077 | { | |
4078 | struct buffer_ref *ref = (struct buffer_ref *)buf->private; | |
4079 | ||
4080 | ref->ref++; | |
4081 | } | |
4082 | ||
4083 | /* Pipe buffer operations for a buffer. */ | |
28dfef8f | 4084 | static const struct pipe_buf_operations buffer_pipe_buf_ops = { |
2cadf913 SR |
4085 | .can_merge = 0, |
4086 | .map = generic_pipe_buf_map, | |
4087 | .unmap = generic_pipe_buf_unmap, | |
4088 | .confirm = generic_pipe_buf_confirm, | |
4089 | .release = buffer_pipe_buf_release, | |
4090 | .steal = buffer_pipe_buf_steal, | |
4091 | .get = buffer_pipe_buf_get, | |
4092 | }; | |
4093 | ||
4094 | /* | |
4095 | * Callback from splice_to_pipe(), if we need to release some pages | |
4096 | * at the end of the spd in case we error'ed out in filling the pipe. | |
4097 | */ | |
4098 | static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i) | |
4099 | { | |
4100 | struct buffer_ref *ref = | |
4101 | (struct buffer_ref *)spd->partial[i].private; | |
4102 | ||
4103 | if (--ref->ref) | |
4104 | return; | |
4105 | ||
4106 | ring_buffer_free_read_page(ref->buffer, ref->page); | |
4107 | kfree(ref); | |
4108 | spd->partial[i].private = 0; | |
4109 | } | |
4110 | ||
4111 | static ssize_t | |
4112 | tracing_buffers_splice_read(struct file *file, loff_t *ppos, | |
4113 | struct pipe_inode_info *pipe, size_t len, | |
4114 | unsigned int flags) | |
4115 | { | |
4116 | struct ftrace_buffer_info *info = file->private_data; | |
35f3d14d JA |
4117 | struct partial_page partial_def[PIPE_DEF_BUFFERS]; |
4118 | struct page *pages_def[PIPE_DEF_BUFFERS]; | |
2cadf913 | 4119 | struct splice_pipe_desc spd = { |
35f3d14d JA |
4120 | .pages = pages_def, |
4121 | .partial = partial_def, | |
2cadf913 SR |
4122 | .flags = flags, |
4123 | .ops = &buffer_pipe_buf_ops, | |
4124 | .spd_release = buffer_spd_release, | |
4125 | }; | |
4126 | struct buffer_ref *ref; | |
93459c6c | 4127 | int entries, size, i; |
2cadf913 SR |
4128 | size_t ret; |
4129 | ||
35f3d14d JA |
4130 | if (splice_grow_spd(pipe, &spd)) |
4131 | return -ENOMEM; | |
4132 | ||
93cfb3c9 LJ |
4133 | if (*ppos & (PAGE_SIZE - 1)) { |
4134 | WARN_ONCE(1, "Ftrace: previous read must page-align\n"); | |
35f3d14d JA |
4135 | ret = -EINVAL; |
4136 | goto out; | |
93cfb3c9 LJ |
4137 | } |
4138 | ||
4139 | if (len & (PAGE_SIZE - 1)) { | |
4140 | WARN_ONCE(1, "Ftrace: splice_read should page-align\n"); | |
35f3d14d JA |
4141 | if (len < PAGE_SIZE) { |
4142 | ret = -EINVAL; | |
4143 | goto out; | |
4144 | } | |
93cfb3c9 LJ |
4145 | len &= PAGE_MASK; |
4146 | } | |
4147 | ||
7e53bd42 | 4148 | trace_access_lock(info->cpu); |
93459c6c SR |
4149 | entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu); |
4150 | ||
35f3d14d | 4151 | for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) { |
2cadf913 SR |
4152 | struct page *page; |
4153 | int r; | |
4154 | ||
4155 | ref = kzalloc(sizeof(*ref), GFP_KERNEL); | |
4156 | if (!ref) | |
4157 | break; | |
4158 | ||
7267fa68 | 4159 | ref->ref = 1; |
2cadf913 | 4160 | ref->buffer = info->tr->buffer; |
7ea59064 | 4161 | ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu); |
2cadf913 SR |
4162 | if (!ref->page) { |
4163 | kfree(ref); | |
4164 | break; | |
4165 | } | |
4166 | ||
4167 | r = ring_buffer_read_page(ref->buffer, &ref->page, | |
f2957f1f | 4168 | len, info->cpu, 1); |
2cadf913 | 4169 | if (r < 0) { |
7ea59064 | 4170 | ring_buffer_free_read_page(ref->buffer, ref->page); |
2cadf913 SR |
4171 | kfree(ref); |
4172 | break; | |
4173 | } | |
4174 | ||
4175 | /* | |
4176 | * zero out any left over data, this is going to | |
4177 | * user land. | |
4178 | */ | |
4179 | size = ring_buffer_page_len(ref->page); | |
4180 | if (size < PAGE_SIZE) | |
4181 | memset(ref->page + size, 0, PAGE_SIZE - size); | |
4182 | ||
4183 | page = virt_to_page(ref->page); | |
4184 | ||
4185 | spd.pages[i] = page; | |
4186 | spd.partial[i].len = PAGE_SIZE; | |
4187 | spd.partial[i].offset = 0; | |
4188 | spd.partial[i].private = (unsigned long)ref; | |
4189 | spd.nr_pages++; | |
93cfb3c9 | 4190 | *ppos += PAGE_SIZE; |
93459c6c SR |
4191 | |
4192 | entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu); | |
2cadf913 SR |
4193 | } |
4194 | ||
7e53bd42 | 4195 | trace_access_unlock(info->cpu); |
2cadf913 SR |
4196 | spd.nr_pages = i; |
4197 | ||
4198 | /* did we read anything? */ | |
4199 | if (!spd.nr_pages) { | |
4200 | if (flags & SPLICE_F_NONBLOCK) | |
4201 | ret = -EAGAIN; | |
4202 | else | |
4203 | ret = 0; | |
4204 | /* TODO: block */ | |
35f3d14d | 4205 | goto out; |
2cadf913 SR |
4206 | } |
4207 | ||
4208 | ret = splice_to_pipe(pipe, &spd); | |
35f3d14d JA |
4209 | splice_shrink_spd(pipe, &spd); |
4210 | out: | |
2cadf913 SR |
4211 | return ret; |
4212 | } | |
4213 | ||
4214 | static const struct file_operations tracing_buffers_fops = { | |
4215 | .open = tracing_buffers_open, | |
4216 | .read = tracing_buffers_read, | |
4217 | .release = tracing_buffers_release, | |
4218 | .splice_read = tracing_buffers_splice_read, | |
4219 | .llseek = no_llseek, | |
4220 | }; | |
4221 | ||
c8d77183 SR |
4222 | static ssize_t |
4223 | tracing_stats_read(struct file *filp, char __user *ubuf, | |
4224 | size_t count, loff_t *ppos) | |
4225 | { | |
4226 | unsigned long cpu = (unsigned long)filp->private_data; | |
4227 | struct trace_array *tr = &global_trace; | |
4228 | struct trace_seq *s; | |
4229 | unsigned long cnt; | |
c64e148a VN |
4230 | unsigned long long t; |
4231 | unsigned long usec_rem; | |
c8d77183 | 4232 | |
e4f2d10f | 4233 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
c8d77183 | 4234 | if (!s) |
a646365c | 4235 | return -ENOMEM; |
c8d77183 SR |
4236 | |
4237 | trace_seq_init(s); | |
4238 | ||
4239 | cnt = ring_buffer_entries_cpu(tr->buffer, cpu); | |
4240 | trace_seq_printf(s, "entries: %ld\n", cnt); | |
4241 | ||
4242 | cnt = ring_buffer_overrun_cpu(tr->buffer, cpu); | |
4243 | trace_seq_printf(s, "overrun: %ld\n", cnt); | |
4244 | ||
4245 | cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu); | |
4246 | trace_seq_printf(s, "commit overrun: %ld\n", cnt); | |
4247 | ||
c64e148a VN |
4248 | cnt = ring_buffer_bytes_cpu(tr->buffer, cpu); |
4249 | trace_seq_printf(s, "bytes: %ld\n", cnt); | |
4250 | ||
4251 | t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu)); | |
4252 | usec_rem = do_div(t, USEC_PER_SEC); | |
4253 | trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem); | |
4254 | ||
4255 | t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu)); | |
4256 | usec_rem = do_div(t, USEC_PER_SEC); | |
4257 | trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem); | |
4258 | ||
c8d77183 SR |
4259 | count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len); |
4260 | ||
4261 | kfree(s); | |
4262 | ||
4263 | return count; | |
4264 | } | |
4265 | ||
4266 | static const struct file_operations tracing_stats_fops = { | |
4267 | .open = tracing_open_generic, | |
4268 | .read = tracing_stats_read, | |
b444786f | 4269 | .llseek = generic_file_llseek, |
c8d77183 SR |
4270 | }; |
4271 | ||
bc0c38d1 SR |
4272 | #ifdef CONFIG_DYNAMIC_FTRACE |
4273 | ||
b807c3d0 SR |
4274 | int __weak ftrace_arch_read_dyn_info(char *buf, int size) |
4275 | { | |
4276 | return 0; | |
4277 | } | |
4278 | ||
bc0c38d1 | 4279 | static ssize_t |
b807c3d0 | 4280 | tracing_read_dyn_info(struct file *filp, char __user *ubuf, |
bc0c38d1 SR |
4281 | size_t cnt, loff_t *ppos) |
4282 | { | |
a26a2a27 SR |
4283 | static char ftrace_dyn_info_buffer[1024]; |
4284 | static DEFINE_MUTEX(dyn_info_mutex); | |
bc0c38d1 | 4285 | unsigned long *p = filp->private_data; |
b807c3d0 | 4286 | char *buf = ftrace_dyn_info_buffer; |
a26a2a27 | 4287 | int size = ARRAY_SIZE(ftrace_dyn_info_buffer); |
bc0c38d1 SR |
4288 | int r; |
4289 | ||
b807c3d0 SR |
4290 | mutex_lock(&dyn_info_mutex); |
4291 | r = sprintf(buf, "%ld ", *p); | |
4bf39a94 | 4292 | |
a26a2a27 | 4293 | r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r); |
b807c3d0 SR |
4294 | buf[r++] = '\n'; |
4295 | ||
4296 | r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); | |
4297 | ||
4298 | mutex_unlock(&dyn_info_mutex); | |
4299 | ||
4300 | return r; | |
bc0c38d1 SR |
4301 | } |
4302 | ||
5e2336a0 | 4303 | static const struct file_operations tracing_dyn_info_fops = { |
4bf39a94 | 4304 | .open = tracing_open_generic, |
b807c3d0 | 4305 | .read = tracing_read_dyn_info, |
b444786f | 4306 | .llseek = generic_file_llseek, |
bc0c38d1 SR |
4307 | }; |
4308 | #endif | |
4309 | ||
4310 | static struct dentry *d_tracer; | |
4311 | ||
4312 | struct dentry *tracing_init_dentry(void) | |
4313 | { | |
4314 | static int once; | |
4315 | ||
4316 | if (d_tracer) | |
4317 | return d_tracer; | |
4318 | ||
3e1f60b8 FW |
4319 | if (!debugfs_initialized()) |
4320 | return NULL; | |
4321 | ||
bc0c38d1 SR |
4322 | d_tracer = debugfs_create_dir("tracing", NULL); |
4323 | ||
4324 | if (!d_tracer && !once) { | |
4325 | once = 1; | |
4326 | pr_warning("Could not create debugfs directory 'tracing'\n"); | |
4327 | return NULL; | |
4328 | } | |
4329 | ||
4330 | return d_tracer; | |
4331 | } | |
4332 | ||
b04cc6b1 FW |
4333 | static struct dentry *d_percpu; |
4334 | ||
4335 | struct dentry *tracing_dentry_percpu(void) | |
4336 | { | |
4337 | static int once; | |
4338 | struct dentry *d_tracer; | |
4339 | ||
4340 | if (d_percpu) | |
4341 | return d_percpu; | |
4342 | ||
4343 | d_tracer = tracing_init_dentry(); | |
4344 | ||
4345 | if (!d_tracer) | |
4346 | return NULL; | |
4347 | ||
4348 | d_percpu = debugfs_create_dir("per_cpu", d_tracer); | |
4349 | ||
4350 | if (!d_percpu && !once) { | |
4351 | once = 1; | |
4352 | pr_warning("Could not create debugfs directory 'per_cpu'\n"); | |
4353 | return NULL; | |
4354 | } | |
4355 | ||
4356 | return d_percpu; | |
4357 | } | |
4358 | ||
4359 | static void tracing_init_debugfs_percpu(long cpu) | |
4360 | { | |
4361 | struct dentry *d_percpu = tracing_dentry_percpu(); | |
5452af66 | 4362 | struct dentry *d_cpu; |
dd49a38c | 4363 | char cpu_dir[30]; /* 30 characters should be more than enough */ |
b04cc6b1 | 4364 | |
dd49a38c | 4365 | snprintf(cpu_dir, 30, "cpu%ld", cpu); |
8656e7a2 FW |
4366 | d_cpu = debugfs_create_dir(cpu_dir, d_percpu); |
4367 | if (!d_cpu) { | |
4368 | pr_warning("Could not create debugfs '%s' entry\n", cpu_dir); | |
4369 | return; | |
4370 | } | |
b04cc6b1 | 4371 | |
8656e7a2 | 4372 | /* per cpu trace_pipe */ |
5452af66 FW |
4373 | trace_create_file("trace_pipe", 0444, d_cpu, |
4374 | (void *) cpu, &tracing_pipe_fops); | |
b04cc6b1 FW |
4375 | |
4376 | /* per cpu trace */ | |
5452af66 FW |
4377 | trace_create_file("trace", 0644, d_cpu, |
4378 | (void *) cpu, &tracing_fops); | |
7f96f93f | 4379 | |
5452af66 FW |
4380 | trace_create_file("trace_pipe_raw", 0444, d_cpu, |
4381 | (void *) cpu, &tracing_buffers_fops); | |
7f96f93f | 4382 | |
c8d77183 SR |
4383 | trace_create_file("stats", 0444, d_cpu, |
4384 | (void *) cpu, &tracing_stats_fops); | |
b04cc6b1 FW |
4385 | } |
4386 | ||
60a11774 SR |
4387 | #ifdef CONFIG_FTRACE_SELFTEST |
4388 | /* Let selftest have access to static functions in this file */ | |
4389 | #include "trace_selftest.c" | |
4390 | #endif | |
4391 | ||
577b785f SR |
4392 | struct trace_option_dentry { |
4393 | struct tracer_opt *opt; | |
4394 | struct tracer_flags *flags; | |
4395 | struct dentry *entry; | |
4396 | }; | |
4397 | ||
4398 | static ssize_t | |
4399 | trace_options_read(struct file *filp, char __user *ubuf, size_t cnt, | |
4400 | loff_t *ppos) | |
4401 | { | |
4402 | struct trace_option_dentry *topt = filp->private_data; | |
4403 | char *buf; | |
4404 | ||
4405 | if (topt->flags->val & topt->opt->bit) | |
4406 | buf = "1\n"; | |
4407 | else | |
4408 | buf = "0\n"; | |
4409 | ||
4410 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); | |
4411 | } | |
4412 | ||
4413 | static ssize_t | |
4414 | trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt, | |
4415 | loff_t *ppos) | |
4416 | { | |
4417 | struct trace_option_dentry *topt = filp->private_data; | |
4418 | unsigned long val; | |
577b785f SR |
4419 | int ret; |
4420 | ||
22fe9b54 PH |
4421 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
4422 | if (ret) | |
577b785f SR |
4423 | return ret; |
4424 | ||
8d18eaaf LZ |
4425 | if (val != 0 && val != 1) |
4426 | return -EINVAL; | |
577b785f | 4427 | |
8d18eaaf | 4428 | if (!!(topt->flags->val & topt->opt->bit) != val) { |
577b785f | 4429 | mutex_lock(&trace_types_lock); |
8d18eaaf | 4430 | ret = __set_tracer_option(current_trace, topt->flags, |
c757bea9 | 4431 | topt->opt, !val); |
577b785f SR |
4432 | mutex_unlock(&trace_types_lock); |
4433 | if (ret) | |
4434 | return ret; | |
577b785f SR |
4435 | } |
4436 | ||
4437 | *ppos += cnt; | |
4438 | ||
4439 | return cnt; | |
4440 | } | |
4441 | ||
4442 | ||
4443 | static const struct file_operations trace_options_fops = { | |
4444 | .open = tracing_open_generic, | |
4445 | .read = trace_options_read, | |
4446 | .write = trace_options_write, | |
b444786f | 4447 | .llseek = generic_file_llseek, |
577b785f SR |
4448 | }; |
4449 | ||
a8259075 SR |
4450 | static ssize_t |
4451 | trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt, | |
4452 | loff_t *ppos) | |
4453 | { | |
4454 | long index = (long)filp->private_data; | |
4455 | char *buf; | |
4456 | ||
4457 | if (trace_flags & (1 << index)) | |
4458 | buf = "1\n"; | |
4459 | else | |
4460 | buf = "0\n"; | |
4461 | ||
4462 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); | |
4463 | } | |
4464 | ||
4465 | static ssize_t | |
4466 | trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt, | |
4467 | loff_t *ppos) | |
4468 | { | |
4469 | long index = (long)filp->private_data; | |
a8259075 SR |
4470 | unsigned long val; |
4471 | int ret; | |
4472 | ||
22fe9b54 PH |
4473 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
4474 | if (ret) | |
a8259075 SR |
4475 | return ret; |
4476 | ||
f2d84b65 | 4477 | if (val != 0 && val != 1) |
a8259075 | 4478 | return -EINVAL; |
f2d84b65 | 4479 | set_tracer_flags(1 << index, val); |
a8259075 SR |
4480 | |
4481 | *ppos += cnt; | |
4482 | ||
4483 | return cnt; | |
4484 | } | |
4485 | ||
a8259075 SR |
4486 | static const struct file_operations trace_options_core_fops = { |
4487 | .open = tracing_open_generic, | |
4488 | .read = trace_options_core_read, | |
4489 | .write = trace_options_core_write, | |
b444786f | 4490 | .llseek = generic_file_llseek, |
a8259075 SR |
4491 | }; |
4492 | ||
5452af66 | 4493 | struct dentry *trace_create_file(const char *name, |
f4ae40a6 | 4494 | umode_t mode, |
5452af66 FW |
4495 | struct dentry *parent, |
4496 | void *data, | |
4497 | const struct file_operations *fops) | |
4498 | { | |
4499 | struct dentry *ret; | |
4500 | ||
4501 | ret = debugfs_create_file(name, mode, parent, data, fops); | |
4502 | if (!ret) | |
4503 | pr_warning("Could not create debugfs '%s' entry\n", name); | |
4504 | ||
4505 | return ret; | |
4506 | } | |
4507 | ||
4508 | ||
a8259075 SR |
4509 | static struct dentry *trace_options_init_dentry(void) |
4510 | { | |
4511 | struct dentry *d_tracer; | |
4512 | static struct dentry *t_options; | |
4513 | ||
4514 | if (t_options) | |
4515 | return t_options; | |
4516 | ||
4517 | d_tracer = tracing_init_dentry(); | |
4518 | if (!d_tracer) | |
4519 | return NULL; | |
4520 | ||
4521 | t_options = debugfs_create_dir("options", d_tracer); | |
4522 | if (!t_options) { | |
4523 | pr_warning("Could not create debugfs directory 'options'\n"); | |
4524 | return NULL; | |
4525 | } | |
4526 | ||
4527 | return t_options; | |
4528 | } | |
4529 | ||
577b785f SR |
4530 | static void |
4531 | create_trace_option_file(struct trace_option_dentry *topt, | |
4532 | struct tracer_flags *flags, | |
4533 | struct tracer_opt *opt) | |
4534 | { | |
4535 | struct dentry *t_options; | |
577b785f SR |
4536 | |
4537 | t_options = trace_options_init_dentry(); | |
4538 | if (!t_options) | |
4539 | return; | |
4540 | ||
4541 | topt->flags = flags; | |
4542 | topt->opt = opt; | |
4543 | ||
5452af66 | 4544 | topt->entry = trace_create_file(opt->name, 0644, t_options, topt, |
577b785f SR |
4545 | &trace_options_fops); |
4546 | ||
577b785f SR |
4547 | } |
4548 | ||
4549 | static struct trace_option_dentry * | |
4550 | create_trace_option_files(struct tracer *tracer) | |
4551 | { | |
4552 | struct trace_option_dentry *topts; | |
4553 | struct tracer_flags *flags; | |
4554 | struct tracer_opt *opts; | |
4555 | int cnt; | |
4556 | ||
4557 | if (!tracer) | |
4558 | return NULL; | |
4559 | ||
4560 | flags = tracer->flags; | |
4561 | ||
4562 | if (!flags || !flags->opts) | |
4563 | return NULL; | |
4564 | ||
4565 | opts = flags->opts; | |
4566 | ||
4567 | for (cnt = 0; opts[cnt].name; cnt++) | |
4568 | ; | |
4569 | ||
0cfe8245 | 4570 | topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL); |
577b785f SR |
4571 | if (!topts) |
4572 | return NULL; | |
4573 | ||
4574 | for (cnt = 0; opts[cnt].name; cnt++) | |
4575 | create_trace_option_file(&topts[cnt], flags, | |
4576 | &opts[cnt]); | |
4577 | ||
4578 | return topts; | |
4579 | } | |
4580 | ||
4581 | static void | |
4582 | destroy_trace_option_files(struct trace_option_dentry *topts) | |
4583 | { | |
4584 | int cnt; | |
4585 | ||
4586 | if (!topts) | |
4587 | return; | |
4588 | ||
4589 | for (cnt = 0; topts[cnt].opt; cnt++) { | |
4590 | if (topts[cnt].entry) | |
4591 | debugfs_remove(topts[cnt].entry); | |
4592 | } | |
4593 | ||
4594 | kfree(topts); | |
4595 | } | |
4596 | ||
a8259075 SR |
4597 | static struct dentry * |
4598 | create_trace_option_core_file(const char *option, long index) | |
4599 | { | |
4600 | struct dentry *t_options; | |
a8259075 SR |
4601 | |
4602 | t_options = trace_options_init_dentry(); | |
4603 | if (!t_options) | |
4604 | return NULL; | |
4605 | ||
5452af66 | 4606 | return trace_create_file(option, 0644, t_options, (void *)index, |
a8259075 | 4607 | &trace_options_core_fops); |
a8259075 SR |
4608 | } |
4609 | ||
4610 | static __init void create_trace_options_dir(void) | |
4611 | { | |
4612 | struct dentry *t_options; | |
a8259075 SR |
4613 | int i; |
4614 | ||
4615 | t_options = trace_options_init_dentry(); | |
4616 | if (!t_options) | |
4617 | return; | |
4618 | ||
5452af66 FW |
4619 | for (i = 0; trace_options[i]; i++) |
4620 | create_trace_option_core_file(trace_options[i], i); | |
a8259075 SR |
4621 | } |
4622 | ||
499e5470 SR |
4623 | static ssize_t |
4624 | rb_simple_read(struct file *filp, char __user *ubuf, | |
4625 | size_t cnt, loff_t *ppos) | |
4626 | { | |
4627 | struct ring_buffer *buffer = filp->private_data; | |
4628 | char buf[64]; | |
4629 | int r; | |
4630 | ||
4631 | if (buffer) | |
4632 | r = ring_buffer_record_is_on(buffer); | |
4633 | else | |
4634 | r = 0; | |
4635 | ||
4636 | r = sprintf(buf, "%d\n", r); | |
4637 | ||
4638 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); | |
4639 | } | |
4640 | ||
4641 | static ssize_t | |
4642 | rb_simple_write(struct file *filp, const char __user *ubuf, | |
4643 | size_t cnt, loff_t *ppos) | |
4644 | { | |
4645 | struct ring_buffer *buffer = filp->private_data; | |
4646 | unsigned long val; | |
4647 | int ret; | |
4648 | ||
4649 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); | |
4650 | if (ret) | |
4651 | return ret; | |
4652 | ||
4653 | if (buffer) { | |
4654 | if (val) | |
4655 | ring_buffer_record_on(buffer); | |
4656 | else | |
4657 | ring_buffer_record_off(buffer); | |
4658 | } | |
4659 | ||
4660 | (*ppos)++; | |
4661 | ||
4662 | return cnt; | |
4663 | } | |
4664 | ||
4665 | static const struct file_operations rb_simple_fops = { | |
4666 | .open = tracing_open_generic, | |
4667 | .read = rb_simple_read, | |
4668 | .write = rb_simple_write, | |
4669 | .llseek = default_llseek, | |
4670 | }; | |
4671 | ||
b5ad384e | 4672 | static __init int tracer_init_debugfs(void) |
bc0c38d1 SR |
4673 | { |
4674 | struct dentry *d_tracer; | |
b04cc6b1 | 4675 | int cpu; |
bc0c38d1 | 4676 | |
7e53bd42 LJ |
4677 | trace_access_lock_init(); |
4678 | ||
bc0c38d1 SR |
4679 | d_tracer = tracing_init_dentry(); |
4680 | ||
5452af66 FW |
4681 | trace_create_file("tracing_enabled", 0644, d_tracer, |
4682 | &global_trace, &tracing_ctrl_fops); | |
bc0c38d1 | 4683 | |
5452af66 FW |
4684 | trace_create_file("trace_options", 0644, d_tracer, |
4685 | NULL, &tracing_iter_fops); | |
bc0c38d1 | 4686 | |
5452af66 FW |
4687 | trace_create_file("tracing_cpumask", 0644, d_tracer, |
4688 | NULL, &tracing_cpumask_fops); | |
4689 | ||
4690 | trace_create_file("trace", 0644, d_tracer, | |
4691 | (void *) TRACE_PIPE_ALL_CPU, &tracing_fops); | |
a8259075 | 4692 | |
5452af66 FW |
4693 | trace_create_file("available_tracers", 0444, d_tracer, |
4694 | &global_trace, &show_traces_fops); | |
4695 | ||
339ae5d3 | 4696 | trace_create_file("current_tracer", 0644, d_tracer, |
5452af66 FW |
4697 | &global_trace, &set_tracer_fops); |
4698 | ||
5d4a9dba | 4699 | #ifdef CONFIG_TRACER_MAX_TRACE |
5452af66 FW |
4700 | trace_create_file("tracing_max_latency", 0644, d_tracer, |
4701 | &tracing_max_latency, &tracing_max_lat_fops); | |
0e950173 | 4702 | #endif |
5452af66 FW |
4703 | |
4704 | trace_create_file("tracing_thresh", 0644, d_tracer, | |
4705 | &tracing_thresh, &tracing_max_lat_fops); | |
a8259075 | 4706 | |
339ae5d3 | 4707 | trace_create_file("README", 0444, d_tracer, |
5452af66 FW |
4708 | NULL, &tracing_readme_fops); |
4709 | ||
4710 | trace_create_file("trace_pipe", 0444, d_tracer, | |
b04cc6b1 | 4711 | (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops); |
5452af66 FW |
4712 | |
4713 | trace_create_file("buffer_size_kb", 0644, d_tracer, | |
4714 | &global_trace, &tracing_entries_fops); | |
4715 | ||
f81ab074 VN |
4716 | trace_create_file("buffer_total_size_kb", 0444, d_tracer, |
4717 | &global_trace, &tracing_total_entries_fops); | |
4718 | ||
4f271a2a VN |
4719 | trace_create_file("free_buffer", 0644, d_tracer, |
4720 | &global_trace, &tracing_free_buffer_fops); | |
4721 | ||
5452af66 FW |
4722 | trace_create_file("trace_marker", 0220, d_tracer, |
4723 | NULL, &tracing_mark_fops); | |
5bf9a1ee | 4724 | |
69abe6a5 AP |
4725 | trace_create_file("saved_cmdlines", 0444, d_tracer, |
4726 | NULL, &tracing_saved_cmdlines_fops); | |
5bf9a1ee | 4727 | |
5079f326 Z |
4728 | trace_create_file("trace_clock", 0644, d_tracer, NULL, |
4729 | &trace_clock_fops); | |
4730 | ||
499e5470 SR |
4731 | trace_create_file("tracing_on", 0644, d_tracer, |
4732 | global_trace.buffer, &rb_simple_fops); | |
4733 | ||
bc0c38d1 | 4734 | #ifdef CONFIG_DYNAMIC_FTRACE |
5452af66 FW |
4735 | trace_create_file("dyn_ftrace_total_info", 0444, d_tracer, |
4736 | &ftrace_update_tot_cnt, &tracing_dyn_info_fops); | |
bc0c38d1 | 4737 | #endif |
b04cc6b1 | 4738 | |
5452af66 FW |
4739 | create_trace_options_dir(); |
4740 | ||
b04cc6b1 FW |
4741 | for_each_tracing_cpu(cpu) |
4742 | tracing_init_debugfs_percpu(cpu); | |
4743 | ||
b5ad384e | 4744 | return 0; |
bc0c38d1 SR |
4745 | } |
4746 | ||
3f5a54e3 SR |
4747 | static int trace_panic_handler(struct notifier_block *this, |
4748 | unsigned long event, void *unused) | |
4749 | { | |
944ac425 | 4750 | if (ftrace_dump_on_oops) |
cecbca96 | 4751 | ftrace_dump(ftrace_dump_on_oops); |
3f5a54e3 SR |
4752 | return NOTIFY_OK; |
4753 | } | |
4754 | ||
4755 | static struct notifier_block trace_panic_notifier = { | |
4756 | .notifier_call = trace_panic_handler, | |
4757 | .next = NULL, | |
4758 | .priority = 150 /* priority: INT_MAX >= x >= 0 */ | |
4759 | }; | |
4760 | ||
4761 | static int trace_die_handler(struct notifier_block *self, | |
4762 | unsigned long val, | |
4763 | void *data) | |
4764 | { | |
4765 | switch (val) { | |
4766 | case DIE_OOPS: | |
944ac425 | 4767 | if (ftrace_dump_on_oops) |
cecbca96 | 4768 | ftrace_dump(ftrace_dump_on_oops); |
3f5a54e3 SR |
4769 | break; |
4770 | default: | |
4771 | break; | |
4772 | } | |
4773 | return NOTIFY_OK; | |
4774 | } | |
4775 | ||
4776 | static struct notifier_block trace_die_notifier = { | |
4777 | .notifier_call = trace_die_handler, | |
4778 | .priority = 200 | |
4779 | }; | |
4780 | ||
4781 | /* | |
4782 | * printk is set to max of 1024, we really don't need it that big. | |
4783 | * Nothing should be printing 1000 characters anyway. | |
4784 | */ | |
4785 | #define TRACE_MAX_PRINT 1000 | |
4786 | ||
4787 | /* | |
4788 | * Define here KERN_TRACE so that we have one place to modify | |
4789 | * it if we decide to change what log level the ftrace dump | |
4790 | * should be at. | |
4791 | */ | |
428aee14 | 4792 | #define KERN_TRACE KERN_EMERG |
3f5a54e3 | 4793 | |
955b61e5 | 4794 | void |
3f5a54e3 SR |
4795 | trace_printk_seq(struct trace_seq *s) |
4796 | { | |
4797 | /* Probably should print a warning here. */ | |
4798 | if (s->len >= 1000) | |
4799 | s->len = 1000; | |
4800 | ||
4801 | /* should be zero ended, but we are paranoid. */ | |
4802 | s->buffer[s->len] = 0; | |
4803 | ||
4804 | printk(KERN_TRACE "%s", s->buffer); | |
4805 | ||
f9520750 | 4806 | trace_seq_init(s); |
3f5a54e3 SR |
4807 | } |
4808 | ||
955b61e5 JW |
4809 | void trace_init_global_iter(struct trace_iterator *iter) |
4810 | { | |
4811 | iter->tr = &global_trace; | |
4812 | iter->trace = current_trace; | |
4813 | iter->cpu_file = TRACE_PIPE_ALL_CPU; | |
4814 | } | |
4815 | ||
cecbca96 FW |
4816 | static void |
4817 | __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode) | |
3f5a54e3 | 4818 | { |
445c8951 | 4819 | static arch_spinlock_t ftrace_dump_lock = |
edc35bd7 | 4820 | (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
3f5a54e3 SR |
4821 | /* use static because iter can be a bit big for the stack */ |
4822 | static struct trace_iterator iter; | |
cf586b61 | 4823 | unsigned int old_userobj; |
3f5a54e3 | 4824 | static int dump_ran; |
d769041f SR |
4825 | unsigned long flags; |
4826 | int cnt = 0, cpu; | |
3f5a54e3 SR |
4827 | |
4828 | /* only one dump */ | |
cd891ae0 | 4829 | local_irq_save(flags); |
0199c4e6 | 4830 | arch_spin_lock(&ftrace_dump_lock); |
3f5a54e3 SR |
4831 | if (dump_ran) |
4832 | goto out; | |
4833 | ||
4834 | dump_ran = 1; | |
4835 | ||
0ee6b6cf | 4836 | tracing_off(); |
cf586b61 | 4837 | |
e0a413f6 SR |
4838 | /* Did function tracer already get disabled? */ |
4839 | if (ftrace_is_dead()) { | |
4840 | printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n"); | |
4841 | printk("# MAY BE MISSING FUNCTION EVENTS\n"); | |
4842 | } | |
4843 | ||
cf586b61 FW |
4844 | if (disable_tracing) |
4845 | ftrace_kill(); | |
3f5a54e3 | 4846 | |
955b61e5 JW |
4847 | trace_init_global_iter(&iter); |
4848 | ||
d769041f | 4849 | for_each_tracing_cpu(cpu) { |
955b61e5 | 4850 | atomic_inc(&iter.tr->data[cpu]->disabled); |
d769041f SR |
4851 | } |
4852 | ||
cf586b61 FW |
4853 | old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ; |
4854 | ||
b54d3de9 TE |
4855 | /* don't look at user memory in panic mode */ |
4856 | trace_flags &= ~TRACE_ITER_SYM_USEROBJ; | |
4857 | ||
e543ad76 | 4858 | /* Simulate the iterator */ |
3f5a54e3 SR |
4859 | iter.tr = &global_trace; |
4860 | iter.trace = current_trace; | |
cecbca96 FW |
4861 | |
4862 | switch (oops_dump_mode) { | |
4863 | case DUMP_ALL: | |
4864 | iter.cpu_file = TRACE_PIPE_ALL_CPU; | |
4865 | break; | |
4866 | case DUMP_ORIG: | |
4867 | iter.cpu_file = raw_smp_processor_id(); | |
4868 | break; | |
4869 | case DUMP_NONE: | |
4870 | goto out_enable; | |
4871 | default: | |
4872 | printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n"); | |
4873 | iter.cpu_file = TRACE_PIPE_ALL_CPU; | |
4874 | } | |
4875 | ||
4876 | printk(KERN_TRACE "Dumping ftrace buffer:\n"); | |
3f5a54e3 SR |
4877 | |
4878 | /* | |
4879 | * We need to stop all tracing on all CPUS to read the | |
4880 | * the next buffer. This is a bit expensive, but is | |
4881 | * not done often. We fill all what we can read, | |
4882 | * and then release the locks again. | |
4883 | */ | |
4884 | ||
3f5a54e3 SR |
4885 | while (!trace_empty(&iter)) { |
4886 | ||
4887 | if (!cnt) | |
4888 | printk(KERN_TRACE "---------------------------------\n"); | |
4889 | ||
4890 | cnt++; | |
4891 | ||
4892 | /* reset all but tr, trace, and overruns */ | |
4893 | memset(&iter.seq, 0, | |
4894 | sizeof(struct trace_iterator) - | |
4895 | offsetof(struct trace_iterator, seq)); | |
4896 | iter.iter_flags |= TRACE_FILE_LAT_FMT; | |
4897 | iter.pos = -1; | |
4898 | ||
955b61e5 | 4899 | if (trace_find_next_entry_inc(&iter) != NULL) { |
74e7ff8c LJ |
4900 | int ret; |
4901 | ||
4902 | ret = print_trace_line(&iter); | |
4903 | if (ret != TRACE_TYPE_NO_CONSUME) | |
4904 | trace_consume(&iter); | |
3f5a54e3 SR |
4905 | } |
4906 | ||
4907 | trace_printk_seq(&iter.seq); | |
4908 | } | |
4909 | ||
4910 | if (!cnt) | |
4911 | printk(KERN_TRACE " (ftrace buffer empty)\n"); | |
4912 | else | |
4913 | printk(KERN_TRACE "---------------------------------\n"); | |
4914 | ||
cecbca96 | 4915 | out_enable: |
cf586b61 FW |
4916 | /* Re-enable tracing if requested */ |
4917 | if (!disable_tracing) { | |
4918 | trace_flags |= old_userobj; | |
4919 | ||
4920 | for_each_tracing_cpu(cpu) { | |
955b61e5 | 4921 | atomic_dec(&iter.tr->data[cpu]->disabled); |
cf586b61 FW |
4922 | } |
4923 | tracing_on(); | |
4924 | } | |
4925 | ||
3f5a54e3 | 4926 | out: |
0199c4e6 | 4927 | arch_spin_unlock(&ftrace_dump_lock); |
cd891ae0 | 4928 | local_irq_restore(flags); |
3f5a54e3 SR |
4929 | } |
4930 | ||
cf586b61 | 4931 | /* By default: disable tracing after the dump */ |
cecbca96 | 4932 | void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) |
cf586b61 | 4933 | { |
cecbca96 | 4934 | __ftrace_dump(true, oops_dump_mode); |
cf586b61 | 4935 | } |
a8eecf22 | 4936 | EXPORT_SYMBOL_GPL(ftrace_dump); |
cf586b61 | 4937 | |
3928a8a2 | 4938 | __init static int tracer_alloc_buffers(void) |
bc0c38d1 | 4939 | { |
73c5162a | 4940 | int ring_buf_size; |
750912fa | 4941 | enum ring_buffer_flags rb_flags; |
4c11d7ae | 4942 | int i; |
9e01c1b7 | 4943 | int ret = -ENOMEM; |
4c11d7ae | 4944 | |
750912fa | 4945 | |
9e01c1b7 RR |
4946 | if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL)) |
4947 | goto out; | |
4948 | ||
4949 | if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL)) | |
4950 | goto out_free_buffer_mask; | |
4c11d7ae | 4951 | |
73c5162a SR |
4952 | /* To save memory, keep the ring buffer size to its minimum */ |
4953 | if (ring_buffer_expanded) | |
4954 | ring_buf_size = trace_buf_size; | |
4955 | else | |
4956 | ring_buf_size = 1; | |
4957 | ||
750912fa DS |
4958 | rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0; |
4959 | ||
9e01c1b7 RR |
4960 | cpumask_copy(tracing_buffer_mask, cpu_possible_mask); |
4961 | cpumask_copy(tracing_cpumask, cpu_all_mask); | |
4962 | ||
4963 | /* TODO: make the number of buffers hot pluggable with CPUS */ | |
750912fa | 4964 | global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags); |
3928a8a2 SR |
4965 | if (!global_trace.buffer) { |
4966 | printk(KERN_ERR "tracer: failed to allocate ring buffer!\n"); | |
4967 | WARN_ON(1); | |
9e01c1b7 | 4968 | goto out_free_cpumask; |
4c11d7ae | 4969 | } |
3928a8a2 | 4970 | global_trace.entries = ring_buffer_size(global_trace.buffer); |
499e5470 SR |
4971 | if (global_trace.buffer_disabled) |
4972 | tracing_off(); | |
4c11d7ae | 4973 | |
9e01c1b7 | 4974 | |
4c11d7ae | 4975 | #ifdef CONFIG_TRACER_MAX_TRACE |
750912fa | 4976 | max_tr.buffer = ring_buffer_alloc(1, rb_flags); |
3928a8a2 SR |
4977 | if (!max_tr.buffer) { |
4978 | printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n"); | |
4979 | WARN_ON(1); | |
4980 | ring_buffer_free(global_trace.buffer); | |
9e01c1b7 | 4981 | goto out_free_cpumask; |
4c11d7ae | 4982 | } |
ef710e10 | 4983 | max_tr.entries = 1; |
a98a3c3f | 4984 | #endif |
ab46428c | 4985 | |
4c11d7ae | 4986 | /* Allocate the first page for all buffers */ |
ab46428c | 4987 | for_each_tracing_cpu(i) { |
566b0aaf | 4988 | global_trace.data[i] = &per_cpu(global_trace_cpu, i); |
9705f69e | 4989 | max_tr.data[i] = &per_cpu(max_tr_data, i); |
4c11d7ae | 4990 | } |
bc0c38d1 | 4991 | |
bc0c38d1 SR |
4992 | trace_init_cmdlines(); |
4993 | ||
43a15386 | 4994 | register_tracer(&nop_trace); |
79fb0768 | 4995 | current_trace = &nop_trace; |
60a11774 SR |
4996 | /* All seems OK, enable tracing */ |
4997 | tracing_disabled = 0; | |
3928a8a2 | 4998 | |
3f5a54e3 SR |
4999 | atomic_notifier_chain_register(&panic_notifier_list, |
5000 | &trace_panic_notifier); | |
5001 | ||
5002 | register_die_notifier(&trace_die_notifier); | |
2fc1dfbe FW |
5003 | |
5004 | return 0; | |
3f5a54e3 | 5005 | |
9e01c1b7 RR |
5006 | out_free_cpumask: |
5007 | free_cpumask_var(tracing_cpumask); | |
5008 | out_free_buffer_mask: | |
5009 | free_cpumask_var(tracing_buffer_mask); | |
5010 | out: | |
5011 | return ret; | |
bc0c38d1 | 5012 | } |
b2821ae6 SR |
5013 | |
5014 | __init static int clear_boot_tracer(void) | |
5015 | { | |
5016 | /* | |
5017 | * The default tracer at boot buffer is an init section. | |
5018 | * This function is called in lateinit. If we did not | |
5019 | * find the boot tracer, then clear it out, to prevent | |
5020 | * later registration from accessing the buffer that is | |
5021 | * about to be freed. | |
5022 | */ | |
5023 | if (!default_bootup_tracer) | |
5024 | return 0; | |
5025 | ||
5026 | printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n", | |
5027 | default_bootup_tracer); | |
5028 | default_bootup_tracer = NULL; | |
5029 | ||
5030 | return 0; | |
5031 | } | |
5032 | ||
b5ad384e FW |
5033 | early_initcall(tracer_alloc_buffers); |
5034 | fs_initcall(tracer_init_debugfs); | |
b2821ae6 | 5035 | late_initcall(clear_boot_tracer); |