trace_output.c 30.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * 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>
10
#include <linux/sched/clock.h>
11
#include <linux/sched/mm.h>
12 13 14 15 16 17

#include "trace_output.h"

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

18
DECLARE_RWSEM(trace_event_sem);
19

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

static int next_event_type = __TRACE_LAST_TYPE + 1;

24 25 26 27 28 29 30 31
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);

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

34
	return trace_handle_return(s);
35 36
}

37 38 39 40 41 42 43 44
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);

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

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

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);

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

60
	return trace_handle_return(s);
61 62
}

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

	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;
81
		if (!first && delim)
82
			trace_seq_puts(p, delim);
83 84
		else
			first = 0;
85 86 87 88 89
		trace_seq_puts(p, str);
	}

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

	trace_seq_putc(p, 0);

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

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

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

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

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

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

120 121
	trace_seq_putc(p, 0);

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

126
#if BITS_PER_LONG == 32
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
const char *
trace_print_flags_seq_u64(struct trace_seq *p, const char *delim,
		      unsigned long long flags,
		      const struct trace_print_flags_u64 *flag_array)
{
	unsigned long long mask;
	const char *str;
	const char *ret = trace_seq_buffer_ptr(p);
	int i, first = 1;

	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;
		if (!first && delim)
			trace_seq_puts(p, delim);
		else
			first = 0;
		trace_seq_puts(p, str);
	}

	/* check for left over flags */
	if (flags) {
		if (!first && delim)
			trace_seq_puts(p, delim);
		trace_seq_printf(p, "0x%llx", flags);
	}

	trace_seq_putc(p, 0);

	return ret;
}
EXPORT_SYMBOL(trace_print_flags_seq_u64);

165
const char *
166
trace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
167 168 169
			 const struct trace_print_flags_u64 *symbol_array)
{
	int i;
170
	const char *ret = trace_seq_buffer_ptr(p);
171 172 173 174 175 176 177 178 179 180

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

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

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

181
	if (ret == (const char *)(trace_seq_buffer_ptr(p)))
182 183 184 185 186 187
		trace_seq_printf(p, "0x%llx", val);

	trace_seq_putc(p, 0);

	return ret;
}
188
EXPORT_SYMBOL(trace_print_symbols_seq_u64);
189 190
#endif

191
const char *
192 193
trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
			unsigned int bitmask_size)
194
{
195
	const char *ret = trace_seq_buffer_ptr(p);
196 197 198 199 200 201

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

	return ret;
}
202
EXPORT_SYMBOL_GPL(trace_print_bitmask_seq);
203

204 205 206 207 208 209 210 211 212 213 214
/**
 * trace_print_hex_seq - print buffer as hex sequence
 * @p: trace seq struct to write to
 * @buf: The buffer to print
 * @buf_len: Length of @buf in bytes
 * @concatenate: Print @buf as single hex string or with spacing
 *
 * Prints the passed buffer as a hex sequence either as a whole,
 * single hex string if @concatenate is true or with spacing after
 * each byte in case @concatenate is false.
 */
215
const char *
216
trace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len,
217
		    bool concatenate)
218 219
{
	int i;
220
	const char *ret = trace_seq_buffer_ptr(p);
221 222

	for (i = 0; i < buf_len; i++)
223
		trace_seq_printf(p, "%s%2.2x", concatenate || i == 0 ? "" : " ",
224
				 buf[i]);
225 226 227 228
	trace_seq_putc(p, 0);

	return ret;
}
229
EXPORT_SYMBOL(trace_print_hex_seq);
230

231
const char *
232 233
trace_print_array_seq(struct trace_seq *p, const void *buf, int count,
		      size_t el_size)
234 235 236 237
{
	const char *ret = trace_seq_buffer_ptr(p);
	const char *prefix = "";
	void *ptr = (void *)buf;
238
	size_t buf_len = count * el_size;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

	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;
}
274
EXPORT_SYMBOL(trace_print_array_seq);
275

276 277
int trace_raw_output_prep(struct trace_iterator *iter,
			  struct trace_event *trace_event)
278
{
279
	struct trace_event_call *event;
280 281 282 283
	struct trace_seq *s = &iter->seq;
	struct trace_seq *p = &iter->tmp_seq;
	struct trace_entry *entry;

284
	event = container_of(trace_event, struct trace_event_call, event);
285 286 287 288 289 290 291 292
	entry = iter->ent;

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

	trace_seq_init(p);
293
	trace_seq_printf(s, "%s: ", trace_event_name(event));
294

295
	return trace_handle_return(s);
296
}
297
EXPORT_SYMBOL(trace_raw_output_prep);
298

299 300
static int trace_output_raw(struct trace_iterator *iter, char *name,
			    char *fmt, va_list ap)
301 302 303
{
	struct trace_seq *s = &iter->seq;

304 305
	trace_seq_printf(s, "%s: ", name);
	trace_seq_vprintf(s, fmt, ap);
306

307
	return trace_handle_return(s);
308 309
}

310
int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...)
311 312 313 314 315
{
	va_list ap;
	int ret;

	va_start(ap, fmt);
316
	ret = trace_output_raw(iter, name, fmt, ap);
317 318 319 320
	va_end(ap);

	return ret;
}
321
EXPORT_SYMBOL_GPL(trace_output_call);
322

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
#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 */

340
static void
341 342 343
seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
{
	char str[KSYM_SYMBOL_LEN];
344
#ifdef CONFIG_KALLSYMS
345 346 347 348 349 350
	const char *name;

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

	name = kretprobed(str);

351 352 353 354
	if (name && strlen(name)) {
		trace_seq_printf(s, fmt, name);
		return;
	}
355
#endif
356 357
	snprintf(str, KSYM_SYMBOL_LEN, "0x%08lx", address);
	trace_seq_printf(s, fmt, str);
358 359
}

360
static void
361 362 363 364
seq_print_sym_offset(struct trace_seq *s, const char *fmt,
		     unsigned long address)
{
	char str[KSYM_SYMBOL_LEN];
365
#ifdef CONFIG_KALLSYMS
366 367 368 369 370
	const char *name;

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

371 372 373 374
	if (name && strlen(name)) {
		trace_seq_printf(s, fmt, name);
		return;
	}
375
#endif
376 377
	snprintf(str, KSYM_SYMBOL_LEN, "0x%08lx", address);
	trace_seq_printf(s, fmt, str);
378 379 380 381 382 383 384 385
}

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

386 387
static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
			     unsigned long ip, unsigned long sym_flags)
388 389 390 391 392
{
	struct file *file = NULL;
	unsigned long vmstart = 0;
	int ret = 1;

393 394 395
	if (s->full)
		return 0;

396 397 398 399 400 401 402 403 404 405 406 407
	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)
408 409
				trace_seq_printf(s, "[+0x%lx]",
						 ip - vmstart);
410 411 412 413
		}
		up_read(&mm->mmap_sem);
	}
	if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
414 415
		trace_seq_printf(s, " <" IP_FMT ">", ip);
	return !trace_seq_has_overflowed(s);
416 417 418 419 420
}

int
seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
{
421 422 423 424
	if (!ip) {
		trace_seq_putc(s, '0');
		goto out;
	}
425 426

	if (sym_flags & TRACE_ITER_SYM_OFFSET)
427
		seq_print_sym_offset(s, "%s", ip);
428
	else
429
		seq_print_sym_short(s, "%s", ip);
430 431

	if (sym_flags & TRACE_ITER_SYM_ADDR)
432 433 434 435
		trace_seq_printf(s, " <" IP_FMT ">", ip);

 out:
	return !trace_seq_has_overflowed(s);
436 437
}

438 439 440 441 442 443
/**
 * 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
444
 * count.
445 446
 */
int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
447
{
448 449 450 451 452
	char hardsoft_irq;
	char need_resched;
	char irqs_off;
	int hardirq;
	int softirq;
453
	int nmi;
454

455
	nmi = entry->flags & TRACE_FLAG_NMI;
456 457
	hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
	softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
458

459 460 461 462
	irqs_off =
		(entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
		(entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' :
		'.';
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

	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;
	}

480
	hardsoft_irq =
481 482
		(nmi && hardirq)     ? 'Z' :
		nmi                  ? 'z' :
483
		(hardirq && softirq) ? 'H' :
484 485 486
		hardirq              ? 'h' :
		softirq              ? 's' :
		                       '.' ;
487

488 489
	trace_seq_printf(s, "%c%c%c",
			 irqs_off, need_resched, hardsoft_irq);
490

491
	if (entry->preempt_count)
492
		trace_seq_printf(s, "%x", entry->preempt_count);
493
	else
494
		trace_seq_putc(s, '.');
495

496
	return !trace_seq_has_overflowed(s);
497 498
}

499 500 501 502 503 504 505
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);

506 507
	trace_seq_printf(s, "%8.8s-%-5d %3d",
			 comm, entry->pid, cpu);
508 509 510 511

	return trace_print_lat_fmt(s, entry);
}

512 513 514 515 516 517 518 519
#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 */
520 521
	MARK(100000000ULL	, '@'), /* 100 msec */
	MARK(10000000ULL	, '*'), /* 10 msec */
522 523 524 525 526 527 528 529 530 531 532 533
	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++) {
534
		if (d > mark[i].val)
535 536 537 538 539
			break;
	}

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

541
static int
542
lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
543
{
544 545
	struct trace_array *tr = iter->tr;
	unsigned long verbose = tr->trace_flags & TRACE_ITER_VERBOSE;
546
	unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
547
	unsigned long long abs_ts = iter->ts - iter->trace_buffer->time_start;
548 549 550 551 552 553 554 555 556 557 558 559 560 561
	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;

562 563 564 565 566 567
		trace_seq_printf(
			s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
			ns2usecs(iter->ts),
			abs_msec, abs_usec,
			rel_msec, rel_usec);

568
	} else if (verbose && !in_ns) {
569 570 571 572
		trace_seq_printf(
			s, "[%016llx] %lld (+%lld): ",
			iter->ts, abs_ts, rel_ts);

573
	} else if (!verbose && in_ns) {
574 575 576
		trace_seq_printf(
			s, " %4lldus%c: ",
			abs_ts,
577
			trace_find_mark(rel_ts * NSEC_PER_USEC));
578

579
	} else { /* !verbose && !in_ns */
580
		trace_seq_printf(s, " %4lld: ", abs_ts);
581
	}
582 583

	return !trace_seq_has_overflowed(s);
584 585 586 587
}

int trace_print_context(struct trace_iterator *iter)
{
588
	struct trace_array *tr = iter->tr;
589 590
	struct trace_seq *s = &iter->seq;
	struct trace_entry *entry = iter->ent;
591 592
	unsigned long long t;
	unsigned long secs, usec_rem;
593 594 595
	char comm[TASK_COMM_LEN];

	trace_find_cmdline(entry->pid, comm);
596

597
	trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
598

599 600 601 602 603 604 605 606 607
	if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
		unsigned int tgid = trace_find_tgid(entry->pid);

		if (!tgid)
			trace_seq_printf(s, "(-----) ");
		else
			trace_seq_printf(s, "(%5d) ", tgid);
	}

608 609
	trace_seq_printf(s, "[%03d] ", iter->cpu);

610
	if (tr->trace_flags & TRACE_ITER_IRQ_INFO)
611
		trace_print_lat_fmt(s, entry);
612

613 614 615 616
	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;
617
		trace_seq_printf(s, " %5lu.%06lu: ", secs, usec_rem);
618
	} else
619 620 621
		trace_seq_printf(s, " %12llu: ", iter->ts);

	return !trace_seq_has_overflowed(s);
622 623 624 625
}

int trace_print_lat_context(struct trace_iterator *iter)
{
626
	struct trace_array *tr = iter->tr;
627 628
	/* trace_find_next_entry will reset ent_size */
	int ent_size = iter->ent_size;
629
	struct trace_seq *s = &iter->seq;
630
	u64 next_ts;
631 632 633
	struct trace_entry *entry = iter->ent,
			   *next_entry = trace_find_next_entry(iter, NULL,
							       &next_ts);
634
	unsigned long verbose = (tr->trace_flags & TRACE_ITER_VERBOSE);
635

636 637 638
	/* Restore the original ent_size */
	iter->ent_size = ent_size;

639 640 641 642
	if (!next_entry)
		next_ts = iter->ts;

	if (verbose) {
643 644 645 646
		char comm[TASK_COMM_LEN];

		trace_find_cmdline(entry->pid, comm);

647 648 649 650
		trace_seq_printf(
			s, "%16s %5d %3d %d %08x %08lx ",
			comm, entry->pid, iter->cpu, entry->flags,
			entry->preempt_count, iter->idx);
651
	} else {
652
		lat_print_generic(s, entry, iter->cpu);
653 654
	}

655
	lat_print_timestamp(iter, next_ts);
656

657
	return !trace_seq_has_overflowed(s);
658 659
}

660 661 662 663 664
/**
 * ftrace_find_event - find a registered event
 * @type: the type of event to look for
 *
 * Returns an event of type @type otherwise NULL
665
 * Called with trace_event_read_lock() held.
666 667 668 669 670 671 672 673
 */
struct trace_event *ftrace_find_event(int type)
{
	struct trace_event *event;
	unsigned key;

	key = type & (EVENT_HASHSIZE - 1);

674
	hlist_for_each_entry(event, &event_hash[key], node) {
675 676 677 678 679 680 681
		if (event->type == type)
			return event;
	}

	return NULL;
}

682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
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??? */
705
	if ((last + 1) > TRACE_EVENT_TYPE_MAX)
706 707 708 709 710 711
		return 0;

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

712 713
void trace_event_read_lock(void)
{
714
	down_read(&trace_event_sem);
715 716 717 718
}

void trace_event_read_unlock(void)
{
719
	up_read(&trace_event_sem);
720 721
}

722
/**
723
 * register_trace_event - register output for an event type
724 725 726 727 728 729 730 731 732 733 734 735 736
 * @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.
 */
737
int register_trace_event(struct trace_event *event)
738 739 740 741
{
	unsigned key;
	int ret = 0;

742
	down_write(&trace_event_sem);
743

744
	if (WARN_ON(!event))
745 746
		goto out;

747 748 749
	if (WARN_ON(!event->funcs))
		goto out;

750 751 752
	INIT_LIST_HEAD(&event->list);

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

755
		if (next_event_type > TRACE_EVENT_TYPE_MAX) {
756 757 758 759 760 761

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

		} else {
762

763 764 765 766 767 768 769 770 771 772
			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) {
773 774 775
		printk(KERN_WARNING "Need to add type to trace.h\n");
		WARN_ON(1);
		goto out;
776 777 778 779 780
	} else {
		/* Is this event already used */
		if (ftrace_find_event(event->type))
			goto out;
	}
781

782 783 784 785 786 787 788 789
	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;
790

791 792
	key = event->type & (EVENT_HASHSIZE - 1);

793
	hlist_add_head(&event->node, &event_hash[key]);
794 795 796

	ret = event->type;
 out:
797
	up_write(&trace_event_sem);
798 799 800

	return ret;
}
801
EXPORT_SYMBOL_GPL(register_trace_event);
802

803
/*
804
 * Used by module code with the trace_event_sem held for write.
805
 */
806
int __unregister_trace_event(struct trace_event *event)
807 808 809 810 811 812
{
	hlist_del(&event->node);
	list_del(&event->list);
	return 0;
}

813
/**
814
 * unregister_trace_event - remove a no longer used event
815 816
 * @event: the event to remove
 */
817
int unregister_trace_event(struct trace_event *event)
818
{
819
	down_write(&trace_event_sem);
820
	__unregister_trace_event(event);
821
	up_write(&trace_event_sem);
822 823 824

	return 0;
}
825
EXPORT_SYMBOL_GPL(unregister_trace_event);
826 827 828 829 830

/*
 * Standard events
 */

831 832
enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
				  struct trace_event *event)
833
{
834
	trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type);
835

836
	return trace_handle_return(&iter->seq);
837 838 839
}

/* TRACE_FN */
840 841
static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
					struct trace_event *event)
842 843
{
	struct ftrace_entry *field;
844
	struct trace_seq *s = &iter->seq;
845

846
	trace_assign_type(field, iter->ent);
847

848
	seq_print_ip_sym(s, field->ip, flags);
849 850

	if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
851 852
		trace_seq_puts(s, " <-");
		seq_print_ip_sym(s, field->parent_ip, flags);
853 854
	}

855
	trace_seq_putc(s, '\n');
856

857
	return trace_handle_return(s);
858 859
}

860 861
static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
				      struct trace_event *event)
862 863 864
{
	struct ftrace_entry *field;

865
	trace_assign_type(field, iter->ent);
866

867 868 869
	trace_seq_printf(&iter->seq, "%lx %lx\n",
			 field->ip,
			 field->parent_ip);
870

871
	return trace_handle_return(&iter->seq);
872 873
}

874 875
static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
				      struct trace_event *event)
876 877
{
	struct ftrace_entry *field;
878
	struct trace_seq *s = &iter->seq;
879

880
	trace_assign_type(field, iter->ent);
881

882 883
	SEQ_PUT_HEX_FIELD(s, field->ip);
	SEQ_PUT_HEX_FIELD(s, field->parent_ip);
884

885
	return trace_handle_return(s);
886 887
}

888 889
static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
				      struct trace_event *event)
890 891
{
	struct ftrace_entry *field;
892
	struct trace_seq *s = &iter->seq;
893

894
	trace_assign_type(field, iter->ent);
895

896 897
	SEQ_PUT_FIELD(s, field->ip);
	SEQ_PUT_FIELD(s, field->parent_ip);
898

899
	return trace_handle_return(s);
900 901
}

902
static struct trace_event_functions trace_fn_funcs = {
903 904 905 906 907 908
	.trace		= trace_fn_trace,
	.raw		= trace_fn_raw,
	.hex		= trace_fn_hex,
	.binary		= trace_fn_bin,
};

909 910 911 912 913
static struct trace_event trace_fn_event = {
	.type		= TRACE_FN,
	.funcs		= &trace_fn_funcs,
};

914
/* TRACE_CTX an TRACE_WAKE */
915 916
static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
					     char *delim)
917 918
{
	struct ctx_switch_entry *field;
919
	char comm[TASK_COMM_LEN];
920 921
	int S, T;

922

923
	trace_assign_type(field, iter->ent);
924

925 926
	T = __task_state_to_char(field->next_state);
	S = __task_state_to_char(field->prev_state);
927
	trace_find_cmdline(field->next_pid, comm);
928 929 930 931 932 933 934 935 936 937 938
	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);
939 940
}

941 942
static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
					 struct trace_event *event)
943
{
944
	return trace_ctxwake_print(iter, "==>");
945 946
}

947
static enum print_line_t trace_wake_print(struct trace_iterator *iter,
948
					  int flags, struct trace_event *event)
949
{
950
	return trace_ctxwake_print(iter, "  +");
951 952
}

953
static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
954 955 956 957
{
	struct ctx_switch_entry *field;
	int T;

958
	trace_assign_type(field, iter->ent);
959 960

	if (!S)
961 962
		S = __task_state_to_char(field->prev_state);
	T = __task_state_to_char(field->next_state);
963 964 965 966 967 968 969 970 971 972
	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);
973 974
}

975 976
static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
				       struct trace_event *event)
977
{
978
	return trace_ctxwake_raw(iter, 0);
979 980
}

981 982
static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
					struct trace_event *event)
983
{
984
	return trace_ctxwake_raw(iter, '+');
985 986 987
}


988
static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
989 990
{
	struct ctx_switch_entry *field;
991
	struct trace_seq *s = &iter->seq;
992 993
	int T;

994
	trace_assign_type(field, iter->ent);
995 996

	if (!S)
997 998
		S = __task_state_to_char(field->prev_state);
	T = __task_state_to_char(field->next_state);
999

1000 1001 1002 1003 1004 1005 1006
	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);
1007

1008
	return trace_handle_return(s);
1009 1010
}

1011 1012
static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
				       struct trace_event *event)
1013
{
1014
	return trace_ctxwake_hex(iter, 0);
1015 1016
}

1017 1018
static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
					struct trace_event *event)
1019
{
1020
	return trace_ctxwake_hex(iter, '+');
1021 1022
}

1023
static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1024
					   int flags, struct trace_event *event)
1025 1026
{
	struct ctx_switch_entry *field;
1027
	struct trace_seq *s = &iter->seq;
1028

1029
	trace_assign_type(field, iter->ent);
1030

1031 1032 1033 1034 1035 1036 1037
	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);
1038

1039
	return trace_handle_return(s);
1040 1041
}

1042
static struct trace_event_functions trace_ctx_funcs = {
1043 1044 1045 1046 1047 1048
	.trace		= trace_ctx_print,
	.raw		= trace_ctx_raw,
	.hex		= trace_ctx_hex,
	.binary		= trace_ctxwake_bin,
};

1049 1050 1051 1052 1053 1054
static struct trace_event trace_ctx_event = {
	.type		= TRACE_CTX,
	.funcs		= &trace_ctx_funcs,
};

static struct trace_event_functions trace_wake_funcs = {
1055 1056 1057 1058 1059 1060
	.trace		= trace_wake_print,
	.raw		= trace_wake_raw,
	.hex		= trace_wake_hex,
	.binary		= trace_ctxwake_bin,
};

1061 1062 1063 1064 1065
static struct trace_event trace_wake_event = {
	.type		= TRACE_WAKE,
	.funcs		= &trace_wake_funcs,
};

1066 1067
/* TRACE_STACK */

1068
static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1069
					   int flags, struct trace_event *event)
1070 1071
{
	struct stack_entry *field;
1072
	struct trace_seq *s = &iter->seq;
1073 1074
	unsigned long *p;
	unsigned long *end;
1075

1076
	trace_assign_type(field, iter->ent);
1077
	end = (unsigned long *)((long)iter->ent + iter->ent_size);
1078

1079
	trace_seq_puts(s, "<stack trace>\n");
1080 1081

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

1083 1084
		if (trace_seq_has_overflowed(s))
			break;
1085

1086 1087 1088 1089
		trace_seq_puts(s, " => ");
		seq_print_ip_sym(s, *p, flags);
		trace_seq_putc(s, '\n');
	}
1090

1091
	return trace_handle_return(s);
1092 1093
}

1094
static struct trace_event_functions trace_stack_funcs = {
1095 1096 1097
	.trace		= trace_stack_print,
};

1098 1099 1100 1101 1102
static struct trace_event trace_stack_event = {
	.type		= TRACE_STACK,
	.funcs		= &trace_stack_funcs,
};

1103
/* TRACE_USER_STACK */
1104
static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1105
						int flags, struct trace_event *event)
1106
{
1107
	struct trace_array *tr = iter->tr;
1108
	struct userstack_entry *field;
1109
	struct trace_seq *s = &iter->seq;
1110 1111
	struct mm_struct *mm = NULL;
	unsigned int i;
1112

1113
	trace_assign_type(field, iter->ent);
1114

1115
	trace_seq_puts(s, "<user stack trace>\n");
1116

1117
	if (tr->trace_flags & TRACE_ITER_SYM_USEROBJ) {
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
		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);
1150

1151
	return trace_handle_return(s);
1152 1153
}

1154
static struct trace_event_functions trace_user_stack_funcs = {
1155 1156 1157
	.trace		= trace_user_stack_print,
};

1158 1159 1160 1161 1162
static struct trace_event trace_user_stack_event = {
	.type		= TRACE_USER_STACK,
	.funcs		= &trace_user_stack_funcs,
};

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
/* 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);

1174
	trace_seq_printf(s, "#%-5u inner/outer(us): %4llu/%-5llu ts:%lld.%09ld",
1175 1176 1177
			 field->seqnum,
			 field->duration,
			 field->outer_duration,
1178
			 (long long)field->timestamp.tv_sec,
1179 1180
			 field->timestamp.tv_nsec);

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
	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');

1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	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);

1208
	trace_seq_printf(s, "%llu %lld %lld %09ld %u\n",
1209 1210
			 field->duration,
			 field->outer_duration,
1211
			 (long long)field->timestamp.tv_sec,
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
			 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,
};

1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
/* 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);

1239 1240 1241
	seq_print_ip_sym(s, field->ip, flags);
	trace_seq_puts(s, ": ");
	trace_seq_puts(s, field->str);
1242

1243
	return trace_handle_return(s);
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
}


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);

1256 1257
	trace_seq_printf(s, ": %lx : ", field->ip);
	trace_seq_puts(s, field->str);
1258

1259
	return trace_handle_return(s);
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
}

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,
};

1272
/* TRACE_BPRINT */
1273
static enum print_line_t
1274 1275
trace_bprint_print(struct trace_iterator *iter, int flags,
		   struct trace_event *event)
1276 1277 1278
{
	struct trace_entry *entry = iter->ent;
	struct trace_seq *s = &iter->seq;
1279
	struct bprint_entry *field;
1280 1281 1282

	trace_assign_type(field, entry);

1283 1284 1285
	seq_print_ip_sym(s, field->ip, flags);
	trace_seq_puts(s, ": ");
	trace_seq_bprintf(s, field->fmt, field->buf);
1286

1287
	return trace_handle_return(s);
1288 1289
}

1290

1291
static enum print_line_t
1292 1293
trace_bprint_raw(struct trace_iterator *iter, int flags,
		 struct trace_event *event)
1294
{
1295
	struct bprint_entry *field;
1296 1297
	struct trace_seq *s = &iter->seq;

1298
	trace_assign_type(field, iter->ent);
1299

1300 1301
	trace_seq_printf(s, ": %lx : ", field->ip);
	trace_seq_bprintf(s, field->fmt, field->buf);
1302

1303
	return trace_handle_return(s);
1304 1305
}

1306 1307 1308 1309
static struct trace_event_functions trace_bprint_funcs = {
	.trace		= trace_bprint_print,
	.raw		= trace_bprint_raw,
};
1310

1311 1312
static struct trace_event trace_bprint_event = {
	.type		= TRACE_BPRINT,
1313
	.funcs		= &trace_bprint_funcs,
1314 1315 1316 1317
};

/* TRACE_PRINT */
static enum print_line_t trace_print_print(struct trace_iterator *iter,
1318
					   int flags, struct trace_event *event)
1319 1320 1321 1322 1323 1324
{
	struct print_entry *field;
	struct trace_seq *s = &iter->seq;

	trace_assign_type(field, iter->ent);

1325 1326
	seq_print_ip_sym(s, field->ip, flags);
	trace_seq_printf(s, ": %s", field->buf);
1327

1328
	return trace_handle_return(s);