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