Commit | Line | Data |
---|---|---|
bc0c38d1 SR |
1 | /* |
2 | * ring buffer based function tracer | |
3 | * | |
4 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> | |
5 | * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com> | |
6 | * | |
7 | * Originally taken from the RT patch by: | |
8 | * Arnaldo Carvalho de Melo <acme@redhat.com> | |
9 | * | |
10 | * Based on code from the latency_tracer, that is: | |
11 | * Copyright (C) 2004-2006 Ingo Molnar | |
12 | * Copyright (C) 2004 William Lee Irwin III | |
13 | */ | |
14 | #include <linux/utsrelease.h> | |
15 | #include <linux/kallsyms.h> | |
16 | #include <linux/seq_file.h> | |
17 | #include <linux/debugfs.h> | |
4c11d7ae | 18 | #include <linux/pagemap.h> |
bc0c38d1 SR |
19 | #include <linux/hardirq.h> |
20 | #include <linux/linkage.h> | |
21 | #include <linux/uaccess.h> | |
22 | #include <linux/ftrace.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/percpu.h> | |
25 | #include <linux/ctype.h> | |
26 | #include <linux/init.h> | |
2a2cc8f7 | 27 | #include <linux/poll.h> |
bc0c38d1 SR |
28 | #include <linux/gfp.h> |
29 | #include <linux/fs.h> | |
30 | ||
86387f7e IM |
31 | #include <linux/stacktrace.h> |
32 | ||
bc0c38d1 SR |
33 | #include "trace.h" |
34 | ||
35 | unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX; | |
36 | unsigned long __read_mostly tracing_thresh; | |
37 | ||
60a11774 SR |
38 | static int tracing_disabled = 1; |
39 | ||
e309b41d | 40 | static long |
bc0c38d1 SR |
41 | ns2usecs(cycle_t nsec) |
42 | { | |
43 | nsec += 500; | |
44 | do_div(nsec, 1000); | |
45 | return nsec; | |
46 | } | |
47 | ||
e309b41d | 48 | cycle_t ftrace_now(int cpu) |
750ed1a4 | 49 | { |
0fd9e0da | 50 | return cpu_clock(cpu); |
750ed1a4 IM |
51 | } |
52 | ||
bc0c38d1 SR |
53 | static struct trace_array global_trace; |
54 | ||
55 | static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu); | |
56 | ||
57 | static struct trace_array max_tr; | |
58 | ||
59 | static DEFINE_PER_CPU(struct trace_array_cpu, max_data); | |
60 | ||
26994ead | 61 | static int tracer_enabled = 1; |
57422797 | 62 | static unsigned long trace_nr_entries = 65536UL; |
bc0c38d1 SR |
63 | |
64 | static struct tracer *trace_types __read_mostly; | |
65 | static struct tracer *current_trace __read_mostly; | |
66 | static int max_tracer_type_len; | |
67 | ||
68 | static DEFINE_MUTEX(trace_types_lock); | |
4e655519 IM |
69 | static DECLARE_WAIT_QUEUE_HEAD(trace_wait); |
70 | ||
71 | unsigned long trace_flags = TRACE_ITER_PRINT_PARENT; | |
72 | ||
4e655519 IM |
73 | void trace_wake_up(void) |
74 | { | |
017730c1 IM |
75 | /* |
76 | * The runqueue_is_locked() can fail, but this is the best we | |
77 | * have for now: | |
78 | */ | |
79 | if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked()) | |
4e655519 IM |
80 | wake_up(&trace_wait); |
81 | } | |
bc0c38d1 | 82 | |
4c11d7ae SR |
83 | #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry)) |
84 | ||
bc0c38d1 SR |
85 | static int __init set_nr_entries(char *str) |
86 | { | |
87 | if (!str) | |
88 | return 0; | |
89 | trace_nr_entries = simple_strtoul(str, &str, 0); | |
90 | return 1; | |
91 | } | |
92 | __setup("trace_entries=", set_nr_entries); | |
93 | ||
57f50be1 SR |
94 | unsigned long nsecs_to_usecs(unsigned long nsecs) |
95 | { | |
96 | return nsecs / 1000; | |
97 | } | |
98 | ||
bc0c38d1 SR |
99 | enum trace_type { |
100 | __TRACE_FIRST_TYPE = 0, | |
101 | ||
102 | TRACE_FN, | |
103 | TRACE_CTX, | |
57422797 | 104 | TRACE_WAKE, |
86387f7e | 105 | TRACE_STACK, |
f0a920d5 | 106 | TRACE_SPECIAL, |
bc0c38d1 SR |
107 | |
108 | __TRACE_LAST_TYPE | |
109 | }; | |
110 | ||
111 | enum trace_flag_type { | |
112 | TRACE_FLAG_IRQS_OFF = 0x01, | |
113 | TRACE_FLAG_NEED_RESCHED = 0x02, | |
114 | TRACE_FLAG_HARDIRQ = 0x04, | |
115 | TRACE_FLAG_SOFTIRQ = 0x08, | |
116 | }; | |
117 | ||
bc0c38d1 SR |
118 | #define TRACE_ITER_SYM_MASK \ |
119 | (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR) | |
120 | ||
121 | /* These must match the bit postions above */ | |
122 | static const char *trace_options[] = { | |
123 | "print-parent", | |
124 | "sym-offset", | |
125 | "sym-addr", | |
126 | "verbose", | |
f9896bf3 | 127 | "raw", |
5e3ca0ec | 128 | "hex", |
cb0f12aa | 129 | "bin", |
2a2cc8f7 | 130 | "block", |
86387f7e | 131 | "stacktrace", |
4ac3ba41 | 132 | "sched-tree", |
bc0c38d1 SR |
133 | NULL |
134 | }; | |
135 | ||
4c11d7ae | 136 | static DEFINE_SPINLOCK(ftrace_max_lock); |
bc0c38d1 SR |
137 | |
138 | /* | |
139 | * Copy the new maximum trace into the separate maximum-trace | |
140 | * structure. (this way the maximum trace is permanently saved, | |
141 | * for later retrieval via /debugfs/tracing/latency_trace) | |
142 | */ | |
e309b41d | 143 | static void |
bc0c38d1 SR |
144 | __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
145 | { | |
146 | struct trace_array_cpu *data = tr->data[cpu]; | |
147 | ||
148 | max_tr.cpu = cpu; | |
149 | max_tr.time_start = data->preempt_timestamp; | |
150 | ||
151 | data = max_tr.data[cpu]; | |
152 | data->saved_latency = tracing_max_latency; | |
153 | ||
154 | memcpy(data->comm, tsk->comm, TASK_COMM_LEN); | |
155 | data->pid = tsk->pid; | |
156 | data->uid = tsk->uid; | |
157 | data->nice = tsk->static_prio - 20 - MAX_RT_PRIO; | |
158 | data->policy = tsk->policy; | |
159 | data->rt_priority = tsk->rt_priority; | |
160 | ||
161 | /* record this tasks comm */ | |
162 | tracing_record_cmdline(current); | |
163 | } | |
164 | ||
c7aafc54 IM |
165 | void check_pages(struct trace_array_cpu *data) |
166 | { | |
167 | struct page *page, *tmp; | |
168 | ||
169 | BUG_ON(data->trace_pages.next->prev != &data->trace_pages); | |
170 | BUG_ON(data->trace_pages.prev->next != &data->trace_pages); | |
171 | ||
172 | list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) { | |
173 | BUG_ON(page->lru.next->prev != &page->lru); | |
174 | BUG_ON(page->lru.prev->next != &page->lru); | |
175 | } | |
176 | } | |
177 | ||
178 | void *head_page(struct trace_array_cpu *data) | |
179 | { | |
180 | struct page *page; | |
181 | ||
182 | check_pages(data); | |
183 | if (list_empty(&data->trace_pages)) | |
184 | return NULL; | |
185 | ||
186 | page = list_entry(data->trace_pages.next, struct page, lru); | |
187 | BUG_ON(&page->lru == &data->trace_pages); | |
188 | ||
189 | return page_address(page); | |
190 | } | |
191 | ||
e309b41d | 192 | static int |
214023c3 SR |
193 | trace_seq_printf(struct trace_seq *s, const char *fmt, ...) |
194 | { | |
195 | int len = (PAGE_SIZE - 1) - s->len; | |
196 | va_list ap; | |
b3806b43 | 197 | int ret; |
214023c3 SR |
198 | |
199 | if (!len) | |
200 | return 0; | |
201 | ||
202 | va_start(ap, fmt); | |
b3806b43 | 203 | ret = vsnprintf(s->buffer + s->len, len, fmt, ap); |
214023c3 SR |
204 | va_end(ap); |
205 | ||
b3806b43 SR |
206 | /* If we can't write it all, don't bother writing anything */ |
207 | if (ret > len) | |
208 | return 0; | |
209 | ||
210 | s->len += ret; | |
214023c3 SR |
211 | |
212 | return len; | |
213 | } | |
214 | ||
e309b41d | 215 | static int |
214023c3 SR |
216 | trace_seq_puts(struct trace_seq *s, const char *str) |
217 | { | |
218 | int len = strlen(str); | |
219 | ||
220 | if (len > ((PAGE_SIZE - 1) - s->len)) | |
b3806b43 | 221 | return 0; |
214023c3 SR |
222 | |
223 | memcpy(s->buffer + s->len, str, len); | |
224 | s->len += len; | |
225 | ||
226 | return len; | |
227 | } | |
228 | ||
e309b41d | 229 | static int |
214023c3 SR |
230 | trace_seq_putc(struct trace_seq *s, unsigned char c) |
231 | { | |
232 | if (s->len >= (PAGE_SIZE - 1)) | |
233 | return 0; | |
234 | ||
235 | s->buffer[s->len++] = c; | |
236 | ||
237 | return 1; | |
238 | } | |
239 | ||
e309b41d | 240 | static int |
cb0f12aa IM |
241 | trace_seq_putmem(struct trace_seq *s, void *mem, size_t len) |
242 | { | |
243 | if (len > ((PAGE_SIZE - 1) - s->len)) | |
244 | return 0; | |
245 | ||
246 | memcpy(s->buffer + s->len, mem, len); | |
247 | s->len += len; | |
248 | ||
249 | return len; | |
250 | } | |
251 | ||
5e3ca0ec IM |
252 | #define HEX_CHARS 17 |
253 | ||
e309b41d | 254 | static int |
5e3ca0ec IM |
255 | trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len) |
256 | { | |
257 | unsigned char hex[HEX_CHARS]; | |
258 | unsigned char *data; | |
259 | unsigned char byte; | |
260 | int i, j; | |
261 | ||
262 | BUG_ON(len >= HEX_CHARS); | |
263 | ||
264 | data = mem; | |
265 | ||
266 | #ifdef __BIG_ENDIAN | |
267 | for (i = 0, j = 0; i < len; i++) { | |
268 | #else | |
269 | for (i = len-1, j = 0; i >= 0; i--) { | |
270 | #endif | |
271 | byte = data[i]; | |
272 | ||
273 | hex[j] = byte & 0x0f; | |
274 | if (hex[j] >= 10) | |
275 | hex[j] += 'a' - 10; | |
276 | else | |
277 | hex[j] += '0'; | |
278 | j++; | |
279 | ||
280 | hex[j] = byte >> 4; | |
281 | if (hex[j] >= 10) | |
282 | hex[j] += 'a' - 10; | |
283 | else | |
284 | hex[j] += '0'; | |
285 | j++; | |
286 | } | |
287 | hex[j] = ' '; | |
288 | j++; | |
289 | ||
290 | return trace_seq_putmem(s, hex, j); | |
291 | } | |
292 | ||
e309b41d | 293 | static void |
214023c3 SR |
294 | trace_seq_reset(struct trace_seq *s) |
295 | { | |
296 | s->len = 0; | |
297 | } | |
298 | ||
e309b41d | 299 | static void |
214023c3 SR |
300 | trace_print_seq(struct seq_file *m, struct trace_seq *s) |
301 | { | |
302 | int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len; | |
303 | ||
304 | s->buffer[len] = 0; | |
305 | seq_puts(m, s->buffer); | |
306 | ||
307 | trace_seq_reset(s); | |
308 | } | |
309 | ||
e309b41d | 310 | static void |
c7aafc54 IM |
311 | flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2) |
312 | { | |
313 | struct list_head flip_pages; | |
314 | ||
315 | INIT_LIST_HEAD(&flip_pages); | |
316 | ||
93a588f4 | 317 | memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx, |
c7aafc54 | 318 | sizeof(struct trace_array_cpu) - |
93a588f4 | 319 | offsetof(struct trace_array_cpu, trace_head_idx)); |
c7aafc54 IM |
320 | |
321 | check_pages(tr1); | |
322 | check_pages(tr2); | |
323 | list_splice_init(&tr1->trace_pages, &flip_pages); | |
324 | list_splice_init(&tr2->trace_pages, &tr1->trace_pages); | |
325 | list_splice_init(&flip_pages, &tr2->trace_pages); | |
326 | BUG_ON(!list_empty(&flip_pages)); | |
327 | check_pages(tr1); | |
328 | check_pages(tr2); | |
329 | } | |
330 | ||
e309b41d | 331 | void |
bc0c38d1 SR |
332 | update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
333 | { | |
334 | struct trace_array_cpu *data; | |
bc0c38d1 SR |
335 | int i; |
336 | ||
4c11d7ae SR |
337 | WARN_ON_ONCE(!irqs_disabled()); |
338 | spin_lock(&ftrace_max_lock); | |
bc0c38d1 SR |
339 | /* clear out all the previous traces */ |
340 | for_each_possible_cpu(i) { | |
341 | data = tr->data[i]; | |
c7aafc54 | 342 | flip_trace(max_tr.data[i], data); |
89b2f978 | 343 | tracing_reset(data); |
bc0c38d1 SR |
344 | } |
345 | ||
346 | __update_max_tr(tr, tsk, cpu); | |
4c11d7ae | 347 | spin_unlock(&ftrace_max_lock); |
bc0c38d1 SR |
348 | } |
349 | ||
350 | /** | |
351 | * update_max_tr_single - only copy one trace over, and reset the rest | |
352 | * @tr - tracer | |
353 | * @tsk - task with the latency | |
354 | * @cpu - the cpu of the buffer to copy. | |
355 | */ | |
e309b41d | 356 | void |
bc0c38d1 SR |
357 | update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) |
358 | { | |
359 | struct trace_array_cpu *data = tr->data[cpu]; | |
bc0c38d1 SR |
360 | int i; |
361 | ||
4c11d7ae SR |
362 | WARN_ON_ONCE(!irqs_disabled()); |
363 | spin_lock(&ftrace_max_lock); | |
bc0c38d1 SR |
364 | for_each_possible_cpu(i) |
365 | tracing_reset(max_tr.data[i]); | |
366 | ||
c7aafc54 | 367 | flip_trace(max_tr.data[cpu], data); |
89b2f978 | 368 | tracing_reset(data); |
bc0c38d1 SR |
369 | |
370 | __update_max_tr(tr, tsk, cpu); | |
4c11d7ae | 371 | spin_unlock(&ftrace_max_lock); |
bc0c38d1 SR |
372 | } |
373 | ||
374 | int register_tracer(struct tracer *type) | |
375 | { | |
376 | struct tracer *t; | |
377 | int len; | |
378 | int ret = 0; | |
379 | ||
380 | if (!type->name) { | |
381 | pr_info("Tracer must have a name\n"); | |
382 | return -1; | |
383 | } | |
384 | ||
385 | mutex_lock(&trace_types_lock); | |
386 | for (t = trace_types; t; t = t->next) { | |
387 | if (strcmp(type->name, t->name) == 0) { | |
388 | /* already found */ | |
389 | pr_info("Trace %s already registered\n", | |
390 | type->name); | |
391 | ret = -1; | |
392 | goto out; | |
393 | } | |
394 | } | |
395 | ||
60a11774 SR |
396 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
397 | if (type->selftest) { | |
398 | struct tracer *saved_tracer = current_trace; | |
399 | struct trace_array_cpu *data; | |
400 | struct trace_array *tr = &global_trace; | |
401 | int saved_ctrl = tr->ctrl; | |
402 | int i; | |
403 | /* | |
404 | * Run a selftest on this tracer. | |
405 | * Here we reset the trace buffer, and set the current | |
406 | * tracer to be this tracer. The tracer can then run some | |
407 | * internal tracing to verify that everything is in order. | |
408 | * If we fail, we do not register this tracer. | |
409 | */ | |
410 | for_each_possible_cpu(i) { | |
60a11774 | 411 | data = tr->data[i]; |
c7aafc54 IM |
412 | if (!head_page(data)) |
413 | continue; | |
60a11774 SR |
414 | tracing_reset(data); |
415 | } | |
416 | current_trace = type; | |
417 | tr->ctrl = 0; | |
418 | /* the test is responsible for initializing and enabling */ | |
419 | pr_info("Testing tracer %s: ", type->name); | |
420 | ret = type->selftest(type, tr); | |
421 | /* the test is responsible for resetting too */ | |
422 | current_trace = saved_tracer; | |
423 | tr->ctrl = saved_ctrl; | |
424 | if (ret) { | |
425 | printk(KERN_CONT "FAILED!\n"); | |
426 | goto out; | |
427 | } | |
1d4db00a SR |
428 | /* Only reset on passing, to avoid touching corrupted buffers */ |
429 | for_each_possible_cpu(i) { | |
430 | data = tr->data[i]; | |
431 | if (!head_page(data)) | |
432 | continue; | |
433 | tracing_reset(data); | |
434 | } | |
60a11774 SR |
435 | printk(KERN_CONT "PASSED\n"); |
436 | } | |
437 | #endif | |
438 | ||
bc0c38d1 SR |
439 | type->next = trace_types; |
440 | trace_types = type; | |
441 | len = strlen(type->name); | |
442 | if (len > max_tracer_type_len) | |
443 | max_tracer_type_len = len; | |
60a11774 | 444 | |
bc0c38d1 SR |
445 | out: |
446 | mutex_unlock(&trace_types_lock); | |
447 | ||
448 | return ret; | |
449 | } | |
450 | ||
451 | void unregister_tracer(struct tracer *type) | |
452 | { | |
453 | struct tracer **t; | |
454 | int len; | |
455 | ||
456 | mutex_lock(&trace_types_lock); | |
457 | for (t = &trace_types; *t; t = &(*t)->next) { | |
458 | if (*t == type) | |
459 | goto found; | |
460 | } | |
461 | pr_info("Trace %s not registered\n", type->name); | |
462 | goto out; | |
463 | ||
464 | found: | |
465 | *t = (*t)->next; | |
466 | if (strlen(type->name) != max_tracer_type_len) | |
467 | goto out; | |
468 | ||
469 | max_tracer_type_len = 0; | |
470 | for (t = &trace_types; *t; t = &(*t)->next) { | |
471 | len = strlen((*t)->name); | |
472 | if (len > max_tracer_type_len) | |
473 | max_tracer_type_len = len; | |
474 | } | |
475 | out: | |
476 | mutex_unlock(&trace_types_lock); | |
477 | } | |
478 | ||
e309b41d | 479 | void tracing_reset(struct trace_array_cpu *data) |
bc0c38d1 SR |
480 | { |
481 | data->trace_idx = 0; | |
93a588f4 SR |
482 | data->trace_head = data->trace_tail = head_page(data); |
483 | data->trace_head_idx = 0; | |
484 | data->trace_tail_idx = 0; | |
bc0c38d1 SR |
485 | } |
486 | ||
bc0c38d1 SR |
487 | #define SAVED_CMDLINES 128 |
488 | static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1]; | |
489 | static unsigned map_cmdline_to_pid[SAVED_CMDLINES]; | |
490 | static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN]; | |
491 | static int cmdline_idx; | |
492 | static DEFINE_SPINLOCK(trace_cmdline_lock); | |
493 | atomic_t trace_record_cmdline_disabled; | |
494 | ||
495 | static void trace_init_cmdlines(void) | |
496 | { | |
497 | memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline)); | |
498 | memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid)); | |
499 | cmdline_idx = 0; | |
500 | } | |
501 | ||
e309b41d | 502 | void trace_stop_cmdline_recording(void); |
bc0c38d1 | 503 | |
e309b41d | 504 | static void trace_save_cmdline(struct task_struct *tsk) |
bc0c38d1 SR |
505 | { |
506 | unsigned map; | |
507 | unsigned idx; | |
508 | ||
509 | if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT)) | |
510 | return; | |
511 | ||
512 | /* | |
513 | * It's not the end of the world if we don't get | |
514 | * the lock, but we also don't want to spin | |
515 | * nor do we want to disable interrupts, | |
516 | * so if we miss here, then better luck next time. | |
517 | */ | |
518 | if (!spin_trylock(&trace_cmdline_lock)) | |
519 | return; | |
520 | ||
521 | idx = map_pid_to_cmdline[tsk->pid]; | |
522 | if (idx >= SAVED_CMDLINES) { | |
523 | idx = (cmdline_idx + 1) % SAVED_CMDLINES; | |
524 | ||
525 | map = map_cmdline_to_pid[idx]; | |
526 | if (map <= PID_MAX_DEFAULT) | |
527 | map_pid_to_cmdline[map] = (unsigned)-1; | |
528 | ||
529 | map_pid_to_cmdline[tsk->pid] = idx; | |
530 | ||
531 | cmdline_idx = idx; | |
532 | } | |
533 | ||
534 | memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN); | |
535 | ||
536 | spin_unlock(&trace_cmdline_lock); | |
537 | } | |
538 | ||
e309b41d | 539 | static char *trace_find_cmdline(int pid) |
bc0c38d1 SR |
540 | { |
541 | char *cmdline = "<...>"; | |
542 | unsigned map; | |
543 | ||
544 | if (!pid) | |
545 | return "<idle>"; | |
546 | ||
547 | if (pid > PID_MAX_DEFAULT) | |
548 | goto out; | |
549 | ||
550 | map = map_pid_to_cmdline[pid]; | |
551 | if (map >= SAVED_CMDLINES) | |
552 | goto out; | |
553 | ||
554 | cmdline = saved_cmdlines[map]; | |
555 | ||
556 | out: | |
557 | return cmdline; | |
558 | } | |
559 | ||
e309b41d | 560 | void tracing_record_cmdline(struct task_struct *tsk) |
bc0c38d1 SR |
561 | { |
562 | if (atomic_read(&trace_record_cmdline_disabled)) | |
563 | return; | |
564 | ||
565 | trace_save_cmdline(tsk); | |
566 | } | |
567 | ||
e309b41d | 568 | static inline struct list_head * |
93a588f4 SR |
569 | trace_next_list(struct trace_array_cpu *data, struct list_head *next) |
570 | { | |
571 | /* | |
572 | * Roundrobin - but skip the head (which is not a real page): | |
573 | */ | |
574 | next = next->next; | |
575 | if (unlikely(next == &data->trace_pages)) | |
576 | next = next->next; | |
577 | BUG_ON(next == &data->trace_pages); | |
578 | ||
579 | return next; | |
580 | } | |
581 | ||
e309b41d | 582 | static inline void * |
93a588f4 SR |
583 | trace_next_page(struct trace_array_cpu *data, void *addr) |
584 | { | |
585 | struct list_head *next; | |
586 | struct page *page; | |
587 | ||
588 | page = virt_to_page(addr); | |
589 | ||
590 | next = trace_next_list(data, &page->lru); | |
591 | page = list_entry(next, struct page, lru); | |
592 | ||
593 | return page_address(page); | |
594 | } | |
595 | ||
e309b41d | 596 | static inline struct trace_entry * |
c7aafc54 | 597 | tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data) |
bc0c38d1 SR |
598 | { |
599 | unsigned long idx, idx_next; | |
600 | struct trace_entry *entry; | |
601 | ||
4c11d7ae | 602 | data->trace_idx++; |
93a588f4 | 603 | idx = data->trace_head_idx; |
bc0c38d1 SR |
604 | idx_next = idx + 1; |
605 | ||
c7aafc54 IM |
606 | BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE); |
607 | ||
93a588f4 | 608 | entry = data->trace_head + idx * TRACE_ENTRY_SIZE; |
4c11d7ae SR |
609 | |
610 | if (unlikely(idx_next >= ENTRIES_PER_PAGE)) { | |
93a588f4 | 611 | data->trace_head = trace_next_page(data, data->trace_head); |
bc0c38d1 SR |
612 | idx_next = 0; |
613 | } | |
614 | ||
93a588f4 SR |
615 | if (data->trace_head == data->trace_tail && |
616 | idx_next == data->trace_tail_idx) { | |
617 | /* overrun */ | |
618 | data->trace_tail_idx++; | |
619 | if (data->trace_tail_idx >= ENTRIES_PER_PAGE) { | |
620 | data->trace_tail = | |
621 | trace_next_page(data, data->trace_tail); | |
622 | data->trace_tail_idx = 0; | |
623 | } | |
624 | } | |
625 | ||
626 | data->trace_head_idx = idx_next; | |
bc0c38d1 SR |
627 | |
628 | return entry; | |
629 | } | |
630 | ||
e309b41d | 631 | static inline void |
c7aafc54 | 632 | tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags) |
bc0c38d1 SR |
633 | { |
634 | struct task_struct *tsk = current; | |
635 | unsigned long pc; | |
636 | ||
637 | pc = preempt_count(); | |
638 | ||
c7aafc54 IM |
639 | entry->preempt_count = pc & 0xff; |
640 | entry->pid = tsk->pid; | |
750ed1a4 | 641 | entry->t = ftrace_now(raw_smp_processor_id()); |
bc0c38d1 SR |
642 | entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) | |
643 | ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) | | |
644 | ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) | | |
645 | (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0); | |
646 | } | |
647 | ||
e309b41d | 648 | void |
6fb44b71 SR |
649 | trace_function(struct trace_array *tr, struct trace_array_cpu *data, |
650 | unsigned long ip, unsigned long parent_ip, unsigned long flags) | |
bc0c38d1 SR |
651 | { |
652 | struct trace_entry *entry; | |
dcb6308f | 653 | unsigned long irq_flags; |
bc0c38d1 | 654 | |
dcb6308f | 655 | spin_lock_irqsave(&data->lock, irq_flags); |
c7aafc54 | 656 | entry = tracing_get_trace_entry(tr, data); |
bc0c38d1 | 657 | tracing_generic_entry_update(entry, flags); |
c7aafc54 IM |
658 | entry->type = TRACE_FN; |
659 | entry->fn.ip = ip; | |
660 | entry->fn.parent_ip = parent_ip; | |
dcb6308f | 661 | spin_unlock_irqrestore(&data->lock, irq_flags); |
bc0c38d1 SR |
662 | } |
663 | ||
e309b41d | 664 | void |
2e0f5761 IM |
665 | ftrace(struct trace_array *tr, struct trace_array_cpu *data, |
666 | unsigned long ip, unsigned long parent_ip, unsigned long flags) | |
667 | { | |
668 | if (likely(!atomic_read(&data->disabled))) | |
6fb44b71 | 669 | trace_function(tr, data, ip, parent_ip, flags); |
2e0f5761 IM |
670 | } |
671 | ||
e309b41d | 672 | void |
4e655519 IM |
673 | __trace_special(void *__tr, void *__data, |
674 | unsigned long arg1, unsigned long arg2, unsigned long arg3) | |
f0a920d5 | 675 | { |
4e655519 IM |
676 | struct trace_array_cpu *data = __data; |
677 | struct trace_array *tr = __tr; | |
f0a920d5 | 678 | struct trace_entry *entry; |
dcb6308f | 679 | unsigned long irq_flags; |
f0a920d5 | 680 | |
dcb6308f | 681 | spin_lock_irqsave(&data->lock, irq_flags); |
f0a920d5 IM |
682 | entry = tracing_get_trace_entry(tr, data); |
683 | tracing_generic_entry_update(entry, 0); | |
684 | entry->type = TRACE_SPECIAL; | |
685 | entry->special.arg1 = arg1; | |
686 | entry->special.arg2 = arg2; | |
687 | entry->special.arg3 = arg3; | |
dcb6308f | 688 | spin_unlock_irqrestore(&data->lock, irq_flags); |
017730c1 IM |
689 | |
690 | trace_wake_up(); | |
86387f7e IM |
691 | } |
692 | ||
693 | void __trace_stack(struct trace_array *tr, | |
694 | struct trace_array_cpu *data, | |
695 | unsigned long flags, | |
696 | int skip) | |
697 | { | |
698 | struct trace_entry *entry; | |
699 | struct stack_trace trace; | |
700 | ||
701 | if (!(trace_flags & TRACE_ITER_STACKTRACE)) | |
702 | return; | |
703 | ||
704 | entry = tracing_get_trace_entry(tr, data); | |
705 | tracing_generic_entry_update(entry, flags); | |
706 | entry->type = TRACE_STACK; | |
707 | ||
708 | memset(&entry->stack, 0, sizeof(entry->stack)); | |
709 | ||
710 | trace.nr_entries = 0; | |
711 | trace.max_entries = FTRACE_STACK_ENTRIES; | |
712 | trace.skip = skip; | |
713 | trace.entries = entry->stack.caller; | |
714 | ||
715 | save_stack_trace(&trace); | |
f0a920d5 IM |
716 | } |
717 | ||
e309b41d | 718 | void |
bc0c38d1 SR |
719 | tracing_sched_switch_trace(struct trace_array *tr, |
720 | struct trace_array_cpu *data, | |
86387f7e IM |
721 | struct task_struct *prev, |
722 | struct task_struct *next, | |
bc0c38d1 SR |
723 | unsigned long flags) |
724 | { | |
725 | struct trace_entry *entry; | |
dcb6308f | 726 | unsigned long irq_flags; |
bc0c38d1 | 727 | |
dcb6308f | 728 | spin_lock_irqsave(&data->lock, irq_flags); |
c7aafc54 | 729 | entry = tracing_get_trace_entry(tr, data); |
bc0c38d1 SR |
730 | tracing_generic_entry_update(entry, flags); |
731 | entry->type = TRACE_CTX; | |
732 | entry->ctx.prev_pid = prev->pid; | |
733 | entry->ctx.prev_prio = prev->prio; | |
734 | entry->ctx.prev_state = prev->state; | |
735 | entry->ctx.next_pid = next->pid; | |
736 | entry->ctx.next_prio = next->prio; | |
bac524d3 | 737 | entry->ctx.next_state = next->state; |
86387f7e | 738 | __trace_stack(tr, data, flags, 4); |
dcb6308f | 739 | spin_unlock_irqrestore(&data->lock, irq_flags); |
bc0c38d1 SR |
740 | } |
741 | ||
57422797 IM |
742 | void |
743 | tracing_sched_wakeup_trace(struct trace_array *tr, | |
744 | struct trace_array_cpu *data, | |
86387f7e IM |
745 | struct task_struct *wakee, |
746 | struct task_struct *curr, | |
57422797 IM |
747 | unsigned long flags) |
748 | { | |
749 | struct trace_entry *entry; | |
750 | unsigned long irq_flags; | |
751 | ||
752 | spin_lock_irqsave(&data->lock, irq_flags); | |
753 | entry = tracing_get_trace_entry(tr, data); | |
754 | tracing_generic_entry_update(entry, flags); | |
755 | entry->type = TRACE_WAKE; | |
756 | entry->ctx.prev_pid = curr->pid; | |
757 | entry->ctx.prev_prio = curr->prio; | |
758 | entry->ctx.prev_state = curr->state; | |
759 | entry->ctx.next_pid = wakee->pid; | |
760 | entry->ctx.next_prio = wakee->prio; | |
bac524d3 | 761 | entry->ctx.next_state = wakee->state; |
86387f7e | 762 | __trace_stack(tr, data, flags, 5); |
57422797 | 763 | spin_unlock_irqrestore(&data->lock, irq_flags); |
017730c1 IM |
764 | |
765 | trace_wake_up(); | |
57422797 IM |
766 | } |
767 | ||
2e0f5761 | 768 | #ifdef CONFIG_FTRACE |
e309b41d | 769 | static void |
2e0f5761 IM |
770 | function_trace_call(unsigned long ip, unsigned long parent_ip) |
771 | { | |
772 | struct trace_array *tr = &global_trace; | |
773 | struct trace_array_cpu *data; | |
774 | unsigned long flags; | |
775 | long disabled; | |
776 | int cpu; | |
777 | ||
778 | if (unlikely(!tracer_enabled)) | |
779 | return; | |
780 | ||
781 | local_irq_save(flags); | |
782 | cpu = raw_smp_processor_id(); | |
783 | data = tr->data[cpu]; | |
784 | disabled = atomic_inc_return(&data->disabled); | |
785 | ||
786 | if (likely(disabled == 1)) | |
6fb44b71 | 787 | trace_function(tr, data, ip, parent_ip, flags); |
2e0f5761 IM |
788 | |
789 | atomic_dec(&data->disabled); | |
790 | local_irq_restore(flags); | |
791 | } | |
792 | ||
793 | static struct ftrace_ops trace_ops __read_mostly = | |
794 | { | |
795 | .func = function_trace_call, | |
796 | }; | |
797 | ||
e309b41d | 798 | void tracing_start_function_trace(void) |
2e0f5761 IM |
799 | { |
800 | register_ftrace_function(&trace_ops); | |
801 | } | |
802 | ||
e309b41d | 803 | void tracing_stop_function_trace(void) |
2e0f5761 IM |
804 | { |
805 | unregister_ftrace_function(&trace_ops); | |
806 | } | |
807 | #endif | |
808 | ||
bc0c38d1 SR |
809 | enum trace_file_type { |
810 | TRACE_FILE_LAT_FMT = 1, | |
811 | }; | |
812 | ||
813 | static struct trace_entry * | |
4c11d7ae SR |
814 | trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data, |
815 | struct trace_iterator *iter, int cpu) | |
bc0c38d1 | 816 | { |
4c11d7ae SR |
817 | struct page *page; |
818 | struct trace_entry *array; | |
bc0c38d1 | 819 | |
4c11d7ae | 820 | if (iter->next_idx[cpu] >= tr->entries || |
b3806b43 SR |
821 | iter->next_idx[cpu] >= data->trace_idx || |
822 | (data->trace_head == data->trace_tail && | |
823 | data->trace_head_idx == data->trace_tail_idx)) | |
bc0c38d1 SR |
824 | return NULL; |
825 | ||
4c11d7ae | 826 | if (!iter->next_page[cpu]) { |
93a588f4 SR |
827 | /* Initialize the iterator for this cpu trace buffer */ |
828 | WARN_ON(!data->trace_tail); | |
829 | page = virt_to_page(data->trace_tail); | |
830 | iter->next_page[cpu] = &page->lru; | |
831 | iter->next_page_idx[cpu] = data->trace_tail_idx; | |
4c11d7ae | 832 | } |
bc0c38d1 | 833 | |
4c11d7ae | 834 | page = list_entry(iter->next_page[cpu], struct page, lru); |
c7aafc54 IM |
835 | BUG_ON(&data->trace_pages == &page->lru); |
836 | ||
4c11d7ae SR |
837 | array = page_address(page); |
838 | ||
93a588f4 | 839 | WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE); |
4c11d7ae | 840 | return &array[iter->next_page_idx[cpu]]; |
bc0c38d1 SR |
841 | } |
842 | ||
e309b41d | 843 | static struct trace_entry * |
bc0c38d1 SR |
844 | find_next_entry(struct trace_iterator *iter, int *ent_cpu) |
845 | { | |
846 | struct trace_array *tr = iter->tr; | |
847 | struct trace_entry *ent, *next = NULL; | |
848 | int next_cpu = -1; | |
849 | int cpu; | |
850 | ||
851 | for_each_possible_cpu(cpu) { | |
c7aafc54 | 852 | if (!head_page(tr->data[cpu])) |
bc0c38d1 | 853 | continue; |
4c11d7ae | 854 | ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu); |
cdd31cd2 IM |
855 | /* |
856 | * Pick the entry with the smallest timestamp: | |
857 | */ | |
858 | if (ent && (!next || ent->t < next->t)) { | |
bc0c38d1 SR |
859 | next = ent; |
860 | next_cpu = cpu; | |
861 | } | |
862 | } | |
863 | ||
864 | if (ent_cpu) | |
865 | *ent_cpu = next_cpu; | |
866 | ||
867 | return next; | |
868 | } | |
869 | ||
e309b41d | 870 | static void trace_iterator_increment(struct trace_iterator *iter) |
bc0c38d1 | 871 | { |
b3806b43 SR |
872 | iter->idx++; |
873 | iter->next_idx[iter->cpu]++; | |
874 | iter->next_page_idx[iter->cpu]++; | |
8c523a9d | 875 | |
b3806b43 SR |
876 | if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) { |
877 | struct trace_array_cpu *data = iter->tr->data[iter->cpu]; | |
bc0c38d1 | 878 | |
b3806b43 SR |
879 | iter->next_page_idx[iter->cpu] = 0; |
880 | iter->next_page[iter->cpu] = | |
881 | trace_next_list(data, iter->next_page[iter->cpu]); | |
882 | } | |
883 | } | |
bc0c38d1 | 884 | |
e309b41d | 885 | static void trace_consume(struct trace_iterator *iter) |
b3806b43 SR |
886 | { |
887 | struct trace_array_cpu *data = iter->tr->data[iter->cpu]; | |
888 | ||
889 | data->trace_tail_idx++; | |
890 | if (data->trace_tail_idx >= ENTRIES_PER_PAGE) { | |
891 | data->trace_tail = trace_next_page(data, data->trace_tail); | |
892 | data->trace_tail_idx = 0; | |
893 | } | |
4e3c3333 | 894 | |
b3806b43 SR |
895 | /* Check if we empty it, then reset the index */ |
896 | if (data->trace_head == data->trace_tail && | |
897 | data->trace_head_idx == data->trace_tail_idx) | |
898 | data->trace_idx = 0; | |
b3806b43 SR |
899 | } |
900 | ||
e309b41d | 901 | static void *find_next_entry_inc(struct trace_iterator *iter) |
b3806b43 SR |
902 | { |
903 | struct trace_entry *next; | |
904 | int next_cpu = -1; | |
905 | ||
906 | next = find_next_entry(iter, &next_cpu); | |
93a588f4 | 907 | |
4e3c3333 IM |
908 | iter->prev_ent = iter->ent; |
909 | iter->prev_cpu = iter->cpu; | |
910 | ||
bc0c38d1 SR |
911 | iter->ent = next; |
912 | iter->cpu = next_cpu; | |
913 | ||
b3806b43 SR |
914 | if (next) |
915 | trace_iterator_increment(iter); | |
916 | ||
bc0c38d1 SR |
917 | return next ? iter : NULL; |
918 | } | |
919 | ||
e309b41d | 920 | static void *s_next(struct seq_file *m, void *v, loff_t *pos) |
bc0c38d1 SR |
921 | { |
922 | struct trace_iterator *iter = m->private; | |
bc0c38d1 SR |
923 | void *last_ent = iter->ent; |
924 | int i = (int)*pos; | |
4e3c3333 | 925 | void *ent; |
bc0c38d1 SR |
926 | |
927 | (*pos)++; | |
928 | ||
929 | /* can't go backwards */ | |
930 | if (iter->idx > i) | |
931 | return NULL; | |
932 | ||
933 | if (iter->idx < 0) | |
934 | ent = find_next_entry_inc(iter); | |
935 | else | |
936 | ent = iter; | |
937 | ||
938 | while (ent && iter->idx < i) | |
939 | ent = find_next_entry_inc(iter); | |
940 | ||
941 | iter->pos = *pos; | |
942 | ||
943 | if (last_ent && !ent) | |
944 | seq_puts(m, "\n\nvim:ft=help\n"); | |
945 | ||
946 | return ent; | |
947 | } | |
948 | ||
949 | static void *s_start(struct seq_file *m, loff_t *pos) | |
950 | { | |
951 | struct trace_iterator *iter = m->private; | |
952 | void *p = NULL; | |
953 | loff_t l = 0; | |
954 | int i; | |
955 | ||
956 | mutex_lock(&trace_types_lock); | |
957 | ||
958 | if (!current_trace || current_trace != iter->trace) | |
959 | return NULL; | |
960 | ||
961 | atomic_inc(&trace_record_cmdline_disabled); | |
962 | ||
963 | /* let the tracer grab locks here if needed */ | |
964 | if (current_trace->start) | |
965 | current_trace->start(iter); | |
966 | ||
967 | if (*pos != iter->pos) { | |
968 | iter->ent = NULL; | |
969 | iter->cpu = 0; | |
970 | iter->idx = -1; | |
4e3c3333 IM |
971 | iter->prev_ent = NULL; |
972 | iter->prev_cpu = -1; | |
bc0c38d1 | 973 | |
4c11d7ae | 974 | for_each_possible_cpu(i) { |
bc0c38d1 | 975 | iter->next_idx[i] = 0; |
4c11d7ae SR |
976 | iter->next_page[i] = NULL; |
977 | } | |
bc0c38d1 SR |
978 | |
979 | for (p = iter; p && l < *pos; p = s_next(m, p, &l)) | |
980 | ; | |
981 | ||
982 | } else { | |
4c11d7ae | 983 | l = *pos - 1; |
bc0c38d1 SR |
984 | p = s_next(m, p, &l); |
985 | } | |
986 | ||
987 | return p; | |
988 | } | |
989 | ||
990 | static void s_stop(struct seq_file *m, void *p) | |
991 | { | |
992 | struct trace_iterator *iter = m->private; | |
993 | ||
994 | atomic_dec(&trace_record_cmdline_disabled); | |
995 | ||
996 | /* let the tracer release locks here if needed */ | |
997 | if (current_trace && current_trace == iter->trace && iter->trace->stop) | |
998 | iter->trace->stop(iter); | |
999 | ||
1000 | mutex_unlock(&trace_types_lock); | |
1001 | } | |
1002 | ||
b3806b43 | 1003 | static int |
214023c3 | 1004 | seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address) |
bc0c38d1 SR |
1005 | { |
1006 | #ifdef CONFIG_KALLSYMS | |
1007 | char str[KSYM_SYMBOL_LEN]; | |
1008 | ||
1009 | kallsyms_lookup(address, NULL, NULL, NULL, str); | |
1010 | ||
b3806b43 | 1011 | return trace_seq_printf(s, fmt, str); |
bc0c38d1 | 1012 | #endif |
b3806b43 | 1013 | return 1; |
bc0c38d1 SR |
1014 | } |
1015 | ||
b3806b43 | 1016 | static int |
214023c3 SR |
1017 | seq_print_sym_offset(struct trace_seq *s, const char *fmt, |
1018 | unsigned long address) | |
bc0c38d1 SR |
1019 | { |
1020 | #ifdef CONFIG_KALLSYMS | |
1021 | char str[KSYM_SYMBOL_LEN]; | |
1022 | ||
1023 | sprint_symbol(str, address); | |
b3806b43 | 1024 | return trace_seq_printf(s, fmt, str); |
bc0c38d1 | 1025 | #endif |
b3806b43 | 1026 | return 1; |
bc0c38d1 SR |
1027 | } |
1028 | ||
1029 | #ifndef CONFIG_64BIT | |
1030 | # define IP_FMT "%08lx" | |
1031 | #else | |
1032 | # define IP_FMT "%016lx" | |
1033 | #endif | |
1034 | ||
e309b41d | 1035 | static int |
214023c3 | 1036 | seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags) |
bc0c38d1 | 1037 | { |
b3806b43 SR |
1038 | int ret; |
1039 | ||
1040 | if (!ip) | |
1041 | return trace_seq_printf(s, "0"); | |
bc0c38d1 SR |
1042 | |
1043 | if (sym_flags & TRACE_ITER_SYM_OFFSET) | |
b3806b43 | 1044 | ret = seq_print_sym_offset(s, "%s", ip); |
bc0c38d1 | 1045 | else |
b3806b43 SR |
1046 | ret = seq_print_sym_short(s, "%s", ip); |
1047 | ||
1048 | if (!ret) | |
1049 | return 0; | |
bc0c38d1 SR |
1050 | |
1051 | if (sym_flags & TRACE_ITER_SYM_ADDR) | |
b3806b43 SR |
1052 | ret = trace_seq_printf(s, " <" IP_FMT ">", ip); |
1053 | return ret; | |
bc0c38d1 SR |
1054 | } |
1055 | ||
e309b41d | 1056 | static void print_lat_help_header(struct seq_file *m) |
bc0c38d1 SR |
1057 | { |
1058 | seq_puts(m, "# _------=> CPU# \n"); | |
1059 | seq_puts(m, "# / _-----=> irqs-off \n"); | |
1060 | seq_puts(m, "# | / _----=> need-resched \n"); | |
1061 | seq_puts(m, "# || / _---=> hardirq/softirq \n"); | |
1062 | seq_puts(m, "# ||| / _--=> preempt-depth \n"); | |
1063 | seq_puts(m, "# |||| / \n"); | |
1064 | seq_puts(m, "# ||||| delay \n"); | |
1065 | seq_puts(m, "# cmd pid ||||| time | caller \n"); | |
1066 | seq_puts(m, "# \\ / ||||| \\ | / \n"); | |
1067 | } | |
1068 | ||
e309b41d | 1069 | static void print_func_help_header(struct seq_file *m) |
bc0c38d1 SR |
1070 | { |
1071 | seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"); | |
1072 | seq_puts(m, "# | | | | |\n"); | |
1073 | } | |
1074 | ||
1075 | ||
e309b41d | 1076 | static void |
bc0c38d1 SR |
1077 | print_trace_header(struct seq_file *m, struct trace_iterator *iter) |
1078 | { | |
1079 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); | |
1080 | struct trace_array *tr = iter->tr; | |
1081 | struct trace_array_cpu *data = tr->data[tr->cpu]; | |
1082 | struct tracer *type = current_trace; | |
4c11d7ae SR |
1083 | unsigned long total = 0; |
1084 | unsigned long entries = 0; | |
bc0c38d1 SR |
1085 | int cpu; |
1086 | const char *name = "preemption"; | |
1087 | ||
1088 | if (type) | |
1089 | name = type->name; | |
1090 | ||
1091 | for_each_possible_cpu(cpu) { | |
c7aafc54 | 1092 | if (head_page(tr->data[cpu])) { |
4c11d7ae SR |
1093 | total += tr->data[cpu]->trace_idx; |
1094 | if (tr->data[cpu]->trace_idx > tr->entries) | |
bc0c38d1 | 1095 | entries += tr->entries; |
4c11d7ae | 1096 | else |
bc0c38d1 SR |
1097 | entries += tr->data[cpu]->trace_idx; |
1098 | } | |
1099 | } | |
1100 | ||
1101 | seq_printf(m, "%s latency trace v1.1.5 on %s\n", | |
1102 | name, UTS_RELEASE); | |
1103 | seq_puts(m, "-----------------------------------" | |
1104 | "---------------------------------\n"); | |
1105 | seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |" | |
1106 | " (M:%s VP:%d, KP:%d, SP:%d HP:%d", | |
57f50be1 | 1107 | nsecs_to_usecs(data->saved_latency), |
bc0c38d1 | 1108 | entries, |
4c11d7ae | 1109 | total, |
bc0c38d1 SR |
1110 | tr->cpu, |
1111 | #if defined(CONFIG_PREEMPT_NONE) | |
1112 | "server", | |
1113 | #elif defined(CONFIG_PREEMPT_VOLUNTARY) | |
1114 | "desktop", | |
1115 | #elif defined(CONFIG_PREEMPT_DESKTOP) | |
1116 | "preempt", | |
1117 | #else | |
1118 | "unknown", | |
1119 | #endif | |
1120 | /* These are reserved for later use */ | |
1121 | 0, 0, 0, 0); | |
1122 | #ifdef CONFIG_SMP | |
1123 | seq_printf(m, " #P:%d)\n", num_online_cpus()); | |
1124 | #else | |
1125 | seq_puts(m, ")\n"); | |
1126 | #endif | |
1127 | seq_puts(m, " -----------------\n"); | |
1128 | seq_printf(m, " | task: %.16s-%d " | |
1129 | "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n", | |
1130 | data->comm, data->pid, data->uid, data->nice, | |
1131 | data->policy, data->rt_priority); | |
1132 | seq_puts(m, " -----------------\n"); | |
1133 | ||
1134 | if (data->critical_start) { | |
1135 | seq_puts(m, " => started at: "); | |
214023c3 SR |
1136 | seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags); |
1137 | trace_print_seq(m, &iter->seq); | |
bc0c38d1 | 1138 | seq_puts(m, "\n => ended at: "); |
214023c3 SR |
1139 | seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags); |
1140 | trace_print_seq(m, &iter->seq); | |
bc0c38d1 SR |
1141 | seq_puts(m, "\n"); |
1142 | } | |
1143 | ||
1144 | seq_puts(m, "\n"); | |
1145 | } | |
1146 | ||
e309b41d | 1147 | static void |
214023c3 | 1148 | lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu) |
bc0c38d1 SR |
1149 | { |
1150 | int hardirq, softirq; | |
1151 | char *comm; | |
1152 | ||
1153 | comm = trace_find_cmdline(entry->pid); | |
1154 | ||
214023c3 SR |
1155 | trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid); |
1156 | trace_seq_printf(s, "%d", cpu); | |
1157 | trace_seq_printf(s, "%c%c", | |
1158 | (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.', | |
1159 | ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.')); | |
bc0c38d1 SR |
1160 | |
1161 | hardirq = entry->flags & TRACE_FLAG_HARDIRQ; | |
1162 | softirq = entry->flags & TRACE_FLAG_SOFTIRQ; | |
1163 | if (hardirq && softirq) | |
214023c3 | 1164 | trace_seq_putc(s, 'H'); |
bc0c38d1 SR |
1165 | else { |
1166 | if (hardirq) | |
214023c3 | 1167 | trace_seq_putc(s, 'h'); |
bc0c38d1 SR |
1168 | else { |
1169 | if (softirq) | |
214023c3 | 1170 | trace_seq_putc(s, 's'); |
bc0c38d1 | 1171 | else |
214023c3 | 1172 | trace_seq_putc(s, '.'); |
bc0c38d1 SR |
1173 | } |
1174 | } | |
1175 | ||
1176 | if (entry->preempt_count) | |
214023c3 | 1177 | trace_seq_printf(s, "%x", entry->preempt_count); |
bc0c38d1 | 1178 | else |
214023c3 | 1179 | trace_seq_puts(s, "."); |
bc0c38d1 SR |
1180 | } |
1181 | ||
1182 | unsigned long preempt_mark_thresh = 100; | |
1183 | ||
e309b41d | 1184 | static void |
214023c3 | 1185 | lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs, |
bc0c38d1 SR |
1186 | unsigned long rel_usecs) |
1187 | { | |
214023c3 | 1188 | trace_seq_printf(s, " %4lldus", abs_usecs); |
bc0c38d1 | 1189 | if (rel_usecs > preempt_mark_thresh) |
214023c3 | 1190 | trace_seq_puts(s, "!: "); |
bc0c38d1 | 1191 | else if (rel_usecs > 1) |
214023c3 | 1192 | trace_seq_puts(s, "+: "); |
bc0c38d1 | 1193 | else |
214023c3 | 1194 | trace_seq_puts(s, " : "); |
bc0c38d1 SR |
1195 | } |
1196 | ||
1197 | static const char state_to_char[] = TASK_STATE_TO_CHAR_STR; | |
1198 | ||
e309b41d | 1199 | static int |
214023c3 | 1200 | print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu) |
bc0c38d1 | 1201 | { |
214023c3 | 1202 | struct trace_seq *s = &iter->seq; |
bc0c38d1 SR |
1203 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); |
1204 | struct trace_entry *next_entry = find_next_entry(iter, NULL); | |
1205 | unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE); | |
1206 | struct trace_entry *entry = iter->ent; | |
1207 | unsigned long abs_usecs; | |
1208 | unsigned long rel_usecs; | |
1209 | char *comm; | |
bac524d3 | 1210 | int S, T; |
86387f7e | 1211 | int i; |
bc0c38d1 SR |
1212 | |
1213 | if (!next_entry) | |
1214 | next_entry = entry; | |
1215 | rel_usecs = ns2usecs(next_entry->t - entry->t); | |
1216 | abs_usecs = ns2usecs(entry->t - iter->tr->time_start); | |
1217 | ||
1218 | if (verbose) { | |
1219 | comm = trace_find_cmdline(entry->pid); | |
214023c3 SR |
1220 | trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]" |
1221 | " %ld.%03ldms (+%ld.%03ldms): ", | |
1222 | comm, | |
1223 | entry->pid, cpu, entry->flags, | |
1224 | entry->preempt_count, trace_idx, | |
1225 | ns2usecs(entry->t), | |
1226 | abs_usecs/1000, | |
1227 | abs_usecs % 1000, rel_usecs/1000, | |
1228 | rel_usecs % 1000); | |
bc0c38d1 | 1229 | } else { |
f29c73fe IM |
1230 | lat_print_generic(s, entry, cpu); |
1231 | lat_print_timestamp(s, abs_usecs, rel_usecs); | |
bc0c38d1 SR |
1232 | } |
1233 | switch (entry->type) { | |
1234 | case TRACE_FN: | |
214023c3 SR |
1235 | seq_print_ip_sym(s, entry->fn.ip, sym_flags); |
1236 | trace_seq_puts(s, " ("); | |
1237 | seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags); | |
1238 | trace_seq_puts(s, ")\n"); | |
bc0c38d1 SR |
1239 | break; |
1240 | case TRACE_CTX: | |
57422797 | 1241 | case TRACE_WAKE: |
bc0c38d1 SR |
1242 | S = entry->ctx.prev_state < sizeof(state_to_char) ? |
1243 | state_to_char[entry->ctx.prev_state] : 'X'; | |
bac524d3 PZ |
1244 | T = entry->ctx.next_state < sizeof(state_to_char) ? |
1245 | state_to_char[entry->ctx.next_state] : 'X'; | |
1246 | ||
bc0c38d1 | 1247 | comm = trace_find_cmdline(entry->ctx.next_pid); |
bac524d3 | 1248 | trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n", |
214023c3 SR |
1249 | entry->ctx.prev_pid, |
1250 | entry->ctx.prev_prio, | |
57422797 | 1251 | S, entry->type == TRACE_CTX ? "==>" : " +", |
214023c3 SR |
1252 | entry->ctx.next_pid, |
1253 | entry->ctx.next_prio, | |
bac524d3 | 1254 | T, comm); |
bc0c38d1 | 1255 | break; |
f0a920d5 | 1256 | case TRACE_SPECIAL: |
88a4216c | 1257 | trace_seq_printf(s, "# %ld %ld %ld\n", |
f0a920d5 IM |
1258 | entry->special.arg1, |
1259 | entry->special.arg2, | |
1260 | entry->special.arg3); | |
1261 | break; | |
86387f7e IM |
1262 | case TRACE_STACK: |
1263 | for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { | |
1264 | if (i) | |
1265 | trace_seq_puts(s, " <= "); | |
1266 | seq_print_ip_sym(s, entry->stack.caller[i], sym_flags); | |
1267 | } | |
1268 | trace_seq_puts(s, "\n"); | |
1269 | break; | |
89b2f978 | 1270 | default: |
214023c3 | 1271 | trace_seq_printf(s, "Unknown type %d\n", entry->type); |
bc0c38d1 | 1272 | } |
f9896bf3 | 1273 | return 1; |
bc0c38d1 SR |
1274 | } |
1275 | ||
e309b41d | 1276 | static int print_trace_fmt(struct trace_iterator *iter) |
bc0c38d1 | 1277 | { |
214023c3 | 1278 | struct trace_seq *s = &iter->seq; |
bc0c38d1 | 1279 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); |
4e3c3333 | 1280 | struct trace_entry *entry; |
bc0c38d1 SR |
1281 | unsigned long usec_rem; |
1282 | unsigned long long t; | |
1283 | unsigned long secs; | |
1284 | char *comm; | |
b3806b43 | 1285 | int ret; |
bac524d3 | 1286 | int S, T; |
86387f7e | 1287 | int i; |
bc0c38d1 | 1288 | |
4e3c3333 IM |
1289 | entry = iter->ent; |
1290 | ||
bc0c38d1 SR |
1291 | comm = trace_find_cmdline(iter->ent->pid); |
1292 | ||
cdd31cd2 | 1293 | t = ns2usecs(entry->t); |
bc0c38d1 SR |
1294 | usec_rem = do_div(t, 1000000ULL); |
1295 | secs = (unsigned long)t; | |
1296 | ||
f29c73fe IM |
1297 | ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid); |
1298 | if (!ret) | |
1299 | return 0; | |
1300 | ret = trace_seq_printf(s, "[%02d] ", iter->cpu); | |
1301 | if (!ret) | |
1302 | return 0; | |
1303 | ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem); | |
1304 | if (!ret) | |
1305 | return 0; | |
bc0c38d1 SR |
1306 | |
1307 | switch (entry->type) { | |
1308 | case TRACE_FN: | |
b3806b43 SR |
1309 | ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags); |
1310 | if (!ret) | |
1311 | return 0; | |
bc0c38d1 SR |
1312 | if ((sym_flags & TRACE_ITER_PRINT_PARENT) && |
1313 | entry->fn.parent_ip) { | |
b3806b43 SR |
1314 | ret = trace_seq_printf(s, " <-"); |
1315 | if (!ret) | |
1316 | return 0; | |
1317 | ret = seq_print_ip_sym(s, entry->fn.parent_ip, | |
1318 | sym_flags); | |
1319 | if (!ret) | |
1320 | return 0; | |
bc0c38d1 | 1321 | } |
b3806b43 SR |
1322 | ret = trace_seq_printf(s, "\n"); |
1323 | if (!ret) | |
1324 | return 0; | |
bc0c38d1 SR |
1325 | break; |
1326 | case TRACE_CTX: | |
57422797 | 1327 | case TRACE_WAKE: |
bc0c38d1 SR |
1328 | S = entry->ctx.prev_state < sizeof(state_to_char) ? |
1329 | state_to_char[entry->ctx.prev_state] : 'X'; | |
bac524d3 PZ |
1330 | T = entry->ctx.next_state < sizeof(state_to_char) ? |
1331 | state_to_char[entry->ctx.next_state] : 'X'; | |
1332 | ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n", | |
b3806b43 SR |
1333 | entry->ctx.prev_pid, |
1334 | entry->ctx.prev_prio, | |
1335 | S, | |
57422797 | 1336 | entry->type == TRACE_CTX ? "==>" : " +", |
b3806b43 | 1337 | entry->ctx.next_pid, |
bac524d3 PZ |
1338 | entry->ctx.next_prio, |
1339 | T); | |
b3806b43 SR |
1340 | if (!ret) |
1341 | return 0; | |
bc0c38d1 | 1342 | break; |
f0a920d5 | 1343 | case TRACE_SPECIAL: |
88a4216c | 1344 | ret = trace_seq_printf(s, "# %ld %ld %ld\n", |
f0a920d5 IM |
1345 | entry->special.arg1, |
1346 | entry->special.arg2, | |
1347 | entry->special.arg3); | |
1348 | if (!ret) | |
1349 | return 0; | |
1350 | break; | |
86387f7e IM |
1351 | case TRACE_STACK: |
1352 | for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { | |
1353 | if (i) { | |
1354 | ret = trace_seq_puts(s, " <= "); | |
1355 | if (!ret) | |
1356 | return 0; | |
1357 | } | |
1358 | ret = seq_print_ip_sym(s, entry->stack.caller[i], | |
1359 | sym_flags); | |
1360 | if (!ret) | |
1361 | return 0; | |
1362 | } | |
1363 | ret = trace_seq_puts(s, "\n"); | |
1364 | if (!ret) | |
1365 | return 0; | |
1366 | break; | |
bc0c38d1 | 1367 | } |
b3806b43 | 1368 | return 1; |
bc0c38d1 SR |
1369 | } |
1370 | ||
e309b41d | 1371 | static int print_raw_fmt(struct trace_iterator *iter) |
f9896bf3 IM |
1372 | { |
1373 | struct trace_seq *s = &iter->seq; | |
1374 | struct trace_entry *entry; | |
1375 | int ret; | |
bac524d3 | 1376 | int S, T; |
f9896bf3 IM |
1377 | |
1378 | entry = iter->ent; | |
1379 | ||
1380 | ret = trace_seq_printf(s, "%d %d %llu ", | |
1381 | entry->pid, iter->cpu, entry->t); | |
1382 | if (!ret) | |
1383 | return 0; | |
1384 | ||
1385 | switch (entry->type) { | |
1386 | case TRACE_FN: | |
1387 | ret = trace_seq_printf(s, "%x %x\n", | |
1388 | entry->fn.ip, entry->fn.parent_ip); | |
1389 | if (!ret) | |
1390 | return 0; | |
1391 | break; | |
1392 | case TRACE_CTX: | |
57422797 | 1393 | case TRACE_WAKE: |
f9896bf3 IM |
1394 | S = entry->ctx.prev_state < sizeof(state_to_char) ? |
1395 | state_to_char[entry->ctx.prev_state] : 'X'; | |
bac524d3 PZ |
1396 | T = entry->ctx.next_state < sizeof(state_to_char) ? |
1397 | state_to_char[entry->ctx.next_state] : 'X'; | |
57422797 IM |
1398 | if (entry->type == TRACE_WAKE) |
1399 | S = '+'; | |
bac524d3 | 1400 | ret = trace_seq_printf(s, "%d %d %c %d %d %c\n", |
f9896bf3 IM |
1401 | entry->ctx.prev_pid, |
1402 | entry->ctx.prev_prio, | |
1403 | S, | |
1404 | entry->ctx.next_pid, | |
bac524d3 PZ |
1405 | entry->ctx.next_prio, |
1406 | T); | |
f9896bf3 IM |
1407 | if (!ret) |
1408 | return 0; | |
1409 | break; | |
f0a920d5 | 1410 | case TRACE_SPECIAL: |
86387f7e | 1411 | case TRACE_STACK: |
88a4216c | 1412 | ret = trace_seq_printf(s, "# %ld %ld %ld\n", |
f0a920d5 IM |
1413 | entry->special.arg1, |
1414 | entry->special.arg2, | |
1415 | entry->special.arg3); | |
1416 | if (!ret) | |
1417 | return 0; | |
1418 | break; | |
f9896bf3 IM |
1419 | } |
1420 | return 1; | |
1421 | } | |
1422 | ||
cb0f12aa IM |
1423 | #define SEQ_PUT_FIELD_RET(s, x) \ |
1424 | do { \ | |
1425 | if (!trace_seq_putmem(s, &(x), sizeof(x))) \ | |
1426 | return 0; \ | |
1427 | } while (0) | |
1428 | ||
5e3ca0ec IM |
1429 | #define SEQ_PUT_HEX_FIELD_RET(s, x) \ |
1430 | do { \ | |
1431 | if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \ | |
1432 | return 0; \ | |
1433 | } while (0) | |
1434 | ||
e309b41d | 1435 | static int print_hex_fmt(struct trace_iterator *iter) |
5e3ca0ec IM |
1436 | { |
1437 | struct trace_seq *s = &iter->seq; | |
1438 | unsigned char newline = '\n'; | |
1439 | struct trace_entry *entry; | |
bac524d3 | 1440 | int S, T; |
5e3ca0ec IM |
1441 | |
1442 | entry = iter->ent; | |
1443 | ||
1444 | SEQ_PUT_HEX_FIELD_RET(s, entry->pid); | |
1445 | SEQ_PUT_HEX_FIELD_RET(s, iter->cpu); | |
1446 | SEQ_PUT_HEX_FIELD_RET(s, entry->t); | |
1447 | ||
1448 | switch (entry->type) { | |
1449 | case TRACE_FN: | |
1450 | SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip); | |
1451 | SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip); | |
1452 | break; | |
1453 | case TRACE_CTX: | |
57422797 | 1454 | case TRACE_WAKE: |
5e3ca0ec IM |
1455 | S = entry->ctx.prev_state < sizeof(state_to_char) ? |
1456 | state_to_char[entry->ctx.prev_state] : 'X'; | |
bac524d3 PZ |
1457 | T = entry->ctx.next_state < sizeof(state_to_char) ? |
1458 | state_to_char[entry->ctx.next_state] : 'X'; | |
57422797 IM |
1459 | if (entry->type == TRACE_WAKE) |
1460 | S = '+'; | |
5e3ca0ec IM |
1461 | SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid); |
1462 | SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio); | |
1463 | SEQ_PUT_HEX_FIELD_RET(s, S); | |
1464 | SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid); | |
1465 | SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio); | |
1466 | SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip); | |
bac524d3 | 1467 | SEQ_PUT_HEX_FIELD_RET(s, T); |
5e3ca0ec IM |
1468 | break; |
1469 | case TRACE_SPECIAL: | |
86387f7e | 1470 | case TRACE_STACK: |
5e3ca0ec IM |
1471 | SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1); |
1472 | SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2); | |
1473 | SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3); | |
1474 | break; | |
1475 | } | |
1476 | SEQ_PUT_FIELD_RET(s, newline); | |
1477 | ||
1478 | return 1; | |
1479 | } | |
1480 | ||
e309b41d | 1481 | static int print_bin_fmt(struct trace_iterator *iter) |
cb0f12aa IM |
1482 | { |
1483 | struct trace_seq *s = &iter->seq; | |
1484 | struct trace_entry *entry; | |
1485 | ||
1486 | entry = iter->ent; | |
1487 | ||
1488 | SEQ_PUT_FIELD_RET(s, entry->pid); | |
1489 | SEQ_PUT_FIELD_RET(s, entry->cpu); | |
1490 | SEQ_PUT_FIELD_RET(s, entry->t); | |
1491 | ||
1492 | switch (entry->type) { | |
1493 | case TRACE_FN: | |
1494 | SEQ_PUT_FIELD_RET(s, entry->fn.ip); | |
1495 | SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip); | |
1496 | break; | |
1497 | case TRACE_CTX: | |
1498 | SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid); | |
1499 | SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio); | |
1500 | SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state); | |
1501 | SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid); | |
1502 | SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio); | |
bac524d3 | 1503 | SEQ_PUT_FIELD_RET(s, entry->ctx.next_state); |
cb0f12aa | 1504 | break; |
f0a920d5 | 1505 | case TRACE_SPECIAL: |
86387f7e | 1506 | case TRACE_STACK: |
f0a920d5 IM |
1507 | SEQ_PUT_FIELD_RET(s, entry->special.arg1); |
1508 | SEQ_PUT_FIELD_RET(s, entry->special.arg2); | |
1509 | SEQ_PUT_FIELD_RET(s, entry->special.arg3); | |
1510 | break; | |
cb0f12aa IM |
1511 | } |
1512 | return 1; | |
1513 | } | |
1514 | ||
bc0c38d1 SR |
1515 | static int trace_empty(struct trace_iterator *iter) |
1516 | { | |
1517 | struct trace_array_cpu *data; | |
1518 | int cpu; | |
1519 | ||
1520 | for_each_possible_cpu(cpu) { | |
1521 | data = iter->tr->data[cpu]; | |
1522 | ||
b3806b43 SR |
1523 | if (head_page(data) && data->trace_idx && |
1524 | (data->trace_tail != data->trace_head || | |
1525 | data->trace_tail_idx != data->trace_head_idx)) | |
bc0c38d1 SR |
1526 | return 0; |
1527 | } | |
1528 | return 1; | |
1529 | } | |
1530 | ||
f9896bf3 IM |
1531 | static int print_trace_line(struct trace_iterator *iter) |
1532 | { | |
cb0f12aa IM |
1533 | if (trace_flags & TRACE_ITER_BIN) |
1534 | return print_bin_fmt(iter); | |
1535 | ||
5e3ca0ec IM |
1536 | if (trace_flags & TRACE_ITER_HEX) |
1537 | return print_hex_fmt(iter); | |
1538 | ||
f9896bf3 IM |
1539 | if (trace_flags & TRACE_ITER_RAW) |
1540 | return print_raw_fmt(iter); | |
1541 | ||
1542 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) | |
1543 | return print_lat_fmt(iter, iter->idx, iter->cpu); | |
1544 | ||
1545 | return print_trace_fmt(iter); | |
1546 | } | |
1547 | ||
bc0c38d1 SR |
1548 | static int s_show(struct seq_file *m, void *v) |
1549 | { | |
1550 | struct trace_iterator *iter = v; | |
1551 | ||
1552 | if (iter->ent == NULL) { | |
1553 | if (iter->tr) { | |
1554 | seq_printf(m, "# tracer: %s\n", iter->trace->name); | |
1555 | seq_puts(m, "#\n"); | |
1556 | } | |
1557 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) { | |
1558 | /* print nothing if the buffers are empty */ | |
1559 | if (trace_empty(iter)) | |
1560 | return 0; | |
1561 | print_trace_header(m, iter); | |
1562 | if (!(trace_flags & TRACE_ITER_VERBOSE)) | |
1563 | print_lat_help_header(m); | |
1564 | } else { | |
1565 | if (!(trace_flags & TRACE_ITER_VERBOSE)) | |
1566 | print_func_help_header(m); | |
1567 | } | |
1568 | } else { | |
f9896bf3 | 1569 | print_trace_line(iter); |
214023c3 | 1570 | trace_print_seq(m, &iter->seq); |
bc0c38d1 SR |
1571 | } |
1572 | ||
1573 | return 0; | |
1574 | } | |
1575 | ||
1576 | static struct seq_operations tracer_seq_ops = { | |
4bf39a94 IM |
1577 | .start = s_start, |
1578 | .next = s_next, | |
1579 | .stop = s_stop, | |
1580 | .show = s_show, | |
bc0c38d1 SR |
1581 | }; |
1582 | ||
e309b41d | 1583 | static struct trace_iterator * |
bc0c38d1 SR |
1584 | __tracing_open(struct inode *inode, struct file *file, int *ret) |
1585 | { | |
1586 | struct trace_iterator *iter; | |
1587 | ||
60a11774 SR |
1588 | if (tracing_disabled) { |
1589 | *ret = -ENODEV; | |
1590 | return NULL; | |
1591 | } | |
1592 | ||
bc0c38d1 SR |
1593 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
1594 | if (!iter) { | |
1595 | *ret = -ENOMEM; | |
1596 | goto out; | |
1597 | } | |
1598 | ||
1599 | mutex_lock(&trace_types_lock); | |
1600 | if (current_trace && current_trace->print_max) | |
1601 | iter->tr = &max_tr; | |
1602 | else | |
1603 | iter->tr = inode->i_private; | |
1604 | iter->trace = current_trace; | |
1605 | iter->pos = -1; | |
1606 | ||
1607 | /* TODO stop tracer */ | |
1608 | *ret = seq_open(file, &tracer_seq_ops); | |
1609 | if (!*ret) { | |
1610 | struct seq_file *m = file->private_data; | |
1611 | m->private = iter; | |
1612 | ||
1613 | /* stop the trace while dumping */ | |
1614 | if (iter->tr->ctrl) | |
1615 | tracer_enabled = 0; | |
1616 | ||
1617 | if (iter->trace && iter->trace->open) | |
1618 | iter->trace->open(iter); | |
1619 | } else { | |
1620 | kfree(iter); | |
1621 | iter = NULL; | |
1622 | } | |
1623 | mutex_unlock(&trace_types_lock); | |
1624 | ||
1625 | out: | |
1626 | return iter; | |
1627 | } | |
1628 | ||
1629 | int tracing_open_generic(struct inode *inode, struct file *filp) | |
1630 | { | |
60a11774 SR |
1631 | if (tracing_disabled) |
1632 | return -ENODEV; | |
1633 | ||
bc0c38d1 SR |
1634 | filp->private_data = inode->i_private; |
1635 | return 0; | |
1636 | } | |
1637 | ||
1638 | int tracing_release(struct inode *inode, struct file *file) | |
1639 | { | |
1640 | struct seq_file *m = (struct seq_file *)file->private_data; | |
1641 | struct trace_iterator *iter = m->private; | |
1642 | ||
1643 | mutex_lock(&trace_types_lock); | |
1644 | if (iter->trace && iter->trace->close) | |
1645 | iter->trace->close(iter); | |
1646 | ||
1647 | /* reenable tracing if it was previously enabled */ | |
1648 | if (iter->tr->ctrl) | |
1649 | tracer_enabled = 1; | |
1650 | mutex_unlock(&trace_types_lock); | |
1651 | ||
1652 | seq_release(inode, file); | |
1653 | kfree(iter); | |
1654 | return 0; | |
1655 | } | |
1656 | ||
1657 | static int tracing_open(struct inode *inode, struct file *file) | |
1658 | { | |
1659 | int ret; | |
1660 | ||
1661 | __tracing_open(inode, file, &ret); | |
1662 | ||
1663 | return ret; | |
1664 | } | |
1665 | ||
1666 | static int tracing_lt_open(struct inode *inode, struct file *file) | |
1667 | { | |
1668 | struct trace_iterator *iter; | |
1669 | int ret; | |
1670 | ||
1671 | iter = __tracing_open(inode, file, &ret); | |
1672 | ||
1673 | if (!ret) | |
1674 | iter->iter_flags |= TRACE_FILE_LAT_FMT; | |
1675 | ||
1676 | return ret; | |
1677 | } | |
1678 | ||
1679 | ||
e309b41d | 1680 | static void * |
bc0c38d1 SR |
1681 | t_next(struct seq_file *m, void *v, loff_t *pos) |
1682 | { | |
1683 | struct tracer *t = m->private; | |
1684 | ||
1685 | (*pos)++; | |
1686 | ||
1687 | if (t) | |
1688 | t = t->next; | |
1689 | ||
1690 | m->private = t; | |
1691 | ||
1692 | return t; | |
1693 | } | |
1694 | ||
1695 | static void *t_start(struct seq_file *m, loff_t *pos) | |
1696 | { | |
1697 | struct tracer *t = m->private; | |
1698 | loff_t l = 0; | |
1699 | ||
1700 | mutex_lock(&trace_types_lock); | |
1701 | for (; t && l < *pos; t = t_next(m, t, &l)) | |
1702 | ; | |
1703 | ||
1704 | return t; | |
1705 | } | |
1706 | ||
1707 | static void t_stop(struct seq_file *m, void *p) | |
1708 | { | |
1709 | mutex_unlock(&trace_types_lock); | |
1710 | } | |
1711 | ||
1712 | static int t_show(struct seq_file *m, void *v) | |
1713 | { | |
1714 | struct tracer *t = v; | |
1715 | ||
1716 | if (!t) | |
1717 | return 0; | |
1718 | ||
1719 | seq_printf(m, "%s", t->name); | |
1720 | if (t->next) | |
1721 | seq_putc(m, ' '); | |
1722 | else | |
1723 | seq_putc(m, '\n'); | |
1724 | ||
1725 | return 0; | |
1726 | } | |
1727 | ||
1728 | static struct seq_operations show_traces_seq_ops = { | |
4bf39a94 IM |
1729 | .start = t_start, |
1730 | .next = t_next, | |
1731 | .stop = t_stop, | |
1732 | .show = t_show, | |
bc0c38d1 SR |
1733 | }; |
1734 | ||
1735 | static int show_traces_open(struct inode *inode, struct file *file) | |
1736 | { | |
1737 | int ret; | |
1738 | ||
60a11774 SR |
1739 | if (tracing_disabled) |
1740 | return -ENODEV; | |
1741 | ||
bc0c38d1 SR |
1742 | ret = seq_open(file, &show_traces_seq_ops); |
1743 | if (!ret) { | |
1744 | struct seq_file *m = file->private_data; | |
1745 | m->private = trace_types; | |
1746 | } | |
1747 | ||
1748 | return ret; | |
1749 | } | |
1750 | ||
1751 | static struct file_operations tracing_fops = { | |
4bf39a94 IM |
1752 | .open = tracing_open, |
1753 | .read = seq_read, | |
1754 | .llseek = seq_lseek, | |
1755 | .release = tracing_release, | |
bc0c38d1 SR |
1756 | }; |
1757 | ||
1758 | static struct file_operations tracing_lt_fops = { | |
4bf39a94 IM |
1759 | .open = tracing_lt_open, |
1760 | .read = seq_read, | |
1761 | .llseek = seq_lseek, | |
1762 | .release = tracing_release, | |
bc0c38d1 SR |
1763 | }; |
1764 | ||
1765 | static struct file_operations show_traces_fops = { | |
c7078de1 IM |
1766 | .open = show_traces_open, |
1767 | .read = seq_read, | |
1768 | .release = seq_release, | |
1769 | }; | |
1770 | ||
36dfe925 IM |
1771 | /* |
1772 | * Only trace on a CPU if the bitmask is set: | |
1773 | */ | |
1774 | static cpumask_t tracing_cpumask = CPU_MASK_ALL; | |
1775 | ||
1776 | /* | |
1777 | * When tracing/tracing_cpu_mask is modified then this holds | |
1778 | * the new bitmask we are about to install: | |
1779 | */ | |
1780 | static cpumask_t tracing_cpumask_new; | |
1781 | ||
1782 | /* | |
1783 | * The tracer itself will not take this lock, but still we want | |
1784 | * to provide a consistent cpumask to user-space: | |
1785 | */ | |
1786 | static DEFINE_MUTEX(tracing_cpumask_update_lock); | |
1787 | ||
1788 | /* | |
1789 | * Temporary storage for the character representation of the | |
1790 | * CPU bitmask (and one more byte for the newline): | |
1791 | */ | |
1792 | static char mask_str[NR_CPUS + 1]; | |
1793 | ||
c7078de1 IM |
1794 | static ssize_t |
1795 | tracing_cpumask_read(struct file *filp, char __user *ubuf, | |
1796 | size_t count, loff_t *ppos) | |
1797 | { | |
36dfe925 | 1798 | int len; |
c7078de1 IM |
1799 | |
1800 | mutex_lock(&tracing_cpumask_update_lock); | |
36dfe925 IM |
1801 | |
1802 | len = cpumask_scnprintf(mask_str, count, tracing_cpumask); | |
1803 | if (count - len < 2) { | |
1804 | count = -EINVAL; | |
1805 | goto out_err; | |
1806 | } | |
1807 | len += sprintf(mask_str + len, "\n"); | |
1808 | count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1); | |
1809 | ||
1810 | out_err: | |
c7078de1 IM |
1811 | mutex_unlock(&tracing_cpumask_update_lock); |
1812 | ||
1813 | return count; | |
1814 | } | |
1815 | ||
1816 | static ssize_t | |
1817 | tracing_cpumask_write(struct file *filp, const char __user *ubuf, | |
1818 | size_t count, loff_t *ppos) | |
1819 | { | |
36dfe925 | 1820 | int err, cpu; |
c7078de1 IM |
1821 | |
1822 | mutex_lock(&tracing_cpumask_update_lock); | |
36dfe925 | 1823 | err = cpumask_parse_user(ubuf, count, tracing_cpumask_new); |
c7078de1 | 1824 | if (err) |
36dfe925 IM |
1825 | goto err_unlock; |
1826 | ||
1827 | spin_lock_irq(&ftrace_max_lock); | |
1828 | for_each_possible_cpu(cpu) { | |
1829 | /* | |
1830 | * Increase/decrease the disabled counter if we are | |
1831 | * about to flip a bit in the cpumask: | |
1832 | */ | |
1833 | if (cpu_isset(cpu, tracing_cpumask) && | |
1834 | !cpu_isset(cpu, tracing_cpumask_new)) { | |
1835 | atomic_inc(&global_trace.data[cpu]->disabled); | |
1836 | } | |
1837 | if (!cpu_isset(cpu, tracing_cpumask) && | |
1838 | cpu_isset(cpu, tracing_cpumask_new)) { | |
1839 | atomic_dec(&global_trace.data[cpu]->disabled); | |
1840 | } | |
1841 | } | |
1842 | spin_unlock_irq(&ftrace_max_lock); | |
1843 | ||
1844 | tracing_cpumask = tracing_cpumask_new; | |
1845 | ||
1846 | mutex_unlock(&tracing_cpumask_update_lock); | |
c7078de1 IM |
1847 | |
1848 | return count; | |
36dfe925 IM |
1849 | |
1850 | err_unlock: | |
1851 | mutex_unlock(&tracing_cpumask_update_lock); | |
1852 | ||
1853 | return err; | |
c7078de1 IM |
1854 | } |
1855 | ||
1856 | static struct file_operations tracing_cpumask_fops = { | |
1857 | .open = tracing_open_generic, | |
1858 | .read = tracing_cpumask_read, | |
1859 | .write = tracing_cpumask_write, | |
bc0c38d1 SR |
1860 | }; |
1861 | ||
1862 | static ssize_t | |
1863 | tracing_iter_ctrl_read(struct file *filp, char __user *ubuf, | |
1864 | size_t cnt, loff_t *ppos) | |
1865 | { | |
1866 | char *buf; | |
1867 | int r = 0; | |
1868 | int len = 0; | |
1869 | int i; | |
1870 | ||
1871 | /* calulate max size */ | |
1872 | for (i = 0; trace_options[i]; i++) { | |
1873 | len += strlen(trace_options[i]); | |
1874 | len += 3; /* "no" and space */ | |
1875 | } | |
1876 | ||
1877 | /* +2 for \n and \0 */ | |
1878 | buf = kmalloc(len + 2, GFP_KERNEL); | |
1879 | if (!buf) | |
1880 | return -ENOMEM; | |
1881 | ||
1882 | for (i = 0; trace_options[i]; i++) { | |
1883 | if (trace_flags & (1 << i)) | |
1884 | r += sprintf(buf + r, "%s ", trace_options[i]); | |
1885 | else | |
1886 | r += sprintf(buf + r, "no%s ", trace_options[i]); | |
1887 | } | |
1888 | ||
1889 | r += sprintf(buf + r, "\n"); | |
1890 | WARN_ON(r >= len + 2); | |
1891 | ||
36dfe925 | 1892 | r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
bc0c38d1 SR |
1893 | |
1894 | kfree(buf); | |
1895 | ||
1896 | return r; | |
1897 | } | |
1898 | ||
1899 | static ssize_t | |
1900 | tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf, | |
1901 | size_t cnt, loff_t *ppos) | |
1902 | { | |
1903 | char buf[64]; | |
1904 | char *cmp = buf; | |
1905 | int neg = 0; | |
1906 | int i; | |
1907 | ||
1908 | if (cnt > 63) | |
1909 | cnt = 63; | |
1910 | ||
1911 | if (copy_from_user(&buf, ubuf, cnt)) | |
1912 | return -EFAULT; | |
1913 | ||
1914 | buf[cnt] = 0; | |
1915 | ||
1916 | if (strncmp(buf, "no", 2) == 0) { | |
1917 | neg = 1; | |
1918 | cmp += 2; | |
1919 | } | |
1920 | ||
1921 | for (i = 0; trace_options[i]; i++) { | |
1922 | int len = strlen(trace_options[i]); | |
1923 | ||
1924 | if (strncmp(cmp, trace_options[i], len) == 0) { | |
1925 | if (neg) | |
1926 | trace_flags &= ~(1 << i); | |
1927 | else | |
1928 | trace_flags |= (1 << i); | |
1929 | break; | |
1930 | } | |
1931 | } | |
442e544c IM |
1932 | /* |
1933 | * If no option could be set, return an error: | |
1934 | */ | |
1935 | if (!trace_options[i]) | |
1936 | return -EINVAL; | |
bc0c38d1 SR |
1937 | |
1938 | filp->f_pos += cnt; | |
1939 | ||
1940 | return cnt; | |
1941 | } | |
1942 | ||
1943 | static struct file_operations tracing_iter_fops = { | |
c7078de1 IM |
1944 | .open = tracing_open_generic, |
1945 | .read = tracing_iter_ctrl_read, | |
1946 | .write = tracing_iter_ctrl_write, | |
bc0c38d1 SR |
1947 | }; |
1948 | ||
7bd2f24c IM |
1949 | static const char readme_msg[] = |
1950 | "tracing mini-HOWTO:\n\n" | |
1951 | "# mkdir /debug\n" | |
1952 | "# mount -t debugfs nodev /debug\n\n" | |
1953 | "# cat /debug/tracing/available_tracers\n" | |
1954 | "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n" | |
1955 | "# cat /debug/tracing/current_tracer\n" | |
1956 | "none\n" | |
1957 | "# echo sched_switch > /debug/tracing/current_tracer\n" | |
1958 | "# cat /debug/tracing/current_tracer\n" | |
1959 | "sched_switch\n" | |
1960 | "# cat /debug/tracing/iter_ctrl\n" | |
1961 | "noprint-parent nosym-offset nosym-addr noverbose\n" | |
1962 | "# echo print-parent > /debug/tracing/iter_ctrl\n" | |
1963 | "# echo 1 > /debug/tracing/tracing_enabled\n" | |
1964 | "# cat /debug/tracing/trace > /tmp/trace.txt\n" | |
1965 | "echo 0 > /debug/tracing/tracing_enabled\n" | |
1966 | ; | |
1967 | ||
1968 | static ssize_t | |
1969 | tracing_readme_read(struct file *filp, char __user *ubuf, | |
1970 | size_t cnt, loff_t *ppos) | |
1971 | { | |
1972 | return simple_read_from_buffer(ubuf, cnt, ppos, | |
1973 | readme_msg, strlen(readme_msg)); | |
1974 | } | |
1975 | ||
1976 | static struct file_operations tracing_readme_fops = { | |
c7078de1 IM |
1977 | .open = tracing_open_generic, |
1978 | .read = tracing_readme_read, | |
7bd2f24c IM |
1979 | }; |
1980 | ||
bc0c38d1 SR |
1981 | static ssize_t |
1982 | tracing_ctrl_read(struct file *filp, char __user *ubuf, | |
1983 | size_t cnt, loff_t *ppos) | |
1984 | { | |
1985 | struct trace_array *tr = filp->private_data; | |
1986 | char buf[64]; | |
1987 | int r; | |
1988 | ||
1989 | r = sprintf(buf, "%ld\n", tr->ctrl); | |
4e3c3333 | 1990 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
bc0c38d1 SR |
1991 | } |
1992 | ||
1993 | static ssize_t | |
1994 | tracing_ctrl_write(struct file *filp, const char __user *ubuf, | |
1995 | size_t cnt, loff_t *ppos) | |
1996 | { | |
1997 | struct trace_array *tr = filp->private_data; | |
1998 | long val; | |
1999 | char buf[64]; | |
2000 | ||
2001 | if (cnt > 63) | |
2002 | cnt = 63; | |
2003 | ||
2004 | if (copy_from_user(&buf, ubuf, cnt)) | |
2005 | return -EFAULT; | |
2006 | ||
2007 | buf[cnt] = 0; | |
2008 | ||
2009 | val = simple_strtoul(buf, NULL, 10); | |
2010 | ||
2011 | val = !!val; | |
2012 | ||
2013 | mutex_lock(&trace_types_lock); | |
2014 | if (tr->ctrl ^ val) { | |
2015 | if (val) | |
2016 | tracer_enabled = 1; | |
2017 | else | |
2018 | tracer_enabled = 0; | |
2019 | ||
2020 | tr->ctrl = val; | |
2021 | ||
2022 | if (current_trace && current_trace->ctrl_update) | |
2023 | current_trace->ctrl_update(tr); | |
2024 | } | |
2025 | mutex_unlock(&trace_types_lock); | |
2026 | ||
2027 | filp->f_pos += cnt; | |
2028 | ||
2029 | return cnt; | |
2030 | } | |
2031 | ||
2032 | static ssize_t | |
2033 | tracing_set_trace_read(struct file *filp, char __user *ubuf, | |
2034 | size_t cnt, loff_t *ppos) | |
2035 | { | |
2036 | char buf[max_tracer_type_len+2]; | |
2037 | int r; | |
2038 | ||
2039 | mutex_lock(&trace_types_lock); | |
2040 | if (current_trace) | |
2041 | r = sprintf(buf, "%s\n", current_trace->name); | |
2042 | else | |
2043 | r = sprintf(buf, "\n"); | |
2044 | mutex_unlock(&trace_types_lock); | |
2045 | ||
4bf39a94 | 2046 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
bc0c38d1 SR |
2047 | } |
2048 | ||
2049 | static ssize_t | |
2050 | tracing_set_trace_write(struct file *filp, const char __user *ubuf, | |
2051 | size_t cnt, loff_t *ppos) | |
2052 | { | |
2053 | struct trace_array *tr = &global_trace; | |
2054 | struct tracer *t; | |
2055 | char buf[max_tracer_type_len+1]; | |
2056 | int i; | |
2057 | ||
2058 | if (cnt > max_tracer_type_len) | |
2059 | cnt = max_tracer_type_len; | |
2060 | ||
2061 | if (copy_from_user(&buf, ubuf, cnt)) | |
2062 | return -EFAULT; | |
2063 | ||
2064 | buf[cnt] = 0; | |
2065 | ||
2066 | /* strip ending whitespace. */ | |
2067 | for (i = cnt - 1; i > 0 && isspace(buf[i]); i--) | |
2068 | buf[i] = 0; | |
2069 | ||
2070 | mutex_lock(&trace_types_lock); | |
2071 | for (t = trace_types; t; t = t->next) { | |
2072 | if (strcmp(t->name, buf) == 0) | |
2073 | break; | |
2074 | } | |
2075 | if (!t || t == current_trace) | |
2076 | goto out; | |
2077 | ||
2078 | if (current_trace && current_trace->reset) | |
2079 | current_trace->reset(tr); | |
2080 | ||
2081 | current_trace = t; | |
2082 | if (t->init) | |
2083 | t->init(tr); | |
2084 | ||
2085 | out: | |
2086 | mutex_unlock(&trace_types_lock); | |
2087 | ||
2088 | filp->f_pos += cnt; | |
2089 | ||
2090 | return cnt; | |
2091 | } | |
2092 | ||
2093 | static ssize_t | |
2094 | tracing_max_lat_read(struct file *filp, char __user *ubuf, | |
2095 | size_t cnt, loff_t *ppos) | |
2096 | { | |
2097 | unsigned long *ptr = filp->private_data; | |
2098 | char buf[64]; | |
2099 | int r; | |
2100 | ||
2101 | r = snprintf(buf, 64, "%ld\n", | |
2102 | *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr)); | |
2103 | if (r > 64) | |
2104 | r = 64; | |
4bf39a94 | 2105 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
bc0c38d1 SR |
2106 | } |
2107 | ||
2108 | static ssize_t | |
2109 | tracing_max_lat_write(struct file *filp, const char __user *ubuf, | |
2110 | size_t cnt, loff_t *ppos) | |
2111 | { | |
2112 | long *ptr = filp->private_data; | |
2113 | long val; | |
2114 | char buf[64]; | |
2115 | ||
2116 | if (cnt > 63) | |
2117 | cnt = 63; | |
2118 | ||
2119 | if (copy_from_user(&buf, ubuf, cnt)) | |
2120 | return -EFAULT; | |
2121 | ||
2122 | buf[cnt] = 0; | |
2123 | ||
2124 | val = simple_strtoul(buf, NULL, 10); | |
2125 | ||
2126 | *ptr = val * 1000; | |
2127 | ||
2128 | return cnt; | |
2129 | } | |
2130 | ||
b3806b43 SR |
2131 | static atomic_t tracing_reader; |
2132 | ||
2133 | static int tracing_open_pipe(struct inode *inode, struct file *filp) | |
2134 | { | |
2135 | struct trace_iterator *iter; | |
2136 | ||
2137 | if (tracing_disabled) | |
2138 | return -ENODEV; | |
2139 | ||
2140 | /* We only allow for reader of the pipe */ | |
2141 | if (atomic_inc_return(&tracing_reader) != 1) { | |
2142 | atomic_dec(&tracing_reader); | |
2143 | return -EBUSY; | |
2144 | } | |
2145 | ||
2146 | /* create a buffer to store the information to pass to userspace */ | |
2147 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); | |
2148 | if (!iter) | |
2149 | return -ENOMEM; | |
2150 | ||
2151 | iter->tr = &global_trace; | |
2152 | ||
2153 | filp->private_data = iter; | |
2154 | ||
2155 | return 0; | |
2156 | } | |
2157 | ||
2158 | static int tracing_release_pipe(struct inode *inode, struct file *file) | |
2159 | { | |
2160 | struct trace_iterator *iter = file->private_data; | |
2161 | ||
2162 | kfree(iter); | |
2163 | atomic_dec(&tracing_reader); | |
2164 | ||
2165 | return 0; | |
2166 | } | |
2167 | ||
2a2cc8f7 SSP |
2168 | static unsigned int |
2169 | tracing_poll_pipe(struct file *filp, poll_table *poll_table) | |
2170 | { | |
2171 | struct trace_iterator *iter = filp->private_data; | |
2172 | ||
2173 | if (trace_flags & TRACE_ITER_BLOCK) { | |
2174 | /* | |
2175 | * Always select as readable when in blocking mode | |
2176 | */ | |
2177 | return POLLIN | POLLRDNORM; | |
2178 | } | |
2179 | else { | |
2180 | if (!trace_empty(iter)) | |
2181 | return POLLIN | POLLRDNORM; | |
2182 | poll_wait(filp, &trace_wait, poll_table); | |
2183 | if (!trace_empty(iter)) | |
2184 | return POLLIN | POLLRDNORM; | |
2185 | ||
2186 | return 0; | |
2187 | } | |
2188 | } | |
2189 | ||
b3806b43 SR |
2190 | /* |
2191 | * Consumer reader. | |
2192 | */ | |
2193 | static ssize_t | |
2194 | tracing_read_pipe(struct file *filp, char __user *ubuf, | |
2195 | size_t cnt, loff_t *ppos) | |
2196 | { | |
2197 | struct trace_iterator *iter = filp->private_data; | |
2198 | struct trace_array_cpu *data; | |
2199 | static cpumask_t mask; | |
b3806b43 SR |
2200 | static int start; |
2201 | unsigned long flags; | |
25770467 | 2202 | #ifdef CONFIG_FTRACE |
2e0f5761 | 2203 | int ftrace_save; |
25770467 | 2204 | #endif |
b3806b43 SR |
2205 | int read = 0; |
2206 | int cpu; | |
2207 | int len; | |
2208 | int ret; | |
2209 | ||
2210 | /* return any leftover data */ | |
2211 | if (iter->seq.len > start) { | |
2212 | len = iter->seq.len - start; | |
2213 | if (cnt > len) | |
2214 | cnt = len; | |
2215 | ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt); | |
2216 | if (ret) | |
2217 | cnt = -EFAULT; | |
2218 | ||
2219 | start += len; | |
2220 | ||
2221 | return cnt; | |
2222 | } | |
2223 | ||
2224 | trace_seq_reset(&iter->seq); | |
2225 | start = 0; | |
2226 | ||
2227 | while (trace_empty(iter)) { | |
2a2cc8f7 SSP |
2228 | if (!(trace_flags & TRACE_ITER_BLOCK)) |
2229 | return -EWOULDBLOCK; | |
b3806b43 SR |
2230 | /* |
2231 | * This is a make-shift waitqueue. The reason we don't use | |
2232 | * an actual wait queue is because: | |
2233 | * 1) we only ever have one waiter | |
2234 | * 2) the tracing, traces all functions, we don't want | |
2235 | * the overhead of calling wake_up and friends | |
2236 | * (and tracing them too) | |
2237 | * Anyway, this is really very primitive wakeup. | |
2238 | */ | |
2239 | set_current_state(TASK_INTERRUPTIBLE); | |
2240 | iter->tr->waiter = current; | |
2241 | ||
2242 | /* sleep for one second, and try again. */ | |
2243 | schedule_timeout(HZ); | |
2244 | ||
2245 | iter->tr->waiter = NULL; | |
2246 | ||
2247 | if (signal_pending(current)) | |
2248 | return -EINTR; | |
2249 | ||
2250 | /* | |
2251 | * We block until we read something and tracing is disabled. | |
2252 | * We still block if tracing is disabled, but we have never | |
2253 | * read anything. This allows a user to cat this file, and | |
2254 | * then enable tracing. But after we have read something, | |
2255 | * we give an EOF when tracing is again disabled. | |
2256 | * | |
2257 | * iter->pos will be 0 if we haven't read anything. | |
2258 | */ | |
2259 | if (!tracer_enabled && iter->pos) | |
2260 | break; | |
2261 | ||
2262 | continue; | |
2263 | } | |
2264 | ||
2265 | /* stop when tracing is finished */ | |
2266 | if (trace_empty(iter)) | |
2267 | return 0; | |
2268 | ||
2269 | if (cnt >= PAGE_SIZE) | |
2270 | cnt = PAGE_SIZE - 1; | |
2271 | ||
2272 | memset(iter, 0, sizeof(*iter)); | |
2273 | iter->tr = &global_trace; | |
2274 | iter->pos = -1; | |
2275 | ||
2276 | /* | |
2277 | * We need to stop all tracing on all CPUS to read the | |
2278 | * the next buffer. This is a bit expensive, but is | |
2279 | * not done often. We fill all what we can read, | |
2280 | * and then release the locks again. | |
2281 | */ | |
2282 | ||
2283 | cpus_clear(mask); | |
2284 | local_irq_save(flags); | |
25770467 | 2285 | #ifdef CONFIG_FTRACE |
2e0f5761 IM |
2286 | ftrace_save = ftrace_enabled; |
2287 | ftrace_enabled = 0; | |
25770467 | 2288 | #endif |
2e0f5761 | 2289 | smp_wmb(); |
b3806b43 SR |
2290 | for_each_possible_cpu(cpu) { |
2291 | data = iter->tr->data[cpu]; | |
2292 | ||
2293 | if (!head_page(data) || !data->trace_idx) | |
2294 | continue; | |
2295 | ||
2296 | atomic_inc(&data->disabled); | |
b3806b43 SR |
2297 | cpu_set(cpu, mask); |
2298 | } | |
2299 | ||
2e0f5761 IM |
2300 | for_each_cpu_mask(cpu, mask) { |
2301 | data = iter->tr->data[cpu]; | |
2302 | spin_lock(&data->lock); | |
2303 | } | |
2304 | ||
088b1e42 SR |
2305 | while (find_next_entry_inc(iter) != NULL) { |
2306 | int len = iter->seq.len; | |
2307 | ||
f9896bf3 | 2308 | ret = print_trace_line(iter); |
088b1e42 SR |
2309 | if (!ret) { |
2310 | /* don't print partial lines */ | |
2311 | iter->seq.len = len; | |
b3806b43 | 2312 | break; |
088b1e42 | 2313 | } |
b3806b43 SR |
2314 | |
2315 | trace_consume(iter); | |
2316 | ||
2317 | if (iter->seq.len >= cnt) | |
2318 | break; | |
b3806b43 SR |
2319 | } |
2320 | ||
d4c5a2f5 | 2321 | for_each_cpu_mask(cpu, mask) { |
b3806b43 | 2322 | data = iter->tr->data[cpu]; |
b3806b43 | 2323 | spin_unlock(&data->lock); |
2e0f5761 IM |
2324 | } |
2325 | ||
2326 | for_each_cpu_mask(cpu, mask) { | |
2327 | data = iter->tr->data[cpu]; | |
b3806b43 SR |
2328 | atomic_dec(&data->disabled); |
2329 | } | |
25770467 | 2330 | #ifdef CONFIG_FTRACE |
2e0f5761 | 2331 | ftrace_enabled = ftrace_save; |
25770467 | 2332 | #endif |
b3806b43 SR |
2333 | local_irq_restore(flags); |
2334 | ||
2335 | /* Now copy what we have to the user */ | |
2336 | read = iter->seq.len; | |
2337 | if (read > cnt) | |
2338 | read = cnt; | |
2339 | ||
2340 | ret = copy_to_user(ubuf, iter->seq.buffer, read); | |
2341 | ||
2342 | if (read < iter->seq.len) | |
2343 | start = read; | |
2344 | else | |
2345 | trace_seq_reset(&iter->seq); | |
2346 | ||
2347 | if (ret) | |
2348 | read = -EFAULT; | |
2349 | ||
2350 | return read; | |
2351 | } | |
2352 | ||
bc0c38d1 | 2353 | static struct file_operations tracing_max_lat_fops = { |
4bf39a94 IM |
2354 | .open = tracing_open_generic, |
2355 | .read = tracing_max_lat_read, | |
2356 | .write = tracing_max_lat_write, | |
bc0c38d1 SR |
2357 | }; |
2358 | ||
2359 | static struct file_operations tracing_ctrl_fops = { | |
4bf39a94 IM |
2360 | .open = tracing_open_generic, |
2361 | .read = tracing_ctrl_read, | |
2362 | .write = tracing_ctrl_write, | |
bc0c38d1 SR |
2363 | }; |
2364 | ||
2365 | static struct file_operations set_tracer_fops = { | |
4bf39a94 IM |
2366 | .open = tracing_open_generic, |
2367 | .read = tracing_set_trace_read, | |
2368 | .write = tracing_set_trace_write, | |
bc0c38d1 SR |
2369 | }; |
2370 | ||
b3806b43 | 2371 | static struct file_operations tracing_pipe_fops = { |
4bf39a94 | 2372 | .open = tracing_open_pipe, |
2a2cc8f7 | 2373 | .poll = tracing_poll_pipe, |
4bf39a94 IM |
2374 | .read = tracing_read_pipe, |
2375 | .release = tracing_release_pipe, | |
b3806b43 SR |
2376 | }; |
2377 | ||
bc0c38d1 SR |
2378 | #ifdef CONFIG_DYNAMIC_FTRACE |
2379 | ||
2380 | static ssize_t | |
2381 | tracing_read_long(struct file *filp, char __user *ubuf, | |
2382 | size_t cnt, loff_t *ppos) | |
2383 | { | |
2384 | unsigned long *p = filp->private_data; | |
2385 | char buf[64]; | |
2386 | int r; | |
2387 | ||
2388 | r = sprintf(buf, "%ld\n", *p); | |
4bf39a94 IM |
2389 | |
2390 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); | |
bc0c38d1 SR |
2391 | } |
2392 | ||
2393 | static struct file_operations tracing_read_long_fops = { | |
4bf39a94 IM |
2394 | .open = tracing_open_generic, |
2395 | .read = tracing_read_long, | |
bc0c38d1 SR |
2396 | }; |
2397 | #endif | |
2398 | ||
2399 | static struct dentry *d_tracer; | |
2400 | ||
2401 | struct dentry *tracing_init_dentry(void) | |
2402 | { | |
2403 | static int once; | |
2404 | ||
2405 | if (d_tracer) | |
2406 | return d_tracer; | |
2407 | ||
2408 | d_tracer = debugfs_create_dir("tracing", NULL); | |
2409 | ||
2410 | if (!d_tracer && !once) { | |
2411 | once = 1; | |
2412 | pr_warning("Could not create debugfs directory 'tracing'\n"); | |
2413 | return NULL; | |
2414 | } | |
2415 | ||
2416 | return d_tracer; | |
2417 | } | |
2418 | ||
60a11774 SR |
2419 | #ifdef CONFIG_FTRACE_SELFTEST |
2420 | /* Let selftest have access to static functions in this file */ | |
2421 | #include "trace_selftest.c" | |
2422 | #endif | |
2423 | ||
bc0c38d1 SR |
2424 | static __init void tracer_init_debugfs(void) |
2425 | { | |
2426 | struct dentry *d_tracer; | |
2427 | struct dentry *entry; | |
2428 | ||
2429 | d_tracer = tracing_init_dentry(); | |
2430 | ||
2431 | entry = debugfs_create_file("tracing_enabled", 0644, d_tracer, | |
2432 | &global_trace, &tracing_ctrl_fops); | |
2433 | if (!entry) | |
2434 | pr_warning("Could not create debugfs 'tracing_enabled' entry\n"); | |
2435 | ||
2436 | entry = debugfs_create_file("iter_ctrl", 0644, d_tracer, | |
2437 | NULL, &tracing_iter_fops); | |
2438 | if (!entry) | |
2439 | pr_warning("Could not create debugfs 'iter_ctrl' entry\n"); | |
2440 | ||
c7078de1 IM |
2441 | entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer, |
2442 | NULL, &tracing_cpumask_fops); | |
2443 | if (!entry) | |
2444 | pr_warning("Could not create debugfs 'tracing_cpumask' entry\n"); | |
2445 | ||
bc0c38d1 SR |
2446 | entry = debugfs_create_file("latency_trace", 0444, d_tracer, |
2447 | &global_trace, &tracing_lt_fops); | |
2448 | if (!entry) | |
2449 | pr_warning("Could not create debugfs 'latency_trace' entry\n"); | |
2450 | ||
2451 | entry = debugfs_create_file("trace", 0444, d_tracer, | |
2452 | &global_trace, &tracing_fops); | |
2453 | if (!entry) | |
2454 | pr_warning("Could not create debugfs 'trace' entry\n"); | |
2455 | ||
2456 | entry = debugfs_create_file("available_tracers", 0444, d_tracer, | |
2457 | &global_trace, &show_traces_fops); | |
2458 | if (!entry) | |
2459 | pr_warning("Could not create debugfs 'trace' entry\n"); | |
2460 | ||
2461 | entry = debugfs_create_file("current_tracer", 0444, d_tracer, | |
2462 | &global_trace, &set_tracer_fops); | |
2463 | if (!entry) | |
2464 | pr_warning("Could not create debugfs 'trace' entry\n"); | |
2465 | ||
2466 | entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer, | |
2467 | &tracing_max_latency, | |
2468 | &tracing_max_lat_fops); | |
2469 | if (!entry) | |
2470 | pr_warning("Could not create debugfs " | |
2471 | "'tracing_max_latency' entry\n"); | |
2472 | ||
2473 | entry = debugfs_create_file("tracing_thresh", 0644, d_tracer, | |
2474 | &tracing_thresh, &tracing_max_lat_fops); | |
2475 | if (!entry) | |
2476 | pr_warning("Could not create debugfs " | |
2477 | "'tracing_threash' entry\n"); | |
7bd2f24c IM |
2478 | entry = debugfs_create_file("README", 0644, d_tracer, |
2479 | NULL, &tracing_readme_fops); | |
2480 | if (!entry) | |
2481 | pr_warning("Could not create debugfs 'README' entry\n"); | |
2482 | ||
b3806b43 SR |
2483 | entry = debugfs_create_file("trace_pipe", 0644, d_tracer, |
2484 | NULL, &tracing_pipe_fops); | |
2485 | if (!entry) | |
2486 | pr_warning("Could not create debugfs " | |
2487 | "'tracing_threash' entry\n"); | |
bc0c38d1 SR |
2488 | |
2489 | #ifdef CONFIG_DYNAMIC_FTRACE | |
2490 | entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer, | |
2491 | &ftrace_update_tot_cnt, | |
2492 | &tracing_read_long_fops); | |
2493 | if (!entry) | |
2494 | pr_warning("Could not create debugfs " | |
2495 | "'dyn_ftrace_total_info' entry\n"); | |
2496 | #endif | |
2497 | } | |
2498 | ||
2499 | /* dummy trace to disable tracing */ | |
2500 | static struct tracer no_tracer __read_mostly = | |
2501 | { | |
4bf39a94 | 2502 | .name = "none", |
bc0c38d1 SR |
2503 | }; |
2504 | ||
4c11d7ae | 2505 | static int trace_alloc_page(void) |
bc0c38d1 | 2506 | { |
4c11d7ae | 2507 | struct trace_array_cpu *data; |
4c11d7ae SR |
2508 | struct page *page, *tmp; |
2509 | LIST_HEAD(pages); | |
c7aafc54 | 2510 | void *array; |
4c11d7ae SR |
2511 | int i; |
2512 | ||
2513 | /* first allocate a page for each CPU */ | |
2514 | for_each_possible_cpu(i) { | |
2515 | array = (void *)__get_free_page(GFP_KERNEL); | |
2516 | if (array == NULL) { | |
2517 | printk(KERN_ERR "tracer: failed to allocate page" | |
2518 | "for trace buffer!\n"); | |
2519 | goto free_pages; | |
2520 | } | |
2521 | ||
2522 | page = virt_to_page(array); | |
2523 | list_add(&page->lru, &pages); | |
2524 | ||
2525 | /* Only allocate if we are actually using the max trace */ | |
2526 | #ifdef CONFIG_TRACER_MAX_TRACE | |
2527 | array = (void *)__get_free_page(GFP_KERNEL); | |
2528 | if (array == NULL) { | |
2529 | printk(KERN_ERR "tracer: failed to allocate page" | |
2530 | "for trace buffer!\n"); | |
2531 | goto free_pages; | |
2532 | } | |
2533 | page = virt_to_page(array); | |
2534 | list_add(&page->lru, &pages); | |
2535 | #endif | |
2536 | } | |
2537 | ||
2538 | /* Now that we successfully allocate a page per CPU, add them */ | |
2539 | for_each_possible_cpu(i) { | |
2540 | data = global_trace.data[i]; | |
b3806b43 | 2541 | spin_lock_init(&data->lock); |
d4c5a2f5 | 2542 | lockdep_set_class(&data->lock, &data->lock_key); |
4c11d7ae | 2543 | page = list_entry(pages.next, struct page, lru); |
c7aafc54 | 2544 | list_del_init(&page->lru); |
4c11d7ae SR |
2545 | list_add_tail(&page->lru, &data->trace_pages); |
2546 | ClearPageLRU(page); | |
2547 | ||
2548 | #ifdef CONFIG_TRACER_MAX_TRACE | |
2549 | data = max_tr.data[i]; | |
b3806b43 | 2550 | spin_lock_init(&data->lock); |
d4c5a2f5 | 2551 | lockdep_set_class(&data->lock, &data->lock_key); |
4c11d7ae | 2552 | page = list_entry(pages.next, struct page, lru); |
c7aafc54 | 2553 | list_del_init(&page->lru); |
4c11d7ae SR |
2554 | list_add_tail(&page->lru, &data->trace_pages); |
2555 | SetPageLRU(page); | |
2556 | #endif | |
2557 | } | |
2558 | global_trace.entries += ENTRIES_PER_PAGE; | |
2559 | ||
2560 | return 0; | |
2561 | ||
2562 | free_pages: | |
2563 | list_for_each_entry_safe(page, tmp, &pages, lru) { | |
c7aafc54 | 2564 | list_del_init(&page->lru); |
4c11d7ae SR |
2565 | __free_page(page); |
2566 | } | |
2567 | return -ENOMEM; | |
bc0c38d1 SR |
2568 | } |
2569 | ||
2570 | __init static int tracer_alloc_buffers(void) | |
2571 | { | |
4c11d7ae SR |
2572 | struct trace_array_cpu *data; |
2573 | void *array; | |
2574 | struct page *page; | |
2575 | int pages = 0; | |
60a11774 | 2576 | int ret = -ENOMEM; |
bc0c38d1 SR |
2577 | int i; |
2578 | ||
26994ead SR |
2579 | global_trace.ctrl = tracer_enabled; |
2580 | ||
4c11d7ae | 2581 | /* Allocate the first page for all buffers */ |
bc0c38d1 | 2582 | for_each_possible_cpu(i) { |
4c11d7ae | 2583 | data = global_trace.data[i] = &per_cpu(global_trace_cpu, i); |
bc0c38d1 SR |
2584 | max_tr.data[i] = &per_cpu(max_data, i); |
2585 | ||
4c11d7ae | 2586 | array = (void *)__get_free_page(GFP_KERNEL); |
bc0c38d1 | 2587 | if (array == NULL) { |
4c11d7ae SR |
2588 | printk(KERN_ERR "tracer: failed to allocate page" |
2589 | "for trace buffer!\n"); | |
bc0c38d1 SR |
2590 | goto free_buffers; |
2591 | } | |
4c11d7ae SR |
2592 | |
2593 | /* set the array to the list */ | |
2594 | INIT_LIST_HEAD(&data->trace_pages); | |
2595 | page = virt_to_page(array); | |
2596 | list_add(&page->lru, &data->trace_pages); | |
2597 | /* use the LRU flag to differentiate the two buffers */ | |
2598 | ClearPageLRU(page); | |
bc0c38d1 SR |
2599 | |
2600 | /* Only allocate if we are actually using the max trace */ | |
2601 | #ifdef CONFIG_TRACER_MAX_TRACE | |
4c11d7ae | 2602 | array = (void *)__get_free_page(GFP_KERNEL); |
bc0c38d1 | 2603 | if (array == NULL) { |
4c11d7ae SR |
2604 | printk(KERN_ERR "tracer: failed to allocate page" |
2605 | "for trace buffer!\n"); | |
bc0c38d1 SR |
2606 | goto free_buffers; |
2607 | } | |
4c11d7ae SR |
2608 | |
2609 | INIT_LIST_HEAD(&max_tr.data[i]->trace_pages); | |
2610 | page = virt_to_page(array); | |
2611 | list_add(&page->lru, &max_tr.data[i]->trace_pages); | |
2612 | SetPageLRU(page); | |
bc0c38d1 SR |
2613 | #endif |
2614 | } | |
2615 | ||
2616 | /* | |
2617 | * Since we allocate by orders of pages, we may be able to | |
2618 | * round up a bit. | |
2619 | */ | |
4c11d7ae | 2620 | global_trace.entries = ENTRIES_PER_PAGE; |
4c11d7ae SR |
2621 | pages++; |
2622 | ||
2623 | while (global_trace.entries < trace_nr_entries) { | |
2624 | if (trace_alloc_page()) | |
2625 | break; | |
2626 | pages++; | |
2627 | } | |
89b2f978 | 2628 | max_tr.entries = global_trace.entries; |
bc0c38d1 | 2629 | |
4c11d7ae SR |
2630 | pr_info("tracer: %d pages allocated for %ld", |
2631 | pages, trace_nr_entries); | |
bc0c38d1 SR |
2632 | pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE); |
2633 | pr_info(" actual entries %ld\n", global_trace.entries); | |
2634 | ||
2635 | tracer_init_debugfs(); | |
2636 | ||
2637 | trace_init_cmdlines(); | |
2638 | ||
2639 | register_tracer(&no_tracer); | |
2640 | current_trace = &no_tracer; | |
2641 | ||
60a11774 SR |
2642 | /* All seems OK, enable tracing */ |
2643 | tracing_disabled = 0; | |
2644 | ||
bc0c38d1 SR |
2645 | return 0; |
2646 | ||
2647 | free_buffers: | |
2648 | for (i-- ; i >= 0; i--) { | |
4c11d7ae | 2649 | struct page *page, *tmp; |
bc0c38d1 SR |
2650 | struct trace_array_cpu *data = global_trace.data[i]; |
2651 | ||
c7aafc54 | 2652 | if (data) { |
4c11d7ae SR |
2653 | list_for_each_entry_safe(page, tmp, |
2654 | &data->trace_pages, lru) { | |
c7aafc54 | 2655 | list_del_init(&page->lru); |
4c11d7ae SR |
2656 | __free_page(page); |
2657 | } | |
bc0c38d1 SR |
2658 | } |
2659 | ||
2660 | #ifdef CONFIG_TRACER_MAX_TRACE | |
2661 | data = max_tr.data[i]; | |
c7aafc54 | 2662 | if (data) { |
4c11d7ae SR |
2663 | list_for_each_entry_safe(page, tmp, |
2664 | &data->trace_pages, lru) { | |
c7aafc54 | 2665 | list_del_init(&page->lru); |
4c11d7ae SR |
2666 | __free_page(page); |
2667 | } | |
bc0c38d1 SR |
2668 | } |
2669 | #endif | |
2670 | } | |
60a11774 | 2671 | return ret; |
bc0c38d1 | 2672 | } |
60a11774 | 2673 | fs_initcall(tracer_alloc_buffers); |