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