xref: /openbmc/linux/kernel/trace/trace_output.c (revision 31b90347)
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 
17 DECLARE_RWSEM(trace_event_sem);
18 
19 static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
20 
21 static int next_event_type = __TRACE_LAST_TYPE + 1;
22 
23 int trace_print_seq(struct seq_file *m, struct trace_seq *s)
24 {
25 	int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
26 	int ret;
27 
28 	ret = seq_write(m, s->buffer, len);
29 
30 	/*
31 	 * Only reset this buffer if we successfully wrote to the
32 	 * seq_file buffer.
33 	 */
34 	if (!ret)
35 		trace_seq_init(s);
36 
37 	return ret;
38 }
39 
40 enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter)
41 {
42 	struct trace_seq *s = &iter->seq;
43 	struct trace_entry *entry = iter->ent;
44 	struct bputs_entry *field;
45 	int ret;
46 
47 	trace_assign_type(field, entry);
48 
49 	ret = trace_seq_puts(s, field->str);
50 	if (!ret)
51 		return TRACE_TYPE_PARTIAL_LINE;
52 
53 	return TRACE_TYPE_HANDLED;
54 }
55 
56 enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
57 {
58 	struct trace_seq *s = &iter->seq;
59 	struct trace_entry *entry = iter->ent;
60 	struct bprint_entry *field;
61 	int ret;
62 
63 	trace_assign_type(field, entry);
64 
65 	ret = trace_seq_bprintf(s, field->fmt, field->buf);
66 	if (!ret)
67 		return TRACE_TYPE_PARTIAL_LINE;
68 
69 	return TRACE_TYPE_HANDLED;
70 }
71 
72 enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
73 {
74 	struct trace_seq *s = &iter->seq;
75 	struct trace_entry *entry = iter->ent;
76 	struct print_entry *field;
77 	int ret;
78 
79 	trace_assign_type(field, entry);
80 
81 	ret = trace_seq_puts(s, field->buf);
82 	if (!ret)
83 		return TRACE_TYPE_PARTIAL_LINE;
84 
85 	return TRACE_TYPE_HANDLED;
86 }
87 
88 /**
89  * trace_seq_printf - sequence printing of trace information
90  * @s: trace sequence descriptor
91  * @fmt: printf format string
92  *
93  * It returns 0 if the trace oversizes the buffer's free
94  * space, 1 otherwise.
95  *
96  * The tracer may use either sequence operations or its own
97  * copy to user routines. To simplify formating of a trace
98  * trace_seq_printf is used to store strings into a special
99  * buffer (@s). Then the output may be either used by
100  * the sequencer or pulled into another buffer.
101  */
102 int
103 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
104 {
105 	int len = (PAGE_SIZE - 1) - s->len;
106 	va_list ap;
107 	int ret;
108 
109 	if (s->full || !len)
110 		return 0;
111 
112 	va_start(ap, fmt);
113 	ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
114 	va_end(ap);
115 
116 	/* If we can't write it all, don't bother writing anything */
117 	if (ret >= len) {
118 		s->full = 1;
119 		return 0;
120 	}
121 
122 	s->len += ret;
123 
124 	return 1;
125 }
126 EXPORT_SYMBOL_GPL(trace_seq_printf);
127 
128 /**
129  * trace_seq_vprintf - sequence printing of trace information
130  * @s: trace sequence descriptor
131  * @fmt: printf format string
132  *
133  * The tracer may use either sequence operations or its own
134  * copy to user routines. To simplify formating of a trace
135  * trace_seq_printf is used to store strings into a special
136  * buffer (@s). Then the output may be either used by
137  * the sequencer or pulled into another buffer.
138  */
139 int
140 trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
141 {
142 	int len = (PAGE_SIZE - 1) - s->len;
143 	int ret;
144 
145 	if (s->full || !len)
146 		return 0;
147 
148 	ret = vsnprintf(s->buffer + s->len, len, fmt, args);
149 
150 	/* If we can't write it all, don't bother writing anything */
151 	if (ret >= len) {
152 		s->full = 1;
153 		return 0;
154 	}
155 
156 	s->len += ret;
157 
158 	return len;
159 }
160 EXPORT_SYMBOL_GPL(trace_seq_vprintf);
161 
162 int trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
163 {
164 	int len = (PAGE_SIZE - 1) - s->len;
165 	int ret;
166 
167 	if (s->full || !len)
168 		return 0;
169 
170 	ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
171 
172 	/* If we can't write it all, don't bother writing anything */
173 	if (ret >= len) {
174 		s->full = 1;
175 		return 0;
176 	}
177 
178 	s->len += ret;
179 
180 	return len;
181 }
182 
183 /**
184  * trace_seq_puts - trace sequence printing of simple string
185  * @s: trace sequence descriptor
186  * @str: simple string to record
187  *
188  * The tracer may use either the sequence operations or its own
189  * copy to user routines. This function records a simple string
190  * into a special buffer (@s) for later retrieval by a sequencer
191  * or other mechanism.
192  */
193 int trace_seq_puts(struct trace_seq *s, const char *str)
194 {
195 	int len = strlen(str);
196 
197 	if (s->full)
198 		return 0;
199 
200 	if (len > ((PAGE_SIZE - 1) - s->len)) {
201 		s->full = 1;
202 		return 0;
203 	}
204 
205 	memcpy(s->buffer + s->len, str, len);
206 	s->len += len;
207 
208 	return len;
209 }
210 
211 int trace_seq_putc(struct trace_seq *s, unsigned char c)
212 {
213 	if (s->full)
214 		return 0;
215 
216 	if (s->len >= (PAGE_SIZE - 1)) {
217 		s->full = 1;
218 		return 0;
219 	}
220 
221 	s->buffer[s->len++] = c;
222 
223 	return 1;
224 }
225 EXPORT_SYMBOL(trace_seq_putc);
226 
227 int trace_seq_putmem(struct trace_seq *s, const void *mem, size_t len)
228 {
229 	if (s->full)
230 		return 0;
231 
232 	if (len > ((PAGE_SIZE - 1) - s->len)) {
233 		s->full = 1;
234 		return 0;
235 	}
236 
237 	memcpy(s->buffer + s->len, mem, len);
238 	s->len += len;
239 
240 	return len;
241 }
242 
243 int trace_seq_putmem_hex(struct trace_seq *s, const void *mem, size_t len)
244 {
245 	unsigned char hex[HEX_CHARS];
246 	const unsigned char *data = mem;
247 	int i, j;
248 
249 	if (s->full)
250 		return 0;
251 
252 #ifdef __BIG_ENDIAN
253 	for (i = 0, j = 0; i < len; i++) {
254 #else
255 	for (i = len-1, j = 0; i >= 0; i--) {
256 #endif
257 		hex[j++] = hex_asc_hi(data[i]);
258 		hex[j++] = hex_asc_lo(data[i]);
259 	}
260 	hex[j++] = ' ';
261 
262 	return trace_seq_putmem(s, hex, j);
263 }
264 
265 void *trace_seq_reserve(struct trace_seq *s, size_t len)
266 {
267 	void *ret;
268 
269 	if (s->full)
270 		return NULL;
271 
272 	if (len > ((PAGE_SIZE - 1) - s->len)) {
273 		s->full = 1;
274 		return NULL;
275 	}
276 
277 	ret = s->buffer + s->len;
278 	s->len += len;
279 
280 	return ret;
281 }
282 
283 int trace_seq_path(struct trace_seq *s, const struct path *path)
284 {
285 	unsigned char *p;
286 
287 	if (s->full)
288 		return 0;
289 
290 	if (s->len >= (PAGE_SIZE - 1)) {
291 		s->full = 1;
292 		return 0;
293 	}
294 
295 	p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
296 	if (!IS_ERR(p)) {
297 		p = mangle_path(s->buffer + s->len, p, "\n");
298 		if (p) {
299 			s->len = p - s->buffer;
300 			return 1;
301 		}
302 	} else {
303 		s->buffer[s->len++] = '?';
304 		return 1;
305 	}
306 
307 	s->full = 1;
308 	return 0;
309 }
310 
311 const char *
312 ftrace_print_flags_seq(struct trace_seq *p, const char *delim,
313 		       unsigned long flags,
314 		       const struct trace_print_flags *flag_array)
315 {
316 	unsigned long mask;
317 	const char *str;
318 	const char *ret = p->buffer + p->len;
319 	int i, first = 1;
320 
321 	for (i = 0;  flag_array[i].name && flags; i++) {
322 
323 		mask = flag_array[i].mask;
324 		if ((flags & mask) != mask)
325 			continue;
326 
327 		str = flag_array[i].name;
328 		flags &= ~mask;
329 		if (!first && delim)
330 			trace_seq_puts(p, delim);
331 		else
332 			first = 0;
333 		trace_seq_puts(p, str);
334 	}
335 
336 	/* check for left over flags */
337 	if (flags) {
338 		if (!first && delim)
339 			trace_seq_puts(p, delim);
340 		trace_seq_printf(p, "0x%lx", flags);
341 	}
342 
343 	trace_seq_putc(p, 0);
344 
345 	return ret;
346 }
347 EXPORT_SYMBOL(ftrace_print_flags_seq);
348 
349 const char *
350 ftrace_print_symbols_seq(struct trace_seq *p, unsigned long val,
351 			 const struct trace_print_flags *symbol_array)
352 {
353 	int i;
354 	const char *ret = p->buffer + p->len;
355 
356 	for (i = 0;  symbol_array[i].name; i++) {
357 
358 		if (val != symbol_array[i].mask)
359 			continue;
360 
361 		trace_seq_puts(p, symbol_array[i].name);
362 		break;
363 	}
364 
365 	if (ret == (const char *)(p->buffer + p->len))
366 		trace_seq_printf(p, "0x%lx", val);
367 
368 	trace_seq_putc(p, 0);
369 
370 	return ret;
371 }
372 EXPORT_SYMBOL(ftrace_print_symbols_seq);
373 
374 #if BITS_PER_LONG == 32
375 const char *
376 ftrace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
377 			 const struct trace_print_flags_u64 *symbol_array)
378 {
379 	int i;
380 	const char *ret = p->buffer + p->len;
381 
382 	for (i = 0;  symbol_array[i].name; i++) {
383 
384 		if (val != symbol_array[i].mask)
385 			continue;
386 
387 		trace_seq_puts(p, symbol_array[i].name);
388 		break;
389 	}
390 
391 	if (ret == (const char *)(p->buffer + p->len))
392 		trace_seq_printf(p, "0x%llx", val);
393 
394 	trace_seq_putc(p, 0);
395 
396 	return ret;
397 }
398 EXPORT_SYMBOL(ftrace_print_symbols_seq_u64);
399 #endif
400 
401 const char *
402 ftrace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len)
403 {
404 	int i;
405 	const char *ret = p->buffer + p->len;
406 
407 	for (i = 0; i < buf_len; i++)
408 		trace_seq_printf(p, "%s%2.2x", i == 0 ? "" : " ", buf[i]);
409 
410 	trace_seq_putc(p, 0);
411 
412 	return ret;
413 }
414 EXPORT_SYMBOL(ftrace_print_hex_seq);
415 
416 int ftrace_raw_output_prep(struct trace_iterator *iter,
417 			   struct trace_event *trace_event)
418 {
419 	struct ftrace_event_call *event;
420 	struct trace_seq *s = &iter->seq;
421 	struct trace_seq *p = &iter->tmp_seq;
422 	struct trace_entry *entry;
423 	int ret;
424 
425 	event = container_of(trace_event, struct ftrace_event_call, event);
426 	entry = iter->ent;
427 
428 	if (entry->type != event->event.type) {
429 		WARN_ON_ONCE(1);
430 		return TRACE_TYPE_UNHANDLED;
431 	}
432 
433 	trace_seq_init(p);
434 	ret = trace_seq_printf(s, "%s: ", event->name);
435 	if (!ret)
436 		return TRACE_TYPE_PARTIAL_LINE;
437 
438 	return 0;
439 }
440 EXPORT_SYMBOL(ftrace_raw_output_prep);
441 
442 #ifdef CONFIG_KRETPROBES
443 static inline const char *kretprobed(const char *name)
444 {
445 	static const char tramp_name[] = "kretprobe_trampoline";
446 	int size = sizeof(tramp_name);
447 
448 	if (strncmp(tramp_name, name, size) == 0)
449 		return "[unknown/kretprobe'd]";
450 	return name;
451 }
452 #else
453 static inline const char *kretprobed(const char *name)
454 {
455 	return name;
456 }
457 #endif /* CONFIG_KRETPROBES */
458 
459 static int
460 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
461 {
462 #ifdef CONFIG_KALLSYMS
463 	char str[KSYM_SYMBOL_LEN];
464 	const char *name;
465 
466 	kallsyms_lookup(address, NULL, NULL, NULL, str);
467 
468 	name = kretprobed(str);
469 
470 	return trace_seq_printf(s, fmt, name);
471 #endif
472 	return 1;
473 }
474 
475 static int
476 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
477 		     unsigned long address)
478 {
479 #ifdef CONFIG_KALLSYMS
480 	char str[KSYM_SYMBOL_LEN];
481 	const char *name;
482 
483 	sprint_symbol(str, address);
484 	name = kretprobed(str);
485 
486 	return trace_seq_printf(s, fmt, name);
487 #endif
488 	return 1;
489 }
490 
491 #ifndef CONFIG_64BIT
492 # define IP_FMT "%08lx"
493 #else
494 # define IP_FMT "%016lx"
495 #endif
496 
497 int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
498 		      unsigned long ip, unsigned long sym_flags)
499 {
500 	struct file *file = NULL;
501 	unsigned long vmstart = 0;
502 	int ret = 1;
503 
504 	if (s->full)
505 		return 0;
506 
507 	if (mm) {
508 		const struct vm_area_struct *vma;
509 
510 		down_read(&mm->mmap_sem);
511 		vma = find_vma(mm, ip);
512 		if (vma) {
513 			file = vma->vm_file;
514 			vmstart = vma->vm_start;
515 		}
516 		if (file) {
517 			ret = trace_seq_path(s, &file->f_path);
518 			if (ret)
519 				ret = trace_seq_printf(s, "[+0x%lx]",
520 						       ip - vmstart);
521 		}
522 		up_read(&mm->mmap_sem);
523 	}
524 	if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
525 		ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
526 	return ret;
527 }
528 
529 int
530 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
531 		      unsigned long sym_flags)
532 {
533 	struct mm_struct *mm = NULL;
534 	int ret = 1;
535 	unsigned int i;
536 
537 	if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
538 		struct task_struct *task;
539 		/*
540 		 * we do the lookup on the thread group leader,
541 		 * since individual threads might have already quit!
542 		 */
543 		rcu_read_lock();
544 		task = find_task_by_vpid(entry->tgid);
545 		if (task)
546 			mm = get_task_mm(task);
547 		rcu_read_unlock();
548 	}
549 
550 	for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
551 		unsigned long ip = entry->caller[i];
552 
553 		if (ip == ULONG_MAX || !ret)
554 			break;
555 		if (ret)
556 			ret = trace_seq_puts(s, " => ");
557 		if (!ip) {
558 			if (ret)
559 				ret = trace_seq_puts(s, "??");
560 			if (ret)
561 				ret = trace_seq_putc(s, '\n');
562 			continue;
563 		}
564 		if (!ret)
565 			break;
566 		if (ret)
567 			ret = seq_print_user_ip(s, mm, ip, sym_flags);
568 		ret = trace_seq_putc(s, '\n');
569 	}
570 
571 	if (mm)
572 		mmput(mm);
573 	return ret;
574 }
575 
576 int
577 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
578 {
579 	int ret;
580 
581 	if (!ip)
582 		return trace_seq_putc(s, '0');
583 
584 	if (sym_flags & TRACE_ITER_SYM_OFFSET)
585 		ret = seq_print_sym_offset(s, "%s", ip);
586 	else
587 		ret = seq_print_sym_short(s, "%s", ip);
588 
589 	if (!ret)
590 		return 0;
591 
592 	if (sym_flags & TRACE_ITER_SYM_ADDR)
593 		ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
594 	return ret;
595 }
596 
597 /**
598  * trace_print_lat_fmt - print the irq, preempt and lockdep fields
599  * @s: trace seq struct to write to
600  * @entry: The trace entry field from the ring buffer
601  *
602  * Prints the generic fields of irqs off, in hard or softirq, preempt
603  * count.
604  */
605 int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
606 {
607 	char hardsoft_irq;
608 	char need_resched;
609 	char irqs_off;
610 	int hardirq;
611 	int softirq;
612 	int ret;
613 
614 	hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
615 	softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
616 
617 	irqs_off =
618 		(entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
619 		(entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' :
620 		'.';
621 
622 	switch (entry->flags & (TRACE_FLAG_NEED_RESCHED |
623 				TRACE_FLAG_PREEMPT_RESCHED)) {
624 	case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
625 		need_resched = 'N';
626 		break;
627 	case TRACE_FLAG_NEED_RESCHED:
628 		need_resched = 'n';
629 		break;
630 	case TRACE_FLAG_PREEMPT_RESCHED:
631 		need_resched = 'p';
632 		break;
633 	default:
634 		need_resched = '.';
635 		break;
636 	}
637 
638 	hardsoft_irq =
639 		(hardirq && softirq) ? 'H' :
640 		hardirq ? 'h' :
641 		softirq ? 's' :
642 		'.';
643 
644 	if (!trace_seq_printf(s, "%c%c%c",
645 			      irqs_off, need_resched, hardsoft_irq))
646 		return 0;
647 
648 	if (entry->preempt_count)
649 		ret = trace_seq_printf(s, "%x", entry->preempt_count);
650 	else
651 		ret = trace_seq_putc(s, '.');
652 
653 	return ret;
654 }
655 
656 static int
657 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
658 {
659 	char comm[TASK_COMM_LEN];
660 
661 	trace_find_cmdline(entry->pid, comm);
662 
663 	if (!trace_seq_printf(s, "%8.8s-%-5d %3d",
664 			      comm, entry->pid, cpu))
665 		return 0;
666 
667 	return trace_print_lat_fmt(s, entry);
668 }
669 
670 static unsigned long preempt_mark_thresh_us = 100;
671 
672 static int
673 lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
674 {
675 	unsigned long verbose = trace_flags & TRACE_ITER_VERBOSE;
676 	unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
677 	unsigned long long abs_ts = iter->ts - iter->trace_buffer->time_start;
678 	unsigned long long rel_ts = next_ts - iter->ts;
679 	struct trace_seq *s = &iter->seq;
680 
681 	if (in_ns) {
682 		abs_ts = ns2usecs(abs_ts);
683 		rel_ts = ns2usecs(rel_ts);
684 	}
685 
686 	if (verbose && in_ns) {
687 		unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
688 		unsigned long abs_msec = (unsigned long)abs_ts;
689 		unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
690 		unsigned long rel_msec = (unsigned long)rel_ts;
691 
692 		return trace_seq_printf(
693 				s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
694 				ns2usecs(iter->ts),
695 				abs_msec, abs_usec,
696 				rel_msec, rel_usec);
697 	} else if (verbose && !in_ns) {
698 		return trace_seq_printf(
699 				s, "[%016llx] %lld (+%lld): ",
700 				iter->ts, abs_ts, rel_ts);
701 	} else if (!verbose && in_ns) {
702 		return trace_seq_printf(
703 				s, " %4lldus%c: ",
704 				abs_ts,
705 				rel_ts > preempt_mark_thresh_us ? '!' :
706 				  rel_ts > 1 ? '+' : ' ');
707 	} else { /* !verbose && !in_ns */
708 		return trace_seq_printf(s, " %4lld: ", abs_ts);
709 	}
710 }
711 
712 int trace_print_context(struct trace_iterator *iter)
713 {
714 	struct trace_seq *s = &iter->seq;
715 	struct trace_entry *entry = iter->ent;
716 	unsigned long long t;
717 	unsigned long secs, usec_rem;
718 	char comm[TASK_COMM_LEN];
719 	int ret;
720 
721 	trace_find_cmdline(entry->pid, comm);
722 
723 	ret = trace_seq_printf(s, "%16s-%-5d [%03d] ",
724 			       comm, entry->pid, iter->cpu);
725 	if (!ret)
726 		return 0;
727 
728 	if (trace_flags & TRACE_ITER_IRQ_INFO) {
729 		ret = trace_print_lat_fmt(s, entry);
730 		if (!ret)
731 			return 0;
732 	}
733 
734 	if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
735 		t = ns2usecs(iter->ts);
736 		usec_rem = do_div(t, USEC_PER_SEC);
737 		secs = (unsigned long)t;
738 		return trace_seq_printf(s, " %5lu.%06lu: ", secs, usec_rem);
739 	} else
740 		return trace_seq_printf(s, " %12llu: ", iter->ts);
741 }
742 
743 int trace_print_lat_context(struct trace_iterator *iter)
744 {
745 	u64 next_ts;
746 	int ret;
747 	/* trace_find_next_entry will reset ent_size */
748 	int ent_size = iter->ent_size;
749 	struct trace_seq *s = &iter->seq;
750 	struct trace_entry *entry = iter->ent,
751 			   *next_entry = trace_find_next_entry(iter, NULL,
752 							       &next_ts);
753 	unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
754 
755 	/* Restore the original ent_size */
756 	iter->ent_size = ent_size;
757 
758 	if (!next_entry)
759 		next_ts = iter->ts;
760 
761 	if (verbose) {
762 		char comm[TASK_COMM_LEN];
763 
764 		trace_find_cmdline(entry->pid, comm);
765 
766 		ret = trace_seq_printf(
767 				s, "%16s %5d %3d %d %08x %08lx ",
768 				comm, entry->pid, iter->cpu, entry->flags,
769 				entry->preempt_count, iter->idx);
770 	} else {
771 		ret = lat_print_generic(s, entry, iter->cpu);
772 	}
773 
774 	if (ret)
775 		ret = lat_print_timestamp(iter, next_ts);
776 
777 	return ret;
778 }
779 
780 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
781 
782 static int task_state_char(unsigned long state)
783 {
784 	int bit = state ? __ffs(state) + 1 : 0;
785 
786 	return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
787 }
788 
789 /**
790  * ftrace_find_event - find a registered event
791  * @type: the type of event to look for
792  *
793  * Returns an event of type @type otherwise NULL
794  * Called with trace_event_read_lock() held.
795  */
796 struct trace_event *ftrace_find_event(int type)
797 {
798 	struct trace_event *event;
799 	unsigned key;
800 
801 	key = type & (EVENT_HASHSIZE - 1);
802 
803 	hlist_for_each_entry(event, &event_hash[key], node) {
804 		if (event->type == type)
805 			return event;
806 	}
807 
808 	return NULL;
809 }
810 
811 static LIST_HEAD(ftrace_event_list);
812 
813 static int trace_search_list(struct list_head **list)
814 {
815 	struct trace_event *e;
816 	int last = __TRACE_LAST_TYPE;
817 
818 	if (list_empty(&ftrace_event_list)) {
819 		*list = &ftrace_event_list;
820 		return last + 1;
821 	}
822 
823 	/*
824 	 * We used up all possible max events,
825 	 * lets see if somebody freed one.
826 	 */
827 	list_for_each_entry(e, &ftrace_event_list, list) {
828 		if (e->type != last + 1)
829 			break;
830 		last++;
831 	}
832 
833 	/* Did we used up all 65 thousand events??? */
834 	if ((last + 1) > FTRACE_MAX_EVENT)
835 		return 0;
836 
837 	*list = &e->list;
838 	return last + 1;
839 }
840 
841 void trace_event_read_lock(void)
842 {
843 	down_read(&trace_event_sem);
844 }
845 
846 void trace_event_read_unlock(void)
847 {
848 	up_read(&trace_event_sem);
849 }
850 
851 /**
852  * register_ftrace_event - register output for an event type
853  * @event: the event type to register
854  *
855  * Event types are stored in a hash and this hash is used to
856  * find a way to print an event. If the @event->type is set
857  * then it will use that type, otherwise it will assign a
858  * type to use.
859  *
860  * If you assign your own type, please make sure it is added
861  * to the trace_type enum in trace.h, to avoid collisions
862  * with the dynamic types.
863  *
864  * Returns the event type number or zero on error.
865  */
866 int register_ftrace_event(struct trace_event *event)
867 {
868 	unsigned key;
869 	int ret = 0;
870 
871 	down_write(&trace_event_sem);
872 
873 	if (WARN_ON(!event))
874 		goto out;
875 
876 	if (WARN_ON(!event->funcs))
877 		goto out;
878 
879 	INIT_LIST_HEAD(&event->list);
880 
881 	if (!event->type) {
882 		struct list_head *list = NULL;
883 
884 		if (next_event_type > FTRACE_MAX_EVENT) {
885 
886 			event->type = trace_search_list(&list);
887 			if (!event->type)
888 				goto out;
889 
890 		} else {
891 
892 			event->type = next_event_type++;
893 			list = &ftrace_event_list;
894 		}
895 
896 		if (WARN_ON(ftrace_find_event(event->type)))
897 			goto out;
898 
899 		list_add_tail(&event->list, list);
900 
901 	} else if (event->type > __TRACE_LAST_TYPE) {
902 		printk(KERN_WARNING "Need to add type to trace.h\n");
903 		WARN_ON(1);
904 		goto out;
905 	} else {
906 		/* Is this event already used */
907 		if (ftrace_find_event(event->type))
908 			goto out;
909 	}
910 
911 	if (event->funcs->trace == NULL)
912 		event->funcs->trace = trace_nop_print;
913 	if (event->funcs->raw == NULL)
914 		event->funcs->raw = trace_nop_print;
915 	if (event->funcs->hex == NULL)
916 		event->funcs->hex = trace_nop_print;
917 	if (event->funcs->binary == NULL)
918 		event->funcs->binary = trace_nop_print;
919 
920 	key = event->type & (EVENT_HASHSIZE - 1);
921 
922 	hlist_add_head(&event->node, &event_hash[key]);
923 
924 	ret = event->type;
925  out:
926 	up_write(&trace_event_sem);
927 
928 	return ret;
929 }
930 EXPORT_SYMBOL_GPL(register_ftrace_event);
931 
932 /*
933  * Used by module code with the trace_event_sem held for write.
934  */
935 int __unregister_ftrace_event(struct trace_event *event)
936 {
937 	hlist_del(&event->node);
938 	list_del(&event->list);
939 	return 0;
940 }
941 
942 /**
943  * unregister_ftrace_event - remove a no longer used event
944  * @event: the event to remove
945  */
946 int unregister_ftrace_event(struct trace_event *event)
947 {
948 	down_write(&trace_event_sem);
949 	__unregister_ftrace_event(event);
950 	up_write(&trace_event_sem);
951 
952 	return 0;
953 }
954 EXPORT_SYMBOL_GPL(unregister_ftrace_event);
955 
956 /*
957  * Standard events
958  */
959 
960 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
961 				  struct trace_event *event)
962 {
963 	if (!trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type))
964 		return TRACE_TYPE_PARTIAL_LINE;
965 
966 	return TRACE_TYPE_HANDLED;
967 }
968 
969 /* TRACE_FN */
970 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
971 					struct trace_event *event)
972 {
973 	struct ftrace_entry *field;
974 	struct trace_seq *s = &iter->seq;
975 
976 	trace_assign_type(field, iter->ent);
977 
978 	if (!seq_print_ip_sym(s, field->ip, flags))
979 		goto partial;
980 
981 	if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
982 		if (!trace_seq_puts(s, " <-"))
983 			goto partial;
984 		if (!seq_print_ip_sym(s,
985 				      field->parent_ip,
986 				      flags))
987 			goto partial;
988 	}
989 	if (!trace_seq_putc(s, '\n'))
990 		goto partial;
991 
992 	return TRACE_TYPE_HANDLED;
993 
994  partial:
995 	return TRACE_TYPE_PARTIAL_LINE;
996 }
997 
998 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
999 				      struct trace_event *event)
1000 {
1001 	struct ftrace_entry *field;
1002 
1003 	trace_assign_type(field, iter->ent);
1004 
1005 	if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
1006 			      field->ip,
1007 			      field->parent_ip))
1008 		return TRACE_TYPE_PARTIAL_LINE;
1009 
1010 	return TRACE_TYPE_HANDLED;
1011 }
1012 
1013 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
1014 				      struct trace_event *event)
1015 {
1016 	struct ftrace_entry *field;
1017 	struct trace_seq *s = &iter->seq;
1018 
1019 	trace_assign_type(field, iter->ent);
1020 
1021 	SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1022 	SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1023 
1024 	return TRACE_TYPE_HANDLED;
1025 }
1026 
1027 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
1028 				      struct trace_event *event)
1029 {
1030 	struct ftrace_entry *field;
1031 	struct trace_seq *s = &iter->seq;
1032 
1033 	trace_assign_type(field, iter->ent);
1034 
1035 	SEQ_PUT_FIELD_RET(s, field->ip);
1036 	SEQ_PUT_FIELD_RET(s, field->parent_ip);
1037 
1038 	return TRACE_TYPE_HANDLED;
1039 }
1040 
1041 static struct trace_event_functions trace_fn_funcs = {
1042 	.trace		= trace_fn_trace,
1043 	.raw		= trace_fn_raw,
1044 	.hex		= trace_fn_hex,
1045 	.binary		= trace_fn_bin,
1046 };
1047 
1048 static struct trace_event trace_fn_event = {
1049 	.type		= TRACE_FN,
1050 	.funcs		= &trace_fn_funcs,
1051 };
1052 
1053 /* TRACE_CTX an TRACE_WAKE */
1054 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
1055 					     char *delim)
1056 {
1057 	struct ctx_switch_entry *field;
1058 	char comm[TASK_COMM_LEN];
1059 	int S, T;
1060 
1061 
1062 	trace_assign_type(field, iter->ent);
1063 
1064 	T = task_state_char(field->next_state);
1065 	S = task_state_char(field->prev_state);
1066 	trace_find_cmdline(field->next_pid, comm);
1067 	if (!trace_seq_printf(&iter->seq,
1068 			      " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1069 			      field->prev_pid,
1070 			      field->prev_prio,
1071 			      S, delim,
1072 			      field->next_cpu,
1073 			      field->next_pid,
1074 			      field->next_prio,
1075 			      T, comm))
1076 		return TRACE_TYPE_PARTIAL_LINE;
1077 
1078 	return TRACE_TYPE_HANDLED;
1079 }
1080 
1081 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
1082 					 struct trace_event *event)
1083 {
1084 	return trace_ctxwake_print(iter, "==>");
1085 }
1086 
1087 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
1088 					  int flags, struct trace_event *event)
1089 {
1090 	return trace_ctxwake_print(iter, "  +");
1091 }
1092 
1093 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
1094 {
1095 	struct ctx_switch_entry *field;
1096 	int T;
1097 
1098 	trace_assign_type(field, iter->ent);
1099 
1100 	if (!S)
1101 		S = task_state_char(field->prev_state);
1102 	T = task_state_char(field->next_state);
1103 	if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
1104 			      field->prev_pid,
1105 			      field->prev_prio,
1106 			      S,
1107 			      field->next_cpu,
1108 			      field->next_pid,
1109 			      field->next_prio,
1110 			      T))
1111 		return TRACE_TYPE_PARTIAL_LINE;
1112 
1113 	return TRACE_TYPE_HANDLED;
1114 }
1115 
1116 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
1117 				       struct trace_event *event)
1118 {
1119 	return trace_ctxwake_raw(iter, 0);
1120 }
1121 
1122 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
1123 					struct trace_event *event)
1124 {
1125 	return trace_ctxwake_raw(iter, '+');
1126 }
1127 
1128 
1129 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
1130 {
1131 	struct ctx_switch_entry *field;
1132 	struct trace_seq *s = &iter->seq;
1133 	int T;
1134 
1135 	trace_assign_type(field, iter->ent);
1136 
1137 	if (!S)
1138 		S = task_state_char(field->prev_state);
1139 	T = task_state_char(field->next_state);
1140 
1141 	SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1142 	SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1143 	SEQ_PUT_HEX_FIELD_RET(s, S);
1144 	SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1145 	SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1146 	SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1147 	SEQ_PUT_HEX_FIELD_RET(s, T);
1148 
1149 	return TRACE_TYPE_HANDLED;
1150 }
1151 
1152 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
1153 				       struct trace_event *event)
1154 {
1155 	return trace_ctxwake_hex(iter, 0);
1156 }
1157 
1158 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
1159 					struct trace_event *event)
1160 {
1161 	return trace_ctxwake_hex(iter, '+');
1162 }
1163 
1164 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1165 					   int flags, struct trace_event *event)
1166 {
1167 	struct ctx_switch_entry *field;
1168 	struct trace_seq *s = &iter->seq;
1169 
1170 	trace_assign_type(field, iter->ent);
1171 
1172 	SEQ_PUT_FIELD_RET(s, field->prev_pid);
1173 	SEQ_PUT_FIELD_RET(s, field->prev_prio);
1174 	SEQ_PUT_FIELD_RET(s, field->prev_state);
1175 	SEQ_PUT_FIELD_RET(s, field->next_pid);
1176 	SEQ_PUT_FIELD_RET(s, field->next_prio);
1177 	SEQ_PUT_FIELD_RET(s, field->next_state);
1178 
1179 	return TRACE_TYPE_HANDLED;
1180 }
1181 
1182 static struct trace_event_functions trace_ctx_funcs = {
1183 	.trace		= trace_ctx_print,
1184 	.raw		= trace_ctx_raw,
1185 	.hex		= trace_ctx_hex,
1186 	.binary		= trace_ctxwake_bin,
1187 };
1188 
1189 static struct trace_event trace_ctx_event = {
1190 	.type		= TRACE_CTX,
1191 	.funcs		= &trace_ctx_funcs,
1192 };
1193 
1194 static struct trace_event_functions trace_wake_funcs = {
1195 	.trace		= trace_wake_print,
1196 	.raw		= trace_wake_raw,
1197 	.hex		= trace_wake_hex,
1198 	.binary		= trace_ctxwake_bin,
1199 };
1200 
1201 static struct trace_event trace_wake_event = {
1202 	.type		= TRACE_WAKE,
1203 	.funcs		= &trace_wake_funcs,
1204 };
1205 
1206 /* TRACE_STACK */
1207 
1208 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1209 					   int flags, struct trace_event *event)
1210 {
1211 	struct stack_entry *field;
1212 	struct trace_seq *s = &iter->seq;
1213 	unsigned long *p;
1214 	unsigned long *end;
1215 
1216 	trace_assign_type(field, iter->ent);
1217 	end = (unsigned long *)((long)iter->ent + iter->ent_size);
1218 
1219 	if (!trace_seq_puts(s, "<stack trace>\n"))
1220 		goto partial;
1221 
1222 	for (p = field->caller; p && *p != ULONG_MAX && p < end; p++) {
1223 		if (!trace_seq_puts(s, " => "))
1224 			goto partial;
1225 
1226 		if (!seq_print_ip_sym(s, *p, flags))
1227 			goto partial;
1228 		if (!trace_seq_putc(s, '\n'))
1229 			goto partial;
1230 	}
1231 
1232 	return TRACE_TYPE_HANDLED;
1233 
1234  partial:
1235 	return TRACE_TYPE_PARTIAL_LINE;
1236 }
1237 
1238 static struct trace_event_functions trace_stack_funcs = {
1239 	.trace		= trace_stack_print,
1240 };
1241 
1242 static struct trace_event trace_stack_event = {
1243 	.type		= TRACE_STACK,
1244 	.funcs		= &trace_stack_funcs,
1245 };
1246 
1247 /* TRACE_USER_STACK */
1248 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1249 						int flags, struct trace_event *event)
1250 {
1251 	struct userstack_entry *field;
1252 	struct trace_seq *s = &iter->seq;
1253 
1254 	trace_assign_type(field, iter->ent);
1255 
1256 	if (!trace_seq_puts(s, "<user stack trace>\n"))
1257 		goto partial;
1258 
1259 	if (!seq_print_userip_objs(field, s, flags))
1260 		goto partial;
1261 
1262 	return TRACE_TYPE_HANDLED;
1263 
1264  partial:
1265 	return TRACE_TYPE_PARTIAL_LINE;
1266 }
1267 
1268 static struct trace_event_functions trace_user_stack_funcs = {
1269 	.trace		= trace_user_stack_print,
1270 };
1271 
1272 static struct trace_event trace_user_stack_event = {
1273 	.type		= TRACE_USER_STACK,
1274 	.funcs		= &trace_user_stack_funcs,
1275 };
1276 
1277 /* TRACE_BPUTS */
1278 static enum print_line_t
1279 trace_bputs_print(struct trace_iterator *iter, int flags,
1280 		   struct trace_event *event)
1281 {
1282 	struct trace_entry *entry = iter->ent;
1283 	struct trace_seq *s = &iter->seq;
1284 	struct bputs_entry *field;
1285 
1286 	trace_assign_type(field, entry);
1287 
1288 	if (!seq_print_ip_sym(s, field->ip, flags))
1289 		goto partial;
1290 
1291 	if (!trace_seq_puts(s, ": "))
1292 		goto partial;
1293 
1294 	if (!trace_seq_puts(s, field->str))
1295 		goto partial;
1296 
1297 	return TRACE_TYPE_HANDLED;
1298 
1299  partial:
1300 	return TRACE_TYPE_PARTIAL_LINE;
1301 }
1302 
1303 
1304 static enum print_line_t
1305 trace_bputs_raw(struct trace_iterator *iter, int flags,
1306 		struct trace_event *event)
1307 {
1308 	struct bputs_entry *field;
1309 	struct trace_seq *s = &iter->seq;
1310 
1311 	trace_assign_type(field, iter->ent);
1312 
1313 	if (!trace_seq_printf(s, ": %lx : ", field->ip))
1314 		goto partial;
1315 
1316 	if (!trace_seq_puts(s, field->str))
1317 		goto partial;
1318 
1319 	return TRACE_TYPE_HANDLED;
1320 
1321  partial:
1322 	return TRACE_TYPE_PARTIAL_LINE;
1323 }
1324 
1325 static struct trace_event_functions trace_bputs_funcs = {
1326 	.trace		= trace_bputs_print,
1327 	.raw		= trace_bputs_raw,
1328 };
1329 
1330 static struct trace_event trace_bputs_event = {
1331 	.type		= TRACE_BPUTS,
1332 	.funcs		= &trace_bputs_funcs,
1333 };
1334 
1335 /* TRACE_BPRINT */
1336 static enum print_line_t
1337 trace_bprint_print(struct trace_iterator *iter, int flags,
1338 		   struct trace_event *event)
1339 {
1340 	struct trace_entry *entry = iter->ent;
1341 	struct trace_seq *s = &iter->seq;
1342 	struct bprint_entry *field;
1343 
1344 	trace_assign_type(field, entry);
1345 
1346 	if (!seq_print_ip_sym(s, field->ip, flags))
1347 		goto partial;
1348 
1349 	if (!trace_seq_puts(s, ": "))
1350 		goto partial;
1351 
1352 	if (!trace_seq_bprintf(s, field->fmt, field->buf))
1353 		goto partial;
1354 
1355 	return TRACE_TYPE_HANDLED;
1356 
1357  partial:
1358 	return TRACE_TYPE_PARTIAL_LINE;
1359 }
1360 
1361 
1362 static enum print_line_t
1363 trace_bprint_raw(struct trace_iterator *iter, int flags,
1364 		 struct trace_event *event)
1365 {
1366 	struct bprint_entry *field;
1367 	struct trace_seq *s = &iter->seq;
1368 
1369 	trace_assign_type(field, iter->ent);
1370 
1371 	if (!trace_seq_printf(s, ": %lx : ", field->ip))
1372 		goto partial;
1373 
1374 	if (!trace_seq_bprintf(s, field->fmt, field->buf))
1375 		goto partial;
1376 
1377 	return TRACE_TYPE_HANDLED;
1378 
1379  partial:
1380 	return TRACE_TYPE_PARTIAL_LINE;
1381 }
1382 
1383 static struct trace_event_functions trace_bprint_funcs = {
1384 	.trace		= trace_bprint_print,
1385 	.raw		= trace_bprint_raw,
1386 };
1387 
1388 static struct trace_event trace_bprint_event = {
1389 	.type		= TRACE_BPRINT,
1390 	.funcs		= &trace_bprint_funcs,
1391 };
1392 
1393 /* TRACE_PRINT */
1394 static enum print_line_t trace_print_print(struct trace_iterator *iter,
1395 					   int flags, struct trace_event *event)
1396 {
1397 	struct print_entry *field;
1398 	struct trace_seq *s = &iter->seq;
1399 
1400 	trace_assign_type(field, iter->ent);
1401 
1402 	if (!seq_print_ip_sym(s, field->ip, flags))
1403 		goto partial;
1404 
1405 	if (!trace_seq_printf(s, ": %s", field->buf))
1406 		goto partial;
1407 
1408 	return TRACE_TYPE_HANDLED;
1409 
1410  partial:
1411 	return TRACE_TYPE_PARTIAL_LINE;
1412 }
1413 
1414 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
1415 					 struct trace_event *event)
1416 {
1417 	struct print_entry *field;
1418 
1419 	trace_assign_type(field, iter->ent);
1420 
1421 	if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
1422 		goto partial;
1423 
1424 	return TRACE_TYPE_HANDLED;
1425 
1426  partial:
1427 	return TRACE_TYPE_PARTIAL_LINE;
1428 }
1429 
1430 static struct trace_event_functions trace_print_funcs = {
1431 	.trace		= trace_print_print,
1432 	.raw		= trace_print_raw,
1433 };
1434 
1435 static struct trace_event trace_print_event = {
1436 	.type	 	= TRACE_PRINT,
1437 	.funcs		= &trace_print_funcs,
1438 };
1439 
1440 
1441 static struct trace_event *events[] __initdata = {
1442 	&trace_fn_event,
1443 	&trace_ctx_event,
1444 	&trace_wake_event,
1445 	&trace_stack_event,
1446 	&trace_user_stack_event,
1447 	&trace_bputs_event,
1448 	&trace_bprint_event,
1449 	&trace_print_event,
1450 	NULL
1451 };
1452 
1453 __init static int init_events(void)
1454 {
1455 	struct trace_event *event;
1456 	int i, ret;
1457 
1458 	for (i = 0; events[i]; i++) {
1459 		event = events[i];
1460 
1461 		ret = register_ftrace_event(event);
1462 		if (!ret) {
1463 			printk(KERN_WARNING "event %d failed to register\n",
1464 			       event->type);
1465 			WARN_ON_ONCE(1);
1466 		}
1467 	}
1468 
1469 	return 0;
1470 }
1471 early_initcall(init_events);
1472