]>
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 | */ | |
14 | #include <linux/utsrelease.h> | |
15 | #include <linux/kallsyms.h> | |
16 | #include <linux/seq_file.h> | |
3f5a54e3 | 17 | #include <linux/notifier.h> |
bc0c38d1 | 18 | #include <linux/debugfs.h> |
4c11d7ae | 19 | #include <linux/pagemap.h> |
bc0c38d1 SR |
20 | #include <linux/hardirq.h> |
21 | #include <linux/linkage.h> | |
22 | #include <linux/uaccess.h> | |
23 | #include <linux/ftrace.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/percpu.h> | |
3f5a54e3 | 26 | #include <linux/kdebug.h> |
bc0c38d1 SR |
27 | #include <linux/ctype.h> |
28 | #include <linux/init.h> | |
2a2cc8f7 | 29 | #include <linux/poll.h> |
bc0c38d1 SR |
30 | #include <linux/gfp.h> |
31 | #include <linux/fs.h> | |
76094a2c | 32 | #include <linux/kprobes.h> |
3eefae99 | 33 | #include <linux/writeback.h> |
bc0c38d1 | 34 | |
86387f7e | 35 | #include <linux/stacktrace.h> |
3928a8a2 | 36 | #include <linux/ring_buffer.h> |
21798a84 | 37 | #include <linux/irqflags.h> |
86387f7e | 38 | |
bc0c38d1 SR |
39 | #include "trace.h" |
40 | ||
3928a8a2 SR |
41 | #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE) |
42 | ||
bc0c38d1 SR |
43 | unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX; |
44 | unsigned long __read_mostly tracing_thresh; | |
45 | ||
8e1b82e0 FW |
46 | /* |
47 | * We need to change this state when a selftest is running. | |
ff32504f FW |
48 | * A selftest will lurk into the ring-buffer to count the |
49 | * entries inserted during the selftest although some concurrent | |
50 | * insertions into the ring-buffer such as ftrace_printk could occurred | |
51 | * at the same time, giving false positive or negative results. | |
52 | */ | |
8e1b82e0 | 53 | static bool __read_mostly tracing_selftest_running; |
ff32504f | 54 | |
adf9f195 FW |
55 | /* For tracers that don't implement custom flags */ |
56 | static struct tracer_opt dummy_tracer_opt[] = { | |
57 | { } | |
58 | }; | |
59 | ||
60 | static struct tracer_flags dummy_tracer_flags = { | |
61 | .val = 0, | |
62 | .opts = dummy_tracer_opt | |
63 | }; | |
64 | ||
65 | static int dummy_set_flag(u32 old_flags, u32 bit, int set) | |
66 | { | |
67 | return 0; | |
68 | } | |
0f048701 SR |
69 | |
70 | /* | |
71 | * Kill all tracing for good (never come back). | |
72 | * It is initialized to 1 but will turn to zero if the initialization | |
73 | * of the tracer is successful. But that is the only place that sets | |
74 | * this back to zero. | |
75 | */ | |
76 | int tracing_disabled = 1; | |
77 | ||
d769041f SR |
78 | static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled); |
79 | ||
80 | static inline void ftrace_disable_cpu(void) | |
81 | { | |
82 | preempt_disable(); | |
83 | local_inc(&__get_cpu_var(ftrace_cpu_disabled)); | |
84 | } | |
85 | ||
86 | static inline void ftrace_enable_cpu(void) | |
87 | { | |
88 | local_dec(&__get_cpu_var(ftrace_cpu_disabled)); | |
89 | preempt_enable(); | |
90 | } | |
91 | ||
ab46428c SR |
92 | static cpumask_t __read_mostly tracing_buffer_mask; |
93 | ||
94 | #define for_each_tracing_cpu(cpu) \ | |
95 | for_each_cpu_mask(cpu, tracing_buffer_mask) | |
96 | ||
944ac425 SR |
97 | /* |
98 | * ftrace_dump_on_oops - variable to dump ftrace buffer on oops | |
99 | * | |
100 | * If there is an oops (or kernel panic) and the ftrace_dump_on_oops | |
101 | * is set, then ftrace_dump is called. This will output the contents | |
102 | * of the ftrace buffers to the console. This is very useful for | |
103 | * capturing traces that lead to crashes and outputing it to a | |
104 | * serial console. | |
105 | * | |
106 | * It is default off, but you can enable it with either specifying | |
107 | * "ftrace_dump_on_oops" in the kernel command line, or setting | |
108 | * /proc/sys/kernel/ftrace_dump_on_oops to true. | |
109 | */ | |
110 | int ftrace_dump_on_oops; | |
111 | ||
d9e54076 PZ |
112 | static int tracing_set_tracer(char *buf); |
113 | ||
114 | static int __init set_ftrace(char *str) | |
115 | { | |
116 | tracing_set_tracer(str); | |
117 | return 1; | |
118 | } | |
119 | __setup("ftrace", set_ftrace); | |
120 | ||
944ac425 SR |
121 | static int __init set_ftrace_dump_on_oops(char *str) |
122 | { | |
123 | ftrace_dump_on_oops = 1; | |
124 | return 1; | |
125 | } | |
126 | __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops); | |
60a11774 | 127 | |
72829bc3 | 128 | long |
bc0c38d1 SR |
129 | ns2usecs(cycle_t nsec) |
130 | { | |
131 | nsec += 500; | |
132 | do_div(nsec, 1000); | |
133 | return nsec; | |
134 | } | |
135 | ||
e309b41d | 136 | cycle_t ftrace_now(int cpu) |
750ed1a4 | 137 | { |
3928a8a2 SR |
138 | u64 ts = ring_buffer_time_stamp(cpu); |
139 | ring_buffer_normalize_time_stamp(cpu, &ts); | |
140 | return ts; | |
750ed1a4 IM |
141 | } |
142 | ||
4fcdae83 SR |
143 | /* |
144 | * The global_trace is the descriptor that holds the tracing | |
145 | * buffers for the live tracing. For each CPU, it contains | |
146 | * a link list of pages that will store trace entries. The | |
147 | * page descriptor of the pages in the memory is used to hold | |
148 | * the link list by linking the lru item in the page descriptor | |
149 | * to each of the pages in the buffer per CPU. | |
150 | * | |
151 | * For each active CPU there is a data field that holds the | |
152 | * pages for the buffer for that CPU. Each CPU has the same number | |
153 | * of pages allocated for its buffer. | |
154 | */ | |
bc0c38d1 SR |
155 | static struct trace_array global_trace; |
156 | ||
157 | static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu); | |
158 | ||
4fcdae83 SR |
159 | /* |
160 | * The max_tr is used to snapshot the global_trace when a maximum | |
161 | * latency is reached. Some tracers will use this to store a maximum | |
162 | * trace while it continues examining live traces. | |
163 | * | |
164 | * The buffers for the max_tr are set up the same as the global_trace. | |
165 | * When a snapshot is taken, the link list of the max_tr is swapped | |
166 | * with the link list of the global_trace and the buffers are reset for | |
167 | * the global_trace so the tracing can continue. | |
168 | */ | |
bc0c38d1 SR |
169 | static struct trace_array max_tr; |
170 | ||
171 | static DEFINE_PER_CPU(struct trace_array_cpu, max_data); | |
172 | ||
4fcdae83 | 173 | /* tracer_enabled is used to toggle activation of a tracer */ |
26994ead | 174 | static int tracer_enabled = 1; |
4fcdae83 | 175 | |
9036990d SR |
176 | /** |
177 | * tracing_is_enabled - return tracer_enabled status | |
178 | * | |
179 | * This function is used by other tracers to know the status | |
180 | * of the tracer_enabled flag. Tracers may use this function | |
181 | * to know if it should enable their features when starting | |
182 | * up. See irqsoff tracer for an example (start_irqsoff_tracer). | |
183 | */ | |
184 | int tracing_is_enabled(void) | |
185 | { | |
186 | return tracer_enabled; | |
187 | } | |
188 | ||
60bc0800 SR |
189 | /* function tracing enabled */ |
190 | int ftrace_function_enabled; | |
191 | ||
4fcdae83 | 192 | /* |
3928a8a2 SR |
193 | * trace_buf_size is the size in bytes that is allocated |
194 | * for a buffer. Note, the number of bytes is always rounded | |
195 | * to page size. | |
3f5a54e3 SR |
196 | * |
197 | * This number is purposely set to a low number of 16384. | |
198 | * If the dump on oops happens, it will be much appreciated | |
199 | * to not have to wait for all that output. Anyway this can be | |
200 | * boot time and run time configurable. | |
4fcdae83 | 201 | */ |
3928a8a2 | 202 | #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */ |
3f5a54e3 | 203 | |
3928a8a2 | 204 | static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT; |
bc0c38d1 | 205 | |
4fcdae83 | 206 | /* trace_types holds a link list of available tracers. */ |
bc0c38d1 | 207 | static struct tracer *trace_types __read_mostly; |
4fcdae83 SR |
208 | |
209 | /* current_trace points to the tracer that is currently active */ | |
bc0c38d1 | 210 | static struct tracer *current_trace __read_mostly; |
4fcdae83 SR |
211 | |
212 | /* | |
213 | * max_tracer_type_len is used to simplify the allocating of | |
214 | * buffers to read userspace tracer names. We keep track of | |
215 | * the longest tracer name registered. | |
216 | */ | |
bc0c38d1 SR |
217 | static int max_tracer_type_len; |
218 | ||
4fcdae83 SR |
219 | /* |
220 | * trace_types_lock is used to protect the trace_types list. | |
221 | * This lock is also used to keep user access serialized. | |
222 | * Accesses from userspace will grab this lock while userspace | |
223 | * activities happen inside the kernel. | |
224 | */ | |
bc0c38d1 | 225 | static DEFINE_MUTEX(trace_types_lock); |
4fcdae83 SR |
226 | |
227 | /* trace_wait is a waitqueue for tasks blocked on trace_poll */ | |
4e655519 IM |
228 | static DECLARE_WAIT_QUEUE_HEAD(trace_wait); |
229 | ||
ee6bce52 | 230 | /* trace_flags holds trace_options default values */ |
12ef7d44 SR |
231 | unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | |
232 | TRACE_ITER_ANNOTATE; | |
4e655519 | 233 | |
4fcdae83 SR |
234 | /** |
235 | * trace_wake_up - wake up tasks waiting for trace input | |
236 | * | |
237 | * Simply wakes up any task that is blocked on the trace_wait | |
238 | * queue. These is used with trace_poll for tasks polling the trace. | |
239 | */ | |
4e655519 IM |
240 | void trace_wake_up(void) |
241 | { | |
017730c1 IM |
242 | /* |
243 | * The runqueue_is_locked() can fail, but this is the best we | |
244 | * have for now: | |
245 | */ | |
246 | if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked()) | |
4e655519 IM |
247 | wake_up(&trace_wait); |
248 | } | |
bc0c38d1 | 249 | |
3928a8a2 | 250 | static int __init set_buf_size(char *str) |
bc0c38d1 | 251 | { |
3928a8a2 | 252 | unsigned long buf_size; |
c6caeeb1 SR |
253 | int ret; |
254 | ||
bc0c38d1 SR |
255 | if (!str) |
256 | return 0; | |
3928a8a2 | 257 | ret = strict_strtoul(str, 0, &buf_size); |
c6caeeb1 | 258 | /* nr_entries can not be zero */ |
3928a8a2 | 259 | if (ret < 0 || buf_size == 0) |
c6caeeb1 | 260 | return 0; |
3928a8a2 | 261 | trace_buf_size = buf_size; |
bc0c38d1 SR |
262 | return 1; |
263 | } | |
3928a8a2 | 264 | __setup("trace_buf_size=", set_buf_size); |
bc0c38d1 | 265 | |
57f50be1 SR |
266 | unsigned long nsecs_to_usecs(unsigned long nsecs) |
267 | { | |
268 | return nsecs / 1000; | |
269 | } | |
270 | ||
4fcdae83 | 271 | /* These must match the bit postions in trace_iterator_flags */ |
bc0c38d1 SR |
272 | static const char *trace_options[] = { |
273 | "print-parent", | |
274 | "sym-offset", | |
275 | "sym-addr", | |
276 | "verbose", | |
f9896bf3 | 277 | "raw", |
5e3ca0ec | 278 | "hex", |
cb0f12aa | 279 | "bin", |
2a2cc8f7 | 280 | "block", |
86387f7e | 281 | "stacktrace", |
4ac3ba41 | 282 | "sched-tree", |
f09ce573 | 283 | "ftrace_printk", |
b2a866f9 | 284 | "ftrace_preempt", |
9f029e83 | 285 | "branch", |
12ef7d44 | 286 | "annotate", |
02b67518 | 287 | "userstacktrace", |
b54d3de9 | 288 | "sym-userobj", |
66896a85 | 289 | "printk-msg-only", |
bc0c38d1 SR |
290 | NULL |
291 | }; | |
292 | ||
4fcdae83 SR |
293 | /* |
294 | * ftrace_max_lock is used to protect the swapping of buffers | |
295 | * when taking a max snapshot. The buffers themselves are | |
296 | * protected by per_cpu spinlocks. But the action of the swap | |
297 | * needs its own lock. | |
298 | * | |
299 | * This is defined as a raw_spinlock_t in order to help | |
300 | * with performance when lockdep debugging is enabled. | |
301 | */ | |
92205c23 SR |
302 | static raw_spinlock_t ftrace_max_lock = |
303 | (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; | |
bc0c38d1 SR |
304 | |
305 | /* | |
306 | * Copy the new maximum trace into the separate maximum-trace | |
307 | * structure. (this way the maximum trace is permanently saved, | |
308 | * for later retrieval via /debugfs/tracing/latency_trace) | |
309 | */ | |
e309b41d | 310 | static void |
bc0c38d1 SR |
311 | __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
312 | { | |
313 | struct trace_array_cpu *data = tr->data[cpu]; | |
314 | ||
315 | max_tr.cpu = cpu; | |
316 | max_tr.time_start = data->preempt_timestamp; | |
317 | ||
318 | data = max_tr.data[cpu]; | |
319 | data->saved_latency = tracing_max_latency; | |
320 | ||
321 | memcpy(data->comm, tsk->comm, TASK_COMM_LEN); | |
322 | data->pid = tsk->pid; | |
b6dff3ec | 323 | data->uid = task_uid(tsk); |
bc0c38d1 SR |
324 | data->nice = tsk->static_prio - 20 - MAX_RT_PRIO; |
325 | data->policy = tsk->policy; | |
326 | data->rt_priority = tsk->rt_priority; | |
327 | ||
328 | /* record this tasks comm */ | |
329 | tracing_record_cmdline(current); | |
330 | } | |
331 | ||
4fcdae83 SR |
332 | /** |
333 | * trace_seq_printf - sequence printing of trace information | |
334 | * @s: trace sequence descriptor | |
335 | * @fmt: printf format string | |
336 | * | |
337 | * The tracer may use either sequence operations or its own | |
338 | * copy to user routines. To simplify formating of a trace | |
339 | * trace_seq_printf is used to store strings into a special | |
340 | * buffer (@s). Then the output may be either used by | |
341 | * the sequencer or pulled into another buffer. | |
342 | */ | |
72829bc3 | 343 | int |
214023c3 SR |
344 | trace_seq_printf(struct trace_seq *s, const char *fmt, ...) |
345 | { | |
346 | int len = (PAGE_SIZE - 1) - s->len; | |
347 | va_list ap; | |
b3806b43 | 348 | int ret; |
214023c3 SR |
349 | |
350 | if (!len) | |
351 | return 0; | |
352 | ||
353 | va_start(ap, fmt); | |
b3806b43 | 354 | ret = vsnprintf(s->buffer + s->len, len, fmt, ap); |
214023c3 SR |
355 | va_end(ap); |
356 | ||
b3806b43 | 357 | /* If we can't write it all, don't bother writing anything */ |
72829bc3 | 358 | if (ret >= len) |
b3806b43 SR |
359 | return 0; |
360 | ||
361 | s->len += ret; | |
214023c3 SR |
362 | |
363 | return len; | |
364 | } | |
365 | ||
4fcdae83 SR |
366 | /** |
367 | * trace_seq_puts - trace sequence printing of simple string | |
368 | * @s: trace sequence descriptor | |
369 | * @str: simple string to record | |
370 | * | |
371 | * The tracer may use either the sequence operations or its own | |
372 | * copy to user routines. This function records a simple string | |
373 | * into a special buffer (@s) for later retrieval by a sequencer | |
374 | * or other mechanism. | |
375 | */ | |
e309b41d | 376 | static int |
214023c3 SR |
377 | trace_seq_puts(struct trace_seq *s, const char *str) |
378 | { | |
379 | int len = strlen(str); | |
380 | ||
381 | if (len > ((PAGE_SIZE - 1) - s->len)) | |
b3806b43 | 382 | return 0; |
214023c3 SR |
383 | |
384 | memcpy(s->buffer + s->len, str, len); | |
385 | s->len += len; | |
386 | ||
387 | return len; | |
388 | } | |
389 | ||
e309b41d | 390 | static int |
214023c3 SR |
391 | trace_seq_putc(struct trace_seq *s, unsigned char c) |
392 | { | |
393 | if (s->len >= (PAGE_SIZE - 1)) | |
394 | return 0; | |
395 | ||
396 | s->buffer[s->len++] = c; | |
397 | ||
398 | return 1; | |
399 | } | |
400 | ||
e309b41d | 401 | static int |
cb0f12aa IM |
402 | trace_seq_putmem(struct trace_seq *s, void *mem, size_t len) |
403 | { | |
404 | if (len > ((PAGE_SIZE - 1) - s->len)) | |
405 | return 0; | |
406 | ||
407 | memcpy(s->buffer + s->len, mem, len); | |
408 | s->len += len; | |
409 | ||
410 | return len; | |
411 | } | |
412 | ||
ad0a3b68 HH |
413 | #define MAX_MEMHEX_BYTES 8 |
414 | #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1) | |
5e3ca0ec | 415 | |
e309b41d | 416 | static int |
5e3ca0ec IM |
417 | trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len) |
418 | { | |
419 | unsigned char hex[HEX_CHARS]; | |
93dcc6ea | 420 | unsigned char *data = mem; |
5e3ca0ec IM |
421 | int i, j; |
422 | ||
5e3ca0ec IM |
423 | #ifdef __BIG_ENDIAN |
424 | for (i = 0, j = 0; i < len; i++) { | |
425 | #else | |
426 | for (i = len-1, j = 0; i >= 0; i--) { | |
427 | #endif | |
2fbc4749 HH |
428 | hex[j++] = hex_asc_hi(data[i]); |
429 | hex[j++] = hex_asc_lo(data[i]); | |
5e3ca0ec | 430 | } |
93dcc6ea | 431 | hex[j++] = ' '; |
5e3ca0ec IM |
432 | |
433 | return trace_seq_putmem(s, hex, j); | |
434 | } | |
435 | ||
b54d3de9 TE |
436 | static int |
437 | trace_seq_path(struct trace_seq *s, struct path *path) | |
438 | { | |
439 | unsigned char *p; | |
440 | ||
441 | if (s->len >= (PAGE_SIZE - 1)) | |
442 | return 0; | |
443 | p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len); | |
444 | if (!IS_ERR(p)) { | |
445 | p = mangle_path(s->buffer + s->len, p, "\n"); | |
446 | if (p) { | |
447 | s->len = p - s->buffer; | |
448 | return 1; | |
449 | } | |
450 | } else { | |
451 | s->buffer[s->len++] = '?'; | |
452 | return 1; | |
453 | } | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
e309b41d | 458 | static void |
214023c3 SR |
459 | trace_seq_reset(struct trace_seq *s) |
460 | { | |
461 | s->len = 0; | |
6c6c2796 PP |
462 | s->readpos = 0; |
463 | } | |
464 | ||
465 | ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt) | |
466 | { | |
467 | int len; | |
468 | int ret; | |
469 | ||
470 | if (s->len <= s->readpos) | |
471 | return -EBUSY; | |
472 | ||
473 | len = s->len - s->readpos; | |
474 | if (cnt > len) | |
475 | cnt = len; | |
476 | ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt); | |
477 | if (ret) | |
478 | return -EFAULT; | |
479 | ||
480 | s->readpos += len; | |
481 | return cnt; | |
214023c3 SR |
482 | } |
483 | ||
e309b41d | 484 | static void |
214023c3 SR |
485 | trace_print_seq(struct seq_file *m, struct trace_seq *s) |
486 | { | |
487 | int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len; | |
488 | ||
489 | s->buffer[len] = 0; | |
490 | seq_puts(m, s->buffer); | |
491 | ||
492 | trace_seq_reset(s); | |
493 | } | |
494 | ||
4fcdae83 SR |
495 | /** |
496 | * update_max_tr - snapshot all trace buffers from global_trace to max_tr | |
497 | * @tr: tracer | |
498 | * @tsk: the task with the latency | |
499 | * @cpu: The cpu that initiated the trace. | |
500 | * | |
501 | * Flip the buffers between the @tr and the max_tr and record information | |
502 | * about which task was the cause of this latency. | |
503 | */ | |
e309b41d | 504 | void |
bc0c38d1 SR |
505 | update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
506 | { | |
3928a8a2 | 507 | struct ring_buffer *buf = tr->buffer; |
bc0c38d1 | 508 | |
4c11d7ae | 509 | WARN_ON_ONCE(!irqs_disabled()); |
92205c23 | 510 | __raw_spin_lock(&ftrace_max_lock); |
3928a8a2 SR |
511 | |
512 | tr->buffer = max_tr.buffer; | |
513 | max_tr.buffer = buf; | |
514 | ||
d769041f | 515 | ftrace_disable_cpu(); |
3928a8a2 | 516 | ring_buffer_reset(tr->buffer); |
d769041f | 517 | ftrace_enable_cpu(); |
bc0c38d1 SR |
518 | |
519 | __update_max_tr(tr, tsk, cpu); | |
92205c23 | 520 | __raw_spin_unlock(&ftrace_max_lock); |
bc0c38d1 SR |
521 | } |
522 | ||
523 | /** | |
524 | * update_max_tr_single - only copy one trace over, and reset the rest | |
525 | * @tr - tracer | |
526 | * @tsk - task with the latency | |
527 | * @cpu - the cpu of the buffer to copy. | |
4fcdae83 SR |
528 | * |
529 | * Flip the trace of a single CPU buffer between the @tr and the max_tr. | |
bc0c38d1 | 530 | */ |
e309b41d | 531 | void |
bc0c38d1 SR |
532 | update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) |
533 | { | |
3928a8a2 | 534 | int ret; |
bc0c38d1 | 535 | |
4c11d7ae | 536 | WARN_ON_ONCE(!irqs_disabled()); |
92205c23 | 537 | __raw_spin_lock(&ftrace_max_lock); |
bc0c38d1 | 538 | |
d769041f SR |
539 | ftrace_disable_cpu(); |
540 | ||
3928a8a2 SR |
541 | ring_buffer_reset(max_tr.buffer); |
542 | ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu); | |
543 | ||
d769041f SR |
544 | ftrace_enable_cpu(); |
545 | ||
3928a8a2 | 546 | WARN_ON_ONCE(ret); |
bc0c38d1 SR |
547 | |
548 | __update_max_tr(tr, tsk, cpu); | |
92205c23 | 549 | __raw_spin_unlock(&ftrace_max_lock); |
bc0c38d1 SR |
550 | } |
551 | ||
4fcdae83 SR |
552 | /** |
553 | * register_tracer - register a tracer with the ftrace system. | |
554 | * @type - the plugin for the tracer | |
555 | * | |
556 | * Register a new plugin tracer. | |
557 | */ | |
bc0c38d1 SR |
558 | int register_tracer(struct tracer *type) |
559 | { | |
560 | struct tracer *t; | |
561 | int len; | |
562 | int ret = 0; | |
563 | ||
564 | if (!type->name) { | |
565 | pr_info("Tracer must have a name\n"); | |
566 | return -1; | |
567 | } | |
568 | ||
86fa2f60 IM |
569 | /* |
570 | * When this gets called we hold the BKL which means that | |
571 | * preemption is disabled. Various trace selftests however | |
572 | * need to disable and enable preemption for successful tests. | |
573 | * So we drop the BKL here and grab it after the tests again. | |
574 | */ | |
575 | unlock_kernel(); | |
bc0c38d1 | 576 | mutex_lock(&trace_types_lock); |
86fa2f60 | 577 | |
8e1b82e0 FW |
578 | tracing_selftest_running = true; |
579 | ||
bc0c38d1 SR |
580 | for (t = trace_types; t; t = t->next) { |
581 | if (strcmp(type->name, t->name) == 0) { | |
582 | /* already found */ | |
583 | pr_info("Trace %s already registered\n", | |
584 | type->name); | |
585 | ret = -1; | |
586 | goto out; | |
587 | } | |
588 | } | |
589 | ||
adf9f195 FW |
590 | if (!type->set_flag) |
591 | type->set_flag = &dummy_set_flag; | |
592 | if (!type->flags) | |
593 | type->flags = &dummy_tracer_flags; | |
594 | else | |
595 | if (!type->flags->opts) | |
596 | type->flags->opts = dummy_tracer_opt; | |
597 | ||
60a11774 SR |
598 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
599 | if (type->selftest) { | |
600 | struct tracer *saved_tracer = current_trace; | |
60a11774 | 601 | struct trace_array *tr = &global_trace; |
60a11774 | 602 | int i; |
ff32504f | 603 | |
60a11774 SR |
604 | /* |
605 | * Run a selftest on this tracer. | |
606 | * Here we reset the trace buffer, and set the current | |
607 | * tracer to be this tracer. The tracer can then run some | |
608 | * internal tracing to verify that everything is in order. | |
609 | * If we fail, we do not register this tracer. | |
610 | */ | |
86fa2f60 | 611 | for_each_tracing_cpu(i) |
3928a8a2 | 612 | tracing_reset(tr, i); |
86fa2f60 | 613 | |
60a11774 | 614 | current_trace = type; |
60a11774 SR |
615 | /* the test is responsible for initializing and enabling */ |
616 | pr_info("Testing tracer %s: ", type->name); | |
617 | ret = type->selftest(type, tr); | |
618 | /* the test is responsible for resetting too */ | |
619 | current_trace = saved_tracer; | |
60a11774 SR |
620 | if (ret) { |
621 | printk(KERN_CONT "FAILED!\n"); | |
622 | goto out; | |
623 | } | |
1d4db00a | 624 | /* Only reset on passing, to avoid touching corrupted buffers */ |
86fa2f60 | 625 | for_each_tracing_cpu(i) |
3928a8a2 | 626 | tracing_reset(tr, i); |
86fa2f60 | 627 | |
60a11774 SR |
628 | printk(KERN_CONT "PASSED\n"); |
629 | } | |
630 | #endif | |
631 | ||
bc0c38d1 SR |
632 | type->next = trace_types; |
633 | trace_types = type; | |
634 | len = strlen(type->name); | |
635 | if (len > max_tracer_type_len) | |
636 | max_tracer_type_len = len; | |
60a11774 | 637 | |
bc0c38d1 | 638 | out: |
8e1b82e0 | 639 | tracing_selftest_running = false; |
bc0c38d1 | 640 | mutex_unlock(&trace_types_lock); |
86fa2f60 | 641 | lock_kernel(); |
bc0c38d1 SR |
642 | |
643 | return ret; | |
644 | } | |
645 | ||
646 | void unregister_tracer(struct tracer *type) | |
647 | { | |
648 | struct tracer **t; | |
649 | int len; | |
650 | ||
651 | mutex_lock(&trace_types_lock); | |
652 | for (t = &trace_types; *t; t = &(*t)->next) { | |
653 | if (*t == type) | |
654 | goto found; | |
655 | } | |
656 | pr_info("Trace %s not registered\n", type->name); | |
657 | goto out; | |
658 | ||
659 | found: | |
660 | *t = (*t)->next; | |
661 | if (strlen(type->name) != max_tracer_type_len) | |
662 | goto out; | |
663 | ||
664 | max_tracer_type_len = 0; | |
665 | for (t = &trace_types; *t; t = &(*t)->next) { | |
666 | len = strlen((*t)->name); | |
667 | if (len > max_tracer_type_len) | |
668 | max_tracer_type_len = len; | |
669 | } | |
670 | out: | |
671 | mutex_unlock(&trace_types_lock); | |
672 | } | |
673 | ||
3928a8a2 | 674 | void tracing_reset(struct trace_array *tr, int cpu) |
bc0c38d1 | 675 | { |
d769041f | 676 | ftrace_disable_cpu(); |
3928a8a2 | 677 | ring_buffer_reset_cpu(tr->buffer, cpu); |
d769041f | 678 | ftrace_enable_cpu(); |
bc0c38d1 SR |
679 | } |
680 | ||
213cc060 PE |
681 | void tracing_reset_online_cpus(struct trace_array *tr) |
682 | { | |
683 | int cpu; | |
684 | ||
685 | tr->time_start = ftrace_now(tr->cpu); | |
686 | ||
687 | for_each_online_cpu(cpu) | |
688 | tracing_reset(tr, cpu); | |
689 | } | |
690 | ||
bc0c38d1 SR |
691 | #define SAVED_CMDLINES 128 |
692 | static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1]; | |
693 | static unsigned map_cmdline_to_pid[SAVED_CMDLINES]; | |
694 | static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN]; | |
695 | static int cmdline_idx; | |
696 | static DEFINE_SPINLOCK(trace_cmdline_lock); | |
25b0b44a | 697 | |
25b0b44a SR |
698 | /* temporary disable recording */ |
699 | atomic_t trace_record_cmdline_disabled __read_mostly; | |
bc0c38d1 SR |
700 | |
701 | static void trace_init_cmdlines(void) | |
702 | { | |
703 | memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline)); | |
704 | memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid)); | |
705 | cmdline_idx = 0; | |
706 | } | |
707 | ||
0f048701 SR |
708 | static int trace_stop_count; |
709 | static DEFINE_SPINLOCK(tracing_start_lock); | |
710 | ||
69bb54ec SR |
711 | /** |
712 | * ftrace_off_permanent - disable all ftrace code permanently | |
713 | * | |
714 | * This should only be called when a serious anomally has | |
715 | * been detected. This will turn off the function tracing, | |
716 | * ring buffers, and other tracing utilites. It takes no | |
717 | * locks and can be called from any context. | |
718 | */ | |
719 | void ftrace_off_permanent(void) | |
720 | { | |
721 | tracing_disabled = 1; | |
722 | ftrace_stop(); | |
723 | tracing_off_permanent(); | |
724 | } | |
725 | ||
0f048701 SR |
726 | /** |
727 | * tracing_start - quick start of the tracer | |
728 | * | |
729 | * If tracing is enabled but was stopped by tracing_stop, | |
730 | * this will start the tracer back up. | |
731 | */ | |
732 | void tracing_start(void) | |
733 | { | |
734 | struct ring_buffer *buffer; | |
735 | unsigned long flags; | |
736 | ||
737 | if (tracing_disabled) | |
738 | return; | |
739 | ||
740 | spin_lock_irqsave(&tracing_start_lock, flags); | |
741 | if (--trace_stop_count) | |
742 | goto out; | |
743 | ||
744 | if (trace_stop_count < 0) { | |
745 | /* Someone screwed up their debugging */ | |
746 | WARN_ON_ONCE(1); | |
747 | trace_stop_count = 0; | |
748 | goto out; | |
749 | } | |
750 | ||
751 | ||
752 | buffer = global_trace.buffer; | |
753 | if (buffer) | |
754 | ring_buffer_record_enable(buffer); | |
755 | ||
756 | buffer = max_tr.buffer; | |
757 | if (buffer) | |
758 | ring_buffer_record_enable(buffer); | |
759 | ||
760 | ftrace_start(); | |
761 | out: | |
762 | spin_unlock_irqrestore(&tracing_start_lock, flags); | |
763 | } | |
764 | ||
765 | /** | |
766 | * tracing_stop - quick stop of the tracer | |
767 | * | |
768 | * Light weight way to stop tracing. Use in conjunction with | |
769 | * tracing_start. | |
770 | */ | |
771 | void tracing_stop(void) | |
772 | { | |
773 | struct ring_buffer *buffer; | |
774 | unsigned long flags; | |
775 | ||
776 | ftrace_stop(); | |
777 | spin_lock_irqsave(&tracing_start_lock, flags); | |
778 | if (trace_stop_count++) | |
779 | goto out; | |
780 | ||
781 | buffer = global_trace.buffer; | |
782 | if (buffer) | |
783 | ring_buffer_record_disable(buffer); | |
784 | ||
785 | buffer = max_tr.buffer; | |
786 | if (buffer) | |
787 | ring_buffer_record_disable(buffer); | |
788 | ||
789 | out: | |
790 | spin_unlock_irqrestore(&tracing_start_lock, flags); | |
791 | } | |
792 | ||
e309b41d | 793 | void trace_stop_cmdline_recording(void); |
bc0c38d1 | 794 | |
e309b41d | 795 | static void trace_save_cmdline(struct task_struct *tsk) |
bc0c38d1 SR |
796 | { |
797 | unsigned map; | |
798 | unsigned idx; | |
799 | ||
800 | if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT)) | |
801 | return; | |
802 | ||
803 | /* | |
804 | * It's not the end of the world if we don't get | |
805 | * the lock, but we also don't want to spin | |
806 | * nor do we want to disable interrupts, | |
807 | * so if we miss here, then better luck next time. | |
808 | */ | |
809 | if (!spin_trylock(&trace_cmdline_lock)) | |
810 | return; | |
811 | ||
812 | idx = map_pid_to_cmdline[tsk->pid]; | |
813 | if (idx >= SAVED_CMDLINES) { | |
814 | idx = (cmdline_idx + 1) % SAVED_CMDLINES; | |
815 | ||
816 | map = map_cmdline_to_pid[idx]; | |
817 | if (map <= PID_MAX_DEFAULT) | |
818 | map_pid_to_cmdline[map] = (unsigned)-1; | |
819 | ||
820 | map_pid_to_cmdline[tsk->pid] = idx; | |
821 | ||
822 | cmdline_idx = idx; | |
823 | } | |
824 | ||
825 | memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN); | |
826 | ||
827 | spin_unlock(&trace_cmdline_lock); | |
828 | } | |
829 | ||
660c7f9b | 830 | char *trace_find_cmdline(int pid) |
bc0c38d1 SR |
831 | { |
832 | char *cmdline = "<...>"; | |
833 | unsigned map; | |
834 | ||
835 | if (!pid) | |
836 | return "<idle>"; | |
837 | ||
838 | if (pid > PID_MAX_DEFAULT) | |
839 | goto out; | |
840 | ||
841 | map = map_pid_to_cmdline[pid]; | |
842 | if (map >= SAVED_CMDLINES) | |
843 | goto out; | |
844 | ||
845 | cmdline = saved_cmdlines[map]; | |
846 | ||
847 | out: | |
848 | return cmdline; | |
849 | } | |
850 | ||
e309b41d | 851 | void tracing_record_cmdline(struct task_struct *tsk) |
bc0c38d1 SR |
852 | { |
853 | if (atomic_read(&trace_record_cmdline_disabled)) | |
854 | return; | |
855 | ||
856 | trace_save_cmdline(tsk); | |
857 | } | |
858 | ||
45dcd8b8 | 859 | void |
38697053 SR |
860 | tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags, |
861 | int pc) | |
bc0c38d1 SR |
862 | { |
863 | struct task_struct *tsk = current; | |
bc0c38d1 | 864 | |
777e208d SR |
865 | entry->preempt_count = pc & 0xff; |
866 | entry->pid = (tsk) ? tsk->pid : 0; | |
b54d3de9 | 867 | entry->tgid = (tsk) ? tsk->tgid : 0; |
777e208d | 868 | entry->flags = |
9244489a | 869 | #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT |
2e2ca155 | 870 | (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) | |
9244489a SR |
871 | #else |
872 | TRACE_FLAG_IRQS_NOSUPPORT | | |
873 | #endif | |
bc0c38d1 SR |
874 | ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) | |
875 | ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) | | |
876 | (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0); | |
877 | } | |
878 | ||
e309b41d | 879 | void |
6fb44b71 | 880 | trace_function(struct trace_array *tr, struct trace_array_cpu *data, |
38697053 SR |
881 | unsigned long ip, unsigned long parent_ip, unsigned long flags, |
882 | int pc) | |
bc0c38d1 | 883 | { |
3928a8a2 | 884 | struct ring_buffer_event *event; |
777e208d | 885 | struct ftrace_entry *entry; |
dcb6308f | 886 | unsigned long irq_flags; |
bc0c38d1 | 887 | |
d769041f SR |
888 | /* If we are reading the ring buffer, don't trace */ |
889 | if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled)))) | |
890 | return; | |
891 | ||
3928a8a2 SR |
892 | event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), |
893 | &irq_flags); | |
894 | if (!event) | |
895 | return; | |
896 | entry = ring_buffer_event_data(event); | |
38697053 | 897 | tracing_generic_entry_update(&entry->ent, flags, pc); |
777e208d SR |
898 | entry->ent.type = TRACE_FN; |
899 | entry->ip = ip; | |
900 | entry->parent_ip = parent_ip; | |
3928a8a2 | 901 | ring_buffer_unlock_commit(tr->buffer, event, irq_flags); |
bc0c38d1 SR |
902 | } |
903 | ||
fb52607a | 904 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
287b6e68 FW |
905 | static void __trace_graph_entry(struct trace_array *tr, |
906 | struct trace_array_cpu *data, | |
907 | struct ftrace_graph_ent *trace, | |
908 | unsigned long flags, | |
909 | int pc) | |
910 | { | |
911 | struct ring_buffer_event *event; | |
912 | struct ftrace_graph_ent_entry *entry; | |
913 | unsigned long irq_flags; | |
914 | ||
915 | if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled)))) | |
916 | return; | |
917 | ||
918 | event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry), | |
919 | &irq_flags); | |
920 | if (!event) | |
921 | return; | |
922 | entry = ring_buffer_event_data(event); | |
923 | tracing_generic_entry_update(&entry->ent, flags, pc); | |
924 | entry->ent.type = TRACE_GRAPH_ENT; | |
925 | entry->graph_ent = *trace; | |
926 | ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags); | |
927 | } | |
928 | ||
929 | static void __trace_graph_return(struct trace_array *tr, | |
15e6cb36 | 930 | struct trace_array_cpu *data, |
fb52607a | 931 | struct ftrace_graph_ret *trace, |
15e6cb36 FW |
932 | unsigned long flags, |
933 | int pc) | |
934 | { | |
935 | struct ring_buffer_event *event; | |
287b6e68 | 936 | struct ftrace_graph_ret_entry *entry; |
15e6cb36 FW |
937 | unsigned long irq_flags; |
938 | ||
939 | if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled)))) | |
940 | return; | |
941 | ||
942 | event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry), | |
943 | &irq_flags); | |
944 | if (!event) | |
945 | return; | |
946 | entry = ring_buffer_event_data(event); | |
947 | tracing_generic_entry_update(&entry->ent, flags, pc); | |
287b6e68 FW |
948 | entry->ent.type = TRACE_GRAPH_RET; |
949 | entry->ret = *trace; | |
15e6cb36 FW |
950 | ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags); |
951 | } | |
952 | #endif | |
953 | ||
e309b41d | 954 | void |
2e0f5761 | 955 | ftrace(struct trace_array *tr, struct trace_array_cpu *data, |
38697053 SR |
956 | unsigned long ip, unsigned long parent_ip, unsigned long flags, |
957 | int pc) | |
2e0f5761 IM |
958 | { |
959 | if (likely(!atomic_read(&data->disabled))) | |
38697053 | 960 | trace_function(tr, data, ip, parent_ip, flags, pc); |
2e0f5761 IM |
961 | } |
962 | ||
38697053 SR |
963 | static void ftrace_trace_stack(struct trace_array *tr, |
964 | struct trace_array_cpu *data, | |
965 | unsigned long flags, | |
966 | int skip, int pc) | |
86387f7e | 967 | { |
c2c80529 | 968 | #ifdef CONFIG_STACKTRACE |
3928a8a2 | 969 | struct ring_buffer_event *event; |
777e208d | 970 | struct stack_entry *entry; |
86387f7e | 971 | struct stack_trace trace; |
3928a8a2 | 972 | unsigned long irq_flags; |
86387f7e IM |
973 | |
974 | if (!(trace_flags & TRACE_ITER_STACKTRACE)) | |
975 | return; | |
976 | ||
3928a8a2 SR |
977 | event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), |
978 | &irq_flags); | |
979 | if (!event) | |
980 | return; | |
981 | entry = ring_buffer_event_data(event); | |
38697053 | 982 | tracing_generic_entry_update(&entry->ent, flags, pc); |
777e208d | 983 | entry->ent.type = TRACE_STACK; |
86387f7e | 984 | |
777e208d | 985 | memset(&entry->caller, 0, sizeof(entry->caller)); |
86387f7e IM |
986 | |
987 | trace.nr_entries = 0; | |
988 | trace.max_entries = FTRACE_STACK_ENTRIES; | |
989 | trace.skip = skip; | |
777e208d | 990 | trace.entries = entry->caller; |
86387f7e IM |
991 | |
992 | save_stack_trace(&trace); | |
3928a8a2 | 993 | ring_buffer_unlock_commit(tr->buffer, event, irq_flags); |
c2c80529 | 994 | #endif |
f0a920d5 IM |
995 | } |
996 | ||
38697053 SR |
997 | void __trace_stack(struct trace_array *tr, |
998 | struct trace_array_cpu *data, | |
999 | unsigned long flags, | |
1000 | int skip) | |
1001 | { | |
1002 | ftrace_trace_stack(tr, data, flags, skip, preempt_count()); | |
1003 | } | |
1004 | ||
02b67518 TE |
1005 | static void ftrace_trace_userstack(struct trace_array *tr, |
1006 | struct trace_array_cpu *data, | |
1007 | unsigned long flags, int pc) | |
1008 | { | |
c7425acb | 1009 | #ifdef CONFIG_STACKTRACE |
8d7c6a96 | 1010 | struct ring_buffer_event *event; |
02b67518 TE |
1011 | struct userstack_entry *entry; |
1012 | struct stack_trace trace; | |
02b67518 TE |
1013 | unsigned long irq_flags; |
1014 | ||
1015 | if (!(trace_flags & TRACE_ITER_USERSTACKTRACE)) | |
1016 | return; | |
1017 | ||
1018 | event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), | |
1019 | &irq_flags); | |
1020 | if (!event) | |
1021 | return; | |
1022 | entry = ring_buffer_event_data(event); | |
1023 | tracing_generic_entry_update(&entry->ent, flags, pc); | |
1024 | entry->ent.type = TRACE_USER_STACK; | |
1025 | ||
1026 | memset(&entry->caller, 0, sizeof(entry->caller)); | |
1027 | ||
1028 | trace.nr_entries = 0; | |
1029 | trace.max_entries = FTRACE_STACK_ENTRIES; | |
1030 | trace.skip = 0; | |
1031 | trace.entries = entry->caller; | |
1032 | ||
1033 | save_stack_trace_user(&trace); | |
1034 | ring_buffer_unlock_commit(tr->buffer, event, irq_flags); | |
c7425acb | 1035 | #endif |
02b67518 TE |
1036 | } |
1037 | ||
1038 | void __trace_userstack(struct trace_array *tr, | |
1039 | struct trace_array_cpu *data, | |
1040 | unsigned long flags) | |
1041 | { | |
1042 | ftrace_trace_userstack(tr, data, flags, preempt_count()); | |
1043 | } | |
1044 | ||
38697053 SR |
1045 | static void |
1046 | ftrace_trace_special(void *__tr, void *__data, | |
1047 | unsigned long arg1, unsigned long arg2, unsigned long arg3, | |
1048 | int pc) | |
a4feb834 | 1049 | { |
3928a8a2 | 1050 | struct ring_buffer_event *event; |
a4feb834 IM |
1051 | struct trace_array_cpu *data = __data; |
1052 | struct trace_array *tr = __tr; | |
777e208d | 1053 | struct special_entry *entry; |
a4feb834 IM |
1054 | unsigned long irq_flags; |
1055 | ||
3928a8a2 SR |
1056 | event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), |
1057 | &irq_flags); | |
1058 | if (!event) | |
1059 | return; | |
1060 | entry = ring_buffer_event_data(event); | |
38697053 | 1061 | tracing_generic_entry_update(&entry->ent, 0, pc); |
777e208d SR |
1062 | entry->ent.type = TRACE_SPECIAL; |
1063 | entry->arg1 = arg1; | |
1064 | entry->arg2 = arg2; | |
1065 | entry->arg3 = arg3; | |
3928a8a2 | 1066 | ring_buffer_unlock_commit(tr->buffer, event, irq_flags); |
38697053 | 1067 | ftrace_trace_stack(tr, data, irq_flags, 4, pc); |
02b67518 | 1068 | ftrace_trace_userstack(tr, data, irq_flags, pc); |
a4feb834 IM |
1069 | |
1070 | trace_wake_up(); | |
1071 | } | |
1072 | ||
38697053 SR |
1073 | void |
1074 | __trace_special(void *__tr, void *__data, | |
1075 | unsigned long arg1, unsigned long arg2, unsigned long arg3) | |
1076 | { | |
1077 | ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count()); | |
1078 | } | |
1079 | ||
e309b41d | 1080 | void |
bc0c38d1 SR |
1081 | tracing_sched_switch_trace(struct trace_array *tr, |
1082 | struct trace_array_cpu *data, | |
86387f7e IM |
1083 | struct task_struct *prev, |
1084 | struct task_struct *next, | |
38697053 | 1085 | unsigned long flags, int pc) |
bc0c38d1 | 1086 | { |
3928a8a2 | 1087 | struct ring_buffer_event *event; |
777e208d | 1088 | struct ctx_switch_entry *entry; |
dcb6308f | 1089 | unsigned long irq_flags; |
bc0c38d1 | 1090 | |
3928a8a2 SR |
1091 | event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), |
1092 | &irq_flags); | |
1093 | if (!event) | |
1094 | return; | |
1095 | entry = ring_buffer_event_data(event); | |
38697053 | 1096 | tracing_generic_entry_update(&entry->ent, flags, pc); |
777e208d SR |
1097 | entry->ent.type = TRACE_CTX; |
1098 | entry->prev_pid = prev->pid; | |
1099 | entry->prev_prio = prev->prio; | |
1100 | entry->prev_state = prev->state; | |
1101 | entry->next_pid = next->pid; | |
1102 | entry->next_prio = next->prio; | |
1103 | entry->next_state = next->state; | |
1104 | entry->next_cpu = task_cpu(next); | |
3928a8a2 | 1105 | ring_buffer_unlock_commit(tr->buffer, event, irq_flags); |
38697053 | 1106 | ftrace_trace_stack(tr, data, flags, 5, pc); |
02b67518 | 1107 | ftrace_trace_userstack(tr, data, flags, pc); |
bc0c38d1 SR |
1108 | } |
1109 | ||
57422797 IM |
1110 | void |
1111 | tracing_sched_wakeup_trace(struct trace_array *tr, | |
1112 | struct trace_array_cpu *data, | |
86387f7e IM |
1113 | struct task_struct *wakee, |
1114 | struct task_struct *curr, | |
38697053 | 1115 | unsigned long flags, int pc) |
57422797 | 1116 | { |
3928a8a2 | 1117 | struct ring_buffer_event *event; |
777e208d | 1118 | struct ctx_switch_entry *entry; |
57422797 IM |
1119 | unsigned long irq_flags; |
1120 | ||
3928a8a2 SR |
1121 | event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), |
1122 | &irq_flags); | |
1123 | if (!event) | |
1124 | return; | |
1125 | entry = ring_buffer_event_data(event); | |
38697053 | 1126 | tracing_generic_entry_update(&entry->ent, flags, pc); |
777e208d SR |
1127 | entry->ent.type = TRACE_WAKE; |
1128 | entry->prev_pid = curr->pid; | |
1129 | entry->prev_prio = curr->prio; | |
1130 | entry->prev_state = curr->state; | |
1131 | entry->next_pid = wakee->pid; | |
1132 | entry->next_prio = wakee->prio; | |
1133 | entry->next_state = wakee->state; | |
1134 | entry->next_cpu = task_cpu(wakee); | |
3928a8a2 | 1135 | ring_buffer_unlock_commit(tr->buffer, event, irq_flags); |
38697053 | 1136 | ftrace_trace_stack(tr, data, flags, 6, pc); |
02b67518 | 1137 | ftrace_trace_userstack(tr, data, flags, pc); |
017730c1 IM |
1138 | |
1139 | trace_wake_up(); | |
57422797 IM |
1140 | } |
1141 | ||
4902f884 SR |
1142 | void |
1143 | ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) | |
1144 | { | |
1145 | struct trace_array *tr = &global_trace; | |
1146 | struct trace_array_cpu *data; | |
5aa1ba6a | 1147 | unsigned long flags; |
4902f884 | 1148 | int cpu; |
38697053 | 1149 | int pc; |
4902f884 | 1150 | |
c76f0694 | 1151 | if (tracing_disabled) |
4902f884 SR |
1152 | return; |
1153 | ||
38697053 | 1154 | pc = preempt_count(); |
5aa1ba6a | 1155 | local_irq_save(flags); |
4902f884 SR |
1156 | cpu = raw_smp_processor_id(); |
1157 | data = tr->data[cpu]; | |
4902f884 | 1158 | |
5aa1ba6a | 1159 | if (likely(atomic_inc_return(&data->disabled) == 1)) |
38697053 | 1160 | ftrace_trace_special(tr, data, arg1, arg2, arg3, pc); |
4902f884 | 1161 | |
5aa1ba6a SR |
1162 | atomic_dec(&data->disabled); |
1163 | local_irq_restore(flags); | |
4902f884 SR |
1164 | } |
1165 | ||
606576ce | 1166 | #ifdef CONFIG_FUNCTION_TRACER |
e309b41d | 1167 | static void |
b2a866f9 | 1168 | function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip) |
2e0f5761 IM |
1169 | { |
1170 | struct trace_array *tr = &global_trace; | |
1171 | struct trace_array_cpu *data; | |
1172 | unsigned long flags; | |
1173 | long disabled; | |
38697053 SR |
1174 | int cpu, resched; |
1175 | int pc; | |
2e0f5761 | 1176 | |
60bc0800 | 1177 | if (unlikely(!ftrace_function_enabled)) |
2e0f5761 IM |
1178 | return; |
1179 | ||
38697053 | 1180 | pc = preempt_count(); |
182e9f5f | 1181 | resched = ftrace_preempt_disable(); |
38697053 | 1182 | local_save_flags(flags); |
2e0f5761 IM |
1183 | cpu = raw_smp_processor_id(); |
1184 | data = tr->data[cpu]; | |
1185 | disabled = atomic_inc_return(&data->disabled); | |
1186 | ||
1187 | if (likely(disabled == 1)) | |
38697053 | 1188 | trace_function(tr, data, ip, parent_ip, flags, pc); |
2e0f5761 IM |
1189 | |
1190 | atomic_dec(&data->disabled); | |
182e9f5f | 1191 | ftrace_preempt_enable(resched); |
2e0f5761 IM |
1192 | } |
1193 | ||
b2a866f9 SR |
1194 | static void |
1195 | function_trace_call(unsigned long ip, unsigned long parent_ip) | |
1196 | { | |
1197 | struct trace_array *tr = &global_trace; | |
1198 | struct trace_array_cpu *data; | |
1199 | unsigned long flags; | |
1200 | long disabled; | |
1201 | int cpu; | |
1202 | int pc; | |
1203 | ||
1204 | if (unlikely(!ftrace_function_enabled)) | |
1205 | return; | |
1206 | ||
1207 | /* | |
1208 | * Need to use raw, since this must be called before the | |
1209 | * recursive protection is performed. | |
1210 | */ | |
d51ad7ac | 1211 | local_irq_save(flags); |
b2a866f9 SR |
1212 | cpu = raw_smp_processor_id(); |
1213 | data = tr->data[cpu]; | |
1214 | disabled = atomic_inc_return(&data->disabled); | |
1215 | ||
1216 | if (likely(disabled == 1)) { | |
1217 | pc = preempt_count(); | |
1218 | trace_function(tr, data, ip, parent_ip, flags, pc); | |
1219 | } | |
1220 | ||
1221 | atomic_dec(&data->disabled); | |
d51ad7ac | 1222 | local_irq_restore(flags); |
b2a866f9 SR |
1223 | } |
1224 | ||
fb52607a | 1225 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
e49dc19c | 1226 | int trace_graph_entry(struct ftrace_graph_ent *trace) |
15e6cb36 FW |
1227 | { |
1228 | struct trace_array *tr = &global_trace; | |
1229 | struct trace_array_cpu *data; | |
1230 | unsigned long flags; | |
1231 | long disabled; | |
1232 | int cpu; | |
1233 | int pc; | |
1234 | ||
804a6851 SR |
1235 | if (!ftrace_trace_task(current)) |
1236 | return 0; | |
1237 | ||
ea4e2bc4 SR |
1238 | if (!ftrace_graph_addr(trace->func)) |
1239 | return 0; | |
1240 | ||
a5e25883 | 1241 | local_irq_save(flags); |
15e6cb36 FW |
1242 | cpu = raw_smp_processor_id(); |
1243 | data = tr->data[cpu]; | |
1244 | disabled = atomic_inc_return(&data->disabled); | |
1245 | if (likely(disabled == 1)) { | |
1246 | pc = preempt_count(); | |
287b6e68 FW |
1247 | __trace_graph_entry(tr, data, trace, flags, pc); |
1248 | } | |
ea4e2bc4 SR |
1249 | /* Only do the atomic if it is not already set */ |
1250 | if (!test_tsk_trace_graph(current)) | |
1251 | set_tsk_trace_graph(current); | |
287b6e68 | 1252 | atomic_dec(&data->disabled); |
a5e25883 | 1253 | local_irq_restore(flags); |
e49dc19c SR |
1254 | |
1255 | return 1; | |
287b6e68 FW |
1256 | } |
1257 | ||
1258 | void trace_graph_return(struct ftrace_graph_ret *trace) | |
1259 | { | |
1260 | struct trace_array *tr = &global_trace; | |
1261 | struct trace_array_cpu *data; | |
1262 | unsigned long flags; | |
1263 | long disabled; | |
1264 | int cpu; | |
1265 | int pc; | |
1266 | ||
a5e25883 | 1267 | local_irq_save(flags); |
287b6e68 FW |
1268 | cpu = raw_smp_processor_id(); |
1269 | data = tr->data[cpu]; | |
1270 | disabled = atomic_inc_return(&data->disabled); | |
1271 | if (likely(disabled == 1)) { | |
1272 | pc = preempt_count(); | |
1273 | __trace_graph_return(tr, data, trace, flags, pc); | |
15e6cb36 | 1274 | } |
ea4e2bc4 SR |
1275 | if (!trace->depth) |
1276 | clear_tsk_trace_graph(current); | |
15e6cb36 | 1277 | atomic_dec(&data->disabled); |
a5e25883 | 1278 | local_irq_restore(flags); |
15e6cb36 | 1279 | } |
fb52607a | 1280 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
15e6cb36 | 1281 | |
2e0f5761 IM |
1282 | static struct ftrace_ops trace_ops __read_mostly = |
1283 | { | |
1284 | .func = function_trace_call, | |
1285 | }; | |
1286 | ||
e309b41d | 1287 | void tracing_start_function_trace(void) |
2e0f5761 | 1288 | { |
60bc0800 | 1289 | ftrace_function_enabled = 0; |
b2a866f9 SR |
1290 | |
1291 | if (trace_flags & TRACE_ITER_PREEMPTONLY) | |
1292 | trace_ops.func = function_trace_call_preempt_only; | |
1293 | else | |
1294 | trace_ops.func = function_trace_call; | |
1295 | ||
2e0f5761 | 1296 | register_ftrace_function(&trace_ops); |
9036990d | 1297 | ftrace_function_enabled = 1; |
2e0f5761 IM |
1298 | } |
1299 | ||
e309b41d | 1300 | void tracing_stop_function_trace(void) |
2e0f5761 | 1301 | { |
60bc0800 | 1302 | ftrace_function_enabled = 0; |
2e0f5761 IM |
1303 | unregister_ftrace_function(&trace_ops); |
1304 | } | |
1305 | #endif | |
1306 | ||
bc0c38d1 SR |
1307 | enum trace_file_type { |
1308 | TRACE_FILE_LAT_FMT = 1, | |
12ef7d44 | 1309 | TRACE_FILE_ANNOTATE = 2, |
bc0c38d1 SR |
1310 | }; |
1311 | ||
e2ac8ef5 | 1312 | static void trace_iterator_increment(struct trace_iterator *iter) |
5a90f577 | 1313 | { |
d769041f SR |
1314 | /* Don't allow ftrace to trace into the ring buffers */ |
1315 | ftrace_disable_cpu(); | |
1316 | ||
5a90f577 | 1317 | iter->idx++; |
d769041f SR |
1318 | if (iter->buffer_iter[iter->cpu]) |
1319 | ring_buffer_read(iter->buffer_iter[iter->cpu], NULL); | |
1320 | ||
1321 | ftrace_enable_cpu(); | |
5a90f577 SR |
1322 | } |
1323 | ||
e309b41d | 1324 | static struct trace_entry * |
3928a8a2 | 1325 | peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts) |
dd0e545f | 1326 | { |
3928a8a2 SR |
1327 | struct ring_buffer_event *event; |
1328 | struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu]; | |
dd0e545f | 1329 | |
d769041f SR |
1330 | /* Don't allow ftrace to trace into the ring buffers */ |
1331 | ftrace_disable_cpu(); | |
1332 | ||
1333 | if (buf_iter) | |
1334 | event = ring_buffer_iter_peek(buf_iter, ts); | |
1335 | else | |
1336 | event = ring_buffer_peek(iter->tr->buffer, cpu, ts); | |
1337 | ||
1338 | ftrace_enable_cpu(); | |
1339 | ||
3928a8a2 | 1340 | return event ? ring_buffer_event_data(event) : NULL; |
dd0e545f | 1341 | } |
d769041f | 1342 | |
dd0e545f | 1343 | static struct trace_entry * |
3928a8a2 | 1344 | __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts) |
bc0c38d1 | 1345 | { |
3928a8a2 | 1346 | struct ring_buffer *buffer = iter->tr->buffer; |
bc0c38d1 | 1347 | struct trace_entry *ent, *next = NULL; |
3928a8a2 | 1348 | u64 next_ts = 0, ts; |
bc0c38d1 SR |
1349 | int next_cpu = -1; |
1350 | int cpu; | |
1351 | ||
ab46428c | 1352 | for_each_tracing_cpu(cpu) { |
dd0e545f | 1353 | |
3928a8a2 SR |
1354 | if (ring_buffer_empty_cpu(buffer, cpu)) |
1355 | continue; | |
dd0e545f | 1356 | |
3928a8a2 | 1357 | ent = peek_next_entry(iter, cpu, &ts); |
dd0e545f | 1358 | |
cdd31cd2 IM |
1359 | /* |
1360 | * Pick the entry with the smallest timestamp: | |
1361 | */ | |
3928a8a2 | 1362 | if (ent && (!next || ts < next_ts)) { |
bc0c38d1 SR |
1363 | next = ent; |
1364 | next_cpu = cpu; | |
3928a8a2 | 1365 | next_ts = ts; |
bc0c38d1 SR |
1366 | } |
1367 | } | |
1368 | ||
1369 | if (ent_cpu) | |
1370 | *ent_cpu = next_cpu; | |
1371 | ||
3928a8a2 SR |
1372 | if (ent_ts) |
1373 | *ent_ts = next_ts; | |
1374 | ||
bc0c38d1 SR |
1375 | return next; |
1376 | } | |
1377 | ||
dd0e545f SR |
1378 | /* Find the next real entry, without updating the iterator itself */ |
1379 | static struct trace_entry * | |
3928a8a2 | 1380 | find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts) |
bc0c38d1 | 1381 | { |
3928a8a2 | 1382 | return __find_next_entry(iter, ent_cpu, ent_ts); |
dd0e545f SR |
1383 | } |
1384 | ||
1385 | /* Find the next real entry, and increment the iterator to the next entry */ | |
1386 | static void *find_next_entry_inc(struct trace_iterator *iter) | |
1387 | { | |
3928a8a2 | 1388 | iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts); |
dd0e545f | 1389 | |
3928a8a2 | 1390 | if (iter->ent) |
e2ac8ef5 | 1391 | trace_iterator_increment(iter); |
dd0e545f | 1392 | |
3928a8a2 | 1393 | return iter->ent ? iter : NULL; |
b3806b43 | 1394 | } |
bc0c38d1 | 1395 | |
e309b41d | 1396 | static void trace_consume(struct trace_iterator *iter) |
b3806b43 | 1397 | { |
d769041f SR |
1398 | /* Don't allow ftrace to trace into the ring buffers */ |
1399 | ftrace_disable_cpu(); | |
3928a8a2 | 1400 | ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts); |
d769041f | 1401 | ftrace_enable_cpu(); |
bc0c38d1 SR |
1402 | } |
1403 | ||
e309b41d | 1404 | static void *s_next(struct seq_file *m, void *v, loff_t *pos) |
bc0c38d1 SR |
1405 | { |
1406 | struct trace_iterator *iter = m->private; | |
bc0c38d1 | 1407 | int i = (int)*pos; |
4e3c3333 | 1408 | void *ent; |
bc0c38d1 SR |
1409 | |
1410 | (*pos)++; | |
1411 | ||
1412 | /* can't go backwards */ | |
1413 | if (iter->idx > i) | |
1414 | return NULL; | |
1415 | ||
1416 | if (iter->idx < 0) | |
1417 | ent = find_next_entry_inc(iter); | |
1418 | else | |
1419 | ent = iter; | |
1420 | ||
1421 | while (ent && iter->idx < i) | |
1422 | ent = find_next_entry_inc(iter); | |
1423 | ||
1424 | iter->pos = *pos; | |
1425 | ||
bc0c38d1 SR |
1426 | return ent; |
1427 | } | |
1428 | ||
1429 | static void *s_start(struct seq_file *m, loff_t *pos) | |
1430 | { | |
1431 | struct trace_iterator *iter = m->private; | |
1432 | void *p = NULL; | |
1433 | loff_t l = 0; | |
3928a8a2 | 1434 | int cpu; |
bc0c38d1 SR |
1435 | |
1436 | mutex_lock(&trace_types_lock); | |
1437 | ||
d15f57f2 SR |
1438 | if (!current_trace || current_trace != iter->trace) { |
1439 | mutex_unlock(&trace_types_lock); | |
bc0c38d1 | 1440 | return NULL; |
d15f57f2 | 1441 | } |
bc0c38d1 SR |
1442 | |
1443 | atomic_inc(&trace_record_cmdline_disabled); | |
1444 | ||
bc0c38d1 SR |
1445 | if (*pos != iter->pos) { |
1446 | iter->ent = NULL; | |
1447 | iter->cpu = 0; | |
1448 | iter->idx = -1; | |
1449 | ||
d769041f SR |
1450 | ftrace_disable_cpu(); |
1451 | ||
3928a8a2 SR |
1452 | for_each_tracing_cpu(cpu) { |
1453 | ring_buffer_iter_reset(iter->buffer_iter[cpu]); | |
4c11d7ae | 1454 | } |
bc0c38d1 | 1455 | |
d769041f SR |
1456 | ftrace_enable_cpu(); |
1457 | ||
bc0c38d1 SR |
1458 | for (p = iter; p && l < *pos; p = s_next(m, p, &l)) |
1459 | ; | |
1460 | ||
1461 | } else { | |
4c11d7ae | 1462 | l = *pos - 1; |
bc0c38d1 SR |
1463 | p = s_next(m, p, &l); |
1464 | } | |
1465 | ||
1466 | return p; | |
1467 | } | |
1468 | ||
1469 | static void s_stop(struct seq_file *m, void *p) | |
1470 | { | |
bc0c38d1 | 1471 | atomic_dec(&trace_record_cmdline_disabled); |
bc0c38d1 SR |
1472 | mutex_unlock(&trace_types_lock); |
1473 | } | |
1474 | ||
76094a2c | 1475 | #ifdef CONFIG_KRETPROBES |
b3aa5577 | 1476 | static inline const char *kretprobed(const char *name) |
76094a2c | 1477 | { |
b3aa5577 SR |
1478 | static const char tramp_name[] = "kretprobe_trampoline"; |
1479 | int size = sizeof(tramp_name); | |
1480 | ||
1481 | if (strncmp(tramp_name, name, size) == 0) | |
1482 | return "[unknown/kretprobe'd]"; | |
1483 | return name; | |
76094a2c AS |
1484 | } |
1485 | #else | |
b3aa5577 | 1486 | static inline const char *kretprobed(const char *name) |
76094a2c | 1487 | { |
b3aa5577 | 1488 | return name; |
76094a2c AS |
1489 | } |
1490 | #endif /* CONFIG_KRETPROBES */ | |
1491 | ||
b3806b43 | 1492 | static int |
214023c3 | 1493 | seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address) |
bc0c38d1 SR |
1494 | { |
1495 | #ifdef CONFIG_KALLSYMS | |
1496 | char str[KSYM_SYMBOL_LEN]; | |
b3aa5577 | 1497 | const char *name; |
bc0c38d1 SR |
1498 | |
1499 | kallsyms_lookup(address, NULL, NULL, NULL, str); | |
1500 | ||
b3aa5577 SR |
1501 | name = kretprobed(str); |
1502 | ||
1503 | return trace_seq_printf(s, fmt, name); | |
bc0c38d1 | 1504 | #endif |
b3806b43 | 1505 | return 1; |
bc0c38d1 SR |
1506 | } |
1507 | ||
b3806b43 | 1508 | static int |
214023c3 SR |
1509 | seq_print_sym_offset(struct trace_seq *s, const char *fmt, |
1510 | unsigned long address) | |
bc0c38d1 SR |
1511 | { |
1512 | #ifdef CONFIG_KALLSYMS | |
1513 | char str[KSYM_SYMBOL_LEN]; | |
b3aa5577 | 1514 | const char *name; |
bc0c38d1 SR |
1515 | |
1516 | sprint_symbol(str, address); | |
b3aa5577 SR |
1517 | name = kretprobed(str); |
1518 | ||
1519 | return trace_seq_printf(s, fmt, name); | |
bc0c38d1 | 1520 | #endif |
b3806b43 | 1521 | return 1; |
bc0c38d1 SR |
1522 | } |
1523 | ||
1524 | #ifndef CONFIG_64BIT | |
1525 | # define IP_FMT "%08lx" | |
1526 | #else | |
1527 | # define IP_FMT "%016lx" | |
1528 | #endif | |
1529 | ||
15e6cb36 | 1530 | int |
214023c3 | 1531 | seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags) |
bc0c38d1 | 1532 | { |
b3806b43 SR |
1533 | int ret; |
1534 | ||
1535 | if (!ip) | |
1536 | return trace_seq_printf(s, "0"); | |
bc0c38d1 SR |
1537 | |
1538 | if (sym_flags & TRACE_ITER_SYM_OFFSET) | |
b3806b43 | 1539 | ret = seq_print_sym_offset(s, "%s", ip); |
bc0c38d1 | 1540 | else |
b3806b43 SR |
1541 | ret = seq_print_sym_short(s, "%s", ip); |
1542 | ||
1543 | if (!ret) | |
1544 | return 0; | |
bc0c38d1 SR |
1545 | |
1546 | if (sym_flags & TRACE_ITER_SYM_ADDR) | |
b3806b43 SR |
1547 | ret = trace_seq_printf(s, " <" IP_FMT ">", ip); |
1548 | return ret; | |
bc0c38d1 SR |
1549 | } |
1550 | ||
b54d3de9 TE |
1551 | static inline int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm, |
1552 | unsigned long ip, unsigned long sym_flags) | |
1553 | { | |
1554 | struct file *file = NULL; | |
1555 | unsigned long vmstart = 0; | |
1556 | int ret = 1; | |
1557 | ||
1558 | if (mm) { | |
cffa10ae TE |
1559 | const struct vm_area_struct *vma; |
1560 | ||
1561 | down_read(&mm->mmap_sem); | |
1562 | vma = find_vma(mm, ip); | |
b54d3de9 TE |
1563 | if (vma) { |
1564 | file = vma->vm_file; | |
1565 | vmstart = vma->vm_start; | |
1566 | } | |
e38da592 TE |
1567 | if (file) { |
1568 | ret = trace_seq_path(s, &file->f_path); | |
1569 | if (ret) | |
1570 | ret = trace_seq_printf(s, "[+0x%lx]", ip - vmstart); | |
1571 | } | |
cffa10ae | 1572 | up_read(&mm->mmap_sem); |
b54d3de9 | 1573 | } |
b54d3de9 TE |
1574 | if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file)) |
1575 | ret = trace_seq_printf(s, " <" IP_FMT ">", ip); | |
1576 | return ret; | |
1577 | } | |
1578 | ||
02b67518 TE |
1579 | static int |
1580 | seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s, | |
b54d3de9 | 1581 | unsigned long sym_flags) |
02b67518 | 1582 | { |
b54d3de9 | 1583 | struct mm_struct *mm = NULL; |
02b67518 | 1584 | int ret = 1; |
8d7c6a96 | 1585 | unsigned int i; |
02b67518 | 1586 | |
b54d3de9 TE |
1587 | if (trace_flags & TRACE_ITER_SYM_USEROBJ) { |
1588 | struct task_struct *task; | |
1589 | /* | |
1590 | * we do the lookup on the thread group leader, | |
1591 | * since individual threads might have already quit! | |
1592 | */ | |
1593 | rcu_read_lock(); | |
1594 | task = find_task_by_vpid(entry->ent.tgid); | |
b54d3de9 TE |
1595 | if (task) |
1596 | mm = get_task_mm(task); | |
cffa10ae | 1597 | rcu_read_unlock(); |
b54d3de9 TE |
1598 | } |
1599 | ||
02b67518 TE |
1600 | for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { |
1601 | unsigned long ip = entry->caller[i]; | |
1602 | ||
1603 | if (ip == ULONG_MAX || !ret) | |
1604 | break; | |
b54d3de9 | 1605 | if (i && ret) |
02b67518 TE |
1606 | ret = trace_seq_puts(s, " <- "); |
1607 | if (!ip) { | |
b54d3de9 TE |
1608 | if (ret) |
1609 | ret = trace_seq_puts(s, "??"); | |
02b67518 TE |
1610 | continue; |
1611 | } | |
b54d3de9 TE |
1612 | if (!ret) |
1613 | break; | |
1614 | if (ret) | |
1615 | ret = seq_print_user_ip(s, mm, ip, sym_flags); | |
02b67518 TE |
1616 | } |
1617 | ||
b54d3de9 TE |
1618 | if (mm) |
1619 | mmput(mm); | |
02b67518 TE |
1620 | return ret; |
1621 | } | |
1622 | ||
e309b41d | 1623 | static void print_lat_help_header(struct seq_file *m) |
bc0c38d1 | 1624 | { |
a6168353 ME |
1625 | seq_puts(m, "# _------=> CPU# \n"); |
1626 | seq_puts(m, "# / _-----=> irqs-off \n"); | |
1627 | seq_puts(m, "# | / _----=> need-resched \n"); | |
1628 | seq_puts(m, "# || / _---=> hardirq/softirq \n"); | |
1629 | seq_puts(m, "# ||| / _--=> preempt-depth \n"); | |
1630 | seq_puts(m, "# |||| / \n"); | |
1631 | seq_puts(m, "# ||||| delay \n"); | |
1632 | seq_puts(m, "# cmd pid ||||| time | caller \n"); | |
1633 | seq_puts(m, "# \\ / ||||| \\ | / \n"); | |
bc0c38d1 SR |
1634 | } |
1635 | ||
e309b41d | 1636 | static void print_func_help_header(struct seq_file *m) |
bc0c38d1 | 1637 | { |
a6168353 ME |
1638 | seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"); |
1639 | seq_puts(m, "# | | | | |\n"); | |
bc0c38d1 SR |
1640 | } |
1641 | ||
1642 | ||
e309b41d | 1643 | static void |
bc0c38d1 SR |
1644 | print_trace_header(struct seq_file *m, struct trace_iterator *iter) |
1645 | { | |
1646 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); | |
1647 | struct trace_array *tr = iter->tr; | |
1648 | struct trace_array_cpu *data = tr->data[tr->cpu]; | |
1649 | struct tracer *type = current_trace; | |
3928a8a2 SR |
1650 | unsigned long total; |
1651 | unsigned long entries; | |
bc0c38d1 SR |
1652 | const char *name = "preemption"; |
1653 | ||
1654 | if (type) | |
1655 | name = type->name; | |
1656 | ||
3928a8a2 SR |
1657 | entries = ring_buffer_entries(iter->tr->buffer); |
1658 | total = entries + | |
1659 | ring_buffer_overruns(iter->tr->buffer); | |
bc0c38d1 SR |
1660 | |
1661 | seq_printf(m, "%s latency trace v1.1.5 on %s\n", | |
1662 | name, UTS_RELEASE); | |
1663 | seq_puts(m, "-----------------------------------" | |
1664 | "---------------------------------\n"); | |
1665 | seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |" | |
1666 | " (M:%s VP:%d, KP:%d, SP:%d HP:%d", | |
57f50be1 | 1667 | nsecs_to_usecs(data->saved_latency), |
bc0c38d1 | 1668 | entries, |
4c11d7ae | 1669 | total, |
bc0c38d1 SR |
1670 | tr->cpu, |
1671 | #if defined(CONFIG_PREEMPT_NONE) | |
1672 | "server", | |
1673 | #elif defined(CONFIG_PREEMPT_VOLUNTARY) | |
1674 | "desktop", | |
b5c21b45 | 1675 | #elif defined(CONFIG_PREEMPT) |
bc0c38d1 SR |
1676 | "preempt", |
1677 | #else | |
1678 | "unknown", | |
1679 | #endif | |
1680 | /* These are reserved for later use */ | |
1681 | 0, 0, 0, 0); | |
1682 | #ifdef CONFIG_SMP | |
1683 | seq_printf(m, " #P:%d)\n", num_online_cpus()); | |
1684 | #else | |
1685 | seq_puts(m, ")\n"); | |
1686 | #endif | |
1687 | seq_puts(m, " -----------------\n"); | |
1688 | seq_printf(m, " | task: %.16s-%d " | |
1689 | "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n", | |
1690 | data->comm, data->pid, data->uid, data->nice, | |
1691 | data->policy, data->rt_priority); | |
1692 | seq_puts(m, " -----------------\n"); | |
1693 | ||
1694 | if (data->critical_start) { | |
1695 | seq_puts(m, " => started at: "); | |
214023c3 SR |
1696 | seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags); |
1697 | trace_print_seq(m, &iter->seq); | |
bc0c38d1 | 1698 | seq_puts(m, "\n => ended at: "); |
214023c3 SR |
1699 | seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags); |
1700 | trace_print_seq(m, &iter->seq); | |
bc0c38d1 SR |
1701 | seq_puts(m, "\n"); |
1702 | } | |
1703 | ||
1704 | seq_puts(m, "\n"); | |
1705 | } | |
1706 | ||
e309b41d | 1707 | static void |
214023c3 | 1708 | lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu) |
bc0c38d1 SR |
1709 | { |
1710 | int hardirq, softirq; | |
1711 | char *comm; | |
1712 | ||
777e208d | 1713 | comm = trace_find_cmdline(entry->pid); |
bc0c38d1 | 1714 | |
777e208d | 1715 | trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid); |
a6168353 | 1716 | trace_seq_printf(s, "%3d", cpu); |
214023c3 | 1717 | trace_seq_printf(s, "%c%c", |
9244489a SR |
1718 | (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : |
1719 | (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.', | |
777e208d | 1720 | ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.')); |
bc0c38d1 | 1721 | |
777e208d SR |
1722 | hardirq = entry->flags & TRACE_FLAG_HARDIRQ; |
1723 | softirq = entry->flags & TRACE_FLAG_SOFTIRQ; | |
afc2abc0 | 1724 | if (hardirq && softirq) { |
214023c3 | 1725 | trace_seq_putc(s, 'H'); |
afc2abc0 IM |
1726 | } else { |
1727 | if (hardirq) { | |
214023c3 | 1728 | trace_seq_putc(s, 'h'); |
afc2abc0 | 1729 | } else { |
bc0c38d1 | 1730 | if (softirq) |
214023c3 | 1731 | trace_seq_putc(s, 's'); |
bc0c38d1 | 1732 | else |
214023c3 | 1733 | trace_seq_putc(s, '.'); |
bc0c38d1 SR |
1734 | } |
1735 | } | |
1736 | ||
777e208d SR |
1737 | if (entry->preempt_count) |
1738 | trace_seq_printf(s, "%x", entry->preempt_count); | |
bc0c38d1 | 1739 | else |
214023c3 | 1740 | trace_seq_puts(s, "."); |
bc0c38d1 SR |
1741 | } |
1742 | ||
1743 | unsigned long preempt_mark_thresh = 100; | |
1744 | ||
e309b41d | 1745 | static void |
3928a8a2 | 1746 | lat_print_timestamp(struct trace_seq *s, u64 abs_usecs, |
bc0c38d1 SR |
1747 | unsigned long rel_usecs) |
1748 | { | |
214023c3 | 1749 | trace_seq_printf(s, " %4lldus", abs_usecs); |
bc0c38d1 | 1750 | if (rel_usecs > preempt_mark_thresh) |
214023c3 | 1751 | trace_seq_puts(s, "!: "); |
bc0c38d1 | 1752 | else if (rel_usecs > 1) |
214023c3 | 1753 | trace_seq_puts(s, "+: "); |
bc0c38d1 | 1754 | else |
214023c3 | 1755 | trace_seq_puts(s, " : "); |
bc0c38d1 SR |
1756 | } |
1757 | ||
1758 | static const char state_to_char[] = TASK_STATE_TO_CHAR_STR; | |
1759 | ||
3d9101e9 TG |
1760 | static int task_state_char(unsigned long state) |
1761 | { | |
1762 | int bit = state ? __ffs(state) + 1 : 0; | |
1763 | ||
1764 | return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?'; | |
1765 | } | |
1766 | ||
fc5e27ae PP |
1767 | /* |
1768 | * The message is supposed to contain an ending newline. | |
1769 | * If the printing stops prematurely, try to add a newline of our own. | |
1770 | */ | |
1771 | void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter) | |
dd0e545f | 1772 | { |
dd0e545f | 1773 | struct trace_entry *ent; |
777e208d | 1774 | struct trace_field_cont *cont; |
fc5e27ae | 1775 | bool ok = true; |
dd0e545f | 1776 | |
3928a8a2 | 1777 | ent = peek_next_entry(iter, iter->cpu, NULL); |
dd0e545f SR |
1778 | if (!ent || ent->type != TRACE_CONT) { |
1779 | trace_seq_putc(s, '\n'); | |
1780 | return; | |
1781 | } | |
1782 | ||
1783 | do { | |
777e208d | 1784 | cont = (struct trace_field_cont *)ent; |
fc5e27ae | 1785 | if (ok) |
777e208d | 1786 | ok = (trace_seq_printf(s, "%s", cont->buf) > 0); |
d769041f SR |
1787 | |
1788 | ftrace_disable_cpu(); | |
1789 | ||
1790 | if (iter->buffer_iter[iter->cpu]) | |
1791 | ring_buffer_read(iter->buffer_iter[iter->cpu], NULL); | |
1792 | else | |
1793 | ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL); | |
1794 | ||
1795 | ftrace_enable_cpu(); | |
1796 | ||
3928a8a2 | 1797 | ent = peek_next_entry(iter, iter->cpu, NULL); |
dd0e545f | 1798 | } while (ent && ent->type == TRACE_CONT); |
fc5e27ae PP |
1799 | |
1800 | if (!ok) | |
1801 | trace_seq_putc(s, '\n'); | |
dd0e545f SR |
1802 | } |
1803 | ||
a309720c SR |
1804 | static void test_cpu_buff_start(struct trace_iterator *iter) |
1805 | { | |
1806 | struct trace_seq *s = &iter->seq; | |
1807 | ||
12ef7d44 SR |
1808 | if (!(trace_flags & TRACE_ITER_ANNOTATE)) |
1809 | return; | |
1810 | ||
1811 | if (!(iter->iter_flags & TRACE_FILE_ANNOTATE)) | |
1812 | return; | |
1813 | ||
a309720c SR |
1814 | if (cpu_isset(iter->cpu, iter->started)) |
1815 | return; | |
1816 | ||
1817 | cpu_set(iter->cpu, iter->started); | |
1818 | trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu); | |
1819 | } | |
1820 | ||
2c4f035f | 1821 | static enum print_line_t |
214023c3 | 1822 | print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu) |
bc0c38d1 | 1823 | { |
214023c3 | 1824 | struct trace_seq *s = &iter->seq; |
bc0c38d1 | 1825 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); |
3928a8a2 | 1826 | struct trace_entry *next_entry; |
bc0c38d1 SR |
1827 | unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE); |
1828 | struct trace_entry *entry = iter->ent; | |
1829 | unsigned long abs_usecs; | |
1830 | unsigned long rel_usecs; | |
3928a8a2 | 1831 | u64 next_ts; |
bc0c38d1 | 1832 | char *comm; |
bac524d3 | 1833 | int S, T; |
86387f7e | 1834 | int i; |
bc0c38d1 | 1835 | |
dd0e545f | 1836 | if (entry->type == TRACE_CONT) |
2c4f035f | 1837 | return TRACE_TYPE_HANDLED; |
dd0e545f | 1838 | |
a309720c SR |
1839 | test_cpu_buff_start(iter); |
1840 | ||
3928a8a2 SR |
1841 | next_entry = find_next_entry(iter, NULL, &next_ts); |
1842 | if (!next_entry) | |
1843 | next_ts = iter->ts; | |
1844 | rel_usecs = ns2usecs(next_ts - iter->ts); | |
1845 | abs_usecs = ns2usecs(iter->ts - iter->tr->time_start); | |
bc0c38d1 SR |
1846 | |
1847 | if (verbose) { | |
777e208d | 1848 | comm = trace_find_cmdline(entry->pid); |
a6168353 | 1849 | trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]" |
214023c3 SR |
1850 | " %ld.%03ldms (+%ld.%03ldms): ", |
1851 | comm, | |
777e208d SR |
1852 | entry->pid, cpu, entry->flags, |
1853 | entry->preempt_count, trace_idx, | |
3928a8a2 | 1854 | ns2usecs(iter->ts), |
214023c3 SR |
1855 | abs_usecs/1000, |
1856 | abs_usecs % 1000, rel_usecs/1000, | |
1857 | rel_usecs % 1000); | |
bc0c38d1 | 1858 | } else { |
f29c73fe IM |
1859 | lat_print_generic(s, entry, cpu); |
1860 | lat_print_timestamp(s, abs_usecs, rel_usecs); | |
bc0c38d1 SR |
1861 | } |
1862 | switch (entry->type) { | |
777e208d | 1863 | case TRACE_FN: { |
7104f300 SR |
1864 | struct ftrace_entry *field; |
1865 | ||
1866 | trace_assign_type(field, entry); | |
777e208d SR |
1867 | |
1868 | seq_print_ip_sym(s, field->ip, sym_flags); | |
214023c3 | 1869 | trace_seq_puts(s, " ("); |
b3aa5577 | 1870 | seq_print_ip_sym(s, field->parent_ip, sym_flags); |
214023c3 | 1871 | trace_seq_puts(s, ")\n"); |
bc0c38d1 | 1872 | break; |
777e208d | 1873 | } |
bc0c38d1 | 1874 | case TRACE_CTX: |
777e208d | 1875 | case TRACE_WAKE: { |
7104f300 SR |
1876 | struct ctx_switch_entry *field; |
1877 | ||
1878 | trace_assign_type(field, entry); | |
777e208d | 1879 | |
3d9101e9 TG |
1880 | T = task_state_char(field->next_state); |
1881 | S = task_state_char(field->prev_state); | |
777e208d | 1882 | comm = trace_find_cmdline(field->next_pid); |
80b5e940 | 1883 | trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n", |
777e208d SR |
1884 | field->prev_pid, |
1885 | field->prev_prio, | |
57422797 | 1886 | S, entry->type == TRACE_CTX ? "==>" : " +", |
777e208d SR |
1887 | field->next_cpu, |
1888 | field->next_pid, | |
1889 | field->next_prio, | |
bac524d3 | 1890 | T, comm); |
bc0c38d1 | 1891 | break; |
777e208d SR |
1892 | } |
1893 | case TRACE_SPECIAL: { | |
7104f300 SR |
1894 | struct special_entry *field; |
1895 | ||
1896 | trace_assign_type(field, entry); | |
777e208d | 1897 | |
88a4216c | 1898 | trace_seq_printf(s, "# %ld %ld %ld\n", |
777e208d SR |
1899 | field->arg1, |
1900 | field->arg2, | |
1901 | field->arg3); | |
f0a920d5 | 1902 | break; |
777e208d SR |
1903 | } |
1904 | case TRACE_STACK: { | |
7104f300 SR |
1905 | struct stack_entry *field; |
1906 | ||
1907 | trace_assign_type(field, entry); | |
777e208d | 1908 | |
86387f7e IM |
1909 | for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { |
1910 | if (i) | |
1911 | trace_seq_puts(s, " <= "); | |
777e208d | 1912 | seq_print_ip_sym(s, field->caller[i], sym_flags); |
86387f7e IM |
1913 | } |
1914 | trace_seq_puts(s, "\n"); | |
1915 | break; | |
777e208d SR |
1916 | } |
1917 | case TRACE_PRINT: { | |
7104f300 SR |
1918 | struct print_entry *field; |
1919 | ||
1920 | trace_assign_type(field, entry); | |
777e208d SR |
1921 | |
1922 | seq_print_ip_sym(s, field->ip, sym_flags); | |
1923 | trace_seq_printf(s, ": %s", field->buf); | |
1924 | if (entry->flags & TRACE_FLAG_CONT) | |
dd0e545f SR |
1925 | trace_seq_print_cont(s, iter); |
1926 | break; | |
777e208d | 1927 | } |
9f029e83 SR |
1928 | case TRACE_BRANCH: { |
1929 | struct trace_branch *field; | |
52f232cb SR |
1930 | |
1931 | trace_assign_type(field, entry); | |
1932 | ||
1933 | trace_seq_printf(s, "[%s] %s:%s:%d\n", | |
68d119f0 | 1934 | field->correct ? " ok " : " MISS ", |
52f232cb SR |
1935 | field->func, |
1936 | field->file, | |
1937 | field->line); | |
1938 | break; | |
1939 | } | |
02b67518 TE |
1940 | case TRACE_USER_STACK: { |
1941 | struct userstack_entry *field; | |
1942 | ||
1943 | trace_assign_type(field, entry); | |
1944 | ||
1945 | seq_print_userip_objs(field, s, sym_flags); | |
b54d3de9 | 1946 | trace_seq_putc(s, '\n'); |
02b67518 TE |
1947 | break; |
1948 | } | |
89b2f978 | 1949 | default: |
214023c3 | 1950 | trace_seq_printf(s, "Unknown type %d\n", entry->type); |
bc0c38d1 | 1951 | } |
2c4f035f | 1952 | return TRACE_TYPE_HANDLED; |
bc0c38d1 SR |
1953 | } |
1954 | ||
2c4f035f | 1955 | static enum print_line_t print_trace_fmt(struct trace_iterator *iter) |
bc0c38d1 | 1956 | { |
214023c3 | 1957 | struct trace_seq *s = &iter->seq; |
bc0c38d1 | 1958 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); |
4e3c3333 | 1959 | struct trace_entry *entry; |
bc0c38d1 SR |
1960 | unsigned long usec_rem; |
1961 | unsigned long long t; | |
1962 | unsigned long secs; | |
1963 | char *comm; | |
b3806b43 | 1964 | int ret; |
bac524d3 | 1965 | int S, T; |
86387f7e | 1966 | int i; |
bc0c38d1 | 1967 | |
4e3c3333 | 1968 | entry = iter->ent; |
dd0e545f SR |
1969 | |
1970 | if (entry->type == TRACE_CONT) | |
2c4f035f | 1971 | return TRACE_TYPE_HANDLED; |
dd0e545f | 1972 | |
a309720c SR |
1973 | test_cpu_buff_start(iter); |
1974 | ||
777e208d | 1975 | comm = trace_find_cmdline(iter->ent->pid); |
bc0c38d1 | 1976 | |
3928a8a2 | 1977 | t = ns2usecs(iter->ts); |
bc0c38d1 SR |
1978 | usec_rem = do_div(t, 1000000ULL); |
1979 | secs = (unsigned long)t; | |
1980 | ||
777e208d | 1981 | ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid); |
f29c73fe | 1982 | if (!ret) |
2c4f035f | 1983 | return TRACE_TYPE_PARTIAL_LINE; |
a6168353 | 1984 | ret = trace_seq_printf(s, "[%03d] ", iter->cpu); |
f29c73fe | 1985 | if (!ret) |
2c4f035f | 1986 | return TRACE_TYPE_PARTIAL_LINE; |
f29c73fe IM |
1987 | ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem); |
1988 | if (!ret) | |
2c4f035f | 1989 | return TRACE_TYPE_PARTIAL_LINE; |
bc0c38d1 SR |
1990 | |
1991 | switch (entry->type) { | |
777e208d | 1992 | case TRACE_FN: { |
7104f300 SR |
1993 | struct ftrace_entry *field; |
1994 | ||
1995 | trace_assign_type(field, entry); | |
777e208d SR |
1996 | |
1997 | ret = seq_print_ip_sym(s, field->ip, sym_flags); | |
b3806b43 | 1998 | if (!ret) |
2c4f035f | 1999 | return TRACE_TYPE_PARTIAL_LINE; |
bc0c38d1 | 2000 | if ((sym_flags & TRACE_ITER_PRINT_PARENT) && |
777e208d | 2001 | field->parent_ip) { |
b3806b43 SR |
2002 | ret = trace_seq_printf(s, " <-"); |
2003 | if (!ret) | |
2c4f035f | 2004 | return TRACE_TYPE_PARTIAL_LINE; |
b3aa5577 SR |
2005 | ret = seq_print_ip_sym(s, |
2006 | field->parent_ip, | |
2007 | sym_flags); | |
b3806b43 | 2008 | if (!ret) |
2c4f035f | 2009 | return TRACE_TYPE_PARTIAL_LINE; |
bc0c38d1 | 2010 | } |
b3806b43 SR |
2011 | ret = trace_seq_printf(s, "\n"); |
2012 | if (!ret) | |
2c4f035f | 2013 | return TRACE_TYPE_PARTIAL_LINE; |
bc0c38d1 | 2014 | break; |
777e208d | 2015 | } |
bc0c38d1 | 2016 | case TRACE_CTX: |
777e208d | 2017 | case TRACE_WAKE: { |
7104f300 SR |
2018 | struct ctx_switch_entry *field; |
2019 | ||
2020 | trace_assign_type(field, entry); | |
777e208d | 2021 | |
3d9101e9 TG |
2022 | T = task_state_char(field->next_state); |
2023 | S = task_state_char(field->prev_state); | |
80b5e940 | 2024 | ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n", |
777e208d SR |
2025 | field->prev_pid, |
2026 | field->prev_prio, | |
b3806b43 | 2027 | S, |
57422797 | 2028 | entry->type == TRACE_CTX ? "==>" : " +", |
777e208d SR |
2029 | field->next_cpu, |
2030 | field->next_pid, | |
2031 | field->next_prio, | |
bac524d3 | 2032 | T); |
b3806b43 | 2033 | if (!ret) |
2c4f035f | 2034 | return TRACE_TYPE_PARTIAL_LINE; |
bc0c38d1 | 2035 | break; |
777e208d SR |
2036 | } |
2037 | case TRACE_SPECIAL: { | |
7104f300 SR |
2038 | struct special_entry *field; |
2039 | ||
2040 | trace_assign_type(field, entry); | |
777e208d | 2041 | |
88a4216c | 2042 | ret = trace_seq_printf(s, "# %ld %ld %ld\n", |
777e208d SR |
2043 | field->arg1, |
2044 | field->arg2, | |
2045 | field->arg3); | |
f0a920d5 | 2046 | if (!ret) |
2c4f035f | 2047 | return TRACE_TYPE_PARTIAL_LINE; |
f0a920d5 | 2048 | break; |
777e208d SR |
2049 | } |
2050 | case TRACE_STACK: { | |
7104f300 SR |
2051 | struct stack_entry *field; |
2052 | ||
2053 | trace_assign_type(field, entry); | |
777e208d | 2054 | |
86387f7e IM |
2055 | for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { |
2056 | if (i) { | |
2057 | ret = trace_seq_puts(s, " <= "); | |
2058 | if (!ret) | |
2c4f035f | 2059 | return TRACE_TYPE_PARTIAL_LINE; |
86387f7e | 2060 | } |
777e208d | 2061 | ret = seq_print_ip_sym(s, field->caller[i], |
86387f7e IM |
2062 | sym_flags); |
2063 | if (!ret) | |
2c4f035f | 2064 | return TRACE_TYPE_PARTIAL_LINE; |
86387f7e IM |
2065 | } |
2066 | ret = trace_seq_puts(s, "\n"); | |
2067 | if (!ret) | |
2c4f035f | 2068 | return TRACE_TYPE_PARTIAL_LINE; |
86387f7e | 2069 | break; |
777e208d SR |
2070 | } |
2071 | case TRACE_PRINT: { | |
7104f300 SR |
2072 | struct print_entry *field; |
2073 | ||
2074 | trace_assign_type(field, entry); | |
777e208d SR |
2075 | |
2076 | seq_print_ip_sym(s, field->ip, sym_flags); | |
2077 | trace_seq_printf(s, ": %s", field->buf); | |
2078 | if (entry->flags & TRACE_FLAG_CONT) | |
dd0e545f SR |
2079 | trace_seq_print_cont(s, iter); |
2080 | break; | |
bc0c38d1 | 2081 | } |
287b6e68 FW |
2082 | case TRACE_GRAPH_RET: { |
2083 | return print_graph_function(iter); | |
2084 | } | |
2085 | case TRACE_GRAPH_ENT: { | |
fb52607a | 2086 | return print_graph_function(iter); |
15e6cb36 | 2087 | } |
9f029e83 SR |
2088 | case TRACE_BRANCH: { |
2089 | struct trace_branch *field; | |
52f232cb SR |
2090 | |
2091 | trace_assign_type(field, entry); | |
2092 | ||
2093 | trace_seq_printf(s, "[%s] %s:%s:%d\n", | |
68d119f0 | 2094 | field->correct ? " ok " : " MISS ", |
52f232cb SR |
2095 | field->func, |
2096 | field->file, | |
2097 | field->line); | |
2098 | break; | |
2099 | } | |
02b67518 TE |
2100 | case TRACE_USER_STACK: { |
2101 | struct userstack_entry *field; | |
2102 | ||
2103 | trace_assign_type(field, entry); | |
2104 | ||
2105 | ret = seq_print_userip_objs(field, s, sym_flags); | |
2106 | if (!ret) | |
2107 | return TRACE_TYPE_PARTIAL_LINE; | |
2108 | ret = trace_seq_putc(s, '\n'); | |
2109 | if (!ret) | |
2110 | return TRACE_TYPE_PARTIAL_LINE; | |
2111 | break; | |
2112 | } | |
777e208d | 2113 | } |
2c4f035f | 2114 | return TRACE_TYPE_HANDLED; |
bc0c38d1 SR |
2115 | } |
2116 | ||
2c4f035f | 2117 | static enum print_line_t print_raw_fmt(struct trace_iterator *iter) |
f9896bf3 IM |
2118 | { |
2119 | struct trace_seq *s = &iter->seq; | |
2120 | struct trace_entry *entry; | |
2121 | int ret; | |
bac524d3 | 2122 | int S, T; |
f9896bf3 IM |
2123 | |
2124 | entry = iter->ent; | |
dd0e545f SR |
2125 | |
2126 | if (entry->type == TRACE_CONT) | |
2c4f035f | 2127 | return TRACE_TYPE_HANDLED; |
dd0e545f | 2128 | |
f9896bf3 | 2129 | ret = trace_seq_printf(s, "%d %d %llu ", |
777e208d | 2130 | entry->pid, iter->cpu, iter->ts); |
f9896bf3 | 2131 | if (!ret) |
2c4f035f | 2132 | return TRACE_TYPE_PARTIAL_LINE; |
f9896bf3 IM |
2133 | |
2134 | switch (entry->type) { | |
777e208d | 2135 | case TRACE_FN: { |
7104f300 SR |
2136 | struct ftrace_entry *field; |
2137 | ||
2138 | trace_assign_type(field, entry); | |
777e208d | 2139 | |
f9896bf3 | 2140 | ret = trace_seq_printf(s, "%x %x\n", |
777e208d SR |
2141 | field->ip, |
2142 | field->parent_ip); | |
f9896bf3 | 2143 | if (!ret) |
2c4f035f | 2144 | return TRACE_TYPE_PARTIAL_LINE; |
f9896bf3 | 2145 | break; |
777e208d | 2146 | } |
f9896bf3 | 2147 | case TRACE_CTX: |
777e208d | 2148 | case TRACE_WAKE: { |
7104f300 SR |
2149 | struct ctx_switch_entry *field; |
2150 | ||
2151 | trace_assign_type(field, entry); | |
777e208d | 2152 | |
3d9101e9 TG |
2153 | T = task_state_char(field->next_state); |
2154 | S = entry->type == TRACE_WAKE ? '+' : | |
2155 | task_state_char(field->prev_state); | |
80b5e940 | 2156 | ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n", |
777e208d SR |
2157 | field->prev_pid, |
2158 | field->prev_prio, | |
f9896bf3 | 2159 | S, |
777e208d SR |
2160 | field->next_cpu, |
2161 | field->next_pid, | |
2162 | field->next_prio, | |
bac524d3 | 2163 | T); |
f9896bf3 | 2164 | if (!ret) |
2c4f035f | 2165 | return TRACE_TYPE_PARTIAL_LINE; |
f9896bf3 | 2166 | break; |
777e208d | 2167 | } |
f0a920d5 | 2168 | case TRACE_SPECIAL: |
02b67518 | 2169 | case TRACE_USER_STACK: |
777e208d | 2170 | case TRACE_STACK: { |
7104f300 SR |
2171 | struct special_entry *field; |
2172 | ||
2173 | trace_assign_type(field, entry); | |
777e208d | 2174 | |
88a4216c | 2175 | ret = trace_seq_printf(s, "# %ld %ld %ld\n", |
777e208d SR |
2176 | field->arg1, |
2177 | field->arg2, | |
2178 | field->arg3); | |
f0a920d5 | 2179 | if (!ret) |
2c4f035f | 2180 | return TRACE_TYPE_PARTIAL_LINE; |
f0a920d5 | 2181 | break; |
777e208d SR |
2182 | } |
2183 | case TRACE_PRINT: { | |
7104f300 SR |
2184 | struct print_entry *field; |
2185 | ||
2186 | trace_assign_type(field, entry); | |
777e208d SR |
2187 | |
2188 | trace_seq_printf(s, "# %lx %s", field->ip, field->buf); | |
2189 | if (entry->flags & TRACE_FLAG_CONT) | |
dd0e545f SR |
2190 | trace_seq_print_cont(s, iter); |
2191 | break; | |
f9896bf3 | 2192 | } |
777e208d | 2193 | } |
2c4f035f | 2194 | return TRACE_TYPE_HANDLED; |
f9896bf3 IM |
2195 | } |
2196 | ||
cb0f12aa IM |
2197 | #define SEQ_PUT_FIELD_RET(s, x) \ |
2198 | do { \ | |
2199 | if (!trace_seq_putmem(s, &(x), sizeof(x))) \ | |
2200 | return 0; \ | |
2201 | } while (0) | |
2202 | ||
5e3ca0ec IM |
2203 | #define SEQ_PUT_HEX_FIELD_RET(s, x) \ |
2204 | do { \ | |
ad0a3b68 | 2205 | BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \ |
5e3ca0ec IM |
2206 | if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \ |
2207 | return 0; \ | |
2208 | } while (0) | |
2209 | ||
2c4f035f | 2210 | static enum print_line_t print_hex_fmt(struct trace_iterator *iter) |
5e3ca0ec IM |
2211 | { |
2212 | struct trace_seq *s = &iter->seq; | |
2213 | unsigned char newline = '\n'; | |
2214 | struct trace_entry *entry; | |
bac524d3 | 2215 | int S, T; |
5e3ca0ec IM |
2216 | |
2217 | entry = iter->ent; | |
dd0e545f SR |
2218 | |
2219 | if (entry->type == TRACE_CONT) | |
2c4f035f | 2220 | return TRACE_TYPE_HANDLED; |
dd0e545f | 2221 | |
777e208d | 2222 | SEQ_PUT_HEX_FIELD_RET(s, entry->pid); |
5e3ca0ec | 2223 | SEQ_PUT_HEX_FIELD_RET(s, iter->cpu); |
3928a8a2 | 2224 | SEQ_PUT_HEX_FIELD_RET(s, iter->ts); |
5e3ca0ec IM |
2225 | |
2226 | switch (entry->type) { | |
777e208d | 2227 | case TRACE_FN: { |
7104f300 SR |
2228 | struct ftrace_entry *field; |
2229 | ||
2230 | trace_assign_type(field, entry); | |
777e208d SR |
2231 | |
2232 | SEQ_PUT_HEX_FIELD_RET(s, field->ip); | |
2233 | SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip); | |
5e3ca0ec | 2234 | break; |
777e208d | 2235 | } |
5e3ca0ec | 2236 | case TRACE_CTX: |
777e208d | 2237 | case TRACE_WAKE: { |
7104f300 SR |
2238 | struct ctx_switch_entry *field; |
2239 | ||
2240 | trace_assign_type(field, entry); | |
777e208d | 2241 | |
3d9101e9 TG |
2242 | T = task_state_char(field->next_state); |
2243 | S = entry->type == TRACE_WAKE ? '+' : | |
2244 | task_state_char(field->prev_state); | |
777e208d SR |
2245 | SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid); |
2246 | SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio); | |
5e3ca0ec | 2247 | SEQ_PUT_HEX_FIELD_RET(s, S); |
777e208d SR |
2248 | SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu); |
2249 | SEQ_PUT_HEX_FIELD_RET(s, field->next_pid); | |
2250 | SEQ_PUT_HEX_FIELD_RET(s, field->next_prio); | |
bac524d3 | 2251 | SEQ_PUT_HEX_FIELD_RET(s, T); |
5e3ca0ec | 2252 | break; |
777e208d | 2253 | } |
5e3ca0ec | 2254 | case TRACE_SPECIAL: |
02b67518 | 2255 | case TRACE_USER_STACK: |
777e208d | 2256 | case TRACE_STACK: { |
7104f300 SR |
2257 | struct special_entry *field; |
2258 | ||
2259 | trace_assign_type(field, entry); | |
777e208d SR |
2260 | |
2261 | SEQ_PUT_HEX_FIELD_RET(s, field->arg1); | |
2262 | SEQ_PUT_HEX_FIELD_RET(s, field->arg2); | |
2263 | SEQ_PUT_HEX_FIELD_RET(s, field->arg3); | |
5e3ca0ec IM |
2264 | break; |
2265 | } | |
777e208d | 2266 | } |
5e3ca0ec IM |
2267 | SEQ_PUT_FIELD_RET(s, newline); |
2268 | ||
2c4f035f | 2269 | return TRACE_TYPE_HANDLED; |
5e3ca0ec IM |
2270 | } |
2271 | ||
66896a85 FW |
2272 | static enum print_line_t print_printk_msg_only(struct trace_iterator *iter) |
2273 | { | |
2274 | struct trace_seq *s = &iter->seq; | |
2275 | struct trace_entry *entry = iter->ent; | |
2276 | struct print_entry *field; | |
2277 | int ret; | |
2278 | ||
2279 | trace_assign_type(field, entry); | |
2280 | ||
2281 | ret = trace_seq_printf(s, field->buf); | |
2282 | if (!ret) | |
2283 | return TRACE_TYPE_PARTIAL_LINE; | |
2284 | ||
2285 | if (entry->flags & TRACE_FLAG_CONT) | |
2286 | trace_seq_print_cont(s, iter); | |
2287 | ||
2288 | return TRACE_TYPE_HANDLED; | |
2289 | } | |
2290 | ||
2c4f035f | 2291 | static enum print_line_t print_bin_fmt(struct trace_iterator *iter) |
cb0f12aa IM |
2292 | { |
2293 | struct trace_seq *s = &iter->seq; | |
2294 | struct trace_entry *entry; | |
2295 | ||
2296 | entry = iter->ent; | |
dd0e545f SR |
2297 | |
2298 | if (entry->type == TRACE_CONT) | |
2c4f035f | 2299 | return TRACE_TYPE_HANDLED; |
dd0e545f | 2300 | |
777e208d | 2301 | SEQ_PUT_FIELD_RET(s, entry->pid); |
072ba498 | 2302 | SEQ_PUT_FIELD_RET(s, entry->cpu); |
3928a8a2 | 2303 | SEQ_PUT_FIELD_RET(s, iter->ts); |
cb0f12aa IM |
2304 | |
2305 | switch (entry->type) { | |
777e208d | 2306 | case TRACE_FN: { |
7104f300 SR |
2307 | struct ftrace_entry *field; |
2308 | ||
2309 | trace_assign_type(field, entry); | |
777e208d SR |
2310 | |
2311 | SEQ_PUT_FIELD_RET(s, field->ip); | |
2312 | SEQ_PUT_FIELD_RET(s, field->parent_ip); | |
cb0f12aa | 2313 | break; |
777e208d SR |
2314 | } |
2315 | case TRACE_CTX: { | |
7104f300 SR |
2316 | struct ctx_switch_entry *field; |
2317 | ||
2318 | trace_assign_type(field, entry); | |
777e208d SR |
2319 | |
2320 | SEQ_PUT_FIELD_RET(s, field->prev_pid); | |
2321 | SEQ_PUT_FIELD_RET(s, field->prev_prio); | |
2322 | SEQ_PUT_FIELD_RET(s, field->prev_state); | |
2323 | SEQ_PUT_FIELD_RET(s, field->next_pid); | |
2324 | SEQ_PUT_FIELD_RET(s, field->next_prio); | |
2325 | SEQ_PUT_FIELD_RET(s, field->next_state); | |
cb0f12aa | 2326 | break; |
777e208d | 2327 | } |
f0a920d5 | 2328 | case TRACE_SPECIAL: |
02b67518 | 2329 | case TRACE_USER_STACK: |
777e208d | 2330 | case TRACE_STACK: { |
7104f300 SR |
2331 | struct special_entry *field; |
2332 | ||
2333 | trace_assign_type(field, entry); | |
777e208d SR |
2334 | |
2335 | SEQ_PUT_FIELD_RET(s, field->arg1); | |
2336 | SEQ_PUT_FIELD_RET(s, field->arg2); | |
2337 | SEQ_PUT_FIELD_RET(s, field->arg3); | |
f0a920d5 | 2338 | break; |
cb0f12aa | 2339 | } |
777e208d | 2340 | } |
cb0f12aa IM |
2341 | return 1; |
2342 | } | |
2343 | ||
bc0c38d1 SR |
2344 | static int trace_empty(struct trace_iterator *iter) |
2345 | { | |
bc0c38d1 SR |
2346 | int cpu; |
2347 | ||
ab46428c | 2348 | for_each_tracing_cpu(cpu) { |
d769041f SR |
2349 | if (iter->buffer_iter[cpu]) { |
2350 | if (!ring_buffer_iter_empty(iter->buffer_iter[cpu])) | |
2351 | return 0; | |
2352 | } else { | |
2353 | if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu)) | |
2354 | return 0; | |
2355 | } | |
bc0c38d1 | 2356 | } |
d769041f | 2357 | |
797d3712 | 2358 | return 1; |
bc0c38d1 SR |
2359 | } |
2360 | ||
2c4f035f | 2361 | static enum print_line_t print_trace_line(struct trace_iterator *iter) |
f9896bf3 | 2362 | { |
2c4f035f FW |
2363 | enum print_line_t ret; |
2364 | ||
2365 | if (iter->trace && iter->trace->print_line) { | |
2366 | ret = iter->trace->print_line(iter); | |
2367 | if (ret != TRACE_TYPE_UNHANDLED) | |
2368 | return ret; | |
2369 | } | |
72829bc3 | 2370 | |
66896a85 FW |
2371 | if (iter->ent->type == TRACE_PRINT && |
2372 | trace_flags & TRACE_ITER_PRINTK && | |
2373 | trace_flags & TRACE_ITER_PRINTK_MSGONLY) | |
2374 | return print_printk_msg_only(iter); | |
2375 | ||
cb0f12aa IM |
2376 | if (trace_flags & TRACE_ITER_BIN) |
2377 | return print_bin_fmt(iter); | |
2378 | ||
5e3ca0ec IM |
2379 | if (trace_flags & TRACE_ITER_HEX) |
2380 | return print_hex_fmt(iter); | |
2381 | ||
f9896bf3 IM |
2382 | if (trace_flags & TRACE_ITER_RAW) |
2383 | return print_raw_fmt(iter); | |
2384 | ||
2385 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) | |
2386 | return print_lat_fmt(iter, iter->idx, iter->cpu); | |
2387 | ||
2388 | return print_trace_fmt(iter); | |
2389 | } | |
2390 | ||
bc0c38d1 SR |
2391 | static int s_show(struct seq_file *m, void *v) |
2392 | { | |
2393 | struct trace_iterator *iter = v; | |
2394 | ||
2395 | if (iter->ent == NULL) { | |
2396 | if (iter->tr) { | |
2397 | seq_printf(m, "# tracer: %s\n", iter->trace->name); | |
2398 | seq_puts(m, "#\n"); | |
2399 | } | |
8bba1bf5 MM |
2400 | if (iter->trace && iter->trace->print_header) |
2401 | iter->trace->print_header(m); | |
2402 | else if (iter->iter_flags & TRACE_FILE_LAT_FMT) { | |
bc0c38d1 SR |
2403 | /* print nothing if the buffers are empty */ |
2404 | if (trace_empty(iter)) | |
2405 | return 0; | |
2406 | print_trace_header(m, iter); | |
2407 | if (!(trace_flags & TRACE_ITER_VERBOSE)) | |
2408 | print_lat_help_header(m); | |
2409 | } else { | |
2410 | if (!(trace_flags & TRACE_ITER_VERBOSE)) | |
2411 | print_func_help_header(m); | |
2412 | } | |
2413 | } else { | |
f9896bf3 | 2414 | print_trace_line(iter); |
214023c3 | 2415 | trace_print_seq(m, &iter->seq); |
bc0c38d1 SR |
2416 | } |
2417 | ||
2418 | return 0; | |
2419 | } | |
2420 | ||
2421 | static struct seq_operations tracer_seq_ops = { | |
4bf39a94 IM |
2422 | .start = s_start, |
2423 | .next = s_next, | |
2424 | .stop = s_stop, | |
2425 | .show = s_show, | |
bc0c38d1 SR |
2426 | }; |
2427 | ||
e309b41d | 2428 | static struct trace_iterator * |
bc0c38d1 SR |
2429 | __tracing_open(struct inode *inode, struct file *file, int *ret) |
2430 | { | |
2431 | struct trace_iterator *iter; | |
3928a8a2 SR |
2432 | struct seq_file *m; |
2433 | int cpu; | |
bc0c38d1 | 2434 | |
60a11774 SR |
2435 | if (tracing_disabled) { |
2436 | *ret = -ENODEV; | |
2437 | return NULL; | |
2438 | } | |
2439 | ||
bc0c38d1 SR |
2440 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
2441 | if (!iter) { | |
2442 | *ret = -ENOMEM; | |
2443 | goto out; | |
2444 | } | |
2445 | ||
2446 | mutex_lock(&trace_types_lock); | |
2447 | if (current_trace && current_trace->print_max) | |
2448 | iter->tr = &max_tr; | |
2449 | else | |
2450 | iter->tr = inode->i_private; | |
2451 | iter->trace = current_trace; | |
2452 | iter->pos = -1; | |
2453 | ||
8bba1bf5 MM |
2454 | /* Notify the tracer early; before we stop tracing. */ |
2455 | if (iter->trace && iter->trace->open) | |
a93751ca | 2456 | iter->trace->open(iter); |
8bba1bf5 | 2457 | |
12ef7d44 SR |
2458 | /* Annotate start of buffers if we had overruns */ |
2459 | if (ring_buffer_overruns(iter->tr->buffer)) | |
2460 | iter->iter_flags |= TRACE_FILE_ANNOTATE; | |
2461 | ||
2462 | ||
3928a8a2 | 2463 | for_each_tracing_cpu(cpu) { |
d769041f | 2464 | |
3928a8a2 SR |
2465 | iter->buffer_iter[cpu] = |
2466 | ring_buffer_read_start(iter->tr->buffer, cpu); | |
d769041f | 2467 | |
3928a8a2 SR |
2468 | if (!iter->buffer_iter[cpu]) |
2469 | goto fail_buffer; | |
2470 | } | |
2471 | ||
bc0c38d1 SR |
2472 | /* TODO stop tracer */ |
2473 | *ret = seq_open(file, &tracer_seq_ops); | |
3928a8a2 SR |
2474 | if (*ret) |
2475 | goto fail_buffer; | |
bc0c38d1 | 2476 | |
3928a8a2 SR |
2477 | m = file->private_data; |
2478 | m->private = iter; | |
bc0c38d1 | 2479 | |
3928a8a2 | 2480 | /* stop the trace while dumping */ |
9036990d | 2481 | tracing_stop(); |
3928a8a2 | 2482 | |
bc0c38d1 SR |
2483 | mutex_unlock(&trace_types_lock); |
2484 | ||
2485 | out: | |
2486 | return iter; | |
3928a8a2 SR |
2487 | |
2488 | fail_buffer: | |
2489 | for_each_tracing_cpu(cpu) { | |
2490 | if (iter->buffer_iter[cpu]) | |
2491 | ring_buffer_read_finish(iter->buffer_iter[cpu]); | |
2492 | } | |
2493 | mutex_unlock(&trace_types_lock); | |
0bb943c7 | 2494 | kfree(iter); |
3928a8a2 SR |
2495 | |
2496 | return ERR_PTR(-ENOMEM); | |
bc0c38d1 SR |
2497 | } |
2498 | ||
2499 | int tracing_open_generic(struct inode *inode, struct file *filp) | |
2500 | { | |
60a11774 SR |
2501 | if (tracing_disabled) |
2502 | return -ENODEV; | |
2503 | ||
bc0c38d1 SR |
2504 | filp->private_data = inode->i_private; |
2505 | return 0; | |
2506 | } | |
2507 | ||
2508 | int tracing_release(struct inode *inode, struct file *file) | |
2509 | { | |
2510 | struct seq_file *m = (struct seq_file *)file->private_data; | |
2511 | struct trace_iterator *iter = m->private; | |
3928a8a2 | 2512 | int cpu; |
bc0c38d1 SR |
2513 | |
2514 | mutex_lock(&trace_types_lock); | |
3928a8a2 SR |
2515 | for_each_tracing_cpu(cpu) { |
2516 | if (iter->buffer_iter[cpu]) | |
2517 | ring_buffer_read_finish(iter->buffer_iter[cpu]); | |
2518 | } | |
2519 | ||
bc0c38d1 SR |
2520 | if (iter->trace && iter->trace->close) |
2521 | iter->trace->close(iter); | |
2522 | ||
2523 | /* reenable tracing if it was previously enabled */ | |
9036990d | 2524 | tracing_start(); |
bc0c38d1 SR |
2525 | mutex_unlock(&trace_types_lock); |
2526 | ||
2527 | seq_release(inode, file); | |
2528 | kfree(iter); | |
2529 | return 0; | |
2530 | } | |
2531 | ||
2532 | static int tracing_open(struct inode *inode, struct file *file) | |
2533 | { | |
2534 | int ret; | |
2535 | ||
2536 | __tracing_open(inode, file, &ret); | |
2537 | ||
2538 | return ret; | |
2539 | } | |
2540 | ||
2541 | static int tracing_lt_open(struct inode *inode, struct file *file) | |
2542 | { | |
2543 | struct trace_iterator *iter; | |
2544 | int ret; | |
2545 | ||
2546 | iter = __tracing_open(inode, file, &ret); | |
2547 | ||
2548 | if (!ret) | |
2549 | iter->iter_flags |= TRACE_FILE_LAT_FMT; | |
2550 | ||
2551 | return ret; | |
2552 | } | |
2553 | ||
2554 | ||
e309b41d | 2555 | static void * |
bc0c38d1 SR |
2556 | t_next(struct seq_file *m, void *v, loff_t *pos) |
2557 | { | |
2558 | struct tracer *t = m->private; | |
2559 | ||
2560 | (*pos)++; | |
2561 | ||
2562 | if (t) | |
2563 | t = t->next; | |
2564 | ||
2565 | m->private = t; | |
2566 | ||
2567 | return t; | |
2568 | } | |
2569 | ||
2570 | static void *t_start(struct seq_file *m, loff_t *pos) | |
2571 | { | |
2572 | struct tracer *t = m->private; | |
2573 | loff_t l = 0; | |
2574 | ||
2575 | mutex_lock(&trace_types_lock); | |
2576 | for (; t && l < *pos; t = t_next(m, t, &l)) | |
2577 | ; | |
2578 | ||
2579 | return t; | |
2580 | } | |
2581 | ||
2582 | static void t_stop(struct seq_file *m, void *p) | |
2583 | { | |
2584 | mutex_unlock(&trace_types_lock); | |
2585 | } | |
2586 | ||
2587 | static int t_show(struct seq_file *m, void *v) | |
2588 | { | |
2589 | struct tracer *t = v; | |
2590 | ||
2591 | if (!t) | |
2592 | return 0; | |
2593 | ||
2594 | seq_printf(m, "%s", t->name); | |
2595 | if (t->next) | |
2596 | seq_putc(m, ' '); | |
2597 | else | |
2598 | seq_putc(m, '\n'); | |
2599 | ||
2600 | return 0; | |
2601 | } | |
2602 | ||
2603 | static struct seq_operations show_traces_seq_ops = { | |
4bf39a94 IM |
2604 | .start = t_start, |
2605 | .next = t_next, | |
2606 | .stop = t_stop, | |
2607 | .show = t_show, | |
bc0c38d1 SR |
2608 | }; |
2609 | ||
2610 | static int show_traces_open(struct inode *inode, struct file *file) | |
2611 | { | |
2612 | int ret; | |
2613 | ||
60a11774 SR |
2614 | if (tracing_disabled) |
2615 | return -ENODEV; | |
2616 | ||
bc0c38d1 SR |
2617 | ret = seq_open(file, &show_traces_seq_ops); |
2618 | if (!ret) { | |
2619 | struct seq_file *m = file->private_data; | |
2620 | m->private = trace_types; | |
2621 | } | |
2622 | ||
2623 | return ret; | |
2624 | } | |
2625 | ||
2626 | static struct file_operations tracing_fops = { | |
4bf39a94 IM |
2627 | .open = tracing_open, |
2628 | .read = seq_read, | |
2629 | .llseek = seq_lseek, | |
2630 | .release = tracing_release, | |
bc0c38d1 SR |
2631 | }; |
2632 | ||
2633 | static struct file_operations tracing_lt_fops = { | |
4bf39a94 IM |
2634 | .open = tracing_lt_open, |
2635 | .read = seq_read, | |
2636 | .llseek = seq_lseek, | |
2637 | .release = tracing_release, | |
bc0c38d1 SR |
2638 | }; |
2639 | ||
2640 | static struct file_operations show_traces_fops = { | |
c7078de1 IM |
2641 | .open = show_traces_open, |
2642 | .read = seq_read, | |
2643 | .release = seq_release, | |
2644 | }; | |
2645 | ||
36dfe925 IM |
2646 | /* |
2647 | * Only trace on a CPU if the bitmask is set: | |
2648 | */ | |
2649 | static cpumask_t tracing_cpumask = CPU_MASK_ALL; | |
2650 | ||
2651 | /* | |
2652 | * When tracing/tracing_cpu_mask is modified then this holds | |
2653 | * the new bitmask we are about to install: | |
2654 | */ | |
2655 | static cpumask_t tracing_cpumask_new; | |
2656 | ||
2657 | /* | |
2658 | * The tracer itself will not take this lock, but still we want | |
2659 | * to provide a consistent cpumask to user-space: | |
2660 | */ | |
2661 | static DEFINE_MUTEX(tracing_cpumask_update_lock); | |
2662 | ||
2663 | /* | |
2664 | * Temporary storage for the character representation of the | |
2665 | * CPU bitmask (and one more byte for the newline): | |
2666 | */ | |
2667 | static char mask_str[NR_CPUS + 1]; | |
2668 | ||
c7078de1 IM |
2669 | static ssize_t |
2670 | tracing_cpumask_read(struct file *filp, char __user *ubuf, | |
2671 | size_t count, loff_t *ppos) | |
2672 | { | |
36dfe925 | 2673 | int len; |
c7078de1 IM |
2674 | |
2675 | mutex_lock(&tracing_cpumask_update_lock); | |
36dfe925 | 2676 | |
29c0177e | 2677 | len = cpumask_scnprintf(mask_str, count, &tracing_cpumask); |
36dfe925 IM |
2678 | if (count - len < 2) { |
2679 | count = -EINVAL; | |
2680 | goto out_err; | |
2681 | } | |
2682 | len += sprintf(mask_str + len, "\n"); | |
2683 | count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1); | |
2684 | ||
2685 | out_err: | |
c7078de1 IM |
2686 | mutex_unlock(&tracing_cpumask_update_lock); |
2687 | ||
2688 | return count; | |
2689 | } | |
2690 | ||
2691 | static ssize_t | |
2692 | tracing_cpumask_write(struct file *filp, const char __user *ubuf, | |
2693 | size_t count, loff_t *ppos) | |
2694 | { | |
36dfe925 | 2695 | int err, cpu; |
c7078de1 IM |
2696 | |
2697 | mutex_lock(&tracing_cpumask_update_lock); | |
29c0177e | 2698 | err = cpumask_parse_user(ubuf, count, &tracing_cpumask_new); |
c7078de1 | 2699 | if (err) |
36dfe925 IM |
2700 | goto err_unlock; |
2701 | ||
a5e25883 | 2702 | local_irq_disable(); |
92205c23 | 2703 | __raw_spin_lock(&ftrace_max_lock); |
ab46428c | 2704 | for_each_tracing_cpu(cpu) { |
36dfe925 IM |
2705 | /* |
2706 | * Increase/decrease the disabled counter if we are | |
2707 | * about to flip a bit in the cpumask: | |
2708 | */ | |
2709 | if (cpu_isset(cpu, tracing_cpumask) && | |
2710 | !cpu_isset(cpu, tracing_cpumask_new)) { | |
2711 | atomic_inc(&global_trace.data[cpu]->disabled); | |
2712 | } | |
2713 | if (!cpu_isset(cpu, tracing_cpumask) && | |
2714 | cpu_isset(cpu, tracing_cpumask_new)) { | |
2715 | atomic_dec(&global_trace.data[cpu]->disabled); | |
2716 | } | |
2717 | } | |
92205c23 | 2718 | __raw_spin_unlock(&ftrace_max_lock); |
a5e25883 | 2719 | local_irq_enable(); |
36dfe925 IM |
2720 | |
2721 | tracing_cpumask = tracing_cpumask_new; | |
2722 | ||
2723 | mutex_unlock(&tracing_cpumask_update_lock); | |
c7078de1 IM |
2724 | |
2725 | return count; | |
36dfe925 IM |
2726 | |
2727 | err_unlock: | |
2728 | mutex_unlock(&tracing_cpumask_update_lock); | |
2729 | ||
2730 | return err; | |
c7078de1 IM |
2731 | } |
2732 | ||
2733 | static struct file_operations tracing_cpumask_fops = { | |
2734 | .open = tracing_open_generic, | |
2735 | .read = tracing_cpumask_read, | |
2736 | .write = tracing_cpumask_write, | |
bc0c38d1 SR |
2737 | }; |
2738 | ||
2739 | static ssize_t | |
ee6bce52 | 2740 | tracing_trace_options_read(struct file *filp, char __user *ubuf, |
bc0c38d1 SR |
2741 | size_t cnt, loff_t *ppos) |
2742 | { | |
adf9f195 | 2743 | int i; |
bc0c38d1 SR |
2744 | char *buf; |
2745 | int r = 0; | |
2746 | int len = 0; | |
adf9f195 FW |
2747 | u32 tracer_flags = current_trace->flags->val; |
2748 | struct tracer_opt *trace_opts = current_trace->flags->opts; | |
2749 | ||
bc0c38d1 SR |
2750 | |
2751 | /* calulate max size */ | |
2752 | for (i = 0; trace_options[i]; i++) { | |
2753 | len += strlen(trace_options[i]); | |
2754 | len += 3; /* "no" and space */ | |
2755 | } | |
2756 | ||
adf9f195 FW |
2757 | /* |
2758 | * Increase the size with names of options specific | |
2759 | * of the current tracer. | |
2760 | */ | |
2761 | for (i = 0; trace_opts[i].name; i++) { | |
2762 | len += strlen(trace_opts[i].name); | |
2763 | len += 3; /* "no" and space */ | |
2764 | } | |
2765 | ||
bc0c38d1 SR |
2766 | /* +2 for \n and \0 */ |
2767 | buf = kmalloc(len + 2, GFP_KERNEL); | |
2768 | if (!buf) | |
2769 | return -ENOMEM; | |
2770 | ||
2771 | for (i = 0; trace_options[i]; i++) { | |
2772 | if (trace_flags & (1 << i)) | |
2773 | r += sprintf(buf + r, "%s ", trace_options[i]); | |
2774 | else | |
2775 | r += sprintf(buf + r, "no%s ", trace_options[i]); | |
2776 | } | |
2777 | ||
adf9f195 FW |
2778 | for (i = 0; trace_opts[i].name; i++) { |
2779 | if (tracer_flags & trace_opts[i].bit) | |
2780 | r += sprintf(buf + r, "%s ", | |
2781 | trace_opts[i].name); | |
2782 | else | |
2783 | r += sprintf(buf + r, "no%s ", | |
2784 | trace_opts[i].name); | |
2785 | } | |
2786 | ||
bc0c38d1 SR |
2787 | r += sprintf(buf + r, "\n"); |
2788 | WARN_ON(r >= len + 2); | |
2789 | ||
36dfe925 | 2790 | r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
bc0c38d1 SR |
2791 | |
2792 | kfree(buf); | |
2793 | ||
2794 | return r; | |
2795 | } | |
2796 | ||
adf9f195 FW |
2797 | /* Try to assign a tracer specific option */ |
2798 | static int set_tracer_option(struct tracer *trace, char *cmp, int neg) | |
2799 | { | |
2800 | struct tracer_flags *trace_flags = trace->flags; | |
2801 | struct tracer_opt *opts = NULL; | |
2802 | int ret = 0, i = 0; | |
2803 | int len; | |
2804 | ||
2805 | for (i = 0; trace_flags->opts[i].name; i++) { | |
2806 | opts = &trace_flags->opts[i]; | |
2807 | len = strlen(opts->name); | |
2808 | ||
2809 | if (strncmp(cmp, opts->name, len) == 0) { | |
2810 | ret = trace->set_flag(trace_flags->val, | |
2811 | opts->bit, !neg); | |
2812 | break; | |
2813 | } | |
2814 | } | |
2815 | /* Not found */ | |
2816 | if (!trace_flags->opts[i].name) | |
2817 | return -EINVAL; | |
2818 | ||
2819 | /* Refused to handle */ | |
2820 | if (ret) | |
2821 | return ret; | |
2822 | ||
2823 | if (neg) | |
2824 | trace_flags->val &= ~opts->bit; | |
2825 | else | |
2826 | trace_flags->val |= opts->bit; | |
2827 | ||
2828 | return 0; | |
2829 | } | |
2830 | ||
bc0c38d1 | 2831 | static ssize_t |
ee6bce52 | 2832 | tracing_trace_options_write(struct file *filp, const char __user *ubuf, |
bc0c38d1 SR |
2833 | size_t cnt, loff_t *ppos) |
2834 | { | |
2835 | char buf[64]; | |
2836 | char *cmp = buf; | |
2837 | int neg = 0; | |
adf9f195 | 2838 | int ret; |
bc0c38d1 SR |
2839 | int i; |
2840 | ||
cffae437 SR |
2841 | if (cnt >= sizeof(buf)) |
2842 | return -EINVAL; | |
bc0c38d1 SR |
2843 | |
2844 | if (copy_from_user(&buf, ubuf, cnt)) | |
2845 | return -EFAULT; | |
2846 | ||
2847 | buf[cnt] = 0; | |
2848 | ||
2849 | if (strncmp(buf, "no", 2) == 0) { | |
2850 | neg = 1; | |
2851 | cmp += 2; | |
2852 | } | |
2853 | ||
2854 | for (i = 0; trace_options[i]; i++) { | |
2855 | int len = strlen(trace_options[i]); | |
2856 | ||
2857 | if (strncmp(cmp, trace_options[i], len) == 0) { | |
2858 | if (neg) | |
2859 | trace_flags &= ~(1 << i); | |
2860 | else | |
2861 | trace_flags |= (1 << i); | |
2862 | break; | |
2863 | } | |
2864 | } | |
adf9f195 FW |
2865 | |
2866 | /* If no option could be set, test the specific tracer options */ | |
2867 | if (!trace_options[i]) { | |
2868 | ret = set_tracer_option(current_trace, cmp, neg); | |
2869 | if (ret) | |
2870 | return ret; | |
2871 | } | |
bc0c38d1 SR |
2872 | |
2873 | filp->f_pos += cnt; | |
2874 | ||
2875 | return cnt; | |
2876 | } | |
2877 | ||
2878 | static struct file_operations tracing_iter_fops = { | |
c7078de1 | 2879 | .open = tracing_open_generic, |
ee6bce52 SR |
2880 | .read = tracing_trace_options_read, |
2881 | .write = tracing_trace_options_write, | |
bc0c38d1 SR |
2882 | }; |
2883 | ||
7bd2f24c IM |
2884 | static const char readme_msg[] = |
2885 | "tracing mini-HOWTO:\n\n" | |
2886 | "# mkdir /debug\n" | |
2887 | "# mount -t debugfs nodev /debug\n\n" | |
2888 | "# cat /debug/tracing/available_tracers\n" | |
2889 | "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n" | |
2890 | "# cat /debug/tracing/current_tracer\n" | |
2891 | "none\n" | |
2892 | "# echo sched_switch > /debug/tracing/current_tracer\n" | |
2893 | "# cat /debug/tracing/current_tracer\n" | |
2894 | "sched_switch\n" | |
ee6bce52 | 2895 | "# cat /debug/tracing/trace_options\n" |
7bd2f24c | 2896 | "noprint-parent nosym-offset nosym-addr noverbose\n" |
ee6bce52 | 2897 | "# echo print-parent > /debug/tracing/trace_options\n" |
7bd2f24c IM |
2898 | "# echo 1 > /debug/tracing/tracing_enabled\n" |
2899 | "# cat /debug/tracing/trace > /tmp/trace.txt\n" | |
2900 | "echo 0 > /debug/tracing/tracing_enabled\n" | |
2901 | ; | |
2902 | ||
2903 | static ssize_t | |
2904 | tracing_readme_read(struct file *filp, char __user *ubuf, | |
2905 | size_t cnt, loff_t *ppos) | |
2906 | { | |
2907 | return simple_read_from_buffer(ubuf, cnt, ppos, | |
2908 | readme_msg, strlen(readme_msg)); | |
2909 | } | |
2910 | ||
2911 | static struct file_operations tracing_readme_fops = { | |
c7078de1 IM |
2912 | .open = tracing_open_generic, |
2913 | .read = tracing_readme_read, | |
7bd2f24c IM |
2914 | }; |
2915 | ||
bc0c38d1 SR |
2916 | static ssize_t |
2917 | tracing_ctrl_read(struct file *filp, char __user *ubuf, | |
2918 | size_t cnt, loff_t *ppos) | |
2919 | { | |
bc0c38d1 SR |
2920 | char buf[64]; |
2921 | int r; | |
2922 | ||
9036990d | 2923 | r = sprintf(buf, "%u\n", tracer_enabled); |
4e3c3333 | 2924 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
bc0c38d1 SR |
2925 | } |
2926 | ||
2927 | static ssize_t | |
2928 | tracing_ctrl_write(struct file *filp, const char __user *ubuf, | |
2929 | size_t cnt, loff_t *ppos) | |
2930 | { | |
2931 | struct trace_array *tr = filp->private_data; | |
bc0c38d1 | 2932 | char buf[64]; |
c6caeeb1 SR |
2933 | long val; |
2934 | int ret; | |
bc0c38d1 | 2935 | |
cffae437 SR |
2936 | if (cnt >= sizeof(buf)) |
2937 | return -EINVAL; | |
bc0c38d1 SR |
2938 | |
2939 | if (copy_from_user(&buf, ubuf, cnt)) | |
2940 | return -EFAULT; | |
2941 | ||
2942 | buf[cnt] = 0; | |
2943 | ||
c6caeeb1 SR |
2944 | ret = strict_strtoul(buf, 10, &val); |
2945 | if (ret < 0) | |
2946 | return ret; | |
bc0c38d1 SR |
2947 | |
2948 | val = !!val; | |
2949 | ||
2950 | mutex_lock(&trace_types_lock); | |
9036990d SR |
2951 | if (tracer_enabled ^ val) { |
2952 | if (val) { | |
bc0c38d1 | 2953 | tracer_enabled = 1; |
9036990d SR |
2954 | if (current_trace->start) |
2955 | current_trace->start(tr); | |
2956 | tracing_start(); | |
2957 | } else { | |
bc0c38d1 | 2958 | tracer_enabled = 0; |
9036990d SR |
2959 | tracing_stop(); |
2960 | if (current_trace->stop) | |
2961 | current_trace->stop(tr); | |
2962 | } | |
bc0c38d1 SR |
2963 | } |
2964 | mutex_unlock(&trace_types_lock); | |
2965 | ||
2966 | filp->f_pos += cnt; | |
2967 | ||
2968 | return cnt; | |
2969 | } | |
2970 | ||
2971 | static ssize_t | |
2972 | tracing_set_trace_read(struct file *filp, char __user *ubuf, | |
2973 | size_t cnt, loff_t *ppos) | |
2974 | { | |
2975 | char buf[max_tracer_type_len+2]; | |
2976 | int r; | |
2977 | ||
2978 | mutex_lock(&trace_types_lock); | |
2979 | if (current_trace) | |
2980 | r = sprintf(buf, "%s\n", current_trace->name); | |
2981 | else | |
2982 | r = sprintf(buf, "\n"); | |
2983 | mutex_unlock(&trace_types_lock); | |
2984 | ||
4bf39a94 | 2985 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
bc0c38d1 SR |
2986 | } |
2987 | ||
d9e54076 | 2988 | static int tracing_set_tracer(char *buf) |
bc0c38d1 SR |
2989 | { |
2990 | struct trace_array *tr = &global_trace; | |
2991 | struct tracer *t; | |
d9e54076 | 2992 | int ret = 0; |
bc0c38d1 SR |
2993 | |
2994 | mutex_lock(&trace_types_lock); | |
2995 | for (t = trace_types; t; t = t->next) { | |
2996 | if (strcmp(t->name, buf) == 0) | |
2997 | break; | |
2998 | } | |
c2931e05 FW |
2999 | if (!t) { |
3000 | ret = -EINVAL; | |
3001 | goto out; | |
3002 | } | |
3003 | if (t == current_trace) | |
bc0c38d1 SR |
3004 | goto out; |
3005 | ||
9f029e83 | 3006 | trace_branch_disable(); |
bc0c38d1 SR |
3007 | if (current_trace && current_trace->reset) |
3008 | current_trace->reset(tr); | |
3009 | ||
3010 | current_trace = t; | |
1c80025a FW |
3011 | if (t->init) { |
3012 | ret = t->init(tr); | |
3013 | if (ret) | |
3014 | goto out; | |
3015 | } | |
bc0c38d1 | 3016 | |
9f029e83 | 3017 | trace_branch_enable(tr); |
bc0c38d1 SR |
3018 | out: |
3019 | mutex_unlock(&trace_types_lock); | |
3020 | ||
d9e54076 PZ |
3021 | return ret; |
3022 | } | |
3023 | ||
3024 | static ssize_t | |
3025 | tracing_set_trace_write(struct file *filp, const char __user *ubuf, | |
3026 | size_t cnt, loff_t *ppos) | |
3027 | { | |
3028 | char buf[max_tracer_type_len+1]; | |
3029 | int i; | |
3030 | size_t ret; | |
e6e7a65a FW |
3031 | int err; |
3032 | ||
3033 | ret = cnt; | |
d9e54076 PZ |
3034 | |
3035 | if (cnt > max_tracer_type_len) | |
3036 | cnt = max_tracer_type_len; | |
3037 | ||
3038 | if (copy_from_user(&buf, ubuf, cnt)) | |
3039 | return -EFAULT; | |
3040 | ||
3041 | buf[cnt] = 0; | |
3042 | ||
3043 | /* strip ending whitespace. */ | |
3044 | for (i = cnt - 1; i > 0 && isspace(buf[i]); i--) | |
3045 | buf[i] = 0; | |
3046 | ||
e6e7a65a FW |
3047 | err = tracing_set_tracer(buf); |
3048 | if (err) | |
3049 | return err; | |
d9e54076 | 3050 | |
e6e7a65a | 3051 | filp->f_pos += ret; |
bc0c38d1 | 3052 | |
c2931e05 | 3053 | return ret; |
bc0c38d1 SR |
3054 | } |
3055 | ||
3056 | static ssize_t | |
3057 | tracing_max_lat_read(struct file *filp, char __user *ubuf, | |
3058 | size_t cnt, loff_t *ppos) | |
3059 | { | |
3060 | unsigned long *ptr = filp->private_data; | |
3061 | char buf[64]; | |
3062 | int r; | |
3063 | ||
cffae437 | 3064 | r = snprintf(buf, sizeof(buf), "%ld\n", |
bc0c38d1 | 3065 | *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr)); |
cffae437 SR |
3066 | if (r > sizeof(buf)) |
3067 | r = sizeof(buf); | |
4bf39a94 | 3068 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
bc0c38d1 SR |
3069 | } |
3070 | ||
3071 | static ssize_t | |
3072 | tracing_max_lat_write(struct file *filp, const char __user *ubuf, | |
3073 | size_t cnt, loff_t *ppos) | |
3074 | { | |
3075 | long *ptr = filp->private_data; | |
bc0c38d1 | 3076 | char buf[64]; |
c6caeeb1 SR |
3077 | long val; |
3078 | int ret; | |
bc0c38d1 | 3079 | |
cffae437 SR |
3080 | if (cnt >= sizeof(buf)) |
3081 | return -EINVAL; | |
bc0c38d1 SR |
3082 | |
3083 | if (copy_from_user(&buf, ubuf, cnt)) | |
3084 | return -EFAULT; | |
3085 | ||
3086 | buf[cnt] = 0; | |
3087 | ||
c6caeeb1 SR |
3088 | ret = strict_strtoul(buf, 10, &val); |
3089 | if (ret < 0) | |
3090 | return ret; | |
bc0c38d1 SR |
3091 | |
3092 | *ptr = val * 1000; | |
3093 | ||
3094 | return cnt; | |
3095 | } | |
3096 | ||
b3806b43 SR |
3097 | static atomic_t tracing_reader; |
3098 | ||
3099 | static int tracing_open_pipe(struct inode *inode, struct file *filp) | |
3100 | { | |
3101 | struct trace_iterator *iter; | |
3102 | ||
3103 | if (tracing_disabled) | |
3104 | return -ENODEV; | |
3105 | ||
3106 | /* We only allow for reader of the pipe */ | |
3107 | if (atomic_inc_return(&tracing_reader) != 1) { | |
3108 | atomic_dec(&tracing_reader); | |
3109 | return -EBUSY; | |
3110 | } | |
3111 | ||
3112 | /* create a buffer to store the information to pass to userspace */ | |
3113 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); | |
3114 | if (!iter) | |
3115 | return -ENOMEM; | |
3116 | ||
107bad8b | 3117 | mutex_lock(&trace_types_lock); |
a309720c SR |
3118 | |
3119 | /* trace pipe does not show start of buffer */ | |
3120 | cpus_setall(iter->started); | |
3121 | ||
b3806b43 | 3122 | iter->tr = &global_trace; |
72829bc3 | 3123 | iter->trace = current_trace; |
b3806b43 SR |
3124 | filp->private_data = iter; |
3125 | ||
107bad8b SR |
3126 | if (iter->trace->pipe_open) |
3127 | iter->trace->pipe_open(iter); | |
3128 | mutex_unlock(&trace_types_lock); | |
3129 | ||
b3806b43 SR |
3130 | return 0; |
3131 | } | |
3132 | ||
3133 | static int tracing_release_pipe(struct inode *inode, struct file *file) | |
3134 | { | |
3135 | struct trace_iterator *iter = file->private_data; | |
3136 | ||
3137 | kfree(iter); | |
3138 | atomic_dec(&tracing_reader); | |
3139 | ||
3140 | return 0; | |
3141 | } | |
3142 | ||
2a2cc8f7 SSP |
3143 | static unsigned int |
3144 | tracing_poll_pipe(struct file *filp, poll_table *poll_table) | |
3145 | { | |
3146 | struct trace_iterator *iter = filp->private_data; | |
3147 | ||
3148 | if (trace_flags & TRACE_ITER_BLOCK) { | |
3149 | /* | |
3150 | * Always select as readable when in blocking mode | |
3151 | */ | |
3152 | return POLLIN | POLLRDNORM; | |
afc2abc0 | 3153 | } else { |
2a2cc8f7 SSP |
3154 | if (!trace_empty(iter)) |
3155 | return POLLIN | POLLRDNORM; | |
3156 | poll_wait(filp, &trace_wait, poll_table); | |
3157 | if (!trace_empty(iter)) | |
3158 | return POLLIN | POLLRDNORM; | |
3159 | ||
3160 | return 0; | |
3161 | } | |
3162 | } | |
3163 | ||
b3806b43 SR |
3164 | /* |
3165 | * Consumer reader. | |
3166 | */ | |
3167 | static ssize_t | |
3168 | tracing_read_pipe(struct file *filp, char __user *ubuf, | |
3169 | size_t cnt, loff_t *ppos) | |
3170 | { | |
3171 | struct trace_iterator *iter = filp->private_data; | |
6c6c2796 | 3172 | ssize_t sret; |
b3806b43 SR |
3173 | |
3174 | /* return any leftover data */ | |
6c6c2796 PP |
3175 | sret = trace_seq_to_user(&iter->seq, ubuf, cnt); |
3176 | if (sret != -EBUSY) | |
3177 | return sret; | |
b3806b43 | 3178 | |
6c6c2796 | 3179 | trace_seq_reset(&iter->seq); |
b3806b43 | 3180 | |
107bad8b SR |
3181 | mutex_lock(&trace_types_lock); |
3182 | if (iter->trace->read) { | |
6c6c2796 PP |
3183 | sret = iter->trace->read(iter, filp, ubuf, cnt, ppos); |
3184 | if (sret) | |
107bad8b | 3185 | goto out; |
107bad8b SR |
3186 | } |
3187 | ||
9ff4b974 PP |
3188 | waitagain: |
3189 | sret = 0; | |
b3806b43 | 3190 | while (trace_empty(iter)) { |
2dc8f095 | 3191 | |
107bad8b | 3192 | if ((filp->f_flags & O_NONBLOCK)) { |
6c6c2796 | 3193 | sret = -EAGAIN; |
107bad8b SR |
3194 | goto out; |
3195 | } | |
2dc8f095 | 3196 | |
b3806b43 SR |
3197 | /* |
3198 | * This is a make-shift waitqueue. The reason we don't use | |
3199 | * an actual wait queue is because: | |
3200 | * 1) we only ever have one waiter | |
3201 | * 2) the tracing, traces all functions, we don't want | |
3202 | * the overhead of calling wake_up and friends | |
3203 | * (and tracing them too) | |
3204 | * Anyway, this is really very primitive wakeup. | |
3205 | */ | |
3206 | set_current_state(TASK_INTERRUPTIBLE); | |
3207 | iter->tr->waiter = current; | |
3208 | ||
107bad8b SR |
3209 | mutex_unlock(&trace_types_lock); |
3210 | ||
9fe068e9 IM |
3211 | /* sleep for 100 msecs, and try again. */ |
3212 | schedule_timeout(HZ/10); | |
b3806b43 | 3213 | |
107bad8b SR |
3214 | mutex_lock(&trace_types_lock); |
3215 | ||
b3806b43 SR |
3216 | iter->tr->waiter = NULL; |
3217 | ||
107bad8b | 3218 | if (signal_pending(current)) { |
6c6c2796 | 3219 | sret = -EINTR; |
107bad8b SR |
3220 | goto out; |
3221 | } | |
b3806b43 | 3222 | |
84527997 | 3223 | if (iter->trace != current_trace) |
107bad8b | 3224 | goto out; |
84527997 | 3225 | |
b3806b43 SR |
3226 | /* |
3227 | * We block until we read something and tracing is disabled. | |
3228 | * We still block if tracing is disabled, but we have never | |
3229 | * read anything. This allows a user to cat this file, and | |
3230 | * then enable tracing. But after we have read something, | |
3231 | * we give an EOF when tracing is again disabled. | |
3232 | * | |
3233 | * iter->pos will be 0 if we haven't read anything. | |
3234 | */ | |
3235 | if (!tracer_enabled && iter->pos) | |
3236 | break; | |
3237 | ||
3238 | continue; | |
3239 | } | |
3240 | ||
3241 | /* stop when tracing is finished */ | |
3242 | if (trace_empty(iter)) | |
107bad8b | 3243 | goto out; |
b3806b43 SR |
3244 | |
3245 | if (cnt >= PAGE_SIZE) | |
3246 | cnt = PAGE_SIZE - 1; | |
3247 | ||
53d0aa77 | 3248 | /* reset all but tr, trace, and overruns */ |
53d0aa77 SR |
3249 | memset(&iter->seq, 0, |
3250 | sizeof(struct trace_iterator) - | |
3251 | offsetof(struct trace_iterator, seq)); | |
4823ed7e | 3252 | iter->pos = -1; |
b3806b43 | 3253 | |
088b1e42 | 3254 | while (find_next_entry_inc(iter) != NULL) { |
2c4f035f | 3255 | enum print_line_t ret; |
088b1e42 SR |
3256 | int len = iter->seq.len; |
3257 | ||
f9896bf3 | 3258 | ret = print_trace_line(iter); |
2c4f035f | 3259 | if (ret == TRACE_TYPE_PARTIAL_LINE) { |
088b1e42 SR |
3260 | /* don't print partial lines */ |
3261 | iter->seq.len = len; | |
b3806b43 | 3262 | break; |
088b1e42 | 3263 | } |
b3806b43 SR |
3264 | |
3265 | trace_consume(iter); | |
3266 | ||
3267 | if (iter->seq.len >= cnt) | |
3268 | break; | |
b3806b43 SR |
3269 | } |
3270 | ||
b3806b43 | 3271 | /* Now copy what we have to the user */ |
6c6c2796 PP |
3272 | sret = trace_seq_to_user(&iter->seq, ubuf, cnt); |
3273 | if (iter->seq.readpos >= iter->seq.len) | |
b3806b43 | 3274 | trace_seq_reset(&iter->seq); |
9ff4b974 PP |
3275 | |
3276 | /* | |
3277 | * If there was nothing to send to user, inspite of consuming trace | |
3278 | * entries, go back to wait for more entries. | |
3279 | */ | |
6c6c2796 | 3280 | if (sret == -EBUSY) |
9ff4b974 | 3281 | goto waitagain; |
b3806b43 | 3282 | |
107bad8b SR |
3283 | out: |
3284 | mutex_unlock(&trace_types_lock); | |
3285 | ||
6c6c2796 | 3286 | return sret; |
b3806b43 SR |
3287 | } |
3288 | ||
a98a3c3f SR |
3289 | static ssize_t |
3290 | tracing_entries_read(struct file *filp, char __user *ubuf, | |
3291 | size_t cnt, loff_t *ppos) | |
3292 | { | |
3293 | struct trace_array *tr = filp->private_data; | |
3294 | char buf[64]; | |
3295 | int r; | |
3296 | ||
1696b2b0 | 3297 | r = sprintf(buf, "%lu\n", tr->entries >> 10); |
a98a3c3f SR |
3298 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
3299 | } | |
3300 | ||
3301 | static ssize_t | |
3302 | tracing_entries_write(struct file *filp, const char __user *ubuf, | |
3303 | size_t cnt, loff_t *ppos) | |
3304 | { | |
3305 | unsigned long val; | |
3306 | char buf[64]; | |
bf5e6519 | 3307 | int ret, cpu; |
a98a3c3f | 3308 | |
cffae437 SR |
3309 | if (cnt >= sizeof(buf)) |
3310 | return -EINVAL; | |
a98a3c3f SR |
3311 | |
3312 | if (copy_from_user(&buf, ubuf, cnt)) | |
3313 | return -EFAULT; | |
3314 | ||
3315 | buf[cnt] = 0; | |
3316 | ||
c6caeeb1 SR |
3317 | ret = strict_strtoul(buf, 10, &val); |
3318 | if (ret < 0) | |
3319 | return ret; | |
a98a3c3f SR |
3320 | |
3321 | /* must have at least 1 entry */ | |
3322 | if (!val) | |
3323 | return -EINVAL; | |
3324 | ||
3325 | mutex_lock(&trace_types_lock); | |
3326 | ||
c76f0694 | 3327 | tracing_stop(); |
a98a3c3f | 3328 | |
bf5e6519 SR |
3329 | /* disable all cpu buffers */ |
3330 | for_each_tracing_cpu(cpu) { | |
3331 | if (global_trace.data[cpu]) | |
3332 | atomic_inc(&global_trace.data[cpu]->disabled); | |
3333 | if (max_tr.data[cpu]) | |
3334 | atomic_inc(&max_tr.data[cpu]->disabled); | |
3335 | } | |
3336 | ||
1696b2b0 SR |
3337 | /* value is in KB */ |
3338 | val <<= 10; | |
3339 | ||
3928a8a2 SR |
3340 | if (val != global_trace.entries) { |
3341 | ret = ring_buffer_resize(global_trace.buffer, val); | |
3342 | if (ret < 0) { | |
3343 | cnt = ret; | |
3eefae99 SR |
3344 | goto out; |
3345 | } | |
3346 | ||
3928a8a2 SR |
3347 | ret = ring_buffer_resize(max_tr.buffer, val); |
3348 | if (ret < 0) { | |
3349 | int r; | |
3350 | cnt = ret; | |
3351 | r = ring_buffer_resize(global_trace.buffer, | |
3352 | global_trace.entries); | |
3353 | if (r < 0) { | |
3354 | /* AARGH! We are left with different | |
3355 | * size max buffer!!!! */ | |
3356 | WARN_ON(1); | |
3357 | tracing_disabled = 1; | |
a98a3c3f | 3358 | } |
3928a8a2 | 3359 | goto out; |
a98a3c3f | 3360 | } |
3eefae99 | 3361 | |
3928a8a2 | 3362 | global_trace.entries = val; |
a98a3c3f SR |
3363 | } |
3364 | ||
3365 | filp->f_pos += cnt; | |
3366 | ||
19384c03 SR |
3367 | /* If check pages failed, return ENOMEM */ |
3368 | if (tracing_disabled) | |
3369 | cnt = -ENOMEM; | |
a98a3c3f | 3370 | out: |
bf5e6519 SR |
3371 | for_each_tracing_cpu(cpu) { |
3372 | if (global_trace.data[cpu]) | |
3373 | atomic_dec(&global_trace.data[cpu]->disabled); | |
3374 | if (max_tr.data[cpu]) | |
3375 | atomic_dec(&max_tr.data[cpu]->disabled); | |
3376 | } | |
3377 | ||
c76f0694 | 3378 | tracing_start(); |
a98a3c3f SR |
3379 | max_tr.entries = global_trace.entries; |
3380 | mutex_unlock(&trace_types_lock); | |
3381 | ||
3382 | return cnt; | |
3383 | } | |
3384 | ||
5bf9a1ee PP |
3385 | static int mark_printk(const char *fmt, ...) |
3386 | { | |
3387 | int ret; | |
3388 | va_list args; | |
3389 | va_start(args, fmt); | |
1fd8f2a3 | 3390 | ret = trace_vprintk(0, -1, fmt, args); |
5bf9a1ee PP |
3391 | va_end(args); |
3392 | return ret; | |
3393 | } | |
3394 | ||
3395 | static ssize_t | |
3396 | tracing_mark_write(struct file *filp, const char __user *ubuf, | |
3397 | size_t cnt, loff_t *fpos) | |
3398 | { | |
3399 | char *buf; | |
3400 | char *end; | |
5bf9a1ee | 3401 | |
c76f0694 | 3402 | if (tracing_disabled) |
5bf9a1ee PP |
3403 | return -EINVAL; |
3404 | ||
3405 | if (cnt > TRACE_BUF_SIZE) | |
3406 | cnt = TRACE_BUF_SIZE; | |
3407 | ||
3408 | buf = kmalloc(cnt + 1, GFP_KERNEL); | |
3409 | if (buf == NULL) | |
3410 | return -ENOMEM; | |
3411 | ||
3412 | if (copy_from_user(buf, ubuf, cnt)) { | |
3413 | kfree(buf); | |
3414 | return -EFAULT; | |
3415 | } | |
3416 | ||
3417 | /* Cut from the first nil or newline. */ | |
3418 | buf[cnt] = '\0'; | |
3419 | end = strchr(buf, '\n'); | |
3420 | if (end) | |
3421 | *end = '\0'; | |
3422 | ||
3423 | cnt = mark_printk("%s\n", buf); | |
3424 | kfree(buf); | |
3425 | *fpos += cnt; | |
3426 | ||
3427 | return cnt; | |
3428 | } | |
3429 | ||
bc0c38d1 | 3430 | static struct file_operations tracing_max_lat_fops = { |
4bf39a94 IM |
3431 | .open = tracing_open_generic, |
3432 | .read = tracing_max_lat_read, | |
3433 | .write = tracing_max_lat_write, | |
bc0c38d1 SR |
3434 | }; |
3435 | ||
3436 | static struct file_operations tracing_ctrl_fops = { | |
4bf39a94 IM |
3437 | .open = tracing_open_generic, |
3438 | .read = tracing_ctrl_read, | |
3439 | .write = tracing_ctrl_write, | |
bc0c38d1 SR |
3440 | }; |
3441 | ||
3442 | static struct file_operations set_tracer_fops = { | |
4bf39a94 IM |
3443 | .open = tracing_open_generic, |
3444 | .read = tracing_set_trace_read, | |
3445 | .write = tracing_set_trace_write, | |
bc0c38d1 SR |
3446 | }; |
3447 | ||
b3806b43 | 3448 | static struct file_operations tracing_pipe_fops = { |
4bf39a94 | 3449 | .open = tracing_open_pipe, |
2a2cc8f7 | 3450 | .poll = tracing_poll_pipe, |
4bf39a94 IM |
3451 | .read = tracing_read_pipe, |
3452 | .release = tracing_release_pipe, | |
b3806b43 SR |
3453 | }; |
3454 | ||
a98a3c3f SR |
3455 | static struct file_operations tracing_entries_fops = { |
3456 | .open = tracing_open_generic, | |
3457 | .read = tracing_entries_read, | |
3458 | .write = tracing_entries_write, | |
3459 | }; | |
3460 | ||
5bf9a1ee | 3461 | static struct file_operations tracing_mark_fops = { |
43a15386 | 3462 | .open = tracing_open_generic, |
5bf9a1ee PP |
3463 | .write = tracing_mark_write, |
3464 | }; | |
3465 | ||
bc0c38d1 SR |
3466 | #ifdef CONFIG_DYNAMIC_FTRACE |
3467 | ||
b807c3d0 SR |
3468 | int __weak ftrace_arch_read_dyn_info(char *buf, int size) |
3469 | { | |
3470 | return 0; | |
3471 | } | |
3472 | ||
bc0c38d1 | 3473 | static ssize_t |
b807c3d0 | 3474 | tracing_read_dyn_info(struct file *filp, char __user *ubuf, |
bc0c38d1 SR |
3475 | size_t cnt, loff_t *ppos) |
3476 | { | |
a26a2a27 SR |
3477 | static char ftrace_dyn_info_buffer[1024]; |
3478 | static DEFINE_MUTEX(dyn_info_mutex); | |
bc0c38d1 | 3479 | unsigned long *p = filp->private_data; |
b807c3d0 | 3480 | char *buf = ftrace_dyn_info_buffer; |
a26a2a27 | 3481 | int size = ARRAY_SIZE(ftrace_dyn_info_buffer); |
bc0c38d1 SR |
3482 | int r; |
3483 | ||
b807c3d0 SR |
3484 | mutex_lock(&dyn_info_mutex); |
3485 | r = sprintf(buf, "%ld ", *p); | |
4bf39a94 | 3486 | |
a26a2a27 | 3487 | r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r); |
b807c3d0 SR |
3488 | buf[r++] = '\n'; |
3489 | ||
3490 | r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); | |
3491 | ||
3492 | mutex_unlock(&dyn_info_mutex); | |
3493 | ||
3494 | return r; | |
bc0c38d1 SR |
3495 | } |
3496 | ||
b807c3d0 | 3497 | static struct file_operations tracing_dyn_info_fops = { |
4bf39a94 | 3498 | .open = tracing_open_generic, |
b807c3d0 | 3499 | .read = tracing_read_dyn_info, |
bc0c38d1 SR |
3500 | }; |
3501 | #endif | |
3502 | ||
3503 | static struct dentry *d_tracer; | |
3504 | ||
3505 | struct dentry *tracing_init_dentry(void) | |
3506 | { | |
3507 | static int once; | |
3508 | ||
3509 | if (d_tracer) | |
3510 | return d_tracer; | |
3511 | ||
3512 | d_tracer = debugfs_create_dir("tracing", NULL); | |
3513 | ||
3514 | if (!d_tracer && !once) { | |
3515 | once = 1; | |
3516 | pr_warning("Could not create debugfs directory 'tracing'\n"); | |
3517 | return NULL; | |
3518 | } | |
3519 | ||
3520 | return d_tracer; | |
3521 | } | |
3522 | ||
60a11774 SR |
3523 | #ifdef CONFIG_FTRACE_SELFTEST |
3524 | /* Let selftest have access to static functions in this file */ | |
3525 | #include "trace_selftest.c" | |
3526 | #endif | |
3527 | ||
b5ad384e | 3528 | static __init int tracer_init_debugfs(void) |
bc0c38d1 SR |
3529 | { |
3530 | struct dentry *d_tracer; | |
3531 | struct dentry *entry; | |
3532 | ||
3533 | d_tracer = tracing_init_dentry(); | |
3534 | ||
3535 | entry = debugfs_create_file("tracing_enabled", 0644, d_tracer, | |
3536 | &global_trace, &tracing_ctrl_fops); | |
3537 | if (!entry) | |
3538 | pr_warning("Could not create debugfs 'tracing_enabled' entry\n"); | |
3539 | ||
ee6bce52 | 3540 | entry = debugfs_create_file("trace_options", 0644, d_tracer, |
bc0c38d1 SR |
3541 | NULL, &tracing_iter_fops); |
3542 | if (!entry) | |
ee6bce52 | 3543 | pr_warning("Could not create debugfs 'trace_options' entry\n"); |
bc0c38d1 | 3544 | |
c7078de1 IM |
3545 | entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer, |
3546 | NULL, &tracing_cpumask_fops); | |
3547 | if (!entry) | |
3548 | pr_warning("Could not create debugfs 'tracing_cpumask' entry\n"); | |
3549 | ||
bc0c38d1 SR |
3550 | entry = debugfs_create_file("latency_trace", 0444, d_tracer, |
3551 | &global_trace, &tracing_lt_fops); | |
3552 | if (!entry) | |
3553 | pr_warning("Could not create debugfs 'latency_trace' entry\n"); | |
3554 | ||
3555 | entry = debugfs_create_file("trace", 0444, d_tracer, | |
3556 | &global_trace, &tracing_fops); | |
3557 | if (!entry) | |
3558 | pr_warning("Could not create debugfs 'trace' entry\n"); | |
3559 | ||
3560 | entry = debugfs_create_file("available_tracers", 0444, d_tracer, | |
3561 | &global_trace, &show_traces_fops); | |
3562 | if (!entry) | |
98a983aa | 3563 | pr_warning("Could not create debugfs 'available_tracers' entry\n"); |
bc0c38d1 SR |
3564 | |
3565 | entry = debugfs_create_file("current_tracer", 0444, d_tracer, | |
3566 | &global_trace, &set_tracer_fops); | |
3567 | if (!entry) | |
98a983aa | 3568 | pr_warning("Could not create debugfs 'current_tracer' entry\n"); |
bc0c38d1 SR |
3569 | |
3570 | entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer, | |
3571 | &tracing_max_latency, | |
3572 | &tracing_max_lat_fops); | |
3573 | if (!entry) | |
3574 | pr_warning("Could not create debugfs " | |
3575 | "'tracing_max_latency' entry\n"); | |
3576 | ||
3577 | entry = debugfs_create_file("tracing_thresh", 0644, d_tracer, | |
3578 | &tracing_thresh, &tracing_max_lat_fops); | |
3579 | if (!entry) | |
3580 | pr_warning("Could not create debugfs " | |
98a983aa | 3581 | "'tracing_thresh' entry\n"); |
7bd2f24c IM |
3582 | entry = debugfs_create_file("README", 0644, d_tracer, |
3583 | NULL, &tracing_readme_fops); | |
3584 | if (!entry) | |
3585 | pr_warning("Could not create debugfs 'README' entry\n"); | |
3586 | ||
b3806b43 SR |
3587 | entry = debugfs_create_file("trace_pipe", 0644, d_tracer, |
3588 | NULL, &tracing_pipe_fops); | |
3589 | if (!entry) | |
3590 | pr_warning("Could not create debugfs " | |
98a983aa | 3591 | "'trace_pipe' entry\n"); |
bc0c38d1 | 3592 | |
a94c80e7 | 3593 | entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer, |
a98a3c3f SR |
3594 | &global_trace, &tracing_entries_fops); |
3595 | if (!entry) | |
3596 | pr_warning("Could not create debugfs " | |
a94c80e7 | 3597 | "'buffer_size_kb' entry\n"); |
a98a3c3f | 3598 | |
5bf9a1ee PP |
3599 | entry = debugfs_create_file("trace_marker", 0220, d_tracer, |
3600 | NULL, &tracing_mark_fops); | |
3601 | if (!entry) | |
3602 | pr_warning("Could not create debugfs " | |
3603 | "'trace_marker' entry\n"); | |
3604 | ||
bc0c38d1 SR |
3605 | #ifdef CONFIG_DYNAMIC_FTRACE |
3606 | entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer, | |
3607 | &ftrace_update_tot_cnt, | |
b807c3d0 | 3608 | &tracing_dyn_info_fops); |
bc0c38d1 SR |
3609 | if (!entry) |
3610 | pr_warning("Could not create debugfs " | |
3611 | "'dyn_ftrace_total_info' entry\n"); | |
3612 | #endif | |
d618b3e6 IM |
3613 | #ifdef CONFIG_SYSPROF_TRACER |
3614 | init_tracer_sysprof_debugfs(d_tracer); | |
3615 | #endif | |
b5ad384e | 3616 | return 0; |
bc0c38d1 SR |
3617 | } |
3618 | ||
1fd8f2a3 | 3619 | int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args) |
dd0e545f | 3620 | { |
dd0e545f SR |
3621 | static DEFINE_SPINLOCK(trace_buf_lock); |
3622 | static char trace_buf[TRACE_BUF_SIZE]; | |
f09ce573 | 3623 | |
3928a8a2 | 3624 | struct ring_buffer_event *event; |
f09ce573 | 3625 | struct trace_array *tr = &global_trace; |
dd0e545f | 3626 | struct trace_array_cpu *data; |
38697053 | 3627 | int cpu, len = 0, size, pc; |
e726f5f9 IM |
3628 | struct print_entry *entry; |
3629 | unsigned long irq_flags; | |
dd0e545f | 3630 | |
8e1b82e0 | 3631 | if (tracing_disabled || tracing_selftest_running) |
dd0e545f SR |
3632 | return 0; |
3633 | ||
38697053 SR |
3634 | pc = preempt_count(); |
3635 | preempt_disable_notrace(); | |
dd0e545f SR |
3636 | cpu = raw_smp_processor_id(); |
3637 | data = tr->data[cpu]; | |
dd0e545f | 3638 | |
3ea2e6d7 | 3639 | if (unlikely(atomic_read(&data->disabled))) |
dd0e545f SR |
3640 | goto out; |
3641 | ||
380c4b14 FW |
3642 | pause_graph_tracing(); |
3643 | spin_lock_irqsave(&trace_buf_lock, irq_flags); | |
801fe400 | 3644 | len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args); |
dd0e545f SR |
3645 | |
3646 | len = min(len, TRACE_BUF_SIZE-1); | |
3647 | trace_buf[len] = 0; | |
3648 | ||
777e208d SR |
3649 | size = sizeof(*entry) + len + 1; |
3650 | event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags); | |
3928a8a2 SR |
3651 | if (!event) |
3652 | goto out_unlock; | |
777e208d | 3653 | entry = ring_buffer_event_data(event); |
e726f5f9 | 3654 | tracing_generic_entry_update(&entry->ent, irq_flags, pc); |
777e208d SR |
3655 | entry->ent.type = TRACE_PRINT; |
3656 | entry->ip = ip; | |
1fd8f2a3 | 3657 | entry->depth = depth; |
dd0e545f | 3658 | |
777e208d SR |
3659 | memcpy(&entry->buf, trace_buf, len); |
3660 | entry->buf[len] = 0; | |
3928a8a2 | 3661 | ring_buffer_unlock_commit(tr->buffer, event, irq_flags); |
dd0e545f | 3662 | |
3928a8a2 | 3663 | out_unlock: |
380c4b14 FW |
3664 | spin_unlock_irqrestore(&trace_buf_lock, irq_flags); |
3665 | unpause_graph_tracing(); | |
dd0e545f | 3666 | out: |
38697053 | 3667 | preempt_enable_notrace(); |
dd0e545f SR |
3668 | |
3669 | return len; | |
3670 | } | |
801fe400 PP |
3671 | EXPORT_SYMBOL_GPL(trace_vprintk); |
3672 | ||
3673 | int __ftrace_printk(unsigned long ip, const char *fmt, ...) | |
3674 | { | |
3675 | int ret; | |
3676 | va_list ap; | |
3677 | ||
3678 | if (!(trace_flags & TRACE_ITER_PRINTK)) | |
3679 | return 0; | |
3680 | ||
3681 | va_start(ap, fmt); | |
21a8c466 | 3682 | ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap); |
801fe400 PP |
3683 | va_end(ap); |
3684 | return ret; | |
3685 | } | |
dd0e545f SR |
3686 | EXPORT_SYMBOL_GPL(__ftrace_printk); |
3687 | ||
3f5a54e3 SR |
3688 | static int trace_panic_handler(struct notifier_block *this, |
3689 | unsigned long event, void *unused) | |
3690 | { | |
944ac425 SR |
3691 | if (ftrace_dump_on_oops) |
3692 | ftrace_dump(); | |
3f5a54e3 SR |
3693 | return NOTIFY_OK; |
3694 | } | |
3695 | ||
3696 | static struct notifier_block trace_panic_notifier = { | |
3697 | .notifier_call = trace_panic_handler, | |
3698 | .next = NULL, | |
3699 | .priority = 150 /* priority: INT_MAX >= x >= 0 */ | |
3700 | }; | |
3701 | ||
3702 | static int trace_die_handler(struct notifier_block *self, | |
3703 | unsigned long val, | |
3704 | void *data) | |
3705 | { | |
3706 | switch (val) { | |
3707 | case DIE_OOPS: | |
944ac425 SR |
3708 | if (ftrace_dump_on_oops) |
3709 | ftrace_dump(); | |
3f5a54e3 SR |
3710 | break; |
3711 | default: | |
3712 | break; | |
3713 | } | |
3714 | return NOTIFY_OK; | |
3715 | } | |
3716 | ||
3717 | static struct notifier_block trace_die_notifier = { | |
3718 | .notifier_call = trace_die_handler, | |
3719 | .priority = 200 | |
3720 | }; | |
3721 | ||
3722 | /* | |
3723 | * printk is set to max of 1024, we really don't need it that big. | |
3724 | * Nothing should be printing 1000 characters anyway. | |
3725 | */ | |
3726 | #define TRACE_MAX_PRINT 1000 | |
3727 | ||
3728 | /* | |
3729 | * Define here KERN_TRACE so that we have one place to modify | |
3730 | * it if we decide to change what log level the ftrace dump | |
3731 | * should be at. | |
3732 | */ | |
3733 | #define KERN_TRACE KERN_INFO | |
3734 | ||
3735 | static void | |
3736 | trace_printk_seq(struct trace_seq *s) | |
3737 | { | |
3738 | /* Probably should print a warning here. */ | |
3739 | if (s->len >= 1000) | |
3740 | s->len = 1000; | |
3741 | ||
3742 | /* should be zero ended, but we are paranoid. */ | |
3743 | s->buffer[s->len] = 0; | |
3744 | ||
3745 | printk(KERN_TRACE "%s", s->buffer); | |
3746 | ||
3747 | trace_seq_reset(s); | |
3748 | } | |
3749 | ||
3f5a54e3 SR |
3750 | void ftrace_dump(void) |
3751 | { | |
3752 | static DEFINE_SPINLOCK(ftrace_dump_lock); | |
3753 | /* use static because iter can be a bit big for the stack */ | |
3754 | static struct trace_iterator iter; | |
3f5a54e3 SR |
3755 | static cpumask_t mask; |
3756 | static int dump_ran; | |
d769041f SR |
3757 | unsigned long flags; |
3758 | int cnt = 0, cpu; | |
3f5a54e3 SR |
3759 | |
3760 | /* only one dump */ | |
3761 | spin_lock_irqsave(&ftrace_dump_lock, flags); | |
3762 | if (dump_ran) | |
3763 | goto out; | |
3764 | ||
3765 | dump_ran = 1; | |
3766 | ||
3767 | /* No turning back! */ | |
81adbdc0 | 3768 | ftrace_kill(); |
3f5a54e3 | 3769 | |
d769041f SR |
3770 | for_each_tracing_cpu(cpu) { |
3771 | atomic_inc(&global_trace.data[cpu]->disabled); | |
3772 | } | |
3773 | ||
b54d3de9 TE |
3774 | /* don't look at user memory in panic mode */ |
3775 | trace_flags &= ~TRACE_ITER_SYM_USEROBJ; | |
3776 | ||
3f5a54e3 SR |
3777 | printk(KERN_TRACE "Dumping ftrace buffer:\n"); |
3778 | ||
3779 | iter.tr = &global_trace; | |
3780 | iter.trace = current_trace; | |
3781 | ||
3782 | /* | |
3783 | * We need to stop all tracing on all CPUS to read the | |
3784 | * the next buffer. This is a bit expensive, but is | |
3785 | * not done often. We fill all what we can read, | |
3786 | * and then release the locks again. | |
3787 | */ | |
3788 | ||
3789 | cpus_clear(mask); | |
3790 | ||
3f5a54e3 SR |
3791 | while (!trace_empty(&iter)) { |
3792 | ||
3793 | if (!cnt) | |
3794 | printk(KERN_TRACE "---------------------------------\n"); | |
3795 | ||
3796 | cnt++; | |
3797 | ||
3798 | /* reset all but tr, trace, and overruns */ | |
3799 | memset(&iter.seq, 0, | |
3800 | sizeof(struct trace_iterator) - | |
3801 | offsetof(struct trace_iterator, seq)); | |
3802 | iter.iter_flags |= TRACE_FILE_LAT_FMT; | |
3803 | iter.pos = -1; | |
3804 | ||
3805 | if (find_next_entry_inc(&iter) != NULL) { | |
3806 | print_trace_line(&iter); | |
3807 | trace_consume(&iter); | |
3808 | } | |
3809 | ||
3810 | trace_printk_seq(&iter.seq); | |
3811 | } | |
3812 | ||
3813 | if (!cnt) | |
3814 | printk(KERN_TRACE " (ftrace buffer empty)\n"); | |
3815 | else | |
3816 | printk(KERN_TRACE "---------------------------------\n"); | |
3817 | ||
3f5a54e3 SR |
3818 | out: |
3819 | spin_unlock_irqrestore(&ftrace_dump_lock, flags); | |
3820 | } | |
3821 | ||
3928a8a2 | 3822 | __init static int tracer_alloc_buffers(void) |
bc0c38d1 | 3823 | { |
4c11d7ae | 3824 | struct trace_array_cpu *data; |
4c11d7ae SR |
3825 | int i; |
3826 | ||
3928a8a2 SR |
3827 | /* TODO: make the number of buffers hot pluggable with CPUS */ |
3828 | tracing_buffer_mask = cpu_possible_map; | |
4c11d7ae | 3829 | |
3928a8a2 SR |
3830 | global_trace.buffer = ring_buffer_alloc(trace_buf_size, |
3831 | TRACE_BUFFER_FLAGS); | |
3832 | if (!global_trace.buffer) { | |
3833 | printk(KERN_ERR "tracer: failed to allocate ring buffer!\n"); | |
3834 | WARN_ON(1); | |
3835 | return 0; | |
4c11d7ae | 3836 | } |
3928a8a2 | 3837 | global_trace.entries = ring_buffer_size(global_trace.buffer); |
4c11d7ae SR |
3838 | |
3839 | #ifdef CONFIG_TRACER_MAX_TRACE | |
3928a8a2 SR |
3840 | max_tr.buffer = ring_buffer_alloc(trace_buf_size, |
3841 | TRACE_BUFFER_FLAGS); | |
3842 | if (!max_tr.buffer) { | |
3843 | printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n"); | |
3844 | WARN_ON(1); | |
3845 | ring_buffer_free(global_trace.buffer); | |
3846 | return 0; | |
4c11d7ae | 3847 | } |
3928a8a2 SR |
3848 | max_tr.entries = ring_buffer_size(max_tr.buffer); |
3849 | WARN_ON(max_tr.entries != global_trace.entries); | |
a98a3c3f | 3850 | #endif |
ab46428c | 3851 | |
4c11d7ae | 3852 | /* Allocate the first page for all buffers */ |
ab46428c | 3853 | for_each_tracing_cpu(i) { |
4c11d7ae | 3854 | data = global_trace.data[i] = &per_cpu(global_trace_cpu, i); |
bc0c38d1 | 3855 | max_tr.data[i] = &per_cpu(max_data, i); |
4c11d7ae | 3856 | } |
bc0c38d1 | 3857 | |
bc0c38d1 SR |
3858 | trace_init_cmdlines(); |
3859 | ||
43a15386 | 3860 | register_tracer(&nop_trace); |
b5ad384e FW |
3861 | #ifdef CONFIG_BOOT_TRACER |
3862 | register_tracer(&boot_tracer); | |
3863 | current_trace = &boot_tracer; | |
3864 | current_trace->init(&global_trace); | |
3865 | #else | |
43a15386 | 3866 | current_trace = &nop_trace; |
b5ad384e | 3867 | #endif |
bc0c38d1 | 3868 | |
60a11774 SR |
3869 | /* All seems OK, enable tracing */ |
3870 | tracing_disabled = 0; | |
3928a8a2 | 3871 | |
3f5a54e3 SR |
3872 | atomic_notifier_chain_register(&panic_notifier_list, |
3873 | &trace_panic_notifier); | |
3874 | ||
3875 | register_die_notifier(&trace_die_notifier); | |
3876 | ||
bc0c38d1 | 3877 | return 0; |
bc0c38d1 | 3878 | } |
b5ad384e FW |
3879 | early_initcall(tracer_alloc_buffers); |
3880 | fs_initcall(tracer_init_debugfs); |