trace_output.c 28.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * trace_output.c
 *
 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
 *
 */

#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/ftrace.h>

#include "trace_output.h"

/* must be a power of 2 */
#define EVENT_HASHSIZE	128

17
DECLARE_RWSEM(trace_event_sem);
18

19 20 21 22
static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;

static int next_event_type = __TRACE_LAST_TYPE + 1;

23 24 25 26 27 28 29 30
enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter)
{
	struct trace_seq *s = &iter->seq;
	struct trace_entry *entry = iter->ent;
	struct bputs_entry *field;

	trace_assign_type(field, entry);

31
	trace_seq_puts(s, field->str);
32

33
	return trace_handle_return(s);
34 35
}

36 37 38 39 40 41 42 43
enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
{
	struct trace_seq *s = &iter->seq;
	struct trace_entry *entry = iter->ent;
	struct bprint_entry *field;

	trace_assign_type(field, entry);

44
	trace_seq_bprintf(s, field->fmt, field->buf);
45

46
	return trace_handle_return(s);
47 48 49 50 51 52 53 54 55 56
}

enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
{
	struct trace_seq *s = &iter->seq;
	struct trace_entry *entry = iter->ent;
	struct print_entry *field;

	trace_assign_type(field, entry);

57
	trace_seq_puts(s, field->buf);
58

59
	return trace_handle_return(s);
60 61
}

62
const char *
63 64 65
trace_print_flags_seq(struct trace_seq *p, const char *delim,
		      unsigned long flags,
		      const struct trace_print_flags *flag_array)
66 67 68
{
	unsigned long mask;
	const char *str;
69
	const char *ret = trace_seq_buffer_ptr(p);
70
	int i, first = 1;
71 72 73 74 75 76 77 78 79

	for (i = 0;  flag_array[i].name && flags; i++) {

		mask = flag_array[i].mask;
		if ((flags & mask) != mask)
			continue;

		str = flag_array[i].name;
		flags &= ~mask;
80
		if (!first && delim)
81
			trace_seq_puts(p, delim);
82 83
		else
			first = 0;
84 85 86 87 88
		trace_seq_puts(p, str);
	}

	/* check for left over flags */
	if (flags) {
89
		if (!first && delim)
90 91 92 93 94 95
			trace_seq_puts(p, delim);
		trace_seq_printf(p, "0x%lx", flags);
	}

	trace_seq_putc(p, 0);

96
	return ret;
97
}
98
EXPORT_SYMBOL(trace_print_flags_seq);
99

100
const char *
101 102
trace_print_symbols_seq(struct trace_seq *p, unsigned long val,
			const struct trace_print_flags *symbol_array)
103 104
{
	int i;
105
	const char *ret = trace_seq_buffer_ptr(p);
106 107 108 109 110 111 112 113 114 115

	for (i = 0;  symbol_array[i].name; i++) {

		if (val != symbol_array[i].mask)
			continue;

		trace_seq_puts(p, symbol_array[i].name);
		break;
	}

116
	if (ret == (const char *)(trace_seq_buffer_ptr(p)))
117
		trace_seq_printf(p, "0x%lx", val);
118

119 120
	trace_seq_putc(p, 0);

121
	return ret;
122
}
123
EXPORT_SYMBOL(trace_print_symbols_seq);
124

125 126
#if BITS_PER_LONG == 32
const char *
127
trace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
128 129 130
			 const struct trace_print_flags_u64 *symbol_array)
{
	int i;
131
	const char *ret = trace_seq_buffer_ptr(p);
132 133 134 135 136 137 138 139 140 141

	for (i = 0;  symbol_array[i].name; i++) {

		if (val != symbol_array[i].mask)
			continue;

		trace_seq_puts(p, symbol_array[i].name);
		break;
	}

142
	if (ret == (const char *)(trace_seq_buffer_ptr(p)))
143 144 145 146 147 148
		trace_seq_printf(p, "0x%llx", val);

	trace_seq_putc(p, 0);

	return ret;
}
149
EXPORT_SYMBOL(trace_print_symbols_seq_u64);
150 151
#endif

152
const char *
153 154
trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
			unsigned int bitmask_size)
155
{
156
	const char *ret = trace_seq_buffer_ptr(p);
157 158 159 160 161 162

	trace_seq_bitmask(p, bitmask_ptr, bitmask_size * 8);
	trace_seq_putc(p, 0);

	return ret;
}
163
EXPORT_SYMBOL_GPL(trace_print_bitmask_seq);
164

K
Kei Tokunaga 已提交
165
const char *
166
trace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len)
K
Kei Tokunaga 已提交
167 168
{
	int i;
169
	const char *ret = trace_seq_buffer_ptr(p);
K
Kei Tokunaga 已提交
170 171 172 173 174 175 176 177

	for (i = 0; i < buf_len; i++)
		trace_seq_printf(p, "%s%2.2x", i == 0 ? "" : " ", buf[i]);

	trace_seq_putc(p, 0);

	return ret;
}
178
EXPORT_SYMBOL(trace_print_hex_seq);
K
Kei Tokunaga 已提交
179

D
Dave Martin 已提交
180
const char *
181 182
trace_print_array_seq(struct trace_seq *p, const void *buf, int count,
		      size_t el_size)
D
Dave Martin 已提交
183 184 185 186
{
	const char *ret = trace_seq_buffer_ptr(p);
	const char *prefix = "";
	void *ptr = (void *)buf;
187
	size_t buf_len = count * el_size;
D
Dave Martin 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

	trace_seq_putc(p, '{');

	while (ptr < buf + buf_len) {
		switch (el_size) {
		case 1:
			trace_seq_printf(p, "%s0x%x", prefix,
					 *(u8 *)ptr);
			break;
		case 2:
			trace_seq_printf(p, "%s0x%x", prefix,
					 *(u16 *)ptr);
			break;
		case 4:
			trace_seq_printf(p, "%s0x%x", prefix,
					 *(u32 *)ptr);
			break;
		case 8:
			trace_seq_printf(p, "%s0x%llx", prefix,
					 *(u64 *)ptr);
			break;
		default:
			trace_seq_printf(p, "BAD SIZE:%zu 0x%x", el_size,
					 *(u8 *)ptr);
			el_size = 1;
		}
		prefix = ",";
		ptr += el_size;
	}

	trace_seq_putc(p, '}');
	trace_seq_putc(p, 0);

	return ret;
}
223
EXPORT_SYMBOL(trace_print_array_seq);
D
Dave Martin 已提交
224

225 226
int trace_raw_output_prep(struct trace_iterator *iter,
			  struct trace_event *trace_event)
227
{
228
	struct trace_event_call *event;
229 230 231 232
	struct trace_seq *s = &iter->seq;
	struct trace_seq *p = &iter->tmp_seq;
	struct trace_entry *entry;

233
	event = container_of(trace_event, struct trace_event_call, event);
234 235 236 237 238 239 240 241
	entry = iter->ent;

	if (entry->type != event->event.type) {
		WARN_ON_ONCE(1);
		return TRACE_TYPE_UNHANDLED;
	}

	trace_seq_init(p);
242
	trace_seq_printf(s, "%s: ", trace_event_name(event));
243

244
	return trace_handle_return(s);
245
}
246
EXPORT_SYMBOL(trace_raw_output_prep);
247

248 249
static int trace_output_raw(struct trace_iterator *iter, char *name,
			    char *fmt, va_list ap)
250 251 252
{
	struct trace_seq *s = &iter->seq;

253 254
	trace_seq_printf(s, "%s: ", name);
	trace_seq_vprintf(s, fmt, ap);
255

256
	return trace_handle_return(s);
257 258
}

259
int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...)
260 261 262 263 264
{
	va_list ap;
	int ret;

	va_start(ap, fmt);
265
	ret = trace_output_raw(iter, name, fmt, ap);
266 267 268 269
	va_end(ap);

	return ret;
}
270
EXPORT_SYMBOL_GPL(trace_output_call);
271

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
#ifdef CONFIG_KRETPROBES
static inline const char *kretprobed(const char *name)
{
	static const char tramp_name[] = "kretprobe_trampoline";
	int size = sizeof(tramp_name);

	if (strncmp(tramp_name, name, size) == 0)
		return "[unknown/kretprobe'd]";
	return name;
}
#else
static inline const char *kretprobed(const char *name)
{
	return name;
}
#endif /* CONFIG_KRETPROBES */

289
static void
290 291 292 293 294 295 296 297 298 299
seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
{
#ifdef CONFIG_KALLSYMS
	char str[KSYM_SYMBOL_LEN];
	const char *name;

	kallsyms_lookup(address, NULL, NULL, NULL, str);

	name = kretprobed(str);

300
	trace_seq_printf(s, fmt, name);
301 302 303
#endif
}

304
static void
305 306 307 308 309 310 311 312 313 314
seq_print_sym_offset(struct trace_seq *s, const char *fmt,
		     unsigned long address)
{
#ifdef CONFIG_KALLSYMS
	char str[KSYM_SYMBOL_LEN];
	const char *name;

	sprint_symbol(str, address);
	name = kretprobed(str);

315
	trace_seq_printf(s, fmt, name);
316 317 318 319 320 321 322 323 324
#endif
}

#ifndef CONFIG_64BIT
# define IP_FMT "%08lx"
#else
# define IP_FMT "%016lx"
#endif

325 326
static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
			     unsigned long ip, unsigned long sym_flags)
327 328 329 330 331
{
	struct file *file = NULL;
	unsigned long vmstart = 0;
	int ret = 1;

332 333 334
	if (s->full)
		return 0;

335 336 337 338 339 340 341 342 343 344 345 346
	if (mm) {
		const struct vm_area_struct *vma;

		down_read(&mm->mmap_sem);
		vma = find_vma(mm, ip);
		if (vma) {
			file = vma->vm_file;
			vmstart = vma->vm_start;
		}
		if (file) {
			ret = trace_seq_path(s, &file->f_path);
			if (ret)
347 348
				trace_seq_printf(s, "[+0x%lx]",
						 ip - vmstart);
349 350 351 352
		}
		up_read(&mm->mmap_sem);
	}
	if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
353 354
		trace_seq_printf(s, " <" IP_FMT ">", ip);
	return !trace_seq_has_overflowed(s);
355 356 357 358 359
}

int
seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
{
360 361 362 363
	if (!ip) {
		trace_seq_putc(s, '0');
		goto out;
	}
364 365

	if (sym_flags & TRACE_ITER_SYM_OFFSET)
366
		seq_print_sym_offset(s, "%s", ip);
367
	else
368
		seq_print_sym_short(s, "%s", ip);
369 370

	if (sym_flags & TRACE_ITER_SYM_ADDR)
371 372 373 374
		trace_seq_printf(s, " <" IP_FMT ">", ip);

 out:
	return !trace_seq_has_overflowed(s);
375 376
}

377 378 379 380 381 382
/**
 * trace_print_lat_fmt - print the irq, preempt and lockdep fields
 * @s: trace seq struct to write to
 * @entry: The trace entry field from the ring buffer
 *
 * Prints the generic fields of irqs off, in hard or softirq, preempt
383
 * count.
384 385
 */
int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
386
{
387 388 389 390 391
	char hardsoft_irq;
	char need_resched;
	char irqs_off;
	int hardirq;
	int softirq;
392
	int nmi;
393

394
	nmi = entry->flags & TRACE_FLAG_NMI;
395 396
	hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
	softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
397

398 399 400 401
	irqs_off =
		(entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
		(entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' :
		'.';
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

	switch (entry->flags & (TRACE_FLAG_NEED_RESCHED |
				TRACE_FLAG_PREEMPT_RESCHED)) {
	case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
		need_resched = 'N';
		break;
	case TRACE_FLAG_NEED_RESCHED:
		need_resched = 'n';
		break;
	case TRACE_FLAG_PREEMPT_RESCHED:
		need_resched = 'p';
		break;
	default:
		need_resched = '.';
		break;
	}

419
	hardsoft_irq =
420 421
		(nmi && hardirq)     ? 'Z' :
		nmi                  ? 'z' :
422
		(hardirq && softirq) ? 'H' :
423 424 425
		hardirq              ? 'h' :
		softirq              ? 's' :
		                       '.' ;
426

427 428
	trace_seq_printf(s, "%c%c%c",
			 irqs_off, need_resched, hardsoft_irq);
429

430
	if (entry->preempt_count)
431
		trace_seq_printf(s, "%x", entry->preempt_count);
432
	else
433
		trace_seq_putc(s, '.');
434

435
	return !trace_seq_has_overflowed(s);
436 437
}

438 439 440 441 442 443 444
static int
lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
{
	char comm[TASK_COMM_LEN];

	trace_find_cmdline(entry->pid, comm);

445 446
	trace_seq_printf(s, "%8.8s-%-5d %3d",
			 comm, entry->pid, cpu);
447 448 449 450

	return trace_print_lat_fmt(s, entry);
}

451 452 453 454 455 456 457 458
#undef MARK
#define MARK(v, s) {.val = v, .sym = s}
/* trace overhead mark */
static const struct trace_mark {
	unsigned long long	val; /* unit: nsec */
	char			sym;
} mark[] = {
	MARK(1000000000ULL	, '$'), /* 1 sec */
459 460
	MARK(100000000ULL	, '@'), /* 100 msec */
	MARK(10000000ULL	, '*'), /* 10 msec */
461 462 463 464 465 466 467 468 469 470 471 472
	MARK(1000000ULL		, '#'), /* 1000 usecs */
	MARK(100000ULL		, '!'), /* 100 usecs */
	MARK(10000ULL		, '+'), /* 10 usecs */
};
#undef MARK

char trace_find_mark(unsigned long long d)
{
	int i;
	int size = ARRAY_SIZE(mark);

	for (i = 0; i < size; i++) {
473
		if (d > mark[i].val)
474 475 476 477 478
			break;
	}

	return (i == size) ? ' ' : mark[i].sym;
}
479

480
static int
481
lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
482
{
483 484
	struct trace_array *tr = iter->tr;
	unsigned long verbose = tr->trace_flags & TRACE_ITER_VERBOSE;
485
	unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
486
	unsigned long long abs_ts = iter->ts - iter->trace_buffer->time_start;
487 488 489 490 491 492 493 494 495 496 497 498 499 500
	unsigned long long rel_ts = next_ts - iter->ts;
	struct trace_seq *s = &iter->seq;

	if (in_ns) {
		abs_ts = ns2usecs(abs_ts);
		rel_ts = ns2usecs(rel_ts);
	}

	if (verbose && in_ns) {
		unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
		unsigned long abs_msec = (unsigned long)abs_ts;
		unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
		unsigned long rel_msec = (unsigned long)rel_ts;

501 502 503 504 505 506
		trace_seq_printf(
			s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
			ns2usecs(iter->ts),
			abs_msec, abs_usec,
			rel_msec, rel_usec);

507
	} else if (verbose && !in_ns) {
508 509 510 511
		trace_seq_printf(
			s, "[%016llx] %lld (+%lld): ",
			iter->ts, abs_ts, rel_ts);

512
	} else if (!verbose && in_ns) {
513 514 515
		trace_seq_printf(
			s, " %4lldus%c: ",
			abs_ts,
516
			trace_find_mark(rel_ts * NSEC_PER_USEC));
517

518
	} else { /* !verbose && !in_ns */
519
		trace_seq_printf(s, " %4lld: ", abs_ts);
520
	}
521 522

	return !trace_seq_has_overflowed(s);
523 524 525 526
}

int trace_print_context(struct trace_iterator *iter)
{
527
	struct trace_array *tr = iter->tr;
528 529
	struct trace_seq *s = &iter->seq;
	struct trace_entry *entry = iter->ent;
530 531
	unsigned long long t;
	unsigned long secs, usec_rem;
532 533 534
	char comm[TASK_COMM_LEN];

	trace_find_cmdline(entry->pid, comm);
535

536
	trace_seq_printf(s, "%16s-%-5d [%03d] ",
537 538
			       comm, entry->pid, iter->cpu);

539
	if (tr->trace_flags & TRACE_ITER_IRQ_INFO)
540
		trace_print_lat_fmt(s, entry);
541

542 543 544 545
	if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
		t = ns2usecs(iter->ts);
		usec_rem = do_div(t, USEC_PER_SEC);
		secs = (unsigned long)t;
546
		trace_seq_printf(s, " %5lu.%06lu: ", secs, usec_rem);
547
	} else
548 549 550
		trace_seq_printf(s, " %12llu: ", iter->ts);

	return !trace_seq_has_overflowed(s);
551 552 553 554
}

int trace_print_lat_context(struct trace_iterator *iter)
{
555
	struct trace_array *tr = iter->tr;
556 557
	/* trace_find_next_entry will reset ent_size */
	int ent_size = iter->ent_size;
558
	struct trace_seq *s = &iter->seq;
559
	u64 next_ts;
560 561 562
	struct trace_entry *entry = iter->ent,
			   *next_entry = trace_find_next_entry(iter, NULL,
							       &next_ts);
563
	unsigned long verbose = (tr->trace_flags & TRACE_ITER_VERBOSE);
564

565 566 567
	/* Restore the original ent_size */
	iter->ent_size = ent_size;

568 569 570 571
	if (!next_entry)
		next_ts = iter->ts;

	if (verbose) {
572 573 574 575
		char comm[TASK_COMM_LEN];

		trace_find_cmdline(entry->pid, comm);

576 577 578 579
		trace_seq_printf(
			s, "%16s %5d %3d %d %08x %08lx ",
			comm, entry->pid, iter->cpu, entry->flags,
			entry->preempt_count, iter->idx);
580
	} else {
581
		lat_print_generic(s, entry, iter->cpu);
582 583
	}

584
	lat_print_timestamp(iter, next_ts);
585

586
	return !trace_seq_has_overflowed(s);
587 588
}

589 590 591 592 593 594 595 596 597
static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;

static int task_state_char(unsigned long state)
{
	int bit = state ? __ffs(state) + 1 : 0;

	return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
}

598 599 600 601 602
/**
 * ftrace_find_event - find a registered event
 * @type: the type of event to look for
 *
 * Returns an event of type @type otherwise NULL
603
 * Called with trace_event_read_lock() held.
604 605 606 607 608 609 610 611
 */
struct trace_event *ftrace_find_event(int type)
{
	struct trace_event *event;
	unsigned key;

	key = type & (EVENT_HASHSIZE - 1);

612
	hlist_for_each_entry(event, &event_hash[key], node) {
613 614 615 616 617 618 619
		if (event->type == type)
			return event;
	}

	return NULL;
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
static LIST_HEAD(ftrace_event_list);

static int trace_search_list(struct list_head **list)
{
	struct trace_event *e;
	int last = __TRACE_LAST_TYPE;

	if (list_empty(&ftrace_event_list)) {
		*list = &ftrace_event_list;
		return last + 1;
	}

	/*
	 * We used up all possible max events,
	 * lets see if somebody freed one.
	 */
	list_for_each_entry(e, &ftrace_event_list, list) {
		if (e->type != last + 1)
			break;
		last++;
	}

	/* Did we used up all 65 thousand events??? */
643
	if ((last + 1) > TRACE_EVENT_TYPE_MAX)
644 645 646 647 648 649
		return 0;

	*list = &e->list;
	return last + 1;
}

650 651
void trace_event_read_lock(void)
{
652
	down_read(&trace_event_sem);
653 654 655 656
}

void trace_event_read_unlock(void)
{
657
	up_read(&trace_event_sem);
658 659
}

660
/**
661
 * register_trace_event - register output for an event type
662 663 664 665 666 667 668 669 670 671 672 673 674
 * @event: the event type to register
 *
 * Event types are stored in a hash and this hash is used to
 * find a way to print an event. If the @event->type is set
 * then it will use that type, otherwise it will assign a
 * type to use.
 *
 * If you assign your own type, please make sure it is added
 * to the trace_type enum in trace.h, to avoid collisions
 * with the dynamic types.
 *
 * Returns the event type number or zero on error.
 */
675
int register_trace_event(struct trace_event *event)
676 677 678 679
{
	unsigned key;
	int ret = 0;

680
	down_write(&trace_event_sem);
681

682
	if (WARN_ON(!event))
683 684
		goto out;

685 686 687
	if (WARN_ON(!event->funcs))
		goto out;

688 689 690
	INIT_LIST_HEAD(&event->list);

	if (!event->type) {
691
		struct list_head *list = NULL;
692

693
		if (next_event_type > TRACE_EVENT_TYPE_MAX) {
694 695 696 697 698 699

			event->type = trace_search_list(&list);
			if (!event->type)
				goto out;

		} else {
700

701 702 703 704 705 706 707 708 709 710
			event->type = next_event_type++;
			list = &ftrace_event_list;
		}

		if (WARN_ON(ftrace_find_event(event->type)))
			goto out;

		list_add_tail(&event->list, list);

	} else if (event->type > __TRACE_LAST_TYPE) {
711 712 713
		printk(KERN_WARNING "Need to add type to trace.h\n");
		WARN_ON(1);
		goto out;
714 715 716 717 718
	} else {
		/* Is this event already used */
		if (ftrace_find_event(event->type))
			goto out;
	}
719

720 721 722 723 724 725 726 727
	if (event->funcs->trace == NULL)
		event->funcs->trace = trace_nop_print;
	if (event->funcs->raw == NULL)
		event->funcs->raw = trace_nop_print;
	if (event->funcs->hex == NULL)
		event->funcs->hex = trace_nop_print;
	if (event->funcs->binary == NULL)
		event->funcs->binary = trace_nop_print;
728

729 730
	key = event->type & (EVENT_HASHSIZE - 1);

731
	hlist_add_head(&event->node, &event_hash[key]);
732 733 734

	ret = event->type;
 out:
735
	up_write(&trace_event_sem);
736 737 738

	return ret;
}
739
EXPORT_SYMBOL_GPL(register_trace_event);
740

741
/*
742
 * Used by module code with the trace_event_sem held for write.
743
 */
744
int __unregister_trace_event(struct trace_event *event)
745 746 747 748 749 750
{
	hlist_del(&event->node);
	list_del(&event->list);
	return 0;
}

751
/**
752
 * unregister_trace_event - remove a no longer used event
753 754
 * @event: the event to remove
 */
755
int unregister_trace_event(struct trace_event *event)
756
{
757
	down_write(&trace_event_sem);
758
	__unregister_trace_event(event);
759
	up_write(&trace_event_sem);
760 761 762

	return 0;
}
763
EXPORT_SYMBOL_GPL(unregister_trace_event);
764 765 766 767 768

/*
 * Standard events
 */

769 770
enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
				  struct trace_event *event)
771
{
772
	trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type);
773

774
	return trace_handle_return(&iter->seq);
775 776 777
}

/* TRACE_FN */
778 779
static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
					struct trace_event *event)
780 781
{
	struct ftrace_entry *field;
782
	struct trace_seq *s = &iter->seq;
783

784
	trace_assign_type(field, iter->ent);
785

786
	seq_print_ip_sym(s, field->ip, flags);
787 788

	if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
789 790
		trace_seq_puts(s, " <-");
		seq_print_ip_sym(s, field->parent_ip, flags);
791 792
	}

793
	trace_seq_putc(s, '\n');
794

795
	return trace_handle_return(s);
796 797
}

798 799
static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
				      struct trace_event *event)
800 801 802
{
	struct ftrace_entry *field;

803
	trace_assign_type(field, iter->ent);
804

805 806 807
	trace_seq_printf(&iter->seq, "%lx %lx\n",
			 field->ip,
			 field->parent_ip);
808

809
	return trace_handle_return(&iter->seq);
810 811
}

812 813
static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
				      struct trace_event *event)
814 815
{
	struct ftrace_entry *field;
816
	struct trace_seq *s = &iter->seq;
817

818
	trace_assign_type(field, iter->ent);
819

820 821
	SEQ_PUT_HEX_FIELD(s, field->ip);
	SEQ_PUT_HEX_FIELD(s, field->parent_ip);
822

823
	return trace_handle_return(s);
824 825
}

826 827
static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
				      struct trace_event *event)
828 829
{
	struct ftrace_entry *field;
830
	struct trace_seq *s = &iter->seq;
831

832
	trace_assign_type(field, iter->ent);
833

834 835
	SEQ_PUT_FIELD(s, field->ip);
	SEQ_PUT_FIELD(s, field->parent_ip);
836

837
	return trace_handle_return(s);
838 839
}

840
static struct trace_event_functions trace_fn_funcs = {
841 842 843 844 845 846
	.trace		= trace_fn_trace,
	.raw		= trace_fn_raw,
	.hex		= trace_fn_hex,
	.binary		= trace_fn_bin,
};

847 848 849 850 851
static struct trace_event trace_fn_event = {
	.type		= TRACE_FN,
	.funcs		= &trace_fn_funcs,
};

852
/* TRACE_CTX an TRACE_WAKE */
853 854
static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
					     char *delim)
855 856
{
	struct ctx_switch_entry *field;
857
	char comm[TASK_COMM_LEN];
858 859
	int S, T;

860

861
	trace_assign_type(field, iter->ent);
862 863 864

	T = task_state_char(field->next_state);
	S = task_state_char(field->prev_state);
865
	trace_find_cmdline(field->next_pid, comm);
866 867 868 869 870 871 872 873 874 875 876
	trace_seq_printf(&iter->seq,
			 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
			 field->prev_pid,
			 field->prev_prio,
			 S, delim,
			 field->next_cpu,
			 field->next_pid,
			 field->next_prio,
			 T, comm);

	return trace_handle_return(&iter->seq);
877 878
}

879 880
static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
					 struct trace_event *event)
881
{
882
	return trace_ctxwake_print(iter, "==>");
883 884
}

885
static enum print_line_t trace_wake_print(struct trace_iterator *iter,
886
					  int flags, struct trace_event *event)
887
{
888
	return trace_ctxwake_print(iter, "  +");
889 890
}

891
static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
892 893 894 895
{
	struct ctx_switch_entry *field;
	int T;

896
	trace_assign_type(field, iter->ent);
897 898

	if (!S)
899
		S = task_state_char(field->prev_state);
900
	T = task_state_char(field->next_state);
901 902 903 904 905 906 907 908 909 910
	trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
			 field->prev_pid,
			 field->prev_prio,
			 S,
			 field->next_cpu,
			 field->next_pid,
			 field->next_prio,
			 T);

	return trace_handle_return(&iter->seq);
911 912
}

913 914
static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
				       struct trace_event *event)
915
{
916
	return trace_ctxwake_raw(iter, 0);
917 918
}

919 920
static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
					struct trace_event *event)
921
{
922
	return trace_ctxwake_raw(iter, '+');
923 924 925
}


926
static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
927 928
{
	struct ctx_switch_entry *field;
929
	struct trace_seq *s = &iter->seq;
930 931
	int T;

932
	trace_assign_type(field, iter->ent);
933 934

	if (!S)
935
		S = task_state_char(field->prev_state);
936 937
	T = task_state_char(field->next_state);

938 939 940 941 942 943 944
	SEQ_PUT_HEX_FIELD(s, field->prev_pid);
	SEQ_PUT_HEX_FIELD(s, field->prev_prio);
	SEQ_PUT_HEX_FIELD(s, S);
	SEQ_PUT_HEX_FIELD(s, field->next_cpu);
	SEQ_PUT_HEX_FIELD(s, field->next_pid);
	SEQ_PUT_HEX_FIELD(s, field->next_prio);
	SEQ_PUT_HEX_FIELD(s, T);
945

946
	return trace_handle_return(s);
947 948
}

949 950
static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
				       struct trace_event *event)
951
{
952
	return trace_ctxwake_hex(iter, 0);
953 954
}

955 956
static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
					struct trace_event *event)
957
{
958
	return trace_ctxwake_hex(iter, '+');
959 960
}

961
static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
962
					   int flags, struct trace_event *event)
963 964
{
	struct ctx_switch_entry *field;
965
	struct trace_seq *s = &iter->seq;
966

967
	trace_assign_type(field, iter->ent);
968

969 970 971 972 973 974 975
	SEQ_PUT_FIELD(s, field->prev_pid);
	SEQ_PUT_FIELD(s, field->prev_prio);
	SEQ_PUT_FIELD(s, field->prev_state);
	SEQ_PUT_FIELD(s, field->next_cpu);
	SEQ_PUT_FIELD(s, field->next_pid);
	SEQ_PUT_FIELD(s, field->next_prio);
	SEQ_PUT_FIELD(s, field->next_state);
976

977
	return trace_handle_return(s);
978 979
}

980
static struct trace_event_functions trace_ctx_funcs = {
981 982 983 984 985 986
	.trace		= trace_ctx_print,
	.raw		= trace_ctx_raw,
	.hex		= trace_ctx_hex,
	.binary		= trace_ctxwake_bin,
};

987 988 989 990 991 992
static struct trace_event trace_ctx_event = {
	.type		= TRACE_CTX,
	.funcs		= &trace_ctx_funcs,
};

static struct trace_event_functions trace_wake_funcs = {
993 994 995 996 997 998
	.trace		= trace_wake_print,
	.raw		= trace_wake_raw,
	.hex		= trace_wake_hex,
	.binary		= trace_ctxwake_bin,
};

999 1000 1001 1002 1003
static struct trace_event trace_wake_event = {
	.type		= TRACE_WAKE,
	.funcs		= &trace_wake_funcs,
};

1004 1005
/* TRACE_STACK */

1006
static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1007
					   int flags, struct trace_event *event)
1008 1009
{
	struct stack_entry *field;
1010
	struct trace_seq *s = &iter->seq;
1011 1012
	unsigned long *p;
	unsigned long *end;
1013

1014
	trace_assign_type(field, iter->ent);
1015
	end = (unsigned long *)((long)iter->ent + iter->ent_size);
1016

1017
	trace_seq_puts(s, "<stack trace>\n");
1018 1019

	for (p = field->caller; p && *p != ULONG_MAX && p < end; p++) {
1020

1021 1022
		if (trace_seq_has_overflowed(s))
			break;
1023

1024 1025 1026 1027
		trace_seq_puts(s, " => ");
		seq_print_ip_sym(s, *p, flags);
		trace_seq_putc(s, '\n');
	}
1028

1029
	return trace_handle_return(s);
1030 1031
}

1032
static struct trace_event_functions trace_stack_funcs = {
1033 1034 1035
	.trace		= trace_stack_print,
};

1036 1037 1038 1039 1040
static struct trace_event trace_stack_event = {
	.type		= TRACE_STACK,
	.funcs		= &trace_stack_funcs,
};

1041
/* TRACE_USER_STACK */
1042
static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1043
						int flags, struct trace_event *event)
1044
{
1045
	struct trace_array *tr = iter->tr;
1046
	struct userstack_entry *field;
1047
	struct trace_seq *s = &iter->seq;
1048 1049
	struct mm_struct *mm = NULL;
	unsigned int i;
1050

1051
	trace_assign_type(field, iter->ent);
1052

1053
	trace_seq_puts(s, "<user stack trace>\n");
1054

1055
	if (tr->trace_flags & TRACE_ITER_SYM_USEROBJ) {
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
		struct task_struct *task;
		/*
		 * we do the lookup on the thread group leader,
		 * since individual threads might have already quit!
		 */
		rcu_read_lock();
		task = find_task_by_vpid(field->tgid);
		if (task)
			mm = get_task_mm(task);
		rcu_read_unlock();
	}

	for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
		unsigned long ip = field->caller[i];

		if (ip == ULONG_MAX || trace_seq_has_overflowed(s))
			break;

		trace_seq_puts(s, " => ");

		if (!ip) {
			trace_seq_puts(s, "??");
			trace_seq_putc(s, '\n');
			continue;
		}

		seq_print_user_ip(s, mm, ip, flags);
		trace_seq_putc(s, '\n');
	}

	if (mm)
		mmput(mm);
1088

1089
	return trace_handle_return(s);
1090 1091
}

1092
static struct trace_event_functions trace_user_stack_funcs = {
1093 1094 1095
	.trace		= trace_user_stack_print,
};

1096 1097 1098 1099 1100
static struct trace_event trace_user_stack_event = {
	.type		= TRACE_USER_STACK,
	.funcs		= &trace_user_stack_funcs,
};

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
/* TRACE_HWLAT */
static enum print_line_t
trace_hwlat_print(struct trace_iterator *iter, int flags,
		  struct trace_event *event)
{
	struct trace_entry *entry = iter->ent;
	struct trace_seq *s = &iter->seq;
	struct hwlat_entry *field;

	trace_assign_type(field, entry);

1112
	trace_seq_printf(s, "#%-5u inner/outer(us): %4llu/%-5llu ts:%ld.%09ld",
1113 1114 1115 1116 1117 1118
			 field->seqnum,
			 field->duration,
			 field->outer_duration,
			 field->timestamp.tv_sec,
			 field->timestamp.tv_nsec);

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
	if (field->nmi_count) {
		/*
		 * The generic sched_clock() is not NMI safe, thus
		 * we only record the count and not the time.
		 */
		if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK))
			trace_seq_printf(s, " nmi-total:%llu",
					 field->nmi_total_ts);
		trace_seq_printf(s, " nmi-count:%u",
				 field->nmi_count);
	}

	trace_seq_putc(s, '\n');

1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
	return trace_handle_return(s);
}


static enum print_line_t
trace_hwlat_raw(struct trace_iterator *iter, int flags,
		struct trace_event *event)
{
	struct hwlat_entry *field;
	struct trace_seq *s = &iter->seq;

	trace_assign_type(field, iter->ent);

	trace_seq_printf(s, "%llu %lld %ld %09ld %u\n",
			 field->duration,
			 field->outer_duration,
			 field->timestamp.tv_sec,
			 field->timestamp.tv_nsec,
			 field->seqnum);

	return trace_handle_return(s);
}

static struct trace_event_functions trace_hwlat_funcs = {
	.trace		= trace_hwlat_print,
	.raw		= trace_hwlat_raw,
};

static struct trace_event trace_hwlat_event = {
	.type		= TRACE_HWLAT,
	.funcs		= &trace_hwlat_funcs,
};

1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
/* TRACE_BPUTS */
static enum print_line_t
trace_bputs_print(struct trace_iterator *iter, int flags,
		   struct trace_event *event)
{
	struct trace_entry *entry = iter->ent;
	struct trace_seq *s = &iter->seq;
	struct bputs_entry *field;

	trace_assign_type(field, entry);

1177 1178 1179
	seq_print_ip_sym(s, field->ip, flags);
	trace_seq_puts(s, ": ");
	trace_seq_puts(s, field->str);
1180

1181
	return trace_handle_return(s);
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
}


static enum print_line_t
trace_bputs_raw(struct trace_iterator *iter, int flags,
		struct trace_event *event)
{
	struct bputs_entry *field;
	struct trace_seq *s = &iter->seq;

	trace_assign_type(field, iter->ent);

1194 1195
	trace_seq_printf(s, ": %lx : ", field->ip);
	trace_seq_puts(s, field->str);
1196

1197
	return trace_handle_return(s);
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
}

static struct trace_event_functions trace_bputs_funcs = {
	.trace		= trace_bputs_print,
	.raw		= trace_bputs_raw,
};

static struct trace_event trace_bputs_event = {
	.type		= TRACE_BPUTS,
	.funcs		= &trace_bputs_funcs,
};

1210
/* TRACE_BPRINT */
1211
static enum print_line_t
1212 1213
trace_bprint_print(struct trace_iterator *iter, int flags,
		   struct trace_event *event)
1214 1215 1216
{
	struct trace_entry *entry = iter->ent;
	struct trace_seq *s = &iter->seq;
1217
	struct bprint_entry *field;
1218 1219 1220

	trace_assign_type(field, entry);

1221 1222 1223
	seq_print_ip_sym(s, field->ip, flags);
	trace_seq_puts(s, ": ");
	trace_seq_bprintf(s, field->fmt, field->buf);
1224

1225
	return trace_handle_return(s);
1226 1227
}

1228

1229
static enum print_line_t
1230 1231
trace_bprint_raw(struct trace_iterator *iter, int flags,
		 struct trace_event *event)
1232
{
1233
	struct bprint_entry *field;
1234 1235
	struct trace_seq *s = &iter->seq;

1236
	trace_assign_type(field, iter->ent);
1237

1238 1239
	trace_seq_printf(s, ": %lx : ", field->ip);
	trace_seq_bprintf(s, field->fmt, field->buf);
1240

1241
	return trace_handle_return(s);
1242 1243
}

1244 1245 1246 1247
static struct trace_event_functions trace_bprint_funcs = {
	.trace		= trace_bprint_print,
	.raw		= trace_bprint_raw,
};
1248

1249 1250
static struct trace_event trace_bprint_event = {
	.type		= TRACE_BPRINT,
1251
	.funcs		= &trace_bprint_funcs,
1252 1253 1254 1255
};

/* TRACE_PRINT */
static enum print_line_t trace_print_print(struct trace_iterator *iter,
1256
					   int flags, struct trace_event *event)
1257 1258 1259 1260 1261 1262
{
	struct print_entry *field;
	struct trace_seq *s = &iter->seq;

	trace_assign_type(field, iter->ent);

1263 1264
	seq_print_ip_sym(s, field->ip, flags);
	trace_seq_printf(s, ": %s", field->buf);
1265

1266
	return trace_handle_return(s);
1267 1268
}

1269 1270
static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
					 struct trace_event *event)
1271 1272 1273 1274 1275
{
	struct print_entry *field;

	trace_assign_type(field, iter->ent);

1276
	trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf);
1277

1278
	return trace_handle_return(&iter->seq);
1279 1280
}

1281
static struct trace_event_functions trace_print_funcs = {
1282 1283
	.trace		= trace_print_print,
	.raw		= trace_print_raw,
1284 1285
};

1286 1287 1288 1289 1290
static struct trace_event trace_print_event = {
	.type	 	= TRACE_PRINT,
	.funcs		= &trace_print_funcs,
};

1291

1292 1293 1294 1295 1296 1297
static struct trace_event *events[] __initdata = {
	&trace_fn_event,
	&trace_ctx_event,
	&trace_wake_event,
	&trace_stack_event,
	&trace_user_stack_event,
1298
	&trace_bputs_event,
1299
	&trace_bprint_event,
1300
	&trace_print_event,
1301
	&trace_hwlat_event,
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
	NULL
};

__init static int init_events(void)
{
	struct trace_event *event;
	int i, ret;

	for (i = 0; events[i]; i++) {
		event = events[i];

1313
		ret = register_trace_event(event);
1314 1315 1316 1317 1318 1319 1320 1321 1322
		if (!ret) {
			printk(KERN_WARNING "event %d failed to register\n",
			       event->type);
			WARN_ON_ONCE(1);
		}
	}

	return 0;
}
1323
early_initcall(init_events);