xref: /linux/kernel/trace/trace_output.c (revision 7f4f3b14e8079ecde096bd734af10e30d40c27b7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace_output.c
4  *
5  * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6  *
7  */
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/ftrace.h>
11 #include <linux/kprobes.h>
12 #include <linux/sched/clock.h>
13 #include <linux/sched/mm.h>
14 #include <linux/idr.h>
15 
16 #include "trace_output.h"
17 
18 /* must be a power of 2 */
19 #define EVENT_HASHSIZE	128
20 
21 DECLARE_RWSEM(trace_event_sem);
22 
23 static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
24 
25 enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter)
26 {
27 	struct trace_seq *s = &iter->seq;
28 	struct trace_entry *entry = iter->ent;
29 	struct bputs_entry *field;
30 
31 	trace_assign_type(field, entry);
32 
33 	trace_seq_puts(s, field->str);
34 
35 	return trace_handle_return(s);
36 }
37 
38 enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
39 {
40 	struct trace_seq *s = &iter->seq;
41 	struct trace_entry *entry = iter->ent;
42 	struct bprint_entry *field;
43 
44 	trace_assign_type(field, entry);
45 
46 	trace_seq_bprintf(s, field->fmt, field->buf);
47 
48 	return trace_handle_return(s);
49 }
50 
51 enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
52 {
53 	struct trace_seq *s = &iter->seq;
54 	struct trace_entry *entry = iter->ent;
55 	struct print_entry *field;
56 
57 	trace_assign_type(field, entry);
58 
59 	trace_seq_puts(s, field->buf);
60 
61 	return trace_handle_return(s);
62 }
63 
64 const char *
65 trace_print_flags_seq(struct trace_seq *p, const char *delim,
66 		      unsigned long flags,
67 		      const struct trace_print_flags *flag_array)
68 {
69 	unsigned long mask;
70 	const char *str;
71 	const char *ret = trace_seq_buffer_ptr(p);
72 	int i, first = 1;
73 
74 	for (i = 0;  flag_array[i].name && flags; i++) {
75 
76 		mask = flag_array[i].mask;
77 		if ((flags & mask) != mask)
78 			continue;
79 
80 		str = flag_array[i].name;
81 		flags &= ~mask;
82 		if (!first && delim)
83 			trace_seq_puts(p, delim);
84 		else
85 			first = 0;
86 		trace_seq_puts(p, str);
87 	}
88 
89 	/* check for left over flags */
90 	if (flags) {
91 		if (!first && delim)
92 			trace_seq_puts(p, delim);
93 		trace_seq_printf(p, "0x%lx", flags);
94 	}
95 
96 	trace_seq_putc(p, 0);
97 
98 	return ret;
99 }
100 EXPORT_SYMBOL(trace_print_flags_seq);
101 
102 const char *
103 trace_print_symbols_seq(struct trace_seq *p, unsigned long val,
104 			const struct trace_print_flags *symbol_array)
105 {
106 	int i;
107 	const char *ret = trace_seq_buffer_ptr(p);
108 
109 	for (i = 0;  symbol_array[i].name; i++) {
110 
111 		if (val != symbol_array[i].mask)
112 			continue;
113 
114 		trace_seq_puts(p, symbol_array[i].name);
115 		break;
116 	}
117 
118 	if (ret == (const char *)(trace_seq_buffer_ptr(p)))
119 		trace_seq_printf(p, "0x%lx", val);
120 
121 	trace_seq_putc(p, 0);
122 
123 	return ret;
124 }
125 EXPORT_SYMBOL(trace_print_symbols_seq);
126 
127 #if BITS_PER_LONG == 32
128 const char *
129 trace_print_flags_seq_u64(struct trace_seq *p, const char *delim,
130 		      unsigned long long flags,
131 		      const struct trace_print_flags_u64 *flag_array)
132 {
133 	unsigned long long mask;
134 	const char *str;
135 	const char *ret = trace_seq_buffer_ptr(p);
136 	int i, first = 1;
137 
138 	for (i = 0;  flag_array[i].name && flags; i++) {
139 
140 		mask = flag_array[i].mask;
141 		if ((flags & mask) != mask)
142 			continue;
143 
144 		str = flag_array[i].name;
145 		flags &= ~mask;
146 		if (!first && delim)
147 			trace_seq_puts(p, delim);
148 		else
149 			first = 0;
150 		trace_seq_puts(p, str);
151 	}
152 
153 	/* check for left over flags */
154 	if (flags) {
155 		if (!first && delim)
156 			trace_seq_puts(p, delim);
157 		trace_seq_printf(p, "0x%llx", flags);
158 	}
159 
160 	trace_seq_putc(p, 0);
161 
162 	return ret;
163 }
164 EXPORT_SYMBOL(trace_print_flags_seq_u64);
165 
166 const char *
167 trace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
168 			 const struct trace_print_flags_u64 *symbol_array)
169 {
170 	int i;
171 	const char *ret = trace_seq_buffer_ptr(p);
172 
173 	for (i = 0;  symbol_array[i].name; i++) {
174 
175 		if (val != symbol_array[i].mask)
176 			continue;
177 
178 		trace_seq_puts(p, symbol_array[i].name);
179 		break;
180 	}
181 
182 	if (ret == (const char *)(trace_seq_buffer_ptr(p)))
183 		trace_seq_printf(p, "0x%llx", val);
184 
185 	trace_seq_putc(p, 0);
186 
187 	return ret;
188 }
189 EXPORT_SYMBOL(trace_print_symbols_seq_u64);
190 #endif
191 
192 const char *
193 trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
194 			unsigned int bitmask_size)
195 {
196 	const char *ret = trace_seq_buffer_ptr(p);
197 
198 	trace_seq_bitmask(p, bitmask_ptr, bitmask_size * 8);
199 	trace_seq_putc(p, 0);
200 
201 	return ret;
202 }
203 EXPORT_SYMBOL_GPL(trace_print_bitmask_seq);
204 
205 /**
206  * trace_print_hex_seq - print buffer as hex sequence
207  * @p: trace seq struct to write to
208  * @buf: The buffer to print
209  * @buf_len: Length of @buf in bytes
210  * @concatenate: Print @buf as single hex string or with spacing
211  *
212  * Prints the passed buffer as a hex sequence either as a whole,
213  * single hex string if @concatenate is true or with spacing after
214  * each byte in case @concatenate is false.
215  */
216 const char *
217 trace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len,
218 		    bool concatenate)
219 {
220 	int i;
221 	const char *ret = trace_seq_buffer_ptr(p);
222 	const char *fmt = concatenate ? "%*phN" : "%*ph";
223 
224 	for (i = 0; i < buf_len; i += 16) {
225 		if (!concatenate && i != 0)
226 			trace_seq_putc(p, ' ');
227 		trace_seq_printf(p, fmt, min(buf_len - i, 16), &buf[i]);
228 	}
229 	trace_seq_putc(p, 0);
230 
231 	return ret;
232 }
233 EXPORT_SYMBOL(trace_print_hex_seq);
234 
235 const char *
236 trace_print_array_seq(struct trace_seq *p, const void *buf, int count,
237 		      size_t el_size)
238 {
239 	const char *ret = trace_seq_buffer_ptr(p);
240 	const char *prefix = "";
241 	void *ptr = (void *)buf;
242 	size_t buf_len = count * el_size;
243 
244 	trace_seq_putc(p, '{');
245 
246 	while (ptr < buf + buf_len) {
247 		switch (el_size) {
248 		case 1:
249 			trace_seq_printf(p, "%s0x%x", prefix,
250 					 *(u8 *)ptr);
251 			break;
252 		case 2:
253 			trace_seq_printf(p, "%s0x%x", prefix,
254 					 *(u16 *)ptr);
255 			break;
256 		case 4:
257 			trace_seq_printf(p, "%s0x%x", prefix,
258 					 *(u32 *)ptr);
259 			break;
260 		case 8:
261 			trace_seq_printf(p, "%s0x%llx", prefix,
262 					 *(u64 *)ptr);
263 			break;
264 		default:
265 			trace_seq_printf(p, "BAD SIZE:%zu 0x%x", el_size,
266 					 *(u8 *)ptr);
267 			el_size = 1;
268 		}
269 		prefix = ",";
270 		ptr += el_size;
271 	}
272 
273 	trace_seq_putc(p, '}');
274 	trace_seq_putc(p, 0);
275 
276 	return ret;
277 }
278 EXPORT_SYMBOL(trace_print_array_seq);
279 
280 const char *
281 trace_print_hex_dump_seq(struct trace_seq *p, const char *prefix_str,
282 			 int prefix_type, int rowsize, int groupsize,
283 			 const void *buf, size_t len, bool ascii)
284 {
285 	const char *ret = trace_seq_buffer_ptr(p);
286 
287 	trace_seq_putc(p, '\n');
288 	trace_seq_hex_dump(p, prefix_str, prefix_type,
289 			   rowsize, groupsize, buf, len, ascii);
290 	trace_seq_putc(p, 0);
291 	return ret;
292 }
293 EXPORT_SYMBOL(trace_print_hex_dump_seq);
294 
295 int trace_raw_output_prep(struct trace_iterator *iter,
296 			  struct trace_event *trace_event)
297 {
298 	struct trace_event_call *event;
299 	struct trace_seq *s = &iter->seq;
300 	struct trace_seq *p = &iter->tmp_seq;
301 	struct trace_entry *entry;
302 
303 	event = container_of(trace_event, struct trace_event_call, event);
304 	entry = iter->ent;
305 
306 	if (entry->type != event->event.type) {
307 		WARN_ON_ONCE(1);
308 		return TRACE_TYPE_UNHANDLED;
309 	}
310 
311 	trace_seq_init(p);
312 	trace_seq_printf(s, "%s: ", trace_event_name(event));
313 
314 	return trace_handle_return(s);
315 }
316 EXPORT_SYMBOL(trace_raw_output_prep);
317 
318 void trace_event_printf(struct trace_iterator *iter, const char *fmt, ...)
319 {
320 	va_list ap;
321 
322 	va_start(ap, fmt);
323 	trace_check_vprintf(iter, trace_event_format(iter, fmt), ap);
324 	va_end(ap);
325 }
326 EXPORT_SYMBOL(trace_event_printf);
327 
328 static __printf(3, 0)
329 int trace_output_raw(struct trace_iterator *iter, char *name,
330 		     char *fmt, va_list ap)
331 {
332 	struct trace_seq *s = &iter->seq;
333 
334 	trace_seq_printf(s, "%s: ", name);
335 	trace_seq_vprintf(s, trace_event_format(iter, fmt), ap);
336 
337 	return trace_handle_return(s);
338 }
339 
340 int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...)
341 {
342 	va_list ap;
343 	int ret;
344 
345 	va_start(ap, fmt);
346 	ret = trace_output_raw(iter, name, fmt, ap);
347 	va_end(ap);
348 
349 	return ret;
350 }
351 EXPORT_SYMBOL_GPL(trace_output_call);
352 
353 static inline const char *kretprobed(const char *name, unsigned long addr)
354 {
355 	if (is_kretprobe_trampoline(addr))
356 		return "[unknown/kretprobe'd]";
357 	return name;
358 }
359 
360 void
361 trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset)
362 {
363 #ifdef CONFIG_KALLSYMS
364 	char str[KSYM_SYMBOL_LEN];
365 	const char *name;
366 
367 	if (offset)
368 		sprint_symbol(str, address);
369 	else
370 		kallsyms_lookup(address, NULL, NULL, NULL, str);
371 	name = kretprobed(str, address);
372 
373 	if (name && strlen(name)) {
374 		trace_seq_puts(s, name);
375 		return;
376 	}
377 #endif
378 	trace_seq_printf(s, "0x%08lx", address);
379 }
380 
381 #ifndef CONFIG_64BIT
382 # define IP_FMT "%08lx"
383 #else
384 # define IP_FMT "%016lx"
385 #endif
386 
387 static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
388 			     unsigned long ip, unsigned long sym_flags)
389 {
390 	struct file *file = NULL;
391 	unsigned long vmstart = 0;
392 	int ret = 1;
393 
394 	if (s->full)
395 		return 0;
396 
397 	if (mm) {
398 		const struct vm_area_struct *vma;
399 
400 		mmap_read_lock(mm);
401 		vma = find_vma(mm, ip);
402 		if (vma) {
403 			file = vma->vm_file;
404 			vmstart = vma->vm_start;
405 		}
406 		if (file) {
407 			ret = trace_seq_path(s, file_user_path(file));
408 			if (ret)
409 				trace_seq_printf(s, "[+0x%lx]",
410 						 ip - vmstart);
411 		}
412 		mmap_read_unlock(mm);
413 	}
414 	if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
415 		trace_seq_printf(s, " <" IP_FMT ">", ip);
416 	return !trace_seq_has_overflowed(s);
417 }
418 
419 int
420 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
421 {
422 	if (!ip) {
423 		trace_seq_putc(s, '0');
424 		goto out;
425 	}
426 
427 	trace_seq_print_sym(s, ip, sym_flags & TRACE_ITER_SYM_OFFSET);
428 
429 	if (sym_flags & TRACE_ITER_SYM_ADDR)
430 		trace_seq_printf(s, " <" IP_FMT ">", ip);
431 
432  out:
433 	return !trace_seq_has_overflowed(s);
434 }
435 
436 /**
437  * trace_print_lat_fmt - print the irq, preempt and lockdep fields
438  * @s: trace seq struct to write to
439  * @entry: The trace entry field from the ring buffer
440  *
441  * Prints the generic fields of irqs off, in hard or softirq, preempt
442  * count.
443  */
444 int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
445 {
446 	char hardsoft_irq;
447 	char need_resched;
448 	char irqs_off;
449 	int hardirq;
450 	int softirq;
451 	int bh_off;
452 	int nmi;
453 
454 	nmi = entry->flags & TRACE_FLAG_NMI;
455 	hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
456 	softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
457 	bh_off = entry->flags & TRACE_FLAG_BH_OFF;
458 
459 	irqs_off =
460 		(entry->flags & TRACE_FLAG_IRQS_OFF && bh_off) ? 'D' :
461 		(entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
462 		bh_off ? 'b' :
463 		'.';
464 
465 	switch (entry->flags & (TRACE_FLAG_NEED_RESCHED |
466 				TRACE_FLAG_PREEMPT_RESCHED)) {
467 	case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
468 		need_resched = 'N';
469 		break;
470 	case TRACE_FLAG_NEED_RESCHED:
471 		need_resched = 'n';
472 		break;
473 	case TRACE_FLAG_PREEMPT_RESCHED:
474 		need_resched = 'p';
475 		break;
476 	default:
477 		need_resched = '.';
478 		break;
479 	}
480 
481 	hardsoft_irq =
482 		(nmi && hardirq)     ? 'Z' :
483 		nmi                  ? 'z' :
484 		(hardirq && softirq) ? 'H' :
485 		hardirq              ? 'h' :
486 		softirq              ? 's' :
487 		                       '.' ;
488 
489 	trace_seq_printf(s, "%c%c%c",
490 			 irqs_off, need_resched, hardsoft_irq);
491 
492 	if (entry->preempt_count & 0xf)
493 		trace_seq_printf(s, "%x", entry->preempt_count & 0xf);
494 	else
495 		trace_seq_putc(s, '.');
496 
497 	if (entry->preempt_count & 0xf0)
498 		trace_seq_printf(s, "%x", entry->preempt_count >> 4);
499 	else
500 		trace_seq_putc(s, '.');
501 
502 	return !trace_seq_has_overflowed(s);
503 }
504 
505 static int
506 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
507 {
508 	char comm[TASK_COMM_LEN];
509 
510 	trace_find_cmdline(entry->pid, comm);
511 
512 	trace_seq_printf(s, "%8.8s-%-7d %3d",
513 			 comm, entry->pid, cpu);
514 
515 	return trace_print_lat_fmt(s, entry);
516 }
517 
518 #undef MARK
519 #define MARK(v, s) {.val = v, .sym = s}
520 /* trace overhead mark */
521 static const struct trace_mark {
522 	unsigned long long	val; /* unit: nsec */
523 	char			sym;
524 } mark[] = {
525 	MARK(1000000000ULL	, '$'), /* 1 sec */
526 	MARK(100000000ULL	, '@'), /* 100 msec */
527 	MARK(10000000ULL	, '*'), /* 10 msec */
528 	MARK(1000000ULL		, '#'), /* 1000 usecs */
529 	MARK(100000ULL		, '!'), /* 100 usecs */
530 	MARK(10000ULL		, '+'), /* 10 usecs */
531 };
532 #undef MARK
533 
534 char trace_find_mark(unsigned long long d)
535 {
536 	int i;
537 	int size = ARRAY_SIZE(mark);
538 
539 	for (i = 0; i < size; i++) {
540 		if (d > mark[i].val)
541 			break;
542 	}
543 
544 	return (i == size) ? ' ' : mark[i].sym;
545 }
546 
547 static int
548 lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
549 {
550 	struct trace_array *tr = iter->tr;
551 	unsigned long verbose = tr->trace_flags & TRACE_ITER_VERBOSE;
552 	unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
553 	unsigned long long abs_ts = iter->ts - iter->array_buffer->time_start;
554 	unsigned long long rel_ts = next_ts - iter->ts;
555 	struct trace_seq *s = &iter->seq;
556 
557 	if (in_ns) {
558 		abs_ts = ns2usecs(abs_ts);
559 		rel_ts = ns2usecs(rel_ts);
560 	}
561 
562 	if (verbose && in_ns) {
563 		unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
564 		unsigned long abs_msec = (unsigned long)abs_ts;
565 		unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
566 		unsigned long rel_msec = (unsigned long)rel_ts;
567 
568 		trace_seq_printf(
569 			s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
570 			ns2usecs(iter->ts),
571 			abs_msec, abs_usec,
572 			rel_msec, rel_usec);
573 
574 	} else if (verbose && !in_ns) {
575 		trace_seq_printf(
576 			s, "[%016llx] %lld (+%lld): ",
577 			iter->ts, abs_ts, rel_ts);
578 
579 	} else if (!verbose && in_ns) {
580 		trace_seq_printf(
581 			s, " %4lldus%c: ",
582 			abs_ts,
583 			trace_find_mark(rel_ts * NSEC_PER_USEC));
584 
585 	} else { /* !verbose && !in_ns */
586 		trace_seq_printf(s, " %4lld: ", abs_ts);
587 	}
588 
589 	return !trace_seq_has_overflowed(s);
590 }
591 
592 static void trace_print_time(struct trace_seq *s, struct trace_iterator *iter,
593 			     unsigned long long ts)
594 {
595 	unsigned long secs, usec_rem;
596 	unsigned long long t;
597 
598 	if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
599 		t = ns2usecs(ts);
600 		usec_rem = do_div(t, USEC_PER_SEC);
601 		secs = (unsigned long)t;
602 		trace_seq_printf(s, " %5lu.%06lu", secs, usec_rem);
603 	} else
604 		trace_seq_printf(s, " %12llu", ts);
605 }
606 
607 int trace_print_context(struct trace_iterator *iter)
608 {
609 	struct trace_array *tr = iter->tr;
610 	struct trace_seq *s = &iter->seq;
611 	struct trace_entry *entry = iter->ent;
612 	char comm[TASK_COMM_LEN];
613 
614 	trace_find_cmdline(entry->pid, comm);
615 
616 	trace_seq_printf(s, "%16s-%-7d ", comm, entry->pid);
617 
618 	if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
619 		unsigned int tgid = trace_find_tgid(entry->pid);
620 
621 		if (!tgid)
622 			trace_seq_printf(s, "(-------) ");
623 		else
624 			trace_seq_printf(s, "(%7d) ", tgid);
625 	}
626 
627 	trace_seq_printf(s, "[%03d] ", iter->cpu);
628 
629 	if (tr->trace_flags & TRACE_ITER_IRQ_INFO)
630 		trace_print_lat_fmt(s, entry);
631 
632 	trace_print_time(s, iter, iter->ts);
633 	trace_seq_puts(s, ": ");
634 
635 	return !trace_seq_has_overflowed(s);
636 }
637 
638 int trace_print_lat_context(struct trace_iterator *iter)
639 {
640 	struct trace_entry *entry, *next_entry;
641 	struct trace_array *tr = iter->tr;
642 	struct trace_seq *s = &iter->seq;
643 	unsigned long verbose = (tr->trace_flags & TRACE_ITER_VERBOSE);
644 	u64 next_ts;
645 
646 	next_entry = trace_find_next_entry(iter, NULL, &next_ts);
647 	if (!next_entry)
648 		next_ts = iter->ts;
649 
650 	/* trace_find_next_entry() may change iter->ent */
651 	entry = iter->ent;
652 
653 	if (verbose) {
654 		char comm[TASK_COMM_LEN];
655 
656 		trace_find_cmdline(entry->pid, comm);
657 
658 		trace_seq_printf(
659 			s, "%16s %7d %3d %d %08x %08lx ",
660 			comm, entry->pid, iter->cpu, entry->flags,
661 			entry->preempt_count & 0xf, iter->idx);
662 	} else {
663 		lat_print_generic(s, entry, iter->cpu);
664 	}
665 
666 	lat_print_timestamp(iter, next_ts);
667 
668 	return !trace_seq_has_overflowed(s);
669 }
670 
671 /**
672  * ftrace_find_event - find a registered event
673  * @type: the type of event to look for
674  *
675  * Returns an event of type @type otherwise NULL
676  * Called with trace_event_read_lock() held.
677  */
678 struct trace_event *ftrace_find_event(int type)
679 {
680 	struct trace_event *event;
681 	unsigned key;
682 
683 	key = type & (EVENT_HASHSIZE - 1);
684 
685 	hlist_for_each_entry(event, &event_hash[key], node) {
686 		if (event->type == type)
687 			return event;
688 	}
689 
690 	return NULL;
691 }
692 
693 static DEFINE_IDA(trace_event_ida);
694 
695 static void free_trace_event_type(int type)
696 {
697 	if (type >= __TRACE_LAST_TYPE)
698 		ida_free(&trace_event_ida, type);
699 }
700 
701 static int alloc_trace_event_type(void)
702 {
703 	int next;
704 
705 	/* Skip static defined type numbers */
706 	next = ida_alloc_range(&trace_event_ida, __TRACE_LAST_TYPE,
707 			       TRACE_EVENT_TYPE_MAX, GFP_KERNEL);
708 	if (next < 0)
709 		return 0;
710 	return next;
711 }
712 
713 void trace_event_read_lock(void)
714 {
715 	down_read(&trace_event_sem);
716 }
717 
718 void trace_event_read_unlock(void)
719 {
720 	up_read(&trace_event_sem);
721 }
722 
723 /**
724  * register_trace_event - register output for an event type
725  * @event: the event type to register
726  *
727  * Event types are stored in a hash and this hash is used to
728  * find a way to print an event. If the @event->type is set
729  * then it will use that type, otherwise it will assign a
730  * type to use.
731  *
732  * If you assign your own type, please make sure it is added
733  * to the trace_type enum in trace.h, to avoid collisions
734  * with the dynamic types.
735  *
736  * Returns the event type number or zero on error.
737  */
738 int register_trace_event(struct trace_event *event)
739 {
740 	unsigned key;
741 	int ret = 0;
742 
743 	down_write(&trace_event_sem);
744 
745 	if (WARN_ON(!event))
746 		goto out;
747 
748 	if (WARN_ON(!event->funcs))
749 		goto out;
750 
751 	if (!event->type) {
752 		event->type = alloc_trace_event_type();
753 		if (!event->type)
754 			goto out;
755 	} else if (WARN(event->type > __TRACE_LAST_TYPE,
756 			"Need to add type to trace.h")) {
757 		goto out;
758 	} else {
759 		/* Is this event already used */
760 		if (ftrace_find_event(event->type))
761 			goto out;
762 	}
763 
764 	if (event->funcs->trace == NULL)
765 		event->funcs->trace = trace_nop_print;
766 	if (event->funcs->raw == NULL)
767 		event->funcs->raw = trace_nop_print;
768 	if (event->funcs->hex == NULL)
769 		event->funcs->hex = trace_nop_print;
770 	if (event->funcs->binary == NULL)
771 		event->funcs->binary = trace_nop_print;
772 
773 	key = event->type & (EVENT_HASHSIZE - 1);
774 
775 	hlist_add_head(&event->node, &event_hash[key]);
776 
777 	ret = event->type;
778  out:
779 	up_write(&trace_event_sem);
780 
781 	return ret;
782 }
783 EXPORT_SYMBOL_GPL(register_trace_event);
784 
785 /*
786  * Used by module code with the trace_event_sem held for write.
787  */
788 int __unregister_trace_event(struct trace_event *event)
789 {
790 	hlist_del(&event->node);
791 	free_trace_event_type(event->type);
792 	return 0;
793 }
794 
795 /**
796  * unregister_trace_event - remove a no longer used event
797  * @event: the event to remove
798  */
799 int unregister_trace_event(struct trace_event *event)
800 {
801 	down_write(&trace_event_sem);
802 	__unregister_trace_event(event);
803 	up_write(&trace_event_sem);
804 
805 	return 0;
806 }
807 EXPORT_SYMBOL_GPL(unregister_trace_event);
808 
809 /*
810  * Standard events
811  */
812 
813 static void print_array(struct trace_iterator *iter, void *pos,
814 			struct ftrace_event_field *field)
815 {
816 	int offset;
817 	int len;
818 	int i;
819 
820 	offset = *(int *)pos & 0xffff;
821 	len = *(int *)pos >> 16;
822 
823 	if (field)
824 		offset += field->offset + sizeof(int);
825 
826 	if (offset + len > iter->ent_size) {
827 		trace_seq_puts(&iter->seq, "<OVERFLOW>");
828 		return;
829 	}
830 
831 	pos = (void *)iter->ent + offset;
832 
833 	for (i = 0; i < len; i++, pos++) {
834 		if (i)
835 			trace_seq_putc(&iter->seq, ',');
836 		trace_seq_printf(&iter->seq, "%02x", *(unsigned char *)pos);
837 	}
838 }
839 
840 static void print_fields(struct trace_iterator *iter, struct trace_event_call *call,
841 			 struct list_head *head)
842 {
843 	struct ftrace_event_field *field;
844 	int offset;
845 	int len;
846 	int ret;
847 	void *pos;
848 
849 	list_for_each_entry_reverse(field, head, link) {
850 		trace_seq_printf(&iter->seq, " %s=", field->name);
851 		if (field->offset + field->size > iter->ent_size) {
852 			trace_seq_puts(&iter->seq, "<OVERFLOW>");
853 			continue;
854 		}
855 		pos = (void *)iter->ent + field->offset;
856 
857 		switch (field->filter_type) {
858 		case FILTER_COMM:
859 		case FILTER_STATIC_STRING:
860 			trace_seq_printf(&iter->seq, "%.*s", field->size, (char *)pos);
861 			break;
862 		case FILTER_RDYN_STRING:
863 		case FILTER_DYN_STRING:
864 			offset = *(int *)pos & 0xffff;
865 			len = *(int *)pos >> 16;
866 
867 			if (field->filter_type == FILTER_RDYN_STRING)
868 				offset += field->offset + sizeof(int);
869 
870 			if (offset + len > iter->ent_size) {
871 				trace_seq_puts(&iter->seq, "<OVERFLOW>");
872 				break;
873 			}
874 			pos = (void *)iter->ent + offset;
875 			trace_seq_printf(&iter->seq, "%.*s", len, (char *)pos);
876 			break;
877 		case FILTER_PTR_STRING:
878 			if (!iter->fmt_size)
879 				trace_iter_expand_format(iter);
880 			pos = *(void **)pos;
881 			ret = strncpy_from_kernel_nofault(iter->fmt, pos,
882 							  iter->fmt_size);
883 			if (ret < 0)
884 				trace_seq_printf(&iter->seq, "(0x%px)", pos);
885 			else
886 				trace_seq_printf(&iter->seq, "(0x%px:%s)",
887 						 pos, iter->fmt);
888 			break;
889 		case FILTER_TRACE_FN:
890 			pos = *(void **)pos;
891 			trace_seq_printf(&iter->seq, "%pS", pos);
892 			break;
893 		case FILTER_CPU:
894 		case FILTER_OTHER:
895 			switch (field->size) {
896 			case 1:
897 				if (isprint(*(char *)pos)) {
898 					trace_seq_printf(&iter->seq, "'%c'",
899 						 *(unsigned char *)pos);
900 				}
901 				trace_seq_printf(&iter->seq, "(%d)",
902 						 *(unsigned char *)pos);
903 				break;
904 			case 2:
905 				trace_seq_printf(&iter->seq, "0x%x (%d)",
906 						 *(unsigned short *)pos,
907 						 *(unsigned short *)pos);
908 				break;
909 			case 4:
910 				/* dynamic array info is 4 bytes */
911 				if (strstr(field->type, "__data_loc")) {
912 					print_array(iter, pos, NULL);
913 					break;
914 				}
915 
916 				if (strstr(field->type, "__rel_loc")) {
917 					print_array(iter, pos, field);
918 					break;
919 				}
920 
921 				trace_seq_printf(&iter->seq, "0x%x (%d)",
922 						 *(unsigned int *)pos,
923 						 *(unsigned int *)pos);
924 				break;
925 			case 8:
926 				trace_seq_printf(&iter->seq, "0x%llx (%lld)",
927 						 *(unsigned long long *)pos,
928 						 *(unsigned long long *)pos);
929 				break;
930 			default:
931 				trace_seq_puts(&iter->seq, "<INVALID-SIZE>");
932 				break;
933 			}
934 			break;
935 		default:
936 			trace_seq_puts(&iter->seq, "<INVALID-TYPE>");
937 		}
938 	}
939 	trace_seq_putc(&iter->seq, '\n');
940 }
941 
942 enum print_line_t print_event_fields(struct trace_iterator *iter,
943 				     struct trace_event *event)
944 {
945 	struct trace_event_call *call;
946 	struct list_head *head;
947 
948 	/* ftrace defined events have separate call structures */
949 	if (event->type <= __TRACE_LAST_TYPE) {
950 		bool found = false;
951 
952 		down_read(&trace_event_sem);
953 		list_for_each_entry(call, &ftrace_events, list) {
954 			if (call->event.type == event->type) {
955 				found = true;
956 				break;
957 			}
958 			/* No need to search all events */
959 			if (call->event.type > __TRACE_LAST_TYPE)
960 				break;
961 		}
962 		up_read(&trace_event_sem);
963 		if (!found) {
964 			trace_seq_printf(&iter->seq, "UNKNOWN TYPE %d\n", event->type);
965 			goto out;
966 		}
967 	} else {
968 		call = container_of(event, struct trace_event_call, event);
969 	}
970 	head = trace_get_fields(call);
971 
972 	trace_seq_printf(&iter->seq, "%s:", trace_event_name(call));
973 
974 	if (head && !list_empty(head))
975 		print_fields(iter, call, head);
976 	else
977 		trace_seq_puts(&iter->seq, "No fields found\n");
978 
979  out:
980 	return trace_handle_return(&iter->seq);
981 }
982 
983 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
984 				  struct trace_event *event)
985 {
986 	trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type);
987 
988 	return trace_handle_return(&iter->seq);
989 }
990 
991 static void print_fn_trace(struct trace_seq *s, unsigned long ip,
992 			   unsigned long parent_ip, long delta, int flags)
993 {
994 	ip += delta;
995 	parent_ip += delta;
996 
997 	seq_print_ip_sym(s, ip, flags);
998 
999 	if ((flags & TRACE_ITER_PRINT_PARENT) && parent_ip) {
1000 		trace_seq_puts(s, " <-");
1001 		seq_print_ip_sym(s, parent_ip, flags);
1002 	}
1003 }
1004 
1005 /* TRACE_FN */
1006 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
1007 					struct trace_event *event)
1008 {
1009 	struct ftrace_entry *field;
1010 	struct trace_seq *s = &iter->seq;
1011 
1012 	trace_assign_type(field, iter->ent);
1013 
1014 	print_fn_trace(s, field->ip, field->parent_ip, iter->tr->text_delta, flags);
1015 	trace_seq_putc(s, '\n');
1016 
1017 	return trace_handle_return(s);
1018 }
1019 
1020 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
1021 				      struct trace_event *event)
1022 {
1023 	struct ftrace_entry *field;
1024 
1025 	trace_assign_type(field, iter->ent);
1026 
1027 	trace_seq_printf(&iter->seq, "%lx %lx\n",
1028 			 field->ip,
1029 			 field->parent_ip);
1030 
1031 	return trace_handle_return(&iter->seq);
1032 }
1033 
1034 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
1035 				      struct trace_event *event)
1036 {
1037 	struct ftrace_entry *field;
1038 	struct trace_seq *s = &iter->seq;
1039 
1040 	trace_assign_type(field, iter->ent);
1041 
1042 	SEQ_PUT_HEX_FIELD(s, field->ip);
1043 	SEQ_PUT_HEX_FIELD(s, field->parent_ip);
1044 
1045 	return trace_handle_return(s);
1046 }
1047 
1048 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
1049 				      struct trace_event *event)
1050 {
1051 	struct ftrace_entry *field;
1052 	struct trace_seq *s = &iter->seq;
1053 
1054 	trace_assign_type(field, iter->ent);
1055 
1056 	SEQ_PUT_FIELD(s, field->ip);
1057 	SEQ_PUT_FIELD(s, field->parent_ip);
1058 
1059 	return trace_handle_return(s);
1060 }
1061 
1062 static struct trace_event_functions trace_fn_funcs = {
1063 	.trace		= trace_fn_trace,
1064 	.raw		= trace_fn_raw,
1065 	.hex		= trace_fn_hex,
1066 	.binary		= trace_fn_bin,
1067 };
1068 
1069 static struct trace_event trace_fn_event = {
1070 	.type		= TRACE_FN,
1071 	.funcs		= &trace_fn_funcs,
1072 };
1073 
1074 /* TRACE_CTX an TRACE_WAKE */
1075 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
1076 					     char *delim)
1077 {
1078 	struct ctx_switch_entry *field;
1079 	char comm[TASK_COMM_LEN];
1080 	int S, T;
1081 
1082 
1083 	trace_assign_type(field, iter->ent);
1084 
1085 	T = task_index_to_char(field->next_state);
1086 	S = task_index_to_char(field->prev_state);
1087 	trace_find_cmdline(field->next_pid, comm);
1088 	trace_seq_printf(&iter->seq,
1089 			 " %7d:%3d:%c %s [%03d] %7d:%3d:%c %s\n",
1090 			 field->prev_pid,
1091 			 field->prev_prio,
1092 			 S, delim,
1093 			 field->next_cpu,
1094 			 field->next_pid,
1095 			 field->next_prio,
1096 			 T, comm);
1097 
1098 	return trace_handle_return(&iter->seq);
1099 }
1100 
1101 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
1102 					 struct trace_event *event)
1103 {
1104 	return trace_ctxwake_print(iter, "==>");
1105 }
1106 
1107 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
1108 					  int flags, struct trace_event *event)
1109 {
1110 	return trace_ctxwake_print(iter, "  +");
1111 }
1112 
1113 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
1114 {
1115 	struct ctx_switch_entry *field;
1116 	int T;
1117 
1118 	trace_assign_type(field, iter->ent);
1119 
1120 	if (!S)
1121 		S = task_index_to_char(field->prev_state);
1122 	T = task_index_to_char(field->next_state);
1123 	trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
1124 			 field->prev_pid,
1125 			 field->prev_prio,
1126 			 S,
1127 			 field->next_cpu,
1128 			 field->next_pid,
1129 			 field->next_prio,
1130 			 T);
1131 
1132 	return trace_handle_return(&iter->seq);
1133 }
1134 
1135 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
1136 				       struct trace_event *event)
1137 {
1138 	return trace_ctxwake_raw(iter, 0);
1139 }
1140 
1141 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
1142 					struct trace_event *event)
1143 {
1144 	return trace_ctxwake_raw(iter, '+');
1145 }
1146 
1147 
1148 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
1149 {
1150 	struct ctx_switch_entry *field;
1151 	struct trace_seq *s = &iter->seq;
1152 	int T;
1153 
1154 	trace_assign_type(field, iter->ent);
1155 
1156 	if (!S)
1157 		S = task_index_to_char(field->prev_state);
1158 	T = task_index_to_char(field->next_state);
1159 
1160 	SEQ_PUT_HEX_FIELD(s, field->prev_pid);
1161 	SEQ_PUT_HEX_FIELD(s, field->prev_prio);
1162 	SEQ_PUT_HEX_FIELD(s, S);
1163 	SEQ_PUT_HEX_FIELD(s, field->next_cpu);
1164 	SEQ_PUT_HEX_FIELD(s, field->next_pid);
1165 	SEQ_PUT_HEX_FIELD(s, field->next_prio);
1166 	SEQ_PUT_HEX_FIELD(s, T);
1167 
1168 	return trace_handle_return(s);
1169 }
1170 
1171 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
1172 				       struct trace_event *event)
1173 {
1174 	return trace_ctxwake_hex(iter, 0);
1175 }
1176 
1177 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
1178 					struct trace_event *event)
1179 {
1180 	return trace_ctxwake_hex(iter, '+');
1181 }
1182 
1183 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1184 					   int flags, struct trace_event *event)
1185 {
1186 	struct ctx_switch_entry *field;
1187 	struct trace_seq *s = &iter->seq;
1188 
1189 	trace_assign_type(field, iter->ent);
1190 
1191 	SEQ_PUT_FIELD(s, field->prev_pid);
1192 	SEQ_PUT_FIELD(s, field->prev_prio);
1193 	SEQ_PUT_FIELD(s, field->prev_state);
1194 	SEQ_PUT_FIELD(s, field->next_cpu);
1195 	SEQ_PUT_FIELD(s, field->next_pid);
1196 	SEQ_PUT_FIELD(s, field->next_prio);
1197 	SEQ_PUT_FIELD(s, field->next_state);
1198 
1199 	return trace_handle_return(s);
1200 }
1201 
1202 static struct trace_event_functions trace_ctx_funcs = {
1203 	.trace		= trace_ctx_print,
1204 	.raw		= trace_ctx_raw,
1205 	.hex		= trace_ctx_hex,
1206 	.binary		= trace_ctxwake_bin,
1207 };
1208 
1209 static struct trace_event trace_ctx_event = {
1210 	.type		= TRACE_CTX,
1211 	.funcs		= &trace_ctx_funcs,
1212 };
1213 
1214 static struct trace_event_functions trace_wake_funcs = {
1215 	.trace		= trace_wake_print,
1216 	.raw		= trace_wake_raw,
1217 	.hex		= trace_wake_hex,
1218 	.binary		= trace_ctxwake_bin,
1219 };
1220 
1221 static struct trace_event trace_wake_event = {
1222 	.type		= TRACE_WAKE,
1223 	.funcs		= &trace_wake_funcs,
1224 };
1225 
1226 /* TRACE_STACK */
1227 
1228 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1229 					   int flags, struct trace_event *event)
1230 {
1231 	struct stack_entry *field;
1232 	struct trace_seq *s = &iter->seq;
1233 	unsigned long *p;
1234 	unsigned long *end;
1235 	long delta = iter->tr->text_delta;
1236 
1237 	trace_assign_type(field, iter->ent);
1238 	end = (unsigned long *)((long)iter->ent + iter->ent_size);
1239 
1240 	trace_seq_puts(s, "<stack trace>\n");
1241 
1242 	for (p = field->caller; p && p < end && *p != ULONG_MAX; p++) {
1243 
1244 		if (trace_seq_has_overflowed(s))
1245 			break;
1246 
1247 		trace_seq_puts(s, " => ");
1248 		if ((*p) == FTRACE_TRAMPOLINE_MARKER) {
1249 			trace_seq_puts(s, "[FTRACE TRAMPOLINE]\n");
1250 			continue;
1251 		}
1252 		seq_print_ip_sym(s, (*p) + delta, flags);
1253 		trace_seq_putc(s, '\n');
1254 	}
1255 
1256 	return trace_handle_return(s);
1257 }
1258 
1259 static struct trace_event_functions trace_stack_funcs = {
1260 	.trace		= trace_stack_print,
1261 };
1262 
1263 static struct trace_event trace_stack_event = {
1264 	.type		= TRACE_STACK,
1265 	.funcs		= &trace_stack_funcs,
1266 };
1267 
1268 /* TRACE_USER_STACK */
1269 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1270 						int flags, struct trace_event *event)
1271 {
1272 	struct trace_array *tr = iter->tr;
1273 	struct userstack_entry *field;
1274 	struct trace_seq *s = &iter->seq;
1275 	struct mm_struct *mm = NULL;
1276 	unsigned int i;
1277 
1278 	trace_assign_type(field, iter->ent);
1279 
1280 	trace_seq_puts(s, "<user stack trace>\n");
1281 
1282 	if (tr->trace_flags & TRACE_ITER_SYM_USEROBJ) {
1283 		struct task_struct *task;
1284 		/*
1285 		 * we do the lookup on the thread group leader,
1286 		 * since individual threads might have already quit!
1287 		 */
1288 		rcu_read_lock();
1289 		task = find_task_by_vpid(field->tgid);
1290 		if (task)
1291 			mm = get_task_mm(task);
1292 		rcu_read_unlock();
1293 	}
1294 
1295 	for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1296 		unsigned long ip = field->caller[i];
1297 
1298 		if (!ip || trace_seq_has_overflowed(s))
1299 			break;
1300 
1301 		trace_seq_puts(s, " => ");
1302 		seq_print_user_ip(s, mm, ip, flags);
1303 		trace_seq_putc(s, '\n');
1304 	}
1305 
1306 	if (mm)
1307 		mmput(mm);
1308 
1309 	return trace_handle_return(s);
1310 }
1311 
1312 static struct trace_event_functions trace_user_stack_funcs = {
1313 	.trace		= trace_user_stack_print,
1314 };
1315 
1316 static struct trace_event trace_user_stack_event = {
1317 	.type		= TRACE_USER_STACK,
1318 	.funcs		= &trace_user_stack_funcs,
1319 };
1320 
1321 /* TRACE_HWLAT */
1322 static enum print_line_t
1323 trace_hwlat_print(struct trace_iterator *iter, int flags,
1324 		  struct trace_event *event)
1325 {
1326 	struct trace_entry *entry = iter->ent;
1327 	struct trace_seq *s = &iter->seq;
1328 	struct hwlat_entry *field;
1329 
1330 	trace_assign_type(field, entry);
1331 
1332 	trace_seq_printf(s, "#%-5u inner/outer(us): %4llu/%-5llu ts:%lld.%09ld count:%d",
1333 			 field->seqnum,
1334 			 field->duration,
1335 			 field->outer_duration,
1336 			 (long long)field->timestamp.tv_sec,
1337 			 field->timestamp.tv_nsec, field->count);
1338 
1339 	if (field->nmi_count) {
1340 		/*
1341 		 * The generic sched_clock() is not NMI safe, thus
1342 		 * we only record the count and not the time.
1343 		 */
1344 		if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK))
1345 			trace_seq_printf(s, " nmi-total:%llu",
1346 					 field->nmi_total_ts);
1347 		trace_seq_printf(s, " nmi-count:%u",
1348 				 field->nmi_count);
1349 	}
1350 
1351 	trace_seq_putc(s, '\n');
1352 
1353 	return trace_handle_return(s);
1354 }
1355 
1356 static enum print_line_t
1357 trace_hwlat_raw(struct trace_iterator *iter, int flags,
1358 		struct trace_event *event)
1359 {
1360 	struct hwlat_entry *field;
1361 	struct trace_seq *s = &iter->seq;
1362 
1363 	trace_assign_type(field, iter->ent);
1364 
1365 	trace_seq_printf(s, "%llu %lld %lld %09ld %u\n",
1366 			 field->duration,
1367 			 field->outer_duration,
1368 			 (long long)field->timestamp.tv_sec,
1369 			 field->timestamp.tv_nsec,
1370 			 field->seqnum);
1371 
1372 	return trace_handle_return(s);
1373 }
1374 
1375 static struct trace_event_functions trace_hwlat_funcs = {
1376 	.trace		= trace_hwlat_print,
1377 	.raw		= trace_hwlat_raw,
1378 };
1379 
1380 static struct trace_event trace_hwlat_event = {
1381 	.type		= TRACE_HWLAT,
1382 	.funcs		= &trace_hwlat_funcs,
1383 };
1384 
1385 /* TRACE_OSNOISE */
1386 static enum print_line_t
1387 trace_osnoise_print(struct trace_iterator *iter, int flags,
1388 		    struct trace_event *event)
1389 {
1390 	struct trace_entry *entry = iter->ent;
1391 	struct trace_seq *s = &iter->seq;
1392 	struct osnoise_entry *field;
1393 	u64 ratio, ratio_dec;
1394 	u64 net_runtime;
1395 
1396 	trace_assign_type(field, entry);
1397 
1398 	/*
1399 	 * compute the available % of cpu time.
1400 	 */
1401 	net_runtime = field->runtime - field->noise;
1402 	ratio = net_runtime * 10000000;
1403 	do_div(ratio, field->runtime);
1404 	ratio_dec = do_div(ratio, 100000);
1405 
1406 	trace_seq_printf(s, "%llu %10llu %3llu.%05llu %7llu",
1407 			 field->runtime,
1408 			 field->noise,
1409 			 ratio, ratio_dec,
1410 			 field->max_sample);
1411 
1412 	trace_seq_printf(s, " %6u", field->hw_count);
1413 	trace_seq_printf(s, " %6u", field->nmi_count);
1414 	trace_seq_printf(s, " %6u", field->irq_count);
1415 	trace_seq_printf(s, " %6u", field->softirq_count);
1416 	trace_seq_printf(s, " %6u", field->thread_count);
1417 
1418 	trace_seq_putc(s, '\n');
1419 
1420 	return trace_handle_return(s);
1421 }
1422 
1423 static enum print_line_t
1424 trace_osnoise_raw(struct trace_iterator *iter, int flags,
1425 		  struct trace_event *event)
1426 {
1427 	struct osnoise_entry *field;
1428 	struct trace_seq *s = &iter->seq;
1429 
1430 	trace_assign_type(field, iter->ent);
1431 
1432 	trace_seq_printf(s, "%lld %llu %llu %u %u %u %u %u\n",
1433 			 field->runtime,
1434 			 field->noise,
1435 			 field->max_sample,
1436 			 field->hw_count,
1437 			 field->nmi_count,
1438 			 field->irq_count,
1439 			 field->softirq_count,
1440 			 field->thread_count);
1441 
1442 	return trace_handle_return(s);
1443 }
1444 
1445 static struct trace_event_functions trace_osnoise_funcs = {
1446 	.trace		= trace_osnoise_print,
1447 	.raw		= trace_osnoise_raw,
1448 };
1449 
1450 static struct trace_event trace_osnoise_event = {
1451 	.type		= TRACE_OSNOISE,
1452 	.funcs		= &trace_osnoise_funcs,
1453 };
1454 
1455 /* TRACE_TIMERLAT */
1456 
1457 static char *timerlat_lat_context[] = {"irq", "thread", "user-ret"};
1458 static enum print_line_t
1459 trace_timerlat_print(struct trace_iterator *iter, int flags,
1460 		     struct trace_event *event)
1461 {
1462 	struct trace_entry *entry = iter->ent;
1463 	struct trace_seq *s = &iter->seq;
1464 	struct timerlat_entry *field;
1465 
1466 	trace_assign_type(field, entry);
1467 
1468 	trace_seq_printf(s, "#%-5u context %6s timer_latency %9llu ns\n",
1469 			 field->seqnum,
1470 			 timerlat_lat_context[field->context],
1471 			 field->timer_latency);
1472 
1473 	return trace_handle_return(s);
1474 }
1475 
1476 static enum print_line_t
1477 trace_timerlat_raw(struct trace_iterator *iter, int flags,
1478 		   struct trace_event *event)
1479 {
1480 	struct timerlat_entry *field;
1481 	struct trace_seq *s = &iter->seq;
1482 
1483 	trace_assign_type(field, iter->ent);
1484 
1485 	trace_seq_printf(s, "%u %d %llu\n",
1486 			 field->seqnum,
1487 			 field->context,
1488 			 field->timer_latency);
1489 
1490 	return trace_handle_return(s);
1491 }
1492 
1493 static struct trace_event_functions trace_timerlat_funcs = {
1494 	.trace		= trace_timerlat_print,
1495 	.raw		= trace_timerlat_raw,
1496 };
1497 
1498 static struct trace_event trace_timerlat_event = {
1499 	.type		= TRACE_TIMERLAT,
1500 	.funcs		= &trace_timerlat_funcs,
1501 };
1502 
1503 /* TRACE_BPUTS */
1504 static enum print_line_t
1505 trace_bputs_print(struct trace_iterator *iter, int flags,
1506 		   struct trace_event *event)
1507 {
1508 	struct trace_entry *entry = iter->ent;
1509 	struct trace_seq *s = &iter->seq;
1510 	struct bputs_entry *field;
1511 
1512 	trace_assign_type(field, entry);
1513 
1514 	seq_print_ip_sym(s, field->ip, flags);
1515 	trace_seq_puts(s, ": ");
1516 	trace_seq_puts(s, field->str);
1517 
1518 	return trace_handle_return(s);
1519 }
1520 
1521 
1522 static enum print_line_t
1523 trace_bputs_raw(struct trace_iterator *iter, int flags,
1524 		struct trace_event *event)
1525 {
1526 	struct bputs_entry *field;
1527 	struct trace_seq *s = &iter->seq;
1528 
1529 	trace_assign_type(field, iter->ent);
1530 
1531 	trace_seq_printf(s, ": %lx : ", field->ip);
1532 	trace_seq_puts(s, field->str);
1533 
1534 	return trace_handle_return(s);
1535 }
1536 
1537 static struct trace_event_functions trace_bputs_funcs = {
1538 	.trace		= trace_bputs_print,
1539 	.raw		= trace_bputs_raw,
1540 };
1541 
1542 static struct trace_event trace_bputs_event = {
1543 	.type		= TRACE_BPUTS,
1544 	.funcs		= &trace_bputs_funcs,
1545 };
1546 
1547 /* TRACE_BPRINT */
1548 static enum print_line_t
1549 trace_bprint_print(struct trace_iterator *iter, int flags,
1550 		   struct trace_event *event)
1551 {
1552 	struct trace_entry *entry = iter->ent;
1553 	struct trace_seq *s = &iter->seq;
1554 	struct bprint_entry *field;
1555 
1556 	trace_assign_type(field, entry);
1557 
1558 	seq_print_ip_sym(s, field->ip, flags);
1559 	trace_seq_puts(s, ": ");
1560 	trace_seq_bprintf(s, field->fmt, field->buf);
1561 
1562 	return trace_handle_return(s);
1563 }
1564 
1565 
1566 static enum print_line_t
1567 trace_bprint_raw(struct trace_iterator *iter, int flags,
1568 		 struct trace_event *event)
1569 {
1570 	struct bprint_entry *field;
1571 	struct trace_seq *s = &iter->seq;
1572 
1573 	trace_assign_type(field, iter->ent);
1574 
1575 	trace_seq_printf(s, ": %lx : ", field->ip);
1576 	trace_seq_bprintf(s, field->fmt, field->buf);
1577 
1578 	return trace_handle_return(s);
1579 }
1580 
1581 static struct trace_event_functions trace_bprint_funcs = {
1582 	.trace		= trace_bprint_print,
1583 	.raw		= trace_bprint_raw,
1584 };
1585 
1586 static struct trace_event trace_bprint_event = {
1587 	.type		= TRACE_BPRINT,
1588 	.funcs		= &trace_bprint_funcs,
1589 };
1590 
1591 /* TRACE_PRINT */
1592 static enum print_line_t trace_print_print(struct trace_iterator *iter,
1593 					   int flags, struct trace_event *event)
1594 {
1595 	struct print_entry *field;
1596 	struct trace_seq *s = &iter->seq;
1597 	unsigned long ip;
1598 
1599 	trace_assign_type(field, iter->ent);
1600 
1601 	ip = field->ip + iter->tr->text_delta;
1602 
1603 	seq_print_ip_sym(s, ip, flags);
1604 	trace_seq_printf(s, ": %s", field->buf);
1605 
1606 	return trace_handle_return(s);
1607 }
1608 
1609 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
1610 					 struct trace_event *event)
1611 {
1612 	struct print_entry *field;
1613 
1614 	trace_assign_type(field, iter->ent);
1615 
1616 	trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf);
1617 
1618 	return trace_handle_return(&iter->seq);
1619 }
1620 
1621 static struct trace_event_functions trace_print_funcs = {
1622 	.trace		= trace_print_print,
1623 	.raw		= trace_print_raw,
1624 };
1625 
1626 static struct trace_event trace_print_event = {
1627 	.type	 	= TRACE_PRINT,
1628 	.funcs		= &trace_print_funcs,
1629 };
1630 
1631 static enum print_line_t trace_raw_data(struct trace_iterator *iter, int flags,
1632 					 struct trace_event *event)
1633 {
1634 	struct raw_data_entry *field;
1635 	int i;
1636 
1637 	trace_assign_type(field, iter->ent);
1638 
1639 	trace_seq_printf(&iter->seq, "# %x buf:", field->id);
1640 
1641 	for (i = 0; i < iter->ent_size - offsetof(struct raw_data_entry, buf); i++)
1642 		trace_seq_printf(&iter->seq, " %02x",
1643 				 (unsigned char)field->buf[i]);
1644 
1645 	trace_seq_putc(&iter->seq, '\n');
1646 
1647 	return trace_handle_return(&iter->seq);
1648 }
1649 
1650 static struct trace_event_functions trace_raw_data_funcs = {
1651 	.trace		= trace_raw_data,
1652 	.raw		= trace_raw_data,
1653 };
1654 
1655 static struct trace_event trace_raw_data_event = {
1656 	.type	 	= TRACE_RAW_DATA,
1657 	.funcs		= &trace_raw_data_funcs,
1658 };
1659 
1660 static enum print_line_t
1661 trace_func_repeats_raw(struct trace_iterator *iter, int flags,
1662 			 struct trace_event *event)
1663 {
1664 	struct func_repeats_entry *field;
1665 	struct trace_seq *s = &iter->seq;
1666 
1667 	trace_assign_type(field, iter->ent);
1668 
1669 	trace_seq_printf(s, "%lu %lu %u %llu\n",
1670 			 field->ip,
1671 			 field->parent_ip,
1672 			 field->count,
1673 			 FUNC_REPEATS_GET_DELTA_TS(field));
1674 
1675 	return trace_handle_return(s);
1676 }
1677 
1678 static enum print_line_t
1679 trace_func_repeats_print(struct trace_iterator *iter, int flags,
1680 			 struct trace_event *event)
1681 {
1682 	struct func_repeats_entry *field;
1683 	struct trace_seq *s = &iter->seq;
1684 
1685 	trace_assign_type(field, iter->ent);
1686 
1687 	print_fn_trace(s, field->ip, field->parent_ip, iter->tr->text_delta, flags);
1688 	trace_seq_printf(s, " (repeats: %u, last_ts:", field->count);
1689 	trace_print_time(s, iter,
1690 			 iter->ts - FUNC_REPEATS_GET_DELTA_TS(field));
1691 	trace_seq_puts(s, ")\n");
1692 
1693 	return trace_handle_return(s);
1694 }
1695 
1696 static struct trace_event_functions trace_func_repeats_funcs = {
1697 	.trace		= trace_func_repeats_print,
1698 	.raw		= trace_func_repeats_raw,
1699 };
1700 
1701 static struct trace_event trace_func_repeats_event = {
1702 	.type	 	= TRACE_FUNC_REPEATS,
1703 	.funcs		= &trace_func_repeats_funcs,
1704 };
1705 
1706 static struct trace_event *events[] __initdata = {
1707 	&trace_fn_event,
1708 	&trace_ctx_event,
1709 	&trace_wake_event,
1710 	&trace_stack_event,
1711 	&trace_user_stack_event,
1712 	&trace_bputs_event,
1713 	&trace_bprint_event,
1714 	&trace_print_event,
1715 	&trace_hwlat_event,
1716 	&trace_osnoise_event,
1717 	&trace_timerlat_event,
1718 	&trace_raw_data_event,
1719 	&trace_func_repeats_event,
1720 	NULL
1721 };
1722 
1723 __init int init_events(void)
1724 {
1725 	struct trace_event *event;
1726 	int i, ret;
1727 
1728 	for (i = 0; events[i]; i++) {
1729 		event = events[i];
1730 		ret = register_trace_event(event);
1731 		WARN_ONCE(!ret, "event %d failed to register", event->type);
1732 	}
1733 
1734 	return 0;
1735 }
1736