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