trace_output.c 26.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_mutex);
18 19

DEFINE_PER_CPU(struct trace_seq, ftrace_event_seq);
20
EXPORT_PER_CPU_SYMBOL(ftrace_event_seq);
21

22 23 24 25
static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;

static int next_event_type = __TRACE_LAST_TYPE + 1;

26
int trace_print_seq(struct seq_file *m, struct trace_seq *s)
27 28
{
	int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
29 30 31
	int ret;

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

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

40
	return ret;
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 72 73 74
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;
}

75 76 77 78 79
/**
 * trace_seq_printf - sequence printing of trace information
 * @s: trace sequence descriptor
 * @fmt: printf format string
 *
80 81 82
 * It returns 0 if the trace oversizes the buffer's free
 * space, 1 otherwise.
 *
83 84 85 86 87 88 89 90 91 92 93 94 95
 * 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;

96
	if (s->full || !len)
97 98 99 100 101 102 103
		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 */
104 105
	if (ret >= len) {
		s->full = 1;
106
		return 0;
107
	}
108 109 110

	s->len += ret;

111
	return 1;
112
}
113
EXPORT_SYMBOL_GPL(trace_seq_printf);
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/**
 * 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;

132
	if (s->full || !len)
133 134 135 136 137
		return 0;

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

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

	s->len += ret;

	return len;
}
EXPORT_SYMBOL_GPL(trace_seq_vprintf);

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

154
	if (s->full || !len)
155 156 157 158 159
		return 0;

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

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

	s->len += ret;

	return len;
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183
/**
 * 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);

184
	if (s->full)
185 186
		return 0;

187 188 189 190 191
	if (len > ((PAGE_SIZE - 1) - s->len)) {
		s->full = 1;
		return 0;
	}

192 193 194 195 196 197 198 199
	memcpy(s->buffer + s->len, str, len);
	s->len += len;

	return len;
}

int trace_seq_putc(struct trace_seq *s, unsigned char c)
{
200
	if (s->full)
201 202
		return 0;

203 204 205 206 207
	if (s->len >= (PAGE_SIZE - 1)) {
		s->full = 1;
		return 0;
	}

208 209 210 211 212
	s->buffer[s->len++] = c;

	return 1;
}

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

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

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

	return len;
}

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

235 236 237
	if (s->full)
		return 0;

238 239 240 241 242 243 244 245 246 247 248 249 250
#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);
}

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

255 256 257 258 259
	if (s->full)
		return 0;

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

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

	return ret;
}

269 270 271 272
int trace_seq_path(struct trace_seq *s, struct path *path)
{
	unsigned char *p;

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

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

281 282 283 284 285 286 287 288 289 290 291 292
	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;
	}

293
	s->full = 1;
294 295 296
	return 0;
}

297 298 299 300 301 302 303
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;
304
	const char *ret = p->buffer + p->len;
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	int i;

	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 (p->len && delim)
			trace_seq_puts(p, delim);
		trace_seq_puts(p, str);
	}

	/* check for left over flags */
	if (flags) {
		if (p->len && delim)
			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 349 350 351 352 353

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

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

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

	if (!p->len)
		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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
#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;

420 421 422
	if (s->full)
		return 0;

423 424 425 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
	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();
460
		task = find_task_by_vpid(entry->tgid);
461 462 463 464 465 466 467 468 469 470
		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;
471 472
		if (ret)
			ret = trace_seq_puts(s, " => ");
473 474 475
		if (!ip) {
			if (ret)
				ret = trace_seq_puts(s, "??");
476 477
			if (ret)
				ret = trace_seq_puts(s, "\n");
478 479 480 481 482 483
			continue;
		}
		if (!ret)
			break;
		if (ret)
			ret = seq_print_user_ip(s, mm, ip, sym_flags);
484
		ret = trace_seq_puts(s, "\n");
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	}

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

513 514 515 516 517 518 519 520 521
/**
 * 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
 * count and lock depth.
 */
int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
522 523
{
	int hardirq, softirq;
524
	int ret;
525 526 527

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

529
	if (!trace_seq_printf(s, "%c%c%c",
530 531 532 533 534 535 536 537
			      (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
				(entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
				  'X' : '.',
			      (entry->flags & TRACE_FLAG_NEED_RESCHED) ?
				'N' : '.',
			      (hardirq && softirq) ? 'H' :
				hardirq ? 'h' : softirq ? 's' : '.'))
		return 0;
538

539 540
	if (entry->preempt_count)
		ret = trace_seq_printf(s, "%x", entry->preempt_count);
541
	else
542 543
		ret = trace_seq_putc(s, '.');

544 545 546
	if (!ret)
		return 0;

547 548 549 550
	if (entry->lock_depth < 0)
		return trace_seq_putc(s, '.');

	return trace_seq_printf(s, "%d", entry->lock_depth);
551 552
}

553 554 555 556 557 558 559 560 561 562 563 564 565 566
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);
}

567 568
static unsigned long preempt_mark_thresh = 100;

569
static int
570 571 572
lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
		    unsigned long rel_usecs)
{
573 574 575
	return trace_seq_printf(s, " %4lldus%c: ", abs_usecs,
				rel_usecs > preempt_mark_thresh ? '!' :
				  rel_usecs > 1 ? '+' : ' ');
576 577 578 579 580 581 582 583 584
}

int trace_print_context(struct trace_iterator *iter)
{
	struct trace_seq *s = &iter->seq;
	struct trace_entry *entry = iter->ent;
	unsigned long long t = ns2usecs(iter->ts);
	unsigned long usec_rem = do_div(t, USEC_PER_SEC);
	unsigned long secs = (unsigned long)t;
585 586 587
	char comm[TASK_COMM_LEN];

	trace_find_cmdline(entry->pid, comm);
588

589
	return trace_seq_printf(s, "%16s-%-5d [%03d] %5lu.%06lu: ",
590
				comm, entry->pid, iter->cpu, secs, usec_rem);
591 592 593 594 595
}

int trace_print_lat_context(struct trace_iterator *iter)
{
	u64 next_ts;
596
	int ret;
597 598 599 600 601 602 603 604 605 606 607 608 609
	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);
	unsigned long abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
	unsigned long rel_usecs;

	if (!next_entry)
		next_ts = iter->ts;
	rel_usecs = ns2usecs(next_ts - iter->ts);

	if (verbose) {
610 611 612 613
		char comm[TASK_COMM_LEN];

		trace_find_cmdline(entry->pid, comm);

614
		ret = trace_seq_printf(s, "%16s %5d %3d %d %08x %08lx [%08llx]"
615
				       " %ld.%03ldms (+%ld.%03ldms): ", comm,
616
				       entry->pid, iter->cpu, entry->flags,
617 618 619 620 621 622
				       entry->preempt_count, iter->idx,
				       ns2usecs(iter->ts),
				       abs_usecs / USEC_PER_MSEC,
				       abs_usecs % USEC_PER_MSEC,
				       rel_usecs / USEC_PER_MSEC,
				       rel_usecs % USEC_PER_MSEC);
623
	} else {
624
		ret = lat_print_generic(s, entry, iter->cpu);
625 626
		if (ret)
			ret = lat_print_timestamp(s, abs_usecs, rel_usecs);
627 628
	}

629
	return ret;
630 631
}

632 633 634 635 636 637 638 639 640
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] : '?';
}

641 642 643 644 645
/**
 * ftrace_find_event - find a registered event
 * @type: the type of event to look for
 *
 * Returns an event of type @type otherwise NULL
646
 * Called with trace_event_read_lock() held.
647 648 649 650 651 652 653 654 655
 */
struct trace_event *ftrace_find_event(int type)
{
	struct trace_event *event;
	struct hlist_node *n;
	unsigned key;

	key = type & (EVENT_HASHSIZE - 1);

656
	hlist_for_each_entry(event, n, &event_hash[key], node) {
657 658 659 660 661 662 663
		if (event->type == type)
			return event;
	}

	return NULL;
}

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
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;
}

694 695 696 697 698 699 700 701 702 703
void trace_event_read_lock(void)
{
	down_read(&trace_event_mutex);
}

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

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
/**
 * 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;

724
	down_write(&trace_event_mutex);
725

726
	if (WARN_ON(!event))
727 728
		goto out;

729 730 731
	INIT_LIST_HEAD(&event->list);

	if (!event->type) {
732
		struct list_head *list = NULL;
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

		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) {
752 753 754
		printk(KERN_WARNING "Need to add type to trace.h\n");
		WARN_ON(1);
		goto out;
755 756 757 758 759
	} else {
		/* Is this event already used */
		if (ftrace_find_event(event->type))
			goto out;
	}
760

761 762 763 764 765 766 767 768 769
	if (event->trace == NULL)
		event->trace = trace_nop_print;
	if (event->raw == NULL)
		event->raw = trace_nop_print;
	if (event->hex == NULL)
		event->hex = trace_nop_print;
	if (event->binary == NULL)
		event->binary = trace_nop_print;

770 771
	key = event->type & (EVENT_HASHSIZE - 1);

772
	hlist_add_head(&event->node, &event_hash[key]);
773 774 775

	ret = event->type;
 out:
776
	up_write(&trace_event_mutex);
777 778 779

	return ret;
}
780
EXPORT_SYMBOL_GPL(register_ftrace_event);
781

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

792 793 794 795 796 797
/**
 * unregister_ftrace_event - remove a no longer used event
 * @event: the event to remove
 */
int unregister_ftrace_event(struct trace_event *event)
{
798
	down_write(&trace_event_mutex);
799
	__unregister_ftrace_event(event);
800
	up_write(&trace_event_mutex);
801 802 803

	return 0;
}
804
EXPORT_SYMBOL_GPL(unregister_ftrace_event);
805 806 807 808 809

/*
 * Standard events
 */

810
enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags)
811
{
812
	return TRACE_TYPE_HANDLED;
813 814 815
}

/* TRACE_FN */
816
static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags)
817 818
{
	struct ftrace_entry *field;
819
	struct trace_seq *s = &iter->seq;
820

821
	trace_assign_type(field, iter->ent);
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836

	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;

837
	return TRACE_TYPE_HANDLED;
838 839 840 841 842

 partial:
	return TRACE_TYPE_PARTIAL_LINE;
}

843
static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags)
844 845 846
{
	struct ftrace_entry *field;

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

849
	if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
L
Lai Jiangshan 已提交
850 851
			      field->ip,
			      field->parent_ip))
852 853
		return TRACE_TYPE_PARTIAL_LINE;

854
	return TRACE_TYPE_HANDLED;
855 856
}

857
static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags)
858 859
{
	struct ftrace_entry *field;
860
	struct trace_seq *s = &iter->seq;
861

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

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

867
	return TRACE_TYPE_HANDLED;
868 869
}

870
static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags)
871 872
{
	struct ftrace_entry *field;
873
	struct trace_seq *s = &iter->seq;
874

875
	trace_assign_type(field, iter->ent);
876 877 878 879

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

880
	return TRACE_TYPE_HANDLED;
881 882 883
}

static struct trace_event trace_fn_event = {
884
	.type		= TRACE_FN,
885 886 887 888 889 890 891
	.trace		= trace_fn_trace,
	.raw		= trace_fn_raw,
	.hex		= trace_fn_hex,
	.binary		= trace_fn_bin,
};

/* TRACE_CTX an TRACE_WAKE */
892 893
static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
					     char *delim)
894 895
{
	struct ctx_switch_entry *field;
896
	char comm[TASK_COMM_LEN];
897 898
	int S, T;

899

900
	trace_assign_type(field, iter->ent);
901 902 903

	T = task_state_char(field->next_state);
	S = task_state_char(field->prev_state);
904
	trace_find_cmdline(field->next_pid, comm);
905 906
	if (!trace_seq_printf(&iter->seq,
			      " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
L
Lai Jiangshan 已提交
907 908 909 910 911 912 913
			      field->prev_pid,
			      field->prev_prio,
			      S, delim,
			      field->next_cpu,
			      field->next_pid,
			      field->next_prio,
			      T, comm))
914 915
		return TRACE_TYPE_PARTIAL_LINE;

916
	return TRACE_TYPE_HANDLED;
917 918
}

919
static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags)
920
{
921
	return trace_ctxwake_print(iter, "==>");
922 923
}

924 925
static enum print_line_t trace_wake_print(struct trace_iterator *iter,
					  int flags)
926
{
927
	return trace_ctxwake_print(iter, "  +");
928 929
}

930
static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
931 932 933 934
{
	struct ctx_switch_entry *field;
	int T;

935
	trace_assign_type(field, iter->ent);
936 937

	if (!S)
938
		S = task_state_char(field->prev_state);
939
	T = task_state_char(field->next_state);
940
	if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
L
Lai Jiangshan 已提交
941 942 943 944 945 946 947
			      field->prev_pid,
			      field->prev_prio,
			      S,
			      field->next_cpu,
			      field->next_pid,
			      field->next_prio,
			      T))
948 949
		return TRACE_TYPE_PARTIAL_LINE;

950
	return TRACE_TYPE_HANDLED;
951 952
}

953
static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags)
954
{
955
	return trace_ctxwake_raw(iter, 0);
956 957
}

958
static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags)
959
{
960
	return trace_ctxwake_raw(iter, '+');
961 962 963
}


964
static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
965 966
{
	struct ctx_switch_entry *field;
967
	struct trace_seq *s = &iter->seq;
968 969
	int T;

970
	trace_assign_type(field, iter->ent);
971 972

	if (!S)
973
		S = task_state_char(field->prev_state);
974 975 976 977 978 979 980 981 982 983
	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);

984
	return TRACE_TYPE_HANDLED;
985 986
}

987
static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags)
988
{
989
	return trace_ctxwake_hex(iter, 0);
990 991
}

992
static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags)
993
{
994
	return trace_ctxwake_hex(iter, '+');
995 996
}

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

1003
	trace_assign_type(field, iter->ent);
1004 1005 1006 1007 1008 1009 1010 1011

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

1012
	return TRACE_TYPE_HANDLED;
1013 1014 1015
}

static struct trace_event trace_ctx_event = {
1016
	.type		= TRACE_CTX,
1017 1018 1019 1020 1021 1022 1023
	.trace		= trace_ctx_print,
	.raw		= trace_ctx_raw,
	.hex		= trace_ctx_hex,
	.binary		= trace_ctxwake_bin,
};

static struct trace_event trace_wake_event = {
1024
	.type		= TRACE_WAKE,
1025 1026 1027 1028 1029 1030 1031
	.trace		= trace_wake_print,
	.raw		= trace_wake_raw,
	.hex		= trace_wake_hex,
	.binary		= trace_ctxwake_bin,
};

/* TRACE_SPECIAL */
1032 1033
static enum print_line_t trace_special_print(struct trace_iterator *iter,
					     int flags)
1034 1035 1036
{
	struct special_entry *field;

1037
	trace_assign_type(field, iter->ent);
1038

1039
	if (!trace_seq_printf(&iter->seq, "# %ld %ld %ld\n",
L
Lai Jiangshan 已提交
1040 1041 1042
			      field->arg1,
			      field->arg2,
			      field->arg3))
1043 1044
		return TRACE_TYPE_PARTIAL_LINE;

1045
	return TRACE_TYPE_HANDLED;
1046 1047
}

1048 1049
static enum print_line_t trace_special_hex(struct trace_iterator *iter,
					   int flags)
1050 1051
{
	struct special_entry *field;
1052
	struct trace_seq *s = &iter->seq;
1053

1054
	trace_assign_type(field, iter->ent);
1055 1056 1057 1058 1059

	SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
	SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
	SEQ_PUT_HEX_FIELD_RET(s, field->arg3);

1060
	return TRACE_TYPE_HANDLED;
1061 1062
}

1063 1064
static enum print_line_t trace_special_bin(struct trace_iterator *iter,
					   int flags)
1065 1066
{
	struct special_entry *field;
1067
	struct trace_seq *s = &iter->seq;
1068

1069
	trace_assign_type(field, iter->ent);
1070 1071 1072 1073 1074

	SEQ_PUT_FIELD_RET(s, field->arg1);
	SEQ_PUT_FIELD_RET(s, field->arg2);
	SEQ_PUT_FIELD_RET(s, field->arg3);

1075
	return TRACE_TYPE_HANDLED;
1076 1077 1078
}

static struct trace_event trace_special_event = {
1079
	.type		= TRACE_SPECIAL,
1080 1081 1082 1083 1084 1085 1086 1087
	.trace		= trace_special_print,
	.raw		= trace_special_print,
	.hex		= trace_special_hex,
	.binary		= trace_special_bin,
};

/* TRACE_STACK */

1088 1089
static enum print_line_t trace_stack_print(struct trace_iterator *iter,
					   int flags)
1090 1091
{
	struct stack_entry *field;
1092
	struct trace_seq *s = &iter->seq;
1093 1094
	int i;

1095
	trace_assign_type(field, iter->ent);
1096

1097
	if (!trace_seq_puts(s, "<stack trace>\n"))
1098
		goto partial;
1099
	for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1100
		if (!field->caller[i] || (field->caller[i] == ULONG_MAX))
1101
			break;
1102 1103
		if (!trace_seq_puts(s, " => "))
			goto partial;
1104

1105 1106
		if (!seq_print_ip_sym(s, field->caller[i], flags))
			goto partial;
L
Lai Jiangshan 已提交
1107
		if (!trace_seq_puts(s, "\n"))
1108 1109 1110
			goto partial;
	}

1111
	return TRACE_TYPE_HANDLED;
1112 1113 1114 1115 1116 1117

 partial:
	return TRACE_TYPE_PARTIAL_LINE;
}

static struct trace_event trace_stack_event = {
1118
	.type		= TRACE_STACK,
1119 1120 1121 1122 1123 1124 1125
	.trace		= trace_stack_print,
	.raw		= trace_special_print,
	.hex		= trace_special_hex,
	.binary		= trace_special_bin,
};

/* TRACE_USER_STACK */
1126 1127
static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
						int flags)
1128 1129
{
	struct userstack_entry *field;
1130
	struct trace_seq *s = &iter->seq;
1131

1132
	trace_assign_type(field, iter->ent);
1133

1134
	if (!trace_seq_puts(s, "<user stack trace>\n"))
1135 1136
		goto partial;

1137
	if (!seq_print_userip_objs(field, s, flags))
1138 1139
		goto partial;

1140
	return TRACE_TYPE_HANDLED;
1141 1142 1143 1144 1145 1146

 partial:
	return TRACE_TYPE_PARTIAL_LINE;
}

static struct trace_event trace_user_stack_event = {
1147
	.type		= TRACE_USER_STACK,
1148 1149 1150 1151 1152 1153
	.trace		= trace_user_stack_print,
	.raw		= trace_special_print,
	.hex		= trace_special_hex,
	.binary		= trace_special_bin,
};

1154
/* TRACE_BPRINT */
1155
static enum print_line_t
1156
trace_bprint_print(struct trace_iterator *iter, int flags)
1157 1158 1159
{
	struct trace_entry *entry = iter->ent;
	struct trace_seq *s = &iter->seq;
1160
	struct bprint_entry *field;
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178

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

1179

1180 1181
static enum print_line_t
trace_bprint_raw(struct trace_iterator *iter, int flags)
1182
{
1183
	struct bprint_entry *field;
1184 1185
	struct trace_seq *s = &iter->seq;

1186
	trace_assign_type(field, iter->ent);
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199

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

1200

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
static struct trace_event trace_bprint_event = {
	.type		= TRACE_BPRINT,
	.trace		= trace_bprint_print,
	.raw		= trace_bprint_raw,
};

/* TRACE_PRINT */
static enum print_line_t trace_print_print(struct trace_iterator *iter,
					   int flags)
{
	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;
}

static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags)
{
	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;
}

1243
static struct trace_event trace_print_event = {
1244
	.type	 	= TRACE_PRINT,
1245 1246
	.trace		= trace_print_print,
	.raw		= trace_print_raw,
1247 1248
};

1249

1250 1251 1252 1253 1254 1255 1256
static struct trace_event *events[] __initdata = {
	&trace_fn_event,
	&trace_ctx_event,
	&trace_wake_event,
	&trace_special_event,
	&trace_stack_event,
	&trace_user_stack_event,
1257
	&trace_bprint_event,
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	&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;
}
device_initcall(init_events);