tracing: Move sleep-time and graph-time options out of the core trace_flags
[linux-2.6-block.git] / kernel / trace / trace_output.c
CommitLineData
f0868d1e
SR
1/*
2 * trace_output.c
3 *
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5 *
6 */
7
8#include <linux/module.h>
9#include <linux/mutex.h>
10#include <linux/ftrace.h>
11
12#include "trace_output.h"
13
14/* must be a power of 2 */
15#define EVENT_HASHSIZE 128
16
52f6ad6d 17DECLARE_RWSEM(trace_event_sem);
be74b73a 18
f0868d1e
SR
19static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
20
21static int next_event_type = __TRACE_LAST_TYPE + 1;
22
09ae7234
SRRH
23enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter)
24{
25 struct trace_seq *s = &iter->seq;
26 struct trace_entry *entry = iter->ent;
27 struct bputs_entry *field;
09ae7234
SRRH
28
29 trace_assign_type(field, entry);
30
19a7fe20 31 trace_seq_puts(s, field->str);
09ae7234 32
19a7fe20 33 return trace_handle_return(s);
09ae7234
SRRH
34}
35
5ef841f6
SR
36enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
37{
38 struct trace_seq *s = &iter->seq;
39 struct trace_entry *entry = iter->ent;
40 struct bprint_entry *field;
5ef841f6
SR
41
42 trace_assign_type(field, entry);
43
19a7fe20 44 trace_seq_bprintf(s, field->fmt, field->buf);
5ef841f6 45
19a7fe20 46 return trace_handle_return(s);
5ef841f6
SR
47}
48
49enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
50{
51 struct trace_seq *s = &iter->seq;
52 struct trace_entry *entry = iter->ent;
53 struct print_entry *field;
5ef841f6
SR
54
55 trace_assign_type(field, entry);
56
19a7fe20 57 trace_seq_puts(s, field->buf);
5ef841f6 58
19a7fe20 59 return trace_handle_return(s);
5ef841f6
SR
60}
61
be74b73a 62const char *
645df987
SRRH
63trace_print_flags_seq(struct trace_seq *p, const char *delim,
64 unsigned long flags,
65 const struct trace_print_flags *flag_array)
be74b73a
SR
66{
67 unsigned long mask;
68 const char *str;
7b039cb4 69 const char *ret = trace_seq_buffer_ptr(p);
e404b321 70 int i, first = 1;
be74b73a 71
be74b73a
SR
72 for (i = 0; flag_array[i].name && flags; i++) {
73
74 mask = flag_array[i].mask;
75 if ((flags & mask) != mask)
76 continue;
77
78 str = flag_array[i].name;
79 flags &= ~mask;
e404b321 80 if (!first && delim)
be74b73a 81 trace_seq_puts(p, delim);
e404b321
AV
82 else
83 first = 0;
be74b73a
SR
84 trace_seq_puts(p, str);
85 }
86
87 /* check for left over flags */
88 if (flags) {
5b349261 89 if (!first && delim)
be74b73a
SR
90 trace_seq_puts(p, delim);
91 trace_seq_printf(p, "0x%lx", flags);
92 }
93
94 trace_seq_putc(p, 0);
95
56d8bd3f 96 return ret;
be74b73a 97}
645df987 98EXPORT_SYMBOL(trace_print_flags_seq);
be74b73a 99
0f4fc29d 100const char *
645df987
SRRH
101trace_print_symbols_seq(struct trace_seq *p, unsigned long val,
102 const struct trace_print_flags *symbol_array)
0f4fc29d
SR
103{
104 int i;
7b039cb4 105 const char *ret = trace_seq_buffer_ptr(p);
0f4fc29d
SR
106
107 for (i = 0; symbol_array[i].name; i++) {
108
109 if (val != symbol_array[i].mask)
110 continue;
111
112 trace_seq_puts(p, symbol_array[i].name);
113 break;
114 }
115
7b039cb4 116 if (ret == (const char *)(trace_seq_buffer_ptr(p)))
0f4fc29d 117 trace_seq_printf(p, "0x%lx", val);
8e1e1df2 118
0f4fc29d
SR
119 trace_seq_putc(p, 0);
120
56d8bd3f 121 return ret;
0f4fc29d 122}
645df987 123EXPORT_SYMBOL(trace_print_symbols_seq);
0f4fc29d 124
2fc1b6f0 125#if BITS_PER_LONG == 32
126const char *
645df987 127trace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
2fc1b6f0 128 const struct trace_print_flags_u64 *symbol_array)
129{
130 int i;
7b039cb4 131 const char *ret = trace_seq_buffer_ptr(p);
2fc1b6f0 132
133 for (i = 0; symbol_array[i].name; i++) {
134
135 if (val != symbol_array[i].mask)
136 continue;
137
138 trace_seq_puts(p, symbol_array[i].name);
139 break;
140 }
141
7b039cb4 142 if (ret == (const char *)(trace_seq_buffer_ptr(p)))
2fc1b6f0 143 trace_seq_printf(p, "0x%llx", val);
144
145 trace_seq_putc(p, 0);
146
147 return ret;
148}
645df987 149EXPORT_SYMBOL(trace_print_symbols_seq_u64);
2fc1b6f0 150#endif
151
4449bf92 152const char *
645df987
SRRH
153trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
154 unsigned int bitmask_size)
4449bf92 155{
7b039cb4 156 const char *ret = trace_seq_buffer_ptr(p);
4449bf92
SRRH
157
158 trace_seq_bitmask(p, bitmask_ptr, bitmask_size * 8);
159 trace_seq_putc(p, 0);
160
161 return ret;
162}
645df987 163EXPORT_SYMBOL_GPL(trace_print_bitmask_seq);
4449bf92 164
5a2e3995 165const char *
645df987 166trace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len)
5a2e3995
KT
167{
168 int i;
7b039cb4 169 const char *ret = trace_seq_buffer_ptr(p);
5a2e3995
KT
170
171 for (i = 0; i < buf_len; i++)
172 trace_seq_printf(p, "%s%2.2x", i == 0 ? "" : " ", buf[i]);
173
174 trace_seq_putc(p, 0);
175
176 return ret;
177}
645df987 178EXPORT_SYMBOL(trace_print_hex_seq);
5a2e3995 179
6ea22486 180const char *
645df987
SRRH
181trace_print_array_seq(struct trace_seq *p, const void *buf, int count,
182 size_t el_size)
6ea22486
DM
183{
184 const char *ret = trace_seq_buffer_ptr(p);
185 const char *prefix = "";
186 void *ptr = (void *)buf;
ac01ce14 187 size_t buf_len = count * el_size;
6ea22486
DM
188
189 trace_seq_putc(p, '{');
190
191 while (ptr < buf + buf_len) {
192 switch (el_size) {
193 case 1:
194 trace_seq_printf(p, "%s0x%x", prefix,
195 *(u8 *)ptr);
196 break;
197 case 2:
198 trace_seq_printf(p, "%s0x%x", prefix,
199 *(u16 *)ptr);
200 break;
201 case 4:
202 trace_seq_printf(p, "%s0x%x", prefix,
203 *(u32 *)ptr);
204 break;
205 case 8:
206 trace_seq_printf(p, "%s0x%llx", prefix,
207 *(u64 *)ptr);
208 break;
209 default:
210 trace_seq_printf(p, "BAD SIZE:%zu 0x%x", el_size,
211 *(u8 *)ptr);
212 el_size = 1;
213 }
214 prefix = ",";
215 ptr += el_size;
216 }
217
218 trace_seq_putc(p, '}');
219 trace_seq_putc(p, 0);
220
221 return ret;
222}
645df987 223EXPORT_SYMBOL(trace_print_array_seq);
6ea22486 224
892c505a
SRRH
225int trace_raw_output_prep(struct trace_iterator *iter,
226 struct trace_event *trace_event)
f71130de 227{
2425bcb9 228 struct trace_event_call *event;
f71130de
LZ
229 struct trace_seq *s = &iter->seq;
230 struct trace_seq *p = &iter->tmp_seq;
231 struct trace_entry *entry;
f71130de 232
2425bcb9 233 event = container_of(trace_event, struct trace_event_call, event);
f71130de
LZ
234 entry = iter->ent;
235
236 if (entry->type != event->event.type) {
237 WARN_ON_ONCE(1);
238 return TRACE_TYPE_UNHANDLED;
239 }
240
241 trace_seq_init(p);
687fcc4a 242 trace_seq_printf(s, "%s: ", trace_event_name(event));
19a7fe20 243
8e2e095c 244 return trace_handle_return(s);
f71130de 245}
892c505a 246EXPORT_SYMBOL(trace_raw_output_prep);
f71130de 247
892c505a
SRRH
248static int trace_output_raw(struct trace_iterator *iter, char *name,
249 char *fmt, va_list ap)
1d6bae96
SR
250{
251 struct trace_seq *s = &iter->seq;
1d6bae96 252
19a7fe20
SRRH
253 trace_seq_printf(s, "%s: ", name);
254 trace_seq_vprintf(s, fmt, ap);
1d6bae96 255
19a7fe20 256 return trace_handle_return(s);
1d6bae96
SR
257}
258
892c505a 259int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...)
1d6bae96
SR
260{
261 va_list ap;
262 int ret;
263
264 va_start(ap, fmt);
892c505a 265 ret = trace_output_raw(iter, name, fmt, ap);
1d6bae96
SR
266 va_end(ap);
267
268 return ret;
269}
892c505a 270EXPORT_SYMBOL_GPL(trace_output_call);
1d6bae96 271
f0868d1e
SR
272#ifdef CONFIG_KRETPROBES
273static inline const char *kretprobed(const char *name)
274{
275 static const char tramp_name[] = "kretprobe_trampoline";
276 int size = sizeof(tramp_name);
277
278 if (strncmp(tramp_name, name, size) == 0)
279 return "[unknown/kretprobe'd]";
280 return name;
281}
282#else
283static inline const char *kretprobed(const char *name)
284{
285 return name;
286}
287#endif /* CONFIG_KRETPROBES */
288
19a7fe20 289static void
f0868d1e
SR
290seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
291{
292#ifdef CONFIG_KALLSYMS
293 char str[KSYM_SYMBOL_LEN];
294 const char *name;
295
296 kallsyms_lookup(address, NULL, NULL, NULL, str);
297
298 name = kretprobed(str);
299
19a7fe20 300 trace_seq_printf(s, fmt, name);
f0868d1e 301#endif
f0868d1e
SR
302}
303
19a7fe20 304static void
f0868d1e
SR
305seq_print_sym_offset(struct trace_seq *s, const char *fmt,
306 unsigned long address)
307{
308#ifdef CONFIG_KALLSYMS
309 char str[KSYM_SYMBOL_LEN];
310 const char *name;
311
312 sprint_symbol(str, address);
313 name = kretprobed(str);
314
19a7fe20 315 trace_seq_printf(s, fmt, name);
f0868d1e 316#endif
f0868d1e
SR
317}
318
319#ifndef CONFIG_64BIT
320# define IP_FMT "%08lx"
321#else
322# define IP_FMT "%016lx"
323#endif
324
ef92480a
SRRH
325static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
326 unsigned long ip, unsigned long sym_flags)
f0868d1e
SR
327{
328 struct file *file = NULL;
329 unsigned long vmstart = 0;
330 int ret = 1;
331
d184b31c
JB
332 if (s->full)
333 return 0;
334
f0868d1e
SR
335 if (mm) {
336 const struct vm_area_struct *vma;
337
338 down_read(&mm->mmap_sem);
339 vma = find_vma(mm, ip);
340 if (vma) {
341 file = vma->vm_file;
342 vmstart = vma->vm_start;
343 }
344 if (file) {
345 ret = trace_seq_path(s, &file->f_path);
346 if (ret)
19a7fe20
SRRH
347 trace_seq_printf(s, "[+0x%lx]",
348 ip - vmstart);
f0868d1e
SR
349 }
350 up_read(&mm->mmap_sem);
351 }
352 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
19a7fe20
SRRH
353 trace_seq_printf(s, " <" IP_FMT ">", ip);
354 return !trace_seq_has_overflowed(s);
f0868d1e
SR
355}
356
f0868d1e
SR
357int
358seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
359{
19a7fe20
SRRH
360 if (!ip) {
361 trace_seq_putc(s, '0');
362 goto out;
363 }
f0868d1e
SR
364
365 if (sym_flags & TRACE_ITER_SYM_OFFSET)
19a7fe20 366 seq_print_sym_offset(s, "%s", ip);
f0868d1e 367 else
19a7fe20 368 seq_print_sym_short(s, "%s", ip);
f0868d1e
SR
369
370 if (sym_flags & TRACE_ITER_SYM_ADDR)
19a7fe20
SRRH
371 trace_seq_printf(s, " <" IP_FMT ">", ip);
372
373 out:
374 return !trace_seq_has_overflowed(s);
f0868d1e
SR
375}
376
f81c972d
SR
377/**
378 * trace_print_lat_fmt - print the irq, preempt and lockdep fields
379 * @s: trace seq struct to write to
380 * @entry: The trace entry field from the ring buffer
381 *
382 * Prints the generic fields of irqs off, in hard or softirq, preempt
e6e1e259 383 * count.
f81c972d
SR
384 */
385int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
c4a8e8be 386{
10da37a6
DS
387 char hardsoft_irq;
388 char need_resched;
389 char irqs_off;
390 int hardirq;
391 int softirq;
c4a8e8be 392
c4a8e8be
FW
393 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
394 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
d9793bd8 395
10da37a6
DS
396 irqs_off =
397 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
398 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' :
399 '.';
e5137b50
PZ
400
401 switch (entry->flags & (TRACE_FLAG_NEED_RESCHED |
402 TRACE_FLAG_PREEMPT_RESCHED)) {
403 case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
404 need_resched = 'N';
405 break;
406 case TRACE_FLAG_NEED_RESCHED:
407 need_resched = 'n';
408 break;
409 case TRACE_FLAG_PREEMPT_RESCHED:
410 need_resched = 'p';
411 break;
412 default:
413 need_resched = '.';
414 break;
415 }
416
10da37a6
DS
417 hardsoft_irq =
418 (hardirq && softirq) ? 'H' :
419 hardirq ? 'h' :
420 softirq ? 's' :
421 '.';
422
19a7fe20
SRRH
423 trace_seq_printf(s, "%c%c%c",
424 irqs_off, need_resched, hardsoft_irq);
c4a8e8be 425
829b876d 426 if (entry->preempt_count)
19a7fe20 427 trace_seq_printf(s, "%x", entry->preempt_count);
637e7e86 428 else
19a7fe20 429 trace_seq_putc(s, '.');
829b876d 430
19a7fe20 431 return !trace_seq_has_overflowed(s);
c4a8e8be
FW
432}
433
f81c972d
SR
434static int
435lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
436{
437 char comm[TASK_COMM_LEN];
438
439 trace_find_cmdline(entry->pid, comm);
440
19a7fe20
SRRH
441 trace_seq_printf(s, "%8.8s-%-5d %3d",
442 comm, entry->pid, cpu);
f81c972d
SR
443
444 return trace_print_lat_fmt(s, entry);
445}
446
8e1e1df2
BP
447#undef MARK
448#define MARK(v, s) {.val = v, .sym = s}
449/* trace overhead mark */
450static const struct trace_mark {
451 unsigned long long val; /* unit: nsec */
452 char sym;
453} mark[] = {
454 MARK(1000000000ULL , '$'), /* 1 sec */
b838e1d9
JL
455 MARK(100000000ULL , '@'), /* 100 msec */
456 MARK(10000000ULL , '*'), /* 10 msec */
8e1e1df2
BP
457 MARK(1000000ULL , '#'), /* 1000 usecs */
458 MARK(100000ULL , '!'), /* 100 usecs */
459 MARK(10000ULL , '+'), /* 10 usecs */
460};
461#undef MARK
462
463char trace_find_mark(unsigned long long d)
464{
465 int i;
466 int size = ARRAY_SIZE(mark);
467
468 for (i = 0; i < size; i++) {
b838e1d9 469 if (d > mark[i].val)
8e1e1df2
BP
470 break;
471 }
472
473 return (i == size) ? ' ' : mark[i].sym;
474}
c4a8e8be 475
d9793bd8 476static int
8be0709f 477lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
c4a8e8be 478{
8be0709f
DS
479 unsigned long verbose = trace_flags & TRACE_ITER_VERBOSE;
480 unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
12883efb 481 unsigned long long abs_ts = iter->ts - iter->trace_buffer->time_start;
8be0709f
DS
482 unsigned long long rel_ts = next_ts - iter->ts;
483 struct trace_seq *s = &iter->seq;
484
485 if (in_ns) {
486 abs_ts = ns2usecs(abs_ts);
487 rel_ts = ns2usecs(rel_ts);
488 }
489
490 if (verbose && in_ns) {
491 unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
492 unsigned long abs_msec = (unsigned long)abs_ts;
493 unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
494 unsigned long rel_msec = (unsigned long)rel_ts;
495
19a7fe20
SRRH
496 trace_seq_printf(
497 s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
498 ns2usecs(iter->ts),
499 abs_msec, abs_usec,
500 rel_msec, rel_usec);
501
8be0709f 502 } else if (verbose && !in_ns) {
19a7fe20
SRRH
503 trace_seq_printf(
504 s, "[%016llx] %lld (+%lld): ",
505 iter->ts, abs_ts, rel_ts);
506
8be0709f 507 } else if (!verbose && in_ns) {
19a7fe20
SRRH
508 trace_seq_printf(
509 s, " %4lldus%c: ",
510 abs_ts,
8e1e1df2 511 trace_find_mark(rel_ts * NSEC_PER_USEC));
19a7fe20 512
8be0709f 513 } else { /* !verbose && !in_ns */
19a7fe20 514 trace_seq_printf(s, " %4lld: ", abs_ts);
8be0709f 515 }
19a7fe20
SRRH
516
517 return !trace_seq_has_overflowed(s);
c4a8e8be
FW
518}
519
520int trace_print_context(struct trace_iterator *iter)
521{
522 struct trace_seq *s = &iter->seq;
523 struct trace_entry *entry = iter->ent;
8be0709f
DS
524 unsigned long long t;
525 unsigned long secs, usec_rem;
4ca53085
SR
526 char comm[TASK_COMM_LEN];
527
528 trace_find_cmdline(entry->pid, comm);
c4a8e8be 529
19a7fe20 530 trace_seq_printf(s, "%16s-%-5d [%03d] ",
77271ce4 531 comm, entry->pid, iter->cpu);
77271ce4 532
19a7fe20
SRRH
533 if (trace_flags & TRACE_ITER_IRQ_INFO)
534 trace_print_lat_fmt(s, entry);
77271ce4 535
8be0709f
DS
536 if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
537 t = ns2usecs(iter->ts);
538 usec_rem = do_div(t, USEC_PER_SEC);
539 secs = (unsigned long)t;
19a7fe20 540 trace_seq_printf(s, " %5lu.%06lu: ", secs, usec_rem);
8be0709f 541 } else
19a7fe20
SRRH
542 trace_seq_printf(s, " %12llu: ", iter->ts);
543
544 return !trace_seq_has_overflowed(s);
c4a8e8be
FW
545}
546
547int trace_print_lat_context(struct trace_iterator *iter)
548{
549 u64 next_ts;
db4c75cb
SR
550 /* trace_find_next_entry will reset ent_size */
551 int ent_size = iter->ent_size;
c4a8e8be
FW
552 struct trace_seq *s = &iter->seq;
553 struct trace_entry *entry = iter->ent,
554 *next_entry = trace_find_next_entry(iter, NULL,
555 &next_ts);
556 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
c4a8e8be 557
db4c75cb
SR
558 /* Restore the original ent_size */
559 iter->ent_size = ent_size;
560
c4a8e8be
FW
561 if (!next_entry)
562 next_ts = iter->ts;
c4a8e8be
FW
563
564 if (verbose) {
4ca53085
SR
565 char comm[TASK_COMM_LEN];
566
567 trace_find_cmdline(entry->pid, comm);
568
19a7fe20
SRRH
569 trace_seq_printf(
570 s, "%16s %5d %3d %d %08x %08lx ",
571 comm, entry->pid, iter->cpu, entry->flags,
572 entry->preempt_count, iter->idx);
c4a8e8be 573 } else {
19a7fe20 574 lat_print_generic(s, entry, iter->cpu);
c4a8e8be
FW
575 }
576
19a7fe20 577 lat_print_timestamp(iter, next_ts);
8be0709f 578
19a7fe20 579 return !trace_seq_has_overflowed(s);
c4a8e8be
FW
580}
581
f633cef0
SR
582static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
583
584static int task_state_char(unsigned long state)
585{
586 int bit = state ? __ffs(state) + 1 : 0;
587
588 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
589}
590
f0868d1e
SR
591/**
592 * ftrace_find_event - find a registered event
593 * @type: the type of event to look for
594 *
595 * Returns an event of type @type otherwise NULL
4f535968 596 * Called with trace_event_read_lock() held.
f0868d1e
SR
597 */
598struct trace_event *ftrace_find_event(int type)
599{
600 struct trace_event *event;
f0868d1e
SR
601 unsigned key;
602
603 key = type & (EVENT_HASHSIZE - 1);
604
b67bfe0d 605 hlist_for_each_entry(event, &event_hash[key], node) {
f0868d1e
SR
606 if (event->type == type)
607 return event;
608 }
609
610 return NULL;
611}
612
060fa5c8
SR
613static LIST_HEAD(ftrace_event_list);
614
615static int trace_search_list(struct list_head **list)
616{
617 struct trace_event *e;
618 int last = __TRACE_LAST_TYPE;
619
620 if (list_empty(&ftrace_event_list)) {
621 *list = &ftrace_event_list;
622 return last + 1;
623 }
624
625 /*
626 * We used up all possible max events,
627 * lets see if somebody freed one.
628 */
629 list_for_each_entry(e, &ftrace_event_list, list) {
630 if (e->type != last + 1)
631 break;
632 last++;
633 }
634
635 /* Did we used up all 65 thousand events??? */
609a7404 636 if ((last + 1) > TRACE_EVENT_TYPE_MAX)
060fa5c8
SR
637 return 0;
638
639 *list = &e->list;
640 return last + 1;
641}
642
4f535968
LJ
643void trace_event_read_lock(void)
644{
52f6ad6d 645 down_read(&trace_event_sem);
4f535968
LJ
646}
647
648void trace_event_read_unlock(void)
649{
52f6ad6d 650 up_read(&trace_event_sem);
4f535968
LJ
651}
652
f0868d1e 653/**
9023c930 654 * register_trace_event - register output for an event type
f0868d1e
SR
655 * @event: the event type to register
656 *
657 * Event types are stored in a hash and this hash is used to
658 * find a way to print an event. If the @event->type is set
659 * then it will use that type, otherwise it will assign a
660 * type to use.
661 *
662 * If you assign your own type, please make sure it is added
663 * to the trace_type enum in trace.h, to avoid collisions
664 * with the dynamic types.
665 *
666 * Returns the event type number or zero on error.
667 */
9023c930 668int register_trace_event(struct trace_event *event)
f0868d1e
SR
669{
670 unsigned key;
671 int ret = 0;
672
52f6ad6d 673 down_write(&trace_event_sem);
f0868d1e 674
060fa5c8 675 if (WARN_ON(!event))
28bea271 676 goto out;
28bea271 677
a9a57763
SR
678 if (WARN_ON(!event->funcs))
679 goto out;
680
060fa5c8
SR
681 INIT_LIST_HEAD(&event->list);
682
683 if (!event->type) {
48dd0fed 684 struct list_head *list = NULL;
060fa5c8 685
609a7404 686 if (next_event_type > TRACE_EVENT_TYPE_MAX) {
060fa5c8
SR
687
688 event->type = trace_search_list(&list);
689 if (!event->type)
690 goto out;
691
692 } else {
8e1e1df2 693
060fa5c8
SR
694 event->type = next_event_type++;
695 list = &ftrace_event_list;
696 }
697
698 if (WARN_ON(ftrace_find_event(event->type)))
699 goto out;
700
701 list_add_tail(&event->list, list);
702
703 } else if (event->type > __TRACE_LAST_TYPE) {
f0868d1e
SR
704 printk(KERN_WARNING "Need to add type to trace.h\n");
705 WARN_ON(1);
f0868d1e 706 goto out;
060fa5c8
SR
707 } else {
708 /* Is this event already used */
709 if (ftrace_find_event(event->type))
710 goto out;
711 }
f0868d1e 712
a9a57763
SR
713 if (event->funcs->trace == NULL)
714 event->funcs->trace = trace_nop_print;
715 if (event->funcs->raw == NULL)
716 event->funcs->raw = trace_nop_print;
717 if (event->funcs->hex == NULL)
718 event->funcs->hex = trace_nop_print;
719 if (event->funcs->binary == NULL)
720 event->funcs->binary = trace_nop_print;
268ccda0 721
f0868d1e
SR
722 key = event->type & (EVENT_HASHSIZE - 1);
723
4f535968 724 hlist_add_head(&event->node, &event_hash[key]);
f0868d1e
SR
725
726 ret = event->type;
727 out:
52f6ad6d 728 up_write(&trace_event_sem);
f0868d1e
SR
729
730 return ret;
731}
9023c930 732EXPORT_SYMBOL_GPL(register_trace_event);
f0868d1e 733
110bf2b7 734/*
52f6ad6d 735 * Used by module code with the trace_event_sem held for write.
110bf2b7 736 */
9023c930 737int __unregister_trace_event(struct trace_event *event)
110bf2b7
SR
738{
739 hlist_del(&event->node);
740 list_del(&event->list);
741 return 0;
742}
743
f0868d1e 744/**
9023c930 745 * unregister_trace_event - remove a no longer used event
f0868d1e
SR
746 * @event: the event to remove
747 */
9023c930 748int unregister_trace_event(struct trace_event *event)
f0868d1e 749{
52f6ad6d 750 down_write(&trace_event_sem);
9023c930 751 __unregister_trace_event(event);
52f6ad6d 752 up_write(&trace_event_sem);
f0868d1e
SR
753
754 return 0;
755}
9023c930 756EXPORT_SYMBOL_GPL(unregister_trace_event);
f633cef0
SR
757
758/*
759 * Standard events
760 */
761
a9a57763
SR
762enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
763 struct trace_event *event)
f633cef0 764{
19a7fe20 765 trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type);
ee5e51f5 766
19a7fe20 767 return trace_handle_return(&iter->seq);
f633cef0
SR
768}
769
770/* TRACE_FN */
a9a57763
SR
771static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
772 struct trace_event *event)
f633cef0
SR
773{
774 struct ftrace_entry *field;
2c9b238e 775 struct trace_seq *s = &iter->seq;
f633cef0 776
2c9b238e 777 trace_assign_type(field, iter->ent);
f633cef0 778
19a7fe20 779 seq_print_ip_sym(s, field->ip, flags);
f633cef0
SR
780
781 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
19a7fe20
SRRH
782 trace_seq_puts(s, " <-");
783 seq_print_ip_sym(s, field->parent_ip, flags);
f633cef0 784 }
f633cef0 785
19a7fe20 786 trace_seq_putc(s, '\n');
f633cef0 787
19a7fe20 788 return trace_handle_return(s);
f633cef0
SR
789}
790
a9a57763
SR
791static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
792 struct trace_event *event)
f633cef0
SR
793{
794 struct ftrace_entry *field;
795
2c9b238e 796 trace_assign_type(field, iter->ent);
f633cef0 797
19a7fe20
SRRH
798 trace_seq_printf(&iter->seq, "%lx %lx\n",
799 field->ip,
800 field->parent_ip);
f633cef0 801
19a7fe20 802 return trace_handle_return(&iter->seq);
f633cef0
SR
803}
804
a9a57763
SR
805static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
806 struct trace_event *event)
f633cef0
SR
807{
808 struct ftrace_entry *field;
2c9b238e 809 struct trace_seq *s = &iter->seq;
f633cef0 810
2c9b238e 811 trace_assign_type(field, iter->ent);
f633cef0 812
19a7fe20
SRRH
813 SEQ_PUT_HEX_FIELD(s, field->ip);
814 SEQ_PUT_HEX_FIELD(s, field->parent_ip);
f633cef0 815
19a7fe20 816 return trace_handle_return(s);
f633cef0
SR
817}
818
a9a57763
SR
819static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
820 struct trace_event *event)
f633cef0
SR
821{
822 struct ftrace_entry *field;
2c9b238e 823 struct trace_seq *s = &iter->seq;
f633cef0 824
2c9b238e 825 trace_assign_type(field, iter->ent);
f633cef0 826
19a7fe20
SRRH
827 SEQ_PUT_FIELD(s, field->ip);
828 SEQ_PUT_FIELD(s, field->parent_ip);
f633cef0 829
19a7fe20 830 return trace_handle_return(s);
f633cef0
SR
831}
832
a9a57763 833static struct trace_event_functions trace_fn_funcs = {
f633cef0 834 .trace = trace_fn_trace,
f633cef0
SR
835 .raw = trace_fn_raw,
836 .hex = trace_fn_hex,
837 .binary = trace_fn_bin,
838};
839
a9a57763
SR
840static struct trace_event trace_fn_event = {
841 .type = TRACE_FN,
842 .funcs = &trace_fn_funcs,
843};
844
f633cef0 845/* TRACE_CTX an TRACE_WAKE */
ae7462b4
ACM
846static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
847 char *delim)
f633cef0
SR
848{
849 struct ctx_switch_entry *field;
4ca53085 850 char comm[TASK_COMM_LEN];
f633cef0
SR
851 int S, T;
852
4ca53085 853
2c9b238e 854 trace_assign_type(field, iter->ent);
f633cef0
SR
855
856 T = task_state_char(field->next_state);
857 S = task_state_char(field->prev_state);
4ca53085 858 trace_find_cmdline(field->next_pid, comm);
19a7fe20
SRRH
859 trace_seq_printf(&iter->seq,
860 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
861 field->prev_pid,
862 field->prev_prio,
863 S, delim,
864 field->next_cpu,
865 field->next_pid,
866 field->next_prio,
867 T, comm);
868
869 return trace_handle_return(&iter->seq);
f633cef0
SR
870}
871
a9a57763
SR
872static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
873 struct trace_event *event)
f633cef0 874{
2c9b238e 875 return trace_ctxwake_print(iter, "==>");
f633cef0
SR
876}
877
ae7462b4 878static enum print_line_t trace_wake_print(struct trace_iterator *iter,
a9a57763 879 int flags, struct trace_event *event)
f633cef0 880{
2c9b238e 881 return trace_ctxwake_print(iter, " +");
f633cef0
SR
882}
883
2c9b238e 884static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
f633cef0
SR
885{
886 struct ctx_switch_entry *field;
887 int T;
888
2c9b238e 889 trace_assign_type(field, iter->ent);
f633cef0
SR
890
891 if (!S)
b0f56f1a 892 S = task_state_char(field->prev_state);
f633cef0 893 T = task_state_char(field->next_state);
19a7fe20
SRRH
894 trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
895 field->prev_pid,
896 field->prev_prio,
897 S,
898 field->next_cpu,
899 field->next_pid,
900 field->next_prio,
901 T);
902
903 return trace_handle_return(&iter->seq);
f633cef0
SR
904}
905
a9a57763
SR
906static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
907 struct trace_event *event)
f633cef0 908{
2c9b238e 909 return trace_ctxwake_raw(iter, 0);
f633cef0
SR
910}
911
a9a57763
SR
912static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
913 struct trace_event *event)
f633cef0 914{
2c9b238e 915 return trace_ctxwake_raw(iter, '+');
f633cef0
SR
916}
917
918
2c9b238e 919static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
f633cef0
SR
920{
921 struct ctx_switch_entry *field;
2c9b238e 922 struct trace_seq *s = &iter->seq;
f633cef0
SR
923 int T;
924
2c9b238e 925 trace_assign_type(field, iter->ent);
f633cef0
SR
926
927 if (!S)
b0f56f1a 928 S = task_state_char(field->prev_state);
f633cef0
SR
929 T = task_state_char(field->next_state);
930
19a7fe20
SRRH
931 SEQ_PUT_HEX_FIELD(s, field->prev_pid);
932 SEQ_PUT_HEX_FIELD(s, field->prev_prio);
933 SEQ_PUT_HEX_FIELD(s, S);
934 SEQ_PUT_HEX_FIELD(s, field->next_cpu);
935 SEQ_PUT_HEX_FIELD(s, field->next_pid);
936 SEQ_PUT_HEX_FIELD(s, field->next_prio);
937 SEQ_PUT_HEX_FIELD(s, T);
f633cef0 938
19a7fe20 939 return trace_handle_return(s);
f633cef0
SR
940}
941
a9a57763
SR
942static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
943 struct trace_event *event)
f633cef0 944{
2c9b238e 945 return trace_ctxwake_hex(iter, 0);
f633cef0
SR
946}
947
a9a57763
SR
948static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
949 struct trace_event *event)
f633cef0 950{
2c9b238e 951 return trace_ctxwake_hex(iter, '+');
f633cef0
SR
952}
953
ae7462b4 954static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
a9a57763 955 int flags, struct trace_event *event)
f633cef0
SR
956{
957 struct ctx_switch_entry *field;
2c9b238e 958 struct trace_seq *s = &iter->seq;
f633cef0 959
2c9b238e 960 trace_assign_type(field, iter->ent);
f633cef0 961
19a7fe20
SRRH
962 SEQ_PUT_FIELD(s, field->prev_pid);
963 SEQ_PUT_FIELD(s, field->prev_prio);
964 SEQ_PUT_FIELD(s, field->prev_state);
965 SEQ_PUT_FIELD(s, field->next_cpu);
966 SEQ_PUT_FIELD(s, field->next_pid);
967 SEQ_PUT_FIELD(s, field->next_prio);
968 SEQ_PUT_FIELD(s, field->next_state);
f633cef0 969
19a7fe20 970 return trace_handle_return(s);
f633cef0
SR
971}
972
a9a57763 973static struct trace_event_functions trace_ctx_funcs = {
f633cef0 974 .trace = trace_ctx_print,
f633cef0
SR
975 .raw = trace_ctx_raw,
976 .hex = trace_ctx_hex,
977 .binary = trace_ctxwake_bin,
978};
979
a9a57763
SR
980static struct trace_event trace_ctx_event = {
981 .type = TRACE_CTX,
982 .funcs = &trace_ctx_funcs,
983};
984
985static struct trace_event_functions trace_wake_funcs = {
f633cef0 986 .trace = trace_wake_print,
f633cef0
SR
987 .raw = trace_wake_raw,
988 .hex = trace_wake_hex,
989 .binary = trace_ctxwake_bin,
990};
991
a9a57763
SR
992static struct trace_event trace_wake_event = {
993 .type = TRACE_WAKE,
994 .funcs = &trace_wake_funcs,
995};
996
f633cef0
SR
997/* TRACE_STACK */
998
ae7462b4 999static enum print_line_t trace_stack_print(struct trace_iterator *iter,
a9a57763 1000 int flags, struct trace_event *event)
f633cef0
SR
1001{
1002 struct stack_entry *field;
2c9b238e 1003 struct trace_seq *s = &iter->seq;
4a9bd3f1
SR
1004 unsigned long *p;
1005 unsigned long *end;
f633cef0 1006
2c9b238e 1007 trace_assign_type(field, iter->ent);
4a9bd3f1 1008 end = (unsigned long *)((long)iter->ent + iter->ent_size);
f633cef0 1009
19a7fe20 1010 trace_seq_puts(s, "<stack trace>\n");
4a9bd3f1
SR
1011
1012 for (p = field->caller; p && *p != ULONG_MAX && p < end; p++) {
f633cef0 1013
19a7fe20
SRRH
1014 if (trace_seq_has_overflowed(s))
1015 break;
f633cef0 1016
19a7fe20
SRRH
1017 trace_seq_puts(s, " => ");
1018 seq_print_ip_sym(s, *p, flags);
1019 trace_seq_putc(s, '\n');
1020 }
f633cef0 1021
19a7fe20 1022 return trace_handle_return(s);
f633cef0
SR
1023}
1024
a9a57763 1025static struct trace_event_functions trace_stack_funcs = {
f633cef0 1026 .trace = trace_stack_print,
f633cef0
SR
1027};
1028
a9a57763
SR
1029static struct trace_event trace_stack_event = {
1030 .type = TRACE_STACK,
1031 .funcs = &trace_stack_funcs,
1032};
1033
f633cef0 1034/* TRACE_USER_STACK */
ae7462b4 1035static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
a9a57763 1036 int flags, struct trace_event *event)
f633cef0
SR
1037{
1038 struct userstack_entry *field;
2c9b238e 1039 struct trace_seq *s = &iter->seq;
6b1032d5
SRRH
1040 struct mm_struct *mm = NULL;
1041 unsigned int i;
f633cef0 1042
2c9b238e 1043 trace_assign_type(field, iter->ent);
f633cef0 1044
19a7fe20 1045 trace_seq_puts(s, "<user stack trace>\n");
6b1032d5
SRRH
1046
1047 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
1048 struct task_struct *task;
1049 /*
1050 * we do the lookup on the thread group leader,
1051 * since individual threads might have already quit!
1052 */
1053 rcu_read_lock();
1054 task = find_task_by_vpid(field->tgid);
1055 if (task)
1056 mm = get_task_mm(task);
1057 rcu_read_unlock();
1058 }
1059
1060 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1061 unsigned long ip = field->caller[i];
1062
1063 if (ip == ULONG_MAX || trace_seq_has_overflowed(s))
1064 break;
1065
1066 trace_seq_puts(s, " => ");
1067
1068 if (!ip) {
1069 trace_seq_puts(s, "??");
1070 trace_seq_putc(s, '\n');
1071 continue;
1072 }
1073
1074 seq_print_user_ip(s, mm, ip, flags);
1075 trace_seq_putc(s, '\n');
1076 }
1077
1078 if (mm)
1079 mmput(mm);
f633cef0 1080
19a7fe20 1081 return trace_handle_return(s);
f633cef0
SR
1082}
1083
a9a57763 1084static struct trace_event_functions trace_user_stack_funcs = {
f633cef0 1085 .trace = trace_user_stack_print,
f633cef0
SR
1086};
1087
a9a57763
SR
1088static struct trace_event trace_user_stack_event = {
1089 .type = TRACE_USER_STACK,
1090 .funcs = &trace_user_stack_funcs,
1091};
1092
09ae7234
SRRH
1093/* TRACE_BPUTS */
1094static enum print_line_t
1095trace_bputs_print(struct trace_iterator *iter, int flags,
1096 struct trace_event *event)
1097{
1098 struct trace_entry *entry = iter->ent;
1099 struct trace_seq *s = &iter->seq;
1100 struct bputs_entry *field;
1101
1102 trace_assign_type(field, entry);
1103
19a7fe20
SRRH
1104 seq_print_ip_sym(s, field->ip, flags);
1105 trace_seq_puts(s, ": ");
1106 trace_seq_puts(s, field->str);
09ae7234 1107
19a7fe20 1108 return trace_handle_return(s);
09ae7234
SRRH
1109}
1110
1111
1112static enum print_line_t
1113trace_bputs_raw(struct trace_iterator *iter, int flags,
1114 struct trace_event *event)
1115{
1116 struct bputs_entry *field;
1117 struct trace_seq *s = &iter->seq;
1118
1119 trace_assign_type(field, iter->ent);
1120
19a7fe20
SRRH
1121 trace_seq_printf(s, ": %lx : ", field->ip);
1122 trace_seq_puts(s, field->str);
09ae7234 1123
19a7fe20 1124 return trace_handle_return(s);
09ae7234
SRRH
1125}
1126
1127static struct trace_event_functions trace_bputs_funcs = {
1128 .trace = trace_bputs_print,
1129 .raw = trace_bputs_raw,
1130};
1131
1132static struct trace_event trace_bputs_event = {
1133 .type = TRACE_BPUTS,
1134 .funcs = &trace_bputs_funcs,
1135};
1136
48ead020 1137/* TRACE_BPRINT */
1427cdf0 1138static enum print_line_t
a9a57763
SR
1139trace_bprint_print(struct trace_iterator *iter, int flags,
1140 struct trace_event *event)
1427cdf0
LJ
1141{
1142 struct trace_entry *entry = iter->ent;
1143 struct trace_seq *s = &iter->seq;
48ead020 1144 struct bprint_entry *field;
1427cdf0
LJ
1145
1146 trace_assign_type(field, entry);
1147
19a7fe20
SRRH
1148 seq_print_ip_sym(s, field->ip, flags);
1149 trace_seq_puts(s, ": ");
1150 trace_seq_bprintf(s, field->fmt, field->buf);
1427cdf0 1151
19a7fe20 1152 return trace_handle_return(s);
1427cdf0
LJ
1153}
1154
769b0441 1155
48ead020 1156static enum print_line_t
a9a57763
SR
1157trace_bprint_raw(struct trace_iterator *iter, int flags,
1158 struct trace_event *event)
1427cdf0 1159{
48ead020 1160 struct bprint_entry *field;
1427cdf0 1161 struct trace_seq *s = &iter->seq;
1427cdf0 1162
769b0441 1163 trace_assign_type(field, iter->ent);
1427cdf0 1164
19a7fe20
SRRH
1165 trace_seq_printf(s, ": %lx : ", field->ip);
1166 trace_seq_bprintf(s, field->fmt, field->buf);
1427cdf0 1167
19a7fe20 1168 return trace_handle_return(s);
1427cdf0
LJ
1169}
1170
a9a57763
SR
1171static struct trace_event_functions trace_bprint_funcs = {
1172 .trace = trace_bprint_print,
1173 .raw = trace_bprint_raw,
1174};
769b0441 1175
48ead020
FW
1176static struct trace_event trace_bprint_event = {
1177 .type = TRACE_BPRINT,
a9a57763 1178 .funcs = &trace_bprint_funcs,
48ead020
FW
1179};
1180
1181/* TRACE_PRINT */
1182static enum print_line_t trace_print_print(struct trace_iterator *iter,
a9a57763 1183 int flags, struct trace_event *event)
48ead020
FW
1184{
1185 struct print_entry *field;
1186 struct trace_seq *s = &iter->seq;
1187
1188 trace_assign_type(field, iter->ent);
1189
19a7fe20
SRRH
1190 seq_print_ip_sym(s, field->ip, flags);
1191 trace_seq_printf(s, ": %s", field->buf);
48ead020 1192
19a7fe20 1193 return trace_handle_return(s);
48ead020
FW
1194}
1195
a9a57763
SR
1196static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
1197 struct trace_event *event)
48ead020
FW
1198{
1199 struct print_entry *field;
1200
1201 trace_assign_type(field, iter->ent);
1202
19a7fe20 1203 trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf);
48ead020 1204
19a7fe20 1205 return trace_handle_return(&iter->seq);
48ead020
FW
1206}
1207
a9a57763 1208static struct trace_event_functions trace_print_funcs = {
769b0441
FW
1209 .trace = trace_print_print,
1210 .raw = trace_print_raw,
1427cdf0
LJ
1211};
1212
a9a57763
SR
1213static struct trace_event trace_print_event = {
1214 .type = TRACE_PRINT,
1215 .funcs = &trace_print_funcs,
1216};
1217
48ead020 1218
f633cef0
SR
1219static struct trace_event *events[] __initdata = {
1220 &trace_fn_event,
1221 &trace_ctx_event,
1222 &trace_wake_event,
f633cef0
SR
1223 &trace_stack_event,
1224 &trace_user_stack_event,
09ae7234 1225 &trace_bputs_event,
48ead020 1226 &trace_bprint_event,
f633cef0
SR
1227 &trace_print_event,
1228 NULL
1229};
1230
1231__init static int init_events(void)
1232{
1233 struct trace_event *event;
1234 int i, ret;
1235
1236 for (i = 0; events[i]; i++) {
1237 event = events[i];
1238
9023c930 1239 ret = register_trace_event(event);
f633cef0
SR
1240 if (!ret) {
1241 printk(KERN_WARNING "event %d failed to register\n",
1242 event->type);
1243 WARN_ON_ONCE(1);
1244 }
1245 }
1246
1247 return 0;
1248}
7374e827 1249early_initcall(init_events);