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