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