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