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