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