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