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