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