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