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