xref: /linux/kernel/trace/trace_output.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
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 | TRACE_FLAG_NEED_RESCHED_LAZY |
466 				TRACE_FLAG_PREEMPT_RESCHED)) {
467 	case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_NEED_RESCHED_LAZY | TRACE_FLAG_PREEMPT_RESCHED:
468 		need_resched = 'B';
469 		break;
470 	case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
471 		need_resched = 'N';
472 		break;
473 	case TRACE_FLAG_NEED_RESCHED_LAZY | TRACE_FLAG_PREEMPT_RESCHED:
474 		need_resched = 'L';
475 		break;
476 	case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_NEED_RESCHED_LAZY:
477 		need_resched = 'b';
478 		break;
479 	case TRACE_FLAG_NEED_RESCHED:
480 		need_resched = 'n';
481 		break;
482 	case TRACE_FLAG_PREEMPT_RESCHED:
483 		need_resched = 'p';
484 		break;
485 	case TRACE_FLAG_NEED_RESCHED_LAZY:
486 		need_resched = 'l';
487 		break;
488 	default:
489 		need_resched = '.';
490 		break;
491 	}
492 
493 	hardsoft_irq =
494 		(nmi && hardirq)     ? 'Z' :
495 		nmi                  ? 'z' :
496 		(hardirq && softirq) ? 'H' :
497 		hardirq              ? 'h' :
498 		softirq              ? 's' :
499 		                       '.' ;
500 
501 	trace_seq_printf(s, "%c%c%c",
502 			 irqs_off, need_resched, hardsoft_irq);
503 
504 	if (entry->preempt_count & 0xf)
505 		trace_seq_printf(s, "%x", entry->preempt_count & 0xf);
506 	else
507 		trace_seq_putc(s, '.');
508 
509 	if (entry->preempt_count & 0xf0)
510 		trace_seq_printf(s, "%x", entry->preempt_count >> 4);
511 	else
512 		trace_seq_putc(s, '.');
513 
514 	return !trace_seq_has_overflowed(s);
515 }
516 
517 static int
518 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
519 {
520 	char comm[TASK_COMM_LEN];
521 
522 	trace_find_cmdline(entry->pid, comm);
523 
524 	trace_seq_printf(s, "%8.8s-%-7d %3d",
525 			 comm, entry->pid, cpu);
526 
527 	return trace_print_lat_fmt(s, entry);
528 }
529 
530 #undef MARK
531 #define MARK(v, s) {.val = v, .sym = s}
532 /* trace overhead mark */
533 static const struct trace_mark {
534 	unsigned long long	val; /* unit: nsec */
535 	char			sym;
536 } mark[] = {
537 	MARK(1000000000ULL	, '$'), /* 1 sec */
538 	MARK(100000000ULL	, '@'), /* 100 msec */
539 	MARK(10000000ULL	, '*'), /* 10 msec */
540 	MARK(1000000ULL		, '#'), /* 1000 usecs */
541 	MARK(100000ULL		, '!'), /* 100 usecs */
542 	MARK(10000ULL		, '+'), /* 10 usecs */
543 };
544 #undef MARK
545 
546 char trace_find_mark(unsigned long long d)
547 {
548 	int i;
549 	int size = ARRAY_SIZE(mark);
550 
551 	for (i = 0; i < size; i++) {
552 		if (d > mark[i].val)
553 			break;
554 	}
555 
556 	return (i == size) ? ' ' : mark[i].sym;
557 }
558 
559 static int
560 lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
561 {
562 	struct trace_array *tr = iter->tr;
563 	unsigned long verbose = tr->trace_flags & TRACE_ITER_VERBOSE;
564 	unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
565 	unsigned long long abs_ts = iter->ts - iter->array_buffer->time_start;
566 	unsigned long long rel_ts = next_ts - iter->ts;
567 	struct trace_seq *s = &iter->seq;
568 
569 	if (in_ns) {
570 		abs_ts = ns2usecs(abs_ts);
571 		rel_ts = ns2usecs(rel_ts);
572 	}
573 
574 	if (verbose && in_ns) {
575 		unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
576 		unsigned long abs_msec = (unsigned long)abs_ts;
577 		unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
578 		unsigned long rel_msec = (unsigned long)rel_ts;
579 
580 		trace_seq_printf(
581 			s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
582 			ns2usecs(iter->ts),
583 			abs_msec, abs_usec,
584 			rel_msec, rel_usec);
585 
586 	} else if (verbose && !in_ns) {
587 		trace_seq_printf(
588 			s, "[%016llx] %lld (+%lld): ",
589 			iter->ts, abs_ts, rel_ts);
590 
591 	} else if (!verbose && in_ns) {
592 		trace_seq_printf(
593 			s, " %4lldus%c: ",
594 			abs_ts,
595 			trace_find_mark(rel_ts * NSEC_PER_USEC));
596 
597 	} else { /* !verbose && !in_ns */
598 		trace_seq_printf(s, " %4lld: ", abs_ts);
599 	}
600 
601 	return !trace_seq_has_overflowed(s);
602 }
603 
604 static void trace_print_time(struct trace_seq *s, struct trace_iterator *iter,
605 			     unsigned long long ts)
606 {
607 	unsigned long secs, usec_rem;
608 	unsigned long long t;
609 
610 	if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
611 		t = ns2usecs(ts);
612 		usec_rem = do_div(t, USEC_PER_SEC);
613 		secs = (unsigned long)t;
614 		trace_seq_printf(s, " %5lu.%06lu", secs, usec_rem);
615 	} else
616 		trace_seq_printf(s, " %12llu", ts);
617 }
618 
619 int trace_print_context(struct trace_iterator *iter)
620 {
621 	struct trace_array *tr = iter->tr;
622 	struct trace_seq *s = &iter->seq;
623 	struct trace_entry *entry = iter->ent;
624 	char comm[TASK_COMM_LEN];
625 
626 	trace_find_cmdline(entry->pid, comm);
627 
628 	trace_seq_printf(s, "%16s-%-7d ", comm, entry->pid);
629 
630 	if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
631 		unsigned int tgid = trace_find_tgid(entry->pid);
632 
633 		if (!tgid)
634 			trace_seq_printf(s, "(-------) ");
635 		else
636 			trace_seq_printf(s, "(%7d) ", tgid);
637 	}
638 
639 	trace_seq_printf(s, "[%03d] ", iter->cpu);
640 
641 	if (tr->trace_flags & TRACE_ITER_IRQ_INFO)
642 		trace_print_lat_fmt(s, entry);
643 
644 	trace_print_time(s, iter, iter->ts);
645 	trace_seq_puts(s, ": ");
646 
647 	return !trace_seq_has_overflowed(s);
648 }
649 
650 int trace_print_lat_context(struct trace_iterator *iter)
651 {
652 	struct trace_entry *entry, *next_entry;
653 	struct trace_array *tr = iter->tr;
654 	struct trace_seq *s = &iter->seq;
655 	unsigned long verbose = (tr->trace_flags & TRACE_ITER_VERBOSE);
656 	u64 next_ts;
657 
658 	next_entry = trace_find_next_entry(iter, NULL, &next_ts);
659 	if (!next_entry)
660 		next_ts = iter->ts;
661 
662 	/* trace_find_next_entry() may change iter->ent */
663 	entry = iter->ent;
664 
665 	if (verbose) {
666 		char comm[TASK_COMM_LEN];
667 
668 		trace_find_cmdline(entry->pid, comm);
669 
670 		trace_seq_printf(
671 			s, "%16s %7d %3d %d %08x %08lx ",
672 			comm, entry->pid, iter->cpu, entry->flags,
673 			entry->preempt_count & 0xf, iter->idx);
674 	} else {
675 		lat_print_generic(s, entry, iter->cpu);
676 	}
677 
678 	lat_print_timestamp(iter, next_ts);
679 
680 	return !trace_seq_has_overflowed(s);
681 }
682 
683 /**
684  * ftrace_find_event - find a registered event
685  * @type: the type of event to look for
686  *
687  * Returns an event of type @type otherwise NULL
688  * Called with trace_event_read_lock() held.
689  */
690 struct trace_event *ftrace_find_event(int type)
691 {
692 	struct trace_event *event;
693 	unsigned key;
694 
695 	key = type & (EVENT_HASHSIZE - 1);
696 
697 	hlist_for_each_entry(event, &event_hash[key], node) {
698 		if (event->type == type)
699 			return event;
700 	}
701 
702 	return NULL;
703 }
704 
705 static DEFINE_IDA(trace_event_ida);
706 
707 static void free_trace_event_type(int type)
708 {
709 	if (type >= __TRACE_LAST_TYPE)
710 		ida_free(&trace_event_ida, type);
711 }
712 
713 static int alloc_trace_event_type(void)
714 {
715 	int next;
716 
717 	/* Skip static defined type numbers */
718 	next = ida_alloc_range(&trace_event_ida, __TRACE_LAST_TYPE,
719 			       TRACE_EVENT_TYPE_MAX, GFP_KERNEL);
720 	if (next < 0)
721 		return 0;
722 	return next;
723 }
724 
725 void trace_event_read_lock(void)
726 {
727 	down_read(&trace_event_sem);
728 }
729 
730 void trace_event_read_unlock(void)
731 {
732 	up_read(&trace_event_sem);
733 }
734 
735 /**
736  * register_trace_event - register output for an event type
737  * @event: the event type to register
738  *
739  * Event types are stored in a hash and this hash is used to
740  * find a way to print an event. If the @event->type is set
741  * then it will use that type, otherwise it will assign a
742  * type to use.
743  *
744  * If you assign your own type, please make sure it is added
745  * to the trace_type enum in trace.h, to avoid collisions
746  * with the dynamic types.
747  *
748  * Returns the event type number or zero on error.
749  */
750 int register_trace_event(struct trace_event *event)
751 {
752 	unsigned key;
753 	int ret = 0;
754 
755 	down_write(&trace_event_sem);
756 
757 	if (WARN_ON(!event))
758 		goto out;
759 
760 	if (WARN_ON(!event->funcs))
761 		goto out;
762 
763 	if (!event->type) {
764 		event->type = alloc_trace_event_type();
765 		if (!event->type)
766 			goto out;
767 	} else if (WARN(event->type > __TRACE_LAST_TYPE,
768 			"Need to add type to trace.h")) {
769 		goto out;
770 	} else {
771 		/* Is this event already used */
772 		if (ftrace_find_event(event->type))
773 			goto out;
774 	}
775 
776 	if (event->funcs->trace == NULL)
777 		event->funcs->trace = trace_nop_print;
778 	if (event->funcs->raw == NULL)
779 		event->funcs->raw = trace_nop_print;
780 	if (event->funcs->hex == NULL)
781 		event->funcs->hex = trace_nop_print;
782 	if (event->funcs->binary == NULL)
783 		event->funcs->binary = trace_nop_print;
784 
785 	key = event->type & (EVENT_HASHSIZE - 1);
786 
787 	hlist_add_head(&event->node, &event_hash[key]);
788 
789 	ret = event->type;
790  out:
791 	up_write(&trace_event_sem);
792 
793 	return ret;
794 }
795 EXPORT_SYMBOL_GPL(register_trace_event);
796 
797 /*
798  * Used by module code with the trace_event_sem held for write.
799  */
800 int __unregister_trace_event(struct trace_event *event)
801 {
802 	hlist_del(&event->node);
803 	free_trace_event_type(event->type);
804 	return 0;
805 }
806 
807 /**
808  * unregister_trace_event - remove a no longer used event
809  * @event: the event to remove
810  */
811 int unregister_trace_event(struct trace_event *event)
812 {
813 	down_write(&trace_event_sem);
814 	__unregister_trace_event(event);
815 	up_write(&trace_event_sem);
816 
817 	return 0;
818 }
819 EXPORT_SYMBOL_GPL(unregister_trace_event);
820 
821 /*
822  * Standard events
823  */
824 
825 static void print_array(struct trace_iterator *iter, void *pos,
826 			struct ftrace_event_field *field)
827 {
828 	int offset;
829 	int len;
830 	int i;
831 
832 	offset = *(int *)pos & 0xffff;
833 	len = *(int *)pos >> 16;
834 
835 	if (field)
836 		offset += field->offset + sizeof(int);
837 
838 	if (offset + len > iter->ent_size) {
839 		trace_seq_puts(&iter->seq, "<OVERFLOW>");
840 		return;
841 	}
842 
843 	pos = (void *)iter->ent + offset;
844 
845 	for (i = 0; i < len; i++, pos++) {
846 		if (i)
847 			trace_seq_putc(&iter->seq, ',');
848 		trace_seq_printf(&iter->seq, "%02x", *(unsigned char *)pos);
849 	}
850 }
851 
852 static void print_fields(struct trace_iterator *iter, struct trace_event_call *call,
853 			 struct list_head *head)
854 {
855 	struct ftrace_event_field *field;
856 	int offset;
857 	int len;
858 	int ret;
859 	void *pos;
860 
861 	list_for_each_entry_reverse(field, head, link) {
862 		trace_seq_printf(&iter->seq, " %s=", field->name);
863 		if (field->offset + field->size > iter->ent_size) {
864 			trace_seq_puts(&iter->seq, "<OVERFLOW>");
865 			continue;
866 		}
867 		pos = (void *)iter->ent + field->offset;
868 
869 		switch (field->filter_type) {
870 		case FILTER_COMM:
871 		case FILTER_STATIC_STRING:
872 			trace_seq_printf(&iter->seq, "%.*s", field->size, (char *)pos);
873 			break;
874 		case FILTER_RDYN_STRING:
875 		case FILTER_DYN_STRING:
876 			offset = *(int *)pos & 0xffff;
877 			len = *(int *)pos >> 16;
878 
879 			if (field->filter_type == FILTER_RDYN_STRING)
880 				offset += field->offset + sizeof(int);
881 
882 			if (offset + len > iter->ent_size) {
883 				trace_seq_puts(&iter->seq, "<OVERFLOW>");
884 				break;
885 			}
886 			pos = (void *)iter->ent + offset;
887 			trace_seq_printf(&iter->seq, "%.*s", len, (char *)pos);
888 			break;
889 		case FILTER_PTR_STRING:
890 			if (!iter->fmt_size)
891 				trace_iter_expand_format(iter);
892 			pos = *(void **)pos;
893 			ret = strncpy_from_kernel_nofault(iter->fmt, pos,
894 							  iter->fmt_size);
895 			if (ret < 0)
896 				trace_seq_printf(&iter->seq, "(0x%px)", pos);
897 			else
898 				trace_seq_printf(&iter->seq, "(0x%px:%s)",
899 						 pos, iter->fmt);
900 			break;
901 		case FILTER_TRACE_FN:
902 			pos = *(void **)pos;
903 			trace_seq_printf(&iter->seq, "%pS", pos);
904 			break;
905 		case FILTER_CPU:
906 		case FILTER_OTHER:
907 			switch (field->size) {
908 			case 1:
909 				if (isprint(*(char *)pos)) {
910 					trace_seq_printf(&iter->seq, "'%c'",
911 						 *(unsigned char *)pos);
912 				}
913 				trace_seq_printf(&iter->seq, "(%d)",
914 						 *(unsigned char *)pos);
915 				break;
916 			case 2:
917 				trace_seq_printf(&iter->seq, "0x%x (%d)",
918 						 *(unsigned short *)pos,
919 						 *(unsigned short *)pos);
920 				break;
921 			case 4:
922 				/* dynamic array info is 4 bytes */
923 				if (strstr(field->type, "__data_loc")) {
924 					print_array(iter, pos, NULL);
925 					break;
926 				}
927 
928 				if (strstr(field->type, "__rel_loc")) {
929 					print_array(iter, pos, field);
930 					break;
931 				}
932 
933 				trace_seq_printf(&iter->seq, "0x%x (%d)",
934 						 *(unsigned int *)pos,
935 						 *(unsigned int *)pos);
936 				break;
937 			case 8:
938 				trace_seq_printf(&iter->seq, "0x%llx (%lld)",
939 						 *(unsigned long long *)pos,
940 						 *(unsigned long long *)pos);
941 				break;
942 			default:
943 				trace_seq_puts(&iter->seq, "<INVALID-SIZE>");
944 				break;
945 			}
946 			break;
947 		default:
948 			trace_seq_puts(&iter->seq, "<INVALID-TYPE>");
949 		}
950 	}
951 	trace_seq_putc(&iter->seq, '\n');
952 }
953 
954 enum print_line_t print_event_fields(struct trace_iterator *iter,
955 				     struct trace_event *event)
956 {
957 	struct trace_event_call *call;
958 	struct list_head *head;
959 
960 	/* ftrace defined events have separate call structures */
961 	if (event->type <= __TRACE_LAST_TYPE) {
962 		bool found = false;
963 
964 		down_read(&trace_event_sem);
965 		list_for_each_entry(call, &ftrace_events, list) {
966 			if (call->event.type == event->type) {
967 				found = true;
968 				break;
969 			}
970 			/* No need to search all events */
971 			if (call->event.type > __TRACE_LAST_TYPE)
972 				break;
973 		}
974 		up_read(&trace_event_sem);
975 		if (!found) {
976 			trace_seq_printf(&iter->seq, "UNKNOWN TYPE %d\n", event->type);
977 			goto out;
978 		}
979 	} else {
980 		call = container_of(event, struct trace_event_call, event);
981 	}
982 	head = trace_get_fields(call);
983 
984 	trace_seq_printf(&iter->seq, "%s:", trace_event_name(call));
985 
986 	if (head && !list_empty(head))
987 		print_fields(iter, call, head);
988 	else
989 		trace_seq_puts(&iter->seq, "No fields found\n");
990 
991  out:
992 	return trace_handle_return(&iter->seq);
993 }
994 
995 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
996 				  struct trace_event *event)
997 {
998 	trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type);
999 
1000 	return trace_handle_return(&iter->seq);
1001 }
1002 
1003 static void print_fn_trace(struct trace_seq *s, unsigned long ip,
1004 			   unsigned long parent_ip, long delta, int flags)
1005 {
1006 	ip += delta;
1007 	parent_ip += delta;
1008 
1009 	seq_print_ip_sym(s, ip, flags);
1010 
1011 	if ((flags & TRACE_ITER_PRINT_PARENT) && parent_ip) {
1012 		trace_seq_puts(s, " <-");
1013 		seq_print_ip_sym(s, parent_ip, flags);
1014 	}
1015 }
1016 
1017 /* TRACE_FN */
1018 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
1019 					struct trace_event *event)
1020 {
1021 	struct ftrace_entry *field;
1022 	struct trace_seq *s = &iter->seq;
1023 
1024 	trace_assign_type(field, iter->ent);
1025 
1026 	print_fn_trace(s, field->ip, field->parent_ip, iter->tr->text_delta, flags);
1027 	trace_seq_putc(s, '\n');
1028 
1029 	return trace_handle_return(s);
1030 }
1031 
1032 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
1033 				      struct trace_event *event)
1034 {
1035 	struct ftrace_entry *field;
1036 
1037 	trace_assign_type(field, iter->ent);
1038 
1039 	trace_seq_printf(&iter->seq, "%lx %lx\n",
1040 			 field->ip,
1041 			 field->parent_ip);
1042 
1043 	return trace_handle_return(&iter->seq);
1044 }
1045 
1046 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
1047 				      struct trace_event *event)
1048 {
1049 	struct ftrace_entry *field;
1050 	struct trace_seq *s = &iter->seq;
1051 
1052 	trace_assign_type(field, iter->ent);
1053 
1054 	SEQ_PUT_HEX_FIELD(s, field->ip);
1055 	SEQ_PUT_HEX_FIELD(s, field->parent_ip);
1056 
1057 	return trace_handle_return(s);
1058 }
1059 
1060 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
1061 				      struct trace_event *event)
1062 {
1063 	struct ftrace_entry *field;
1064 	struct trace_seq *s = &iter->seq;
1065 
1066 	trace_assign_type(field, iter->ent);
1067 
1068 	SEQ_PUT_FIELD(s, field->ip);
1069 	SEQ_PUT_FIELD(s, field->parent_ip);
1070 
1071 	return trace_handle_return(s);
1072 }
1073 
1074 static struct trace_event_functions trace_fn_funcs = {
1075 	.trace		= trace_fn_trace,
1076 	.raw		= trace_fn_raw,
1077 	.hex		= trace_fn_hex,
1078 	.binary		= trace_fn_bin,
1079 };
1080 
1081 static struct trace_event trace_fn_event = {
1082 	.type		= TRACE_FN,
1083 	.funcs		= &trace_fn_funcs,
1084 };
1085 
1086 /* TRACE_CTX an TRACE_WAKE */
1087 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
1088 					     char *delim)
1089 {
1090 	struct ctx_switch_entry *field;
1091 	char comm[TASK_COMM_LEN];
1092 	int S, T;
1093 
1094 
1095 	trace_assign_type(field, iter->ent);
1096 
1097 	T = task_index_to_char(field->next_state);
1098 	S = task_index_to_char(field->prev_state);
1099 	trace_find_cmdline(field->next_pid, comm);
1100 	trace_seq_printf(&iter->seq,
1101 			 " %7d:%3d:%c %s [%03d] %7d:%3d:%c %s\n",
1102 			 field->prev_pid,
1103 			 field->prev_prio,
1104 			 S, delim,
1105 			 field->next_cpu,
1106 			 field->next_pid,
1107 			 field->next_prio,
1108 			 T, comm);
1109 
1110 	return trace_handle_return(&iter->seq);
1111 }
1112 
1113 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
1114 					 struct trace_event *event)
1115 {
1116 	return trace_ctxwake_print(iter, "==>");
1117 }
1118 
1119 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
1120 					  int flags, struct trace_event *event)
1121 {
1122 	return trace_ctxwake_print(iter, "  +");
1123 }
1124 
1125 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
1126 {
1127 	struct ctx_switch_entry *field;
1128 	int T;
1129 
1130 	trace_assign_type(field, iter->ent);
1131 
1132 	if (!S)
1133 		S = task_index_to_char(field->prev_state);
1134 	T = task_index_to_char(field->next_state);
1135 	trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
1136 			 field->prev_pid,
1137 			 field->prev_prio,
1138 			 S,
1139 			 field->next_cpu,
1140 			 field->next_pid,
1141 			 field->next_prio,
1142 			 T);
1143 
1144 	return trace_handle_return(&iter->seq);
1145 }
1146 
1147 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
1148 				       struct trace_event *event)
1149 {
1150 	return trace_ctxwake_raw(iter, 0);
1151 }
1152 
1153 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
1154 					struct trace_event *event)
1155 {
1156 	return trace_ctxwake_raw(iter, '+');
1157 }
1158 
1159 
1160 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
1161 {
1162 	struct ctx_switch_entry *field;
1163 	struct trace_seq *s = &iter->seq;
1164 	int T;
1165 
1166 	trace_assign_type(field, iter->ent);
1167 
1168 	if (!S)
1169 		S = task_index_to_char(field->prev_state);
1170 	T = task_index_to_char(field->next_state);
1171 
1172 	SEQ_PUT_HEX_FIELD(s, field->prev_pid);
1173 	SEQ_PUT_HEX_FIELD(s, field->prev_prio);
1174 	SEQ_PUT_HEX_FIELD(s, S);
1175 	SEQ_PUT_HEX_FIELD(s, field->next_cpu);
1176 	SEQ_PUT_HEX_FIELD(s, field->next_pid);
1177 	SEQ_PUT_HEX_FIELD(s, field->next_prio);
1178 	SEQ_PUT_HEX_FIELD(s, T);
1179 
1180 	return trace_handle_return(s);
1181 }
1182 
1183 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
1184 				       struct trace_event *event)
1185 {
1186 	return trace_ctxwake_hex(iter, 0);
1187 }
1188 
1189 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
1190 					struct trace_event *event)
1191 {
1192 	return trace_ctxwake_hex(iter, '+');
1193 }
1194 
1195 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1196 					   int flags, struct trace_event *event)
1197 {
1198 	struct ctx_switch_entry *field;
1199 	struct trace_seq *s = &iter->seq;
1200 
1201 	trace_assign_type(field, iter->ent);
1202 
1203 	SEQ_PUT_FIELD(s, field->prev_pid);
1204 	SEQ_PUT_FIELD(s, field->prev_prio);
1205 	SEQ_PUT_FIELD(s, field->prev_state);
1206 	SEQ_PUT_FIELD(s, field->next_cpu);
1207 	SEQ_PUT_FIELD(s, field->next_pid);
1208 	SEQ_PUT_FIELD(s, field->next_prio);
1209 	SEQ_PUT_FIELD(s, field->next_state);
1210 
1211 	return trace_handle_return(s);
1212 }
1213 
1214 static struct trace_event_functions trace_ctx_funcs = {
1215 	.trace		= trace_ctx_print,
1216 	.raw		= trace_ctx_raw,
1217 	.hex		= trace_ctx_hex,
1218 	.binary		= trace_ctxwake_bin,
1219 };
1220 
1221 static struct trace_event trace_ctx_event = {
1222 	.type		= TRACE_CTX,
1223 	.funcs		= &trace_ctx_funcs,
1224 };
1225 
1226 static struct trace_event_functions trace_wake_funcs = {
1227 	.trace		= trace_wake_print,
1228 	.raw		= trace_wake_raw,
1229 	.hex		= trace_wake_hex,
1230 	.binary		= trace_ctxwake_bin,
1231 };
1232 
1233 static struct trace_event trace_wake_event = {
1234 	.type		= TRACE_WAKE,
1235 	.funcs		= &trace_wake_funcs,
1236 };
1237 
1238 /* TRACE_STACK */
1239 
1240 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1241 					   int flags, struct trace_event *event)
1242 {
1243 	struct stack_entry *field;
1244 	struct trace_seq *s = &iter->seq;
1245 	unsigned long *p;
1246 	unsigned long *end;
1247 	long delta = iter->tr->text_delta;
1248 
1249 	trace_assign_type(field, iter->ent);
1250 	end = (unsigned long *)((long)iter->ent + iter->ent_size);
1251 
1252 	trace_seq_puts(s, "<stack trace>\n");
1253 
1254 	for (p = field->caller; p && p < end && *p != ULONG_MAX; p++) {
1255 
1256 		if (trace_seq_has_overflowed(s))
1257 			break;
1258 
1259 		trace_seq_puts(s, " => ");
1260 		if ((*p) == FTRACE_TRAMPOLINE_MARKER) {
1261 			trace_seq_puts(s, "[FTRACE TRAMPOLINE]\n");
1262 			continue;
1263 		}
1264 		seq_print_ip_sym(s, (*p) + delta, flags);
1265 		trace_seq_putc(s, '\n');
1266 	}
1267 
1268 	return trace_handle_return(s);
1269 }
1270 
1271 static struct trace_event_functions trace_stack_funcs = {
1272 	.trace		= trace_stack_print,
1273 };
1274 
1275 static struct trace_event trace_stack_event = {
1276 	.type		= TRACE_STACK,
1277 	.funcs		= &trace_stack_funcs,
1278 };
1279 
1280 /* TRACE_USER_STACK */
1281 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1282 						int flags, struct trace_event *event)
1283 {
1284 	struct trace_array *tr = iter->tr;
1285 	struct userstack_entry *field;
1286 	struct trace_seq *s = &iter->seq;
1287 	struct mm_struct *mm = NULL;
1288 	unsigned int i;
1289 
1290 	trace_assign_type(field, iter->ent);
1291 
1292 	trace_seq_puts(s, "<user stack trace>\n");
1293 
1294 	if (tr->trace_flags & TRACE_ITER_SYM_USEROBJ) {
1295 		struct task_struct *task;
1296 		/*
1297 		 * we do the lookup on the thread group leader,
1298 		 * since individual threads might have already quit!
1299 		 */
1300 		rcu_read_lock();
1301 		task = find_task_by_vpid(field->tgid);
1302 		if (task)
1303 			mm = get_task_mm(task);
1304 		rcu_read_unlock();
1305 	}
1306 
1307 	for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1308 		unsigned long ip = field->caller[i];
1309 
1310 		if (!ip || trace_seq_has_overflowed(s))
1311 			break;
1312 
1313 		trace_seq_puts(s, " => ");
1314 		seq_print_user_ip(s, mm, ip, flags);
1315 		trace_seq_putc(s, '\n');
1316 	}
1317 
1318 	if (mm)
1319 		mmput(mm);
1320 
1321 	return trace_handle_return(s);
1322 }
1323 
1324 static struct trace_event_functions trace_user_stack_funcs = {
1325 	.trace		= trace_user_stack_print,
1326 };
1327 
1328 static struct trace_event trace_user_stack_event = {
1329 	.type		= TRACE_USER_STACK,
1330 	.funcs		= &trace_user_stack_funcs,
1331 };
1332 
1333 /* TRACE_HWLAT */
1334 static enum print_line_t
1335 trace_hwlat_print(struct trace_iterator *iter, int flags,
1336 		  struct trace_event *event)
1337 {
1338 	struct trace_entry *entry = iter->ent;
1339 	struct trace_seq *s = &iter->seq;
1340 	struct hwlat_entry *field;
1341 
1342 	trace_assign_type(field, entry);
1343 
1344 	trace_seq_printf(s, "#%-5u inner/outer(us): %4llu/%-5llu ts:%lld.%09ld count:%d",
1345 			 field->seqnum,
1346 			 field->duration,
1347 			 field->outer_duration,
1348 			 (long long)field->timestamp.tv_sec,
1349 			 field->timestamp.tv_nsec, field->count);
1350 
1351 	if (field->nmi_count) {
1352 		/*
1353 		 * The generic sched_clock() is not NMI safe, thus
1354 		 * we only record the count and not the time.
1355 		 */
1356 		if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK))
1357 			trace_seq_printf(s, " nmi-total:%llu",
1358 					 field->nmi_total_ts);
1359 		trace_seq_printf(s, " nmi-count:%u",
1360 				 field->nmi_count);
1361 	}
1362 
1363 	trace_seq_putc(s, '\n');
1364 
1365 	return trace_handle_return(s);
1366 }
1367 
1368 static enum print_line_t
1369 trace_hwlat_raw(struct trace_iterator *iter, int flags,
1370 		struct trace_event *event)
1371 {
1372 	struct hwlat_entry *field;
1373 	struct trace_seq *s = &iter->seq;
1374 
1375 	trace_assign_type(field, iter->ent);
1376 
1377 	trace_seq_printf(s, "%llu %lld %lld %09ld %u\n",
1378 			 field->duration,
1379 			 field->outer_duration,
1380 			 (long long)field->timestamp.tv_sec,
1381 			 field->timestamp.tv_nsec,
1382 			 field->seqnum);
1383 
1384 	return trace_handle_return(s);
1385 }
1386 
1387 static struct trace_event_functions trace_hwlat_funcs = {
1388 	.trace		= trace_hwlat_print,
1389 	.raw		= trace_hwlat_raw,
1390 };
1391 
1392 static struct trace_event trace_hwlat_event = {
1393 	.type		= TRACE_HWLAT,
1394 	.funcs		= &trace_hwlat_funcs,
1395 };
1396 
1397 /* TRACE_OSNOISE */
1398 static enum print_line_t
1399 trace_osnoise_print(struct trace_iterator *iter, int flags,
1400 		    struct trace_event *event)
1401 {
1402 	struct trace_entry *entry = iter->ent;
1403 	struct trace_seq *s = &iter->seq;
1404 	struct osnoise_entry *field;
1405 	u64 ratio, ratio_dec;
1406 	u64 net_runtime;
1407 
1408 	trace_assign_type(field, entry);
1409 
1410 	/*
1411 	 * compute the available % of cpu time.
1412 	 */
1413 	net_runtime = field->runtime - field->noise;
1414 	ratio = net_runtime * 10000000;
1415 	do_div(ratio, field->runtime);
1416 	ratio_dec = do_div(ratio, 100000);
1417 
1418 	trace_seq_printf(s, "%llu %10llu %3llu.%05llu %7llu",
1419 			 field->runtime,
1420 			 field->noise,
1421 			 ratio, ratio_dec,
1422 			 field->max_sample);
1423 
1424 	trace_seq_printf(s, " %6u", field->hw_count);
1425 	trace_seq_printf(s, " %6u", field->nmi_count);
1426 	trace_seq_printf(s, " %6u", field->irq_count);
1427 	trace_seq_printf(s, " %6u", field->softirq_count);
1428 	trace_seq_printf(s, " %6u", field->thread_count);
1429 
1430 	trace_seq_putc(s, '\n');
1431 
1432 	return trace_handle_return(s);
1433 }
1434 
1435 static enum print_line_t
1436 trace_osnoise_raw(struct trace_iterator *iter, int flags,
1437 		  struct trace_event *event)
1438 {
1439 	struct osnoise_entry *field;
1440 	struct trace_seq *s = &iter->seq;
1441 
1442 	trace_assign_type(field, iter->ent);
1443 
1444 	trace_seq_printf(s, "%lld %llu %llu %u %u %u %u %u\n",
1445 			 field->runtime,
1446 			 field->noise,
1447 			 field->max_sample,
1448 			 field->hw_count,
1449 			 field->nmi_count,
1450 			 field->irq_count,
1451 			 field->softirq_count,
1452 			 field->thread_count);
1453 
1454 	return trace_handle_return(s);
1455 }
1456 
1457 static struct trace_event_functions trace_osnoise_funcs = {
1458 	.trace		= trace_osnoise_print,
1459 	.raw		= trace_osnoise_raw,
1460 };
1461 
1462 static struct trace_event trace_osnoise_event = {
1463 	.type		= TRACE_OSNOISE,
1464 	.funcs		= &trace_osnoise_funcs,
1465 };
1466 
1467 /* TRACE_TIMERLAT */
1468 
1469 static char *timerlat_lat_context[] = {"irq", "thread", "user-ret"};
1470 static enum print_line_t
1471 trace_timerlat_print(struct trace_iterator *iter, int flags,
1472 		     struct trace_event *event)
1473 {
1474 	struct trace_entry *entry = iter->ent;
1475 	struct trace_seq *s = &iter->seq;
1476 	struct timerlat_entry *field;
1477 
1478 	trace_assign_type(field, entry);
1479 
1480 	trace_seq_printf(s, "#%-5u context %6s timer_latency %9llu ns\n",
1481 			 field->seqnum,
1482 			 timerlat_lat_context[field->context],
1483 			 field->timer_latency);
1484 
1485 	return trace_handle_return(s);
1486 }
1487 
1488 static enum print_line_t
1489 trace_timerlat_raw(struct trace_iterator *iter, int flags,
1490 		   struct trace_event *event)
1491 {
1492 	struct timerlat_entry *field;
1493 	struct trace_seq *s = &iter->seq;
1494 
1495 	trace_assign_type(field, iter->ent);
1496 
1497 	trace_seq_printf(s, "%u %d %llu\n",
1498 			 field->seqnum,
1499 			 field->context,
1500 			 field->timer_latency);
1501 
1502 	return trace_handle_return(s);
1503 }
1504 
1505 static struct trace_event_functions trace_timerlat_funcs = {
1506 	.trace		= trace_timerlat_print,
1507 	.raw		= trace_timerlat_raw,
1508 };
1509 
1510 static struct trace_event trace_timerlat_event = {
1511 	.type		= TRACE_TIMERLAT,
1512 	.funcs		= &trace_timerlat_funcs,
1513 };
1514 
1515 /* TRACE_BPUTS */
1516 static enum print_line_t
1517 trace_bputs_print(struct trace_iterator *iter, int flags,
1518 		   struct trace_event *event)
1519 {
1520 	struct trace_entry *entry = iter->ent;
1521 	struct trace_seq *s = &iter->seq;
1522 	struct bputs_entry *field;
1523 
1524 	trace_assign_type(field, entry);
1525 
1526 	seq_print_ip_sym(s, field->ip, flags);
1527 	trace_seq_puts(s, ": ");
1528 	trace_seq_puts(s, field->str);
1529 
1530 	return trace_handle_return(s);
1531 }
1532 
1533 
1534 static enum print_line_t
1535 trace_bputs_raw(struct trace_iterator *iter, int flags,
1536 		struct trace_event *event)
1537 {
1538 	struct bputs_entry *field;
1539 	struct trace_seq *s = &iter->seq;
1540 
1541 	trace_assign_type(field, iter->ent);
1542 
1543 	trace_seq_printf(s, ": %lx : ", field->ip);
1544 	trace_seq_puts(s, field->str);
1545 
1546 	return trace_handle_return(s);
1547 }
1548 
1549 static struct trace_event_functions trace_bputs_funcs = {
1550 	.trace		= trace_bputs_print,
1551 	.raw		= trace_bputs_raw,
1552 };
1553 
1554 static struct trace_event trace_bputs_event = {
1555 	.type		= TRACE_BPUTS,
1556 	.funcs		= &trace_bputs_funcs,
1557 };
1558 
1559 /* TRACE_BPRINT */
1560 static enum print_line_t
1561 trace_bprint_print(struct trace_iterator *iter, int flags,
1562 		   struct trace_event *event)
1563 {
1564 	struct trace_entry *entry = iter->ent;
1565 	struct trace_seq *s = &iter->seq;
1566 	struct bprint_entry *field;
1567 
1568 	trace_assign_type(field, entry);
1569 
1570 	seq_print_ip_sym(s, field->ip, flags);
1571 	trace_seq_puts(s, ": ");
1572 	trace_seq_bprintf(s, field->fmt, field->buf);
1573 
1574 	return trace_handle_return(s);
1575 }
1576 
1577 
1578 static enum print_line_t
1579 trace_bprint_raw(struct trace_iterator *iter, int flags,
1580 		 struct trace_event *event)
1581 {
1582 	struct bprint_entry *field;
1583 	struct trace_seq *s = &iter->seq;
1584 
1585 	trace_assign_type(field, iter->ent);
1586 
1587 	trace_seq_printf(s, ": %lx : ", field->ip);
1588 	trace_seq_bprintf(s, field->fmt, field->buf);
1589 
1590 	return trace_handle_return(s);
1591 }
1592 
1593 static struct trace_event_functions trace_bprint_funcs = {
1594 	.trace		= trace_bprint_print,
1595 	.raw		= trace_bprint_raw,
1596 };
1597 
1598 static struct trace_event trace_bprint_event = {
1599 	.type		= TRACE_BPRINT,
1600 	.funcs		= &trace_bprint_funcs,
1601 };
1602 
1603 /* TRACE_PRINT */
1604 static enum print_line_t trace_print_print(struct trace_iterator *iter,
1605 					   int flags, struct trace_event *event)
1606 {
1607 	struct print_entry *field;
1608 	struct trace_seq *s = &iter->seq;
1609 	unsigned long ip;
1610 
1611 	trace_assign_type(field, iter->ent);
1612 
1613 	ip = field->ip + iter->tr->text_delta;
1614 
1615 	seq_print_ip_sym(s, ip, flags);
1616 	trace_seq_printf(s, ": %s", field->buf);
1617 
1618 	return trace_handle_return(s);
1619 }
1620 
1621 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
1622 					 struct trace_event *event)
1623 {
1624 	struct print_entry *field;
1625 
1626 	trace_assign_type(field, iter->ent);
1627 
1628 	trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf);
1629 
1630 	return trace_handle_return(&iter->seq);
1631 }
1632 
1633 static struct trace_event_functions trace_print_funcs = {
1634 	.trace		= trace_print_print,
1635 	.raw		= trace_print_raw,
1636 };
1637 
1638 static struct trace_event trace_print_event = {
1639 	.type	 	= TRACE_PRINT,
1640 	.funcs		= &trace_print_funcs,
1641 };
1642 
1643 static enum print_line_t trace_raw_data(struct trace_iterator *iter, int flags,
1644 					 struct trace_event *event)
1645 {
1646 	struct raw_data_entry *field;
1647 	int i;
1648 
1649 	trace_assign_type(field, iter->ent);
1650 
1651 	trace_seq_printf(&iter->seq, "# %x buf:", field->id);
1652 
1653 	for (i = 0; i < iter->ent_size - offsetof(struct raw_data_entry, buf); i++)
1654 		trace_seq_printf(&iter->seq, " %02x",
1655 				 (unsigned char)field->buf[i]);
1656 
1657 	trace_seq_putc(&iter->seq, '\n');
1658 
1659 	return trace_handle_return(&iter->seq);
1660 }
1661 
1662 static struct trace_event_functions trace_raw_data_funcs = {
1663 	.trace		= trace_raw_data,
1664 	.raw		= trace_raw_data,
1665 };
1666 
1667 static struct trace_event trace_raw_data_event = {
1668 	.type	 	= TRACE_RAW_DATA,
1669 	.funcs		= &trace_raw_data_funcs,
1670 };
1671 
1672 static enum print_line_t
1673 trace_func_repeats_raw(struct trace_iterator *iter, int flags,
1674 			 struct trace_event *event)
1675 {
1676 	struct func_repeats_entry *field;
1677 	struct trace_seq *s = &iter->seq;
1678 
1679 	trace_assign_type(field, iter->ent);
1680 
1681 	trace_seq_printf(s, "%lu %lu %u %llu\n",
1682 			 field->ip,
1683 			 field->parent_ip,
1684 			 field->count,
1685 			 FUNC_REPEATS_GET_DELTA_TS(field));
1686 
1687 	return trace_handle_return(s);
1688 }
1689 
1690 static enum print_line_t
1691 trace_func_repeats_print(struct trace_iterator *iter, int flags,
1692 			 struct trace_event *event)
1693 {
1694 	struct func_repeats_entry *field;
1695 	struct trace_seq *s = &iter->seq;
1696 
1697 	trace_assign_type(field, iter->ent);
1698 
1699 	print_fn_trace(s, field->ip, field->parent_ip, iter->tr->text_delta, flags);
1700 	trace_seq_printf(s, " (repeats: %u, last_ts:", field->count);
1701 	trace_print_time(s, iter,
1702 			 iter->ts - FUNC_REPEATS_GET_DELTA_TS(field));
1703 	trace_seq_puts(s, ")\n");
1704 
1705 	return trace_handle_return(s);
1706 }
1707 
1708 static struct trace_event_functions trace_func_repeats_funcs = {
1709 	.trace		= trace_func_repeats_print,
1710 	.raw		= trace_func_repeats_raw,
1711 };
1712 
1713 static struct trace_event trace_func_repeats_event = {
1714 	.type	 	= TRACE_FUNC_REPEATS,
1715 	.funcs		= &trace_func_repeats_funcs,
1716 };
1717 
1718 static struct trace_event *events[] __initdata = {
1719 	&trace_fn_event,
1720 	&trace_ctx_event,
1721 	&trace_wake_event,
1722 	&trace_stack_event,
1723 	&trace_user_stack_event,
1724 	&trace_bputs_event,
1725 	&trace_bprint_event,
1726 	&trace_print_event,
1727 	&trace_hwlat_event,
1728 	&trace_osnoise_event,
1729 	&trace_timerlat_event,
1730 	&trace_raw_data_event,
1731 	&trace_func_repeats_event,
1732 	NULL
1733 };
1734 
1735 __init int init_events(void)
1736 {
1737 	struct trace_event *event;
1738 	int i, ret;
1739 
1740 	for (i = 0; events[i]; i++) {
1741 		event = events[i];
1742 		ret = register_trace_event(event);
1743 		WARN_ONCE(!ret, "event %d failed to register", event->type);
1744 	}
1745 
1746 	return 0;
1747 }
1748