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