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