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