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