1 /*
2  * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
3  *
4  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License (not later!)
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20  *
21  *  The parts for function graph printing was taken and modified from the
22  *  Linux Kernel that were written by Frederic Weisbecker.
23  */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 
30 #include "../perf.h"
31 #include "util.h"
32 #include "trace-event.h"
33 
34 int header_page_ts_offset;
35 int header_page_ts_size;
36 int header_page_size_offset;
37 int header_page_size_size;
38 int header_page_overwrite_offset;
39 int header_page_overwrite_size;
40 int header_page_data_offset;
41 int header_page_data_size;
42 
43 bool latency_format;
44 
45 static char *input_buf;
46 static unsigned long long input_buf_ptr;
47 static unsigned long long input_buf_siz;
48 
49 static int cpus;
50 static int long_size;
51 static int is_flag_field;
52 static int is_symbolic_field;
53 
54 static struct format_field *
55 find_any_field(struct event *event, const char *name);
56 
57 static void init_input_buf(char *buf, unsigned long long size)
58 {
59 	input_buf = buf;
60 	input_buf_siz = size;
61 	input_buf_ptr = 0;
62 }
63 
64 struct cmdline {
65 	char *comm;
66 	int pid;
67 };
68 
69 static struct cmdline *cmdlines;
70 static int cmdline_count;
71 
72 static int cmdline_cmp(const void *a, const void *b)
73 {
74 	const struct cmdline *ca = a;
75 	const struct cmdline *cb = b;
76 
77 	if (ca->pid < cb->pid)
78 		return -1;
79 	if (ca->pid > cb->pid)
80 		return 1;
81 
82 	return 0;
83 }
84 
85 void parse_cmdlines(char *file, int size __unused)
86 {
87 	struct cmdline_list {
88 		struct cmdline_list	*next;
89 		char			*comm;
90 		int			pid;
91 	} *list = NULL, *item;
92 	char *line;
93 	char *next = NULL;
94 	int i;
95 
96 	line = strtok_r(file, "\n", &next);
97 	while (line) {
98 		item = malloc_or_die(sizeof(*item));
99 		sscanf(line, "%d %as", &item->pid,
100 		       (float *)(void *)&item->comm); /* workaround gcc warning */
101 		item->next = list;
102 		list = item;
103 		line = strtok_r(NULL, "\n", &next);
104 		cmdline_count++;
105 	}
106 
107 	cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
108 
109 	i = 0;
110 	while (list) {
111 		cmdlines[i].pid = list->pid;
112 		cmdlines[i].comm = list->comm;
113 		i++;
114 		item = list;
115 		list = list->next;
116 		free(item);
117 	}
118 
119 	qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
120 }
121 
122 static struct func_map {
123 	unsigned long long		addr;
124 	char				*func;
125 	char				*mod;
126 } *func_list;
127 static unsigned int func_count;
128 
129 static int func_cmp(const void *a, const void *b)
130 {
131 	const struct func_map *fa = a;
132 	const struct func_map *fb = b;
133 
134 	if (fa->addr < fb->addr)
135 		return -1;
136 	if (fa->addr > fb->addr)
137 		return 1;
138 
139 	return 0;
140 }
141 
142 void parse_proc_kallsyms(char *file, unsigned int size __unused)
143 {
144 	struct func_list {
145 		struct func_list	*next;
146 		unsigned long long	addr;
147 		char			*func;
148 		char			*mod;
149 	} *list = NULL, *item;
150 	char *line;
151 	char *next = NULL;
152 	char *addr_str;
153 	char ch;
154 	int ret __used;
155 	int i;
156 
157 	line = strtok_r(file, "\n", &next);
158 	while (line) {
159 		item = malloc_or_die(sizeof(*item));
160 		item->mod = NULL;
161 		ret = sscanf(line, "%as %c %as\t[%as",
162 			     (float *)(void *)&addr_str, /* workaround gcc warning */
163 			     &ch,
164 			     (float *)(void *)&item->func,
165 			     (float *)(void *)&item->mod);
166 		item->addr = strtoull(addr_str, NULL, 16);
167 		free(addr_str);
168 
169 		/* truncate the extra ']' */
170 		if (item->mod)
171 			item->mod[strlen(item->mod) - 1] = 0;
172 
173 
174 		item->next = list;
175 		list = item;
176 		line = strtok_r(NULL, "\n", &next);
177 		func_count++;
178 	}
179 
180 	func_list = malloc_or_die(sizeof(*func_list) * (func_count + 1));
181 
182 	i = 0;
183 	while (list) {
184 		func_list[i].func = list->func;
185 		func_list[i].addr = list->addr;
186 		func_list[i].mod = list->mod;
187 		i++;
188 		item = list;
189 		list = list->next;
190 		free(item);
191 	}
192 
193 	qsort(func_list, func_count, sizeof(*func_list), func_cmp);
194 
195 	/*
196 	 * Add a special record at the end.
197 	 */
198 	func_list[func_count].func = NULL;
199 	func_list[func_count].addr = 0;
200 	func_list[func_count].mod = NULL;
201 }
202 
203 /*
204  * We are searching for a record in between, not an exact
205  * match.
206  */
207 static int func_bcmp(const void *a, const void *b)
208 {
209 	const struct func_map *fa = a;
210 	const struct func_map *fb = b;
211 
212 	if ((fa->addr == fb->addr) ||
213 
214 	    (fa->addr > fb->addr &&
215 	     fa->addr < (fb+1)->addr))
216 		return 0;
217 
218 	if (fa->addr < fb->addr)
219 		return -1;
220 
221 	return 1;
222 }
223 
224 static struct func_map *find_func(unsigned long long addr)
225 {
226 	struct func_map *func;
227 	struct func_map key;
228 
229 	key.addr = addr;
230 
231 	func = bsearch(&key, func_list, func_count, sizeof(*func_list),
232 		       func_bcmp);
233 
234 	return func;
235 }
236 
237 void print_funcs(void)
238 {
239 	int i;
240 
241 	for (i = 0; i < (int)func_count; i++) {
242 		printf("%016llx %s",
243 		       func_list[i].addr,
244 		       func_list[i].func);
245 		if (func_list[i].mod)
246 			printf(" [%s]\n", func_list[i].mod);
247 		else
248 			printf("\n");
249 	}
250 }
251 
252 static struct printk_map {
253 	unsigned long long		addr;
254 	char				*printk;
255 } *printk_list;
256 static unsigned int printk_count;
257 
258 static int printk_cmp(const void *a, const void *b)
259 {
260 	const struct func_map *fa = a;
261 	const struct func_map *fb = b;
262 
263 	if (fa->addr < fb->addr)
264 		return -1;
265 	if (fa->addr > fb->addr)
266 		return 1;
267 
268 	return 0;
269 }
270 
271 static struct printk_map *find_printk(unsigned long long addr)
272 {
273 	struct printk_map *printk;
274 	struct printk_map key;
275 
276 	key.addr = addr;
277 
278 	printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
279 			 printk_cmp);
280 
281 	return printk;
282 }
283 
284 void parse_ftrace_printk(char *file, unsigned int size __unused)
285 {
286 	struct printk_list {
287 		struct printk_list	*next;
288 		unsigned long long	addr;
289 		char			*printk;
290 	} *list = NULL, *item;
291 	char *line;
292 	char *next = NULL;
293 	char *addr_str;
294 	int i;
295 
296 	line = strtok_r(file, "\n", &next);
297 	while (line) {
298 		addr_str = strsep(&line, ":");
299 		if (!line) {
300 			warning("error parsing print strings");
301 			break;
302 		}
303 		item = malloc_or_die(sizeof(*item));
304 		item->addr = strtoull(addr_str, NULL, 16);
305 		/* fmt still has a space, skip it */
306 		item->printk = strdup(line+1);
307 		item->next = list;
308 		list = item;
309 		line = strtok_r(NULL, "\n", &next);
310 		printk_count++;
311 	}
312 
313 	printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
314 
315 	i = 0;
316 	while (list) {
317 		printk_list[i].printk = list->printk;
318 		printk_list[i].addr = list->addr;
319 		i++;
320 		item = list;
321 		list = list->next;
322 		free(item);
323 	}
324 
325 	qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
326 }
327 
328 void print_printk(void)
329 {
330 	int i;
331 
332 	for (i = 0; i < (int)printk_count; i++) {
333 		printf("%016llx %s\n",
334 		       printk_list[i].addr,
335 		       printk_list[i].printk);
336 	}
337 }
338 
339 static struct event *alloc_event(void)
340 {
341 	struct event *event;
342 
343 	event = malloc_or_die(sizeof(*event));
344 	memset(event, 0, sizeof(*event));
345 
346 	return event;
347 }
348 
349 enum event_type {
350 	EVENT_ERROR,
351 	EVENT_NONE,
352 	EVENT_SPACE,
353 	EVENT_NEWLINE,
354 	EVENT_OP,
355 	EVENT_DELIM,
356 	EVENT_ITEM,
357 	EVENT_DQUOTE,
358 	EVENT_SQUOTE,
359 };
360 
361 static struct event *event_list;
362 
363 static void add_event(struct event *event)
364 {
365 	event->next = event_list;
366 	event_list = event;
367 }
368 
369 static int event_item_type(enum event_type type)
370 {
371 	switch (type) {
372 	case EVENT_ITEM ... EVENT_SQUOTE:
373 		return 1;
374 	case EVENT_ERROR ... EVENT_DELIM:
375 	default:
376 		return 0;
377 	}
378 }
379 
380 static void free_arg(struct print_arg *arg)
381 {
382 	if (!arg)
383 		return;
384 
385 	switch (arg->type) {
386 	case PRINT_ATOM:
387 		if (arg->atom.atom)
388 			free(arg->atom.atom);
389 		break;
390 	case PRINT_NULL:
391 	case PRINT_FIELD ... PRINT_OP:
392 	default:
393 		/* todo */
394 		break;
395 	}
396 
397 	free(arg);
398 }
399 
400 static enum event_type get_type(int ch)
401 {
402 	if (ch == '\n')
403 		return EVENT_NEWLINE;
404 	if (isspace(ch))
405 		return EVENT_SPACE;
406 	if (isalnum(ch) || ch == '_')
407 		return EVENT_ITEM;
408 	if (ch == '\'')
409 		return EVENT_SQUOTE;
410 	if (ch == '"')
411 		return EVENT_DQUOTE;
412 	if (!isprint(ch))
413 		return EVENT_NONE;
414 	if (ch == '(' || ch == ')' || ch == ',')
415 		return EVENT_DELIM;
416 
417 	return EVENT_OP;
418 }
419 
420 static int __read_char(void)
421 {
422 	if (input_buf_ptr >= input_buf_siz)
423 		return -1;
424 
425 	return input_buf[input_buf_ptr++];
426 }
427 
428 static int __peek_char(void)
429 {
430 	if (input_buf_ptr >= input_buf_siz)
431 		return -1;
432 
433 	return input_buf[input_buf_ptr];
434 }
435 
436 static enum event_type __read_token(char **tok)
437 {
438 	char buf[BUFSIZ];
439 	int ch, last_ch, quote_ch, next_ch;
440 	int i = 0;
441 	int tok_size = 0;
442 	enum event_type type;
443 
444 	*tok = NULL;
445 
446 
447 	ch = __read_char();
448 	if (ch < 0)
449 		return EVENT_NONE;
450 
451 	type = get_type(ch);
452 	if (type == EVENT_NONE)
453 		return type;
454 
455 	buf[i++] = ch;
456 
457 	switch (type) {
458 	case EVENT_NEWLINE:
459 	case EVENT_DELIM:
460 		*tok = malloc_or_die(2);
461 		(*tok)[0] = ch;
462 		(*tok)[1] = 0;
463 		return type;
464 
465 	case EVENT_OP:
466 		switch (ch) {
467 		case '-':
468 			next_ch = __peek_char();
469 			if (next_ch == '>') {
470 				buf[i++] = __read_char();
471 				break;
472 			}
473 			/* fall through */
474 		case '+':
475 		case '|':
476 		case '&':
477 		case '>':
478 		case '<':
479 			last_ch = ch;
480 			ch = __peek_char();
481 			if (ch != last_ch)
482 				goto test_equal;
483 			buf[i++] = __read_char();
484 			switch (last_ch) {
485 			case '>':
486 			case '<':
487 				goto test_equal;
488 			default:
489 				break;
490 			}
491 			break;
492 		case '!':
493 		case '=':
494 			goto test_equal;
495 		default: /* what should we do instead? */
496 			break;
497 		}
498 		buf[i] = 0;
499 		*tok = strdup(buf);
500 		return type;
501 
502  test_equal:
503 		ch = __peek_char();
504 		if (ch == '=')
505 			buf[i++] = __read_char();
506 		break;
507 
508 	case EVENT_DQUOTE:
509 	case EVENT_SQUOTE:
510 		/* don't keep quotes */
511 		i--;
512 		quote_ch = ch;
513 		last_ch = 0;
514 		do {
515 			if (i == (BUFSIZ - 1)) {
516 				buf[i] = 0;
517 				if (*tok) {
518 					*tok = realloc(*tok, tok_size + BUFSIZ);
519 					if (!*tok)
520 						return EVENT_NONE;
521 					strcat(*tok, buf);
522 				} else
523 					*tok = strdup(buf);
524 
525 				if (!*tok)
526 					return EVENT_NONE;
527 				tok_size += BUFSIZ;
528 				i = 0;
529 			}
530 			last_ch = ch;
531 			ch = __read_char();
532 			buf[i++] = ch;
533 			/* the '\' '\' will cancel itself */
534 			if (ch == '\\' && last_ch == '\\')
535 				last_ch = 0;
536 		} while (ch != quote_ch || last_ch == '\\');
537 		/* remove the last quote */
538 		i--;
539 		goto out;
540 
541 	case EVENT_ERROR ... EVENT_SPACE:
542 	case EVENT_ITEM:
543 	default:
544 		break;
545 	}
546 
547 	while (get_type(__peek_char()) == type) {
548 		if (i == (BUFSIZ - 1)) {
549 			buf[i] = 0;
550 			if (*tok) {
551 				*tok = realloc(*tok, tok_size + BUFSIZ);
552 				if (!*tok)
553 					return EVENT_NONE;
554 				strcat(*tok, buf);
555 			} else
556 				*tok = strdup(buf);
557 
558 			if (!*tok)
559 				return EVENT_NONE;
560 			tok_size += BUFSIZ;
561 			i = 0;
562 		}
563 		ch = __read_char();
564 		buf[i++] = ch;
565 	}
566 
567  out:
568 	buf[i] = 0;
569 	if (*tok) {
570 		*tok = realloc(*tok, tok_size + i);
571 		if (!*tok)
572 			return EVENT_NONE;
573 		strcat(*tok, buf);
574 	} else
575 		*tok = strdup(buf);
576 	if (!*tok)
577 		return EVENT_NONE;
578 
579 	return type;
580 }
581 
582 static void free_token(char *tok)
583 {
584 	if (tok)
585 		free(tok);
586 }
587 
588 static enum event_type read_token(char **tok)
589 {
590 	enum event_type type;
591 
592 	for (;;) {
593 		type = __read_token(tok);
594 		if (type != EVENT_SPACE)
595 			return type;
596 
597 		free_token(*tok);
598 	}
599 
600 	/* not reached */
601 	return EVENT_NONE;
602 }
603 
604 /* no newline */
605 static enum event_type read_token_item(char **tok)
606 {
607 	enum event_type type;
608 
609 	for (;;) {
610 		type = __read_token(tok);
611 		if (type != EVENT_SPACE && type != EVENT_NEWLINE)
612 			return type;
613 
614 		free_token(*tok);
615 	}
616 
617 	/* not reached */
618 	return EVENT_NONE;
619 }
620 
621 static int test_type(enum event_type type, enum event_type expect)
622 {
623 	if (type != expect) {
624 		warning("Error: expected type %d but read %d",
625 		    expect, type);
626 		return -1;
627 	}
628 	return 0;
629 }
630 
631 static int __test_type_token(enum event_type type, char *token,
632 			     enum event_type expect, const char *expect_tok,
633 			     bool warn)
634 {
635 	if (type != expect) {
636 		if (warn)
637 			warning("Error: expected type %d but read %d",
638 				expect, type);
639 		return -1;
640 	}
641 
642 	if (strcmp(token, expect_tok) != 0) {
643 		if (warn)
644 			warning("Error: expected '%s' but read '%s'",
645 				expect_tok, token);
646 		return -1;
647 	}
648 	return 0;
649 }
650 
651 static int test_type_token(enum event_type type, char *token,
652 			   enum event_type expect, const char *expect_tok)
653 {
654 	return __test_type_token(type, token, expect, expect_tok, true);
655 }
656 
657 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
658 {
659 	enum event_type type;
660 
661 	if (newline_ok)
662 		type = read_token(tok);
663 	else
664 		type = read_token_item(tok);
665 	return test_type(type, expect);
666 }
667 
668 static int read_expect_type(enum event_type expect, char **tok)
669 {
670 	return __read_expect_type(expect, tok, 1);
671 }
672 
673 static int __read_expected(enum event_type expect, const char *str,
674 			   int newline_ok, bool warn)
675 {
676 	enum event_type type;
677 	char *token;
678 	int ret;
679 
680 	if (newline_ok)
681 		type = read_token(&token);
682 	else
683 		type = read_token_item(&token);
684 
685 	ret = __test_type_token(type, token, expect, str, warn);
686 
687 	free_token(token);
688 
689 	return ret;
690 }
691 
692 static int read_expected(enum event_type expect, const char *str)
693 {
694 	return __read_expected(expect, str, 1, true);
695 }
696 
697 static int read_expected_item(enum event_type expect, const char *str)
698 {
699 	return __read_expected(expect, str, 0, true);
700 }
701 
702 static char *event_read_name(void)
703 {
704 	char *token;
705 
706 	if (read_expected(EVENT_ITEM, "name") < 0)
707 		return NULL;
708 
709 	if (read_expected(EVENT_OP, ":") < 0)
710 		return NULL;
711 
712 	if (read_expect_type(EVENT_ITEM, &token) < 0)
713 		goto fail;
714 
715 	return token;
716 
717  fail:
718 	free_token(token);
719 	return NULL;
720 }
721 
722 static int event_read_id(void)
723 {
724 	char *token;
725 	int id;
726 
727 	if (read_expected_item(EVENT_ITEM, "ID") < 0)
728 		return -1;
729 
730 	if (read_expected(EVENT_OP, ":") < 0)
731 		return -1;
732 
733 	if (read_expect_type(EVENT_ITEM, &token) < 0)
734 		goto fail;
735 
736 	id = strtoul(token, NULL, 0);
737 	free_token(token);
738 	return id;
739 
740  fail:
741 	free_token(token);
742 	return -1;
743 }
744 
745 static int field_is_string(struct format_field *field)
746 {
747 	if ((field->flags & FIELD_IS_ARRAY) &&
748 	    (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
749 	     !strstr(field->type, "s8")))
750 		return 1;
751 
752 	return 0;
753 }
754 
755 static int field_is_dynamic(struct format_field *field)
756 {
757 	if (!strncmp(field->type, "__data_loc", 10))
758 		return 1;
759 
760 	return 0;
761 }
762 
763 static int event_read_fields(struct event *event, struct format_field **fields)
764 {
765 	struct format_field *field = NULL;
766 	enum event_type type;
767 	char *token;
768 	char *last_token;
769 	int count = 0;
770 
771 	do {
772 		type = read_token(&token);
773 		if (type == EVENT_NEWLINE) {
774 			free_token(token);
775 			return count;
776 		}
777 
778 		count++;
779 
780 		if (test_type_token(type, token, EVENT_ITEM, "field"))
781 			goto fail;
782 		free_token(token);
783 
784 		type = read_token(&token);
785 		/*
786 		 * The ftrace fields may still use the "special" name.
787 		 * Just ignore it.
788 		 */
789 		if (event->flags & EVENT_FL_ISFTRACE &&
790 		    type == EVENT_ITEM && strcmp(token, "special") == 0) {
791 			free_token(token);
792 			type = read_token(&token);
793 		}
794 
795 		if (test_type_token(type, token, EVENT_OP, ":") < 0)
796 			return -1;
797 
798 		if (read_expect_type(EVENT_ITEM, &token) < 0)
799 			goto fail;
800 
801 		last_token = token;
802 
803 		field = malloc_or_die(sizeof(*field));
804 		memset(field, 0, sizeof(*field));
805 
806 		/* read the rest of the type */
807 		for (;;) {
808 			type = read_token(&token);
809 			if (type == EVENT_ITEM ||
810 			    (type == EVENT_OP && strcmp(token, "*") == 0) ||
811 			    /*
812 			     * Some of the ftrace fields are broken and have
813 			     * an illegal "." in them.
814 			     */
815 			    (event->flags & EVENT_FL_ISFTRACE &&
816 			     type == EVENT_OP && strcmp(token, ".") == 0)) {
817 
818 				if (strcmp(token, "*") == 0)
819 					field->flags |= FIELD_IS_POINTER;
820 
821 				if (field->type) {
822 					field->type = realloc(field->type,
823 							      strlen(field->type) +
824 							      strlen(last_token) + 2);
825 					strcat(field->type, " ");
826 					strcat(field->type, last_token);
827 				} else
828 					field->type = last_token;
829 				last_token = token;
830 				continue;
831 			}
832 
833 			break;
834 		}
835 
836 		if (!field->type) {
837 			die("no type found");
838 			goto fail;
839 		}
840 		field->name = last_token;
841 
842 		if (test_type(type, EVENT_OP))
843 			goto fail;
844 
845 		if (strcmp(token, "[") == 0) {
846 			enum event_type last_type = type;
847 			char *brackets = token;
848 			int len;
849 
850 			field->flags |= FIELD_IS_ARRAY;
851 
852 			type = read_token(&token);
853 		        while (strcmp(token, "]") != 0) {
854 				if (last_type == EVENT_ITEM &&
855 				    type == EVENT_ITEM)
856 					len = 2;
857 				else
858 					len = 1;
859 				last_type = type;
860 
861 				brackets = realloc(brackets,
862 						   strlen(brackets) +
863 						   strlen(token) + len);
864 				if (len == 2)
865 					strcat(brackets, " ");
866 				strcat(brackets, token);
867 				free_token(token);
868 				type = read_token(&token);
869 				if (type == EVENT_NONE) {
870 					die("failed to find token");
871 					goto fail;
872 				}
873 			}
874 
875 			free_token(token);
876 
877 			brackets = realloc(brackets, strlen(brackets) + 2);
878 			strcat(brackets, "]");
879 
880 			/* add brackets to type */
881 
882 			type = read_token(&token);
883 			/*
884 			 * If the next token is not an OP, then it is of
885 			 * the format: type [] item;
886 			 */
887 			if (type == EVENT_ITEM) {
888 				field->type = realloc(field->type,
889 						      strlen(field->type) +
890 						      strlen(field->name) +
891 						      strlen(brackets) + 2);
892 				strcat(field->type, " ");
893 				strcat(field->type, field->name);
894 				free_token(field->name);
895 				strcat(field->type, brackets);
896 				field->name = token;
897 				type = read_token(&token);
898 			} else {
899 				field->type = realloc(field->type,
900 						      strlen(field->type) +
901 						      strlen(brackets) + 1);
902 				strcat(field->type, brackets);
903 			}
904 			free(brackets);
905 		}
906 
907 		if (field_is_string(field)) {
908 			field->flags |= FIELD_IS_STRING;
909 			if (field_is_dynamic(field))
910 				field->flags |= FIELD_IS_DYNAMIC;
911 		}
912 
913 		if (test_type_token(type, token,  EVENT_OP, ";"))
914 			goto fail;
915 		free_token(token);
916 
917 		if (read_expected(EVENT_ITEM, "offset") < 0)
918 			goto fail_expect;
919 
920 		if (read_expected(EVENT_OP, ":") < 0)
921 			goto fail_expect;
922 
923 		if (read_expect_type(EVENT_ITEM, &token))
924 			goto fail;
925 		field->offset = strtoul(token, NULL, 0);
926 		free_token(token);
927 
928 		if (read_expected(EVENT_OP, ";") < 0)
929 			goto fail_expect;
930 
931 		if (read_expected(EVENT_ITEM, "size") < 0)
932 			goto fail_expect;
933 
934 		if (read_expected(EVENT_OP, ":") < 0)
935 			goto fail_expect;
936 
937 		if (read_expect_type(EVENT_ITEM, &token))
938 			goto fail;
939 		field->size = strtoul(token, NULL, 0);
940 		free_token(token);
941 
942 		if (read_expected(EVENT_OP, ";") < 0)
943 			goto fail_expect;
944 
945 		type = read_token(&token);
946 		if (type != EVENT_NEWLINE) {
947 			/* newer versions of the kernel have a "signed" type */
948 			if (test_type_token(type, token, EVENT_ITEM, "signed"))
949 				goto fail;
950 
951 			free_token(token);
952 
953 			if (read_expected(EVENT_OP, ":") < 0)
954 				goto fail_expect;
955 
956 			if (read_expect_type(EVENT_ITEM, &token))
957 				goto fail;
958 
959 			if (strtoul(token, NULL, 0))
960 				field->flags |= FIELD_IS_SIGNED;
961 
962 			free_token(token);
963 			if (read_expected(EVENT_OP, ";") < 0)
964 				goto fail_expect;
965 
966 			if (read_expect_type(EVENT_NEWLINE, &token))
967 				goto fail;
968 		}
969 
970 		free_token(token);
971 
972 		*fields = field;
973 		fields = &field->next;
974 
975 	} while (1);
976 
977 	return 0;
978 
979 fail:
980 	free_token(token);
981 fail_expect:
982 	if (field)
983 		free(field);
984 	return -1;
985 }
986 
987 static int event_read_format(struct event *event)
988 {
989 	char *token;
990 	int ret;
991 
992 	if (read_expected_item(EVENT_ITEM, "format") < 0)
993 		return -1;
994 
995 	if (read_expected(EVENT_OP, ":") < 0)
996 		return -1;
997 
998 	if (read_expect_type(EVENT_NEWLINE, &token))
999 		goto fail;
1000 	free_token(token);
1001 
1002 	ret = event_read_fields(event, &event->format.common_fields);
1003 	if (ret < 0)
1004 		return ret;
1005 	event->format.nr_common = ret;
1006 
1007 	ret = event_read_fields(event, &event->format.fields);
1008 	if (ret < 0)
1009 		return ret;
1010 	event->format.nr_fields = ret;
1011 
1012 	return 0;
1013 
1014  fail:
1015 	free_token(token);
1016 	return -1;
1017 }
1018 
1019 enum event_type
1020 process_arg_token(struct event *event, struct print_arg *arg,
1021 		  char **tok, enum event_type type);
1022 
1023 static enum event_type
1024 process_arg(struct event *event, struct print_arg *arg, char **tok)
1025 {
1026 	enum event_type type;
1027 	char *token;
1028 
1029 	type = read_token(&token);
1030 	*tok = token;
1031 
1032 	return process_arg_token(event, arg, tok, type);
1033 }
1034 
1035 static enum event_type
1036 process_cond(struct event *event, struct print_arg *top, char **tok)
1037 {
1038 	struct print_arg *arg, *left, *right;
1039 	enum event_type type;
1040 	char *token = NULL;
1041 
1042 	arg = malloc_or_die(sizeof(*arg));
1043 	memset(arg, 0, sizeof(*arg));
1044 
1045 	left = malloc_or_die(sizeof(*left));
1046 
1047 	right = malloc_or_die(sizeof(*right));
1048 
1049 	arg->type = PRINT_OP;
1050 	arg->op.left = left;
1051 	arg->op.right = right;
1052 
1053 	*tok = NULL;
1054 	type = process_arg(event, left, &token);
1055 	if (test_type_token(type, token, EVENT_OP, ":"))
1056 		goto out_free;
1057 
1058 	arg->op.op = token;
1059 
1060 	type = process_arg(event, right, &token);
1061 
1062 	top->op.right = arg;
1063 
1064 	*tok = token;
1065 	return type;
1066 
1067 out_free:
1068 	free_token(*tok);
1069 	free(right);
1070 	free(left);
1071 	free_arg(arg);
1072 	return EVENT_ERROR;
1073 }
1074 
1075 static enum event_type
1076 process_array(struct event *event, struct print_arg *top, char **tok)
1077 {
1078 	struct print_arg *arg;
1079 	enum event_type type;
1080 	char *token = NULL;
1081 
1082 	arg = malloc_or_die(sizeof(*arg));
1083 	memset(arg, 0, sizeof(*arg));
1084 
1085 	*tok = NULL;
1086 	type = process_arg(event, arg, &token);
1087 	if (test_type_token(type, token, EVENT_OP, "]"))
1088 		goto out_free;
1089 
1090 	top->op.right = arg;
1091 
1092 	free_token(token);
1093 	type = read_token_item(&token);
1094 	*tok = token;
1095 
1096 	return type;
1097 
1098 out_free:
1099 	free_token(*tok);
1100 	free_arg(arg);
1101 	return EVENT_ERROR;
1102 }
1103 
1104 static int get_op_prio(char *op)
1105 {
1106 	if (!op[1]) {
1107 		switch (op[0]) {
1108 		case '*':
1109 		case '/':
1110 		case '%':
1111 			return 6;
1112 		case '+':
1113 		case '-':
1114 			return 7;
1115 			/* '>>' and '<<' are 8 */
1116 		case '<':
1117 		case '>':
1118 			return 9;
1119 			/* '==' and '!=' are 10 */
1120 		case '&':
1121 			return 11;
1122 		case '^':
1123 			return 12;
1124 		case '|':
1125 			return 13;
1126 		case '?':
1127 			return 16;
1128 		default:
1129 			die("unknown op '%c'", op[0]);
1130 			return -1;
1131 		}
1132 	} else {
1133 		if (strcmp(op, "++") == 0 ||
1134 		    strcmp(op, "--") == 0) {
1135 			return 3;
1136 		} else if (strcmp(op, ">>") == 0 ||
1137 			   strcmp(op, "<<") == 0) {
1138 			return 8;
1139 		} else if (strcmp(op, ">=") == 0 ||
1140 			   strcmp(op, "<=") == 0) {
1141 			return 9;
1142 		} else if (strcmp(op, "==") == 0 ||
1143 			   strcmp(op, "!=") == 0) {
1144 			return 10;
1145 		} else if (strcmp(op, "&&") == 0) {
1146 			return 14;
1147 		} else if (strcmp(op, "||") == 0) {
1148 			return 15;
1149 		} else {
1150 			die("unknown op '%s'", op);
1151 			return -1;
1152 		}
1153 	}
1154 }
1155 
1156 static void set_op_prio(struct print_arg *arg)
1157 {
1158 
1159 	/* single ops are the greatest */
1160 	if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1161 		arg->op.prio = 0;
1162 		return;
1163 	}
1164 
1165 	arg->op.prio = get_op_prio(arg->op.op);
1166 }
1167 
1168 static enum event_type
1169 process_op(struct event *event, struct print_arg *arg, char **tok)
1170 {
1171 	struct print_arg *left, *right = NULL;
1172 	enum event_type type;
1173 	char *token;
1174 
1175 	/* the op is passed in via tok */
1176 	token = *tok;
1177 
1178 	if (arg->type == PRINT_OP && !arg->op.left) {
1179 		/* handle single op */
1180 		if (token[1]) {
1181 			die("bad op token %s", token);
1182 			return EVENT_ERROR;
1183 		}
1184 		switch (token[0]) {
1185 		case '!':
1186 		case '+':
1187 		case '-':
1188 			break;
1189 		default:
1190 			die("bad op token %s", token);
1191 			return EVENT_ERROR;
1192 		}
1193 
1194 		/* make an empty left */
1195 		left = malloc_or_die(sizeof(*left));
1196 		left->type = PRINT_NULL;
1197 		arg->op.left = left;
1198 
1199 		right = malloc_or_die(sizeof(*right));
1200 		arg->op.right = right;
1201 
1202 		type = process_arg(event, right, tok);
1203 
1204 	} else if (strcmp(token, "?") == 0) {
1205 
1206 		left = malloc_or_die(sizeof(*left));
1207 		/* copy the top arg to the left */
1208 		*left = *arg;
1209 
1210 		arg->type = PRINT_OP;
1211 		arg->op.op = token;
1212 		arg->op.left = left;
1213 		arg->op.prio = 0;
1214 
1215 		type = process_cond(event, arg, tok);
1216 
1217 	} else if (strcmp(token, ">>") == 0 ||
1218 		   strcmp(token, "<<") == 0 ||
1219 		   strcmp(token, "&") == 0 ||
1220 		   strcmp(token, "|") == 0 ||
1221 		   strcmp(token, "&&") == 0 ||
1222 		   strcmp(token, "||") == 0 ||
1223 		   strcmp(token, "-") == 0 ||
1224 		   strcmp(token, "+") == 0 ||
1225 		   strcmp(token, "*") == 0 ||
1226 		   strcmp(token, "^") == 0 ||
1227 		   strcmp(token, "/") == 0 ||
1228 		   strcmp(token, "<") == 0 ||
1229 		   strcmp(token, ">") == 0 ||
1230 		   strcmp(token, "==") == 0 ||
1231 		   strcmp(token, "!=") == 0) {
1232 
1233 		left = malloc_or_die(sizeof(*left));
1234 
1235 		/* copy the top arg to the left */
1236 		*left = *arg;
1237 
1238 		arg->type = PRINT_OP;
1239 		arg->op.op = token;
1240 		arg->op.left = left;
1241 
1242 		set_op_prio(arg);
1243 
1244 		right = malloc_or_die(sizeof(*right));
1245 
1246 		type = read_token_item(&token);
1247 		*tok = token;
1248 
1249 		/* could just be a type pointer */
1250 		if ((strcmp(arg->op.op, "*") == 0) &&
1251 		    type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1252 			if (left->type != PRINT_ATOM)
1253 				die("bad pointer type");
1254 			left->atom.atom = realloc(left->atom.atom,
1255 					    sizeof(left->atom.atom) + 3);
1256 			strcat(left->atom.atom, " *");
1257 			*arg = *left;
1258 			free(arg);
1259 
1260 			return type;
1261 		}
1262 
1263 		type = process_arg_token(event, right, tok, type);
1264 
1265 		arg->op.right = right;
1266 
1267 	} else if (strcmp(token, "[") == 0) {
1268 
1269 		left = malloc_or_die(sizeof(*left));
1270 		*left = *arg;
1271 
1272 		arg->type = PRINT_OP;
1273 		arg->op.op = token;
1274 		arg->op.left = left;
1275 
1276 		arg->op.prio = 0;
1277 		type = process_array(event, arg, tok);
1278 
1279 	} else {
1280 		warning("unknown op '%s'", token);
1281 		event->flags |= EVENT_FL_FAILED;
1282 		/* the arg is now the left side */
1283 		return EVENT_NONE;
1284 	}
1285 
1286 	if (type == EVENT_OP) {
1287 		int prio;
1288 
1289 		/* higher prios need to be closer to the root */
1290 		prio = get_op_prio(*tok);
1291 
1292 		if (prio > arg->op.prio)
1293 			return process_op(event, arg, tok);
1294 
1295 		return process_op(event, right, tok);
1296 	}
1297 
1298 	return type;
1299 }
1300 
1301 static enum event_type
1302 process_entry(struct event *event __unused, struct print_arg *arg,
1303 	      char **tok)
1304 {
1305 	enum event_type type;
1306 	char *field;
1307 	char *token;
1308 
1309 	if (read_expected(EVENT_OP, "->") < 0)
1310 		return EVENT_ERROR;
1311 
1312 	if (read_expect_type(EVENT_ITEM, &token) < 0)
1313 		goto fail;
1314 	field = token;
1315 
1316 	arg->type = PRINT_FIELD;
1317 	arg->field.name = field;
1318 
1319 	if (is_flag_field) {
1320 		arg->field.field = find_any_field(event, arg->field.name);
1321 		arg->field.field->flags |= FIELD_IS_FLAG;
1322 		is_flag_field = 0;
1323 	} else if (is_symbolic_field) {
1324 		arg->field.field = find_any_field(event, arg->field.name);
1325 		arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1326 		is_symbolic_field = 0;
1327 	}
1328 
1329 	type = read_token(&token);
1330 	*tok = token;
1331 
1332 	return type;
1333 
1334 fail:
1335 	free_token(token);
1336 	return EVENT_ERROR;
1337 }
1338 
1339 static char *arg_eval (struct print_arg *arg);
1340 
1341 static long long arg_num_eval(struct print_arg *arg)
1342 {
1343 	long long left, right;
1344 	long long val = 0;
1345 
1346 	switch (arg->type) {
1347 	case PRINT_ATOM:
1348 		val = strtoll(arg->atom.atom, NULL, 0);
1349 		break;
1350 	case PRINT_TYPE:
1351 		val = arg_num_eval(arg->typecast.item);
1352 		break;
1353 	case PRINT_OP:
1354 		switch (arg->op.op[0]) {
1355 		case '|':
1356 			left = arg_num_eval(arg->op.left);
1357 			right = arg_num_eval(arg->op.right);
1358 			if (arg->op.op[1])
1359 				val = left || right;
1360 			else
1361 				val = left | right;
1362 			break;
1363 		case '&':
1364 			left = arg_num_eval(arg->op.left);
1365 			right = arg_num_eval(arg->op.right);
1366 			if (arg->op.op[1])
1367 				val = left && right;
1368 			else
1369 				val = left & right;
1370 			break;
1371 		case '<':
1372 			left = arg_num_eval(arg->op.left);
1373 			right = arg_num_eval(arg->op.right);
1374 			switch (arg->op.op[1]) {
1375 			case 0:
1376 				val = left < right;
1377 				break;
1378 			case '<':
1379 				val = left << right;
1380 				break;
1381 			case '=':
1382 				val = left <= right;
1383 				break;
1384 			default:
1385 				die("unknown op '%s'", arg->op.op);
1386 			}
1387 			break;
1388 		case '>':
1389 			left = arg_num_eval(arg->op.left);
1390 			right = arg_num_eval(arg->op.right);
1391 			switch (arg->op.op[1]) {
1392 			case 0:
1393 				val = left > right;
1394 				break;
1395 			case '>':
1396 				val = left >> right;
1397 				break;
1398 			case '=':
1399 				val = left >= right;
1400 				break;
1401 			default:
1402 				die("unknown op '%s'", arg->op.op);
1403 			}
1404 			break;
1405 		case '=':
1406 			left = arg_num_eval(arg->op.left);
1407 			right = arg_num_eval(arg->op.right);
1408 
1409 			if (arg->op.op[1] != '=')
1410 				die("unknown op '%s'", arg->op.op);
1411 
1412 			val = left == right;
1413 			break;
1414 		case '!':
1415 			left = arg_num_eval(arg->op.left);
1416 			right = arg_num_eval(arg->op.right);
1417 
1418 			switch (arg->op.op[1]) {
1419 			case '=':
1420 				val = left != right;
1421 				break;
1422 			default:
1423 				die("unknown op '%s'", arg->op.op);
1424 			}
1425 			break;
1426 		default:
1427 			die("unknown op '%s'", arg->op.op);
1428 		}
1429 		break;
1430 
1431 	case PRINT_NULL:
1432 	case PRINT_FIELD ... PRINT_SYMBOL:
1433 	case PRINT_STRING:
1434 	default:
1435 		die("invalid eval type %d", arg->type);
1436 
1437 	}
1438 	return val;
1439 }
1440 
1441 static char *arg_eval (struct print_arg *arg)
1442 {
1443 	long long val;
1444 	static char buf[20];
1445 
1446 	switch (arg->type) {
1447 	case PRINT_ATOM:
1448 		return arg->atom.atom;
1449 	case PRINT_TYPE:
1450 		return arg_eval(arg->typecast.item);
1451 	case PRINT_OP:
1452 		val = arg_num_eval(arg);
1453 		sprintf(buf, "%lld", val);
1454 		return buf;
1455 
1456 	case PRINT_NULL:
1457 	case PRINT_FIELD ... PRINT_SYMBOL:
1458 	case PRINT_STRING:
1459 	default:
1460 		die("invalid eval type %d", arg->type);
1461 		break;
1462 	}
1463 
1464 	return NULL;
1465 }
1466 
1467 static enum event_type
1468 process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1469 {
1470 	enum event_type type;
1471 	struct print_arg *arg = NULL;
1472 	struct print_flag_sym *field;
1473 	char *token = NULL;
1474 	char *value;
1475 
1476 	do {
1477 		free_token(token);
1478 		type = read_token_item(&token);
1479 		if (test_type_token(type, token, EVENT_OP, "{"))
1480 			break;
1481 
1482 		arg = malloc_or_die(sizeof(*arg));
1483 
1484 		free_token(token);
1485 		type = process_arg(event, arg, &token);
1486 		if (test_type_token(type, token, EVENT_DELIM, ","))
1487 			goto out_free;
1488 
1489 		field = malloc_or_die(sizeof(*field));
1490 		memset(field, 0, sizeof(*field));
1491 
1492 		value = arg_eval(arg);
1493 		field->value = strdup(value);
1494 
1495 		free_token(token);
1496 		type = process_arg(event, arg, &token);
1497 		if (test_type_token(type, token, EVENT_OP, "}"))
1498 			goto out_free;
1499 
1500 		value = arg_eval(arg);
1501 		field->str = strdup(value);
1502 		free_arg(arg);
1503 		arg = NULL;
1504 
1505 		*list = field;
1506 		list = &field->next;
1507 
1508 		free_token(token);
1509 		type = read_token_item(&token);
1510 	} while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1511 
1512 	*tok = token;
1513 	return type;
1514 
1515 out_free:
1516 	free_arg(arg);
1517 	free_token(token);
1518 
1519 	return EVENT_ERROR;
1520 }
1521 
1522 static enum event_type
1523 process_flags(struct event *event, struct print_arg *arg, char **tok)
1524 {
1525 	struct print_arg *field;
1526 	enum event_type type;
1527 	char *token;
1528 
1529 	memset(arg, 0, sizeof(*arg));
1530 	arg->type = PRINT_FLAGS;
1531 
1532 	if (read_expected_item(EVENT_DELIM, "(") < 0)
1533 		return EVENT_ERROR;
1534 
1535 	field = malloc_or_die(sizeof(*field));
1536 
1537 	type = process_arg(event, field, &token);
1538 	while (type == EVENT_OP)
1539 		type = process_op(event, field, &token);
1540 	if (test_type_token(type, token, EVENT_DELIM, ","))
1541 		goto out_free;
1542 
1543 	arg->flags.field = field;
1544 
1545 	type = read_token_item(&token);
1546 	if (event_item_type(type)) {
1547 		arg->flags.delim = token;
1548 		type = read_token_item(&token);
1549 	}
1550 
1551 	if (test_type_token(type, token, EVENT_DELIM, ","))
1552 		goto out_free;
1553 
1554 	type = process_fields(event, &arg->flags.flags, &token);
1555 	if (test_type_token(type, token, EVENT_DELIM, ")"))
1556 		goto out_free;
1557 
1558 	free_token(token);
1559 	type = read_token_item(tok);
1560 	return type;
1561 
1562 out_free:
1563 	free_token(token);
1564 	return EVENT_ERROR;
1565 }
1566 
1567 static enum event_type
1568 process_symbols(struct event *event, struct print_arg *arg, char **tok)
1569 {
1570 	struct print_arg *field;
1571 	enum event_type type;
1572 	char *token;
1573 
1574 	memset(arg, 0, sizeof(*arg));
1575 	arg->type = PRINT_SYMBOL;
1576 
1577 	if (read_expected_item(EVENT_DELIM, "(") < 0)
1578 		return EVENT_ERROR;
1579 
1580 	field = malloc_or_die(sizeof(*field));
1581 
1582 	type = process_arg(event, field, &token);
1583 	if (test_type_token(type, token, EVENT_DELIM, ","))
1584 		goto out_free;
1585 
1586 	arg->symbol.field = field;
1587 
1588 	type = process_fields(event, &arg->symbol.symbols, &token);
1589 	if (test_type_token(type, token, EVENT_DELIM, ")"))
1590 		goto out_free;
1591 
1592 	free_token(token);
1593 	type = read_token_item(tok);
1594 	return type;
1595 
1596 out_free:
1597 	free_token(token);
1598 	return EVENT_ERROR;
1599 }
1600 
1601 static enum event_type
1602 process_paren(struct event *event, struct print_arg *arg, char **tok)
1603 {
1604 	struct print_arg *item_arg;
1605 	enum event_type type;
1606 	char *token;
1607 
1608 	type = process_arg(event, arg, &token);
1609 
1610 	if (type == EVENT_ERROR)
1611 		return EVENT_ERROR;
1612 
1613 	if (type == EVENT_OP)
1614 		type = process_op(event, arg, &token);
1615 
1616 	if (type == EVENT_ERROR)
1617 		return EVENT_ERROR;
1618 
1619 	if (test_type_token(type, token, EVENT_DELIM, ")")) {
1620 		free_token(token);
1621 		return EVENT_ERROR;
1622 	}
1623 
1624 	free_token(token);
1625 	type = read_token_item(&token);
1626 
1627 	/*
1628 	 * If the next token is an item or another open paren, then
1629 	 * this was a typecast.
1630 	 */
1631 	if (event_item_type(type) ||
1632 	    (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1633 
1634 		/* make this a typecast and contine */
1635 
1636 		/* prevous must be an atom */
1637 		if (arg->type != PRINT_ATOM)
1638 			die("previous needed to be PRINT_ATOM");
1639 
1640 		item_arg = malloc_or_die(sizeof(*item_arg));
1641 
1642 		arg->type = PRINT_TYPE;
1643 		arg->typecast.type = arg->atom.atom;
1644 		arg->typecast.item = item_arg;
1645 		type = process_arg_token(event, item_arg, &token, type);
1646 
1647 	}
1648 
1649 	*tok = token;
1650 	return type;
1651 }
1652 
1653 
1654 static enum event_type
1655 process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1656 {
1657 	enum event_type type;
1658 	char *token;
1659 
1660 	if (read_expected(EVENT_DELIM, "(") < 0)
1661 		return EVENT_ERROR;
1662 
1663 	if (read_expect_type(EVENT_ITEM, &token) < 0)
1664 		goto fail;
1665 
1666 	arg->type = PRINT_STRING;
1667 	arg->string.string = token;
1668 	arg->string.offset = -1;
1669 
1670 	if (read_expected(EVENT_DELIM, ")") < 0)
1671 		return EVENT_ERROR;
1672 
1673 	type = read_token(&token);
1674 	*tok = token;
1675 
1676 	return type;
1677 fail:
1678 	free_token(token);
1679 	return EVENT_ERROR;
1680 }
1681 
1682 enum event_type
1683 process_arg_token(struct event *event, struct print_arg *arg,
1684 		  char **tok, enum event_type type)
1685 {
1686 	char *token;
1687 	char *atom;
1688 
1689 	token = *tok;
1690 
1691 	switch (type) {
1692 	case EVENT_ITEM:
1693 		if (strcmp(token, "REC") == 0) {
1694 			free_token(token);
1695 			type = process_entry(event, arg, &token);
1696 		} else if (strcmp(token, "__print_flags") == 0) {
1697 			free_token(token);
1698 			is_flag_field = 1;
1699 			type = process_flags(event, arg, &token);
1700 		} else if (strcmp(token, "__print_symbolic") == 0) {
1701 			free_token(token);
1702 			is_symbolic_field = 1;
1703 			type = process_symbols(event, arg, &token);
1704 		} else if (strcmp(token, "__get_str") == 0) {
1705 			free_token(token);
1706 			type = process_str(event, arg, &token);
1707 		} else {
1708 			atom = token;
1709 			/* test the next token */
1710 			type = read_token_item(&token);
1711 
1712 			/* atoms can be more than one token long */
1713 			while (type == EVENT_ITEM) {
1714 				atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1715 				strcat(atom, " ");
1716 				strcat(atom, token);
1717 				free_token(token);
1718 				type = read_token_item(&token);
1719 			}
1720 
1721 			/* todo, test for function */
1722 
1723 			arg->type = PRINT_ATOM;
1724 			arg->atom.atom = atom;
1725 		}
1726 		break;
1727 	case EVENT_DQUOTE:
1728 	case EVENT_SQUOTE:
1729 		arg->type = PRINT_ATOM;
1730 		arg->atom.atom = token;
1731 		type = read_token_item(&token);
1732 		break;
1733 	case EVENT_DELIM:
1734 		if (strcmp(token, "(") == 0) {
1735 			free_token(token);
1736 			type = process_paren(event, arg, &token);
1737 			break;
1738 		}
1739 	case EVENT_OP:
1740 		/* handle single ops */
1741 		arg->type = PRINT_OP;
1742 		arg->op.op = token;
1743 		arg->op.left = NULL;
1744 		type = process_op(event, arg, &token);
1745 
1746 		break;
1747 
1748 	case EVENT_ERROR ... EVENT_NEWLINE:
1749 	default:
1750 		die("unexpected type %d", type);
1751 	}
1752 	*tok = token;
1753 
1754 	return type;
1755 }
1756 
1757 static int event_read_print_args(struct event *event, struct print_arg **list)
1758 {
1759 	enum event_type type = EVENT_ERROR;
1760 	struct print_arg *arg;
1761 	char *token;
1762 	int args = 0;
1763 
1764 	do {
1765 		if (type == EVENT_NEWLINE) {
1766 			free_token(token);
1767 			type = read_token_item(&token);
1768 			continue;
1769 		}
1770 
1771 		arg = malloc_or_die(sizeof(*arg));
1772 		memset(arg, 0, sizeof(*arg));
1773 
1774 		type = process_arg(event, arg, &token);
1775 
1776 		if (type == EVENT_ERROR) {
1777 			free_arg(arg);
1778 			return -1;
1779 		}
1780 
1781 		*list = arg;
1782 		args++;
1783 
1784 		if (type == EVENT_OP) {
1785 			type = process_op(event, arg, &token);
1786 			list = &arg->next;
1787 			continue;
1788 		}
1789 
1790 		if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1791 			free_token(token);
1792 			*list = arg;
1793 			list = &arg->next;
1794 			continue;
1795 		}
1796 		break;
1797 	} while (type != EVENT_NONE);
1798 
1799 	if (type != EVENT_NONE)
1800 		free_token(token);
1801 
1802 	return args;
1803 }
1804 
1805 static int event_read_print(struct event *event)
1806 {
1807 	enum event_type type;
1808 	char *token;
1809 	int ret;
1810 
1811 	if (read_expected_item(EVENT_ITEM, "print") < 0)
1812 		return -1;
1813 
1814 	if (read_expected(EVENT_ITEM, "fmt") < 0)
1815 		return -1;
1816 
1817 	if (read_expected(EVENT_OP, ":") < 0)
1818 		return -1;
1819 
1820 	if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1821 		goto fail;
1822 
1823  concat:
1824 	event->print_fmt.format = token;
1825 	event->print_fmt.args = NULL;
1826 
1827 	/* ok to have no arg */
1828 	type = read_token_item(&token);
1829 
1830 	if (type == EVENT_NONE)
1831 		return 0;
1832 
1833 	/* Handle concatination of print lines */
1834 	if (type == EVENT_DQUOTE) {
1835 		char *cat;
1836 
1837 		cat = malloc_or_die(strlen(event->print_fmt.format) +
1838 				    strlen(token) + 1);
1839 		strcpy(cat, event->print_fmt.format);
1840 		strcat(cat, token);
1841 		free_token(token);
1842 		free_token(event->print_fmt.format);
1843 		event->print_fmt.format = NULL;
1844 		token = cat;
1845 		goto concat;
1846 	}
1847 
1848 	if (test_type_token(type, token, EVENT_DELIM, ","))
1849 		goto fail;
1850 
1851 	free_token(token);
1852 
1853 	ret = event_read_print_args(event, &event->print_fmt.args);
1854 	if (ret < 0)
1855 		return -1;
1856 
1857 	return ret;
1858 
1859  fail:
1860 	free_token(token);
1861 	return -1;
1862 }
1863 
1864 static struct format_field *
1865 find_common_field(struct event *event, const char *name)
1866 {
1867 	struct format_field *format;
1868 
1869 	for (format = event->format.common_fields;
1870 	     format; format = format->next) {
1871 		if (strcmp(format->name, name) == 0)
1872 			break;
1873 	}
1874 
1875 	return format;
1876 }
1877 
1878 static struct format_field *
1879 find_field(struct event *event, const char *name)
1880 {
1881 	struct format_field *format;
1882 
1883 	for (format = event->format.fields;
1884 	     format; format = format->next) {
1885 		if (strcmp(format->name, name) == 0)
1886 			break;
1887 	}
1888 
1889 	return format;
1890 }
1891 
1892 static struct format_field *
1893 find_any_field(struct event *event, const char *name)
1894 {
1895 	struct format_field *format;
1896 
1897 	format = find_common_field(event, name);
1898 	if (format)
1899 		return format;
1900 	return find_field(event, name);
1901 }
1902 
1903 unsigned long long read_size(void *ptr, int size)
1904 {
1905 	switch (size) {
1906 	case 1:
1907 		return *(unsigned char *)ptr;
1908 	case 2:
1909 		return data2host2(ptr);
1910 	case 4:
1911 		return data2host4(ptr);
1912 	case 8:
1913 		return data2host8(ptr);
1914 	default:
1915 		/* BUG! */
1916 		return 0;
1917 	}
1918 }
1919 
1920 unsigned long long
1921 raw_field_value(struct event *event, const char *name, void *data)
1922 {
1923 	struct format_field *field;
1924 
1925 	field = find_any_field(event, name);
1926 	if (!field)
1927 		return 0ULL;
1928 
1929 	return read_size(data + field->offset, field->size);
1930 }
1931 
1932 void *raw_field_ptr(struct event *event, const char *name, void *data)
1933 {
1934 	struct format_field *field;
1935 
1936 	field = find_any_field(event, name);
1937 	if (!field)
1938 		return NULL;
1939 
1940 	if (field->flags & FIELD_IS_DYNAMIC) {
1941 		int offset;
1942 
1943 		offset = *(int *)(data + field->offset);
1944 		offset &= 0xffff;
1945 
1946 		return data + offset;
1947 	}
1948 
1949 	return data + field->offset;
1950 }
1951 
1952 static int get_common_info(const char *type, int *offset, int *size)
1953 {
1954 	struct event *event;
1955 	struct format_field *field;
1956 
1957 	/*
1958 	 * All events should have the same common elements.
1959 	 * Pick any event to find where the type is;
1960 	 */
1961 	if (!event_list)
1962 		die("no event_list!");
1963 
1964 	event = event_list;
1965 	field = find_common_field(event, type);
1966 	if (!field)
1967 		die("field '%s' not found", type);
1968 
1969 	*offset = field->offset;
1970 	*size = field->size;
1971 
1972 	return 0;
1973 }
1974 
1975 static int __parse_common(void *data, int *size, int *offset,
1976 			  const char *name)
1977 {
1978 	int ret;
1979 
1980 	if (!*size) {
1981 		ret = get_common_info(name, offset, size);
1982 		if (ret < 0)
1983 			return ret;
1984 	}
1985 	return read_size(data + *offset, *size);
1986 }
1987 
1988 int trace_parse_common_type(void *data)
1989 {
1990 	static int type_offset;
1991 	static int type_size;
1992 
1993 	return __parse_common(data, &type_size, &type_offset,
1994 			      "common_type");
1995 }
1996 
1997 int trace_parse_common_pid(void *data)
1998 {
1999 	static int pid_offset;
2000 	static int pid_size;
2001 
2002 	return __parse_common(data, &pid_size, &pid_offset,
2003 			      "common_pid");
2004 }
2005 
2006 int parse_common_pc(void *data)
2007 {
2008 	static int pc_offset;
2009 	static int pc_size;
2010 
2011 	return __parse_common(data, &pc_size, &pc_offset,
2012 			      "common_preempt_count");
2013 }
2014 
2015 int parse_common_flags(void *data)
2016 {
2017 	static int flags_offset;
2018 	static int flags_size;
2019 
2020 	return __parse_common(data, &flags_size, &flags_offset,
2021 			      "common_flags");
2022 }
2023 
2024 int parse_common_lock_depth(void *data)
2025 {
2026 	static int ld_offset;
2027 	static int ld_size;
2028 	int ret;
2029 
2030 	ret = __parse_common(data, &ld_size, &ld_offset,
2031 			     "common_lock_depth");
2032 	if (ret < 0)
2033 		return -1;
2034 
2035 	return ret;
2036 }
2037 
2038 struct event *trace_find_event(int id)
2039 {
2040 	struct event *event;
2041 
2042 	for (event = event_list; event; event = event->next) {
2043 		if (event->id == id)
2044 			break;
2045 	}
2046 	return event;
2047 }
2048 
2049 struct event *trace_find_next_event(struct event *event)
2050 {
2051 	if (!event)
2052 		return event_list;
2053 
2054 	return event->next;
2055 }
2056 
2057 static unsigned long long eval_num_arg(void *data, int size,
2058 				   struct event *event, struct print_arg *arg)
2059 {
2060 	unsigned long long val = 0;
2061 	unsigned long long left, right;
2062 	struct print_arg *larg;
2063 
2064 	switch (arg->type) {
2065 	case PRINT_NULL:
2066 		/* ?? */
2067 		return 0;
2068 	case PRINT_ATOM:
2069 		return strtoull(arg->atom.atom, NULL, 0);
2070 	case PRINT_FIELD:
2071 		if (!arg->field.field) {
2072 			arg->field.field = find_any_field(event, arg->field.name);
2073 			if (!arg->field.field)
2074 				die("field %s not found", arg->field.name);
2075 		}
2076 		/* must be a number */
2077 		val = read_size(data + arg->field.field->offset,
2078 				arg->field.field->size);
2079 		break;
2080 	case PRINT_FLAGS:
2081 	case PRINT_SYMBOL:
2082 		break;
2083 	case PRINT_TYPE:
2084 		return eval_num_arg(data, size, event, arg->typecast.item);
2085 	case PRINT_STRING:
2086 		return 0;
2087 		break;
2088 	case PRINT_OP:
2089 		if (strcmp(arg->op.op, "[") == 0) {
2090 			/*
2091 			 * Arrays are special, since we don't want
2092 			 * to read the arg as is.
2093 			 */
2094 			if (arg->op.left->type != PRINT_FIELD)
2095 				goto default_op; /* oops, all bets off */
2096 			larg = arg->op.left;
2097 			if (!larg->field.field) {
2098 				larg->field.field =
2099 					find_any_field(event, larg->field.name);
2100 				if (!larg->field.field)
2101 					die("field %s not found", larg->field.name);
2102 			}
2103 			right = eval_num_arg(data, size, event, arg->op.right);
2104 			val = read_size(data + larg->field.field->offset +
2105 					right * long_size, long_size);
2106 			break;
2107 		}
2108  default_op:
2109 		left = eval_num_arg(data, size, event, arg->op.left);
2110 		right = eval_num_arg(data, size, event, arg->op.right);
2111 		switch (arg->op.op[0]) {
2112 		case '|':
2113 			if (arg->op.op[1])
2114 				val = left || right;
2115 			else
2116 				val = left | right;
2117 			break;
2118 		case '&':
2119 			if (arg->op.op[1])
2120 				val = left && right;
2121 			else
2122 				val = left & right;
2123 			break;
2124 		case '<':
2125 			switch (arg->op.op[1]) {
2126 			case 0:
2127 				val = left < right;
2128 				break;
2129 			case '<':
2130 				val = left << right;
2131 				break;
2132 			case '=':
2133 				val = left <= right;
2134 				break;
2135 			default:
2136 				die("unknown op '%s'", arg->op.op);
2137 			}
2138 			break;
2139 		case '>':
2140 			switch (arg->op.op[1]) {
2141 			case 0:
2142 				val = left > right;
2143 				break;
2144 			case '>':
2145 				val = left >> right;
2146 				break;
2147 			case '=':
2148 				val = left >= right;
2149 				break;
2150 			default:
2151 				die("unknown op '%s'", arg->op.op);
2152 			}
2153 			break;
2154 		case '=':
2155 			if (arg->op.op[1] != '=')
2156 				die("unknown op '%s'", arg->op.op);
2157 			val = left == right;
2158 			break;
2159 		case '-':
2160 			val = left - right;
2161 			break;
2162 		case '+':
2163 			val = left + right;
2164 			break;
2165 		default:
2166 			die("unknown op '%s'", arg->op.op);
2167 		}
2168 		break;
2169 	default: /* not sure what to do there */
2170 		return 0;
2171 	}
2172 	return val;
2173 }
2174 
2175 struct flag {
2176 	const char *name;
2177 	unsigned long long value;
2178 };
2179 
2180 static const struct flag flags[] = {
2181 	{ "HI_SOFTIRQ", 0 },
2182 	{ "TIMER_SOFTIRQ", 1 },
2183 	{ "NET_TX_SOFTIRQ", 2 },
2184 	{ "NET_RX_SOFTIRQ", 3 },
2185 	{ "BLOCK_SOFTIRQ", 4 },
2186 	{ "BLOCK_IOPOLL_SOFTIRQ", 5 },
2187 	{ "TASKLET_SOFTIRQ", 6 },
2188 	{ "SCHED_SOFTIRQ", 7 },
2189 	{ "HRTIMER_SOFTIRQ", 8 },
2190 	{ "RCU_SOFTIRQ", 9 },
2191 
2192 	{ "HRTIMER_NORESTART", 0 },
2193 	{ "HRTIMER_RESTART", 1 },
2194 };
2195 
2196 unsigned long long eval_flag(const char *flag)
2197 {
2198 	int i;
2199 
2200 	/*
2201 	 * Some flags in the format files do not get converted.
2202 	 * If the flag is not numeric, see if it is something that
2203 	 * we already know about.
2204 	 */
2205 	if (isdigit(flag[0]))
2206 		return strtoull(flag, NULL, 0);
2207 
2208 	for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2209 		if (strcmp(flags[i].name, flag) == 0)
2210 			return flags[i].value;
2211 
2212 	return 0;
2213 }
2214 
2215 static void print_str_arg(void *data, int size,
2216 			  struct event *event, struct print_arg *arg)
2217 {
2218 	struct print_flag_sym *flag;
2219 	unsigned long long val, fval;
2220 	char *str;
2221 	int print;
2222 
2223 	switch (arg->type) {
2224 	case PRINT_NULL:
2225 		/* ?? */
2226 		return;
2227 	case PRINT_ATOM:
2228 		printf("%s", arg->atom.atom);
2229 		return;
2230 	case PRINT_FIELD:
2231 		if (!arg->field.field) {
2232 			arg->field.field = find_any_field(event, arg->field.name);
2233 			if (!arg->field.field)
2234 				die("field %s not found", arg->field.name);
2235 		}
2236 		str = malloc_or_die(arg->field.field->size + 1);
2237 		memcpy(str, data + arg->field.field->offset,
2238 		       arg->field.field->size);
2239 		str[arg->field.field->size] = 0;
2240 		printf("%s", str);
2241 		free(str);
2242 		break;
2243 	case PRINT_FLAGS:
2244 		val = eval_num_arg(data, size, event, arg->flags.field);
2245 		print = 0;
2246 		for (flag = arg->flags.flags; flag; flag = flag->next) {
2247 			fval = eval_flag(flag->value);
2248 			if (!val && !fval) {
2249 				printf("%s", flag->str);
2250 				break;
2251 			}
2252 			if (fval && (val & fval) == fval) {
2253 				if (print && arg->flags.delim)
2254 					printf("%s", arg->flags.delim);
2255 				printf("%s", flag->str);
2256 				print = 1;
2257 				val &= ~fval;
2258 			}
2259 		}
2260 		break;
2261 	case PRINT_SYMBOL:
2262 		val = eval_num_arg(data, size, event, arg->symbol.field);
2263 		for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2264 			fval = eval_flag(flag->value);
2265 			if (val == fval) {
2266 				printf("%s", flag->str);
2267 				break;
2268 			}
2269 		}
2270 		break;
2271 
2272 	case PRINT_TYPE:
2273 		break;
2274 	case PRINT_STRING: {
2275 		int str_offset;
2276 
2277 		if (arg->string.offset == -1) {
2278 			struct format_field *f;
2279 
2280 			f = find_any_field(event, arg->string.string);
2281 			arg->string.offset = f->offset;
2282 		}
2283 		str_offset = *(int *)(data + arg->string.offset);
2284 		str_offset &= 0xffff;
2285 		printf("%s", ((char *)data) + str_offset);
2286 		break;
2287 	}
2288 	case PRINT_OP:
2289 		/*
2290 		 * The only op for string should be ? :
2291 		 */
2292 		if (arg->op.op[0] != '?')
2293 			return;
2294 		val = eval_num_arg(data, size, event, arg->op.left);
2295 		if (val)
2296 			print_str_arg(data, size, event, arg->op.right->op.left);
2297 		else
2298 			print_str_arg(data, size, event, arg->op.right->op.right);
2299 		break;
2300 	default:
2301 		/* well... */
2302 		break;
2303 	}
2304 }
2305 
2306 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2307 {
2308 	static struct format_field *field, *ip_field;
2309 	struct print_arg *args, *arg, **next;
2310 	unsigned long long ip, val;
2311 	char *ptr;
2312 	void *bptr;
2313 
2314 	if (!field) {
2315 		field = find_field(event, "buf");
2316 		if (!field)
2317 			die("can't find buffer field for binary printk");
2318 		ip_field = find_field(event, "ip");
2319 		if (!ip_field)
2320 			die("can't find ip field for binary printk");
2321 	}
2322 
2323 	ip = read_size(data + ip_field->offset, ip_field->size);
2324 
2325 	/*
2326 	 * The first arg is the IP pointer.
2327 	 */
2328 	args = malloc_or_die(sizeof(*args));
2329 	arg = args;
2330 	arg->next = NULL;
2331 	next = &arg->next;
2332 
2333 	arg->type = PRINT_ATOM;
2334 	arg->atom.atom = malloc_or_die(32);
2335 	sprintf(arg->atom.atom, "%lld", ip);
2336 
2337 	/* skip the first "%pf : " */
2338 	for (ptr = fmt + 6, bptr = data + field->offset;
2339 	     bptr < data + size && *ptr; ptr++) {
2340 		int ls = 0;
2341 
2342 		if (*ptr == '%') {
2343  process_again:
2344 			ptr++;
2345 			switch (*ptr) {
2346 			case '%':
2347 				break;
2348 			case 'l':
2349 				ls++;
2350 				goto process_again;
2351 			case 'L':
2352 				ls = 2;
2353 				goto process_again;
2354 			case '0' ... '9':
2355 				goto process_again;
2356 			case 'p':
2357 				ls = 1;
2358 				/* fall through */
2359 			case 'd':
2360 			case 'u':
2361 			case 'x':
2362 			case 'i':
2363 				/* the pointers are always 4 bytes aligned */
2364 				bptr = (void *)(((unsigned long)bptr + 3) &
2365 						~3);
2366 				switch (ls) {
2367 				case 0:
2368 				case 1:
2369 					ls = long_size;
2370 					break;
2371 				case 2:
2372 					ls = 8;
2373 				default:
2374 					break;
2375 				}
2376 				val = read_size(bptr, ls);
2377 				bptr += ls;
2378 				arg = malloc_or_die(sizeof(*arg));
2379 				arg->next = NULL;
2380 				arg->type = PRINT_ATOM;
2381 				arg->atom.atom = malloc_or_die(32);
2382 				sprintf(arg->atom.atom, "%lld", val);
2383 				*next = arg;
2384 				next = &arg->next;
2385 				break;
2386 			case 's':
2387 				arg = malloc_or_die(sizeof(*arg));
2388 				arg->next = NULL;
2389 				arg->type = PRINT_STRING;
2390 				arg->string.string = strdup(bptr);
2391 				bptr += strlen(bptr) + 1;
2392 				*next = arg;
2393 				next = &arg->next;
2394 			default:
2395 				break;
2396 			}
2397 		}
2398 	}
2399 
2400 	return args;
2401 }
2402 
2403 static void free_args(struct print_arg *args)
2404 {
2405 	struct print_arg *next;
2406 
2407 	while (args) {
2408 		next = args->next;
2409 
2410 		if (args->type == PRINT_ATOM)
2411 			free(args->atom.atom);
2412 		else
2413 			free(args->string.string);
2414 		free(args);
2415 		args = next;
2416 	}
2417 }
2418 
2419 static char *get_bprint_format(void *data, int size __unused, struct event *event)
2420 {
2421 	unsigned long long addr;
2422 	static struct format_field *field;
2423 	struct printk_map *printk;
2424 	char *format;
2425 	char *p;
2426 
2427 	if (!field) {
2428 		field = find_field(event, "fmt");
2429 		if (!field)
2430 			die("can't find format field for binary printk");
2431 		printf("field->offset = %d size=%d\n", field->offset, field->size);
2432 	}
2433 
2434 	addr = read_size(data + field->offset, field->size);
2435 
2436 	printk = find_printk(addr);
2437 	if (!printk) {
2438 		format = malloc_or_die(45);
2439 		sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2440 			addr);
2441 		return format;
2442 	}
2443 
2444 	p = printk->printk;
2445 	/* Remove any quotes. */
2446 	if (*p == '"')
2447 		p++;
2448 	format = malloc_or_die(strlen(p) + 10);
2449 	sprintf(format, "%s : %s", "%pf", p);
2450 	/* remove ending quotes and new line since we will add one too */
2451 	p = format + strlen(format) - 1;
2452 	if (*p == '"')
2453 		*p = 0;
2454 
2455 	p -= 2;
2456 	if (strcmp(p, "\\n") == 0)
2457 		*p = 0;
2458 
2459 	return format;
2460 }
2461 
2462 static void pretty_print(void *data, int size, struct event *event)
2463 {
2464 	struct print_fmt *print_fmt = &event->print_fmt;
2465 	struct print_arg *arg = print_fmt->args;
2466 	struct print_arg *args = NULL;
2467 	const char *ptr = print_fmt->format;
2468 	unsigned long long val;
2469 	struct func_map *func;
2470 	const char *saveptr;
2471 	char *bprint_fmt = NULL;
2472 	char format[32];
2473 	int show_func;
2474 	int len;
2475 	int ls;
2476 
2477 	if (event->flags & EVENT_FL_ISFUNC)
2478 		ptr = " %pF <-- %pF";
2479 
2480 	if (event->flags & EVENT_FL_ISBPRINT) {
2481 		bprint_fmt = get_bprint_format(data, size, event);
2482 		args = make_bprint_args(bprint_fmt, data, size, event);
2483 		arg = args;
2484 		ptr = bprint_fmt;
2485 	}
2486 
2487 	for (; *ptr; ptr++) {
2488 		ls = 0;
2489 		if (*ptr == '\\') {
2490 			ptr++;
2491 			switch (*ptr) {
2492 			case 'n':
2493 				printf("\n");
2494 				break;
2495 			case 't':
2496 				printf("\t");
2497 				break;
2498 			case 'r':
2499 				printf("\r");
2500 				break;
2501 			case '\\':
2502 				printf("\\");
2503 				break;
2504 			default:
2505 				printf("%c", *ptr);
2506 				break;
2507 			}
2508 
2509 		} else if (*ptr == '%') {
2510 			saveptr = ptr;
2511 			show_func = 0;
2512  cont_process:
2513 			ptr++;
2514 			switch (*ptr) {
2515 			case '%':
2516 				printf("%%");
2517 				break;
2518 			case 'l':
2519 				ls++;
2520 				goto cont_process;
2521 			case 'L':
2522 				ls = 2;
2523 				goto cont_process;
2524 			case 'z':
2525 			case 'Z':
2526 			case '0' ... '9':
2527 				goto cont_process;
2528 			case 'p':
2529 				if (long_size == 4)
2530 					ls = 1;
2531 				else
2532 					ls = 2;
2533 
2534 				if (*(ptr+1) == 'F' ||
2535 				    *(ptr+1) == 'f') {
2536 					ptr++;
2537 					show_func = *ptr;
2538 				}
2539 
2540 				/* fall through */
2541 			case 'd':
2542 			case 'i':
2543 			case 'x':
2544 			case 'X':
2545 			case 'u':
2546 				if (!arg)
2547 					die("no argument match");
2548 
2549 				len = ((unsigned long)ptr + 1) -
2550 					(unsigned long)saveptr;
2551 
2552 				/* should never happen */
2553 				if (len > 32)
2554 					die("bad format!");
2555 
2556 				memcpy(format, saveptr, len);
2557 				format[len] = 0;
2558 
2559 				val = eval_num_arg(data, size, event, arg);
2560 				arg = arg->next;
2561 
2562 				if (show_func) {
2563 					func = find_func(val);
2564 					if (func) {
2565 						printf("%s", func->func);
2566 						if (show_func == 'F')
2567 							printf("+0x%llx",
2568 							       val - func->addr);
2569 						break;
2570 					}
2571 				}
2572 				switch (ls) {
2573 				case 0:
2574 					printf(format, (int)val);
2575 					break;
2576 				case 1:
2577 					printf(format, (long)val);
2578 					break;
2579 				case 2:
2580 					printf(format, (long long)val);
2581 					break;
2582 				default:
2583 					die("bad count (%d)", ls);
2584 				}
2585 				break;
2586 			case 's':
2587 				if (!arg)
2588 					die("no matching argument");
2589 
2590 				print_str_arg(data, size, event, arg);
2591 				arg = arg->next;
2592 				break;
2593 			default:
2594 				printf(">%c<", *ptr);
2595 
2596 			}
2597 		} else
2598 			printf("%c", *ptr);
2599 	}
2600 
2601 	if (args) {
2602 		free_args(args);
2603 		free(bprint_fmt);
2604 	}
2605 }
2606 
2607 static inline int log10_cpu(int nb)
2608 {
2609 	if (nb / 100)
2610 		return 3;
2611 	if (nb / 10)
2612 		return 2;
2613 	return 1;
2614 }
2615 
2616 static void print_lat_fmt(void *data, int size __unused)
2617 {
2618 	unsigned int lat_flags;
2619 	unsigned int pc;
2620 	int lock_depth;
2621 	int hardirq;
2622 	int softirq;
2623 
2624 	lat_flags = parse_common_flags(data);
2625 	pc = parse_common_pc(data);
2626 	lock_depth = parse_common_lock_depth(data);
2627 
2628 	hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
2629 	softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
2630 
2631 	printf("%c%c%c",
2632 	       (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
2633 	       (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
2634 	       'X' : '.',
2635 	       (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
2636 	       'N' : '.',
2637 	       (hardirq && softirq) ? 'H' :
2638 	       hardirq ? 'h' : softirq ? 's' : '.');
2639 
2640 	if (pc)
2641 		printf("%x", pc);
2642 	else
2643 		printf(".");
2644 
2645 	if (lock_depth < 0)
2646 		printf(". ");
2647 	else
2648 		printf("%d ", lock_depth);
2649 }
2650 
2651 #define TRACE_GRAPH_INDENT	2
2652 
2653 static struct record *
2654 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2655 		    struct record *next)
2656 {
2657 	struct format_field *field;
2658 	struct event *event;
2659 	unsigned long val;
2660 	int type;
2661 	int pid;
2662 
2663 	type = trace_parse_common_type(next->data);
2664 	event = trace_find_event(type);
2665 	if (!event)
2666 		return NULL;
2667 
2668 	if (!(event->flags & EVENT_FL_ISFUNCRET))
2669 		return NULL;
2670 
2671 	pid = trace_parse_common_pid(next->data);
2672 	field = find_field(event, "func");
2673 	if (!field)
2674 		die("function return does not have field func");
2675 
2676 	val = read_size(next->data + field->offset, field->size);
2677 
2678 	if (cur_pid != pid || cur_func != val)
2679 		return NULL;
2680 
2681 	/* this is a leaf, now advance the iterator */
2682 	return trace_read_data(cpu);
2683 }
2684 
2685 /* Signal a overhead of time execution to the output */
2686 static void print_graph_overhead(unsigned long long duration)
2687 {
2688 	/* Non nested entry or return */
2689 	if (duration == ~0ULL)
2690 		return (void)printf("  ");
2691 
2692 	/* Duration exceeded 100 msecs */
2693 	if (duration > 100000ULL)
2694 		return (void)printf("! ");
2695 
2696 	/* Duration exceeded 10 msecs */
2697 	if (duration > 10000ULL)
2698 		return (void)printf("+ ");
2699 
2700 	printf("  ");
2701 }
2702 
2703 static void print_graph_duration(unsigned long long duration)
2704 {
2705 	unsigned long usecs = duration / 1000;
2706 	unsigned long nsecs_rem = duration % 1000;
2707 	/* log10(ULONG_MAX) + '\0' */
2708 	char msecs_str[21];
2709 	char nsecs_str[5];
2710 	int len;
2711 	int i;
2712 
2713 	sprintf(msecs_str, "%lu", usecs);
2714 
2715 	/* Print msecs */
2716 	len = printf("%lu", usecs);
2717 
2718 	/* Print nsecs (we don't want to exceed 7 numbers) */
2719 	if (len < 7) {
2720 		snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2721 		len += printf(".%s", nsecs_str);
2722 	}
2723 
2724 	printf(" us ");
2725 
2726 	/* Print remaining spaces to fit the row's width */
2727 	for (i = len; i < 7; i++)
2728 		printf(" ");
2729 
2730 	printf("|  ");
2731 }
2732 
2733 static void
2734 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2735 {
2736 	unsigned long long rettime, calltime;
2737 	unsigned long long duration, depth;
2738 	unsigned long long val;
2739 	struct format_field *field;
2740 	struct func_map *func;
2741 	struct event *ret_event;
2742 	int type;
2743 	int i;
2744 
2745 	type = trace_parse_common_type(ret_rec->data);
2746 	ret_event = trace_find_event(type);
2747 
2748 	field = find_field(ret_event, "rettime");
2749 	if (!field)
2750 		die("can't find rettime in return graph");
2751 	rettime = read_size(ret_rec->data + field->offset, field->size);
2752 
2753 	field = find_field(ret_event, "calltime");
2754 	if (!field)
2755 		die("can't find rettime in return graph");
2756 	calltime = read_size(ret_rec->data + field->offset, field->size);
2757 
2758 	duration = rettime - calltime;
2759 
2760 	/* Overhead */
2761 	print_graph_overhead(duration);
2762 
2763 	/* Duration */
2764 	print_graph_duration(duration);
2765 
2766 	field = find_field(event, "depth");
2767 	if (!field)
2768 		die("can't find depth in entry graph");
2769 	depth = read_size(data + field->offset, field->size);
2770 
2771 	/* Function */
2772 	for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2773 		printf(" ");
2774 
2775 	field = find_field(event, "func");
2776 	if (!field)
2777 		die("can't find func in entry graph");
2778 	val = read_size(data + field->offset, field->size);
2779 	func = find_func(val);
2780 
2781 	if (func)
2782 		printf("%s();", func->func);
2783 	else
2784 		printf("%llx();", val);
2785 }
2786 
2787 static void print_graph_nested(struct event *event, void *data)
2788 {
2789 	struct format_field *field;
2790 	unsigned long long depth;
2791 	unsigned long long val;
2792 	struct func_map *func;
2793 	int i;
2794 
2795 	/* No overhead */
2796 	print_graph_overhead(-1);
2797 
2798 	/* No time */
2799 	printf("           |  ");
2800 
2801 	field = find_field(event, "depth");
2802 	if (!field)
2803 		die("can't find depth in entry graph");
2804 	depth = read_size(data + field->offset, field->size);
2805 
2806 	/* Function */
2807 	for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2808 		printf(" ");
2809 
2810 	field = find_field(event, "func");
2811 	if (!field)
2812 		die("can't find func in entry graph");
2813 	val = read_size(data + field->offset, field->size);
2814 	func = find_func(val);
2815 
2816 	if (func)
2817 		printf("%s() {", func->func);
2818 	else
2819 		printf("%llx() {", val);
2820 }
2821 
2822 static void
2823 pretty_print_func_ent(void *data, int size, struct event *event,
2824 		      int cpu, int pid)
2825 {
2826 	struct format_field *field;
2827 	struct record *rec;
2828 	void *copy_data;
2829 	unsigned long val;
2830 
2831 	if (latency_format) {
2832 		print_lat_fmt(data, size);
2833 		printf(" | ");
2834 	}
2835 
2836 	field = find_field(event, "func");
2837 	if (!field)
2838 		die("function entry does not have func field");
2839 
2840 	val = read_size(data + field->offset, field->size);
2841 
2842 	/*
2843 	 * peek_data may unmap the data pointer. Copy it first.
2844 	 */
2845 	copy_data = malloc_or_die(size);
2846 	memcpy(copy_data, data, size);
2847 	data = copy_data;
2848 
2849 	rec = trace_peek_data(cpu);
2850 	if (rec) {
2851 		rec = get_return_for_leaf(cpu, pid, val, rec);
2852 		if (rec) {
2853 			print_graph_entry_leaf(event, data, rec);
2854 			goto out_free;
2855 		}
2856 	}
2857 	print_graph_nested(event, data);
2858 out_free:
2859 	free(data);
2860 }
2861 
2862 static void
2863 pretty_print_func_ret(void *data, int size __unused, struct event *event)
2864 {
2865 	unsigned long long rettime, calltime;
2866 	unsigned long long duration, depth;
2867 	struct format_field *field;
2868 	int i;
2869 
2870 	if (latency_format) {
2871 		print_lat_fmt(data, size);
2872 		printf(" | ");
2873 	}
2874 
2875 	field = find_field(event, "rettime");
2876 	if (!field)
2877 		die("can't find rettime in return graph");
2878 	rettime = read_size(data + field->offset, field->size);
2879 
2880 	field = find_field(event, "calltime");
2881 	if (!field)
2882 		die("can't find calltime in return graph");
2883 	calltime = read_size(data + field->offset, field->size);
2884 
2885 	duration = rettime - calltime;
2886 
2887 	/* Overhead */
2888 	print_graph_overhead(duration);
2889 
2890 	/* Duration */
2891 	print_graph_duration(duration);
2892 
2893 	field = find_field(event, "depth");
2894 	if (!field)
2895 		die("can't find depth in entry graph");
2896 	depth = read_size(data + field->offset, field->size);
2897 
2898 	/* Function */
2899 	for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2900 		printf(" ");
2901 
2902 	printf("}");
2903 }
2904 
2905 static void
2906 pretty_print_func_graph(void *data, int size, struct event *event,
2907 			int cpu, int pid)
2908 {
2909 	if (event->flags & EVENT_FL_ISFUNCENT)
2910 		pretty_print_func_ent(data, size, event, cpu, pid);
2911 	else if (event->flags & EVENT_FL_ISFUNCRET)
2912 		pretty_print_func_ret(data, size, event);
2913 	printf("\n");
2914 }
2915 
2916 void print_trace_event(int cpu, void *data, int size)
2917 {
2918 	struct event *event;
2919 	int type;
2920 	int pid;
2921 
2922 	type = trace_parse_common_type(data);
2923 
2924 	event = trace_find_event(type);
2925 	if (!event) {
2926 		warning("ug! no event found for type %d", type);
2927 		return;
2928 	}
2929 
2930 	pid = trace_parse_common_pid(data);
2931 
2932 	if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
2933 		return pretty_print_func_graph(data, size, event, cpu, pid);
2934 
2935 	if (latency_format)
2936 		print_lat_fmt(data, size);
2937 
2938 	if (event->flags & EVENT_FL_FAILED) {
2939 		printf("EVENT '%s' FAILED TO PARSE\n",
2940 		       event->name);
2941 		return;
2942 	}
2943 
2944 	pretty_print(data, size, event);
2945 }
2946 
2947 static void print_fields(struct print_flag_sym *field)
2948 {
2949 	printf("{ %s, %s }", field->value, field->str);
2950 	if (field->next) {
2951 		printf(", ");
2952 		print_fields(field->next);
2953 	}
2954 }
2955 
2956 static void print_args(struct print_arg *args)
2957 {
2958 	int print_paren = 1;
2959 
2960 	switch (args->type) {
2961 	case PRINT_NULL:
2962 		printf("null");
2963 		break;
2964 	case PRINT_ATOM:
2965 		printf("%s", args->atom.atom);
2966 		break;
2967 	case PRINT_FIELD:
2968 		printf("REC->%s", args->field.name);
2969 		break;
2970 	case PRINT_FLAGS:
2971 		printf("__print_flags(");
2972 		print_args(args->flags.field);
2973 		printf(", %s, ", args->flags.delim);
2974 		print_fields(args->flags.flags);
2975 		printf(")");
2976 		break;
2977 	case PRINT_SYMBOL:
2978 		printf("__print_symbolic(");
2979 		print_args(args->symbol.field);
2980 		printf(", ");
2981 		print_fields(args->symbol.symbols);
2982 		printf(")");
2983 		break;
2984 	case PRINT_STRING:
2985 		printf("__get_str(%s)", args->string.string);
2986 		break;
2987 	case PRINT_TYPE:
2988 		printf("(%s)", args->typecast.type);
2989 		print_args(args->typecast.item);
2990 		break;
2991 	case PRINT_OP:
2992 		if (strcmp(args->op.op, ":") == 0)
2993 			print_paren = 0;
2994 		if (print_paren)
2995 			printf("(");
2996 		print_args(args->op.left);
2997 		printf(" %s ", args->op.op);
2998 		print_args(args->op.right);
2999 		if (print_paren)
3000 			printf(")");
3001 		break;
3002 	default:
3003 		/* we should warn... */
3004 		return;
3005 	}
3006 	if (args->next) {
3007 		printf("\n");
3008 		print_args(args->next);
3009 	}
3010 }
3011 
3012 int parse_ftrace_file(char *buf, unsigned long size)
3013 {
3014 	struct format_field *field;
3015 	struct print_arg *arg, **list;
3016 	struct event *event;
3017 	int ret;
3018 
3019 	init_input_buf(buf, size);
3020 
3021 	event = alloc_event();
3022 	if (!event)
3023 		return -ENOMEM;
3024 
3025 	event->flags |= EVENT_FL_ISFTRACE;
3026 
3027 	event->name = event_read_name();
3028 	if (!event->name)
3029 		die("failed to read ftrace event name");
3030 
3031 	if (strcmp(event->name, "function") == 0)
3032 		event->flags |= EVENT_FL_ISFUNC;
3033 
3034 	else if (strcmp(event->name, "funcgraph_entry") == 0)
3035 		event->flags |= EVENT_FL_ISFUNCENT;
3036 
3037 	else if (strcmp(event->name, "funcgraph_exit") == 0)
3038 		event->flags |= EVENT_FL_ISFUNCRET;
3039 
3040 	else if (strcmp(event->name, "bprint") == 0)
3041 		event->flags |= EVENT_FL_ISBPRINT;
3042 
3043 	event->id = event_read_id();
3044 	if (event->id < 0)
3045 		die("failed to read ftrace event id");
3046 
3047 	add_event(event);
3048 
3049 	ret = event_read_format(event);
3050 	if (ret < 0)
3051 		die("failed to read ftrace event format");
3052 
3053 	ret = event_read_print(event);
3054 	if (ret < 0)
3055 		die("failed to read ftrace event print fmt");
3056 
3057 	/* New ftrace handles args */
3058 	if (ret > 0)
3059 		return 0;
3060 	/*
3061 	 * The arguments for ftrace files are parsed by the fields.
3062 	 * Set up the fields as their arguments.
3063 	 */
3064 	list = &event->print_fmt.args;
3065 	for (field = event->format.fields; field; field = field->next) {
3066 		arg = malloc_or_die(sizeof(*arg));
3067 		memset(arg, 0, sizeof(*arg));
3068 		*list = arg;
3069 		list = &arg->next;
3070 		arg->type = PRINT_FIELD;
3071 		arg->field.name = field->name;
3072 		arg->field.field = field;
3073 	}
3074 	return 0;
3075 }
3076 
3077 int parse_event_file(char *buf, unsigned long size, char *sys)
3078 {
3079 	struct event *event;
3080 	int ret;
3081 
3082 	init_input_buf(buf, size);
3083 
3084 	event = alloc_event();
3085 	if (!event)
3086 		return -ENOMEM;
3087 
3088 	event->name = event_read_name();
3089 	if (!event->name)
3090 		die("failed to read event name");
3091 
3092 	event->id = event_read_id();
3093 	if (event->id < 0)
3094 		die("failed to read event id");
3095 
3096 	ret = event_read_format(event);
3097 	if (ret < 0) {
3098 		warning("failed to read event format for %s", event->name);
3099 		goto event_failed;
3100 	}
3101 
3102 	ret = event_read_print(event);
3103 	if (ret < 0) {
3104 		warning("failed to read event print fmt for %s", event->name);
3105 		goto event_failed;
3106 	}
3107 
3108 	event->system = strdup(sys);
3109 
3110 #define PRINT_ARGS 0
3111 	if (PRINT_ARGS && event->print_fmt.args)
3112 		print_args(event->print_fmt.args);
3113 
3114 	add_event(event);
3115 	return 0;
3116 
3117  event_failed:
3118 	event->flags |= EVENT_FL_FAILED;
3119 	/* still add it even if it failed */
3120 	add_event(event);
3121 	return -1;
3122 }
3123 
3124 void parse_set_info(int nr_cpus, int long_sz)
3125 {
3126 	cpus = nr_cpus;
3127 	long_size = long_sz;
3128 }
3129 
3130 int common_pc(struct scripting_context *context)
3131 {
3132 	return parse_common_pc(context->event_data);
3133 }
3134 
3135 int common_flags(struct scripting_context *context)
3136 {
3137 	return parse_common_flags(context->event_data);
3138 }
3139 
3140 int common_lock_depth(struct scripting_context *context)
3141 {
3142 	return parse_common_lock_depth(context->event_data);
3143 }
3144