trace_output.c 29.0 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_mutex);
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
int trace_print_seq(struct seq_file *m, struct trace_seq *s)
24 25
{
	int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
26 27 28
	int ret;

	ret = seq_write(m, s->buffer, len);
29

30 31 32 33 34 35
	/*
	 * Only reset this buffer if we successfully wrote to the
	 * seq_file buffer.
	 */
	if (!ret)
		trace_seq_init(s);
36

37
	return ret;
38 39
}

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
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;
	int ret;

	trace_assign_type(field, entry);

	ret = trace_seq_bprintf(s, field->fmt, field->buf);
	if (!ret)
		return TRACE_TYPE_PARTIAL_LINE;

	return TRACE_TYPE_HANDLED;
}

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;
	int ret;

	trace_assign_type(field, entry);

	ret = trace_seq_printf(s, "%s", field->buf);
	if (!ret)
		return TRACE_TYPE_PARTIAL_LINE;

	return TRACE_TYPE_HANDLED;
}

72 73 74 75 76
/**
 * trace_seq_printf - sequence printing of trace information
 * @s: trace sequence descriptor
 * @fmt: printf format string
 *
77 78 79
 * It returns 0 if the trace oversizes the buffer's free
 * space, 1 otherwise.
 *
80 81 82 83 84 85 86 87 88 89 90 91 92
 * The tracer may use either sequence operations or its own
 * copy to user routines. To simplify formating of a trace
 * trace_seq_printf is used to store strings into a special
 * buffer (@s). Then the output may be either used by
 * the sequencer or pulled into another buffer.
 */
int
trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
{
	int len = (PAGE_SIZE - 1) - s->len;
	va_list ap;
	int ret;

93
	if (s->full || !len)
94 95 96 97 98 99 100
		return 0;

	va_start(ap, fmt);
	ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
	va_end(ap);

	/* If we can't write it all, don't bother writing anything */
101 102
	if (ret >= len) {
		s->full = 1;
103
		return 0;
104
	}
105 106 107

	s->len += ret;

108
	return 1;
109
}
110
EXPORT_SYMBOL_GPL(trace_seq_printf);
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/**
 * trace_seq_vprintf - sequence printing of trace information
 * @s: trace sequence descriptor
 * @fmt: printf format string
 *
 * The tracer may use either sequence operations or its own
 * copy to user routines. To simplify formating of a trace
 * trace_seq_printf is used to store strings into a special
 * buffer (@s). Then the output may be either used by
 * the sequencer or pulled into another buffer.
 */
int
trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
{
	int len = (PAGE_SIZE - 1) - s->len;
	int ret;

129
	if (s->full || !len)
130 131 132 133 134
		return 0;

	ret = vsnprintf(s->buffer + s->len, len, fmt, args);

	/* If we can't write it all, don't bother writing anything */
135 136
	if (ret >= len) {
		s->full = 1;
137
		return 0;
138
	}
139 140 141 142 143 144 145

	s->len += ret;

	return len;
}
EXPORT_SYMBOL_GPL(trace_seq_vprintf);

146
int trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
147 148 149 150
{
	int len = (PAGE_SIZE - 1) - s->len;
	int ret;

151
	if (s->full || !len)
152 153 154 155 156
		return 0;

	ret = bstr_printf(s->buffer + s->len, len, fmt, binary);

	/* If we can't write it all, don't bother writing anything */
157 158
	if (ret >= len) {
		s->full = 1;
159
		return 0;
160
	}
161 162 163 164 165 166

	s->len += ret;

	return len;
}

167 168 169 170 171 172 173 174 175 176 177 178 179 180
/**
 * trace_seq_puts - trace sequence printing of simple string
 * @s: trace sequence descriptor
 * @str: simple string to record
 *
 * The tracer may use either the sequence operations or its own
 * copy to user routines. This function records a simple string
 * into a special buffer (@s) for later retrieval by a sequencer
 * or other mechanism.
 */
int trace_seq_puts(struct trace_seq *s, const char *str)
{
	int len = strlen(str);

181
	if (s->full)
182 183
		return 0;

184 185 186 187 188
	if (len > ((PAGE_SIZE - 1) - s->len)) {
		s->full = 1;
		return 0;
	}

189 190 191 192 193 194 195 196
	memcpy(s->buffer + s->len, str, len);
	s->len += len;

	return len;
}

int trace_seq_putc(struct trace_seq *s, unsigned char c)
{
197
	if (s->full)
198 199
		return 0;

200 201 202 203 204
	if (s->len >= (PAGE_SIZE - 1)) {
		s->full = 1;
		return 0;
	}

205 206 207 208
	s->buffer[s->len++] = c;

	return 1;
}
209
EXPORT_SYMBOL(trace_seq_putc);
210

211
int trace_seq_putmem(struct trace_seq *s, const void *mem, size_t len)
212
{
213
	if (s->full)
214 215
		return 0;

216 217 218 219 220
	if (len > ((PAGE_SIZE - 1) - s->len)) {
		s->full = 1;
		return 0;
	}

221 222 223 224 225 226
	memcpy(s->buffer + s->len, mem, len);
	s->len += len;

	return len;
}

227
int trace_seq_putmem_hex(struct trace_seq *s, const void *mem, size_t len)
228 229
{
	unsigned char hex[HEX_CHARS];
230
	const unsigned char *data = mem;
231 232
	int i, j;

233 234 235
	if (s->full)
		return 0;

236 237 238 239 240 241 242 243 244 245 246 247 248
#ifdef __BIG_ENDIAN
	for (i = 0, j = 0; i < len; i++) {
#else
	for (i = len-1, j = 0; i >= 0; i--) {
#endif
		hex[j++] = hex_asc_hi(data[i]);
		hex[j++] = hex_asc_lo(data[i]);
	}
	hex[j++] = ' ';

	return trace_seq_putmem(s, hex, j);
}

249 250 251 252
void *trace_seq_reserve(struct trace_seq *s, size_t len)
{
	void *ret;

253
	if (s->full)
254
		return NULL;
255 256 257

	if (len > ((PAGE_SIZE - 1) - s->len)) {
		s->full = 1;
258
		return NULL;
259
	}
260 261 262 263 264 265 266

	ret = s->buffer + s->len;
	s->len += len;

	return ret;
}

267
int trace_seq_path(struct trace_seq *s, const struct path *path)
268 269 270
{
	unsigned char *p;

271
	if (s->full)
272
		return 0;
273 274 275 276 277 278

	if (s->len >= (PAGE_SIZE - 1)) {
		s->full = 1;
		return 0;
	}

279 280 281 282 283 284 285 286 287 288 289 290
	p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
	if (!IS_ERR(p)) {
		p = mangle_path(s->buffer + s->len, p, "\n");
		if (p) {
			s->len = p - s->buffer;
			return 1;
		}
	} else {
		s->buffer[s->len++] = '?';
		return 1;
	}

291
	s->full = 1;
292 293 294
	return 0;
}

295 296 297 298 299 300 301
const char *
ftrace_print_flags_seq(struct trace_seq *p, const char *delim,
		       unsigned long flags,
		       const struct trace_print_flags *flag_array)
{
	unsigned long mask;
	const char *str;
302
	const char *ret = p->buffer + p->len;
303
	int i, first = 1;
304 305 306 307 308 309 310 311 312

	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;
313
		if (!first && delim)
314
			trace_seq_puts(p, delim);
315 316
		else
			first = 0;
317 318 319 320 321
		trace_seq_puts(p, str);
	}

	/* check for left over flags */
	if (flags) {
322
		if (!first && delim)
323 324 325 326 327 328
			trace_seq_puts(p, delim);
		trace_seq_printf(p, "0x%lx", flags);
	}

	trace_seq_putc(p, 0);

329
	return ret;
330
}
331
EXPORT_SYMBOL(ftrace_print_flags_seq);
332

333 334 335 336 337
const char *
ftrace_print_symbols_seq(struct trace_seq *p, unsigned long val,
			 const struct trace_print_flags *symbol_array)
{
	int i;
338
	const char *ret = p->buffer + p->len;
339 340 341 342 343 344 345 346 347 348

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

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

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

349
	if (ret == (const char *)(p->buffer + p->len))
350 351 352 353
		trace_seq_printf(p, "0x%lx", val);
		
	trace_seq_putc(p, 0);

354
	return ret;
355
}
356
EXPORT_SYMBOL(ftrace_print_symbols_seq);
357

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
#if BITS_PER_LONG == 32
const char *
ftrace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
			 const struct trace_print_flags_u64 *symbol_array)
{
	int i;
	const char *ret = p->buffer + p->len;

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

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

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

375
	if (ret == (const char *)(p->buffer + p->len))
376 377 378 379 380 381 382 383 384
		trace_seq_printf(p, "0x%llx", val);

	trace_seq_putc(p, 0);

	return ret;
}
EXPORT_SYMBOL(ftrace_print_symbols_seq_u64);
#endif

K
Kei Tokunaga 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
const char *
ftrace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len)
{
	int i;
	const char *ret = p->buffer + p->len;

	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;
}
EXPORT_SYMBOL(ftrace_print_hex_seq);

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
int ftrace_raw_output_prep(struct trace_iterator *iter,
			   struct trace_event *trace_event)
{
	struct ftrace_event_call *event;
	struct trace_seq *s = &iter->seq;
	struct trace_seq *p = &iter->tmp_seq;
	struct trace_entry *entry;
	int ret;

	event = container_of(trace_event, struct ftrace_event_call, event);
	entry = iter->ent;

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

	trace_seq_init(p);
	ret = trace_seq_printf(s, "%s: ", event->name);
	if (!ret)
		return TRACE_TYPE_PARTIAL_LINE;

	return 0;
}
EXPORT_SYMBOL(ftrace_raw_output_prep);

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
#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 */

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

	return trace_seq_printf(s, fmt, name);
#endif
	return 1;
}

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

	return trace_seq_printf(s, fmt, name);
#endif
	return 1;
}

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

int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
		      unsigned long ip, unsigned long sym_flags)
{
	struct file *file = NULL;
	unsigned long vmstart = 0;
	int ret = 1;

488 489 490
	if (s->full)
		return 0;

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	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)
				ret = trace_seq_printf(s, "[+0x%lx]",
						       ip - vmstart);
		}
		up_read(&mm->mmap_sem);
	}
	if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
		ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
	return ret;
}

int
seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
		      unsigned long sym_flags)
{
	struct mm_struct *mm = NULL;
	int ret = 1;
	unsigned int i;

	if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
		struct task_struct *task;
		/*
		 * we do the lookup on the thread group leader,
		 * since individual threads might have already quit!
		 */
		rcu_read_lock();
528
		task = find_task_by_vpid(entry->tgid);
529 530 531 532 533 534 535 536 537 538
		if (task)
			mm = get_task_mm(task);
		rcu_read_unlock();
	}

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

		if (ip == ULONG_MAX || !ret)
			break;
539 540
		if (ret)
			ret = trace_seq_puts(s, " => ");
541 542 543
		if (!ip) {
			if (ret)
				ret = trace_seq_puts(s, "??");
544 545
			if (ret)
				ret = trace_seq_puts(s, "\n");
546 547 548 549 550 551
			continue;
		}
		if (!ret)
			break;
		if (ret)
			ret = seq_print_user_ip(s, mm, ip, sym_flags);
552
		ret = trace_seq_puts(s, "\n");
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
	}

	if (mm)
		mmput(mm);
	return ret;
}

int
seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
{
	int ret;

	if (!ip)
		return trace_seq_printf(s, "0");

	if (sym_flags & TRACE_ITER_SYM_OFFSET)
		ret = seq_print_sym_offset(s, "%s", ip);
	else
		ret = seq_print_sym_short(s, "%s", ip);

	if (!ret)
		return 0;

	if (sym_flags & TRACE_ITER_SYM_ADDR)
		ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
	return ret;
}

581 582 583 584 585 586
/**
 * 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
587
 * count.
588 589
 */
int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
590
{
591 592 593 594 595
	char hardsoft_irq;
	char need_resched;
	char irqs_off;
	int hardirq;
	int softirq;
596
	int ret;
597 598 599

	hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
	softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
600

601 602 603 604 605 606 607 608 609 610 611 612
	irqs_off =
		(entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
		(entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' :
		'.';
	need_resched =
		(entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.';
	hardsoft_irq =
		(hardirq && softirq) ? 'H' :
		hardirq ? 'h' :
		softirq ? 's' :
		'.';

613
	if (!trace_seq_printf(s, "%c%c%c",
614
			      irqs_off, need_resched, hardsoft_irq))
615
		return 0;
616

617 618
	if (entry->preempt_count)
		ret = trace_seq_printf(s, "%x", entry->preempt_count);
619
	else
620 621
		ret = trace_seq_putc(s, '.');

622
	return ret;
623 624
}

625 626 627 628 629 630 631 632 633 634 635 636 637 638
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);

	if (!trace_seq_printf(s, "%8.8s-%-5d %3d",
			      comm, entry->pid, cpu))
		return 0;

	return trace_print_lat_fmt(s, entry);
}

639
static unsigned long preempt_mark_thresh_us = 100;
640

641
static int
642
lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
643
{
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
	unsigned long verbose = trace_flags & TRACE_ITER_VERBOSE;
	unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
	unsigned long long abs_ts = iter->ts - iter->tr->time_start;
	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;

		return trace_seq_printf(
				s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
				ns2usecs(iter->ts),
				abs_msec, abs_usec,
				rel_msec, rel_usec);
	} else if (verbose && !in_ns) {
		return trace_seq_printf(
				s, "[%016llx] %lld (+%lld): ",
				iter->ts, abs_ts, rel_ts);
	} else if (!verbose && in_ns) {
		return trace_seq_printf(
				s, " %4lldus%c: ",
				abs_ts,
				rel_ts > preempt_mark_thresh_us ? '!' :
				  rel_ts > 1 ? '+' : ' ');
	} else { /* !verbose && !in_ns */
		return trace_seq_printf(s, " %4lld: ", abs_ts);
	}
679 680 681 682 683 684
}

int trace_print_context(struct trace_iterator *iter)
{
	struct trace_seq *s = &iter->seq;
	struct trace_entry *entry = iter->ent;
685 686
	unsigned long long t;
	unsigned long secs, usec_rem;
687
	char comm[TASK_COMM_LEN];
688
	int ret;
689 690

	trace_find_cmdline(entry->pid, comm);
691

692 693 694 695 696 697 698 699 700 701 702
	ret = trace_seq_printf(s, "%16s-%-5d [%03d] ",
			       comm, entry->pid, iter->cpu);
	if (!ret)
		return 0;

	if (trace_flags & TRACE_ITER_IRQ_INFO) {
		ret = trace_print_lat_fmt(s, entry);
		if (!ret)
			return 0;
	}

703 704 705 706 707 708 709
	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;
		return trace_seq_printf(s, " %5lu.%06lu: ", secs, usec_rem);
	} else
		return trace_seq_printf(s, " %12llu: ", iter->ts);
710 711 712 713 714
}

int trace_print_lat_context(struct trace_iterator *iter)
{
	u64 next_ts;
715
	int ret;
716 717
	/* trace_find_next_entry will reset ent_size */
	int ent_size = iter->ent_size;
718 719 720 721 722 723
	struct trace_seq *s = &iter->seq;
	struct trace_entry *entry = iter->ent,
			   *next_entry = trace_find_next_entry(iter, NULL,
							       &next_ts);
	unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);

724 725 726
	/* Restore the original ent_size */
	iter->ent_size = ent_size;

727 728 729 730
	if (!next_entry)
		next_ts = iter->ts;

	if (verbose) {
731 732 733 734
		char comm[TASK_COMM_LEN];

		trace_find_cmdline(entry->pid, comm);

735 736 737 738
		ret = trace_seq_printf(
				s, "%16s %5d %3d %d %08x %08lx ",
				comm, entry->pid, iter->cpu, entry->flags,
				entry->preempt_count, iter->idx);
739
	} else {
740
		ret = lat_print_generic(s, entry, iter->cpu);
741 742
	}

743 744 745
	if (ret)
		ret = lat_print_timestamp(iter, next_ts);

746
	return ret;
747 748
}

749 750 751 752 753 754 755 756 757
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] : '?';
}

758 759 760 761 762
/**
 * ftrace_find_event - find a registered event
 * @type: the type of event to look for
 *
 * Returns an event of type @type otherwise NULL
763
 * Called with trace_event_read_lock() held.
764 765 766 767 768 769 770 771 772
 */
struct trace_event *ftrace_find_event(int type)
{
	struct trace_event *event;
	struct hlist_node *n;
	unsigned key;

	key = type & (EVENT_HASHSIZE - 1);

773
	hlist_for_each_entry(event, n, &event_hash[key], node) {
774 775 776 777 778 779 780
		if (event->type == type)
			return event;
	}

	return NULL;
}

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
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??? */
	if ((last + 1) > FTRACE_MAX_EVENT)
		return 0;

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

811 812 813 814 815 816 817 818 819 820
void trace_event_read_lock(void)
{
	down_read(&trace_event_mutex);
}

void trace_event_read_unlock(void)
{
	up_read(&trace_event_mutex);
}

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
/**
 * register_ftrace_event - register output for an event type
 * @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.
 */
int register_ftrace_event(struct trace_event *event)
{
	unsigned key;
	int ret = 0;

841
	down_write(&trace_event_mutex);
842

843
	if (WARN_ON(!event))
844 845
		goto out;

846 847 848
	if (WARN_ON(!event->funcs))
		goto out;

849 850 851
	INIT_LIST_HEAD(&event->list);

	if (!event->type) {
852
		struct list_head *list = NULL;
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

		if (next_event_type > FTRACE_MAX_EVENT) {

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

		} else {
			
			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) {
872 873 874
		printk(KERN_WARNING "Need to add type to trace.h\n");
		WARN_ON(1);
		goto out;
875 876 877 878 879
	} else {
		/* Is this event already used */
		if (ftrace_find_event(event->type))
			goto out;
	}
880

881 882 883 884 885 886 887 888
	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;
889

890 891
	key = event->type & (EVENT_HASHSIZE - 1);

892
	hlist_add_head(&event->node, &event_hash[key]);
893 894 895

	ret = event->type;
 out:
896
	up_write(&trace_event_mutex);
897 898 899

	return ret;
}
900
EXPORT_SYMBOL_GPL(register_ftrace_event);
901

902 903 904 905 906 907 908 909 910 911
/*
 * Used by module code with the trace_event_mutex held for write.
 */
int __unregister_ftrace_event(struct trace_event *event)
{
	hlist_del(&event->node);
	list_del(&event->list);
	return 0;
}

912 913 914 915 916 917
/**
 * unregister_ftrace_event - remove a no longer used event
 * @event: the event to remove
 */
int unregister_ftrace_event(struct trace_event *event)
{
918
	down_write(&trace_event_mutex);
919
	__unregister_ftrace_event(event);
920
	up_write(&trace_event_mutex);
921 922 923

	return 0;
}
924
EXPORT_SYMBOL_GPL(unregister_ftrace_event);
925 926 927 928 929

/*
 * Standard events
 */

930 931
enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
				  struct trace_event *event)
932
{
933 934 935
	if (!trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type))
		return TRACE_TYPE_PARTIAL_LINE;

936
	return TRACE_TYPE_HANDLED;
937 938 939
}

/* TRACE_FN */
940 941
static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
					struct trace_event *event)
942 943
{
	struct ftrace_entry *field;
944
	struct trace_seq *s = &iter->seq;
945

946
	trace_assign_type(field, iter->ent);
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961

	if (!seq_print_ip_sym(s, field->ip, flags))
		goto partial;

	if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
		if (!trace_seq_printf(s, " <-"))
			goto partial;
		if (!seq_print_ip_sym(s,
				      field->parent_ip,
				      flags))
			goto partial;
	}
	if (!trace_seq_printf(s, "\n"))
		goto partial;

962
	return TRACE_TYPE_HANDLED;
963 964 965 966 967

 partial:
	return TRACE_TYPE_PARTIAL_LINE;
}

968 969
static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
				      struct trace_event *event)
970 971 972
{
	struct ftrace_entry *field;

973
	trace_assign_type(field, iter->ent);
974

975
	if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
L
Lai Jiangshan 已提交
976 977
			      field->ip,
			      field->parent_ip))
978 979
		return TRACE_TYPE_PARTIAL_LINE;

980
	return TRACE_TYPE_HANDLED;
981 982
}

983 984
static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
				      struct trace_event *event)
985 986
{
	struct ftrace_entry *field;
987
	struct trace_seq *s = &iter->seq;
988

989
	trace_assign_type(field, iter->ent);
990 991 992 993

	SEQ_PUT_HEX_FIELD_RET(s, field->ip);
	SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);

994
	return TRACE_TYPE_HANDLED;
995 996
}

997 998
static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
				      struct trace_event *event)
999 1000
{
	struct ftrace_entry *field;
1001
	struct trace_seq *s = &iter->seq;
1002

1003
	trace_assign_type(field, iter->ent);
1004 1005 1006 1007

	SEQ_PUT_FIELD_RET(s, field->ip);
	SEQ_PUT_FIELD_RET(s, field->parent_ip);

1008
	return TRACE_TYPE_HANDLED;
1009 1010
}

1011
static struct trace_event_functions trace_fn_funcs = {
1012 1013 1014 1015 1016 1017
	.trace		= trace_fn_trace,
	.raw		= trace_fn_raw,
	.hex		= trace_fn_hex,
	.binary		= trace_fn_bin,
};

1018 1019 1020 1021 1022
static struct trace_event trace_fn_event = {
	.type		= TRACE_FN,
	.funcs		= &trace_fn_funcs,
};

1023
/* TRACE_CTX an TRACE_WAKE */
1024 1025
static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
					     char *delim)
1026 1027
{
	struct ctx_switch_entry *field;
1028
	char comm[TASK_COMM_LEN];
1029 1030
	int S, T;

1031

1032
	trace_assign_type(field, iter->ent);
1033 1034 1035

	T = task_state_char(field->next_state);
	S = task_state_char(field->prev_state);
1036
	trace_find_cmdline(field->next_pid, comm);
1037 1038
	if (!trace_seq_printf(&iter->seq,
			      " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
L
Lai Jiangshan 已提交
1039 1040 1041 1042 1043 1044 1045
			      field->prev_pid,
			      field->prev_prio,
			      S, delim,
			      field->next_cpu,
			      field->next_pid,
			      field->next_prio,
			      T, comm))
1046 1047
		return TRACE_TYPE_PARTIAL_LINE;

1048
	return TRACE_TYPE_HANDLED;
1049 1050
}

1051 1052
static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
					 struct trace_event *event)
1053
{
1054
	return trace_ctxwake_print(iter, "==>");
1055 1056
}

1057
static enum print_line_t trace_wake_print(struct trace_iterator *iter,
1058
					  int flags, struct trace_event *event)
1059
{
1060
	return trace_ctxwake_print(iter, "  +");
1061 1062
}

1063
static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
1064 1065 1066 1067
{
	struct ctx_switch_entry *field;
	int T;

1068
	trace_assign_type(field, iter->ent);
1069 1070

	if (!S)
1071
		S = task_state_char(field->prev_state);
1072
	T = task_state_char(field->next_state);
1073
	if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
L
Lai Jiangshan 已提交
1074 1075 1076 1077 1078 1079 1080
			      field->prev_pid,
			      field->prev_prio,
			      S,
			      field->next_cpu,
			      field->next_pid,
			      field->next_prio,
			      T))
1081 1082
		return TRACE_TYPE_PARTIAL_LINE;

1083
	return TRACE_TYPE_HANDLED;
1084 1085
}

1086 1087
static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
				       struct trace_event *event)
1088
{
1089
	return trace_ctxwake_raw(iter, 0);
1090 1091
}

1092 1093
static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
					struct trace_event *event)
1094
{
1095
	return trace_ctxwake_raw(iter, '+');
1096 1097 1098
}


1099
static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
1100 1101
{
	struct ctx_switch_entry *field;
1102
	struct trace_seq *s = &iter->seq;
1103 1104
	int T;

1105
	trace_assign_type(field, iter->ent);
1106 1107

	if (!S)
1108
		S = task_state_char(field->prev_state);
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	T = task_state_char(field->next_state);

	SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
	SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
	SEQ_PUT_HEX_FIELD_RET(s, S);
	SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
	SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
	SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
	SEQ_PUT_HEX_FIELD_RET(s, T);

1119
	return TRACE_TYPE_HANDLED;
1120 1121
}

1122 1123
static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
				       struct trace_event *event)
1124
{
1125
	return trace_ctxwake_hex(iter, 0);
1126 1127
}

1128 1129
static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
					struct trace_event *event)
1130
{
1131
	return trace_ctxwake_hex(iter, '+');
1132 1133
}

1134
static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1135
					   int flags, struct trace_event *event)
1136 1137
{
	struct ctx_switch_entry *field;
1138
	struct trace_seq *s = &iter->seq;
1139

1140
	trace_assign_type(field, iter->ent);
1141 1142 1143 1144 1145 1146 1147 1148

	SEQ_PUT_FIELD_RET(s, field->prev_pid);
	SEQ_PUT_FIELD_RET(s, field->prev_prio);
	SEQ_PUT_FIELD_RET(s, field->prev_state);
	SEQ_PUT_FIELD_RET(s, field->next_pid);
	SEQ_PUT_FIELD_RET(s, field->next_prio);
	SEQ_PUT_FIELD_RET(s, field->next_state);

1149
	return TRACE_TYPE_HANDLED;
1150 1151
}

1152
static struct trace_event_functions trace_ctx_funcs = {
1153 1154 1155 1156 1157 1158
	.trace		= trace_ctx_print,
	.raw		= trace_ctx_raw,
	.hex		= trace_ctx_hex,
	.binary		= trace_ctxwake_bin,
};

1159 1160 1161 1162 1163 1164
static struct trace_event trace_ctx_event = {
	.type		= TRACE_CTX,
	.funcs		= &trace_ctx_funcs,
};

static struct trace_event_functions trace_wake_funcs = {
1165 1166 1167 1168 1169 1170
	.trace		= trace_wake_print,
	.raw		= trace_wake_raw,
	.hex		= trace_wake_hex,
	.binary		= trace_ctxwake_bin,
};

1171 1172 1173 1174 1175
static struct trace_event trace_wake_event = {
	.type		= TRACE_WAKE,
	.funcs		= &trace_wake_funcs,
};

1176 1177
/* TRACE_STACK */

1178
static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1179
					   int flags, struct trace_event *event)
1180 1181
{
	struct stack_entry *field;
1182
	struct trace_seq *s = &iter->seq;
1183 1184
	unsigned long *p;
	unsigned long *end;
1185

1186
	trace_assign_type(field, iter->ent);
1187
	end = (unsigned long *)((long)iter->ent + iter->ent_size);
1188

1189
	if (!trace_seq_puts(s, "<stack trace>\n"))
1190
		goto partial;
1191 1192

	for (p = field->caller; p && *p != ULONG_MAX && p < end; p++) {
1193 1194
		if (!trace_seq_puts(s, " => "))
			goto partial;
1195

1196
		if (!seq_print_ip_sym(s, *p, flags))
1197
			goto partial;
L
Lai Jiangshan 已提交
1198
		if (!trace_seq_puts(s, "\n"))
1199 1200 1201
			goto partial;
	}

1202
	return TRACE_TYPE_HANDLED;
1203 1204 1205 1206 1207

 partial:
	return TRACE_TYPE_PARTIAL_LINE;
}

1208
static struct trace_event_functions trace_stack_funcs = {
1209 1210 1211
	.trace		= trace_stack_print,
};

1212 1213 1214 1215 1216
static struct trace_event trace_stack_event = {
	.type		= TRACE_STACK,
	.funcs		= &trace_stack_funcs,
};

1217
/* TRACE_USER_STACK */
1218
static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1219
						int flags, struct trace_event *event)
1220 1221
{
	struct userstack_entry *field;
1222
	struct trace_seq *s = &iter->seq;
1223

1224
	trace_assign_type(field, iter->ent);
1225

1226
	if (!trace_seq_puts(s, "<user stack trace>\n"))
1227 1228
		goto partial;

1229
	if (!seq_print_userip_objs(field, s, flags))
1230 1231
		goto partial;

1232
	return TRACE_TYPE_HANDLED;
1233 1234 1235 1236 1237

 partial:
	return TRACE_TYPE_PARTIAL_LINE;
}

1238
static struct trace_event_functions trace_user_stack_funcs = {
1239 1240 1241
	.trace		= trace_user_stack_print,
};

1242 1243 1244 1245 1246
static struct trace_event trace_user_stack_event = {
	.type		= TRACE_USER_STACK,
	.funcs		= &trace_user_stack_funcs,
};

1247
/* TRACE_BPRINT */
1248
static enum print_line_t
1249 1250
trace_bprint_print(struct trace_iterator *iter, int flags,
		   struct trace_event *event)
1251 1252 1253
{
	struct trace_entry *entry = iter->ent;
	struct trace_seq *s = &iter->seq;
1254
	struct bprint_entry *field;
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272

	trace_assign_type(field, entry);

	if (!seq_print_ip_sym(s, field->ip, flags))
		goto partial;

	if (!trace_seq_puts(s, ": "))
		goto partial;

	if (!trace_seq_bprintf(s, field->fmt, field->buf))
		goto partial;

	return TRACE_TYPE_HANDLED;

 partial:
	return TRACE_TYPE_PARTIAL_LINE;
}

1273

1274
static enum print_line_t
1275 1276
trace_bprint_raw(struct trace_iterator *iter, int flags,
		 struct trace_event *event)
1277
{
1278
	struct bprint_entry *field;
1279 1280
	struct trace_seq *s = &iter->seq;

1281
	trace_assign_type(field, iter->ent);
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294

	if (!trace_seq_printf(s, ": %lx : ", field->ip))
		goto partial;

	if (!trace_seq_bprintf(s, field->fmt, field->buf))
		goto partial;

	return TRACE_TYPE_HANDLED;

 partial:
	return TRACE_TYPE_PARTIAL_LINE;
}

1295 1296 1297 1298
static struct trace_event_functions trace_bprint_funcs = {
	.trace		= trace_bprint_print,
	.raw		= trace_bprint_raw,
};
1299

1300 1301
static struct trace_event trace_bprint_event = {
	.type		= TRACE_BPRINT,
1302
	.funcs		= &trace_bprint_funcs,
1303 1304 1305 1306
};

/* TRACE_PRINT */
static enum print_line_t trace_print_print(struct trace_iterator *iter,
1307
					   int flags, struct trace_event *event)
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
{
	struct print_entry *field;
	struct trace_seq *s = &iter->seq;

	trace_assign_type(field, iter->ent);

	if (!seq_print_ip_sym(s, field->ip, flags))
		goto partial;

	if (!trace_seq_printf(s, ": %s", field->buf))
		goto partial;

	return TRACE_TYPE_HANDLED;

 partial:
	return TRACE_TYPE_PARTIAL_LINE;
}

1326 1327
static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
					 struct trace_event *event)
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
{
	struct print_entry *field;

	trace_assign_type(field, iter->ent);

	if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
		goto partial;

	return TRACE_TYPE_HANDLED;

 partial:
	return TRACE_TYPE_PARTIAL_LINE;
}

1342
static struct trace_event_functions trace_print_funcs = {
1343 1344
	.trace		= trace_print_print,
	.raw		= trace_print_raw,
1345 1346
};

1347 1348 1349 1350 1351
static struct trace_event trace_print_event = {
	.type	 	= TRACE_PRINT,
	.funcs		= &trace_print_funcs,
};

1352

1353 1354 1355 1356 1357 1358
static struct trace_event *events[] __initdata = {
	&trace_fn_event,
	&trace_ctx_event,
	&trace_wake_event,
	&trace_stack_event,
	&trace_user_stack_event,
1359
	&trace_bprint_event,
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
	&trace_print_event,
	NULL
};

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

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

		ret = register_ftrace_event(event);
		if (!ret) {
			printk(KERN_WARNING "event %d failed to register\n",
			       event->type);
			WARN_ON_ONCE(1);
		}
	}

	return 0;
}
1382
early_initcall(init_events);