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