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